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