1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * (c) Copyright 2010-2012 RisingTide Systems LLC.
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 target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
50 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
52 cit->ct_item_ops = _item_ops; \
53 cit->ct_group_ops = _group_ops; \
54 cit->ct_attrs = _attrs; \
55 cit->ct_owner = tf->tf_module; \
56 pr_debug("Setup generic %s\n", __stringify(_name)); \
59 /* Start of tfc_tpg_mappedlun_cit */
61 static int target_fabric_mappedlun_link(
62 struct config_item *lun_acl_ci,
63 struct config_item *lun_ci)
65 struct se_dev_entry *deve;
66 struct se_lun *lun = container_of(to_config_group(lun_ci),
67 struct se_lun, lun_group);
68 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
69 struct se_lun_acl, se_lun_group);
70 struct se_portal_group *se_tpg;
71 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
72 int ret = 0, lun_access;
74 * Ensure that the source port exists
76 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
77 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
78 "_tpg does not exist\n");
81 se_tpg = lun->lun_sep->sep_tpg;
83 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
84 tpg_ci = &nacl_ci->ci_group->cg_item;
85 wwn_ci = &tpg_ci->ci_group->cg_item;
86 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
87 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
89 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
91 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
92 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
93 config_item_name(wwn_ci));
96 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
97 pr_err("Illegal Initiator ACL Symlink outside of %s"
98 " TPGT: %s\n", config_item_name(wwn_ci),
99 config_item_name(tpg_ci));
103 * If this struct se_node_acl was dynamically generated with
104 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
105 * which be will write protected (READ-ONLY) when
106 * tpg_1/attrib/demo_mode_write_protect=1
108 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
109 deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun];
110 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
111 lun_access = deve->lun_flags;
114 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
115 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
116 TRANSPORT_LUNFLAGS_READ_WRITE;
117 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
119 * Determine the actual mapped LUN value user wants..
121 * This value is what the SCSI Initiator actually sees the
122 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
124 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
125 lun->unpacked_lun, lun_access);
127 return (ret < 0) ? -EINVAL : 0;
130 static int target_fabric_mappedlun_unlink(
131 struct config_item *lun_acl_ci,
132 struct config_item *lun_ci)
135 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
136 struct se_lun_acl, se_lun_group);
137 struct se_node_acl *nacl = lacl->se_lun_nacl;
138 struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun];
139 struct se_portal_group *se_tpg;
141 * Determine if the underlying MappedLUN has already been released..
146 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
147 se_tpg = lun->lun_sep->sep_tpg;
149 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
153 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
154 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
155 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
156 __CONFIGFS_EATTR(_name, _mode, \
157 target_fabric_mappedlun_show_##_name, \
158 target_fabric_mappedlun_store_##_name);
160 static ssize_t target_fabric_mappedlun_show_write_protect(
161 struct se_lun_acl *lacl,
164 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
165 struct se_dev_entry *deve;
168 spin_lock_irq(&se_nacl->device_list_lock);
169 deve = se_nacl->device_list[lacl->mapped_lun];
170 len = sprintf(page, "%d\n",
171 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
173 spin_unlock_irq(&se_nacl->device_list_lock);
178 static ssize_t target_fabric_mappedlun_store_write_protect(
179 struct se_lun_acl *lacl,
183 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
184 struct se_portal_group *se_tpg = se_nacl->se_tpg;
187 if (strict_strtoul(page, 0, &op))
190 if ((op != 1) && (op != 0))
193 core_update_device_list_access(lacl->mapped_lun, (op) ?
194 TRANSPORT_LUNFLAGS_READ_ONLY :
195 TRANSPORT_LUNFLAGS_READ_WRITE,
198 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
199 " Mapped LUN: %u Write Protect bit to %s\n",
200 se_tpg->se_tpg_tfo->get_fabric_name(),
201 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
207 TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
209 CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
211 static void target_fabric_mappedlun_release(struct config_item *item)
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;
217 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
220 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
221 &target_fabric_mappedlun_write_protect.attr,
225 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
226 .release = target_fabric_mappedlun_release,
227 .show_attribute = target_fabric_mappedlun_attr_show,
228 .store_attribute = target_fabric_mappedlun_attr_store,
229 .allow_link = target_fabric_mappedlun_link,
230 .drop_link = target_fabric_mappedlun_unlink,
233 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
234 target_fabric_mappedlun_attrs);
236 /* End of tfc_tpg_mappedlun_cit */
238 /* Start of tfc_tpg_mappedlun_port_cit */
240 static struct config_group *target_core_mappedlun_stat_mkdir(
241 struct config_group *group,
244 return ERR_PTR(-ENOSYS);
247 static void target_core_mappedlun_stat_rmdir(
248 struct config_group *group,
249 struct config_item *item)
254 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
255 .make_group = target_core_mappedlun_stat_mkdir,
256 .drop_item = target_core_mappedlun_stat_rmdir,
259 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
262 /* End of tfc_tpg_mappedlun_port_cit */
264 /* Start of tfc_tpg_nacl_attrib_cit */
266 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
268 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
269 .show_attribute = target_fabric_nacl_attrib_attr_show,
270 .store_attribute = target_fabric_nacl_attrib_attr_store,
273 TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
275 /* End of tfc_tpg_nacl_attrib_cit */
277 /* Start of tfc_tpg_nacl_auth_cit */
279 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
281 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
282 .show_attribute = target_fabric_nacl_auth_attr_show,
283 .store_attribute = target_fabric_nacl_auth_attr_store,
286 TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
288 /* End of tfc_tpg_nacl_auth_cit */
290 /* Start of tfc_tpg_nacl_param_cit */
292 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
294 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
295 .show_attribute = target_fabric_nacl_param_attr_show,
296 .store_attribute = target_fabric_nacl_param_attr_store,
299 TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
301 /* End of tfc_tpg_nacl_param_cit */
303 /* Start of tfc_tpg_nacl_base_cit */
305 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
307 static struct config_group *target_fabric_make_mappedlun(
308 struct config_group *group,
311 struct se_node_acl *se_nacl = container_of(group,
312 struct se_node_acl, acl_group);
313 struct se_portal_group *se_tpg = se_nacl->se_tpg;
314 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
315 struct se_lun_acl *lacl;
316 struct config_item *acl_ci;
317 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
319 unsigned long mapped_lun;
322 acl_ci = &group->cg_item;
324 pr_err("Unable to locatel acl_ci\n");
328 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
330 pr_err("Unable to allocate memory for name buf\n");
331 return ERR_PTR(-ENOMEM);
333 snprintf(buf, strlen(name) + 1, "%s", name);
335 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
337 if (strstr(buf, "lun_") != buf) {
338 pr_err("Unable to locate \"lun_\" from buf: %s"
339 " name: %s\n", buf, name);
344 * Determine the Mapped LUN value. This is what the SCSI Initiator
345 * Port will actually see.
347 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
352 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
353 config_item_name(acl_ci), &ret);
359 lacl_cg = &lacl->se_lun_group;
360 lacl_cg->default_groups = kzalloc(sizeof(struct config_group *) * 2,
362 if (!lacl_cg->default_groups) {
363 pr_err("Unable to allocate lacl_cg->default_groups\n");
368 config_group_init_type_name(&lacl->se_lun_group, name,
369 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
370 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
371 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
372 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
373 lacl_cg->default_groups[1] = NULL;
375 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
376 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 3,
378 if (!ml_stat_grp->default_groups) {
379 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
383 target_stat_setup_mappedlun_default_groups(lacl);
386 return &lacl->se_lun_group;
389 kfree(lacl_cg->default_groups);
394 static void target_fabric_drop_mappedlun(
395 struct config_group *group,
396 struct config_item *item)
398 struct se_lun_acl *lacl = container_of(to_config_group(item),
399 struct se_lun_acl, se_lun_group);
400 struct config_item *df_item;
401 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
404 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
405 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
406 df_item = &ml_stat_grp->default_groups[i]->cg_item;
407 ml_stat_grp->default_groups[i] = NULL;
408 config_item_put(df_item);
410 kfree(ml_stat_grp->default_groups);
412 lacl_cg = &lacl->se_lun_group;
413 for (i = 0; lacl_cg->default_groups[i]; i++) {
414 df_item = &lacl_cg->default_groups[i]->cg_item;
415 lacl_cg->default_groups[i] = NULL;
416 config_item_put(df_item);
418 kfree(lacl_cg->default_groups);
420 config_item_put(item);
423 static void target_fabric_nacl_base_release(struct config_item *item)
425 struct se_node_acl *se_nacl = container_of(to_config_group(item),
426 struct se_node_acl, acl_group);
427 struct se_portal_group *se_tpg = se_nacl->se_tpg;
428 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
430 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
433 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
434 .release = target_fabric_nacl_base_release,
435 .show_attribute = target_fabric_nacl_base_attr_show,
436 .store_attribute = target_fabric_nacl_base_attr_store,
439 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
440 .make_group = target_fabric_make_mappedlun,
441 .drop_item = target_fabric_drop_mappedlun,
444 TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
445 &target_fabric_nacl_base_group_ops, NULL);
447 /* End of tfc_tpg_nacl_base_cit */
449 /* Start of tfc_node_fabric_stats_cit */
451 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
452 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
454 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
456 /* End of tfc_wwn_fabric_stats_cit */
458 /* Start of tfc_tpg_nacl_cit */
460 static struct config_group *target_fabric_make_nodeacl(
461 struct config_group *group,
464 struct se_portal_group *se_tpg = container_of(group,
465 struct se_portal_group, tpg_acl_group);
466 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
467 struct se_node_acl *se_nacl;
468 struct config_group *nacl_cg;
470 if (!tf->tf_ops.fabric_make_nodeacl) {
471 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
472 return ERR_PTR(-ENOSYS);
475 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
477 return ERR_CAST(se_nacl);
479 nacl_cg = &se_nacl->acl_group;
480 nacl_cg->default_groups = se_nacl->acl_default_groups;
481 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
482 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
483 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
484 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
485 nacl_cg->default_groups[4] = NULL;
487 config_group_init_type_name(&se_nacl->acl_group, name,
488 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
489 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
490 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
491 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
492 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
493 config_group_init_type_name(&se_nacl->acl_param_group, "param",
494 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
495 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
497 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
499 return &se_nacl->acl_group;
502 static void target_fabric_drop_nodeacl(
503 struct config_group *group,
504 struct config_item *item)
506 struct se_node_acl *se_nacl = container_of(to_config_group(item),
507 struct se_node_acl, acl_group);
508 struct config_item *df_item;
509 struct config_group *nacl_cg;
512 nacl_cg = &se_nacl->acl_group;
513 for (i = 0; nacl_cg->default_groups[i]; i++) {
514 df_item = &nacl_cg->default_groups[i]->cg_item;
515 nacl_cg->default_groups[i] = NULL;
516 config_item_put(df_item);
519 * struct se_node_acl free is done in target_fabric_nacl_base_release()
521 config_item_put(item);
524 static struct configfs_group_operations target_fabric_nacl_group_ops = {
525 .make_group = target_fabric_make_nodeacl,
526 .drop_item = target_fabric_drop_nodeacl,
529 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
531 /* End of tfc_tpg_nacl_cit */
533 /* Start of tfc_tpg_np_base_cit */
535 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
537 static void target_fabric_np_base_release(struct config_item *item)
539 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
540 struct se_tpg_np, tpg_np_group);
541 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
542 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
544 tf->tf_ops.fabric_drop_np(se_tpg_np);
547 static struct configfs_item_operations target_fabric_np_base_item_ops = {
548 .release = target_fabric_np_base_release,
549 .show_attribute = target_fabric_np_base_attr_show,
550 .store_attribute = target_fabric_np_base_attr_store,
553 TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
555 /* End of tfc_tpg_np_base_cit */
557 /* Start of tfc_tpg_np_cit */
559 static struct config_group *target_fabric_make_np(
560 struct config_group *group,
563 struct se_portal_group *se_tpg = container_of(group,
564 struct se_portal_group, tpg_np_group);
565 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
566 struct se_tpg_np *se_tpg_np;
568 if (!tf->tf_ops.fabric_make_np) {
569 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
570 return ERR_PTR(-ENOSYS);
573 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
574 if (!se_tpg_np || IS_ERR(se_tpg_np))
575 return ERR_PTR(-EINVAL);
577 se_tpg_np->tpg_np_parent = se_tpg;
578 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
579 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
581 return &se_tpg_np->tpg_np_group;
584 static void target_fabric_drop_np(
585 struct config_group *group,
586 struct config_item *item)
589 * struct se_tpg_np is released via target_fabric_np_base_release()
591 config_item_put(item);
594 static struct configfs_group_operations target_fabric_np_group_ops = {
595 .make_group = &target_fabric_make_np,
596 .drop_item = &target_fabric_drop_np,
599 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
601 /* End of tfc_tpg_np_cit */
603 /* Start of tfc_tpg_port_cit */
605 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
606 #define TCM_PORT_ATTR(_name, _mode) \
607 static struct target_fabric_port_attribute target_fabric_port_##_name = \
608 __CONFIGFS_EATTR(_name, _mode, \
609 target_fabric_port_show_attr_##_name, \
610 target_fabric_port_store_attr_##_name);
612 #define TCM_PORT_ATTOR_RO(_name) \
613 __CONFIGFS_EATTR_RO(_name, \
614 target_fabric_port_show_attr_##_name);
619 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
623 if (!lun || !lun->lun_sep)
626 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
629 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
634 if (!lun || !lun->lun_sep)
637 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
640 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
645 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
649 if (!lun || !lun->lun_sep)
652 return core_alua_show_offline_bit(lun, page);
655 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
660 if (!lun || !lun->lun_sep)
663 return core_alua_store_offline_bit(lun, page, count);
666 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
671 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
675 if (!lun || !lun->lun_sep)
678 return core_alua_show_secondary_status(lun, page);
681 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
686 if (!lun || !lun->lun_sep)
689 return core_alua_store_secondary_status(lun, page, count);
692 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
695 * alua_tg_pt_write_md
697 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
701 if (!lun || !lun->lun_sep)
704 return core_alua_show_secondary_write_metadata(lun, page);
707 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
712 if (!lun || !lun->lun_sep)
715 return core_alua_store_secondary_write_metadata(lun, page, count);
718 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
721 static struct configfs_attribute *target_fabric_port_attrs[] = {
722 &target_fabric_port_alua_tg_pt_gp.attr,
723 &target_fabric_port_alua_tg_pt_offline.attr,
724 &target_fabric_port_alua_tg_pt_status.attr,
725 &target_fabric_port_alua_tg_pt_write_md.attr,
729 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
731 static int target_fabric_port_link(
732 struct config_item *lun_ci,
733 struct config_item *se_dev_ci)
735 struct config_item *tpg_ci;
736 struct se_lun *lun = container_of(to_config_group(lun_ci),
737 struct se_lun, lun_group);
738 struct se_lun *lun_p;
739 struct se_portal_group *se_tpg;
740 struct se_device *dev =
741 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
742 struct target_fabric_configfs *tf;
745 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
746 se_tpg = container_of(to_config_group(tpg_ci),
747 struct se_portal_group, tpg_group);
748 tf = se_tpg->se_tpg_wwn->wwn_tf;
750 if (lun->lun_se_dev != NULL) {
751 pr_err("Port Symlink already exists\n");
755 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
757 pr_err("core_dev_add_lun() failed\n");
758 ret = PTR_ERR(lun_p);
762 if (tf->tf_ops.fabric_post_link) {
764 * Call the optional fabric_post_link() to allow a
765 * fabric module to setup any additional state once
766 * core_dev_add_lun() has been called..
768 tf->tf_ops.fabric_post_link(se_tpg, lun);
776 static int target_fabric_port_unlink(
777 struct config_item *lun_ci,
778 struct config_item *se_dev_ci)
780 struct se_lun *lun = container_of(to_config_group(lun_ci),
781 struct se_lun, lun_group);
782 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
783 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
785 if (tf->tf_ops.fabric_pre_unlink) {
787 * Call the optional fabric_pre_unlink() to allow a
788 * fabric module to release any additional stat before
789 * core_dev_del_lun() is called.
791 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
794 core_dev_del_lun(se_tpg, lun->unpacked_lun);
798 static struct configfs_item_operations target_fabric_port_item_ops = {
799 .show_attribute = target_fabric_port_attr_show,
800 .store_attribute = target_fabric_port_attr_store,
801 .allow_link = target_fabric_port_link,
802 .drop_link = target_fabric_port_unlink,
805 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
807 /* End of tfc_tpg_port_cit */
809 /* Start of tfc_tpg_port_stat_cit */
811 static struct config_group *target_core_port_stat_mkdir(
812 struct config_group *group,
815 return ERR_PTR(-ENOSYS);
818 static void target_core_port_stat_rmdir(
819 struct config_group *group,
820 struct config_item *item)
825 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
826 .make_group = target_core_port_stat_mkdir,
827 .drop_item = target_core_port_stat_rmdir,
830 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
832 /* End of tfc_tpg_port_stat_cit */
834 /* Start of tfc_tpg_lun_cit */
836 static struct config_group *target_fabric_make_lun(
837 struct config_group *group,
841 struct se_portal_group *se_tpg = container_of(group,
842 struct se_portal_group, tpg_lun_group);
843 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
844 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
845 unsigned long unpacked_lun;
848 if (strstr(name, "lun_") != name) {
849 pr_err("Unable to locate \'_\" in"
850 " \"lun_$LUN_NUMBER\"\n");
851 return ERR_PTR(-EINVAL);
853 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
854 return ERR_PTR(-EINVAL);
856 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
858 return ERR_PTR(-EINVAL);
860 lun_cg = &lun->lun_group;
861 lun_cg->default_groups = kzalloc(sizeof(struct config_group *) * 2,
863 if (!lun_cg->default_groups) {
864 pr_err("Unable to allocate lun_cg->default_groups\n");
865 return ERR_PTR(-ENOMEM);
868 config_group_init_type_name(&lun->lun_group, name,
869 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
870 config_group_init_type_name(&lun->port_stat_grps.stat_group,
871 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
872 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
873 lun_cg->default_groups[1] = NULL;
875 port_stat_grp = &lun->port_stat_grps.stat_group;
876 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
878 if (!port_stat_grp->default_groups) {
879 pr_err("Unable to allocate port_stat_grp->default_groups\n");
883 target_stat_setup_port_default_groups(lun);
885 return &lun->lun_group;
888 kfree(lun_cg->default_groups);
889 return ERR_PTR(errno);
892 static void target_fabric_drop_lun(
893 struct config_group *group,
894 struct config_item *item)
896 struct se_lun *lun = container_of(to_config_group(item),
897 struct se_lun, lun_group);
898 struct config_item *df_item;
899 struct config_group *lun_cg, *port_stat_grp;
902 port_stat_grp = &lun->port_stat_grps.stat_group;
903 for (i = 0; port_stat_grp->default_groups[i]; i++) {
904 df_item = &port_stat_grp->default_groups[i]->cg_item;
905 port_stat_grp->default_groups[i] = NULL;
906 config_item_put(df_item);
908 kfree(port_stat_grp->default_groups);
910 lun_cg = &lun->lun_group;
911 for (i = 0; lun_cg->default_groups[i]; i++) {
912 df_item = &lun_cg->default_groups[i]->cg_item;
913 lun_cg->default_groups[i] = NULL;
914 config_item_put(df_item);
916 kfree(lun_cg->default_groups);
918 config_item_put(item);
921 static struct configfs_group_operations target_fabric_lun_group_ops = {
922 .make_group = &target_fabric_make_lun,
923 .drop_item = &target_fabric_drop_lun,
926 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
928 /* End of tfc_tpg_lun_cit */
930 /* Start of tfc_tpg_attrib_cit */
932 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
934 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
935 .show_attribute = target_fabric_tpg_attrib_attr_show,
936 .store_attribute = target_fabric_tpg_attrib_attr_store,
939 TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
941 /* End of tfc_tpg_attrib_cit */
943 /* Start of tfc_tpg_param_cit */
945 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
947 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
948 .show_attribute = target_fabric_tpg_param_attr_show,
949 .store_attribute = target_fabric_tpg_param_attr_store,
952 TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
954 /* End of tfc_tpg_param_cit */
956 /* Start of tfc_tpg_base_cit */
958 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
960 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
962 static void target_fabric_tpg_release(struct config_item *item)
964 struct se_portal_group *se_tpg = container_of(to_config_group(item),
965 struct se_portal_group, tpg_group);
966 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
967 struct target_fabric_configfs *tf = wwn->wwn_tf;
969 tf->tf_ops.fabric_drop_tpg(se_tpg);
972 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
973 .release = target_fabric_tpg_release,
974 .show_attribute = target_fabric_tpg_attr_show,
975 .store_attribute = target_fabric_tpg_attr_store,
978 TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
980 /* End of tfc_tpg_base_cit */
982 /* Start of tfc_tpg_cit */
984 static struct config_group *target_fabric_make_tpg(
985 struct config_group *group,
988 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
989 struct target_fabric_configfs *tf = wwn->wwn_tf;
990 struct se_portal_group *se_tpg;
992 if (!tf->tf_ops.fabric_make_tpg) {
993 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
994 return ERR_PTR(-ENOSYS);
997 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
998 if (!se_tpg || IS_ERR(se_tpg))
999 return ERR_PTR(-EINVAL);
1001 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1003 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1004 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1005 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1006 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1007 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1008 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1009 se_tpg->tpg_group.default_groups[5] = NULL;
1011 config_group_init_type_name(&se_tpg->tpg_group, name,
1012 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1013 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1014 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1015 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1016 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1017 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1018 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1019 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1020 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1021 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1022 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1024 return &se_tpg->tpg_group;
1027 static void target_fabric_drop_tpg(
1028 struct config_group *group,
1029 struct config_item *item)
1031 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1032 struct se_portal_group, tpg_group);
1033 struct config_group *tpg_cg = &se_tpg->tpg_group;
1034 struct config_item *df_item;
1037 * Release default groups, but do not release tpg_cg->default_groups
1038 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1040 for (i = 0; tpg_cg->default_groups[i]; i++) {
1041 df_item = &tpg_cg->default_groups[i]->cg_item;
1042 tpg_cg->default_groups[i] = NULL;
1043 config_item_put(df_item);
1046 config_item_put(item);
1049 static void target_fabric_release_wwn(struct config_item *item)
1051 struct se_wwn *wwn = container_of(to_config_group(item),
1052 struct se_wwn, wwn_group);
1053 struct target_fabric_configfs *tf = wwn->wwn_tf;
1055 tf->tf_ops.fabric_drop_wwn(wwn);
1058 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1059 .release = target_fabric_release_wwn,
1062 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1063 .make_group = target_fabric_make_tpg,
1064 .drop_item = target_fabric_drop_tpg,
1067 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1070 /* End of tfc_tpg_cit */
1072 /* Start of tfc_wwn_fabric_stats_cit */
1074 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1075 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1077 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1079 /* End of tfc_wwn_fabric_stats_cit */
1081 /* Start of tfc_wwn_cit */
1083 static struct config_group *target_fabric_make_wwn(
1084 struct config_group *group,
1087 struct target_fabric_configfs *tf = container_of(group,
1088 struct target_fabric_configfs, tf_group);
1091 if (!tf->tf_ops.fabric_make_wwn) {
1092 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1093 return ERR_PTR(-ENOSYS);
1096 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
1097 if (!wwn || IS_ERR(wwn))
1098 return ERR_PTR(-EINVAL);
1102 * Setup default groups from pre-allocated wwn->wwn_default_groups
1104 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1105 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1106 wwn->wwn_group.default_groups[1] = NULL;
1108 config_group_init_type_name(&wwn->wwn_group, name,
1109 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
1110 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1111 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
1113 return &wwn->wwn_group;
1116 static void target_fabric_drop_wwn(
1117 struct config_group *group,
1118 struct config_item *item)
1120 struct se_wwn *wwn = container_of(to_config_group(item),
1121 struct se_wwn, wwn_group);
1122 struct config_item *df_item;
1123 struct config_group *cg = &wwn->wwn_group;
1126 for (i = 0; cg->default_groups[i]; i++) {
1127 df_item = &cg->default_groups[i]->cg_item;
1128 cg->default_groups[i] = NULL;
1129 config_item_put(df_item);
1132 config_item_put(item);
1135 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1136 .make_group = target_fabric_make_wwn,
1137 .drop_item = target_fabric_drop_wwn,
1140 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1142 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1144 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1145 .show_attribute = target_fabric_wwn_attr_show,
1146 .store_attribute = target_fabric_wwn_attr_store,
1149 TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1151 /* End of tfc_wwn_cit */
1153 /* Start of tfc_discovery_cit */
1155 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1158 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1159 .show_attribute = target_fabric_discovery_attr_show,
1160 .store_attribute = target_fabric_discovery_attr_store,
1163 TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1165 /* End of tfc_discovery_cit */
1167 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1169 target_fabric_setup_discovery_cit(tf);
1170 target_fabric_setup_wwn_cit(tf);
1171 target_fabric_setup_wwn_fabric_stats_cit(tf);
1172 target_fabric_setup_tpg_cit(tf);
1173 target_fabric_setup_tpg_base_cit(tf);
1174 target_fabric_setup_tpg_port_cit(tf);
1175 target_fabric_setup_tpg_port_stat_cit(tf);
1176 target_fabric_setup_tpg_lun_cit(tf);
1177 target_fabric_setup_tpg_np_cit(tf);
1178 target_fabric_setup_tpg_np_base_cit(tf);
1179 target_fabric_setup_tpg_attrib_cit(tf);
1180 target_fabric_setup_tpg_param_cit(tf);
1181 target_fabric_setup_tpg_nacl_cit(tf);
1182 target_fabric_setup_tpg_nacl_base_cit(tf);
1183 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1184 target_fabric_setup_tpg_nacl_auth_cit(tf);
1185 target_fabric_setup_tpg_nacl_param_cit(tf);
1186 target_fabric_setup_tpg_nacl_stat_cit(tf);
1187 target_fabric_setup_tpg_mappedlun_cit(tf);
1188 target_fabric_setup_tpg_mappedlun_stat_cit(tf);