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 struct config_item *acl_ci; 273 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL; 274 char *buf; 275 unsigned long long mapped_lun; 276 int ret = 0; 277 278 acl_ci = &group->cg_item; 279 if (!acl_ci) { 280 pr_err("Unable to locatel acl_ci\n"); 281 return NULL; 282 } 283 284 buf = kzalloc(strlen(name) + 1, GFP_KERNEL); 285 if (!buf) { 286 pr_err("Unable to allocate memory for name buf\n"); 287 return ERR_PTR(-ENOMEM); 288 } 289 snprintf(buf, strlen(name) + 1, "%s", name); 290 /* 291 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID. 292 */ 293 if (strstr(buf, "lun_") != buf) { 294 pr_err("Unable to locate \"lun_\" from buf: %s" 295 " name: %s\n", buf, name); 296 ret = -EINVAL; 297 goto out; 298 } 299 /* 300 * Determine the Mapped LUN value. This is what the SCSI Initiator 301 * Port will actually see. 302 */ 303 ret = kstrtoull(buf + 4, 0, &mapped_lun); 304 if (ret) 305 goto out; 306 307 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl, 308 mapped_lun, &ret); 309 if (!lacl) { 310 ret = -EINVAL; 311 goto out; 312 } 313 314 lacl_cg = &lacl->se_lun_group; 315 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2, 316 GFP_KERNEL); 317 if (!lacl_cg->default_groups) { 318 pr_err("Unable to allocate lacl_cg->default_groups\n"); 319 ret = -ENOMEM; 320 goto out; 321 } 322 323 config_group_init_type_name(&lacl->se_lun_group, name, 324 &tf->tf_tpg_mappedlun_cit); 325 config_group_init_type_name(&lacl->ml_stat_grps.stat_group, 326 "statistics", &tf->tf_tpg_mappedlun_stat_cit); 327 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group; 328 lacl_cg->default_groups[1] = NULL; 329 330 ml_stat_grp = &lacl->ml_stat_grps.stat_group; 331 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3, 332 GFP_KERNEL); 333 if (!ml_stat_grp->default_groups) { 334 pr_err("Unable to allocate ml_stat_grp->default_groups\n"); 335 ret = -ENOMEM; 336 goto out; 337 } 338 target_stat_setup_mappedlun_default_groups(lacl); 339 340 kfree(buf); 341 return &lacl->se_lun_group; 342 out: 343 if (lacl_cg) 344 kfree(lacl_cg->default_groups); 345 kfree(lacl); 346 kfree(buf); 347 return ERR_PTR(ret); 348 } 349 350 static void target_fabric_drop_mappedlun( 351 struct config_group *group, 352 struct config_item *item) 353 { 354 struct se_lun_acl *lacl = container_of(to_config_group(item), 355 struct se_lun_acl, se_lun_group); 356 struct config_item *df_item; 357 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL; 358 int i; 359 360 ml_stat_grp = &lacl->ml_stat_grps.stat_group; 361 for (i = 0; ml_stat_grp->default_groups[i]; i++) { 362 df_item = &ml_stat_grp->default_groups[i]->cg_item; 363 ml_stat_grp->default_groups[i] = NULL; 364 config_item_put(df_item); 365 } 366 kfree(ml_stat_grp->default_groups); 367 368 lacl_cg = &lacl->se_lun_group; 369 for (i = 0; lacl_cg->default_groups[i]; i++) { 370 df_item = &lacl_cg->default_groups[i]->cg_item; 371 lacl_cg->default_groups[i] = NULL; 372 config_item_put(df_item); 373 } 374 kfree(lacl_cg->default_groups); 375 376 config_item_put(item); 377 } 378 379 static void target_fabric_nacl_base_release(struct config_item *item) 380 { 381 struct se_node_acl *se_nacl = container_of(to_config_group(item), 382 struct se_node_acl, acl_group); 383 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf; 384 385 if (tf->tf_ops->fabric_cleanup_nodeacl) 386 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl); 387 core_tpg_del_initiator_node_acl(se_nacl); 388 } 389 390 static struct configfs_item_operations target_fabric_nacl_base_item_ops = { 391 .release = target_fabric_nacl_base_release, 392 }; 393 394 static struct configfs_group_operations target_fabric_nacl_base_group_ops = { 395 .make_group = target_fabric_make_mappedlun, 396 .drop_item = target_fabric_drop_mappedlun, 397 }; 398 399 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops, 400 &target_fabric_nacl_base_group_ops); 401 402 /* End of tfc_tpg_nacl_base_cit */ 403 404 /* Start of tfc_node_fabric_stats_cit */ 405 /* 406 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group 407 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[] 408 */ 409 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL); 410 411 /* End of tfc_wwn_fabric_stats_cit */ 412 413 /* Start of tfc_tpg_nacl_cit */ 414 415 static struct config_group *target_fabric_make_nodeacl( 416 struct config_group *group, 417 const char *name) 418 { 419 struct se_portal_group *se_tpg = container_of(group, 420 struct se_portal_group, tpg_acl_group); 421 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 422 struct se_node_acl *se_nacl; 423 struct config_group *nacl_cg; 424 425 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name); 426 if (IS_ERR(se_nacl)) 427 return ERR_CAST(se_nacl); 428 429 if (tf->tf_ops->fabric_init_nodeacl) { 430 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name); 431 if (ret) { 432 core_tpg_del_initiator_node_acl(se_nacl); 433 return ERR_PTR(ret); 434 } 435 } 436 437 nacl_cg = &se_nacl->acl_group; 438 nacl_cg->default_groups = se_nacl->acl_default_groups; 439 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group; 440 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group; 441 nacl_cg->default_groups[2] = &se_nacl->acl_param_group; 442 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group; 443 nacl_cg->default_groups[4] = NULL; 444 445 config_group_init_type_name(&se_nacl->acl_group, name, 446 &tf->tf_tpg_nacl_base_cit); 447 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib", 448 &tf->tf_tpg_nacl_attrib_cit); 449 config_group_init_type_name(&se_nacl->acl_auth_group, "auth", 450 &tf->tf_tpg_nacl_auth_cit); 451 config_group_init_type_name(&se_nacl->acl_param_group, "param", 452 &tf->tf_tpg_nacl_param_cit); 453 config_group_init_type_name(&se_nacl->acl_fabric_stat_group, 454 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit); 455 456 return &se_nacl->acl_group; 457 } 458 459 static void target_fabric_drop_nodeacl( 460 struct config_group *group, 461 struct config_item *item) 462 { 463 struct se_node_acl *se_nacl = container_of(to_config_group(item), 464 struct se_node_acl, acl_group); 465 struct config_item *df_item; 466 struct config_group *nacl_cg; 467 int i; 468 469 nacl_cg = &se_nacl->acl_group; 470 for (i = 0; nacl_cg->default_groups[i]; i++) { 471 df_item = &nacl_cg->default_groups[i]->cg_item; 472 nacl_cg->default_groups[i] = NULL; 473 config_item_put(df_item); 474 } 475 /* 476 * struct se_node_acl free is done in target_fabric_nacl_base_release() 477 */ 478 config_item_put(item); 479 } 480 481 static struct configfs_group_operations target_fabric_nacl_group_ops = { 482 .make_group = target_fabric_make_nodeacl, 483 .drop_item = target_fabric_drop_nodeacl, 484 }; 485 486 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL); 487 488 /* End of tfc_tpg_nacl_cit */ 489 490 /* Start of tfc_tpg_np_base_cit */ 491 492 static void target_fabric_np_base_release(struct config_item *item) 493 { 494 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item), 495 struct se_tpg_np, tpg_np_group); 496 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent; 497 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 498 499 tf->tf_ops->fabric_drop_np(se_tpg_np); 500 } 501 502 static struct configfs_item_operations target_fabric_np_base_item_ops = { 503 .release = target_fabric_np_base_release, 504 }; 505 506 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL); 507 508 /* End of tfc_tpg_np_base_cit */ 509 510 /* Start of tfc_tpg_np_cit */ 511 512 static struct config_group *target_fabric_make_np( 513 struct config_group *group, 514 const char *name) 515 { 516 struct se_portal_group *se_tpg = container_of(group, 517 struct se_portal_group, tpg_np_group); 518 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 519 struct se_tpg_np *se_tpg_np; 520 521 if (!tf->tf_ops->fabric_make_np) { 522 pr_err("tf->tf_ops.fabric_make_np is NULL\n"); 523 return ERR_PTR(-ENOSYS); 524 } 525 526 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name); 527 if (!se_tpg_np || IS_ERR(se_tpg_np)) 528 return ERR_PTR(-EINVAL); 529 530 se_tpg_np->tpg_np_parent = se_tpg; 531 config_group_init_type_name(&se_tpg_np->tpg_np_group, name, 532 &tf->tf_tpg_np_base_cit); 533 534 return &se_tpg_np->tpg_np_group; 535 } 536 537 static void target_fabric_drop_np( 538 struct config_group *group, 539 struct config_item *item) 540 { 541 /* 542 * struct se_tpg_np is released via target_fabric_np_base_release() 543 */ 544 config_item_put(item); 545 } 546 547 static struct configfs_group_operations target_fabric_np_group_ops = { 548 .make_group = &target_fabric_make_np, 549 .drop_item = &target_fabric_drop_np, 550 }; 551 552 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL); 553 554 /* End of tfc_tpg_np_cit */ 555 556 /* Start of tfc_tpg_port_cit */ 557 558 static struct se_lun *item_to_lun(struct config_item *item) 559 { 560 return container_of(to_config_group(item), struct se_lun, 561 lun_group); 562 } 563 564 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item, 565 char *page) 566 { 567 struct se_lun *lun = item_to_lun(item); 568 569 if (!lun || !lun->lun_se_dev) 570 return -ENODEV; 571 572 return core_alua_show_tg_pt_gp_info(lun, page); 573 } 574 575 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item, 576 const char *page, size_t count) 577 { 578 struct se_lun *lun = item_to_lun(item); 579 580 if (!lun || !lun->lun_se_dev) 581 return -ENODEV; 582 583 return core_alua_store_tg_pt_gp_info(lun, page, count); 584 } 585 586 static ssize_t target_fabric_port_alua_tg_pt_offline_show( 587 struct config_item *item, char *page) 588 { 589 struct se_lun *lun = item_to_lun(item); 590 591 if (!lun || !lun->lun_se_dev) 592 return -ENODEV; 593 594 return core_alua_show_offline_bit(lun, page); 595 } 596 597 static ssize_t target_fabric_port_alua_tg_pt_offline_store( 598 struct config_item *item, const char *page, size_t count) 599 { 600 struct se_lun *lun = item_to_lun(item); 601 602 if (!lun || !lun->lun_se_dev) 603 return -ENODEV; 604 605 return core_alua_store_offline_bit(lun, page, count); 606 } 607 608 static ssize_t target_fabric_port_alua_tg_pt_status_show( 609 struct config_item *item, char *page) 610 { 611 struct se_lun *lun = item_to_lun(item); 612 613 if (!lun || !lun->lun_se_dev) 614 return -ENODEV; 615 616 return core_alua_show_secondary_status(lun, page); 617 } 618 619 static ssize_t target_fabric_port_alua_tg_pt_status_store( 620 struct config_item *item, const char *page, size_t count) 621 { 622 struct se_lun *lun = item_to_lun(item); 623 624 if (!lun || !lun->lun_se_dev) 625 return -ENODEV; 626 627 return core_alua_store_secondary_status(lun, page, count); 628 } 629 630 static ssize_t target_fabric_port_alua_tg_pt_write_md_show( 631 struct config_item *item, char *page) 632 { 633 struct se_lun *lun = item_to_lun(item); 634 635 if (!lun || !lun->lun_se_dev) 636 return -ENODEV; 637 638 return core_alua_show_secondary_write_metadata(lun, page); 639 } 640 641 static ssize_t target_fabric_port_alua_tg_pt_write_md_store( 642 struct config_item *item, const char *page, size_t count) 643 { 644 struct se_lun *lun = item_to_lun(item); 645 646 if (!lun || !lun->lun_se_dev) 647 return -ENODEV; 648 649 return core_alua_store_secondary_write_metadata(lun, page, count); 650 } 651 652 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp); 653 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline); 654 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status); 655 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md); 656 657 static struct configfs_attribute *target_fabric_port_attrs[] = { 658 &target_fabric_port_attr_alua_tg_pt_gp, 659 &target_fabric_port_attr_alua_tg_pt_offline, 660 &target_fabric_port_attr_alua_tg_pt_status, 661 &target_fabric_port_attr_alua_tg_pt_write_md, 662 NULL, 663 }; 664 665 static int target_fabric_port_link( 666 struct config_item *lun_ci, 667 struct config_item *se_dev_ci) 668 { 669 struct config_item *tpg_ci; 670 struct se_lun *lun = container_of(to_config_group(lun_ci), 671 struct se_lun, lun_group); 672 struct se_portal_group *se_tpg; 673 struct se_device *dev = 674 container_of(to_config_group(se_dev_ci), struct se_device, dev_group); 675 struct target_fabric_configfs *tf; 676 int ret; 677 678 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) { 679 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:" 680 " %p to struct se_device: %p\n", se_dev_ci, dev); 681 return -EFAULT; 682 } 683 684 if (!(dev->dev_flags & DF_CONFIGURED)) { 685 pr_err("se_device not configured yet, cannot port link\n"); 686 return -ENODEV; 687 } 688 689 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item; 690 se_tpg = container_of(to_config_group(tpg_ci), 691 struct se_portal_group, tpg_group); 692 tf = se_tpg->se_tpg_wwn->wwn_tf; 693 694 if (lun->lun_se_dev != NULL) { 695 pr_err("Port Symlink already exists\n"); 696 return -EEXIST; 697 } 698 699 ret = core_dev_add_lun(se_tpg, dev, lun); 700 if (ret) { 701 pr_err("core_dev_add_lun() failed: %d\n", ret); 702 goto out; 703 } 704 705 if (tf->tf_ops->fabric_post_link) { 706 /* 707 * Call the optional fabric_post_link() to allow a 708 * fabric module to setup any additional state once 709 * core_dev_add_lun() has been called.. 710 */ 711 tf->tf_ops->fabric_post_link(se_tpg, lun); 712 } 713 714 return 0; 715 out: 716 return ret; 717 } 718 719 static int target_fabric_port_unlink( 720 struct config_item *lun_ci, 721 struct config_item *se_dev_ci) 722 { 723 struct se_lun *lun = container_of(to_config_group(lun_ci), 724 struct se_lun, lun_group); 725 struct se_portal_group *se_tpg = lun->lun_tpg; 726 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 727 728 if (tf->tf_ops->fabric_pre_unlink) { 729 /* 730 * Call the optional fabric_pre_unlink() to allow a 731 * fabric module to release any additional stat before 732 * core_dev_del_lun() is called. 733 */ 734 tf->tf_ops->fabric_pre_unlink(se_tpg, lun); 735 } 736 737 core_dev_del_lun(se_tpg, lun); 738 return 0; 739 } 740 741 static void target_fabric_port_release(struct config_item *item) 742 { 743 struct se_lun *lun = container_of(to_config_group(item), 744 struct se_lun, lun_group); 745 746 kfree_rcu(lun, rcu_head); 747 } 748 749 static struct configfs_item_operations target_fabric_port_item_ops = { 750 .release = target_fabric_port_release, 751 .allow_link = target_fabric_port_link, 752 .drop_link = target_fabric_port_unlink, 753 }; 754 755 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs); 756 757 /* End of tfc_tpg_port_cit */ 758 759 /* Start of tfc_tpg_port_stat_cit */ 760 761 static struct config_group *target_core_port_stat_mkdir( 762 struct config_group *group, 763 const char *name) 764 { 765 return ERR_PTR(-ENOSYS); 766 } 767 768 static void target_core_port_stat_rmdir( 769 struct config_group *group, 770 struct config_item *item) 771 { 772 return; 773 } 774 775 static struct configfs_group_operations target_fabric_port_stat_group_ops = { 776 .make_group = target_core_port_stat_mkdir, 777 .drop_item = target_core_port_stat_rmdir, 778 }; 779 780 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL); 781 782 /* End of tfc_tpg_port_stat_cit */ 783 784 /* Start of tfc_tpg_lun_cit */ 785 786 static struct config_group *target_fabric_make_lun( 787 struct config_group *group, 788 const char *name) 789 { 790 struct se_lun *lun; 791 struct se_portal_group *se_tpg = container_of(group, 792 struct se_portal_group, tpg_lun_group); 793 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 794 struct config_group *lun_cg = NULL, *port_stat_grp = NULL; 795 unsigned long long unpacked_lun; 796 int errno; 797 798 if (strstr(name, "lun_") != name) { 799 pr_err("Unable to locate \'_\" in" 800 " \"lun_$LUN_NUMBER\"\n"); 801 return ERR_PTR(-EINVAL); 802 } 803 errno = kstrtoull(name + 4, 0, &unpacked_lun); 804 if (errno) 805 return ERR_PTR(errno); 806 807 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun); 808 if (IS_ERR(lun)) 809 return ERR_CAST(lun); 810 811 lun_cg = &lun->lun_group; 812 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2, 813 GFP_KERNEL); 814 if (!lun_cg->default_groups) { 815 pr_err("Unable to allocate lun_cg->default_groups\n"); 816 kfree(lun); 817 return ERR_PTR(-ENOMEM); 818 } 819 820 config_group_init_type_name(&lun->lun_group, name, 821 &tf->tf_tpg_port_cit); 822 config_group_init_type_name(&lun->port_stat_grps.stat_group, 823 "statistics", &tf->tf_tpg_port_stat_cit); 824 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group; 825 lun_cg->default_groups[1] = NULL; 826 827 port_stat_grp = &lun->port_stat_grps.stat_group; 828 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4, 829 GFP_KERNEL); 830 if (!port_stat_grp->default_groups) { 831 pr_err("Unable to allocate port_stat_grp->default_groups\n"); 832 kfree(lun_cg->default_groups); 833 kfree(lun); 834 return ERR_PTR(-ENOMEM); 835 } 836 target_stat_setup_port_default_groups(lun); 837 838 return &lun->lun_group; 839 } 840 841 static void target_fabric_drop_lun( 842 struct config_group *group, 843 struct config_item *item) 844 { 845 struct se_lun *lun = container_of(to_config_group(item), 846 struct se_lun, lun_group); 847 struct config_item *df_item; 848 struct config_group *lun_cg, *port_stat_grp; 849 int i; 850 851 port_stat_grp = &lun->port_stat_grps.stat_group; 852 for (i = 0; port_stat_grp->default_groups[i]; i++) { 853 df_item = &port_stat_grp->default_groups[i]->cg_item; 854 port_stat_grp->default_groups[i] = NULL; 855 config_item_put(df_item); 856 } 857 kfree(port_stat_grp->default_groups); 858 859 lun_cg = &lun->lun_group; 860 for (i = 0; lun_cg->default_groups[i]; i++) { 861 df_item = &lun_cg->default_groups[i]->cg_item; 862 lun_cg->default_groups[i] = NULL; 863 config_item_put(df_item); 864 } 865 kfree(lun_cg->default_groups); 866 867 config_item_put(item); 868 } 869 870 static struct configfs_group_operations target_fabric_lun_group_ops = { 871 .make_group = &target_fabric_make_lun, 872 .drop_item = &target_fabric_drop_lun, 873 }; 874 875 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL); 876 877 /* End of tfc_tpg_lun_cit */ 878 879 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL); 880 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL); 881 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL); 882 883 /* Start of tfc_tpg_base_cit */ 884 885 static void target_fabric_tpg_release(struct config_item *item) 886 { 887 struct se_portal_group *se_tpg = container_of(to_config_group(item), 888 struct se_portal_group, tpg_group); 889 struct se_wwn *wwn = se_tpg->se_tpg_wwn; 890 struct target_fabric_configfs *tf = wwn->wwn_tf; 891 892 tf->tf_ops->fabric_drop_tpg(se_tpg); 893 } 894 895 static struct configfs_item_operations target_fabric_tpg_base_item_ops = { 896 .release = target_fabric_tpg_release, 897 }; 898 899 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL); 900 901 /* End of tfc_tpg_base_cit */ 902 903 /* Start of tfc_tpg_cit */ 904 905 static struct config_group *target_fabric_make_tpg( 906 struct config_group *group, 907 const char *name) 908 { 909 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group); 910 struct target_fabric_configfs *tf = wwn->wwn_tf; 911 struct se_portal_group *se_tpg; 912 913 if (!tf->tf_ops->fabric_make_tpg) { 914 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n"); 915 return ERR_PTR(-ENOSYS); 916 } 917 918 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name); 919 if (!se_tpg || IS_ERR(se_tpg)) 920 return ERR_PTR(-EINVAL); 921 /* 922 * Setup default groups from pre-allocated se_tpg->tpg_default_groups 923 */ 924 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups; 925 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group; 926 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group; 927 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group; 928 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group; 929 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group; 930 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group; 931 se_tpg->tpg_group.default_groups[6] = NULL; 932 933 config_group_init_type_name(&se_tpg->tpg_group, name, 934 &tf->tf_tpg_base_cit); 935 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun", 936 &tf->tf_tpg_lun_cit); 937 config_group_init_type_name(&se_tpg->tpg_np_group, "np", 938 &tf->tf_tpg_np_cit); 939 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls", 940 &tf->tf_tpg_nacl_cit); 941 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib", 942 &tf->tf_tpg_attrib_cit); 943 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth", 944 &tf->tf_tpg_auth_cit); 945 config_group_init_type_name(&se_tpg->tpg_param_group, "param", 946 &tf->tf_tpg_param_cit); 947 948 return &se_tpg->tpg_group; 949 } 950 951 static void target_fabric_drop_tpg( 952 struct config_group *group, 953 struct config_item *item) 954 { 955 struct se_portal_group *se_tpg = container_of(to_config_group(item), 956 struct se_portal_group, tpg_group); 957 struct config_group *tpg_cg = &se_tpg->tpg_group; 958 struct config_item *df_item; 959 int i; 960 /* 961 * Release default groups, but do not release tpg_cg->default_groups 962 * memory as it is statically allocated at se_tpg->tpg_default_groups. 963 */ 964 for (i = 0; tpg_cg->default_groups[i]; i++) { 965 df_item = &tpg_cg->default_groups[i]->cg_item; 966 tpg_cg->default_groups[i] = NULL; 967 config_item_put(df_item); 968 } 969 970 config_item_put(item); 971 } 972 973 static void target_fabric_release_wwn(struct config_item *item) 974 { 975 struct se_wwn *wwn = container_of(to_config_group(item), 976 struct se_wwn, wwn_group); 977 struct target_fabric_configfs *tf = wwn->wwn_tf; 978 979 tf->tf_ops->fabric_drop_wwn(wwn); 980 } 981 982 static struct configfs_item_operations target_fabric_tpg_item_ops = { 983 .release = target_fabric_release_wwn, 984 }; 985 986 static struct configfs_group_operations target_fabric_tpg_group_ops = { 987 .make_group = target_fabric_make_tpg, 988 .drop_item = target_fabric_drop_tpg, 989 }; 990 991 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops, 992 NULL); 993 994 /* End of tfc_tpg_cit */ 995 996 /* Start of tfc_wwn_fabric_stats_cit */ 997 /* 998 * This is used as a placeholder for struct se_wwn->fabric_stat_group 999 * to allow fabrics access to ->fabric_stat_group->default_groups[] 1000 */ 1001 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL); 1002 1003 /* End of tfc_wwn_fabric_stats_cit */ 1004 1005 /* Start of tfc_wwn_cit */ 1006 1007 static struct config_group *target_fabric_make_wwn( 1008 struct config_group *group, 1009 const char *name) 1010 { 1011 struct target_fabric_configfs *tf = container_of(group, 1012 struct target_fabric_configfs, tf_group); 1013 struct se_wwn *wwn; 1014 1015 if (!tf->tf_ops->fabric_make_wwn) { 1016 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n"); 1017 return ERR_PTR(-ENOSYS); 1018 } 1019 1020 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name); 1021 if (!wwn || IS_ERR(wwn)) 1022 return ERR_PTR(-EINVAL); 1023 1024 wwn->wwn_tf = tf; 1025 /* 1026 * Setup default groups from pre-allocated wwn->wwn_default_groups 1027 */ 1028 wwn->wwn_group.default_groups = wwn->wwn_default_groups; 1029 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group; 1030 wwn->wwn_group.default_groups[1] = NULL; 1031 1032 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit); 1033 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics", 1034 &tf->tf_wwn_fabric_stats_cit); 1035 1036 return &wwn->wwn_group; 1037 } 1038 1039 static void target_fabric_drop_wwn( 1040 struct config_group *group, 1041 struct config_item *item) 1042 { 1043 struct se_wwn *wwn = container_of(to_config_group(item), 1044 struct se_wwn, wwn_group); 1045 struct config_item *df_item; 1046 struct config_group *cg = &wwn->wwn_group; 1047 int i; 1048 1049 for (i = 0; cg->default_groups[i]; i++) { 1050 df_item = &cg->default_groups[i]->cg_item; 1051 cg->default_groups[i] = NULL; 1052 config_item_put(df_item); 1053 } 1054 1055 config_item_put(item); 1056 } 1057 1058 static struct configfs_group_operations target_fabric_wwn_group_ops = { 1059 .make_group = target_fabric_make_wwn, 1060 .drop_item = target_fabric_drop_wwn, 1061 }; 1062 1063 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops); 1064 TF_CIT_SETUP_DRV(discovery, NULL, NULL); 1065 1066 int target_fabric_setup_cits(struct target_fabric_configfs *tf) 1067 { 1068 target_fabric_setup_discovery_cit(tf); 1069 target_fabric_setup_wwn_cit(tf); 1070 target_fabric_setup_wwn_fabric_stats_cit(tf); 1071 target_fabric_setup_tpg_cit(tf); 1072 target_fabric_setup_tpg_base_cit(tf); 1073 target_fabric_setup_tpg_port_cit(tf); 1074 target_fabric_setup_tpg_port_stat_cit(tf); 1075 target_fabric_setup_tpg_lun_cit(tf); 1076 target_fabric_setup_tpg_np_cit(tf); 1077 target_fabric_setup_tpg_np_base_cit(tf); 1078 target_fabric_setup_tpg_attrib_cit(tf); 1079 target_fabric_setup_tpg_auth_cit(tf); 1080 target_fabric_setup_tpg_param_cit(tf); 1081 target_fabric_setup_tpg_nacl_cit(tf); 1082 target_fabric_setup_tpg_nacl_base_cit(tf); 1083 target_fabric_setup_tpg_nacl_attrib_cit(tf); 1084 target_fabric_setup_tpg_nacl_auth_cit(tf); 1085 target_fabric_setup_tpg_nacl_param_cit(tf); 1086 target_fabric_setup_tpg_nacl_stat_cit(tf); 1087 target_fabric_setup_tpg_mappedlun_cit(tf); 1088 target_fabric_setup_tpg_mappedlun_stat_cit(tf); 1089 1090 return 0; 1091 } 1092