arm64: remove duplicate PWM entry in defconfig
[linux-2.6/btrfs-unstable.git] / drivers / target / target_core_fabric_configfs.c
blob31a096aa16aba6dc4239cd412f813709709723f8
1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
5 * TCM v4.x code
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>
26 #include <linux/fs.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) \
45 { \
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) \
57 { \
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;
81 bool lun_access_ro;
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);
86 return -EFAULT;
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");
93 return -EINVAL;
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));
108 return -EINVAL;
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));
114 return -EINVAL;
117 * If this struct se_node_acl was dynamically generated with
118 * tpg_1/attrib/generate_node_acls=1, use the existing
119 * deve->lun_access_ro value, which will be true when
120 * tpg_1/attrib/demo_mode_write_protect=1
122 rcu_read_lock();
123 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
124 if (deve)
125 lun_access_ro = deve->lun_access_ro;
126 else
127 lun_access_ro =
128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
129 se_tpg)) ? true : false;
130 rcu_read_unlock();
132 * Determine the actual mapped LUN value user wants..
134 * This value is what the SCSI Initiator actually sees the
135 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
137 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
140 static int target_fabric_mappedlun_unlink(
141 struct config_item *lun_acl_ci,
142 struct config_item *lun_ci)
144 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
145 struct se_lun_acl, se_lun_group);
146 struct se_lun *lun = container_of(to_config_group(lun_ci),
147 struct se_lun, lun_group);
149 return core_dev_del_initiator_node_lun_acl(lun, lacl);
152 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
154 return container_of(to_config_group(item), struct se_lun_acl,
155 se_lun_group);
158 static ssize_t target_fabric_mappedlun_write_protect_show(
159 struct config_item *item, char *page)
161 struct se_lun_acl *lacl = item_to_lun_acl(item);
162 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
163 struct se_dev_entry *deve;
164 ssize_t len = 0;
166 rcu_read_lock();
167 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
168 if (deve) {
169 len = sprintf(page, "%d\n", deve->lun_access_ro);
171 rcu_read_unlock();
173 return len;
176 static ssize_t target_fabric_mappedlun_write_protect_store(
177 struct config_item *item, const char *page, size_t count)
179 struct se_lun_acl *lacl = item_to_lun_acl(item);
180 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
181 struct se_portal_group *se_tpg = se_nacl->se_tpg;
182 unsigned long wp;
183 int ret;
185 ret = kstrtoul(page, 0, &wp);
186 if (ret)
187 return ret;
189 if ((wp != 1) && (wp != 0))
190 return -EINVAL;
192 /* wp=1 means lun_access_ro=true */
193 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
195 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
196 " Mapped LUN: %llu Write Protect bit to %s\n",
197 se_tpg->se_tpg_tfo->get_fabric_name(),
198 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
200 return count;
204 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
206 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
207 &target_fabric_mappedlun_attr_write_protect,
208 NULL,
211 static void target_fabric_mappedlun_release(struct config_item *item)
213 struct se_lun_acl *lacl = container_of(to_config_group(item),
214 struct se_lun_acl, se_lun_group);
215 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
217 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
220 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
221 .release = target_fabric_mappedlun_release,
222 .allow_link = target_fabric_mappedlun_link,
223 .drop_link = target_fabric_mappedlun_unlink,
226 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
227 target_fabric_mappedlun_attrs);
229 /* End of tfc_tpg_mappedlun_cit */
231 /* Start of tfc_tpg_mappedlun_port_cit */
233 static struct config_group *target_core_mappedlun_stat_mkdir(
234 struct config_group *group,
235 const char *name)
237 return ERR_PTR(-ENOSYS);
240 static void target_core_mappedlun_stat_rmdir(
241 struct config_group *group,
242 struct config_item *item)
244 return;
247 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
248 .make_group = target_core_mappedlun_stat_mkdir,
249 .drop_item = target_core_mappedlun_stat_rmdir,
252 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
253 NULL);
255 /* End of tfc_tpg_mappedlun_port_cit */
257 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
258 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
259 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
261 /* Start of tfc_tpg_nacl_base_cit */
263 static struct config_group *target_fabric_make_mappedlun(
264 struct config_group *group,
265 const char *name)
267 struct se_node_acl *se_nacl = container_of(group,
268 struct se_node_acl, acl_group);
269 struct se_portal_group *se_tpg = se_nacl->se_tpg;
270 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
271 struct se_lun_acl *lacl = NULL;
272 char *buf;
273 unsigned long long mapped_lun;
274 int ret = 0;
276 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
277 if (!buf) {
278 pr_err("Unable to allocate memory for name buf\n");
279 return ERR_PTR(-ENOMEM);
281 snprintf(buf, strlen(name) + 1, "%s", name);
283 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
285 if (strstr(buf, "lun_") != buf) {
286 pr_err("Unable to locate \"lun_\" from buf: %s"
287 " name: %s\n", buf, name);
288 ret = -EINVAL;
289 goto out;
292 * Determine the Mapped LUN value. This is what the SCSI Initiator
293 * Port will actually see.
295 ret = kstrtoull(buf + 4, 0, &mapped_lun);
296 if (ret)
297 goto out;
299 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
300 mapped_lun, &ret);
301 if (!lacl) {
302 ret = -EINVAL;
303 goto out;
306 config_group_init_type_name(&lacl->se_lun_group, name,
307 &tf->tf_tpg_mappedlun_cit);
309 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
310 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
311 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
312 &lacl->se_lun_group);
314 target_stat_setup_mappedlun_default_groups(lacl);
316 kfree(buf);
317 return &lacl->se_lun_group;
318 out:
319 kfree(lacl);
320 kfree(buf);
321 return ERR_PTR(ret);
324 static void target_fabric_drop_mappedlun(
325 struct config_group *group,
326 struct config_item *item)
328 struct se_lun_acl *lacl = container_of(to_config_group(item),
329 struct se_lun_acl, se_lun_group);
331 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
332 configfs_remove_default_groups(&lacl->se_lun_group);
334 config_item_put(item);
337 static void target_fabric_nacl_base_release(struct config_item *item)
339 struct se_node_acl *se_nacl = container_of(to_config_group(item),
340 struct se_node_acl, acl_group);
342 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
343 core_tpg_del_initiator_node_acl(se_nacl);
346 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
347 .release = target_fabric_nacl_base_release,
350 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
351 .make_group = target_fabric_make_mappedlun,
352 .drop_item = target_fabric_drop_mappedlun,
355 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
356 &target_fabric_nacl_base_group_ops);
358 /* End of tfc_tpg_nacl_base_cit */
360 /* Start of tfc_node_fabric_stats_cit */
362 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
363 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
365 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
367 /* End of tfc_wwn_fabric_stats_cit */
369 /* Start of tfc_tpg_nacl_cit */
371 static struct config_group *target_fabric_make_nodeacl(
372 struct config_group *group,
373 const char *name)
375 struct se_portal_group *se_tpg = container_of(group,
376 struct se_portal_group, tpg_acl_group);
377 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
378 struct se_node_acl *se_nacl;
380 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
381 if (IS_ERR(se_nacl))
382 return ERR_CAST(se_nacl);
384 config_group_init_type_name(&se_nacl->acl_group, name,
385 &tf->tf_tpg_nacl_base_cit);
387 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
388 &tf->tf_tpg_nacl_attrib_cit);
389 configfs_add_default_group(&se_nacl->acl_attrib_group,
390 &se_nacl->acl_group);
392 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
393 &tf->tf_tpg_nacl_auth_cit);
394 configfs_add_default_group(&se_nacl->acl_auth_group,
395 &se_nacl->acl_group);
397 config_group_init_type_name(&se_nacl->acl_param_group, "param",
398 &tf->tf_tpg_nacl_param_cit);
399 configfs_add_default_group(&se_nacl->acl_param_group,
400 &se_nacl->acl_group);
402 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
403 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
404 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
405 &se_nacl->acl_group);
407 if (tf->tf_ops->fabric_init_nodeacl) {
408 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
409 if (ret) {
410 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
411 core_tpg_del_initiator_node_acl(se_nacl);
412 return ERR_PTR(ret);
416 return &se_nacl->acl_group;
419 static void target_fabric_drop_nodeacl(
420 struct config_group *group,
421 struct config_item *item)
423 struct se_node_acl *se_nacl = container_of(to_config_group(item),
424 struct se_node_acl, acl_group);
426 configfs_remove_default_groups(&se_nacl->acl_group);
429 * struct se_node_acl free is done in target_fabric_nacl_base_release()
431 config_item_put(item);
434 static struct configfs_group_operations target_fabric_nacl_group_ops = {
435 .make_group = target_fabric_make_nodeacl,
436 .drop_item = target_fabric_drop_nodeacl,
439 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
441 /* End of tfc_tpg_nacl_cit */
443 /* Start of tfc_tpg_np_base_cit */
445 static void target_fabric_np_base_release(struct config_item *item)
447 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
448 struct se_tpg_np, tpg_np_group);
449 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
450 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
452 tf->tf_ops->fabric_drop_np(se_tpg_np);
455 static struct configfs_item_operations target_fabric_np_base_item_ops = {
456 .release = target_fabric_np_base_release,
459 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
461 /* End of tfc_tpg_np_base_cit */
463 /* Start of tfc_tpg_np_cit */
465 static struct config_group *target_fabric_make_np(
466 struct config_group *group,
467 const char *name)
469 struct se_portal_group *se_tpg = container_of(group,
470 struct se_portal_group, tpg_np_group);
471 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
472 struct se_tpg_np *se_tpg_np;
474 if (!tf->tf_ops->fabric_make_np) {
475 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
476 return ERR_PTR(-ENOSYS);
479 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
480 if (!se_tpg_np || IS_ERR(se_tpg_np))
481 return ERR_PTR(-EINVAL);
483 se_tpg_np->tpg_np_parent = se_tpg;
484 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
485 &tf->tf_tpg_np_base_cit);
487 return &se_tpg_np->tpg_np_group;
490 static void target_fabric_drop_np(
491 struct config_group *group,
492 struct config_item *item)
495 * struct se_tpg_np is released via target_fabric_np_base_release()
497 config_item_put(item);
500 static struct configfs_group_operations target_fabric_np_group_ops = {
501 .make_group = &target_fabric_make_np,
502 .drop_item = &target_fabric_drop_np,
505 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
507 /* End of tfc_tpg_np_cit */
509 /* Start of tfc_tpg_port_cit */
511 static struct se_lun *item_to_lun(struct config_item *item)
513 return container_of(to_config_group(item), struct se_lun,
514 lun_group);
517 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
518 char *page)
520 struct se_lun *lun = item_to_lun(item);
522 if (!lun || !lun->lun_se_dev)
523 return -ENODEV;
525 return core_alua_show_tg_pt_gp_info(lun, page);
528 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
529 const char *page, size_t count)
531 struct se_lun *lun = item_to_lun(item);
533 if (!lun || !lun->lun_se_dev)
534 return -ENODEV;
536 return core_alua_store_tg_pt_gp_info(lun, page, count);
539 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
540 struct config_item *item, char *page)
542 struct se_lun *lun = item_to_lun(item);
544 if (!lun || !lun->lun_se_dev)
545 return -ENODEV;
547 return core_alua_show_offline_bit(lun, page);
550 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
551 struct config_item *item, const char *page, size_t count)
553 struct se_lun *lun = item_to_lun(item);
555 if (!lun || !lun->lun_se_dev)
556 return -ENODEV;
558 return core_alua_store_offline_bit(lun, page, count);
561 static ssize_t target_fabric_port_alua_tg_pt_status_show(
562 struct config_item *item, char *page)
564 struct se_lun *lun = item_to_lun(item);
566 if (!lun || !lun->lun_se_dev)
567 return -ENODEV;
569 return core_alua_show_secondary_status(lun, page);
572 static ssize_t target_fabric_port_alua_tg_pt_status_store(
573 struct config_item *item, const char *page, size_t count)
575 struct se_lun *lun = item_to_lun(item);
577 if (!lun || !lun->lun_se_dev)
578 return -ENODEV;
580 return core_alua_store_secondary_status(lun, page, count);
583 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
584 struct config_item *item, char *page)
586 struct se_lun *lun = item_to_lun(item);
588 if (!lun || !lun->lun_se_dev)
589 return -ENODEV;
591 return core_alua_show_secondary_write_metadata(lun, page);
594 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
595 struct config_item *item, const char *page, size_t count)
597 struct se_lun *lun = item_to_lun(item);
599 if (!lun || !lun->lun_se_dev)
600 return -ENODEV;
602 return core_alua_store_secondary_write_metadata(lun, page, count);
605 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
606 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
607 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
608 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
610 static struct configfs_attribute *target_fabric_port_attrs[] = {
611 &target_fabric_port_attr_alua_tg_pt_gp,
612 &target_fabric_port_attr_alua_tg_pt_offline,
613 &target_fabric_port_attr_alua_tg_pt_status,
614 &target_fabric_port_attr_alua_tg_pt_write_md,
615 NULL,
618 static int target_fabric_port_link(
619 struct config_item *lun_ci,
620 struct config_item *se_dev_ci)
622 struct config_item *tpg_ci;
623 struct se_lun *lun = container_of(to_config_group(lun_ci),
624 struct se_lun, lun_group);
625 struct se_portal_group *se_tpg;
626 struct se_device *dev =
627 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
628 struct target_fabric_configfs *tf;
629 int ret;
631 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
632 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
633 " %p to struct se_device: %p\n", se_dev_ci, dev);
634 return -EFAULT;
637 if (!(dev->dev_flags & DF_CONFIGURED)) {
638 pr_err("se_device not configured yet, cannot port link\n");
639 return -ENODEV;
642 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
643 se_tpg = container_of(to_config_group(tpg_ci),
644 struct se_portal_group, tpg_group);
645 tf = se_tpg->se_tpg_wwn->wwn_tf;
647 if (lun->lun_se_dev != NULL) {
648 pr_err("Port Symlink already exists\n");
649 return -EEXIST;
652 ret = core_dev_add_lun(se_tpg, dev, lun);
653 if (ret) {
654 pr_err("core_dev_add_lun() failed: %d\n", ret);
655 goto out;
658 if (tf->tf_ops->fabric_post_link) {
660 * Call the optional fabric_post_link() to allow a
661 * fabric module to setup any additional state once
662 * core_dev_add_lun() has been called..
664 tf->tf_ops->fabric_post_link(se_tpg, lun);
667 return 0;
668 out:
669 return ret;
672 static int target_fabric_port_unlink(
673 struct config_item *lun_ci,
674 struct config_item *se_dev_ci)
676 struct se_lun *lun = container_of(to_config_group(lun_ci),
677 struct se_lun, lun_group);
678 struct se_portal_group *se_tpg = lun->lun_tpg;
679 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
681 if (tf->tf_ops->fabric_pre_unlink) {
683 * Call the optional fabric_pre_unlink() to allow a
684 * fabric module to release any additional stat before
685 * core_dev_del_lun() is called.
687 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
690 core_dev_del_lun(se_tpg, lun);
691 return 0;
694 static void target_fabric_port_release(struct config_item *item)
696 struct se_lun *lun = container_of(to_config_group(item),
697 struct se_lun, lun_group);
699 kfree_rcu(lun, rcu_head);
702 static struct configfs_item_operations target_fabric_port_item_ops = {
703 .release = target_fabric_port_release,
704 .allow_link = target_fabric_port_link,
705 .drop_link = target_fabric_port_unlink,
708 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
710 /* End of tfc_tpg_port_cit */
712 /* Start of tfc_tpg_port_stat_cit */
714 static struct config_group *target_core_port_stat_mkdir(
715 struct config_group *group,
716 const char *name)
718 return ERR_PTR(-ENOSYS);
721 static void target_core_port_stat_rmdir(
722 struct config_group *group,
723 struct config_item *item)
725 return;
728 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
729 .make_group = target_core_port_stat_mkdir,
730 .drop_item = target_core_port_stat_rmdir,
733 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
735 /* End of tfc_tpg_port_stat_cit */
737 /* Start of tfc_tpg_lun_cit */
739 static struct config_group *target_fabric_make_lun(
740 struct config_group *group,
741 const char *name)
743 struct se_lun *lun;
744 struct se_portal_group *se_tpg = container_of(group,
745 struct se_portal_group, tpg_lun_group);
746 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
747 unsigned long long unpacked_lun;
748 int errno;
750 if (strstr(name, "lun_") != name) {
751 pr_err("Unable to locate \'_\" in"
752 " \"lun_$LUN_NUMBER\"\n");
753 return ERR_PTR(-EINVAL);
755 errno = kstrtoull(name + 4, 0, &unpacked_lun);
756 if (errno)
757 return ERR_PTR(errno);
759 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
760 if (IS_ERR(lun))
761 return ERR_CAST(lun);
763 config_group_init_type_name(&lun->lun_group, name,
764 &tf->tf_tpg_port_cit);
766 config_group_init_type_name(&lun->port_stat_grps.stat_group,
767 "statistics", &tf->tf_tpg_port_stat_cit);
768 configfs_add_default_group(&lun->port_stat_grps.stat_group,
769 &lun->lun_group);
771 target_stat_setup_port_default_groups(lun);
773 return &lun->lun_group;
776 static void target_fabric_drop_lun(
777 struct config_group *group,
778 struct config_item *item)
780 struct se_lun *lun = container_of(to_config_group(item),
781 struct se_lun, lun_group);
783 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
784 configfs_remove_default_groups(&lun->lun_group);
786 config_item_put(item);
789 static struct configfs_group_operations target_fabric_lun_group_ops = {
790 .make_group = &target_fabric_make_lun,
791 .drop_item = &target_fabric_drop_lun,
794 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
796 /* End of tfc_tpg_lun_cit */
798 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
799 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
800 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
802 /* Start of tfc_tpg_base_cit */
804 static void target_fabric_tpg_release(struct config_item *item)
806 struct se_portal_group *se_tpg = container_of(to_config_group(item),
807 struct se_portal_group, tpg_group);
808 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
809 struct target_fabric_configfs *tf = wwn->wwn_tf;
811 tf->tf_ops->fabric_drop_tpg(se_tpg);
814 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
815 .release = target_fabric_tpg_release,
818 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
820 /* End of tfc_tpg_base_cit */
822 /* Start of tfc_tpg_cit */
824 static struct config_group *target_fabric_make_tpg(
825 struct config_group *group,
826 const char *name)
828 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
829 struct target_fabric_configfs *tf = wwn->wwn_tf;
830 struct se_portal_group *se_tpg;
832 if (!tf->tf_ops->fabric_make_tpg) {
833 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
834 return ERR_PTR(-ENOSYS);
837 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
838 if (!se_tpg || IS_ERR(se_tpg))
839 return ERR_PTR(-EINVAL);
841 config_group_init_type_name(&se_tpg->tpg_group, name,
842 &tf->tf_tpg_base_cit);
844 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
845 &tf->tf_tpg_lun_cit);
846 configfs_add_default_group(&se_tpg->tpg_lun_group,
847 &se_tpg->tpg_group);
849 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
850 &tf->tf_tpg_np_cit);
851 configfs_add_default_group(&se_tpg->tpg_np_group,
852 &se_tpg->tpg_group);
854 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
855 &tf->tf_tpg_nacl_cit);
856 configfs_add_default_group(&se_tpg->tpg_acl_group,
857 &se_tpg->tpg_group);
859 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
860 &tf->tf_tpg_attrib_cit);
861 configfs_add_default_group(&se_tpg->tpg_attrib_group,
862 &se_tpg->tpg_group);
864 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
865 &tf->tf_tpg_auth_cit);
866 configfs_add_default_group(&se_tpg->tpg_auth_group,
867 &se_tpg->tpg_group);
869 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
870 &tf->tf_tpg_param_cit);
871 configfs_add_default_group(&se_tpg->tpg_param_group,
872 &se_tpg->tpg_group);
874 return &se_tpg->tpg_group;
877 static void target_fabric_drop_tpg(
878 struct config_group *group,
879 struct config_item *item)
881 struct se_portal_group *se_tpg = container_of(to_config_group(item),
882 struct se_portal_group, tpg_group);
884 configfs_remove_default_groups(&se_tpg->tpg_group);
885 config_item_put(item);
888 static void target_fabric_release_wwn(struct config_item *item)
890 struct se_wwn *wwn = container_of(to_config_group(item),
891 struct se_wwn, wwn_group);
892 struct target_fabric_configfs *tf = wwn->wwn_tf;
894 configfs_remove_default_groups(&wwn->fabric_stat_group);
895 tf->tf_ops->fabric_drop_wwn(wwn);
898 static struct configfs_item_operations target_fabric_tpg_item_ops = {
899 .release = target_fabric_release_wwn,
902 static struct configfs_group_operations target_fabric_tpg_group_ops = {
903 .make_group = target_fabric_make_tpg,
904 .drop_item = target_fabric_drop_tpg,
907 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
908 NULL);
910 /* End of tfc_tpg_cit */
912 /* Start of tfc_wwn_fabric_stats_cit */
914 * This is used as a placeholder for struct se_wwn->fabric_stat_group
915 * to allow fabrics access to ->fabric_stat_group->default_groups[]
917 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
919 /* End of tfc_wwn_fabric_stats_cit */
921 /* Start of tfc_wwn_cit */
923 static struct config_group *target_fabric_make_wwn(
924 struct config_group *group,
925 const char *name)
927 struct target_fabric_configfs *tf = container_of(group,
928 struct target_fabric_configfs, tf_group);
929 struct se_wwn *wwn;
931 if (!tf->tf_ops->fabric_make_wwn) {
932 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
933 return ERR_PTR(-ENOSYS);
936 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
937 if (!wwn || IS_ERR(wwn))
938 return ERR_PTR(-EINVAL);
940 wwn->wwn_tf = tf;
942 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
944 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
945 &tf->tf_wwn_fabric_stats_cit);
946 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
948 if (tf->tf_ops->add_wwn_groups)
949 tf->tf_ops->add_wwn_groups(wwn);
950 return &wwn->wwn_group;
953 static void target_fabric_drop_wwn(
954 struct config_group *group,
955 struct config_item *item)
957 struct se_wwn *wwn = container_of(to_config_group(item),
958 struct se_wwn, wwn_group);
960 configfs_remove_default_groups(&wwn->wwn_group);
961 config_item_put(item);
964 static struct configfs_group_operations target_fabric_wwn_group_ops = {
965 .make_group = target_fabric_make_wwn,
966 .drop_item = target_fabric_drop_wwn,
969 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
970 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
972 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
974 target_fabric_setup_discovery_cit(tf);
975 target_fabric_setup_wwn_cit(tf);
976 target_fabric_setup_wwn_fabric_stats_cit(tf);
977 target_fabric_setup_tpg_cit(tf);
978 target_fabric_setup_tpg_base_cit(tf);
979 target_fabric_setup_tpg_port_cit(tf);
980 target_fabric_setup_tpg_port_stat_cit(tf);
981 target_fabric_setup_tpg_lun_cit(tf);
982 target_fabric_setup_tpg_np_cit(tf);
983 target_fabric_setup_tpg_np_base_cit(tf);
984 target_fabric_setup_tpg_attrib_cit(tf);
985 target_fabric_setup_tpg_auth_cit(tf);
986 target_fabric_setup_tpg_param_cit(tf);
987 target_fabric_setup_tpg_nacl_cit(tf);
988 target_fabric_setup_tpg_nacl_base_cit(tf);
989 target_fabric_setup_tpg_nacl_attrib_cit(tf);
990 target_fabric_setup_tpg_nacl_auth_cit(tf);
991 target_fabric_setup_tpg_nacl_param_cit(tf);
992 target_fabric_setup_tpg_nacl_stat_cit(tf);
993 target_fabric_setup_tpg_mappedlun_cit(tf);
994 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
996 return 0;