blob: f916d18ccb487c0910ec79ec3f0d8dda232a7b30 [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;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -070081 int lun_access;
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
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
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)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800125 lun_access = deve->lun_flags;
126 else
127 lun_access =
Andy Grovere3d6f902011-07-19 08:55:10 +0000128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
130 TRANSPORT_LUNFLAGS_READ_WRITE;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700131 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800132 /*
133 * Determine the actual mapped LUN value user wants..
134 *
135 * This value is what the SCSI Initiator actually sees the
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700136 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 */
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700138 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139}
140
141static int target_fabric_mappedlun_unlink(
142 struct config_item *lun_acl_ci,
143 struct config_item *lun_ci)
144{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800145 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
146 struct se_lun_acl, se_lun_group);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700147 struct se_lun *lun = container_of(to_config_group(lun_ci),
148 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700150 return core_dev_del_initiator_node_lun_acl(lun, lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800151}
152
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200153static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800154{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200155 return container_of(to_config_group(item), struct se_lun_acl,
156 se_lun_group);
157}
158
159static ssize_t target_fabric_mappedlun_write_protect_show(
160 struct config_item *item, char *page)
161{
162 struct se_lun_acl *lacl = item_to_lun_acl(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
164 struct se_dev_entry *deve;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700165 ssize_t len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700167 rcu_read_lock();
168 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
169 if (deve) {
170 len = sprintf(page, "%d\n",
171 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
172 }
173 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174
175 return len;
176}
177
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200178static ssize_t target_fabric_mappedlun_write_protect_store(
179 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200181 struct se_lun_acl *lacl = item_to_lun_acl(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800182 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
183 struct se_portal_group *se_tpg = se_nacl->se_tpg;
184 unsigned long op;
Jingoo Han57103d72013-07-19 16:22:19 +0900185 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800186
Jingoo Han57103d72013-07-19 16:22:19 +0900187 ret = kstrtoul(page, 0, &op);
188 if (ret)
189 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190
191 if ((op != 1) && (op != 0))
192 return -EINVAL;
193
194 core_update_device_list_access(lacl->mapped_lun, (op) ?
195 TRANSPORT_LUNFLAGS_READ_ONLY :
196 TRANSPORT_LUNFLAGS_READ_WRITE,
197 lacl->se_lun_nacl);
198
Andy Grover6708bb22011-06-08 10:36:43 -0700199 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +0200200 " Mapped LUN: %llu Write Protect bit to %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000201 se_tpg->se_tpg_tfo->get_fabric_name(),
Chris Zankelb6a54b82015-07-20 16:29:50 -0700202 se_nacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203
204 return count;
205
206}
207
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200208CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800209
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200210static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
211 &target_fabric_mappedlun_attr_write_protect,
212 NULL,
213};
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800215static void target_fabric_mappedlun_release(struct config_item *item)
216{
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;
220
221 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
222}
223
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800225 .release = target_fabric_mappedlun_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226 .allow_link = target_fabric_mappedlun_link,
227 .drop_link = target_fabric_mappedlun_unlink,
228};
229
230TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
231 target_fabric_mappedlun_attrs);
232
233/* End of tfc_tpg_mappedlun_cit */
234
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700235/* Start of tfc_tpg_mappedlun_port_cit */
236
237static struct config_group *target_core_mappedlun_stat_mkdir(
238 struct config_group *group,
239 const char *name)
240{
241 return ERR_PTR(-ENOSYS);
242}
243
244static void target_core_mappedlun_stat_rmdir(
245 struct config_group *group,
246 struct config_item *item)
247{
248 return;
249}
250
251static 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,
254};
255
256TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
257 NULL);
258
259/* End of tfc_tpg_mappedlun_port_cit */
260
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200261TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
262TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
263TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800264
265/* Start of tfc_tpg_nacl_base_cit */
266
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800267static struct config_group *target_fabric_make_mappedlun(
268 struct config_group *group,
269 const char *name)
270{
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;
Joern Engelda0abae2014-09-02 17:49:57 -0400275 struct se_lun_acl *lacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 struct config_item *acl_ci;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700277 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278 char *buf;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200279 unsigned long long mapped_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 int ret = 0;
281
282 acl_ci = &group->cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700283 if (!acl_ci) {
284 pr_err("Unable to locatel acl_ci\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 return NULL;
286 }
287
288 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700289 if (!buf) {
290 pr_err("Unable to allocate memory for name buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800291 return ERR_PTR(-ENOMEM);
292 }
293 snprintf(buf, strlen(name) + 1, "%s", name);
294 /*
295 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
296 */
297 if (strstr(buf, "lun_") != buf) {
Andy Grover6708bb22011-06-08 10:36:43 -0700298 pr_err("Unable to locate \"lun_\" from buf: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 " name: %s\n", buf, name);
300 ret = -EINVAL;
301 goto out;
302 }
303 /*
304 * Determine the Mapped LUN value. This is what the SCSI Initiator
305 * Port will actually see.
306 */
Hannes Reineckef2d30682015-06-10 08:41:22 +0200307 ret = kstrtoull(buf + 4, 0, &mapped_lun);
Jingoo Han57103d72013-07-19 16:22:19 +0900308 if (ret)
309 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310
Nicholas Bellingerfcf29482013-02-18 18:00:33 -0800311 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
312 mapped_lun, &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700313 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700314 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700316 }
317
318 lacl_cg = &lacl->se_lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100319 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700320 GFP_KERNEL);
321 if (!lacl_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700322 pr_err("Unable to allocate lacl_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700323 ret = -ENOMEM;
324 goto out;
325 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326
327 config_group_init_type_name(&lacl->se_lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200328 &tf->tf_tpg_mappedlun_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700329 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200330 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700331 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
332 lacl_cg->default_groups[1] = NULL;
333
Andy Grovere3d6f902011-07-19 08:55:10 +0000334 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100335 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700336 GFP_KERNEL);
337 if (!ml_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700338 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700339 ret = -ENOMEM;
340 goto out;
341 }
342 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343
344 kfree(buf);
345 return &lacl->se_lun_group;
346out:
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700347 if (lacl_cg)
348 kfree(lacl_cg->default_groups);
Joern Engelda0abae2014-09-02 17:49:57 -0400349 kfree(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 kfree(buf);
351 return ERR_PTR(ret);
352}
353
354static void target_fabric_drop_mappedlun(
355 struct config_group *group,
356 struct config_item *item)
357{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700358 struct se_lun_acl *lacl = container_of(to_config_group(item),
359 struct se_lun_acl, se_lun_group);
360 struct config_item *df_item;
361 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
362 int i;
363
Andy Grovere3d6f902011-07-19 08:55:10 +0000364 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700365 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
366 df_item = &ml_stat_grp->default_groups[i]->cg_item;
367 ml_stat_grp->default_groups[i] = NULL;
368 config_item_put(df_item);
369 }
370 kfree(ml_stat_grp->default_groups);
371
372 lacl_cg = &lacl->se_lun_group;
373 for (i = 0; lacl_cg->default_groups[i]; i++) {
374 df_item = &lacl_cg->default_groups[i]->cg_item;
375 lacl_cg->default_groups[i] = NULL;
376 config_item_put(df_item);
377 }
378 kfree(lacl_cg->default_groups);
379
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800381}
382
383static void target_fabric_nacl_base_release(struct config_item *item)
384{
385 struct se_node_acl *se_nacl = container_of(to_config_group(item),
386 struct se_node_acl, acl_group);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200387 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800388
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200389 if (tf->tf_ops->fabric_cleanup_nodeacl)
390 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200391 core_tpg_del_initiator_node_acl(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392}
393
394static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800395 .release = target_fabric_nacl_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396};
397
398static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
399 .make_group = target_fabric_make_mappedlun,
400 .drop_item = target_fabric_drop_mappedlun,
401};
402
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200403TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
404 &target_fabric_nacl_base_group_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800405
406/* End of tfc_tpg_nacl_base_cit */
407
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700408/* Start of tfc_node_fabric_stats_cit */
409/*
410 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
411 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
412 */
413TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
414
415/* End of tfc_wwn_fabric_stats_cit */
416
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417/* Start of tfc_tpg_nacl_cit */
418
419static struct config_group *target_fabric_make_nodeacl(
420 struct config_group *group,
421 const char *name)
422{
423 struct se_portal_group *se_tpg = container_of(group,
424 struct se_portal_group, tpg_acl_group);
425 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
426 struct se_node_acl *se_nacl;
427 struct config_group *nacl_cg;
428
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200429 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200431 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200433 if (tf->tf_ops->fabric_init_nodeacl) {
434 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200435 if (ret) {
436 core_tpg_del_initiator_node_acl(se_nacl);
437 return ERR_PTR(ret);
438 }
439 }
440
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441 nacl_cg = &se_nacl->acl_group;
442 nacl_cg->default_groups = se_nacl->acl_default_groups;
443 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
444 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
445 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700446 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
447 nacl_cg->default_groups[4] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800448
449 config_group_init_type_name(&se_nacl->acl_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200450 &tf->tf_tpg_nacl_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200452 &tf->tf_tpg_nacl_attrib_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200454 &tf->tf_tpg_nacl_auth_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 config_group_init_type_name(&se_nacl->acl_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200456 &tf->tf_tpg_nacl_param_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700457 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200458 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459
460 return &se_nacl->acl_group;
461}
462
463static void target_fabric_drop_nodeacl(
464 struct config_group *group,
465 struct config_item *item)
466{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 struct se_node_acl *se_nacl = container_of(to_config_group(item),
468 struct se_node_acl, acl_group);
469 struct config_item *df_item;
470 struct config_group *nacl_cg;
471 int i;
472
473 nacl_cg = &se_nacl->acl_group;
474 for (i = 0; nacl_cg->default_groups[i]; i++) {
475 df_item = &nacl_cg->default_groups[i]->cg_item;
476 nacl_cg->default_groups[i] = NULL;
477 config_item_put(df_item);
478 }
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800479 /*
480 * struct se_node_acl free is done in target_fabric_nacl_base_release()
481 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483}
484
485static struct configfs_group_operations target_fabric_nacl_group_ops = {
486 .make_group = target_fabric_make_nodeacl,
487 .drop_item = target_fabric_drop_nodeacl,
488};
489
490TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
491
492/* End of tfc_tpg_nacl_cit */
493
494/* Start of tfc_tpg_np_base_cit */
495
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800496static void target_fabric_np_base_release(struct config_item *item)
497{
498 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
499 struct se_tpg_np, tpg_np_group);
500 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
501 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
502
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200503 tf->tf_ops->fabric_drop_np(se_tpg_np);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800504}
505
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800507 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508};
509
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200510TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511
512/* End of tfc_tpg_np_base_cit */
513
514/* Start of tfc_tpg_np_cit */
515
516static struct config_group *target_fabric_make_np(
517 struct config_group *group,
518 const char *name)
519{
520 struct se_portal_group *se_tpg = container_of(group,
521 struct se_portal_group, tpg_np_group);
522 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
523 struct se_tpg_np *se_tpg_np;
524
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200525 if (!tf->tf_ops->fabric_make_np) {
Andy Grover6708bb22011-06-08 10:36:43 -0700526 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 return ERR_PTR(-ENOSYS);
528 }
529
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200530 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700531 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532 return ERR_PTR(-EINVAL);
533
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800534 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200536 &tf->tf_tpg_np_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537
538 return &se_tpg_np->tpg_np_group;
539}
540
541static void target_fabric_drop_np(
542 struct config_group *group,
543 struct config_item *item)
544{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800545 /*
546 * struct se_tpg_np is released via target_fabric_np_base_release()
547 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549}
550
551static struct configfs_group_operations target_fabric_np_group_ops = {
552 .make_group = &target_fabric_make_np,
553 .drop_item = &target_fabric_drop_np,
554};
555
556TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
557
558/* End of tfc_tpg_np_cit */
559
560/* Start of tfc_tpg_port_cit */
561
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200562static struct se_lun *item_to_lun(struct config_item *item)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800563{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200564 return container_of(to_config_group(item), struct se_lun,
565 lun_group);
566}
567
568static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
569 char *page)
570{
571 struct se_lun *lun = item_to_lun(item);
572
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700573 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574 return -ENODEV;
575
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700576 return core_alua_show_tg_pt_gp_info(lun, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800577}
578
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200579static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
580 const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200582 struct se_lun *lun = item_to_lun(item);
583
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700584 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585 return -ENODEV;
586
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700587 return core_alua_store_tg_pt_gp_info(lun, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588}
589
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200590static ssize_t target_fabric_port_alua_tg_pt_offline_show(
591 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200593 struct se_lun *lun = item_to_lun(item);
594
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700595 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596 return -ENODEV;
597
598 return core_alua_show_offline_bit(lun, page);
599}
600
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200601static ssize_t target_fabric_port_alua_tg_pt_offline_store(
602 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800603{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200604 struct se_lun *lun = item_to_lun(item);
605
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700606 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607 return -ENODEV;
608
609 return core_alua_store_offline_bit(lun, page, count);
610}
611
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200612static ssize_t target_fabric_port_alua_tg_pt_status_show(
613 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200615 struct se_lun *lun = item_to_lun(item);
616
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700617 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618 return -ENODEV;
619
620 return core_alua_show_secondary_status(lun, page);
621}
622
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200623static ssize_t target_fabric_port_alua_tg_pt_status_store(
624 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200626 struct se_lun *lun = item_to_lun(item);
627
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700628 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629 return -ENODEV;
630
631 return core_alua_store_secondary_status(lun, page, count);
632}
633
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200634static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
635 struct config_item *item, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200637 struct se_lun *lun = item_to_lun(item);
638
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700639 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 return -ENODEV;
641
642 return core_alua_show_secondary_write_metadata(lun, page);
643}
644
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200645static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
646 struct config_item *item, const char *page, size_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647{
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200648 struct se_lun *lun = item_to_lun(item);
649
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700650 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651 return -ENODEV;
652
653 return core_alua_store_secondary_write_metadata(lun, page, count);
654}
655
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200656CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
657CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
658CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
659CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800660
661static struct configfs_attribute *target_fabric_port_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200662 &target_fabric_port_attr_alua_tg_pt_gp,
663 &target_fabric_port_attr_alua_tg_pt_offline,
664 &target_fabric_port_attr_alua_tg_pt_status,
665 &target_fabric_port_attr_alua_tg_pt_write_md,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 NULL,
667};
668
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669static int target_fabric_port_link(
670 struct config_item *lun_ci,
671 struct config_item *se_dev_ci)
672{
673 struct config_item *tpg_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800674 struct se_lun *lun = container_of(to_config_group(lun_ci),
675 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676 struct se_portal_group *se_tpg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400677 struct se_device *dev =
678 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 struct target_fabric_configfs *tf;
680 int ret;
681
Nicholas Bellinger0ff87542012-12-04 23:43:57 -0800682 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
683 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
684 " %p to struct se_device: %p\n", se_dev_ci, dev);
685 return -EFAULT;
686 }
687
Nicholas Bellingerfaa06ab2013-01-31 14:56:12 -0800688 if (!(dev->dev_flags & DF_CONFIGURED)) {
689 pr_err("se_device not configured yet, cannot port link\n");
690 return -ENODEV;
691 }
692
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
694 se_tpg = container_of(to_config_group(tpg_ci),
695 struct se_portal_group, tpg_group);
696 tf = se_tpg->se_tpg_wwn->wwn_tf;
697
698 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700699 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 return -EEXIST;
701 }
702
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700703 ret = core_dev_add_lun(se_tpg, dev, lun);
704 if (ret) {
705 pr_err("core_dev_add_lun() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800706 goto out;
707 }
708
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200709 if (tf->tf_ops->fabric_post_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 /*
711 * Call the optional fabric_post_link() to allow a
712 * fabric module to setup any additional state once
713 * core_dev_add_lun() has been called..
714 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200715 tf->tf_ops->fabric_post_link(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 }
717
718 return 0;
719out:
720 return ret;
721}
722
723static int target_fabric_port_unlink(
724 struct config_item *lun_ci,
725 struct config_item *se_dev_ci)
726{
727 struct se_lun *lun = container_of(to_config_group(lun_ci),
728 struct se_lun, lun_group);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700729 struct se_portal_group *se_tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800730 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
731
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200732 if (tf->tf_ops->fabric_pre_unlink) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 /*
734 * Call the optional fabric_pre_unlink() to allow a
735 * fabric module to release any additional stat before
736 * core_dev_del_lun() is called.
737 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200738 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800739 }
740
Andy Grovercd9d7cb2014-06-30 16:39:44 -0700741 core_dev_del_lun(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 return 0;
743}
744
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700745static void target_fabric_port_release(struct config_item *item)
746{
747 struct se_lun *lun = container_of(to_config_group(item),
748 struct se_lun, lun_group);
749
750 kfree_rcu(lun, rcu_head);
751}
752
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753static struct configfs_item_operations target_fabric_port_item_ops = {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700754 .release = target_fabric_port_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755 .allow_link = target_fabric_port_link,
756 .drop_link = target_fabric_port_unlink,
757};
758
759TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
760
761/* End of tfc_tpg_port_cit */
762
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700763/* Start of tfc_tpg_port_stat_cit */
764
765static struct config_group *target_core_port_stat_mkdir(
766 struct config_group *group,
767 const char *name)
768{
769 return ERR_PTR(-ENOSYS);
770}
771
772static void target_core_port_stat_rmdir(
773 struct config_group *group,
774 struct config_item *item)
775{
776 return;
777}
778
779static struct configfs_group_operations target_fabric_port_stat_group_ops = {
780 .make_group = target_core_port_stat_mkdir,
781 .drop_item = target_core_port_stat_rmdir,
782};
783
784TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
785
786/* End of tfc_tpg_port_stat_cit */
787
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788/* Start of tfc_tpg_lun_cit */
789
790static struct config_group *target_fabric_make_lun(
791 struct config_group *group,
792 const char *name)
793{
794 struct se_lun *lun;
795 struct se_portal_group *se_tpg = container_of(group,
796 struct se_portal_group, tpg_lun_group);
797 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700798 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200799 unsigned long long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700800 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801
802 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700803 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804 " \"lun_$LUN_NUMBER\"\n");
805 return ERR_PTR(-EINVAL);
806 }
Hannes Reineckef2d30682015-06-10 08:41:22 +0200807 errno = kstrtoull(name + 4, 0, &unpacked_lun);
Jingoo Han57103d72013-07-19 16:22:19 +0900808 if (errno)
809 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700811 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
812 if (IS_ERR(lun))
813 return ERR_CAST(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700815 lun_cg = &lun->lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100816 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700817 GFP_KERNEL);
818 if (!lun_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700819 pr_err("Unable to allocate lun_cg->default_groups\n");
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700820 kfree(lun);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700821 return ERR_PTR(-ENOMEM);
822 }
823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824 config_group_init_type_name(&lun->lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200825 &tf->tf_tpg_port_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700826 config_group_init_type_name(&lun->port_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200827 "statistics", &tf->tf_tpg_port_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700828 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
829 lun_cg->default_groups[1] = NULL;
830
Andy Grovere3d6f902011-07-19 08:55:10 +0000831 port_stat_grp = &lun->port_stat_grps.stat_group;
Andy Groverab6dae82013-12-09 14:27:36 -0800832 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700833 GFP_KERNEL);
834 if (!port_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700835 pr_err("Unable to allocate port_stat_grp->default_groups\n");
Joern Engel1481473b2014-09-17 15:11:28 -0700836 kfree(lun_cg->default_groups);
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700837 kfree(lun);
Joern Engel1481473b2014-09-17 15:11:28 -0700838 return ERR_PTR(-ENOMEM);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700839 }
840 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800841
842 return &lun->lun_group;
843}
844
845static void target_fabric_drop_lun(
846 struct config_group *group,
847 struct config_item *item)
848{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700849 struct se_lun *lun = container_of(to_config_group(item),
850 struct se_lun, lun_group);
851 struct config_item *df_item;
852 struct config_group *lun_cg, *port_stat_grp;
853 int i;
854
Andy Grovere3d6f902011-07-19 08:55:10 +0000855 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700856 for (i = 0; port_stat_grp->default_groups[i]; i++) {
857 df_item = &port_stat_grp->default_groups[i]->cg_item;
858 port_stat_grp->default_groups[i] = NULL;
859 config_item_put(df_item);
860 }
861 kfree(port_stat_grp->default_groups);
862
863 lun_cg = &lun->lun_group;
864 for (i = 0; lun_cg->default_groups[i]; i++) {
865 df_item = &lun_cg->default_groups[i]->cg_item;
866 lun_cg->default_groups[i] = NULL;
867 config_item_put(df_item);
868 }
869 kfree(lun_cg->default_groups);
870
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871 config_item_put(item);
872}
873
874static struct configfs_group_operations target_fabric_lun_group_ops = {
875 .make_group = &target_fabric_make_lun,
876 .drop_item = &target_fabric_drop_lun,
877};
878
879TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
880
881/* End of tfc_tpg_lun_cit */
882
Christoph Hellwig2eafd722015-10-03 15:32:55 +0200883TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
884TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
885TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886
887/* Start of tfc_tpg_base_cit */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800888
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800889static void target_fabric_tpg_release(struct config_item *item)
890{
891 struct se_portal_group *se_tpg = container_of(to_config_group(item),
892 struct se_portal_group, tpg_group);
893 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
894 struct target_fabric_configfs *tf = wwn->wwn_tf;
895
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200896 tf->tf_ops->fabric_drop_tpg(se_tpg);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800897}
898
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800900 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901};
902
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200903TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800904
905/* End of tfc_tpg_base_cit */
906
907/* Start of tfc_tpg_cit */
908
909static struct config_group *target_fabric_make_tpg(
910 struct config_group *group,
911 const char *name)
912{
913 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
914 struct target_fabric_configfs *tf = wwn->wwn_tf;
915 struct se_portal_group *se_tpg;
916
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200917 if (!tf->tf_ops->fabric_make_tpg) {
918 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800919 return ERR_PTR(-ENOSYS);
920 }
921
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200922 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700923 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924 return ERR_PTR(-EINVAL);
925 /*
926 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
927 */
928 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
929 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
930 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
931 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
932 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700933 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
934 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
935 se_tpg->tpg_group.default_groups[6] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936
937 config_group_init_type_name(&se_tpg->tpg_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200938 &tf->tf_tpg_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800939 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200940 &tf->tf_tpg_lun_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800941 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200942 &tf->tf_tpg_np_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200944 &tf->tf_tpg_nacl_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800945 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200946 &tf->tf_tpg_attrib_cit);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700947 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200948 &tf->tf_tpg_auth_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200950 &tf->tf_tpg_param_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951
952 return &se_tpg->tpg_group;
953}
954
955static void target_fabric_drop_tpg(
956 struct config_group *group,
957 struct config_item *item)
958{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800959 struct se_portal_group *se_tpg = container_of(to_config_group(item),
960 struct se_portal_group, tpg_group);
961 struct config_group *tpg_cg = &se_tpg->tpg_group;
962 struct config_item *df_item;
963 int i;
964 /*
965 * Release default groups, but do not release tpg_cg->default_groups
966 * memory as it is statically allocated at se_tpg->tpg_default_groups.
967 */
968 for (i = 0; tpg_cg->default_groups[i]; i++) {
969 df_item = &tpg_cg->default_groups[i]->cg_item;
970 tpg_cg->default_groups[i] = NULL;
971 config_item_put(df_item);
972 }
973
974 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975}
976
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800977static void target_fabric_release_wwn(struct config_item *item)
978{
979 struct se_wwn *wwn = container_of(to_config_group(item),
980 struct se_wwn, wwn_group);
981 struct target_fabric_configfs *tf = wwn->wwn_tf;
982
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200983 tf->tf_ops->fabric_drop_wwn(wwn);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800984}
985
986static struct configfs_item_operations target_fabric_tpg_item_ops = {
987 .release = target_fabric_release_wwn,
988};
989
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800990static struct configfs_group_operations target_fabric_tpg_group_ops = {
991 .make_group = target_fabric_make_tpg,
992 .drop_item = target_fabric_drop_tpg,
993};
994
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800995TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
996 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800997
998/* End of tfc_tpg_cit */
999
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001000/* Start of tfc_wwn_fabric_stats_cit */
1001/*
1002 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1003 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1004 */
1005TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1006
1007/* End of tfc_wwn_fabric_stats_cit */
1008
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001009/* Start of tfc_wwn_cit */
1010
1011static struct config_group *target_fabric_make_wwn(
1012 struct config_group *group,
1013 const char *name)
1014{
1015 struct target_fabric_configfs *tf = container_of(group,
1016 struct target_fabric_configfs, tf_group);
1017 struct se_wwn *wwn;
1018
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001019 if (!tf->tf_ops->fabric_make_wwn) {
Andy Grover6708bb22011-06-08 10:36:43 -07001020 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001021 return ERR_PTR(-ENOSYS);
1022 }
1023
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001024 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001025 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026 return ERR_PTR(-EINVAL);
1027
1028 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001029 /*
1030 * Setup default groups from pre-allocated wwn->wwn_default_groups
1031 */
1032 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1033 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1034 wwn->wwn_group.default_groups[1] = NULL;
1035
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001036 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001037 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001038 &tf->tf_wwn_fabric_stats_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001039
1040 return &wwn->wwn_group;
1041}
1042
1043static void target_fabric_drop_wwn(
1044 struct config_group *group,
1045 struct config_item *item)
1046{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001047 struct se_wwn *wwn = container_of(to_config_group(item),
1048 struct se_wwn, wwn_group);
1049 struct config_item *df_item;
1050 struct config_group *cg = &wwn->wwn_group;
1051 int i;
1052
1053 for (i = 0; cg->default_groups[i]; i++) {
1054 df_item = &cg->default_groups[i]->cg_item;
1055 cg->default_groups[i] = NULL;
1056 config_item_put(df_item);
1057 }
1058
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001060}
1061
1062static struct configfs_group_operations target_fabric_wwn_group_ops = {
1063 .make_group = target_fabric_make_wwn,
1064 .drop_item = target_fabric_drop_wwn,
1065};
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001067TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
1068TF_CIT_SETUP_DRV(discovery, NULL, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069
1070int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1071{
1072 target_fabric_setup_discovery_cit(tf);
1073 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001074 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075 target_fabric_setup_tpg_cit(tf);
1076 target_fabric_setup_tpg_base_cit(tf);
1077 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001078 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079 target_fabric_setup_tpg_lun_cit(tf);
1080 target_fabric_setup_tpg_np_cit(tf);
1081 target_fabric_setup_tpg_np_base_cit(tf);
1082 target_fabric_setup_tpg_attrib_cit(tf);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001083 target_fabric_setup_tpg_auth_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084 target_fabric_setup_tpg_param_cit(tf);
1085 target_fabric_setup_tpg_nacl_cit(tf);
1086 target_fabric_setup_tpg_nacl_base_cit(tf);
1087 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1088 target_fabric_setup_tpg_nacl_auth_cit(tf);
1089 target_fabric_setup_tpg_nacl_param_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001090 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001092 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093
1094 return 0;
1095}