blob: 31a096aa16aba6dc4239cd412f813709709723f8 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2* Filename: target_core_fabric_configfs.c
3 *
4 * This file contains generic fabric module configfs infrastructure for
5 * TCM v4.x code
6 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07007 * (c) Copyright 2010-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
Nicholas Bellingerfd9a11d2012-11-09 14:51:48 -08009 * Nicholas A. Bellinger <nab@linux-iscsi.org>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080010*
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.
15 *
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 ****************************************************************************/
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080024#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>
35
36#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050037#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038
Christoph Hellwige26d99a2011-11-14 12:30:30 -050039#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_pr.h"
42
43#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
44static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
45{ \
Christoph Hellwig968ebe72015-05-03 08:50:55 +020046 struct config_item_type *cit = &tf->tf_##_name##_cit; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047 \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +020051 cit->ct_owner = tf->tf_ops->module; \
Andy Grover6708bb22011-06-08 10:36:43 -070052 pr_debug("Setup generic %s\n", __stringify(_name)); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080053}
54
Christoph Hellwig9ac89282015-04-08 20:01:35 +020055#define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
57{ \
Christoph Hellwig968ebe72015-05-03 08:50:55 +020058 struct config_item_type *cit = &tf->tf_##_name##_cit; \
Christoph Hellwigef0caf82015-05-03 08:50:53 +020059 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
Christoph Hellwig9ac89282015-04-08 20:01:35 +020060 \
61 cit->ct_item_ops = _item_ops; \
62 cit->ct_group_ops = _group_ops; \
63 cit->ct_attrs = attrs; \
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +020064 cit->ct_owner = tf->tf_ops->module; \
Christoph Hellwig9ac89282015-04-08 20:01:35 +020065 pr_debug("Setup generic %s\n", __stringify(_name)); \
66}
67
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068/* Start of tfc_tpg_mappedlun_cit */
69
70static int target_fabric_mappedlun_link(
71 struct config_item *lun_acl_ci,
72 struct config_item *lun_ci)
73{
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;
Andy Grover03a68b42016-02-25 15:14:32 -080081 bool lun_access_ro;
Nicholas Bellinger0ff87542012-12-04 23:43:57 -080082
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;
87 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 /*
89 * Ensure that the source port exists
90 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -070091 if (!lun->lun_se_dev) {
92 pr_err("Source se_lun->lun_se_dev does not exist\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080093 return -EINVAL;
94 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -070095 se_tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096
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;
102 /*
103 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
104 */
105 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700106 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800107 config_item_name(wwn_ci));
108 return -EINVAL;
109 }
110 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700111 pr_err("Illegal Initiator ACL Symlink outside of %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 " TPGT: %s\n", config_item_name(wwn_ci),
113 config_item_name(tpg_ci));
114 return -EINVAL;
115 }
116 /*
117 * If this struct se_node_acl was dynamically generated with
Andy Grover03a68b42016-02-25 15:14:32 -0800118 * tpg_1/attrib/generate_node_acls=1, use the existing
119 * deve->lun_access_ro value, which will be true when
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 * tpg_1/attrib/demo_mode_write_protect=1
121 */
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700122 rcu_read_lock();
123 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
124 if (deve)
Andy Grover03a68b42016-02-25 15:14:32 -0800125 lun_access_ro = deve->lun_access_ro;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800126 else
Andy Grover03a68b42016-02-25 15:14:32 -0800127 lun_access_ro =
Andy Grovere3d6f902011-07-19 08:55:10 +0000128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
Andy Grover03a68b42016-02-25 15:14:32 -0800129 se_tpg)) ? true : false;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700130 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800131 /*
132 * Determine the actual mapped LUN value user wants..
133 *
134 * This value is what the SCSI Initiator actually sees the
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700135 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 */
Andy Grover03a68b42016-02-25 15:14:32 -0800137 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138}
139
140static int target_fabric_mappedlun_unlink(
141 struct config_item *lun_acl_ci,
142 struct config_item *lun_ci)
143{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
145 struct se_lun_acl, se_lun_group);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700146 struct se_lun *lun = container_of(to_config_group(lun_ci),
147 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700149 return core_dev_del_initiator_node_lun_acl(lun, lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800150}
151
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200152static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800153{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200154 return container_of(to_config_group(item), struct se_lun_acl,
155 se_lun_group);
156}
157
158static ssize_t target_fabric_mappedlun_write_protect_show(
159 struct config_item *item, char *page)
160{
161 struct se_lun_acl *lacl = item_to_lun_acl(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
163 struct se_dev_entry *deve;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700164 ssize_t len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700166 rcu_read_lock();
167 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
168 if (deve) {
Andy Grover03a68b42016-02-25 15:14:32 -0800169 len = sprintf(page, "%d\n", deve->lun_access_ro);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700170 }
171 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800172
173 return len;
174}
175
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200176static ssize_t target_fabric_mappedlun_write_protect_store(
177 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800178{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200179 struct se_lun_acl *lacl = item_to_lun_acl(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
181 struct se_portal_group *se_tpg = se_nacl->se_tpg;
Andy Grover03a68b42016-02-25 15:14:32 -0800182 unsigned long wp;
Jingoo Han57103d72013-07-19 16:22:19 +0900183 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800184
Andy Grover03a68b42016-02-25 15:14:32 -0800185 ret = kstrtoul(page, 0, &wp);
Jingoo Han57103d72013-07-19 16:22:19 +0900186 if (ret)
187 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800188
Andy Grover03a68b42016-02-25 15:14:32 -0800189 if ((wp != 1) && (wp != 0))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 return -EINVAL;
191
Andy Grover03a68b42016-02-25 15:14:32 -0800192 /* wp=1 means lun_access_ro=true */
193 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800194
Andy Grover6708bb22011-06-08 10:36:43 -0700195 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +0200196 " Mapped LUN: %llu Write Protect bit to %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000197 se_tpg->se_tpg_tfo->get_fabric_name(),
Andy Grover03a68b42016-02-25 15:14:32 -0800198 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199
200 return count;
201
202}
203
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200204CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200206static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
207 &target_fabric_mappedlun_attr_write_protect,
208 NULL,
209};
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800210
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800211static void target_fabric_mappedlun_release(struct config_item *item)
212{
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;
216
217 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
218}
219
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800221 .release = target_fabric_mappedlun_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800222 .allow_link = target_fabric_mappedlun_link,
223 .drop_link = target_fabric_mappedlun_unlink,
224};
225
226TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
227 target_fabric_mappedlun_attrs);
228
229/* End of tfc_tpg_mappedlun_cit */
230
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700231/* Start of tfc_tpg_mappedlun_port_cit */
232
233static struct config_group *target_core_mappedlun_stat_mkdir(
234 struct config_group *group,
235 const char *name)
236{
237 return ERR_PTR(-ENOSYS);
238}
239
240static void target_core_mappedlun_stat_rmdir(
241 struct config_group *group,
242 struct config_item *item)
243{
244 return;
245}
246
247static 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,
250};
251
252TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
253 NULL);
254
255/* End of tfc_tpg_mappedlun_port_cit */
256
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200257TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
258TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
259TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800260
261/* Start of tfc_tpg_nacl_base_cit */
262
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263static struct config_group *target_fabric_make_mappedlun(
264 struct config_group *group,
265 const char *name)
266{
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;
Joern Engelda0abae2014-09-02 17:49:57 -0400271 struct se_lun_acl *lacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800272 char *buf;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200273 unsigned long long mapped_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800274 int ret = 0;
275
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700277 if (!buf) {
278 pr_err("Unable to allocate memory for name buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 return ERR_PTR(-ENOMEM);
280 }
281 snprintf(buf, strlen(name) + 1, "%s", name);
282 /*
283 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
284 */
285 if (strstr(buf, "lun_") != buf) {
Andy Grover6708bb22011-06-08 10:36:43 -0700286 pr_err("Unable to locate \"lun_\" from buf: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 " name: %s\n", buf, name);
288 ret = -EINVAL;
289 goto out;
290 }
291 /*
292 * Determine the Mapped LUN value. This is what the SCSI Initiator
293 * Port will actually see.
294 */
Hannes Reineckef2d30682015-06-10 08:41:22 +0200295 ret = kstrtoull(buf + 4, 0, &mapped_lun);
Jingoo Han57103d72013-07-19 16:22:19 +0900296 if (ret)
297 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298
Nicholas Bellingerfcf29482013-02-18 18:00:33 -0800299 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
300 mapped_lun, &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700301 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700302 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700304 }
305
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 config_group_init_type_name(&lacl->se_lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200307 &tf->tf_tpg_mappedlun_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100308
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700309 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200310 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100311 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
312 &lacl->se_lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700313
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700314 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315
316 kfree(buf);
317 return &lacl->se_lun_group;
318out:
Joern Engelda0abae2014-09-02 17:49:57 -0400319 kfree(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 kfree(buf);
321 return ERR_PTR(ret);
322}
323
324static void target_fabric_drop_mappedlun(
325 struct config_group *group,
326 struct config_item *item)
327{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700328 struct se_lun_acl *lacl = container_of(to_config_group(item),
329 struct se_lun_acl, se_lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700330
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100331 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
332 configfs_remove_default_groups(&lacl->se_lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700333
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800335}
336
337static void target_fabric_nacl_base_release(struct config_item *item)
338{
339 struct se_node_acl *se_nacl = container_of(to_config_group(item),
340 struct se_node_acl, acl_group);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800341
Christoph Hellwigce7043f2016-03-29 13:03:33 +0200342 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200343 core_tpg_del_initiator_node_acl(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344}
345
346static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800347 .release = target_fabric_nacl_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800348};
349
350static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
351 .make_group = target_fabric_make_mappedlun,
352 .drop_item = target_fabric_drop_mappedlun,
353};
354
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200355TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
356 &target_fabric_nacl_base_group_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357
358/* End of tfc_tpg_nacl_base_cit */
359
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700360/* Start of tfc_node_fabric_stats_cit */
361/*
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[]
364 */
365TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
366
367/* End of tfc_wwn_fabric_stats_cit */
368
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369/* Start of tfc_tpg_nacl_cit */
370
371static struct config_group *target_fabric_make_nodeacl(
372 struct config_group *group,
373 const char *name)
374{
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;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200380 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200382 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 config_group_init_type_name(&se_nacl->acl_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200385 &tf->tf_tpg_nacl_base_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100386
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200388 &tf->tf_tpg_nacl_attrib_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100389 configfs_add_default_group(&se_nacl->acl_attrib_group,
390 &se_nacl->acl_group);
391
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200393 &tf->tf_tpg_nacl_auth_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100394 configfs_add_default_group(&se_nacl->acl_auth_group,
395 &se_nacl->acl_group);
396
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 config_group_init_type_name(&se_nacl->acl_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200398 &tf->tf_tpg_nacl_param_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100399 configfs_add_default_group(&se_nacl->acl_param_group,
400 &se_nacl->acl_group);
401
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700402 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200403 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100404 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
405 &se_nacl->acl_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406
Christoph Hellwige6e202e2016-03-29 13:03:34 +0200407 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);
413 }
414 }
415
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416 return &se_nacl->acl_group;
417}
418
419static void target_fabric_drop_nodeacl(
420 struct config_group *group,
421 struct config_item *item)
422{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423 struct se_node_acl *se_nacl = container_of(to_config_group(item),
424 struct se_node_acl, acl_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100426 configfs_remove_default_groups(&se_nacl->acl_group);
427
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800428 /*
429 * struct se_node_acl free is done in target_fabric_nacl_base_release()
430 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432}
433
434static struct configfs_group_operations target_fabric_nacl_group_ops = {
435 .make_group = target_fabric_make_nodeacl,
436 .drop_item = target_fabric_drop_nodeacl,
437};
438
439TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
440
441/* End of tfc_tpg_nacl_cit */
442
443/* Start of tfc_tpg_np_base_cit */
444
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800445static void target_fabric_np_base_release(struct config_item *item)
446{
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;
451
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200452 tf->tf_ops->fabric_drop_np(se_tpg_np);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800453}
454
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800456 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457};
458
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200459TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460
461/* End of tfc_tpg_np_base_cit */
462
463/* Start of tfc_tpg_np_cit */
464
465static struct config_group *target_fabric_make_np(
466 struct config_group *group,
467 const char *name)
468{
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;
473
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200474 if (!tf->tf_ops->fabric_make_np) {
Andy Grover6708bb22011-06-08 10:36:43 -0700475 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800476 return ERR_PTR(-ENOSYS);
477 }
478
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200479 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700480 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 return ERR_PTR(-EINVAL);
482
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800483 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200485 &tf->tf_tpg_np_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486
487 return &se_tpg_np->tpg_np_group;
488}
489
490static void target_fabric_drop_np(
491 struct config_group *group,
492 struct config_item *item)
493{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800494 /*
495 * struct se_tpg_np is released via target_fabric_np_base_release()
496 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498}
499
500static struct configfs_group_operations target_fabric_np_group_ops = {
501 .make_group = &target_fabric_make_np,
502 .drop_item = &target_fabric_drop_np,
503};
504
505TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
506
507/* End of tfc_tpg_np_cit */
508
509/* Start of tfc_tpg_port_cit */
510
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200511static struct se_lun *item_to_lun(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200513 return container_of(to_config_group(item), struct se_lun,
514 lun_group);
515}
516
517static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
518 char *page)
519{
520 struct se_lun *lun = item_to_lun(item);
521
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700522 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 return -ENODEV;
524
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700525 return core_alua_show_tg_pt_gp_info(lun, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526}
527
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200528static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
529 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200531 struct se_lun *lun = item_to_lun(item);
532
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700533 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 return -ENODEV;
535
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700536 return core_alua_store_tg_pt_gp_info(lun, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537}
538
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200539static ssize_t target_fabric_port_alua_tg_pt_offline_show(
540 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200542 struct se_lun *lun = item_to_lun(item);
543
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700544 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 return -ENODEV;
546
547 return core_alua_show_offline_bit(lun, page);
548}
549
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200550static ssize_t target_fabric_port_alua_tg_pt_offline_store(
551 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200553 struct se_lun *lun = item_to_lun(item);
554
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700555 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556 return -ENODEV;
557
558 return core_alua_store_offline_bit(lun, page, count);
559}
560
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200561static ssize_t target_fabric_port_alua_tg_pt_status_show(
562 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800563{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200564 struct se_lun *lun = item_to_lun(item);
565
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700566 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 return -ENODEV;
568
569 return core_alua_show_secondary_status(lun, page);
570}
571
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200572static ssize_t target_fabric_port_alua_tg_pt_status_store(
573 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200575 struct se_lun *lun = item_to_lun(item);
576
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700577 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 return -ENODEV;
579
580 return core_alua_store_secondary_status(lun, page, count);
581}
582
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200583static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
584 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200586 struct se_lun *lun = item_to_lun(item);
587
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700588 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800589 return -ENODEV;
590
591 return core_alua_show_secondary_write_metadata(lun, page);
592}
593
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200594static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
595 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200597 struct se_lun *lun = item_to_lun(item);
598
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700599 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800600 return -ENODEV;
601
602 return core_alua_store_secondary_write_metadata(lun, page, count);
603}
604
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200605CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
606CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
607CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
608CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609
610static struct configfs_attribute *target_fabric_port_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200611 &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,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615 NULL,
616};
617
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618static int target_fabric_port_link(
619 struct config_item *lun_ci,
620 struct config_item *se_dev_ci)
621{
622 struct config_item *tpg_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623 struct se_lun *lun = container_of(to_config_group(lun_ci),
624 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 struct se_portal_group *se_tpg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400626 struct se_device *dev =
627 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800628 struct target_fabric_configfs *tf;
629 int ret;
630
Nicholas Bellinger0ff87542012-12-04 23:43:57 -0800631 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;
635 }
636
Nicholas Bellingerfaa06ab2013-01-31 14:56:12 -0800637 if (!(dev->dev_flags & DF_CONFIGURED)) {
638 pr_err("se_device not configured yet, cannot port link\n");
639 return -ENODEV;
640 }
641
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642 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;
646
647 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700648 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 return -EEXIST;
650 }
651
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700652 ret = core_dev_add_lun(se_tpg, dev, lun);
653 if (ret) {
654 pr_err("core_dev_add_lun() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 goto out;
656 }
657
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200658 if (tf->tf_ops->fabric_post_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659 /*
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..
663 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200664 tf->tf_ops->fabric_post_link(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800665 }
666
667 return 0;
668out:
669 return ret;
670}
671
672static int target_fabric_port_unlink(
673 struct config_item *lun_ci,
674 struct config_item *se_dev_ci)
675{
676 struct se_lun *lun = container_of(to_config_group(lun_ci),
677 struct se_lun, lun_group);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700678 struct se_portal_group *se_tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
680
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200681 if (tf->tf_ops->fabric_pre_unlink) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682 /*
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.
686 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200687 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800688 }
689
Andy Grovercd9d7cb2014-06-30 16:39:44 -0700690 core_dev_del_lun(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691 return 0;
692}
693
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700694static void target_fabric_port_release(struct config_item *item)
695{
696 struct se_lun *lun = container_of(to_config_group(item),
697 struct se_lun, lun_group);
698
699 kfree_rcu(lun, rcu_head);
700}
701
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702static struct configfs_item_operations target_fabric_port_item_ops = {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700703 .release = target_fabric_port_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704 .allow_link = target_fabric_port_link,
705 .drop_link = target_fabric_port_unlink,
706};
707
708TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
709
710/* End of tfc_tpg_port_cit */
711
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700712/* Start of tfc_tpg_port_stat_cit */
713
714static struct config_group *target_core_port_stat_mkdir(
715 struct config_group *group,
716 const char *name)
717{
718 return ERR_PTR(-ENOSYS);
719}
720
721static void target_core_port_stat_rmdir(
722 struct config_group *group,
723 struct config_item *item)
724{
725 return;
726}
727
728static 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,
731};
732
733TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
734
735/* End of tfc_tpg_port_stat_cit */
736
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737/* Start of tfc_tpg_lun_cit */
738
739static struct config_group *target_fabric_make_lun(
740 struct config_group *group,
741 const char *name)
742{
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;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200747 unsigned long long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700748 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749
750 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700751 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800752 " \"lun_$LUN_NUMBER\"\n");
753 return ERR_PTR(-EINVAL);
754 }
Hannes Reineckef2d30682015-06-10 08:41:22 +0200755 errno = kstrtoull(name + 4, 0, &unpacked_lun);
Jingoo Han57103d72013-07-19 16:22:19 +0900756 if (errno)
757 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800758
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700759 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
760 if (IS_ERR(lun))
761 return ERR_CAST(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762
763 config_group_init_type_name(&lun->lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200764 &tf->tf_tpg_port_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100765
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700766 config_group_init_type_name(&lun->port_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200767 "statistics", &tf->tf_tpg_port_stat_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100768 configfs_add_default_group(&lun->port_stat_grps.stat_group,
769 &lun->lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700770
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700771 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800772
773 return &lun->lun_group;
774}
775
776static void target_fabric_drop_lun(
777 struct config_group *group,
778 struct config_item *item)
779{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700780 struct se_lun *lun = container_of(to_config_group(item),
781 struct se_lun, lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700782
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100783 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
784 configfs_remove_default_groups(&lun->lun_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700785
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800786 config_item_put(item);
787}
788
789static struct configfs_group_operations target_fabric_lun_group_ops = {
790 .make_group = &target_fabric_make_lun,
791 .drop_item = &target_fabric_drop_lun,
792};
793
794TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
795
796/* End of tfc_tpg_lun_cit */
797
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200798TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
799TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
800TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801
802/* Start of tfc_tpg_base_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800803
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800804static void target_fabric_tpg_release(struct config_item *item)
805{
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;
810
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200811 tf->tf_ops->fabric_drop_tpg(se_tpg);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800812}
813
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800815 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816};
817
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200818TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800819
820/* End of tfc_tpg_base_cit */
821
822/* Start of tfc_tpg_cit */
823
824static struct config_group *target_fabric_make_tpg(
825 struct config_group *group,
826 const char *name)
827{
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;
831
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200832 if (!tf->tf_ops->fabric_make_tpg) {
833 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834 return ERR_PTR(-ENOSYS);
835 }
836
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200837 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700838 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 return ERR_PTR(-EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840
841 config_group_init_type_name(&se_tpg->tpg_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200842 &tf->tf_tpg_base_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100843
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800844 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200845 &tf->tf_tpg_lun_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100846 configfs_add_default_group(&se_tpg->tpg_lun_group,
847 &se_tpg->tpg_group);
848
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200850 &tf->tf_tpg_np_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100851 configfs_add_default_group(&se_tpg->tpg_np_group,
852 &se_tpg->tpg_group);
853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200855 &tf->tf_tpg_nacl_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100856 configfs_add_default_group(&se_tpg->tpg_acl_group,
857 &se_tpg->tpg_group);
858
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800859 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200860 &tf->tf_tpg_attrib_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100861 configfs_add_default_group(&se_tpg->tpg_attrib_group,
862 &se_tpg->tpg_group);
863
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700864 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200865 &tf->tf_tpg_auth_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100866 configfs_add_default_group(&se_tpg->tpg_auth_group,
867 &se_tpg->tpg_group);
868
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200870 &tf->tf_tpg_param_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100871 configfs_add_default_group(&se_tpg->tpg_param_group,
872 &se_tpg->tpg_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873
874 return &se_tpg->tpg_group;
875}
876
877static void target_fabric_drop_tpg(
878 struct config_group *group,
879 struct config_item *item)
880{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800881 struct se_portal_group *se_tpg = container_of(to_config_group(item),
882 struct se_portal_group, tpg_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800883
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100884 configfs_remove_default_groups(&se_tpg->tpg_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886}
887
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800888static void target_fabric_release_wwn(struct config_item *item)
889{
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;
893
Christoph Hellwig839559e2016-03-29 13:03:35 +0200894 configfs_remove_default_groups(&wwn->fabric_stat_group);
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200895 tf->tf_ops->fabric_drop_wwn(wwn);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800896}
897
898static struct configfs_item_operations target_fabric_tpg_item_ops = {
899 .release = target_fabric_release_wwn,
900};
901
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902static struct configfs_group_operations target_fabric_tpg_group_ops = {
903 .make_group = target_fabric_make_tpg,
904 .drop_item = target_fabric_drop_tpg,
905};
906
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800907TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
908 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800909
910/* End of tfc_tpg_cit */
911
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700912/* Start of tfc_wwn_fabric_stats_cit */
913/*
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[]
916 */
917TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
918
919/* End of tfc_wwn_fabric_stats_cit */
920
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800921/* Start of tfc_wwn_cit */
922
923static struct config_group *target_fabric_make_wwn(
924 struct config_group *group,
925 const char *name)
926{
927 struct target_fabric_configfs *tf = container_of(group,
928 struct target_fabric_configfs, tf_group);
929 struct se_wwn *wwn;
930
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200931 if (!tf->tf_ops->fabric_make_wwn) {
Andy Grover6708bb22011-06-08 10:36:43 -0700932 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 return ERR_PTR(-ENOSYS);
934 }
935
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200936 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700937 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800938 return ERR_PTR(-EINVAL);
939
940 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700941
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200942 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100943
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700944 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200945 &tf->tf_wwn_fabric_stats_cit);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100946 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947
Christoph Hellwig839559e2016-03-29 13:03:35 +0200948 if (tf->tf_ops->add_wwn_groups)
949 tf->tf_ops->add_wwn_groups(wwn);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800950 return &wwn->wwn_group;
951}
952
953static void target_fabric_drop_wwn(
954 struct config_group *group,
955 struct config_item *item)
956{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700957 struct se_wwn *wwn = container_of(to_config_group(item),
958 struct se_wwn, wwn_group);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700959
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100960 configfs_remove_default_groups(&wwn->wwn_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800962}
963
964static struct configfs_group_operations target_fabric_wwn_group_ops = {
965 .make_group = target_fabric_make_wwn,
966 .drop_item = target_fabric_drop_wwn,
967};
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200969TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
970TF_CIT_SETUP_DRV(discovery, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800971
972int target_fabric_setup_cits(struct target_fabric_configfs *tf)
973{
974 target_fabric_setup_discovery_cit(tf);
975 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700976 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977 target_fabric_setup_tpg_cit(tf);
978 target_fabric_setup_tpg_base_cit(tf);
979 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700980 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800981 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);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700985 target_fabric_setup_tpg_auth_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986 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);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700992 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800993 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700994 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800995
996 return 0;
997}