blob: 267f160bf60c0175f85216e778882b8b41607b1d [file] [log] [blame]
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001/*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
Peter Lievenaef172f2017-12-08 12:51:07 +01005 * Copyright (c) 2012-2017 Peter Lieven <pl@kamp.de>
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11006 *
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 Maydell80c71a22016-01-18 18:01:42 +000026#include "qemu/osdep.h"
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110027
28#include <poll.h>
Peter Lievenefc6de02014-05-30 23:36:47 +020029#include <math.h>
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +100030#include <arpa/inet.h>
Markus Armbrustera8d25322019-05-23 16:35:08 +020031#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/config-file.h"
33#include "qemu/error-report.h"
Peter Lievenb03c3802014-04-28 13:11:32 +020034#include "qemu/bitops.h"
35#include "qemu/bitmap.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010036#include "block/block_int.h"
Max Reitz609f45e2018-06-14 21:14:28 +020037#include "block/qdict.h"
Paolo Bonzini08e2c9f2017-08-22 09:23:55 +020038#include "scsi/constants.h"
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -070039#include "qemu/iov.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020040#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010041#include "qemu/option.h"
Fam Zhengcea25272016-09-21 12:27:14 +080042#include "qemu/uuid.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010043#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060044#include "qapi/qapi-commands-misc.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010045#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010046#include "qapi/qmp/qstring.h"
Daniel P. Berrangeb1893462016-01-21 14:19:21 +000047#include "crypto/secret.h"
Paolo Bonzinie5b57282017-08-22 07:08:27 +020048#include "scsi/utils.h"
Fam Zhengecc983a2018-07-10 14:31:16 +080049#include "trace.h"
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110050
Paolo Bonzinie5b57282017-08-22 07:08:27 +020051/* Conflict between scsi/utils.h and libiscsi! :( */
52#define SCSI_XFER_NONE ISCSI_XFER_NONE
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110053#include <iscsi/iscsi.h>
Thomas Huthdf71ca82019-01-14 14:37:20 +010054#define inline __attribute__((gnu_inline)) /* required for libiscsi v1.9.0 */
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110055#include <iscsi/scsi-lowlevel.h>
Thomas Huthdf71ca82019-01-14 14:37:20 +010056#undef inline
Paolo Bonzinie5b57282017-08-22 07:08:27 +020057#undef SCSI_XFER_NONE
58QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110059
Ronnie Sahlberg98392452012-05-25 21:59:01 +100060#ifdef __linux__
61#include <scsi/sg.h>
Ronnie Sahlberg98392452012-05-25 21:59:01 +100062#endif
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110063
64typedef struct IscsiLun {
65 struct iscsi_context *iscsi;
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +020066 AioContext *aio_context;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110067 int lun;
Ronnie Sahlbergdbfff6d2012-05-25 21:59:01 +100068 enum scsi_inquiry_peripheral_device_type type;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +110069 int block_size;
Paolo Bonzinic7b4a952012-05-26 09:41:13 +020070 uint64_t num_blocks;
Ronnie Sahlbergc9b9f682012-05-22 19:56:36 +100071 int events;
Peter Lieven5b5d34e2012-12-06 10:46:47 +010072 QEMUTimer *nop_timer;
Peter Lieven05b685f2015-04-07 22:08:15 +020073 QEMUTimer *event_timer;
Paolo Bonzinid045c462017-02-22 19:07:25 +010074 QemuMutex mutex;
Peter Lievenf18a7cb2013-07-19 09:19:39 +020075 struct scsi_inquiry_logical_block_provisioning lbp;
76 struct scsi_inquiry_block_limits bl;
Fam Zhengcc9743c2018-06-01 17:26:44 +080077 struct scsi_inquiry_device_designator *dd;
Peter Lievend4cd9612013-10-24 12:07:02 +020078 unsigned char *zeroblock;
Peter Lievene1123a32016-07-18 10:52:20 +020079 /* The allocmap tracks which clusters (pages) on the iSCSI target are
80 * allocated and which are not. In case a target returns zeros for
81 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
82 * of reading zeros over the wire if a read request falls within an
83 * unallocated block. As there are 3 possible states we need 2 bitmaps to
84 * track. allocmap_valid keeps track if QEMU's information about a page is
85 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
86 * valid information about a page the corresponding allocmap entry should be
87 * switched to unallocated as well to force a new lookup of the allocation
88 * status as lookups are generally skipped if a page is suspect to be
89 * allocated. If a iSCSI target is opened with cache.direct = on the
90 * allocmap_valid does not exist turning all cached information invalid so
91 * that a fresh lookup is made for any page even if allocmap entry returns
92 * it's unallocated. */
93 unsigned long *allocmap;
94 unsigned long *allocmap_valid;
95 long allocmap_size;
Eric Blakeba059e72018-02-13 14:26:46 -060096 int cluster_size;
Peter Lieven9281fe92014-06-04 15:47:39 +020097 bool use_16_for_rw;
Fam Zheng43ae8fb2015-02-25 12:40:08 +080098 bool write_protected;
Peter Lieven0a386e42015-04-16 16:08:26 +020099 bool lbpme;
100 bool lbprz;
Peter Lieven752ce452015-04-16 16:08:28 +0200101 bool dpofua;
Peter Lieven0a386e42015-04-16 16:08:26 +0200102 bool has_write_same;
Peter Lieven5dd7a532015-06-16 13:45:07 +0200103 bool request_timed_out;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100104} IscsiLun;
105
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200106typedef struct IscsiTask {
107 int status;
108 int complete;
109 int retries;
110 int do_retry;
111 struct scsi_task *task;
112 Coroutine *co;
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +0200113 IscsiLun *iscsilun;
Peter Lievenefc6de02014-05-30 23:36:47 +0200114 QEMUTimer retry_timer;
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800115 int err_code;
Peter Lievene38bc232017-12-08 12:51:08 +0100116 char *err_str;
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200117} IscsiTask;
118
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100119typedef struct IscsiAIOCB {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200120 BlockAIOCB common;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100121 QEMUBH *bh;
122 IscsiLun *iscsilun;
123 struct scsi_task *task;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100124 int status;
Peter Lieven1dde7162013-02-21 16:15:54 +0100125 int64_t sector_num;
126 int nb_sectors;
Fam Zheng4bb17ab2015-11-09 18:16:49 +0800127 int ret;
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000128#ifdef __linux__
129 sg_io_hdr_t *ioh;
130#endif
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100131 bool cancelled;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100132} IscsiAIOCB;
133
Peter Lieven5dd7a532015-06-16 13:45:07 +0200134/* libiscsi uses time_t so its enough to process events every second */
135#define EVENT_INTERVAL 1000
Peter Lieven5b5d34e2012-12-06 10:46:47 +0100136#define NOP_INTERVAL 5000
137#define MAX_NOP_FAILURES 3
Peter Lievenefc6de02014-05-30 23:36:47 +0200138#define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
Peter Lieven59dd0a22015-04-16 16:08:30 +0200139static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
Peter Lieven5b5d34e2012-12-06 10:46:47 +0100140
Stefan Weil5d831be2014-06-13 20:42:57 +0200141/* this threshold is a trade-off knob to choose between
Peter Lieven5917af82014-04-28 17:11:33 +0200142 * the potential additional overhead of an extra GET_LBA_STATUS request
143 * vs. unnecessarily reading a lot of zero sectors over the wire.
144 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
145 * sectors we check the allocation status of the area covered by the
146 * request first if the allocationmap indicates that the area might be
147 * unallocated. */
148#define ISCSI_CHECKALLOC_THRES 64
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100149
Philippe Mathieu-Daudéd4cef0c2019-02-20 01:05:53 +0100150#ifdef __linux__
151
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100152static void
Paolo Bonzinicfb3f502012-08-18 23:38:03 +0200153iscsi_bh_cb(void *p)
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200154{
155 IscsiAIOCB *acb = p;
156
157 qemu_bh_delete(acb->bh);
158
Fam Zheng722d9332014-09-11 13:41:15 +0800159 acb->common.cb(acb->common.opaque, acb->status);
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200160
Paolo Bonzini1bd075f2012-08-18 23:35:49 +0200161 if (acb->task != NULL) {
162 scsi_free_scsi_task(acb->task);
163 acb->task = NULL;
164 }
165
Fam Zheng80074292014-09-11 13:41:28 +0800166 qemu_aio_unref(acb);
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200167}
168
Paolo Bonzinicfb3f502012-08-18 23:38:03 +0200169static void
170iscsi_schedule_bh(IscsiAIOCB *acb)
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200171{
Paolo Bonzini1bd075f2012-08-18 23:35:49 +0200172 if (acb->bh) {
173 return;
174 }
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +0200175 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200176 qemu_bh_schedule(acb->bh);
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200177}
178
Philippe Mathieu-Daudéd4cef0c2019-02-20 01:05:53 +0100179#endif
180
Peter Lieven8b9dfe92013-12-14 17:31:40 +0100181static void iscsi_co_generic_bh_cb(void *opaque)
182{
183 struct IscsiTask *iTask = opaque;
Paolo Bonzini19196312017-02-13 14:52:31 +0100184
Peter Lievenfcd470d2014-06-10 09:52:16 +0200185 iTask->complete = 1;
Paolo Bonzini19196312017-02-13 14:52:31 +0100186 aio_co_wake(iTask->co);
Peter Lieven8b9dfe92013-12-14 17:31:40 +0100187}
188
Peter Lievenefc6de02014-05-30 23:36:47 +0200189static void iscsi_retry_timer_expired(void *opaque)
190{
191 struct IscsiTask *iTask = opaque;
Peter Lievenfcd470d2014-06-10 09:52:16 +0200192 iTask->complete = 1;
Peter Lievenefc6de02014-05-30 23:36:47 +0200193 if (iTask->co) {
Paolo Bonzini2f47da52017-02-13 14:52:29 +0100194 aio_co_wake(iTask->co);
Peter Lievenefc6de02014-05-30 23:36:47 +0200195 }
196}
197
198static inline unsigned exp_random(double mean)
199{
200 return -mean * log((double)rand() / RAND_MAX);
201}
202
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800203/* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
204 * libiscsi 1.10.0, together with other constants we need. Use it as
205 * a hint that we have to define them ourselves if needed, to keep the
206 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
207 * the test because SCSI_STATUS_* is an enum.
208 *
209 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
210 * an enum, check against the LIBISCSI_API_VERSION macro, which was
211 * introduced in 1.11.0. If it is present, there is no need to define
212 * anything.
213 */
214#if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
215 !defined(LIBISCSI_API_VERSION)
216#define SCSI_STATUS_TASK_SET_FULL 0x28
217#define SCSI_STATUS_TIMEOUT 0x0f000002
218#define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
219#define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
Peter Lieven90497362015-06-26 12:18:01 +0200220#endif
221
Roy Shterman583ec222016-10-09 11:14:55 +0300222#ifndef LIBISCSI_API_VERSION
223#define LIBISCSI_API_VERSION 20130701
224#endif
225
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800226static int iscsi_translate_sense(struct scsi_sense *sense)
227{
Fam Zheng28751352017-08-21 22:10:05 +0800228 return - scsi_sense_to_errno(sense->key,
229 (sense->ascq & 0xFF00) >> 8,
230 sense->ascq & 0xFF);
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800231}
232
Paolo Bonzinid045c462017-02-22 19:07:25 +0100233/* Called (via iscsi_service) with QemuMutex held. */
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200234static void
235iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
236 void *command_data, void *opaque)
237{
238 struct IscsiTask *iTask = opaque;
239 struct scsi_task *task = command_data;
240
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200241 iTask->status = status;
242 iTask->do_retry = 0;
243 iTask->task = task;
244
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200245 if (status != SCSI_STATUS_GOOD) {
Peter Lievenefc6de02014-05-30 23:36:47 +0200246 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
247 if (status == SCSI_STATUS_CHECK_CONDITION
248 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
249 error_report("iSCSI CheckCondition: %s",
250 iscsi_get_error(iscsi));
251 iTask->do_retry = 1;
252 goto out;
253 }
Peter Lieven90497362015-06-26 12:18:01 +0200254 if (status == SCSI_STATUS_BUSY ||
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800255 status == SCSI_STATUS_TIMEOUT ||
256 status == SCSI_STATUS_TASK_SET_FULL) {
Peter Lievenefc6de02014-05-30 23:36:47 +0200257 unsigned retry_time =
258 exp_random(iscsi_retry_times[iTask->retries - 1]);
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800259 if (status == SCSI_STATUS_TIMEOUT) {
Peter Lieven5dd7a532015-06-16 13:45:07 +0200260 /* make sure the request is rescheduled AFTER the
261 * reconnect is initiated */
262 retry_time = EVENT_INTERVAL * 2;
263 iTask->iscsilun->request_timed_out = true;
264 }
265 error_report("iSCSI Busy/TaskSetFull/TimeOut"
266 " (retry #%u in %u ms): %s",
Peter Lievenefc6de02014-05-30 23:36:47 +0200267 iTask->retries, retry_time,
268 iscsi_get_error(iscsi));
269 aio_timer_init(iTask->iscsilun->aio_context,
270 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
271 SCALE_MS, iscsi_retry_timer_expired, iTask);
272 timer_mod(&iTask->retry_timer,
273 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
274 iTask->do_retry = 1;
275 return;
276 }
277 }
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800278 iTask->err_code = iscsi_translate_sense(&task->sense);
Peter Lievene38bc232017-12-08 12:51:08 +0100279 iTask->err_str = g_strdup(iscsi_get_error(iscsi));
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200280 }
281
282out:
283 if (iTask->co) {
Paolo Bonzinifffb6e12016-10-03 18:14:16 +0200284 aio_bh_schedule_oneshot(iTask->iscsilun->aio_context,
285 iscsi_co_generic_bh_cb, iTask);
Peter Lievenfcd470d2014-06-10 09:52:16 +0200286 } else {
287 iTask->complete = 1;
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200288 }
289}
290
291static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
292{
293 *iTask = (struct IscsiTask) {
Peter Lievenefc6de02014-05-30 23:36:47 +0200294 .co = qemu_coroutine_self(),
Peter Lievenefc6de02014-05-30 23:36:47 +0200295 .iscsilun = iscsilun,
Peter Lieven54a5c1d2013-07-19 09:19:40 +0200296 };
297}
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200298
Philippe Mathieu-Daudéd4cef0c2019-02-20 01:05:53 +0100299#ifdef __linux__
300
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100301/* Called (via iscsi_service) with QemuMutex held. */
Paolo Bonzini27cbd822012-08-18 23:37:31 +0200302static void
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100303iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
304 void *private_data)
305{
Paolo Bonzini1bd075f2012-08-18 23:35:49 +0200306 IscsiAIOCB *acb = private_data;
307
Stefan Hajnoczi88e94fd2018-02-15 11:15:26 +0000308 /* If the command callback hasn't been called yet, drop the task */
309 if (!acb->bh) {
310 /* Call iscsi_aio_ioctl_cb() with SCSI_STATUS_CANCELLED */
311 iscsi_scsi_cancel_task(iscsi, acb->task);
312 }
313
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100314 qemu_aio_unref(acb); /* acquired in iscsi_aio_cancel() */
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100315}
316
317static void
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200318iscsi_aio_cancel(BlockAIOCB *blockacb)
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100319{
320 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
321 IscsiLun *iscsilun = acb->iscsilun;
322
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100323 qemu_mutex_lock(&iscsilun->mutex);
324
325 /* If it was cancelled or completed already, our work is done here */
326 if (acb->cancelled || acb->status != -EINPROGRESS) {
327 qemu_mutex_unlock(&iscsilun->mutex);
Paolo Bonzini1bd075f2012-08-18 23:35:49 +0200328 return;
329 }
330
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100331 acb->cancelled = true;
Paolo Bonzinib2090912012-08-19 00:12:39 +0200332
Stefan Hajnoczic1004482018-02-03 07:16:21 +0100333 qemu_aio_ref(acb); /* released in iscsi_abort_task_cb() */
334
335 /* send a task mgmt call to the target to cancel the task on the target */
336 if (iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
337 iscsi_abort_task_cb, acb) < 0) {
338 qemu_aio_unref(acb); /* since iscsi_abort_task_cb() won't be called */
339 }
340
341 qemu_mutex_unlock(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100342}
343
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100344static const AIOCBInfo iscsi_aiocb_info = {
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100345 .aiocb_size = sizeof(IscsiAIOCB),
Fam Zheng722d9332014-09-11 13:41:15 +0800346 .cancel_async = iscsi_aio_cancel,
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100347};
348
Philippe Mathieu-Daudéd4cef0c2019-02-20 01:05:53 +0100349#endif
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100350
351static void iscsi_process_read(void *arg);
352static void iscsi_process_write(void *arg);
353
Paolo Bonzinid045c462017-02-22 19:07:25 +0100354/* Called with QemuMutex held. */
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100355static void
356iscsi_set_events(IscsiLun *iscsilun)
357{
358 struct iscsi_context *iscsi = iscsilun->iscsi;
Peter Lieven05b685f2015-04-07 22:08:15 +0200359 int ev = iscsi_which_events(iscsi);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100360
Ronnie Sahlbergc9b9f682012-05-22 19:56:36 +1000361 if (ev != iscsilun->events) {
Fam Zhengdca21ef2015-10-23 11:08:05 +0800362 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
363 false,
Peter Lieven05b685f2015-04-07 22:08:15 +0200364 (ev & POLLIN) ? iscsi_process_read : NULL,
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +0200365 (ev & POLLOUT) ? iscsi_process_write : NULL,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000366 NULL,
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +0200367 iscsilun);
Peter Lieven05b685f2015-04-07 22:08:15 +0200368 iscsilun->events = ev;
Ronnie Sahlbergc9b9f682012-05-22 19:56:36 +1000369 }
Peter Lieven05b685f2015-04-07 22:08:15 +0200370}
371
Peter Lieven5dd7a532015-06-16 13:45:07 +0200372static void iscsi_timed_check_events(void *opaque)
Peter Lieven05b685f2015-04-07 22:08:15 +0200373{
374 IscsiLun *iscsilun = opaque;
Peter Lieven5dd7a532015-06-16 13:45:07 +0200375
Stefan Hajnoczi83d11972018-02-03 07:16:20 +0100376 qemu_mutex_lock(&iscsilun->mutex);
377
Peter Lieven5dd7a532015-06-16 13:45:07 +0200378 /* check for timed out requests */
379 iscsi_service(iscsilun->iscsi, 0);
380
381 if (iscsilun->request_timed_out) {
382 iscsilun->request_timed_out = false;
383 iscsi_reconnect(iscsilun->iscsi);
384 }
385
386 /* newer versions of libiscsi may return zero events. Ensure we are able
387 * to return to service once this situation changes. */
Peter Lieven05b685f2015-04-07 22:08:15 +0200388 iscsi_set_events(iscsilun);
Peter Lieven5dd7a532015-06-16 13:45:07 +0200389
Stefan Hajnoczi83d11972018-02-03 07:16:20 +0100390 qemu_mutex_unlock(&iscsilun->mutex);
391
Peter Lieven5dd7a532015-06-16 13:45:07 +0200392 timer_mod(iscsilun->event_timer,
393 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100394}
395
396static void
397iscsi_process_read(void *arg)
398{
399 IscsiLun *iscsilun = arg;
400 struct iscsi_context *iscsi = iscsilun->iscsi;
401
Paolo Bonzinid045c462017-02-22 19:07:25 +0100402 qemu_mutex_lock(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100403 iscsi_service(iscsi, POLLIN);
404 iscsi_set_events(iscsilun);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100405 qemu_mutex_unlock(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100406}
407
408static void
409iscsi_process_write(void *arg)
410{
411 IscsiLun *iscsilun = arg;
412 struct iscsi_context *iscsi = iscsilun->iscsi;
413
Paolo Bonzinid045c462017-02-22 19:07:25 +0100414 qemu_mutex_lock(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100415 iscsi_service(iscsi, POLLOUT);
416 iscsi_set_events(iscsilun);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100417 qemu_mutex_unlock(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100418}
419
Peter Lieven0777b5d2013-07-11 14:16:25 +0200420static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
421{
422 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
423}
424
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100425static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
426{
427 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
428}
429
Eric Blake94d047a2016-06-01 15:10:05 -0600430static bool is_byte_request_lun_aligned(int64_t offset, int count,
431 IscsiLun *iscsilun)
Peter Lieven91bea4e2013-07-11 14:16:27 +0200432{
Eric Blake94d047a2016-06-01 15:10:05 -0600433 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
434 error_report("iSCSI misaligned request: "
435 "iscsilun->block_size %u, offset %" PRIi64
436 ", count %d",
437 iscsilun->block_size, offset, count);
438 return false;
Peter Lieven91bea4e2013-07-11 14:16:27 +0200439 }
Eric Blake94d047a2016-06-01 15:10:05 -0600440 return true;
441}
442
443static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
444 IscsiLun *iscsilun)
445{
Peter Lieven0ead9312016-06-20 11:24:40 +0200446 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS);
Eric Blake94d047a2016-06-01 15:10:05 -0600447 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS,
448 nb_sectors << BDRV_SECTOR_BITS,
449 iscsilun);
Peter Lieven91bea4e2013-07-11 14:16:27 +0200450}
451
Peter Lievene1123a32016-07-18 10:52:20 +0200452static void iscsi_allocmap_free(IscsiLun *iscsilun)
Peter Lievena9fe4c92014-09-30 09:09:12 +0200453{
Peter Lievene1123a32016-07-18 10:52:20 +0200454 g_free(iscsilun->allocmap);
455 g_free(iscsilun->allocmap_valid);
456 iscsilun->allocmap = NULL;
457 iscsilun->allocmap_valid = NULL;
Peter Lievena9fe4c92014-09-30 09:09:12 +0200458}
459
Peter Lievene1123a32016-07-18 10:52:20 +0200460
461static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags)
Peter Lievenb03c3802014-04-28 13:11:32 +0200462{
Peter Lievene1123a32016-07-18 10:52:20 +0200463 iscsi_allocmap_free(iscsilun);
464
Eric Blakeba059e72018-02-13 14:26:46 -0600465 assert(iscsilun->cluster_size);
Peter Lievene1123a32016-07-18 10:52:20 +0200466 iscsilun->allocmap_size =
Eric Blakeba059e72018-02-13 14:26:46 -0600467 DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size,
468 iscsilun->cluster_size);
Peter Lievene1123a32016-07-18 10:52:20 +0200469
470 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
471 if (!iscsilun->allocmap) {
472 return -ENOMEM;
Peter Lievenb03c3802014-04-28 13:11:32 +0200473 }
Peter Lievene1123a32016-07-18 10:52:20 +0200474
475 if (open_flags & BDRV_O_NOCACHE) {
Eric Blakeba059e72018-02-13 14:26:46 -0600476 /* when cache.direct = on all allocmap entries are
Peter Lievene1123a32016-07-18 10:52:20 +0200477 * treated as invalid to force a relookup of the block
478 * status on every read request */
479 return 0;
480 }
481
482 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
483 if (!iscsilun->allocmap_valid) {
484 /* if we are under memory pressure free the allocmap as well */
485 iscsi_allocmap_free(iscsilun);
486 return -ENOMEM;
487 }
488
489 return 0;
Peter Lievenb03c3802014-04-28 13:11:32 +0200490}
491
Peter Lievene1123a32016-07-18 10:52:20 +0200492static void
Eric Blake04a408f2018-02-13 14:26:47 -0600493iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset,
494 int64_t bytes, bool allocated, bool valid)
Peter Lievenb03c3802014-04-28 13:11:32 +0200495{
Peter Lievene1123a32016-07-18 10:52:20 +0200496 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
497
498 if (iscsilun->allocmap == NULL) {
Peter Lievenb03c3802014-04-28 13:11:32 +0200499 return;
500 }
Peter Lievene1123a32016-07-18 10:52:20 +0200501 /* expand to entirely contain all affected clusters */
Eric Blake04a408f2018-02-13 14:26:47 -0600502 assert(iscsilun->cluster_size);
503 cl_num_expanded = offset / iscsilun->cluster_size;
504 nb_cls_expanded = DIV_ROUND_UP(offset + bytes,
505 iscsilun->cluster_size) - cl_num_expanded;
Peter Lievene1123a32016-07-18 10:52:20 +0200506 /* shrink to touch only completely contained clusters */
Eric Blake04a408f2018-02-13 14:26:47 -0600507 cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size);
508 nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk;
Peter Lievene1123a32016-07-18 10:52:20 +0200509 if (allocated) {
510 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
511 } else {
Peter Lieven1da45e02017-01-16 16:17:12 +0100512 if (nb_cls_shrunk > 0) {
513 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
514 }
Peter Lievenb03c3802014-04-28 13:11:32 +0200515 }
Peter Lievene1123a32016-07-18 10:52:20 +0200516
517 if (iscsilun->allocmap_valid == NULL) {
518 return;
519 }
520 if (valid) {
Peter Lieven1da45e02017-01-16 16:17:12 +0100521 if (nb_cls_shrunk > 0) {
522 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
523 }
Peter Lievene1123a32016-07-18 10:52:20 +0200524 } else {
525 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
526 nb_cls_expanded);
527 }
528}
529
530static void
Eric Blake04a408f2018-02-13 14:26:47 -0600531iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset,
532 int64_t bytes)
Peter Lievene1123a32016-07-18 10:52:20 +0200533{
Eric Blake04a408f2018-02-13 14:26:47 -0600534 iscsi_allocmap_update(iscsilun, offset, bytes, true, true);
Peter Lievene1123a32016-07-18 10:52:20 +0200535}
536
537static void
Eric Blake04a408f2018-02-13 14:26:47 -0600538iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset,
539 int64_t bytes)
Peter Lievene1123a32016-07-18 10:52:20 +0200540{
541 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
542 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
543 */
Eric Blake04a408f2018-02-13 14:26:47 -0600544 iscsi_allocmap_update(iscsilun, offset, bytes, false, true);
Peter Lievene1123a32016-07-18 10:52:20 +0200545}
546
Eric Blake04a408f2018-02-13 14:26:47 -0600547static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset,
548 int64_t bytes)
Peter Lievene1123a32016-07-18 10:52:20 +0200549{
Eric Blake04a408f2018-02-13 14:26:47 -0600550 iscsi_allocmap_update(iscsilun, offset, bytes, false, false);
Peter Lievene1123a32016-07-18 10:52:20 +0200551}
552
553static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
554{
555 if (iscsilun->allocmap) {
556 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
557 }
558 if (iscsilun->allocmap_valid) {
559 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
560 }
561}
562
563static inline bool
Eric Blake04a408f2018-02-13 14:26:47 -0600564iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset,
565 int64_t bytes)
Peter Lievene1123a32016-07-18 10:52:20 +0200566{
567 unsigned long size;
568 if (iscsilun->allocmap == NULL) {
569 return true;
570 }
Eric Blakeba059e72018-02-13 14:26:46 -0600571 assert(iscsilun->cluster_size);
Eric Blake04a408f2018-02-13 14:26:47 -0600572 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
Peter Lievene1123a32016-07-18 10:52:20 +0200573 return !(find_next_bit(iscsilun->allocmap, size,
Eric Blake04a408f2018-02-13 14:26:47 -0600574 offset / iscsilun->cluster_size) == size);
Peter Lievene1123a32016-07-18 10:52:20 +0200575}
576
577static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
Eric Blake04a408f2018-02-13 14:26:47 -0600578 int64_t offset, int64_t bytes)
Peter Lievene1123a32016-07-18 10:52:20 +0200579{
580 unsigned long size;
581 if (iscsilun->allocmap_valid == NULL) {
582 return false;
583 }
Eric Blakeba059e72018-02-13 14:26:46 -0600584 assert(iscsilun->cluster_size);
Eric Blake04a408f2018-02-13 14:26:47 -0600585 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
Peter Lievene1123a32016-07-18 10:52:20 +0200586 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
Eric Blake04a408f2018-02-13 14:26:47 -0600587 offset / iscsilun->cluster_size) == size);
Peter Lievenb03c3802014-04-28 13:11:32 +0200588}
589
Fam Zheng66e75c02018-06-01 17:26:45 +0800590static void coroutine_fn iscsi_co_wait_for_task(IscsiTask *iTask,
591 IscsiLun *iscsilun)
592{
593 while (!iTask->complete) {
594 iscsi_set_events(iscsilun);
595 qemu_mutex_unlock(&iscsilun->mutex);
596 qemu_coroutine_yield();
597 qemu_mutex_lock(&iscsilun->mutex);
598 }
599}
600
Kevin Wolf9f0eb9e2016-03-10 13:55:50 +0100601static int coroutine_fn
Eric Blakee18a58b2018-04-24 17:01:57 -0500602iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
603 QEMUIOVector *iov, int flags)
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100604{
Peter Lieven063c3372013-12-05 16:47:17 +0100605 IscsiLun *iscsilun = bs->opaque;
606 struct IscsiTask iTask;
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000607 uint64_t lba;
Peter Lieven063c3372013-12-05 16:47:17 +0100608 uint32_t num_sectors;
Eric Blake4df863f2016-05-03 16:39:06 -0600609 bool fua = flags & BDRV_REQ_FUA;
Paolo Bonzinid045c462017-02-22 19:07:25 +0100610 int r = 0;
Peter Lieven063c3372013-12-05 16:47:17 +0100611
Eric Blake4df863f2016-05-03 16:39:06 -0600612 if (fua) {
613 assert(iscsilun->dpofua);
614 }
Eric Blake94d047a2016-06-01 15:10:05 -0600615 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
Peter Lieven063c3372013-12-05 16:47:17 +0100616 return -EINVAL;
617 }
618
Eric Blake6bd01f12016-07-15 12:32:04 -0600619 if (bs->bl.max_transfer) {
620 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
Peter Lievendc9e7162014-10-27 10:18:48 +0100621 }
622
Peter Lieven063c3372013-12-05 16:47:17 +0100623 lba = sector_qemu2lun(sector_num, iscsilun);
624 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
Peter Lieven063c3372013-12-05 16:47:17 +0100625 iscsi_co_init_iscsitask(iscsilun, &iTask);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100626 qemu_mutex_lock(&iscsilun->mutex);
Peter Lieven063c3372013-12-05 16:47:17 +0100627retry:
Peter Lieven9281fe92014-06-04 15:47:39 +0200628 if (iscsilun->use_16_for_rw) {
Roy Shterman583ec222016-10-09 11:14:55 +0300629#if LIBISCSI_API_VERSION >= (20160603)
630 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
631 NULL, num_sectors * iscsilun->block_size,
632 iscsilun->block_size, 0, 0, fua, 0, 0,
633 iscsi_co_generic_cb, &iTask,
634 (struct scsi_iovec *)iov->iov, iov->niov);
635 } else {
636 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
637 NULL, num_sectors * iscsilun->block_size,
638 iscsilun->block_size, 0, 0, fua, 0, 0,
639 iscsi_co_generic_cb, &iTask,
640 (struct scsi_iovec *)iov->iov, iov->niov);
641 }
642#else
Peter Lieven9281fe92014-06-04 15:47:39 +0200643 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
Peter Lieven8c215a92014-06-18 18:40:22 +0200644 NULL, num_sectors * iscsilun->block_size,
Peter Lieven73b53942015-04-16 16:08:29 +0200645 iscsilun->block_size, 0, 0, fua, 0, 0,
Peter Lieven9281fe92014-06-04 15:47:39 +0200646 iscsi_co_generic_cb, &iTask);
647 } else {
648 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
Peter Lieven8c215a92014-06-18 18:40:22 +0200649 NULL, num_sectors * iscsilun->block_size,
Peter Lieven73b53942015-04-16 16:08:29 +0200650 iscsilun->block_size, 0, 0, fua, 0, 0,
Peter Lieven9281fe92014-06-04 15:47:39 +0200651 iscsi_co_generic_cb, &iTask);
652 }
Roy Shterman583ec222016-10-09 11:14:55 +0300653#endif
Peter Lieven063c3372013-12-05 16:47:17 +0100654 if (iTask.task == NULL) {
Paolo Bonzinif6eb0b32017-03-03 16:23:36 +0100655 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lieven92397112013-12-20 10:02:47 +0100656 return -ENOMEM;
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000657 }
Roy Shterman583ec222016-10-09 11:14:55 +0300658#if LIBISCSI_API_VERSION < (20160603)
Peter Lieven063c3372013-12-05 16:47:17 +0100659 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
660 iov->niov);
Roy Shterman583ec222016-10-09 11:14:55 +0300661#endif
Fam Zheng66e75c02018-06-01 17:26:45 +0800662 iscsi_co_wait_for_task(&iTask, iscsilun);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100663
Peter Lieven063c3372013-12-05 16:47:17 +0100664 if (iTask.task != NULL) {
665 scsi_free_scsi_task(iTask.task);
666 iTask.task = NULL;
667 }
668
669 if (iTask.do_retry) {
Peter Lieven837c3902014-02-18 13:08:39 +0100670 iTask.complete = 0;
Peter Lieven063c3372013-12-05 16:47:17 +0100671 goto retry;
672 }
673
Peter Lieven063c3372013-12-05 16:47:17 +0100674 if (iTask.status != SCSI_STATUS_GOOD) {
Eric Blake04a408f2018-02-13 14:26:47 -0600675 iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
676 nb_sectors * BDRV_SECTOR_SIZE);
Peter Lievene38bc232017-12-08 12:51:08 +0100677 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba,
678 iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100679 r = iTask.err_code;
680 goto out_unlock;
Peter Lieven063c3372013-12-05 16:47:17 +0100681 }
Peter Lieven7371d562012-12-03 20:35:15 +0100682
Eric Blake04a408f2018-02-13 14:26:47 -0600683 iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
684 nb_sectors * BDRV_SECTOR_SIZE);
Peter Lievenb03c3802014-04-28 13:11:32 +0200685
Paolo Bonzinid045c462017-02-22 19:07:25 +0100686out_unlock:
687 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lievene38bc232017-12-08 12:51:08 +0100688 g_free(iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100689 return r;
Peter Lieven1dde7162013-02-21 16:15:54 +0100690}
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100691
Peter Lievenb03c3802014-04-28 13:11:32 +0200692
Peter Lievenb03c3802014-04-28 13:11:32 +0200693
Eric Blake92809c32018-02-13 14:26:48 -0600694static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs,
695 bool want_zero, int64_t offset,
696 int64_t bytes, int64_t *pnum,
697 int64_t *map,
698 BlockDriverState **file)
Peter Lievenb03c3802014-04-28 13:11:32 +0200699{
700 IscsiLun *iscsilun = bs->opaque;
701 struct scsi_get_lba_status *lbas = NULL;
702 struct scsi_lba_status_descriptor *lbasd = NULL;
703 struct IscsiTask iTask;
Peter Lievene38bc232017-12-08 12:51:08 +0100704 uint64_t lba;
Eric Blake92809c32018-02-13 14:26:48 -0600705 int ret;
Peter Lievenb03c3802014-04-28 13:11:32 +0200706
Peter Lieven79f9c752018-01-08 16:27:27 +0100707 iscsi_co_init_iscsitask(iscsilun, &iTask);
708
Eric Blake92809c32018-02-13 14:26:48 -0600709 assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size));
Peter Lievenb03c3802014-04-28 13:11:32 +0200710
711 /* default to all sectors allocated */
Eric Blake92809c32018-02-13 14:26:48 -0600712 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
713 if (map) {
714 *map = offset;
715 }
716 *pnum = bytes;
Peter Lievenb03c3802014-04-28 13:11:32 +0200717
718 /* LUN does not support logical block provisioning */
Peter Lieven0a386e42015-04-16 16:08:26 +0200719 if (!iscsilun->lbpme) {
Peter Lievenb03c3802014-04-28 13:11:32 +0200720 goto out;
721 }
722
Eric Blake92809c32018-02-13 14:26:48 -0600723 lba = offset / iscsilun->block_size;
Peter Lievene38bc232017-12-08 12:51:08 +0100724
Paolo Bonzinid045c462017-02-22 19:07:25 +0100725 qemu_mutex_lock(&iscsilun->mutex);
Peter Lievenb03c3802014-04-28 13:11:32 +0200726retry:
727 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
Peter Lievene38bc232017-12-08 12:51:08 +0100728 lba, 8 + 16, iscsi_co_generic_cb,
Peter Lievenb03c3802014-04-28 13:11:32 +0200729 &iTask) == NULL) {
730 ret = -ENOMEM;
Paolo Bonzinid045c462017-02-22 19:07:25 +0100731 goto out_unlock;
Peter Lievenb03c3802014-04-28 13:11:32 +0200732 }
Fam Zheng66e75c02018-06-01 17:26:45 +0800733 iscsi_co_wait_for_task(&iTask, iscsilun);
Peter Lievenb03c3802014-04-28 13:11:32 +0200734
735 if (iTask.do_retry) {
736 if (iTask.task != NULL) {
737 scsi_free_scsi_task(iTask.task);
738 iTask.task = NULL;
739 }
740 iTask.complete = 0;
741 goto retry;
742 }
743
744 if (iTask.status != SCSI_STATUS_GOOD) {
745 /* in case the get_lba_status_callout fails (i.e.
746 * because the device is busy or the cmd is not
747 * supported) we pretend all blocks are allocated
748 * for backwards compatibility */
Peter Lievene38bc232017-12-08 12:51:08 +0100749 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s",
750 lba, iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100751 goto out_unlock;
Peter Lievenb03c3802014-04-28 13:11:32 +0200752 }
753
754 lbas = scsi_datain_unmarshall(iTask.task);
755 if (lbas == NULL) {
756 ret = -EIO;
Paolo Bonzinid045c462017-02-22 19:07:25 +0100757 goto out_unlock;
Peter Lievenb03c3802014-04-28 13:11:32 +0200758 }
759
760 lbasd = &lbas->descriptors[0];
761
Eric Blake92809c32018-02-13 14:26:48 -0600762 if (lba != lbasd->lba) {
Peter Lievenb03c3802014-04-28 13:11:32 +0200763 ret = -EIO;
Paolo Bonzinid045c462017-02-22 19:07:25 +0100764 goto out_unlock;
Peter Lievenb03c3802014-04-28 13:11:32 +0200765 }
766
Eric Blake8ee1cef2018-05-08 16:27:18 -0500767 *pnum = (int64_t) lbasd->num_blocks * iscsilun->block_size;
Peter Lievenb03c3802014-04-28 13:11:32 +0200768
769 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
770 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
771 ret &= ~BDRV_BLOCK_DATA;
772 if (iscsilun->lbprz) {
773 ret |= BDRV_BLOCK_ZERO;
774 }
775 }
776
777 if (ret & BDRV_BLOCK_ZERO) {
Eric Blake92809c32018-02-13 14:26:48 -0600778 iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum);
Peter Lievenb03c3802014-04-28 13:11:32 +0200779 } else {
Eric Blake92809c32018-02-13 14:26:48 -0600780 iscsi_allocmap_set_allocated(iscsilun, offset, *pnum);
Peter Lievenb03c3802014-04-28 13:11:32 +0200781 }
782
Eric Blake92809c32018-02-13 14:26:48 -0600783 if (*pnum > bytes) {
784 *pnum = bytes;
Peter Lievenb03c3802014-04-28 13:11:32 +0200785 }
Paolo Bonzinid045c462017-02-22 19:07:25 +0100786out_unlock:
787 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lievene38bc232017-12-08 12:51:08 +0100788 g_free(iTask.err_str);
Peter Lievenb03c3802014-04-28 13:11:32 +0200789out:
790 if (iTask.task != NULL) {
791 scsi_free_scsi_task(iTask.task);
792 }
Eric Blake92809c32018-02-13 14:26:48 -0600793 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) {
Fam Zheng33998332016-01-26 11:58:52 +0800794 *file = bs;
795 }
Peter Lievenb03c3802014-04-28 13:11:32 +0200796 return ret;
797}
798
Peter Lieven063c3372013-12-05 16:47:17 +0100799static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
800 int64_t sector_num, int nb_sectors,
801 QEMUIOVector *iov)
Peter Lieven1dde7162013-02-21 16:15:54 +0100802{
803 IscsiLun *iscsilun = bs->opaque;
Peter Lieven063c3372013-12-05 16:47:17 +0100804 struct IscsiTask iTask;
Peter Lieven1dde7162013-02-21 16:15:54 +0100805 uint64_t lba;
806 uint32_t num_sectors;
Peter Lievene38bc232017-12-08 12:51:08 +0100807 int r = 0;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100808
Eric Blake94d047a2016-06-01 15:10:05 -0600809 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
Peter Lieven063c3372013-12-05 16:47:17 +0100810 return -EINVAL;
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000811 }
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000812
Eric Blake6bd01f12016-07-15 12:32:04 -0600813 if (bs->bl.max_transfer) {
814 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
Peter Lievendc9e7162014-10-27 10:18:48 +0100815 }
816
Peter Lievene1123a32016-07-18 10:52:20 +0200817 /* if cache.direct is off and we have a valid entry in our allocation map
818 * we can skip checking the block status and directly return zeroes if
819 * the request falls within an unallocated area */
Eric Blake04a408f2018-02-13 14:26:47 -0600820 if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
821 nb_sectors * BDRV_SECTOR_SIZE) &&
822 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
823 nb_sectors * BDRV_SECTOR_SIZE)) {
Peter Lievene1123a32016-07-18 10:52:20 +0200824 qemu_iovec_memset(iov, 0, 0x00, iov->size);
825 return 0;
826 }
827
828 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
Eric Blake04a408f2018-02-13 14:26:47 -0600829 !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
830 nb_sectors * BDRV_SECTOR_SIZE) &&
831 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
832 nb_sectors * BDRV_SECTOR_SIZE)) {
Eric Blake92809c32018-02-13 14:26:48 -0600833 int64_t pnum;
Peter Lievene1123a32016-07-18 10:52:20 +0200834 /* check the block status from the beginning of the cluster
835 * containing the start sector */
Eric Blake92809c32018-02-13 14:26:48 -0600836 int64_t head;
837 int ret;
Eric Blakeba059e72018-02-13 14:26:46 -0600838
Eric Blake92809c32018-02-13 14:26:48 -0600839 assert(iscsilun->cluster_size);
840 head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size;
841 ret = iscsi_co_block_status(bs, true,
842 sector_num * BDRV_SECTOR_SIZE - head,
843 BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL);
Peter Lievenb03c3802014-04-28 13:11:32 +0200844 if (ret < 0) {
845 return ret;
846 }
Peter Lievene1123a32016-07-18 10:52:20 +0200847 /* if the whole request falls into an unallocated area we can avoid
Eric Blakeba059e72018-02-13 14:26:46 -0600848 * reading and directly return zeroes instead */
Peter Lievene1123a32016-07-18 10:52:20 +0200849 if (ret & BDRV_BLOCK_ZERO &&
Eric Blake92809c32018-02-13 14:26:48 -0600850 pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) {
Peter Lievenb03c3802014-04-28 13:11:32 +0200851 qemu_iovec_memset(iov, 0, 0x00, iov->size);
852 return 0;
853 }
854 }
Peter Lievenb03c3802014-04-28 13:11:32 +0200855
Peter Lieven063c3372013-12-05 16:47:17 +0100856 lba = sector_qemu2lun(sector_num, iscsilun);
857 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000858
Peter Lieven063c3372013-12-05 16:47:17 +0100859 iscsi_co_init_iscsitask(iscsilun, &iTask);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100860 qemu_mutex_lock(&iscsilun->mutex);
Peter Lieven063c3372013-12-05 16:47:17 +0100861retry:
Peter Lieven9281fe92014-06-04 15:47:39 +0200862 if (iscsilun->use_16_for_rw) {
Roy Shterman583ec222016-10-09 11:14:55 +0300863#if LIBISCSI_API_VERSION >= (20160603)
864 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
865 num_sectors * iscsilun->block_size,
866 iscsilun->block_size, 0, 0, 0, 0, 0,
867 iscsi_co_generic_cb, &iTask,
868 (struct scsi_iovec *)iov->iov, iov->niov);
869 } else {
870 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
871 num_sectors * iscsilun->block_size,
872 iscsilun->block_size,
873 0, 0, 0, 0, 0,
874 iscsi_co_generic_cb, &iTask,
875 (struct scsi_iovec *)iov->iov, iov->niov);
876 }
877#else
Peter Lieven063c3372013-12-05 16:47:17 +0100878 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
879 num_sectors * iscsilun->block_size,
880 iscsilun->block_size, 0, 0, 0, 0, 0,
881 iscsi_co_generic_cb, &iTask);
Peter Lieven9281fe92014-06-04 15:47:39 +0200882 } else {
Peter Lieven063c3372013-12-05 16:47:17 +0100883 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
884 num_sectors * iscsilun->block_size,
Stefan Weil219c2522013-12-17 08:57:10 +0100885 iscsilun->block_size,
Stefan Weil219c2522013-12-17 08:57:10 +0100886 0, 0, 0, 0, 0,
Peter Lieven063c3372013-12-05 16:47:17 +0100887 iscsi_co_generic_cb, &iTask);
Ronnie Sahlbergf4dfa672012-05-22 20:10:05 +1000888 }
Roy Shterman583ec222016-10-09 11:14:55 +0300889#endif
Peter Lieven063c3372013-12-05 16:47:17 +0100890 if (iTask.task == NULL) {
Paolo Bonzinif6eb0b32017-03-03 16:23:36 +0100891 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lieven92397112013-12-20 10:02:47 +0100892 return -ENOMEM;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100893 }
Roy Shterman583ec222016-10-09 11:14:55 +0300894#if LIBISCSI_API_VERSION < (20160603)
Peter Lieven063c3372013-12-05 16:47:17 +0100895 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
Roy Shterman583ec222016-10-09 11:14:55 +0300896#endif
Peter Lieven91bea4e2013-07-11 14:16:27 +0200897
Fam Zheng66e75c02018-06-01 17:26:45 +0800898 iscsi_co_wait_for_task(&iTask, iscsilun);
Peter Lieven063c3372013-12-05 16:47:17 +0100899 if (iTask.task != NULL) {
900 scsi_free_scsi_task(iTask.task);
901 iTask.task = NULL;
Peter Lieven1dde7162013-02-21 16:15:54 +0100902 }
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100903
Peter Lieven063c3372013-12-05 16:47:17 +0100904 if (iTask.do_retry) {
Peter Lieven837c3902014-02-18 13:08:39 +0100905 iTask.complete = 0;
Peter Lieven063c3372013-12-05 16:47:17 +0100906 goto retry;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100907 }
908
Peter Lieven063c3372013-12-05 16:47:17 +0100909 if (iTask.status != SCSI_STATUS_GOOD) {
Peter Lievene38bc232017-12-08 12:51:08 +0100910 error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s",
911 lba, iTask.err_str);
912 r = iTask.err_code;
Peter Lieven1dde7162013-02-21 16:15:54 +0100913 }
914
Peter Lievene38bc232017-12-08 12:51:08 +0100915 qemu_mutex_unlock(&iscsilun->mutex);
916 g_free(iTask.err_str);
917 return r;
Peter Lieven1dde7162013-02-21 16:15:54 +0100918}
919
Peter Lieven063c3372013-12-05 16:47:17 +0100920static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
Peter Lieven1dde7162013-02-21 16:15:54 +0100921{
922 IscsiLun *iscsilun = bs->opaque;
Peter Lieven063c3372013-12-05 16:47:17 +0100923 struct IscsiTask iTask;
Peter Lievene38bc232017-12-08 12:51:08 +0100924 int r = 0;
Peter Lieven1dde7162013-02-21 16:15:54 +0100925
Peter Lieven73b53942015-04-16 16:08:29 +0200926 iscsi_co_init_iscsitask(iscsilun, &iTask);
Paolo Bonzinid045c462017-02-22 19:07:25 +0100927 qemu_mutex_lock(&iscsilun->mutex);
Peter Lieven063c3372013-12-05 16:47:17 +0100928retry:
929 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
930 0, iscsi_co_generic_cb, &iTask) == NULL) {
Paolo Bonzinif6eb0b32017-03-03 16:23:36 +0100931 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lieven92397112013-12-20 10:02:47 +0100932 return -ENOMEM;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100933 }
934
Fam Zheng66e75c02018-06-01 17:26:45 +0800935 iscsi_co_wait_for_task(&iTask, iscsilun);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100936
Peter Lieven063c3372013-12-05 16:47:17 +0100937 if (iTask.task != NULL) {
938 scsi_free_scsi_task(iTask.task);
939 iTask.task = NULL;
940 }
941
942 if (iTask.do_retry) {
Peter Lieven837c3902014-02-18 13:08:39 +0100943 iTask.complete = 0;
Peter Lieven063c3372013-12-05 16:47:17 +0100944 goto retry;
945 }
946
947 if (iTask.status != SCSI_STATUS_GOOD) {
Peter Lievene38bc232017-12-08 12:51:08 +0100948 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str);
949 r = iTask.err_code;
Peter Lieven063c3372013-12-05 16:47:17 +0100950 }
951
Peter Lievene38bc232017-12-08 12:51:08 +0100952 qemu_mutex_unlock(&iscsilun->mutex);
953 g_free(iTask.err_str);
954 return r;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +1100955}
956
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000957#ifdef __linux__
Paolo Bonzinid045c462017-02-22 19:07:25 +0100958/* Called (via iscsi_service) with QemuMutex held. */
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000959static void
960iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
961 void *command_data, void *opaque)
962{
963 IscsiAIOCB *acb = opaque;
964
Stefan Hajnoczi88e94fd2018-02-15 11:15:26 +0000965 if (status == SCSI_STATUS_CANCELLED) {
966 if (!acb->bh) {
967 acb->status = -ECANCELED;
968 iscsi_schedule_bh(acb);
969 }
970 return;
971 }
972
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000973 acb->status = 0;
974 if (status < 0) {
975 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
976 iscsi_get_error(iscsi));
Fam Zhenge01dd3d2015-11-05 13:00:09 +0800977 acb->status = iscsi_translate_sense(&acb->task->sense);
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000978 }
979
980 acb->ioh->driver_status = 0;
981 acb->ioh->host_status = 0;
982 acb->ioh->resid = 0;
Vadim Rozenfeld644c6869d2016-05-13 13:03:22 +0200983 acb->ioh->status = status;
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000984
985#define SG_ERR_DRIVER_SENSE 0x08
986
987 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
988 int ss;
989
990 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
991
992 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
993 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
994 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
995 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
996 }
997
Paolo Bonzinicfb3f502012-08-18 23:38:03 +0200998 iscsi_schedule_bh(acb);
Ronnie Sahlberg98392452012-05-25 21:59:01 +1000999}
1000
Fam Zheng4bb17ab2015-11-09 18:16:49 +08001001static void iscsi_ioctl_bh_completion(void *opaque)
1002{
1003 IscsiAIOCB *acb = opaque;
1004
1005 qemu_bh_delete(acb->bh);
1006 acb->common.cb(acb->common.opaque, acb->ret);
1007 qemu_aio_unref(acb);
1008}
1009
1010static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
1011{
1012 BlockDriverState *bs = acb->common.bs;
1013 IscsiLun *iscsilun = bs->opaque;
1014 int ret = 0;
1015
1016 switch (req) {
1017 case SG_GET_VERSION_NUM:
1018 *(int *)buf = 30000;
1019 break;
1020 case SG_GET_SCSI_ID:
1021 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
1022 break;
1023 default:
1024 ret = -EINVAL;
1025 }
1026 assert(!acb->bh);
1027 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
1028 iscsi_ioctl_bh_completion, acb);
1029 acb->ret = ret;
1030 qemu_bh_schedule(acb->bh);
1031}
1032
Markus Armbruster7c84b1b2014-10-07 13:59:14 +02001033static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001034 unsigned long int req, void *buf,
Markus Armbruster097310b2014-10-07 13:59:15 +02001035 BlockCompletionFunc *cb, void *opaque)
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001036{
1037 IscsiLun *iscsilun = bs->opaque;
1038 struct iscsi_context *iscsi = iscsilun->iscsi;
1039 struct iscsi_data data;
1040 IscsiAIOCB *acb;
1041
Stefan Hajnoczid7331be2012-10-31 16:34:37 +01001042 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001043
1044 acb->iscsilun = iscsilun;
Paolo Bonzini1bd075f2012-08-18 23:35:49 +02001045 acb->bh = NULL;
1046 acb->status = -EINPROGRESS;
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001047 acb->ioh = buf;
Stefan Hajnoczic1004482018-02-03 07:16:21 +01001048 acb->cancelled = false;
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001049
Fam Zheng4bb17ab2015-11-09 18:16:49 +08001050 if (req != SG_IO) {
1051 iscsi_ioctl_handle_emulated(acb, req, buf);
1052 return &acb->common;
1053 }
1054
Peter Lievena6b31672016-05-24 10:59:28 +02001055 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
1056 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1057 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
1058 qemu_aio_unref(acb);
1059 return NULL;
1060 }
1061
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001062 acb->task = malloc(sizeof(struct scsi_task));
1063 if (acb->task == NULL) {
1064 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1065 iscsi_get_error(iscsi));
Fam Zheng80074292014-09-11 13:41:28 +08001066 qemu_aio_unref(acb);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001067 return NULL;
1068 }
1069 memset(acb->task, 0, sizeof(struct scsi_task));
1070
1071 switch (acb->ioh->dxfer_direction) {
1072 case SG_DXFER_TO_DEV:
1073 acb->task->xfer_dir = SCSI_XFER_WRITE;
1074 break;
1075 case SG_DXFER_FROM_DEV:
1076 acb->task->xfer_dir = SCSI_XFER_READ;
1077 break;
1078 default:
1079 acb->task->xfer_dir = SCSI_XFER_NONE;
1080 break;
1081 }
1082
1083 acb->task->cdb_size = acb->ioh->cmd_len;
1084 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
1085 acb->task->expxferlen = acb->ioh->dxfer_len;
1086
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001087 data.size = 0;
Paolo Bonzinid045c462017-02-22 19:07:25 +01001088 qemu_mutex_lock(&iscsilun->mutex);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001089 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001090 if (acb->ioh->iovec_count == 0) {
1091 data.data = acb->ioh->dxferp;
1092 data.size = acb->ioh->dxfer_len;
1093 } else {
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001094 scsi_task_set_iov_out(acb->task,
1095 (struct scsi_iovec *) acb->ioh->dxferp,
1096 acb->ioh->iovec_count);
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001097 }
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001098 }
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001099
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001100 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1101 iscsi_aio_ioctl_cb,
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001102 (data.size > 0) ? &data : NULL,
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001103 acb) != 0) {
Paolo Bonzinid045c462017-02-22 19:07:25 +01001104 qemu_mutex_unlock(&iscsilun->mutex);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001105 scsi_free_scsi_task(acb->task);
Fam Zheng80074292014-09-11 13:41:28 +08001106 qemu_aio_unref(acb);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001107 return NULL;
1108 }
1109
1110 /* tell libiscsi to read straight into the buffer we got from ioctl */
1111 if (acb->task->xfer_dir == SCSI_XFER_READ) {
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001112 if (acb->ioh->iovec_count == 0) {
1113 scsi_task_add_data_in_buffer(acb->task,
1114 acb->ioh->dxfer_len,
1115 acb->ioh->dxferp);
1116 } else {
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001117 scsi_task_set_iov_in(acb->task,
1118 (struct scsi_iovec *) acb->ioh->dxferp,
1119 acb->ioh->iovec_count);
Ronnie Sahlberg0a53f012013-06-23 08:07:08 -07001120 }
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001121 }
1122
1123 iscsi_set_events(iscsilun);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001124 qemu_mutex_unlock(&iscsilun->mutex);
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001125
1126 return &acb->common;
1127}
1128
Ronnie Sahlberg98392452012-05-25 21:59:01 +10001129#endif
1130
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001131static int64_t
1132iscsi_getlength(BlockDriverState *bs)
1133{
1134 IscsiLun *iscsilun = bs->opaque;
1135 int64_t len;
1136
1137 len = iscsilun->num_blocks;
1138 len *= iscsilun->block_size;
1139
1140 return len;
1141}
1142
Peter Lieven65f3e332013-07-19 09:19:41 +02001143static int
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001144coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
Peter Lieven65f3e332013-07-19 09:19:41 +02001145{
1146 IscsiLun *iscsilun = bs->opaque;
1147 struct IscsiTask iTask;
1148 struct unmap_list list;
Paolo Bonzinid045c462017-02-22 19:07:25 +01001149 int r = 0;
Peter Lieven65f3e332013-07-19 09:19:41 +02001150
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001151 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
Eric Blake49228d12016-11-17 14:13:57 -06001152 return -ENOTSUP;
1153 }
Peter Lieven65f3e332013-07-19 09:19:41 +02001154
1155 if (!iscsilun->lbp.lbpu) {
1156 /* UNMAP is not supported by the target */
1157 return 0;
1158 }
1159
Eric Blake97c7e852016-07-15 17:23:01 -06001160 list.lba = offset / iscsilun->block_size;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001161 list.num = bytes / iscsilun->block_size;
Peter Lieven65f3e332013-07-19 09:19:41 +02001162
Peter Lieven01a6a232013-10-24 12:07:01 +02001163 iscsi_co_init_iscsitask(iscsilun, &iTask);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001164 qemu_mutex_lock(&iscsilun->mutex);
Peter Lieven01a6a232013-10-24 12:07:01 +02001165retry:
1166 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
Eric Blake97c7e852016-07-15 17:23:01 -06001167 iscsi_co_generic_cb, &iTask) == NULL) {
Paolo Bonzinid045c462017-02-22 19:07:25 +01001168 r = -ENOMEM;
1169 goto out_unlock;
Peter Lieven65f3e332013-07-19 09:19:41 +02001170 }
1171
Fam Zheng66e75c02018-06-01 17:26:45 +08001172 iscsi_co_wait_for_task(&iTask, iscsilun);
Peter Lieven65f3e332013-07-19 09:19:41 +02001173
Peter Lieven01a6a232013-10-24 12:07:01 +02001174 if (iTask.task != NULL) {
1175 scsi_free_scsi_task(iTask.task);
1176 iTask.task = NULL;
1177 }
Peter Lieven65f3e332013-07-19 09:19:41 +02001178
Peter Lieven01a6a232013-10-24 12:07:01 +02001179 if (iTask.do_retry) {
Peter Lieven837c3902014-02-18 13:08:39 +01001180 iTask.complete = 0;
Peter Lieven01a6a232013-10-24 12:07:01 +02001181 goto retry;
1182 }
Peter Lieven65f3e332013-07-19 09:19:41 +02001183
Eric Blake04a408f2018-02-13 14:26:47 -06001184 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
Peter Lievenaef172f2017-12-08 12:51:07 +01001185
Peter Lieven01a6a232013-10-24 12:07:01 +02001186 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1187 /* the target might fail with a check condition if it
1188 is not happy with the alignment of the UNMAP request
1189 we silently fail in this case */
Paolo Bonzinid045c462017-02-22 19:07:25 +01001190 goto out_unlock;
Peter Lieven01a6a232013-10-24 12:07:01 +02001191 }
Peter Lieven65f3e332013-07-19 09:19:41 +02001192
Peter Lieven01a6a232013-10-24 12:07:01 +02001193 if (iTask.status != SCSI_STATUS_GOOD) {
Peter Lievene38bc232017-12-08 12:51:08 +01001194 error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s",
1195 list.lba, iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001196 r = iTask.err_code;
1197 goto out_unlock;
Peter Lieven65f3e332013-07-19 09:19:41 +02001198 }
1199
Paolo Bonzinid045c462017-02-22 19:07:25 +01001200out_unlock:
1201 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lievene38bc232017-12-08 12:51:08 +01001202 g_free(iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001203 return r;
Peter Lieven65f3e332013-07-19 09:19:41 +02001204}
1205
Peter Lievend4cd9612013-10-24 12:07:02 +02001206static int
Eric Blake94d047a2016-06-01 15:10:05 -06001207coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001208 int bytes, BdrvRequestFlags flags)
Peter Lievend4cd9612013-10-24 12:07:02 +02001209{
1210 IscsiLun *iscsilun = bs->opaque;
1211 struct IscsiTask iTask;
1212 uint64_t lba;
1213 uint32_t nb_blocks;
Peter Lieven9281fe92014-06-04 15:47:39 +02001214 bool use_16_for_ws = iscsilun->use_16_for_rw;
Paolo Bonzinid045c462017-02-22 19:07:25 +01001215 int r = 0;
Peter Lievend4cd9612013-10-24 12:07:02 +02001216
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001217 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
Eric Blake94d047a2016-06-01 15:10:05 -06001218 return -ENOTSUP;
Peter Lievend4cd9612013-10-24 12:07:02 +02001219 }
1220
Peter Lieven9281fe92014-06-04 15:47:39 +02001221 if (flags & BDRV_REQ_MAY_UNMAP) {
1222 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1223 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1224 use_16_for_ws = true;
1225 }
1226 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1227 /* WRITESAME16 with UNMAP is not supported by the target,
1228 * fall back and try WRITESAME10/16 without UNMAP */
1229 flags &= ~BDRV_REQ_MAY_UNMAP;
1230 use_16_for_ws = iscsilun->use_16_for_rw;
1231 }
Paolo Bonzinifa6252b2013-11-22 13:39:54 +01001232 }
1233
Peter Lievendbe5c582014-04-28 13:23:25 +02001234 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
Peter Lieven9281fe92014-06-04 15:47:39 +02001235 /* WRITESAME without UNMAP is not supported by the target */
Peter Lievend4cd9612013-10-24 12:07:02 +02001236 return -ENOTSUP;
1237 }
1238
Eric Blake94d047a2016-06-01 15:10:05 -06001239 lba = offset / iscsilun->block_size;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001240 nb_blocks = bytes / iscsilun->block_size;
Peter Lievend4cd9612013-10-24 12:07:02 +02001241
1242 if (iscsilun->zeroblock == NULL) {
Kevin Wolf4d5a3f82014-05-20 13:30:49 +02001243 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1244 if (iscsilun->zeroblock == NULL) {
1245 return -ENOMEM;
1246 }
Peter Lievend4cd9612013-10-24 12:07:02 +02001247 }
1248
Paolo Bonzinid045c462017-02-22 19:07:25 +01001249 qemu_mutex_lock(&iscsilun->mutex);
Peter Lievend4cd9612013-10-24 12:07:02 +02001250 iscsi_co_init_iscsitask(iscsilun, &iTask);
1251retry:
Peter Lieven9281fe92014-06-04 15:47:39 +02001252 if (use_16_for_ws) {
1253 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1254 iscsilun->zeroblock, iscsilun->block_size,
1255 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1256 0, 0, iscsi_co_generic_cb, &iTask);
1257 } else {
1258 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1259 iscsilun->zeroblock, iscsilun->block_size,
1260 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1261 0, 0, iscsi_co_generic_cb, &iTask);
1262 }
1263 if (iTask.task == NULL) {
Paolo Bonzinif6eb0b32017-03-03 16:23:36 +01001264 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lieven92397112013-12-20 10:02:47 +01001265 return -ENOMEM;
Peter Lievend4cd9612013-10-24 12:07:02 +02001266 }
1267
Fam Zheng66e75c02018-06-01 17:26:45 +08001268 iscsi_co_wait_for_task(&iTask, iscsilun);
Peter Lievend4cd9612013-10-24 12:07:02 +02001269
Peter Lievend9738fd2014-02-22 13:17:24 +01001270 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1271 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
Paolo Bonzini27898a52014-04-02 12:12:50 +02001272 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1273 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
Peter Lievend9738fd2014-02-22 13:17:24 +01001274 /* WRITE SAME is not supported by the target */
1275 iscsilun->has_write_same = false;
1276 scsi_free_scsi_task(iTask.task);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001277 r = -ENOTSUP;
1278 goto out_unlock;
Peter Lievend9738fd2014-02-22 13:17:24 +01001279 }
1280
Peter Lievend4cd9612013-10-24 12:07:02 +02001281 if (iTask.task != NULL) {
1282 scsi_free_scsi_task(iTask.task);
1283 iTask.task = NULL;
1284 }
1285
1286 if (iTask.do_retry) {
Peter Lieven837c3902014-02-18 13:08:39 +01001287 iTask.complete = 0;
Peter Lievend4cd9612013-10-24 12:07:02 +02001288 goto retry;
1289 }
1290
1291 if (iTask.status != SCSI_STATUS_GOOD) {
Eric Blake04a408f2018-02-13 14:26:47 -06001292 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
Peter Lievene38bc232017-12-08 12:51:08 +01001293 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s",
1294 lba, iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001295 r = iTask.err_code;
1296 goto out_unlock;
Peter Lievend4cd9612013-10-24 12:07:02 +02001297 }
1298
Peter Lievenb03c3802014-04-28 13:11:32 +02001299 if (flags & BDRV_REQ_MAY_UNMAP) {
Eric Blake04a408f2018-02-13 14:26:47 -06001300 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
Peter Lievenb03c3802014-04-28 13:11:32 +02001301 } else {
Eric Blake04a408f2018-02-13 14:26:47 -06001302 iscsi_allocmap_set_allocated(iscsilun, offset, bytes);
Peter Lievenb03c3802014-04-28 13:11:32 +02001303 }
1304
Paolo Bonzinid045c462017-02-22 19:07:25 +01001305out_unlock:
1306 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lievene38bc232017-12-08 12:51:08 +01001307 g_free(iTask.err_str);
Paolo Bonzinid045c462017-02-22 19:07:25 +01001308 return r;
Peter Lievend4cd9612013-10-24 12:07:02 +02001309}
1310
Kevin Wolf43171422016-12-08 14:23:07 +01001311static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts,
Paolo Bonzinif2917852014-02-17 14:43:53 +01001312 Error **errp)
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001313{
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001314 const char *user = NULL;
1315 const char *password = NULL;
Daniel P. Berrangeb1893462016-01-21 14:19:21 +00001316 const char *secretid;
1317 char *secret = NULL;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001318
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001319 user = qemu_opt_get(opts, "user");
1320 if (!user) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001321 return;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001322 }
1323
Daniel P. Berrangeb1893462016-01-21 14:19:21 +00001324 secretid = qemu_opt_get(opts, "password-secret");
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001325 password = qemu_opt_get(opts, "password");
Daniel P. Berrangeb1893462016-01-21 14:19:21 +00001326 if (secretid && password) {
1327 error_setg(errp, "'password' and 'password-secret' properties are "
1328 "mutually exclusive");
1329 return;
1330 }
1331 if (secretid) {
1332 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1333 if (!secret) {
1334 return;
1335 }
1336 password = secret;
1337 } else if (!password) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001338 error_setg(errp, "CHAP username specified but no password was given");
1339 return;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001340 }
1341
1342 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001343 error_setg(errp, "Failed to set initiator username and password");
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001344 }
Daniel P. Berrangeb1893462016-01-21 14:19:21 +00001345
1346 g_free(secret);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001347}
1348
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001349static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts,
Paolo Bonzinif2917852014-02-17 14:43:53 +01001350 Error **errp)
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001351{
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001352 const char *digest = NULL;
1353
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001354 digest = qemu_opt_get(opts, "header-digest");
1355 if (!digest) {
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001356 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
Kevin Wolf31eb1202016-12-08 14:23:11 +01001357 } else if (!strcmp(digest, "crc32c")) {
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001358 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
Kevin Wolf31eb1202016-12-08 14:23:11 +01001359 } else if (!strcmp(digest, "none")) {
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001360 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
Kevin Wolf31eb1202016-12-08 14:23:11 +01001361 } else if (!strcmp(digest, "crc32c-none")) {
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001362 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
Kevin Wolf31eb1202016-12-08 14:23:11 +01001363 } else if (!strcmp(digest, "none-crc32c")) {
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001364 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1365 } else {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001366 error_setg(errp, "Invalid header-digest setting : %s", digest);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001367 }
1368}
1369
Kevin Wolfd4e79922016-12-08 14:23:08 +01001370static char *get_initiator_name(QemuOpts *opts)
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001371{
Paolo Bonzini5accc842013-08-02 17:02:01 +02001372 const char *name;
1373 char *iscsi_name;
1374 UuidInfo *uuid_info;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001375
Kevin Wolfd4e79922016-12-08 14:23:08 +01001376 name = qemu_opt_get(opts, "initiator-name");
1377 if (name) {
1378 return g_strdup(name);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001379 }
1380
Paolo Bonzini5accc842013-08-02 17:02:01 +02001381 uuid_info = qmp_query_uuid(NULL);
1382 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1383 name = qemu_get_vm_name();
Paolo Bonzinif2ef4a62012-08-06 10:54:41 +02001384 } else {
Paolo Bonzini5accc842013-08-02 17:02:01 +02001385 name = uuid_info->UUID;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001386 }
Paolo Bonzini5accc842013-08-02 17:02:01 +02001387 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1388 name ? ":" : "", name ? name : "");
1389 qapi_free_UuidInfo(uuid_info);
1390 return iscsi_name;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001391}
1392
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001393static void iscsi_nop_timed_event(void *opaque)
1394{
1395 IscsiLun *iscsilun = opaque;
1396
Paolo Bonzinid045c462017-02-22 19:07:25 +01001397 qemu_mutex_lock(&iscsilun->mutex);
Peter Lieven5dd7a532015-06-16 13:45:07 +02001398 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001399 error_report("iSCSI: NOP timeout. Reconnecting...");
Peter Lieven5dd7a532015-06-16 13:45:07 +02001400 iscsilun->request_timed_out = true;
1401 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001402 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
Paolo Bonzini2f47da52017-02-13 14:52:29 +01001403 goto out;
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001404 }
1405
Alex Blighbc72ad62013-08-21 16:03:08 +01001406 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001407 iscsi_set_events(iscsilun);
Paolo Bonzini2f47da52017-02-13 14:52:29 +01001408
1409out:
Paolo Bonzinid045c462017-02-22 19:07:25 +01001410 qemu_mutex_unlock(&iscsilun->mutex);
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001411}
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001412
Paolo Bonzinif2917852014-02-17 14:43:53 +01001413static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
Peter Lievencb1b83e2013-02-18 14:50:46 +01001414{
1415 struct scsi_task *task = NULL;
1416 struct scsi_readcapacity10 *rc10 = NULL;
1417 struct scsi_readcapacity16 *rc16 = NULL;
Peter Lievencb1b83e2013-02-18 14:50:46 +01001418 int retries = ISCSI_CMD_RETRIES;
1419
Paolo Bonzini12888442013-05-31 13:56:24 +02001420 do {
1421 if (task != NULL) {
1422 scsi_free_scsi_task(task);
1423 task = NULL;
Peter Lievencb1b83e2013-02-18 14:50:46 +01001424 }
Peter Lievencb1b83e2013-02-18 14:50:46 +01001425
Paolo Bonzini12888442013-05-31 13:56:24 +02001426 switch (iscsilun->type) {
1427 case TYPE_DISK:
1428 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1429 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1430 rc16 = scsi_datain_unmarshall(task);
1431 if (rc16 == NULL) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001432 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
Paolo Bonzini12888442013-05-31 13:56:24 +02001433 } else {
1434 iscsilun->block_size = rc16->block_length;
1435 iscsilun->num_blocks = rc16->returned_lba + 1;
Peter Lieven0a386e42015-04-16 16:08:26 +02001436 iscsilun->lbpme = !!rc16->lbpme;
1437 iscsilun->lbprz = !!rc16->lbprz;
Peter Lieven9281fe92014-06-04 15:47:39 +02001438 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
Paolo Bonzini12888442013-05-31 13:56:24 +02001439 }
Zhu Lingshan1cb6d132015-12-29 11:32:14 +08001440 break;
Paolo Bonzini12888442013-05-31 13:56:24 +02001441 }
Zhu Lingshan1cb6d132015-12-29 11:32:14 +08001442 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1443 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1444 break;
1445 }
1446 /* Fall through and try READ CAPACITY(10) instead. */
Paolo Bonzini12888442013-05-31 13:56:24 +02001447 case TYPE_ROM:
1448 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1449 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1450 rc10 = scsi_datain_unmarshall(task);
1451 if (rc10 == NULL) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001452 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
Paolo Bonzini12888442013-05-31 13:56:24 +02001453 } else {
1454 iscsilun->block_size = rc10->block_size;
1455 if (rc10->lba == 0) {
1456 /* blank disk loaded */
1457 iscsilun->num_blocks = 0;
1458 } else {
1459 iscsilun->num_blocks = rc10->lba + 1;
1460 }
1461 }
1462 }
1463 break;
1464 default:
Paolo Bonzinif2917852014-02-17 14:43:53 +01001465 return;
Paolo Bonzini12888442013-05-31 13:56:24 +02001466 }
1467 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1468 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1469 && retries-- > 0);
1470
1471 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
Zhu Lingshan240125b2015-12-16 12:40:40 +08001472 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
Peter Lieven6d1f2522015-08-14 13:33:36 +02001473 } else if (!iscsilun->block_size ||
1474 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1475 error_setg(errp, "iSCSI: the target returned an invalid "
1476 "block size of %d.", iscsilun->block_size);
Paolo Bonzini12888442013-05-31 13:56:24 +02001477 }
Peter Lievencb1b83e2013-02-18 14:50:46 +01001478 if (task) {
1479 scsi_free_scsi_task(task);
1480 }
Peter Lievencb1b83e2013-02-18 14:50:46 +01001481}
1482
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001483static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
Peter Lieven24d3bd62014-02-17 18:34:08 +01001484 int evpd, int pc, void **inq, Error **errp)
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001485{
1486 int full_size;
1487 struct scsi_task *task = NULL;
1488 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1489 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1490 goto fail;
1491 }
1492 full_size = scsi_datain_getfullsize(task);
1493 if (full_size > task->datain.size) {
1494 scsi_free_scsi_task(task);
1495
1496 /* we need more data for the full list */
1497 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001498 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1499 goto fail;
1500 }
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001501 }
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001502
Peter Lieven24d3bd62014-02-17 18:34:08 +01001503 *inq = scsi_datain_unmarshall(task);
1504 if (*inq == NULL) {
1505 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
Markus Armbruster172fc4d2014-04-25 16:50:35 +02001506 goto fail_with_err;
Peter Lieven24d3bd62014-02-17 18:34:08 +01001507 }
1508
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001509 return task;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001510
1511fail:
Markus Armbruster172fc4d2014-04-25 16:50:35 +02001512 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1513 iscsi_get_error(iscsi));
1514fail_with_err:
Peter Lieven24d3bd62014-02-17 18:34:08 +01001515 if (task != NULL) {
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001516 scsi_free_scsi_task(task);
Paolo Bonzini35cb1742014-02-17 14:43:52 +01001517 }
1518 return NULL;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001519}
1520
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001521static void iscsi_detach_aio_context(BlockDriverState *bs)
1522{
1523 IscsiLun *iscsilun = bs->opaque;
1524
Fam Zhengdca21ef2015-10-23 11:08:05 +08001525 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +00001526 false, NULL, NULL, NULL, NULL);
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001527 iscsilun->events = 0;
1528
1529 if (iscsilun->nop_timer) {
1530 timer_del(iscsilun->nop_timer);
1531 timer_free(iscsilun->nop_timer);
1532 iscsilun->nop_timer = NULL;
1533 }
Peter Lieven05b685f2015-04-07 22:08:15 +02001534 if (iscsilun->event_timer) {
1535 timer_del(iscsilun->event_timer);
1536 timer_free(iscsilun->event_timer);
1537 iscsilun->event_timer = NULL;
1538 }
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001539}
1540
1541static void iscsi_attach_aio_context(BlockDriverState *bs,
1542 AioContext *new_context)
1543{
1544 IscsiLun *iscsilun = bs->opaque;
1545
1546 iscsilun->aio_context = new_context;
1547 iscsi_set_events(iscsilun);
1548
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001549 /* Set up a timer for sending out iSCSI NOPs */
1550 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1551 QEMU_CLOCK_REALTIME, SCALE_MS,
1552 iscsi_nop_timed_event, iscsilun);
1553 timer_mod(iscsilun->nop_timer,
1554 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
Peter Lieven05b685f2015-04-07 22:08:15 +02001555
Peter Lieven5dd7a532015-06-16 13:45:07 +02001556 /* Set up a timer for periodic calls to iscsi_set_events and to
1557 * scan for command timeout */
Peter Lieven05b685f2015-04-07 22:08:15 +02001558 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1559 QEMU_CLOCK_REALTIME, SCALE_MS,
Peter Lieven5dd7a532015-06-16 13:45:07 +02001560 iscsi_timed_check_events, iscsilun);
1561 timer_mod(iscsilun->event_timer,
1562 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001563}
1564
Peter Lieven7191f202015-04-16 16:08:27 +02001565static void iscsi_modesense_sync(IscsiLun *iscsilun)
Fam Zhengc1d40962014-10-30 19:23:46 +08001566{
1567 struct scsi_task *task;
1568 struct scsi_mode_sense *ms = NULL;
Peter Lieven7191f202015-04-16 16:08:27 +02001569 iscsilun->write_protected = false;
Peter Lieven752ce452015-04-16 16:08:28 +02001570 iscsilun->dpofua = false;
Fam Zhengc1d40962014-10-30 19:23:46 +08001571
1572 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1573 1, SCSI_MODESENSE_PC_CURRENT,
1574 0x3F, 0, 255);
1575 if (task == NULL) {
1576 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1577 iscsi_get_error(iscsilun->iscsi));
1578 goto out;
1579 }
1580
1581 if (task->status != SCSI_STATUS_GOOD) {
1582 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1583 goto out;
1584 }
1585 ms = scsi_datain_unmarshall(task);
1586 if (!ms) {
1587 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1588 iscsi_get_error(iscsilun->iscsi));
1589 goto out;
1590 }
Peter Lieven7191f202015-04-16 16:08:27 +02001591 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
Peter Lieven752ce452015-04-16 16:08:28 +02001592 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
Fam Zhengc1d40962014-10-30 19:23:46 +08001593
1594out:
1595 if (task) {
1596 scsi_free_scsi_task(task);
1597 }
Fam Zhengc1d40962014-10-30 19:23:46 +08001598}
1599
Kevin Wolf43171422016-12-08 14:23:07 +01001600static void iscsi_parse_iscsi_option(const char *target, QDict *options)
1601{
1602 QemuOptsList *list;
1603 QemuOpts *opts;
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001604 const char *user, *password, *password_secret, *initiator_name,
Kevin Wolf1d560102016-12-08 14:23:10 +01001605 *header_digest, *timeout;
Kevin Wolf43171422016-12-08 14:23:07 +01001606
1607 list = qemu_find_opts("iscsi");
1608 if (!list) {
1609 return;
1610 }
1611
1612 opts = qemu_opts_find(list, target);
1613 if (opts == NULL) {
1614 opts = QTAILQ_FIRST(&list->head);
1615 if (!opts) {
1616 return;
1617 }
1618 }
1619
1620 user = qemu_opt_get(opts, "user");
1621 if (user) {
1622 qdict_set_default_str(options, "user", user);
1623 }
1624
1625 password = qemu_opt_get(opts, "password");
1626 if (password) {
1627 qdict_set_default_str(options, "password", password);
1628 }
1629
1630 password_secret = qemu_opt_get(opts, "password-secret");
1631 if (password_secret) {
1632 qdict_set_default_str(options, "password-secret", password_secret);
1633 }
Kevin Wolfd4e79922016-12-08 14:23:08 +01001634
1635 initiator_name = qemu_opt_get(opts, "initiator-name");
1636 if (initiator_name) {
1637 qdict_set_default_str(options, "initiator-name", initiator_name);
1638 }
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001639
1640 header_digest = qemu_opt_get(opts, "header-digest");
1641 if (header_digest) {
Kevin Wolf31eb1202016-12-08 14:23:11 +01001642 /* -iscsi takes upper case values, but QAPI only supports lower case
1643 * enum constant names, so we have to convert here. */
1644 char *qapi_value = g_ascii_strdown(header_digest, -1);
1645 qdict_set_default_str(options, "header-digest", qapi_value);
1646 g_free(qapi_value);
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001647 }
Kevin Wolf1d560102016-12-08 14:23:10 +01001648
1649 timeout = qemu_opt_get(opts, "timeout");
1650 if (timeout) {
1651 qdict_set_default_str(options, "timeout", timeout);
1652 }
Kevin Wolf43171422016-12-08 14:23:07 +01001653}
1654
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001655/*
1656 * We support iscsi url's on the form
1657 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1658 */
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001659static void iscsi_parse_filename(const char *filename, QDict *options,
1660 Error **errp)
1661{
1662 struct iscsi_url *iscsi_url;
1663 const char *transport_name;
1664 char *lun_str;
1665
1666 iscsi_url = iscsi_parse_full_url(NULL, filename);
1667 if (iscsi_url == NULL) {
1668 error_setg(errp, "Failed to parse URL : %s", filename);
1669 return;
1670 }
1671
1672#if LIBISCSI_API_VERSION >= (20160603)
1673 switch (iscsi_url->transport) {
1674 case TCP_TRANSPORT:
1675 transport_name = "tcp";
1676 break;
1677 case ISER_TRANSPORT:
1678 transport_name = "iser";
1679 break;
1680 default:
1681 error_setg(errp, "Unknown transport type (%d)",
1682 iscsi_url->transport);
1683 return;
1684 }
1685#else
1686 transport_name = "tcp";
1687#endif
1688
1689 qdict_set_default_str(options, "transport", transport_name);
1690 qdict_set_default_str(options, "portal", iscsi_url->portal);
1691 qdict_set_default_str(options, "target", iscsi_url->target);
1692
1693 lun_str = g_strdup_printf("%d", iscsi_url->lun);
1694 qdict_set_default_str(options, "lun", lun_str);
1695 g_free(lun_str);
1696
Kevin Wolf43171422016-12-08 14:23:07 +01001697 /* User/password from -iscsi take precedence over those from the URL */
1698 iscsi_parse_iscsi_option(iscsi_url->target, options);
1699
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001700 if (iscsi_url->user[0] != '\0') {
1701 qdict_set_default_str(options, "user", iscsi_url->user);
1702 qdict_set_default_str(options, "password", iscsi_url->passwd);
1703 }
1704
1705 iscsi_destroy_url(iscsi_url);
1706}
1707
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001708static QemuOptsList runtime_opts = {
1709 .name = "iscsi",
1710 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1711 .desc = {
1712 {
1713 .name = "transport",
1714 .type = QEMU_OPT_STRING,
1715 },
1716 {
1717 .name = "portal",
1718 .type = QEMU_OPT_STRING,
1719 },
1720 {
1721 .name = "target",
1722 .type = QEMU_OPT_STRING,
1723 },
1724 {
1725 .name = "user",
1726 .type = QEMU_OPT_STRING,
1727 },
1728 {
1729 .name = "password",
1730 .type = QEMU_OPT_STRING,
1731 },
1732 {
Kevin Wolf43171422016-12-08 14:23:07 +01001733 .name = "password-secret",
1734 .type = QEMU_OPT_STRING,
1735 },
1736 {
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001737 .name = "lun",
1738 .type = QEMU_OPT_NUMBER,
1739 },
Kevin Wolfd4e79922016-12-08 14:23:08 +01001740 {
1741 .name = "initiator-name",
1742 .type = QEMU_OPT_STRING,
1743 },
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001744 {
1745 .name = "header-digest",
1746 .type = QEMU_OPT_STRING,
1747 },
Kevin Wolf1d560102016-12-08 14:23:10 +01001748 {
1749 .name = "timeout",
1750 .type = QEMU_OPT_NUMBER,
1751 },
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001752 { /* end of list */ }
1753 },
1754};
1755
Fam Zhengcc9743c2018-06-01 17:26:44 +08001756static void iscsi_save_designator(IscsiLun *lun,
1757 struct scsi_inquiry_device_identification *inq_di)
1758{
1759 struct scsi_inquiry_device_designator *desig, *copy = NULL;
1760
1761 for (desig = inq_di->designators; desig; desig = desig->next) {
1762 if (desig->association ||
1763 desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) {
1764 continue;
1765 }
1766 /* NAA works better than T10 vendor ID based designator. */
1767 if (!copy || copy->designator_type < desig->designator_type) {
1768 copy = desig;
1769 }
1770 }
1771 if (copy) {
1772 lun->dd = g_new(struct scsi_inquiry_device_designator, 1);
1773 *lun->dd = *copy;
1774 lun->dd->next = NULL;
1775 lun->dd->designator = g_malloc(copy->designator_length);
1776 memcpy(lun->dd->designator, copy->designator, copy->designator_length);
1777 }
1778}
1779
Max Reitz015a1032013-09-05 14:22:29 +02001780static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1781 Error **errp)
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001782{
1783 IscsiLun *iscsilun = bs->opaque;
1784 struct iscsi_context *iscsi = NULL;
Peter Lievene829b0b2012-11-17 14:37:39 +01001785 struct scsi_task *task = NULL;
1786 struct scsi_inquiry_standard *inq = NULL;
Peter Lieven24d3bd62014-02-17 18:34:08 +01001787 struct scsi_inquiry_supported_pages *inq_vpd;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001788 char *initiator_name = NULL;
Kevin Wolf60beb342013-04-12 17:59:59 +02001789 QemuOpts *opts;
1790 Error *local_err = NULL;
Markus Armbrusterdeadbb82018-06-14 21:14:27 +02001791 const char *transport_name, *portal, *target;
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001792#if LIBISCSI_API_VERSION >= (20160603)
1793 enum iscsi_transport_type transport;
1794#endif
1795 int i, ret = 0, timeout = 0, lun;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001796
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001797 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolf60beb342013-04-12 17:59:59 +02001798 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001799 if (local_err) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001800 error_propagate(errp, local_err);
Kevin Wolf60beb342013-04-12 17:59:59 +02001801 ret = -EINVAL;
1802 goto out;
1803 }
1804
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001805 transport_name = qemu_opt_get(opts, "transport");
1806 portal = qemu_opt_get(opts, "portal");
1807 target = qemu_opt_get(opts, "target");
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001808 lun = qemu_opt_get_number(opts, "lun", 0);
Kevin Wolf60beb342013-04-12 17:59:59 +02001809
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001810 if (!transport_name || !portal || !target) {
1811 error_setg(errp, "Need all of transport, portal and target options");
1812 ret = -EINVAL;
1813 goto out;
1814 }
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001815
1816 if (!strcmp(transport_name, "tcp")) {
1817#if LIBISCSI_API_VERSION >= (20160603)
1818 transport = TCP_TRANSPORT;
1819 } else if (!strcmp(transport_name, "iser")) {
1820 transport = ISER_TRANSPORT;
1821#else
1822 /* TCP is what older libiscsi versions always use */
1823#endif
1824 } else {
1825 error_setg(errp, "Unknown transport: %s", transport_name);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001826 ret = -EINVAL;
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02001827 goto out;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001828 }
1829
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001830 memset(iscsilun, 0, sizeof(IscsiLun));
1831
Kevin Wolfd4e79922016-12-08 14:23:08 +01001832 initiator_name = get_initiator_name(opts);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001833
1834 iscsi = iscsi_create_context(initiator_name);
1835 if (iscsi == NULL) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001836 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001837 ret = -ENOMEM;
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02001838 goto out;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001839 }
Roy Shtermane0ae4982016-10-09 11:14:56 +03001840#if LIBISCSI_API_VERSION >= (20160603)
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001841 if (iscsi_init_transport(iscsi, transport)) {
Roy Shtermane0ae4982016-10-09 11:14:56 +03001842 error_setg(errp, ("Error initializing transport."));
1843 ret = -EINVAL;
1844 goto out;
1845 }
1846#endif
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001847 if (iscsi_set_targetname(iscsi, target)) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001848 error_setg(errp, "iSCSI: Failed to set target name.");
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001849 ret = -EINVAL;
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02001850 goto out;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001851 }
1852
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001853 /* check if we got CHAP username/password via the options */
Kevin Wolf43171422016-12-08 14:23:07 +01001854 apply_chap(iscsi, opts, &local_err);
Paolo Bonzinif2917852014-02-17 14:43:53 +01001855 if (local_err != NULL) {
1856 error_propagate(errp, local_err);
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001857 ret = -EINVAL;
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02001858 goto out;
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001859 }
1860
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001861 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001862 error_setg(errp, "iSCSI: Failed to set session type to normal.");
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001863 ret = -EINVAL;
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02001864 goto out;
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001865 }
1866
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001867 /* check if we got HEADER_DIGEST via the options */
Kevin Wolf81aa2a02016-12-08 14:23:09 +01001868 apply_header_digest(iscsi, opts, &local_err);
Paolo Bonzinif2917852014-02-17 14:43:53 +01001869 if (local_err != NULL) {
1870 error_propagate(errp, local_err);
1871 ret = -EINVAL;
1872 goto out;
1873 }
Ronnie Sahlbergf9dadc92012-01-26 09:39:02 +11001874
Peter Lieven90497362015-06-26 12:18:01 +02001875 /* timeout handling is broken in libiscsi before 1.15.0 */
Kevin Wolf1d560102016-12-08 14:23:10 +01001876 timeout = qemu_opt_get_number(opts, "timeout", 0);
Roy Shterman583ec222016-10-09 11:14:55 +03001877#if LIBISCSI_API_VERSION >= 20150621
Peter Lieven90497362015-06-26 12:18:01 +02001878 iscsi_set_timeout(iscsi, timeout);
1879#else
1880 if (timeout) {
Markus Armbruster5197f442018-10-17 10:26:27 +02001881 warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
Peter Lieven90497362015-06-26 12:18:01 +02001882 }
1883#endif
Peter Lieven5dd7a532015-06-16 13:45:07 +02001884
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001885 if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) {
Paolo Bonzinif2917852014-02-17 14:43:53 +01001886 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
Peter Lievene829b0b2012-11-17 14:37:39 +01001887 iscsi_get_error(iscsi));
1888 ret = -EINVAL;
1889 goto out;
1890 }
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11001891
1892 iscsilun->iscsi = iscsi;
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001893 iscsilun->aio_context = bdrv_get_aio_context(bs);
Kevin Wolfd5895fc2016-12-08 14:23:06 +01001894 iscsilun->lun = lun;
Paolo Bonzinifa6252b2013-11-22 13:39:54 +01001895 iscsilun->has_write_same = true;
Peter Lievene829b0b2012-11-17 14:37:39 +01001896
Peter Lieven24d3bd62014-02-17 18:34:08 +01001897 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1898 (void **) &inq, errp);
1899 if (task == NULL) {
1900 ret = -EINVAL;
1901 goto out;
1902 }
1903 iscsilun->type = inq->periperal_device_type;
1904 scsi_free_scsi_task(task);
1905 task = NULL;
1906
Peter Lieven7191f202015-04-16 16:08:27 +02001907 iscsi_modesense_sync(iscsilun);
Eric Blake4df863f2016-05-03 16:39:06 -06001908 if (iscsilun->dpofua) {
1909 bs->supported_write_flags = BDRV_REQ_FUA;
1910 }
Peter Lieven7191f202015-04-16 16:08:27 +02001911
Fam Zhengc1d40962014-10-30 19:23:46 +08001912 /* Check the write protect flag of the LUN if we want to write */
1913 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
Fam Zheng43ae8fb2015-02-25 12:40:08 +08001914 iscsilun->write_protected) {
Kevin Wolf8f3bf502018-10-08 17:27:18 +02001915 ret = bdrv_apply_auto_read_only(bs, "LUN is write protected", errp);
1916 if (ret < 0) {
1917 goto out;
1918 }
1919 flags &= ~BDRV_O_RDWR;
Fam Zhengc1d40962014-10-30 19:23:46 +08001920 }
1921
Paolo Bonzinif2917852014-02-17 14:43:53 +01001922 iscsi_readcapacity_sync(iscsilun, &local_err);
1923 if (local_err != NULL) {
1924 error_propagate(errp, local_err);
Fam Zhengcd82b6f2014-04-10 09:33:55 +08001925 ret = -EINVAL;
Peter Lievencb1b83e2013-02-18 14:50:46 +01001926 goto out;
Peter Lievene829b0b2012-11-17 14:37:39 +01001927 }
Peter Lieven0777b5d2013-07-11 14:16:25 +02001928 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
Peter Lievene829b0b2012-11-17 14:37:39 +01001929
Kevin Wolff47c3f52014-03-05 15:45:00 +01001930 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1931 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1932 * will try to read from the device to guess the image format.
Ronnie Sahlberg622695a2012-05-26 14:56:41 +10001933 */
Kevin Wolff47c3f52014-03-05 15:45:00 +01001934 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
Eric Blake54115412016-06-23 16:37:26 -06001935 bs->sg = true;
Ronnie Sahlberg622695a2012-05-26 14:56:41 +10001936 }
1937
Peter Lieven24d3bd62014-02-17 18:34:08 +01001938 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1939 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1940 (void **) &inq_vpd, errp);
1941 if (task == NULL) {
1942 ret = -EINVAL;
1943 goto out;
1944 }
1945 for (i = 0; i < inq_vpd->num_pages; i++) {
1946 struct scsi_task *inq_task;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001947 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001948 struct scsi_inquiry_block_limits *inq_bl;
Fam Zhengcc9743c2018-06-01 17:26:44 +08001949 struct scsi_inquiry_device_identification *inq_di;
Peter Lieven24d3bd62014-02-17 18:34:08 +01001950 switch (inq_vpd->pages[i]) {
1951 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1952 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1953 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1954 (void **) &inq_lbp, errp);
1955 if (inq_task == NULL) {
1956 ret = -EINVAL;
1957 goto out;
1958 }
1959 memcpy(&iscsilun->lbp, inq_lbp,
1960 sizeof(struct scsi_inquiry_logical_block_provisioning));
1961 scsi_free_scsi_task(inq_task);
1962 break;
1963 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1964 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1965 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1966 (void **) &inq_bl, errp);
1967 if (inq_task == NULL) {
1968 ret = -EINVAL;
1969 goto out;
1970 }
1971 memcpy(&iscsilun->bl, inq_bl,
1972 sizeof(struct scsi_inquiry_block_limits));
1973 scsi_free_scsi_task(inq_task);
1974 break;
Fam Zhengcc9743c2018-06-01 17:26:44 +08001975 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION:
1976 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1977 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION,
1978 (void **) &inq_di, errp);
1979 if (inq_task == NULL) {
1980 ret = -EINVAL;
1981 goto out;
1982 }
1983 iscsi_save_designator(iscsilun, inq_di);
1984 scsi_free_scsi_task(inq_task);
1985 break;
Peter Lieven24d3bd62014-02-17 18:34:08 +01001986 default:
1987 break;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001988 }
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001989 }
Peter Lieven24d3bd62014-02-17 18:34:08 +01001990 scsi_free_scsi_task(task);
1991 task = NULL;
Peter Lievenf18a7cb2013-07-19 09:19:39 +02001992
Paolo Bonzinid045c462017-02-22 19:07:25 +01001993 qemu_mutex_init(&iscsilun->mutex);
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02001994 iscsi_attach_aio_context(bs, iscsilun->aio_context);
Peter Lieven5b5d34e2012-12-06 10:46:47 +01001995
Peter Lievenb03c3802014-04-28 13:11:32 +02001996 /* Guess the internal cluster (page) size of the iscsi target by the means
1997 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1998 * reasonable size */
Peter Lieven3d2acaa2014-04-28 17:18:32 +02001999 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
Peter Lievenb03c3802014-04-28 13:11:32 +02002000 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
Eric Blakeba059e72018-02-13 14:26:46 -06002001 iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran *
2002 iscsilun->block_size;
Peter Lieven9eac3622015-04-16 16:08:33 +02002003 if (iscsilun->lbprz) {
Peter Lievene1123a32016-07-18 10:52:20 +02002004 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
Peter Lievenb03c3802014-04-28 13:11:32 +02002005 }
Peter Lievenb03c3802014-04-28 13:11:32 +02002006 }
2007
Eric Blakee24d8132018-01-26 13:34:39 -06002008 if (iscsilun->lbprz && iscsilun->lbp.lbpws) {
2009 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
2010 }
2011
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02002012out:
Kevin Wolf60beb342013-04-12 17:59:59 +02002013 qemu_opts_del(opts);
Markus Armbrusterf7047c22014-06-06 18:25:12 +02002014 g_free(initiator_name);
Peter Lievene829b0b2012-11-17 14:37:39 +01002015 if (task != NULL) {
2016 scsi_free_scsi_task(task);
2017 }
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02002018
2019 if (ret) {
2020 if (iscsi != NULL) {
Peter Lieven20474e92015-04-16 16:08:25 +02002021 if (iscsi_is_logged_in(iscsi)) {
2022 iscsi_logout_sync(iscsi);
2023 }
Paolo Bonzinib93c94f2012-08-06 10:52:22 +02002024 iscsi_destroy_context(iscsi);
2025 }
2026 memset(iscsilun, 0, sizeof(IscsiLun));
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002027 }
Markus Armbrusterdeadbb82018-06-14 21:14:27 +02002028
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002029 return ret;
2030}
2031
2032static void iscsi_close(BlockDriverState *bs)
2033{
2034 IscsiLun *iscsilun = bs->opaque;
2035 struct iscsi_context *iscsi = iscsilun->iscsi;
2036
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02002037 iscsi_detach_aio_context(bs);
Peter Lieven20474e92015-04-16 16:08:25 +02002038 if (iscsi_is_logged_in(iscsi)) {
2039 iscsi_logout_sync(iscsi);
2040 }
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002041 iscsi_destroy_context(iscsi);
Fam Zhengcc9743c2018-06-01 17:26:44 +08002042 if (iscsilun->dd) {
2043 g_free(iscsilun->dd->designator);
2044 g_free(iscsilun->dd);
2045 }
Peter Lievend4cd9612013-10-24 12:07:02 +02002046 g_free(iscsilun->zeroblock);
Peter Lievene1123a32016-07-18 10:52:20 +02002047 iscsi_allocmap_free(iscsilun);
Paolo Bonzinid045c462017-02-22 19:07:25 +01002048 qemu_mutex_destroy(&iscsilun->mutex);
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002049 memset(iscsilun, 0, sizeof(IscsiLun));
2050}
2051
Kevin Wolf3baca892014-07-16 17:48:16 +02002052static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
Kevin Wolfd34682c2013-12-11 19:26:16 +01002053{
Kevin Wolfd34682c2013-12-11 19:26:16 +01002054 /* We don't actually refresh here, but just return data queried in
2055 * iscsi_open(): iscsi targets don't change their limits. */
Peter Lieven52f6fa12014-10-27 10:18:45 +01002056
2057 IscsiLun *iscsilun = bs->opaque;
Eric Blake5def6b82016-06-23 16:37:19 -06002058 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
Eric Blake95eaa782016-09-07 16:27:20 -05002059 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size);
Peter Lieven52f6fa12014-10-27 10:18:45 +01002060
Eric Blake95eaa782016-09-07 16:27:20 -05002061 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg);
2062
2063 bs->bl.request_alignment = block_size;
Eric Blakec8b3b992016-06-23 16:37:14 -06002064
Peter Lieven52f6fa12014-10-27 10:18:45 +01002065 if (iscsilun->bl.max_xfer_len) {
2066 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
2067 }
2068
Eric Blake95eaa782016-09-07 16:27:20 -05002069 if (max_xfer_len * block_size < INT_MAX) {
Eric Blake5def6b82016-06-23 16:37:19 -06002070 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
2071 }
Peter Lieven52f6fa12014-10-27 10:18:45 +01002072
Paolo Bonzinic97ca292014-04-02 15:30:29 +02002073 if (iscsilun->lbp.lbpu) {
Eric Blake95eaa782016-09-07 16:27:20 -05002074 if (iscsilun->bl.max_unmap < 0xffffffff / block_size) {
Eric Blakeb9f78552016-06-23 16:37:21 -06002075 bs->bl.max_pdiscard =
2076 iscsilun->bl.max_unmap * iscsilun->block_size;
Kevin Wolfd34682c2013-12-11 19:26:16 +01002077 }
Eric Blakeb9f78552016-06-23 16:37:21 -06002078 bs->bl.pdiscard_alignment =
2079 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
Eric Blake8b184742016-06-01 15:10:01 -06002080 } else {
Eric Blakeb9f78552016-06-23 16:37:21 -06002081 bs->bl.pdiscard_alignment = iscsilun->block_size;
Paolo Bonzinic97ca292014-04-02 15:30:29 +02002082 }
Kevin Wolfd34682c2013-12-11 19:26:16 +01002083
Eric Blake95eaa782016-09-07 16:27:20 -05002084 if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) {
Eric Blakecf081fc2016-06-01 15:10:02 -06002085 bs->bl.max_pwrite_zeroes =
2086 iscsilun->bl.max_ws_len * iscsilun->block_size;
Paolo Bonzinic97ca292014-04-02 15:30:29 +02002087 }
2088 if (iscsilun->lbp.lbpws) {
Eric Blakecf081fc2016-06-01 15:10:02 -06002089 bs->bl.pwrite_zeroes_alignment =
2090 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
Eric Blake8b184742016-06-01 15:10:01 -06002091 } else {
Eric Blakecf081fc2016-06-01 15:10:02 -06002092 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
Kevin Wolfd34682c2013-12-11 19:26:16 +01002093 }
Eric Blake5def6b82016-06-23 16:37:19 -06002094 if (iscsilun->bl.opt_xfer_len &&
Eric Blake95eaa782016-09-07 16:27:20 -05002095 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) {
Eric Blake5def6b82016-06-23 16:37:19 -06002096 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
2097 iscsilun->block_size);
2098 }
Anthony Liguorie9f526a2014-01-24 15:50:14 -08002099}
Kevin Wolfd34682c2013-12-11 19:26:16 +01002100
Fam Zheng43ae8fb2015-02-25 12:40:08 +08002101/* Note that this will not re-establish a connection with an iSCSI target - it
2102 * is effectively a NOP. */
Jeff Codydc6afb92014-01-14 13:10:24 -05002103static int iscsi_reopen_prepare(BDRVReopenState *state,
2104 BlockReopenQueue *queue, Error **errp)
2105{
Fam Zheng43ae8fb2015-02-25 12:40:08 +08002106 IscsiLun *iscsilun = state->bs->opaque;
2107
2108 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
2109 error_setg(errp, "Cannot open a write protected LUN as read-write");
2110 return -EACCES;
2111 }
Kevin Wolfd34682c2013-12-11 19:26:16 +01002112 return 0;
2113}
2114
Peter Lievene1123a32016-07-18 10:52:20 +02002115static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
2116{
2117 IscsiLun *iscsilun = reopen_state->bs->opaque;
2118
2119 /* the cache.direct status might have changed */
2120 if (iscsilun->allocmap != NULL) {
2121 iscsi_allocmap_init(iscsilun, reopen_state->flags);
2122 }
2123}
2124
Kevin Wolf061ca8a2018-06-21 17:54:35 +02002125static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
2126 PreallocMode prealloc, Error **errp)
Peter Lievencb1b83e2013-02-18 14:50:46 +01002127{
2128 IscsiLun *iscsilun = bs->opaque;
Paolo Bonzinif2917852014-02-17 14:43:53 +01002129 Error *local_err = NULL;
Peter Lievencb1b83e2013-02-18 14:50:46 +01002130
Max Reitz8243ccb2017-06-13 22:20:52 +02002131 if (prealloc != PREALLOC_MODE_OFF) {
2132 error_setg(errp, "Unsupported preallocation mode '%s'",
Markus Armbruster977c7362017-08-24 10:46:08 +02002133 PreallocMode_str(prealloc));
Max Reitz8243ccb2017-06-13 22:20:52 +02002134 return -ENOTSUP;
2135 }
2136
Peter Lievencb1b83e2013-02-18 14:50:46 +01002137 if (iscsilun->type != TYPE_DISK) {
Max Reitzf59adb32017-03-28 22:51:29 +02002138 error_setg(errp, "Cannot resize non-disk iSCSI devices");
Peter Lievencb1b83e2013-02-18 14:50:46 +01002139 return -ENOTSUP;
2140 }
2141
Paolo Bonzinif2917852014-02-17 14:43:53 +01002142 iscsi_readcapacity_sync(iscsilun, &local_err);
2143 if (local_err != NULL) {
Max Reitz4bff28b2017-03-28 22:51:28 +02002144 error_propagate(errp, local_err);
Paolo Bonzinif2917852014-02-17 14:43:53 +01002145 return -EIO;
Peter Lievencb1b83e2013-02-18 14:50:46 +01002146 }
2147
2148 if (offset > iscsi_getlength(bs)) {
Max Reitzf59adb32017-03-28 22:51:29 +02002149 error_setg(errp, "Cannot grow iSCSI devices");
Peter Lievencb1b83e2013-02-18 14:50:46 +01002150 return -EINVAL;
2151 }
2152
Peter Lievene1123a32016-07-18 10:52:20 +02002153 if (iscsilun->allocmap != NULL) {
2154 iscsi_allocmap_init(iscsilun, bs->open_flags);
Peter Lievenb03c3802014-04-28 13:11:32 +02002155 }
2156
Peter Lievencb1b83e2013-02-18 14:50:46 +01002157 return 0;
2158}
2159
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01002160static int coroutine_fn iscsi_co_create_opts(const char *filename, QemuOpts *opts,
2161 Error **errp)
Peter Lievende8864e2012-11-17 16:13:24 +01002162{
2163 int ret = 0;
2164 int64_t total_size = 0;
Fam Zheng13c91cb2013-08-23 09:14:45 +08002165 BlockDriverState *bs;
Peter Lievende8864e2012-11-17 16:13:24 +01002166 IscsiLun *iscsilun = NULL;
Kevin Wolf60beb342013-04-12 17:59:59 +02002167 QDict *bs_options;
Fam Zheng2ec9a782017-04-10 15:54:51 +08002168 Error *local_err = NULL;
Peter Lievende8864e2012-11-17 16:13:24 +01002169
Markus Armbrustere4e99862014-10-07 13:59:03 +02002170 bs = bdrv_new();
Peter Lievende8864e2012-11-17 16:13:24 +01002171
2172 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +08002173 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2174 BDRV_SECTOR_SIZE);
Markus Armbruster5839e532014-08-19 10:31:08 +02002175 bs->opaque = g_new0(struct IscsiLun, 1);
Fam Zheng13c91cb2013-08-23 09:14:45 +08002176 iscsilun = bs->opaque;
Peter Lievende8864e2012-11-17 16:13:24 +01002177
Kevin Wolf60beb342013-04-12 17:59:59 +02002178 bs_options = qdict_new();
Fam Zheng2ec9a782017-04-10 15:54:51 +08002179 iscsi_parse_filename(filename, bs_options, &local_err);
2180 if (local_err) {
2181 error_propagate(errp, local_err);
2182 ret = -EINVAL;
2183 } else {
2184 ret = iscsi_open(bs, bs_options, 0, NULL);
2185 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02002186 qobject_unref(bs_options);
Kevin Wolf60beb342013-04-12 17:59:59 +02002187
Peter Lievende8864e2012-11-17 16:13:24 +01002188 if (ret != 0) {
2189 goto out;
2190 }
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02002191 iscsi_detach_aio_context(bs);
Peter Lievende8864e2012-11-17 16:13:24 +01002192 if (iscsilun->type != TYPE_DISK) {
2193 ret = -ENODEV;
2194 goto out;
2195 }
Fam Zheng13c91cb2013-08-23 09:14:45 +08002196 if (bs->total_sectors < total_size) {
Peter Lievende8864e2012-11-17 16:13:24 +01002197 ret = -ENOSPC;
Peter Lievend3bda7b2013-07-11 14:16:24 +02002198 goto out;
Peter Lievende8864e2012-11-17 16:13:24 +01002199 }
2200
2201 ret = 0;
2202out:
2203 if (iscsilun->iscsi != NULL) {
2204 iscsi_destroy_context(iscsilun->iscsi);
2205 }
Fam Zheng13c91cb2013-08-23 09:14:45 +08002206 g_free(bs->opaque);
2207 bs->opaque = NULL;
Fam Zheng4f6fd342013-08-23 09:14:47 +08002208 bdrv_unref(bs);
Peter Lievende8864e2012-11-17 16:13:24 +01002209 return ret;
2210}
2211
Peter Lieven186d4f22013-10-24 12:06:55 +02002212static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2213{
2214 IscsiLun *iscsilun = bs->opaque;
Peter Lieven0a386e42015-04-16 16:08:26 +02002215 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
Eric Blakeba059e72018-02-13 14:26:46 -06002216 bdi->cluster_size = iscsilun->cluster_size;
Peter Lieven186d4f22013-10-24 12:06:55 +02002217 return 0;
2218}
2219
Paolo Bonzini2b148f32018-03-01 17:36:18 +01002220static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
2221 Error **errp)
Peter Lievene1123a32016-07-18 10:52:20 +02002222{
2223 IscsiLun *iscsilun = bs->opaque;
2224 iscsi_allocmap_invalidate(iscsilun);
2225}
2226
Fam Zheng604dfaa2018-06-01 17:26:46 +08002227static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs,
2228 BdrvChild *src,
2229 uint64_t src_offset,
2230 BdrvChild *dst,
2231 uint64_t dst_offset,
2232 uint64_t bytes,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03002233 BdrvRequestFlags read_flags,
2234 BdrvRequestFlags write_flags)
Fam Zheng604dfaa2018-06-01 17:26:46 +08002235{
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03002236 return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
2237 read_flags, write_flags);
Fam Zheng604dfaa2018-06-01 17:26:46 +08002238}
2239
2240static struct scsi_task *iscsi_xcopy_task(int param_len)
2241{
2242 struct scsi_task *task;
2243
2244 task = g_new0(struct scsi_task, 1);
2245
2246 task->cdb[0] = EXTENDED_COPY;
2247 task->cdb[10] = (param_len >> 24) & 0xFF;
2248 task->cdb[11] = (param_len >> 16) & 0xFF;
2249 task->cdb[12] = (param_len >> 8) & 0xFF;
2250 task->cdb[13] = param_len & 0xFF;
2251 task->cdb_size = 16;
2252 task->xfer_dir = SCSI_XFER_WRITE;
2253 task->expxferlen = param_len;
2254
2255 return task;
2256}
2257
2258static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun)
2259{
2260 struct scsi_inquiry_device_designator *dd = lun->dd;
2261
2262 memset(desc, 0, 32);
2263 desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
2264 desc[4] = dd->code_set;
2265 desc[5] = (dd->designator_type & 0xF)
2266 | ((dd->association & 3) << 4);
2267 desc[7] = dd->designator_length;
Fam Zheng1439b9c2018-06-29 14:03:27 +08002268 memcpy(desc + 8, dd->designator, MIN(dd->designator_length, 20));
Fam Zheng604dfaa2018-06-01 17:26:46 +08002269
2270 desc[28] = 0;
2271 desc[29] = (lun->block_size >> 16) & 0xFF;
2272 desc[30] = (lun->block_size >> 8) & 0xFF;
2273 desc[31] = lun->block_size & 0xFF;
2274}
2275
2276static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index,
2277 int dst_index)
2278{
2279 hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
2280 hdr[1] = ((dc << 1) | cat) & 0xFF;
2281 hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF;
2282 /* don't account for the first 4 bytes in descriptor header*/
2283 hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
2284 hdr[4] = (src_index >> 8) & 0xFF;
2285 hdr[5] = src_index & 0xFF;
2286 hdr[6] = (dst_index >> 8) & 0xFF;
2287 hdr[7] = dst_index & 0xFF;
2288}
2289
2290static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat,
2291 int src_index, int dst_index, int num_blks,
2292 uint64_t src_lba, uint64_t dst_lba)
2293{
2294 iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index);
2295
2296 /* The caller should verify the request size */
2297 assert(num_blks < 65536);
2298 desc[10] = (num_blks >> 8) & 0xFF;
2299 desc[11] = num_blks & 0xFF;
2300 desc[12] = (src_lba >> 56) & 0xFF;
2301 desc[13] = (src_lba >> 48) & 0xFF;
2302 desc[14] = (src_lba >> 40) & 0xFF;
2303 desc[15] = (src_lba >> 32) & 0xFF;
2304 desc[16] = (src_lba >> 24) & 0xFF;
2305 desc[17] = (src_lba >> 16) & 0xFF;
2306 desc[18] = (src_lba >> 8) & 0xFF;
2307 desc[19] = src_lba & 0xFF;
2308 desc[20] = (dst_lba >> 56) & 0xFF;
2309 desc[21] = (dst_lba >> 48) & 0xFF;
2310 desc[22] = (dst_lba >> 40) & 0xFF;
2311 desc[23] = (dst_lba >> 32) & 0xFF;
2312 desc[24] = (dst_lba >> 24) & 0xFF;
2313 desc[25] = (dst_lba >> 16) & 0xFF;
2314 desc[26] = (dst_lba >> 8) & 0xFF;
2315 desc[27] = dst_lba & 0xFF;
2316}
2317
2318static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str,
2319 int list_id_usage, int prio,
2320 int tgt_desc_len,
2321 int seg_desc_len, int inline_data_len)
2322{
2323 buf[0] = list_id;
2324 buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
2325 buf[2] = (tgt_desc_len >> 8) & 0xFF;
2326 buf[3] = tgt_desc_len & 0xFF;
2327 buf[8] = (seg_desc_len >> 24) & 0xFF;
2328 buf[9] = (seg_desc_len >> 16) & 0xFF;
2329 buf[10] = (seg_desc_len >> 8) & 0xFF;
2330 buf[11] = seg_desc_len & 0xFF;
2331 buf[12] = (inline_data_len >> 24) & 0xFF;
2332 buf[13] = (inline_data_len >> 16) & 0xFF;
2333 buf[14] = (inline_data_len >> 8) & 0xFF;
2334 buf[15] = inline_data_len & 0xFF;
2335}
2336
2337static void iscsi_xcopy_data(struct iscsi_data *data,
2338 IscsiLun *src, int64_t src_lba,
2339 IscsiLun *dst, int64_t dst_lba,
2340 uint16_t num_blocks)
2341{
2342 uint8_t *buf;
2343 const int src_offset = XCOPY_DESC_OFFSET;
2344 const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE;
2345 const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE;
2346
2347 data->size = XCOPY_DESC_OFFSET +
2348 IDENT_DESCR_TGT_DESCR_SIZE * 2 +
2349 XCOPY_BLK2BLK_SEG_DESC_SIZE;
2350 data->data = g_malloc0(data->size);
2351 buf = data->data;
2352
2353 /* Initialise the parameter list header */
2354 iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
2355 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE,
2356 XCOPY_BLK2BLK_SEG_DESC_SIZE,
2357 0);
2358
2359 /* Initialise CSCD list with one src + one dst descriptor */
2360 iscsi_populate_target_desc(&buf[src_offset], src);
2361 iscsi_populate_target_desc(&buf[dst_offset], dst);
2362
2363 /* Initialise one segment descriptor */
2364 iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks,
2365 src_lba, dst_lba);
2366}
2367
2368static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs,
2369 BdrvChild *src,
2370 uint64_t src_offset,
2371 BdrvChild *dst,
2372 uint64_t dst_offset,
2373 uint64_t bytes,
Vladimir Sementsov-Ogievskiy67b51fb2018-07-09 19:37:17 +03002374 BdrvRequestFlags read_flags,
2375 BdrvRequestFlags write_flags)
Fam Zheng604dfaa2018-06-01 17:26:46 +08002376{
2377 IscsiLun *dst_lun = dst->bs->opaque;
2378 IscsiLun *src_lun;
2379 struct IscsiTask iscsi_task;
2380 struct iscsi_data data;
2381 int r = 0;
2382 int block_size;
2383
2384 if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) {
2385 return -ENOTSUP;
2386 }
2387 src_lun = src->bs->opaque;
2388
2389 if (!src_lun->dd || !dst_lun->dd) {
2390 return -ENOTSUP;
2391 }
2392 if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) {
2393 return -ENOTSUP;
2394 }
2395 if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) {
2396 return -ENOTSUP;
2397 }
2398 if (dst_lun->block_size != src_lun->block_size ||
2399 !dst_lun->block_size) {
2400 return -ENOTSUP;
2401 }
2402
2403 block_size = dst_lun->block_size;
2404 if (bytes / block_size > 65535) {
2405 return -ENOTSUP;
2406 }
2407
2408 iscsi_xcopy_data(&data,
2409 src_lun, src_offset / block_size,
2410 dst_lun, dst_offset / block_size,
2411 bytes / block_size);
2412
2413 iscsi_co_init_iscsitask(dst_lun, &iscsi_task);
2414
2415 qemu_mutex_lock(&dst_lun->mutex);
2416 iscsi_task.task = iscsi_xcopy_task(data.size);
2417retry:
2418 if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun,
2419 iscsi_task.task, iscsi_co_generic_cb,
2420 &data,
2421 &iscsi_task) != 0) {
2422 r = -EIO;
2423 goto out_unlock;
2424 }
2425
2426 iscsi_co_wait_for_task(&iscsi_task, dst_lun);
2427
2428 if (iscsi_task.do_retry) {
2429 iscsi_task.complete = 0;
2430 goto retry;
2431 }
2432
2433 if (iscsi_task.status != SCSI_STATUS_GOOD) {
2434 r = iscsi_task.err_code;
2435 goto out_unlock;
2436 }
2437
2438out_unlock:
Fam Zhengecc983a2018-07-10 14:31:16 +08002439
2440 trace_iscsi_xcopy(src_lun, src_offset, dst_lun, dst_offset, bytes, r);
Fam Zheng604dfaa2018-06-01 17:26:46 +08002441 g_free(iscsi_task.task);
2442 qemu_mutex_unlock(&dst_lun->mutex);
2443 g_free(iscsi_task.err_str);
2444 return r;
2445}
2446
Chunyan Liua59479e2014-06-05 17:20:55 +08002447static QemuOptsList iscsi_create_opts = {
2448 .name = "iscsi-create-opts",
2449 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
2450 .desc = {
2451 {
2452 .name = BLOCK_OPT_SIZE,
2453 .type = QEMU_OPT_SIZE,
2454 .help = "Virtual disk size"
2455 },
2456 { /* end of list */ }
2457 }
Peter Lievende8864e2012-11-17 16:13:24 +01002458};
2459
Max Reitz26542672019-02-01 20:29:25 +01002460static const char *const iscsi_strong_runtime_opts[] = {
2461 "transport",
2462 "portal",
2463 "target",
2464 "user",
2465 "password",
2466 "password-secret",
2467 "lun",
2468 "initiator-name",
2469 "header-digest",
2470
2471 NULL
2472};
2473
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002474static BlockDriver bdrv_iscsi = {
2475 .format_name = "iscsi",
2476 .protocol_name = "iscsi",
2477
Kevin Wolfd5895fc2016-12-08 14:23:06 +01002478 .instance_size = sizeof(IscsiLun),
2479 .bdrv_parse_filename = iscsi_parse_filename,
2480 .bdrv_file_open = iscsi_open,
2481 .bdrv_close = iscsi_close,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01002482 .bdrv_co_create_opts = iscsi_co_create_opts,
Kevin Wolfd5895fc2016-12-08 14:23:06 +01002483 .create_opts = &iscsi_create_opts,
2484 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2485 .bdrv_reopen_commit = iscsi_reopen_commit,
Paolo Bonzini2b148f32018-03-01 17:36:18 +01002486 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002487
2488 .bdrv_getlength = iscsi_getlength,
Peter Lieven186d4f22013-10-24 12:06:55 +02002489 .bdrv_get_info = iscsi_get_info,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02002490 .bdrv_co_truncate = iscsi_co_truncate,
Kevin Wolfd34682c2013-12-11 19:26:16 +01002491 .bdrv_refresh_limits = iscsi_refresh_limits,
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002492
Eric Blake92809c32018-02-13 14:26:48 -06002493 .bdrv_co_block_status = iscsi_co_block_status,
Eric Blake97c7e852016-07-15 17:23:01 -06002494 .bdrv_co_pdiscard = iscsi_co_pdiscard,
Fam Zheng604dfaa2018-06-01 17:26:46 +08002495 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2496 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
Eric Blake94d047a2016-06-01 15:10:05 -06002497 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
Peter Lieven063c3372013-12-05 16:47:17 +01002498 .bdrv_co_readv = iscsi_co_readv,
Eric Blakee18a58b2018-04-24 17:01:57 -05002499 .bdrv_co_writev = iscsi_co_writev,
Peter Lieven063c3372013-12-05 16:47:17 +01002500 .bdrv_co_flush_to_disk = iscsi_co_flush,
Ronnie Sahlbergfa6acb02012-04-24 16:29:04 +10002501
Ronnie Sahlberg98392452012-05-25 21:59:01 +10002502#ifdef __linux__
Ronnie Sahlberg98392452012-05-25 21:59:01 +10002503 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2504#endif
Stefan Hajnoczi80cf6252014-05-08 16:34:42 +02002505
2506 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2507 .bdrv_attach_aio_context = iscsi_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01002508
2509 .strong_runtime_opts = iscsi_strong_runtime_opts,
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002510};
2511
Roy Shtermane0ae4982016-10-09 11:14:56 +03002512#if LIBISCSI_API_VERSION >= (20160603)
2513static BlockDriver bdrv_iser = {
2514 .format_name = "iser",
2515 .protocol_name = "iser",
2516
Kevin Wolfd5895fc2016-12-08 14:23:06 +01002517 .instance_size = sizeof(IscsiLun),
2518 .bdrv_parse_filename = iscsi_parse_filename,
2519 .bdrv_file_open = iscsi_open,
2520 .bdrv_close = iscsi_close,
Stefan Hajnocziefc75e22018-01-18 13:43:45 +01002521 .bdrv_co_create_opts = iscsi_co_create_opts,
Kevin Wolfd5895fc2016-12-08 14:23:06 +01002522 .create_opts = &iscsi_create_opts,
2523 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2524 .bdrv_reopen_commit = iscsi_reopen_commit,
Paolo Bonzinib16a7cf2018-03-15 15:30:56 +01002525 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002526
2527 .bdrv_getlength = iscsi_getlength,
2528 .bdrv_get_info = iscsi_get_info,
Kevin Wolf061ca8a2018-06-21 17:54:35 +02002529 .bdrv_co_truncate = iscsi_co_truncate,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002530 .bdrv_refresh_limits = iscsi_refresh_limits,
2531
Eric Blake92809c32018-02-13 14:26:48 -06002532 .bdrv_co_block_status = iscsi_co_block_status,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002533 .bdrv_co_pdiscard = iscsi_co_pdiscard,
Fam Zheng604dfaa2018-06-01 17:26:46 +08002534 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2535 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002536 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2537 .bdrv_co_readv = iscsi_co_readv,
Eric Blakee18a58b2018-04-24 17:01:57 -05002538 .bdrv_co_writev = iscsi_co_writev,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002539 .bdrv_co_flush_to_disk = iscsi_co_flush,
2540
2541#ifdef __linux__
2542 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2543#endif
2544
2545 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2546 .bdrv_attach_aio_context = iscsi_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01002547
2548 .strong_runtime_opts = iscsi_strong_runtime_opts,
Roy Shtermane0ae4982016-10-09 11:14:56 +03002549};
2550#endif
2551
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002552static void iscsi_block_init(void)
2553{
2554 bdrv_register(&bdrv_iscsi);
Roy Shtermane0ae4982016-10-09 11:14:56 +03002555#if LIBISCSI_API_VERSION >= (20160603)
2556 bdrv_register(&bdrv_iser);
2557#endif
Ronnie Sahlbergc589b242011-10-25 19:24:24 +11002558}
2559
2560block_init(iscsi_block_init);