1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * (c) Copyright 2010-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/utsname.h>
25 #include <linux/init.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>
36 #include <target/target_core_base.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_fabric_configfs.h>
39 #include <target/target_core_configfs.h>
40 #include <target/configfs_macros.h>
42 #include "target_core_internal.h"
43 #include "target_core_alua.h"
44 #include "target_core_pr.h"
46 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
47 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
49 struct config_item_type *cit = &tf->tf_##_name##_cit; \
51 cit->ct_item_ops = _item_ops; \
52 cit->ct_group_ops = _group_ops; \
53 cit->ct_attrs = _attrs; \
54 cit->ct_owner = tf->tf_ops->module; \
55 pr_debug("Setup generic %s\n", __stringify(_name)); \
58 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
59 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
61 struct config_item_type *cit = &tf->tf_##_name##_cit; \
62 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
64 cit->ct_item_ops = _item_ops; \
65 cit->ct_group_ops = _group_ops; \
66 cit->ct_attrs = attrs; \
67 cit->ct_owner = tf->tf_ops->module; \
68 pr_debug("Setup generic %s\n", __stringify(_name)); \
71 /* Start of tfc_tpg_mappedlun_cit */
73 static int target_fabric_mappedlun_link(
74 struct config_item *lun_acl_ci,
75 struct config_item *lun_ci)
77 struct se_dev_entry *deve;
78 struct se_lun *lun = container_of(to_config_group(lun_ci),
79 struct se_lun, lun_group);
80 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
81 struct se_lun_acl, se_lun_group);
82 struct se_portal_group *se_tpg;
83 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
84 int ret = 0, lun_access;
86 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
87 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
88 " %p to struct lun: %p\n", lun_ci, lun);
92 * Ensure that the source port exists
94 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
95 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
96 "_tpg does not exist\n");
99 se_tpg = lun->lun_sep->sep_tpg;
101 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
102 tpg_ci = &nacl_ci->ci_group->cg_item;
103 wwn_ci = &tpg_ci->ci_group->cg_item;
104 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
105 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
107 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
109 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
110 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
111 config_item_name(wwn_ci));
114 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
115 pr_err("Illegal Initiator ACL Symlink outside of %s"
116 " TPGT: %s\n", config_item_name(wwn_ci),
117 config_item_name(tpg_ci));
121 * If this struct se_node_acl was dynamically generated with
122 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
123 * which be will write protected (READ-ONLY) when
124 * tpg_1/attrib/demo_mode_write_protect=1
127 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
129 lun_access = deve->lun_flags;
132 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
133 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
134 TRANSPORT_LUNFLAGS_READ_WRITE;
137 * Determine the actual mapped LUN value user wants..
139 * This value is what the SCSI Initiator actually sees the
140 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
142 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
143 lun->unpacked_lun, lun_access);
145 return (ret < 0) ? -EINVAL : 0;
148 static int target_fabric_mappedlun_unlink(
149 struct config_item *lun_acl_ci,
150 struct config_item *lun_ci)
152 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
153 struct se_lun_acl, se_lun_group);
154 struct se_lun *lun = container_of(to_config_group(lun_ci),
155 struct se_lun, lun_group);
156 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
158 return core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
161 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
162 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
163 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
164 __CONFIGFS_EATTR(_name, _mode, \
165 target_fabric_mappedlun_show_##_name, \
166 target_fabric_mappedlun_store_##_name);
168 static ssize_t target_fabric_mappedlun_show_write_protect(
169 struct se_lun_acl *lacl,
172 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
173 struct se_dev_entry *deve;
177 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
179 len = sprintf(page, "%d\n",
180 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
187 static ssize_t target_fabric_mappedlun_store_write_protect(
188 struct se_lun_acl *lacl,
192 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
193 struct se_portal_group *se_tpg = se_nacl->se_tpg;
197 ret = kstrtoul(page, 0, &op);
201 if ((op != 1) && (op != 0))
204 core_update_device_list_access(lacl->mapped_lun, (op) ?
205 TRANSPORT_LUNFLAGS_READ_ONLY :
206 TRANSPORT_LUNFLAGS_READ_WRITE,
209 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
210 " Mapped LUN: %u Write Protect bit to %s\n",
211 se_tpg->se_tpg_tfo->get_fabric_name(),
212 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
218 TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
220 CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
222 static void target_fabric_mappedlun_release(struct config_item *item)
224 struct se_lun_acl *lacl = container_of(to_config_group(item),
225 struct se_lun_acl, se_lun_group);
226 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
228 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
231 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
232 &target_fabric_mappedlun_write_protect.attr,
236 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
237 .release = target_fabric_mappedlun_release,
238 .show_attribute = target_fabric_mappedlun_attr_show,
239 .store_attribute = target_fabric_mappedlun_attr_store,
240 .allow_link = target_fabric_mappedlun_link,
241 .drop_link = target_fabric_mappedlun_unlink,
244 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
245 target_fabric_mappedlun_attrs);
247 /* End of tfc_tpg_mappedlun_cit */
249 /* Start of tfc_tpg_mappedlun_port_cit */
251 static struct config_group *target_core_mappedlun_stat_mkdir(
252 struct config_group *group,
255 return ERR_PTR(-ENOSYS);
258 static void target_core_mappedlun_stat_rmdir(
259 struct config_group *group,
260 struct config_item *item)
265 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
266 .make_group = target_core_mappedlun_stat_mkdir,
267 .drop_item = target_core_mappedlun_stat_rmdir,
270 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
273 /* End of tfc_tpg_mappedlun_port_cit */
275 /* Start of tfc_tpg_nacl_attrib_cit */
277 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
279 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
280 .show_attribute = target_fabric_nacl_attrib_attr_show,
281 .store_attribute = target_fabric_nacl_attrib_attr_store,
284 TF_CIT_SETUP_DRV(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL);
286 /* End of tfc_tpg_nacl_attrib_cit */
288 /* Start of tfc_tpg_nacl_auth_cit */
290 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
292 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
293 .show_attribute = target_fabric_nacl_auth_attr_show,
294 .store_attribute = target_fabric_nacl_auth_attr_store,
297 TF_CIT_SETUP_DRV(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL);
299 /* End of tfc_tpg_nacl_auth_cit */
301 /* Start of tfc_tpg_nacl_param_cit */
303 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
305 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
306 .show_attribute = target_fabric_nacl_param_attr_show,
307 .store_attribute = target_fabric_nacl_param_attr_store,
310 TF_CIT_SETUP_DRV(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL);
312 /* End of tfc_tpg_nacl_param_cit */
314 /* Start of tfc_tpg_nacl_base_cit */
316 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
318 static struct config_group *target_fabric_make_mappedlun(
319 struct config_group *group,
322 struct se_node_acl *se_nacl = container_of(group,
323 struct se_node_acl, acl_group);
324 struct se_portal_group *se_tpg = se_nacl->se_tpg;
325 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
326 struct se_lun_acl *lacl = NULL;
327 struct config_item *acl_ci;
328 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
330 unsigned long mapped_lun;
333 acl_ci = &group->cg_item;
335 pr_err("Unable to locatel acl_ci\n");
339 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
341 pr_err("Unable to allocate memory for name buf\n");
342 return ERR_PTR(-ENOMEM);
344 snprintf(buf, strlen(name) + 1, "%s", name);
346 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
348 if (strstr(buf, "lun_") != buf) {
349 pr_err("Unable to locate \"lun_\" from buf: %s"
350 " name: %s\n", buf, name);
355 * Determine the Mapped LUN value. This is what the SCSI Initiator
356 * Port will actually see.
358 ret = kstrtoul(buf + 4, 0, &mapped_lun);
361 if (mapped_lun > UINT_MAX) {
365 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
366 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
367 "-1: %u for Target Portal Group: %u\n", mapped_lun,
368 TRANSPORT_MAX_LUNS_PER_TPG-1,
369 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
374 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
381 lacl_cg = &lacl->se_lun_group;
382 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
384 if (!lacl_cg->default_groups) {
385 pr_err("Unable to allocate lacl_cg->default_groups\n");
390 config_group_init_type_name(&lacl->se_lun_group, name,
391 &tf->tf_tpg_mappedlun_cit);
392 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
393 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
394 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
395 lacl_cg->default_groups[1] = NULL;
397 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
398 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
400 if (!ml_stat_grp->default_groups) {
401 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
405 target_stat_setup_mappedlun_default_groups(lacl);
408 return &lacl->se_lun_group;
411 kfree(lacl_cg->default_groups);
417 static void target_fabric_drop_mappedlun(
418 struct config_group *group,
419 struct config_item *item)
421 struct se_lun_acl *lacl = container_of(to_config_group(item),
422 struct se_lun_acl, se_lun_group);
423 struct config_item *df_item;
424 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
427 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
428 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
429 df_item = &ml_stat_grp->default_groups[i]->cg_item;
430 ml_stat_grp->default_groups[i] = NULL;
431 config_item_put(df_item);
433 kfree(ml_stat_grp->default_groups);
435 lacl_cg = &lacl->se_lun_group;
436 for (i = 0; lacl_cg->default_groups[i]; i++) {
437 df_item = &lacl_cg->default_groups[i]->cg_item;
438 lacl_cg->default_groups[i] = NULL;
439 config_item_put(df_item);
441 kfree(lacl_cg->default_groups);
443 config_item_put(item);
446 static void target_fabric_nacl_base_release(struct config_item *item)
448 struct se_node_acl *se_nacl = container_of(to_config_group(item),
449 struct se_node_acl, acl_group);
450 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
452 if (tf->tf_ops->fabric_cleanup_nodeacl)
453 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
454 core_tpg_del_initiator_node_acl(se_nacl);
457 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
458 .release = target_fabric_nacl_base_release,
459 .show_attribute = target_fabric_nacl_base_attr_show,
460 .store_attribute = target_fabric_nacl_base_attr_store,
463 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
464 .make_group = target_fabric_make_mappedlun,
465 .drop_item = target_fabric_drop_mappedlun,
468 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
469 &target_fabric_nacl_base_group_ops);
471 /* End of tfc_tpg_nacl_base_cit */
473 /* Start of tfc_node_fabric_stats_cit */
475 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
476 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
478 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
480 /* End of tfc_wwn_fabric_stats_cit */
482 /* Start of tfc_tpg_nacl_cit */
484 static struct config_group *target_fabric_make_nodeacl(
485 struct config_group *group,
488 struct se_portal_group *se_tpg = container_of(group,
489 struct se_portal_group, tpg_acl_group);
490 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
491 struct se_node_acl *se_nacl;
492 struct config_group *nacl_cg;
494 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
496 return ERR_CAST(se_nacl);
498 if (tf->tf_ops->fabric_init_nodeacl) {
499 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
501 core_tpg_del_initiator_node_acl(se_nacl);
506 nacl_cg = &se_nacl->acl_group;
507 nacl_cg->default_groups = se_nacl->acl_default_groups;
508 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
509 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
510 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
511 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
512 nacl_cg->default_groups[4] = NULL;
514 config_group_init_type_name(&se_nacl->acl_group, name,
515 &tf->tf_tpg_nacl_base_cit);
516 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
517 &tf->tf_tpg_nacl_attrib_cit);
518 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
519 &tf->tf_tpg_nacl_auth_cit);
520 config_group_init_type_name(&se_nacl->acl_param_group, "param",
521 &tf->tf_tpg_nacl_param_cit);
522 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
523 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
525 return &se_nacl->acl_group;
528 static void target_fabric_drop_nodeacl(
529 struct config_group *group,
530 struct config_item *item)
532 struct se_node_acl *se_nacl = container_of(to_config_group(item),
533 struct se_node_acl, acl_group);
534 struct config_item *df_item;
535 struct config_group *nacl_cg;
538 nacl_cg = &se_nacl->acl_group;
539 for (i = 0; nacl_cg->default_groups[i]; i++) {
540 df_item = &nacl_cg->default_groups[i]->cg_item;
541 nacl_cg->default_groups[i] = NULL;
542 config_item_put(df_item);
545 * struct se_node_acl free is done in target_fabric_nacl_base_release()
547 config_item_put(item);
550 static struct configfs_group_operations target_fabric_nacl_group_ops = {
551 .make_group = target_fabric_make_nodeacl,
552 .drop_item = target_fabric_drop_nodeacl,
555 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
557 /* End of tfc_tpg_nacl_cit */
559 /* Start of tfc_tpg_np_base_cit */
561 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
563 static void target_fabric_np_base_release(struct config_item *item)
565 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
566 struct se_tpg_np, tpg_np_group);
567 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
568 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
570 tf->tf_ops->fabric_drop_np(se_tpg_np);
573 static struct configfs_item_operations target_fabric_np_base_item_ops = {
574 .release = target_fabric_np_base_release,
575 .show_attribute = target_fabric_np_base_attr_show,
576 .store_attribute = target_fabric_np_base_attr_store,
579 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
581 /* End of tfc_tpg_np_base_cit */
583 /* Start of tfc_tpg_np_cit */
585 static struct config_group *target_fabric_make_np(
586 struct config_group *group,
589 struct se_portal_group *se_tpg = container_of(group,
590 struct se_portal_group, tpg_np_group);
591 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
592 struct se_tpg_np *se_tpg_np;
594 if (!tf->tf_ops->fabric_make_np) {
595 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
596 return ERR_PTR(-ENOSYS);
599 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
600 if (!se_tpg_np || IS_ERR(se_tpg_np))
601 return ERR_PTR(-EINVAL);
603 se_tpg_np->tpg_np_parent = se_tpg;
604 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
605 &tf->tf_tpg_np_base_cit);
607 return &se_tpg_np->tpg_np_group;
610 static void target_fabric_drop_np(
611 struct config_group *group,
612 struct config_item *item)
615 * struct se_tpg_np is released via target_fabric_np_base_release()
617 config_item_put(item);
620 static struct configfs_group_operations target_fabric_np_group_ops = {
621 .make_group = &target_fabric_make_np,
622 .drop_item = &target_fabric_drop_np,
625 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
627 /* End of tfc_tpg_np_cit */
629 /* Start of tfc_tpg_port_cit */
631 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
632 #define TCM_PORT_ATTR(_name, _mode) \
633 static struct target_fabric_port_attribute target_fabric_port_##_name = \
634 __CONFIGFS_EATTR(_name, _mode, \
635 target_fabric_port_show_attr_##_name, \
636 target_fabric_port_store_attr_##_name);
638 #define TCM_PORT_ATTOR_RO(_name) \
639 __CONFIGFS_EATTR_RO(_name, \
640 target_fabric_port_show_attr_##_name);
645 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
649 if (!lun || !lun->lun_sep)
652 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
655 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
660 if (!lun || !lun->lun_sep)
663 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
666 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
671 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
675 if (!lun || !lun->lun_sep)
678 return core_alua_show_offline_bit(lun, page);
681 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
686 if (!lun || !lun->lun_sep)
689 return core_alua_store_offline_bit(lun, page, count);
692 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
697 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
701 if (!lun || !lun->lun_sep)
704 return core_alua_show_secondary_status(lun, page);
707 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
712 if (!lun || !lun->lun_sep)
715 return core_alua_store_secondary_status(lun, page, count);
718 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
721 * alua_tg_pt_write_md
723 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
727 if (!lun || !lun->lun_sep)
730 return core_alua_show_secondary_write_metadata(lun, page);
733 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
738 if (!lun || !lun->lun_sep)
741 return core_alua_store_secondary_write_metadata(lun, page, count);
744 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
747 static struct configfs_attribute *target_fabric_port_attrs[] = {
748 &target_fabric_port_alua_tg_pt_gp.attr,
749 &target_fabric_port_alua_tg_pt_offline.attr,
750 &target_fabric_port_alua_tg_pt_status.attr,
751 &target_fabric_port_alua_tg_pt_write_md.attr,
755 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
757 static int target_fabric_port_link(
758 struct config_item *lun_ci,
759 struct config_item *se_dev_ci)
761 struct config_item *tpg_ci;
762 struct se_lun *lun = container_of(to_config_group(lun_ci),
763 struct se_lun, lun_group);
764 struct se_lun *lun_p;
765 struct se_portal_group *se_tpg;
766 struct se_device *dev =
767 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
768 struct target_fabric_configfs *tf;
771 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
772 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
773 " %p to struct se_device: %p\n", se_dev_ci, dev);
777 if (!(dev->dev_flags & DF_CONFIGURED)) {
778 pr_err("se_device not configured yet, cannot port link\n");
782 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
783 se_tpg = container_of(to_config_group(tpg_ci),
784 struct se_portal_group, tpg_group);
785 tf = se_tpg->se_tpg_wwn->wwn_tf;
787 if (lun->lun_se_dev != NULL) {
788 pr_err("Port Symlink already exists\n");
792 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
794 pr_err("core_dev_add_lun() failed\n");
795 ret = PTR_ERR(lun_p);
799 if (tf->tf_ops->fabric_post_link) {
801 * Call the optional fabric_post_link() to allow a
802 * fabric module to setup any additional state once
803 * core_dev_add_lun() has been called..
805 tf->tf_ops->fabric_post_link(se_tpg, lun);
813 static int target_fabric_port_unlink(
814 struct config_item *lun_ci,
815 struct config_item *se_dev_ci)
817 struct se_lun *lun = container_of(to_config_group(lun_ci),
818 struct se_lun, lun_group);
819 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
820 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
822 if (tf->tf_ops->fabric_pre_unlink) {
824 * Call the optional fabric_pre_unlink() to allow a
825 * fabric module to release any additional stat before
826 * core_dev_del_lun() is called.
828 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
831 core_dev_del_lun(se_tpg, lun);
835 static struct configfs_item_operations target_fabric_port_item_ops = {
836 .show_attribute = target_fabric_port_attr_show,
837 .store_attribute = target_fabric_port_attr_store,
838 .allow_link = target_fabric_port_link,
839 .drop_link = target_fabric_port_unlink,
842 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
844 /* End of tfc_tpg_port_cit */
846 /* Start of tfc_tpg_port_stat_cit */
848 static struct config_group *target_core_port_stat_mkdir(
849 struct config_group *group,
852 return ERR_PTR(-ENOSYS);
855 static void target_core_port_stat_rmdir(
856 struct config_group *group,
857 struct config_item *item)
862 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
863 .make_group = target_core_port_stat_mkdir,
864 .drop_item = target_core_port_stat_rmdir,
867 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
869 /* End of tfc_tpg_port_stat_cit */
871 /* Start of tfc_tpg_lun_cit */
873 static struct config_group *target_fabric_make_lun(
874 struct config_group *group,
878 struct se_portal_group *se_tpg = container_of(group,
879 struct se_portal_group, tpg_lun_group);
880 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
881 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
882 unsigned long unpacked_lun;
885 if (strstr(name, "lun_") != name) {
886 pr_err("Unable to locate \'_\" in"
887 " \"lun_$LUN_NUMBER\"\n");
888 return ERR_PTR(-EINVAL);
890 errno = kstrtoul(name + 4, 0, &unpacked_lun);
892 return ERR_PTR(errno);
893 if (unpacked_lun > UINT_MAX)
894 return ERR_PTR(-EINVAL);
896 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
898 return ERR_PTR(-EINVAL);
900 lun_cg = &lun->lun_group;
901 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
903 if (!lun_cg->default_groups) {
904 pr_err("Unable to allocate lun_cg->default_groups\n");
905 return ERR_PTR(-ENOMEM);
908 config_group_init_type_name(&lun->lun_group, name,
909 &tf->tf_tpg_port_cit);
910 config_group_init_type_name(&lun->port_stat_grps.stat_group,
911 "statistics", &tf->tf_tpg_port_stat_cit);
912 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
913 lun_cg->default_groups[1] = NULL;
915 port_stat_grp = &lun->port_stat_grps.stat_group;
916 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
918 if (!port_stat_grp->default_groups) {
919 pr_err("Unable to allocate port_stat_grp->default_groups\n");
920 kfree(lun_cg->default_groups);
921 return ERR_PTR(-ENOMEM);
923 target_stat_setup_port_default_groups(lun);
925 return &lun->lun_group;
928 static void target_fabric_drop_lun(
929 struct config_group *group,
930 struct config_item *item)
932 struct se_lun *lun = container_of(to_config_group(item),
933 struct se_lun, lun_group);
934 struct config_item *df_item;
935 struct config_group *lun_cg, *port_stat_grp;
938 port_stat_grp = &lun->port_stat_grps.stat_group;
939 for (i = 0; port_stat_grp->default_groups[i]; i++) {
940 df_item = &port_stat_grp->default_groups[i]->cg_item;
941 port_stat_grp->default_groups[i] = NULL;
942 config_item_put(df_item);
944 kfree(port_stat_grp->default_groups);
946 lun_cg = &lun->lun_group;
947 for (i = 0; lun_cg->default_groups[i]; i++) {
948 df_item = &lun_cg->default_groups[i]->cg_item;
949 lun_cg->default_groups[i] = NULL;
950 config_item_put(df_item);
952 kfree(lun_cg->default_groups);
954 config_item_put(item);
957 static struct configfs_group_operations target_fabric_lun_group_ops = {
958 .make_group = &target_fabric_make_lun,
959 .drop_item = &target_fabric_drop_lun,
962 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
964 /* End of tfc_tpg_lun_cit */
966 /* Start of tfc_tpg_attrib_cit */
968 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
970 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
971 .show_attribute = target_fabric_tpg_attrib_attr_show,
972 .store_attribute = target_fabric_tpg_attrib_attr_store,
975 TF_CIT_SETUP_DRV(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL);
977 /* End of tfc_tpg_attrib_cit */
979 /* Start of tfc_tpg_auth_cit */
981 CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
983 static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
984 .show_attribute = target_fabric_tpg_auth_attr_show,
985 .store_attribute = target_fabric_tpg_auth_attr_store,
988 TF_CIT_SETUP_DRV(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL);
990 /* End of tfc_tpg_attrib_cit */
992 /* Start of tfc_tpg_param_cit */
994 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
996 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
997 .show_attribute = target_fabric_tpg_param_attr_show,
998 .store_attribute = target_fabric_tpg_param_attr_store,
1001 TF_CIT_SETUP_DRV(tpg_param, &target_fabric_tpg_param_item_ops, NULL);
1003 /* End of tfc_tpg_param_cit */
1005 /* Start of tfc_tpg_base_cit */
1007 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1009 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1011 static void target_fabric_tpg_release(struct config_item *item)
1013 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1014 struct se_portal_group, tpg_group);
1015 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1016 struct target_fabric_configfs *tf = wwn->wwn_tf;
1018 tf->tf_ops->fabric_drop_tpg(se_tpg);
1021 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1022 .release = target_fabric_tpg_release,
1023 .show_attribute = target_fabric_tpg_attr_show,
1024 .store_attribute = target_fabric_tpg_attr_store,
1027 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
1029 /* End of tfc_tpg_base_cit */
1031 /* Start of tfc_tpg_cit */
1033 static struct config_group *target_fabric_make_tpg(
1034 struct config_group *group,
1037 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1038 struct target_fabric_configfs *tf = wwn->wwn_tf;
1039 struct se_portal_group *se_tpg;
1041 if (!tf->tf_ops->fabric_make_tpg) {
1042 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
1043 return ERR_PTR(-ENOSYS);
1046 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
1047 if (!se_tpg || IS_ERR(se_tpg))
1048 return ERR_PTR(-EINVAL);
1050 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1052 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1053 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1054 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1055 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1056 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1057 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1058 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1059 se_tpg->tpg_group.default_groups[6] = NULL;
1061 config_group_init_type_name(&se_tpg->tpg_group, name,
1062 &tf->tf_tpg_base_cit);
1063 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1064 &tf->tf_tpg_lun_cit);
1065 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1066 &tf->tf_tpg_np_cit);
1067 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1068 &tf->tf_tpg_nacl_cit);
1069 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1070 &tf->tf_tpg_attrib_cit);
1071 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
1072 &tf->tf_tpg_auth_cit);
1073 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1074 &tf->tf_tpg_param_cit);
1076 return &se_tpg->tpg_group;
1079 static void target_fabric_drop_tpg(
1080 struct config_group *group,
1081 struct config_item *item)
1083 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1084 struct se_portal_group, tpg_group);
1085 struct config_group *tpg_cg = &se_tpg->tpg_group;
1086 struct config_item *df_item;
1089 * Release default groups, but do not release tpg_cg->default_groups
1090 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1092 for (i = 0; tpg_cg->default_groups[i]; i++) {
1093 df_item = &tpg_cg->default_groups[i]->cg_item;
1094 tpg_cg->default_groups[i] = NULL;
1095 config_item_put(df_item);
1098 config_item_put(item);
1101 static void target_fabric_release_wwn(struct config_item *item)
1103 struct se_wwn *wwn = container_of(to_config_group(item),
1104 struct se_wwn, wwn_group);
1105 struct target_fabric_configfs *tf = wwn->wwn_tf;
1107 tf->tf_ops->fabric_drop_wwn(wwn);
1110 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1111 .release = target_fabric_release_wwn,
1114 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1115 .make_group = target_fabric_make_tpg,
1116 .drop_item = target_fabric_drop_tpg,
1119 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1122 /* End of tfc_tpg_cit */
1124 /* Start of tfc_wwn_fabric_stats_cit */
1126 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1127 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1129 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1131 /* End of tfc_wwn_fabric_stats_cit */
1133 /* Start of tfc_wwn_cit */
1135 static struct config_group *target_fabric_make_wwn(
1136 struct config_group *group,
1139 struct target_fabric_configfs *tf = container_of(group,
1140 struct target_fabric_configfs, tf_group);
1143 if (!tf->tf_ops->fabric_make_wwn) {
1144 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1145 return ERR_PTR(-ENOSYS);
1148 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1149 if (!wwn || IS_ERR(wwn))
1150 return ERR_PTR(-EINVAL);
1154 * Setup default groups from pre-allocated wwn->wwn_default_groups
1156 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1157 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1158 wwn->wwn_group.default_groups[1] = NULL;
1160 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1161 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1162 &tf->tf_wwn_fabric_stats_cit);
1164 return &wwn->wwn_group;
1167 static void target_fabric_drop_wwn(
1168 struct config_group *group,
1169 struct config_item *item)
1171 struct se_wwn *wwn = container_of(to_config_group(item),
1172 struct se_wwn, wwn_group);
1173 struct config_item *df_item;
1174 struct config_group *cg = &wwn->wwn_group;
1177 for (i = 0; cg->default_groups[i]; i++) {
1178 df_item = &cg->default_groups[i]->cg_item;
1179 cg->default_groups[i] = NULL;
1180 config_item_put(df_item);
1183 config_item_put(item);
1186 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1187 .make_group = target_fabric_make_wwn,
1188 .drop_item = target_fabric_drop_wwn,
1191 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1193 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1195 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1196 .show_attribute = target_fabric_wwn_attr_show,
1197 .store_attribute = target_fabric_wwn_attr_store,
1200 TF_CIT_SETUP_DRV(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops);
1202 /* End of tfc_wwn_cit */
1204 /* Start of tfc_discovery_cit */
1206 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1209 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1210 .show_attribute = target_fabric_discovery_attr_show,
1211 .store_attribute = target_fabric_discovery_attr_store,
1214 TF_CIT_SETUP_DRV(discovery, &target_fabric_discovery_item_ops, NULL);
1216 /* End of tfc_discovery_cit */
1218 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1220 target_fabric_setup_discovery_cit(tf);
1221 target_fabric_setup_wwn_cit(tf);
1222 target_fabric_setup_wwn_fabric_stats_cit(tf);
1223 target_fabric_setup_tpg_cit(tf);
1224 target_fabric_setup_tpg_base_cit(tf);
1225 target_fabric_setup_tpg_port_cit(tf);
1226 target_fabric_setup_tpg_port_stat_cit(tf);
1227 target_fabric_setup_tpg_lun_cit(tf);
1228 target_fabric_setup_tpg_np_cit(tf);
1229 target_fabric_setup_tpg_np_base_cit(tf);
1230 target_fabric_setup_tpg_attrib_cit(tf);
1231 target_fabric_setup_tpg_auth_cit(tf);
1232 target_fabric_setup_tpg_param_cit(tf);
1233 target_fabric_setup_tpg_nacl_cit(tf);
1234 target_fabric_setup_tpg_nacl_base_cit(tf);
1235 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1236 target_fabric_setup_tpg_nacl_auth_cit(tf);
1237 target_fabric_setup_tpg_nacl_param_cit(tf);
1238 target_fabric_setup_tpg_nacl_stat_cit(tf);
1239 target_fabric_setup_tpg_mappedlun_cit(tf);
1240 target_fabric_setup_tpg_mappedlun_stat_cit(tf);