]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU Block driver for iSCSI images | |
3 | * | |
4 | * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]> | |
5 | * Copyright (c) 2012-2013 Peter Lieven <[email protected]> | |
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 "config-host.h" | |
27 | ||
28 | #include <poll.h> | |
29 | #include <arpa/inet.h> | |
30 | #include "qemu-common.h" | |
31 | #include "qemu/config-file.h" | |
32 | #include "qemu/error-report.h" | |
33 | #include "block/block_int.h" | |
34 | #include "trace.h" | |
35 | #include "block/scsi.h" | |
36 | #include "qemu/iov.h" | |
37 | #include "sysemu/sysemu.h" | |
38 | #include "qmp-commands.h" | |
39 | ||
40 | #include <iscsi/iscsi.h> | |
41 | #include <iscsi/scsi-lowlevel.h> | |
42 | ||
43 | #ifdef __linux__ | |
44 | #include <scsi/sg.h> | |
45 | #include <block/scsi.h> | |
46 | #endif | |
47 | ||
48 | typedef struct IscsiLun { | |
49 | struct iscsi_context *iscsi; | |
50 | int lun; | |
51 | enum scsi_inquiry_peripheral_device_type type; | |
52 | int block_size; | |
53 | uint64_t num_blocks; | |
54 | int events; | |
55 | QEMUTimer *nop_timer; | |
56 | uint8_t lbpme; | |
57 | uint8_t lbprz; | |
58 | uint8_t has_write_same; | |
59 | struct scsi_inquiry_logical_block_provisioning lbp; | |
60 | struct scsi_inquiry_block_limits bl; | |
61 | unsigned char *zeroblock; | |
62 | } IscsiLun; | |
63 | ||
64 | typedef struct IscsiTask { | |
65 | int status; | |
66 | int complete; | |
67 | int retries; | |
68 | int do_retry; | |
69 | struct scsi_task *task; | |
70 | Coroutine *co; | |
71 | QEMUBH *bh; | |
72 | } IscsiTask; | |
73 | ||
74 | typedef struct IscsiAIOCB { | |
75 | BlockDriverAIOCB common; | |
76 | QEMUIOVector *qiov; | |
77 | QEMUBH *bh; | |
78 | IscsiLun *iscsilun; | |
79 | struct scsi_task *task; | |
80 | uint8_t *buf; | |
81 | int status; | |
82 | int canceled; | |
83 | int retries; | |
84 | int64_t sector_num; | |
85 | int nb_sectors; | |
86 | #ifdef __linux__ | |
87 | sg_io_hdr_t *ioh; | |
88 | #endif | |
89 | } IscsiAIOCB; | |
90 | ||
91 | #define NOP_INTERVAL 5000 | |
92 | #define MAX_NOP_FAILURES 3 | |
93 | #define ISCSI_CMD_RETRIES 5 | |
94 | ||
95 | static void | |
96 | iscsi_bh_cb(void *p) | |
97 | { | |
98 | IscsiAIOCB *acb = p; | |
99 | ||
100 | qemu_bh_delete(acb->bh); | |
101 | ||
102 | g_free(acb->buf); | |
103 | acb->buf = NULL; | |
104 | ||
105 | if (acb->canceled == 0) { | |
106 | acb->common.cb(acb->common.opaque, acb->status); | |
107 | } | |
108 | ||
109 | if (acb->task != NULL) { | |
110 | scsi_free_scsi_task(acb->task); | |
111 | acb->task = NULL; | |
112 | } | |
113 | ||
114 | qemu_aio_release(acb); | |
115 | } | |
116 | ||
117 | static void | |
118 | iscsi_schedule_bh(IscsiAIOCB *acb) | |
119 | { | |
120 | if (acb->bh) { | |
121 | return; | |
122 | } | |
123 | acb->bh = qemu_bh_new(iscsi_bh_cb, acb); | |
124 | qemu_bh_schedule(acb->bh); | |
125 | } | |
126 | ||
127 | static void iscsi_co_generic_bh_cb(void *opaque) | |
128 | { | |
129 | struct IscsiTask *iTask = opaque; | |
130 | qemu_bh_delete(iTask->bh); | |
131 | qemu_coroutine_enter(iTask->co, NULL); | |
132 | } | |
133 | ||
134 | static void | |
135 | iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, | |
136 | void *command_data, void *opaque) | |
137 | { | |
138 | struct IscsiTask *iTask = opaque; | |
139 | struct scsi_task *task = command_data; | |
140 | ||
141 | iTask->complete = 1; | |
142 | iTask->status = status; | |
143 | iTask->do_retry = 0; | |
144 | iTask->task = task; | |
145 | ||
146 | if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION | |
147 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { | |
148 | error_report("iSCSI CheckCondition: %s", iscsi_get_error(iscsi)); | |
149 | iTask->do_retry = 1; | |
150 | goto out; | |
151 | } | |
152 | ||
153 | if (status != SCSI_STATUS_GOOD) { | |
154 | error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); | |
155 | } | |
156 | ||
157 | out: | |
158 | if (iTask->co) { | |
159 | iTask->bh = qemu_bh_new(iscsi_co_generic_bh_cb, iTask); | |
160 | qemu_bh_schedule(iTask->bh); | |
161 | } | |
162 | } | |
163 | ||
164 | static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) | |
165 | { | |
166 | *iTask = (struct IscsiTask) { | |
167 | .co = qemu_coroutine_self(), | |
168 | .retries = ISCSI_CMD_RETRIES, | |
169 | }; | |
170 | } | |
171 | ||
172 | static void | |
173 | iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, | |
174 | void *private_data) | |
175 | { | |
176 | IscsiAIOCB *acb = private_data; | |
177 | ||
178 | acb->status = -ECANCELED; | |
179 | iscsi_schedule_bh(acb); | |
180 | } | |
181 | ||
182 | static void | |
183 | iscsi_aio_cancel(BlockDriverAIOCB *blockacb) | |
184 | { | |
185 | IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; | |
186 | IscsiLun *iscsilun = acb->iscsilun; | |
187 | ||
188 | if (acb->status != -EINPROGRESS) { | |
189 | return; | |
190 | } | |
191 | ||
192 | acb->canceled = 1; | |
193 | ||
194 | /* send a task mgmt call to the target to cancel the task on the target */ | |
195 | iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, | |
196 | iscsi_abort_task_cb, acb); | |
197 | ||
198 | while (acb->status == -EINPROGRESS) { | |
199 | qemu_aio_wait(); | |
200 | } | |
201 | } | |
202 | ||
203 | static const AIOCBInfo iscsi_aiocb_info = { | |
204 | .aiocb_size = sizeof(IscsiAIOCB), | |
205 | .cancel = iscsi_aio_cancel, | |
206 | }; | |
207 | ||
208 | ||
209 | static void iscsi_process_read(void *arg); | |
210 | static void iscsi_process_write(void *arg); | |
211 | ||
212 | static void | |
213 | iscsi_set_events(IscsiLun *iscsilun) | |
214 | { | |
215 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
216 | int ev; | |
217 | ||
218 | /* We always register a read handler. */ | |
219 | ev = POLLIN; | |
220 | ev |= iscsi_which_events(iscsi); | |
221 | if (ev != iscsilun->events) { | |
222 | qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), | |
223 | iscsi_process_read, | |
224 | (ev & POLLOUT) ? iscsi_process_write : NULL, | |
225 | iscsilun); | |
226 | ||
227 | } | |
228 | ||
229 | iscsilun->events = ev; | |
230 | } | |
231 | ||
232 | static void | |
233 | iscsi_process_read(void *arg) | |
234 | { | |
235 | IscsiLun *iscsilun = arg; | |
236 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
237 | ||
238 | iscsi_service(iscsi, POLLIN); | |
239 | iscsi_set_events(iscsilun); | |
240 | } | |
241 | ||
242 | static void | |
243 | iscsi_process_write(void *arg) | |
244 | { | |
245 | IscsiLun *iscsilun = arg; | |
246 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
247 | ||
248 | iscsi_service(iscsi, POLLOUT); | |
249 | iscsi_set_events(iscsilun); | |
250 | } | |
251 | ||
252 | static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) | |
253 | { | |
254 | return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; | |
255 | } | |
256 | ||
257 | static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) | |
258 | { | |
259 | return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; | |
260 | } | |
261 | ||
262 | static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors, | |
263 | IscsiLun *iscsilun) | |
264 | { | |
265 | if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size || | |
266 | (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) { | |
267 | error_report("iSCSI misaligned request: " | |
268 | "iscsilun->block_size %u, sector_num %" PRIi64 | |
269 | ", nb_sectors %d", | |
270 | iscsilun->block_size, sector_num, nb_sectors); | |
271 | return 0; | |
272 | } | |
273 | return 1; | |
274 | } | |
275 | ||
276 | static int coroutine_fn iscsi_co_writev(BlockDriverState *bs, | |
277 | int64_t sector_num, int nb_sectors, | |
278 | QEMUIOVector *iov) | |
279 | { | |
280 | IscsiLun *iscsilun = bs->opaque; | |
281 | struct IscsiTask iTask; | |
282 | uint64_t lba; | |
283 | uint32_t num_sectors; | |
284 | uint8_t *data = NULL; | |
285 | uint8_t *buf = NULL; | |
286 | ||
287 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
288 | return -EINVAL; | |
289 | } | |
290 | ||
291 | lba = sector_qemu2lun(sector_num, iscsilun); | |
292 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
293 | #if !defined(LIBISCSI_FEATURE_IOVECTOR) | |
294 | /* if the iovec only contains one buffer we can pass it directly */ | |
295 | if (iov->niov == 1) { | |
296 | data = iov->iov[0].iov_base; | |
297 | } else { | |
298 | size_t size = MIN(nb_sectors * BDRV_SECTOR_SIZE, iov->size); | |
299 | buf = g_malloc(size); | |
300 | qemu_iovec_to_buf(iov, 0, buf, size); | |
301 | data = buf; | |
302 | } | |
303 | #endif | |
304 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
305 | retry: | |
306 | iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
307 | data, num_sectors * iscsilun->block_size, | |
308 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
309 | iscsi_co_generic_cb, &iTask); | |
310 | if (iTask.task == NULL) { | |
311 | g_free(buf); | |
312 | return -ENOMEM; | |
313 | } | |
314 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
315 | scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, | |
316 | iov->niov); | |
317 | #endif | |
318 | while (!iTask.complete) { | |
319 | iscsi_set_events(iscsilun); | |
320 | qemu_coroutine_yield(); | |
321 | } | |
322 | ||
323 | if (iTask.task != NULL) { | |
324 | scsi_free_scsi_task(iTask.task); | |
325 | iTask.task = NULL; | |
326 | } | |
327 | ||
328 | if (iTask.do_retry) { | |
329 | iTask.complete = 0; | |
330 | goto retry; | |
331 | } | |
332 | ||
333 | g_free(buf); | |
334 | ||
335 | if (iTask.status != SCSI_STATUS_GOOD) { | |
336 | return -EIO; | |
337 | } | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, | |
343 | int64_t sector_num, int nb_sectors, | |
344 | QEMUIOVector *iov) | |
345 | { | |
346 | IscsiLun *iscsilun = bs->opaque; | |
347 | struct IscsiTask iTask; | |
348 | uint64_t lba; | |
349 | uint32_t num_sectors; | |
350 | #if !defined(LIBISCSI_FEATURE_IOVECTOR) | |
351 | int i; | |
352 | #endif | |
353 | ||
354 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
355 | return -EINVAL; | |
356 | } | |
357 | ||
358 | lba = sector_qemu2lun(sector_num, iscsilun); | |
359 | num_sectors = sector_qemu2lun(nb_sectors, iscsilun); | |
360 | ||
361 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
362 | retry: | |
363 | switch (iscsilun->type) { | |
364 | case TYPE_DISK: | |
365 | iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
366 | num_sectors * iscsilun->block_size, | |
367 | iscsilun->block_size, 0, 0, 0, 0, 0, | |
368 | iscsi_co_generic_cb, &iTask); | |
369 | break; | |
370 | default: | |
371 | iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, | |
372 | num_sectors * iscsilun->block_size, | |
373 | iscsilun->block_size, | |
374 | #if !defined(CONFIG_LIBISCSI_1_4) /* API change from 1.4.0 to 1.5.0 */ | |
375 | 0, 0, 0, 0, 0, | |
376 | #endif | |
377 | iscsi_co_generic_cb, &iTask); | |
378 | break; | |
379 | } | |
380 | if (iTask.task == NULL) { | |
381 | return -ENOMEM; | |
382 | } | |
383 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
384 | scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); | |
385 | #else | |
386 | for (i = 0; i < iov->niov; i++) { | |
387 | scsi_task_add_data_in_buffer(iTask.task, | |
388 | iov->iov[i].iov_len, | |
389 | iov->iov[i].iov_base); | |
390 | } | |
391 | #endif | |
392 | ||
393 | while (!iTask.complete) { | |
394 | iscsi_set_events(iscsilun); | |
395 | qemu_coroutine_yield(); | |
396 | } | |
397 | ||
398 | if (iTask.task != NULL) { | |
399 | scsi_free_scsi_task(iTask.task); | |
400 | iTask.task = NULL; | |
401 | } | |
402 | ||
403 | if (iTask.do_retry) { | |
404 | iTask.complete = 0; | |
405 | goto retry; | |
406 | } | |
407 | ||
408 | if (iTask.status != SCSI_STATUS_GOOD) { | |
409 | return -EIO; | |
410 | } | |
411 | ||
412 | return 0; | |
413 | } | |
414 | ||
415 | static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) | |
416 | { | |
417 | IscsiLun *iscsilun = bs->opaque; | |
418 | struct IscsiTask iTask; | |
419 | ||
420 | if (bs->sg) { | |
421 | return 0; | |
422 | } | |
423 | ||
424 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
425 | ||
426 | retry: | |
427 | if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, | |
428 | 0, iscsi_co_generic_cb, &iTask) == NULL) { | |
429 | return -ENOMEM; | |
430 | } | |
431 | ||
432 | while (!iTask.complete) { | |
433 | iscsi_set_events(iscsilun); | |
434 | qemu_coroutine_yield(); | |
435 | } | |
436 | ||
437 | if (iTask.task != NULL) { | |
438 | scsi_free_scsi_task(iTask.task); | |
439 | iTask.task = NULL; | |
440 | } | |
441 | ||
442 | if (iTask.do_retry) { | |
443 | iTask.complete = 0; | |
444 | goto retry; | |
445 | } | |
446 | ||
447 | if (iTask.status != SCSI_STATUS_GOOD) { | |
448 | return -EIO; | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | #ifdef __linux__ | |
455 | static void | |
456 | iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, | |
457 | void *command_data, void *opaque) | |
458 | { | |
459 | IscsiAIOCB *acb = opaque; | |
460 | ||
461 | g_free(acb->buf); | |
462 | acb->buf = NULL; | |
463 | ||
464 | if (acb->canceled != 0) { | |
465 | return; | |
466 | } | |
467 | ||
468 | acb->status = 0; | |
469 | if (status < 0) { | |
470 | error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", | |
471 | iscsi_get_error(iscsi)); | |
472 | acb->status = -EIO; | |
473 | } | |
474 | ||
475 | acb->ioh->driver_status = 0; | |
476 | acb->ioh->host_status = 0; | |
477 | acb->ioh->resid = 0; | |
478 | ||
479 | #define SG_ERR_DRIVER_SENSE 0x08 | |
480 | ||
481 | if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { | |
482 | int ss; | |
483 | ||
484 | acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; | |
485 | ||
486 | acb->ioh->sb_len_wr = acb->task->datain.size - 2; | |
487 | ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? | |
488 | acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; | |
489 | memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); | |
490 | } | |
491 | ||
492 | iscsi_schedule_bh(acb); | |
493 | } | |
494 | ||
495 | static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, | |
496 | unsigned long int req, void *buf, | |
497 | BlockDriverCompletionFunc *cb, void *opaque) | |
498 | { | |
499 | IscsiLun *iscsilun = bs->opaque; | |
500 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
501 | struct iscsi_data data; | |
502 | IscsiAIOCB *acb; | |
503 | ||
504 | assert(req == SG_IO); | |
505 | ||
506 | acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); | |
507 | ||
508 | acb->iscsilun = iscsilun; | |
509 | acb->canceled = 0; | |
510 | acb->bh = NULL; | |
511 | acb->status = -EINPROGRESS; | |
512 | acb->buf = NULL; | |
513 | acb->ioh = buf; | |
514 | ||
515 | acb->task = malloc(sizeof(struct scsi_task)); | |
516 | if (acb->task == NULL) { | |
517 | error_report("iSCSI: Failed to allocate task for scsi command. %s", | |
518 | iscsi_get_error(iscsi)); | |
519 | qemu_aio_release(acb); | |
520 | return NULL; | |
521 | } | |
522 | memset(acb->task, 0, sizeof(struct scsi_task)); | |
523 | ||
524 | switch (acb->ioh->dxfer_direction) { | |
525 | case SG_DXFER_TO_DEV: | |
526 | acb->task->xfer_dir = SCSI_XFER_WRITE; | |
527 | break; | |
528 | case SG_DXFER_FROM_DEV: | |
529 | acb->task->xfer_dir = SCSI_XFER_READ; | |
530 | break; | |
531 | default: | |
532 | acb->task->xfer_dir = SCSI_XFER_NONE; | |
533 | break; | |
534 | } | |
535 | ||
536 | acb->task->cdb_size = acb->ioh->cmd_len; | |
537 | memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); | |
538 | acb->task->expxferlen = acb->ioh->dxfer_len; | |
539 | ||
540 | data.size = 0; | |
541 | if (acb->task->xfer_dir == SCSI_XFER_WRITE) { | |
542 | if (acb->ioh->iovec_count == 0) { | |
543 | data.data = acb->ioh->dxferp; | |
544 | data.size = acb->ioh->dxfer_len; | |
545 | } else { | |
546 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
547 | scsi_task_set_iov_out(acb->task, | |
548 | (struct scsi_iovec *) acb->ioh->dxferp, | |
549 | acb->ioh->iovec_count); | |
550 | #else | |
551 | struct iovec *iov = (struct iovec *)acb->ioh->dxferp; | |
552 | ||
553 | acb->buf = g_malloc(acb->ioh->dxfer_len); | |
554 | data.data = acb->buf; | |
555 | data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0, | |
556 | acb->buf, acb->ioh->dxfer_len); | |
557 | #endif | |
558 | } | |
559 | } | |
560 | ||
561 | if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, | |
562 | iscsi_aio_ioctl_cb, | |
563 | (data.size > 0) ? &data : NULL, | |
564 | acb) != 0) { | |
565 | scsi_free_scsi_task(acb->task); | |
566 | qemu_aio_release(acb); | |
567 | return NULL; | |
568 | } | |
569 | ||
570 | /* tell libiscsi to read straight into the buffer we got from ioctl */ | |
571 | if (acb->task->xfer_dir == SCSI_XFER_READ) { | |
572 | if (acb->ioh->iovec_count == 0) { | |
573 | scsi_task_add_data_in_buffer(acb->task, | |
574 | acb->ioh->dxfer_len, | |
575 | acb->ioh->dxferp); | |
576 | } else { | |
577 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
578 | scsi_task_set_iov_in(acb->task, | |
579 | (struct scsi_iovec *) acb->ioh->dxferp, | |
580 | acb->ioh->iovec_count); | |
581 | #else | |
582 | int i; | |
583 | for (i = 0; i < acb->ioh->iovec_count; i++) { | |
584 | struct iovec *iov = (struct iovec *)acb->ioh->dxferp; | |
585 | ||
586 | scsi_task_add_data_in_buffer(acb->task, | |
587 | iov[i].iov_len, | |
588 | iov[i].iov_base); | |
589 | } | |
590 | #endif | |
591 | } | |
592 | } | |
593 | ||
594 | iscsi_set_events(iscsilun); | |
595 | ||
596 | return &acb->common; | |
597 | } | |
598 | ||
599 | ||
600 | static void ioctl_cb(void *opaque, int status) | |
601 | { | |
602 | int *p_status = opaque; | |
603 | *p_status = status; | |
604 | } | |
605 | ||
606 | static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
607 | { | |
608 | IscsiLun *iscsilun = bs->opaque; | |
609 | int status; | |
610 | ||
611 | switch (req) { | |
612 | case SG_GET_VERSION_NUM: | |
613 | *(int *)buf = 30000; | |
614 | break; | |
615 | case SG_GET_SCSI_ID: | |
616 | ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; | |
617 | break; | |
618 | case SG_IO: | |
619 | status = -EINPROGRESS; | |
620 | iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status); | |
621 | ||
622 | while (status == -EINPROGRESS) { | |
623 | qemu_aio_wait(); | |
624 | } | |
625 | ||
626 | return 0; | |
627 | default: | |
628 | return -1; | |
629 | } | |
630 | return 0; | |
631 | } | |
632 | #endif | |
633 | ||
634 | static int64_t | |
635 | iscsi_getlength(BlockDriverState *bs) | |
636 | { | |
637 | IscsiLun *iscsilun = bs->opaque; | |
638 | int64_t len; | |
639 | ||
640 | len = iscsilun->num_blocks; | |
641 | len *= iscsilun->block_size; | |
642 | ||
643 | return len; | |
644 | } | |
645 | ||
646 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
647 | ||
648 | static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, | |
649 | int64_t sector_num, | |
650 | int nb_sectors, int *pnum) | |
651 | { | |
652 | IscsiLun *iscsilun = bs->opaque; | |
653 | struct scsi_get_lba_status *lbas = NULL; | |
654 | struct scsi_lba_status_descriptor *lbasd = NULL; | |
655 | struct IscsiTask iTask; | |
656 | int64_t ret; | |
657 | ||
658 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
659 | ||
660 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
661 | ret = -EINVAL; | |
662 | goto out; | |
663 | } | |
664 | ||
665 | /* default to all sectors allocated */ | |
666 | ret = BDRV_BLOCK_DATA; | |
667 | ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; | |
668 | *pnum = nb_sectors; | |
669 | ||
670 | /* LUN does not support logical block provisioning */ | |
671 | if (iscsilun->lbpme == 0) { | |
672 | goto out; | |
673 | } | |
674 | ||
675 | retry: | |
676 | if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, | |
677 | sector_qemu2lun(sector_num, iscsilun), | |
678 | 8 + 16, iscsi_co_generic_cb, | |
679 | &iTask) == NULL) { | |
680 | ret = -ENOMEM; | |
681 | goto out; | |
682 | } | |
683 | ||
684 | while (!iTask.complete) { | |
685 | iscsi_set_events(iscsilun); | |
686 | qemu_coroutine_yield(); | |
687 | } | |
688 | ||
689 | if (iTask.do_retry) { | |
690 | if (iTask.task != NULL) { | |
691 | scsi_free_scsi_task(iTask.task); | |
692 | iTask.task = NULL; | |
693 | } | |
694 | iTask.complete = 0; | |
695 | goto retry; | |
696 | } | |
697 | ||
698 | if (iTask.status != SCSI_STATUS_GOOD) { | |
699 | /* in case the get_lba_status_callout fails (i.e. | |
700 | * because the device is busy or the cmd is not | |
701 | * supported) we pretend all blocks are allocated | |
702 | * for backwards compatibility */ | |
703 | goto out; | |
704 | } | |
705 | ||
706 | lbas = scsi_datain_unmarshall(iTask.task); | |
707 | if (lbas == NULL) { | |
708 | ret = -EIO; | |
709 | goto out; | |
710 | } | |
711 | ||
712 | lbasd = &lbas->descriptors[0]; | |
713 | ||
714 | if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { | |
715 | ret = -EIO; | |
716 | goto out; | |
717 | } | |
718 | ||
719 | *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); | |
720 | if (*pnum > nb_sectors) { | |
721 | *pnum = nb_sectors; | |
722 | } | |
723 | ||
724 | if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || | |
725 | lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { | |
726 | ret &= ~BDRV_BLOCK_DATA; | |
727 | if (iscsilun->lbprz) { | |
728 | ret |= BDRV_BLOCK_ZERO; | |
729 | } | |
730 | } | |
731 | ||
732 | out: | |
733 | if (iTask.task != NULL) { | |
734 | scsi_free_scsi_task(iTask.task); | |
735 | } | |
736 | return ret; | |
737 | } | |
738 | ||
739 | #endif /* LIBISCSI_FEATURE_IOVECTOR */ | |
740 | ||
741 | static int | |
742 | coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num, | |
743 | int nb_sectors) | |
744 | { | |
745 | IscsiLun *iscsilun = bs->opaque; | |
746 | struct IscsiTask iTask; | |
747 | struct unmap_list list; | |
748 | ||
749 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
750 | return -EINVAL; | |
751 | } | |
752 | ||
753 | if (!iscsilun->lbp.lbpu) { | |
754 | /* UNMAP is not supported by the target */ | |
755 | return 0; | |
756 | } | |
757 | ||
758 | list.lba = sector_qemu2lun(sector_num, iscsilun); | |
759 | list.num = sector_qemu2lun(nb_sectors, iscsilun); | |
760 | ||
761 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
762 | retry: | |
763 | if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, | |
764 | iscsi_co_generic_cb, &iTask) == NULL) { | |
765 | return -ENOMEM; | |
766 | } | |
767 | ||
768 | while (!iTask.complete) { | |
769 | iscsi_set_events(iscsilun); | |
770 | qemu_coroutine_yield(); | |
771 | } | |
772 | ||
773 | if (iTask.task != NULL) { | |
774 | scsi_free_scsi_task(iTask.task); | |
775 | iTask.task = NULL; | |
776 | } | |
777 | ||
778 | if (iTask.do_retry) { | |
779 | iTask.complete = 0; | |
780 | goto retry; | |
781 | } | |
782 | ||
783 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { | |
784 | /* the target might fail with a check condition if it | |
785 | is not happy with the alignment of the UNMAP request | |
786 | we silently fail in this case */ | |
787 | return 0; | |
788 | } | |
789 | ||
790 | if (iTask.status != SCSI_STATUS_GOOD) { | |
791 | return -EIO; | |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
797 | #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED) | |
798 | ||
799 | static int | |
800 | coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, | |
801 | int nb_sectors, BdrvRequestFlags flags) | |
802 | { | |
803 | IscsiLun *iscsilun = bs->opaque; | |
804 | struct IscsiTask iTask; | |
805 | uint64_t lba; | |
806 | uint32_t nb_blocks; | |
807 | ||
808 | if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { | |
809 | return -EINVAL; | |
810 | } | |
811 | ||
812 | if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { | |
813 | /* WRITE SAME without UNMAP is not supported by the target */ | |
814 | return -ENOTSUP; | |
815 | } | |
816 | ||
817 | if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) { | |
818 | /* WRITE SAME with UNMAP is not supported by the target */ | |
819 | return -ENOTSUP; | |
820 | } | |
821 | ||
822 | lba = sector_qemu2lun(sector_num, iscsilun); | |
823 | nb_blocks = sector_qemu2lun(nb_sectors, iscsilun); | |
824 | ||
825 | if (iscsilun->zeroblock == NULL) { | |
826 | iscsilun->zeroblock = g_malloc0(iscsilun->block_size); | |
827 | } | |
828 | ||
829 | iscsi_co_init_iscsitask(iscsilun, &iTask); | |
830 | retry: | |
831 | if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, | |
832 | iscsilun->zeroblock, iscsilun->block_size, | |
833 | nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), | |
834 | 0, 0, iscsi_co_generic_cb, &iTask) == NULL) { | |
835 | return -ENOMEM; | |
836 | } | |
837 | ||
838 | while (!iTask.complete) { | |
839 | iscsi_set_events(iscsilun); | |
840 | qemu_coroutine_yield(); | |
841 | } | |
842 | ||
843 | if (iTask.status == SCSI_STATUS_CHECK_CONDITION && | |
844 | iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && | |
845 | (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || | |
846 | iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { | |
847 | /* WRITE SAME is not supported by the target */ | |
848 | iscsilun->has_write_same = false; | |
849 | scsi_free_scsi_task(iTask.task); | |
850 | return -ENOTSUP; | |
851 | } | |
852 | ||
853 | if (iTask.task != NULL) { | |
854 | scsi_free_scsi_task(iTask.task); | |
855 | iTask.task = NULL; | |
856 | } | |
857 | ||
858 | if (iTask.do_retry) { | |
859 | iTask.complete = 0; | |
860 | goto retry; | |
861 | } | |
862 | ||
863 | if (iTask.status != SCSI_STATUS_GOOD) { | |
864 | return -EIO; | |
865 | } | |
866 | ||
867 | return 0; | |
868 | } | |
869 | ||
870 | #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */ | |
871 | ||
872 | static void parse_chap(struct iscsi_context *iscsi, const char *target, | |
873 | Error **errp) | |
874 | { | |
875 | QemuOptsList *list; | |
876 | QemuOpts *opts; | |
877 | const char *user = NULL; | |
878 | const char *password = NULL; | |
879 | ||
880 | list = qemu_find_opts("iscsi"); | |
881 | if (!list) { | |
882 | return; | |
883 | } | |
884 | ||
885 | opts = qemu_opts_find(list, target); | |
886 | if (opts == NULL) { | |
887 | opts = QTAILQ_FIRST(&list->head); | |
888 | if (!opts) { | |
889 | return; | |
890 | } | |
891 | } | |
892 | ||
893 | user = qemu_opt_get(opts, "user"); | |
894 | if (!user) { | |
895 | return; | |
896 | } | |
897 | ||
898 | password = qemu_opt_get(opts, "password"); | |
899 | if (!password) { | |
900 | error_setg(errp, "CHAP username specified but no password was given"); | |
901 | return; | |
902 | } | |
903 | ||
904 | if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { | |
905 | error_setg(errp, "Failed to set initiator username and password"); | |
906 | } | |
907 | } | |
908 | ||
909 | static void parse_header_digest(struct iscsi_context *iscsi, const char *target, | |
910 | Error **errp) | |
911 | { | |
912 | QemuOptsList *list; | |
913 | QemuOpts *opts; | |
914 | const char *digest = NULL; | |
915 | ||
916 | list = qemu_find_opts("iscsi"); | |
917 | if (!list) { | |
918 | return; | |
919 | } | |
920 | ||
921 | opts = qemu_opts_find(list, target); | |
922 | if (opts == NULL) { | |
923 | opts = QTAILQ_FIRST(&list->head); | |
924 | if (!opts) { | |
925 | return; | |
926 | } | |
927 | } | |
928 | ||
929 | digest = qemu_opt_get(opts, "header-digest"); | |
930 | if (!digest) { | |
931 | return; | |
932 | } | |
933 | ||
934 | if (!strcmp(digest, "CRC32C")) { | |
935 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); | |
936 | } else if (!strcmp(digest, "NONE")) { | |
937 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); | |
938 | } else if (!strcmp(digest, "CRC32C-NONE")) { | |
939 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); | |
940 | } else if (!strcmp(digest, "NONE-CRC32C")) { | |
941 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
942 | } else { | |
943 | error_setg(errp, "Invalid header-digest setting : %s", digest); | |
944 | } | |
945 | } | |
946 | ||
947 | static char *parse_initiator_name(const char *target) | |
948 | { | |
949 | QemuOptsList *list; | |
950 | QemuOpts *opts; | |
951 | const char *name; | |
952 | char *iscsi_name; | |
953 | UuidInfo *uuid_info; | |
954 | ||
955 | list = qemu_find_opts("iscsi"); | |
956 | if (list) { | |
957 | opts = qemu_opts_find(list, target); | |
958 | if (!opts) { | |
959 | opts = QTAILQ_FIRST(&list->head); | |
960 | } | |
961 | if (opts) { | |
962 | name = qemu_opt_get(opts, "initiator-name"); | |
963 | if (name) { | |
964 | return g_strdup(name); | |
965 | } | |
966 | } | |
967 | } | |
968 | ||
969 | uuid_info = qmp_query_uuid(NULL); | |
970 | if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { | |
971 | name = qemu_get_vm_name(); | |
972 | } else { | |
973 | name = uuid_info->UUID; | |
974 | } | |
975 | iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", | |
976 | name ? ":" : "", name ? name : ""); | |
977 | qapi_free_UuidInfo(uuid_info); | |
978 | return iscsi_name; | |
979 | } | |
980 | ||
981 | #if defined(LIBISCSI_FEATURE_NOP_COUNTER) | |
982 | static void iscsi_nop_timed_event(void *opaque) | |
983 | { | |
984 | IscsiLun *iscsilun = opaque; | |
985 | ||
986 | if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) { | |
987 | error_report("iSCSI: NOP timeout. Reconnecting..."); | |
988 | iscsi_reconnect(iscsilun->iscsi); | |
989 | } | |
990 | ||
991 | if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { | |
992 | error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); | |
993 | return; | |
994 | } | |
995 | ||
996 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); | |
997 | iscsi_set_events(iscsilun); | |
998 | } | |
999 | #endif | |
1000 | ||
1001 | static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) | |
1002 | { | |
1003 | struct scsi_task *task = NULL; | |
1004 | struct scsi_readcapacity10 *rc10 = NULL; | |
1005 | struct scsi_readcapacity16 *rc16 = NULL; | |
1006 | int retries = ISCSI_CMD_RETRIES; | |
1007 | ||
1008 | do { | |
1009 | if (task != NULL) { | |
1010 | scsi_free_scsi_task(task); | |
1011 | task = NULL; | |
1012 | } | |
1013 | ||
1014 | switch (iscsilun->type) { | |
1015 | case TYPE_DISK: | |
1016 | task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); | |
1017 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1018 | rc16 = scsi_datain_unmarshall(task); | |
1019 | if (rc16 == NULL) { | |
1020 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); | |
1021 | } else { | |
1022 | iscsilun->block_size = rc16->block_length; | |
1023 | iscsilun->num_blocks = rc16->returned_lba + 1; | |
1024 | iscsilun->lbpme = rc16->lbpme; | |
1025 | iscsilun->lbprz = rc16->lbprz; | |
1026 | } | |
1027 | } | |
1028 | break; | |
1029 | case TYPE_ROM: | |
1030 | task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); | |
1031 | if (task != NULL && task->status == SCSI_STATUS_GOOD) { | |
1032 | rc10 = scsi_datain_unmarshall(task); | |
1033 | if (rc10 == NULL) { | |
1034 | error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); | |
1035 | } else { | |
1036 | iscsilun->block_size = rc10->block_size; | |
1037 | if (rc10->lba == 0) { | |
1038 | /* blank disk loaded */ | |
1039 | iscsilun->num_blocks = 0; | |
1040 | } else { | |
1041 | iscsilun->num_blocks = rc10->lba + 1; | |
1042 | } | |
1043 | } | |
1044 | } | |
1045 | break; | |
1046 | default: | |
1047 | return; | |
1048 | } | |
1049 | } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION | |
1050 | && task->sense.key == SCSI_SENSE_UNIT_ATTENTION | |
1051 | && retries-- > 0); | |
1052 | ||
1053 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1054 | error_setg(errp, "iSCSI: failed to send readcapacity10 command."); | |
1055 | } | |
1056 | if (task) { | |
1057 | scsi_free_scsi_task(task); | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | /* TODO Convert to fine grained options */ | |
1062 | static QemuOptsList runtime_opts = { | |
1063 | .name = "iscsi", | |
1064 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
1065 | .desc = { | |
1066 | { | |
1067 | .name = "filename", | |
1068 | .type = QEMU_OPT_STRING, | |
1069 | .help = "URL to the iscsi image", | |
1070 | }, | |
1071 | { /* end of list */ } | |
1072 | }, | |
1073 | }; | |
1074 | ||
1075 | static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, | |
1076 | int evpd, int pc, void **inq, Error **errp) | |
1077 | { | |
1078 | int full_size; | |
1079 | struct scsi_task *task = NULL; | |
1080 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); | |
1081 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1082 | goto fail; | |
1083 | } | |
1084 | full_size = scsi_datain_getfullsize(task); | |
1085 | if (full_size > task->datain.size) { | |
1086 | scsi_free_scsi_task(task); | |
1087 | ||
1088 | /* we need more data for the full list */ | |
1089 | task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); | |
1090 | if (task == NULL || task->status != SCSI_STATUS_GOOD) { | |
1091 | goto fail; | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | *inq = scsi_datain_unmarshall(task); | |
1096 | if (*inq == NULL) { | |
1097 | error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); | |
1098 | goto fail; | |
1099 | } | |
1100 | ||
1101 | return task; | |
1102 | ||
1103 | fail: | |
1104 | error_setg(errp, "iSCSI: Inquiry command failed : %s", | |
1105 | iscsi_get_error(iscsi)); | |
1106 | if (task != NULL) { | |
1107 | scsi_free_scsi_task(task); | |
1108 | } | |
1109 | return NULL; | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * We support iscsi url's on the form | |
1114 | * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> | |
1115 | * | |
1116 | * Note: flags are currently not used by iscsi_open. If this function | |
1117 | * is changed such that flags are used, please examine iscsi_reopen_prepare() | |
1118 | * to see if needs to be changed as well. | |
1119 | */ | |
1120 | static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, | |
1121 | Error **errp) | |
1122 | { | |
1123 | IscsiLun *iscsilun = bs->opaque; | |
1124 | struct iscsi_context *iscsi = NULL; | |
1125 | struct iscsi_url *iscsi_url = NULL; | |
1126 | struct scsi_task *task = NULL; | |
1127 | struct scsi_inquiry_standard *inq = NULL; | |
1128 | struct scsi_inquiry_supported_pages *inq_vpd; | |
1129 | char *initiator_name = NULL; | |
1130 | QemuOpts *opts; | |
1131 | Error *local_err = NULL; | |
1132 | const char *filename; | |
1133 | int i, ret; | |
1134 | ||
1135 | if ((BDRV_SECTOR_SIZE % 512) != 0) { | |
1136 | error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. " | |
1137 | "BDRV_SECTOR_SIZE(%lld) is not a multiple " | |
1138 | "of 512", BDRV_SECTOR_SIZE); | |
1139 | return -EINVAL; | |
1140 | } | |
1141 | ||
1142 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); | |
1143 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
1144 | if (local_err) { | |
1145 | error_propagate(errp, local_err); | |
1146 | ret = -EINVAL; | |
1147 | goto out; | |
1148 | } | |
1149 | ||
1150 | filename = qemu_opt_get(opts, "filename"); | |
1151 | ||
1152 | iscsi_url = iscsi_parse_full_url(iscsi, filename); | |
1153 | if (iscsi_url == NULL) { | |
1154 | error_setg(errp, "Failed to parse URL : %s", filename); | |
1155 | ret = -EINVAL; | |
1156 | goto out; | |
1157 | } | |
1158 | ||
1159 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
1160 | ||
1161 | initiator_name = parse_initiator_name(iscsi_url->target); | |
1162 | ||
1163 | iscsi = iscsi_create_context(initiator_name); | |
1164 | if (iscsi == NULL) { | |
1165 | error_setg(errp, "iSCSI: Failed to create iSCSI context."); | |
1166 | ret = -ENOMEM; | |
1167 | goto out; | |
1168 | } | |
1169 | ||
1170 | if (iscsi_set_targetname(iscsi, iscsi_url->target)) { | |
1171 | error_setg(errp, "iSCSI: Failed to set target name."); | |
1172 | ret = -EINVAL; | |
1173 | goto out; | |
1174 | } | |
1175 | ||
1176 | if (iscsi_url->user != NULL) { | |
1177 | ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, | |
1178 | iscsi_url->passwd); | |
1179 | if (ret != 0) { | |
1180 | error_setg(errp, "Failed to set initiator username and password"); | |
1181 | ret = -EINVAL; | |
1182 | goto out; | |
1183 | } | |
1184 | } | |
1185 | ||
1186 | /* check if we got CHAP username/password via the options */ | |
1187 | parse_chap(iscsi, iscsi_url->target, &local_err); | |
1188 | if (local_err != NULL) { | |
1189 | error_propagate(errp, local_err); | |
1190 | ret = -EINVAL; | |
1191 | goto out; | |
1192 | } | |
1193 | ||
1194 | if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { | |
1195 | error_setg(errp, "iSCSI: Failed to set session type to normal."); | |
1196 | ret = -EINVAL; | |
1197 | goto out; | |
1198 | } | |
1199 | ||
1200 | iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); | |
1201 | ||
1202 | /* check if we got HEADER_DIGEST via the options */ | |
1203 | parse_header_digest(iscsi, iscsi_url->target, &local_err); | |
1204 | if (local_err != NULL) { | |
1205 | error_propagate(errp, local_err); | |
1206 | ret = -EINVAL; | |
1207 | goto out; | |
1208 | } | |
1209 | ||
1210 | if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) { | |
1211 | error_setg(errp, "iSCSI: Failed to connect to LUN : %s", | |
1212 | iscsi_get_error(iscsi)); | |
1213 | ret = -EINVAL; | |
1214 | goto out; | |
1215 | } | |
1216 | ||
1217 | iscsilun->iscsi = iscsi; | |
1218 | iscsilun->lun = iscsi_url->lun; | |
1219 | iscsilun->has_write_same = true; | |
1220 | ||
1221 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, | |
1222 | (void **) &inq, errp); | |
1223 | if (task == NULL) { | |
1224 | ret = -EINVAL; | |
1225 | goto out; | |
1226 | } | |
1227 | iscsilun->type = inq->periperal_device_type; | |
1228 | scsi_free_scsi_task(task); | |
1229 | task = NULL; | |
1230 | ||
1231 | iscsi_readcapacity_sync(iscsilun, &local_err); | |
1232 | if (local_err != NULL) { | |
1233 | error_propagate(errp, local_err); | |
1234 | goto out; | |
1235 | } | |
1236 | bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); | |
1237 | bs->request_alignment = iscsilun->block_size; | |
1238 | ||
1239 | /* We don't have any emulation for devices other than disks and CD-ROMs, so | |
1240 | * this must be sg ioctl compatible. We force it to be sg, otherwise qemu | |
1241 | * will try to read from the device to guess the image format. | |
1242 | */ | |
1243 | if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { | |
1244 | bs->sg = 1; | |
1245 | } | |
1246 | ||
1247 | task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1248 | SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, | |
1249 | (void **) &inq_vpd, errp); | |
1250 | if (task == NULL) { | |
1251 | ret = -EINVAL; | |
1252 | goto out; | |
1253 | } | |
1254 | for (i = 0; i < inq_vpd->num_pages; i++) { | |
1255 | struct scsi_task *inq_task; | |
1256 | struct scsi_inquiry_logical_block_provisioning *inq_lbp; | |
1257 | struct scsi_inquiry_block_limits *inq_bl; | |
1258 | switch (inq_vpd->pages[i]) { | |
1259 | case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: | |
1260 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1261 | SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, | |
1262 | (void **) &inq_lbp, errp); | |
1263 | if (inq_task == NULL) { | |
1264 | ret = -EINVAL; | |
1265 | goto out; | |
1266 | } | |
1267 | memcpy(&iscsilun->lbp, inq_lbp, | |
1268 | sizeof(struct scsi_inquiry_logical_block_provisioning)); | |
1269 | scsi_free_scsi_task(inq_task); | |
1270 | break; | |
1271 | case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: | |
1272 | inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, | |
1273 | SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, | |
1274 | (void **) &inq_bl, errp); | |
1275 | if (inq_task == NULL) { | |
1276 | ret = -EINVAL; | |
1277 | goto out; | |
1278 | } | |
1279 | memcpy(&iscsilun->bl, inq_bl, | |
1280 | sizeof(struct scsi_inquiry_block_limits)); | |
1281 | scsi_free_scsi_task(inq_task); | |
1282 | break; | |
1283 | default: | |
1284 | break; | |
1285 | } | |
1286 | } | |
1287 | scsi_free_scsi_task(task); | |
1288 | task = NULL; | |
1289 | ||
1290 | #if defined(LIBISCSI_FEATURE_NOP_COUNTER) | |
1291 | /* Set up a timer for sending out iSCSI NOPs */ | |
1292 | iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun); | |
1293 | timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); | |
1294 | #endif | |
1295 | ||
1296 | out: | |
1297 | qemu_opts_del(opts); | |
1298 | if (initiator_name != NULL) { | |
1299 | g_free(initiator_name); | |
1300 | } | |
1301 | if (iscsi_url != NULL) { | |
1302 | iscsi_destroy_url(iscsi_url); | |
1303 | } | |
1304 | if (task != NULL) { | |
1305 | scsi_free_scsi_task(task); | |
1306 | } | |
1307 | ||
1308 | if (ret) { | |
1309 | if (iscsi != NULL) { | |
1310 | iscsi_destroy_context(iscsi); | |
1311 | } | |
1312 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
1313 | } | |
1314 | return ret; | |
1315 | } | |
1316 | ||
1317 | static void iscsi_close(BlockDriverState *bs) | |
1318 | { | |
1319 | IscsiLun *iscsilun = bs->opaque; | |
1320 | struct iscsi_context *iscsi = iscsilun->iscsi; | |
1321 | ||
1322 | if (iscsilun->nop_timer) { | |
1323 | timer_del(iscsilun->nop_timer); | |
1324 | timer_free(iscsilun->nop_timer); | |
1325 | } | |
1326 | qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL); | |
1327 | iscsi_destroy_context(iscsi); | |
1328 | g_free(iscsilun->zeroblock); | |
1329 | memset(iscsilun, 0, sizeof(IscsiLun)); | |
1330 | } | |
1331 | ||
1332 | static int iscsi_refresh_limits(BlockDriverState *bs) | |
1333 | { | |
1334 | IscsiLun *iscsilun = bs->opaque; | |
1335 | ||
1336 | /* We don't actually refresh here, but just return data queried in | |
1337 | * iscsi_open(): iscsi targets don't change their limits. */ | |
1338 | if (iscsilun->lbp.lbpu || iscsilun->lbp.lbpws) { | |
1339 | if (iscsilun->bl.max_unmap < 0xffffffff) { | |
1340 | bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap, | |
1341 | iscsilun); | |
1342 | } | |
1343 | bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran, | |
1344 | iscsilun); | |
1345 | ||
1346 | if (iscsilun->bl.max_ws_len < 0xffffffff) { | |
1347 | bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len, | |
1348 | iscsilun); | |
1349 | } | |
1350 | bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran, | |
1351 | iscsilun); | |
1352 | } | |
1353 | bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len, | |
1354 | iscsilun); | |
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in | |
1359 | * prepare. Note that this will not re-establish a connection with an iSCSI | |
1360 | * target - it is effectively a NOP. */ | |
1361 | static int iscsi_reopen_prepare(BDRVReopenState *state, | |
1362 | BlockReopenQueue *queue, Error **errp) | |
1363 | { | |
1364 | /* NOP */ | |
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | static int iscsi_truncate(BlockDriverState *bs, int64_t offset) | |
1369 | { | |
1370 | IscsiLun *iscsilun = bs->opaque; | |
1371 | Error *local_err = NULL; | |
1372 | ||
1373 | if (iscsilun->type != TYPE_DISK) { | |
1374 | return -ENOTSUP; | |
1375 | } | |
1376 | ||
1377 | iscsi_readcapacity_sync(iscsilun, &local_err); | |
1378 | if (local_err != NULL) { | |
1379 | error_free(local_err); | |
1380 | return -EIO; | |
1381 | } | |
1382 | ||
1383 | if (offset > iscsi_getlength(bs)) { | |
1384 | return -EINVAL; | |
1385 | } | |
1386 | ||
1387 | return 0; | |
1388 | } | |
1389 | ||
1390 | static int iscsi_create(const char *filename, QEMUOptionParameter *options, | |
1391 | Error **errp) | |
1392 | { | |
1393 | int ret = 0; | |
1394 | int64_t total_size = 0; | |
1395 | BlockDriverState *bs; | |
1396 | IscsiLun *iscsilun = NULL; | |
1397 | QDict *bs_options; | |
1398 | ||
1399 | bs = bdrv_new(""); | |
1400 | ||
1401 | /* Read out options */ | |
1402 | while (options && options->name) { | |
1403 | if (!strcmp(options->name, "size")) { | |
1404 | total_size = options->value.n / BDRV_SECTOR_SIZE; | |
1405 | } | |
1406 | options++; | |
1407 | } | |
1408 | ||
1409 | bs->opaque = g_malloc0(sizeof(struct IscsiLun)); | |
1410 | iscsilun = bs->opaque; | |
1411 | ||
1412 | bs_options = qdict_new(); | |
1413 | qdict_put(bs_options, "filename", qstring_from_str(filename)); | |
1414 | ret = iscsi_open(bs, bs_options, 0, NULL); | |
1415 | QDECREF(bs_options); | |
1416 | ||
1417 | if (ret != 0) { | |
1418 | goto out; | |
1419 | } | |
1420 | if (iscsilun->nop_timer) { | |
1421 | timer_del(iscsilun->nop_timer); | |
1422 | timer_free(iscsilun->nop_timer); | |
1423 | } | |
1424 | if (iscsilun->type != TYPE_DISK) { | |
1425 | ret = -ENODEV; | |
1426 | goto out; | |
1427 | } | |
1428 | if (bs->total_sectors < total_size) { | |
1429 | ret = -ENOSPC; | |
1430 | goto out; | |
1431 | } | |
1432 | ||
1433 | ret = 0; | |
1434 | out: | |
1435 | if (iscsilun->iscsi != NULL) { | |
1436 | iscsi_destroy_context(iscsilun->iscsi); | |
1437 | } | |
1438 | g_free(bs->opaque); | |
1439 | bs->opaque = NULL; | |
1440 | bdrv_unref(bs); | |
1441 | return ret; | |
1442 | } | |
1443 | ||
1444 | static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
1445 | { | |
1446 | IscsiLun *iscsilun = bs->opaque; | |
1447 | bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz; | |
1448 | bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; | |
1449 | /* Guess the internal cluster (page) size of the iscsi target by the means | |
1450 | * of opt_unmap_gran. Transfer the unmap granularity only if it has a | |
1451 | * reasonable size for bdi->cluster_size */ | |
1452 | if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 64 * 1024 && | |
1453 | iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { | |
1454 | bdi->cluster_size = iscsilun->bl.opt_unmap_gran * iscsilun->block_size; | |
1455 | } | |
1456 | return 0; | |
1457 | } | |
1458 | ||
1459 | static QEMUOptionParameter iscsi_create_options[] = { | |
1460 | { | |
1461 | .name = BLOCK_OPT_SIZE, | |
1462 | .type = OPT_SIZE, | |
1463 | .help = "Virtual disk size" | |
1464 | }, | |
1465 | { NULL } | |
1466 | }; | |
1467 | ||
1468 | static BlockDriver bdrv_iscsi = { | |
1469 | .format_name = "iscsi", | |
1470 | .protocol_name = "iscsi", | |
1471 | ||
1472 | .instance_size = sizeof(IscsiLun), | |
1473 | .bdrv_needs_filename = true, | |
1474 | .bdrv_file_open = iscsi_open, | |
1475 | .bdrv_close = iscsi_close, | |
1476 | .bdrv_create = iscsi_create, | |
1477 | .create_options = iscsi_create_options, | |
1478 | .bdrv_reopen_prepare = iscsi_reopen_prepare, | |
1479 | ||
1480 | .bdrv_getlength = iscsi_getlength, | |
1481 | .bdrv_get_info = iscsi_get_info, | |
1482 | .bdrv_truncate = iscsi_truncate, | |
1483 | .bdrv_refresh_limits = iscsi_refresh_limits, | |
1484 | ||
1485 | #if defined(LIBISCSI_FEATURE_IOVECTOR) | |
1486 | .bdrv_co_get_block_status = iscsi_co_get_block_status, | |
1487 | #endif | |
1488 | .bdrv_co_discard = iscsi_co_discard, | |
1489 | #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED) | |
1490 | .bdrv_co_write_zeroes = iscsi_co_write_zeroes, | |
1491 | #endif | |
1492 | .bdrv_co_readv = iscsi_co_readv, | |
1493 | .bdrv_co_writev = iscsi_co_writev, | |
1494 | .bdrv_co_flush_to_disk = iscsi_co_flush, | |
1495 | ||
1496 | #ifdef __linux__ | |
1497 | .bdrv_ioctl = iscsi_ioctl, | |
1498 | .bdrv_aio_ioctl = iscsi_aio_ioctl, | |
1499 | #endif | |
1500 | }; | |
1501 | ||
1502 | static QemuOptsList qemu_iscsi_opts = { | |
1503 | .name = "iscsi", | |
1504 | .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head), | |
1505 | .desc = { | |
1506 | { | |
1507 | .name = "user", | |
1508 | .type = QEMU_OPT_STRING, | |
1509 | .help = "username for CHAP authentication to target", | |
1510 | },{ | |
1511 | .name = "password", | |
1512 | .type = QEMU_OPT_STRING, | |
1513 | .help = "password for CHAP authentication to target", | |
1514 | },{ | |
1515 | .name = "header-digest", | |
1516 | .type = QEMU_OPT_STRING, | |
1517 | .help = "HeaderDigest setting. " | |
1518 | "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}", | |
1519 | },{ | |
1520 | .name = "initiator-name", | |
1521 | .type = QEMU_OPT_STRING, | |
1522 | .help = "Initiator iqn name to use when connecting", | |
1523 | }, | |
1524 | { /* end of list */ } | |
1525 | }, | |
1526 | }; | |
1527 | ||
1528 | static void iscsi_block_init(void) | |
1529 | { | |
1530 | bdrv_register(&bdrv_iscsi); | |
1531 | qemu_add_opts(&qemu_iscsi_opts); | |
1532 | } | |
1533 | ||
1534 | block_init(iscsi_block_init); |