xref: /spdk/module/bdev/iscsi/bdev_iscsi.c (revision 7192849ed24874f3e9cc31e8a33a9b32c49b9506)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/bdev.h"
37 #include "spdk/conf.h"
38 #include "spdk/env.h"
39 #include "spdk/fd.h"
40 #include "spdk/thread.h"
41 #include "spdk/json.h"
42 #include "spdk/util.h"
43 #include "spdk/rpc.h"
44 #include "spdk/string.h"
45 #include "spdk/iscsi_spec.h"
46 
47 #include "spdk_internal/log.h"
48 #include "spdk/bdev_module.h"
49 
50 #include "iscsi/iscsi.h"
51 #include "iscsi/scsi-lowlevel.h"
52 
53 #include "bdev_iscsi.h"
54 
55 struct bdev_iscsi_lun;
56 
57 #define BDEV_ISCSI_CONNECTION_POLL_US 500 /* 0.5 ms */
58 #define BDEV_ISCSI_NO_MASTER_CH_POLL_US 10000 /* 10ms */
59 
60 #define DEFAULT_INITIATOR_NAME "iqn.2016-06.io.spdk:init"
61 
62 static int bdev_iscsi_initialize(void);
63 static TAILQ_HEAD(, bdev_iscsi_conn_req) g_iscsi_conn_req = TAILQ_HEAD_INITIALIZER(
64 			g_iscsi_conn_req);
65 static struct spdk_poller *g_conn_poller = NULL;
66 
67 struct bdev_iscsi_io {
68 	struct spdk_thread *submit_td;
69 	enum spdk_bdev_io_status status;
70 	int scsi_status;
71 	enum spdk_scsi_sense sk;
72 	uint8_t asc;
73 	uint8_t ascq;
74 };
75 
76 struct bdev_iscsi_lun {
77 	struct spdk_bdev		bdev;
78 	struct iscsi_context		*context;
79 	char				*initiator_iqn;
80 	int				lun_id;
81 	char				*url;
82 	pthread_mutex_t			mutex;
83 	uint32_t			ch_count;
84 	struct spdk_thread		*master_td;
85 	struct spdk_poller		*no_master_ch_poller;
86 	struct spdk_thread		*no_master_ch_poller_td;
87 	bool				unmap_supported;
88 	struct spdk_poller		*poller;
89 };
90 
91 struct bdev_iscsi_io_channel {
92 	struct bdev_iscsi_lun	*lun;
93 };
94 
95 struct bdev_iscsi_conn_req {
96 	char					*url;
97 	char					*bdev_name;
98 	char					*initiator_iqn;
99 	struct iscsi_context			*context;
100 	spdk_bdev_iscsi_create_cb		create_cb;
101 	void					*create_cb_arg;
102 	bool					unmap_supported;
103 	int					lun;
104 	int					status;
105 	TAILQ_ENTRY(bdev_iscsi_conn_req)	link;
106 };
107 
108 static void
109 complete_conn_req(struct bdev_iscsi_conn_req *req, struct spdk_bdev *bdev,
110 		  int status)
111 {
112 	TAILQ_REMOVE(&g_iscsi_conn_req, req, link);
113 	req->create_cb(req->create_cb_arg, bdev, status);
114 
115 	/*
116 	 * we are still running in the context of iscsi_service()
117 	 * so do not tear down its data structures here
118 	 */
119 	req->status = status;
120 }
121 
122 static int
123 bdev_iscsi_get_ctx_size(void)
124 {
125 	return sizeof(struct bdev_iscsi_io);
126 }
127 
128 static void
129 _iscsi_free_lun(void *arg)
130 {
131 	struct bdev_iscsi_lun *lun = arg;
132 
133 	assert(lun != NULL);
134 	iscsi_destroy_context(lun->context);
135 	pthread_mutex_destroy(&lun->mutex);
136 	free(lun->bdev.name);
137 	free(lun->url);
138 	free(lun->initiator_iqn);
139 
140 	spdk_bdev_destruct_done(&lun->bdev, 0);
141 	free(lun);
142 }
143 
144 static void
145 _bdev_iscsi_conn_req_free(struct bdev_iscsi_conn_req *req)
146 {
147 	free(req->initiator_iqn);
148 	free(req->bdev_name);
149 	free(req->url);
150 	/* destroy will call iscsi_disconnect() implicitly if connected */
151 	iscsi_destroy_context(req->context);
152 	free(req);
153 }
154 
155 static void
156 bdev_iscsi_finish(void)
157 {
158 	struct bdev_iscsi_conn_req *req, *tmp;
159 
160 	/* clear out pending connection requests here. We cannot
161 	 * simply set the state to a non SCSI_STATUS_GOOD state as
162 	 * the connection poller wont run anymore
163 	 */
164 	TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) {
165 		_bdev_iscsi_conn_req_free(req);
166 	}
167 
168 	if (g_conn_poller) {
169 		spdk_poller_unregister(&g_conn_poller);
170 	}
171 }
172 
173 static struct spdk_bdev_module g_iscsi_bdev_module = {
174 	.name		= "iscsi",
175 	.module_init	= bdev_iscsi_initialize,
176 	.module_fini	= bdev_iscsi_finish,
177 	.get_ctx_size	= bdev_iscsi_get_ctx_size,
178 	.async_init	= true,
179 };
180 
181 SPDK_BDEV_MODULE_REGISTER(iscsi, &g_iscsi_bdev_module);
182 
183 static void
184 _bdev_iscsi_io_complete(void *_iscsi_io)
185 {
186 	struct bdev_iscsi_io *iscsi_io = _iscsi_io;
187 
188 	if (iscsi_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
189 		spdk_bdev_io_complete_scsi_status(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->scsi_status,
190 						  iscsi_io->sk, iscsi_io->asc, iscsi_io->ascq);
191 	} else {
192 		spdk_bdev_io_complete(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->status);
193 	}
194 }
195 
196 static void
197 bdev_iscsi_io_complete(struct bdev_iscsi_io *iscsi_io, enum spdk_bdev_io_status status)
198 {
199 	iscsi_io->status = status;
200 	if (iscsi_io->submit_td != NULL) {
201 		spdk_thread_send_msg(iscsi_io->submit_td, _bdev_iscsi_io_complete, iscsi_io);
202 	} else {
203 		_bdev_iscsi_io_complete(iscsi_io);
204 	}
205 }
206 
207 /* Common call back function for read/write/flush command */
208 static void
209 bdev_iscsi_command_cb(struct iscsi_context *context, int status, void *_task, void *_iscsi_io)
210 {
211 	struct scsi_task *task = _task;
212 	struct bdev_iscsi_io *iscsi_io = _iscsi_io;
213 
214 	iscsi_io->scsi_status = status;
215 	iscsi_io->sk = (uint8_t)task->sense.key;
216 	iscsi_io->asc = (task->sense.ascq >> 8) & 0xFF;
217 	iscsi_io->ascq = task->sense.ascq & 0xFF;
218 
219 	scsi_free_scsi_task(task);
220 	bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS);
221 }
222 
223 static void
224 bdev_iscsi_readv(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io,
225 		 struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba)
226 {
227 	struct scsi_task *task;
228 
229 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI_INIT, "read %d iovs size %lu to lba: %#lx\n",
230 		      iovcnt, nbytes, lba);
231 
232 	task = iscsi_read16_task(lun->context, lun->lun_id, lba, nbytes, lun->bdev.blocklen, 0, 0, 0, 0, 0,
233 				 bdev_iscsi_command_cb, iscsi_io);
234 	if (task == NULL) {
235 		SPDK_ERRLOG("failed to get read16_task\n");
236 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
237 		return;
238 	}
239 
240 #if defined(LIBISCSI_FEATURE_IOVECTOR)
241 	scsi_task_set_iov_in(task, (struct scsi_iovec *)iov, iovcnt);
242 #else
243 	int i;
244 	for (i = 0; i < iovcnt; i++) {
245 		scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base);
246 	}
247 #endif
248 }
249 
250 static void
251 bdev_iscsi_writev(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io,
252 		  struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba)
253 {
254 	struct scsi_task *task;
255 
256 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI_INIT, "write %d iovs size %lu to lba: %#lx\n",
257 		      iovcnt, nbytes, lba);
258 
259 	task = iscsi_write16_task(lun->context, lun->lun_id, lba, NULL, nbytes, lun->bdev.blocklen, 0, 0, 0,
260 				  0, 0,
261 				  bdev_iscsi_command_cb, iscsi_io);
262 	if (task == NULL) {
263 		SPDK_ERRLOG("failed to get write16_task\n");
264 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
265 		return;
266 	}
267 
268 #if defined(LIBISCSI_FEATURE_IOVECTOR)
269 	scsi_task_set_iov_out(task, (struct scsi_iovec *)iov, iovcnt);
270 #else
271 	int i;
272 	for (i = 0; i < iovcnt; i++) {
273 		scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base);
274 	}
275 #endif
276 }
277 
278 static void
279 bdev_iscsi_destruct_cb(void *ctx)
280 {
281 	struct bdev_iscsi_lun *lun = ctx;
282 
283 	spdk_poller_unregister(&lun->no_master_ch_poller);
284 	spdk_io_device_unregister(lun, _iscsi_free_lun);
285 }
286 
287 static int
288 bdev_iscsi_destruct(void *ctx)
289 {
290 	struct bdev_iscsi_lun *lun = ctx;
291 
292 	assert(lun->no_master_ch_poller_td);
293 	spdk_thread_send_msg(lun->no_master_ch_poller_td, bdev_iscsi_destruct_cb, lun);
294 	return 1;
295 }
296 
297 static void
298 bdev_iscsi_flush(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, uint32_t num_blocks,
299 		 int immed, uint64_t lba)
300 {
301 	struct scsi_task *task;
302 
303 	task = iscsi_synchronizecache16_task(lun->context, lun->lun_id, lba,
304 					     num_blocks, 0, immed, bdev_iscsi_command_cb, iscsi_io);
305 	if (task == NULL) {
306 		SPDK_ERRLOG("failed to get sync16_task\n");
307 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
308 		return;
309 	}
310 }
311 
312 static void
313 bdev_iscsi_unmap(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io,
314 		 uint64_t lba, uint64_t num_blocks)
315 {
316 	struct scsi_task *task;
317 	struct unmap_list list[1];
318 
319 	list[0].lba = lba;
320 	list[0].num = num_blocks;
321 	task = iscsi_unmap_task(lun->context, 0, 0, 0, list, 1,
322 				bdev_iscsi_command_cb, iscsi_io);
323 	if (task == NULL) {
324 		SPDK_ERRLOG("failed to get unmap_task\n");
325 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
326 		return;
327 	}
328 }
329 
330 static void
331 bdev_iscsi_reset_cb(struct iscsi_context *context __attribute__((unused)), int status,
332 		    void *command_data, void *private_data)
333 {
334 	uint32_t tmf_response;
335 	struct bdev_iscsi_io *iscsi_io = private_data;
336 
337 	tmf_response = *(uint32_t *)command_data;
338 	if (tmf_response == ISCSI_TASK_FUNC_RESP_COMPLETE) {
339 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS);
340 	} else {
341 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
342 	}
343 }
344 
345 static void
346 _bdev_iscsi_reset(void *_bdev_io)
347 {
348 	int rc;
349 	struct spdk_bdev_io *bdev_io = _bdev_io;
350 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
351 	struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx;
352 	struct iscsi_context *context = lun->context;
353 
354 	rc = iscsi_task_mgmt_lun_reset_async(context, lun->lun_id,
355 					     bdev_iscsi_reset_cb, iscsi_io);
356 	if (rc != 0) {
357 		SPDK_ERRLOG("failed to do iscsi reset\n");
358 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
359 		return;
360 	}
361 }
362 
363 static void
364 bdev_iscsi_reset(struct spdk_bdev_io *bdev_io)
365 {
366 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
367 	spdk_thread_send_msg(lun->master_td, _bdev_iscsi_reset, bdev_io);
368 }
369 
370 static int
371 bdev_iscsi_poll_lun(void *_lun)
372 {
373 	struct bdev_iscsi_lun *lun = _lun;
374 	struct pollfd pfd = {};
375 
376 	pfd.fd = iscsi_get_fd(lun->context);
377 	pfd.events = iscsi_which_events(lun->context);
378 
379 	if (poll(&pfd, 1, 0) < 0) {
380 		SPDK_ERRLOG("poll failed\n");
381 		return SPDK_POLLER_IDLE;
382 	}
383 
384 	if (pfd.revents != 0) {
385 		if (iscsi_service(lun->context, pfd.revents) < 0) {
386 			SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(lun->context));
387 		}
388 
389 		return SPDK_POLLER_BUSY;
390 	}
391 
392 	return SPDK_POLLER_IDLE;
393 }
394 
395 static int
396 bdev_iscsi_no_master_ch_poll(void *arg)
397 {
398 	struct bdev_iscsi_lun *lun = arg;
399 	enum spdk_thread_poller_rc rc = SPDK_POLLER_IDLE;
400 
401 	if (pthread_mutex_trylock(&lun->mutex)) {
402 		/* Don't care about the error code here. */
403 		return SPDK_POLLER_IDLE;
404 	}
405 
406 	if (lun->ch_count == 0) {
407 		rc = bdev_iscsi_poll_lun(arg);
408 	}
409 
410 	pthread_mutex_unlock(&lun->mutex);
411 	return rc;
412 }
413 
414 static void
415 bdev_iscsi_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
416 		      bool success)
417 {
418 	if (!success) {
419 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
420 		return;
421 	}
422 
423 	bdev_iscsi_readv((struct bdev_iscsi_lun *)bdev_io->bdev->ctxt,
424 			 (struct bdev_iscsi_io *)bdev_io->driver_ctx,
425 			 bdev_io->u.bdev.iovs,
426 			 bdev_io->u.bdev.iovcnt,
427 			 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
428 			 bdev_io->u.bdev.offset_blocks);
429 }
430 
431 static void _bdev_iscsi_submit_request(void *_bdev_io)
432 {
433 	struct spdk_bdev_io *bdev_io = _bdev_io;
434 	struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx;
435 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
436 
437 	switch (bdev_io->type) {
438 	case SPDK_BDEV_IO_TYPE_READ:
439 		spdk_bdev_io_get_buf(bdev_io, bdev_iscsi_get_buf_cb,
440 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
441 		break;
442 
443 	case SPDK_BDEV_IO_TYPE_WRITE:
444 		bdev_iscsi_writev(lun, iscsi_io,
445 				  bdev_io->u.bdev.iovs,
446 				  bdev_io->u.bdev.iovcnt,
447 				  bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
448 				  bdev_io->u.bdev.offset_blocks);
449 		break;
450 	case SPDK_BDEV_IO_TYPE_FLUSH:
451 		bdev_iscsi_flush(lun, iscsi_io,
452 				 bdev_io->u.bdev.num_blocks,
453 				 ISCSI_IMMEDIATE_DATA_NO,
454 				 bdev_io->u.bdev.offset_blocks);
455 		break;
456 	case SPDK_BDEV_IO_TYPE_RESET:
457 		bdev_iscsi_reset(bdev_io);
458 		break;
459 	case SPDK_BDEV_IO_TYPE_UNMAP:
460 		bdev_iscsi_unmap(lun, iscsi_io,
461 				 bdev_io->u.bdev.offset_blocks,
462 				 bdev_io->u.bdev.num_blocks);
463 		break;
464 	default:
465 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
466 		break;
467 	}
468 }
469 
470 static void bdev_iscsi_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
471 {
472 	struct spdk_thread *submit_td = spdk_io_channel_get_thread(_ch);
473 	struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx;
474 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
475 
476 	if (lun->master_td != submit_td) {
477 		iscsi_io->submit_td = submit_td;
478 		spdk_thread_send_msg(lun->master_td, _bdev_iscsi_submit_request, bdev_io);
479 		return;
480 	} else {
481 		iscsi_io->submit_td = NULL;
482 	}
483 
484 	_bdev_iscsi_submit_request(bdev_io);
485 }
486 
487 static bool
488 bdev_iscsi_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
489 {
490 	struct bdev_iscsi_lun *lun = ctx;
491 
492 	switch (io_type) {
493 	case SPDK_BDEV_IO_TYPE_READ:
494 	case SPDK_BDEV_IO_TYPE_WRITE:
495 	case SPDK_BDEV_IO_TYPE_FLUSH:
496 	case SPDK_BDEV_IO_TYPE_RESET:
497 		return true;
498 
499 	case SPDK_BDEV_IO_TYPE_UNMAP:
500 		return lun->unmap_supported;
501 	default:
502 		return false;
503 	}
504 }
505 
506 static int
507 bdev_iscsi_create_cb(void *io_device, void *ctx_buf)
508 {
509 	struct bdev_iscsi_io_channel *ch = ctx_buf;
510 	struct bdev_iscsi_lun *lun = io_device;
511 
512 	pthread_mutex_lock(&lun->mutex);
513 	if (lun->ch_count == 0) {
514 		assert(lun->master_td == NULL);
515 		lun->master_td = spdk_get_thread();
516 		lun->poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun, lun, 0);
517 		ch->lun = lun;
518 	}
519 	lun->ch_count++;
520 	pthread_mutex_unlock(&lun->mutex);
521 
522 	return 0;
523 }
524 
525 static void
526 _iscsi_destroy_cb(void *ctx)
527 {
528 	struct bdev_iscsi_lun *lun = ctx;
529 
530 	pthread_mutex_lock(&lun->mutex);
531 
532 	assert(lun->master_td == spdk_get_thread());
533 	assert(lun->ch_count > 0);
534 
535 	lun->ch_count--;
536 	if (lun->ch_count > 0) {
537 		pthread_mutex_unlock(&lun->mutex);
538 		return;
539 	}
540 
541 	lun->master_td = NULL;
542 	spdk_poller_unregister(&lun->poller);
543 
544 	pthread_mutex_unlock(&lun->mutex);
545 }
546 
547 static void
548 bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf)
549 {
550 	struct bdev_iscsi_lun *lun = io_device;
551 	struct spdk_thread *thread;
552 
553 	pthread_mutex_lock(&lun->mutex);
554 	lun->ch_count--;
555 	if (lun->ch_count == 0) {
556 		assert(lun->master_td != NULL);
557 
558 		if (lun->master_td != spdk_get_thread()) {
559 			/* The final channel was destroyed on a different thread
560 			 * than where the first channel was created. Pass a message
561 			 * to the master thread to unregister the poller. */
562 			lun->ch_count++;
563 			thread = lun->master_td;
564 			pthread_mutex_unlock(&lun->mutex);
565 			spdk_thread_send_msg(thread, _iscsi_destroy_cb, lun);
566 			return;
567 		}
568 
569 		lun->master_td = NULL;
570 		spdk_poller_unregister(&lun->poller);
571 	}
572 	pthread_mutex_unlock(&lun->mutex);
573 }
574 
575 static struct spdk_io_channel *
576 bdev_iscsi_get_io_channel(void *ctx)
577 {
578 	struct bdev_iscsi_lun *lun = ctx;
579 
580 	return spdk_get_io_channel(lun);
581 }
582 
583 static int
584 bdev_iscsi_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
585 {
586 	struct bdev_iscsi_lun *lun = ctx;
587 
588 	spdk_json_write_named_object_begin(w, "iscsi");
589 	spdk_json_write_named_string(w, "initiator_name", lun->initiator_iqn);
590 	spdk_json_write_named_string(w, "url", lun->url);
591 	spdk_json_write_object_end(w);
592 
593 	return 0;
594 }
595 
596 static void
597 bdev_iscsi_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
598 {
599 	struct bdev_iscsi_lun *lun = bdev->ctxt;
600 
601 	pthread_mutex_lock(&lun->mutex);
602 	spdk_json_write_object_begin(w);
603 
604 	spdk_json_write_named_string(w, "method", "bdev_iscsi_create");
605 
606 	spdk_json_write_named_object_begin(w, "params");
607 	spdk_json_write_named_string(w, "name", bdev->name);
608 	spdk_json_write_named_string(w, "initiator_iqn", lun->initiator_iqn);
609 	spdk_json_write_named_string(w, "url", lun->url);
610 	spdk_json_write_object_end(w);
611 
612 	spdk_json_write_object_end(w);
613 	pthread_mutex_unlock(&lun->mutex);
614 }
615 
616 static const struct spdk_bdev_fn_table iscsi_fn_table = {
617 	.destruct		= bdev_iscsi_destruct,
618 	.submit_request		= bdev_iscsi_submit_request,
619 	.io_type_supported	= bdev_iscsi_io_type_supported,
620 	.get_io_channel		= bdev_iscsi_get_io_channel,
621 	.dump_info_json		= bdev_iscsi_dump_info_json,
622 	.write_config_json	= bdev_iscsi_write_config_json,
623 };
624 
625 static int
626 create_iscsi_lun(struct iscsi_context *context, int lun_id, char *url, char *initiator_iqn,
627 		 char *name,
628 		 uint64_t num_blocks, uint32_t block_size, struct spdk_bdev **bdev, bool unmap_supported)
629 {
630 	struct bdev_iscsi_lun *lun;
631 	int rc;
632 
633 	lun = calloc(sizeof(*lun), 1);
634 	if (!lun) {
635 		SPDK_ERRLOG("Unable to allocate enough memory for iscsi backend\n");
636 		return -ENOMEM;
637 	}
638 
639 	lun->context = context;
640 	lun->lun_id = lun_id;
641 	lun->url = url;
642 	lun->initiator_iqn = initiator_iqn;
643 
644 	pthread_mutex_init(&lun->mutex, NULL);
645 
646 	lun->bdev.name = name;
647 	lun->bdev.product_name = "iSCSI LUN";
648 	lun->bdev.module = &g_iscsi_bdev_module;
649 	lun->bdev.blocklen = block_size;
650 	lun->bdev.blockcnt = num_blocks;
651 	lun->bdev.ctxt = lun;
652 	lun->unmap_supported = unmap_supported;
653 
654 	lun->bdev.fn_table = &iscsi_fn_table;
655 
656 	spdk_io_device_register(lun, bdev_iscsi_create_cb, bdev_iscsi_destroy_cb,
657 				sizeof(struct bdev_iscsi_io_channel),
658 				name);
659 	rc = spdk_bdev_register(&lun->bdev);
660 	if (rc) {
661 		spdk_io_device_unregister(lun, NULL);
662 		pthread_mutex_destroy(&lun->mutex);
663 		free(lun);
664 		return rc;
665 	}
666 
667 	lun->no_master_ch_poller_td = spdk_get_thread();
668 	lun->no_master_ch_poller = SPDK_POLLER_REGISTER(bdev_iscsi_no_master_ch_poll, lun,
669 				   BDEV_ISCSI_NO_MASTER_CH_POLL_US);
670 
671 	*bdev = &lun->bdev;
672 	return 0;
673 }
674 
675 static void
676 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
677 			void *command_data, void *private_data)
678 {
679 	struct bdev_iscsi_conn_req *req = private_data;
680 	struct scsi_readcapacity16 *readcap16;
681 	struct spdk_bdev *bdev = NULL;
682 	struct scsi_task *task = command_data;
683 
684 	if (status != SPDK_SCSI_STATUS_GOOD) {
685 		SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(iscsi));
686 		goto ret;
687 	}
688 
689 	readcap16 = scsi_datain_unmarshall(task);
690 	if (!readcap16) {
691 		status = -ENOMEM;
692 		goto ret;
693 	}
694 
695 	status = create_iscsi_lun(req->context, req->lun, req->url, req->initiator_iqn, req->bdev_name,
696 				  readcap16->returned_lba + 1, readcap16->block_length, &bdev, req->unmap_supported);
697 	if (status) {
698 		SPDK_ERRLOG("Unable to create iscsi bdev: %s (%d)\n", spdk_strerror(-status), status);
699 	}
700 
701 ret:
702 	scsi_free_scsi_task(task);
703 	complete_conn_req(req, bdev, status);
704 }
705 
706 static void
707 bdev_iscsi_inquiry_cb(struct iscsi_context *context, int status, void *_task, void *private_data)
708 {
709 	struct scsi_task *task = _task;
710 	struct scsi_inquiry_logical_block_provisioning *lbp_inq = NULL;
711 	struct bdev_iscsi_conn_req *req = private_data;
712 
713 	if (status == SPDK_SCSI_STATUS_GOOD) {
714 		lbp_inq = scsi_datain_unmarshall(task);
715 		if (lbp_inq != NULL && lbp_inq->lbpu) {
716 			req->unmap_supported = true;
717 		}
718 	}
719 
720 	task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req);
721 	if (task) {
722 		return;
723 	}
724 
725 	SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context));
726 	complete_conn_req(req, NULL, status);
727 }
728 
729 static void
730 iscsi_connect_cb(struct iscsi_context *iscsi, int status,
731 		 void *command_data, void *private_data)
732 {
733 	struct bdev_iscsi_conn_req *req = private_data;
734 	struct scsi_task *task;
735 
736 	if (status != SPDK_SCSI_STATUS_GOOD) {
737 		goto ret;
738 	}
739 
740 	task = iscsi_inquiry_task(iscsi, req->lun, 1,
741 				  SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
742 				  255, bdev_iscsi_inquiry_cb, req);
743 	if (task) {
744 		return;
745 	}
746 
747 ret:
748 	SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context));
749 	complete_conn_req(req, NULL, status);
750 }
751 
752 static int
753 iscsi_bdev_conn_poll(void *arg)
754 {
755 	struct bdev_iscsi_conn_req *req, *tmp;
756 	struct pollfd pfd;
757 	struct iscsi_context *context;
758 
759 	if (TAILQ_EMPTY(&g_iscsi_conn_req)) {
760 		return SPDK_POLLER_IDLE;
761 	}
762 
763 	TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) {
764 		context = req->context;
765 		pfd.fd = iscsi_get_fd(context);
766 		pfd.events = iscsi_which_events(context);
767 		pfd.revents = 0;
768 		if (poll(&pfd, 1, 0) < 0) {
769 			SPDK_ERRLOG("poll failed\n");
770 			return SPDK_POLLER_BUSY;
771 		}
772 
773 		if (pfd.revents != 0) {
774 			if (iscsi_service(context, pfd.revents) < 0) {
775 				SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(context));
776 			}
777 		}
778 
779 		if (req->status == 0) {
780 			/*
781 			 * The request completed successfully.
782 			 */
783 			free(req);
784 		} else if (req->status > 0) {
785 			/*
786 			 * An error has occurred during connecting.  This req has already
787 			 * been removed from the g_iscsi_conn_req list, but we needed to
788 			 * wait until iscsi_service unwound before we could free the req.
789 			 */
790 			_bdev_iscsi_conn_req_free(req);
791 		}
792 	}
793 	return SPDK_POLLER_BUSY;
794 }
795 
796 int
797 create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_iqn,
798 		  spdk_bdev_iscsi_create_cb cb_fn, void *cb_arg)
799 {
800 	struct bdev_iscsi_conn_req *req;
801 	struct iscsi_url *iscsi_url = NULL;
802 	int rc;
803 
804 	if (!bdev_name || !url || !initiator_iqn || strlen(initiator_iqn) == 0 || !cb_fn) {
805 		return -EINVAL;
806 	}
807 
808 	req = calloc(1, sizeof(struct bdev_iscsi_conn_req));
809 	if (!req) {
810 		SPDK_ERRLOG("Cannot allocate pointer of struct bdev_iscsi_conn_req\n");
811 		return -ENOMEM;
812 	}
813 
814 	req->status = SCSI_STATUS_GOOD;
815 	req->bdev_name = strdup(bdev_name);
816 	req->url = strdup(url);
817 	req->initiator_iqn = strdup(initiator_iqn);
818 	req->context = iscsi_create_context(initiator_iqn);
819 	if (!req->bdev_name || !req->url || !req->initiator_iqn || !req->context) {
820 		SPDK_ERRLOG("Out of memory\n");
821 		rc = -ENOMEM;
822 		goto err;
823 	}
824 
825 	req->create_cb = cb_fn;
826 	req->create_cb_arg = cb_arg;
827 
828 	iscsi_url = iscsi_parse_full_url(req->context, url);
829 	if (iscsi_url == NULL) {
830 		SPDK_ERRLOG("could not parse URL: %s\n", iscsi_get_error(req->context));
831 		rc = -EINVAL;
832 		goto err;
833 	}
834 
835 	req->lun = iscsi_url->lun;
836 	rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL);
837 	rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE);
838 	rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target);
839 	rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun,
840 						iscsi_connect_cb, req);
841 	if (rc == 0 && iscsi_url->user[0] != '\0') {
842 		rc = iscsi_set_initiator_username_pwd(req->context, iscsi_url->user, iscsi_url->passwd);
843 	}
844 
845 	if (rc < 0) {
846 		SPDK_ERRLOG("Failed to connect provided URL=%s: %s\n", url, iscsi_get_error(req->context));
847 		goto err;
848 	}
849 
850 	iscsi_destroy_url(iscsi_url);
851 	req->status = -1;
852 	TAILQ_INSERT_TAIL(&g_iscsi_conn_req, req, link);
853 	if (!g_conn_poller) {
854 		g_conn_poller = SPDK_POLLER_REGISTER(iscsi_bdev_conn_poll, NULL, BDEV_ISCSI_CONNECTION_POLL_US);
855 	}
856 
857 	return 0;
858 
859 err:
860 	/* iscsi_destroy_url() is not NULL-proof */
861 	if (iscsi_url) {
862 		iscsi_destroy_url(iscsi_url);
863 	}
864 
865 	if (req->context) {
866 		iscsi_destroy_context(req->context);
867 	}
868 
869 	free(req->initiator_iqn);
870 	free(req->bdev_name);
871 	free(req->url);
872 	free(req);
873 	return rc;
874 }
875 
876 void
877 delete_iscsi_disk(struct spdk_bdev *bdev, spdk_delete_iscsi_complete cb_fn, void *cb_arg)
878 {
879 	if (!bdev || bdev->module != &g_iscsi_bdev_module) {
880 		cb_fn(cb_arg, -ENODEV);
881 		return;
882 	}
883 
884 	spdk_bdev_unregister(bdev, cb_fn, cb_arg);
885 }
886 
887 static void
888 bdev_iscsi_initialize_cb(void *cb_arg, struct spdk_bdev *bdev, int status)
889 {
890 	if (TAILQ_EMPTY(&g_iscsi_conn_req)) {
891 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
892 	}
893 }
894 
895 static int
896 bdev_iscsi_initialize(void)
897 {
898 	struct spdk_conf_section *sp;
899 
900 	const char *url, *bdev_name, *initiator_iqn;
901 	int i, rc;
902 
903 	sp = spdk_conf_find_section(NULL, "iSCSI_Initiator");
904 	if (sp == NULL) {
905 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
906 		return 0;
907 	}
908 
909 	initiator_iqn = spdk_conf_section_get_val(sp, "initiator_name");
910 	if (!initiator_iqn) {
911 		initiator_iqn = DEFAULT_INITIATOR_NAME;
912 	}
913 
914 	rc = 0;
915 	for (i = 0; (url = spdk_conf_section_get_nmval(sp, "URL", i, 0)) != NULL; i++) {
916 		bdev_name = spdk_conf_section_get_nmval(sp, "URL", i, 1);
917 		if (bdev_name == NULL) {
918 			SPDK_ERRLOG("no bdev name specified for URL %s\n", url);
919 			rc = -EINVAL;
920 			break;
921 		}
922 
923 		rc = create_iscsi_disk(bdev_name, url, initiator_iqn, bdev_iscsi_initialize_cb, NULL);
924 		if (rc) {
925 			break;
926 		}
927 	}
928 
929 	if (i == 0) {
930 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
931 	}
932 
933 	return rc;
934 }
935 
936 SPDK_LOG_REGISTER_COMPONENT("iscsi_init", SPDK_LOG_ISCSI_INIT)
937