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>
39 #include "target_core_internal.h"
40 #include "target_core_alua.h"
41 #include "target_core_pr.h"
43 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
44 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
46 struct config_item_type *cit = &tf->tf_##_name##_cit; \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
51 cit->ct_owner = tf->tf_ops->module; \
52 pr_debug("Setup generic %s\n", __stringify(_name)); \
55 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
58 struct config_item_type *cit = &tf->tf_##_name##_cit; \
59 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
61 cit->ct_item_ops = _item_ops; \
62 cit->ct_group_ops = _group_ops; \
63 cit->ct_attrs = attrs; \
64 cit->ct_owner = tf->tf_ops->module; \
65 pr_debug("Setup generic %s\n", __stringify(_name)); \
68 /* Start of tfc_tpg_mappedlun_cit */
70 static int target_fabric_mappedlun_link(
71 struct config_item *lun_acl_ci,
72 struct config_item *lun_ci)
74 struct se_dev_entry *deve;
75 struct se_lun *lun = container_of(to_config_group(lun_ci),
76 struct se_lun, lun_group);
77 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
78 struct se_lun_acl, se_lun_group);
79 struct se_portal_group *se_tpg;
80 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
83 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
84 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
85 " %p to struct lun: %p\n", lun_ci, lun);
89 * Ensure that the source port exists
91 if (!lun->lun_se_dev) {
92 pr_err("Source se_lun->lun_se_dev does not exist\n");
95 if (lun->lun_shutdown) {
96 pr_err("Unable to create mappedlun symlink because"
97 " lun->lun_shutdown=true\n");
100 se_tpg = lun->lun_tpg;
102 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
103 tpg_ci = &nacl_ci->ci_group->cg_item;
104 wwn_ci = &tpg_ci->ci_group->cg_item;
105 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
106 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
108 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
110 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
111 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
112 config_item_name(wwn_ci));
115 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
116 pr_err("Illegal Initiator ACL Symlink outside of %s"
117 " TPGT: %s\n", config_item_name(wwn_ci),
118 config_item_name(tpg_ci));
122 * If this struct se_node_acl was dynamically generated with
123 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
124 * which be will write protected (READ-ONLY) when
125 * tpg_1/attrib/demo_mode_write_protect=1
128 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
130 lun_access = deve->lun_flags;
133 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
134 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
135 TRANSPORT_LUNFLAGS_READ_WRITE;
138 * Determine the actual mapped LUN value user wants..
140 * This value is what the SCSI Initiator actually sees the
141 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
143 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
146 static int target_fabric_mappedlun_unlink(
147 struct config_item *lun_acl_ci,
148 struct config_item *lun_ci)
150 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
151 struct se_lun_acl, se_lun_group);
152 struct se_lun *lun = container_of(to_config_group(lun_ci),
153 struct se_lun, lun_group);
155 return core_dev_del_initiator_node_lun_acl(lun, lacl);
158 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
160 return container_of(to_config_group(item), struct se_lun_acl,
164 static ssize_t target_fabric_mappedlun_write_protect_show(
165 struct config_item *item, char *page)
167 struct se_lun_acl *lacl = item_to_lun_acl(item);
168 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
169 struct se_dev_entry *deve;
173 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
175 len = sprintf(page, "%d\n",
176 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
183 static ssize_t target_fabric_mappedlun_write_protect_store(
184 struct config_item *item, const char *page, size_t count)
186 struct se_lun_acl *lacl = item_to_lun_acl(item);
187 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
188 struct se_portal_group *se_tpg = se_nacl->se_tpg;
192 ret = kstrtoul(page, 0, &op);
196 if ((op != 1) && (op != 0))
199 core_update_device_list_access(lacl->mapped_lun, (op) ?
200 TRANSPORT_LUNFLAGS_READ_ONLY :
201 TRANSPORT_LUNFLAGS_READ_WRITE,
204 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
205 " Mapped LUN: %llu Write Protect bit to %s\n",
206 se_tpg->se_tpg_tfo->get_fabric_name(),
207 se_nacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
213 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
215 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
216 &target_fabric_mappedlun_attr_write_protect,
220 static void target_fabric_mappedlun_release(struct config_item *item)
222 struct se_lun_acl *lacl = container_of(to_config_group(item),
223 struct se_lun_acl, se_lun_group);
224 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
226 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
229 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
230 .release = target_fabric_mappedlun_release,
231 .allow_link = target_fabric_mappedlun_link,
232 .drop_link = target_fabric_mappedlun_unlink,
235 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
236 target_fabric_mappedlun_attrs);
238 /* End of tfc_tpg_mappedlun_cit */
240 /* Start of tfc_tpg_mappedlun_port_cit */
242 static struct config_group *target_core_mappedlun_stat_mkdir(
243 struct config_group *group,
246 return ERR_PTR(-ENOSYS);
249 static void target_core_mappedlun_stat_rmdir(
250 struct config_group *group,
251 struct config_item *item)
256 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
257 .make_group = target_core_mappedlun_stat_mkdir,
258 .drop_item = target_core_mappedlun_stat_rmdir,
261 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
264 /* End of tfc_tpg_mappedlun_port_cit */
266 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
267 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
268 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
270 /* Start of tfc_tpg_nacl_base_cit */
272 static struct config_group *target_fabric_make_mappedlun(
273 struct config_group *group,
276 struct se_node_acl *se_nacl = container_of(group,
277 struct se_node_acl, acl_group);
278 struct se_portal_group *se_tpg = se_nacl->se_tpg;
279 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
280 struct se_lun_acl *lacl = NULL;
281 struct config_item *acl_ci;
282 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
284 unsigned long long mapped_lun;
287 acl_ci = &group->cg_item;
289 pr_err("Unable to locatel acl_ci\n");
293 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
295 pr_err("Unable to allocate memory for name buf\n");
296 return ERR_PTR(-ENOMEM);
298 snprintf(buf, strlen(name) + 1, "%s", name);
300 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
302 if (strstr(buf, "lun_") != buf) {
303 pr_err("Unable to locate \"lun_\" from buf: %s"
304 " name: %s\n", buf, name);
309 * Determine the Mapped LUN value. This is what the SCSI Initiator
310 * Port will actually see.
312 ret = kstrtoull(buf + 4, 0, &mapped_lun);
316 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
323 lacl_cg = &lacl->se_lun_group;
324 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
326 if (!lacl_cg->default_groups) {
327 pr_err("Unable to allocate lacl_cg->default_groups\n");
332 config_group_init_type_name(&lacl->se_lun_group, name,
333 &tf->tf_tpg_mappedlun_cit);
334 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
335 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
336 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
337 lacl_cg->default_groups[1] = NULL;
339 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
340 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
342 if (!ml_stat_grp->default_groups) {
343 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
347 target_stat_setup_mappedlun_default_groups(lacl);
350 return &lacl->se_lun_group;
353 kfree(lacl_cg->default_groups);
359 static void target_fabric_drop_mappedlun(
360 struct config_group *group,
361 struct config_item *item)
363 struct se_lun_acl *lacl = container_of(to_config_group(item),
364 struct se_lun_acl, se_lun_group);
365 struct config_item *df_item;
366 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
369 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
370 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
371 df_item = &ml_stat_grp->default_groups[i]->cg_item;
372 ml_stat_grp->default_groups[i] = NULL;
373 config_item_put(df_item);
375 kfree(ml_stat_grp->default_groups);
377 lacl_cg = &lacl->se_lun_group;
378 for (i = 0; lacl_cg->default_groups[i]; i++) {
379 df_item = &lacl_cg->default_groups[i]->cg_item;
380 lacl_cg->default_groups[i] = NULL;
381 config_item_put(df_item);
383 kfree(lacl_cg->default_groups);
385 config_item_put(item);
388 static void target_fabric_nacl_base_release(struct config_item *item)
390 struct se_node_acl *se_nacl = container_of(to_config_group(item),
391 struct se_node_acl, acl_group);
392 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
394 if (tf->tf_ops->fabric_cleanup_nodeacl)
395 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
396 core_tpg_del_initiator_node_acl(se_nacl);
399 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
400 .release = target_fabric_nacl_base_release,
403 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
404 .make_group = target_fabric_make_mappedlun,
405 .drop_item = target_fabric_drop_mappedlun,
408 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
409 &target_fabric_nacl_base_group_ops);
411 /* End of tfc_tpg_nacl_base_cit */
413 /* Start of tfc_node_fabric_stats_cit */
415 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
416 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
418 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
420 /* End of tfc_wwn_fabric_stats_cit */
422 /* Start of tfc_tpg_nacl_cit */
424 static struct config_group *target_fabric_make_nodeacl(
425 struct config_group *group,
428 struct se_portal_group *se_tpg = container_of(group,
429 struct se_portal_group, tpg_acl_group);
430 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
431 struct se_node_acl *se_nacl;
432 struct config_group *nacl_cg;
434 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
436 return ERR_CAST(se_nacl);
438 if (tf->tf_ops->fabric_init_nodeacl) {
439 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
441 core_tpg_del_initiator_node_acl(se_nacl);
446 nacl_cg = &se_nacl->acl_group;
447 nacl_cg->default_groups = se_nacl->acl_default_groups;
448 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
449 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
450 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
451 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
452 nacl_cg->default_groups[4] = NULL;
454 config_group_init_type_name(&se_nacl->acl_group, name,
455 &tf->tf_tpg_nacl_base_cit);
456 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
457 &tf->tf_tpg_nacl_attrib_cit);
458 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
459 &tf->tf_tpg_nacl_auth_cit);
460 config_group_init_type_name(&se_nacl->acl_param_group, "param",
461 &tf->tf_tpg_nacl_param_cit);
462 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
463 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
465 return &se_nacl->acl_group;
468 static void target_fabric_drop_nodeacl(
469 struct config_group *group,
470 struct config_item *item)
472 struct se_node_acl *se_nacl = container_of(to_config_group(item),
473 struct se_node_acl, acl_group);
474 struct config_item *df_item;
475 struct config_group *nacl_cg;
478 nacl_cg = &se_nacl->acl_group;
479 for (i = 0; nacl_cg->default_groups[i]; i++) {
480 df_item = &nacl_cg->default_groups[i]->cg_item;
481 nacl_cg->default_groups[i] = NULL;
482 config_item_put(df_item);
485 * struct se_node_acl free is done in target_fabric_nacl_base_release()
487 config_item_put(item);
490 static struct configfs_group_operations target_fabric_nacl_group_ops = {
491 .make_group = target_fabric_make_nodeacl,
492 .drop_item = target_fabric_drop_nodeacl,
495 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
497 /* End of tfc_tpg_nacl_cit */
499 /* Start of tfc_tpg_np_base_cit */
501 static void target_fabric_np_base_release(struct config_item *item)
503 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
504 struct se_tpg_np, tpg_np_group);
505 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
506 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
508 tf->tf_ops->fabric_drop_np(se_tpg_np);
511 static struct configfs_item_operations target_fabric_np_base_item_ops = {
512 .release = target_fabric_np_base_release,
515 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
517 /* End of tfc_tpg_np_base_cit */
519 /* Start of tfc_tpg_np_cit */
521 static struct config_group *target_fabric_make_np(
522 struct config_group *group,
525 struct se_portal_group *se_tpg = container_of(group,
526 struct se_portal_group, tpg_np_group);
527 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
528 struct se_tpg_np *se_tpg_np;
530 if (!tf->tf_ops->fabric_make_np) {
531 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
532 return ERR_PTR(-ENOSYS);
535 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
536 if (!se_tpg_np || IS_ERR(se_tpg_np))
537 return ERR_PTR(-EINVAL);
539 se_tpg_np->tpg_np_parent = se_tpg;
540 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
541 &tf->tf_tpg_np_base_cit);
543 return &se_tpg_np->tpg_np_group;
546 static void target_fabric_drop_np(
547 struct config_group *group,
548 struct config_item *item)
551 * struct se_tpg_np is released via target_fabric_np_base_release()
553 config_item_put(item);
556 static struct configfs_group_operations target_fabric_np_group_ops = {
557 .make_group = &target_fabric_make_np,
558 .drop_item = &target_fabric_drop_np,
561 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
563 /* End of tfc_tpg_np_cit */
565 /* Start of tfc_tpg_port_cit */
567 static struct se_lun *item_to_lun(struct config_item *item)
569 return container_of(to_config_group(item), struct se_lun,
573 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
576 struct se_lun *lun = item_to_lun(item);
578 if (!lun || !lun->lun_se_dev)
581 return core_alua_show_tg_pt_gp_info(lun, page);
584 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
585 const char *page, size_t count)
587 struct se_lun *lun = item_to_lun(item);
589 if (!lun || !lun->lun_se_dev)
592 return core_alua_store_tg_pt_gp_info(lun, page, count);
595 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
596 struct config_item *item, char *page)
598 struct se_lun *lun = item_to_lun(item);
600 if (!lun || !lun->lun_se_dev)
603 return core_alua_show_offline_bit(lun, page);
606 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
607 struct config_item *item, const char *page, size_t count)
609 struct se_lun *lun = item_to_lun(item);
611 if (!lun || !lun->lun_se_dev)
614 return core_alua_store_offline_bit(lun, page, count);
617 static ssize_t target_fabric_port_alua_tg_pt_status_show(
618 struct config_item *item, char *page)
620 struct se_lun *lun = item_to_lun(item);
622 if (!lun || !lun->lun_se_dev)
625 return core_alua_show_secondary_status(lun, page);
628 static ssize_t target_fabric_port_alua_tg_pt_status_store(
629 struct config_item *item, const char *page, size_t count)
631 struct se_lun *lun = item_to_lun(item);
633 if (!lun || !lun->lun_se_dev)
636 return core_alua_store_secondary_status(lun, page, count);
639 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
640 struct config_item *item, char *page)
642 struct se_lun *lun = item_to_lun(item);
644 if (!lun || !lun->lun_se_dev)
647 return core_alua_show_secondary_write_metadata(lun, page);
650 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
651 struct config_item *item, const char *page, size_t count)
653 struct se_lun *lun = item_to_lun(item);
655 if (!lun || !lun->lun_se_dev)
658 return core_alua_store_secondary_write_metadata(lun, page, count);
661 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
662 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
663 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
664 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
666 static struct configfs_attribute *target_fabric_port_attrs[] = {
667 &target_fabric_port_attr_alua_tg_pt_gp,
668 &target_fabric_port_attr_alua_tg_pt_offline,
669 &target_fabric_port_attr_alua_tg_pt_status,
670 &target_fabric_port_attr_alua_tg_pt_write_md,
674 static int target_fabric_port_link(
675 struct config_item *lun_ci,
676 struct config_item *se_dev_ci)
678 struct config_item *tpg_ci;
679 struct se_lun *lun = container_of(to_config_group(lun_ci),
680 struct se_lun, lun_group);
681 struct se_portal_group *se_tpg;
682 struct se_device *dev =
683 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
684 struct target_fabric_configfs *tf;
687 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
688 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
689 " %p to struct se_device: %p\n", se_dev_ci, dev);
693 if (!(dev->dev_flags & DF_CONFIGURED)) {
694 pr_err("se_device not configured yet, cannot port link\n");
698 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
699 se_tpg = container_of(to_config_group(tpg_ci),
700 struct se_portal_group, tpg_group);
701 tf = se_tpg->se_tpg_wwn->wwn_tf;
703 if (lun->lun_se_dev != NULL) {
704 pr_err("Port Symlink already exists\n");
708 ret = core_dev_add_lun(se_tpg, dev, lun);
710 pr_err("core_dev_add_lun() failed: %d\n", ret);
714 if (tf->tf_ops->fabric_post_link) {
716 * Call the optional fabric_post_link() to allow a
717 * fabric module to setup any additional state once
718 * core_dev_add_lun() has been called..
720 tf->tf_ops->fabric_post_link(se_tpg, lun);
728 static int target_fabric_port_unlink(
729 struct config_item *lun_ci,
730 struct config_item *se_dev_ci)
732 struct se_lun *lun = container_of(to_config_group(lun_ci),
733 struct se_lun, lun_group);
734 struct se_portal_group *se_tpg = lun->lun_tpg;
735 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
737 if (tf->tf_ops->fabric_pre_unlink) {
739 * Call the optional fabric_pre_unlink() to allow a
740 * fabric module to release any additional stat before
741 * core_dev_del_lun() is called.
743 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
746 core_dev_del_lun(se_tpg, lun);
750 static void target_fabric_port_release(struct config_item *item)
752 struct se_lun *lun = container_of(to_config_group(item),
753 struct se_lun, lun_group);
755 kfree_rcu(lun, rcu_head);
758 static struct configfs_item_operations target_fabric_port_item_ops = {
759 .release = target_fabric_port_release,
760 .allow_link = target_fabric_port_link,
761 .drop_link = target_fabric_port_unlink,
764 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
766 /* End of tfc_tpg_port_cit */
768 /* Start of tfc_tpg_port_stat_cit */
770 static struct config_group *target_core_port_stat_mkdir(
771 struct config_group *group,
774 return ERR_PTR(-ENOSYS);
777 static void target_core_port_stat_rmdir(
778 struct config_group *group,
779 struct config_item *item)
784 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
785 .make_group = target_core_port_stat_mkdir,
786 .drop_item = target_core_port_stat_rmdir,
789 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
791 /* End of tfc_tpg_port_stat_cit */
793 /* Start of tfc_tpg_lun_cit */
795 static struct config_group *target_fabric_make_lun(
796 struct config_group *group,
800 struct se_portal_group *se_tpg = container_of(group,
801 struct se_portal_group, tpg_lun_group);
802 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
803 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
804 unsigned long long unpacked_lun;
807 if (strstr(name, "lun_") != name) {
808 pr_err("Unable to locate \'_\" in"
809 " \"lun_$LUN_NUMBER\"\n");
810 return ERR_PTR(-EINVAL);
812 errno = kstrtoull(name + 4, 0, &unpacked_lun);
814 return ERR_PTR(errno);
816 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
818 return ERR_CAST(lun);
820 lun_cg = &lun->lun_group;
821 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
823 if (!lun_cg->default_groups) {
824 pr_err("Unable to allocate lun_cg->default_groups\n");
826 return ERR_PTR(-ENOMEM);
829 config_group_init_type_name(&lun->lun_group, name,
830 &tf->tf_tpg_port_cit);
831 config_group_init_type_name(&lun->port_stat_grps.stat_group,
832 "statistics", &tf->tf_tpg_port_stat_cit);
833 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
834 lun_cg->default_groups[1] = NULL;
836 port_stat_grp = &lun->port_stat_grps.stat_group;
837 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
839 if (!port_stat_grp->default_groups) {
840 pr_err("Unable to allocate port_stat_grp->default_groups\n");
841 kfree(lun_cg->default_groups);
843 return ERR_PTR(-ENOMEM);
845 target_stat_setup_port_default_groups(lun);
847 return &lun->lun_group;
850 static void target_fabric_drop_lun(
851 struct config_group *group,
852 struct config_item *item)
854 struct se_lun *lun = container_of(to_config_group(item),
855 struct se_lun, lun_group);
856 struct config_item *df_item;
857 struct config_group *lun_cg, *port_stat_grp;
860 port_stat_grp = &lun->port_stat_grps.stat_group;
861 for (i = 0; port_stat_grp->default_groups[i]; i++) {
862 df_item = &port_stat_grp->default_groups[i]->cg_item;
863 port_stat_grp->default_groups[i] = NULL;
864 config_item_put(df_item);
866 kfree(port_stat_grp->default_groups);
868 lun_cg = &lun->lun_group;
869 for (i = 0; lun_cg->default_groups[i]; i++) {
870 df_item = &lun_cg->default_groups[i]->cg_item;
871 lun_cg->default_groups[i] = NULL;
872 config_item_put(df_item);
874 kfree(lun_cg->default_groups);
876 config_item_put(item);
879 static struct configfs_group_operations target_fabric_lun_group_ops = {
880 .make_group = &target_fabric_make_lun,
881 .drop_item = &target_fabric_drop_lun,
884 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
886 /* End of tfc_tpg_lun_cit */
888 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
889 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
890 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
892 /* Start of tfc_tpg_base_cit */
894 static void target_fabric_tpg_release(struct config_item *item)
896 struct se_portal_group *se_tpg = container_of(to_config_group(item),
897 struct se_portal_group, tpg_group);
898 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
899 struct target_fabric_configfs *tf = wwn->wwn_tf;
901 tf->tf_ops->fabric_drop_tpg(se_tpg);
904 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
905 .release = target_fabric_tpg_release,
908 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
910 /* End of tfc_tpg_base_cit */
912 /* Start of tfc_tpg_cit */
914 static struct config_group *target_fabric_make_tpg(
915 struct config_group *group,
918 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
919 struct target_fabric_configfs *tf = wwn->wwn_tf;
920 struct se_portal_group *se_tpg;
922 if (!tf->tf_ops->fabric_make_tpg) {
923 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
924 return ERR_PTR(-ENOSYS);
927 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
928 if (!se_tpg || IS_ERR(se_tpg))
929 return ERR_PTR(-EINVAL);
931 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
933 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
934 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
935 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
936 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
937 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
938 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
939 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
940 se_tpg->tpg_group.default_groups[6] = NULL;
942 config_group_init_type_name(&se_tpg->tpg_group, name,
943 &tf->tf_tpg_base_cit);
944 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
945 &tf->tf_tpg_lun_cit);
946 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
948 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
949 &tf->tf_tpg_nacl_cit);
950 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
951 &tf->tf_tpg_attrib_cit);
952 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
953 &tf->tf_tpg_auth_cit);
954 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
955 &tf->tf_tpg_param_cit);
957 return &se_tpg->tpg_group;
960 static void target_fabric_drop_tpg(
961 struct config_group *group,
962 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 config_group *tpg_cg = &se_tpg->tpg_group;
967 struct config_item *df_item;
970 * Release default groups, but do not release tpg_cg->default_groups
971 * memory as it is statically allocated at se_tpg->tpg_default_groups.
973 for (i = 0; tpg_cg->default_groups[i]; i++) {
974 df_item = &tpg_cg->default_groups[i]->cg_item;
975 tpg_cg->default_groups[i] = NULL;
976 config_item_put(df_item);
979 config_item_put(item);
982 static void target_fabric_release_wwn(struct config_item *item)
984 struct se_wwn *wwn = container_of(to_config_group(item),
985 struct se_wwn, wwn_group);
986 struct target_fabric_configfs *tf = wwn->wwn_tf;
988 tf->tf_ops->fabric_drop_wwn(wwn);
991 static struct configfs_item_operations target_fabric_tpg_item_ops = {
992 .release = target_fabric_release_wwn,
995 static struct configfs_group_operations target_fabric_tpg_group_ops = {
996 .make_group = target_fabric_make_tpg,
997 .drop_item = target_fabric_drop_tpg,
1000 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1003 /* End of tfc_tpg_cit */
1005 /* Start of tfc_wwn_fabric_stats_cit */
1007 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1008 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1010 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1012 /* End of tfc_wwn_fabric_stats_cit */
1014 /* Start of tfc_wwn_cit */
1016 static struct config_group *target_fabric_make_wwn(
1017 struct config_group *group,
1020 struct target_fabric_configfs *tf = container_of(group,
1021 struct target_fabric_configfs, tf_group);
1024 if (!tf->tf_ops->fabric_make_wwn) {
1025 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1026 return ERR_PTR(-ENOSYS);
1029 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1030 if (!wwn || IS_ERR(wwn))
1031 return ERR_PTR(-EINVAL);
1035 * Setup default groups from pre-allocated wwn->wwn_default_groups
1037 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1038 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1039 wwn->wwn_group.default_groups[1] = NULL;
1041 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1042 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1043 &tf->tf_wwn_fabric_stats_cit);
1045 return &wwn->wwn_group;
1048 static void target_fabric_drop_wwn(
1049 struct config_group *group,
1050 struct config_item *item)
1052 struct se_wwn *wwn = container_of(to_config_group(item),
1053 struct se_wwn, wwn_group);
1054 struct config_item *df_item;
1055 struct config_group *cg = &wwn->wwn_group;
1058 for (i = 0; cg->default_groups[i]; i++) {
1059 df_item = &cg->default_groups[i]->cg_item;
1060 cg->default_groups[i] = NULL;
1061 config_item_put(df_item);
1064 config_item_put(item);
1067 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1068 .make_group = target_fabric_make_wwn,
1069 .drop_item = target_fabric_drop_wwn,
1072 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
1073 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
1075 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1077 target_fabric_setup_discovery_cit(tf);
1078 target_fabric_setup_wwn_cit(tf);
1079 target_fabric_setup_wwn_fabric_stats_cit(tf);
1080 target_fabric_setup_tpg_cit(tf);
1081 target_fabric_setup_tpg_base_cit(tf);
1082 target_fabric_setup_tpg_port_cit(tf);
1083 target_fabric_setup_tpg_port_stat_cit(tf);
1084 target_fabric_setup_tpg_lun_cit(tf);
1085 target_fabric_setup_tpg_np_cit(tf);
1086 target_fabric_setup_tpg_np_base_cit(tf);
1087 target_fabric_setup_tpg_attrib_cit(tf);
1088 target_fabric_setup_tpg_auth_cit(tf);
1089 target_fabric_setup_tpg_param_cit(tf);
1090 target_fabric_setup_tpg_nacl_cit(tf);
1091 target_fabric_setup_tpg_nacl_base_cit(tf);
1092 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1093 target_fabric_setup_tpg_nacl_auth_cit(tf);
1094 target_fabric_setup_tpg_nacl_param_cit(tf);
1095 target_fabric_setup_tpg_nacl_stat_cit(tf);
1096 target_fabric_setup_tpg_mappedlun_cit(tf);
1097 target_fabric_setup_tpg_mappedlun_stat_cit(tf);