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 | aef172f | 2017-12-08 12:51:07 +0100 | [diff] [blame] | 5 | * Copyright (c) 2012-2017 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> |
Markus Armbruster | a8d2532 | 2019-05-23 16:35:08 +0200 | [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" |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 37 | #include "block/qdict.h" |
Paolo Bonzini | 08e2c9f | 2017-08-22 09:23:55 +0200 | [diff] [blame] | 38 | #include "scsi/constants.h" |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 39 | #include "qemu/iov.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 40 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 41 | #include "qemu/option.h" |
Fam Zheng | cea2527 | 2016-09-21 12:27:14 +0800 | [diff] [blame] | 42 | #include "qemu/uuid.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 43 | #include "qapi/error.h" |
Markus Armbruster | 112ed24 | 2018-02-26 17:13:27 -0600 | [diff] [blame] | 44 | #include "qapi/qapi-commands-misc.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 45 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 46 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 47 | #include "crypto/secret.h" |
Paolo Bonzini | e5b5728 | 2017-08-22 07:08:27 +0200 | [diff] [blame] | 48 | #include "scsi/utils.h" |
Fam Zheng | ecc983a | 2018-07-10 14:31:16 +0800 | [diff] [blame] | 49 | #include "trace.h" |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 50 | |
Paolo Bonzini | e5b5728 | 2017-08-22 07:08:27 +0200 | [diff] [blame] | 51 | /* Conflict between scsi/utils.h and libiscsi! :( */ |
| 52 | #define SCSI_XFER_NONE ISCSI_XFER_NONE |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 53 | #include <iscsi/iscsi.h> |
Thomas Huth | df71ca8 | 2019-01-14 14:37:20 +0100 | [diff] [blame] | 54 | #define inline __attribute__((gnu_inline)) /* required for libiscsi v1.9.0 */ |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 55 | #include <iscsi/scsi-lowlevel.h> |
Thomas Huth | df71ca8 | 2019-01-14 14:37:20 +0100 | [diff] [blame] | 56 | #undef inline |
Paolo Bonzini | e5b5728 | 2017-08-22 07:08:27 +0200 | [diff] [blame] | 57 | #undef SCSI_XFER_NONE |
| 58 | QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 59 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 60 | #ifdef __linux__ |
| 61 | #include <scsi/sg.h> |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 62 | #endif |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 63 | |
| 64 | typedef struct IscsiLun { |
| 65 | struct iscsi_context *iscsi; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 66 | AioContext *aio_context; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 67 | int lun; |
Ronnie Sahlberg | dbfff6d | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 68 | enum scsi_inquiry_peripheral_device_type type; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 69 | int block_size; |
Paolo Bonzini | c7b4a95 | 2012-05-26 09:41:13 +0200 | [diff] [blame] | 70 | uint64_t num_blocks; |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 71 | int events; |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 72 | QEMUTimer *nop_timer; |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 73 | QEMUTimer *event_timer; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 74 | QemuMutex mutex; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 75 | struct scsi_inquiry_logical_block_provisioning lbp; |
| 76 | struct scsi_inquiry_block_limits bl; |
Fam Zheng | cc9743c | 2018-06-01 17:26:44 +0800 | [diff] [blame] | 77 | struct scsi_inquiry_device_designator *dd; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 78 | unsigned char *zeroblock; |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 79 | /* The allocmap tracks which clusters (pages) on the iSCSI target are |
| 80 | * allocated and which are not. In case a target returns zeros for |
| 81 | * unallocated pages (iscsilun->lprz) we can directly return zeros instead |
| 82 | * of reading zeros over the wire if a read request falls within an |
| 83 | * unallocated block. As there are 3 possible states we need 2 bitmaps to |
| 84 | * track. allocmap_valid keeps track if QEMU's information about a page is |
| 85 | * valid. allocmap tracks if a page is allocated or not. In case QEMU has no |
| 86 | * valid information about a page the corresponding allocmap entry should be |
| 87 | * switched to unallocated as well to force a new lookup of the allocation |
| 88 | * status as lookups are generally skipped if a page is suspect to be |
| 89 | * allocated. If a iSCSI target is opened with cache.direct = on the |
| 90 | * allocmap_valid does not exist turning all cached information invalid so |
| 91 | * that a fresh lookup is made for any page even if allocmap entry returns |
| 92 | * it's unallocated. */ |
| 93 | unsigned long *allocmap; |
| 94 | unsigned long *allocmap_valid; |
| 95 | long allocmap_size; |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 96 | int cluster_size; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 97 | bool use_16_for_rw; |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 98 | bool write_protected; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 99 | bool lbpme; |
| 100 | bool lbprz; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 101 | bool dpofua; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 102 | bool has_write_same; |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 103 | bool request_timed_out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 104 | } IscsiLun; |
| 105 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 106 | typedef struct IscsiTask { |
| 107 | int status; |
| 108 | int complete; |
| 109 | int retries; |
| 110 | int do_retry; |
| 111 | struct scsi_task *task; |
| 112 | Coroutine *co; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 113 | IscsiLun *iscsilun; |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 114 | QEMUTimer retry_timer; |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 115 | int err_code; |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 116 | char *err_str; |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 117 | } IscsiTask; |
| 118 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 119 | typedef struct IscsiAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 120 | BlockAIOCB common; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 121 | QEMUBH *bh; |
| 122 | IscsiLun *iscsilun; |
| 123 | struct scsi_task *task; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 124 | int status; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 125 | int64_t sector_num; |
| 126 | int nb_sectors; |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 127 | int ret; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 128 | #ifdef __linux__ |
| 129 | sg_io_hdr_t *ioh; |
| 130 | #endif |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 131 | bool cancelled; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 132 | } IscsiAIOCB; |
| 133 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 134 | /* libiscsi uses time_t so its enough to process events every second */ |
| 135 | #define EVENT_INTERVAL 1000 |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 136 | #define NOP_INTERVAL 5000 |
| 137 | #define MAX_NOP_FAILURES 3 |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 138 | #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) |
Peter Lieven | 59dd0a2 | 2015-04-16 16:08:30 +0200 | [diff] [blame] | 139 | 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] | 140 | |
Stefan Weil | 5d831be | 2014-06-13 20:42:57 +0200 | [diff] [blame] | 141 | /* this threshold is a trade-off knob to choose between |
Peter Lieven | 5917af8 | 2014-04-28 17:11:33 +0200 | [diff] [blame] | 142 | * the potential additional overhead of an extra GET_LBA_STATUS request |
| 143 | * vs. unnecessarily reading a lot of zero sectors over the wire. |
| 144 | * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES |
| 145 | * sectors we check the allocation status of the area covered by the |
| 146 | * request first if the allocationmap indicates that the area might be |
| 147 | * unallocated. */ |
| 148 | #define ISCSI_CHECKALLOC_THRES 64 |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 149 | |
Philippe Mathieu-Daudé | d4cef0c | 2019-02-20 01:05:53 +0100 | [diff] [blame] | 150 | #ifdef __linux__ |
| 151 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 152 | static void |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 153 | iscsi_bh_cb(void *p) |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 154 | { |
| 155 | IscsiAIOCB *acb = p; |
| 156 | |
| 157 | qemu_bh_delete(acb->bh); |
| 158 | |
Fam Zheng | 722d933 | 2014-09-11 13:41:15 +0800 | [diff] [blame] | 159 | acb->common.cb(acb->common.opaque, acb->status); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 160 | |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 161 | if (acb->task != NULL) { |
| 162 | scsi_free_scsi_task(acb->task); |
| 163 | acb->task = NULL; |
| 164 | } |
| 165 | |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 166 | qemu_aio_unref(acb); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 169 | static void |
| 170 | iscsi_schedule_bh(IscsiAIOCB *acb) |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 171 | { |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 172 | if (acb->bh) { |
| 173 | return; |
| 174 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 175 | 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] | 176 | qemu_bh_schedule(acb->bh); |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Philippe Mathieu-Daudé | d4cef0c | 2019-02-20 01:05:53 +0100 | [diff] [blame] | 179 | #endif |
| 180 | |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 181 | static void iscsi_co_generic_bh_cb(void *opaque) |
| 182 | { |
| 183 | struct IscsiTask *iTask = opaque; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 184 | |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 185 | iTask->complete = 1; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 186 | aio_co_wake(iTask->co); |
Peter Lieven | 8b9dfe9 | 2013-12-14 17:31:40 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 189 | static void iscsi_retry_timer_expired(void *opaque) |
| 190 | { |
| 191 | struct IscsiTask *iTask = opaque; |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 192 | iTask->complete = 1; |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 193 | if (iTask->co) { |
Paolo Bonzini | 2f47da5 | 2017-02-13 14:52:29 +0100 | [diff] [blame] | 194 | aio_co_wake(iTask->co); |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | static inline unsigned exp_random(double mean) |
| 199 | { |
| 200 | return -mean * log((double)rand() / RAND_MAX); |
| 201 | } |
| 202 | |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 203 | /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in |
| 204 | * libiscsi 1.10.0, together with other constants we need. Use it as |
| 205 | * a hint that we have to define them ourselves if needed, to keep the |
| 206 | * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for |
| 207 | * the test because SCSI_STATUS_* is an enum. |
| 208 | * |
| 209 | * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes |
| 210 | * an enum, check against the LIBISCSI_API_VERSION macro, which was |
| 211 | * introduced in 1.11.0. If it is present, there is no need to define |
| 212 | * anything. |
| 213 | */ |
| 214 | #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ |
| 215 | !defined(LIBISCSI_API_VERSION) |
| 216 | #define SCSI_STATUS_TASK_SET_FULL 0x28 |
| 217 | #define SCSI_STATUS_TIMEOUT 0x0f000002 |
| 218 | #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 |
| 219 | #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 220 | #endif |
| 221 | |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 222 | #ifndef LIBISCSI_API_VERSION |
| 223 | #define LIBISCSI_API_VERSION 20130701 |
| 224 | #endif |
| 225 | |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 226 | static int iscsi_translate_sense(struct scsi_sense *sense) |
| 227 | { |
Fam Zheng | 2875135 | 2017-08-21 22:10:05 +0800 | [diff] [blame] | 228 | return - scsi_sense_to_errno(sense->key, |
| 229 | (sense->ascq & 0xFF00) >> 8, |
| 230 | sense->ascq & 0xFF); |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 233 | /* Called (via iscsi_service) with QemuMutex held. */ |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 234 | static void |
| 235 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, |
| 236 | void *command_data, void *opaque) |
| 237 | { |
| 238 | struct IscsiTask *iTask = opaque; |
| 239 | struct scsi_task *task = command_data; |
| 240 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 241 | iTask->status = status; |
| 242 | iTask->do_retry = 0; |
| 243 | iTask->task = task; |
| 244 | |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 245 | if (status != SCSI_STATUS_GOOD) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 246 | if (iTask->retries++ < ISCSI_CMD_RETRIES) { |
| 247 | if (status == SCSI_STATUS_CHECK_CONDITION |
| 248 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { |
| 249 | error_report("iSCSI CheckCondition: %s", |
| 250 | iscsi_get_error(iscsi)); |
| 251 | iTask->do_retry = 1; |
| 252 | goto out; |
| 253 | } |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 254 | if (status == SCSI_STATUS_BUSY || |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 255 | status == SCSI_STATUS_TIMEOUT || |
| 256 | status == SCSI_STATUS_TASK_SET_FULL) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 257 | unsigned retry_time = |
| 258 | exp_random(iscsi_retry_times[iTask->retries - 1]); |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 259 | if (status == SCSI_STATUS_TIMEOUT) { |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 260 | /* make sure the request is rescheduled AFTER the |
| 261 | * reconnect is initiated */ |
| 262 | retry_time = EVENT_INTERVAL * 2; |
| 263 | iTask->iscsilun->request_timed_out = true; |
| 264 | } |
| 265 | error_report("iSCSI Busy/TaskSetFull/TimeOut" |
| 266 | " (retry #%u in %u ms): %s", |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 267 | iTask->retries, retry_time, |
| 268 | iscsi_get_error(iscsi)); |
| 269 | aio_timer_init(iTask->iscsilun->aio_context, |
| 270 | &iTask->retry_timer, QEMU_CLOCK_REALTIME, |
| 271 | SCALE_MS, iscsi_retry_timer_expired, iTask); |
| 272 | timer_mod(&iTask->retry_timer, |
| 273 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); |
| 274 | iTask->do_retry = 1; |
| 275 | return; |
| 276 | } |
| 277 | } |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 278 | iTask->err_code = iscsi_translate_sense(&task->sense); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 279 | iTask->err_str = g_strdup(iscsi_get_error(iscsi)); |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | out: |
| 283 | if (iTask->co) { |
Paolo Bonzini | fffb6e1 | 2016-10-03 18:14:16 +0200 | [diff] [blame] | 284 | aio_bh_schedule_oneshot(iTask->iscsilun->aio_context, |
| 285 | iscsi_co_generic_bh_cb, iTask); |
Peter Lieven | fcd470d | 2014-06-10 09:52:16 +0200 | [diff] [blame] | 286 | } else { |
| 287 | iTask->complete = 1; |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) |
| 292 | { |
| 293 | *iTask = (struct IscsiTask) { |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 294 | .co = qemu_coroutine_self(), |
Peter Lieven | efc6de0 | 2014-05-30 23:36:47 +0200 | [diff] [blame] | 295 | .iscsilun = iscsilun, |
Peter Lieven | 54a5c1d | 2013-07-19 09:19:40 +0200 | [diff] [blame] | 296 | }; |
| 297 | } |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 298 | |
Philippe Mathieu-Daudé | d4cef0c | 2019-02-20 01:05:53 +0100 | [diff] [blame] | 299 | #ifdef __linux__ |
| 300 | |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 301 | /* Called (via iscsi_service) with QemuMutex held. */ |
Paolo Bonzini | 27cbd82 | 2012-08-18 23:37:31 +0200 | [diff] [blame] | 302 | static void |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 303 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, |
| 304 | void *private_data) |
| 305 | { |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 306 | IscsiAIOCB *acb = private_data; |
| 307 | |
Stefan Hajnoczi | 88e94fd | 2018-02-15 11:15:26 +0000 | [diff] [blame] | 308 | /* If the command callback hasn't been called yet, drop the task */ |
| 309 | if (!acb->bh) { |
| 310 | /* Call iscsi_aio_ioctl_cb() with SCSI_STATUS_CANCELLED */ |
| 311 | iscsi_scsi_cancel_task(iscsi, acb->task); |
| 312 | } |
| 313 | |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 314 | qemu_aio_unref(acb); /* acquired in iscsi_aio_cancel() */ |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 318 | iscsi_aio_cancel(BlockAIOCB *blockacb) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 319 | { |
| 320 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; |
| 321 | IscsiLun *iscsilun = acb->iscsilun; |
| 322 | |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 323 | qemu_mutex_lock(&iscsilun->mutex); |
| 324 | |
| 325 | /* If it was cancelled or completed already, our work is done here */ |
| 326 | if (acb->cancelled || acb->status != -EINPROGRESS) { |
| 327 | qemu_mutex_unlock(&iscsilun->mutex); |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 331 | acb->cancelled = true; |
Paolo Bonzini | b209091 | 2012-08-19 00:12:39 +0200 | [diff] [blame] | 332 | |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 333 | qemu_aio_ref(acb); /* released in iscsi_abort_task_cb() */ |
| 334 | |
| 335 | /* send a task mgmt call to the target to cancel the task on the target */ |
| 336 | if (iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, |
| 337 | iscsi_abort_task_cb, acb) < 0) { |
| 338 | qemu_aio_unref(acb); /* since iscsi_abort_task_cb() won't be called */ |
| 339 | } |
| 340 | |
| 341 | qemu_mutex_unlock(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 342 | } |
| 343 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 344 | static const AIOCBInfo iscsi_aiocb_info = { |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 345 | .aiocb_size = sizeof(IscsiAIOCB), |
Fam Zheng | 722d933 | 2014-09-11 13:41:15 +0800 | [diff] [blame] | 346 | .cancel_async = iscsi_aio_cancel, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 347 | }; |
| 348 | |
Philippe Mathieu-Daudé | d4cef0c | 2019-02-20 01:05:53 +0100 | [diff] [blame] | 349 | #endif |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 350 | |
| 351 | static void iscsi_process_read(void *arg); |
| 352 | static void iscsi_process_write(void *arg); |
| 353 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 354 | /* Called with QemuMutex held. */ |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 355 | static void |
| 356 | iscsi_set_events(IscsiLun *iscsilun) |
| 357 | { |
| 358 | struct iscsi_context *iscsi = iscsilun->iscsi; |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 359 | int ev = iscsi_which_events(iscsi); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 360 | |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 361 | if (ev != iscsilun->events) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 362 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), |
| 363 | false, |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 364 | (ev & POLLIN) ? iscsi_process_read : NULL, |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 365 | (ev & POLLOUT) ? iscsi_process_write : NULL, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 366 | NULL, |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 367 | iscsilun); |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 368 | iscsilun->events = ev; |
Ronnie Sahlberg | c9b9f68 | 2012-05-22 19:56:36 +1000 | [diff] [blame] | 369 | } |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 372 | static void iscsi_timed_check_events(void *opaque) |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 373 | { |
| 374 | IscsiLun *iscsilun = opaque; |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 375 | |
Stefan Hajnoczi | 83d1197 | 2018-02-03 07:16:20 +0100 | [diff] [blame] | 376 | qemu_mutex_lock(&iscsilun->mutex); |
| 377 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 378 | /* check for timed out requests */ |
| 379 | iscsi_service(iscsilun->iscsi, 0); |
| 380 | |
| 381 | if (iscsilun->request_timed_out) { |
| 382 | iscsilun->request_timed_out = false; |
| 383 | iscsi_reconnect(iscsilun->iscsi); |
| 384 | } |
| 385 | |
| 386 | /* newer versions of libiscsi may return zero events. Ensure we are able |
| 387 | * to return to service once this situation changes. */ |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 388 | iscsi_set_events(iscsilun); |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 389 | |
Stefan Hajnoczi | 83d1197 | 2018-02-03 07:16:20 +0100 | [diff] [blame] | 390 | qemu_mutex_unlock(&iscsilun->mutex); |
| 391 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 392 | timer_mod(iscsilun->event_timer, |
| 393 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static void |
| 397 | iscsi_process_read(void *arg) |
| 398 | { |
| 399 | IscsiLun *iscsilun = arg; |
| 400 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 401 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 402 | qemu_mutex_lock(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 403 | iscsi_service(iscsi, POLLIN); |
| 404 | iscsi_set_events(iscsilun); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 405 | qemu_mutex_unlock(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static void |
| 409 | iscsi_process_write(void *arg) |
| 410 | { |
| 411 | IscsiLun *iscsilun = arg; |
| 412 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 413 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 414 | qemu_mutex_lock(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 415 | iscsi_service(iscsi, POLLOUT); |
| 416 | iscsi_set_events(iscsilun); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 417 | qemu_mutex_unlock(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 418 | } |
| 419 | |
Peter Lieven | 0777b5d | 2013-07-11 14:16:25 +0200 | [diff] [blame] | 420 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) |
| 421 | { |
| 422 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; |
| 423 | } |
| 424 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 425 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) |
| 426 | { |
| 427 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; |
| 428 | } |
| 429 | |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 430 | static bool is_byte_request_lun_aligned(int64_t offset, int count, |
| 431 | IscsiLun *iscsilun) |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 432 | { |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 433 | if (offset % iscsilun->block_size || count % iscsilun->block_size) { |
| 434 | error_report("iSCSI misaligned request: " |
| 435 | "iscsilun->block_size %u, offset %" PRIi64 |
| 436 | ", count %d", |
| 437 | iscsilun->block_size, offset, count); |
| 438 | return false; |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 439 | } |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | |
| 443 | static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors, |
| 444 | IscsiLun *iscsilun) |
| 445 | { |
Peter Lieven | 0ead931 | 2016-06-20 11:24:40 +0200 | [diff] [blame] | 446 | assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS); |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 447 | return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS, |
| 448 | nb_sectors << BDRV_SECTOR_BITS, |
| 449 | iscsilun); |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 452 | static void iscsi_allocmap_free(IscsiLun *iscsilun) |
Peter Lieven | a9fe4c9 | 2014-09-30 09:09:12 +0200 | [diff] [blame] | 453 | { |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 454 | g_free(iscsilun->allocmap); |
| 455 | g_free(iscsilun->allocmap_valid); |
| 456 | iscsilun->allocmap = NULL; |
| 457 | iscsilun->allocmap_valid = NULL; |
Peter Lieven | a9fe4c9 | 2014-09-30 09:09:12 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 460 | |
| 461 | static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags) |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 462 | { |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 463 | iscsi_allocmap_free(iscsilun); |
| 464 | |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 465 | assert(iscsilun->cluster_size); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 466 | iscsilun->allocmap_size = |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 467 | DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size, |
| 468 | iscsilun->cluster_size); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 469 | |
| 470 | iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size); |
| 471 | if (!iscsilun->allocmap) { |
| 472 | return -ENOMEM; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 473 | } |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 474 | |
| 475 | if (open_flags & BDRV_O_NOCACHE) { |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 476 | /* when cache.direct = on all allocmap entries are |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 477 | * treated as invalid to force a relookup of the block |
| 478 | * status on every read request */ |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size); |
| 483 | if (!iscsilun->allocmap_valid) { |
| 484 | /* if we are under memory pressure free the allocmap as well */ |
| 485 | iscsi_allocmap_free(iscsilun); |
| 486 | return -ENOMEM; |
| 487 | } |
| 488 | |
| 489 | return 0; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 492 | static void |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 493 | iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset, |
| 494 | int64_t bytes, bool allocated, bool valid) |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 495 | { |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 496 | int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk; |
| 497 | |
| 498 | if (iscsilun->allocmap == NULL) { |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 499 | return; |
| 500 | } |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 501 | /* expand to entirely contain all affected clusters */ |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 502 | assert(iscsilun->cluster_size); |
| 503 | cl_num_expanded = offset / iscsilun->cluster_size; |
| 504 | nb_cls_expanded = DIV_ROUND_UP(offset + bytes, |
| 505 | iscsilun->cluster_size) - cl_num_expanded; |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 506 | /* shrink to touch only completely contained clusters */ |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 507 | cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size); |
| 508 | nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk; |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 509 | if (allocated) { |
| 510 | bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded); |
| 511 | } else { |
Peter Lieven | 1da45e0 | 2017-01-16 16:17:12 +0100 | [diff] [blame] | 512 | if (nb_cls_shrunk > 0) { |
| 513 | bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk); |
| 514 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 515 | } |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 516 | |
| 517 | if (iscsilun->allocmap_valid == NULL) { |
| 518 | return; |
| 519 | } |
| 520 | if (valid) { |
Peter Lieven | 1da45e0 | 2017-01-16 16:17:12 +0100 | [diff] [blame] | 521 | if (nb_cls_shrunk > 0) { |
| 522 | bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk); |
| 523 | } |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 524 | } else { |
| 525 | bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded, |
| 526 | nb_cls_expanded); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | static void |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 531 | iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset, |
| 532 | int64_t bytes) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 533 | { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 534 | iscsi_allocmap_update(iscsilun, offset, bytes, true, true); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 538 | iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset, |
| 539 | int64_t bytes) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 540 | { |
| 541 | /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update |
| 542 | * is ignored, so this will in effect be an iscsi_allocmap_set_invalid. |
| 543 | */ |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 544 | iscsi_allocmap_update(iscsilun, offset, bytes, false, true); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 547 | static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset, |
| 548 | int64_t bytes) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 549 | { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 550 | iscsi_allocmap_update(iscsilun, offset, bytes, false, false); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void iscsi_allocmap_invalidate(IscsiLun *iscsilun) |
| 554 | { |
| 555 | if (iscsilun->allocmap) { |
| 556 | bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size); |
| 557 | } |
| 558 | if (iscsilun->allocmap_valid) { |
| 559 | bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static inline bool |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 564 | iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset, |
| 565 | int64_t bytes) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 566 | { |
| 567 | unsigned long size; |
| 568 | if (iscsilun->allocmap == NULL) { |
| 569 | return true; |
| 570 | } |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 571 | assert(iscsilun->cluster_size); |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 572 | size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 573 | return !(find_next_bit(iscsilun->allocmap, size, |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 574 | offset / iscsilun->cluster_size) == size); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun, |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 578 | int64_t offset, int64_t bytes) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 579 | { |
| 580 | unsigned long size; |
| 581 | if (iscsilun->allocmap_valid == NULL) { |
| 582 | return false; |
| 583 | } |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 584 | assert(iscsilun->cluster_size); |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 585 | size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 586 | return (find_next_zero_bit(iscsilun->allocmap_valid, size, |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 587 | offset / iscsilun->cluster_size) == size); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 590 | static void coroutine_fn iscsi_co_wait_for_task(IscsiTask *iTask, |
| 591 | IscsiLun *iscsilun) |
| 592 | { |
| 593 | while (!iTask->complete) { |
| 594 | iscsi_set_events(iscsilun); |
| 595 | qemu_mutex_unlock(&iscsilun->mutex); |
| 596 | qemu_coroutine_yield(); |
| 597 | qemu_mutex_lock(&iscsilun->mutex); |
| 598 | } |
| 599 | } |
| 600 | |
Kevin Wolf | 9f0eb9e | 2016-03-10 13:55:50 +0100 | [diff] [blame] | 601 | static int coroutine_fn |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 602 | iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
| 603 | QEMUIOVector *iov, int flags) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 604 | { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 605 | IscsiLun *iscsilun = bs->opaque; |
| 606 | struct IscsiTask iTask; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 607 | uint64_t lba; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 608 | uint32_t num_sectors; |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 609 | bool fua = flags & BDRV_REQ_FUA; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 610 | int r = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 611 | |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 612 | if (fua) { |
| 613 | assert(iscsilun->dpofua); |
| 614 | } |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 615 | if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 616 | return -EINVAL; |
| 617 | } |
| 618 | |
Eric Blake | 6bd01f1 | 2016-07-15 12:32:04 -0600 | [diff] [blame] | 619 | if (bs->bl.max_transfer) { |
| 620 | assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); |
Peter Lieven | dc9e716 | 2014-10-27 10:18:48 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 623 | lba = sector_qemu2lun(sector_num, iscsilun); |
| 624 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 625 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 626 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 627 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 628 | if (iscsilun->use_16_for_rw) { |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 629 | #if LIBISCSI_API_VERSION >= (20160603) |
| 630 | iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 631 | NULL, num_sectors * iscsilun->block_size, |
| 632 | iscsilun->block_size, 0, 0, fua, 0, 0, |
| 633 | iscsi_co_generic_cb, &iTask, |
| 634 | (struct scsi_iovec *)iov->iov, iov->niov); |
| 635 | } else { |
| 636 | iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 637 | NULL, num_sectors * iscsilun->block_size, |
| 638 | iscsilun->block_size, 0, 0, fua, 0, 0, |
| 639 | iscsi_co_generic_cb, &iTask, |
| 640 | (struct scsi_iovec *)iov->iov, iov->niov); |
| 641 | } |
| 642 | #else |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 643 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, |
Peter Lieven | 8c215a9 | 2014-06-18 18:40:22 +0200 | [diff] [blame] | 644 | NULL, num_sectors * iscsilun->block_size, |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 645 | iscsilun->block_size, 0, 0, fua, 0, 0, |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 646 | iscsi_co_generic_cb, &iTask); |
| 647 | } else { |
| 648 | iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, |
Peter Lieven | 8c215a9 | 2014-06-18 18:40:22 +0200 | [diff] [blame] | 649 | NULL, num_sectors * iscsilun->block_size, |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 650 | iscsilun->block_size, 0, 0, fua, 0, 0, |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 651 | iscsi_co_generic_cb, &iTask); |
| 652 | } |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 653 | #endif |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 654 | if (iTask.task == NULL) { |
Paolo Bonzini | f6eb0b3 | 2017-03-03 16:23:36 +0100 | [diff] [blame] | 655 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 656 | return -ENOMEM; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 657 | } |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 658 | #if LIBISCSI_API_VERSION < (20160603) |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 659 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, |
| 660 | iov->niov); |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 661 | #endif |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 662 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 663 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 664 | if (iTask.task != NULL) { |
| 665 | scsi_free_scsi_task(iTask.task); |
| 666 | iTask.task = NULL; |
| 667 | } |
| 668 | |
| 669 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 670 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 671 | goto retry; |
| 672 | } |
| 673 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 674 | if (iTask.status != SCSI_STATUS_GOOD) { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 675 | iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 676 | nb_sectors * BDRV_SECTOR_SIZE); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 677 | error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba, |
| 678 | iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 679 | r = iTask.err_code; |
| 680 | goto out_unlock; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 681 | } |
Peter Lieven | 7371d56 | 2012-12-03 20:35:15 +0100 | [diff] [blame] | 682 | |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 683 | iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 684 | nb_sectors * BDRV_SECTOR_SIZE); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 685 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 686 | out_unlock: |
| 687 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 688 | g_free(iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 689 | return r; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 690 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 691 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 692 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 693 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 694 | static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs, |
| 695 | bool want_zero, int64_t offset, |
| 696 | int64_t bytes, int64_t *pnum, |
| 697 | int64_t *map, |
| 698 | BlockDriverState **file) |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 699 | { |
| 700 | IscsiLun *iscsilun = bs->opaque; |
| 701 | struct scsi_get_lba_status *lbas = NULL; |
| 702 | struct scsi_lba_status_descriptor *lbasd = NULL; |
| 703 | struct IscsiTask iTask; |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 704 | uint64_t lba; |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 705 | int ret; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 706 | |
Peter Lieven | 79f9c75 | 2018-01-08 16:27:27 +0100 | [diff] [blame] | 707 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 708 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 709 | assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size)); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 710 | |
| 711 | /* default to all sectors allocated */ |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 712 | ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; |
| 713 | if (map) { |
| 714 | *map = offset; |
| 715 | } |
| 716 | *pnum = bytes; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 717 | |
| 718 | /* LUN does not support logical block provisioning */ |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 719 | if (!iscsilun->lbpme) { |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 720 | goto out; |
| 721 | } |
| 722 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 723 | lba = offset / iscsilun->block_size; |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 724 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 725 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 726 | retry: |
| 727 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 728 | lba, 8 + 16, iscsi_co_generic_cb, |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 729 | &iTask) == NULL) { |
| 730 | ret = -ENOMEM; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 731 | goto out_unlock; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 732 | } |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 733 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 734 | |
| 735 | if (iTask.do_retry) { |
| 736 | if (iTask.task != NULL) { |
| 737 | scsi_free_scsi_task(iTask.task); |
| 738 | iTask.task = NULL; |
| 739 | } |
| 740 | iTask.complete = 0; |
| 741 | goto retry; |
| 742 | } |
| 743 | |
| 744 | if (iTask.status != SCSI_STATUS_GOOD) { |
| 745 | /* in case the get_lba_status_callout fails (i.e. |
| 746 | * because the device is busy or the cmd is not |
| 747 | * supported) we pretend all blocks are allocated |
| 748 | * for backwards compatibility */ |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 749 | error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s", |
| 750 | lba, iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 751 | goto out_unlock; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | lbas = scsi_datain_unmarshall(iTask.task); |
| 755 | if (lbas == NULL) { |
| 756 | ret = -EIO; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 757 | goto out_unlock; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | lbasd = &lbas->descriptors[0]; |
| 761 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 762 | if (lba != lbasd->lba) { |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 763 | ret = -EIO; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 764 | goto out_unlock; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Eric Blake | 8ee1cef | 2018-05-08 16:27:18 -0500 | [diff] [blame] | 767 | *pnum = (int64_t) lbasd->num_blocks * iscsilun->block_size; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 768 | |
| 769 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || |
| 770 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { |
| 771 | ret &= ~BDRV_BLOCK_DATA; |
| 772 | if (iscsilun->lbprz) { |
| 773 | ret |= BDRV_BLOCK_ZERO; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (ret & BDRV_BLOCK_ZERO) { |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 778 | iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 779 | } else { |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 780 | iscsi_allocmap_set_allocated(iscsilun, offset, *pnum); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 783 | if (*pnum > bytes) { |
| 784 | *pnum = bytes; |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 785 | } |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 786 | out_unlock: |
| 787 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 788 | g_free(iTask.err_str); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 789 | out: |
| 790 | if (iTask.task != NULL) { |
| 791 | scsi_free_scsi_task(iTask.task); |
| 792 | } |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 793 | if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) { |
Fam Zheng | 3399833 | 2016-01-26 11:58:52 +0800 | [diff] [blame] | 794 | *file = bs; |
| 795 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 796 | return ret; |
| 797 | } |
| 798 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 799 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, |
| 800 | int64_t sector_num, int nb_sectors, |
| 801 | QEMUIOVector *iov) |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 802 | { |
| 803 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 804 | struct IscsiTask iTask; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 805 | uint64_t lba; |
| 806 | uint32_t num_sectors; |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 807 | int r = 0; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 808 | |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 809 | if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 810 | return -EINVAL; |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 811 | } |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 812 | |
Eric Blake | 6bd01f1 | 2016-07-15 12:32:04 -0600 | [diff] [blame] | 813 | if (bs->bl.max_transfer) { |
| 814 | assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); |
Peter Lieven | dc9e716 | 2014-10-27 10:18:48 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 817 | /* if cache.direct is off and we have a valid entry in our allocation map |
| 818 | * we can skip checking the block status and directly return zeroes if |
| 819 | * the request falls within an unallocated area */ |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 820 | if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 821 | nb_sectors * BDRV_SECTOR_SIZE) && |
| 822 | !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 823 | nb_sectors * BDRV_SECTOR_SIZE)) { |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 824 | qemu_iovec_memset(iov, 0, 0x00, iov->size); |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | if (nb_sectors >= ISCSI_CHECKALLOC_THRES && |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 829 | !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 830 | nb_sectors * BDRV_SECTOR_SIZE) && |
| 831 | !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, |
| 832 | nb_sectors * BDRV_SECTOR_SIZE)) { |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 833 | int64_t pnum; |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 834 | /* check the block status from the beginning of the cluster |
| 835 | * containing the start sector */ |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 836 | int64_t head; |
| 837 | int ret; |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 838 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 839 | assert(iscsilun->cluster_size); |
| 840 | head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size; |
| 841 | ret = iscsi_co_block_status(bs, true, |
| 842 | sector_num * BDRV_SECTOR_SIZE - head, |
| 843 | BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 844 | if (ret < 0) { |
| 845 | return ret; |
| 846 | } |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 847 | /* if the whole request falls into an unallocated area we can avoid |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 848 | * reading and directly return zeroes instead */ |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 849 | if (ret & BDRV_BLOCK_ZERO && |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 850 | pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) { |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 851 | qemu_iovec_memset(iov, 0, 0x00, iov->size); |
| 852 | return 0; |
| 853 | } |
| 854 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 855 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 856 | lba = sector_qemu2lun(sector_num, iscsilun); |
| 857 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 858 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 859 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 860 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 861 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 862 | if (iscsilun->use_16_for_rw) { |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 863 | #if LIBISCSI_API_VERSION >= (20160603) |
| 864 | iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 865 | num_sectors * iscsilun->block_size, |
| 866 | iscsilun->block_size, 0, 0, 0, 0, 0, |
| 867 | iscsi_co_generic_cb, &iTask, |
| 868 | (struct scsi_iovec *)iov->iov, iov->niov); |
| 869 | } else { |
| 870 | iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 871 | num_sectors * iscsilun->block_size, |
| 872 | iscsilun->block_size, |
| 873 | 0, 0, 0, 0, 0, |
| 874 | iscsi_co_generic_cb, &iTask, |
| 875 | (struct scsi_iovec *)iov->iov, iov->niov); |
| 876 | } |
| 877 | #else |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 878 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 879 | num_sectors * iscsilun->block_size, |
| 880 | iscsilun->block_size, 0, 0, 0, 0, 0, |
| 881 | iscsi_co_generic_cb, &iTask); |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 882 | } else { |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 883 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 884 | num_sectors * iscsilun->block_size, |
Stefan Weil | 219c252 | 2013-12-17 08:57:10 +0100 | [diff] [blame] | 885 | iscsilun->block_size, |
Stefan Weil | 219c252 | 2013-12-17 08:57:10 +0100 | [diff] [blame] | 886 | 0, 0, 0, 0, 0, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 887 | iscsi_co_generic_cb, &iTask); |
Ronnie Sahlberg | f4dfa67 | 2012-05-22 20:10:05 +1000 | [diff] [blame] | 888 | } |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 889 | #endif |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 890 | if (iTask.task == NULL) { |
Paolo Bonzini | f6eb0b3 | 2017-03-03 16:23:36 +0100 | [diff] [blame] | 891 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 892 | return -ENOMEM; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 893 | } |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 894 | #if LIBISCSI_API_VERSION < (20160603) |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 895 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 896 | #endif |
Peter Lieven | 91bea4e | 2013-07-11 14:16:27 +0200 | [diff] [blame] | 897 | |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 898 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 899 | if (iTask.task != NULL) { |
| 900 | scsi_free_scsi_task(iTask.task); |
| 901 | iTask.task = NULL; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 902 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 903 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 904 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 905 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 906 | goto retry; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 907 | } |
| 908 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 909 | if (iTask.status != SCSI_STATUS_GOOD) { |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 910 | error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s", |
| 911 | lba, iTask.err_str); |
| 912 | r = iTask.err_code; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 913 | } |
| 914 | |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 915 | qemu_mutex_unlock(&iscsilun->mutex); |
| 916 | g_free(iTask.err_str); |
| 917 | return r; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 920 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 921 | { |
| 922 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 923 | struct IscsiTask iTask; |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 924 | int r = 0; |
Peter Lieven | 1dde716 | 2013-02-21 16:15:54 +0100 | [diff] [blame] | 925 | |
Peter Lieven | 73b5394 | 2015-04-16 16:08:29 +0200 | [diff] [blame] | 926 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 927 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 928 | retry: |
| 929 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, |
| 930 | 0, iscsi_co_generic_cb, &iTask) == NULL) { |
Paolo Bonzini | f6eb0b3 | 2017-03-03 16:23:36 +0100 | [diff] [blame] | 931 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 932 | return -ENOMEM; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 933 | } |
| 934 | |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 935 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 936 | |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 937 | if (iTask.task != NULL) { |
| 938 | scsi_free_scsi_task(iTask.task); |
| 939 | iTask.task = NULL; |
| 940 | } |
| 941 | |
| 942 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 943 | iTask.complete = 0; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 944 | goto retry; |
| 945 | } |
| 946 | |
| 947 | if (iTask.status != SCSI_STATUS_GOOD) { |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 948 | error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str); |
| 949 | r = iTask.err_code; |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 952 | qemu_mutex_unlock(&iscsilun->mutex); |
| 953 | g_free(iTask.err_str); |
| 954 | return r; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 955 | } |
| 956 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 957 | #ifdef __linux__ |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 958 | /* Called (via iscsi_service) with QemuMutex held. */ |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 959 | static void |
| 960 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, |
| 961 | void *command_data, void *opaque) |
| 962 | { |
| 963 | IscsiAIOCB *acb = opaque; |
| 964 | |
Stefan Hajnoczi | 88e94fd | 2018-02-15 11:15:26 +0000 | [diff] [blame] | 965 | if (status == SCSI_STATUS_CANCELLED) { |
| 966 | if (!acb->bh) { |
| 967 | acb->status = -ECANCELED; |
| 968 | iscsi_schedule_bh(acb); |
| 969 | } |
| 970 | return; |
| 971 | } |
| 972 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 973 | acb->status = 0; |
| 974 | if (status < 0) { |
| 975 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", |
| 976 | iscsi_get_error(iscsi)); |
Fam Zheng | e01dd3d | 2015-11-05 13:00:09 +0800 | [diff] [blame] | 977 | acb->status = iscsi_translate_sense(&acb->task->sense); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | acb->ioh->driver_status = 0; |
| 981 | acb->ioh->host_status = 0; |
| 982 | acb->ioh->resid = 0; |
Vadim Rozenfeld | 644c6869d | 2016-05-13 13:03:22 +0200 | [diff] [blame] | 983 | acb->ioh->status = status; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 984 | |
| 985 | #define SG_ERR_DRIVER_SENSE 0x08 |
| 986 | |
| 987 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { |
| 988 | int ss; |
| 989 | |
| 990 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; |
| 991 | |
| 992 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; |
| 993 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? |
| 994 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; |
| 995 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); |
| 996 | } |
| 997 | |
Paolo Bonzini | cfb3f50 | 2012-08-18 23:38:03 +0200 | [diff] [blame] | 998 | iscsi_schedule_bh(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 999 | } |
| 1000 | |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 1001 | static void iscsi_ioctl_bh_completion(void *opaque) |
| 1002 | { |
| 1003 | IscsiAIOCB *acb = opaque; |
| 1004 | |
| 1005 | qemu_bh_delete(acb->bh); |
| 1006 | acb->common.cb(acb->common.opaque, acb->ret); |
| 1007 | qemu_aio_unref(acb); |
| 1008 | } |
| 1009 | |
| 1010 | static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) |
| 1011 | { |
| 1012 | BlockDriverState *bs = acb->common.bs; |
| 1013 | IscsiLun *iscsilun = bs->opaque; |
| 1014 | int ret = 0; |
| 1015 | |
| 1016 | switch (req) { |
| 1017 | case SG_GET_VERSION_NUM: |
| 1018 | *(int *)buf = 30000; |
| 1019 | break; |
| 1020 | case SG_GET_SCSI_ID: |
| 1021 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; |
| 1022 | break; |
| 1023 | default: |
| 1024 | ret = -EINVAL; |
| 1025 | } |
| 1026 | assert(!acb->bh); |
| 1027 | acb->bh = aio_bh_new(bdrv_get_aio_context(bs), |
| 1028 | iscsi_ioctl_bh_completion, acb); |
| 1029 | acb->ret = ret; |
| 1030 | qemu_bh_schedule(acb->bh); |
| 1031 | } |
| 1032 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 1033 | static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1034 | unsigned long int req, void *buf, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 1035 | BlockCompletionFunc *cb, void *opaque) |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1036 | { |
| 1037 | IscsiLun *iscsilun = bs->opaque; |
| 1038 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 1039 | struct iscsi_data data; |
| 1040 | IscsiAIOCB *acb; |
| 1041 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 1042 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1043 | |
| 1044 | acb->iscsilun = iscsilun; |
Paolo Bonzini | 1bd075f | 2012-08-18 23:35:49 +0200 | [diff] [blame] | 1045 | acb->bh = NULL; |
| 1046 | acb->status = -EINPROGRESS; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1047 | acb->ioh = buf; |
Stefan Hajnoczi | c100448 | 2018-02-03 07:16:21 +0100 | [diff] [blame] | 1048 | acb->cancelled = false; |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1049 | |
Fam Zheng | 4bb17ab | 2015-11-09 18:16:49 +0800 | [diff] [blame] | 1050 | if (req != SG_IO) { |
| 1051 | iscsi_ioctl_handle_emulated(acb, req, buf); |
| 1052 | return &acb->common; |
| 1053 | } |
| 1054 | |
Peter Lieven | a6b3167 | 2016-05-24 10:59:28 +0200 | [diff] [blame] | 1055 | if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) { |
| 1056 | error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)", |
| 1057 | acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE); |
| 1058 | qemu_aio_unref(acb); |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1062 | acb->task = malloc(sizeof(struct scsi_task)); |
| 1063 | if (acb->task == NULL) { |
| 1064 | error_report("iSCSI: Failed to allocate task for scsi command. %s", |
| 1065 | iscsi_get_error(iscsi)); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 1066 | qemu_aio_unref(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1067 | return NULL; |
| 1068 | } |
| 1069 | memset(acb->task, 0, sizeof(struct scsi_task)); |
| 1070 | |
| 1071 | switch (acb->ioh->dxfer_direction) { |
| 1072 | case SG_DXFER_TO_DEV: |
| 1073 | acb->task->xfer_dir = SCSI_XFER_WRITE; |
| 1074 | break; |
| 1075 | case SG_DXFER_FROM_DEV: |
| 1076 | acb->task->xfer_dir = SCSI_XFER_READ; |
| 1077 | break; |
| 1078 | default: |
| 1079 | acb->task->xfer_dir = SCSI_XFER_NONE; |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | acb->task->cdb_size = acb->ioh->cmd_len; |
| 1084 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); |
| 1085 | acb->task->expxferlen = acb->ioh->dxfer_len; |
| 1086 | |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1087 | data.size = 0; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1088 | qemu_mutex_lock(&iscsilun->mutex); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1089 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1090 | if (acb->ioh->iovec_count == 0) { |
| 1091 | data.data = acb->ioh->dxferp; |
| 1092 | data.size = acb->ioh->dxfer_len; |
| 1093 | } else { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1094 | scsi_task_set_iov_out(acb->task, |
| 1095 | (struct scsi_iovec *) acb->ioh->dxferp, |
| 1096 | acb->ioh->iovec_count); |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1097 | } |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1098 | } |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1099 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1100 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, |
| 1101 | iscsi_aio_ioctl_cb, |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1102 | (data.size > 0) ? &data : NULL, |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1103 | acb) != 0) { |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1104 | qemu_mutex_unlock(&iscsilun->mutex); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1105 | scsi_free_scsi_task(acb->task); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 1106 | qemu_aio_unref(acb); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1107 | return NULL; |
| 1108 | } |
| 1109 | |
| 1110 | /* tell libiscsi to read straight into the buffer we got from ioctl */ |
| 1111 | if (acb->task->xfer_dir == SCSI_XFER_READ) { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1112 | if (acb->ioh->iovec_count == 0) { |
| 1113 | scsi_task_add_data_in_buffer(acb->task, |
| 1114 | acb->ioh->dxfer_len, |
| 1115 | acb->ioh->dxferp); |
| 1116 | } else { |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1117 | scsi_task_set_iov_in(acb->task, |
| 1118 | (struct scsi_iovec *) acb->ioh->dxferp, |
| 1119 | acb->ioh->iovec_count); |
Ronnie Sahlberg | 0a53f01 | 2013-06-23 08:07:08 -0700 | [diff] [blame] | 1120 | } |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | iscsi_set_events(iscsilun); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1124 | qemu_mutex_unlock(&iscsilun->mutex); |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1125 | |
| 1126 | return &acb->common; |
| 1127 | } |
| 1128 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 1129 | #endif |
| 1130 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1131 | static int64_t |
| 1132 | iscsi_getlength(BlockDriverState *bs) |
| 1133 | { |
| 1134 | IscsiLun *iscsilun = bs->opaque; |
| 1135 | int64_t len; |
| 1136 | |
| 1137 | len = iscsilun->num_blocks; |
| 1138 | len *= iscsilun->block_size; |
| 1139 | |
| 1140 | return len; |
| 1141 | } |
| 1142 | |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1143 | static int |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1144 | coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1145 | { |
| 1146 | IscsiLun *iscsilun = bs->opaque; |
| 1147 | struct IscsiTask iTask; |
| 1148 | struct unmap_list list; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1149 | int r = 0; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1150 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1151 | if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) { |
Eric Blake | 49228d1 | 2016-11-17 14:13:57 -0600 | [diff] [blame] | 1152 | return -ENOTSUP; |
| 1153 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1154 | |
| 1155 | if (!iscsilun->lbp.lbpu) { |
| 1156 | /* UNMAP is not supported by the target */ |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Eric Blake | 97c7e85 | 2016-07-15 17:23:01 -0600 | [diff] [blame] | 1160 | list.lba = offset / iscsilun->block_size; |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1161 | list.num = bytes / iscsilun->block_size; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1162 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1163 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1164 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1165 | retry: |
| 1166 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, |
Eric Blake | 97c7e85 | 2016-07-15 17:23:01 -0600 | [diff] [blame] | 1167 | iscsi_co_generic_cb, &iTask) == NULL) { |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1168 | r = -ENOMEM; |
| 1169 | goto out_unlock; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 1172 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1173 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1174 | if (iTask.task != NULL) { |
| 1175 | scsi_free_scsi_task(iTask.task); |
| 1176 | iTask.task = NULL; |
| 1177 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1178 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1179 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 1180 | iTask.complete = 0; |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1181 | goto retry; |
| 1182 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1183 | |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 1184 | iscsi_allocmap_set_invalid(iscsilun, offset, bytes); |
Peter Lieven | aef172f | 2017-12-08 12:51:07 +0100 | [diff] [blame] | 1185 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1186 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { |
| 1187 | /* the target might fail with a check condition if it |
| 1188 | is not happy with the alignment of the UNMAP request |
| 1189 | we silently fail in this case */ |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1190 | goto out_unlock; |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1191 | } |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1192 | |
Peter Lieven | 01a6a23 | 2013-10-24 12:07:01 +0200 | [diff] [blame] | 1193 | if (iTask.status != SCSI_STATUS_GOOD) { |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 1194 | error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s", |
| 1195 | list.lba, iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1196 | r = iTask.err_code; |
| 1197 | goto out_unlock; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1200 | out_unlock: |
| 1201 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 1202 | g_free(iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1203 | return r; |
Peter Lieven | 65f3e33 | 2013-07-19 09:19:41 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1206 | static int |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 1207 | coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1208 | int bytes, BdrvRequestFlags flags) |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1209 | { |
| 1210 | IscsiLun *iscsilun = bs->opaque; |
| 1211 | struct IscsiTask iTask; |
| 1212 | uint64_t lba; |
| 1213 | uint32_t nb_blocks; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1214 | bool use_16_for_ws = iscsilun->use_16_for_rw; |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1215 | int r = 0; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1216 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1217 | if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) { |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 1218 | return -ENOTSUP; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1221 | if (flags & BDRV_REQ_MAY_UNMAP) { |
| 1222 | if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { |
| 1223 | /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ |
| 1224 | use_16_for_ws = true; |
| 1225 | } |
| 1226 | if (use_16_for_ws && !iscsilun->lbp.lbpws) { |
| 1227 | /* WRITESAME16 with UNMAP is not supported by the target, |
| 1228 | * fall back and try WRITESAME10/16 without UNMAP */ |
| 1229 | flags &= ~BDRV_REQ_MAY_UNMAP; |
| 1230 | use_16_for_ws = iscsilun->use_16_for_rw; |
| 1231 | } |
Paolo Bonzini | fa6252b | 2013-11-22 13:39:54 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
Peter Lieven | dbe5c58 | 2014-04-28 13:23:25 +0200 | [diff] [blame] | 1234 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1235 | /* WRITESAME without UNMAP is not supported by the target */ |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1236 | return -ENOTSUP; |
| 1237 | } |
| 1238 | |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 1239 | lba = offset / iscsilun->block_size; |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 1240 | nb_blocks = bytes / iscsilun->block_size; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1241 | |
| 1242 | if (iscsilun->zeroblock == NULL) { |
Kevin Wolf | 4d5a3f8 | 2014-05-20 13:30:49 +0200 | [diff] [blame] | 1243 | iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); |
| 1244 | if (iscsilun->zeroblock == NULL) { |
| 1245 | return -ENOMEM; |
| 1246 | } |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1249 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1250 | iscsi_co_init_iscsitask(iscsilun, &iTask); |
| 1251 | retry: |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1252 | if (use_16_for_ws) { |
| 1253 | iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 1254 | iscsilun->zeroblock, iscsilun->block_size, |
| 1255 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), |
| 1256 | 0, 0, iscsi_co_generic_cb, &iTask); |
| 1257 | } else { |
| 1258 | iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, |
| 1259 | iscsilun->zeroblock, iscsilun->block_size, |
| 1260 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), |
| 1261 | 0, 0, iscsi_co_generic_cb, &iTask); |
| 1262 | } |
| 1263 | if (iTask.task == NULL) { |
Paolo Bonzini | f6eb0b3 | 2017-03-03 16:23:36 +0100 | [diff] [blame] | 1264 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | 9239711 | 2013-12-20 10:02:47 +0100 | [diff] [blame] | 1265 | return -ENOMEM; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Fam Zheng | 66e75c0 | 2018-06-01 17:26:45 +0800 | [diff] [blame] | 1268 | iscsi_co_wait_for_task(&iTask, iscsilun); |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1269 | |
Peter Lieven | d9738fd | 2014-02-22 13:17:24 +0100 | [diff] [blame] | 1270 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && |
| 1271 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && |
Paolo Bonzini | 27898a5 | 2014-04-02 12:12:50 +0200 | [diff] [blame] | 1272 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || |
| 1273 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { |
Peter Lieven | d9738fd | 2014-02-22 13:17:24 +0100 | [diff] [blame] | 1274 | /* WRITE SAME is not supported by the target */ |
| 1275 | iscsilun->has_write_same = false; |
| 1276 | scsi_free_scsi_task(iTask.task); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1277 | r = -ENOTSUP; |
| 1278 | goto out_unlock; |
Peter Lieven | d9738fd | 2014-02-22 13:17:24 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1281 | if (iTask.task != NULL) { |
| 1282 | scsi_free_scsi_task(iTask.task); |
| 1283 | iTask.task = NULL; |
| 1284 | } |
| 1285 | |
| 1286 | if (iTask.do_retry) { |
Peter Lieven | 837c390 | 2014-02-18 13:08:39 +0100 | [diff] [blame] | 1287 | iTask.complete = 0; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1288 | goto retry; |
| 1289 | } |
| 1290 | |
| 1291 | if (iTask.status != SCSI_STATUS_GOOD) { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 1292 | iscsi_allocmap_set_invalid(iscsilun, offset, bytes); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 1293 | error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s", |
| 1294 | lba, iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1295 | r = iTask.err_code; |
| 1296 | goto out_unlock; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1299 | if (flags & BDRV_REQ_MAY_UNMAP) { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 1300 | iscsi_allocmap_set_invalid(iscsilun, offset, bytes); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1301 | } else { |
Eric Blake | 04a408f | 2018-02-13 14:26:47 -0600 | [diff] [blame] | 1302 | iscsi_allocmap_set_allocated(iscsilun, offset, bytes); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1305 | out_unlock: |
| 1306 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | e38bc23 | 2017-12-08 12:51:08 +0100 | [diff] [blame] | 1307 | g_free(iTask.err_str); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1308 | return r; |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1311 | static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts, |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1312 | Error **errp) |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1313 | { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1314 | const char *user = NULL; |
| 1315 | const char *password = NULL; |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1316 | const char *secretid; |
| 1317 | char *secret = NULL; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1318 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1319 | user = qemu_opt_get(opts, "user"); |
| 1320 | if (!user) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1321 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1322 | } |
| 1323 | |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1324 | secretid = qemu_opt_get(opts, "password-secret"); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1325 | password = qemu_opt_get(opts, "password"); |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1326 | if (secretid && password) { |
| 1327 | error_setg(errp, "'password' and 'password-secret' properties are " |
| 1328 | "mutually exclusive"); |
| 1329 | return; |
| 1330 | } |
| 1331 | if (secretid) { |
| 1332 | secret = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 1333 | if (!secret) { |
| 1334 | return; |
| 1335 | } |
| 1336 | password = secret; |
| 1337 | } else if (!password) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1338 | error_setg(errp, "CHAP username specified but no password was given"); |
| 1339 | return; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1343 | error_setg(errp, "Failed to set initiator username and password"); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1344 | } |
Daniel P. Berrange | b189346 | 2016-01-21 14:19:21 +0000 | [diff] [blame] | 1345 | |
| 1346 | g_free(secret); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1347 | } |
| 1348 | |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1349 | static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts, |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1350 | Error **errp) |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1351 | { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1352 | const char *digest = NULL; |
| 1353 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1354 | digest = qemu_opt_get(opts, "header-digest"); |
| 1355 | if (!digest) { |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1356 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
Kevin Wolf | 31eb120 | 2016-12-08 14:23:11 +0100 | [diff] [blame] | 1357 | } else if (!strcmp(digest, "crc32c")) { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1358 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); |
Kevin Wolf | 31eb120 | 2016-12-08 14:23:11 +0100 | [diff] [blame] | 1359 | } else if (!strcmp(digest, "none")) { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1360 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); |
Kevin Wolf | 31eb120 | 2016-12-08 14:23:11 +0100 | [diff] [blame] | 1361 | } else if (!strcmp(digest, "crc32c-none")) { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1362 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); |
Kevin Wolf | 31eb120 | 2016-12-08 14:23:11 +0100 | [diff] [blame] | 1363 | } else if (!strcmp(digest, "none-crc32c")) { |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1364 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); |
| 1365 | } else { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1366 | error_setg(errp, "Invalid header-digest setting : %s", digest); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Kevin Wolf | d4e7992 | 2016-12-08 14:23:08 +0100 | [diff] [blame] | 1370 | static char *get_initiator_name(QemuOpts *opts) |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1371 | { |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1372 | const char *name; |
| 1373 | char *iscsi_name; |
| 1374 | UuidInfo *uuid_info; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1375 | |
Kevin Wolf | d4e7992 | 2016-12-08 14:23:08 +0100 | [diff] [blame] | 1376 | name = qemu_opt_get(opts, "initiator-name"); |
| 1377 | if (name) { |
| 1378 | return g_strdup(name); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1379 | } |
| 1380 | |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1381 | uuid_info = qmp_query_uuid(NULL); |
| 1382 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { |
| 1383 | name = qemu_get_vm_name(); |
Paolo Bonzini | f2ef4a6 | 2012-08-06 10:54:41 +0200 | [diff] [blame] | 1384 | } else { |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1385 | name = uuid_info->UUID; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1386 | } |
Paolo Bonzini | 5accc84 | 2013-08-02 17:02:01 +0200 | [diff] [blame] | 1387 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", |
| 1388 | name ? ":" : "", name ? name : ""); |
| 1389 | qapi_free_UuidInfo(uuid_info); |
| 1390 | return iscsi_name; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1391 | } |
| 1392 | |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1393 | static void iscsi_nop_timed_event(void *opaque) |
| 1394 | { |
| 1395 | IscsiLun *iscsilun = opaque; |
| 1396 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1397 | qemu_mutex_lock(&iscsilun->mutex); |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1398 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1399 | error_report("iSCSI: NOP timeout. Reconnecting..."); |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1400 | iscsilun->request_timed_out = true; |
| 1401 | } 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] | 1402 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); |
Paolo Bonzini | 2f47da5 | 2017-02-13 14:52:29 +0100 | [diff] [blame] | 1403 | goto out; |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 1406 | 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] | 1407 | iscsi_set_events(iscsilun); |
Paolo Bonzini | 2f47da5 | 2017-02-13 14:52:29 +0100 | [diff] [blame] | 1408 | |
| 1409 | out: |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1410 | qemu_mutex_unlock(&iscsilun->mutex); |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1411 | } |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1412 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1413 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1414 | { |
| 1415 | struct scsi_task *task = NULL; |
| 1416 | struct scsi_readcapacity10 *rc10 = NULL; |
| 1417 | struct scsi_readcapacity16 *rc16 = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1418 | int retries = ISCSI_CMD_RETRIES; |
| 1419 | |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1420 | do { |
| 1421 | if (task != NULL) { |
| 1422 | scsi_free_scsi_task(task); |
| 1423 | task = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1424 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1425 | |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1426 | switch (iscsilun->type) { |
| 1427 | case TYPE_DISK: |
| 1428 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); |
| 1429 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { |
| 1430 | rc16 = scsi_datain_unmarshall(task); |
| 1431 | if (rc16 == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1432 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1433 | } else { |
| 1434 | iscsilun->block_size = rc16->block_length; |
| 1435 | iscsilun->num_blocks = rc16->returned_lba + 1; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 1436 | iscsilun->lbpme = !!rc16->lbpme; |
| 1437 | iscsilun->lbprz = !!rc16->lbprz; |
Peter Lieven | 9281fe9 | 2014-06-04 15:47:39 +0200 | [diff] [blame] | 1438 | iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1439 | } |
Zhu Lingshan | 1cb6d13 | 2015-12-29 11:32:14 +0800 | [diff] [blame] | 1440 | break; |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1441 | } |
Zhu Lingshan | 1cb6d13 | 2015-12-29 11:32:14 +0800 | [diff] [blame] | 1442 | if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
| 1443 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { |
| 1444 | break; |
| 1445 | } |
| 1446 | /* Fall through and try READ CAPACITY(10) instead. */ |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1447 | case TYPE_ROM: |
| 1448 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); |
| 1449 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { |
| 1450 | rc10 = scsi_datain_unmarshall(task); |
| 1451 | if (rc10 == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1452 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1453 | } else { |
| 1454 | iscsilun->block_size = rc10->block_size; |
| 1455 | if (rc10->lba == 0) { |
| 1456 | /* blank disk loaded */ |
| 1457 | iscsilun->num_blocks = 0; |
| 1458 | } else { |
| 1459 | iscsilun->num_blocks = rc10->lba + 1; |
| 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | break; |
| 1464 | default: |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1465 | return; |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1466 | } |
| 1467 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION |
| 1468 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION |
| 1469 | && retries-- > 0); |
| 1470 | |
| 1471 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
Zhu Lingshan | 240125b | 2015-12-16 12:40:40 +0800 | [diff] [blame] | 1472 | error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); |
Peter Lieven | 6d1f252 | 2015-08-14 13:33:36 +0200 | [diff] [blame] | 1473 | } else if (!iscsilun->block_size || |
| 1474 | iscsilun->block_size % BDRV_SECTOR_SIZE) { |
| 1475 | error_setg(errp, "iSCSI: the target returned an invalid " |
| 1476 | "block size of %d.", iscsilun->block_size); |
Paolo Bonzini | 1288844 | 2013-05-31 13:56:24 +0200 | [diff] [blame] | 1477 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1478 | if (task) { |
| 1479 | scsi_free_scsi_task(task); |
| 1480 | } |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1483 | 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] | 1484 | int evpd, int pc, void **inq, Error **errp) |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1485 | { |
| 1486 | int full_size; |
| 1487 | struct scsi_task *task = NULL; |
| 1488 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); |
| 1489 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
| 1490 | goto fail; |
| 1491 | } |
| 1492 | full_size = scsi_datain_getfullsize(task); |
| 1493 | if (full_size > task->datain.size) { |
| 1494 | scsi_free_scsi_task(task); |
| 1495 | |
| 1496 | /* we need more data for the full list */ |
| 1497 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1498 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { |
| 1499 | goto fail; |
| 1500 | } |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1501 | } |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1502 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1503 | *inq = scsi_datain_unmarshall(task); |
| 1504 | if (*inq == NULL) { |
| 1505 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); |
Markus Armbruster | 172fc4d | 2014-04-25 16:50:35 +0200 | [diff] [blame] | 1506 | goto fail_with_err; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1509 | return task; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1510 | |
| 1511 | fail: |
Markus Armbruster | 172fc4d | 2014-04-25 16:50:35 +0200 | [diff] [blame] | 1512 | error_setg(errp, "iSCSI: Inquiry command failed : %s", |
| 1513 | iscsi_get_error(iscsi)); |
| 1514 | fail_with_err: |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1515 | if (task != NULL) { |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1516 | scsi_free_scsi_task(task); |
Paolo Bonzini | 35cb174 | 2014-02-17 14:43:52 +0100 | [diff] [blame] | 1517 | } |
| 1518 | return NULL; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1521 | static void iscsi_detach_aio_context(BlockDriverState *bs) |
| 1522 | { |
| 1523 | IscsiLun *iscsilun = bs->opaque; |
| 1524 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 1525 | aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 1526 | false, NULL, NULL, NULL, NULL); |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1527 | iscsilun->events = 0; |
| 1528 | |
| 1529 | if (iscsilun->nop_timer) { |
| 1530 | timer_del(iscsilun->nop_timer); |
| 1531 | timer_free(iscsilun->nop_timer); |
| 1532 | iscsilun->nop_timer = NULL; |
| 1533 | } |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1534 | if (iscsilun->event_timer) { |
| 1535 | timer_del(iscsilun->event_timer); |
| 1536 | timer_free(iscsilun->event_timer); |
| 1537 | iscsilun->event_timer = NULL; |
| 1538 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | static void iscsi_attach_aio_context(BlockDriverState *bs, |
| 1542 | AioContext *new_context) |
| 1543 | { |
| 1544 | IscsiLun *iscsilun = bs->opaque; |
| 1545 | |
| 1546 | iscsilun->aio_context = new_context; |
| 1547 | iscsi_set_events(iscsilun); |
| 1548 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1549 | /* Set up a timer for sending out iSCSI NOPs */ |
| 1550 | iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, |
| 1551 | QEMU_CLOCK_REALTIME, SCALE_MS, |
| 1552 | iscsi_nop_timed_event, iscsilun); |
| 1553 | timer_mod(iscsilun->nop_timer, |
| 1554 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1555 | |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1556 | /* Set up a timer for periodic calls to iscsi_set_events and to |
| 1557 | * scan for command timeout */ |
Peter Lieven | 05b685f | 2015-04-07 22:08:15 +0200 | [diff] [blame] | 1558 | iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, |
| 1559 | QEMU_CLOCK_REALTIME, SCALE_MS, |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1560 | iscsi_timed_check_events, iscsilun); |
| 1561 | timer_mod(iscsilun->event_timer, |
| 1562 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1565 | static void iscsi_modesense_sync(IscsiLun *iscsilun) |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1566 | { |
| 1567 | struct scsi_task *task; |
| 1568 | struct scsi_mode_sense *ms = NULL; |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1569 | iscsilun->write_protected = false; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 1570 | iscsilun->dpofua = false; |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1571 | |
| 1572 | task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, |
| 1573 | 1, SCSI_MODESENSE_PC_CURRENT, |
| 1574 | 0x3F, 0, 255); |
| 1575 | if (task == NULL) { |
| 1576 | error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", |
| 1577 | iscsi_get_error(iscsilun->iscsi)); |
| 1578 | goto out; |
| 1579 | } |
| 1580 | |
| 1581 | if (task->status != SCSI_STATUS_GOOD) { |
| 1582 | error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); |
| 1583 | goto out; |
| 1584 | } |
| 1585 | ms = scsi_datain_unmarshall(task); |
| 1586 | if (!ms) { |
| 1587 | error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", |
| 1588 | iscsi_get_error(iscsilun->iscsi)); |
| 1589 | goto out; |
| 1590 | } |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1591 | iscsilun->write_protected = ms->device_specific_parameter & 0x80; |
Peter Lieven | 752ce45 | 2015-04-16 16:08:28 +0200 | [diff] [blame] | 1592 | iscsilun->dpofua = ms->device_specific_parameter & 0x10; |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1593 | |
| 1594 | out: |
| 1595 | if (task) { |
| 1596 | scsi_free_scsi_task(task); |
| 1597 | } |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1598 | } |
| 1599 | |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1600 | static void iscsi_parse_iscsi_option(const char *target, QDict *options) |
| 1601 | { |
| 1602 | QemuOptsList *list; |
| 1603 | QemuOpts *opts; |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1604 | const char *user, *password, *password_secret, *initiator_name, |
Kevin Wolf | 1d56010 | 2016-12-08 14:23:10 +0100 | [diff] [blame] | 1605 | *header_digest, *timeout; |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1606 | |
| 1607 | list = qemu_find_opts("iscsi"); |
| 1608 | if (!list) { |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | opts = qemu_opts_find(list, target); |
| 1613 | if (opts == NULL) { |
| 1614 | opts = QTAILQ_FIRST(&list->head); |
| 1615 | if (!opts) { |
| 1616 | return; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | user = qemu_opt_get(opts, "user"); |
| 1621 | if (user) { |
| 1622 | qdict_set_default_str(options, "user", user); |
| 1623 | } |
| 1624 | |
| 1625 | password = qemu_opt_get(opts, "password"); |
| 1626 | if (password) { |
| 1627 | qdict_set_default_str(options, "password", password); |
| 1628 | } |
| 1629 | |
| 1630 | password_secret = qemu_opt_get(opts, "password-secret"); |
| 1631 | if (password_secret) { |
| 1632 | qdict_set_default_str(options, "password-secret", password_secret); |
| 1633 | } |
Kevin Wolf | d4e7992 | 2016-12-08 14:23:08 +0100 | [diff] [blame] | 1634 | |
| 1635 | initiator_name = qemu_opt_get(opts, "initiator-name"); |
| 1636 | if (initiator_name) { |
| 1637 | qdict_set_default_str(options, "initiator-name", initiator_name); |
| 1638 | } |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1639 | |
| 1640 | header_digest = qemu_opt_get(opts, "header-digest"); |
| 1641 | if (header_digest) { |
Kevin Wolf | 31eb120 | 2016-12-08 14:23:11 +0100 | [diff] [blame] | 1642 | /* -iscsi takes upper case values, but QAPI only supports lower case |
| 1643 | * enum constant names, so we have to convert here. */ |
| 1644 | char *qapi_value = g_ascii_strdown(header_digest, -1); |
| 1645 | qdict_set_default_str(options, "header-digest", qapi_value); |
| 1646 | g_free(qapi_value); |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1647 | } |
Kevin Wolf | 1d56010 | 2016-12-08 14:23:10 +0100 | [diff] [blame] | 1648 | |
| 1649 | timeout = qemu_opt_get(opts, "timeout"); |
| 1650 | if (timeout) { |
| 1651 | qdict_set_default_str(options, "timeout", timeout); |
| 1652 | } |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1653 | } |
| 1654 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1655 | /* |
| 1656 | * We support iscsi url's on the form |
| 1657 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> |
| 1658 | */ |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1659 | static void iscsi_parse_filename(const char *filename, QDict *options, |
| 1660 | Error **errp) |
| 1661 | { |
| 1662 | struct iscsi_url *iscsi_url; |
| 1663 | const char *transport_name; |
| 1664 | char *lun_str; |
| 1665 | |
| 1666 | iscsi_url = iscsi_parse_full_url(NULL, filename); |
| 1667 | if (iscsi_url == NULL) { |
| 1668 | error_setg(errp, "Failed to parse URL : %s", filename); |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | #if LIBISCSI_API_VERSION >= (20160603) |
| 1673 | switch (iscsi_url->transport) { |
| 1674 | case TCP_TRANSPORT: |
| 1675 | transport_name = "tcp"; |
| 1676 | break; |
| 1677 | case ISER_TRANSPORT: |
| 1678 | transport_name = "iser"; |
| 1679 | break; |
| 1680 | default: |
| 1681 | error_setg(errp, "Unknown transport type (%d)", |
| 1682 | iscsi_url->transport); |
| 1683 | return; |
| 1684 | } |
| 1685 | #else |
| 1686 | transport_name = "tcp"; |
| 1687 | #endif |
| 1688 | |
| 1689 | qdict_set_default_str(options, "transport", transport_name); |
| 1690 | qdict_set_default_str(options, "portal", iscsi_url->portal); |
| 1691 | qdict_set_default_str(options, "target", iscsi_url->target); |
| 1692 | |
| 1693 | lun_str = g_strdup_printf("%d", iscsi_url->lun); |
| 1694 | qdict_set_default_str(options, "lun", lun_str); |
| 1695 | g_free(lun_str); |
| 1696 | |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1697 | /* User/password from -iscsi take precedence over those from the URL */ |
| 1698 | iscsi_parse_iscsi_option(iscsi_url->target, options); |
| 1699 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1700 | if (iscsi_url->user[0] != '\0') { |
| 1701 | qdict_set_default_str(options, "user", iscsi_url->user); |
| 1702 | qdict_set_default_str(options, "password", iscsi_url->passwd); |
| 1703 | } |
| 1704 | |
| 1705 | iscsi_destroy_url(iscsi_url); |
| 1706 | } |
| 1707 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1708 | static QemuOptsList runtime_opts = { |
| 1709 | .name = "iscsi", |
| 1710 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 1711 | .desc = { |
| 1712 | { |
| 1713 | .name = "transport", |
| 1714 | .type = QEMU_OPT_STRING, |
| 1715 | }, |
| 1716 | { |
| 1717 | .name = "portal", |
| 1718 | .type = QEMU_OPT_STRING, |
| 1719 | }, |
| 1720 | { |
| 1721 | .name = "target", |
| 1722 | .type = QEMU_OPT_STRING, |
| 1723 | }, |
| 1724 | { |
| 1725 | .name = "user", |
| 1726 | .type = QEMU_OPT_STRING, |
| 1727 | }, |
| 1728 | { |
| 1729 | .name = "password", |
| 1730 | .type = QEMU_OPT_STRING, |
| 1731 | }, |
| 1732 | { |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1733 | .name = "password-secret", |
| 1734 | .type = QEMU_OPT_STRING, |
| 1735 | }, |
| 1736 | { |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1737 | .name = "lun", |
| 1738 | .type = QEMU_OPT_NUMBER, |
| 1739 | }, |
Kevin Wolf | d4e7992 | 2016-12-08 14:23:08 +0100 | [diff] [blame] | 1740 | { |
| 1741 | .name = "initiator-name", |
| 1742 | .type = QEMU_OPT_STRING, |
| 1743 | }, |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1744 | { |
| 1745 | .name = "header-digest", |
| 1746 | .type = QEMU_OPT_STRING, |
| 1747 | }, |
Kevin Wolf | 1d56010 | 2016-12-08 14:23:10 +0100 | [diff] [blame] | 1748 | { |
| 1749 | .name = "timeout", |
| 1750 | .type = QEMU_OPT_NUMBER, |
| 1751 | }, |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1752 | { /* end of list */ } |
| 1753 | }, |
| 1754 | }; |
| 1755 | |
Fam Zheng | cc9743c | 2018-06-01 17:26:44 +0800 | [diff] [blame] | 1756 | static void iscsi_save_designator(IscsiLun *lun, |
| 1757 | struct scsi_inquiry_device_identification *inq_di) |
| 1758 | { |
| 1759 | struct scsi_inquiry_device_designator *desig, *copy = NULL; |
| 1760 | |
| 1761 | for (desig = inq_di->designators; desig; desig = desig->next) { |
| 1762 | if (desig->association || |
| 1763 | desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) { |
| 1764 | continue; |
| 1765 | } |
| 1766 | /* NAA works better than T10 vendor ID based designator. */ |
| 1767 | if (!copy || copy->designator_type < desig->designator_type) { |
| 1768 | copy = desig; |
| 1769 | } |
| 1770 | } |
| 1771 | if (copy) { |
| 1772 | lun->dd = g_new(struct scsi_inquiry_device_designator, 1); |
| 1773 | *lun->dd = *copy; |
| 1774 | lun->dd->next = NULL; |
| 1775 | lun->dd->designator = g_malloc(copy->designator_length); |
| 1776 | memcpy(lun->dd->designator, copy->designator, copy->designator_length); |
| 1777 | } |
| 1778 | } |
| 1779 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 1780 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, |
| 1781 | Error **errp) |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1782 | { |
| 1783 | IscsiLun *iscsilun = bs->opaque; |
| 1784 | struct iscsi_context *iscsi = NULL; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1785 | struct scsi_task *task = NULL; |
| 1786 | struct scsi_inquiry_standard *inq = NULL; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1787 | struct scsi_inquiry_supported_pages *inq_vpd; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1788 | char *initiator_name = NULL; |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1789 | QemuOpts *opts; |
| 1790 | Error *local_err = NULL; |
Markus Armbruster | deadbb8 | 2018-06-14 21:14:27 +0200 | [diff] [blame] | 1791 | const char *transport_name, *portal, *target; |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1792 | #if LIBISCSI_API_VERSION >= (20160603) |
| 1793 | enum iscsi_transport_type transport; |
| 1794 | #endif |
| 1795 | int i, ret = 0, timeout = 0, lun; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1796 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 1797 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1798 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1799 | if (local_err) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1800 | error_propagate(errp, local_err); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1801 | ret = -EINVAL; |
| 1802 | goto out; |
| 1803 | } |
| 1804 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1805 | transport_name = qemu_opt_get(opts, "transport"); |
| 1806 | portal = qemu_opt_get(opts, "portal"); |
| 1807 | target = qemu_opt_get(opts, "target"); |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1808 | lun = qemu_opt_get_number(opts, "lun", 0); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 1809 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1810 | if (!transport_name || !portal || !target) { |
| 1811 | error_setg(errp, "Need all of transport, portal and target options"); |
| 1812 | ret = -EINVAL; |
| 1813 | goto out; |
| 1814 | } |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1815 | |
| 1816 | if (!strcmp(transport_name, "tcp")) { |
| 1817 | #if LIBISCSI_API_VERSION >= (20160603) |
| 1818 | transport = TCP_TRANSPORT; |
| 1819 | } else if (!strcmp(transport_name, "iser")) { |
| 1820 | transport = ISER_TRANSPORT; |
| 1821 | #else |
| 1822 | /* TCP is what older libiscsi versions always use */ |
| 1823 | #endif |
| 1824 | } else { |
| 1825 | error_setg(errp, "Unknown transport: %s", transport_name); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1826 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1827 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1828 | } |
| 1829 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1830 | memset(iscsilun, 0, sizeof(IscsiLun)); |
| 1831 | |
Kevin Wolf | d4e7992 | 2016-12-08 14:23:08 +0100 | [diff] [blame] | 1832 | initiator_name = get_initiator_name(opts); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1833 | |
| 1834 | iscsi = iscsi_create_context(initiator_name); |
| 1835 | if (iscsi == NULL) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1836 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1837 | ret = -ENOMEM; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1838 | goto out; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1839 | } |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 1840 | #if LIBISCSI_API_VERSION >= (20160603) |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1841 | if (iscsi_init_transport(iscsi, transport)) { |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 1842 | error_setg(errp, ("Error initializing transport.")); |
| 1843 | ret = -EINVAL; |
| 1844 | goto out; |
| 1845 | } |
| 1846 | #endif |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1847 | if (iscsi_set_targetname(iscsi, target)) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1848 | error_setg(errp, "iSCSI: Failed to set target name."); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1849 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1850 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1851 | } |
| 1852 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1853 | /* check if we got CHAP username/password via the options */ |
Kevin Wolf | 4317142 | 2016-12-08 14:23:07 +0100 | [diff] [blame] | 1854 | apply_chap(iscsi, opts, &local_err); |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1855 | if (local_err != NULL) { |
| 1856 | error_propagate(errp, local_err); |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1857 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1858 | goto out; |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1859 | } |
| 1860 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1861 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1862 | error_setg(errp, "iSCSI: Failed to set session type to normal."); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1863 | ret = -EINVAL; |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 1864 | goto out; |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1865 | } |
| 1866 | |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1867 | /* check if we got HEADER_DIGEST via the options */ |
Kevin Wolf | 81aa2a0 | 2016-12-08 14:23:09 +0100 | [diff] [blame] | 1868 | apply_header_digest(iscsi, opts, &local_err); |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1869 | if (local_err != NULL) { |
| 1870 | error_propagate(errp, local_err); |
| 1871 | ret = -EINVAL; |
| 1872 | goto out; |
| 1873 | } |
Ronnie Sahlberg | f9dadc9 | 2012-01-26 09:39:02 +1100 | [diff] [blame] | 1874 | |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 1875 | /* timeout handling is broken in libiscsi before 1.15.0 */ |
Kevin Wolf | 1d56010 | 2016-12-08 14:23:10 +0100 | [diff] [blame] | 1876 | timeout = qemu_opt_get_number(opts, "timeout", 0); |
Roy Shterman | 583ec22 | 2016-10-09 11:14:55 +0300 | [diff] [blame] | 1877 | #if LIBISCSI_API_VERSION >= 20150621 |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 1878 | iscsi_set_timeout(iscsi, timeout); |
| 1879 | #else |
| 1880 | if (timeout) { |
Markus Armbruster | 5197f44 | 2018-10-17 10:26:27 +0200 | [diff] [blame] | 1881 | warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); |
Peter Lieven | 9049736 | 2015-06-26 12:18:01 +0200 | [diff] [blame] | 1882 | } |
| 1883 | #endif |
Peter Lieven | 5dd7a53 | 2015-06-16 13:45:07 +0200 | [diff] [blame] | 1884 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1885 | if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) { |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1886 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1887 | iscsi_get_error(iscsi)); |
| 1888 | ret = -EINVAL; |
| 1889 | goto out; |
| 1890 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 1891 | |
| 1892 | iscsilun->iscsi = iscsi; |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1893 | iscsilun->aio_context = bdrv_get_aio_context(bs); |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 1894 | iscsilun->lun = lun; |
Paolo Bonzini | fa6252b | 2013-11-22 13:39:54 +0100 | [diff] [blame] | 1895 | iscsilun->has_write_same = true; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1896 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1897 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, |
| 1898 | (void **) &inq, errp); |
| 1899 | if (task == NULL) { |
| 1900 | ret = -EINVAL; |
| 1901 | goto out; |
| 1902 | } |
| 1903 | iscsilun->type = inq->periperal_device_type; |
| 1904 | scsi_free_scsi_task(task); |
| 1905 | task = NULL; |
| 1906 | |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1907 | iscsi_modesense_sync(iscsilun); |
Eric Blake | 4df863f | 2016-05-03 16:39:06 -0600 | [diff] [blame] | 1908 | if (iscsilun->dpofua) { |
| 1909 | bs->supported_write_flags = BDRV_REQ_FUA; |
| 1910 | } |
Peter Lieven | 7191f20 | 2015-04-16 16:08:27 +0200 | [diff] [blame] | 1911 | |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1912 | /* Check the write protect flag of the LUN if we want to write */ |
| 1913 | if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 1914 | iscsilun->write_protected) { |
Kevin Wolf | 8f3bf50 | 2018-10-08 17:27:18 +0200 | [diff] [blame] | 1915 | ret = bdrv_apply_auto_read_only(bs, "LUN is write protected", errp); |
| 1916 | if (ret < 0) { |
| 1917 | goto out; |
| 1918 | } |
| 1919 | flags &= ~BDRV_O_RDWR; |
Fam Zheng | c1d4096 | 2014-10-30 19:23:46 +0800 | [diff] [blame] | 1920 | } |
| 1921 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 1922 | iscsi_readcapacity_sync(iscsilun, &local_err); |
| 1923 | if (local_err != NULL) { |
| 1924 | error_propagate(errp, local_err); |
Fam Zheng | cd82b6f | 2014-04-10 09:33:55 +0800 | [diff] [blame] | 1925 | ret = -EINVAL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 1926 | goto out; |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1927 | } |
Peter Lieven | 0777b5d | 2013-07-11 14:16:25 +0200 | [diff] [blame] | 1928 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 1929 | |
Kevin Wolf | f47c3f5 | 2014-03-05 15:45:00 +0100 | [diff] [blame] | 1930 | /* We don't have any emulation for devices other than disks and CD-ROMs, so |
| 1931 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu |
| 1932 | * will try to read from the device to guess the image format. |
Ronnie Sahlberg | 622695a | 2012-05-26 14:56:41 +1000 | [diff] [blame] | 1933 | */ |
Kevin Wolf | f47c3f5 | 2014-03-05 15:45:00 +0100 | [diff] [blame] | 1934 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { |
Eric Blake | 5411541 | 2016-06-23 16:37:26 -0600 | [diff] [blame] | 1935 | bs->sg = true; |
Ronnie Sahlberg | 622695a | 2012-05-26 14:56:41 +1000 | [diff] [blame] | 1936 | } |
| 1937 | |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1938 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1939 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, |
| 1940 | (void **) &inq_vpd, errp); |
| 1941 | if (task == NULL) { |
| 1942 | ret = -EINVAL; |
| 1943 | goto out; |
| 1944 | } |
| 1945 | for (i = 0; i < inq_vpd->num_pages; i++) { |
| 1946 | struct scsi_task *inq_task; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1947 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1948 | struct scsi_inquiry_block_limits *inq_bl; |
Fam Zheng | cc9743c | 2018-06-01 17:26:44 +0800 | [diff] [blame] | 1949 | struct scsi_inquiry_device_identification *inq_di; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1950 | switch (inq_vpd->pages[i]) { |
| 1951 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: |
| 1952 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1953 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, |
| 1954 | (void **) &inq_lbp, errp); |
| 1955 | if (inq_task == NULL) { |
| 1956 | ret = -EINVAL; |
| 1957 | goto out; |
| 1958 | } |
| 1959 | memcpy(&iscsilun->lbp, inq_lbp, |
| 1960 | sizeof(struct scsi_inquiry_logical_block_provisioning)); |
| 1961 | scsi_free_scsi_task(inq_task); |
| 1962 | break; |
| 1963 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: |
| 1964 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1965 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, |
| 1966 | (void **) &inq_bl, errp); |
| 1967 | if (inq_task == NULL) { |
| 1968 | ret = -EINVAL; |
| 1969 | goto out; |
| 1970 | } |
| 1971 | memcpy(&iscsilun->bl, inq_bl, |
| 1972 | sizeof(struct scsi_inquiry_block_limits)); |
| 1973 | scsi_free_scsi_task(inq_task); |
| 1974 | break; |
Fam Zheng | cc9743c | 2018-06-01 17:26:44 +0800 | [diff] [blame] | 1975 | case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION: |
| 1976 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, |
| 1977 | SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION, |
| 1978 | (void **) &inq_di, errp); |
| 1979 | if (inq_task == NULL) { |
| 1980 | ret = -EINVAL; |
| 1981 | goto out; |
| 1982 | } |
| 1983 | iscsi_save_designator(iscsilun, inq_di); |
| 1984 | scsi_free_scsi_task(inq_task); |
| 1985 | break; |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1986 | default: |
| 1987 | break; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1988 | } |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1989 | } |
Peter Lieven | 24d3bd6 | 2014-02-17 18:34:08 +0100 | [diff] [blame] | 1990 | scsi_free_scsi_task(task); |
| 1991 | task = NULL; |
Peter Lieven | f18a7cb | 2013-07-19 09:19:39 +0200 | [diff] [blame] | 1992 | |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 1993 | qemu_mutex_init(&iscsilun->mutex); |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 1994 | iscsi_attach_aio_context(bs, iscsilun->aio_context); |
Peter Lieven | 5b5d34e | 2012-12-06 10:46:47 +0100 | [diff] [blame] | 1995 | |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 1996 | /* Guess the internal cluster (page) size of the iscsi target by the means |
| 1997 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a |
| 1998 | * reasonable size */ |
Peter Lieven | 3d2acaa | 2014-04-28 17:18:32 +0200 | [diff] [blame] | 1999 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 2000 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 2001 | iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran * |
| 2002 | iscsilun->block_size; |
Peter Lieven | 9eac362 | 2015-04-16 16:08:33 +0200 | [diff] [blame] | 2003 | if (iscsilun->lbprz) { |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 2004 | ret = iscsi_allocmap_init(iscsilun, bs->open_flags); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 2005 | } |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Eric Blake | e24d813 | 2018-01-26 13:34:39 -0600 | [diff] [blame] | 2008 | if (iscsilun->lbprz && iscsilun->lbp.lbpws) { |
| 2009 | bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; |
| 2010 | } |
| 2011 | |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 2012 | out: |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 2013 | qemu_opts_del(opts); |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 2014 | g_free(initiator_name); |
Peter Lieven | e829b0b | 2012-11-17 14:37:39 +0100 | [diff] [blame] | 2015 | if (task != NULL) { |
| 2016 | scsi_free_scsi_task(task); |
| 2017 | } |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 2018 | |
| 2019 | if (ret) { |
| 2020 | if (iscsi != NULL) { |
Peter Lieven | 20474e9 | 2015-04-16 16:08:25 +0200 | [diff] [blame] | 2021 | if (iscsi_is_logged_in(iscsi)) { |
| 2022 | iscsi_logout_sync(iscsi); |
| 2023 | } |
Paolo Bonzini | b93c94f | 2012-08-06 10:52:22 +0200 | [diff] [blame] | 2024 | iscsi_destroy_context(iscsi); |
| 2025 | } |
| 2026 | memset(iscsilun, 0, sizeof(IscsiLun)); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2027 | } |
Markus Armbruster | deadbb8 | 2018-06-14 21:14:27 +0200 | [diff] [blame] | 2028 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | static void iscsi_close(BlockDriverState *bs) |
| 2033 | { |
| 2034 | IscsiLun *iscsilun = bs->opaque; |
| 2035 | struct iscsi_context *iscsi = iscsilun->iscsi; |
| 2036 | |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 2037 | iscsi_detach_aio_context(bs); |
Peter Lieven | 20474e9 | 2015-04-16 16:08:25 +0200 | [diff] [blame] | 2038 | if (iscsi_is_logged_in(iscsi)) { |
| 2039 | iscsi_logout_sync(iscsi); |
| 2040 | } |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2041 | iscsi_destroy_context(iscsi); |
Fam Zheng | cc9743c | 2018-06-01 17:26:44 +0800 | [diff] [blame] | 2042 | if (iscsilun->dd) { |
| 2043 | g_free(iscsilun->dd->designator); |
| 2044 | g_free(iscsilun->dd); |
| 2045 | } |
Peter Lieven | d4cd961 | 2013-10-24 12:07:02 +0200 | [diff] [blame] | 2046 | g_free(iscsilun->zeroblock); |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 2047 | iscsi_allocmap_free(iscsilun); |
Paolo Bonzini | d045c46 | 2017-02-22 19:07:25 +0100 | [diff] [blame] | 2048 | qemu_mutex_destroy(&iscsilun->mutex); |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2049 | memset(iscsilun, 0, sizeof(IscsiLun)); |
| 2050 | } |
| 2051 | |
Kevin Wolf | 3baca89 | 2014-07-16 17:48:16 +0200 | [diff] [blame] | 2052 | static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2053 | { |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2054 | /* We don't actually refresh here, but just return data queried in |
| 2055 | * iscsi_open(): iscsi targets don't change their limits. */ |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 2056 | |
| 2057 | IscsiLun *iscsilun = bs->opaque; |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2058 | uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2059 | unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size); |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 2060 | |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2061 | assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg); |
| 2062 | |
| 2063 | bs->bl.request_alignment = block_size; |
Eric Blake | c8b3b99 | 2016-06-23 16:37:14 -0600 | [diff] [blame] | 2064 | |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 2065 | if (iscsilun->bl.max_xfer_len) { |
| 2066 | max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); |
| 2067 | } |
| 2068 | |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2069 | if (max_xfer_len * block_size < INT_MAX) { |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2070 | bs->bl.max_transfer = max_xfer_len * iscsilun->block_size; |
| 2071 | } |
Peter Lieven | 52f6fa1 | 2014-10-27 10:18:45 +0100 | [diff] [blame] | 2072 | |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 2073 | if (iscsilun->lbp.lbpu) { |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2074 | if (iscsilun->bl.max_unmap < 0xffffffff / block_size) { |
Eric Blake | b9f7855 | 2016-06-23 16:37:21 -0600 | [diff] [blame] | 2075 | bs->bl.max_pdiscard = |
| 2076 | iscsilun->bl.max_unmap * iscsilun->block_size; |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2077 | } |
Eric Blake | b9f7855 | 2016-06-23 16:37:21 -0600 | [diff] [blame] | 2078 | bs->bl.pdiscard_alignment = |
| 2079 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size; |
Eric Blake | 8b18474 | 2016-06-01 15:10:01 -0600 | [diff] [blame] | 2080 | } else { |
Eric Blake | b9f7855 | 2016-06-23 16:37:21 -0600 | [diff] [blame] | 2081 | bs->bl.pdiscard_alignment = iscsilun->block_size; |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 2082 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2083 | |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2084 | if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) { |
Eric Blake | cf081fc | 2016-06-01 15:10:02 -0600 | [diff] [blame] | 2085 | bs->bl.max_pwrite_zeroes = |
| 2086 | iscsilun->bl.max_ws_len * iscsilun->block_size; |
Paolo Bonzini | c97ca29 | 2014-04-02 15:30:29 +0200 | [diff] [blame] | 2087 | } |
| 2088 | if (iscsilun->lbp.lbpws) { |
Eric Blake | cf081fc | 2016-06-01 15:10:02 -0600 | [diff] [blame] | 2089 | bs->bl.pwrite_zeroes_alignment = |
| 2090 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size; |
Eric Blake | 8b18474 | 2016-06-01 15:10:01 -0600 | [diff] [blame] | 2091 | } else { |
Eric Blake | cf081fc | 2016-06-01 15:10:02 -0600 | [diff] [blame] | 2092 | bs->bl.pwrite_zeroes_alignment = iscsilun->block_size; |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2093 | } |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2094 | if (iscsilun->bl.opt_xfer_len && |
Eric Blake | 95eaa78 | 2016-09-07 16:27:20 -0500 | [diff] [blame] | 2095 | iscsilun->bl.opt_xfer_len < INT_MAX / block_size) { |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2096 | bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len * |
| 2097 | iscsilun->block_size); |
| 2098 | } |
Anthony Liguori | e9f526a | 2014-01-24 15:50:14 -0800 | [diff] [blame] | 2099 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2100 | |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 2101 | /* Note that this will not re-establish a connection with an iSCSI target - it |
| 2102 | * is effectively a NOP. */ |
Jeff Cody | dc6afb9 | 2014-01-14 13:10:24 -0500 | [diff] [blame] | 2103 | static int iscsi_reopen_prepare(BDRVReopenState *state, |
| 2104 | BlockReopenQueue *queue, Error **errp) |
| 2105 | { |
Fam Zheng | 43ae8fb | 2015-02-25 12:40:08 +0800 | [diff] [blame] | 2106 | IscsiLun *iscsilun = state->bs->opaque; |
| 2107 | |
| 2108 | if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { |
| 2109 | error_setg(errp, "Cannot open a write protected LUN as read-write"); |
| 2110 | return -EACCES; |
| 2111 | } |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2112 | return 0; |
| 2113 | } |
| 2114 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 2115 | static void iscsi_reopen_commit(BDRVReopenState *reopen_state) |
| 2116 | { |
| 2117 | IscsiLun *iscsilun = reopen_state->bs->opaque; |
| 2118 | |
| 2119 | /* the cache.direct status might have changed */ |
| 2120 | if (iscsilun->allocmap != NULL) { |
| 2121 | iscsi_allocmap_init(iscsilun, reopen_state->flags); |
| 2122 | } |
| 2123 | } |
| 2124 | |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 2125 | static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset, |
| 2126 | PreallocMode prealloc, Error **errp) |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2127 | { |
| 2128 | IscsiLun *iscsilun = bs->opaque; |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 2129 | Error *local_err = NULL; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2130 | |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 2131 | if (prealloc != PREALLOC_MODE_OFF) { |
| 2132 | error_setg(errp, "Unsupported preallocation mode '%s'", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 2133 | PreallocMode_str(prealloc)); |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 2134 | return -ENOTSUP; |
| 2135 | } |
| 2136 | |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2137 | if (iscsilun->type != TYPE_DISK) { |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 2138 | error_setg(errp, "Cannot resize non-disk iSCSI devices"); |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2139 | return -ENOTSUP; |
| 2140 | } |
| 2141 | |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 2142 | iscsi_readcapacity_sync(iscsilun, &local_err); |
| 2143 | if (local_err != NULL) { |
Max Reitz | 4bff28b | 2017-03-28 22:51:28 +0200 | [diff] [blame] | 2144 | error_propagate(errp, local_err); |
Paolo Bonzini | f291785 | 2014-02-17 14:43:53 +0100 | [diff] [blame] | 2145 | return -EIO; |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | if (offset > iscsi_getlength(bs)) { |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 2149 | error_setg(errp, "Cannot grow iSCSI devices"); |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2150 | return -EINVAL; |
| 2151 | } |
| 2152 | |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 2153 | if (iscsilun->allocmap != NULL) { |
| 2154 | iscsi_allocmap_init(iscsilun, bs->open_flags); |
Peter Lieven | b03c380 | 2014-04-28 13:11:32 +0200 | [diff] [blame] | 2155 | } |
| 2156 | |
Peter Lieven | cb1b83e | 2013-02-18 14:50:46 +0100 | [diff] [blame] | 2157 | return 0; |
| 2158 | } |
| 2159 | |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 2160 | static int coroutine_fn iscsi_co_create_opts(const char *filename, QemuOpts *opts, |
| 2161 | Error **errp) |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2162 | { |
| 2163 | int ret = 0; |
| 2164 | int64_t total_size = 0; |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 2165 | BlockDriverState *bs; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2166 | IscsiLun *iscsilun = NULL; |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 2167 | QDict *bs_options; |
Fam Zheng | 2ec9a78 | 2017-04-10 15:54:51 +0800 | [diff] [blame] | 2168 | Error *local_err = NULL; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2169 | |
Markus Armbruster | e4e9986 | 2014-10-07 13:59:03 +0200 | [diff] [blame] | 2170 | bs = bdrv_new(); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2171 | |
| 2172 | /* Read out options */ |
Hu Tao | c2eb918 | 2014-09-10 17:05:45 +0800 | [diff] [blame] | 2173 | total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 2174 | BDRV_SECTOR_SIZE); |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 2175 | bs->opaque = g_new0(struct IscsiLun, 1); |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 2176 | iscsilun = bs->opaque; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2177 | |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 2178 | bs_options = qdict_new(); |
Fam Zheng | 2ec9a78 | 2017-04-10 15:54:51 +0800 | [diff] [blame] | 2179 | iscsi_parse_filename(filename, bs_options, &local_err); |
| 2180 | if (local_err) { |
| 2181 | error_propagate(errp, local_err); |
| 2182 | ret = -EINVAL; |
| 2183 | } else { |
| 2184 | ret = iscsi_open(bs, bs_options, 0, NULL); |
| 2185 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2186 | qobject_unref(bs_options); |
Kevin Wolf | 60beb34 | 2013-04-12 17:59:59 +0200 | [diff] [blame] | 2187 | |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2188 | if (ret != 0) { |
| 2189 | goto out; |
| 2190 | } |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 2191 | iscsi_detach_aio_context(bs); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2192 | if (iscsilun->type != TYPE_DISK) { |
| 2193 | ret = -ENODEV; |
| 2194 | goto out; |
| 2195 | } |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 2196 | if (bs->total_sectors < total_size) { |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2197 | ret = -ENOSPC; |
Peter Lieven | d3bda7b | 2013-07-11 14:16:24 +0200 | [diff] [blame] | 2198 | goto out; |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | ret = 0; |
| 2202 | out: |
| 2203 | if (iscsilun->iscsi != NULL) { |
| 2204 | iscsi_destroy_context(iscsilun->iscsi); |
| 2205 | } |
Fam Zheng | 13c91cb | 2013-08-23 09:14:45 +0800 | [diff] [blame] | 2206 | g_free(bs->opaque); |
| 2207 | bs->opaque = NULL; |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 2208 | bdrv_unref(bs); |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2209 | return ret; |
| 2210 | } |
| 2211 | |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 2212 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 2213 | { |
| 2214 | IscsiLun *iscsilun = bs->opaque; |
Peter Lieven | 0a386e4 | 2015-04-16 16:08:26 +0200 | [diff] [blame] | 2215 | bdi->unallocated_blocks_are_zero = iscsilun->lbprz; |
Eric Blake | ba059e7 | 2018-02-13 14:26:46 -0600 | [diff] [blame] | 2216 | bdi->cluster_size = iscsilun->cluster_size; |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 2220 | static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs, |
| 2221 | Error **errp) |
Peter Lieven | e1123a3 | 2016-07-18 10:52:20 +0200 | [diff] [blame] | 2222 | { |
| 2223 | IscsiLun *iscsilun = bs->opaque; |
| 2224 | iscsi_allocmap_invalidate(iscsilun); |
| 2225 | } |
| 2226 | |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2227 | static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs, |
| 2228 | BdrvChild *src, |
| 2229 | uint64_t src_offset, |
| 2230 | BdrvChild *dst, |
| 2231 | uint64_t dst_offset, |
| 2232 | uint64_t bytes, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 2233 | BdrvRequestFlags read_flags, |
| 2234 | BdrvRequestFlags write_flags) |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2235 | { |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 2236 | return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, |
| 2237 | read_flags, write_flags); |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | static struct scsi_task *iscsi_xcopy_task(int param_len) |
| 2241 | { |
| 2242 | struct scsi_task *task; |
| 2243 | |
| 2244 | task = g_new0(struct scsi_task, 1); |
| 2245 | |
| 2246 | task->cdb[0] = EXTENDED_COPY; |
| 2247 | task->cdb[10] = (param_len >> 24) & 0xFF; |
| 2248 | task->cdb[11] = (param_len >> 16) & 0xFF; |
| 2249 | task->cdb[12] = (param_len >> 8) & 0xFF; |
| 2250 | task->cdb[13] = param_len & 0xFF; |
| 2251 | task->cdb_size = 16; |
| 2252 | task->xfer_dir = SCSI_XFER_WRITE; |
| 2253 | task->expxferlen = param_len; |
| 2254 | |
| 2255 | return task; |
| 2256 | } |
| 2257 | |
| 2258 | static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun) |
| 2259 | { |
| 2260 | struct scsi_inquiry_device_designator *dd = lun->dd; |
| 2261 | |
| 2262 | memset(desc, 0, 32); |
| 2263 | desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */ |
| 2264 | desc[4] = dd->code_set; |
| 2265 | desc[5] = (dd->designator_type & 0xF) |
| 2266 | | ((dd->association & 3) << 4); |
| 2267 | desc[7] = dd->designator_length; |
Fam Zheng | 1439b9c | 2018-06-29 14:03:27 +0800 | [diff] [blame] | 2268 | memcpy(desc + 8, dd->designator, MIN(dd->designator_length, 20)); |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2269 | |
| 2270 | desc[28] = 0; |
| 2271 | desc[29] = (lun->block_size >> 16) & 0xFF; |
| 2272 | desc[30] = (lun->block_size >> 8) & 0xFF; |
| 2273 | desc[31] = lun->block_size & 0xFF; |
| 2274 | } |
| 2275 | |
| 2276 | static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index, |
| 2277 | int dst_index) |
| 2278 | { |
| 2279 | hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */ |
| 2280 | hdr[1] = ((dc << 1) | cat) & 0xFF; |
| 2281 | hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF; |
| 2282 | /* don't account for the first 4 bytes in descriptor header*/ |
| 2283 | hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF; |
| 2284 | hdr[4] = (src_index >> 8) & 0xFF; |
| 2285 | hdr[5] = src_index & 0xFF; |
| 2286 | hdr[6] = (dst_index >> 8) & 0xFF; |
| 2287 | hdr[7] = dst_index & 0xFF; |
| 2288 | } |
| 2289 | |
| 2290 | static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat, |
| 2291 | int src_index, int dst_index, int num_blks, |
| 2292 | uint64_t src_lba, uint64_t dst_lba) |
| 2293 | { |
| 2294 | iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index); |
| 2295 | |
| 2296 | /* The caller should verify the request size */ |
| 2297 | assert(num_blks < 65536); |
| 2298 | desc[10] = (num_blks >> 8) & 0xFF; |
| 2299 | desc[11] = num_blks & 0xFF; |
| 2300 | desc[12] = (src_lba >> 56) & 0xFF; |
| 2301 | desc[13] = (src_lba >> 48) & 0xFF; |
| 2302 | desc[14] = (src_lba >> 40) & 0xFF; |
| 2303 | desc[15] = (src_lba >> 32) & 0xFF; |
| 2304 | desc[16] = (src_lba >> 24) & 0xFF; |
| 2305 | desc[17] = (src_lba >> 16) & 0xFF; |
| 2306 | desc[18] = (src_lba >> 8) & 0xFF; |
| 2307 | desc[19] = src_lba & 0xFF; |
| 2308 | desc[20] = (dst_lba >> 56) & 0xFF; |
| 2309 | desc[21] = (dst_lba >> 48) & 0xFF; |
| 2310 | desc[22] = (dst_lba >> 40) & 0xFF; |
| 2311 | desc[23] = (dst_lba >> 32) & 0xFF; |
| 2312 | desc[24] = (dst_lba >> 24) & 0xFF; |
| 2313 | desc[25] = (dst_lba >> 16) & 0xFF; |
| 2314 | desc[26] = (dst_lba >> 8) & 0xFF; |
| 2315 | desc[27] = dst_lba & 0xFF; |
| 2316 | } |
| 2317 | |
| 2318 | static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str, |
| 2319 | int list_id_usage, int prio, |
| 2320 | int tgt_desc_len, |
| 2321 | int seg_desc_len, int inline_data_len) |
| 2322 | { |
| 2323 | buf[0] = list_id; |
| 2324 | buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7); |
| 2325 | buf[2] = (tgt_desc_len >> 8) & 0xFF; |
| 2326 | buf[3] = tgt_desc_len & 0xFF; |
| 2327 | buf[8] = (seg_desc_len >> 24) & 0xFF; |
| 2328 | buf[9] = (seg_desc_len >> 16) & 0xFF; |
| 2329 | buf[10] = (seg_desc_len >> 8) & 0xFF; |
| 2330 | buf[11] = seg_desc_len & 0xFF; |
| 2331 | buf[12] = (inline_data_len >> 24) & 0xFF; |
| 2332 | buf[13] = (inline_data_len >> 16) & 0xFF; |
| 2333 | buf[14] = (inline_data_len >> 8) & 0xFF; |
| 2334 | buf[15] = inline_data_len & 0xFF; |
| 2335 | } |
| 2336 | |
| 2337 | static void iscsi_xcopy_data(struct iscsi_data *data, |
| 2338 | IscsiLun *src, int64_t src_lba, |
| 2339 | IscsiLun *dst, int64_t dst_lba, |
| 2340 | uint16_t num_blocks) |
| 2341 | { |
| 2342 | uint8_t *buf; |
| 2343 | const int src_offset = XCOPY_DESC_OFFSET; |
| 2344 | const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE; |
| 2345 | const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE; |
| 2346 | |
| 2347 | data->size = XCOPY_DESC_OFFSET + |
| 2348 | IDENT_DESCR_TGT_DESCR_SIZE * 2 + |
| 2349 | XCOPY_BLK2BLK_SEG_DESC_SIZE; |
| 2350 | data->data = g_malloc0(data->size); |
| 2351 | buf = data->data; |
| 2352 | |
| 2353 | /* Initialise the parameter list header */ |
| 2354 | iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */, |
| 2355 | 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE, |
| 2356 | XCOPY_BLK2BLK_SEG_DESC_SIZE, |
| 2357 | 0); |
| 2358 | |
| 2359 | /* Initialise CSCD list with one src + one dst descriptor */ |
| 2360 | iscsi_populate_target_desc(&buf[src_offset], src); |
| 2361 | iscsi_populate_target_desc(&buf[dst_offset], dst); |
| 2362 | |
| 2363 | /* Initialise one segment descriptor */ |
| 2364 | iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks, |
| 2365 | src_lba, dst_lba); |
| 2366 | } |
| 2367 | |
| 2368 | static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs, |
| 2369 | BdrvChild *src, |
| 2370 | uint64_t src_offset, |
| 2371 | BdrvChild *dst, |
| 2372 | uint64_t dst_offset, |
| 2373 | uint64_t bytes, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 2374 | BdrvRequestFlags read_flags, |
| 2375 | BdrvRequestFlags write_flags) |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2376 | { |
| 2377 | IscsiLun *dst_lun = dst->bs->opaque; |
| 2378 | IscsiLun *src_lun; |
| 2379 | struct IscsiTask iscsi_task; |
| 2380 | struct iscsi_data data; |
| 2381 | int r = 0; |
| 2382 | int block_size; |
| 2383 | |
| 2384 | if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) { |
| 2385 | return -ENOTSUP; |
| 2386 | } |
| 2387 | src_lun = src->bs->opaque; |
| 2388 | |
| 2389 | if (!src_lun->dd || !dst_lun->dd) { |
| 2390 | return -ENOTSUP; |
| 2391 | } |
| 2392 | if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) { |
| 2393 | return -ENOTSUP; |
| 2394 | } |
| 2395 | if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) { |
| 2396 | return -ENOTSUP; |
| 2397 | } |
| 2398 | if (dst_lun->block_size != src_lun->block_size || |
| 2399 | !dst_lun->block_size) { |
| 2400 | return -ENOTSUP; |
| 2401 | } |
| 2402 | |
| 2403 | block_size = dst_lun->block_size; |
| 2404 | if (bytes / block_size > 65535) { |
| 2405 | return -ENOTSUP; |
| 2406 | } |
| 2407 | |
| 2408 | iscsi_xcopy_data(&data, |
| 2409 | src_lun, src_offset / block_size, |
| 2410 | dst_lun, dst_offset / block_size, |
| 2411 | bytes / block_size); |
| 2412 | |
| 2413 | iscsi_co_init_iscsitask(dst_lun, &iscsi_task); |
| 2414 | |
| 2415 | qemu_mutex_lock(&dst_lun->mutex); |
| 2416 | iscsi_task.task = iscsi_xcopy_task(data.size); |
| 2417 | retry: |
| 2418 | if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun, |
| 2419 | iscsi_task.task, iscsi_co_generic_cb, |
| 2420 | &data, |
| 2421 | &iscsi_task) != 0) { |
| 2422 | r = -EIO; |
| 2423 | goto out_unlock; |
| 2424 | } |
| 2425 | |
| 2426 | iscsi_co_wait_for_task(&iscsi_task, dst_lun); |
| 2427 | |
| 2428 | if (iscsi_task.do_retry) { |
| 2429 | iscsi_task.complete = 0; |
| 2430 | goto retry; |
| 2431 | } |
| 2432 | |
| 2433 | if (iscsi_task.status != SCSI_STATUS_GOOD) { |
| 2434 | r = iscsi_task.err_code; |
| 2435 | goto out_unlock; |
| 2436 | } |
| 2437 | |
| 2438 | out_unlock: |
Fam Zheng | ecc983a | 2018-07-10 14:31:16 +0800 | [diff] [blame] | 2439 | |
| 2440 | trace_iscsi_xcopy(src_lun, src_offset, dst_lun, dst_offset, bytes, r); |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2441 | g_free(iscsi_task.task); |
| 2442 | qemu_mutex_unlock(&dst_lun->mutex); |
| 2443 | g_free(iscsi_task.err_str); |
| 2444 | return r; |
| 2445 | } |
| 2446 | |
Chunyan Liu | a59479e | 2014-06-05 17:20:55 +0800 | [diff] [blame] | 2447 | static QemuOptsList iscsi_create_opts = { |
| 2448 | .name = "iscsi-create-opts", |
| 2449 | .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), |
| 2450 | .desc = { |
| 2451 | { |
| 2452 | .name = BLOCK_OPT_SIZE, |
| 2453 | .type = QEMU_OPT_SIZE, |
| 2454 | .help = "Virtual disk size" |
| 2455 | }, |
| 2456 | { /* end of list */ } |
| 2457 | } |
Peter Lieven | de8864e | 2012-11-17 16:13:24 +0100 | [diff] [blame] | 2458 | }; |
| 2459 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 2460 | static const char *const iscsi_strong_runtime_opts[] = { |
| 2461 | "transport", |
| 2462 | "portal", |
| 2463 | "target", |
| 2464 | "user", |
| 2465 | "password", |
| 2466 | "password-secret", |
| 2467 | "lun", |
| 2468 | "initiator-name", |
| 2469 | "header-digest", |
| 2470 | |
| 2471 | NULL |
| 2472 | }; |
| 2473 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2474 | static BlockDriver bdrv_iscsi = { |
| 2475 | .format_name = "iscsi", |
| 2476 | .protocol_name = "iscsi", |
| 2477 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 2478 | .instance_size = sizeof(IscsiLun), |
| 2479 | .bdrv_parse_filename = iscsi_parse_filename, |
| 2480 | .bdrv_file_open = iscsi_open, |
| 2481 | .bdrv_close = iscsi_close, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 2482 | .bdrv_co_create_opts = iscsi_co_create_opts, |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 2483 | .create_opts = &iscsi_create_opts, |
| 2484 | .bdrv_reopen_prepare = iscsi_reopen_prepare, |
| 2485 | .bdrv_reopen_commit = iscsi_reopen_commit, |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 2486 | .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2487 | |
| 2488 | .bdrv_getlength = iscsi_getlength, |
Peter Lieven | 186d4f2 | 2013-10-24 12:06:55 +0200 | [diff] [blame] | 2489 | .bdrv_get_info = iscsi_get_info, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 2490 | .bdrv_co_truncate = iscsi_co_truncate, |
Kevin Wolf | d34682c | 2013-12-11 19:26:16 +0100 | [diff] [blame] | 2491 | .bdrv_refresh_limits = iscsi_refresh_limits, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2492 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 2493 | .bdrv_co_block_status = iscsi_co_block_status, |
Eric Blake | 97c7e85 | 2016-07-15 17:23:01 -0600 | [diff] [blame] | 2494 | .bdrv_co_pdiscard = iscsi_co_pdiscard, |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2495 | .bdrv_co_copy_range_from = iscsi_co_copy_range_from, |
| 2496 | .bdrv_co_copy_range_to = iscsi_co_copy_range_to, |
Eric Blake | 94d047a | 2016-06-01 15:10:05 -0600 | [diff] [blame] | 2497 | .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 2498 | .bdrv_co_readv = iscsi_co_readv, |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 2499 | .bdrv_co_writev = iscsi_co_writev, |
Peter Lieven | 063c337 | 2013-12-05 16:47:17 +0100 | [diff] [blame] | 2500 | .bdrv_co_flush_to_disk = iscsi_co_flush, |
Ronnie Sahlberg | fa6acb0 | 2012-04-24 16:29:04 +1000 | [diff] [blame] | 2501 | |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 2502 | #ifdef __linux__ |
Ronnie Sahlberg | 9839245 | 2012-05-25 21:59:01 +1000 | [diff] [blame] | 2503 | .bdrv_aio_ioctl = iscsi_aio_ioctl, |
| 2504 | #endif |
Stefan Hajnoczi | 80cf625 | 2014-05-08 16:34:42 +0200 | [diff] [blame] | 2505 | |
| 2506 | .bdrv_detach_aio_context = iscsi_detach_aio_context, |
| 2507 | .bdrv_attach_aio_context = iscsi_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 2508 | |
| 2509 | .strong_runtime_opts = iscsi_strong_runtime_opts, |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2510 | }; |
| 2511 | |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2512 | #if LIBISCSI_API_VERSION >= (20160603) |
| 2513 | static BlockDriver bdrv_iser = { |
| 2514 | .format_name = "iser", |
| 2515 | .protocol_name = "iser", |
| 2516 | |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 2517 | .instance_size = sizeof(IscsiLun), |
| 2518 | .bdrv_parse_filename = iscsi_parse_filename, |
| 2519 | .bdrv_file_open = iscsi_open, |
| 2520 | .bdrv_close = iscsi_close, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 2521 | .bdrv_co_create_opts = iscsi_co_create_opts, |
Kevin Wolf | d5895fc | 2016-12-08 14:23:06 +0100 | [diff] [blame] | 2522 | .create_opts = &iscsi_create_opts, |
| 2523 | .bdrv_reopen_prepare = iscsi_reopen_prepare, |
| 2524 | .bdrv_reopen_commit = iscsi_reopen_commit, |
Paolo Bonzini | b16a7cf | 2018-03-15 15:30:56 +0100 | [diff] [blame] | 2525 | .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2526 | |
| 2527 | .bdrv_getlength = iscsi_getlength, |
| 2528 | .bdrv_get_info = iscsi_get_info, |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 2529 | .bdrv_co_truncate = iscsi_co_truncate, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2530 | .bdrv_refresh_limits = iscsi_refresh_limits, |
| 2531 | |
Eric Blake | 92809c3 | 2018-02-13 14:26:48 -0600 | [diff] [blame] | 2532 | .bdrv_co_block_status = iscsi_co_block_status, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2533 | .bdrv_co_pdiscard = iscsi_co_pdiscard, |
Fam Zheng | 604dfaa | 2018-06-01 17:26:46 +0800 | [diff] [blame] | 2534 | .bdrv_co_copy_range_from = iscsi_co_copy_range_from, |
| 2535 | .bdrv_co_copy_range_to = iscsi_co_copy_range_to, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2536 | .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, |
| 2537 | .bdrv_co_readv = iscsi_co_readv, |
Eric Blake | e18a58b | 2018-04-24 17:01:57 -0500 | [diff] [blame] | 2538 | .bdrv_co_writev = iscsi_co_writev, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2539 | .bdrv_co_flush_to_disk = iscsi_co_flush, |
| 2540 | |
| 2541 | #ifdef __linux__ |
| 2542 | .bdrv_aio_ioctl = iscsi_aio_ioctl, |
| 2543 | #endif |
| 2544 | |
| 2545 | .bdrv_detach_aio_context = iscsi_detach_aio_context, |
| 2546 | .bdrv_attach_aio_context = iscsi_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 2547 | |
| 2548 | .strong_runtime_opts = iscsi_strong_runtime_opts, |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2549 | }; |
| 2550 | #endif |
| 2551 | |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2552 | static void iscsi_block_init(void) |
| 2553 | { |
| 2554 | bdrv_register(&bdrv_iscsi); |
Roy Shterman | e0ae498 | 2016-10-09 11:14:56 +0300 | [diff] [blame] | 2555 | #if LIBISCSI_API_VERSION >= (20160603) |
| 2556 | bdrv_register(&bdrv_iser); |
| 2557 | #endif |
Ronnie Sahlberg | c589b24 | 2011-10-25 19:24:24 +1100 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | block_init(iscsi_block_init); |