1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * Copyright (c) 2010,2011 Rising Tide Systems
8 * Copyright (c) 2010,2011 Linux-iSCSI.org
10 * Copyright (c) Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
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>
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>
38 #include <target/target_core_base.h>
39 #include <target/target_core_device.h>
40 #include <target/target_core_tpg.h>
41 #include <target/target_core_transport.h>
42 #include <target/target_core_fabric_ops.h>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
47 #include "target_core_internal.h"
48 #include "target_core_alua.h"
49 #include "target_core_pr.h"
51 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
52 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
54 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
55 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
57 cit->ct_item_ops = _item_ops; \
58 cit->ct_group_ops = _group_ops; \
59 cit->ct_attrs = _attrs; \
60 cit->ct_owner = tf->tf_module; \
61 pr_debug("Setup generic %s\n", __stringify(_name)); \
64 /* Start of tfc_tpg_mappedlun_cit */
66 static int target_fabric_mappedlun_link(
67 struct config_item *lun_acl_ci,
68 struct config_item *lun_ci)
70 struct se_dev_entry *deve;
71 struct se_lun *lun = container_of(to_config_group(lun_ci),
72 struct se_lun, lun_group);
73 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
74 struct se_lun_acl, se_lun_group);
75 struct se_portal_group *se_tpg;
76 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
77 int ret = 0, lun_access;
79 * Ensure that the source port exists
81 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
82 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
83 "_tpg does not exist\n");
86 se_tpg = lun->lun_sep->sep_tpg;
88 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
89 tpg_ci = &nacl_ci->ci_group->cg_item;
90 wwn_ci = &tpg_ci->ci_group->cg_item;
91 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
92 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
94 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
96 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
97 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
98 config_item_name(wwn_ci));
101 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
102 pr_err("Illegal Initiator ACL Symlink outside of %s"
103 " TPGT: %s\n", config_item_name(wwn_ci),
104 config_item_name(tpg_ci));
108 * If this struct se_node_acl was dynamically generated with
109 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
110 * which be will write protected (READ-ONLY) when
111 * tpg_1/attrib/demo_mode_write_protect=1
113 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
114 deve = &lacl->se_lun_nacl->device_list[lacl->mapped_lun];
115 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
116 lun_access = deve->lun_flags;
119 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
120 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
121 TRANSPORT_LUNFLAGS_READ_WRITE;
122 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
124 * Determine the actual mapped LUN value user wants..
126 * This value is what the SCSI Initiator actually sees the
127 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
129 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
130 lun->unpacked_lun, lun_access);
132 return (ret < 0) ? -EINVAL : 0;
135 static int target_fabric_mappedlun_unlink(
136 struct config_item *lun_acl_ci,
137 struct config_item *lun_ci)
140 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
141 struct se_lun_acl, se_lun_group);
142 struct se_node_acl *nacl = lacl->se_lun_nacl;
143 struct se_dev_entry *deve = &nacl->device_list[lacl->mapped_lun];
144 struct se_portal_group *se_tpg;
146 * Determine if the underlying MappedLUN has already been released..
151 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
152 se_tpg = lun->lun_sep->sep_tpg;
154 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
158 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
159 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
160 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
161 __CONFIGFS_EATTR(_name, _mode, \
162 target_fabric_mappedlun_show_##_name, \
163 target_fabric_mappedlun_store_##_name);
165 static ssize_t target_fabric_mappedlun_show_write_protect(
166 struct se_lun_acl *lacl,
169 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
170 struct se_dev_entry *deve;
173 spin_lock_irq(&se_nacl->device_list_lock);
174 deve = &se_nacl->device_list[lacl->mapped_lun];
175 len = sprintf(page, "%d\n",
176 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
178 spin_unlock_irq(&se_nacl->device_list_lock);
183 static ssize_t target_fabric_mappedlun_store_write_protect(
184 struct se_lun_acl *lacl,
188 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
189 struct se_portal_group *se_tpg = se_nacl->se_tpg;
192 if (strict_strtoul(page, 0, &op))
195 if ((op != 1) && (op != 0))
198 core_update_device_list_access(lacl->mapped_lun, (op) ?
199 TRANSPORT_LUNFLAGS_READ_ONLY :
200 TRANSPORT_LUNFLAGS_READ_WRITE,
203 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
204 " Mapped LUN: %u Write Protect bit to %s\n",
205 se_tpg->se_tpg_tfo->get_fabric_name(),
206 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
212 TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
214 CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
216 static void target_fabric_mappedlun_release(struct config_item *item)
218 struct se_lun_acl *lacl = container_of(to_config_group(item),
219 struct se_lun_acl, se_lun_group);
220 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
222 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
225 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
226 &target_fabric_mappedlun_write_protect.attr,
230 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
231 .release = target_fabric_mappedlun_release,
232 .show_attribute = target_fabric_mappedlun_attr_show,
233 .store_attribute = target_fabric_mappedlun_attr_store,
234 .allow_link = target_fabric_mappedlun_link,
235 .drop_link = target_fabric_mappedlun_unlink,
238 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
239 target_fabric_mappedlun_attrs);
241 /* End of tfc_tpg_mappedlun_cit */
243 /* Start of tfc_tpg_mappedlun_port_cit */
245 static struct config_group *target_core_mappedlun_stat_mkdir(
246 struct config_group *group,
249 return ERR_PTR(-ENOSYS);
252 static void target_core_mappedlun_stat_rmdir(
253 struct config_group *group,
254 struct config_item *item)
259 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
260 .make_group = target_core_mappedlun_stat_mkdir,
261 .drop_item = target_core_mappedlun_stat_rmdir,
264 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
267 /* End of tfc_tpg_mappedlun_port_cit */
269 /* Start of tfc_tpg_nacl_attrib_cit */
271 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
273 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
274 .show_attribute = target_fabric_nacl_attrib_attr_show,
275 .store_attribute = target_fabric_nacl_attrib_attr_store,
278 TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
280 /* End of tfc_tpg_nacl_attrib_cit */
282 /* Start of tfc_tpg_nacl_auth_cit */
284 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
286 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
287 .show_attribute = target_fabric_nacl_auth_attr_show,
288 .store_attribute = target_fabric_nacl_auth_attr_store,
291 TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
293 /* End of tfc_tpg_nacl_auth_cit */
295 /* Start of tfc_tpg_nacl_param_cit */
297 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
299 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
300 .show_attribute = target_fabric_nacl_param_attr_show,
301 .store_attribute = target_fabric_nacl_param_attr_store,
304 TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
306 /* End of tfc_tpg_nacl_param_cit */
308 /* Start of tfc_tpg_nacl_base_cit */
310 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
312 static struct config_group *target_fabric_make_mappedlun(
313 struct config_group *group,
316 struct se_node_acl *se_nacl = container_of(group,
317 struct se_node_acl, acl_group);
318 struct se_portal_group *se_tpg = se_nacl->se_tpg;
319 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
320 struct se_lun_acl *lacl;
321 struct config_item *acl_ci;
322 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
324 unsigned long mapped_lun;
327 acl_ci = &group->cg_item;
329 pr_err("Unable to locatel acl_ci\n");
333 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
335 pr_err("Unable to allocate memory for name buf\n");
336 return ERR_PTR(-ENOMEM);
338 snprintf(buf, strlen(name) + 1, "%s", name);
340 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
342 if (strstr(buf, "lun_") != buf) {
343 pr_err("Unable to locate \"lun_\" from buf: %s"
344 " name: %s\n", buf, name);
349 * Determine the Mapped LUN value. This is what the SCSI Initiator
350 * Port will actually see.
352 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
357 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
358 config_item_name(acl_ci), &ret);
364 lacl_cg = &lacl->se_lun_group;
365 lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
367 if (!lacl_cg->default_groups) {
368 pr_err("Unable to allocate lacl_cg->default_groups\n");
373 config_group_init_type_name(&lacl->se_lun_group, name,
374 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
375 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
376 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
377 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
378 lacl_cg->default_groups[1] = NULL;
380 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
381 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
383 if (!ml_stat_grp->default_groups) {
384 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
388 target_stat_setup_mappedlun_default_groups(lacl);
391 return &lacl->se_lun_group;
394 kfree(lacl_cg->default_groups);
399 static void target_fabric_drop_mappedlun(
400 struct config_group *group,
401 struct config_item *item)
403 struct se_lun_acl *lacl = container_of(to_config_group(item),
404 struct se_lun_acl, se_lun_group);
405 struct config_item *df_item;
406 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
409 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
410 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
411 df_item = &ml_stat_grp->default_groups[i]->cg_item;
412 ml_stat_grp->default_groups[i] = NULL;
413 config_item_put(df_item);
415 kfree(ml_stat_grp->default_groups);
417 lacl_cg = &lacl->se_lun_group;
418 for (i = 0; lacl_cg->default_groups[i]; i++) {
419 df_item = &lacl_cg->default_groups[i]->cg_item;
420 lacl_cg->default_groups[i] = NULL;
421 config_item_put(df_item);
423 kfree(lacl_cg->default_groups);
425 config_item_put(item);
428 static void target_fabric_nacl_base_release(struct config_item *item)
430 struct se_node_acl *se_nacl = container_of(to_config_group(item),
431 struct se_node_acl, acl_group);
432 struct se_portal_group *se_tpg = se_nacl->se_tpg;
433 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
435 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
438 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
439 .release = target_fabric_nacl_base_release,
440 .show_attribute = target_fabric_nacl_base_attr_show,
441 .store_attribute = target_fabric_nacl_base_attr_store,
444 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
445 .make_group = target_fabric_make_mappedlun,
446 .drop_item = target_fabric_drop_mappedlun,
449 TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
450 &target_fabric_nacl_base_group_ops, NULL);
452 /* End of tfc_tpg_nacl_base_cit */
454 /* Start of tfc_node_fabric_stats_cit */
456 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
457 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
459 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
461 /* End of tfc_wwn_fabric_stats_cit */
463 /* Start of tfc_tpg_nacl_cit */
465 static struct config_group *target_fabric_make_nodeacl(
466 struct config_group *group,
469 struct se_portal_group *se_tpg = container_of(group,
470 struct se_portal_group, tpg_acl_group);
471 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
472 struct se_node_acl *se_nacl;
473 struct config_group *nacl_cg;
475 if (!tf->tf_ops.fabric_make_nodeacl) {
476 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
477 return ERR_PTR(-ENOSYS);
480 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
482 return ERR_CAST(se_nacl);
484 nacl_cg = &se_nacl->acl_group;
485 nacl_cg->default_groups = se_nacl->acl_default_groups;
486 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
487 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
488 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
489 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
490 nacl_cg->default_groups[4] = NULL;
492 config_group_init_type_name(&se_nacl->acl_group, name,
493 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
494 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
495 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
496 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
497 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
498 config_group_init_type_name(&se_nacl->acl_param_group, "param",
499 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
500 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
502 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
504 return &se_nacl->acl_group;
507 static void target_fabric_drop_nodeacl(
508 struct config_group *group,
509 struct config_item *item)
511 struct se_node_acl *se_nacl = container_of(to_config_group(item),
512 struct se_node_acl, acl_group);
513 struct config_item *df_item;
514 struct config_group *nacl_cg;
517 nacl_cg = &se_nacl->acl_group;
518 for (i = 0; nacl_cg->default_groups[i]; i++) {
519 df_item = &nacl_cg->default_groups[i]->cg_item;
520 nacl_cg->default_groups[i] = NULL;
521 config_item_put(df_item);
524 * struct se_node_acl free is done in target_fabric_nacl_base_release()
526 config_item_put(item);
529 static struct configfs_group_operations target_fabric_nacl_group_ops = {
530 .make_group = target_fabric_make_nodeacl,
531 .drop_item = target_fabric_drop_nodeacl,
534 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
536 /* End of tfc_tpg_nacl_cit */
538 /* Start of tfc_tpg_np_base_cit */
540 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
542 static void target_fabric_np_base_release(struct config_item *item)
544 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
545 struct se_tpg_np, tpg_np_group);
546 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
547 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
549 tf->tf_ops.fabric_drop_np(se_tpg_np);
552 static struct configfs_item_operations target_fabric_np_base_item_ops = {
553 .release = target_fabric_np_base_release,
554 .show_attribute = target_fabric_np_base_attr_show,
555 .store_attribute = target_fabric_np_base_attr_store,
558 TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
560 /* End of tfc_tpg_np_base_cit */
562 /* Start of tfc_tpg_np_cit */
564 static struct config_group *target_fabric_make_np(
565 struct config_group *group,
568 struct se_portal_group *se_tpg = container_of(group,
569 struct se_portal_group, tpg_np_group);
570 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
571 struct se_tpg_np *se_tpg_np;
573 if (!tf->tf_ops.fabric_make_np) {
574 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
575 return ERR_PTR(-ENOSYS);
578 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
579 if (!se_tpg_np || IS_ERR(se_tpg_np))
580 return ERR_PTR(-EINVAL);
582 se_tpg_np->tpg_np_parent = se_tpg;
583 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
584 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
586 return &se_tpg_np->tpg_np_group;
589 static void target_fabric_drop_np(
590 struct config_group *group,
591 struct config_item *item)
594 * struct se_tpg_np is released via target_fabric_np_base_release()
596 config_item_put(item);
599 static struct configfs_group_operations target_fabric_np_group_ops = {
600 .make_group = &target_fabric_make_np,
601 .drop_item = &target_fabric_drop_np,
604 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
606 /* End of tfc_tpg_np_cit */
608 /* Start of tfc_tpg_port_cit */
610 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
611 #define TCM_PORT_ATTR(_name, _mode) \
612 static struct target_fabric_port_attribute target_fabric_port_##_name = \
613 __CONFIGFS_EATTR(_name, _mode, \
614 target_fabric_port_show_attr_##_name, \
615 target_fabric_port_store_attr_##_name);
617 #define TCM_PORT_ATTOR_RO(_name) \
618 __CONFIGFS_EATTR_RO(_name, \
619 target_fabric_port_show_attr_##_name);
624 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
628 if (!lun || !lun->lun_sep)
631 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
634 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
639 if (!lun || !lun->lun_sep)
642 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
645 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
650 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
654 if (!lun || !lun->lun_sep)
657 return core_alua_show_offline_bit(lun, page);
660 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
665 if (!lun || !lun->lun_sep)
668 return core_alua_store_offline_bit(lun, page, count);
671 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
676 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
680 if (!lun || !lun->lun_sep)
683 return core_alua_show_secondary_status(lun, page);
686 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
691 if (!lun || !lun->lun_sep)
694 return core_alua_store_secondary_status(lun, page, count);
697 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
700 * alua_tg_pt_write_md
702 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
706 if (!lun || !lun->lun_sep)
709 return core_alua_show_secondary_write_metadata(lun, page);
712 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
717 if (!lun || !lun->lun_sep)
720 return core_alua_store_secondary_write_metadata(lun, page, count);
723 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
726 static struct configfs_attribute *target_fabric_port_attrs[] = {
727 &target_fabric_port_alua_tg_pt_gp.attr,
728 &target_fabric_port_alua_tg_pt_offline.attr,
729 &target_fabric_port_alua_tg_pt_status.attr,
730 &target_fabric_port_alua_tg_pt_write_md.attr,
734 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
736 static int target_fabric_port_link(
737 struct config_item *lun_ci,
738 struct config_item *se_dev_ci)
740 struct config_item *tpg_ci;
741 struct se_device *dev;
742 struct se_lun *lun = container_of(to_config_group(lun_ci),
743 struct se_lun, lun_group);
744 struct se_lun *lun_p;
745 struct se_portal_group *se_tpg;
746 struct se_subsystem_dev *se_dev = container_of(
747 to_config_group(se_dev_ci), struct se_subsystem_dev,
749 struct target_fabric_configfs *tf;
752 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
753 se_tpg = container_of(to_config_group(tpg_ci),
754 struct se_portal_group, tpg_group);
755 tf = se_tpg->se_tpg_wwn->wwn_tf;
757 if (lun->lun_se_dev != NULL) {
758 pr_err("Port Symlink already exists\n");
762 dev = se_dev->se_dev_ptr;
764 pr_err("Unable to locate struct se_device pointer from"
765 " %s\n", config_item_name(se_dev_ci));
770 lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
772 if (IS_ERR(lun_p) || !lun_p) {
773 pr_err("core_dev_add_lun() failed\n");
778 if (tf->tf_ops.fabric_post_link) {
780 * Call the optional fabric_post_link() to allow a
781 * fabric module to setup any additional state once
782 * core_dev_add_lun() has been called..
784 tf->tf_ops.fabric_post_link(se_tpg, lun);
792 static int target_fabric_port_unlink(
793 struct config_item *lun_ci,
794 struct config_item *se_dev_ci)
796 struct se_lun *lun = container_of(to_config_group(lun_ci),
797 struct se_lun, lun_group);
798 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
799 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
801 if (tf->tf_ops.fabric_pre_unlink) {
803 * Call the optional fabric_pre_unlink() to allow a
804 * fabric module to release any additional stat before
805 * core_dev_del_lun() is called.
807 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
810 core_dev_del_lun(se_tpg, lun->unpacked_lun);
814 static struct configfs_item_operations target_fabric_port_item_ops = {
815 .show_attribute = target_fabric_port_attr_show,
816 .store_attribute = target_fabric_port_attr_store,
817 .allow_link = target_fabric_port_link,
818 .drop_link = target_fabric_port_unlink,
821 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
823 /* End of tfc_tpg_port_cit */
825 /* Start of tfc_tpg_port_stat_cit */
827 static struct config_group *target_core_port_stat_mkdir(
828 struct config_group *group,
831 return ERR_PTR(-ENOSYS);
834 static void target_core_port_stat_rmdir(
835 struct config_group *group,
836 struct config_item *item)
841 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
842 .make_group = target_core_port_stat_mkdir,
843 .drop_item = target_core_port_stat_rmdir,
846 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
848 /* End of tfc_tpg_port_stat_cit */
850 /* Start of tfc_tpg_lun_cit */
852 static struct config_group *target_fabric_make_lun(
853 struct config_group *group,
857 struct se_portal_group *se_tpg = container_of(group,
858 struct se_portal_group, tpg_lun_group);
859 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
860 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
861 unsigned long unpacked_lun;
864 if (strstr(name, "lun_") != name) {
865 pr_err("Unable to locate \'_\" in"
866 " \"lun_$LUN_NUMBER\"\n");
867 return ERR_PTR(-EINVAL);
869 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
870 return ERR_PTR(-EINVAL);
872 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
874 return ERR_PTR(-EINVAL);
876 lun_cg = &lun->lun_group;
877 lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
879 if (!lun_cg->default_groups) {
880 pr_err("Unable to allocate lun_cg->default_groups\n");
881 return ERR_PTR(-ENOMEM);
884 config_group_init_type_name(&lun->lun_group, name,
885 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
886 config_group_init_type_name(&lun->port_stat_grps.stat_group,
887 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
888 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
889 lun_cg->default_groups[1] = NULL;
891 port_stat_grp = &lun->port_stat_grps.stat_group;
892 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
894 if (!port_stat_grp->default_groups) {
895 pr_err("Unable to allocate port_stat_grp->default_groups\n");
899 target_stat_setup_port_default_groups(lun);
901 return &lun->lun_group;
904 kfree(lun_cg->default_groups);
905 return ERR_PTR(errno);
908 static void target_fabric_drop_lun(
909 struct config_group *group,
910 struct config_item *item)
912 struct se_lun *lun = container_of(to_config_group(item),
913 struct se_lun, lun_group);
914 struct config_item *df_item;
915 struct config_group *lun_cg, *port_stat_grp;
918 port_stat_grp = &lun->port_stat_grps.stat_group;
919 for (i = 0; port_stat_grp->default_groups[i]; i++) {
920 df_item = &port_stat_grp->default_groups[i]->cg_item;
921 port_stat_grp->default_groups[i] = NULL;
922 config_item_put(df_item);
924 kfree(port_stat_grp->default_groups);
926 lun_cg = &lun->lun_group;
927 for (i = 0; lun_cg->default_groups[i]; i++) {
928 df_item = &lun_cg->default_groups[i]->cg_item;
929 lun_cg->default_groups[i] = NULL;
930 config_item_put(df_item);
932 kfree(lun_cg->default_groups);
934 config_item_put(item);
937 static struct configfs_group_operations target_fabric_lun_group_ops = {
938 .make_group = &target_fabric_make_lun,
939 .drop_item = &target_fabric_drop_lun,
942 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
944 /* End of tfc_tpg_lun_cit */
946 /* Start of tfc_tpg_attrib_cit */
948 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
950 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
951 .show_attribute = target_fabric_tpg_attrib_attr_show,
952 .store_attribute = target_fabric_tpg_attrib_attr_store,
955 TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
957 /* End of tfc_tpg_attrib_cit */
959 /* Start of tfc_tpg_param_cit */
961 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
963 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
964 .show_attribute = target_fabric_tpg_param_attr_show,
965 .store_attribute = target_fabric_tpg_param_attr_store,
968 TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
970 /* End of tfc_tpg_param_cit */
972 /* Start of tfc_tpg_base_cit */
974 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
976 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
978 static void target_fabric_tpg_release(struct config_item *item)
980 struct se_portal_group *se_tpg = container_of(to_config_group(item),
981 struct se_portal_group, tpg_group);
982 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
983 struct target_fabric_configfs *tf = wwn->wwn_tf;
985 tf->tf_ops.fabric_drop_tpg(se_tpg);
988 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
989 .release = target_fabric_tpg_release,
990 .show_attribute = target_fabric_tpg_attr_show,
991 .store_attribute = target_fabric_tpg_attr_store,
994 TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
996 /* End of tfc_tpg_base_cit */
998 /* Start of tfc_tpg_cit */
1000 static struct config_group *target_fabric_make_tpg(
1001 struct config_group *group,
1004 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1005 struct target_fabric_configfs *tf = wwn->wwn_tf;
1006 struct se_portal_group *se_tpg;
1008 if (!tf->tf_ops.fabric_make_tpg) {
1009 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
1010 return ERR_PTR(-ENOSYS);
1013 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
1014 if (!se_tpg || IS_ERR(se_tpg))
1015 return ERR_PTR(-EINVAL);
1017 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1019 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1020 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1021 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1022 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1023 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1024 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1025 se_tpg->tpg_group.default_groups[5] = NULL;
1027 config_group_init_type_name(&se_tpg->tpg_group, name,
1028 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1029 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1030 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1031 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1032 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1033 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1034 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1035 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1036 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1037 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1038 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1040 return &se_tpg->tpg_group;
1043 static void target_fabric_drop_tpg(
1044 struct config_group *group,
1045 struct config_item *item)
1047 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1048 struct se_portal_group, tpg_group);
1049 struct config_group *tpg_cg = &se_tpg->tpg_group;
1050 struct config_item *df_item;
1053 * Release default groups, but do not release tpg_cg->default_groups
1054 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1056 for (i = 0; tpg_cg->default_groups[i]; i++) {
1057 df_item = &tpg_cg->default_groups[i]->cg_item;
1058 tpg_cg->default_groups[i] = NULL;
1059 config_item_put(df_item);
1062 config_item_put(item);
1065 static void target_fabric_release_wwn(struct config_item *item)
1067 struct se_wwn *wwn = container_of(to_config_group(item),
1068 struct se_wwn, wwn_group);
1069 struct target_fabric_configfs *tf = wwn->wwn_tf;
1071 tf->tf_ops.fabric_drop_wwn(wwn);
1074 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1075 .release = target_fabric_release_wwn,
1078 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1079 .make_group = target_fabric_make_tpg,
1080 .drop_item = target_fabric_drop_tpg,
1083 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1086 /* End of tfc_tpg_cit */
1088 /* Start of tfc_wwn_fabric_stats_cit */
1090 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1091 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1093 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1095 /* End of tfc_wwn_fabric_stats_cit */
1097 /* Start of tfc_wwn_cit */
1099 static struct config_group *target_fabric_make_wwn(
1100 struct config_group *group,
1103 struct target_fabric_configfs *tf = container_of(group,
1104 struct target_fabric_configfs, tf_group);
1107 if (!tf->tf_ops.fabric_make_wwn) {
1108 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1109 return ERR_PTR(-ENOSYS);
1112 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
1113 if (!wwn || IS_ERR(wwn))
1114 return ERR_PTR(-EINVAL);
1118 * Setup default groups from pre-allocated wwn->wwn_default_groups
1120 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1121 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1122 wwn->wwn_group.default_groups[1] = NULL;
1124 config_group_init_type_name(&wwn->wwn_group, name,
1125 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
1126 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1127 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
1129 return &wwn->wwn_group;
1132 static void target_fabric_drop_wwn(
1133 struct config_group *group,
1134 struct config_item *item)
1136 struct se_wwn *wwn = container_of(to_config_group(item),
1137 struct se_wwn, wwn_group);
1138 struct config_item *df_item;
1139 struct config_group *cg = &wwn->wwn_group;
1142 for (i = 0; cg->default_groups[i]; i++) {
1143 df_item = &cg->default_groups[i]->cg_item;
1144 cg->default_groups[i] = NULL;
1145 config_item_put(df_item);
1148 config_item_put(item);
1151 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1152 .make_group = target_fabric_make_wwn,
1153 .drop_item = target_fabric_drop_wwn,
1156 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1158 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1160 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1161 .show_attribute = target_fabric_wwn_attr_show,
1162 .store_attribute = target_fabric_wwn_attr_store,
1165 TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1167 /* End of tfc_wwn_cit */
1169 /* Start of tfc_discovery_cit */
1171 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1174 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1175 .show_attribute = target_fabric_discovery_attr_show,
1176 .store_attribute = target_fabric_discovery_attr_store,
1179 TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1181 /* End of tfc_discovery_cit */
1183 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1185 target_fabric_setup_discovery_cit(tf);
1186 target_fabric_setup_wwn_cit(tf);
1187 target_fabric_setup_wwn_fabric_stats_cit(tf);
1188 target_fabric_setup_tpg_cit(tf);
1189 target_fabric_setup_tpg_base_cit(tf);
1190 target_fabric_setup_tpg_port_cit(tf);
1191 target_fabric_setup_tpg_port_stat_cit(tf);
1192 target_fabric_setup_tpg_lun_cit(tf);
1193 target_fabric_setup_tpg_np_cit(tf);
1194 target_fabric_setup_tpg_np_base_cit(tf);
1195 target_fabric_setup_tpg_attrib_cit(tf);
1196 target_fabric_setup_tpg_param_cit(tf);
1197 target_fabric_setup_tpg_nacl_cit(tf);
1198 target_fabric_setup_tpg_nacl_base_cit(tf);
1199 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1200 target_fabric_setup_tpg_nacl_auth_cit(tf);
1201 target_fabric_setup_tpg_nacl_param_cit(tf);
1202 target_fabric_setup_tpg_nacl_stat_cit(tf);
1203 target_fabric_setup_tpg_mappedlun_cit(tf);
1204 target_fabric_setup_tpg_mappedlun_stat_cit(tf);