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 * Copyright (c) 2010,2011 Rising Tide Systems 8 * Copyright (c) 2010,2011 Linux-iSCSI.org 9 * 10 * Copyright (c) Nicholas A. Bellinger <nab@linux-iscsi.org> 11* 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 ****************************************************************************/ 22 23#include <linux/module.h> 24#include <linux/moduleparam.h> 25#include <generated/utsrelease.h> 26#include <linux/utsname.h> 27#include <linux/init.h> 28#include <linux/fs.h> 29#include <linux/namei.h> 30#include <linux/slab.h> 31#include <linux/types.h> 32#include <linux/delay.h> 33#include <linux/unistd.h> 34#include <linux/string.h> 35#include <linux/syscalls.h> 36#include <linux/configfs.h> 37 38#include <target/target_core_base.h> 39#include <target/target_core_fabric.h> 40#include <target/target_core_fabric_configfs.h> 41#include <target/target_core_configfs.h> 42#include <target/configfs_macros.h> 43 44#include "target_core_internal.h" 45#include "target_core_alua.h" 46#include "target_core_pr.h" 47 48#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \ 49static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \ 50{ \ 51 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \ 52 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \ 53 \ 54 cit->ct_item_ops = _item_ops; \ 55 cit->ct_group_ops = _group_ops; \ 56 cit->ct_attrs = _attrs; \ 57 cit->ct_owner = tf->tf_module; \ 58 pr_debug("Setup generic %s\n", __stringify(_name)); \ 59} 60 61/* Start of tfc_tpg_mappedlun_cit */ 62 63static int target_fabric_mappedlun_link( 64 struct config_item *lun_acl_ci, 65 struct config_item *lun_ci) 66{ 67 struct se_dev_entry *deve; 68 struct se_lun *lun = container_of(to_config_group(lun_ci), 69 struct se_lun, lun_group); 70 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci), 71 struct se_lun_acl, se_lun_group); 72 struct se_portal_group *se_tpg; 73 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s; 74 int ret = 0, lun_access; 75 /* 76 * Ensure that the source port exists 77 */ 78 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) { 79 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep" 80 "_tpg does not exist\n"); 81 return -EINVAL; 82 } 83 se_tpg = lun->lun_sep->sep_tpg; 84 85 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item; 86 tpg_ci = &nacl_ci->ci_group->cg_item; 87 wwn_ci = &tpg_ci->ci_group->cg_item; 88 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item; 89 wwn_ci_s = &tpg_ci_s->ci_group->cg_item; 90 /* 91 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT 92 */ 93 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) { 94 pr_err("Illegal Initiator ACL SymLink outside of %s\n", 95 config_item_name(wwn_ci)); 96 return -EINVAL; 97 } 98 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) { 99 pr_err("Illegal Initiator ACL Symlink outside of %s" 100 " TPGT: %s\n", config_item_name(wwn_ci), 101 config_item_name(tpg_ci)); 102 return -EINVAL; 103 } 104 /* 105 * If this struct se_node_acl was dynamically generated with 106 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags, 107 * which be will write protected (READ-ONLY) when 108 * tpg_1/attrib/demo_mode_write_protect=1 109 */ 110 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock); 111 deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun]; 112 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) 113 lun_access = deve->lun_flags; 114 else 115 lun_access = 116 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect( 117 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY : 118 TRANSPORT_LUNFLAGS_READ_WRITE; 119 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock); 120 /* 121 * Determine the actual mapped LUN value user wants.. 122 * 123 * This value is what the SCSI Initiator actually sees the 124 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports. 125 */ 126 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl, 127 lun->unpacked_lun, lun_access); 128 129 return (ret < 0) ? -EINVAL : 0; 130} 131 132static int target_fabric_mappedlun_unlink( 133 struct config_item *lun_acl_ci, 134 struct config_item *lun_ci) 135{ 136 struct se_lun *lun; 137 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci), 138 struct se_lun_acl, se_lun_group); 139 struct se_node_acl *nacl = lacl->se_lun_nacl; 140 struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun]; 141 struct se_portal_group *se_tpg; 142 /* 143 * Determine if the underlying MappedLUN has already been released.. 144 */ 145 if (!deve->se_lun) 146 return 0; 147 148 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group); 149 se_tpg = lun->lun_sep->sep_tpg; 150 151 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl); 152 return 0; 153} 154 155CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl); 156#define TCM_MAPPEDLUN_ATTR(_name, _mode) \ 157static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \ 158 __CONFIGFS_EATTR(_name, _mode, \ 159 target_fabric_mappedlun_show_##_name, \ 160 target_fabric_mappedlun_store_##_name); 161 162static ssize_t target_fabric_mappedlun_show_write_protect( 163 struct se_lun_acl *lacl, 164 char *page) 165{ 166 struct se_node_acl *se_nacl = lacl->se_lun_nacl; 167 struct se_dev_entry *deve; 168 ssize_t len; 169 170 spin_lock_irq(&se_nacl->device_list_lock); 171 deve = se_nacl->device_list[lacl->mapped_lun]; 172 len = sprintf(page, "%d\n", 173 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 174 1 : 0); 175 spin_unlock_irq(&se_nacl->device_list_lock); 176 177 return len; 178} 179 180static ssize_t target_fabric_mappedlun_store_write_protect( 181 struct se_lun_acl *lacl, 182 const char *page, 183 size_t count) 184{ 185 struct se_node_acl *se_nacl = lacl->se_lun_nacl; 186 struct se_portal_group *se_tpg = se_nacl->se_tpg; 187 unsigned long op; 188 189 if (strict_strtoul(page, 0, &op)) 190 return -EINVAL; 191 192 if ((op != 1) && (op != 0)) 193 return -EINVAL; 194 195 core_update_device_list_access(lacl->mapped_lun, (op) ? 196 TRANSPORT_LUNFLAGS_READ_ONLY : 197 TRANSPORT_LUNFLAGS_READ_WRITE, 198 lacl->se_lun_nacl); 199 200 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s" 201 " Mapped LUN: %u Write Protect bit to %s\n", 202 se_tpg->se_tpg_tfo->get_fabric_name(), 203 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF"); 204 205 return count; 206 207} 208 209TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR); 210 211CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group); 212 213static void target_fabric_mappedlun_release(struct config_item *item) 214{ 215 struct se_lun_acl *lacl = container_of(to_config_group(item), 216 struct se_lun_acl, se_lun_group); 217 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg; 218 219 core_dev_free_initiator_node_lun_acl(se_tpg, lacl); 220} 221 222static struct configfs_attribute *target_fabric_mappedlun_attrs[] = { 223 &target_fabric_mappedlun_write_protect.attr, 224 NULL, 225}; 226 227static struct configfs_item_operations target_fabric_mappedlun_item_ops = { 228 .release = target_fabric_mappedlun_release, 229 .show_attribute = target_fabric_mappedlun_attr_show, 230 .store_attribute = target_fabric_mappedlun_attr_store, 231 .allow_link = target_fabric_mappedlun_link, 232 .drop_link = target_fabric_mappedlun_unlink, 233}; 234 235TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL, 236 target_fabric_mappedlun_attrs); 237 238/* End of tfc_tpg_mappedlun_cit */ 239 240/* Start of tfc_tpg_mappedlun_port_cit */ 241 242static struct config_group *target_core_mappedlun_stat_mkdir( 243 struct config_group *group, 244 const char *name) 245{ 246 return ERR_PTR(-ENOSYS); 247} 248 249static void target_core_mappedlun_stat_rmdir( 250 struct config_group *group, 251 struct config_item *item) 252{ 253 return; 254} 255 256static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = { 257 .make_group = target_core_mappedlun_stat_mkdir, 258 .drop_item = target_core_mappedlun_stat_rmdir, 259}; 260 261TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops, 262 NULL); 263 264/* End of tfc_tpg_mappedlun_port_cit */ 265 266/* Start of tfc_tpg_nacl_attrib_cit */ 267 268CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group); 269 270static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = { 271 .show_attribute = target_fabric_nacl_attrib_attr_show, 272 .store_attribute = target_fabric_nacl_attrib_attr_store, 273}; 274 275TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL); 276 277/* End of tfc_tpg_nacl_attrib_cit */ 278 279/* Start of tfc_tpg_nacl_auth_cit */ 280 281CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group); 282 283static struct configfs_item_operations target_fabric_nacl_auth_item_ops = { 284 .show_attribute = target_fabric_nacl_auth_attr_show, 285 .store_attribute = target_fabric_nacl_auth_attr_store, 286}; 287 288TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL); 289 290/* End of tfc_tpg_nacl_auth_cit */ 291 292/* Start of tfc_tpg_nacl_param_cit */ 293 294CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group); 295 296static struct configfs_item_operations target_fabric_nacl_param_item_ops = { 297 .show_attribute = target_fabric_nacl_param_attr_show, 298 .store_attribute = target_fabric_nacl_param_attr_store, 299}; 300 301TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL); 302 303/* End of tfc_tpg_nacl_param_cit */ 304 305/* Start of tfc_tpg_nacl_base_cit */ 306 307CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group); 308 309static struct config_group *target_fabric_make_mappedlun( 310 struct config_group *group, 311 const char *name) 312{ 313 struct se_node_acl *se_nacl = container_of(group, 314 struct se_node_acl, acl_group); 315 struct se_portal_group *se_tpg = se_nacl->se_tpg; 316 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 317 struct se_lun_acl *lacl; 318 struct config_item *acl_ci; 319 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL; 320 char *buf; 321 unsigned long mapped_lun; 322 int ret = 0; 323 324 acl_ci = &group->cg_item; 325 if (!acl_ci) { 326 pr_err("Unable to locatel acl_ci\n"); 327 return NULL; 328 } 329 330 buf = kzalloc(strlen(name) + 1, GFP_KERNEL); 331 if (!buf) { 332 pr_err("Unable to allocate memory for name buf\n"); 333 return ERR_PTR(-ENOMEM); 334 } 335 snprintf(buf, strlen(name) + 1, "%s", name); 336 /* 337 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID. 338 */ 339 if (strstr(buf, "lun_") != buf) { 340 pr_err("Unable to locate \"lun_\" from buf: %s" 341 " name: %s\n", buf, name); 342 ret = -EINVAL; 343 goto out; 344 } 345 /* 346 * Determine the Mapped LUN value. This is what the SCSI Initiator 347 * Port will actually see. 348 */ 349 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) { 350 ret = -EINVAL; 351 goto out; 352 } 353 354 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun, 355 config_item_name(acl_ci), &ret); 356 if (!lacl) { 357 ret = -EINVAL; 358 goto out; 359 } 360 361 lacl_cg = &lacl->se_lun_group; 362 lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2, 363 GFP_KERNEL); 364 if (!lacl_cg->default_groups) { 365 pr_err("Unable to allocate lacl_cg->default_groups\n"); 366 ret = -ENOMEM; 367 goto out; 368 } 369 370 config_group_init_type_name(&lacl->se_lun_group, name, 371 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit); 372 config_group_init_type_name(&lacl->ml_stat_grps.stat_group, 373 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit); 374 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group; 375 lacl_cg->default_groups[1] = NULL; 376 377 ml_stat_grp = &lacl->ml_stat_grps.stat_group; 378 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3, 379 GFP_KERNEL); 380 if (!ml_stat_grp->default_groups) { 381 pr_err("Unable to allocate ml_stat_grp->default_groups\n"); 382 ret = -ENOMEM; 383 goto out; 384 } 385 target_stat_setup_mappedlun_default_groups(lacl); 386 387 kfree(buf); 388 return &lacl->se_lun_group; 389out: 390 if (lacl_cg) 391 kfree(lacl_cg->default_groups); 392 kfree(buf); 393 return ERR_PTR(ret); 394} 395 396static void target_fabric_drop_mappedlun( 397 struct config_group *group, 398 struct config_item *item) 399{ 400 struct se_lun_acl *lacl = container_of(to_config_group(item), 401 struct se_lun_acl, se_lun_group); 402 struct config_item *df_item; 403 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL; 404 int i; 405 406 ml_stat_grp = &lacl->ml_stat_grps.stat_group; 407 for (i = 0; ml_stat_grp->default_groups[i]; i++) { 408 df_item = &ml_stat_grp->default_groups[i]->cg_item; 409 ml_stat_grp->default_groups[i] = NULL; 410 config_item_put(df_item); 411 } 412 kfree(ml_stat_grp->default_groups); 413 414 lacl_cg = &lacl->se_lun_group; 415 for (i = 0; lacl_cg->default_groups[i]; i++) { 416 df_item = &lacl_cg->default_groups[i]->cg_item; 417 lacl_cg->default_groups[i] = NULL; 418 config_item_put(df_item); 419 } 420 kfree(lacl_cg->default_groups); 421 422 config_item_put(item); 423} 424 425static void target_fabric_nacl_base_release(struct config_item *item) 426{ 427 struct se_node_acl *se_nacl = container_of(to_config_group(item), 428 struct se_node_acl, acl_group); 429 struct se_portal_group *se_tpg = se_nacl->se_tpg; 430 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 431 432 tf->tf_ops.fabric_drop_nodeacl(se_nacl); 433} 434 435static struct configfs_item_operations target_fabric_nacl_base_item_ops = { 436 .release = target_fabric_nacl_base_release, 437 .show_attribute = target_fabric_nacl_base_attr_show, 438 .store_attribute = target_fabric_nacl_base_attr_store, 439}; 440 441static struct configfs_group_operations target_fabric_nacl_base_group_ops = { 442 .make_group = target_fabric_make_mappedlun, 443 .drop_item = target_fabric_drop_mappedlun, 444}; 445 446TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops, 447 &target_fabric_nacl_base_group_ops, NULL); 448 449/* End of tfc_tpg_nacl_base_cit */ 450 451/* Start of tfc_node_fabric_stats_cit */ 452/* 453 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group 454 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[] 455 */ 456TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL); 457 458/* End of tfc_wwn_fabric_stats_cit */ 459 460/* Start of tfc_tpg_nacl_cit */ 461 462static struct config_group *target_fabric_make_nodeacl( 463 struct config_group *group, 464 const char *name) 465{ 466 struct se_portal_group *se_tpg = container_of(group, 467 struct se_portal_group, tpg_acl_group); 468 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 469 struct se_node_acl *se_nacl; 470 struct config_group *nacl_cg; 471 472 if (!tf->tf_ops.fabric_make_nodeacl) { 473 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n"); 474 return ERR_PTR(-ENOSYS); 475 } 476 477 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name); 478 if (IS_ERR(se_nacl)) 479 return ERR_CAST(se_nacl); 480 481 nacl_cg = &se_nacl->acl_group; 482 nacl_cg->default_groups = se_nacl->acl_default_groups; 483 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group; 484 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group; 485 nacl_cg->default_groups[2] = &se_nacl->acl_param_group; 486 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group; 487 nacl_cg->default_groups[4] = NULL; 488 489 config_group_init_type_name(&se_nacl->acl_group, name, 490 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit); 491 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib", 492 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit); 493 config_group_init_type_name(&se_nacl->acl_auth_group, "auth", 494 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit); 495 config_group_init_type_name(&se_nacl->acl_param_group, "param", 496 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit); 497 config_group_init_type_name(&se_nacl->acl_fabric_stat_group, 498 "fabric_statistics", 499 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit); 500 501 return &se_nacl->acl_group; 502} 503 504static void target_fabric_drop_nodeacl( 505 struct config_group *group, 506 struct config_item *item) 507{ 508 struct se_node_acl *se_nacl = container_of(to_config_group(item), 509 struct se_node_acl, acl_group); 510 struct config_item *df_item; 511 struct config_group *nacl_cg; 512 int i; 513 514 nacl_cg = &se_nacl->acl_group; 515 for (i = 0; nacl_cg->default_groups[i]; i++) { 516 df_item = &nacl_cg->default_groups[i]->cg_item; 517 nacl_cg->default_groups[i] = NULL; 518 config_item_put(df_item); 519 } 520 /* 521 * struct se_node_acl free is done in target_fabric_nacl_base_release() 522 */ 523 config_item_put(item); 524} 525 526static struct configfs_group_operations target_fabric_nacl_group_ops = { 527 .make_group = target_fabric_make_nodeacl, 528 .drop_item = target_fabric_drop_nodeacl, 529}; 530 531TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL); 532 533/* End of tfc_tpg_nacl_cit */ 534 535/* Start of tfc_tpg_np_base_cit */ 536 537CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group); 538 539static void target_fabric_np_base_release(struct config_item *item) 540{ 541 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item), 542 struct se_tpg_np, tpg_np_group); 543 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent; 544 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 545 546 tf->tf_ops.fabric_drop_np(se_tpg_np); 547} 548 549static struct configfs_item_operations target_fabric_np_base_item_ops = { 550 .release = target_fabric_np_base_release, 551 .show_attribute = target_fabric_np_base_attr_show, 552 .store_attribute = target_fabric_np_base_attr_store, 553}; 554 555TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL); 556 557/* End of tfc_tpg_np_base_cit */ 558 559/* Start of tfc_tpg_np_cit */ 560 561static struct config_group *target_fabric_make_np( 562 struct config_group *group, 563 const char *name) 564{ 565 struct se_portal_group *se_tpg = container_of(group, 566 struct se_portal_group, tpg_np_group); 567 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 568 struct se_tpg_np *se_tpg_np; 569 570 if (!tf->tf_ops.fabric_make_np) { 571 pr_err("tf->tf_ops.fabric_make_np is NULL\n"); 572 return ERR_PTR(-ENOSYS); 573 } 574 575 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name); 576 if (!se_tpg_np || IS_ERR(se_tpg_np)) 577 return ERR_PTR(-EINVAL); 578 579 se_tpg_np->tpg_np_parent = se_tpg; 580 config_group_init_type_name(&se_tpg_np->tpg_np_group, name, 581 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit); 582 583 return &se_tpg_np->tpg_np_group; 584} 585 586static void target_fabric_drop_np( 587 struct config_group *group, 588 struct config_item *item) 589{ 590 /* 591 * struct se_tpg_np is released via target_fabric_np_base_release() 592 */ 593 config_item_put(item); 594} 595 596static struct configfs_group_operations target_fabric_np_group_ops = { 597 .make_group = &target_fabric_make_np, 598 .drop_item = &target_fabric_drop_np, 599}; 600 601TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL); 602 603/* End of tfc_tpg_np_cit */ 604 605/* Start of tfc_tpg_port_cit */ 606 607CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun); 608#define TCM_PORT_ATTR(_name, _mode) \ 609static struct target_fabric_port_attribute target_fabric_port_##_name = \ 610 __CONFIGFS_EATTR(_name, _mode, \ 611 target_fabric_port_show_attr_##_name, \ 612 target_fabric_port_store_attr_##_name); 613 614#define TCM_PORT_ATTOR_RO(_name) \ 615 __CONFIGFS_EATTR_RO(_name, \ 616 target_fabric_port_show_attr_##_name); 617 618/* 619 * alua_tg_pt_gp 620 */ 621static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp( 622 struct se_lun *lun, 623 char *page) 624{ 625 if (!lun || !lun->lun_sep) 626 return -ENODEV; 627 628 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page); 629} 630 631static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp( 632 struct se_lun *lun, 633 const char *page, 634 size_t count) 635{ 636 if (!lun || !lun->lun_sep) 637 return -ENODEV; 638 639 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count); 640} 641 642TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR); 643 644/* 645 * alua_tg_pt_offline 646 */ 647static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline( 648 struct se_lun *lun, 649 char *page) 650{ 651 if (!lun || !lun->lun_sep) 652 return -ENODEV; 653 654 return core_alua_show_offline_bit(lun, page); 655} 656 657static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline( 658 struct se_lun *lun, 659 const char *page, 660 size_t count) 661{ 662 if (!lun || !lun->lun_sep) 663 return -ENODEV; 664 665 return core_alua_store_offline_bit(lun, page, count); 666} 667 668TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR); 669 670/* 671 * alua_tg_pt_status 672 */ 673static ssize_t target_fabric_port_show_attr_alua_tg_pt_status( 674 struct se_lun *lun, 675 char *page) 676{ 677 if (!lun || !lun->lun_sep) 678 return -ENODEV; 679 680 return core_alua_show_secondary_status(lun, page); 681} 682 683static ssize_t target_fabric_port_store_attr_alua_tg_pt_status( 684 struct se_lun *lun, 685 const char *page, 686 size_t count) 687{ 688 if (!lun || !lun->lun_sep) 689 return -ENODEV; 690 691 return core_alua_store_secondary_status(lun, page, count); 692} 693 694TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR); 695 696/* 697 * alua_tg_pt_write_md 698 */ 699static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md( 700 struct se_lun *lun, 701 char *page) 702{ 703 if (!lun || !lun->lun_sep) 704 return -ENODEV; 705 706 return core_alua_show_secondary_write_metadata(lun, page); 707} 708 709static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md( 710 struct se_lun *lun, 711 const char *page, 712 size_t count) 713{ 714 if (!lun || !lun->lun_sep) 715 return -ENODEV; 716 717 return core_alua_store_secondary_write_metadata(lun, page, count); 718} 719 720TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR); 721 722 723static struct configfs_attribute *target_fabric_port_attrs[] = { 724 &target_fabric_port_alua_tg_pt_gp.attr, 725 &target_fabric_port_alua_tg_pt_offline.attr, 726 &target_fabric_port_alua_tg_pt_status.attr, 727 &target_fabric_port_alua_tg_pt_write_md.attr, 728 NULL, 729}; 730 731CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group); 732 733static int target_fabric_port_link( 734 struct config_item *lun_ci, 735 struct config_item *se_dev_ci) 736{ 737 struct config_item *tpg_ci; 738 struct se_device *dev; 739 struct se_lun *lun = container_of(to_config_group(lun_ci), 740 struct se_lun, lun_group); 741 struct se_lun *lun_p; 742 struct se_portal_group *se_tpg; 743 struct se_subsystem_dev *se_dev = container_of( 744 to_config_group(se_dev_ci), struct se_subsystem_dev, 745 se_dev_group); 746 struct target_fabric_configfs *tf; 747 int ret; 748 749 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item; 750 se_tpg = container_of(to_config_group(tpg_ci), 751 struct se_portal_group, tpg_group); 752 tf = se_tpg->se_tpg_wwn->wwn_tf; 753 754 if (lun->lun_se_dev != NULL) { 755 pr_err("Port Symlink already exists\n"); 756 return -EEXIST; 757 } 758 759 dev = se_dev->se_dev_ptr; 760 if (!dev) { 761 pr_err("Unable to locate struct se_device pointer from" 762 " %s\n", config_item_name(se_dev_ci)); 763 ret = -ENODEV; 764 goto out; 765 } 766 767 lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev, 768 lun->unpacked_lun); 769 if (IS_ERR(lun_p)) { 770 pr_err("core_dev_add_lun() failed\n"); 771 ret = PTR_ERR(lun_p); 772 goto out; 773 } 774 775 if (tf->tf_ops.fabric_post_link) { 776 /* 777 * Call the optional fabric_post_link() to allow a 778 * fabric module to setup any additional state once 779 * core_dev_add_lun() has been called.. 780 */ 781 tf->tf_ops.fabric_post_link(se_tpg, lun); 782 } 783 784 return 0; 785out: 786 return ret; 787} 788 789static int target_fabric_port_unlink( 790 struct config_item *lun_ci, 791 struct config_item *se_dev_ci) 792{ 793 struct se_lun *lun = container_of(to_config_group(lun_ci), 794 struct se_lun, lun_group); 795 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg; 796 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 797 798 if (tf->tf_ops.fabric_pre_unlink) { 799 /* 800 * Call the optional fabric_pre_unlink() to allow a 801 * fabric module to release any additional stat before 802 * core_dev_del_lun() is called. 803 */ 804 tf->tf_ops.fabric_pre_unlink(se_tpg, lun); 805 } 806 807 core_dev_del_lun(se_tpg, lun->unpacked_lun); 808 return 0; 809} 810 811static struct configfs_item_operations target_fabric_port_item_ops = { 812 .show_attribute = target_fabric_port_attr_show, 813 .store_attribute = target_fabric_port_attr_store, 814 .allow_link = target_fabric_port_link, 815 .drop_link = target_fabric_port_unlink, 816}; 817 818TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs); 819 820/* End of tfc_tpg_port_cit */ 821 822/* Start of tfc_tpg_port_stat_cit */ 823 824static struct config_group *target_core_port_stat_mkdir( 825 struct config_group *group, 826 const char *name) 827{ 828 return ERR_PTR(-ENOSYS); 829} 830 831static void target_core_port_stat_rmdir( 832 struct config_group *group, 833 struct config_item *item) 834{ 835 return; 836} 837 838static struct configfs_group_operations target_fabric_port_stat_group_ops = { 839 .make_group = target_core_port_stat_mkdir, 840 .drop_item = target_core_port_stat_rmdir, 841}; 842 843TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL); 844 845/* End of tfc_tpg_port_stat_cit */ 846 847/* Start of tfc_tpg_lun_cit */ 848 849static struct config_group *target_fabric_make_lun( 850 struct config_group *group, 851 const char *name) 852{ 853 struct se_lun *lun; 854 struct se_portal_group *se_tpg = container_of(group, 855 struct se_portal_group, tpg_lun_group); 856 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 857 struct config_group *lun_cg = NULL, *port_stat_grp = NULL; 858 unsigned long unpacked_lun; 859 int errno; 860 861 if (strstr(name, "lun_") != name) { 862 pr_err("Unable to locate \'_\" in" 863 " \"lun_$LUN_NUMBER\"\n"); 864 return ERR_PTR(-EINVAL); 865 } 866 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX) 867 return ERR_PTR(-EINVAL); 868 869 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun); 870 if (!lun) 871 return ERR_PTR(-EINVAL); 872 873 lun_cg = &lun->lun_group; 874 lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2, 875 GFP_KERNEL); 876 if (!lun_cg->default_groups) { 877 pr_err("Unable to allocate lun_cg->default_groups\n"); 878 return ERR_PTR(-ENOMEM); 879 } 880 881 config_group_init_type_name(&lun->lun_group, name, 882 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit); 883 config_group_init_type_name(&lun->port_stat_grps.stat_group, 884 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit); 885 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group; 886 lun_cg->default_groups[1] = NULL; 887 888 port_stat_grp = &lun->port_stat_grps.stat_group; 889 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3, 890 GFP_KERNEL); 891 if (!port_stat_grp->default_groups) { 892 pr_err("Unable to allocate port_stat_grp->default_groups\n"); 893 errno = -ENOMEM; 894 goto out; 895 } 896 target_stat_setup_port_default_groups(lun); 897 898 return &lun->lun_group; 899out: 900 if (lun_cg) 901 kfree(lun_cg->default_groups); 902 return ERR_PTR(errno); 903} 904 905static void target_fabric_drop_lun( 906 struct config_group *group, 907 struct config_item *item) 908{ 909 struct se_lun *lun = container_of(to_config_group(item), 910 struct se_lun, lun_group); 911 struct config_item *df_item; 912 struct config_group *lun_cg, *port_stat_grp; 913 int i; 914 915 port_stat_grp = &lun->port_stat_grps.stat_group; 916 for (i = 0; port_stat_grp->default_groups[i]; i++) { 917 df_item = &port_stat_grp->default_groups[i]->cg_item; 918 port_stat_grp->default_groups[i] = NULL; 919 config_item_put(df_item); 920 } 921 kfree(port_stat_grp->default_groups); 922 923 lun_cg = &lun->lun_group; 924 for (i = 0; lun_cg->default_groups[i]; i++) { 925 df_item = &lun_cg->default_groups[i]->cg_item; 926 lun_cg->default_groups[i] = NULL; 927 config_item_put(df_item); 928 } 929 kfree(lun_cg->default_groups); 930 931 config_item_put(item); 932} 933 934static struct configfs_group_operations target_fabric_lun_group_ops = { 935 .make_group = &target_fabric_make_lun, 936 .drop_item = &target_fabric_drop_lun, 937}; 938 939TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL); 940 941/* End of tfc_tpg_lun_cit */ 942 943/* Start of tfc_tpg_attrib_cit */ 944 945CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group); 946 947static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = { 948 .show_attribute = target_fabric_tpg_attrib_attr_show, 949 .store_attribute = target_fabric_tpg_attrib_attr_store, 950}; 951 952TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL); 953 954/* End of tfc_tpg_attrib_cit */ 955 956/* Start of tfc_tpg_param_cit */ 957 958CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group); 959 960static struct configfs_item_operations target_fabric_tpg_param_item_ops = { 961 .show_attribute = target_fabric_tpg_param_attr_show, 962 .store_attribute = target_fabric_tpg_param_attr_store, 963}; 964 965TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL); 966 967/* End of tfc_tpg_param_cit */ 968 969/* Start of tfc_tpg_base_cit */ 970/* 971 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO() 972 */ 973CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group); 974 975static void target_fabric_tpg_release(struct config_item *item) 976{ 977 struct se_portal_group *se_tpg = container_of(to_config_group(item), 978 struct se_portal_group, tpg_group); 979 struct se_wwn *wwn = se_tpg->se_tpg_wwn; 980 struct target_fabric_configfs *tf = wwn->wwn_tf; 981 982 tf->tf_ops.fabric_drop_tpg(se_tpg); 983} 984 985static struct configfs_item_operations target_fabric_tpg_base_item_ops = { 986 .release = target_fabric_tpg_release, 987 .show_attribute = target_fabric_tpg_attr_show, 988 .store_attribute = target_fabric_tpg_attr_store, 989}; 990 991TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL); 992 993/* End of tfc_tpg_base_cit */ 994 995/* Start of tfc_tpg_cit */ 996 997static struct config_group *target_fabric_make_tpg( 998 struct config_group *group, 999 const char *name) 1000{ 1001 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group); 1002 struct target_fabric_configfs *tf = wwn->wwn_tf; 1003 struct se_portal_group *se_tpg; 1004 1005 if (!tf->tf_ops.fabric_make_tpg) { 1006 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n"); 1007 return ERR_PTR(-ENOSYS); 1008 } 1009 1010 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name); 1011 if (!se_tpg || IS_ERR(se_tpg)) 1012 return ERR_PTR(-EINVAL); 1013 /* 1014 * Setup default groups from pre-allocated se_tpg->tpg_default_groups 1015 */ 1016 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups; 1017 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group; 1018 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group; 1019 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group; 1020 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group; 1021 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group; 1022 se_tpg->tpg_group.default_groups[5] = NULL; 1023 1024 config_group_init_type_name(&se_tpg->tpg_group, name, 1025 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit); 1026 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun", 1027 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit); 1028 config_group_init_type_name(&se_tpg->tpg_np_group, "np", 1029 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit); 1030 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls", 1031 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit); 1032 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib", 1033 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit); 1034 config_group_init_type_name(&se_tpg->tpg_param_group, "param", 1035 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit); 1036 1037 return &se_tpg->tpg_group; 1038} 1039 1040static void target_fabric_drop_tpg( 1041 struct config_group *group, 1042 struct config_item *item) 1043{ 1044 struct se_portal_group *se_tpg = container_of(to_config_group(item), 1045 struct se_portal_group, tpg_group); 1046 struct config_group *tpg_cg = &se_tpg->tpg_group; 1047 struct config_item *df_item; 1048 int i; 1049 /* 1050 * Release default groups, but do not release tpg_cg->default_groups 1051 * memory as it is statically allocated at se_tpg->tpg_default_groups. 1052 */ 1053 for (i = 0; tpg_cg->default_groups[i]; i++) { 1054 df_item = &tpg_cg->default_groups[i]->cg_item; 1055 tpg_cg->default_groups[i] = NULL; 1056 config_item_put(df_item); 1057 } 1058 1059 config_item_put(item); 1060} 1061 1062static void target_fabric_release_wwn(struct config_item *item) 1063{ 1064 struct se_wwn *wwn = container_of(to_config_group(item), 1065 struct se_wwn, wwn_group); 1066 struct target_fabric_configfs *tf = wwn->wwn_tf; 1067 1068 tf->tf_ops.fabric_drop_wwn(wwn); 1069} 1070 1071static struct configfs_item_operations target_fabric_tpg_item_ops = { 1072 .release = target_fabric_release_wwn, 1073}; 1074 1075static struct configfs_group_operations target_fabric_tpg_group_ops = { 1076 .make_group = target_fabric_make_tpg, 1077 .drop_item = target_fabric_drop_tpg, 1078}; 1079 1080TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops, 1081 NULL); 1082 1083/* End of tfc_tpg_cit */ 1084 1085/* Start of tfc_wwn_fabric_stats_cit */ 1086/* 1087 * This is used as a placeholder for struct se_wwn->fabric_stat_group 1088 * to allow fabrics access to ->fabric_stat_group->default_groups[] 1089 */ 1090TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL); 1091 1092/* End of tfc_wwn_fabric_stats_cit */ 1093 1094/* Start of tfc_wwn_cit */ 1095 1096static struct config_group *target_fabric_make_wwn( 1097 struct config_group *group, 1098 const char *name) 1099{ 1100 struct target_fabric_configfs *tf = container_of(group, 1101 struct target_fabric_configfs, tf_group); 1102 struct se_wwn *wwn; 1103 1104 if (!tf->tf_ops.fabric_make_wwn) { 1105 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n"); 1106 return ERR_PTR(-ENOSYS); 1107 } 1108 1109 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name); 1110 if (!wwn || IS_ERR(wwn)) 1111 return ERR_PTR(-EINVAL); 1112 1113 wwn->wwn_tf = tf; 1114 /* 1115 * Setup default groups from pre-allocated wwn->wwn_default_groups 1116 */ 1117 wwn->wwn_group.default_groups = wwn->wwn_default_groups; 1118 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group; 1119 wwn->wwn_group.default_groups[1] = NULL; 1120 1121 config_group_init_type_name(&wwn->wwn_group, name, 1122 &TF_CIT_TMPL(tf)->tfc_tpg_cit); 1123 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics", 1124 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit); 1125 1126 return &wwn->wwn_group; 1127} 1128 1129static void target_fabric_drop_wwn( 1130 struct config_group *group, 1131 struct config_item *item) 1132{ 1133 struct se_wwn *wwn = container_of(to_config_group(item), 1134 struct se_wwn, wwn_group); 1135 struct config_item *df_item; 1136 struct config_group *cg = &wwn->wwn_group; 1137 int i; 1138 1139 for (i = 0; cg->default_groups[i]; i++) { 1140 df_item = &cg->default_groups[i]->cg_item; 1141 cg->default_groups[i] = NULL; 1142 config_item_put(df_item); 1143 } 1144 1145 config_item_put(item); 1146} 1147 1148static struct configfs_group_operations target_fabric_wwn_group_ops = { 1149 .make_group = target_fabric_make_wwn, 1150 .drop_item = target_fabric_drop_wwn, 1151}; 1152/* 1153 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO() 1154 */ 1155CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group); 1156 1157static struct configfs_item_operations target_fabric_wwn_item_ops = { 1158 .show_attribute = target_fabric_wwn_attr_show, 1159 .store_attribute = target_fabric_wwn_attr_store, 1160}; 1161 1162TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL); 1163 1164/* End of tfc_wwn_cit */ 1165 1166/* Start of tfc_discovery_cit */ 1167 1168CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs, 1169 tf_disc_group); 1170 1171static struct configfs_item_operations target_fabric_discovery_item_ops = { 1172 .show_attribute = target_fabric_discovery_attr_show, 1173 .store_attribute = target_fabric_discovery_attr_store, 1174}; 1175 1176TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL); 1177 1178/* End of tfc_discovery_cit */ 1179 1180int target_fabric_setup_cits(struct target_fabric_configfs *tf) 1181{ 1182 target_fabric_setup_discovery_cit(tf); 1183 target_fabric_setup_wwn_cit(tf); 1184 target_fabric_setup_wwn_fabric_stats_cit(tf); 1185 target_fabric_setup_tpg_cit(tf); 1186 target_fabric_setup_tpg_base_cit(tf); 1187 target_fabric_setup_tpg_port_cit(tf); 1188 target_fabric_setup_tpg_port_stat_cit(tf); 1189 target_fabric_setup_tpg_lun_cit(tf); 1190 target_fabric_setup_tpg_np_cit(tf); 1191 target_fabric_setup_tpg_np_base_cit(tf); 1192 target_fabric_setup_tpg_attrib_cit(tf); 1193 target_fabric_setup_tpg_param_cit(tf); 1194 target_fabric_setup_tpg_nacl_cit(tf); 1195 target_fabric_setup_tpg_nacl_base_cit(tf); 1196 target_fabric_setup_tpg_nacl_attrib_cit(tf); 1197 target_fabric_setup_tpg_nacl_auth_cit(tf); 1198 target_fabric_setup_tpg_nacl_param_cit(tf); 1199 target_fabric_setup_tpg_nacl_stat_cit(tf); 1200 target_fabric_setup_tpg_mappedlun_cit(tf); 1201 target_fabric_setup_tpg_mappedlun_stat_cit(tf); 1202 1203 return 0; 1204} 1205