xref: /spdk/lib/iscsi/tgt_node.c (revision 8bb0ded3e55c182cea67af1f6790f8de5f38c05f)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/sock.h"
38 #include "spdk/scsi.h"
39 
40 #include "spdk/log.h"
41 
42 #include "iscsi/iscsi.h"
43 #include "iscsi/conn.h"
44 #include "iscsi/tgt_node.h"
45 #include "iscsi/portal_grp.h"
46 #include "iscsi/init_grp.h"
47 #include "iscsi/task.h"
48 
49 #define MAX_TMPBUF 4096
50 #define MAX_MASKBUF 128
51 
52 
53 #define MAX_TMP_NAME_BUF (11 /* TargetName= */ + MAX_TARGET_NAME + 1 /* null */)
54 #define MAX_TMP_ADDR_BUF (14 /* TargetAddress= */ + MAX_PORTAL_ADDR + 1 /* : */ + \
55 			  MAX_PORTAL_PORT + 1 /* , */ + 10 /* max length of int in Decimal */ + 1 /* null */)
56 
57 static bool
58 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
59 {
60 	struct in6_addr in6_mask;
61 	struct in6_addr in6_addr;
62 	char mask[MAX_MASKBUF];
63 	const char *p;
64 	size_t n;
65 	int bits, bmask;
66 	int i;
67 
68 	if (netmask[0] != '[') {
69 		return false;
70 	}
71 	p = strchr(netmask, ']');
72 	if (p == NULL) {
73 		return false;
74 	}
75 	n = p - (netmask + 1);
76 	if (n + 1 > sizeof mask) {
77 		return false;
78 	}
79 
80 	memcpy(mask, netmask + 1, n);
81 	mask[n] = '\0';
82 	p++;
83 
84 	if (p[0] == '/') {
85 		bits = (int) strtol(p + 1, NULL, 10);
86 		if (bits <= 0 || bits > 128) {
87 			return false;
88 		}
89 	} else {
90 		bits = 128;
91 	}
92 
93 #if 0
94 	SPDK_DEBUGLOG(iscsi, "input %s\n", addr);
95 	SPDK_DEBUGLOG(iscsi, "mask  %s / %d\n", mask, bits);
96 #endif
97 
98 	/* presentation to network order binary */
99 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
100 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
101 		return false;
102 	}
103 
104 	/* check 128bits */
105 	for (i = 0; i < (bits / 8); i++) {
106 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
107 			return false;
108 		}
109 	}
110 	if (bits % 8) {
111 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
112 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
113 			return false;
114 		}
115 	}
116 
117 	/* match */
118 	return true;
119 }
120 
121 static bool
122 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
123 {
124 	struct in_addr in4_mask;
125 	struct in_addr in4_addr;
126 	char mask[MAX_MASKBUF];
127 	const char *p;
128 	uint32_t bmask;
129 	size_t n;
130 	int bits;
131 
132 	p = strchr(netmask, '/');
133 	if (p == NULL) {
134 		p = netmask + strlen(netmask);
135 	}
136 	n = p - netmask;
137 	if (n + 1 > sizeof mask) {
138 		return false;
139 	}
140 
141 	memcpy(mask, netmask, n);
142 	mask[n] = '\0';
143 
144 	if (p[0] == '/') {
145 		bits = (int) strtol(p + 1, NULL, 10);
146 		if (bits <= 0 || bits > 32) {
147 			return false;
148 		}
149 	} else {
150 		bits = 32;
151 	}
152 
153 	/* presentation to network order binary */
154 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
155 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
156 		return false;
157 	}
158 
159 	/* check 32bits */
160 	bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
161 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
162 		return false;
163 	}
164 
165 	/* match */
166 	return true;
167 }
168 
169 static bool
170 iscsi_netmask_allow_addr(const char *netmask, const char *addr)
171 {
172 	if (netmask == NULL || addr == NULL) {
173 		return false;
174 	}
175 	if (strcasecmp(netmask, "ANY") == 0) {
176 		return true;
177 	}
178 	if (netmask[0] == '[') {
179 		/* IPv6 */
180 		if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
181 			return true;
182 		}
183 	} else {
184 		/* IPv4 */
185 		if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
186 			return true;
187 		}
188 	}
189 	return false;
190 }
191 
192 static bool
193 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
194 			  const char *addr)
195 {
196 	struct spdk_iscsi_initiator_netmask *imask;
197 
198 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
199 		SPDK_DEBUGLOG(iscsi, "netmask=%s, addr=%s\n",
200 			      imask->mask, addr);
201 		if (iscsi_netmask_allow_addr(imask->mask, addr)) {
202 			return true;
203 		}
204 	}
205 	return false;
206 }
207 
208 static int
209 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
210 				const char *iqn, bool *result)
211 {
212 	struct spdk_iscsi_initiator_name *iname;
213 
214 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
215 		/* denied if iqn is matched */
216 		if ((iname->name[0] == '!')
217 		    && (strcasecmp(&iname->name[1], "ANY") == 0
218 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
219 			*result = false;
220 			return 0;
221 		}
222 		/* allowed if iqn is matched */
223 		if (strcasecmp(iname->name, "ANY") == 0
224 		    || strcasecmp(iname->name, iqn) == 0) {
225 			*result = true;
226 			return 0;
227 		}
228 	}
229 	return -1;
230 }
231 
232 static struct spdk_iscsi_pg_map *
233 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
234 			   struct spdk_iscsi_portal_grp *pg);
235 
236 bool
237 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
238 		      struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
239 {
240 	struct spdk_iscsi_portal_grp *pg;
241 	struct spdk_iscsi_pg_map *pg_map;
242 	struct spdk_iscsi_ig_map *ig_map;
243 	int rc;
244 	bool allowed = false;
245 
246 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
247 		return false;
248 	}
249 	pg = conn->portal->group;
250 
251 	SPDK_DEBUGLOG(iscsi, "pg=%d, iqn=%s, addr=%s\n",
252 		      pg->tag, iqn, addr);
253 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
254 	if (pg_map == NULL) {
255 		return false;
256 	}
257 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
258 		rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
259 		if (rc == 0) {
260 			if (allowed == false) {
261 				goto denied;
262 			} else {
263 				if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
264 					return true;
265 				}
266 			}
267 		} else {
268 			/* netmask is denied in this initiator group */
269 		}
270 	}
271 
272 denied:
273 	SPDK_DEBUGLOG(iscsi, "access denied from %s (%s) to %s (%s:%s,%d)\n",
274 		      iqn, addr, target->name, conn->portal_host,
275 		      conn->portal_port, conn->pg_tag);
276 	return false;
277 }
278 
279 static bool
280 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
281 {
282 	struct spdk_iscsi_pg_map *pg_map;
283 	struct spdk_iscsi_ig_map *ig_map;
284 	int rc;
285 	bool result = false;
286 
287 	if (target == NULL || iqn == NULL) {
288 		return false;
289 	}
290 
291 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
292 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
293 			rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
294 			if (rc == 0) {
295 				return result;
296 			}
297 		}
298 	}
299 
300 	return false;
301 }
302 
303 static bool
304 iscsi_copy_str(char *data, int *total, int alloc_len,
305 	       int *previous_completed_len, int expected_size, char *src)
306 {
307 	int len = 0;
308 
309 	assert(*previous_completed_len >= 0);
310 
311 	if (alloc_len - *total < 1) {
312 		return true;
313 	}
314 
315 	if (*previous_completed_len < expected_size) {
316 		len = spdk_min(alloc_len - *total, expected_size - *previous_completed_len);
317 		memcpy((char *)data + *total, src + *previous_completed_len, len);
318 		*total += len;
319 		*previous_completed_len = 0;
320 	} else {
321 		*previous_completed_len -= expected_size;
322 	}
323 
324 	return false;
325 }
326 
327 static int
328 iscsi_send_tgt_portals(struct spdk_iscsi_conn *conn,
329 		       struct spdk_iscsi_tgt_node *target,
330 		       uint8_t *data, int alloc_len, int total,
331 		       int *previous_completed_len, bool *no_buf_space)
332 {
333 	char buf[MAX_TARGET_ADDR + 2];
334 	struct spdk_iscsi_portal_grp *pg;
335 	struct spdk_iscsi_pg_map *pg_map;
336 	struct spdk_iscsi_portal *p;
337 	char *host;
338 	char tmp_buf[MAX_TMP_ADDR_BUF];
339 	int len;
340 
341 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
342 		pg = pg_map->pg;
343 
344 		if (pg->is_private) {
345 			/* Skip the private portal group. Portals in the private portal group
346 			 * will be returned only by temporary login redirection responses.
347 			 */
348 			continue;
349 		}
350 
351 		TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
352 			host = p->host;
353 			/* wildcard? */
354 			if (strcasecmp(host, "[::]") == 0 || strcasecmp(host, "0.0.0.0") == 0) {
355 				if (spdk_sock_is_ipv6(conn->sock)) {
356 					snprintf(buf, sizeof buf, "[%s]", conn->target_addr);
357 					host = buf;
358 				} else if (spdk_sock_is_ipv4(conn->sock)) {
359 					snprintf(buf, sizeof buf, "%s", conn->target_addr);
360 					host = buf;
361 				} else {
362 					/* skip portal for the family */
363 					continue;
364 				}
365 			}
366 			SPDK_DEBUGLOG(iscsi, "TargetAddress=%s:%s,%d\n",
367 				      host, p->port, pg->tag);
368 
369 			memset(tmp_buf, 0, sizeof(tmp_buf));
370 			/* Caculate the whole string size */
371 			len = snprintf(NULL, 0, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
372 			assert(len < MAX_TMPBUF);
373 
374 			/* string contents are not fully copied */
375 			if (*previous_completed_len < len) {
376 				/* Copy the string into the temporary buffer */
377 				snprintf(tmp_buf, len + 1, "TargetAddress=%s:%s,%d", host, p->port, pg->tag);
378 			}
379 
380 			*no_buf_space = iscsi_copy_str(data, &total, alloc_len, previous_completed_len,
381 						       len + 1, tmp_buf);
382 			if (*no_buf_space) {
383 				break;
384 			}
385 		}
386 	}
387 
388 	return total;
389 }
390 
391 int
392 iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
393 		const char *tiqn, uint8_t *data, int alloc_len, int data_len)
394 {
395 	struct spdk_iscsi_tgt_node *target;
396 	int total;
397 	int len;
398 	int rc;
399 	int previous_completed_size = 0;
400 	bool no_buf_space = false;
401 	char tmp_buf[MAX_TMP_NAME_BUF];
402 
403 	if (conn == NULL) {
404 		return 0;
405 	}
406 	previous_completed_size = conn->send_tgt_completed_size;
407 
408 	total = data_len;
409 	if (alloc_len < 1) {
410 		return 0;
411 	}
412 	if (total >= alloc_len) {
413 		total = alloc_len;
414 		data[total - 1] = '\0';
415 		return total;
416 	}
417 
418 	pthread_mutex_lock(&g_iscsi.mutex);
419 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
420 		if (strcasecmp(tiqn, "ALL") != 0
421 		    && strcasecmp(tiqn, target->name) != 0) {
422 			continue;
423 		}
424 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
425 		if (rc == 0) {
426 			continue;
427 		}
428 
429 		memset(tmp_buf, 0, sizeof(tmp_buf));
430 		/* Calculate the whole string size */
431 		len = snprintf(NULL, 0, "TargetName=%s", target->name);
432 		assert(len < MAX_TMPBUF);
433 
434 		/* String contents are not copyied */
435 		if (previous_completed_size < len) {
436 			/* Copy the string into the temporary buffer */
437 			snprintf(tmp_buf, len + 1, "TargetName=%s", target->name);
438 		}
439 
440 		no_buf_space = iscsi_copy_str(data, &total, alloc_len, &previous_completed_size,
441 					      len + 1, tmp_buf);
442 		if (no_buf_space) {
443 			break;
444 		}
445 
446 		total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total,
447 					       &previous_completed_size, &no_buf_space);
448 		if (no_buf_space) {
449 			break;
450 		}
451 	}
452 	pthread_mutex_unlock(&g_iscsi.mutex);
453 
454 	/* Only set it when it is not succesufully completed */
455 	if (no_buf_space) {
456 		conn->send_tgt_completed_size += total;
457 	} else {
458 		conn->send_tgt_completed_size = 0;
459 	}
460 
461 	return total;
462 }
463 
464 struct spdk_iscsi_tgt_node *
465 iscsi_find_tgt_node(const char *target_name)
466 {
467 	struct spdk_iscsi_tgt_node *target;
468 
469 	if (target_name == NULL) {
470 		return NULL;
471 	}
472 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
473 		if (strcasecmp(target_name, target->name) == 0) {
474 			return target;
475 		}
476 	}
477 	return NULL;
478 }
479 
480 static int
481 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
482 {
483 	pthread_mutex_lock(&g_iscsi.mutex);
484 
485 	if (iscsi_find_tgt_node(target->name) != NULL) {
486 		pthread_mutex_unlock(&g_iscsi.mutex);
487 		return -EEXIST;
488 	}
489 
490 	TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
491 
492 	pthread_mutex_unlock(&g_iscsi.mutex);
493 	return 0;
494 }
495 
496 static int
497 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
498 {
499 	struct spdk_iscsi_tgt_node *t;
500 
501 	TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
502 		if (t == target) {
503 			TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
504 			return 0;
505 		}
506 	}
507 
508 	return -1;
509 }
510 
511 static struct spdk_iscsi_ig_map *
512 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
513 			 struct spdk_iscsi_init_grp *ig)
514 {
515 	struct spdk_iscsi_ig_map *ig_map;
516 
517 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
518 		if (ig_map->ig == ig) {
519 			return ig_map;
520 		}
521 	}
522 
523 	return NULL;
524 }
525 
526 static struct spdk_iscsi_ig_map *
527 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
528 			struct spdk_iscsi_init_grp *ig)
529 {
530 	struct spdk_iscsi_ig_map *ig_map;
531 
532 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
533 		return NULL;
534 	}
535 
536 	ig_map = malloc(sizeof(*ig_map));
537 	if (ig_map == NULL) {
538 		return NULL;
539 	}
540 
541 	ig_map->ig = ig;
542 	ig->ref++;
543 	pg_map->num_ig_maps++;
544 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
545 
546 	return ig_map;
547 }
548 
549 static void
550 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
551 			    struct spdk_iscsi_ig_map *ig_map)
552 {
553 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
554 	pg_map->num_ig_maps--;
555 	ig_map->ig->ref--;
556 	free(ig_map);
557 }
558 
559 static int
560 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
561 			   struct spdk_iscsi_init_grp *ig)
562 {
563 	struct spdk_iscsi_ig_map *ig_map;
564 
565 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
566 	if (ig_map == NULL) {
567 		return -ENOENT;
568 	}
569 
570 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
571 	return 0;
572 }
573 
574 static void
575 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
576 {
577 	struct spdk_iscsi_ig_map *ig_map, *tmp;
578 
579 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
580 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
581 	}
582 }
583 
584 static struct spdk_iscsi_pg_map *
585 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
586 			   struct spdk_iscsi_portal_grp *pg)
587 {
588 	struct spdk_iscsi_pg_map *pg_map;
589 
590 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
591 		if (pg_map->pg == pg) {
592 			return pg_map;
593 		}
594 	}
595 
596 	return NULL;
597 }
598 
599 static struct spdk_iscsi_pg_map *
600 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
601 			  struct spdk_iscsi_portal_grp *pg)
602 {
603 	struct spdk_iscsi_pg_map *pg_map;
604 	char port_name[MAX_TMPBUF];
605 	int rc;
606 
607 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
608 		return NULL;
609 	}
610 
611 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
612 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
613 			    SPDK_SCSI_DEV_MAX_PORTS);
614 		return NULL;
615 	}
616 
617 	pg_map = calloc(1, sizeof(*pg_map));
618 	if (pg_map == NULL) {
619 		return NULL;
620 	}
621 
622 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
623 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
624 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
625 	if (rc != 0) {
626 		free(pg_map);
627 		return NULL;
628 	}
629 
630 	TAILQ_INIT(&pg_map->ig_map_head);
631 	pg_map->num_ig_maps = 0;
632 	pg->ref++;
633 	pg_map->pg = pg;
634 	target->num_pg_maps++;
635 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
636 
637 	return pg_map;
638 }
639 
640 static void
641 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
642 			      struct spdk_iscsi_pg_map *pg_map)
643 {
644 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
645 	target->num_pg_maps--;
646 	pg_map->pg->ref--;
647 
648 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
649 
650 	free(pg_map);
651 }
652 
653 static int
654 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
655 			     struct spdk_iscsi_portal_grp *pg)
656 {
657 	struct spdk_iscsi_pg_map *pg_map;
658 
659 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
660 	if (pg_map == NULL) {
661 		return -ENOENT;
662 	}
663 
664 	if (pg_map->num_ig_maps > 0) {
665 		SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n",
666 			      pg_map->num_ig_maps);
667 	}
668 
669 	iscsi_pg_map_delete_all_ig_maps(pg_map);
670 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
671 	return 0;
672 }
673 
674 static void
675 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
676 			      struct spdk_iscsi_init_grp *ig)
677 {
678 	struct spdk_iscsi_pg_map *pg_map, *tmp;
679 
680 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
681 		iscsi_pg_map_delete_ig_map(pg_map, ig);
682 		if (pg_map->num_ig_maps == 0) {
683 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
684 		}
685 	}
686 }
687 
688 static void
689 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
690 {
691 	struct spdk_iscsi_pg_map *pg_map, *tmp;
692 
693 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
694 		iscsi_pg_map_delete_all_ig_maps(pg_map);
695 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
696 	}
697 }
698 
699 static void
700 _iscsi_tgt_node_destruct(void *cb_arg, int rc)
701 {
702 	struct spdk_iscsi_tgt_node *target = cb_arg;
703 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
704 	void *destruct_cb_arg = target->destruct_cb_arg;
705 
706 	if (rc != 0) {
707 		if (destruct_cb_fn) {
708 			destruct_cb_fn(destruct_cb_arg, rc);
709 		}
710 		return;
711 	}
712 
713 	pthread_mutex_lock(&g_iscsi.mutex);
714 	iscsi_tgt_node_delete_all_pg_maps(target);
715 	pthread_mutex_unlock(&g_iscsi.mutex);
716 
717 	pthread_mutex_destroy(&target->mutex);
718 	free(target);
719 
720 	if (destruct_cb_fn) {
721 		destruct_cb_fn(destruct_cb_arg, 0);
722 	}
723 }
724 
725 static int
726 iscsi_tgt_node_check_active_conns(void *arg)
727 {
728 	struct spdk_iscsi_tgt_node *target = arg;
729 
730 	if (iscsi_get_active_conns(target) != 0) {
731 		return SPDK_POLLER_BUSY;
732 	}
733 
734 	spdk_poller_unregister(&target->destruct_poller);
735 
736 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
737 
738 	return SPDK_POLLER_BUSY;
739 }
740 
741 static void
742 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
743 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
744 {
745 	if (target == NULL) {
746 		if (cb_fn) {
747 			cb_fn(cb_arg, -ENOENT);
748 		}
749 		return;
750 	}
751 
752 	if (target->destructed) {
753 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
754 		if (cb_fn) {
755 			cb_fn(cb_arg, -EBUSY);
756 		}
757 		return;
758 	}
759 
760 	target->destructed = true;
761 	target->destruct_cb_fn = cb_fn;
762 	target->destruct_cb_arg = cb_arg;
763 
764 	iscsi_conns_request_logout(target, -1);
765 
766 	if (iscsi_get_active_conns(target) != 0) {
767 		target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
768 					  target, 10);
769 	} else {
770 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
771 	}
772 
773 }
774 
775 static int
776 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
777 				int pg_tag, int ig_tag)
778 {
779 	struct spdk_iscsi_portal_grp	*pg;
780 	struct spdk_iscsi_init_grp	*ig;
781 	struct spdk_iscsi_pg_map	*pg_map;
782 	struct spdk_iscsi_ig_map	*ig_map;
783 
784 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
785 	if (pg == NULL) {
786 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
787 		return -ENOENT;
788 	}
789 	ig = iscsi_init_grp_find_by_tag(ig_tag);
790 	if (ig == NULL) {
791 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
792 		return -ENOENT;
793 	}
794 
795 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
796 	if (pg_map == NULL) {
797 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
798 		return -ENOENT;
799 	}
800 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
801 	if (ig_map == NULL) {
802 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
803 		return -ENOENT;
804 	}
805 
806 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
807 	if (pg_map->num_ig_maps == 0) {
808 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
809 	}
810 
811 	return 0;
812 }
813 
814 static int
815 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
816 			     int pg_tag, int ig_tag)
817 {
818 	struct spdk_iscsi_portal_grp	*pg;
819 	struct spdk_iscsi_pg_map	*pg_map;
820 	struct spdk_iscsi_init_grp	*ig;
821 	struct spdk_iscsi_ig_map	*ig_map;
822 	bool				new_pg_map = false;
823 
824 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
825 	if (pg == NULL) {
826 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
827 		return -ENOENT;
828 	}
829 	ig = iscsi_init_grp_find_by_tag(ig_tag);
830 	if (ig == NULL) {
831 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
832 		return -ENOENT;
833 	}
834 
835 	/* get existing pg_map or create new pg_map and add it to target */
836 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
837 	if (pg_map == NULL) {
838 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
839 		if (pg_map == NULL) {
840 			goto failed;
841 		}
842 		new_pg_map = true;
843 	}
844 
845 	/* create new ig_map and add it to pg_map */
846 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
847 	if (ig_map == NULL) {
848 		goto failed;
849 	}
850 
851 	return 0;
852 
853 failed:
854 	if (new_pg_map) {
855 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
856 	}
857 
858 	return -1;
859 }
860 
861 int
862 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
863 				 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
864 {
865 	uint16_t i;
866 	int rc;
867 
868 	pthread_mutex_lock(&g_iscsi.mutex);
869 	for (i = 0; i < num_maps; i++) {
870 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
871 						  ig_tag_list[i]);
872 		if (rc != 0) {
873 			SPDK_ERRLOG("could not add map to target\n");
874 			goto invalid;
875 		}
876 	}
877 	pthread_mutex_unlock(&g_iscsi.mutex);
878 	return 0;
879 
880 invalid:
881 	for (; i > 0; --i) {
882 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
883 						ig_tag_list[i - 1]);
884 	}
885 	pthread_mutex_unlock(&g_iscsi.mutex);
886 	return -1;
887 }
888 
889 int
890 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
891 				    int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
892 {
893 	uint16_t i;
894 	int rc;
895 
896 	pthread_mutex_lock(&g_iscsi.mutex);
897 	for (i = 0; i < num_maps; i++) {
898 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
899 						     ig_tag_list[i]);
900 		if (rc != 0) {
901 			SPDK_ERRLOG("could not delete map from target\n");
902 			goto invalid;
903 		}
904 	}
905 	pthread_mutex_unlock(&g_iscsi.mutex);
906 	return 0;
907 
908 invalid:
909 	for (; i > 0; --i) {
910 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
911 						  ig_tag_list[i - 1]);
912 		if (rc != 0) {
913 			iscsi_tgt_node_delete_all_pg_maps(target);
914 			break;
915 		}
916 	}
917 	pthread_mutex_unlock(&g_iscsi.mutex);
918 	return -1;
919 }
920 
921 int
922 iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
923 			const char *host, const char *port)
924 {
925 	struct spdk_iscsi_portal_grp *pg;
926 	struct spdk_iscsi_pg_map *pg_map;
927 	struct sockaddr_storage sa;
928 
929 	if (target == NULL) {
930 		return -EINVAL;
931 	}
932 
933 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
934 	if (pg == NULL) {
935 		SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
936 		return -EINVAL;
937 	}
938 
939 	if (pg->is_private) {
940 		SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
941 		return -EINVAL;
942 	}
943 
944 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
945 	if (pg_map == NULL) {
946 		SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
947 		return -EINVAL;
948 	}
949 
950 	if (host == NULL && port == NULL) {
951 		/* Clear redirect setting. */
952 		memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
953 		memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
954 	} else {
955 		if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
956 			SPDK_ERRLOG("IP address-port pair is not valid.\n");
957 			return -EINVAL;
958 		}
959 
960 		if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
961 			SPDK_ERRLOG("IP address-port pair must be chosen from a "
962 				    "different private portal group\n");
963 			return -EINVAL;
964 		}
965 
966 		snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
967 		snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
968 	}
969 
970 	return 0;
971 }
972 
973 bool
974 iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
975 			     struct spdk_iscsi_tgt_node *target,
976 			     char *buf, int buf_len)
977 {
978 	struct spdk_iscsi_pg_map *pg_map;
979 
980 	if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
981 		return false;
982 	}
983 
984 	pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
985 	if (pg_map == NULL) {
986 		return false;
987 	}
988 
989 	if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
990 		return false;
991 	}
992 
993 	snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
994 
995 	return true;
996 }
997 
998 static int
999 check_iscsi_name(const char *name)
1000 {
1001 	const unsigned char *up = (const unsigned char *) name;
1002 	size_t n;
1003 
1004 	/* valid iSCSI name no larger than 223 bytes */
1005 	if (strlen(name) > MAX_TARGET_NAME) {
1006 		return -1;
1007 	}
1008 
1009 	/* valid iSCSI name? */
1010 	for (n = 0; up[n] != 0; n++) {
1011 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
1012 			return -1;
1013 		}
1014 		if (up[n] == 0x2fU) {
1015 			return -1;
1016 		}
1017 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
1018 			return -1;
1019 		}
1020 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
1021 			return -1;
1022 		}
1023 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
1024 			return -1;
1025 		}
1026 		if (isspace(up[n])) {
1027 			return -1;
1028 		}
1029 	}
1030 	/* valid format? */
1031 	if (strncasecmp(name, "iqn.", 4) == 0) {
1032 		/* iqn.YYYY-MM.reversed.domain.name */
1033 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
1034 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
1035 		    || !isdigit(up[10]) || up[11] != '.') {
1036 			SPDK_ERRLOG("invalid iqn format. "
1037 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
1038 			return -1;
1039 		}
1040 	} else if (strncasecmp(name, "eui.", 4) == 0) {
1041 		/* EUI-64 -> 16bytes */
1042 		/* XXX */
1043 	} else if (strncasecmp(name, "naa.", 4) == 0) {
1044 		/* 64bit -> 16bytes, 128bit -> 32bytes */
1045 		/* XXX */
1046 	}
1047 	/* OK */
1048 	return 0;
1049 }
1050 
1051 bool
1052 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
1053 {
1054 	if (group < 0) {
1055 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
1056 		return false;
1057 	}
1058 	if ((!disable && !require && !mutual) ||	/* Auto */
1059 	    (disable && !require && !mutual) ||	/* None */
1060 	    (!disable && require && !mutual) ||	/* CHAP */
1061 	    (!disable && require && mutual)) {	/* CHAP Mutual */
1062 		return true;
1063 	}
1064 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1065 		    disable, require, mutual);
1066 	return false;
1067 }
1068 
1069 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1070 		const char *name, const char *alias,
1071 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1072 		const char *bdev_name_list[], int *lun_id_list, int num_luns,
1073 		int queue_depth,
1074 		bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1075 		bool header_digest, bool data_digest)
1076 {
1077 	char				fullname[MAX_TMPBUF];
1078 	struct spdk_iscsi_tgt_node	*target;
1079 	int				rc;
1080 
1081 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1082 				     mutual_chap, chap_group)) {
1083 		return NULL;
1084 	}
1085 
1086 	if (num_maps == 0) {
1087 		SPDK_ERRLOG("num_maps = 0\n");
1088 		return NULL;
1089 	}
1090 
1091 	if (name == NULL) {
1092 		SPDK_ERRLOG("TargetName not found\n");
1093 		return NULL;
1094 	}
1095 
1096 	if (strncasecmp(name, "iqn.", 4) != 0
1097 	    && strncasecmp(name, "eui.", 4) != 0
1098 	    && strncasecmp(name, "naa.", 4) != 0) {
1099 		snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
1100 	} else {
1101 		snprintf(fullname, sizeof(fullname), "%s", name);
1102 	}
1103 
1104 	if (check_iscsi_name(fullname) != 0) {
1105 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1106 			    name);
1107 		return NULL;
1108 	}
1109 
1110 	target = calloc(1, sizeof(*target));
1111 	if (!target) {
1112 		SPDK_ERRLOG("could not allocate target\n");
1113 		return NULL;
1114 	}
1115 
1116 	rc = pthread_mutex_init(&target->mutex, NULL);
1117 	if (rc != 0) {
1118 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1119 		iscsi_tgt_node_destruct(target, NULL, NULL);
1120 		return NULL;
1121 	}
1122 
1123 	target->num = target_index;
1124 
1125 	memcpy(target->name, fullname, strlen(fullname));
1126 
1127 	if (alias != NULL) {
1128 		if (strlen(alias) > MAX_TARGET_NAME) {
1129 			iscsi_tgt_node_destruct(target, NULL, NULL);
1130 			return NULL;
1131 		}
1132 		memcpy(target->alias, alias, strlen(alias));
1133 	}
1134 
1135 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1136 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1137 	if (!target->dev) {
1138 		SPDK_ERRLOG("Could not construct SCSI device\n");
1139 		iscsi_tgt_node_destruct(target, NULL, NULL);
1140 		return NULL;
1141 	}
1142 
1143 	TAILQ_INIT(&target->pg_map_head);
1144 	rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1145 					      ig_tag_list, num_maps);
1146 	if (rc != 0) {
1147 		SPDK_ERRLOG("could not add map to target\n");
1148 		iscsi_tgt_node_destruct(target, NULL, NULL);
1149 		return NULL;
1150 	}
1151 
1152 	target->disable_chap = disable_chap;
1153 	target->require_chap = require_chap;
1154 	target->mutual_chap = mutual_chap;
1155 	target->chap_group = chap_group;
1156 	target->header_digest = header_digest;
1157 	target->data_digest = data_digest;
1158 
1159 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1160 		target->queue_depth = queue_depth;
1161 	} else {
1162 		SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n",
1163 			      queue_depth, g_iscsi.MaxQueueDepth);
1164 		target->queue_depth = g_iscsi.MaxQueueDepth;
1165 	}
1166 
1167 	rc = iscsi_tgt_node_register(target);
1168 	if (rc != 0) {
1169 		SPDK_ERRLOG("register target is failed\n");
1170 		iscsi_tgt_node_destruct(target, NULL, NULL);
1171 		return NULL;
1172 	}
1173 
1174 	return target;
1175 }
1176 
1177 void
1178 iscsi_shutdown_tgt_nodes(void)
1179 {
1180 	struct spdk_iscsi_tgt_node *target;
1181 
1182 	pthread_mutex_lock(&g_iscsi.mutex);
1183 	while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1184 		target = TAILQ_FIRST(&g_iscsi.target_head);
1185 		TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1186 
1187 		pthread_mutex_unlock(&g_iscsi.mutex);
1188 
1189 		iscsi_tgt_node_destruct(target, NULL, NULL);
1190 
1191 		pthread_mutex_lock(&g_iscsi.mutex);
1192 	}
1193 	pthread_mutex_unlock(&g_iscsi.mutex);
1194 }
1195 
1196 void
1197 iscsi_shutdown_tgt_node_by_name(const char *target_name,
1198 				iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1199 {
1200 	struct spdk_iscsi_tgt_node *target;
1201 
1202 	pthread_mutex_lock(&g_iscsi.mutex);
1203 	target = iscsi_find_tgt_node(target_name);
1204 	if (target != NULL) {
1205 		iscsi_tgt_node_unregister(target);
1206 		pthread_mutex_unlock(&g_iscsi.mutex);
1207 
1208 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1209 
1210 		return;
1211 	}
1212 	pthread_mutex_unlock(&g_iscsi.mutex);
1213 
1214 	if (cb_fn) {
1215 		cb_fn(cb_arg, -ENOENT);
1216 	}
1217 }
1218 
1219 bool
1220 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1221 {
1222 	return target->destructed;
1223 }
1224 
1225 int
1226 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1227 			    struct spdk_iscsi_tgt_node *target)
1228 {
1229 	int i;
1230 	struct spdk_iscsi_task *task;
1231 
1232 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1233 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1234 
1235 		if (!lun) {
1236 			continue;
1237 		}
1238 
1239 		/* we create a fake management task per LUN to cleanup */
1240 		task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1241 		if (!task) {
1242 			SPDK_ERRLOG("Unable to acquire task\n");
1243 			return -1;
1244 		}
1245 
1246 		task->scsi.target_port = conn->target_port;
1247 		task->scsi.initiator_port = conn->initiator_port;
1248 		task->scsi.lun = lun;
1249 
1250 		iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1251 	}
1252 
1253 	return 0;
1254 }
1255 
1256 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1257 			       struct spdk_iscsi_init_grp *initiator_group)
1258 {
1259 	struct spdk_iscsi_tgt_node *target;
1260 
1261 	pthread_mutex_lock(&g_iscsi.mutex);
1262 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1263 		if (portal_group) {
1264 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1265 		}
1266 		if (initiator_group) {
1267 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1268 		}
1269 	}
1270 	pthread_mutex_unlock(&g_iscsi.mutex);
1271 }
1272 
1273 int
1274 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1275 		       const char *bdev_name, int lun_id)
1276 {
1277 	struct spdk_scsi_dev *dev;
1278 	int rc;
1279 
1280 	if (target->num_active_conns > 0) {
1281 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1282 			    target->num_active_conns);
1283 		return -1;
1284 	}
1285 
1286 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1287 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1288 		return -1;
1289 	}
1290 
1291 	dev = target->dev;
1292 	if (dev == NULL) {
1293 		SPDK_ERRLOG("SCSI device is not found\n");
1294 		return -1;
1295 	}
1296 
1297 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1298 	if (rc != 0) {
1299 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1300 		return -1;
1301 	}
1302 
1303 	return 0;
1304 }
1305 
1306 int
1307 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1308 			       bool disable_chap, bool require_chap,
1309 			       bool mutual_chap, int32_t chap_group)
1310 {
1311 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1312 				     mutual_chap, chap_group)) {
1313 		return -EINVAL;
1314 	}
1315 
1316 	pthread_mutex_lock(&target->mutex);
1317 	target->disable_chap = disable_chap;
1318 	target->require_chap = require_chap;
1319 	target->mutual_chap = mutual_chap;
1320 	target->chap_group = chap_group;
1321 	pthread_mutex_unlock(&target->mutex);
1322 
1323 	return 0;
1324 }
1325 
1326 static void
1327 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1328 			 struct spdk_json_write_ctx *w)
1329 {
1330 	struct spdk_iscsi_pg_map *pg_map;
1331 	struct spdk_iscsi_ig_map *ig_map;
1332 	int i;
1333 
1334 	spdk_json_write_object_begin(w);
1335 
1336 	spdk_json_write_named_string(w, "name", target->name);
1337 
1338 	if (target->alias[0] != '\0') {
1339 		spdk_json_write_named_string(w, "alias_name", target->alias);
1340 	}
1341 
1342 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1343 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1344 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1345 			spdk_json_write_object_begin(w);
1346 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1347 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1348 			spdk_json_write_object_end(w);
1349 		}
1350 	}
1351 	spdk_json_write_array_end(w);
1352 
1353 	spdk_json_write_named_array_begin(w, "luns");
1354 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1355 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1356 
1357 		if (lun) {
1358 			spdk_json_write_object_begin(w);
1359 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1360 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1361 			spdk_json_write_object_end(w);
1362 		}
1363 	}
1364 	spdk_json_write_array_end(w);
1365 
1366 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1367 
1368 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1369 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1370 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1371 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1372 
1373 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1374 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1375 
1376 	spdk_json_write_object_end(w);
1377 }
1378 
1379 static void
1380 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1381 			   struct spdk_json_write_ctx *w)
1382 {
1383 	spdk_json_write_object_begin(w);
1384 
1385 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1386 
1387 	spdk_json_write_name(w, "params");
1388 	iscsi_tgt_node_info_json(target, w);
1389 
1390 	spdk_json_write_object_end(w);
1391 }
1392 
1393 void
1394 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1395 {
1396 	struct spdk_iscsi_tgt_node *target;
1397 
1398 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1399 		iscsi_tgt_node_info_json(target, w);
1400 	}
1401 }
1402 
1403 void
1404 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1405 {
1406 	struct spdk_iscsi_tgt_node *target;
1407 
1408 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1409 		iscsi_tgt_node_config_json(target, w);
1410 	}
1411 }
1412