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