xref: /spdk/lib/iscsi/tgt_node.c (revision d73077b84a71985da1db1c9847ea7c042189bae2)
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 = conn->send_tgt_completed_size;
400 	bool no_buf_space = false;
401 	char tmp_buf[MAX_TMP_NAME_BUF];
402 
403 	if (conn == NULL) {
404 		return 0;
405 	}
406 
407 	total = data_len;
408 	if (alloc_len < 1) {
409 		return 0;
410 	}
411 	if (total >= alloc_len) {
412 		total = alloc_len;
413 		data[total - 1] = '\0';
414 		return total;
415 	}
416 
417 	pthread_mutex_lock(&g_iscsi.mutex);
418 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
419 		if (strcasecmp(tiqn, "ALL") != 0
420 		    && strcasecmp(tiqn, target->name) != 0) {
421 			continue;
422 		}
423 		rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn);
424 		if (rc == 0) {
425 			continue;
426 		}
427 
428 		memset(tmp_buf, 0, sizeof(tmp_buf));
429 		/* Calculate the whole string size */
430 		len = snprintf(NULL, 0, "TargetName=%s", target->name);
431 		assert(len < MAX_TMPBUF);
432 
433 		/* String contents are not copyied */
434 		if (previous_completed_size < len) {
435 			/* Copy the string into the temporary buffer */
436 			snprintf(tmp_buf, len + 1, "TargetName=%s", target->name);
437 		}
438 
439 		no_buf_space = iscsi_copy_str(data, &total, alloc_len, &previous_completed_size,
440 					      len + 1, tmp_buf);
441 		if (no_buf_space) {
442 			break;
443 		}
444 
445 		total = iscsi_send_tgt_portals(conn, target, data, alloc_len, total,
446 					       &previous_completed_size, &no_buf_space);
447 		if (no_buf_space) {
448 			break;
449 		}
450 	}
451 	pthread_mutex_unlock(&g_iscsi.mutex);
452 
453 	/* Only set it when it is not succesufully completed */
454 	if (no_buf_space) {
455 		conn->send_tgt_completed_size += total;
456 	} else {
457 		conn->send_tgt_completed_size = 0;
458 	}
459 
460 	return total;
461 }
462 
463 struct spdk_iscsi_tgt_node *
464 iscsi_find_tgt_node(const char *target_name)
465 {
466 	struct spdk_iscsi_tgt_node *target;
467 
468 	if (target_name == NULL) {
469 		return NULL;
470 	}
471 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
472 		if (strcasecmp(target_name, target->name) == 0) {
473 			return target;
474 		}
475 	}
476 	return NULL;
477 }
478 
479 static int
480 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
481 {
482 	pthread_mutex_lock(&g_iscsi.mutex);
483 
484 	if (iscsi_find_tgt_node(target->name) != NULL) {
485 		pthread_mutex_unlock(&g_iscsi.mutex);
486 		return -EEXIST;
487 	}
488 
489 	TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq);
490 
491 	pthread_mutex_unlock(&g_iscsi.mutex);
492 	return 0;
493 }
494 
495 static int
496 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
497 {
498 	struct spdk_iscsi_tgt_node *t;
499 
500 	TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) {
501 		if (t == target) {
502 			TAILQ_REMOVE(&g_iscsi.target_head, t, tailq);
503 			return 0;
504 		}
505 	}
506 
507 	return -1;
508 }
509 
510 static struct spdk_iscsi_ig_map *
511 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
512 			 struct spdk_iscsi_init_grp *ig)
513 {
514 	struct spdk_iscsi_ig_map *ig_map;
515 
516 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
517 		if (ig_map->ig == ig) {
518 			return ig_map;
519 		}
520 	}
521 
522 	return NULL;
523 }
524 
525 static struct spdk_iscsi_ig_map *
526 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
527 			struct spdk_iscsi_init_grp *ig)
528 {
529 	struct spdk_iscsi_ig_map *ig_map;
530 
531 	if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
532 		return NULL;
533 	}
534 
535 	ig_map = malloc(sizeof(*ig_map));
536 	if (ig_map == NULL) {
537 		return NULL;
538 	}
539 
540 	ig_map->ig = ig;
541 	ig->ref++;
542 	pg_map->num_ig_maps++;
543 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
544 
545 	return ig_map;
546 }
547 
548 static void
549 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
550 			    struct spdk_iscsi_ig_map *ig_map)
551 {
552 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
553 	pg_map->num_ig_maps--;
554 	ig_map->ig->ref--;
555 	free(ig_map);
556 }
557 
558 static int
559 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
560 			   struct spdk_iscsi_init_grp *ig)
561 {
562 	struct spdk_iscsi_ig_map *ig_map;
563 
564 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
565 	if (ig_map == NULL) {
566 		return -ENOENT;
567 	}
568 
569 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
570 	return 0;
571 }
572 
573 static void
574 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
575 {
576 	struct spdk_iscsi_ig_map *ig_map, *tmp;
577 
578 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
579 		_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
580 	}
581 }
582 
583 static struct spdk_iscsi_pg_map *
584 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
585 			   struct spdk_iscsi_portal_grp *pg)
586 {
587 	struct spdk_iscsi_pg_map *pg_map;
588 
589 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
590 		if (pg_map->pg == pg) {
591 			return pg_map;
592 		}
593 	}
594 
595 	return NULL;
596 }
597 
598 static struct spdk_iscsi_pg_map *
599 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
600 			  struct spdk_iscsi_portal_grp *pg)
601 {
602 	struct spdk_iscsi_pg_map *pg_map;
603 	char port_name[MAX_TMPBUF];
604 	int rc;
605 
606 	if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
607 		return NULL;
608 	}
609 
610 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
611 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
612 			    SPDK_SCSI_DEV_MAX_PORTS);
613 		return NULL;
614 	}
615 
616 	pg_map = calloc(1, sizeof(*pg_map));
617 	if (pg_map == NULL) {
618 		return NULL;
619 	}
620 
621 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
622 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
623 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
624 	if (rc != 0) {
625 		free(pg_map);
626 		return NULL;
627 	}
628 
629 	TAILQ_INIT(&pg_map->ig_map_head);
630 	pg_map->num_ig_maps = 0;
631 	pg->ref++;
632 	pg_map->pg = pg;
633 	target->num_pg_maps++;
634 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
635 
636 	return pg_map;
637 }
638 
639 static void
640 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
641 			      struct spdk_iscsi_pg_map *pg_map)
642 {
643 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
644 	target->num_pg_maps--;
645 	pg_map->pg->ref--;
646 
647 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
648 
649 	free(pg_map);
650 }
651 
652 static int
653 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
654 			     struct spdk_iscsi_portal_grp *pg)
655 {
656 	struct spdk_iscsi_pg_map *pg_map;
657 
658 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
659 	if (pg_map == NULL) {
660 		return -ENOENT;
661 	}
662 
663 	if (pg_map->num_ig_maps > 0) {
664 		SPDK_DEBUGLOG(iscsi, "delete %d ig_maps forcefully\n",
665 			      pg_map->num_ig_maps);
666 	}
667 
668 	iscsi_pg_map_delete_all_ig_maps(pg_map);
669 	_iscsi_tgt_node_delete_pg_map(target, pg_map);
670 	return 0;
671 }
672 
673 static void
674 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
675 			      struct spdk_iscsi_init_grp *ig)
676 {
677 	struct spdk_iscsi_pg_map *pg_map, *tmp;
678 
679 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
680 		iscsi_pg_map_delete_ig_map(pg_map, ig);
681 		if (pg_map->num_ig_maps == 0) {
682 			_iscsi_tgt_node_delete_pg_map(target, pg_map);
683 		}
684 	}
685 }
686 
687 static void
688 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
689 {
690 	struct spdk_iscsi_pg_map *pg_map, *tmp;
691 
692 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
693 		iscsi_pg_map_delete_all_ig_maps(pg_map);
694 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
695 	}
696 }
697 
698 static void
699 _iscsi_tgt_node_destruct(void *cb_arg, int rc)
700 {
701 	struct spdk_iscsi_tgt_node *target = cb_arg;
702 	iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn;
703 	void *destruct_cb_arg = target->destruct_cb_arg;
704 
705 	if (rc != 0) {
706 		if (destruct_cb_fn) {
707 			destruct_cb_fn(destruct_cb_arg, rc);
708 		}
709 		return;
710 	}
711 
712 	pthread_mutex_lock(&g_iscsi.mutex);
713 	iscsi_tgt_node_delete_all_pg_maps(target);
714 	pthread_mutex_unlock(&g_iscsi.mutex);
715 
716 	pthread_mutex_destroy(&target->mutex);
717 	free(target);
718 
719 	if (destruct_cb_fn) {
720 		destruct_cb_fn(destruct_cb_arg, 0);
721 	}
722 }
723 
724 static int
725 iscsi_tgt_node_check_active_conns(void *arg)
726 {
727 	struct spdk_iscsi_tgt_node *target = arg;
728 
729 	if (iscsi_get_active_conns(target) != 0) {
730 		return SPDK_POLLER_BUSY;
731 	}
732 
733 	spdk_poller_unregister(&target->destruct_poller);
734 
735 	spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
736 
737 	return SPDK_POLLER_BUSY;
738 }
739 
740 static void
741 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target,
742 			iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
743 {
744 	if (target == NULL) {
745 		if (cb_fn) {
746 			cb_fn(cb_arg, -ENOENT);
747 		}
748 		return;
749 	}
750 
751 	if (target->destructed) {
752 		SPDK_ERRLOG("Destructing %s is already started\n", target->name);
753 		if (cb_fn) {
754 			cb_fn(cb_arg, -EBUSY);
755 		}
756 		return;
757 	}
758 
759 	target->destructed = true;
760 	target->destruct_cb_fn = cb_fn;
761 	target->destruct_cb_arg = cb_arg;
762 
763 	iscsi_conns_request_logout(target, -1);
764 
765 	if (iscsi_get_active_conns(target) != 0) {
766 		target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns,
767 					  target, 10);
768 	} else {
769 		spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target);
770 	}
771 
772 }
773 
774 static int
775 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
776 				int pg_tag, int ig_tag)
777 {
778 	struct spdk_iscsi_portal_grp	*pg;
779 	struct spdk_iscsi_init_grp	*ig;
780 	struct spdk_iscsi_pg_map	*pg_map;
781 	struct spdk_iscsi_ig_map	*ig_map;
782 
783 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
784 	if (pg == NULL) {
785 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
786 		return -ENOENT;
787 	}
788 	ig = iscsi_init_grp_find_by_tag(ig_tag);
789 	if (ig == NULL) {
790 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
791 		return -ENOENT;
792 	}
793 
794 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
795 	if (pg_map == NULL) {
796 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
797 		return -ENOENT;
798 	}
799 	ig_map = iscsi_pg_map_find_ig_map(pg_map, ig);
800 	if (ig_map == NULL) {
801 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
802 		return -ENOENT;
803 	}
804 
805 	_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
806 	if (pg_map->num_ig_maps == 0) {
807 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
808 	}
809 
810 	return 0;
811 }
812 
813 static int
814 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
815 			     int pg_tag, int ig_tag)
816 {
817 	struct spdk_iscsi_portal_grp	*pg;
818 	struct spdk_iscsi_pg_map	*pg_map;
819 	struct spdk_iscsi_init_grp	*ig;
820 	struct spdk_iscsi_ig_map	*ig_map;
821 	bool				new_pg_map = false;
822 
823 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
824 	if (pg == NULL) {
825 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
826 		return -ENOENT;
827 	}
828 	ig = iscsi_init_grp_find_by_tag(ig_tag);
829 	if (ig == NULL) {
830 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
831 		return -ENOENT;
832 	}
833 
834 	/* get existing pg_map or create new pg_map and add it to target */
835 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
836 	if (pg_map == NULL) {
837 		pg_map = iscsi_tgt_node_add_pg_map(target, pg);
838 		if (pg_map == NULL) {
839 			goto failed;
840 		}
841 		new_pg_map = true;
842 	}
843 
844 	/* create new ig_map and add it to pg_map */
845 	ig_map = iscsi_pg_map_add_ig_map(pg_map, ig);
846 	if (ig_map == NULL) {
847 		goto failed;
848 	}
849 
850 	return 0;
851 
852 failed:
853 	if (new_pg_map) {
854 		_iscsi_tgt_node_delete_pg_map(target, pg_map);
855 	}
856 
857 	return -1;
858 }
859 
860 int
861 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
862 				 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
863 {
864 	uint16_t i;
865 	int rc;
866 
867 	pthread_mutex_lock(&g_iscsi.mutex);
868 	for (i = 0; i < num_maps; i++) {
869 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
870 						  ig_tag_list[i]);
871 		if (rc != 0) {
872 			SPDK_ERRLOG("could not add map to target\n");
873 			goto invalid;
874 		}
875 	}
876 	pthread_mutex_unlock(&g_iscsi.mutex);
877 	return 0;
878 
879 invalid:
880 	for (; i > 0; --i) {
881 		iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
882 						ig_tag_list[i - 1]);
883 	}
884 	pthread_mutex_unlock(&g_iscsi.mutex);
885 	return -1;
886 }
887 
888 int
889 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
890 				    int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
891 {
892 	uint16_t i;
893 	int rc;
894 
895 	pthread_mutex_lock(&g_iscsi.mutex);
896 	for (i = 0; i < num_maps; i++) {
897 		rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
898 						     ig_tag_list[i]);
899 		if (rc != 0) {
900 			SPDK_ERRLOG("could not delete map from target\n");
901 			goto invalid;
902 		}
903 	}
904 	pthread_mutex_unlock(&g_iscsi.mutex);
905 	return 0;
906 
907 invalid:
908 	for (; i > 0; --i) {
909 		rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
910 						  ig_tag_list[i - 1]);
911 		if (rc != 0) {
912 			iscsi_tgt_node_delete_all_pg_maps(target);
913 			break;
914 		}
915 	}
916 	pthread_mutex_unlock(&g_iscsi.mutex);
917 	return -1;
918 }
919 
920 int
921 iscsi_tgt_node_redirect(struct spdk_iscsi_tgt_node *target, int pg_tag,
922 			const char *host, const char *port)
923 {
924 	struct spdk_iscsi_portal_grp *pg;
925 	struct spdk_iscsi_pg_map *pg_map;
926 	struct sockaddr_storage sa;
927 
928 	if (target == NULL) {
929 		return -EINVAL;
930 	}
931 
932 	pg = iscsi_portal_grp_find_by_tag(pg_tag);
933 	if (pg == NULL) {
934 		SPDK_ERRLOG("Portal group %d is not found.\n", pg_tag);
935 		return -EINVAL;
936 	}
937 
938 	if (pg->is_private) {
939 		SPDK_ERRLOG("Portal group %d is not public portal group.\n", pg_tag);
940 		return -EINVAL;
941 	}
942 
943 	pg_map = iscsi_tgt_node_find_pg_map(target, pg);
944 	if (pg_map == NULL) {
945 		SPDK_ERRLOG("Portal group %d is not mapped.\n", pg_tag);
946 		return -EINVAL;
947 	}
948 
949 	if (host == NULL && port == NULL) {
950 		/* Clear redirect setting. */
951 		memset(pg_map->redirect_host, 0, MAX_PORTAL_ADDR + 1);
952 		memset(pg_map->redirect_port, 0, MAX_PORTAL_PORT + 1);
953 	} else {
954 		if (iscsi_parse_redirect_addr(&sa, host, port) != 0) {
955 			SPDK_ERRLOG("IP address-port pair is not valid.\n");
956 			return -EINVAL;
957 		}
958 
959 		if (iscsi_portal_grp_find_portal_by_addr(pg, port, host) != NULL) {
960 			SPDK_ERRLOG("IP address-port pair must be chosen from a "
961 				    "different private portal group\n");
962 			return -EINVAL;
963 		}
964 
965 		snprintf(pg_map->redirect_host, MAX_PORTAL_ADDR + 1, "%s", host);
966 		snprintf(pg_map->redirect_port, MAX_PORTAL_PORT + 1, "%s", port);
967 	}
968 
969 	return 0;
970 }
971 
972 bool
973 iscsi_tgt_node_is_redirected(struct spdk_iscsi_conn *conn,
974 			     struct spdk_iscsi_tgt_node *target,
975 			     char *buf, int buf_len)
976 {
977 	struct spdk_iscsi_pg_map *pg_map;
978 
979 	if (conn == NULL || target == NULL || buf == NULL || buf_len == 0) {
980 		return false;
981 	}
982 
983 	pg_map = iscsi_tgt_node_find_pg_map(target, conn->portal->group);
984 	if (pg_map == NULL) {
985 		return false;
986 	}
987 
988 	if (pg_map->redirect_host[0] == '\0' || pg_map->redirect_port[0] == '\0') {
989 		return false;
990 	}
991 
992 	snprintf(buf, buf_len, "%s:%s", pg_map->redirect_host, pg_map->redirect_port);
993 
994 	return true;
995 }
996 
997 static int
998 check_iscsi_name(const char *name)
999 {
1000 	const unsigned char *up = (const unsigned char *) name;
1001 	size_t n;
1002 
1003 	/* valid iSCSI name no larger than 223 bytes */
1004 	if (strlen(name) > MAX_TARGET_NAME) {
1005 		return -1;
1006 	}
1007 
1008 	/* valid iSCSI name? */
1009 	for (n = 0; up[n] != 0; n++) {
1010 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
1011 			return -1;
1012 		}
1013 		if (up[n] == 0x2fU) {
1014 			return -1;
1015 		}
1016 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
1017 			return -1;
1018 		}
1019 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
1020 			return -1;
1021 		}
1022 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
1023 			return -1;
1024 		}
1025 		if (isspace(up[n])) {
1026 			return -1;
1027 		}
1028 	}
1029 	/* valid format? */
1030 	if (strncasecmp(name, "iqn.", 4) == 0) {
1031 		/* iqn.YYYY-MM.reversed.domain.name */
1032 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
1033 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
1034 		    || !isdigit(up[10]) || up[11] != '.') {
1035 			SPDK_ERRLOG("invalid iqn format. "
1036 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
1037 			return -1;
1038 		}
1039 	} else if (strncasecmp(name, "eui.", 4) == 0) {
1040 		/* EUI-64 -> 16bytes */
1041 		/* XXX */
1042 	} else if (strncasecmp(name, "naa.", 4) == 0) {
1043 		/* 64bit -> 16bytes, 128bit -> 32bytes */
1044 		/* XXX */
1045 	}
1046 	/* OK */
1047 	return 0;
1048 }
1049 
1050 bool
1051 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group)
1052 {
1053 	if (group < 0) {
1054 		SPDK_ERRLOG("Invalid auth group ID (%d)\n", group);
1055 		return false;
1056 	}
1057 	if ((!disable && !require && !mutual) ||	/* Auto */
1058 	    (disable && !require && !mutual) ||	/* None */
1059 	    (!disable && require && !mutual) ||	/* CHAP */
1060 	    (!disable && require && mutual)) {	/* CHAP Mutual */
1061 		return true;
1062 	}
1063 	SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n",
1064 		    disable, require, mutual);
1065 	return false;
1066 }
1067 
1068 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index,
1069 		const char *name, const char *alias,
1070 		int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
1071 		const char *bdev_name_list[], int *lun_id_list, int num_luns,
1072 		int queue_depth,
1073 		bool disable_chap, bool require_chap, bool mutual_chap, int chap_group,
1074 		bool header_digest, bool data_digest)
1075 {
1076 	char				fullname[MAX_TMPBUF];
1077 	struct spdk_iscsi_tgt_node	*target;
1078 	int				rc;
1079 
1080 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1081 				     mutual_chap, chap_group)) {
1082 		return NULL;
1083 	}
1084 
1085 	if (num_maps == 0) {
1086 		SPDK_ERRLOG("num_maps = 0\n");
1087 		return NULL;
1088 	}
1089 
1090 	if (name == NULL) {
1091 		SPDK_ERRLOG("TargetName not found\n");
1092 		return NULL;
1093 	}
1094 
1095 	if (strncasecmp(name, "iqn.", 4) != 0
1096 	    && strncasecmp(name, "eui.", 4) != 0
1097 	    && strncasecmp(name, "naa.", 4) != 0) {
1098 		snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name);
1099 	} else {
1100 		snprintf(fullname, sizeof(fullname), "%s", name);
1101 	}
1102 
1103 	if (check_iscsi_name(fullname) != 0) {
1104 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
1105 			    name);
1106 		return NULL;
1107 	}
1108 
1109 	target = calloc(1, sizeof(*target));
1110 	if (!target) {
1111 		SPDK_ERRLOG("could not allocate target\n");
1112 		return NULL;
1113 	}
1114 
1115 	rc = pthread_mutex_init(&target->mutex, NULL);
1116 	if (rc != 0) {
1117 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
1118 		iscsi_tgt_node_destruct(target, NULL, NULL);
1119 		return NULL;
1120 	}
1121 
1122 	target->num = target_index;
1123 
1124 	memcpy(target->name, fullname, strlen(fullname));
1125 
1126 	if (alias != NULL) {
1127 		if (strlen(alias) > MAX_TARGET_NAME) {
1128 			iscsi_tgt_node_destruct(target, NULL, NULL);
1129 			return NULL;
1130 		}
1131 		memcpy(target->alias, alias, strlen(alias));
1132 	}
1133 
1134 	target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns,
1135 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
1136 	if (!target->dev) {
1137 		SPDK_ERRLOG("Could not construct SCSI device\n");
1138 		iscsi_tgt_node_destruct(target, NULL, NULL);
1139 		return NULL;
1140 	}
1141 
1142 	TAILQ_INIT(&target->pg_map_head);
1143 	rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list,
1144 					      ig_tag_list, num_maps);
1145 	if (rc != 0) {
1146 		SPDK_ERRLOG("could not add map to target\n");
1147 		iscsi_tgt_node_destruct(target, NULL, NULL);
1148 		return NULL;
1149 	}
1150 
1151 	target->disable_chap = disable_chap;
1152 	target->require_chap = require_chap;
1153 	target->mutual_chap = mutual_chap;
1154 	target->chap_group = chap_group;
1155 	target->header_digest = header_digest;
1156 	target->data_digest = data_digest;
1157 
1158 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) {
1159 		target->queue_depth = queue_depth;
1160 	} else {
1161 		SPDK_DEBUGLOG(iscsi, "QueueDepth %d is invalid and %d is used instead.\n",
1162 			      queue_depth, g_iscsi.MaxQueueDepth);
1163 		target->queue_depth = g_iscsi.MaxQueueDepth;
1164 	}
1165 
1166 	rc = iscsi_tgt_node_register(target);
1167 	if (rc != 0) {
1168 		SPDK_ERRLOG("register target is failed\n");
1169 		iscsi_tgt_node_destruct(target, NULL, NULL);
1170 		return NULL;
1171 	}
1172 
1173 	return target;
1174 }
1175 
1176 void
1177 iscsi_shutdown_tgt_nodes(void)
1178 {
1179 	struct spdk_iscsi_tgt_node *target;
1180 
1181 	pthread_mutex_lock(&g_iscsi.mutex);
1182 	while (!TAILQ_EMPTY(&g_iscsi.target_head)) {
1183 		target = TAILQ_FIRST(&g_iscsi.target_head);
1184 		TAILQ_REMOVE(&g_iscsi.target_head, target, tailq);
1185 
1186 		pthread_mutex_unlock(&g_iscsi.mutex);
1187 
1188 		iscsi_tgt_node_destruct(target, NULL, NULL);
1189 
1190 		pthread_mutex_lock(&g_iscsi.mutex);
1191 	}
1192 	pthread_mutex_unlock(&g_iscsi.mutex);
1193 }
1194 
1195 void
1196 iscsi_shutdown_tgt_node_by_name(const char *target_name,
1197 				iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg)
1198 {
1199 	struct spdk_iscsi_tgt_node *target;
1200 
1201 	pthread_mutex_lock(&g_iscsi.mutex);
1202 	target = iscsi_find_tgt_node(target_name);
1203 	if (target != NULL) {
1204 		iscsi_tgt_node_unregister(target);
1205 		pthread_mutex_unlock(&g_iscsi.mutex);
1206 
1207 		iscsi_tgt_node_destruct(target, cb_fn, cb_arg);
1208 
1209 		return;
1210 	}
1211 	pthread_mutex_unlock(&g_iscsi.mutex);
1212 
1213 	if (cb_fn) {
1214 		cb_fn(cb_arg, -ENOENT);
1215 	}
1216 }
1217 
1218 bool
1219 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target)
1220 {
1221 	return target->destructed;
1222 }
1223 
1224 int
1225 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1226 			    struct spdk_iscsi_tgt_node *target)
1227 {
1228 	int i;
1229 	struct spdk_iscsi_task *task;
1230 
1231 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1232 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1233 
1234 		if (!lun) {
1235 			continue;
1236 		}
1237 
1238 		/* we create a fake management task per LUN to cleanup */
1239 		task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl);
1240 		if (!task) {
1241 			SPDK_ERRLOG("Unable to acquire task\n");
1242 			return -1;
1243 		}
1244 
1245 		task->scsi.target_port = conn->target_port;
1246 		task->scsi.initiator_port = conn->initiator_port;
1247 		task->scsi.lun = lun;
1248 
1249 		iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1250 	}
1251 
1252 	return 0;
1253 }
1254 
1255 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1256 			       struct spdk_iscsi_init_grp *initiator_group)
1257 {
1258 	struct spdk_iscsi_tgt_node *target;
1259 
1260 	pthread_mutex_lock(&g_iscsi.mutex);
1261 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1262 		if (portal_group) {
1263 			iscsi_tgt_node_delete_pg_map(target, portal_group);
1264 		}
1265 		if (initiator_group) {
1266 			iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1267 		}
1268 	}
1269 	pthread_mutex_unlock(&g_iscsi.mutex);
1270 }
1271 
1272 int
1273 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target,
1274 		       const char *bdev_name, int lun_id)
1275 {
1276 	struct spdk_scsi_dev *dev;
1277 	int rc;
1278 
1279 	if (target->num_active_conns > 0) {
1280 		SPDK_ERRLOG("Target has active connections (count=%d)\n",
1281 			    target->num_active_conns);
1282 		return -1;
1283 	}
1284 
1285 	if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
1286 		SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id);
1287 		return -1;
1288 	}
1289 
1290 	dev = target->dev;
1291 	if (dev == NULL) {
1292 		SPDK_ERRLOG("SCSI device is not found\n");
1293 		return -1;
1294 	}
1295 
1296 	rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL);
1297 	if (rc != 0) {
1298 		SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n");
1299 		return -1;
1300 	}
1301 
1302 	return 0;
1303 }
1304 
1305 int
1306 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target,
1307 			       bool disable_chap, bool require_chap,
1308 			       bool mutual_chap, int32_t chap_group)
1309 {
1310 	if (!iscsi_check_chap_params(disable_chap, require_chap,
1311 				     mutual_chap, chap_group)) {
1312 		return -EINVAL;
1313 	}
1314 
1315 	pthread_mutex_lock(&target->mutex);
1316 	target->disable_chap = disable_chap;
1317 	target->require_chap = require_chap;
1318 	target->mutual_chap = mutual_chap;
1319 	target->chap_group = chap_group;
1320 	pthread_mutex_unlock(&target->mutex);
1321 
1322 	return 0;
1323 }
1324 
1325 static void
1326 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target,
1327 			 struct spdk_json_write_ctx *w)
1328 {
1329 	struct spdk_iscsi_pg_map *pg_map;
1330 	struct spdk_iscsi_ig_map *ig_map;
1331 	int i;
1332 
1333 	spdk_json_write_object_begin(w);
1334 
1335 	spdk_json_write_named_string(w, "name", target->name);
1336 
1337 	if (target->alias[0] != '\0') {
1338 		spdk_json_write_named_string(w, "alias_name", target->alias);
1339 	}
1340 
1341 	spdk_json_write_named_array_begin(w, "pg_ig_maps");
1342 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
1343 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
1344 			spdk_json_write_object_begin(w);
1345 			spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag);
1346 			spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag);
1347 			spdk_json_write_object_end(w);
1348 		}
1349 	}
1350 	spdk_json_write_array_end(w);
1351 
1352 	spdk_json_write_named_array_begin(w, "luns");
1353 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1354 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1355 
1356 		if (lun) {
1357 			spdk_json_write_object_begin(w);
1358 			spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun));
1359 			spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun));
1360 			spdk_json_write_object_end(w);
1361 		}
1362 	}
1363 	spdk_json_write_array_end(w);
1364 
1365 	spdk_json_write_named_int32(w, "queue_depth", target->queue_depth);
1366 
1367 	spdk_json_write_named_bool(w, "disable_chap", target->disable_chap);
1368 	spdk_json_write_named_bool(w, "require_chap", target->require_chap);
1369 	spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap);
1370 	spdk_json_write_named_int32(w, "chap_group", target->chap_group);
1371 
1372 	spdk_json_write_named_bool(w, "header_digest", target->header_digest);
1373 	spdk_json_write_named_bool(w, "data_digest", target->data_digest);
1374 
1375 	spdk_json_write_object_end(w);
1376 }
1377 
1378 static void
1379 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target,
1380 			   struct spdk_json_write_ctx *w)
1381 {
1382 	spdk_json_write_object_begin(w);
1383 
1384 	spdk_json_write_named_string(w, "method", "iscsi_create_target_node");
1385 
1386 	spdk_json_write_name(w, "params");
1387 	iscsi_tgt_node_info_json(target, w);
1388 
1389 	spdk_json_write_object_end(w);
1390 }
1391 
1392 void
1393 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w)
1394 {
1395 	struct spdk_iscsi_tgt_node *target;
1396 
1397 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1398 		iscsi_tgt_node_info_json(target, w);
1399 	}
1400 }
1401 
1402 void
1403 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w)
1404 {
1405 	struct spdk_iscsi_tgt_node *target;
1406 
1407 	TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) {
1408 		iscsi_tgt_node_config_json(target, w);
1409 	}
1410 }
1411