1 /******************************************************************************* 2 * Filename: target_core_fabric_configfs.c 3 * 4 * This file contains generic fabric module configfs infrastructure for 5 * TCM v4.x code 6 * 7 * (c) Copyright 2010-2013 Datera, Inc. 8 * 9 * Nicholas A. Bellinger <nab@linux-iscsi.org> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 ****************************************************************************/ 21 22 #include <linux/module.h> 23 #include <linux/moduleparam.h> 24 #include <linux/utsname.h> 25 #include <linux/init.h> 26 #include <linux/fs.h> 27 #include <linux/namei.h> 28 #include <linux/slab.h> 29 #include <linux/types.h> 30 #include <linux/delay.h> 31 #include <linux/unistd.h> 32 #include <linux/string.h> 33 #include <linux/syscalls.h> 34 #include <linux/configfs.h> 35 36 #include <target/target_core_base.h> 37 #include <target/target_core_fabric.h> 38 39 #include "target_core_internal.h" 40 #include "target_core_alua.h" 41 #include "target_core_pr.h" 42 43 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \ 44 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \ 45 { \ 46 struct config_item_type *cit = &tf->tf_##_name##_cit; \ 47 \ 48 cit->ct_item_ops = _item_ops; \ 49 cit->ct_group_ops = _group_ops; \ 50 cit->ct_attrs = _attrs; \ 51 cit->ct_owner = tf->tf_ops->module; \ 52 pr_debug("Setup generic %s\n", __stringify(_name)); \ 53 } 54 55 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \ 56 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \ 57 { \ 58 struct config_item_type *cit = &tf->tf_##_name##_cit; \ 59 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \ 60 \ 61 cit->ct_item_ops = _item_ops; \ 62 cit->ct_group_ops = _group_ops; \ 63 cit->ct_attrs = attrs; \ 64 cit->ct_owner = tf->tf_ops->module; \ 65 pr_debug("Setup generic %s\n", __stringify(_name)); \ 66 } 67 68 /* Start of tfc_tpg_mappedlun_cit */ 69 70 static int target_fabric_mappedlun_link( 71 struct config_item *lun_acl_ci, 72 struct config_item *lun_ci) 73 { 74 struct se_dev_entry *deve; 75 struct se_lun *lun = container_of(to_config_group(lun_ci), 76 struct se_lun, lun_group); 77 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci), 78 struct se_lun_acl, se_lun_group); 79 struct se_portal_group *se_tpg; 80 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s; 81 bool lun_access_ro; 82 83 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) { 84 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:" 85 " %p to struct lun: %p\n", lun_ci, lun); 86 return -EFAULT; 87 } 88 /* 89 * Ensure that the source port exists 90 */ 91 if (!lun->lun_se_dev) { 92 pr_err("Source se_lun->lun_se_dev does not exist\n"); 93 return -EINVAL; 94 } 95 se_tpg = lun->lun_tpg; 96 97 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item; 98 tpg_ci = &nacl_ci->ci_group->cg_item; 99 wwn_ci = &tpg_ci->ci_group->cg_item; 100 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item; 101 wwn_ci_s = &tpg_ci_s->ci_group->cg_item; 102 /* 103 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT 104 */ 105 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) { 106 pr_err("Illegal Initiator ACL SymLink outside of %s\n", 107 config_item_name(wwn_ci)); 108 return -EINVAL; 109 } 110 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) { 111 pr_err("Illegal Initiator ACL Symlink outside of %s" 112 " TPGT: %s\n", config_item_name(wwn_ci), 113 config_item_name(tpg_ci)); 114 return -EINVAL; 115 } 116 /* 117 * If this struct se_node_acl was dynamically generated with 118 * tpg_1/attrib/generate_node_acls=1, use the existing 119 * deve->lun_access_ro value, which will be true when 120 * tpg_1/attrib/demo_mode_write_protect=1 121 */ 122 rcu_read_lock(); 123 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun); 124 if (deve) 125 lun_access_ro = deve->lun_access_ro; 126 else 127 lun_access_ro = 128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect( 129 se_tpg)) ? true : false; 130 rcu_read_unlock(); 131 /* 132 * Determine the actual mapped LUN value user wants.. 133 * 134 * This value is what the SCSI Initiator actually sees the 135 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports. 136 */ 137 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro); 138 } 139 140 static int target_fabric_mappedlun_unlink( 141 struct config_item *lun_acl_ci, 142 struct config_item *lun_ci) 143 { 144 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci), 145 struct se_lun_acl, se_lun_group); 146 struct se_lun *lun = container_of(to_config_group(lun_ci), 147 struct se_lun, lun_group); 148 149 return core_dev_del_initiator_node_lun_acl(lun, lacl); 150 } 151 152 static struct se_lun_acl *item_to_lun_acl(struct config_item *item) 153 { 154 return container_of(to_config_group(item), struct se_lun_acl, 155 se_lun_group); 156 } 157 158 static ssize_t target_fabric_mappedlun_write_protect_show( 159 struct config_item *item, char *page) 160 { 161 struct se_lun_acl *lacl = item_to_lun_acl(item); 162 struct se_node_acl *se_nacl = lacl->se_lun_nacl; 163 struct se_dev_entry *deve; 164 ssize_t len = 0; 165 166 rcu_read_lock(); 167 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun); 168 if (deve) { 169 len = sprintf(page, "%d\n", deve->lun_access_ro); 170 } 171 rcu_read_unlock(); 172 173 return len; 174 } 175 176 static ssize_t target_fabric_mappedlun_write_protect_store( 177 struct config_item *item, const char *page, size_t count) 178 { 179 struct se_lun_acl *lacl = item_to_lun_acl(item); 180 struct se_node_acl *se_nacl = lacl->se_lun_nacl; 181 struct se_portal_group *se_tpg = se_nacl->se_tpg; 182 unsigned long wp; 183 int ret; 184 185 ret = kstrtoul(page, 0, &wp); 186 if (ret) 187 return ret; 188 189 if ((wp != 1) && (wp != 0)) 190 return -EINVAL; 191 192 /* wp=1 means lun_access_ro=true */ 193 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl); 194 195 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s" 196 " Mapped LUN: %llu Write Protect bit to %s\n", 197 se_tpg->se_tpg_tfo->get_fabric_name(), 198 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF"); 199 200 return count; 201 202 } 203 204 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect); 205 206 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = { 207 &target_fabric_mappedlun_attr_write_protect, 208 NULL, 209 }; 210 211 static void target_fabric_mappedlun_release(struct config_item *item) 212 { 213 struct se_lun_acl *lacl = container_of(to_config_group(item), 214 struct se_lun_acl, se_lun_group); 215 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg; 216 217 core_dev_free_initiator_node_lun_acl(se_tpg, lacl); 218 } 219 220 static struct configfs_item_operations target_fabric_mappedlun_item_ops = { 221 .release = target_fabric_mappedlun_release, 222 .allow_link = target_fabric_mappedlun_link, 223 .drop_link = target_fabric_mappedlun_unlink, 224 }; 225 226 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL, 227 target_fabric_mappedlun_attrs); 228 229 /* End of tfc_tpg_mappedlun_cit */ 230 231 /* Start of tfc_tpg_mappedlun_port_cit */ 232 233 static struct config_group *target_core_mappedlun_stat_mkdir( 234 struct config_group *group, 235 const char *name) 236 { 237 return ERR_PTR(-ENOSYS); 238 } 239 240 static void target_core_mappedlun_stat_rmdir( 241 struct config_group *group, 242 struct config_item *item) 243 { 244 return; 245 } 246 247 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = { 248 .make_group = target_core_mappedlun_stat_mkdir, 249 .drop_item = target_core_mappedlun_stat_rmdir, 250 }; 251 252 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops, 253 NULL); 254 255 /* End of tfc_tpg_mappedlun_port_cit */ 256 257 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL); 258 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL); 259 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL); 260 261 /* Start of tfc_tpg_nacl_base_cit */ 262 263 static struct config_group *target_fabric_make_mappedlun( 264 struct config_group *group, 265 const char *name) 266 { 267 struct se_node_acl *se_nacl = container_of(group, 268 struct se_node_acl, acl_group); 269 struct se_portal_group *se_tpg = se_nacl->se_tpg; 270 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 271 struct se_lun_acl *lacl = NULL; 272 char *buf; 273 unsigned long long mapped_lun; 274 int ret = 0; 275 276 buf = kzalloc(strlen(name) + 1, GFP_KERNEL); 277 if (!buf) { 278 pr_err("Unable to allocate memory for name buf\n"); 279 return ERR_PTR(-ENOMEM); 280 } 281 snprintf(buf, strlen(name) + 1, "%s", name); 282 /* 283 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID. 284 */ 285 if (strstr(buf, "lun_") != buf) { 286 pr_err("Unable to locate \"lun_\" from buf: %s" 287 " name: %s\n", buf, name); 288 ret = -EINVAL; 289 goto out; 290 } 291 /* 292 * Determine the Mapped LUN value. This is what the SCSI Initiator 293 * Port will actually see. 294 */ 295 ret = kstrtoull(buf + 4, 0, &mapped_lun); 296 if (ret) 297 goto out; 298 299 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl, 300 mapped_lun, &ret); 301 if (!lacl) { 302 ret = -EINVAL; 303 goto out; 304 } 305 306 config_group_init_type_name(&lacl->se_lun_group, name, 307 &tf->tf_tpg_mappedlun_cit); 308 309 config_group_init_type_name(&lacl->ml_stat_grps.stat_group, 310 "statistics", &tf->tf_tpg_mappedlun_stat_cit); 311 configfs_add_default_group(&lacl->ml_stat_grps.stat_group, 312 &lacl->se_lun_group); 313 314 target_stat_setup_mappedlun_default_groups(lacl); 315 316 kfree(buf); 317 return &lacl->se_lun_group; 318 out: 319 kfree(lacl); 320 kfree(buf); 321 return ERR_PTR(ret); 322 } 323 324 static void target_fabric_drop_mappedlun( 325 struct config_group *group, 326 struct config_item *item) 327 { 328 struct se_lun_acl *lacl = container_of(to_config_group(item), 329 struct se_lun_acl, se_lun_group); 330 331 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group); 332 configfs_remove_default_groups(&lacl->se_lun_group); 333 334 config_item_put(item); 335 } 336 337 static void target_fabric_nacl_base_release(struct config_item *item) 338 { 339 struct se_node_acl *se_nacl = container_of(to_config_group(item), 340 struct se_node_acl, acl_group); 341 342 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group); 343 core_tpg_del_initiator_node_acl(se_nacl); 344 } 345 346 static struct configfs_item_operations target_fabric_nacl_base_item_ops = { 347 .release = target_fabric_nacl_base_release, 348 }; 349 350 static struct configfs_group_operations target_fabric_nacl_base_group_ops = { 351 .make_group = target_fabric_make_mappedlun, 352 .drop_item = target_fabric_drop_mappedlun, 353 }; 354 355 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops, 356 &target_fabric_nacl_base_group_ops); 357 358 /* End of tfc_tpg_nacl_base_cit */ 359 360 /* Start of tfc_node_fabric_stats_cit */ 361 /* 362 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group 363 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[] 364 */ 365 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL); 366 367 /* End of tfc_wwn_fabric_stats_cit */ 368 369 /* Start of tfc_tpg_nacl_cit */ 370 371 static struct config_group *target_fabric_make_nodeacl( 372 struct config_group *group, 373 const char *name) 374 { 375 struct se_portal_group *se_tpg = container_of(group, 376 struct se_portal_group, tpg_acl_group); 377 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 378 struct se_node_acl *se_nacl; 379 380 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name); 381 if (IS_ERR(se_nacl)) 382 return ERR_CAST(se_nacl); 383 384 if (tf->tf_ops->fabric_init_nodeacl) { 385 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name); 386 if (ret) { 387 core_tpg_del_initiator_node_acl(se_nacl); 388 return ERR_PTR(ret); 389 } 390 } 391 392 config_group_init_type_name(&se_nacl->acl_group, name, 393 &tf->tf_tpg_nacl_base_cit); 394 395 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib", 396 &tf->tf_tpg_nacl_attrib_cit); 397 configfs_add_default_group(&se_nacl->acl_attrib_group, 398 &se_nacl->acl_group); 399 400 config_group_init_type_name(&se_nacl->acl_auth_group, "auth", 401 &tf->tf_tpg_nacl_auth_cit); 402 configfs_add_default_group(&se_nacl->acl_auth_group, 403 &se_nacl->acl_group); 404 405 config_group_init_type_name(&se_nacl->acl_param_group, "param", 406 &tf->tf_tpg_nacl_param_cit); 407 configfs_add_default_group(&se_nacl->acl_param_group, 408 &se_nacl->acl_group); 409 410 config_group_init_type_name(&se_nacl->acl_fabric_stat_group, 411 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit); 412 configfs_add_default_group(&se_nacl->acl_fabric_stat_group, 413 &se_nacl->acl_group); 414 415 return &se_nacl->acl_group; 416 } 417 418 static void target_fabric_drop_nodeacl( 419 struct config_group *group, 420 struct config_item *item) 421 { 422 struct se_node_acl *se_nacl = container_of(to_config_group(item), 423 struct se_node_acl, acl_group); 424 425 configfs_remove_default_groups(&se_nacl->acl_group); 426 427 /* 428 * struct se_node_acl free is done in target_fabric_nacl_base_release() 429 */ 430 config_item_put(item); 431 } 432 433 static struct configfs_group_operations target_fabric_nacl_group_ops = { 434 .make_group = target_fabric_make_nodeacl, 435 .drop_item = target_fabric_drop_nodeacl, 436 }; 437 438 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL); 439 440 /* End of tfc_tpg_nacl_cit */ 441 442 /* Start of tfc_tpg_np_base_cit */ 443 444 static void target_fabric_np_base_release(struct config_item *item) 445 { 446 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item), 447 struct se_tpg_np, tpg_np_group); 448 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent; 449 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 450 451 tf->tf_ops->fabric_drop_np(se_tpg_np); 452 } 453 454 static struct configfs_item_operations target_fabric_np_base_item_ops = { 455 .release = target_fabric_np_base_release, 456 }; 457 458 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL); 459 460 /* End of tfc_tpg_np_base_cit */ 461 462 /* Start of tfc_tpg_np_cit */ 463 464 static struct config_group *target_fabric_make_np( 465 struct config_group *group, 466 const char *name) 467 { 468 struct se_portal_group *se_tpg = container_of(group, 469 struct se_portal_group, tpg_np_group); 470 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 471 struct se_tpg_np *se_tpg_np; 472 473 if (!tf->tf_ops->fabric_make_np) { 474 pr_err("tf->tf_ops.fabric_make_np is NULL\n"); 475 return ERR_PTR(-ENOSYS); 476 } 477 478 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name); 479 if (!se_tpg_np || IS_ERR(se_tpg_np)) 480 return ERR_PTR(-EINVAL); 481 482 se_tpg_np->tpg_np_parent = se_tpg; 483 config_group_init_type_name(&se_tpg_np->tpg_np_group, name, 484 &tf->tf_tpg_np_base_cit); 485 486 return &se_tpg_np->tpg_np_group; 487 } 488 489 static void target_fabric_drop_np( 490 struct config_group *group, 491 struct config_item *item) 492 { 493 /* 494 * struct se_tpg_np is released via target_fabric_np_base_release() 495 */ 496 config_item_put(item); 497 } 498 499 static struct configfs_group_operations target_fabric_np_group_ops = { 500 .make_group = &target_fabric_make_np, 501 .drop_item = &target_fabric_drop_np, 502 }; 503 504 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL); 505 506 /* End of tfc_tpg_np_cit */ 507 508 /* Start of tfc_tpg_port_cit */ 509 510 static struct se_lun *item_to_lun(struct config_item *item) 511 { 512 return container_of(to_config_group(item), struct se_lun, 513 lun_group); 514 } 515 516 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item, 517 char *page) 518 { 519 struct se_lun *lun = item_to_lun(item); 520 521 if (!lun || !lun->lun_se_dev) 522 return -ENODEV; 523 524 return core_alua_show_tg_pt_gp_info(lun, page); 525 } 526 527 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item, 528 const char *page, size_t count) 529 { 530 struct se_lun *lun = item_to_lun(item); 531 532 if (!lun || !lun->lun_se_dev) 533 return -ENODEV; 534 535 return core_alua_store_tg_pt_gp_info(lun, page, count); 536 } 537 538 static ssize_t target_fabric_port_alua_tg_pt_offline_show( 539 struct config_item *item, char *page) 540 { 541 struct se_lun *lun = item_to_lun(item); 542 543 if (!lun || !lun->lun_se_dev) 544 return -ENODEV; 545 546 return core_alua_show_offline_bit(lun, page); 547 } 548 549 static ssize_t target_fabric_port_alua_tg_pt_offline_store( 550 struct config_item *item, const char *page, size_t count) 551 { 552 struct se_lun *lun = item_to_lun(item); 553 554 if (!lun || !lun->lun_se_dev) 555 return -ENODEV; 556 557 return core_alua_store_offline_bit(lun, page, count); 558 } 559 560 static ssize_t target_fabric_port_alua_tg_pt_status_show( 561 struct config_item *item, char *page) 562 { 563 struct se_lun *lun = item_to_lun(item); 564 565 if (!lun || !lun->lun_se_dev) 566 return -ENODEV; 567 568 return core_alua_show_secondary_status(lun, page); 569 } 570 571 static ssize_t target_fabric_port_alua_tg_pt_status_store( 572 struct config_item *item, const char *page, size_t count) 573 { 574 struct se_lun *lun = item_to_lun(item); 575 576 if (!lun || !lun->lun_se_dev) 577 return -ENODEV; 578 579 return core_alua_store_secondary_status(lun, page, count); 580 } 581 582 static ssize_t target_fabric_port_alua_tg_pt_write_md_show( 583 struct config_item *item, char *page) 584 { 585 struct se_lun *lun = item_to_lun(item); 586 587 if (!lun || !lun->lun_se_dev) 588 return -ENODEV; 589 590 return core_alua_show_secondary_write_metadata(lun, page); 591 } 592 593 static ssize_t target_fabric_port_alua_tg_pt_write_md_store( 594 struct config_item *item, const char *page, size_t count) 595 { 596 struct se_lun *lun = item_to_lun(item); 597 598 if (!lun || !lun->lun_se_dev) 599 return -ENODEV; 600 601 return core_alua_store_secondary_write_metadata(lun, page, count); 602 } 603 604 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp); 605 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline); 606 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status); 607 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md); 608 609 static struct configfs_attribute *target_fabric_port_attrs[] = { 610 &target_fabric_port_attr_alua_tg_pt_gp, 611 &target_fabric_port_attr_alua_tg_pt_offline, 612 &target_fabric_port_attr_alua_tg_pt_status, 613 &target_fabric_port_attr_alua_tg_pt_write_md, 614 NULL, 615 }; 616 617 static int target_fabric_port_link( 618 struct config_item *lun_ci, 619 struct config_item *se_dev_ci) 620 { 621 struct config_item *tpg_ci; 622 struct se_lun *lun = container_of(to_config_group(lun_ci), 623 struct se_lun, lun_group); 624 struct se_portal_group *se_tpg; 625 struct se_device *dev = 626 container_of(to_config_group(se_dev_ci), struct se_device, dev_group); 627 struct target_fabric_configfs *tf; 628 int ret; 629 630 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) { 631 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:" 632 " %p to struct se_device: %p\n", se_dev_ci, dev); 633 return -EFAULT; 634 } 635 636 if (!(dev->dev_flags & DF_CONFIGURED)) { 637 pr_err("se_device not configured yet, cannot port link\n"); 638 return -ENODEV; 639 } 640 641 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item; 642 se_tpg = container_of(to_config_group(tpg_ci), 643 struct se_portal_group, tpg_group); 644 tf = se_tpg->se_tpg_wwn->wwn_tf; 645 646 if (lun->lun_se_dev != NULL) { 647 pr_err("Port Symlink already exists\n"); 648 return -EEXIST; 649 } 650 651 ret = core_dev_add_lun(se_tpg, dev, lun); 652 if (ret) { 653 pr_err("core_dev_add_lun() failed: %d\n", ret); 654 goto out; 655 } 656 657 if (tf->tf_ops->fabric_post_link) { 658 /* 659 * Call the optional fabric_post_link() to allow a 660 * fabric module to setup any additional state once 661 * core_dev_add_lun() has been called.. 662 */ 663 tf->tf_ops->fabric_post_link(se_tpg, lun); 664 } 665 666 return 0; 667 out: 668 return ret; 669 } 670 671 static int target_fabric_port_unlink( 672 struct config_item *lun_ci, 673 struct config_item *se_dev_ci) 674 { 675 struct se_lun *lun = container_of(to_config_group(lun_ci), 676 struct se_lun, lun_group); 677 struct se_portal_group *se_tpg = lun->lun_tpg; 678 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 679 680 if (tf->tf_ops->fabric_pre_unlink) { 681 /* 682 * Call the optional fabric_pre_unlink() to allow a 683 * fabric module to release any additional stat before 684 * core_dev_del_lun() is called. 685 */ 686 tf->tf_ops->fabric_pre_unlink(se_tpg, lun); 687 } 688 689 core_dev_del_lun(se_tpg, lun); 690 return 0; 691 } 692 693 static void target_fabric_port_release(struct config_item *item) 694 { 695 struct se_lun *lun = container_of(to_config_group(item), 696 struct se_lun, lun_group); 697 698 kfree_rcu(lun, rcu_head); 699 } 700 701 static struct configfs_item_operations target_fabric_port_item_ops = { 702 .release = target_fabric_port_release, 703 .allow_link = target_fabric_port_link, 704 .drop_link = target_fabric_port_unlink, 705 }; 706 707 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs); 708 709 /* End of tfc_tpg_port_cit */ 710 711 /* Start of tfc_tpg_port_stat_cit */ 712 713 static struct config_group *target_core_port_stat_mkdir( 714 struct config_group *group, 715 const char *name) 716 { 717 return ERR_PTR(-ENOSYS); 718 } 719 720 static void target_core_port_stat_rmdir( 721 struct config_group *group, 722 struct config_item *item) 723 { 724 return; 725 } 726 727 static struct configfs_group_operations target_fabric_port_stat_group_ops = { 728 .make_group = target_core_port_stat_mkdir, 729 .drop_item = target_core_port_stat_rmdir, 730 }; 731 732 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL); 733 734 /* End of tfc_tpg_port_stat_cit */ 735 736 /* Start of tfc_tpg_lun_cit */ 737 738 static struct config_group *target_fabric_make_lun( 739 struct config_group *group, 740 const char *name) 741 { 742 struct se_lun *lun; 743 struct se_portal_group *se_tpg = container_of(group, 744 struct se_portal_group, tpg_lun_group); 745 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 746 unsigned long long unpacked_lun; 747 int errno; 748 749 if (strstr(name, "lun_") != name) { 750 pr_err("Unable to locate \'_\" in" 751 " \"lun_$LUN_NUMBER\"\n"); 752 return ERR_PTR(-EINVAL); 753 } 754 errno = kstrtoull(name + 4, 0, &unpacked_lun); 755 if (errno) 756 return ERR_PTR(errno); 757 758 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun); 759 if (IS_ERR(lun)) 760 return ERR_CAST(lun); 761 762 config_group_init_type_name(&lun->lun_group, name, 763 &tf->tf_tpg_port_cit); 764 765 config_group_init_type_name(&lun->port_stat_grps.stat_group, 766 "statistics", &tf->tf_tpg_port_stat_cit); 767 configfs_add_default_group(&lun->port_stat_grps.stat_group, 768 &lun->lun_group); 769 770 target_stat_setup_port_default_groups(lun); 771 772 return &lun->lun_group; 773 } 774 775 static void target_fabric_drop_lun( 776 struct config_group *group, 777 struct config_item *item) 778 { 779 struct se_lun *lun = container_of(to_config_group(item), 780 struct se_lun, lun_group); 781 782 configfs_remove_default_groups(&lun->port_stat_grps.stat_group); 783 configfs_remove_default_groups(&lun->lun_group); 784 785 config_item_put(item); 786 } 787 788 static struct configfs_group_operations target_fabric_lun_group_ops = { 789 .make_group = &target_fabric_make_lun, 790 .drop_item = &target_fabric_drop_lun, 791 }; 792 793 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL); 794 795 /* End of tfc_tpg_lun_cit */ 796 797 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL); 798 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL); 799 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL); 800 801 /* Start of tfc_tpg_base_cit */ 802 803 static void target_fabric_tpg_release(struct config_item *item) 804 { 805 struct se_portal_group *se_tpg = container_of(to_config_group(item), 806 struct se_portal_group, tpg_group); 807 struct se_wwn *wwn = se_tpg->se_tpg_wwn; 808 struct target_fabric_configfs *tf = wwn->wwn_tf; 809 810 tf->tf_ops->fabric_drop_tpg(se_tpg); 811 } 812 813 static struct configfs_item_operations target_fabric_tpg_base_item_ops = { 814 .release = target_fabric_tpg_release, 815 }; 816 817 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL); 818 819 /* End of tfc_tpg_base_cit */ 820 821 /* Start of tfc_tpg_cit */ 822 823 static struct config_group *target_fabric_make_tpg( 824 struct config_group *group, 825 const char *name) 826 { 827 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group); 828 struct target_fabric_configfs *tf = wwn->wwn_tf; 829 struct se_portal_group *se_tpg; 830 831 if (!tf->tf_ops->fabric_make_tpg) { 832 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n"); 833 return ERR_PTR(-ENOSYS); 834 } 835 836 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name); 837 if (!se_tpg || IS_ERR(se_tpg)) 838 return ERR_PTR(-EINVAL); 839 840 config_group_init_type_name(&se_tpg->tpg_group, name, 841 &tf->tf_tpg_base_cit); 842 843 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun", 844 &tf->tf_tpg_lun_cit); 845 configfs_add_default_group(&se_tpg->tpg_lun_group, 846 &se_tpg->tpg_group); 847 848 config_group_init_type_name(&se_tpg->tpg_np_group, "np", 849 &tf->tf_tpg_np_cit); 850 configfs_add_default_group(&se_tpg->tpg_np_group, 851 &se_tpg->tpg_group); 852 853 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls", 854 &tf->tf_tpg_nacl_cit); 855 configfs_add_default_group(&se_tpg->tpg_acl_group, 856 &se_tpg->tpg_group); 857 858 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib", 859 &tf->tf_tpg_attrib_cit); 860 configfs_add_default_group(&se_tpg->tpg_attrib_group, 861 &se_tpg->tpg_group); 862 863 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth", 864 &tf->tf_tpg_auth_cit); 865 configfs_add_default_group(&se_tpg->tpg_auth_group, 866 &se_tpg->tpg_group); 867 868 config_group_init_type_name(&se_tpg->tpg_param_group, "param", 869 &tf->tf_tpg_param_cit); 870 configfs_add_default_group(&se_tpg->tpg_param_group, 871 &se_tpg->tpg_group); 872 873 return &se_tpg->tpg_group; 874 } 875 876 static void target_fabric_drop_tpg( 877 struct config_group *group, 878 struct config_item *item) 879 { 880 struct se_portal_group *se_tpg = container_of(to_config_group(item), 881 struct se_portal_group, tpg_group); 882 883 configfs_remove_default_groups(&se_tpg->tpg_group); 884 config_item_put(item); 885 } 886 887 static void target_fabric_release_wwn(struct config_item *item) 888 { 889 struct se_wwn *wwn = container_of(to_config_group(item), 890 struct se_wwn, wwn_group); 891 struct target_fabric_configfs *tf = wwn->wwn_tf; 892 893 tf->tf_ops->fabric_drop_wwn(wwn); 894 } 895 896 static struct configfs_item_operations target_fabric_tpg_item_ops = { 897 .release = target_fabric_release_wwn, 898 }; 899 900 static struct configfs_group_operations target_fabric_tpg_group_ops = { 901 .make_group = target_fabric_make_tpg, 902 .drop_item = target_fabric_drop_tpg, 903 }; 904 905 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops, 906 NULL); 907 908 /* End of tfc_tpg_cit */ 909 910 /* Start of tfc_wwn_fabric_stats_cit */ 911 /* 912 * This is used as a placeholder for struct se_wwn->fabric_stat_group 913 * to allow fabrics access to ->fabric_stat_group->default_groups[] 914 */ 915 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL); 916 917 /* End of tfc_wwn_fabric_stats_cit */ 918 919 /* Start of tfc_wwn_cit */ 920 921 static struct config_group *target_fabric_make_wwn( 922 struct config_group *group, 923 const char *name) 924 { 925 struct target_fabric_configfs *tf = container_of(group, 926 struct target_fabric_configfs, tf_group); 927 struct se_wwn *wwn; 928 929 if (!tf->tf_ops->fabric_make_wwn) { 930 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n"); 931 return ERR_PTR(-ENOSYS); 932 } 933 934 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name); 935 if (!wwn || IS_ERR(wwn)) 936 return ERR_PTR(-EINVAL); 937 938 wwn->wwn_tf = tf; 939 940 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit); 941 942 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics", 943 &tf->tf_wwn_fabric_stats_cit); 944 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group); 945 946 return &wwn->wwn_group; 947 } 948 949 static void target_fabric_drop_wwn( 950 struct config_group *group, 951 struct config_item *item) 952 { 953 struct se_wwn *wwn = container_of(to_config_group(item), 954 struct se_wwn, wwn_group); 955 956 configfs_remove_default_groups(&wwn->wwn_group); 957 config_item_put(item); 958 } 959 960 static struct configfs_group_operations target_fabric_wwn_group_ops = { 961 .make_group = target_fabric_make_wwn, 962 .drop_item = target_fabric_drop_wwn, 963 }; 964 965 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops); 966 TF_CIT_SETUP_DRV(discovery, NULL, NULL); 967 968 int target_fabric_setup_cits(struct target_fabric_configfs *tf) 969 { 970 target_fabric_setup_discovery_cit(tf); 971 target_fabric_setup_wwn_cit(tf); 972 target_fabric_setup_wwn_fabric_stats_cit(tf); 973 target_fabric_setup_tpg_cit(tf); 974 target_fabric_setup_tpg_base_cit(tf); 975 target_fabric_setup_tpg_port_cit(tf); 976 target_fabric_setup_tpg_port_stat_cit(tf); 977 target_fabric_setup_tpg_lun_cit(tf); 978 target_fabric_setup_tpg_np_cit(tf); 979 target_fabric_setup_tpg_np_base_cit(tf); 980 target_fabric_setup_tpg_attrib_cit(tf); 981 target_fabric_setup_tpg_auth_cit(tf); 982 target_fabric_setup_tpg_param_cit(tf); 983 target_fabric_setup_tpg_nacl_cit(tf); 984 target_fabric_setup_tpg_nacl_base_cit(tf); 985 target_fabric_setup_tpg_nacl_attrib_cit(tf); 986 target_fabric_setup_tpg_nacl_auth_cit(tf); 987 target_fabric_setup_tpg_nacl_param_cit(tf); 988 target_fabric_setup_tpg_nacl_stat_cit(tf); 989 target_fabric_setup_tpg_mappedlun_cit(tf); 990 target_fabric_setup_tpg_mappedlun_stat_cit(tf); 991 992 return 0; 993 } 994