xref: /spdk/module/bdev/iscsi/bdev_iscsi.c (revision 1f4f4cc75a522f897856e980a0b35d3c8fac24ed)
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 -1;
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 
390 	return -1;
391 }
392 
393 static int
394 bdev_iscsi_no_master_ch_poll(void *arg)
395 {
396 	struct bdev_iscsi_lun *lun = arg;
397 	int rc = 0;
398 
399 	if (pthread_mutex_trylock(&lun->mutex)) {
400 		/* Don't care about the error code here. */
401 		return -1;
402 	}
403 
404 	if (lun->ch_count == 0) {
405 		rc = bdev_iscsi_poll_lun(arg);
406 	}
407 
408 	pthread_mutex_unlock(&lun->mutex);
409 	return rc;
410 }
411 
412 static void
413 bdev_iscsi_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
414 		      bool success)
415 {
416 	if (!success) {
417 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
418 		return;
419 	}
420 
421 	bdev_iscsi_readv((struct bdev_iscsi_lun *)bdev_io->bdev->ctxt,
422 			 (struct bdev_iscsi_io *)bdev_io->driver_ctx,
423 			 bdev_io->u.bdev.iovs,
424 			 bdev_io->u.bdev.iovcnt,
425 			 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
426 			 bdev_io->u.bdev.offset_blocks);
427 }
428 
429 static void _bdev_iscsi_submit_request(void *_bdev_io)
430 {
431 	struct spdk_bdev_io *bdev_io = _bdev_io;
432 	struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx;
433 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
434 
435 	switch (bdev_io->type) {
436 	case SPDK_BDEV_IO_TYPE_READ:
437 		spdk_bdev_io_get_buf(bdev_io, bdev_iscsi_get_buf_cb,
438 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
439 		break;
440 
441 	case SPDK_BDEV_IO_TYPE_WRITE:
442 		bdev_iscsi_writev(lun, iscsi_io,
443 				  bdev_io->u.bdev.iovs,
444 				  bdev_io->u.bdev.iovcnt,
445 				  bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen,
446 				  bdev_io->u.bdev.offset_blocks);
447 		break;
448 	case SPDK_BDEV_IO_TYPE_FLUSH:
449 		bdev_iscsi_flush(lun, iscsi_io,
450 				 bdev_io->u.bdev.num_blocks,
451 				 ISCSI_IMMEDIATE_DATA_NO,
452 				 bdev_io->u.bdev.offset_blocks);
453 		break;
454 	case SPDK_BDEV_IO_TYPE_RESET:
455 		bdev_iscsi_reset(bdev_io);
456 		break;
457 	case SPDK_BDEV_IO_TYPE_UNMAP:
458 		bdev_iscsi_unmap(lun, iscsi_io,
459 				 bdev_io->u.bdev.offset_blocks,
460 				 bdev_io->u.bdev.num_blocks);
461 		break;
462 	default:
463 		bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED);
464 		break;
465 	}
466 }
467 
468 static void bdev_iscsi_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
469 {
470 	struct spdk_thread *submit_td = spdk_io_channel_get_thread(_ch);
471 	struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx;
472 	struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt;
473 
474 	if (lun->master_td != submit_td) {
475 		iscsi_io->submit_td = submit_td;
476 		spdk_thread_send_msg(lun->master_td, _bdev_iscsi_submit_request, bdev_io);
477 		return;
478 	} else {
479 		iscsi_io->submit_td = NULL;
480 	}
481 
482 	_bdev_iscsi_submit_request(bdev_io);
483 }
484 
485 static bool
486 bdev_iscsi_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
487 {
488 	struct bdev_iscsi_lun *lun = ctx;
489 
490 	switch (io_type) {
491 	case SPDK_BDEV_IO_TYPE_READ:
492 	case SPDK_BDEV_IO_TYPE_WRITE:
493 	case SPDK_BDEV_IO_TYPE_FLUSH:
494 	case SPDK_BDEV_IO_TYPE_RESET:
495 		return true;
496 
497 	case SPDK_BDEV_IO_TYPE_UNMAP:
498 		return lun->unmap_supported;
499 	default:
500 		return false;
501 	}
502 }
503 
504 static int
505 bdev_iscsi_create_cb(void *io_device, void *ctx_buf)
506 {
507 	struct bdev_iscsi_io_channel *ch = ctx_buf;
508 	struct bdev_iscsi_lun *lun = io_device;
509 
510 	pthread_mutex_lock(&lun->mutex);
511 	if (lun->ch_count == 0) {
512 		assert(lun->master_td == NULL);
513 		lun->master_td = spdk_get_thread();
514 		lun->poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun, lun, 0);
515 		ch->lun = lun;
516 	}
517 	lun->ch_count++;
518 	pthread_mutex_unlock(&lun->mutex);
519 
520 	return 0;
521 }
522 
523 static void
524 _iscsi_destroy_cb(void *ctx)
525 {
526 	struct bdev_iscsi_lun *lun = ctx;
527 
528 	pthread_mutex_lock(&lun->mutex);
529 
530 	assert(lun->master_td == spdk_get_thread());
531 	assert(lun->ch_count > 0);
532 
533 	lun->ch_count--;
534 	if (lun->ch_count > 0) {
535 		pthread_mutex_unlock(&lun->mutex);
536 		return;
537 	}
538 
539 	lun->master_td = NULL;
540 	spdk_poller_unregister(&lun->poller);
541 
542 	pthread_mutex_unlock(&lun->mutex);
543 }
544 
545 static void
546 bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf)
547 {
548 	struct bdev_iscsi_lun *lun = io_device;
549 	struct spdk_thread *thread;
550 
551 	pthread_mutex_lock(&lun->mutex);
552 	lun->ch_count--;
553 	if (lun->ch_count == 0) {
554 		assert(lun->master_td != NULL);
555 
556 		if (lun->master_td != spdk_get_thread()) {
557 			/* The final channel was destroyed on a different thread
558 			 * than where the first channel was created. Pass a message
559 			 * to the master thread to unregister the poller. */
560 			lun->ch_count++;
561 			thread = lun->master_td;
562 			pthread_mutex_unlock(&lun->mutex);
563 			spdk_thread_send_msg(thread, _iscsi_destroy_cb, lun);
564 			return;
565 		}
566 
567 		lun->master_td = NULL;
568 		spdk_poller_unregister(&lun->poller);
569 	}
570 	pthread_mutex_unlock(&lun->mutex);
571 }
572 
573 static struct spdk_io_channel *
574 bdev_iscsi_get_io_channel(void *ctx)
575 {
576 	struct bdev_iscsi_lun *lun = ctx;
577 
578 	return spdk_get_io_channel(lun);
579 }
580 
581 static int
582 bdev_iscsi_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
583 {
584 	struct bdev_iscsi_lun *lun = ctx;
585 
586 	spdk_json_write_named_object_begin(w, "iscsi");
587 	spdk_json_write_named_string(w, "initiator_name", lun->initiator_iqn);
588 	spdk_json_write_named_string(w, "url", lun->url);
589 	spdk_json_write_object_end(w);
590 
591 	return 0;
592 }
593 
594 static void
595 bdev_iscsi_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
596 {
597 	struct bdev_iscsi_lun *lun = bdev->ctxt;
598 
599 	pthread_mutex_lock(&lun->mutex);
600 	spdk_json_write_object_begin(w);
601 
602 	spdk_json_write_named_string(w, "method", "bdev_iscsi_create");
603 
604 	spdk_json_write_named_object_begin(w, "params");
605 	spdk_json_write_named_string(w, "name", bdev->name);
606 	spdk_json_write_named_string(w, "initiator_iqn", lun->initiator_iqn);
607 	spdk_json_write_named_string(w, "url", lun->url);
608 	spdk_json_write_object_end(w);
609 
610 	spdk_json_write_object_end(w);
611 	pthread_mutex_unlock(&lun->mutex);
612 }
613 
614 static const struct spdk_bdev_fn_table iscsi_fn_table = {
615 	.destruct		= bdev_iscsi_destruct,
616 	.submit_request		= bdev_iscsi_submit_request,
617 	.io_type_supported	= bdev_iscsi_io_type_supported,
618 	.get_io_channel		= bdev_iscsi_get_io_channel,
619 	.dump_info_json		= bdev_iscsi_dump_info_json,
620 	.write_config_json	= bdev_iscsi_write_config_json,
621 };
622 
623 static int
624 create_iscsi_lun(struct iscsi_context *context, int lun_id, char *url, char *initiator_iqn,
625 		 char *name,
626 		 uint64_t num_blocks, uint32_t block_size, struct spdk_bdev **bdev, bool unmap_supported)
627 {
628 	struct bdev_iscsi_lun *lun;
629 	int rc;
630 
631 	lun = calloc(sizeof(*lun), 1);
632 	if (!lun) {
633 		SPDK_ERRLOG("Unable to allocate enough memory for iscsi backend\n");
634 		return -ENOMEM;
635 	}
636 
637 	lun->context = context;
638 	lun->lun_id = lun_id;
639 	lun->url = url;
640 	lun->initiator_iqn = initiator_iqn;
641 
642 	pthread_mutex_init(&lun->mutex, NULL);
643 
644 	lun->bdev.name = name;
645 	lun->bdev.product_name = "iSCSI LUN";
646 	lun->bdev.module = &g_iscsi_bdev_module;
647 	lun->bdev.blocklen = block_size;
648 	lun->bdev.blockcnt = num_blocks;
649 	lun->bdev.ctxt = lun;
650 	lun->unmap_supported = unmap_supported;
651 
652 	lun->bdev.fn_table = &iscsi_fn_table;
653 
654 	spdk_io_device_register(lun, bdev_iscsi_create_cb, bdev_iscsi_destroy_cb,
655 				sizeof(struct bdev_iscsi_io_channel),
656 				name);
657 	rc = spdk_bdev_register(&lun->bdev);
658 	if (rc) {
659 		spdk_io_device_unregister(lun, NULL);
660 		pthread_mutex_destroy(&lun->mutex);
661 		free(lun);
662 		return rc;
663 	}
664 
665 	lun->no_master_ch_poller_td = spdk_get_thread();
666 	lun->no_master_ch_poller = SPDK_POLLER_REGISTER(bdev_iscsi_no_master_ch_poll, lun,
667 				   BDEV_ISCSI_NO_MASTER_CH_POLL_US);
668 
669 	*bdev = &lun->bdev;
670 	return 0;
671 }
672 
673 static void
674 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
675 			void *command_data, void *private_data)
676 {
677 	struct bdev_iscsi_conn_req *req = private_data;
678 	struct scsi_readcapacity16 *readcap16;
679 	struct spdk_bdev *bdev = NULL;
680 	struct scsi_task *task = command_data;
681 
682 	if (status != SPDK_SCSI_STATUS_GOOD) {
683 		SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(iscsi));
684 		goto ret;
685 	}
686 
687 	readcap16 = scsi_datain_unmarshall(task);
688 	if (!readcap16) {
689 		status = -ENOMEM;
690 		goto ret;
691 	}
692 
693 	status = create_iscsi_lun(req->context, req->lun, req->url, req->initiator_iqn, req->bdev_name,
694 				  readcap16->returned_lba + 1, readcap16->block_length, &bdev, req->unmap_supported);
695 	if (status) {
696 		SPDK_ERRLOG("Unable to create iscsi bdev: %s (%d)\n", spdk_strerror(-status), status);
697 	}
698 
699 ret:
700 	scsi_free_scsi_task(task);
701 	complete_conn_req(req, bdev, status);
702 }
703 
704 static void
705 bdev_iscsi_inquiry_cb(struct iscsi_context *context, int status, void *_task, void *private_data)
706 {
707 	struct scsi_task *task = _task;
708 	struct scsi_inquiry_logical_block_provisioning *lbp_inq = NULL;
709 	struct bdev_iscsi_conn_req *req = private_data;
710 
711 	if (status == SPDK_SCSI_STATUS_GOOD) {
712 		lbp_inq = scsi_datain_unmarshall(task);
713 		if (lbp_inq != NULL && lbp_inq->lbpu) {
714 			req->unmap_supported = true;
715 		}
716 	}
717 
718 	task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req);
719 	if (task) {
720 		return;
721 	}
722 
723 	SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context));
724 	complete_conn_req(req, NULL, status);
725 }
726 
727 static void
728 iscsi_connect_cb(struct iscsi_context *iscsi, int status,
729 		 void *command_data, void *private_data)
730 {
731 	struct bdev_iscsi_conn_req *req = private_data;
732 	struct scsi_task *task;
733 
734 	if (status != SPDK_SCSI_STATUS_GOOD) {
735 		goto ret;
736 	}
737 
738 	task = iscsi_inquiry_task(iscsi, req->lun, 1,
739 				  SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
740 				  255, bdev_iscsi_inquiry_cb, req);
741 	if (task) {
742 		return;
743 	}
744 
745 ret:
746 	SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context));
747 	complete_conn_req(req, NULL, status);
748 }
749 
750 static int
751 iscsi_bdev_conn_poll(void *arg)
752 {
753 	struct bdev_iscsi_conn_req *req, *tmp;
754 	struct pollfd pfd;
755 	struct iscsi_context *context;
756 
757 	TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) {
758 		context = req->context;
759 		pfd.fd = iscsi_get_fd(context);
760 		pfd.events = iscsi_which_events(context);
761 		pfd.revents = 0;
762 		if (poll(&pfd, 1, 0) < 0) {
763 			SPDK_ERRLOG("poll failed\n");
764 			return -1;
765 		}
766 
767 		if (pfd.revents != 0) {
768 			if (iscsi_service(context, pfd.revents) < 0) {
769 				SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(context));
770 			}
771 		}
772 
773 		if (req->status == 0) {
774 			/*
775 			 * The request completed successfully.
776 			 */
777 			free(req);
778 		} else if (req->status > 0) {
779 			/*
780 			 * An error has occurred during connecting.  This req has already
781 			 * been removed from the g_iscsi_conn_req list, but we needed to
782 			 * wait until iscsi_service unwound before we could free the req.
783 			 */
784 			_bdev_iscsi_conn_req_free(req);
785 		}
786 	}
787 	return -1;
788 }
789 
790 int
791 create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_iqn,
792 		  spdk_bdev_iscsi_create_cb cb_fn, void *cb_arg)
793 {
794 	struct bdev_iscsi_conn_req *req;
795 	struct iscsi_url *iscsi_url = NULL;
796 	int rc;
797 
798 	if (!bdev_name || !url || !initiator_iqn || strlen(initiator_iqn) == 0 || !cb_fn) {
799 		return -EINVAL;
800 	}
801 
802 	req = calloc(1, sizeof(struct bdev_iscsi_conn_req));
803 	if (!req) {
804 		SPDK_ERRLOG("Cannot allocate pointer of struct bdev_iscsi_conn_req\n");
805 		return -ENOMEM;
806 	}
807 
808 	req->status = SCSI_STATUS_GOOD;
809 	req->bdev_name = strdup(bdev_name);
810 	req->url = strdup(url);
811 	req->initiator_iqn = strdup(initiator_iqn);
812 	req->context = iscsi_create_context(initiator_iqn);
813 	if (!req->bdev_name || !req->url || !req->initiator_iqn || !req->context) {
814 		SPDK_ERRLOG("Out of memory\n");
815 		rc = -ENOMEM;
816 		goto err;
817 	}
818 
819 	req->create_cb = cb_fn;
820 	req->create_cb_arg = cb_arg;
821 
822 	iscsi_url = iscsi_parse_full_url(req->context, url);
823 	if (iscsi_url == NULL) {
824 		SPDK_ERRLOG("could not parse URL: %s\n", iscsi_get_error(req->context));
825 		rc = -EINVAL;
826 		goto err;
827 	}
828 
829 	req->lun = iscsi_url->lun;
830 	rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL);
831 	rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE);
832 	rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target);
833 	rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun,
834 						iscsi_connect_cb, req);
835 	if (rc == 0 && iscsi_url->user[0] != '\0') {
836 		rc = iscsi_set_initiator_username_pwd(req->context, iscsi_url->user, iscsi_url->passwd);
837 	}
838 
839 	if (rc < 0) {
840 		SPDK_ERRLOG("Failed to connect provided URL=%s: %s\n", url, iscsi_get_error(req->context));
841 		goto err;
842 	}
843 
844 	iscsi_destroy_url(iscsi_url);
845 	req->status = -1;
846 	TAILQ_INSERT_TAIL(&g_iscsi_conn_req, req, link);
847 	if (!g_conn_poller) {
848 		g_conn_poller = SPDK_POLLER_REGISTER(iscsi_bdev_conn_poll, NULL, BDEV_ISCSI_CONNECTION_POLL_US);
849 	}
850 
851 	return 0;
852 
853 err:
854 	/* iscsi_destroy_url() is not NULL-proof */
855 	if (iscsi_url) {
856 		iscsi_destroy_url(iscsi_url);
857 	}
858 
859 	if (req->context) {
860 		iscsi_destroy_context(req->context);
861 	}
862 
863 	free(req->initiator_iqn);
864 	free(req->bdev_name);
865 	free(req->url);
866 	free(req);
867 	return rc;
868 }
869 
870 void
871 delete_iscsi_disk(struct spdk_bdev *bdev, spdk_delete_iscsi_complete cb_fn, void *cb_arg)
872 {
873 	if (!bdev || bdev->module != &g_iscsi_bdev_module) {
874 		cb_fn(cb_arg, -ENODEV);
875 		return;
876 	}
877 
878 	spdk_bdev_unregister(bdev, cb_fn, cb_arg);
879 }
880 
881 static void
882 bdev_iscsi_initialize_cb(void *cb_arg, struct spdk_bdev *bdev, int status)
883 {
884 	if (TAILQ_EMPTY(&g_iscsi_conn_req)) {
885 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
886 	}
887 }
888 
889 static int
890 bdev_iscsi_initialize(void)
891 {
892 	struct spdk_conf_section *sp;
893 
894 	const char *url, *bdev_name, *initiator_iqn;
895 	int i, rc;
896 
897 	sp = spdk_conf_find_section(NULL, "iSCSI_Initiator");
898 	if (sp == NULL) {
899 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
900 		return 0;
901 	}
902 
903 	initiator_iqn = spdk_conf_section_get_val(sp, "initiator_name");
904 	if (!initiator_iqn) {
905 		initiator_iqn = DEFAULT_INITIATOR_NAME;
906 	}
907 
908 	rc = 0;
909 	for (i = 0; (url = spdk_conf_section_get_nmval(sp, "URL", i, 0)) != NULL; i++) {
910 		bdev_name = spdk_conf_section_get_nmval(sp, "URL", i, 1);
911 		if (bdev_name == NULL) {
912 			SPDK_ERRLOG("no bdev name specified for URL %s\n", url);
913 			rc = -EINVAL;
914 			break;
915 		}
916 
917 		rc = create_iscsi_disk(bdev_name, url, initiator_iqn, bdev_iscsi_initialize_cb, NULL);
918 		if (rc) {
919 			break;
920 		}
921 	}
922 
923 	if (i == 0) {
924 		spdk_bdev_module_init_done(&g_iscsi_bdev_module);
925 	}
926 
927 	return rc;
928 }
929 
930 SPDK_LOG_REGISTER_COMPONENT("iscsi_init", SPDK_LOG_ISCSI_INIT)
931