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