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/conf.h" 38 #include "spdk/sock.h" 39 #include "spdk/scsi.h" 40 41 #include "spdk_internal/log.h" 42 43 #include "iscsi/iscsi.h" 44 #include "iscsi/conn.h" 45 #include "iscsi/tgt_node.h" 46 #include "iscsi/portal_grp.h" 47 #include "iscsi/init_grp.h" 48 #include "iscsi/task.h" 49 50 #define MAX_TMPBUF 1024 51 #define MAX_MASKBUF 128 52 53 static bool 54 spdk_iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr) 55 { 56 struct in6_addr in6_mask; 57 struct in6_addr in6_addr; 58 char mask[MAX_MASKBUF]; 59 const char *p; 60 size_t n; 61 int bits, bmask; 62 int i; 63 64 if (netmask[0] != '[') { 65 return false; 66 } 67 p = strchr(netmask, ']'); 68 if (p == NULL) { 69 return false; 70 } 71 n = p - (netmask + 1); 72 if (n + 1 > sizeof mask) { 73 return false; 74 } 75 76 memcpy(mask, netmask + 1, n); 77 mask[n] = '\0'; 78 p++; 79 80 if (p[0] == '/') { 81 bits = (int) strtol(p + 1, NULL, 10); 82 if (bits <= 0 || bits > 128) { 83 return false; 84 } 85 } else { 86 bits = 128; 87 } 88 89 #if 0 90 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "input %s\n", addr); 91 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "mask %s / %d\n", mask, bits); 92 #endif 93 94 /* presentation to network order binary */ 95 if (inet_pton(AF_INET6, mask, &in6_mask) <= 0 96 || inet_pton(AF_INET6, addr, &in6_addr) <= 0) { 97 return false; 98 } 99 100 /* check 128bits */ 101 for (i = 0; i < (bits / 8); i++) { 102 if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) { 103 return false; 104 } 105 } 106 if (bits % 8) { 107 bmask = (0xffU << (8 - (bits % 8))) & 0xffU; 108 if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) { 109 return false; 110 } 111 } 112 113 /* match */ 114 return true; 115 } 116 117 static bool 118 spdk_iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr) 119 { 120 struct in_addr in4_mask; 121 struct in_addr in4_addr; 122 char mask[MAX_MASKBUF]; 123 const char *p; 124 uint32_t bmask; 125 size_t n; 126 int bits; 127 128 p = strchr(netmask, '/'); 129 if (p == NULL) { 130 p = netmask + strlen(netmask); 131 } 132 n = p - netmask; 133 if (n + 1 > sizeof mask) { 134 return false; 135 } 136 137 memcpy(mask, netmask, n); 138 mask[n] = '\0'; 139 140 if (p[0] == '/') { 141 bits = (int) strtol(p + 1, NULL, 10); 142 if (bits <= 0 || bits > 32) { 143 return false; 144 } 145 } else { 146 bits = 32; 147 } 148 149 /* presentation to network order binary */ 150 if (inet_pton(AF_INET, mask, &in4_mask) <= 0 151 || inet_pton(AF_INET, addr, &in4_addr) <= 0) { 152 return false; 153 } 154 155 /* check 32bits */ 156 bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU; 157 if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) { 158 return false; 159 } 160 161 /* match */ 162 return true; 163 } 164 165 static bool 166 spdk_iscsi_netmask_allow_addr(const char *netmask, const char *addr) 167 { 168 if (netmask == NULL || addr == NULL) { 169 return false; 170 } 171 if (strcasecmp(netmask, "ANY") == 0) { 172 return true; 173 } 174 if (netmask[0] == '[') { 175 /* IPv6 */ 176 if (spdk_iscsi_ipv6_netmask_allow_addr(netmask, addr)) { 177 return true; 178 } 179 } else { 180 /* IPv4 */ 181 if (spdk_iscsi_ipv4_netmask_allow_addr(netmask, addr)) { 182 return true; 183 } 184 } 185 return false; 186 } 187 188 static bool 189 spdk_iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp, 190 const char *addr) 191 { 192 struct spdk_iscsi_initiator_netmask *imask; 193 194 TAILQ_FOREACH(imask, &igp->netmask_head, tailq) { 195 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "netmask=%s, addr=%s\n", 196 imask->mask, addr); 197 if (spdk_iscsi_netmask_allow_addr(imask->mask, addr)) { 198 return true; 199 } 200 } 201 return false; 202 } 203 204 static int 205 spdk_iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp, 206 const char *iqn, bool *result) 207 { 208 struct spdk_iscsi_initiator_name *iname; 209 210 TAILQ_FOREACH(iname, &igp->initiator_head, tailq) { 211 /* denied if iqn is matched */ 212 if ((iname->name[0] == '!') 213 && (strcasecmp(&iname->name[1], "ANY") == 0 214 || strcasecmp(&iname->name[1], iqn) == 0)) { 215 *result = false; 216 return 0; 217 } 218 /* allowed if iqn is matched */ 219 if (strcasecmp(iname->name, "ANY") == 0 220 || strcasecmp(iname->name, iqn) == 0) { 221 *result = true; 222 return 0; 223 } 224 } 225 return -1; 226 } 227 228 static struct spdk_iscsi_pg_map * 229 spdk_iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 230 struct spdk_iscsi_portal_grp *pg); 231 232 bool 233 spdk_iscsi_tgt_node_access(struct spdk_iscsi_conn *conn, 234 struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr) 235 { 236 struct spdk_iscsi_portal_grp *pg; 237 struct spdk_iscsi_pg_map *pg_map; 238 struct spdk_iscsi_ig_map *ig_map; 239 int rc; 240 bool allowed = false; 241 242 if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) { 243 return false; 244 } 245 pg = conn->portal->group; 246 247 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "pg=%d, iqn=%s, addr=%s\n", 248 pg->tag, iqn, addr); 249 pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg); 250 if (pg_map == NULL) { 251 return false; 252 } 253 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 254 rc = spdk_iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed); 255 if (rc == 0) { 256 if (allowed == false) { 257 goto denied; 258 } else { 259 if (spdk_iscsi_init_grp_allow_addr(ig_map->ig, addr)) { 260 return true; 261 } 262 } 263 } else { 264 /* netmask is denied in this initiator group */ 265 } 266 } 267 268 denied: 269 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "access denied from %s (%s) to %s (%s:%s,%d)\n", 270 iqn, addr, target->name, conn->portal->host, 271 conn->portal->port, conn->portal->group->tag); 272 return false; 273 } 274 275 static bool 276 spdk_iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn) 277 { 278 struct spdk_iscsi_pg_map *pg_map; 279 struct spdk_iscsi_ig_map *ig_map; 280 int rc; 281 bool result = false; 282 283 if (target == NULL || iqn == NULL) { 284 return false; 285 } 286 287 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 288 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 289 rc = spdk_iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result); 290 if (rc == 0) { 291 return result; 292 } 293 } 294 } 295 296 return false; 297 } 298 299 int 300 spdk_iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn, 301 const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len, 302 int data_len) 303 { 304 char buf[MAX_TMPBUF]; 305 struct spdk_iscsi_portal_grp *pg; 306 struct spdk_iscsi_pg_map *pg_map; 307 struct spdk_iscsi_portal *p; 308 struct spdk_iscsi_tgt_node *target; 309 char *host; 310 int total; 311 int len; 312 int rc; 313 314 if (conn == NULL) { 315 return 0; 316 } 317 318 total = data_len; 319 if (alloc_len < 1) { 320 return 0; 321 } 322 if (total > alloc_len) { 323 total = alloc_len; 324 data[total - 1] = '\0'; 325 return total; 326 } 327 328 if (alloc_len - total < 1) { 329 SPDK_ERRLOG("data space small %d\n", alloc_len); 330 return total; 331 } 332 333 pthread_mutex_lock(&g_spdk_iscsi.mutex); 334 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 335 if (strcasecmp(tiqn, "ALL") != 0 336 && strcasecmp(tiqn, target->name) != 0) { 337 continue; 338 } 339 rc = spdk_iscsi_tgt_node_allow_iscsi_name(target, iiqn); 340 if (rc == 0) { 341 continue; 342 } 343 344 /* DO SENDTARGETS */ 345 len = snprintf((char *) data + total, alloc_len - total, 346 "TargetName=%s", target->name); 347 total += len + 1; 348 349 /* write to data */ 350 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 351 pg = pg_map->pg; 352 TAILQ_FOREACH(p, &pg->head, per_pg_tailq) { 353 if (alloc_len - total < 1) { 354 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 355 SPDK_ERRLOG("data space small %d\n", alloc_len); 356 return total; 357 } 358 host = p->host; 359 /* wildcard? */ 360 if (strcasecmp(host, "[::]") == 0 361 || strcasecmp(host, "0.0.0.0") == 0) { 362 if (spdk_sock_is_ipv6(conn->sock)) { 363 snprintf(buf, sizeof buf, "[%s]", 364 conn->target_addr); 365 host = buf; 366 } else if (spdk_sock_is_ipv4(conn->sock)) { 367 snprintf(buf, sizeof buf, "%s", 368 conn->target_addr); 369 host = buf; 370 } else { 371 /* skip portal for the family */ 372 continue; 373 } 374 } 375 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 376 "TargetAddress=%s:%s,%d\n", 377 host, p->port, pg->tag); 378 len = snprintf((char *) data + total, 379 alloc_len - total, 380 "TargetAddress=%s:%s,%d", 381 host, p->port, pg->tag); 382 total += len + 1; 383 } 384 } 385 } 386 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 387 388 return total; 389 } 390 391 static void 392 spdk_iscsi_tgt_node_list_init(void) 393 { 394 g_spdk_iscsi.ntargets = 0; 395 TAILQ_INIT(&g_spdk_iscsi.target_head); 396 } 397 398 struct spdk_iscsi_tgt_node * 399 spdk_iscsi_find_tgt_node(const char *target_name) 400 { 401 struct spdk_iscsi_tgt_node *target; 402 403 if (target_name == NULL) { 404 return NULL; 405 } 406 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 407 if (strcasecmp(target_name, target->name) == 0) { 408 return target; 409 } 410 } 411 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name); 412 return NULL; 413 } 414 415 static int 416 spdk_iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target) 417 { 418 pthread_mutex_lock(&g_spdk_iscsi.mutex); 419 420 if (spdk_iscsi_find_tgt_node(target->name) != NULL) { 421 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 422 return -EEXIST; 423 } 424 425 TAILQ_INSERT_TAIL(&g_spdk_iscsi.target_head, target, tailq); 426 g_spdk_iscsi.ntargets++; 427 428 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 429 return 0; 430 } 431 432 static int 433 spdk_iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target) 434 { 435 struct spdk_iscsi_tgt_node *t; 436 437 TAILQ_FOREACH(t, &g_spdk_iscsi.target_head, tailq) { 438 if (t == target) { 439 TAILQ_REMOVE(&g_spdk_iscsi.target_head, t, tailq); 440 g_spdk_iscsi.ntargets--; 441 return 0; 442 } 443 } 444 445 return -1; 446 } 447 448 static struct spdk_iscsi_ig_map * 449 spdk_iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map, 450 struct spdk_iscsi_init_grp *ig) 451 { 452 struct spdk_iscsi_ig_map *ig_map; 453 454 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 455 if (ig_map->ig == ig) { 456 return ig_map; 457 } 458 } 459 460 return NULL; 461 } 462 463 static struct spdk_iscsi_ig_map * 464 spdk_iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map, 465 struct spdk_iscsi_init_grp *ig) 466 { 467 struct spdk_iscsi_ig_map *ig_map; 468 469 if (spdk_iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) { 470 return NULL; 471 } 472 473 ig_map = malloc(sizeof(*ig_map)); 474 if (ig_map == NULL) { 475 return NULL; 476 } 477 478 ig_map->ig = ig; 479 ig->ref++; 480 pg_map->num_ig_maps++; 481 TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq); 482 483 return ig_map; 484 } 485 486 static void 487 _spdk_iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 488 struct spdk_iscsi_ig_map *ig_map) 489 { 490 TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq); 491 pg_map->num_ig_maps--; 492 ig_map->ig->ref--; 493 free(ig_map); 494 } 495 496 static int 497 spdk_iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 498 struct spdk_iscsi_init_grp *ig) 499 { 500 struct spdk_iscsi_ig_map *ig_map; 501 502 ig_map = spdk_iscsi_pg_map_find_ig_map(pg_map, ig); 503 if (ig_map == NULL) { 504 return -ENOENT; 505 } 506 507 _spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map); 508 return 0; 509 } 510 511 static void 512 spdk_iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map) 513 { 514 struct spdk_iscsi_ig_map *ig_map, *tmp; 515 516 TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) { 517 _spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map); 518 } 519 } 520 521 static struct spdk_iscsi_pg_map * 522 spdk_iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 523 struct spdk_iscsi_portal_grp *pg) 524 { 525 struct spdk_iscsi_pg_map *pg_map; 526 527 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 528 if (pg_map->pg == pg) { 529 return pg_map; 530 } 531 } 532 533 return NULL; 534 } 535 536 static struct spdk_iscsi_pg_map * 537 spdk_iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target, 538 struct spdk_iscsi_portal_grp *pg) 539 { 540 struct spdk_iscsi_pg_map *pg_map; 541 char port_name[MAX_TMPBUF]; 542 int rc; 543 544 if (spdk_iscsi_tgt_node_find_pg_map(target, pg) != NULL) { 545 return NULL; 546 } 547 548 if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) { 549 SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n", 550 SPDK_SCSI_DEV_MAX_PORTS); 551 return NULL; 552 } 553 554 pg_map = malloc(sizeof(*pg_map)); 555 if (pg_map == NULL) { 556 return NULL; 557 } 558 559 snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x", 560 spdk_scsi_dev_get_name(target->dev), pg->tag); 561 rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name); 562 if (rc != 0) { 563 free(pg_map); 564 return NULL; 565 } 566 567 TAILQ_INIT(&pg_map->ig_map_head); 568 pg_map->num_ig_maps = 0; 569 pg->ref++; 570 pg_map->pg = pg; 571 target->num_pg_maps++; 572 TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq); 573 574 return pg_map; 575 } 576 577 static void 578 _spdk_iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 579 struct spdk_iscsi_pg_map *pg_map) 580 { 581 TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq); 582 target->num_pg_maps--; 583 pg_map->pg->ref--; 584 585 spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag); 586 587 free(pg_map); 588 } 589 590 static int 591 spdk_iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 592 struct spdk_iscsi_portal_grp *pg) 593 { 594 struct spdk_iscsi_pg_map *pg_map; 595 596 pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg); 597 if (pg_map == NULL) { 598 return -ENOENT; 599 } 600 601 if (pg_map->num_ig_maps > 0) { 602 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n", 603 pg_map->num_ig_maps); 604 } 605 606 spdk_iscsi_pg_map_delete_all_ig_maps(pg_map); 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 static bool 847 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group) 848 { 849 if (group < 0) { 850 SPDK_ERRLOG("Invalid auth group ID (%d)\n", group); 851 return false; 852 } 853 if ((!disable && !require && !mutual) || /* Auto */ 854 (disable && !require && !mutual) || /* None */ 855 (!disable && require && !mutual) || /* CHAP */ 856 (!disable && require && mutual)) { /* CHAP Mutual */ 857 return true; 858 } 859 SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n", 860 disable, require, mutual); 861 return false; 862 } 863 864 _spdk_iscsi_tgt_node * 865 spdk_iscsi_tgt_node_construct(int target_index, 866 const char *name, const char *alias, 867 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps, 868 const char *bdev_name_list[], int *lun_id_list, int num_luns, 869 int queue_depth, 870 bool disable_chap, bool require_chap, bool mutual_chap, int chap_group, 871 bool header_digest, bool data_digest) 872 { 873 char fullname[MAX_TMPBUF]; 874 struct spdk_iscsi_tgt_node *target; 875 int rc; 876 877 if (!spdk_iscsi_check_chap_params(disable_chap, require_chap, 878 mutual_chap, chap_group)) { 879 return NULL; 880 } 881 882 if (num_maps == 0) { 883 SPDK_ERRLOG("num_maps = 0\n"); 884 return NULL; 885 } 886 887 if (name == NULL) { 888 SPDK_ERRLOG("TargetName not found\n"); 889 return NULL; 890 } 891 892 if (strncasecmp(name, "iqn.", 4) != 0 893 && strncasecmp(name, "eui.", 4) != 0 894 && strncasecmp(name, "naa.", 4) != 0) { 895 snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name); 896 } else { 897 snprintf(fullname, sizeof(fullname), "%s", name); 898 } 899 900 if (spdk_check_iscsi_name(fullname) != 0) { 901 SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n", 902 name); 903 return NULL; 904 } 905 906 target = malloc(sizeof(*target)); 907 if (!target) { 908 SPDK_ERRLOG("could not allocate target\n"); 909 return NULL; 910 } 911 912 memset(target, 0, sizeof(*target)); 913 914 rc = pthread_mutex_init(&target->mutex, NULL); 915 if (rc != 0) { 916 SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num); 917 spdk_iscsi_tgt_node_destruct(target); 918 return NULL; 919 } 920 921 target->num = target_index; 922 923 target->name = strdup(fullname); 924 if (!target->name) { 925 SPDK_ERRLOG("Could not allocate TargetName\n"); 926 spdk_iscsi_tgt_node_destruct(target); 927 return NULL; 928 } 929 930 if (alias == NULL) { 931 target->alias = NULL; 932 } else { 933 target->alias = strdup(alias); 934 if (!target->alias) { 935 SPDK_ERRLOG("Could not allocate TargetAlias\n"); 936 spdk_iscsi_tgt_node_destruct(target); 937 return NULL; 938 } 939 } 940 941 target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns, 942 SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL); 943 if (!target->dev) { 944 SPDK_ERRLOG("Could not construct SCSI device\n"); 945 spdk_iscsi_tgt_node_destruct(target); 946 return NULL; 947 } 948 949 TAILQ_INIT(&target->pg_map_head); 950 rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps); 951 if (rc != 0) { 952 SPDK_ERRLOG("could not add map to target\n"); 953 spdk_iscsi_tgt_node_destruct(target); 954 return NULL; 955 } 956 957 target->disable_chap = disable_chap; 958 target->require_chap = require_chap; 959 target->mutual_chap = mutual_chap; 960 target->chap_group = chap_group; 961 target->header_digest = header_digest; 962 target->data_digest = data_digest; 963 964 if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) { 965 target->queue_depth = queue_depth; 966 } else { 967 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n", 968 queue_depth, g_spdk_iscsi.MaxQueueDepth); 969 target->queue_depth = g_spdk_iscsi.MaxQueueDepth; 970 } 971 972 rc = spdk_iscsi_tgt_node_register(target); 973 if (rc != 0) { 974 SPDK_ERRLOG("register target is failed\n"); 975 spdk_iscsi_tgt_node_destruct(target); 976 return NULL; 977 } 978 979 return target; 980 } 981 982 static int 983 spdk_cf_add_iscsi_tgt_node(struct spdk_conf_section *sp) 984 { 985 char buf[MAX_TMPBUF]; 986 struct spdk_iscsi_tgt_node *target; 987 int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP]; 988 int num_target_maps; 989 const char *alias, *pg_tag, *ig_tag; 990 const char *ag_tag; 991 const char *val, *name; 992 int target_num, chap_group, pg_tag_i, ig_tag_i; 993 bool header_digest, data_digest; 994 bool disable_chap, require_chap, mutual_chap; 995 int i; 996 int lun_id_list[SPDK_SCSI_DEV_MAX_LUN]; 997 const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN]; 998 int num_luns, queue_depth; 999 1000 target_num = spdk_conf_section_get_num(sp); 1001 1002 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num); 1003 1004 data_digest = false; 1005 header_digest = false; 1006 1007 name = spdk_conf_section_get_val(sp, "TargetName"); 1008 1009 if (name == NULL) { 1010 SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num); 1011 return -1; 1012 } 1013 1014 alias = spdk_conf_section_get_val(sp, "TargetAlias"); 1015 1016 /* Setup initiator and portal group mapping */ 1017 val = spdk_conf_section_get_val(sp, "Mapping"); 1018 if (val == NULL) { 1019 /* no map */ 1020 SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num); 1021 return -1; 1022 } 1023 1024 for (i = 0; i < MAX_TARGET_MAP; i++) { 1025 val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1026 if (val == NULL) { 1027 break; 1028 } 1029 pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1030 ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1); 1031 if (pg_tag == NULL || ig_tag == NULL) { 1032 SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num); 1033 return -1; 1034 } 1035 if (strncasecmp(pg_tag, "PortalGroup", 1036 strlen("PortalGroup")) != 0 1037 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) { 1038 SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num); 1039 return -1; 1040 } 1041 if (strncasecmp(ig_tag, "InitiatorGroup", 1042 strlen("InitiatorGroup")) != 0 1043 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) { 1044 SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num); 1045 return -1; 1046 } 1047 if (pg_tag_i < 1 || ig_tag_i < 1) { 1048 SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num); 1049 return -1; 1050 } 1051 pg_tag_list[i] = pg_tag_i; 1052 ig_tag_list[i] = ig_tag_i; 1053 } 1054 1055 num_target_maps = i; 1056 1057 /* Setup AuthMethod */ 1058 val = spdk_conf_section_get_val(sp, "AuthMethod"); 1059 disable_chap = false; 1060 require_chap = false; 1061 mutual_chap = false; 1062 if (val != NULL) { 1063 for (i = 0; ; i++) { 1064 val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i); 1065 if (val == NULL) { 1066 break; 1067 } 1068 if (strcasecmp(val, "CHAP") == 0) { 1069 require_chap = true; 1070 } else if (strcasecmp(val, "Mutual") == 0) { 1071 mutual_chap = true; 1072 } else if (strcasecmp(val, "Auto") == 0) { 1073 disable_chap = false; 1074 require_chap = false; 1075 mutual_chap = false; 1076 } else if (strcasecmp(val, "None") == 0) { 1077 disable_chap = true; 1078 require_chap = false; 1079 mutual_chap = false; 1080 } else { 1081 SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num); 1082 return -1; 1083 } 1084 } 1085 if (mutual_chap && !require_chap) { 1086 SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num); 1087 return -1; 1088 } 1089 } 1090 if (disable_chap) { 1091 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n"); 1092 } else if (!require_chap) { 1093 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n"); 1094 } else { 1095 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n", 1096 mutual_chap ? "Mutual" : ""); 1097 } 1098 1099 val = spdk_conf_section_get_val(sp, "AuthGroup"); 1100 if (val == NULL) { 1101 chap_group = 0; 1102 } else { 1103 ag_tag = val; 1104 if (strcasecmp(ag_tag, "None") == 0) { 1105 chap_group = 0; 1106 } else { 1107 if (strncasecmp(ag_tag, "AuthGroup", 1108 strlen("AuthGroup")) != 0 1109 || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) { 1110 SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num); 1111 return -1; 1112 } 1113 if (chap_group == 0) { 1114 SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num); 1115 return -1; 1116 } 1117 } 1118 } 1119 if (chap_group == 0) { 1120 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n"); 1121 } else { 1122 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group); 1123 } 1124 1125 val = spdk_conf_section_get_val(sp, "UseDigest"); 1126 if (val != NULL) { 1127 for (i = 0; ; i++) { 1128 val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i); 1129 if (val == NULL) { 1130 break; 1131 } 1132 if (strcasecmp(val, "Header") == 0) { 1133 header_digest = true; 1134 } else if (strcasecmp(val, "Data") == 0) { 1135 data_digest = true; 1136 } else if (strcasecmp(val, "Auto") == 0) { 1137 header_digest = false; 1138 data_digest = false; 1139 } else { 1140 SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num); 1141 return -1; 1142 } 1143 } 1144 } 1145 if (!header_digest && !data_digest) { 1146 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n"); 1147 } else { 1148 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n", 1149 header_digest ? "Header" : "", 1150 data_digest ? "Data" : ""); 1151 } 1152 1153 val = spdk_conf_section_get_val(sp, "QueueDepth"); 1154 if (val == NULL) { 1155 queue_depth = g_spdk_iscsi.MaxQueueDepth; 1156 } else { 1157 queue_depth = (int) strtol(val, NULL, 10); 1158 } 1159 1160 num_luns = 0; 1161 1162 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1163 snprintf(buf, sizeof(buf), "LUN%d", i); 1164 val = spdk_conf_section_get_val(sp, buf); 1165 if (val == NULL) { 1166 continue; 1167 } 1168 1169 bdev_name_list[num_luns] = val; 1170 lun_id_list[num_luns] = i; 1171 num_luns++; 1172 } 1173 1174 if (num_luns == 0) { 1175 SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name); 1176 return -1; 1177 } 1178 1179 target = spdk_iscsi_tgt_node_construct(target_num, name, alias, 1180 pg_tag_list, ig_tag_list, num_target_maps, 1181 bdev_name_list, lun_id_list, num_luns, queue_depth, 1182 disable_chap, require_chap, mutual_chap, chap_group, 1183 header_digest, data_digest); 1184 1185 if (target == NULL) { 1186 SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num); 1187 return -1; 1188 } 1189 1190 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1191 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1192 1193 if (lun) { 1194 SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n", 1195 spdk_scsi_dev_get_id(target->dev), 1196 spdk_scsi_lun_get_id(lun), 1197 spdk_scsi_lun_get_bdev_name(lun)); 1198 } 1199 } 1200 1201 return 0; 1202 } 1203 1204 int spdk_iscsi_init_tgt_nodes(void) 1205 { 1206 struct spdk_conf_section *sp; 1207 int rc; 1208 1209 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_init_tgt_nodes\n"); 1210 1211 spdk_iscsi_tgt_node_list_init(); 1212 1213 sp = spdk_conf_first_section(NULL); 1214 while (sp != NULL) { 1215 if (spdk_conf_section_match_prefix(sp, "TargetNode")) { 1216 int tag = spdk_conf_section_get_num(sp); 1217 1218 if (tag > SPDK_TN_TAG_MAX) { 1219 SPDK_ERRLOG("tag %d is invalid\n", tag); 1220 return -1; 1221 } 1222 rc = spdk_cf_add_iscsi_tgt_node(sp); 1223 if (rc < 0) { 1224 SPDK_ERRLOG("spdk_cf_add_iscsi_tgt_node() failed\n"); 1225 return -1; 1226 } 1227 } 1228 sp = spdk_conf_next_section(sp); 1229 } 1230 return 0; 1231 } 1232 1233 void 1234 spdk_iscsi_shutdown_tgt_nodes(void) 1235 { 1236 struct spdk_iscsi_tgt_node *target, *tmp; 1237 1238 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1239 TAILQ_FOREACH_SAFE(target, &g_spdk_iscsi.target_head, tailq, tmp) { 1240 TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq); 1241 g_spdk_iscsi.ntargets--; 1242 spdk_iscsi_tgt_node_destruct(target); 1243 } 1244 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1245 } 1246 1247 int 1248 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name) 1249 { 1250 struct spdk_iscsi_tgt_node *target; 1251 1252 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1253 target = spdk_iscsi_find_tgt_node(target_name); 1254 if (target != NULL) { 1255 spdk_iscsi_tgt_node_unregister(target); 1256 spdk_iscsi_tgt_node_destruct(target); 1257 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1258 1259 return 0; 1260 } 1261 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1262 1263 return -ENOENT; 1264 } 1265 1266 int 1267 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn, 1268 struct spdk_iscsi_tgt_node *target) 1269 { 1270 int i; 1271 struct spdk_iscsi_task *task; 1272 1273 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1274 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1275 1276 if (!lun) { 1277 continue; 1278 } 1279 1280 /* we create a fake management task per LUN to cleanup */ 1281 task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl); 1282 if (!task) { 1283 SPDK_ERRLOG("Unable to acquire task\n"); 1284 return -1; 1285 } 1286 1287 task->scsi.target_port = conn->target_port; 1288 task->scsi.initiator_port = conn->initiator_port; 1289 task->scsi.lun = lun; 1290 1291 spdk_scsi_dev_queue_mgmt_task(target->dev, &task->scsi, SPDK_SCSI_TASK_FUNC_LUN_RESET); 1292 } 1293 1294 return 0; 1295 } 1296 1297 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group, 1298 struct spdk_iscsi_init_grp *initiator_group) 1299 { 1300 struct spdk_iscsi_tgt_node *target; 1301 1302 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1303 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1304 if (portal_group) { 1305 spdk_iscsi_tgt_node_delete_pg_map(target, portal_group); 1306 } 1307 if (initiator_group) { 1308 spdk_iscsi_tgt_node_delete_ig_maps(target, initiator_group); 1309 } 1310 } 1311 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1312 } 1313 1314 int 1315 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target, 1316 const char *bdev_name, int lun_id) 1317 { 1318 struct spdk_scsi_dev *dev; 1319 int rc; 1320 1321 if (target->num_active_conns > 0) { 1322 SPDK_ERRLOG("Target has active connections (count=%d)\n", 1323 target->num_active_conns); 1324 return -1; 1325 } 1326 1327 if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 1328 SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id); 1329 return -1; 1330 } 1331 1332 dev = target->dev; 1333 if (dev == NULL) { 1334 SPDK_ERRLOG("SCSI device is not found\n"); 1335 return -1; 1336 } 1337 1338 rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL); 1339 if (rc != 0) { 1340 SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n"); 1341 return -1; 1342 } 1343 1344 return 0; 1345 } 1346