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