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/configfs_macros.h>
41 #include "target_core_internal.h"
42 #include "target_core_alua.h"
43 #include "target_core_pr.h"
45 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
46 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
48 struct config_item_type *cit = &tf->tf_##_name##_cit; \
50 cit->ct_item_ops = _item_ops; \
51 cit->ct_group_ops = _group_ops; \
52 cit->ct_attrs = _attrs; \
53 cit->ct_owner = tf->tf_ops->module; \
54 pr_debug("Setup generic %s\n", __stringify(_name)); \
57 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
58 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
60 struct config_item_type *cit = &tf->tf_##_name##_cit; \
61 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
63 cit->ct_item_ops = _item_ops; \
64 cit->ct_group_ops = _group_ops; \
65 cit->ct_attrs = attrs; \
66 cit->ct_owner = tf->tf_ops->module; \
67 pr_debug("Setup generic %s\n", __stringify(_name)); \
70 /* Start of tfc_tpg_mappedlun_cit */
72 static int target_fabric_mappedlun_link(
73 struct config_item *lun_acl_ci,
74 struct config_item *lun_ci)
76 struct se_dev_entry *deve;
77 struct se_lun *lun = container_of(to_config_group(lun_ci),
78 struct se_lun, lun_group);
79 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
80 struct se_lun_acl, se_lun_group);
81 struct se_portal_group *se_tpg;
82 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
85 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
86 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
87 " %p to struct lun: %p\n", lun_ci, lun);
91 * Ensure that the source port exists
93 if (!lun->lun_se_dev) {
94 pr_err("Source se_lun->lun_se_dev does not exist\n");
97 se_tpg = lun->lun_tpg;
99 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
100 tpg_ci = &nacl_ci->ci_group->cg_item;
101 wwn_ci = &tpg_ci->ci_group->cg_item;
102 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
103 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
105 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
107 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
108 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
109 config_item_name(wwn_ci));
112 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
113 pr_err("Illegal Initiator ACL Symlink outside of %s"
114 " TPGT: %s\n", config_item_name(wwn_ci),
115 config_item_name(tpg_ci));
119 * If this struct se_node_acl was dynamically generated with
120 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
121 * which be will write protected (READ-ONLY) when
122 * tpg_1/attrib/demo_mode_write_protect=1
125 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
127 lun_access = deve->lun_flags;
130 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
131 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
132 TRANSPORT_LUNFLAGS_READ_WRITE;
135 * Determine the actual mapped LUN value user wants..
137 * This value is what the SCSI Initiator actually sees the
138 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
140 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
143 static int target_fabric_mappedlun_unlink(
144 struct config_item *lun_acl_ci,
145 struct config_item *lun_ci)
147 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
148 struct se_lun_acl, se_lun_group);
149 struct se_lun *lun = container_of(to_config_group(lun_ci),
150 struct se_lun, lun_group);
152 return core_dev_del_initiator_node_lun_acl(lun, lacl);
155 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
156 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
157 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
158 __CONFIGFS_EATTR(_name, _mode, \
159 target_fabric_mappedlun_show_##_name, \
160 target_fabric_mappedlun_store_##_name);
162 static ssize_t target_fabric_mappedlun_show_write_protect(
163 struct se_lun_acl *lacl,
166 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
167 struct se_dev_entry *deve;
171 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
173 len = sprintf(page, "%d\n",
174 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
181 static ssize_t target_fabric_mappedlun_store_write_protect(
182 struct se_lun_acl *lacl,
186 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
187 struct se_portal_group *se_tpg = se_nacl->se_tpg;
191 ret = kstrtoul(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: %llu Write Protect bit to %s\n",
205 se_tpg->se_tpg_tfo->get_fabric_name(),
206 se_nacl->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_DRV(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, 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_DRV(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, 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_DRV(tpg_nacl_param, &target_fabric_nacl_param_item_ops, 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 = NULL;
321 struct config_item *acl_ci;
322 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
324 unsigned long 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 ret = kstrtoull(buf + 4, 0, &mapped_lun);
356 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
363 lacl_cg = &lacl->se_lun_group;
364 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
366 if (!lacl_cg->default_groups) {
367 pr_err("Unable to allocate lacl_cg->default_groups\n");
372 config_group_init_type_name(&lacl->se_lun_group, name,
373 &tf->tf_tpg_mappedlun_cit);
374 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
375 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
376 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
377 lacl_cg->default_groups[1] = NULL;
379 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
380 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
382 if (!ml_stat_grp->default_groups) {
383 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
387 target_stat_setup_mappedlun_default_groups(lacl);
390 return &lacl->se_lun_group;
393 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 target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
434 if (tf->tf_ops->fabric_cleanup_nodeacl)
435 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
436 core_tpg_del_initiator_node_acl(se_nacl);
439 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
440 .release = target_fabric_nacl_base_release,
441 .show_attribute = target_fabric_nacl_base_attr_show,
442 .store_attribute = target_fabric_nacl_base_attr_store,
445 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
446 .make_group = target_fabric_make_mappedlun,
447 .drop_item = target_fabric_drop_mappedlun,
450 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
451 &target_fabric_nacl_base_group_ops);
453 /* End of tfc_tpg_nacl_base_cit */
455 /* Start of tfc_node_fabric_stats_cit */
457 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
458 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
460 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
462 /* End of tfc_wwn_fabric_stats_cit */
464 /* Start of tfc_tpg_nacl_cit */
466 static struct config_group *target_fabric_make_nodeacl(
467 struct config_group *group,
470 struct se_portal_group *se_tpg = container_of(group,
471 struct se_portal_group, tpg_acl_group);
472 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
473 struct se_node_acl *se_nacl;
474 struct config_group *nacl_cg;
476 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
478 return ERR_CAST(se_nacl);
480 if (tf->tf_ops->fabric_init_nodeacl) {
481 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
483 core_tpg_del_initiator_node_acl(se_nacl);
488 nacl_cg = &se_nacl->acl_group;
489 nacl_cg->default_groups = se_nacl->acl_default_groups;
490 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
491 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
492 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
493 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
494 nacl_cg->default_groups[4] = NULL;
496 config_group_init_type_name(&se_nacl->acl_group, name,
497 &tf->tf_tpg_nacl_base_cit);
498 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
499 &tf->tf_tpg_nacl_attrib_cit);
500 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
501 &tf->tf_tpg_nacl_auth_cit);
502 config_group_init_type_name(&se_nacl->acl_param_group, "param",
503 &tf->tf_tpg_nacl_param_cit);
504 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
505 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
507 return &se_nacl->acl_group;
510 static void target_fabric_drop_nodeacl(
511 struct config_group *group,
512 struct config_item *item)
514 struct se_node_acl *se_nacl = container_of(to_config_group(item),
515 struct se_node_acl, acl_group);
516 struct config_item *df_item;
517 struct config_group *nacl_cg;
520 nacl_cg = &se_nacl->acl_group;
521 for (i = 0; nacl_cg->default_groups[i]; i++) {
522 df_item = &nacl_cg->default_groups[i]->cg_item;
523 nacl_cg->default_groups[i] = NULL;
524 config_item_put(df_item);
527 * struct se_node_acl free is done in target_fabric_nacl_base_release()
529 config_item_put(item);
532 static struct configfs_group_operations target_fabric_nacl_group_ops = {
533 .make_group = target_fabric_make_nodeacl,
534 .drop_item = target_fabric_drop_nodeacl,
537 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
539 /* End of tfc_tpg_nacl_cit */
541 /* Start of tfc_tpg_np_base_cit */
543 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
545 static void target_fabric_np_base_release(struct config_item *item)
547 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
548 struct se_tpg_np, tpg_np_group);
549 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
550 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
552 tf->tf_ops->fabric_drop_np(se_tpg_np);
555 static struct configfs_item_operations target_fabric_np_base_item_ops = {
556 .release = target_fabric_np_base_release,
557 .show_attribute = target_fabric_np_base_attr_show,
558 .store_attribute = target_fabric_np_base_attr_store,
561 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
563 /* End of tfc_tpg_np_base_cit */
565 /* Start of tfc_tpg_np_cit */
567 static struct config_group *target_fabric_make_np(
568 struct config_group *group,
571 struct se_portal_group *se_tpg = container_of(group,
572 struct se_portal_group, tpg_np_group);
573 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
574 struct se_tpg_np *se_tpg_np;
576 if (!tf->tf_ops->fabric_make_np) {
577 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
578 return ERR_PTR(-ENOSYS);
581 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
582 if (!se_tpg_np || IS_ERR(se_tpg_np))
583 return ERR_PTR(-EINVAL);
585 se_tpg_np->tpg_np_parent = se_tpg;
586 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
587 &tf->tf_tpg_np_base_cit);
589 return &se_tpg_np->tpg_np_group;
592 static void target_fabric_drop_np(
593 struct config_group *group,
594 struct config_item *item)
597 * struct se_tpg_np is released via target_fabric_np_base_release()
599 config_item_put(item);
602 static struct configfs_group_operations target_fabric_np_group_ops = {
603 .make_group = &target_fabric_make_np,
604 .drop_item = &target_fabric_drop_np,
607 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
609 /* End of tfc_tpg_np_cit */
611 /* Start of tfc_tpg_port_cit */
613 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
614 #define TCM_PORT_ATTR(_name, _mode) \
615 static struct target_fabric_port_attribute target_fabric_port_##_name = \
616 __CONFIGFS_EATTR(_name, _mode, \
617 target_fabric_port_show_attr_##_name, \
618 target_fabric_port_store_attr_##_name);
620 #define TCM_PORT_ATTOR_RO(_name) \
621 __CONFIGFS_EATTR_RO(_name, \
622 target_fabric_port_show_attr_##_name);
627 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
631 if (!lun || !lun->lun_se_dev)
634 return core_alua_show_tg_pt_gp_info(lun, page);
637 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
642 if (!lun || !lun->lun_se_dev)
645 return core_alua_store_tg_pt_gp_info(lun, page, count);
648 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
653 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
657 if (!lun || !lun->lun_se_dev)
660 return core_alua_show_offline_bit(lun, page);
663 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
668 if (!lun || !lun->lun_se_dev)
671 return core_alua_store_offline_bit(lun, page, count);
674 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
679 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
683 if (!lun || !lun->lun_se_dev)
686 return core_alua_show_secondary_status(lun, page);
689 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
694 if (!lun || !lun->lun_se_dev)
697 return core_alua_store_secondary_status(lun, page, count);
700 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
703 * alua_tg_pt_write_md
705 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
709 if (!lun || !lun->lun_se_dev)
712 return core_alua_show_secondary_write_metadata(lun, page);
715 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
720 if (!lun || !lun->lun_se_dev)
723 return core_alua_store_secondary_write_metadata(lun, page, count);
726 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
729 static struct configfs_attribute *target_fabric_port_attrs[] = {
730 &target_fabric_port_alua_tg_pt_gp.attr,
731 &target_fabric_port_alua_tg_pt_offline.attr,
732 &target_fabric_port_alua_tg_pt_status.attr,
733 &target_fabric_port_alua_tg_pt_write_md.attr,
737 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
739 static int target_fabric_port_link(
740 struct config_item *lun_ci,
741 struct config_item *se_dev_ci)
743 struct config_item *tpg_ci;
744 struct se_lun *lun = container_of(to_config_group(lun_ci),
745 struct se_lun, lun_group);
746 struct se_portal_group *se_tpg;
747 struct se_device *dev =
748 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
749 struct target_fabric_configfs *tf;
752 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
753 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
754 " %p to struct se_device: %p\n", se_dev_ci, dev);
758 if (!(dev->dev_flags & DF_CONFIGURED)) {
759 pr_err("se_device not configured yet, cannot port link\n");
763 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
764 se_tpg = container_of(to_config_group(tpg_ci),
765 struct se_portal_group, tpg_group);
766 tf = se_tpg->se_tpg_wwn->wwn_tf;
768 if (lun->lun_se_dev != NULL) {
769 pr_err("Port Symlink already exists\n");
773 ret = core_dev_add_lun(se_tpg, dev, lun);
775 pr_err("core_dev_add_lun() failed: %d\n", ret);
779 if (tf->tf_ops->fabric_post_link) {
781 * Call the optional fabric_post_link() to allow a
782 * fabric module to setup any additional state once
783 * core_dev_add_lun() has been called..
785 tf->tf_ops->fabric_post_link(se_tpg, lun);
793 static int target_fabric_port_unlink(
794 struct config_item *lun_ci,
795 struct config_item *se_dev_ci)
797 struct se_lun *lun = container_of(to_config_group(lun_ci),
798 struct se_lun, lun_group);
799 struct se_portal_group *se_tpg = lun->lun_tpg;
800 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
802 if (tf->tf_ops->fabric_pre_unlink) {
804 * Call the optional fabric_pre_unlink() to allow a
805 * fabric module to release any additional stat before
806 * core_dev_del_lun() is called.
808 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
811 core_dev_del_lun(se_tpg, lun);
815 static void target_fabric_port_release(struct config_item *item)
817 struct se_lun *lun = container_of(to_config_group(item),
818 struct se_lun, lun_group);
820 kfree_rcu(lun, rcu_head);
823 static struct configfs_item_operations target_fabric_port_item_ops = {
824 .show_attribute = target_fabric_port_attr_show,
825 .store_attribute = target_fabric_port_attr_store,
826 .release = target_fabric_port_release,
827 .allow_link = target_fabric_port_link,
828 .drop_link = target_fabric_port_unlink,
831 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
833 /* End of tfc_tpg_port_cit */
835 /* Start of tfc_tpg_port_stat_cit */
837 static struct config_group *target_core_port_stat_mkdir(
838 struct config_group *group,
841 return ERR_PTR(-ENOSYS);
844 static void target_core_port_stat_rmdir(
845 struct config_group *group,
846 struct config_item *item)
851 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
852 .make_group = target_core_port_stat_mkdir,
853 .drop_item = target_core_port_stat_rmdir,
856 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
858 /* End of tfc_tpg_port_stat_cit */
860 /* Start of tfc_tpg_lun_cit */
862 static struct config_group *target_fabric_make_lun(
863 struct config_group *group,
867 struct se_portal_group *se_tpg = container_of(group,
868 struct se_portal_group, tpg_lun_group);
869 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
870 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
871 unsigned long long unpacked_lun;
874 if (strstr(name, "lun_") != name) {
875 pr_err("Unable to locate \'_\" in"
876 " \"lun_$LUN_NUMBER\"\n");
877 return ERR_PTR(-EINVAL);
879 errno = kstrtoull(name + 4, 0, &unpacked_lun);
881 return ERR_PTR(errno);
883 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
885 return ERR_CAST(lun);
887 lun_cg = &lun->lun_group;
888 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
890 if (!lun_cg->default_groups) {
891 pr_err("Unable to allocate lun_cg->default_groups\n");
893 return ERR_PTR(-ENOMEM);
896 config_group_init_type_name(&lun->lun_group, name,
897 &tf->tf_tpg_port_cit);
898 config_group_init_type_name(&lun->port_stat_grps.stat_group,
899 "statistics", &tf->tf_tpg_port_stat_cit);
900 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
901 lun_cg->default_groups[1] = NULL;
903 port_stat_grp = &lun->port_stat_grps.stat_group;
904 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
906 if (!port_stat_grp->default_groups) {
907 pr_err("Unable to allocate port_stat_grp->default_groups\n");
908 kfree(lun_cg->default_groups);
910 return ERR_PTR(-ENOMEM);
912 target_stat_setup_port_default_groups(lun);
914 return &lun->lun_group;
917 static void target_fabric_drop_lun(
918 struct config_group *group,
919 struct config_item *item)
921 struct se_lun *lun = container_of(to_config_group(item),
922 struct se_lun, lun_group);
923 struct config_item *df_item;
924 struct config_group *lun_cg, *port_stat_grp;
927 port_stat_grp = &lun->port_stat_grps.stat_group;
928 for (i = 0; port_stat_grp->default_groups[i]; i++) {
929 df_item = &port_stat_grp->default_groups[i]->cg_item;
930 port_stat_grp->default_groups[i] = NULL;
931 config_item_put(df_item);
933 kfree(port_stat_grp->default_groups);
935 lun_cg = &lun->lun_group;
936 for (i = 0; lun_cg->default_groups[i]; i++) {
937 df_item = &lun_cg->default_groups[i]->cg_item;
938 lun_cg->default_groups[i] = NULL;
939 config_item_put(df_item);
941 kfree(lun_cg->default_groups);
943 config_item_put(item);
946 static struct configfs_group_operations target_fabric_lun_group_ops = {
947 .make_group = &target_fabric_make_lun,
948 .drop_item = &target_fabric_drop_lun,
951 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
953 /* End of tfc_tpg_lun_cit */
955 /* Start of tfc_tpg_attrib_cit */
957 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
959 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
960 .show_attribute = target_fabric_tpg_attrib_attr_show,
961 .store_attribute = target_fabric_tpg_attrib_attr_store,
964 TF_CIT_SETUP_DRV(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL);
966 /* End of tfc_tpg_attrib_cit */
968 /* Start of tfc_tpg_auth_cit */
970 CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
972 static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
973 .show_attribute = target_fabric_tpg_auth_attr_show,
974 .store_attribute = target_fabric_tpg_auth_attr_store,
977 TF_CIT_SETUP_DRV(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL);
979 /* End of tfc_tpg_attrib_cit */
981 /* Start of tfc_tpg_param_cit */
983 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
985 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
986 .show_attribute = target_fabric_tpg_param_attr_show,
987 .store_attribute = target_fabric_tpg_param_attr_store,
990 TF_CIT_SETUP_DRV(tpg_param, &target_fabric_tpg_param_item_ops, NULL);
992 /* End of tfc_tpg_param_cit */
994 /* Start of tfc_tpg_base_cit */
996 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
998 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1000 static void target_fabric_tpg_release(struct config_item *item)
1002 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1003 struct se_portal_group, tpg_group);
1004 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1005 struct target_fabric_configfs *tf = wwn->wwn_tf;
1007 tf->tf_ops->fabric_drop_tpg(se_tpg);
1010 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1011 .release = target_fabric_tpg_release,
1012 .show_attribute = target_fabric_tpg_attr_show,
1013 .store_attribute = target_fabric_tpg_attr_store,
1016 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
1018 /* End of tfc_tpg_base_cit */
1020 /* Start of tfc_tpg_cit */
1022 static struct config_group *target_fabric_make_tpg(
1023 struct config_group *group,
1026 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1027 struct target_fabric_configfs *tf = wwn->wwn_tf;
1028 struct se_portal_group *se_tpg;
1030 if (!tf->tf_ops->fabric_make_tpg) {
1031 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
1032 return ERR_PTR(-ENOSYS);
1035 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
1036 if (!se_tpg || IS_ERR(se_tpg))
1037 return ERR_PTR(-EINVAL);
1039 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1041 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1042 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1043 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1044 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1045 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1046 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1047 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1048 se_tpg->tpg_group.default_groups[6] = NULL;
1050 config_group_init_type_name(&se_tpg->tpg_group, name,
1051 &tf->tf_tpg_base_cit);
1052 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1053 &tf->tf_tpg_lun_cit);
1054 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1055 &tf->tf_tpg_np_cit);
1056 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1057 &tf->tf_tpg_nacl_cit);
1058 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1059 &tf->tf_tpg_attrib_cit);
1060 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
1061 &tf->tf_tpg_auth_cit);
1062 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1063 &tf->tf_tpg_param_cit);
1065 return &se_tpg->tpg_group;
1068 static void target_fabric_drop_tpg(
1069 struct config_group *group,
1070 struct config_item *item)
1072 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1073 struct se_portal_group, tpg_group);
1074 struct config_group *tpg_cg = &se_tpg->tpg_group;
1075 struct config_item *df_item;
1078 * Release default groups, but do not release tpg_cg->default_groups
1079 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1081 for (i = 0; tpg_cg->default_groups[i]; i++) {
1082 df_item = &tpg_cg->default_groups[i]->cg_item;
1083 tpg_cg->default_groups[i] = NULL;
1084 config_item_put(df_item);
1087 config_item_put(item);
1090 static void target_fabric_release_wwn(struct config_item *item)
1092 struct se_wwn *wwn = container_of(to_config_group(item),
1093 struct se_wwn, wwn_group);
1094 struct target_fabric_configfs *tf = wwn->wwn_tf;
1096 tf->tf_ops->fabric_drop_wwn(wwn);
1099 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1100 .release = target_fabric_release_wwn,
1103 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1104 .make_group = target_fabric_make_tpg,
1105 .drop_item = target_fabric_drop_tpg,
1108 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1111 /* End of tfc_tpg_cit */
1113 /* Start of tfc_wwn_fabric_stats_cit */
1115 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1116 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1118 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1120 /* End of tfc_wwn_fabric_stats_cit */
1122 /* Start of tfc_wwn_cit */
1124 static struct config_group *target_fabric_make_wwn(
1125 struct config_group *group,
1128 struct target_fabric_configfs *tf = container_of(group,
1129 struct target_fabric_configfs, tf_group);
1132 if (!tf->tf_ops->fabric_make_wwn) {
1133 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1134 return ERR_PTR(-ENOSYS);
1137 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1138 if (!wwn || IS_ERR(wwn))
1139 return ERR_PTR(-EINVAL);
1143 * Setup default groups from pre-allocated wwn->wwn_default_groups
1145 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1146 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1147 wwn->wwn_group.default_groups[1] = NULL;
1149 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1150 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1151 &tf->tf_wwn_fabric_stats_cit);
1153 return &wwn->wwn_group;
1156 static void target_fabric_drop_wwn(
1157 struct config_group *group,
1158 struct config_item *item)
1160 struct se_wwn *wwn = container_of(to_config_group(item),
1161 struct se_wwn, wwn_group);
1162 struct config_item *df_item;
1163 struct config_group *cg = &wwn->wwn_group;
1166 for (i = 0; cg->default_groups[i]; i++) {
1167 df_item = &cg->default_groups[i]->cg_item;
1168 cg->default_groups[i] = NULL;
1169 config_item_put(df_item);
1172 config_item_put(item);
1175 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1176 .make_group = target_fabric_make_wwn,
1177 .drop_item = target_fabric_drop_wwn,
1180 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1182 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1184 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1185 .show_attribute = target_fabric_wwn_attr_show,
1186 .store_attribute = target_fabric_wwn_attr_store,
1189 TF_CIT_SETUP_DRV(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops);
1191 /* End of tfc_wwn_cit */
1193 /* Start of tfc_discovery_cit */
1195 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1198 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1199 .show_attribute = target_fabric_discovery_attr_show,
1200 .store_attribute = target_fabric_discovery_attr_store,
1203 TF_CIT_SETUP_DRV(discovery, &target_fabric_discovery_item_ops, NULL);
1205 /* End of tfc_discovery_cit */
1207 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1209 target_fabric_setup_discovery_cit(tf);
1210 target_fabric_setup_wwn_cit(tf);
1211 target_fabric_setup_wwn_fabric_stats_cit(tf);
1212 target_fabric_setup_tpg_cit(tf);
1213 target_fabric_setup_tpg_base_cit(tf);
1214 target_fabric_setup_tpg_port_cit(tf);
1215 target_fabric_setup_tpg_port_stat_cit(tf);
1216 target_fabric_setup_tpg_lun_cit(tf);
1217 target_fabric_setup_tpg_np_cit(tf);
1218 target_fabric_setup_tpg_np_base_cit(tf);
1219 target_fabric_setup_tpg_attrib_cit(tf);
1220 target_fabric_setup_tpg_auth_cit(tf);
1221 target_fabric_setup_tpg_param_cit(tf);
1222 target_fabric_setup_tpg_nacl_cit(tf);
1223 target_fabric_setup_tpg_nacl_base_cit(tf);
1224 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1225 target_fabric_setup_tpg_nacl_auth_cit(tf);
1226 target_fabric_setup_tpg_nacl_param_cit(tf);
1227 target_fabric_setup_tpg_nacl_stat_cit(tf);
1228 target_fabric_setup_tpg_mappedlun_cit(tf);
1229 target_fabric_setup_tpg_mappedlun_stat_cit(tf);