]> git.proxmox.com Git - qemu.git/blob - block/iscsi.c
ISCSI: redo how we set up the events
[qemu.git] / block / iscsi.c
1 /*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "config-host.h"
26
27 #include <poll.h>
28 #include "qemu-common.h"
29 #include "qemu-error.h"
30 #include "block_int.h"
31 #include "trace.h"
32
33 #include <iscsi/iscsi.h>
34 #include <iscsi/scsi-lowlevel.h>
35
36
37 typedef struct IscsiLun {
38 struct iscsi_context *iscsi;
39 int lun;
40 int block_size;
41 unsigned long num_blocks;
42 int events;
43 } IscsiLun;
44
45 typedef struct IscsiAIOCB {
46 BlockDriverAIOCB common;
47 QEMUIOVector *qiov;
48 QEMUBH *bh;
49 IscsiLun *iscsilun;
50 struct scsi_task *task;
51 uint8_t *buf;
52 int status;
53 int canceled;
54 size_t read_size;
55 size_t read_offset;
56 } IscsiAIOCB;
57
58 struct IscsiTask {
59 IscsiLun *iscsilun;
60 BlockDriverState *bs;
61 int status;
62 int complete;
63 };
64
65 static void
66 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
67 void *private_data)
68 {
69 }
70
71 static void
72 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
73 {
74 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
75 IscsiLun *iscsilun = acb->iscsilun;
76
77 acb->common.cb(acb->common.opaque, -ECANCELED);
78 acb->canceled = 1;
79
80 /* send a task mgmt call to the target to cancel the task on the target */
81 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
82 iscsi_abort_task_cb, NULL);
83
84 /* then also cancel the task locally in libiscsi */
85 iscsi_scsi_task_cancel(iscsilun->iscsi, acb->task);
86 }
87
88 static AIOPool iscsi_aio_pool = {
89 .aiocb_size = sizeof(IscsiAIOCB),
90 .cancel = iscsi_aio_cancel,
91 };
92
93
94 static void iscsi_process_read(void *arg);
95 static void iscsi_process_write(void *arg);
96
97 static int iscsi_process_flush(void *arg)
98 {
99 IscsiLun *iscsilun = arg;
100
101 return iscsi_queue_length(iscsilun->iscsi) > 0;
102 }
103
104 static void
105 iscsi_set_events(IscsiLun *iscsilun)
106 {
107 struct iscsi_context *iscsi = iscsilun->iscsi;
108 int ev;
109
110 /* We always register a read handler. */
111 ev = POLLIN;
112 ev |= iscsi_which_events(iscsi);
113 if (ev != iscsilun->events) {
114 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
115 iscsi_process_read,
116 (ev & POLLOUT) ? iscsi_process_write : NULL,
117 iscsi_process_flush,
118 iscsilun);
119
120 }
121
122 /* If we just added an event, the callback might be delayed
123 * unless we call qemu_notify_event().
124 */
125 if (ev & ~iscsilun->events) {
126 qemu_notify_event();
127 }
128 iscsilun->events = ev;
129 }
130
131 static void
132 iscsi_process_read(void *arg)
133 {
134 IscsiLun *iscsilun = arg;
135 struct iscsi_context *iscsi = iscsilun->iscsi;
136
137 iscsi_service(iscsi, POLLIN);
138 iscsi_set_events(iscsilun);
139 }
140
141 static void
142 iscsi_process_write(void *arg)
143 {
144 IscsiLun *iscsilun = arg;
145 struct iscsi_context *iscsi = iscsilun->iscsi;
146
147 iscsi_service(iscsi, POLLOUT);
148 iscsi_set_events(iscsilun);
149 }
150
151
152 static int
153 iscsi_schedule_bh(QEMUBHFunc *cb, IscsiAIOCB *acb)
154 {
155 acb->bh = qemu_bh_new(cb, acb);
156 if (!acb->bh) {
157 error_report("oom: could not create iscsi bh");
158 return -EIO;
159 }
160
161 qemu_bh_schedule(acb->bh);
162 return 0;
163 }
164
165 static void
166 iscsi_readv_writev_bh_cb(void *p)
167 {
168 IscsiAIOCB *acb = p;
169
170 qemu_bh_delete(acb->bh);
171
172 if (acb->canceled == 0) {
173 acb->common.cb(acb->common.opaque, acb->status);
174 }
175
176 qemu_aio_release(acb);
177 }
178
179
180 static void
181 iscsi_aio_write10_cb(struct iscsi_context *iscsi, int status,
182 void *command_data, void *opaque)
183 {
184 IscsiAIOCB *acb = opaque;
185
186 trace_iscsi_aio_write10_cb(iscsi, status, acb, acb->canceled);
187
188 g_free(acb->buf);
189
190 if (acb->canceled != 0) {
191 qemu_aio_release(acb);
192 scsi_free_scsi_task(acb->task);
193 acb->task = NULL;
194 return;
195 }
196
197 acb->status = 0;
198 if (status < 0) {
199 error_report("Failed to write10 data to iSCSI lun. %s",
200 iscsi_get_error(iscsi));
201 acb->status = -EIO;
202 }
203
204 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
205 scsi_free_scsi_task(acb->task);
206 acb->task = NULL;
207 }
208
209 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
210 {
211 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
212 }
213
214 static BlockDriverAIOCB *
215 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
216 QEMUIOVector *qiov, int nb_sectors,
217 BlockDriverCompletionFunc *cb,
218 void *opaque)
219 {
220 IscsiLun *iscsilun = bs->opaque;
221 struct iscsi_context *iscsi = iscsilun->iscsi;
222 IscsiAIOCB *acb;
223 size_t size;
224 int fua = 0;
225
226 /* set FUA on writes when cache mode is write through */
227 if (!(bs->open_flags & BDRV_O_CACHE_WB)) {
228 fua = 1;
229 }
230
231 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
232 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
233
234 acb->iscsilun = iscsilun;
235 acb->qiov = qiov;
236
237 acb->canceled = 0;
238
239 /* XXX we should pass the iovec to write10 to avoid the extra copy */
240 /* this will allow us to get rid of 'buf' completely */
241 size = nb_sectors * BDRV_SECTOR_SIZE;
242 acb->buf = g_malloc(size);
243 qemu_iovec_to_buffer(acb->qiov, acb->buf);
244 acb->task = iscsi_write10_task(iscsi, iscsilun->lun, acb->buf, size,
245 sector_qemu2lun(sector_num, iscsilun),
246 fua, 0, iscsilun->block_size,
247 iscsi_aio_write10_cb, acb);
248 if (acb->task == NULL) {
249 error_report("iSCSI: Failed to send write10 command. %s",
250 iscsi_get_error(iscsi));
251 g_free(acb->buf);
252 qemu_aio_release(acb);
253 return NULL;
254 }
255
256 iscsi_set_events(iscsilun);
257
258 return &acb->common;
259 }
260
261 static void
262 iscsi_aio_read10_cb(struct iscsi_context *iscsi, int status,
263 void *command_data, void *opaque)
264 {
265 IscsiAIOCB *acb = opaque;
266
267 trace_iscsi_aio_read10_cb(iscsi, status, acb, acb->canceled);
268
269 if (acb->canceled != 0) {
270 qemu_aio_release(acb);
271 scsi_free_scsi_task(acb->task);
272 acb->task = NULL;
273 return;
274 }
275
276 acb->status = 0;
277 if (status != 0) {
278 error_report("Failed to read10 data from iSCSI lun. %s",
279 iscsi_get_error(iscsi));
280 acb->status = -EIO;
281 }
282
283 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
284 scsi_free_scsi_task(acb->task);
285 acb->task = NULL;
286 }
287
288 static BlockDriverAIOCB *
289 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
290 QEMUIOVector *qiov, int nb_sectors,
291 BlockDriverCompletionFunc *cb,
292 void *opaque)
293 {
294 IscsiLun *iscsilun = bs->opaque;
295 struct iscsi_context *iscsi = iscsilun->iscsi;
296 IscsiAIOCB *acb;
297 size_t qemu_read_size, lun_read_size;
298 int i;
299
300 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
301
302 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
303 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
304
305 acb->iscsilun = iscsilun;
306 acb->qiov = qiov;
307
308 acb->canceled = 0;
309 acb->read_size = qemu_read_size;
310 acb->buf = NULL;
311
312 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
313 * may be misaligned to the LUN, so we may need to read some extra
314 * data.
315 */
316 acb->read_offset = 0;
317 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
318 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
319
320 acb->read_offset = bdrv_offset % iscsilun->block_size;
321 }
322
323 lun_read_size = (qemu_read_size + iscsilun->block_size
324 + acb->read_offset - 1)
325 / iscsilun->block_size * iscsilun->block_size;
326 acb->task = iscsi_read10_task(iscsi, iscsilun->lun,
327 sector_qemu2lun(sector_num, iscsilun),
328 lun_read_size, iscsilun->block_size,
329 iscsi_aio_read10_cb, acb);
330 if (acb->task == NULL) {
331 error_report("iSCSI: Failed to send read10 command. %s",
332 iscsi_get_error(iscsi));
333 qemu_aio_release(acb);
334 return NULL;
335 }
336
337 for (i = 0; i < acb->qiov->niov; i++) {
338 scsi_task_add_data_in_buffer(acb->task,
339 acb->qiov->iov[i].iov_len,
340 acb->qiov->iov[i].iov_base);
341 }
342
343 iscsi_set_events(iscsilun);
344
345 return &acb->common;
346 }
347
348
349 static void
350 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
351 void *command_data, void *opaque)
352 {
353 IscsiAIOCB *acb = opaque;
354
355 if (acb->canceled != 0) {
356 qemu_aio_release(acb);
357 scsi_free_scsi_task(acb->task);
358 acb->task = NULL;
359 return;
360 }
361
362 acb->status = 0;
363 if (status < 0) {
364 error_report("Failed to sync10 data on iSCSI lun. %s",
365 iscsi_get_error(iscsi));
366 acb->status = -EIO;
367 }
368
369 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
370 scsi_free_scsi_task(acb->task);
371 acb->task = NULL;
372 }
373
374 static BlockDriverAIOCB *
375 iscsi_aio_flush(BlockDriverState *bs,
376 BlockDriverCompletionFunc *cb, void *opaque)
377 {
378 IscsiLun *iscsilun = bs->opaque;
379 struct iscsi_context *iscsi = iscsilun->iscsi;
380 IscsiAIOCB *acb;
381
382 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
383
384 acb->iscsilun = iscsilun;
385 acb->canceled = 0;
386
387 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
388 0, 0, 0, 0,
389 iscsi_synccache10_cb,
390 acb);
391 if (acb->task == NULL) {
392 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
393 iscsi_get_error(iscsi));
394 qemu_aio_release(acb);
395 return NULL;
396 }
397
398 iscsi_set_events(iscsilun);
399
400 return &acb->common;
401 }
402
403 static void
404 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
405 void *command_data, void *opaque)
406 {
407 IscsiAIOCB *acb = opaque;
408
409 if (acb->canceled != 0) {
410 qemu_aio_release(acb);
411 scsi_free_scsi_task(acb->task);
412 acb->task = NULL;
413 return;
414 }
415
416 acb->status = 0;
417 if (status < 0) {
418 error_report("Failed to unmap data on iSCSI lun. %s",
419 iscsi_get_error(iscsi));
420 acb->status = -EIO;
421 }
422
423 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
424 scsi_free_scsi_task(acb->task);
425 acb->task = NULL;
426 }
427
428 static BlockDriverAIOCB *
429 iscsi_aio_discard(BlockDriverState *bs,
430 int64_t sector_num, int nb_sectors,
431 BlockDriverCompletionFunc *cb, void *opaque)
432 {
433 IscsiLun *iscsilun = bs->opaque;
434 struct iscsi_context *iscsi = iscsilun->iscsi;
435 IscsiAIOCB *acb;
436 struct unmap_list list[1];
437
438 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
439
440 acb->iscsilun = iscsilun;
441 acb->canceled = 0;
442
443 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
444 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
445
446 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
447 0, 0, &list[0], 1,
448 iscsi_unmap_cb,
449 acb);
450 if (acb->task == NULL) {
451 error_report("iSCSI: Failed to send unmap command. %s",
452 iscsi_get_error(iscsi));
453 qemu_aio_release(acb);
454 return NULL;
455 }
456
457 iscsi_set_events(iscsilun);
458
459 return &acb->common;
460 }
461
462 static int64_t
463 iscsi_getlength(BlockDriverState *bs)
464 {
465 IscsiLun *iscsilun = bs->opaque;
466 int64_t len;
467
468 len = iscsilun->num_blocks;
469 len *= iscsilun->block_size;
470
471 return len;
472 }
473
474 static void
475 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
476 void *command_data, void *opaque)
477 {
478 struct IscsiTask *itask = opaque;
479 struct scsi_readcapacity16 *rc16;
480 struct scsi_task *task = command_data;
481
482 if (status != 0) {
483 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
484 iscsi_get_error(iscsi));
485 itask->status = 1;
486 itask->complete = 1;
487 scsi_free_scsi_task(task);
488 return;
489 }
490
491 rc16 = scsi_datain_unmarshall(task);
492 if (rc16 == NULL) {
493 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
494 itask->status = 1;
495 itask->complete = 1;
496 scsi_free_scsi_task(task);
497 return;
498 }
499
500 itask->iscsilun->block_size = rc16->block_length;
501 itask->iscsilun->num_blocks = rc16->returned_lba + 1;
502 itask->bs->total_sectors = itask->iscsilun->num_blocks *
503 itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
504
505 itask->status = 0;
506 itask->complete = 1;
507 scsi_free_scsi_task(task);
508 }
509
510 static void
511 iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data,
512 void *opaque)
513 {
514 struct IscsiTask *itask = opaque;
515 struct scsi_task *task;
516
517 if (status != 0) {
518 itask->status = 1;
519 itask->complete = 1;
520 return;
521 }
522
523 task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun,
524 iscsi_readcapacity16_cb, opaque);
525 if (task == NULL) {
526 error_report("iSCSI: failed to send readcapacity16 command.");
527 itask->status = 1;
528 itask->complete = 1;
529 return;
530 }
531 }
532
533 static int parse_chap(struct iscsi_context *iscsi, const char *target)
534 {
535 QemuOptsList *list;
536 QemuOpts *opts;
537 const char *user = NULL;
538 const char *password = NULL;
539
540 list = qemu_find_opts("iscsi");
541 if (!list) {
542 return 0;
543 }
544
545 opts = qemu_opts_find(list, target);
546 if (opts == NULL) {
547 opts = QTAILQ_FIRST(&list->head);
548 if (!opts) {
549 return 0;
550 }
551 }
552
553 user = qemu_opt_get(opts, "user");
554 if (!user) {
555 return 0;
556 }
557
558 password = qemu_opt_get(opts, "password");
559 if (!password) {
560 error_report("CHAP username specified but no password was given");
561 return -1;
562 }
563
564 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
565 error_report("Failed to set initiator username and password");
566 return -1;
567 }
568
569 return 0;
570 }
571
572 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
573 {
574 QemuOptsList *list;
575 QemuOpts *opts;
576 const char *digest = NULL;
577
578 list = qemu_find_opts("iscsi");
579 if (!list) {
580 return;
581 }
582
583 opts = qemu_opts_find(list, target);
584 if (opts == NULL) {
585 opts = QTAILQ_FIRST(&list->head);
586 if (!opts) {
587 return;
588 }
589 }
590
591 digest = qemu_opt_get(opts, "header-digest");
592 if (!digest) {
593 return;
594 }
595
596 if (!strcmp(digest, "CRC32C")) {
597 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
598 } else if (!strcmp(digest, "NONE")) {
599 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
600 } else if (!strcmp(digest, "CRC32C-NONE")) {
601 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
602 } else if (!strcmp(digest, "NONE-CRC32C")) {
603 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
604 } else {
605 error_report("Invalid header-digest setting : %s", digest);
606 }
607 }
608
609 static char *parse_initiator_name(const char *target)
610 {
611 QemuOptsList *list;
612 QemuOpts *opts;
613 const char *name = NULL;
614
615 list = qemu_find_opts("iscsi");
616 if (!list) {
617 return g_strdup("iqn.2008-11.org.linux-kvm");
618 }
619
620 opts = qemu_opts_find(list, target);
621 if (opts == NULL) {
622 opts = QTAILQ_FIRST(&list->head);
623 if (!opts) {
624 return g_strdup("iqn.2008-11.org.linux-kvm");
625 }
626 }
627
628 name = qemu_opt_get(opts, "initiator-name");
629 if (!name) {
630 return g_strdup("iqn.2008-11.org.linux-kvm");
631 }
632
633 return g_strdup(name);
634 }
635
636 /*
637 * We support iscsi url's on the form
638 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
639 */
640 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
641 {
642 IscsiLun *iscsilun = bs->opaque;
643 struct iscsi_context *iscsi = NULL;
644 struct iscsi_url *iscsi_url = NULL;
645 struct IscsiTask task;
646 char *initiator_name = NULL;
647 int ret;
648
649 if ((BDRV_SECTOR_SIZE % 512) != 0) {
650 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
651 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
652 "of 512", BDRV_SECTOR_SIZE);
653 return -EINVAL;
654 }
655
656 iscsi_url = iscsi_parse_full_url(iscsi, filename);
657 if (iscsi_url == NULL) {
658 error_report("Failed to parse URL : %s %s", filename,
659 iscsi_get_error(iscsi));
660 ret = -EINVAL;
661 goto failed;
662 }
663
664 memset(iscsilun, 0, sizeof(IscsiLun));
665
666 initiator_name = parse_initiator_name(iscsi_url->target);
667
668 iscsi = iscsi_create_context(initiator_name);
669 if (iscsi == NULL) {
670 error_report("iSCSI: Failed to create iSCSI context.");
671 ret = -ENOMEM;
672 goto failed;
673 }
674
675 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
676 error_report("iSCSI: Failed to set target name.");
677 ret = -EINVAL;
678 goto failed;
679 }
680
681 if (iscsi_url->user != NULL) {
682 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
683 iscsi_url->passwd);
684 if (ret != 0) {
685 error_report("Failed to set initiator username and password");
686 ret = -EINVAL;
687 goto failed;
688 }
689 }
690
691 /* check if we got CHAP username/password via the options */
692 if (parse_chap(iscsi, iscsi_url->target) != 0) {
693 error_report("iSCSI: Failed to set CHAP user/password");
694 ret = -EINVAL;
695 goto failed;
696 }
697
698 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
699 error_report("iSCSI: Failed to set session type to normal.");
700 ret = -EINVAL;
701 goto failed;
702 }
703
704 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
705
706 /* check if we got HEADER_DIGEST via the options */
707 parse_header_digest(iscsi, iscsi_url->target);
708
709 task.iscsilun = iscsilun;
710 task.status = 0;
711 task.complete = 0;
712 task.bs = bs;
713
714 iscsilun->iscsi = iscsi;
715 iscsilun->lun = iscsi_url->lun;
716
717 if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun,
718 iscsi_connect_cb, &task)
719 != 0) {
720 error_report("iSCSI: Failed to start async connect.");
721 ret = -EINVAL;
722 goto failed;
723 }
724
725 while (!task.complete) {
726 iscsi_set_events(iscsilun);
727 qemu_aio_wait();
728 }
729 if (task.status != 0) {
730 error_report("iSCSI: Failed to connect to LUN : %s",
731 iscsi_get_error(iscsi));
732 ret = -EINVAL;
733 goto failed;
734 }
735
736 if (iscsi_url != NULL) {
737 iscsi_destroy_url(iscsi_url);
738 }
739 return 0;
740
741 failed:
742 if (initiator_name != NULL) {
743 g_free(initiator_name);
744 }
745 if (iscsi_url != NULL) {
746 iscsi_destroy_url(iscsi_url);
747 }
748 if (iscsi != NULL) {
749 iscsi_destroy_context(iscsi);
750 }
751 memset(iscsilun, 0, sizeof(IscsiLun));
752 return ret;
753 }
754
755 static void iscsi_close(BlockDriverState *bs)
756 {
757 IscsiLun *iscsilun = bs->opaque;
758 struct iscsi_context *iscsi = iscsilun->iscsi;
759
760 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
761 iscsi_destroy_context(iscsi);
762 memset(iscsilun, 0, sizeof(IscsiLun));
763 }
764
765 static BlockDriver bdrv_iscsi = {
766 .format_name = "iscsi",
767 .protocol_name = "iscsi",
768
769 .instance_size = sizeof(IscsiLun),
770 .bdrv_file_open = iscsi_open,
771 .bdrv_close = iscsi_close,
772
773 .bdrv_getlength = iscsi_getlength,
774
775 .bdrv_aio_readv = iscsi_aio_readv,
776 .bdrv_aio_writev = iscsi_aio_writev,
777 .bdrv_aio_flush = iscsi_aio_flush,
778
779 .bdrv_aio_discard = iscsi_aio_discard,
780 };
781
782 static void iscsi_block_init(void)
783 {
784 bdrv_register(&bdrv_iscsi);
785 }
786
787 block_init(iscsi_block_init);