1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_fabric_configfs.c
5 * This file contains generic fabric module configfs infrastructure for
8 * (c) Copyright 2010-2013 Datera, Inc.
12 ****************************************************************************/
14 #include <linux/kstrtox.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/utsname.h>
18 #include <linux/init.h>
20 #include <linux/namei.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 #include <linux/delay.h>
24 #include <linux/unistd.h>
25 #include <linux/string.h>
26 #include <linux/syscalls.h>
27 #include <linux/configfs.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_backend.h>
31 #include <target/target_core_fabric.h>
33 #include "target_core_internal.h"
34 #include "target_core_alua.h"
35 #include "target_core_pr.h"
37 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
38 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
40 struct config_item_type *cit = &tf->tf_##_name##_cit; \
42 cit->ct_item_ops = _item_ops; \
43 cit->ct_group_ops = _group_ops; \
44 cit->ct_attrs = _attrs; \
45 cit->ct_owner = tf->tf_ops->module; \
46 pr_debug("Setup generic %s\n", __stringify(_name)); \
49 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
50 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
52 struct config_item_type *cit = &tf->tf_##_name##_cit; \
53 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = attrs; \
58 cit->ct_owner = tf->tf_ops->module; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
62 static struct configfs_item_operations target_fabric_port_item_ops;
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;
72 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
73 struct se_lun_acl, se_lun_group);
74 struct se_portal_group *se_tpg;
75 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
78 if (!lun_ci->ci_type ||
79 lun_ci->ci_type->ct_item_ops != &target_fabric_port_item_ops) {
80 pr_err("Bad lun_ci, not a valid lun_ci pointer: %p\n", lun_ci);
83 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
86 * Ensure that the source port exists
88 if (!lun->lun_se_dev) {
89 pr_err("Source se_lun->lun_se_dev does not exist\n");
92 if (lun->lun_shutdown) {
93 pr_err("Unable to create mappedlun symlink because"
94 " lun->lun_shutdown=true\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
121 * deve->lun_access_ro value, which will be true 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_ro = deve->lun_access_ro;
130 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
131 se_tpg)) ? true : false;
134 * Determine the actual mapped LUN value user wants..
136 * This value is what the SCSI Initiator actually sees the
137 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
139 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
142 static void target_fabric_mappedlun_unlink(
143 struct config_item *lun_acl_ci,
144 struct config_item *lun_ci)
146 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
147 struct se_lun_acl, se_lun_group);
148 struct se_lun *lun = container_of(to_config_group(lun_ci),
149 struct se_lun, lun_group);
151 core_dev_del_initiator_node_lun_acl(lun, lacl);
154 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
156 return container_of(to_config_group(item), struct se_lun_acl,
160 static ssize_t target_fabric_mappedlun_write_protect_show(
161 struct config_item *item, char *page)
163 struct se_lun_acl *lacl = item_to_lun_acl(item);
164 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
165 struct se_dev_entry *deve;
169 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
171 len = sprintf(page, "%d\n", deve->lun_access_ro);
178 static ssize_t target_fabric_mappedlun_write_protect_store(
179 struct config_item *item, const char *page, size_t count)
181 struct se_lun_acl *lacl = item_to_lun_acl(item);
182 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
183 struct se_portal_group *se_tpg = se_nacl->se_tpg;
187 ret = kstrtoul(page, 0, &wp);
191 if ((wp != 1) && (wp != 0))
194 /* wp=1 means lun_access_ro=true */
195 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
197 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
198 " Mapped LUN: %llu Write Protect bit to %s\n",
199 se_tpg->se_tpg_tfo->fabric_name,
200 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
206 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
208 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
209 &target_fabric_mappedlun_attr_write_protect,
213 static void target_fabric_mappedlun_release(struct config_item *item)
215 struct se_lun_acl *lacl = container_of(to_config_group(item),
216 struct se_lun_acl, se_lun_group);
217 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
219 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
222 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
223 .release = target_fabric_mappedlun_release,
224 .allow_link = target_fabric_mappedlun_link,
225 .drop_link = target_fabric_mappedlun_unlink,
228 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
229 target_fabric_mappedlun_attrs);
231 /* End of tfc_tpg_mappedlun_cit */
233 /* Start of tfc_tpg_mappedlun_port_cit */
235 static struct config_group *target_core_mappedlun_stat_mkdir(
236 struct config_group *group,
239 return ERR_PTR(-ENOSYS);
242 static void target_core_mappedlun_stat_rmdir(
243 struct config_group *group,
244 struct config_item *item)
249 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
250 .make_group = target_core_mappedlun_stat_mkdir,
251 .drop_item = target_core_mappedlun_stat_rmdir,
254 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
257 /* End of tfc_tpg_mappedlun_port_cit */
259 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
260 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
261 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
263 /* Start of tfc_tpg_nacl_base_cit */
265 static struct config_group *target_fabric_make_mappedlun(
266 struct config_group *group,
269 struct se_node_acl *se_nacl = container_of(group,
270 struct se_node_acl, acl_group);
271 struct se_portal_group *se_tpg = se_nacl->se_tpg;
272 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
273 struct se_lun_acl *lacl = NULL;
275 unsigned long long mapped_lun;
278 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
280 pr_err("Unable to allocate memory for name buf\n");
281 return ERR_PTR(-ENOMEM);
283 snprintf(buf, strlen(name) + 1, "%s", name);
285 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
287 if (strstr(buf, "lun_") != buf) {
288 pr_err("Unable to locate \"lun_\" from buf: %s"
289 " name: %s\n", buf, name);
294 * Determine the Mapped LUN value. This is what the SCSI Initiator
295 * Port will actually see.
297 ret = kstrtoull(buf + 4, 0, &mapped_lun);
301 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
308 config_group_init_type_name(&lacl->se_lun_group, name,
309 &tf->tf_tpg_mappedlun_cit);
311 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
312 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
313 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
314 &lacl->se_lun_group);
316 target_stat_setup_mappedlun_default_groups(lacl);
319 return &lacl->se_lun_group;
326 static void target_fabric_drop_mappedlun(
327 struct config_group *group,
328 struct config_item *item)
330 struct se_lun_acl *lacl = container_of(to_config_group(item),
331 struct se_lun_acl, se_lun_group);
333 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
334 configfs_remove_default_groups(&lacl->se_lun_group);
336 config_item_put(item);
339 static void target_fabric_nacl_base_release(struct config_item *item)
341 struct se_node_acl *se_nacl = container_of(to_config_group(item),
342 struct se_node_acl, acl_group);
344 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
345 core_tpg_del_initiator_node_acl(se_nacl);
348 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
349 .release = target_fabric_nacl_base_release,
352 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
353 .make_group = target_fabric_make_mappedlun,
354 .drop_item = target_fabric_drop_mappedlun,
357 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
358 &target_fabric_nacl_base_group_ops);
360 /* End of tfc_tpg_nacl_base_cit */
362 /* Start of tfc_node_fabric_stats_cit */
364 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
365 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
367 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
369 /* End of tfc_wwn_fabric_stats_cit */
371 /* Start of tfc_tpg_nacl_cit */
373 static struct config_group *target_fabric_make_nodeacl(
374 struct config_group *group,
377 struct se_portal_group *se_tpg = container_of(group,
378 struct se_portal_group, tpg_acl_group);
379 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
380 struct se_node_acl *se_nacl;
382 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
384 return ERR_CAST(se_nacl);
386 config_group_init_type_name(&se_nacl->acl_group, name,
387 &tf->tf_tpg_nacl_base_cit);
389 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
390 &tf->tf_tpg_nacl_attrib_cit);
391 configfs_add_default_group(&se_nacl->acl_attrib_group,
392 &se_nacl->acl_group);
394 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
395 &tf->tf_tpg_nacl_auth_cit);
396 configfs_add_default_group(&se_nacl->acl_auth_group,
397 &se_nacl->acl_group);
399 config_group_init_type_name(&se_nacl->acl_param_group, "param",
400 &tf->tf_tpg_nacl_param_cit);
401 configfs_add_default_group(&se_nacl->acl_param_group,
402 &se_nacl->acl_group);
404 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
405 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
406 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
407 &se_nacl->acl_group);
409 if (tf->tf_ops->fabric_init_nodeacl) {
410 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
412 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
413 core_tpg_del_initiator_node_acl(se_nacl);
418 return &se_nacl->acl_group;
421 static void target_fabric_drop_nodeacl(
422 struct config_group *group,
423 struct config_item *item)
425 struct se_node_acl *se_nacl = container_of(to_config_group(item),
426 struct se_node_acl, acl_group);
428 configfs_remove_default_groups(&se_nacl->acl_group);
431 * struct se_node_acl free is done in target_fabric_nacl_base_release()
433 config_item_put(item);
436 static struct configfs_group_operations target_fabric_nacl_group_ops = {
437 .make_group = target_fabric_make_nodeacl,
438 .drop_item = target_fabric_drop_nodeacl,
441 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
443 /* End of tfc_tpg_nacl_cit */
445 /* Start of tfc_tpg_np_base_cit */
447 static void target_fabric_np_base_release(struct config_item *item)
449 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
450 struct se_tpg_np, tpg_np_group);
451 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
452 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
454 tf->tf_ops->fabric_drop_np(se_tpg_np);
457 static struct configfs_item_operations target_fabric_np_base_item_ops = {
458 .release = target_fabric_np_base_release,
461 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
463 /* End of tfc_tpg_np_base_cit */
465 /* Start of tfc_tpg_np_cit */
467 static struct config_group *target_fabric_make_np(
468 struct config_group *group,
471 struct se_portal_group *se_tpg = container_of(group,
472 struct se_portal_group, tpg_np_group);
473 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
474 struct se_tpg_np *se_tpg_np;
476 if (!tf->tf_ops->fabric_make_np) {
477 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
478 return ERR_PTR(-ENOSYS);
481 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
482 if (!se_tpg_np || IS_ERR(se_tpg_np))
483 return ERR_PTR(-EINVAL);
485 se_tpg_np->tpg_np_parent = se_tpg;
486 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
487 &tf->tf_tpg_np_base_cit);
489 return &se_tpg_np->tpg_np_group;
492 static void target_fabric_drop_np(
493 struct config_group *group,
494 struct config_item *item)
497 * struct se_tpg_np is released via target_fabric_np_base_release()
499 config_item_put(item);
502 static struct configfs_group_operations target_fabric_np_group_ops = {
503 .make_group = &target_fabric_make_np,
504 .drop_item = &target_fabric_drop_np,
507 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
509 /* End of tfc_tpg_np_cit */
511 /* Start of tfc_tpg_port_cit */
513 static struct se_lun *item_to_lun(struct config_item *item)
515 return container_of(to_config_group(item), struct se_lun,
519 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
522 struct se_lun *lun = item_to_lun(item);
524 if (!lun->lun_se_dev)
527 return core_alua_show_tg_pt_gp_info(lun, page);
530 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
531 const char *page, size_t count)
533 struct se_lun *lun = item_to_lun(item);
535 if (!lun->lun_se_dev)
538 return core_alua_store_tg_pt_gp_info(lun, page, count);
541 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
542 struct config_item *item, char *page)
544 struct se_lun *lun = item_to_lun(item);
546 if (!lun->lun_se_dev)
549 return core_alua_show_offline_bit(lun, page);
552 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
553 struct config_item *item, const char *page, size_t count)
555 struct se_lun *lun = item_to_lun(item);
557 if (!lun->lun_se_dev)
560 return core_alua_store_offline_bit(lun, page, count);
563 static ssize_t target_fabric_port_alua_tg_pt_status_show(
564 struct config_item *item, char *page)
566 struct se_lun *lun = item_to_lun(item);
568 if (!lun->lun_se_dev)
571 return core_alua_show_secondary_status(lun, page);
574 static ssize_t target_fabric_port_alua_tg_pt_status_store(
575 struct config_item *item, const char *page, size_t count)
577 struct se_lun *lun = item_to_lun(item);
579 if (!lun->lun_se_dev)
582 return core_alua_store_secondary_status(lun, page, count);
585 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
586 struct config_item *item, char *page)
588 struct se_lun *lun = item_to_lun(item);
590 if (!lun->lun_se_dev)
593 return core_alua_show_secondary_write_metadata(lun, page);
596 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
597 struct config_item *item, const char *page, size_t count)
599 struct se_lun *lun = item_to_lun(item);
601 if (!lun->lun_se_dev)
604 return core_alua_store_secondary_write_metadata(lun, page, count);
607 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
608 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
609 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
610 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
612 static struct configfs_attribute *target_fabric_port_attrs[] = {
613 &target_fabric_port_attr_alua_tg_pt_gp,
614 &target_fabric_port_attr_alua_tg_pt_offline,
615 &target_fabric_port_attr_alua_tg_pt_status,
616 &target_fabric_port_attr_alua_tg_pt_write_md,
620 static int target_fabric_port_link(
621 struct config_item *lun_ci,
622 struct config_item *se_dev_ci)
624 struct config_item *tpg_ci;
625 struct se_lun *lun = container_of(to_config_group(lun_ci),
626 struct se_lun, lun_group);
627 struct se_portal_group *se_tpg;
628 struct se_device *dev;
629 struct target_fabric_configfs *tf;
632 if (!se_dev_ci->ci_type ||
633 se_dev_ci->ci_type->ct_item_ops != &target_core_dev_item_ops) {
634 pr_err("Bad se_dev_ci, not a valid se_dev_ci pointer: %p\n", se_dev_ci);
637 dev = container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
639 if (!target_dev_configured(dev)) {
640 pr_err("se_device not configured yet, cannot port link\n");
644 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
645 se_tpg = container_of(to_config_group(tpg_ci),
646 struct se_portal_group, tpg_group);
647 tf = se_tpg->se_tpg_wwn->wwn_tf;
649 if (lun->lun_se_dev != NULL) {
650 pr_err("Port Symlink already exists\n");
654 ret = core_dev_add_lun(se_tpg, dev, lun);
656 pr_err("core_dev_add_lun() failed: %d\n", ret);
660 if (tf->tf_ops->fabric_post_link) {
662 * Call the optional fabric_post_link() to allow a
663 * fabric module to setup any additional state once
664 * core_dev_add_lun() has been called..
666 tf->tf_ops->fabric_post_link(se_tpg, lun);
674 static void target_fabric_port_unlink(
675 struct config_item *lun_ci,
676 struct config_item *se_dev_ci)
678 struct se_lun *lun = container_of(to_config_group(lun_ci),
679 struct se_lun, lun_group);
680 struct se_portal_group *se_tpg = lun->lun_tpg;
681 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
683 if (tf->tf_ops->fabric_pre_unlink) {
685 * Call the optional fabric_pre_unlink() to allow a
686 * fabric module to release any additional stat before
687 * core_dev_del_lun() is called.
689 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
692 core_dev_del_lun(se_tpg, lun);
695 static void target_fabric_port_release(struct config_item *item)
697 struct se_lun *lun = container_of(to_config_group(item),
698 struct se_lun, lun_group);
700 kfree_rcu(lun, rcu_head);
703 static struct configfs_item_operations target_fabric_port_item_ops = {
704 .release = target_fabric_port_release,
705 .allow_link = target_fabric_port_link,
706 .drop_link = target_fabric_port_unlink,
709 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
711 /* End of tfc_tpg_port_cit */
713 /* Start of tfc_tpg_port_stat_cit */
715 static struct config_group *target_core_port_stat_mkdir(
716 struct config_group *group,
719 return ERR_PTR(-ENOSYS);
722 static void target_core_port_stat_rmdir(
723 struct config_group *group,
724 struct config_item *item)
729 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
730 .make_group = target_core_port_stat_mkdir,
731 .drop_item = target_core_port_stat_rmdir,
734 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
736 /* End of tfc_tpg_port_stat_cit */
738 /* Start of tfc_tpg_lun_cit */
740 static struct config_group *target_fabric_make_lun(
741 struct config_group *group,
745 struct se_portal_group *se_tpg = container_of(group,
746 struct se_portal_group, tpg_lun_group);
747 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
748 unsigned long long unpacked_lun;
751 if (strstr(name, "lun_") != name) {
752 pr_err("Unable to locate \'_\" in"
753 " \"lun_$LUN_NUMBER\"\n");
754 return ERR_PTR(-EINVAL);
756 errno = kstrtoull(name + 4, 0, &unpacked_lun);
758 return ERR_PTR(errno);
760 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
762 return ERR_CAST(lun);
764 config_group_init_type_name(&lun->lun_group, name,
765 &tf->tf_tpg_port_cit);
767 config_group_init_type_name(&lun->port_stat_grps.stat_group,
768 "statistics", &tf->tf_tpg_port_stat_cit);
769 configfs_add_default_group(&lun->port_stat_grps.stat_group,
772 target_stat_setup_port_default_groups(lun);
774 return &lun->lun_group;
777 static void target_fabric_drop_lun(
778 struct config_group *group,
779 struct config_item *item)
781 struct se_lun *lun = container_of(to_config_group(item),
782 struct se_lun, lun_group);
784 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
785 configfs_remove_default_groups(&lun->lun_group);
787 config_item_put(item);
790 static struct configfs_group_operations target_fabric_lun_group_ops = {
791 .make_group = &target_fabric_make_lun,
792 .drop_item = &target_fabric_drop_lun,
795 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
797 /* End of tfc_tpg_lun_cit */
799 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
800 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
801 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
803 /* Start of tfc_tpg_base_cit */
805 static void target_fabric_tpg_release(struct config_item *item)
807 struct se_portal_group *se_tpg = container_of(to_config_group(item),
808 struct se_portal_group, tpg_group);
809 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
810 struct target_fabric_configfs *tf = wwn->wwn_tf;
812 tf->tf_ops->fabric_drop_tpg(se_tpg);
815 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
816 .release = target_fabric_tpg_release,
819 static ssize_t target_fabric_tpg_base_enable_show(struct config_item *item,
822 return sysfs_emit(page, "%d\n", to_tpg(item)->enabled);
825 static ssize_t target_fabric_tpg_base_enable_store(struct config_item *item,
829 struct se_portal_group *se_tpg = to_tpg(item);
833 ret = kstrtobool(page, &op);
837 if (se_tpg->enabled == op)
840 ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, op);
844 se_tpg->enabled = op;
849 CONFIGFS_ATTR(target_fabric_tpg_base_, enable);
852 target_fabric_setup_tpg_base_cit(struct target_fabric_configfs *tf)
854 struct config_item_type *cit = &tf->tf_tpg_base_cit;
855 struct configfs_attribute **attrs = NULL;
859 if (tf->tf_ops->tfc_tpg_base_attrs)
860 while (tf->tf_ops->tfc_tpg_base_attrs[nr_attrs] != NULL)
863 if (tf->tf_ops->fabric_enable_tpg)
869 /* + 1 for final NULL in the array */
870 attrs = kcalloc(nr_attrs + 1, sizeof(*attrs), GFP_KERNEL);
874 if (tf->tf_ops->tfc_tpg_base_attrs)
875 for (; tf->tf_ops->tfc_tpg_base_attrs[i] != NULL; i++)
876 attrs[i] = tf->tf_ops->tfc_tpg_base_attrs[i];
878 if (tf->tf_ops->fabric_enable_tpg)
879 attrs[i] = &target_fabric_tpg_base_attr_enable;
882 cit->ct_item_ops = &target_fabric_tpg_base_item_ops;
883 cit->ct_attrs = attrs;
884 cit->ct_owner = tf->tf_ops->module;
885 pr_debug("Setup generic tpg_base\n");
889 /* End of tfc_tpg_base_cit */
891 /* Start of tfc_tpg_cit */
893 static struct config_group *target_fabric_make_tpg(
894 struct config_group *group,
897 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
898 struct target_fabric_configfs *tf = wwn->wwn_tf;
899 struct se_portal_group *se_tpg;
901 if (!tf->tf_ops->fabric_make_tpg) {
902 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
903 return ERR_PTR(-ENOSYS);
906 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, name);
907 if (!se_tpg || IS_ERR(se_tpg))
908 return ERR_PTR(-EINVAL);
910 config_group_init_type_name(&se_tpg->tpg_group, name,
911 &tf->tf_tpg_base_cit);
913 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
914 &tf->tf_tpg_lun_cit);
915 configfs_add_default_group(&se_tpg->tpg_lun_group,
918 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
920 configfs_add_default_group(&se_tpg->tpg_np_group,
923 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
924 &tf->tf_tpg_nacl_cit);
925 configfs_add_default_group(&se_tpg->tpg_acl_group,
928 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
929 &tf->tf_tpg_attrib_cit);
930 configfs_add_default_group(&se_tpg->tpg_attrib_group,
933 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
934 &tf->tf_tpg_auth_cit);
935 configfs_add_default_group(&se_tpg->tpg_auth_group,
938 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
939 &tf->tf_tpg_param_cit);
940 configfs_add_default_group(&se_tpg->tpg_param_group,
943 return &se_tpg->tpg_group;
946 static void target_fabric_drop_tpg(
947 struct config_group *group,
948 struct config_item *item)
950 struct se_portal_group *se_tpg = container_of(to_config_group(item),
951 struct se_portal_group, tpg_group);
953 configfs_remove_default_groups(&se_tpg->tpg_group);
954 config_item_put(item);
957 static void target_fabric_release_wwn(struct config_item *item)
959 struct se_wwn *wwn = container_of(to_config_group(item),
960 struct se_wwn, wwn_group);
961 struct target_fabric_configfs *tf = wwn->wwn_tf;
963 configfs_remove_default_groups(&wwn->fabric_stat_group);
964 configfs_remove_default_groups(&wwn->param_group);
965 tf->tf_ops->fabric_drop_wwn(wwn);
968 static struct configfs_item_operations target_fabric_tpg_item_ops = {
969 .release = target_fabric_release_wwn,
972 static struct configfs_group_operations target_fabric_tpg_group_ops = {
973 .make_group = target_fabric_make_tpg,
974 .drop_item = target_fabric_drop_tpg,
977 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
980 /* End of tfc_tpg_cit */
982 /* Start of tfc_wwn_fabric_stats_cit */
984 * This is used as a placeholder for struct se_wwn->fabric_stat_group
985 * to allow fabrics access to ->fabric_stat_group->default_groups[]
987 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
989 /* End of tfc_wwn_fabric_stats_cit */
992 target_fabric_wwn_cmd_completion_affinity_show(struct config_item *item,
995 struct se_wwn *wwn = container_of(to_config_group(item), struct se_wwn,
997 return sprintf(page, "%d\n",
998 wwn->cmd_compl_affinity == WORK_CPU_UNBOUND ?
999 SE_COMPL_AFFINITY_CURR_CPU : wwn->cmd_compl_affinity);
1003 target_fabric_wwn_cmd_completion_affinity_store(struct config_item *item,
1004 const char *page, size_t count)
1006 struct se_wwn *wwn = container_of(to_config_group(item), struct se_wwn,
1010 if (kstrtoint(page, 0, &compl_val))
1013 switch (compl_val) {
1014 case SE_COMPL_AFFINITY_CPUID:
1015 wwn->cmd_compl_affinity = compl_val;
1017 case SE_COMPL_AFFINITY_CURR_CPU:
1018 wwn->cmd_compl_affinity = WORK_CPU_UNBOUND;
1021 if (compl_val < 0 || compl_val >= nr_cpu_ids ||
1022 !cpu_online(compl_val)) {
1023 pr_err("Command completion value must be between %d and %d or an online CPU.\n",
1024 SE_COMPL_AFFINITY_CPUID,
1025 SE_COMPL_AFFINITY_CURR_CPU);
1028 wwn->cmd_compl_affinity = compl_val;
1033 CONFIGFS_ATTR(target_fabric_wwn_, cmd_completion_affinity);
1035 static struct configfs_attribute *target_fabric_wwn_param_attrs[] = {
1036 &target_fabric_wwn_attr_cmd_completion_affinity,
1040 TF_CIT_SETUP(wwn_param, NULL, NULL, target_fabric_wwn_param_attrs);
1042 /* Start of tfc_wwn_cit */
1044 static struct config_group *target_fabric_make_wwn(
1045 struct config_group *group,
1048 struct target_fabric_configfs *tf = container_of(group,
1049 struct target_fabric_configfs, tf_group);
1052 if (!tf->tf_ops->fabric_make_wwn) {
1053 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1054 return ERR_PTR(-ENOSYS);
1057 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1058 if (!wwn || IS_ERR(wwn))
1059 return ERR_PTR(-EINVAL);
1061 wwn->cmd_compl_affinity = SE_COMPL_AFFINITY_CPUID;
1064 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1066 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1067 &tf->tf_wwn_fabric_stats_cit);
1068 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
1070 config_group_init_type_name(&wwn->param_group, "param",
1071 &tf->tf_wwn_param_cit);
1072 configfs_add_default_group(&wwn->param_group, &wwn->wwn_group);
1074 if (tf->tf_ops->add_wwn_groups)
1075 tf->tf_ops->add_wwn_groups(wwn);
1076 return &wwn->wwn_group;
1079 static void target_fabric_drop_wwn(
1080 struct config_group *group,
1081 struct config_item *item)
1083 struct se_wwn *wwn = container_of(to_config_group(item),
1084 struct se_wwn, wwn_group);
1086 configfs_remove_default_groups(&wwn->wwn_group);
1087 config_item_put(item);
1090 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1091 .make_group = target_fabric_make_wwn,
1092 .drop_item = target_fabric_drop_wwn,
1095 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
1096 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
1098 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1102 target_fabric_setup_discovery_cit(tf);
1103 target_fabric_setup_wwn_cit(tf);
1104 target_fabric_setup_wwn_fabric_stats_cit(tf);
1105 target_fabric_setup_wwn_param_cit(tf);
1106 target_fabric_setup_tpg_cit(tf);
1108 ret = target_fabric_setup_tpg_base_cit(tf);
1112 target_fabric_setup_tpg_port_cit(tf);
1113 target_fabric_setup_tpg_port_stat_cit(tf);
1114 target_fabric_setup_tpg_lun_cit(tf);
1115 target_fabric_setup_tpg_np_cit(tf);
1116 target_fabric_setup_tpg_np_base_cit(tf);
1117 target_fabric_setup_tpg_attrib_cit(tf);
1118 target_fabric_setup_tpg_auth_cit(tf);
1119 target_fabric_setup_tpg_param_cit(tf);
1120 target_fabric_setup_tpg_nacl_cit(tf);
1121 target_fabric_setup_tpg_nacl_base_cit(tf);
1122 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1123 target_fabric_setup_tpg_nacl_auth_cit(tf);
1124 target_fabric_setup_tpg_nacl_param_cit(tf);
1125 target_fabric_setup_tpg_nacl_stat_cit(tf);
1126 target_fabric_setup_tpg_mappedlun_cit(tf);
1127 target_fabric_setup_tpg_mappedlun_stat_cit(tf);