1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/cpuvar.h> 27 #include <sys/types.h> 28 #include <sys/conf.h> 29 #include <sys/file.h> 30 #include <sys/ddi.h> 31 #include <sys/sunddi.h> 32 #include <sys/modctl.h> 33 #include <sys/sysmacros.h> 34 #include <sys/sdt.h> 35 36 #include <sys/socket.h> 37 #include <sys/strsubr.h> 38 39 #include <sys/stmf.h> 40 #include <sys/stmf_ioctl.h> 41 #include <sys/portif.h> 42 #include <sys/idm/idm.h> 43 44 #define ISCSIT_TGT_SM_STRINGS 45 #include <iscsit.h> 46 #include <iscsit_isns.h> 47 48 typedef struct { 49 list_node_t te_ctx_node; 50 iscsit_tgt_event_t te_ctx_event; 51 } tgt_event_ctx_t; 52 53 static void 54 tgt_sm_event_dispatch(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 55 56 static void 57 tgt_sm_created(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 58 59 static void 60 tgt_sm_onlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 61 62 static void 63 tgt_sm_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 64 65 static void 66 tgt_sm_stmf_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 67 68 static void 69 tgt_sm_deleting_need_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 70 71 static void 72 tgt_sm_offlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 73 74 static void 75 tgt_sm_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 76 77 static void 78 tgt_sm_stmf_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 79 80 static void 81 tgt_sm_deleting_stmf_dereg(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 82 83 static void 84 tgt_sm_deleting_stmf_dereg_fail(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 85 86 static void 87 tgt_sm_deleting(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx); 88 89 static void 90 iscsit_tgt_dereg_retry(void *arg); 91 92 static void 93 iscsit_tgt_dereg_task(void *arg); 94 95 static void 96 tgt_sm_new_state(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx, 97 iscsit_tgt_state_t new_state); 98 99 100 static iscsit_tgt_t * 101 iscsit_tgt_create(it_tgt_t *cfg_tgt); 102 103 static void 104 iscsit_tgt_unref(void *tgt); 105 106 static void 107 iscsit_tgt_async_wait_ref(iscsit_tgt_t *tgt, idm_refcnt_cb_t *cb_func); 108 109 static void 110 iscsit_tgt_destroy(iscsit_tgt_t *tgt); 111 112 static iscsit_tpgt_t * 113 iscsit_tgt_lookup_tpgt_locked(iscsit_tgt_t *tgt, uint16_t tag); 114 115 static iscsit_tpg_t * 116 iscsit_tpg_lookup_locked(char *tpg_name); 117 118 static iscsit_portal_t * 119 iscsit_tpg_portal_lookup_locked(iscsit_tpg_t *tpg, 120 struct sockaddr_storage *sa); 121 122 static idm_status_t 123 iscsit_tgt_online(iscsit_tgt_t *tgt); 124 125 static void 126 iscsit_tgt_offline(iscsit_tgt_t *tgt); 127 128 static idm_status_t 129 iscsit_tgt_modify(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt); 130 131 static idm_status_t 132 iscsit_tgt_merge_tpgt(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt, 133 list_t *tpgt_del_list); 134 135 static iscsit_tpgt_t * 136 iscsit_tpgt_create(it_tpgt_t *cfg_tpgt); 137 138 static iscsit_tpgt_t * 139 iscsit_tpgt_create_default(); 140 141 static void 142 iscsit_tpgt_destroy(iscsit_tpgt_t *tpgt); 143 144 static iscsit_tpg_t * 145 iscsit_tpg_create(it_tpg_t *tpg); 146 147 static void 148 iscsit_tpg_modify(iscsit_tpg_t *tpg, it_tpg_t *cfg_tpg); 149 150 static void 151 iscsit_tpg_destroy(iscsit_tpg_t *tpg); 152 153 static iscsit_portal_t * 154 iscsit_portal_create(iscsit_tpg_t *tpg, struct sockaddr_storage *sa); 155 156 static void 157 iscsit_portal_destroy(iscsit_portal_t *portal); 158 159 static idm_status_t 160 iscsit_portal_online(iscsit_portal_t *portal); 161 162 static void 163 iscsit_portal_offline(iscsit_portal_t *portal); 164 165 166 167 /* 168 * Target state machine 169 */ 170 171 void 172 iscsit_tgt_sm_event(iscsit_tgt_t *tgt, iscsit_tgt_event_t event) 173 { 174 mutex_enter(&tgt->target_mutex); 175 tgt_sm_event_locked(tgt, event); 176 mutex_exit(&tgt->target_mutex); 177 } 178 179 void 180 tgt_sm_event_locked(iscsit_tgt_t *tgt, iscsit_tgt_event_t event) 181 { 182 tgt_event_ctx_t *ctx; 183 184 iscsit_tgt_hold(tgt); 185 186 ctx = kmem_zalloc(sizeof (*ctx), KM_SLEEP); 187 188 ctx->te_ctx_event = event; 189 190 list_insert_tail(&tgt->target_events, ctx); 191 /* 192 * Use the target_sm_busy flag to keep the state machine single 193 * threaded. This also serves as recursion avoidance since this 194 * flag will always be set if we call iscsit_tgt_sm_event from 195 * within the state machine code. 196 */ 197 if (!tgt->target_sm_busy) { 198 tgt->target_sm_busy = B_TRUE; 199 while (!list_is_empty(&tgt->target_events)) { 200 ctx = list_head(&tgt->target_events); 201 list_remove(&tgt->target_events, ctx); 202 idm_sm_audit_event(&tgt->target_state_audit, 203 SAS_ISCSIT_TGT, (int)tgt->target_state, 204 (int)ctx->te_ctx_event, 0); 205 mutex_exit(&tgt->target_mutex); 206 tgt_sm_event_dispatch(tgt, ctx); 207 mutex_enter(&tgt->target_mutex); 208 } 209 tgt->target_sm_busy = B_FALSE; 210 211 } 212 213 iscsit_tgt_rele(tgt); 214 } 215 216 static void 217 tgt_sm_event_dispatch(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 218 { 219 DTRACE_PROBE2(tgt__event, iscsit_tgt_t *, tgt, 220 tgt_event_ctx_t *, ctx); 221 222 IDM_SM_LOG(CE_NOTE, "tgt_sm_event_dispatch: tgt %p event %s(%d)", 223 (void *)tgt, iscsit_te_name[ctx->te_ctx_event], ctx->te_ctx_event); 224 225 /* State independent actions */ 226 switch (ctx->te_ctx_event) { 227 case TE_DELETE: 228 tgt->target_deleting = B_TRUE; 229 break; 230 } 231 232 /* State dependent actions */ 233 switch (tgt->target_state) { 234 case TS_CREATED: 235 tgt_sm_created(tgt, ctx); 236 break; 237 case TS_ONLINING: 238 tgt_sm_onlining(tgt, ctx); 239 break; 240 case TS_ONLINE: 241 tgt_sm_online(tgt, ctx); 242 break; 243 case TS_STMF_ONLINE: 244 tgt_sm_stmf_online(tgt, ctx); 245 break; 246 case TS_DELETING_NEED_OFFLINE: 247 tgt_sm_deleting_need_offline(tgt, ctx); 248 break; 249 case TS_OFFLINING: 250 tgt_sm_offlining(tgt, ctx); 251 break; 252 case TS_OFFLINE: 253 tgt_sm_offline(tgt, ctx); 254 break; 255 case TS_STMF_OFFLINE: 256 tgt_sm_stmf_offline(tgt, ctx); 257 break; 258 case TS_DELETING_STMF_DEREG: 259 tgt_sm_deleting_stmf_dereg(tgt, ctx); 260 break; 261 case TS_DELETING_STMF_DEREG_FAIL: 262 tgt_sm_deleting_stmf_dereg_fail(tgt, ctx); 263 break; 264 case TS_DELETING: 265 tgt_sm_deleting(tgt, ctx); 266 break; 267 default: 268 ASSERT(0); 269 } 270 271 kmem_free(ctx, sizeof (*ctx)); 272 } 273 274 static void 275 tgt_sm_created(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 276 { 277 stmf_change_status_t scs; 278 279 switch (ctx->te_ctx_event) { 280 case TE_STMF_ONLINE_REQ: 281 tgt_sm_new_state(tgt, ctx, TS_ONLINING); 282 break; 283 case TE_DELETE: 284 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 285 break; 286 case TE_STMF_OFFLINE_REQ: 287 /* 288 * We're already offline but update to an equivelant 289 * state just to note that STMF talked to us. 290 */ 291 scs.st_completion_status = STMF_SUCCESS; 292 scs.st_additional_info = NULL; 293 tgt_sm_new_state(tgt, ctx, TS_OFFLINE); 294 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 295 tgt->target_stmf_lport, &scs); 296 break; 297 case TE_STMF_ONLINE_COMPLETE_ACK: 298 case TE_STMF_OFFLINE_COMPLETE_ACK: 299 /* Ignore */ 300 break; 301 default: 302 ASSERT(0); 303 } 304 } 305 306 static void 307 tgt_sm_onlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 308 { 309 stmf_change_status_t scs; 310 311 switch (ctx->te_ctx_event) { 312 case TE_ONLINE_SUCCESS: 313 tgt_sm_new_state(tgt, ctx, TS_ONLINE); 314 break; 315 case TE_ONLINE_FAIL: 316 tgt_sm_new_state(tgt, ctx, TS_STMF_OFFLINE); 317 break; 318 case TE_DELETE: 319 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 320 break; 321 case TE_STMF_ONLINE_REQ: 322 case TE_STMF_OFFLINE_REQ: 323 /* 324 * We can't complete STMF's request since we are busy going 325 * online. 326 */ 327 scs.st_completion_status = STMF_INVALID_ARG; 328 scs.st_additional_info = NULL; 329 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 330 STMF_CMD_LPORT_ONLINE_COMPLETE : 331 STMF_CMD_LPORT_OFFLINE_COMPLETE, 332 tgt->target_stmf_lport, &scs); 333 break; 334 case TE_STMF_ONLINE_COMPLETE_ACK: 335 case TE_STMF_OFFLINE_COMPLETE_ACK: 336 /* Ignore */ 337 break; 338 default: 339 ASSERT(0); 340 } 341 } 342 343 static void 344 tgt_sm_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 345 { 346 stmf_change_status_t scs; 347 348 switch (ctx->te_ctx_event) { 349 case TE_STMF_ONLINE_COMPLETE_ACK: 350 if (tgt->target_deleting) { 351 tgt_sm_new_state(tgt, ctx, TS_DELETING_NEED_OFFLINE); 352 } else { 353 tgt_sm_new_state(tgt, ctx, TS_STMF_ONLINE); 354 } 355 break; 356 case TE_DELETE: 357 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 358 break; 359 case TE_STMF_ONLINE_REQ: 360 case TE_STMF_OFFLINE_REQ: 361 /* 362 * We can't complete STMF's request since we are busy going 363 * online (waiting for acknowlegement from STMF) 364 */ 365 scs.st_completion_status = STMF_INVALID_ARG; 366 scs.st_additional_info = NULL; 367 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 368 STMF_CMD_LPORT_ONLINE_COMPLETE : 369 STMF_CMD_LPORT_OFFLINE_COMPLETE, 370 tgt->target_stmf_lport, &scs); 371 break; 372 case TE_STMF_OFFLINE_COMPLETE_ACK: 373 /* Ignore */ 374 break; 375 default: 376 ASSERT(0); 377 } 378 } 379 380 381 static void 382 tgt_sm_stmf_online(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 383 { 384 stmf_change_status_t scs; 385 386 /* Deregister target with iSNS whenever we leave this state */ 387 388 switch (ctx->te_ctx_event) { 389 case TE_DELETE: 390 (void) iscsit_isns_deregister(tgt); 391 tgt_sm_new_state(tgt, ctx, TS_DELETING_NEED_OFFLINE); 392 break; 393 case TE_STMF_OFFLINE_REQ: 394 (void) iscsit_isns_deregister(tgt); 395 tgt_sm_new_state(tgt, ctx, TS_OFFLINING); 396 break; 397 case TE_STMF_ONLINE_REQ: 398 /* Already online */ 399 scs.st_completion_status = STMF_ALREADY; 400 scs.st_additional_info = NULL; 401 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, 402 tgt->target_stmf_lport, &scs); 403 break; 404 case TE_STMF_ONLINE_COMPLETE_ACK: 405 case TE_STMF_OFFLINE_COMPLETE_ACK: 406 /* Ignore */ 407 break; 408 default: 409 ASSERT(0); 410 } 411 } 412 413 414 static void 415 tgt_sm_deleting_need_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 416 { 417 stmf_change_status_t scs; 418 419 switch (ctx->te_ctx_event) { 420 case TE_STMF_OFFLINE_REQ: 421 tgt_sm_new_state(tgt, ctx, TS_OFFLINING); 422 break; 423 case TE_DELETE: 424 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 425 break; 426 case TE_STMF_ONLINE_REQ: 427 /* 428 * We can't complete STMF's request since we need to be offlined 429 */ 430 scs.st_completion_status = STMF_INVALID_ARG; 431 scs.st_additional_info = NULL; 432 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, 433 tgt->target_stmf_lport, &scs); 434 break; 435 case TE_STMF_ONLINE_COMPLETE_ACK: 436 case TE_STMF_OFFLINE_COMPLETE_ACK: 437 /* Ignore */ 438 break; 439 default: 440 ASSERT(0); 441 } 442 } 443 444 445 static void 446 tgt_sm_offlining(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 447 { 448 stmf_change_status_t scs; 449 450 switch (ctx->te_ctx_event) { 451 case TE_OFFLINE_COMPLETE: 452 tgt_sm_new_state(tgt, ctx, TS_OFFLINE); 453 break; 454 case TE_DELETE: 455 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 456 break; 457 case TE_STMF_ONLINE_REQ: 458 case TE_STMF_OFFLINE_REQ: 459 /* 460 * We can't complete STMF's request since we are busy going 461 * offline. 462 */ 463 scs.st_completion_status = STMF_INVALID_ARG; 464 scs.st_additional_info = NULL; 465 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 466 STMF_CMD_LPORT_ONLINE_COMPLETE : 467 STMF_CMD_LPORT_OFFLINE_COMPLETE, 468 tgt->target_stmf_lport, &scs); 469 break; 470 case TE_STMF_ONLINE_COMPLETE_ACK: 471 case TE_STMF_OFFLINE_COMPLETE_ACK: 472 /* Ignore */ 473 break; 474 default: 475 ASSERT(0); 476 } 477 } 478 479 480 static void 481 tgt_sm_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 482 { 483 stmf_change_status_t scs; 484 485 switch (ctx->te_ctx_event) { 486 case TE_STMF_OFFLINE_COMPLETE_ACK: 487 if (tgt->target_deleting) { 488 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 489 } else { 490 tgt_sm_new_state(tgt, ctx, TS_STMF_OFFLINE); 491 } 492 break; 493 case TE_DELETE: 494 /* TE_DELETE is handled in tgt_sm_event_dispatch() */ 495 break; 496 case TE_STMF_ONLINE_REQ: 497 case TE_STMF_OFFLINE_REQ: 498 /* 499 * We can't complete STMF's request since we are busy going 500 * offline. 501 */ 502 scs.st_completion_status = STMF_INVALID_ARG; 503 scs.st_additional_info = NULL; 504 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 505 STMF_CMD_LPORT_ONLINE_COMPLETE : 506 STMF_CMD_LPORT_OFFLINE_COMPLETE, 507 tgt->target_stmf_lport, &scs); 508 break; 509 case TE_STMF_ONLINE_COMPLETE_ACK: 510 /* Ignore */ 511 break; 512 default: 513 ASSERT(0); 514 } 515 } 516 517 518 static void 519 tgt_sm_stmf_offline(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 520 { 521 stmf_change_status_t scs; 522 523 switch (ctx->te_ctx_event) { 524 case TE_STMF_ONLINE_REQ: 525 tgt_sm_new_state(tgt, ctx, TS_ONLINING); 526 break; 527 case TE_DELETE: 528 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 529 break; 530 case TE_STMF_OFFLINE_REQ: 531 /* Already offline */ 532 scs.st_completion_status = STMF_ALREADY; 533 scs.st_additional_info = NULL; 534 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 535 tgt->target_stmf_lport, &scs); 536 break; 537 case TE_STMF_ONLINE_COMPLETE_ACK: 538 case TE_STMF_OFFLINE_COMPLETE_ACK: 539 /* Ignore */ 540 break; 541 default: 542 ASSERT(0); 543 } 544 } 545 546 547 static void 548 tgt_sm_deleting_stmf_dereg(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 549 { 550 stmf_change_status_t scs; 551 552 /* Terminal state, no events */ 553 switch (ctx->te_ctx_event) { 554 case TE_STMF_ONLINE_REQ: 555 case TE_STMF_OFFLINE_REQ: 556 /* 557 * We can't complete STMF's request since we are being deleted 558 */ 559 scs.st_completion_status = STMF_INVALID_ARG; 560 scs.st_additional_info = NULL; 561 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 562 STMF_CMD_LPORT_ONLINE_COMPLETE : 563 STMF_CMD_LPORT_OFFLINE_COMPLETE, 564 tgt->target_stmf_lport, &scs); 565 break; 566 case TE_STMF_ONLINE_COMPLETE_ACK: 567 case TE_STMF_OFFLINE_COMPLETE_ACK: 568 /* Ignore */ 569 break; 570 case TE_STMF_DEREG_SUCCESS: 571 tgt_sm_new_state(tgt, ctx, TS_DELETING); 572 break; 573 case TE_STMF_DEREG_FAIL: 574 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG_FAIL); 575 break; 576 default: 577 ASSERT(0); 578 } 579 } 580 581 static void 582 tgt_sm_deleting_stmf_dereg_fail(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 583 { 584 stmf_change_status_t scs; 585 586 /* Terminal state, no events */ 587 switch (ctx->te_ctx_event) { 588 case TE_STMF_ONLINE_REQ: 589 case TE_STMF_OFFLINE_REQ: 590 /* 591 * We can't complete STMF's request since we are being deleted 592 */ 593 scs.st_completion_status = STMF_INVALID_ARG; 594 scs.st_additional_info = NULL; 595 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 596 STMF_CMD_LPORT_ONLINE_COMPLETE : 597 STMF_CMD_LPORT_OFFLINE_COMPLETE, 598 tgt->target_stmf_lport, &scs); 599 break; 600 case TE_STMF_ONLINE_COMPLETE_ACK: 601 case TE_STMF_OFFLINE_COMPLETE_ACK: 602 /* Ignore */ 603 break; 604 case TE_STMF_DEREG_RETRY: 605 tgt_sm_new_state(tgt, ctx, TS_DELETING_STMF_DEREG); 606 break; 607 default: 608 ASSERT(0); 609 } 610 } 611 612 static void 613 tgt_sm_deleting(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx) 614 { 615 stmf_change_status_t scs; 616 617 /* Terminal state, no events */ 618 switch (ctx->te_ctx_event) { 619 case TE_STMF_ONLINE_REQ: 620 case TE_STMF_OFFLINE_REQ: 621 /* 622 * We can't complete STMF's request since we are being deleted 623 */ 624 scs.st_completion_status = STMF_INVALID_ARG; 625 scs.st_additional_info = NULL; 626 (void) stmf_ctl((ctx->te_ctx_event == TE_STMF_ONLINE_REQ) ? 627 STMF_CMD_LPORT_ONLINE_COMPLETE : 628 STMF_CMD_LPORT_OFFLINE_COMPLETE, 629 tgt->target_stmf_lport, &scs); 630 break; 631 case TE_STMF_ONLINE_COMPLETE_ACK: 632 case TE_STMF_OFFLINE_COMPLETE_ACK: 633 /* Ignore */ 634 break; 635 default: 636 ASSERT(0); 637 } 638 } 639 640 641 static void 642 iscsit_tgt_dereg_retry(void *arg) 643 { 644 iscsit_tgt_t *tgt = arg; 645 646 /* 647 * Rather than guaranteeing the target state machine code will not 648 * block for long periods of time (tying up this callout thread) 649 * we will queue a task on the taskq to send the retry event. 650 * If it fails we'll setup another timeout and try again later. 651 */ 652 if (taskq_dispatch(iscsit_global.global_dispatch_taskq, 653 iscsit_tgt_dereg_task, tgt, DDI_NOSLEEP) == NULL) { 654 /* Dispatch failed, try again later */ 655 (void) timeout(iscsit_tgt_dereg_retry, tgt, 656 drv_usectohz(TGT_DEREG_RETRY_SECONDS * 1000000)); 657 } 658 } 659 660 static void 661 iscsit_tgt_dereg_task(void *arg) 662 { 663 iscsit_tgt_t *tgt = arg; 664 665 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_RETRY); 666 } 667 668 static void 669 tgt_sm_new_state(iscsit_tgt_t *tgt, tgt_event_ctx_t *ctx, 670 iscsit_tgt_state_t new_state) 671 { 672 stmf_local_port_t *lport = tgt->target_stmf_lport; 673 stmf_change_status_t scs; 674 stmf_state_change_info_t sci; 675 idm_status_t idmrc; 676 stmf_status_t stmfrc; 677 678 scs.st_completion_status = STMF_SUCCESS; 679 scs.st_additional_info = NULL; 680 681 /* 682 * Validate new state 683 */ 684 ASSERT(new_state != TS_UNDEFINED); 685 ASSERT3U(new_state, <, TS_MAX_STATE); 686 687 new_state = (new_state < TS_MAX_STATE) ? 688 new_state : TS_UNDEFINED; 689 690 IDM_SM_LOG(CE_NOTE, "tgt_sm_new_state: tgt %p, %s(%d) --> %s(%d)\n", 691 (void *) tgt, iscsit_ts_name[tgt->target_state], tgt->target_state, 692 iscsit_ts_name[new_state], new_state); 693 DTRACE_PROBE3(target__state__change, 694 iscsit_tgt_t *, tgt, tgt_event_ctx_t *, ctx, 695 iscsit_tgt_state_t, new_state); 696 697 mutex_enter(&tgt->target_mutex); 698 idm_sm_audit_state_change(&tgt->target_state_audit, SAS_ISCSIT_TGT, 699 (int)tgt->target_state, (int)new_state); 700 tgt->target_last_state = tgt->target_state; 701 tgt->target_state = new_state; 702 mutex_exit(&tgt->target_mutex); 703 704 switch (tgt->target_state) { 705 case TS_ONLINING: 706 idmrc = iscsit_tgt_online(tgt); 707 if (idmrc != IDM_STATUS_SUCCESS) { 708 scs.st_completion_status = STMF_TARGET_FAILURE; 709 iscsit_tgt_sm_event(tgt, TE_ONLINE_FAIL); 710 } else { 711 iscsit_tgt_sm_event(tgt, TE_ONLINE_SUCCESS); 712 } 713 /* 714 * Let STMF know the how the online operation completed. 715 * STMF will respond with an acknowlege later 716 */ 717 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport, &scs); 718 break; 719 case TS_ONLINE: 720 break; 721 case TS_STMF_ONLINE: 722 (void) iscsit_isns_register(tgt); 723 break; 724 case TS_DELETING_NEED_OFFLINE: 725 sci.st_rflags = STMF_RFLAG_STAY_OFFLINED; 726 sci.st_additional_info = "Offline for delete"; 727 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE, lport, &sci); 728 break; 729 case TS_OFFLINING: 730 /* Async callback generates completion event */ 731 iscsit_tgt_offline(tgt); 732 break; 733 case TS_OFFLINE: 734 break; 735 case TS_STMF_OFFLINE: 736 break; 737 case TS_DELETING_STMF_DEREG: 738 stmfrc = stmf_deregister_local_port(tgt->target_stmf_lport); 739 if (stmfrc == STMF_SUCCESS) { 740 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_SUCCESS); 741 } else { 742 iscsit_tgt_sm_event(tgt, TE_STMF_DEREG_FAIL); 743 } 744 break; 745 case TS_DELETING_STMF_DEREG_FAIL: 746 /* Retry dereg in 1 second */ 747 (void) timeout(iscsit_tgt_dereg_retry, tgt, 748 drv_usectohz(TGT_DEREG_RETRY_SECONDS * 1000000)); 749 break; 750 case TS_DELETING: 751 iscsit_tgt_async_wait_ref(tgt, iscsit_tgt_unref); 752 break; 753 default: 754 ASSERT(0); 755 } 756 } 757 758 759 /* 760 * Target, TPGT, TPG utility functions 761 */ 762 763 it_cfg_status_t 764 iscsit_config_merge_tgt(it_config_t *cfg) 765 { 766 it_tgt_t *cfg_tgt; 767 iscsit_tgt_t *tgt, *next_tgt; 768 769 770 /* 771 * 1. >> Lock << 772 * 2. Removing deleted objects 773 * 3. Add deleted targets to global delete list 774 * 4. "delete" event to target state machine 775 * 5. >> Unlock << 776 * 6. Create new targets, update modified targets 777 */ 778 for (tgt = avl_first(&iscsit_global.global_target_list); 779 tgt != NULL; 780 tgt = next_tgt) { 781 next_tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt); 782 783 if (it_tgt_lookup(cfg, tgt->target_name) == NULL) { 784 avl_remove(&iscsit_global.global_target_list, tgt); 785 list_insert_tail( 786 &iscsit_global.global_deleted_target_list, tgt); 787 iscsit_tgt_sm_event(tgt, TE_DELETE); 788 } 789 } 790 791 /* Now walk through the list of configured targets */ 792 for (cfg_tgt = cfg->config_tgt_list; 793 cfg_tgt != NULL; 794 cfg_tgt = cfg_tgt->tgt_next) { 795 /* See if we have an existing target */ 796 tgt = iscsit_tgt_lookup_locked(cfg_tgt->tgt_name); 797 798 if (tgt == NULL) { 799 tgt = iscsit_tgt_create(cfg_tgt); 800 if (tgt == NULL) 801 return (ITCFG_TGT_CREATE_ERR); 802 avl_add(&iscsit_global.global_target_list, tgt); 803 } else { 804 (void) iscsit_tgt_modify(tgt, cfg_tgt); 805 iscsit_tgt_rele(tgt); 806 } 807 } 808 809 /* 810 * Targets on the iscsit_global.global_deleted_target_list will remove 811 * and destroy themselves when their associated state machines reach 812 * the TS_DELETED state and all references are released. 813 */ 814 return (ITCFG_SUCCESS); 815 } 816 817 iscsit_tgt_t * 818 iscsit_tgt_lookup(char *target_name) 819 { 820 iscsit_tgt_t *result; 821 822 ISCSIT_GLOBAL_LOCK(RW_READER); 823 result = iscsit_tgt_lookup_locked(target_name); 824 ISCSIT_GLOBAL_UNLOCK(); 825 826 return (result); 827 } 828 829 iscsit_tgt_t * 830 iscsit_tgt_lookup_locked(char *target_name) 831 { 832 iscsit_tgt_t tmp_tgt; 833 iscsit_tgt_t *result; 834 835 /* 836 * Use a dummy target for lookup, filling in all fields used in AVL 837 * comparison. 838 */ 839 tmp_tgt.target_name = target_name; 840 if ((result = avl_find(&iscsit_global.global_target_list, 841 &tmp_tgt, NULL)) != NULL) { 842 iscsit_tgt_hold(result); 843 } 844 845 return (result); 846 } 847 848 iscsit_tgt_t * 849 iscsit_tgt_create(it_tgt_t *cfg_tgt) 850 { 851 iscsit_tgt_t *result; 852 stmf_local_port_t *lport; 853 854 /* 855 * Each target is an STMF local port. 856 */ 857 lport = stmf_alloc(STMF_STRUCT_STMF_LOCAL_PORT, 858 sizeof (iscsit_tgt_t) + sizeof (scsi_devid_desc_t) + 859 strnlen(cfg_tgt->tgt_name, MAX_ISCSI_NODENAMELEN) + 1, 0); 860 if (lport == NULL) { 861 return (NULL); 862 } 863 864 result = lport->lport_port_private; 865 result->target_state = TS_CREATED; 866 result->target_stmf_lport_registered = 0; 867 /* Use pointer arithmetic to find scsi_devid_desc_t */ 868 result->target_devid = (scsi_devid_desc_t *)(result + 1); 869 (void) strcpy((char *)result->target_devid->ident, cfg_tgt->tgt_name); 870 result->target_devid->ident_length = 871 strnlen(cfg_tgt->tgt_name, MAX_ISCSI_NODENAMELEN); 872 result->target_devid->protocol_id = PROTOCOL_iSCSI; 873 result->target_devid->piv = 1; 874 result->target_devid->code_set = CODE_SET_ASCII; 875 result->target_devid->association = ID_IS_TARGET_PORT; 876 877 /* Store a shortcut to the target name */ 878 result->target_name = (char *)result->target_devid->ident; 879 idm_sm_audit_init(&result->target_state_audit); 880 mutex_init(&result->target_mutex, NULL, MUTEX_DEFAULT, NULL); 881 avl_create(&result->target_sess_list, iscsit_sess_avl_compare, 882 sizeof (iscsit_sess_t), offsetof(iscsit_sess_t, ist_tgt_ln)); 883 avl_create(&result->target_tpgt_list, iscsit_tpgt_avl_compare, 884 sizeof (iscsit_tpgt_t), offsetof(iscsit_tpgt_t, tpgt_tgt_ln)); 885 list_create(&result->target_events, sizeof (tgt_event_ctx_t), 886 offsetof(tgt_event_ctx_t, te_ctx_node)); 887 idm_refcnt_init(&result->target_refcnt, result); 888 idm_refcnt_init(&result->target_sess_refcnt, result); 889 890 /* Finish initializing local port */ 891 /* 892 * Would like infinite timeout, but this is about as long as can 893 * be specified to stmf on a 32 bit kernel. 894 */ 895 lport->lport_abort_timeout = 2000; /* seconds */ 896 lport->lport_id = result->target_devid; 897 lport->lport_pp = iscsit_global.global_pp; 898 lport->lport_ds = iscsit_global.global_dbuf_store; 899 lport->lport_xfer_data = &iscsit_xfer_scsi_data; 900 lport->lport_send_status = &iscsit_send_scsi_status; 901 lport->lport_task_free = &iscsit_lport_task_free; 902 lport->lport_abort = &iscsit_abort; 903 lport->lport_ctl = &iscsit_ctl; 904 result->target_stmf_lport = lport; 905 906 /* 907 * Additional target modifications from config 908 */ 909 if (iscsit_tgt_modify(result, cfg_tgt) != IDM_STATUS_SUCCESS) { 910 iscsit_tgt_destroy(result); 911 return (NULL); 912 } 913 914 /* 915 * Register the target with STMF but not until we have all the 916 * TPGT bindings and any other additional config setup. STMF 917 * may immediately ask us to go online. 918 */ 919 if (stmf_register_local_port(lport) != STMF_SUCCESS) { 920 iscsit_tgt_destroy(result); 921 return (NULL); 922 } 923 result->target_stmf_lport_registered = 1; 924 925 iscsit_global_hold(); 926 927 return (result); 928 } 929 930 static idm_status_t 931 iscsit_tgt_modify(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt) 932 { 933 idm_status_t idmrc = IDM_STATUS_SUCCESS; 934 list_t tpgt_del_list; 935 936 /* Merge TPGT */ 937 list_create(&tpgt_del_list, sizeof (iscsit_tpgt_t), 938 offsetof(iscsit_tpgt_t, tpgt_delete_ln)); 939 940 mutex_enter(&tgt->target_mutex); 941 if (tgt->target_props) { 942 nvlist_free(tgt->target_props); 943 tgt->target_props = NULL; 944 } 945 (void) nvlist_dup(cfg_tgt->tgt_properties, &tgt->target_props, 946 KM_SLEEP); 947 948 if ((idmrc = iscsit_tgt_merge_tpgt(tgt, cfg_tgt, &tpgt_del_list)) != 949 IDM_STATUS_SUCCESS) { 950 /* This should never happen */ 951 cmn_err(CE_WARN, "Fail to configure TPGTs for " 952 "target %s, the target modification could not be " 953 "completed.", tgt->target_name); 954 } 955 956 mutex_exit(&tgt->target_mutex); 957 958 iscsit_config_destroy_tpgts(&tpgt_del_list); 959 960 /* 961 * If the target is truly modified (not newly created), 962 * inform iSNS to update the target registration. 963 */ 964 if ((tgt->target_generation > 0) && 965 (cfg_tgt->tgt_generation > tgt->target_generation)) { 966 iscsit_isns_target_update(tgt); 967 } 968 969 tgt->target_generation = cfg_tgt->tgt_generation; 970 971 return (idmrc); 972 } 973 974 void 975 iscsit_config_destroy_tpgts(list_t *tpgt_del_list) 976 { 977 iscsit_tpgt_t *tpgt, *next_tpgt; 978 979 for (tpgt = list_head(tpgt_del_list); 980 tpgt != NULL; 981 tpgt = next_tpgt) { 982 next_tpgt = list_next(tpgt_del_list, tpgt); 983 984 list_remove(tpgt_del_list, tpgt); 985 idm_refcnt_wait_ref(&tpgt->tpgt_refcnt); 986 iscsit_tpgt_destroy(tpgt); 987 } 988 } 989 990 void 991 iscsit_tgt_unref(void *tgt_void) 992 { 993 iscsit_tgt_t *tgt = tgt_void; 994 995 ISCSIT_GLOBAL_LOCK(RW_WRITER); 996 list_remove(&iscsit_global.global_deleted_target_list, tgt); 997 ISCSIT_GLOBAL_UNLOCK(); 998 iscsit_tgt_destroy(tgt); 999 } 1000 1001 void 1002 iscsit_tgt_async_wait_ref(iscsit_tgt_t *tgt, idm_refcnt_cb_t *cb_func) 1003 { 1004 idm_refcnt_async_wait_ref(&tgt->target_refcnt, cb_func); 1005 } 1006 1007 static void 1008 iscsit_tgt_destroy(iscsit_tgt_t *tgt) 1009 { 1010 iscsit_tpgt_t *tpgt, *next_tpgt; 1011 1012 ASSERT(tgt->target_state == TS_DELETING); 1013 1014 /* 1015 * Destroy all target portal group tags 1016 */ 1017 mutex_enter(&tgt->target_mutex); 1018 for (tpgt = avl_first(&tgt->target_tpgt_list); 1019 tpgt != NULL; 1020 tpgt = next_tpgt) { 1021 next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt); 1022 avl_remove(&tgt->target_tpgt_list, tpgt); 1023 iscsit_tpgt_destroy(tpgt); 1024 } 1025 1026 if (tgt->target_props) { 1027 nvlist_free(tgt->target_props); 1028 } 1029 mutex_exit(&tgt->target_mutex); 1030 1031 /* 1032 * Destroy target 1033 */ 1034 idm_refcnt_destroy(&tgt->target_sess_refcnt); 1035 idm_refcnt_destroy(&tgt->target_refcnt); 1036 list_destroy(&tgt->target_events); 1037 avl_destroy(&tgt->target_tpgt_list); 1038 avl_destroy(&tgt->target_sess_list); 1039 mutex_destroy(&tgt->target_mutex); 1040 stmf_free(tgt->target_stmf_lport); /* Also frees "tgt' */ 1041 iscsit_global_rele(); 1042 } 1043 1044 void 1045 iscsit_tgt_hold(iscsit_tgt_t *tgt) 1046 { 1047 idm_refcnt_hold(&tgt->target_refcnt); 1048 } 1049 1050 void 1051 iscsit_tgt_rele(iscsit_tgt_t *tgt) 1052 { 1053 idm_refcnt_rele(&tgt->target_refcnt); 1054 } 1055 1056 int 1057 iscsit_tgt_avl_compare(const void *void_tgt1, const void *void_tgt2) 1058 { 1059 const iscsit_tgt_t *tgt1 = void_tgt1; 1060 const iscsit_tgt_t *tgt2 = void_tgt2; 1061 int result; 1062 1063 /* 1064 * Sort by ISID first then TSIH 1065 */ 1066 result = strcmp(tgt1->target_name, tgt2->target_name); 1067 if (result < 0) { 1068 return (-1); 1069 } else if (result > 0) { 1070 return (1); 1071 } 1072 1073 return (0); 1074 } 1075 1076 1077 iscsit_tpgt_t * 1078 iscsit_tgt_lookup_tpgt(iscsit_tgt_t *tgt, uint16_t tag) 1079 { 1080 iscsit_tpgt_t *result; 1081 1082 mutex_enter(&tgt->target_mutex); 1083 result = iscsit_tgt_lookup_tpgt_locked(tgt, tag); 1084 mutex_exit(&tgt->target_mutex); 1085 1086 return (result); 1087 } 1088 1089 static iscsit_tpgt_t * 1090 iscsit_tgt_lookup_tpgt_locked(iscsit_tgt_t *tgt, uint16_t tag) 1091 { 1092 iscsit_tpgt_t tmp_tpgt; 1093 iscsit_tpgt_t *result; 1094 1095 /* Caller holds tgt->target_mutex */ 1096 tmp_tpgt.tpgt_tag = tag; 1097 if ((result = avl_find(&tgt->target_tpgt_list, &tmp_tpgt, NULL)) != 1098 NULL) { 1099 iscsit_tpgt_hold(result); 1100 } 1101 1102 return (result); 1103 } 1104 1105 iscsit_portal_t * 1106 iscsit_tgt_lookup_portal(iscsit_tgt_t *tgt, struct sockaddr_storage *sa, 1107 iscsit_tpgt_t **output_tpgt) 1108 { 1109 iscsit_tpgt_t *tpgt; 1110 iscsit_portal_t *portal; 1111 1112 /* Caller holds tgt->target_mutex */ 1113 ASSERT(mutex_owned(&tgt->target_mutex)); 1114 for (tpgt = avl_first(&tgt->target_tpgt_list); 1115 tpgt != NULL; 1116 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1117 portal = iscsit_tpg_portal_lookup(tpgt->tpgt_tpg, sa); 1118 if (portal) { 1119 iscsit_tpgt_hold(tpgt); 1120 *output_tpgt = tpgt; 1121 return (portal); 1122 } 1123 } 1124 1125 return (NULL); 1126 } 1127 1128 1129 void 1130 iscsit_tgt_bind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess) 1131 { 1132 if (tgt) { 1133 sess->ist_lport = tgt->target_stmf_lport; 1134 iscsit_tgt_hold(tgt); 1135 idm_refcnt_hold(&tgt->target_sess_refcnt); 1136 mutex_enter(&tgt->target_mutex); 1137 avl_add(&tgt->target_sess_list, sess); 1138 mutex_exit(&tgt->target_mutex); 1139 } else { 1140 /* Discovery session */ 1141 sess->ist_lport = NULL; 1142 ISCSIT_GLOBAL_LOCK(RW_WRITER); 1143 avl_add(&iscsit_global.global_discovery_sessions, sess); 1144 ISCSIT_GLOBAL_UNLOCK(); 1145 } 1146 } 1147 1148 void 1149 iscsit_tgt_unbind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess) 1150 { 1151 if (tgt) { 1152 mutex_enter(&tgt->target_mutex); 1153 avl_remove(&tgt->target_sess_list, sess); 1154 mutex_exit(&tgt->target_mutex); 1155 sess->ist_tgt = (iscsit_tgt_t *)SESS_UNBOUND_FROM_TGT; 1156 idm_refcnt_rele(&tgt->target_sess_refcnt); 1157 iscsit_tgt_rele(tgt); 1158 } else { 1159 /* Discovery session */ 1160 ISCSIT_GLOBAL_LOCK(RW_WRITER); 1161 avl_remove(&iscsit_global.global_discovery_sessions, sess); 1162 ISCSIT_GLOBAL_UNLOCK(); 1163 } 1164 } 1165 1166 #define LOCK_FOR_SESS_LOOKUP(lookup_tgt) { \ 1167 if ((lookup_tgt) == NULL) { \ 1168 ISCSIT_GLOBAL_LOCK(RW_READER); \ 1169 } else { \ 1170 mutex_enter(&(lookup_tgt)->target_mutex); \ 1171 } \ 1172 } 1173 1174 #define UNLOCK_FOR_SESS_LOOKUP(lookup_tgt) { \ 1175 if ((lookup_tgt) == NULL) { \ 1176 ISCSIT_GLOBAL_UNLOCK(); \ 1177 } else { \ 1178 mutex_exit(&(lookup_tgt)->target_mutex); \ 1179 } \ 1180 } 1181 1182 iscsit_sess_t * 1183 iscsit_tgt_lookup_sess(iscsit_tgt_t *tgt, char *initiator_name, 1184 uint8_t *isid, uint16_t tsih, uint16_t tag) 1185 { 1186 iscsit_sess_t tmp_sess; 1187 avl_tree_t *sess_avl; 1188 avl_index_t where; 1189 iscsit_sess_t *result; 1190 1191 /* 1192 * If tgt is NULL then we are looking for a discovery session 1193 */ 1194 if (tgt == NULL) { 1195 sess_avl = &iscsit_global.global_discovery_sessions; 1196 } else { 1197 sess_avl = &tgt->target_sess_list; 1198 } 1199 1200 LOCK_FOR_SESS_LOOKUP(tgt); 1201 if (avl_numnodes(sess_avl) == NULL) { 1202 UNLOCK_FOR_SESS_LOOKUP(tgt); 1203 return (NULL); 1204 } 1205 1206 /* 1207 * We'll try to find a session matching ISID + TSIH first. If we 1208 * can't find one then we will return the closest match. If the 1209 * caller needs an exact match it must compare the TSIH after 1210 * the session is returned. 1211 * 1212 * The reason we do this "fuzzy matching" is to allow matching 1213 * sessions with different TSIH values on the same AVL list. This 1214 * makes session reinstatement much easier since the new session can 1215 * live on the list at the same time as the old session is cleaning up. 1216 */ 1217 bcopy(isid, tmp_sess.ist_isid, ISCSI_ISID_LEN); 1218 tmp_sess.ist_initiator_name = initiator_name; 1219 tmp_sess.ist_tsih = tsih; 1220 tmp_sess.ist_tpgt_tag = tag; 1221 1222 result = avl_find(sess_avl, &tmp_sess, &where); 1223 if (result != NULL) { 1224 iscsit_sess_hold(result); 1225 UNLOCK_FOR_SESS_LOOKUP(tgt); 1226 return (result); 1227 } 1228 1229 /* 1230 * avl_find_nearest() may return a result with a different ISID so 1231 * we should only return a result if the name and ISID match 1232 */ 1233 result = avl_nearest(sess_avl, where, AVL_BEFORE); 1234 if ((result != NULL) && 1235 (strcmp(result->ist_initiator_name, initiator_name) == 0) && 1236 (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) && 1237 (result->ist_tpgt_tag == tag)) { 1238 iscsit_sess_hold(result); 1239 UNLOCK_FOR_SESS_LOOKUP(tgt); 1240 return (result); 1241 } 1242 1243 result = avl_nearest(sess_avl, where, AVL_AFTER); 1244 if ((result != NULL) && 1245 (strcmp(result->ist_initiator_name, initiator_name) == 0) && 1246 (memcmp(result->ist_isid, isid, ISCSI_ISID_LEN) == 0) && 1247 (result->ist_tpgt_tag == tag)) { 1248 iscsit_sess_hold(result); 1249 UNLOCK_FOR_SESS_LOOKUP(tgt); 1250 return (result); 1251 } 1252 1253 UNLOCK_FOR_SESS_LOOKUP(tgt); 1254 1255 return (NULL); 1256 } 1257 1258 static idm_status_t 1259 iscsit_tgt_merge_tpgt(iscsit_tgt_t *tgt, it_tgt_t *cfg_tgt, 1260 list_t *tpgt_del_list) 1261 { 1262 iscsit_tpgt_t *tpgt, *next_tpgt; 1263 it_tpgt_t *cfg_tpgt; 1264 1265 /* 1266 * 1. >> Lock << 1267 * 2. Removing all objects and place on a temp list 1268 * 3. Add new objects 1269 * 4. >> Unlock << 1270 * 5. tpgt_del_list contains deleted objects 1271 */ 1272 ASSERT(avl_is_empty(&tgt->target_tpgt_list) || 1273 (tpgt_del_list != NULL)); 1274 1275 if (tpgt_del_list) { 1276 for (tpgt = avl_first(&tgt->target_tpgt_list); 1277 tpgt != NULL; tpgt = next_tpgt) { 1278 next_tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt); 1279 avl_remove(&tgt->target_tpgt_list, tpgt); 1280 if (tgt->target_state == TS_STMF_ONLINE) { 1281 tpgt->tpgt_needs_tpg_offline = B_TRUE; 1282 } 1283 list_insert_tail(tpgt_del_list, tpgt); 1284 } 1285 } 1286 1287 if (cfg_tgt->tgt_tpgt_list == NULL) { 1288 tpgt = iscsit_tpgt_create_default(); 1289 if (tgt->target_state == TS_STMF_ONLINE) { 1290 (void) iscsit_tpg_online(tpgt->tpgt_tpg); 1291 } 1292 avl_add(&tgt->target_tpgt_list, tpgt); 1293 } else { 1294 /* Add currently defined TPGTs */ 1295 for (cfg_tpgt = cfg_tgt->tgt_tpgt_list; 1296 cfg_tpgt != NULL; 1297 cfg_tpgt = cfg_tpgt->tpgt_next) { 1298 tpgt = iscsit_tpgt_create(cfg_tpgt); 1299 if (tpgt == NULL) 1300 goto tpgt_merge_error; 1301 if (tgt->target_state == TS_STMF_ONLINE) { 1302 (void) iscsit_tpg_online(tpgt->tpgt_tpg); 1303 } 1304 avl_add(&tgt->target_tpgt_list, tpgt); 1305 } 1306 } 1307 1308 return (IDM_STATUS_SUCCESS); 1309 1310 tpgt_merge_error: 1311 /* 1312 * This is a problem in configuration we received so it should never 1313 * happen. That's good because we're probably no longer in a state 1314 * requested by the user -- all the unbind operations have already 1315 * taken place. 1316 */ 1317 return (IDM_STATUS_FAIL); 1318 } 1319 1320 static iscsit_tpgt_t * 1321 iscsit_tpgt_create(it_tpgt_t *cfg_tpgt) 1322 { 1323 iscsit_tpg_t *tpg; 1324 iscsit_tpgt_t *result; 1325 1326 /* This takes a reference on the TPG */ 1327 tpg = iscsit_tpg_lookup_locked(cfg_tpgt->tpgt_tpg_name); 1328 if (tpg == NULL) 1329 return (NULL); 1330 1331 result = kmem_zalloc(sizeof (*result), KM_SLEEP); 1332 1333 result->tpgt_tpg = tpg; 1334 result->tpgt_tag = cfg_tpgt->tpgt_tag; 1335 1336 return (result); 1337 } 1338 1339 iscsit_tpgt_t * 1340 iscsit_tpgt_create_default() 1341 { 1342 iscsit_tpgt_t *result; 1343 1344 result = kmem_zalloc(sizeof (*result), KM_SLEEP); 1345 1346 result->tpgt_tpg = iscsit_global.global_default_tpg; 1347 iscsit_tpg_hold(result->tpgt_tpg); 1348 result->tpgt_tag = ISCSIT_DEFAULT_TPGT; 1349 1350 return (result); 1351 } 1352 1353 void 1354 iscsit_tpgt_destroy(iscsit_tpgt_t *tpgt) 1355 { 1356 if (tpgt->tpgt_needs_tpg_offline) { 1357 iscsit_tpg_offline(tpgt->tpgt_tpg); 1358 } 1359 iscsit_tpg_rele(tpgt->tpgt_tpg); 1360 kmem_free(tpgt, sizeof (*tpgt)); 1361 } 1362 1363 void 1364 iscsit_tpgt_hold(iscsit_tpgt_t *tpgt) 1365 { 1366 idm_refcnt_hold(&tpgt->tpgt_refcnt); 1367 } 1368 1369 void 1370 iscsit_tpgt_rele(iscsit_tpgt_t *tpgt) 1371 { 1372 idm_refcnt_rele(&tpgt->tpgt_refcnt); 1373 } 1374 1375 int 1376 iscsit_tpgt_avl_compare(const void *void_tpgt1, const void *void_tpgt2) 1377 { 1378 const iscsit_tpgt_t *tpgt1 = void_tpgt1; 1379 const iscsit_tpgt_t *tpgt2 = void_tpgt2; 1380 1381 if (tpgt1->tpgt_tag < tpgt2->tpgt_tag) 1382 return (-1); 1383 else if (tpgt1->tpgt_tag > tpgt2->tpgt_tag) 1384 return (1); 1385 1386 return (0); 1387 } 1388 1389 static idm_status_t 1390 iscsit_tgt_online(iscsit_tgt_t *tgt) 1391 { 1392 iscsit_tpgt_t *tpgt, *tpgt_fail; 1393 idm_status_t rc; 1394 1395 mutex_enter(&tgt->target_mutex); 1396 1397 ASSERT(tgt->target_sess_list.avl_numnodes == 0); 1398 idm_refcnt_reset(&tgt->target_sess_refcnt); 1399 for (tpgt = avl_first(&tgt->target_tpgt_list); 1400 tpgt != NULL; 1401 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1402 rc = iscsit_tpg_online(tpgt->tpgt_tpg); 1403 if (rc != IDM_STATUS_SUCCESS) { 1404 tpgt_fail = tpgt; 1405 goto tgt_online_fail; 1406 } 1407 } 1408 1409 mutex_exit(&tgt->target_mutex); 1410 1411 return (IDM_STATUS_SUCCESS); 1412 1413 tgt_online_fail: 1414 /* Offline all the tpgs we successfully onlined up to the failure */ 1415 for (tpgt = avl_first(&tgt->target_tpgt_list); 1416 tpgt != tpgt_fail; 1417 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1418 iscsit_tpg_offline(tpgt->tpgt_tpg); 1419 } 1420 mutex_exit(&tgt->target_mutex); 1421 return (rc); 1422 } 1423 1424 static void 1425 iscsit_tgt_offline_cb(void *tgt_void) 1426 { 1427 iscsit_tgt_t *tgt = tgt_void; 1428 stmf_change_status_t scs; 1429 1430 iscsit_tgt_sm_event(tgt, TE_OFFLINE_COMPLETE); 1431 1432 scs.st_completion_status = STMF_SUCCESS; 1433 scs.st_additional_info = NULL; 1434 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, 1435 tgt->target_stmf_lport, &scs); 1436 } 1437 1438 static void 1439 iscsit_tgt_offline(iscsit_tgt_t *tgt) 1440 { 1441 iscsit_tpgt_t *tpgt; 1442 iscsit_sess_t *ist; 1443 1444 mutex_enter(&tgt->target_mutex); 1445 1446 /* Offline target portal groups */ 1447 for (tpgt = avl_first(&tgt->target_tpgt_list); 1448 tpgt != NULL; 1449 tpgt = AVL_NEXT(&tgt->target_tpgt_list, tpgt)) { 1450 iscsit_tpg_offline(tpgt->tpgt_tpg); 1451 } 1452 1453 /* Close any active sessions */ 1454 for (ist = avl_first(&tgt->target_sess_list); 1455 ist != NULL; 1456 ist = AVL_NEXT(&tgt->target_sess_list, ist)) { 1457 /* 1458 * This is not a synchronous operation but after all 1459 * sessions have been cleaned up there will be no 1460 * more session-related holds on the target. 1461 */ 1462 iscsit_sess_close(ist); 1463 } 1464 1465 mutex_exit(&tgt->target_mutex); 1466 1467 /* 1468 * Wait for all the sessions to quiesce. 1469 */ 1470 idm_refcnt_async_wait_ref(&tgt->target_sess_refcnt, 1471 &iscsit_tgt_offline_cb); 1472 } 1473 1474 it_cfg_status_t 1475 iscsit_config_merge_tpg(it_config_t *cfg, list_t *tpg_del_list) 1476 { 1477 it_tpg_t *cfg_tpg; 1478 iscsit_tpg_t *tpg, *next_tpg; 1479 1480 /* 1481 * 1. >> Lock << 1482 * 2. Removing deleted objects and place on a temp list 1483 * 3. Add new objects 1484 * 4. >> Unlock << 1485 * 5. tpg_del_list contains objects to destroy 1486 */ 1487 for (tpg = avl_first(&iscsit_global.global_tpg_list); 1488 tpg != NULL; 1489 tpg = next_tpg) { 1490 next_tpg = AVL_NEXT(&iscsit_global.global_tpg_list, tpg); 1491 1492 if (it_tpg_lookup(cfg, tpg->tpg_name) == NULL) { 1493 /* 1494 * The policy around when to allow a target portal 1495 * group to be deleted is implemented in libiscsit. 1496 * By the time the request gets to the kernel module 1497 * we expect that it conforms to policy so we will 1498 * cleanup all references to TPG and destroy it if it 1499 * is possible to do so. 1500 * 1501 */ 1502 avl_remove(&iscsit_global.global_tpg_list, tpg); 1503 list_insert_tail(tpg_del_list, tpg); 1504 } 1505 } 1506 1507 /* Now walk through the list of configured target portal groups */ 1508 for (cfg_tpg = cfg->config_tpg_list; 1509 cfg_tpg != NULL; 1510 cfg_tpg = cfg_tpg->tpg_next) { 1511 /* See if we have an existing target portal group */ 1512 tpg = iscsit_tpg_lookup_locked(cfg_tpg->tpg_name); 1513 1514 if (tpg == NULL) { 1515 tpg = iscsit_tpg_create(cfg_tpg); 1516 ASSERT(tpg != NULL); 1517 avl_add(&iscsit_global.global_tpg_list, tpg); 1518 } else { 1519 mutex_enter(&tpg->tpg_mutex); 1520 iscsit_tpg_modify(tpg, cfg_tpg); 1521 mutex_exit(&tpg->tpg_mutex); 1522 iscsit_tpg_rele(tpg); 1523 } 1524 } 1525 1526 return (ITCFG_SUCCESS); 1527 } 1528 1529 1530 void 1531 iscsit_config_destroy_tpgs(list_t *tpg_del_list) 1532 { 1533 iscsit_tpg_t *tpg, *next_tpg; 1534 1535 /* Now finish destroying the target portal groups */ 1536 for (tpg = list_head(tpg_del_list); 1537 tpg != NULL; 1538 tpg = next_tpg) { 1539 next_tpg = list_next(tpg_del_list, tpg); 1540 list_remove(tpg_del_list, tpg); 1541 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1542 1543 /* Kill it */ 1544 iscsit_tpg_destroy(tpg); 1545 } 1546 } 1547 1548 iscsit_tpg_t * 1549 iscsit_tpg_lookup(char *tpg_name) 1550 { 1551 iscsit_tpg_t *result; 1552 1553 ISCSIT_GLOBAL_LOCK(RW_READER); 1554 result = iscsit_tpg_lookup_locked(tpg_name); 1555 ISCSIT_GLOBAL_UNLOCK(); 1556 1557 return (result); 1558 } 1559 1560 static iscsit_tpg_t * 1561 iscsit_tpg_lookup_locked(char *tpg_name) 1562 { 1563 iscsit_tpg_t tmp_tpg; 1564 iscsit_tpg_t *result; 1565 1566 (void) strlcpy(tmp_tpg.tpg_name, tpg_name, MAX_ISCSI_NODENAMELEN); 1567 if ((result = avl_find(&iscsit_global.global_tpg_list, 1568 &tmp_tpg, NULL)) != NULL) { 1569 iscsit_tpg_hold(result); 1570 } 1571 1572 return (result); 1573 } 1574 1575 iscsit_tpg_t * 1576 iscsit_tpg_create(it_tpg_t *cfg_tpg) 1577 { 1578 iscsit_tpg_t *tpg; 1579 1580 tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP); 1581 1582 mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL); 1583 (void) strlcpy(tpg->tpg_name, cfg_tpg->tpg_name, MAX_TPG_NAMELEN); 1584 avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare, 1585 sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln)); 1586 idm_refcnt_init(&tpg->tpg_refcnt, tpg); 1587 1588 mutex_enter(&tpg->tpg_mutex); 1589 iscsit_tpg_modify(tpg, cfg_tpg); 1590 mutex_exit(&tpg->tpg_mutex); 1591 iscsit_global_hold(); 1592 1593 return (tpg); 1594 } 1595 1596 static void 1597 iscsit_tpg_modify(iscsit_tpg_t *tpg, it_tpg_t *cfg_tpg) 1598 { 1599 iscsit_portal_t *portal, *next_portal; 1600 it_portal_t *cfg_portal; 1601 1602 /* Update portals */ 1603 for (portal = avl_first(&tpg->tpg_portal_list); 1604 portal != NULL; 1605 portal = next_portal) { 1606 next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal); 1607 if (it_portal_lookup(cfg_tpg, &portal->portal_addr) == NULL) { 1608 avl_remove(&tpg->tpg_portal_list, portal); 1609 iscsit_portal_destroy(portal); 1610 } 1611 } 1612 1613 for (cfg_portal = cfg_tpg->tpg_portal_list; 1614 cfg_portal != NULL; 1615 cfg_portal = cfg_portal->next) { 1616 if ((portal = iscsit_tpg_portal_lookup_locked(tpg, 1617 &cfg_portal->portal_addr)) == NULL) { 1618 (void) iscsit_portal_create(tpg, 1619 &cfg_portal->portal_addr); 1620 } else { 1621 iscsit_portal_rele(portal); 1622 } 1623 } 1624 } 1625 1626 void 1627 iscsit_tpg_destroy(iscsit_tpg_t *tpg) 1628 { 1629 iscsit_portal_t *portal, *next_portal; 1630 1631 for (portal = avl_first(&tpg->tpg_portal_list); 1632 portal != NULL; 1633 portal = next_portal) { 1634 next_portal = AVL_NEXT(&tpg->tpg_portal_list, portal); 1635 avl_remove(&tpg->tpg_portal_list, portal); 1636 iscsit_portal_destroy(portal); 1637 } 1638 1639 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1640 idm_refcnt_destroy(&tpg->tpg_refcnt); 1641 avl_destroy(&tpg->tpg_portal_list); 1642 mutex_destroy(&tpg->tpg_mutex); 1643 kmem_free(tpg, sizeof (*tpg)); 1644 iscsit_global_rele(); 1645 } 1646 1647 void 1648 iscsit_tpg_hold(iscsit_tpg_t *tpg) 1649 { 1650 idm_refcnt_hold(&tpg->tpg_refcnt); 1651 } 1652 1653 void 1654 iscsit_tpg_rele(iscsit_tpg_t *tpg) 1655 { 1656 idm_refcnt_rele(&tpg->tpg_refcnt); 1657 } 1658 1659 iscsit_tpg_t * 1660 iscsit_tpg_createdefault() 1661 { 1662 iscsit_tpg_t *tpg; 1663 1664 tpg = kmem_zalloc(sizeof (*tpg), KM_SLEEP); 1665 1666 mutex_init(&tpg->tpg_mutex, NULL, MUTEX_DEFAULT, NULL); 1667 (void) strlcpy(tpg->tpg_name, ISCSIT_DEFAULT_TPG, MAX_TPG_NAMELEN); 1668 avl_create(&tpg->tpg_portal_list, iscsit_portal_avl_compare, 1669 sizeof (iscsit_portal_t), offsetof(iscsit_portal_t, portal_tpg_ln)); 1670 idm_refcnt_init(&tpg->tpg_refcnt, tpg); 1671 1672 /* Now create default portal */ 1673 if (iscsit_portal_create(tpg, NULL) == NULL) { 1674 iscsit_tpg_destroy(tpg); 1675 return (NULL); 1676 } 1677 1678 return (tpg); 1679 } 1680 1681 void 1682 iscsit_tpg_destroydefault(iscsit_tpg_t *tpg) 1683 { 1684 iscsit_portal_t *portal; 1685 1686 portal = avl_first(&tpg->tpg_portal_list); 1687 ASSERT(portal != NULL); 1688 avl_remove(&tpg->tpg_portal_list, portal); 1689 iscsit_portal_destroy(portal); 1690 1691 idm_refcnt_wait_ref(&tpg->tpg_refcnt); 1692 idm_refcnt_destroy(&tpg->tpg_refcnt); 1693 avl_destroy(&tpg->tpg_portal_list); 1694 mutex_destroy(&tpg->tpg_mutex); 1695 kmem_free(tpg, sizeof (*tpg)); 1696 } 1697 1698 int 1699 iscsit_tpg_avl_compare(const void *void_tpg1, const void *void_tpg2) 1700 { 1701 const iscsit_tpg_t *tpg1 = void_tpg1; 1702 const iscsit_tpg_t *tpg2 = void_tpg2; 1703 int result; 1704 1705 /* 1706 * Sort by ISID first then TSIH 1707 */ 1708 result = strcmp(tpg1->tpg_name, tpg2->tpg_name); 1709 if (result < 0) { 1710 return (-1); 1711 } else if (result > 0) { 1712 return (1); 1713 } 1714 1715 return (0); 1716 } 1717 1718 idm_status_t 1719 iscsit_tpg_online(iscsit_tpg_t *tpg) 1720 { 1721 iscsit_portal_t *portal, *portal_fail; 1722 idm_status_t rc; 1723 1724 mutex_enter(&tpg->tpg_mutex); 1725 if (tpg->tpg_online == 0) { 1726 for (portal = avl_first(&tpg->tpg_portal_list); 1727 portal != NULL; 1728 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1729 rc = iscsit_portal_online(portal); 1730 if (rc != IDM_STATUS_SUCCESS) { 1731 portal_fail = portal; 1732 goto tpg_online_fail; 1733 } 1734 } 1735 } 1736 tpg->tpg_online++; 1737 1738 mutex_exit(&tpg->tpg_mutex); 1739 return (IDM_STATUS_SUCCESS); 1740 1741 tpg_online_fail: 1742 /* Offline all the portals we successfully onlined up to the failure */ 1743 for (portal = avl_first(&tpg->tpg_portal_list); 1744 portal != portal_fail; 1745 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1746 iscsit_portal_offline(portal); 1747 } 1748 mutex_exit(&tpg->tpg_mutex); 1749 return (rc); 1750 } 1751 1752 void 1753 iscsit_tpg_offline(iscsit_tpg_t *tpg) 1754 { 1755 iscsit_portal_t *portal; 1756 1757 mutex_enter(&tpg->tpg_mutex); 1758 tpg->tpg_online--; 1759 if (tpg->tpg_online == 0) { 1760 for (portal = avl_first(&tpg->tpg_portal_list); 1761 portal != NULL; 1762 portal = AVL_NEXT(&tpg->tpg_portal_list, portal)) { 1763 iscsit_portal_offline(portal); 1764 } 1765 } 1766 mutex_exit(&tpg->tpg_mutex); 1767 } 1768 1769 iscsit_portal_t * 1770 iscsit_tpg_portal_lookup(iscsit_tpg_t *tpg, struct sockaddr_storage *sa) 1771 { 1772 iscsit_portal_t *result; 1773 1774 mutex_enter(&tpg->tpg_mutex); 1775 result = iscsit_tpg_portal_lookup_locked(tpg, sa); 1776 mutex_exit(&tpg->tpg_mutex); 1777 1778 return (result); 1779 } 1780 1781 static iscsit_portal_t * 1782 iscsit_tpg_portal_lookup_locked(iscsit_tpg_t *tpg, 1783 struct sockaddr_storage *sa) 1784 { 1785 iscsit_portal_t tmp_portal; 1786 iscsit_portal_t *result; 1787 1788 /* Caller holds tpg->tpg_mutex */ 1789 bcopy(sa, &tmp_portal.portal_addr, sizeof (*sa)); 1790 if ((result = avl_find(&tpg->tpg_portal_list, &tmp_portal, NULL)) != 1791 NULL) { 1792 iscsit_portal_hold(result); 1793 } 1794 1795 return (result); 1796 } 1797 1798 iscsit_portal_t * 1799 iscsit_portal_create(iscsit_tpg_t *tpg, struct sockaddr_storage *sa) 1800 { 1801 iscsit_portal_t *portal; 1802 1803 portal = kmem_zalloc(sizeof (*portal), KM_SLEEP); 1804 /* 1805 * If (sa == NULL) then we are being asked to create the default 1806 * portal -- targets will use this portal when no portals are 1807 * explicitly configured. 1808 */ 1809 if (sa == NULL) { 1810 portal->portal_default = B_TRUE; 1811 } else { 1812 portal->portal_default = B_FALSE; 1813 bcopy(sa, &portal->portal_addr, sizeof (*sa)); 1814 } 1815 1816 idm_refcnt_init(&portal->portal_refcnt, portal); 1817 1818 /* 1819 * Add this portal to the list 1820 */ 1821 avl_add(&tpg->tpg_portal_list, portal); 1822 1823 return (portal); 1824 } 1825 1826 void 1827 iscsit_portal_destroy(iscsit_portal_t *portal) 1828 { 1829 ASSERT(portal->portal_svc == NULL); 1830 idm_refcnt_destroy(&portal->portal_refcnt); 1831 kmem_free(portal, sizeof (*portal)); 1832 } 1833 1834 void 1835 iscsit_portal_hold(iscsit_portal_t *portal) 1836 { 1837 idm_refcnt_hold(&portal->portal_refcnt); 1838 } 1839 1840 void 1841 iscsit_portal_rele(iscsit_portal_t *portal) 1842 { 1843 idm_refcnt_rele(&portal->portal_refcnt); 1844 } 1845 1846 int 1847 iscsit_portal_avl_compare(const void *void_portal1, const void *void_portal2) 1848 { 1849 const iscsit_portal_t *portal1 = void_portal1; 1850 const iscsit_portal_t *portal2 = void_portal2; 1851 const struct sockaddr_storage *ss1, *ss2; 1852 const struct in_addr *in1, *in2; 1853 const struct in6_addr *in61, *in62; 1854 int i; 1855 1856 /* 1857 * Compare ports, then address family, then ip address 1858 */ 1859 ss1 = &portal1->portal_addr; 1860 ss2 = &portal2->portal_addr; 1861 if (((struct sockaddr_in *)ss1)->sin_port != 1862 ((struct sockaddr_in *)ss2)->sin_port) { 1863 if (((struct sockaddr_in *)ss1)->sin_port > 1864 ((struct sockaddr_in *)ss2)->sin_port) 1865 return (1); 1866 else 1867 return (-1); 1868 } 1869 1870 /* 1871 * ports are the same 1872 */ 1873 if (ss1->ss_family != ss2->ss_family) { 1874 if (ss1->ss_family == AF_INET) 1875 return (1); 1876 else 1877 return (-1); 1878 } 1879 /* 1880 * address families are the same 1881 */ 1882 if (ss1->ss_family == AF_INET) { 1883 in1 = &((struct sockaddr_in *)ss1)->sin_addr; 1884 in2 = &((struct sockaddr_in *)ss2)->sin_addr; 1885 1886 if (in1->s_addr > in2->s_addr) 1887 return (1); 1888 else if (in1->s_addr < in2->s_addr) 1889 return (-1); 1890 else 1891 return (0); 1892 } else if (ss1->ss_family == AF_INET6) { 1893 in61 = &((struct sockaddr_in6 *)ss1)->sin6_addr; 1894 in62 = &((struct sockaddr_in6 *)ss2)->sin6_addr; 1895 1896 for (i = 0; i < 4; i++) { 1897 if (in61->s6_addr32[i] > in62->s6_addr32[i]) 1898 return (1); 1899 else if (in61->s6_addr32[i] < in62->s6_addr32[i]) 1900 return (-1); 1901 } 1902 return (0); 1903 } else 1904 cmn_err(CE_WARN, 1905 "iscsit_portal_avl_compare: unknown ss_family %d", 1906 ss1->ss_family); 1907 1908 return (1); 1909 } 1910 1911 1912 idm_status_t 1913 iscsit_portal_online(iscsit_portal_t *portal) 1914 { 1915 idm_status_t rc; 1916 idm_svc_t *svc; 1917 idm_svc_req_t sr; 1918 uint16_t port; 1919 struct sockaddr_in *sin; 1920 1921 /* Caller holds parent TPG mutex */ 1922 if (portal->portal_online == 0) { 1923 /* 1924 * If there is no existing IDM service instance for this port, 1925 * create one. If the service exists, then the lookup, 1926 * creates a reference on the existing service. 1927 */ 1928 sin = (struct sockaddr_in *)&portal->portal_addr; 1929 port = ntohs(sin->sin_port); 1930 if (port == 0) 1931 port = ISCSI_LISTEN_PORT; 1932 ASSERT(portal->portal_svc == NULL); 1933 if ((svc = idm_tgt_svc_lookup(port)) == NULL) { 1934 sr.sr_port = port; 1935 sr.sr_li = iscsit_global.global_li; 1936 sr.sr_conn_ops.icb_rx_scsi_cmd = &iscsit_op_scsi_cmd; 1937 sr.sr_conn_ops.icb_rx_scsi_rsp = NULL; 1938 sr.sr_conn_ops.icb_rx_misc = &iscsit_rx_pdu; 1939 sr.sr_conn_ops.icb_rx_error = &iscsit_rx_pdu_error; 1940 sr.sr_conn_ops.icb_task_aborted = &iscsit_task_aborted; 1941 sr.sr_conn_ops.icb_client_notify = 1942 &iscsit_client_notify; 1943 sr.sr_conn_ops.icb_build_hdr = &iscsit_build_hdr; 1944 sr.sr_conn_ops.icb_keepalive = &iscsit_keepalive; 1945 1946 if (idm_tgt_svc_create(&sr, &svc) != 1947 IDM_STATUS_SUCCESS) { 1948 return (IDM_STATUS_FAIL); 1949 } 1950 1951 /* Get reference on the service we just created */ 1952 idm_tgt_svc_hold(svc); 1953 } 1954 if ((rc = idm_tgt_svc_online(svc)) != IDM_STATUS_SUCCESS) { 1955 idm_tgt_svc_rele_and_destroy(svc); 1956 return (IDM_STATUS_FAIL); 1957 } 1958 portal->portal_svc = svc; 1959 1960 /* 1961 * Only call iSNS for first online 1962 */ 1963 iscsit_isns_portal_online(portal); 1964 } 1965 1966 portal->portal_online++; 1967 1968 return (rc); 1969 } 1970 1971 void 1972 iscsit_portal_offline(iscsit_portal_t *portal) 1973 { 1974 portal->portal_online--; 1975 1976 if (portal->portal_online == 0) { 1977 /* 1978 * Only call iSNS for last offline 1979 */ 1980 iscsit_isns_portal_offline(portal); 1981 idm_tgt_svc_offline(portal->portal_svc); 1982 /* If service is unreferenced, destroy it too */ 1983 idm_tgt_svc_rele_and_destroy(portal->portal_svc); 1984 portal->portal_svc = NULL; 1985 } 1986 1987 } 1988 1989 it_cfg_status_t 1990 iscsit_config_merge_ini(it_config_t *cfg) 1991 { 1992 iscsit_ini_t *ini, *next_ini; 1993 it_ini_t *cfg_ini; 1994 1995 /* 1996 * Initiator objects are so simple we will just destroy all the current 1997 * objects and build new ones. Nothing should ever reference an 1998 * initator object.. instead just lookup the initiator object and 1999 * grab the properties while holding the global config lock. 2000 */ 2001 for (ini = avl_first(&iscsit_global.global_ini_list); 2002 ini != NULL; 2003 ini = next_ini) { 2004 next_ini = AVL_NEXT(&iscsit_global.global_ini_list, ini); 2005 avl_remove(&iscsit_global.global_ini_list, ini); 2006 nvlist_free(ini->ini_props); 2007 kmem_free(ini, sizeof (*ini)); 2008 iscsit_global_rele(); 2009 } 2010 2011 for (cfg_ini = cfg->config_ini_list; 2012 cfg_ini != NULL; 2013 cfg_ini = cfg_ini->ini_next) { 2014 ini = kmem_zalloc(sizeof (iscsit_ini_t), KM_SLEEP); 2015 (void) strlcpy(ini->ini_name, cfg_ini->ini_name, 2016 MAX_ISCSI_NODENAMELEN); 2017 (void) nvlist_dup(cfg_ini->ini_properties, &ini->ini_props, 2018 KM_SLEEP); 2019 avl_add(&iscsit_global.global_ini_list, ini); 2020 iscsit_global_hold(); 2021 } 2022 2023 return (ITCFG_SUCCESS); 2024 } 2025 2026 int 2027 iscsit_ini_avl_compare(const void *void_ini1, const void *void_ini2) 2028 { 2029 const iscsit_ini_t *ini1 = void_ini1; 2030 const iscsit_ini_t *ini2 = void_ini2; 2031 int result; 2032 2033 /* 2034 * Sort by ISID first then TSIH 2035 */ 2036 result = strcmp(ini1->ini_name, ini2->ini_name); 2037 if (result < 0) { 2038 return (-1); 2039 } else if (result > 0) { 2040 return (1); 2041 } 2042 2043 return (0); 2044 } 2045 2046 iscsit_ini_t * 2047 iscsit_ini_lookup_locked(char *ini_name) 2048 { 2049 iscsit_ini_t tmp_ini; 2050 iscsit_ini_t *result; 2051 2052 /* 2053 * Use a dummy target for lookup, filling in all fields used in AVL 2054 * comparison. 2055 */ 2056 (void) strlcpy(tmp_ini.ini_name, ini_name, MAX_ISCSI_NODENAMELEN); 2057 result = avl_find(&iscsit_global.global_ini_list, &tmp_ini, NULL); 2058 2059 return (result); 2060 } 2061