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 se_tpg = lun->lun_tpg;
97 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
98 tpg_ci = &nacl_ci->ci_group->cg_item;
99 wwn_ci = &tpg_ci->ci_group->cg_item;
100 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
101 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
103 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
105 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
106 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
107 config_item_name(wwn_ci));
110 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
111 pr_err("Illegal Initiator ACL Symlink outside of %s"
112 " TPGT: %s\n", config_item_name(wwn_ci),
113 config_item_name(tpg_ci));
117 * If this struct se_node_acl was dynamically generated with
118 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
119 * which be will write protected (READ-ONLY) when
120 * tpg_1/attrib/demo_mode_write_protect=1
123 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
125 lun_access = deve->lun_flags;
128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
129 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
130 TRANSPORT_LUNFLAGS_READ_WRITE;
133 * Determine the actual mapped LUN value user wants..
135 * This value is what the SCSI Initiator actually sees the
136 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
138 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
141 static int target_fabric_mappedlun_unlink(
142 struct config_item *lun_acl_ci,
143 struct config_item *lun_ci)
145 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
146 struct se_lun_acl, se_lun_group);
147 struct se_lun *lun = container_of(to_config_group(lun_ci),
148 struct se_lun, lun_group);
150 return core_dev_del_initiator_node_lun_acl(lun, lacl);
153 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
155 return container_of(to_config_group(item), struct se_lun_acl,
159 static ssize_t target_fabric_mappedlun_write_protect_show(
160 struct config_item *item, char *page)
162 struct se_lun_acl *lacl = item_to_lun_acl(item);
163 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
164 struct se_dev_entry *deve;
168 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
170 len = sprintf(page, "%d\n",
171 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
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, &op);
191 if ((op != 1) && (op != 0))
194 core_update_device_list_access(lacl->mapped_lun, (op) ?
195 TRANSPORT_LUNFLAGS_READ_ONLY :
196 TRANSPORT_LUNFLAGS_READ_WRITE,
199 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
200 " Mapped LUN: %llu Write Protect bit to %s\n",
201 se_tpg->se_tpg_tfo->get_fabric_name(),
202 se_nacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
208 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
210 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
211 &target_fabric_mappedlun_attr_write_protect,
215 static void target_fabric_mappedlun_release(struct config_item *item)
217 struct se_lun_acl *lacl = container_of(to_config_group(item),
218 struct se_lun_acl, se_lun_group);
219 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
221 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
224 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
225 .release = target_fabric_mappedlun_release,
226 .allow_link = target_fabric_mappedlun_link,
227 .drop_link = target_fabric_mappedlun_unlink,
230 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
231 target_fabric_mappedlun_attrs);
233 /* End of tfc_tpg_mappedlun_cit */
235 /* Start of tfc_tpg_mappedlun_port_cit */
237 static struct config_group *target_core_mappedlun_stat_mkdir(
238 struct config_group *group,
241 return ERR_PTR(-ENOSYS);
244 static void target_core_mappedlun_stat_rmdir(
245 struct config_group *group,
246 struct config_item *item)
251 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
252 .make_group = target_core_mappedlun_stat_mkdir,
253 .drop_item = target_core_mappedlun_stat_rmdir,
256 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
259 /* End of tfc_tpg_mappedlun_port_cit */
261 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
262 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
263 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
265 /* Start of tfc_tpg_nacl_base_cit */
267 static struct config_group *target_fabric_make_mappedlun(
268 struct config_group *group,
271 struct se_node_acl *se_nacl = container_of(group,
272 struct se_node_acl, acl_group);
273 struct se_portal_group *se_tpg = se_nacl->se_tpg;
274 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
275 struct se_lun_acl *lacl = NULL;
277 unsigned long long mapped_lun;
280 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
282 pr_err("Unable to allocate memory for name buf\n");
283 return ERR_PTR(-ENOMEM);
285 snprintf(buf, strlen(name) + 1, "%s", name);
287 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
289 if (strstr(buf, "lun_") != buf) {
290 pr_err("Unable to locate \"lun_\" from buf: %s"
291 " name: %s\n", buf, name);
296 * Determine the Mapped LUN value. This is what the SCSI Initiator
297 * Port will actually see.
299 ret = kstrtoull(buf + 4, 0, &mapped_lun);
303 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
310 config_group_init_type_name(&lacl->se_lun_group, name,
311 &tf->tf_tpg_mappedlun_cit);
313 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
314 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
315 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
316 &lacl->se_lun_group);
318 target_stat_setup_mappedlun_default_groups(lacl);
321 return &lacl->se_lun_group;
328 static void target_fabric_drop_mappedlun(
329 struct config_group *group,
330 struct config_item *item)
332 struct se_lun_acl *lacl = container_of(to_config_group(item),
333 struct se_lun_acl, se_lun_group);
335 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
336 configfs_remove_default_groups(&lacl->se_lun_group);
338 config_item_put(item);
341 static void target_fabric_nacl_base_release(struct config_item *item)
343 struct se_node_acl *se_nacl = container_of(to_config_group(item),
344 struct se_node_acl, acl_group);
345 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
347 if (tf->tf_ops->fabric_cleanup_nodeacl)
348 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
349 core_tpg_del_initiator_node_acl(se_nacl);
352 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
353 .release = target_fabric_nacl_base_release,
356 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
357 .make_group = target_fabric_make_mappedlun,
358 .drop_item = target_fabric_drop_mappedlun,
361 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
362 &target_fabric_nacl_base_group_ops);
364 /* End of tfc_tpg_nacl_base_cit */
366 /* Start of tfc_node_fabric_stats_cit */
368 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
369 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
371 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
373 /* End of tfc_wwn_fabric_stats_cit */
375 /* Start of tfc_tpg_nacl_cit */
377 static struct config_group *target_fabric_make_nodeacl(
378 struct config_group *group,
381 struct se_portal_group *se_tpg = container_of(group,
382 struct se_portal_group, tpg_acl_group);
383 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
384 struct se_node_acl *se_nacl;
386 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
388 return ERR_CAST(se_nacl);
390 if (tf->tf_ops->fabric_init_nodeacl) {
391 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
393 core_tpg_del_initiator_node_acl(se_nacl);
398 config_group_init_type_name(&se_nacl->acl_group, name,
399 &tf->tf_tpg_nacl_base_cit);
401 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
402 &tf->tf_tpg_nacl_attrib_cit);
403 configfs_add_default_group(&se_nacl->acl_attrib_group,
404 &se_nacl->acl_group);
406 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
407 &tf->tf_tpg_nacl_auth_cit);
408 configfs_add_default_group(&se_nacl->acl_auth_group,
409 &se_nacl->acl_group);
411 config_group_init_type_name(&se_nacl->acl_param_group, "param",
412 &tf->tf_tpg_nacl_param_cit);
413 configfs_add_default_group(&se_nacl->acl_param_group,
414 &se_nacl->acl_group);
416 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
417 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
418 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
419 &se_nacl->acl_group);
421 return &se_nacl->acl_group;
424 static void target_fabric_drop_nodeacl(
425 struct config_group *group,
426 struct config_item *item)
428 struct se_node_acl *se_nacl = container_of(to_config_group(item),
429 struct se_node_acl, acl_group);
431 configfs_remove_default_groups(&se_nacl->acl_group);
434 * struct se_node_acl free is done in target_fabric_nacl_base_release()
436 config_item_put(item);
439 static struct configfs_group_operations target_fabric_nacl_group_ops = {
440 .make_group = target_fabric_make_nodeacl,
441 .drop_item = target_fabric_drop_nodeacl,
444 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
446 /* End of tfc_tpg_nacl_cit */
448 /* Start of tfc_tpg_np_base_cit */
450 static void target_fabric_np_base_release(struct config_item *item)
452 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
453 struct se_tpg_np, tpg_np_group);
454 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
455 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
457 tf->tf_ops->fabric_drop_np(se_tpg_np);
460 static struct configfs_item_operations target_fabric_np_base_item_ops = {
461 .release = target_fabric_np_base_release,
464 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
466 /* End of tfc_tpg_np_base_cit */
468 /* Start of tfc_tpg_np_cit */
470 static struct config_group *target_fabric_make_np(
471 struct config_group *group,
474 struct se_portal_group *se_tpg = container_of(group,
475 struct se_portal_group, tpg_np_group);
476 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
477 struct se_tpg_np *se_tpg_np;
479 if (!tf->tf_ops->fabric_make_np) {
480 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
481 return ERR_PTR(-ENOSYS);
484 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
485 if (!se_tpg_np || IS_ERR(se_tpg_np))
486 return ERR_PTR(-EINVAL);
488 se_tpg_np->tpg_np_parent = se_tpg;
489 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
490 &tf->tf_tpg_np_base_cit);
492 return &se_tpg_np->tpg_np_group;
495 static void target_fabric_drop_np(
496 struct config_group *group,
497 struct config_item *item)
500 * struct se_tpg_np is released via target_fabric_np_base_release()
502 config_item_put(item);
505 static struct configfs_group_operations target_fabric_np_group_ops = {
506 .make_group = &target_fabric_make_np,
507 .drop_item = &target_fabric_drop_np,
510 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
512 /* End of tfc_tpg_np_cit */
514 /* Start of tfc_tpg_port_cit */
516 static struct se_lun *item_to_lun(struct config_item *item)
518 return container_of(to_config_group(item), struct se_lun,
522 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
525 struct se_lun *lun = item_to_lun(item);
527 if (!lun || !lun->lun_se_dev)
530 return core_alua_show_tg_pt_gp_info(lun, page);
533 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
534 const char *page, size_t count)
536 struct se_lun *lun = item_to_lun(item);
538 if (!lun || !lun->lun_se_dev)
541 return core_alua_store_tg_pt_gp_info(lun, page, count);
544 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
545 struct config_item *item, char *page)
547 struct se_lun *lun = item_to_lun(item);
549 if (!lun || !lun->lun_se_dev)
552 return core_alua_show_offline_bit(lun, page);
555 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
556 struct config_item *item, const char *page, size_t count)
558 struct se_lun *lun = item_to_lun(item);
560 if (!lun || !lun->lun_se_dev)
563 return core_alua_store_offline_bit(lun, page, count);
566 static ssize_t target_fabric_port_alua_tg_pt_status_show(
567 struct config_item *item, char *page)
569 struct se_lun *lun = item_to_lun(item);
571 if (!lun || !lun->lun_se_dev)
574 return core_alua_show_secondary_status(lun, page);
577 static ssize_t target_fabric_port_alua_tg_pt_status_store(
578 struct config_item *item, const char *page, size_t count)
580 struct se_lun *lun = item_to_lun(item);
582 if (!lun || !lun->lun_se_dev)
585 return core_alua_store_secondary_status(lun, page, count);
588 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
589 struct config_item *item, char *page)
591 struct se_lun *lun = item_to_lun(item);
593 if (!lun || !lun->lun_se_dev)
596 return core_alua_show_secondary_write_metadata(lun, page);
599 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
600 struct config_item *item, const char *page, size_t count)
602 struct se_lun *lun = item_to_lun(item);
604 if (!lun || !lun->lun_se_dev)
607 return core_alua_store_secondary_write_metadata(lun, page, count);
610 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
611 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
612 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
613 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
615 static struct configfs_attribute *target_fabric_port_attrs[] = {
616 &target_fabric_port_attr_alua_tg_pt_gp,
617 &target_fabric_port_attr_alua_tg_pt_offline,
618 &target_fabric_port_attr_alua_tg_pt_status,
619 &target_fabric_port_attr_alua_tg_pt_write_md,
623 static int target_fabric_port_link(
624 struct config_item *lun_ci,
625 struct config_item *se_dev_ci)
627 struct config_item *tpg_ci;
628 struct se_lun *lun = container_of(to_config_group(lun_ci),
629 struct se_lun, lun_group);
630 struct se_portal_group *se_tpg;
631 struct se_device *dev =
632 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
633 struct target_fabric_configfs *tf;
636 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
637 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
638 " %p to struct se_device: %p\n", se_dev_ci, dev);
642 if (!(dev->dev_flags & DF_CONFIGURED)) {
643 pr_err("se_device not configured yet, cannot port link\n");
647 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
648 se_tpg = container_of(to_config_group(tpg_ci),
649 struct se_portal_group, tpg_group);
650 tf = se_tpg->se_tpg_wwn->wwn_tf;
652 if (lun->lun_se_dev != NULL) {
653 pr_err("Port Symlink already exists\n");
657 ret = core_dev_add_lun(se_tpg, dev, lun);
659 pr_err("core_dev_add_lun() failed: %d\n", ret);
663 if (tf->tf_ops->fabric_post_link) {
665 * Call the optional fabric_post_link() to allow a
666 * fabric module to setup any additional state once
667 * core_dev_add_lun() has been called..
669 tf->tf_ops->fabric_post_link(se_tpg, lun);
677 static int target_fabric_port_unlink(
678 struct config_item *lun_ci,
679 struct config_item *se_dev_ci)
681 struct se_lun *lun = container_of(to_config_group(lun_ci),
682 struct se_lun, lun_group);
683 struct se_portal_group *se_tpg = lun->lun_tpg;
684 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
686 if (tf->tf_ops->fabric_pre_unlink) {
688 * Call the optional fabric_pre_unlink() to allow a
689 * fabric module to release any additional stat before
690 * core_dev_del_lun() is called.
692 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
695 core_dev_del_lun(se_tpg, lun);
699 static void target_fabric_port_release(struct config_item *item)
701 struct se_lun *lun = container_of(to_config_group(item),
702 struct se_lun, lun_group);
704 kfree_rcu(lun, rcu_head);
707 static struct configfs_item_operations target_fabric_port_item_ops = {
708 .release = target_fabric_port_release,
709 .allow_link = target_fabric_port_link,
710 .drop_link = target_fabric_port_unlink,
713 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
715 /* End of tfc_tpg_port_cit */
717 /* Start of tfc_tpg_port_stat_cit */
719 static struct config_group *target_core_port_stat_mkdir(
720 struct config_group *group,
723 return ERR_PTR(-ENOSYS);
726 static void target_core_port_stat_rmdir(
727 struct config_group *group,
728 struct config_item *item)
733 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
734 .make_group = target_core_port_stat_mkdir,
735 .drop_item = target_core_port_stat_rmdir,
738 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
740 /* End of tfc_tpg_port_stat_cit */
742 /* Start of tfc_tpg_lun_cit */
744 static struct config_group *target_fabric_make_lun(
745 struct config_group *group,
749 struct se_portal_group *se_tpg = container_of(group,
750 struct se_portal_group, tpg_lun_group);
751 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
752 unsigned long long unpacked_lun;
755 if (strstr(name, "lun_") != name) {
756 pr_err("Unable to locate \'_\" in"
757 " \"lun_$LUN_NUMBER\"\n");
758 return ERR_PTR(-EINVAL);
760 errno = kstrtoull(name + 4, 0, &unpacked_lun);
762 return ERR_PTR(errno);
764 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
766 return ERR_CAST(lun);
768 config_group_init_type_name(&lun->lun_group, name,
769 &tf->tf_tpg_port_cit);
771 config_group_init_type_name(&lun->port_stat_grps.stat_group,
772 "statistics", &tf->tf_tpg_port_stat_cit);
773 configfs_add_default_group(&lun->port_stat_grps.stat_group,
776 target_stat_setup_port_default_groups(lun);
778 return &lun->lun_group;
781 static void target_fabric_drop_lun(
782 struct config_group *group,
783 struct config_item *item)
785 struct se_lun *lun = container_of(to_config_group(item),
786 struct se_lun, lun_group);
788 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
789 configfs_remove_default_groups(&lun->lun_group);
791 config_item_put(item);
794 static struct configfs_group_operations target_fabric_lun_group_ops = {
795 .make_group = &target_fabric_make_lun,
796 .drop_item = &target_fabric_drop_lun,
799 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
801 /* End of tfc_tpg_lun_cit */
803 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
804 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
805 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
807 /* Start of tfc_tpg_base_cit */
809 static void target_fabric_tpg_release(struct config_item *item)
811 struct se_portal_group *se_tpg = container_of(to_config_group(item),
812 struct se_portal_group, tpg_group);
813 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
814 struct target_fabric_configfs *tf = wwn->wwn_tf;
816 tf->tf_ops->fabric_drop_tpg(se_tpg);
819 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
820 .release = target_fabric_tpg_release,
823 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
825 /* End of tfc_tpg_base_cit */
827 /* Start of tfc_tpg_cit */
829 static struct config_group *target_fabric_make_tpg(
830 struct config_group *group,
833 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
834 struct target_fabric_configfs *tf = wwn->wwn_tf;
835 struct se_portal_group *se_tpg;
837 if (!tf->tf_ops->fabric_make_tpg) {
838 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
839 return ERR_PTR(-ENOSYS);
842 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
843 if (!se_tpg || IS_ERR(se_tpg))
844 return ERR_PTR(-EINVAL);
846 config_group_init_type_name(&se_tpg->tpg_group, name,
847 &tf->tf_tpg_base_cit);
849 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
850 &tf->tf_tpg_lun_cit);
851 configfs_add_default_group(&se_tpg->tpg_lun_group,
854 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
856 configfs_add_default_group(&se_tpg->tpg_np_group,
859 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
860 &tf->tf_tpg_nacl_cit);
861 configfs_add_default_group(&se_tpg->tpg_acl_group,
864 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
865 &tf->tf_tpg_attrib_cit);
866 configfs_add_default_group(&se_tpg->tpg_attrib_group,
869 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
870 &tf->tf_tpg_auth_cit);
871 configfs_add_default_group(&se_tpg->tpg_auth_group,
874 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
875 &tf->tf_tpg_param_cit);
876 configfs_add_default_group(&se_tpg->tpg_param_group,
879 return &se_tpg->tpg_group;
882 static void target_fabric_drop_tpg(
883 struct config_group *group,
884 struct config_item *item)
886 struct se_portal_group *se_tpg = container_of(to_config_group(item),
887 struct se_portal_group, tpg_group);
889 configfs_remove_default_groups(&se_tpg->tpg_group);
890 config_item_put(item);
893 static void target_fabric_release_wwn(struct config_item *item)
895 struct se_wwn *wwn = container_of(to_config_group(item),
896 struct se_wwn, wwn_group);
897 struct target_fabric_configfs *tf = wwn->wwn_tf;
899 tf->tf_ops->fabric_drop_wwn(wwn);
902 static struct configfs_item_operations target_fabric_tpg_item_ops = {
903 .release = target_fabric_release_wwn,
906 static struct configfs_group_operations target_fabric_tpg_group_ops = {
907 .make_group = target_fabric_make_tpg,
908 .drop_item = target_fabric_drop_tpg,
911 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
914 /* End of tfc_tpg_cit */
916 /* Start of tfc_wwn_fabric_stats_cit */
918 * This is used as a placeholder for struct se_wwn->fabric_stat_group
919 * to allow fabrics access to ->fabric_stat_group->default_groups[]
921 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
923 /* End of tfc_wwn_fabric_stats_cit */
925 /* Start of tfc_wwn_cit */
927 static struct config_group *target_fabric_make_wwn(
928 struct config_group *group,
931 struct target_fabric_configfs *tf = container_of(group,
932 struct target_fabric_configfs, tf_group);
935 if (!tf->tf_ops->fabric_make_wwn) {
936 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
937 return ERR_PTR(-ENOSYS);
940 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
941 if (!wwn || IS_ERR(wwn))
942 return ERR_PTR(-EINVAL);
946 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
948 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
949 &tf->tf_wwn_fabric_stats_cit);
950 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
952 return &wwn->wwn_group;
955 static void target_fabric_drop_wwn(
956 struct config_group *group,
957 struct config_item *item)
959 struct se_wwn *wwn = container_of(to_config_group(item),
960 struct se_wwn, wwn_group);
962 configfs_remove_default_groups(&wwn->wwn_group);
963 config_item_put(item);
966 static struct configfs_group_operations target_fabric_wwn_group_ops = {
967 .make_group = target_fabric_make_wwn,
968 .drop_item = target_fabric_drop_wwn,
971 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
972 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
974 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
976 target_fabric_setup_discovery_cit(tf);
977 target_fabric_setup_wwn_cit(tf);
978 target_fabric_setup_wwn_fabric_stats_cit(tf);
979 target_fabric_setup_tpg_cit(tf);
980 target_fabric_setup_tpg_base_cit(tf);
981 target_fabric_setup_tpg_port_cit(tf);
982 target_fabric_setup_tpg_port_stat_cit(tf);
983 target_fabric_setup_tpg_lun_cit(tf);
984 target_fabric_setup_tpg_np_cit(tf);
985 target_fabric_setup_tpg_np_base_cit(tf);
986 target_fabric_setup_tpg_attrib_cit(tf);
987 target_fabric_setup_tpg_auth_cit(tf);
988 target_fabric_setup_tpg_param_cit(tf);
989 target_fabric_setup_tpg_nacl_cit(tf);
990 target_fabric_setup_tpg_nacl_base_cit(tf);
991 target_fabric_setup_tpg_nacl_attrib_cit(tf);
992 target_fabric_setup_tpg_nacl_auth_cit(tf);
993 target_fabric_setup_tpg_nacl_param_cit(tf);
994 target_fabric_setup_tpg_nacl_stat_cit(tf);
995 target_fabric_setup_tpg_mappedlun_cit(tf);
996 target_fabric_setup_tpg_mappedlun_stat_cit(tf);