xref: /spdk/lib/iscsi/tgt_node.c (revision 59970a89be936e68272f889ea856bd60c987cc39)
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/env.h"
38 #include "spdk/event.h"
39 #include "spdk/conf.h"
40 #include "spdk/net.h"
41 
42 #include "spdk_internal/log.h"
43 
44 #include "iscsi/iscsi.h"
45 #include "iscsi/conn.h"
46 #include "iscsi/tgt_node.h"
47 #include "iscsi/portal_grp.h"
48 #include "iscsi/init_grp.h"
49 #include "spdk/scsi.h"
50 #include "iscsi/task.h"
51 
52 #define MAX_TMPBUF 1024
53 #define MAX_MASKBUF 128
54 
55 static bool
56 spdk_iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr)
57 {
58 	struct in6_addr in6_mask;
59 	struct in6_addr in6_addr;
60 	char mask[MAX_MASKBUF];
61 	const char *p;
62 	size_t n;
63 	int bits, bmask;
64 	int i;
65 
66 	if (netmask[0] != '[') {
67 		return false;
68 	}
69 	p = strchr(netmask, ']');
70 	if (p == NULL) {
71 		return false;
72 	}
73 	n = p - (netmask + 1);
74 	if (n + 1 > sizeof mask) {
75 		return false;
76 	}
77 
78 	memcpy(mask, netmask + 1, n);
79 	mask[n] = '\0';
80 	p++;
81 
82 	if (p[0] == '/') {
83 		bits = (int) strtol(p + 1, NULL, 10);
84 		if (bits < 0 || bits > 128) {
85 			return false;
86 		}
87 	} else {
88 		bits = 128;
89 	}
90 
91 #if 0
92 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "input %s\n", addr);
93 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "mask  %s / %d\n", mask, bits);
94 #endif
95 
96 	/* presentation to network order binary */
97 	if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
98 	    || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
99 		return false;
100 	}
101 
102 	/* check 128bits */
103 	for (i = 0; i < (bits / 8); i++) {
104 		if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) {
105 			return false;
106 		}
107 	}
108 	if (bits % 8) {
109 		bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
110 		if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) {
111 			return false;
112 		}
113 	}
114 
115 	/* match */
116 	return true;
117 }
118 
119 static bool
120 spdk_iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr)
121 {
122 	struct in_addr in4_mask;
123 	struct in_addr in4_addr;
124 	char mask[MAX_MASKBUF];
125 	const char *p;
126 	uint32_t bmask;
127 	size_t n;
128 	int bits;
129 
130 	p = strchr(netmask, '/');
131 	if (p == NULL) {
132 		p = netmask + strlen(netmask);
133 	}
134 	n = p - netmask;
135 	if (n + 1 > sizeof mask) {
136 		return false;
137 	}
138 
139 	memcpy(mask, netmask, n);
140 	mask[n] = '\0';
141 
142 	if (p[0] == '/') {
143 		bits = (int) strtol(p + 1, NULL, 10);
144 		if (bits < 0 || bits > 32) {
145 			return false;
146 		}
147 	} else {
148 		bits = 32;
149 	}
150 
151 	/* presentation to network order binary */
152 	if (inet_pton(AF_INET, mask, &in4_mask) <= 0
153 	    || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
154 		return false;
155 	}
156 
157 	/* check 32bits */
158 	bmask = (0xffffffffULL << (32 - bits)) & 0xffffffffU;
159 	if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) {
160 		return false;
161 	}
162 
163 	/* match */
164 	return true;
165 }
166 
167 static bool
168 spdk_iscsi_netmask_allow_addr(const char *netmask, const char *addr)
169 {
170 	if (netmask == NULL || addr == NULL) {
171 		return false;
172 	}
173 	if (strcasecmp(netmask, "ALL") == 0) {
174 		return true;
175 	}
176 	if (netmask[0] == '[') {
177 		/* IPv6 */
178 		if (spdk_iscsi_ipv6_netmask_allow_addr(netmask, addr)) {
179 			return true;
180 		}
181 	} else {
182 		/* IPv4 */
183 		if (spdk_iscsi_ipv4_netmask_allow_addr(netmask, addr)) {
184 			return true;
185 		}
186 	}
187 	return false;
188 }
189 
190 static bool
191 spdk_iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp,
192 			       const char *addr)
193 {
194 	struct spdk_iscsi_initiator_netmask *imask;
195 
196 	TAILQ_FOREACH(imask, &igp->netmask_head, tailq) {
197 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "netmask=%s, addr=%s\n",
198 			      imask->mask, addr);
199 		if (spdk_iscsi_netmask_allow_addr(imask->mask, addr)) {
200 			return true;
201 		}
202 	}
203 	return false;
204 }
205 
206 static int
207 spdk_iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp,
208 				     const char *iqn, bool *result)
209 {
210 	struct spdk_iscsi_initiator_name *iname;
211 
212 	TAILQ_FOREACH(iname, &igp->initiator_head, tailq) {
213 		/* denied if iqn is matched */
214 		if ((iname->name[0] == '!')
215 		    && (strcasecmp(&iname->name[1], "ANY") == 0
216 			|| strcasecmp(&iname->name[1], iqn) == 0)) {
217 			*result = false;
218 			return 0;
219 		}
220 		/* allowed if iqn is matched */
221 		if (strcasecmp(iname->name, "ANY") == 0
222 		    || strcasecmp(iname->name, iqn) == 0) {
223 			*result = true;
224 			return 0;
225 		}
226 	}
227 	return -1;
228 }
229 
230 static struct spdk_iscsi_pg_map *
231 spdk_iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
232 				struct spdk_iscsi_portal_grp *pg);
233 
234 bool
235 spdk_iscsi_tgt_node_access(struct spdk_iscsi_conn *conn,
236 			   struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr)
237 {
238 	struct spdk_iscsi_portal_grp *pg;
239 	struct spdk_iscsi_pg_map *pg_map;
240 	struct spdk_iscsi_ig_map *ig_map;
241 	int rc;
242 	bool allowed = false;
243 
244 	if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) {
245 		return false;
246 	}
247 	pg = conn->portal->group;
248 
249 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "pg=%d, iqn=%s, addr=%s\n",
250 		      pg->tag, iqn, addr);
251 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
252 	if (pg_map == NULL) {
253 		return false;
254 	}
255 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
256 		rc = spdk_iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed);
257 		if (rc == 0) {
258 			if (allowed == false) {
259 				goto denied;
260 			} else {
261 				if (spdk_iscsi_init_grp_allow_addr(ig_map->ig, addr)) {
262 					return true;
263 				}
264 			}
265 		} else {
266 			/* netmask is denied in this initiator group */
267 		}
268 	}
269 
270 denied:
271 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "access denied from %s (%s) to %s (%s:%s,%d)\n",
272 		      iqn, addr, target->name, conn->portal->host,
273 		      conn->portal->port, conn->portal->group->tag);
274 	return false;
275 }
276 
277 static bool
278 spdk_iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn)
279 {
280 	struct spdk_iscsi_pg_map *pg_map;
281 	struct spdk_iscsi_ig_map *ig_map;
282 	int rc;
283 	bool result = false;
284 
285 	if (target == NULL || iqn == NULL) {
286 		return false;
287 	}
288 
289 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
290 		TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
291 			rc = spdk_iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result);
292 			if (rc == 0) {
293 				return result;
294 			}
295 		}
296 	}
297 
298 	return false;
299 }
300 
301 int
302 spdk_iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn,
303 		     const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len,
304 		     int data_len)
305 {
306 	char buf[MAX_TMPBUF];
307 	struct spdk_iscsi_portal_grp	*pg;
308 	struct spdk_iscsi_pg_map	*pg_map;
309 	struct spdk_iscsi_portal	*p;
310 	struct spdk_iscsi_tgt_node	*target;
311 	char *host;
312 	int total;
313 	int len;
314 	int rc;
315 
316 	if (conn == NULL) {
317 		return 0;
318 	}
319 
320 	total = data_len;
321 	if (alloc_len < 1) {
322 		return 0;
323 	}
324 	if (total > alloc_len) {
325 		total = alloc_len;
326 		data[total - 1] = '\0';
327 		return total;
328 	}
329 
330 	if (alloc_len - total < 1) {
331 		SPDK_ERRLOG("data space small %d\n", alloc_len);
332 		return total;
333 	}
334 
335 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
336 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
337 		if (strcasecmp(tiqn, "ALL") != 0
338 		    && strcasecmp(tiqn, target->name) != 0) {
339 			continue;
340 		}
341 		rc = spdk_iscsi_tgt_node_allow_iscsi_name(target, iiqn);
342 		if (rc == 0) {
343 			continue;
344 		}
345 
346 		/* DO SENDTARGETS */
347 		len = snprintf((char *) data + total, alloc_len - total,
348 			       "TargetName=%s", target->name);
349 		total += len + 1;
350 
351 		/* write to data */
352 		TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
353 			pg = pg_map->pg;
354 			TAILQ_FOREACH(p, &pg->head, per_pg_tailq) {
355 				if (alloc_len - total < 1) {
356 					pthread_mutex_unlock(&g_spdk_iscsi.mutex);
357 					SPDK_ERRLOG("data space small %d\n", alloc_len);
358 					return total;
359 				}
360 				host = p->host;
361 				/* wildcard? */
362 				if (strcasecmp(host, "[::]") == 0
363 				    || strcasecmp(host, "0.0.0.0") == 0) {
364 					if (spdk_sock_is_ipv6(conn->sock)) {
365 						snprintf(buf, sizeof buf, "[%s]",
366 							 conn->target_addr);
367 						host = buf;
368 					} else if (spdk_sock_is_ipv4(conn->sock)) {
369 						snprintf(buf, sizeof buf, "%s",
370 							 conn->target_addr);
371 						host = buf;
372 					} else {
373 						/* skip portal for the family */
374 						continue;
375 					}
376 				}
377 				SPDK_DEBUGLOG(SPDK_LOG_ISCSI,
378 					      "TargetAddress=%s:%s,%d\n",
379 					      host, p->port, pg->tag);
380 				len = snprintf((char *) data + total,
381 					       alloc_len - total,
382 					       "TargetAddress=%s:%s,%d",
383 					       host, p->port, pg->tag);
384 				total += len + 1;
385 			}
386 		}
387 	}
388 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
389 
390 	return total;
391 }
392 
393 static void
394 spdk_iscsi_tgt_node_list_init(void)
395 {
396 	g_spdk_iscsi.ntargets = 0;
397 	TAILQ_INIT(&g_spdk_iscsi.target_head);
398 }
399 
400 struct spdk_iscsi_tgt_node *
401 spdk_iscsi_find_tgt_node(const char *target_name)
402 {
403 	struct spdk_iscsi_tgt_node *target;
404 
405 	if (target_name == NULL) {
406 		return NULL;
407 	}
408 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
409 		if (strcasecmp(target_name, target->name) == 0) {
410 			return target;
411 		}
412 	}
413 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name);
414 	return NULL;
415 }
416 
417 static int
418 spdk_iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target)
419 {
420 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
421 
422 	if (spdk_iscsi_find_tgt_node(target->name) != NULL) {
423 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
424 		return -EEXIST;
425 	}
426 
427 	TAILQ_INSERT_TAIL(&g_spdk_iscsi.target_head, target, tailq);
428 	g_spdk_iscsi.ntargets++;
429 
430 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
431 	return 0;
432 }
433 
434 static int
435 spdk_iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target)
436 {
437 	struct spdk_iscsi_tgt_node *t;
438 
439 	TAILQ_FOREACH(t, &g_spdk_iscsi.target_head, tailq) {
440 		if (t == target) {
441 			TAILQ_REMOVE(&g_spdk_iscsi.target_head, t, tailq);
442 			g_spdk_iscsi.ntargets--;
443 			return 0;
444 		}
445 	}
446 
447 	return -1;
448 }
449 
450 static struct spdk_iscsi_ig_map *
451 spdk_iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map,
452 			      struct spdk_iscsi_init_grp *ig)
453 {
454 	struct spdk_iscsi_ig_map *ig_map;
455 
456 	TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) {
457 		if (ig_map->ig == ig) {
458 			return ig_map;
459 		}
460 	}
461 
462 	return NULL;
463 }
464 
465 static struct spdk_iscsi_ig_map *
466 spdk_iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map,
467 			     struct spdk_iscsi_init_grp *ig)
468 {
469 	struct spdk_iscsi_ig_map *ig_map;
470 
471 	if (spdk_iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) {
472 		return NULL;
473 	}
474 
475 	ig_map = malloc(sizeof(*ig_map));
476 	if (ig_map == NULL) {
477 		return NULL;
478 	}
479 
480 	ig_map->ig = ig;
481 	ig->ref++;
482 	pg_map->num_ig_maps++;
483 	TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq);
484 
485 	return ig_map;
486 }
487 
488 static void
489 _spdk_iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
490 				 struct spdk_iscsi_ig_map *ig_map)
491 {
492 	TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq);
493 	pg_map->num_ig_maps--;
494 	ig_map->ig->ref--;
495 	free(ig_map);
496 }
497 
498 static int
499 spdk_iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map,
500 				struct spdk_iscsi_init_grp *ig)
501 {
502 	struct spdk_iscsi_ig_map *ig_map;
503 
504 	ig_map = spdk_iscsi_pg_map_find_ig_map(pg_map, ig);
505 	if (ig_map == NULL) {
506 		return -ENOENT;
507 	}
508 
509 	_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
510 	return 0;
511 }
512 
513 static void
514 spdk_iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map)
515 {
516 	struct spdk_iscsi_ig_map *ig_map, *tmp;
517 
518 	TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) {
519 		_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
520 	}
521 }
522 
523 static struct spdk_iscsi_pg_map *
524 spdk_iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target,
525 				struct spdk_iscsi_portal_grp *pg)
526 {
527 	struct spdk_iscsi_pg_map *pg_map;
528 
529 	TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) {
530 		if (pg_map->pg == pg) {
531 			return pg_map;
532 		}
533 	}
534 
535 	return NULL;
536 }
537 
538 static struct spdk_iscsi_pg_map *
539 spdk_iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target,
540 			       struct spdk_iscsi_portal_grp *pg)
541 {
542 	struct spdk_iscsi_pg_map *pg_map;
543 	char port_name[MAX_TMPBUF];
544 	int rc;
545 
546 	if (spdk_iscsi_tgt_node_find_pg_map(target, pg) != NULL) {
547 		return NULL;
548 	}
549 
550 	if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) {
551 		SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n",
552 			    SPDK_SCSI_DEV_MAX_PORTS);
553 		return NULL;
554 	}
555 
556 	pg_map = malloc(sizeof(*pg_map));
557 	if (pg_map == NULL) {
558 		return NULL;
559 	}
560 
561 	snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x",
562 		 spdk_scsi_dev_get_name(target->dev), pg->tag);
563 	rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name);
564 	if (rc != 0) {
565 		free(pg_map);
566 		return NULL;
567 	}
568 
569 	TAILQ_INIT(&pg_map->ig_map_head);
570 	pg_map->num_ig_maps = 0;
571 	pg->ref++;
572 	pg_map->pg = pg;
573 	target->num_pg_maps++;
574 	TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq);
575 
576 	return pg_map;
577 }
578 
579 static void
580 _spdk_iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
581 				   struct spdk_iscsi_pg_map *pg_map)
582 {
583 	TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq);
584 	target->num_pg_maps--;
585 	pg_map->pg->ref--;
586 
587 	spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag);
588 
589 	free(pg_map);
590 }
591 
592 static int
593 spdk_iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target,
594 				  struct spdk_iscsi_portal_grp *pg)
595 {
596 	struct spdk_iscsi_pg_map *pg_map;
597 
598 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
599 	if (pg_map == NULL) {
600 		return -ENOENT;
601 	}
602 
603 	if (pg_map->num_ig_maps > 0) {
604 		return -ENOTEMPTY;
605 	}
606 
607 	_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
608 	return 0;
609 }
610 
611 static void
612 spdk_iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target,
613 				   struct spdk_iscsi_init_grp *ig)
614 {
615 	struct spdk_iscsi_pg_map *pg_map, *tmp;
616 
617 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
618 		spdk_iscsi_pg_map_delete_ig_map(pg_map, ig);
619 		if (pg_map->num_ig_maps == 0) {
620 			_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
621 		}
622 	}
623 }
624 
625 static void
626 spdk_iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target)
627 {
628 	struct spdk_iscsi_pg_map *pg_map, *tmp;
629 
630 	TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) {
631 		spdk_iscsi_pg_map_delete_all_ig_maps(pg_map);
632 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
633 	}
634 }
635 
636 static void
637 spdk_iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target)
638 {
639 	if (target == NULL) {
640 		return;
641 	}
642 
643 	free(target->name);
644 	free(target->alias);
645 	spdk_iscsi_tgt_node_delete_all_pg_maps(target);
646 	spdk_scsi_dev_destruct(target->dev);
647 
648 	pthread_mutex_destroy(&target->mutex);
649 	free(target);
650 }
651 
652 static int
653 spdk_iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target,
654 				     int pg_tag, int ig_tag)
655 {
656 	struct spdk_iscsi_portal_grp	*pg;
657 	struct spdk_iscsi_init_grp	*ig;
658 	struct spdk_iscsi_pg_map	*pg_map;
659 	struct spdk_iscsi_ig_map	*ig_map;
660 
661 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
662 	if (pg == NULL) {
663 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
664 		return -ENOENT;
665 	}
666 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
667 	if (ig == NULL) {
668 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
669 		return -ENOENT;
670 	}
671 
672 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
673 	if (pg_map == NULL) {
674 		SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag);
675 		return -ENOENT;
676 	}
677 	ig_map = spdk_iscsi_pg_map_find_ig_map(pg_map, ig);
678 	if (ig_map == NULL) {
679 		SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag);
680 		return -ENOENT;
681 	}
682 
683 	_spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map);
684 	if (pg_map->num_ig_maps == 0) {
685 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
686 	}
687 
688 	return 0;
689 }
690 
691 static int
692 spdk_iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target,
693 				  int pg_tag, int ig_tag)
694 {
695 	struct spdk_iscsi_portal_grp	*pg;
696 	struct spdk_iscsi_pg_map 	*pg_map;
697 	struct spdk_iscsi_init_grp	*ig;
698 	struct spdk_iscsi_ig_map 	*ig_map;
699 	bool				new_pg_map = false;
700 
701 	pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag);
702 	if (pg == NULL) {
703 		SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag);
704 		return -ENOENT;
705 	}
706 	ig = spdk_iscsi_init_grp_find_by_tag(ig_tag);
707 	if (ig == NULL) {
708 		SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag);
709 		return -ENOENT;
710 	}
711 
712 	/* get existing pg_map or create new pg_map and add it to target */
713 	pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg);
714 	if (pg_map == NULL) {
715 		pg_map = spdk_iscsi_tgt_node_add_pg_map(target, pg);
716 		if (pg_map == NULL) {
717 			goto failed;
718 		}
719 		new_pg_map = true;
720 	}
721 
722 	/* create new ig_map and add it to pg_map */
723 	ig_map = spdk_iscsi_pg_map_add_ig_map(pg_map, ig);
724 	if (ig_map == NULL) {
725 		goto failed;
726 	}
727 
728 	return 0;
729 
730 failed:
731 	if (new_pg_map) {
732 		_spdk_iscsi_tgt_node_delete_pg_map(target, pg_map);
733 	}
734 
735 	return -1;
736 }
737 
738 int
739 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
740 				   int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
741 {
742 	uint16_t i;
743 	int rc;
744 
745 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
746 	for (i = 0; i < num_maps; i++) {
747 		rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i],
748 						       ig_tag_list[i]);
749 		if (rc != 0) {
750 			SPDK_ERRLOG("could not add map to target\n");
751 			goto invalid;
752 		}
753 	}
754 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
755 	return 0;
756 
757 invalid:
758 	for (; i > 0; --i) {
759 		spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1],
760 						     ig_tag_list[i - 1]);
761 	}
762 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
763 	return -1;
764 }
765 
766 int
767 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target,
768 				      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps)
769 {
770 	uint16_t i;
771 	int rc;
772 
773 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
774 	for (i = 0; i < num_maps; i++) {
775 		rc = spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i],
776 				ig_tag_list[i]);
777 		if (rc != 0) {
778 			SPDK_ERRLOG("could not delete map from target\n");
779 			goto invalid;
780 		}
781 	}
782 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
783 	return 0;
784 
785 invalid:
786 	for (; i > 0; --i) {
787 		rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1],
788 						       ig_tag_list[i - 1]);
789 		if (rc != 0) {
790 			spdk_iscsi_tgt_node_delete_all_pg_maps(target);
791 			break;
792 		}
793 	}
794 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
795 	return -1;
796 }
797 
798 static int
799 spdk_check_iscsi_name(const char *name)
800 {
801 	const unsigned char *up = (const unsigned char *) name;
802 	size_t n;
803 
804 	/* valid iSCSI name? */
805 	for (n = 0; up[n] != 0; n++) {
806 		if (up[n] > 0x00U && up[n] <= 0x2cU) {
807 			return -1;
808 		}
809 		if (up[n] == 0x2fU) {
810 			return -1;
811 		}
812 		if (up[n] >= 0x3bU && up[n] <= 0x40U) {
813 			return -1;
814 		}
815 		if (up[n] >= 0x5bU && up[n] <= 0x60U) {
816 			return -1;
817 		}
818 		if (up[n] >= 0x7bU && up[n] <= 0x7fU) {
819 			return -1;
820 		}
821 		if (isspace(up[n])) {
822 			return -1;
823 		}
824 	}
825 	/* valid format? */
826 	if (strncasecmp(name, "iqn.", 4) == 0) {
827 		/* iqn.YYYY-MM.reversed.domain.name */
828 		if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
829 		    || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
830 		    || !isdigit(up[10]) || up[11] != '.') {
831 			SPDK_ERRLOG("invalid iqn format. "
832 				    "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
833 			return -1;
834 		}
835 	} else if (strncasecmp(name, "eui.", 4) == 0) {
836 		/* EUI-64 -> 16bytes */
837 		/* XXX */
838 	} else if (strncasecmp(name, "naa.", 4) == 0) {
839 		/* 64bit -> 16bytes, 128bit -> 32bytes */
840 		/* XXX */
841 	}
842 	/* OK */
843 	return 0;
844 }
845 
846 _spdk_iscsi_tgt_node *
847 spdk_iscsi_tgt_node_construct(int target_index,
848 			      const char *name, const char *alias,
849 			      int *pg_tag_list, int *ig_tag_list, uint16_t num_maps,
850 			      char *lun_name_list[], int *lun_id_list, int num_luns,
851 			      int queue_depth,
852 			      int auth_chap_disabled, int auth_chap_required, int auth_chap_mutual, int auth_group,
853 			      int header_digest, int data_digest)
854 {
855 	char				fullname[MAX_TMPBUF];
856 	struct spdk_iscsi_tgt_node	*target;
857 	int				rc;
858 
859 	if (auth_chap_disabled && auth_chap_required) {
860 		SPDK_ERRLOG("auth_chap_disabled and auth_chap_required are mutually exclusive\n");
861 		return NULL;
862 	}
863 
864 	if (num_maps == 0) {
865 		SPDK_ERRLOG("num_maps = 0\n");
866 		return NULL;
867 	}
868 
869 	if (name == NULL) {
870 		SPDK_ERRLOG("TargetName not found\n");
871 		return NULL;
872 	}
873 
874 	if (strncasecmp(name, "iqn.", 4) != 0
875 	    && strncasecmp(name, "eui.", 4) != 0
876 	    && strncasecmp(name, "naa.", 4) != 0) {
877 		snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name);
878 	} else {
879 		snprintf(fullname, sizeof(fullname), "%s", name);
880 	}
881 
882 	if (spdk_check_iscsi_name(fullname) != 0) {
883 		SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n",
884 			    name);
885 		return NULL;
886 	}
887 
888 	target = malloc(sizeof(*target));
889 	if (!target) {
890 		SPDK_ERRLOG("could not allocate target\n");
891 		return NULL;
892 	}
893 
894 	memset(target, 0, sizeof(*target));
895 
896 	rc = pthread_mutex_init(&target->mutex, NULL);
897 	if (rc != 0) {
898 		SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num);
899 		spdk_iscsi_tgt_node_destruct(target);
900 		return NULL;
901 	}
902 
903 	target->num = target_index;
904 
905 	target->name = strdup(fullname);
906 	if (!target->name) {
907 		SPDK_ERRLOG("Could not allocate TargetName\n");
908 		spdk_iscsi_tgt_node_destruct(target);
909 		return NULL;
910 	}
911 
912 	if (alias == NULL) {
913 		target->alias = NULL;
914 	} else {
915 		target->alias = strdup(alias);
916 		if (!target->alias) {
917 			SPDK_ERRLOG("Could not allocate TargetAlias\n");
918 			spdk_iscsi_tgt_node_destruct(target);
919 			return NULL;
920 		}
921 	}
922 
923 	target->dev = spdk_scsi_dev_construct(name, lun_name_list, lun_id_list, num_luns,
924 					      SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL);
925 
926 	if (!target->dev) {
927 		SPDK_ERRLOG("Could not construct SCSI device\n");
928 		spdk_iscsi_tgt_node_destruct(target);
929 		return NULL;
930 	}
931 
932 	TAILQ_INIT(&target->pg_map_head);
933 	rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps);
934 	if (rc != 0) {
935 		SPDK_ERRLOG("could not add map to target\n");
936 		spdk_iscsi_tgt_node_destruct(target);
937 		return NULL;
938 	}
939 
940 	target->auth_chap_disabled = auth_chap_disabled;
941 	target->auth_chap_required = auth_chap_required;
942 	target->auth_chap_mutual = auth_chap_mutual;
943 	target->auth_group = auth_group;
944 	target->header_digest = header_digest;
945 	target->data_digest = data_digest;
946 
947 	if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) {
948 		target->queue_depth = queue_depth;
949 	} else {
950 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n",
951 			      queue_depth, g_spdk_iscsi.MaxQueueDepth);
952 		target->queue_depth = g_spdk_iscsi.MaxQueueDepth;
953 	}
954 
955 	rc = spdk_iscsi_tgt_node_register(target);
956 	if (rc != 0) {
957 		SPDK_ERRLOG("register target is failed\n");
958 		spdk_iscsi_tgt_node_destruct(target);
959 		return NULL;
960 	}
961 
962 	return target;
963 }
964 
965 static int
966 spdk_cf_add_iscsi_tgt_node(struct spdk_conf_section *sp)
967 {
968 	char buf[MAX_TMPBUF];
969 	struct spdk_iscsi_tgt_node *target;
970 	int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP];
971 	int num_target_maps;
972 	const char *alias, *pg_tag, *ig_tag;
973 	const char *ag_tag;
974 	const char *val, *name;
975 	int target_num, auth_group, pg_tag_i, ig_tag_i;
976 	int header_digest, data_digest;
977 	int auth_chap_disabled, auth_chap_required, auth_chap_mutual;
978 	int i;
979 	int lun_id_list[SPDK_SCSI_DEV_MAX_LUN];
980 	char lun_name_array[SPDK_SCSI_DEV_MAX_LUN][SPDK_SCSI_LUN_MAX_NAME_LENGTH] = {};
981 	char *lun_name_list[SPDK_SCSI_DEV_MAX_LUN];
982 	int num_luns, queue_depth;
983 
984 	target_num = spdk_conf_section_get_num(sp);
985 
986 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num);
987 
988 	data_digest = 0;
989 	header_digest = 0;
990 
991 	name = spdk_conf_section_get_val(sp, "TargetName");
992 
993 	if (name == NULL) {
994 		SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num);
995 		return -1;
996 	}
997 
998 	alias = spdk_conf_section_get_val(sp, "TargetAlias");
999 
1000 	/* Setup initiator and portal group mapping */
1001 	val = spdk_conf_section_get_val(sp, "Mapping");
1002 	if (val == NULL) {
1003 		/* no map */
1004 		SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num);
1005 		return -1;
1006 	}
1007 
1008 	for (i = 0; i < MAX_TARGET_MAP; i++) {
1009 		val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1010 		if (val == NULL) {
1011 			break;
1012 		}
1013 		pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0);
1014 		ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1);
1015 		if (pg_tag == NULL || ig_tag == NULL) {
1016 			SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num);
1017 			return -1;
1018 		}
1019 		if (strncasecmp(pg_tag, "PortalGroup",
1020 				strlen("PortalGroup")) != 0
1021 		    || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1022 			SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num);
1023 			return -1;
1024 		}
1025 		if (strncasecmp(ig_tag, "InitiatorGroup",
1026 				strlen("InitiatorGroup")) != 0
1027 		    || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1028 			SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num);
1029 			return -1;
1030 		}
1031 		if (pg_tag_i < 1 || ig_tag_i < 1) {
1032 			SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num);
1033 			return -1;
1034 		}
1035 		pg_tag_list[i] = pg_tag_i;
1036 		ig_tag_list[i] = ig_tag_i;
1037 	}
1038 
1039 	num_target_maps = i;
1040 
1041 	/* Setup AuthMethod */
1042 	val = spdk_conf_section_get_val(sp, "AuthMethod");
1043 	auth_chap_disabled = 0;
1044 	auth_chap_required = 0;
1045 	auth_chap_mutual = 0;
1046 	if (val != NULL) {
1047 		for (i = 0; ; i++) {
1048 			val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i);
1049 			if (val == NULL) {
1050 				break;
1051 			}
1052 			if (strcasecmp(val, "CHAP") == 0) {
1053 				auth_chap_required = 1;
1054 			} else if (strcasecmp(val, "Mutual") == 0) {
1055 				auth_chap_mutual = 1;
1056 			} else if (strcasecmp(val, "Auto") == 0) {
1057 				auth_chap_disabled = 0;
1058 				auth_chap_required = 0;
1059 				auth_chap_mutual = 0;
1060 			} else if (strcasecmp(val, "None") == 0) {
1061 				auth_chap_disabled = 1;
1062 				auth_chap_required = 0;
1063 				auth_chap_mutual = 0;
1064 			} else {
1065 				SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num);
1066 				return -1;
1067 			}
1068 		}
1069 		if (auth_chap_mutual && !auth_chap_required) {
1070 			SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num);
1071 			return -1;
1072 		}
1073 	}
1074 	if (auth_chap_disabled == 1) {
1075 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n");
1076 	} else if (auth_chap_required == 0) {
1077 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n");
1078 	} else {
1079 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n",
1080 			      auth_chap_mutual ? "Mutual" : "");
1081 	}
1082 
1083 	val = spdk_conf_section_get_val(sp, "AuthGroup");
1084 	if (val == NULL) {
1085 		auth_group = 0;
1086 	} else {
1087 		ag_tag = val;
1088 		if (strcasecmp(ag_tag, "None") == 0) {
1089 			auth_group = 0;
1090 		} else {
1091 			if (strncasecmp(ag_tag, "AuthGroup",
1092 					strlen("AuthGroup")) != 0
1093 			    || sscanf(ag_tag, "%*[^0-9]%d", &auth_group) != 1) {
1094 				SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num);
1095 				return -1;
1096 			}
1097 			if (auth_group == 0) {
1098 				SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num);
1099 				return -1;
1100 			}
1101 		}
1102 	}
1103 	if (auth_group == 0) {
1104 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n");
1105 	} else {
1106 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n",
1107 			      auth_group);
1108 	}
1109 
1110 	val = spdk_conf_section_get_val(sp, "UseDigest");
1111 	if (val != NULL) {
1112 		for (i = 0; ; i++) {
1113 			val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i);
1114 			if (val == NULL) {
1115 				break;
1116 			}
1117 			if (strcasecmp(val, "Header") == 0) {
1118 				header_digest = 1;
1119 			} else if (strcasecmp(val, "Data") == 0) {
1120 				data_digest = 1;
1121 			} else if (strcasecmp(val, "Auto") == 0) {
1122 				header_digest = 0;
1123 				data_digest = 0;
1124 			} else {
1125 				SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num);
1126 				return -1;
1127 			}
1128 		}
1129 	}
1130 	if (header_digest == 0 && data_digest == 0) {
1131 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n");
1132 	} else {
1133 		SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n",
1134 			      header_digest ? "Header" : "",
1135 			      data_digest ? "Data" : "");
1136 	}
1137 
1138 	val = spdk_conf_section_get_val(sp, "QueueDepth");
1139 	if (val == NULL) {
1140 		queue_depth = g_spdk_iscsi.MaxQueueDepth;
1141 	} else {
1142 		queue_depth = (int) strtol(val, NULL, 10);
1143 	}
1144 
1145 	num_luns = 0;
1146 
1147 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1148 		snprintf(buf, sizeof(buf), "LUN%d", i);
1149 		val = spdk_conf_section_get_val(sp, buf);
1150 		if (val == NULL) {
1151 			continue;
1152 		}
1153 
1154 		snprintf(lun_name_array[num_luns], SPDK_SCSI_LUN_MAX_NAME_LENGTH, "%s", val);
1155 		lun_name_list[num_luns] = lun_name_array[num_luns];
1156 		lun_id_list[num_luns] = i;
1157 		num_luns++;
1158 	}
1159 
1160 	if (num_luns == 0) {
1161 		SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name);
1162 		return -1;
1163 	}
1164 
1165 	target = spdk_iscsi_tgt_node_construct(target_num, name, alias,
1166 					       pg_tag_list, ig_tag_list, num_target_maps,
1167 					       lun_name_list, lun_id_list, num_luns, queue_depth,
1168 					       auth_chap_disabled, auth_chap_required,
1169 					       auth_chap_mutual, auth_group,
1170 					       header_digest, data_digest);
1171 
1172 	if (target == NULL) {
1173 		SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num);
1174 		return -1;
1175 	}
1176 
1177 	spdk_scsi_dev_print(target->dev);
1178 	return 0;
1179 }
1180 
1181 int spdk_iscsi_init_tgt_nodes(void)
1182 {
1183 	struct spdk_conf_section *sp;
1184 	int rc;
1185 
1186 	SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_init_tgt_nodes\n");
1187 
1188 	spdk_iscsi_tgt_node_list_init();
1189 
1190 	sp = spdk_conf_first_section(NULL);
1191 	while (sp != NULL) {
1192 		if (spdk_conf_section_match_prefix(sp, "TargetNode")) {
1193 			int tag = spdk_conf_section_get_num(sp);
1194 
1195 			if (tag > SPDK_TN_TAG_MAX) {
1196 				SPDK_ERRLOG("tag %d is invalid\n", tag);
1197 				return -1;
1198 			}
1199 			rc = spdk_cf_add_iscsi_tgt_node(sp);
1200 			if (rc < 0) {
1201 				SPDK_ERRLOG("spdk_cf_add_iscsi_tgt_node() failed\n");
1202 				return -1;
1203 			}
1204 		}
1205 		sp = spdk_conf_next_section(sp);
1206 	}
1207 	return 0;
1208 }
1209 
1210 void
1211 spdk_iscsi_shutdown_tgt_nodes(void)
1212 {
1213 	struct spdk_iscsi_tgt_node *target, *tmp;
1214 
1215 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1216 	TAILQ_FOREACH_SAFE(target, &g_spdk_iscsi.target_head, tailq, tmp) {
1217 		TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq);
1218 		g_spdk_iscsi.ntargets--;
1219 		spdk_iscsi_tgt_node_destruct(target);
1220 	}
1221 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1222 }
1223 
1224 int
1225 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name)
1226 {
1227 	struct spdk_iscsi_tgt_node *target;
1228 
1229 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1230 	target = spdk_iscsi_find_tgt_node(target_name);
1231 	if (target != NULL) {
1232 		spdk_iscsi_tgt_node_unregister(target);
1233 		spdk_iscsi_tgt_node_destruct(target);
1234 		pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1235 
1236 		return 0;
1237 	}
1238 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1239 
1240 	return -ENOENT;
1241 }
1242 
1243 int
1244 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn,
1245 				 struct spdk_iscsi_tgt_node *target)
1246 {
1247 	int i;
1248 	struct spdk_iscsi_task *task;
1249 
1250 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
1251 		struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i);
1252 
1253 		if (!lun) {
1254 			continue;
1255 		}
1256 
1257 		/* we create a fake management task per LUN to cleanup */
1258 		task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl);
1259 		if (!task) {
1260 			SPDK_ERRLOG("Unable to acquire task\n");
1261 			return -1;
1262 		}
1263 
1264 		task->scsi.target_port = conn->target_port;
1265 		task->scsi.initiator_port = conn->initiator_port;
1266 		task->scsi.lun = lun;
1267 
1268 		spdk_scsi_dev_queue_mgmt_task(target->dev, &task->scsi, SPDK_SCSI_TASK_FUNC_LUN_RESET);
1269 	}
1270 
1271 	return 0;
1272 }
1273 
1274 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group,
1275 				    struct spdk_iscsi_init_grp *initiator_group)
1276 {
1277 	struct spdk_iscsi_tgt_node *target;
1278 
1279 	pthread_mutex_lock(&g_spdk_iscsi.mutex);
1280 	TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) {
1281 		if (portal_group) {
1282 			spdk_iscsi_tgt_node_delete_pg_map(target, portal_group);
1283 		}
1284 		if (initiator_group) {
1285 			spdk_iscsi_tgt_node_delete_ig_maps(target, initiator_group);
1286 		}
1287 	}
1288 	pthread_mutex_unlock(&g_spdk_iscsi.mutex);
1289 }
1290