blob: 0ee182fce1a6a7fd28b2c49f7b86cf3201f730b2 [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#include <target/target_core_fabric_configfs.h>
39#include <target/target_core_configfs.h>
40#include <target/configfs_macros.h>
41
Christoph Hellwige26d99a2011-11-14 12:30:30 -050042#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044#include "target_core_pr.h"
45
46#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
47static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
48{ \
Christoph Hellwig968ebe72015-05-03 08:50:55 +020049 struct config_item_type *cit = &tf->tf_##_name##_cit; \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050 \
51 cit->ct_item_ops = _item_ops; \
52 cit->ct_group_ops = _group_ops; \
53 cit->ct_attrs = _attrs; \
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +020054 cit->ct_owner = tf->tf_ops->module; \
Andy Grover6708bb22011-06-08 10:36:43 -070055 pr_debug("Setup generic %s\n", __stringify(_name)); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056}
57
Christoph Hellwig9ac89282015-04-08 20:01:35 +020058#define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
59static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
60{ \
Christoph Hellwig968ebe72015-05-03 08:50:55 +020061 struct config_item_type *cit = &tf->tf_##_name##_cit; \
Christoph Hellwigef0caf82015-05-03 08:50:53 +020062 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
Christoph Hellwig9ac89282015-04-08 20:01:35 +020063 \
64 cit->ct_item_ops = _item_ops; \
65 cit->ct_group_ops = _group_ops; \
66 cit->ct_attrs = attrs; \
Christoph Hellwig0dc2e8d2015-05-03 08:50:54 +020067 cit->ct_owner = tf->tf_ops->module; \
Christoph Hellwig9ac89282015-04-08 20:01:35 +020068 pr_debug("Setup generic %s\n", __stringify(_name)); \
69}
70
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080071/* Start of tfc_tpg_mappedlun_cit */
72
73static int target_fabric_mappedlun_link(
74 struct config_item *lun_acl_ci,
75 struct config_item *lun_ci)
76{
77 struct se_dev_entry *deve;
78 struct se_lun *lun = container_of(to_config_group(lun_ci),
79 struct se_lun, lun_group);
80 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
81 struct se_lun_acl, se_lun_group);
82 struct se_portal_group *se_tpg;
83 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -070084 int lun_access;
Nicholas Bellinger0ff87542012-12-04 23:43:57 -080085
86 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
87 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
88 " %p to struct lun: %p\n", lun_ci, lun);
89 return -EFAULT;
90 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080091 /*
92 * Ensure that the source port exists
93 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -070094 if (!lun->lun_se_dev) {
95 pr_err("Source se_lun->lun_se_dev does not exist\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096 return -EINVAL;
97 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -070098 se_tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099
100 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
101 tpg_ci = &nacl_ci->ci_group->cg_item;
102 wwn_ci = &tpg_ci->ci_group->cg_item;
103 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
104 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
105 /*
106 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
107 */
108 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700109 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800110 config_item_name(wwn_ci));
111 return -EINVAL;
112 }
113 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700114 pr_err("Illegal Initiator ACL Symlink outside of %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800115 " TPGT: %s\n", config_item_name(wwn_ci),
116 config_item_name(tpg_ci));
117 return -EINVAL;
118 }
119 /*
120 * If this struct se_node_acl was dynamically generated with
121 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
122 * which be will write protected (READ-ONLY) when
123 * tpg_1/attrib/demo_mode_write_protect=1
124 */
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700125 rcu_read_lock();
126 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
127 if (deve)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 lun_access = deve->lun_flags;
129 else
130 lun_access =
Andy Grovere3d6f902011-07-19 08:55:10 +0000131 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800132 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
133 TRANSPORT_LUNFLAGS_READ_WRITE;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700134 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135 /*
136 * Determine the actual mapped LUN value user wants..
137 *
138 * This value is what the SCSI Initiator actually sees the
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700139 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 */
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700141 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142}
143
144static int target_fabric_mappedlun_unlink(
145 struct config_item *lun_acl_ci,
146 struct config_item *lun_ci)
147{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
149 struct se_lun_acl, se_lun_group);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700150 struct se_lun *lun = container_of(to_config_group(lun_ci),
151 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800152
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700153 return core_dev_del_initiator_node_lun_acl(lun, lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800154}
155
156CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
157#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
158static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
159 __CONFIGFS_EATTR(_name, _mode, \
160 target_fabric_mappedlun_show_##_name, \
161 target_fabric_mappedlun_store_##_name);
162
163static ssize_t target_fabric_mappedlun_show_write_protect(
164 struct se_lun_acl *lacl,
165 char *page)
166{
167 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
168 struct se_dev_entry *deve;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700169 ssize_t len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700171 rcu_read_lock();
172 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
173 if (deve) {
174 len = sprintf(page, "%d\n",
175 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
176 }
177 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800178
179 return len;
180}
181
182static ssize_t target_fabric_mappedlun_store_write_protect(
183 struct se_lun_acl *lacl,
184 const char *page,
185 size_t count)
186{
187 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
188 struct se_portal_group *se_tpg = se_nacl->se_tpg;
189 unsigned long op;
Jingoo Han57103d72013-07-19 16:22:19 +0900190 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800191
Jingoo Han57103d72013-07-19 16:22:19 +0900192 ret = kstrtoul(page, 0, &op);
193 if (ret)
194 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800195
196 if ((op != 1) && (op != 0))
197 return -EINVAL;
198
199 core_update_device_list_access(lacl->mapped_lun, (op) ?
200 TRANSPORT_LUNFLAGS_READ_ONLY :
201 TRANSPORT_LUNFLAGS_READ_WRITE,
202 lacl->se_lun_nacl);
203
Andy Grover6708bb22011-06-08 10:36:43 -0700204 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205 " Mapped LUN: %u Write Protect bit to %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000206 se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
208
209 return count;
210
211}
212
213TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
214
215CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
216
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800217static void target_fabric_mappedlun_release(struct config_item *item)
218{
219 struct se_lun_acl *lacl = container_of(to_config_group(item),
220 struct se_lun_acl, se_lun_group);
221 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
222
223 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
224}
225
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
227 &target_fabric_mappedlun_write_protect.attr,
228 NULL,
229};
230
231static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800232 .release = target_fabric_mappedlun_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800233 .show_attribute = target_fabric_mappedlun_attr_show,
234 .store_attribute = target_fabric_mappedlun_attr_store,
235 .allow_link = target_fabric_mappedlun_link,
236 .drop_link = target_fabric_mappedlun_unlink,
237};
238
239TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
240 target_fabric_mappedlun_attrs);
241
242/* End of tfc_tpg_mappedlun_cit */
243
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700244/* Start of tfc_tpg_mappedlun_port_cit */
245
246static struct config_group *target_core_mappedlun_stat_mkdir(
247 struct config_group *group,
248 const char *name)
249{
250 return ERR_PTR(-ENOSYS);
251}
252
253static void target_core_mappedlun_stat_rmdir(
254 struct config_group *group,
255 struct config_item *item)
256{
257 return;
258}
259
260static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
261 .make_group = target_core_mappedlun_stat_mkdir,
262 .drop_item = target_core_mappedlun_stat_rmdir,
263};
264
265TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
266 NULL);
267
268/* End of tfc_tpg_mappedlun_port_cit */
269
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800270/* Start of tfc_tpg_nacl_attrib_cit */
271
272CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
273
274static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
275 .show_attribute = target_fabric_nacl_attrib_attr_show,
276 .store_attribute = target_fabric_nacl_attrib_attr_store,
277};
278
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200279TF_CIT_SETUP_DRV(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280
281/* End of tfc_tpg_nacl_attrib_cit */
282
283/* Start of tfc_tpg_nacl_auth_cit */
284
285CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
286
287static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
288 .show_attribute = target_fabric_nacl_auth_attr_show,
289 .store_attribute = target_fabric_nacl_auth_attr_store,
290};
291
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200292TF_CIT_SETUP_DRV(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800293
294/* End of tfc_tpg_nacl_auth_cit */
295
296/* Start of tfc_tpg_nacl_param_cit */
297
298CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
299
300static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
301 .show_attribute = target_fabric_nacl_param_attr_show,
302 .store_attribute = target_fabric_nacl_param_attr_store,
303};
304
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200305TF_CIT_SETUP_DRV(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306
307/* End of tfc_tpg_nacl_param_cit */
308
309/* Start of tfc_tpg_nacl_base_cit */
310
311CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
312
313static struct config_group *target_fabric_make_mappedlun(
314 struct config_group *group,
315 const char *name)
316{
317 struct se_node_acl *se_nacl = container_of(group,
318 struct se_node_acl, acl_group);
319 struct se_portal_group *se_tpg = se_nacl->se_tpg;
320 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Joern Engelda0abae2014-09-02 17:49:57 -0400321 struct se_lun_acl *lacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800322 struct config_item *acl_ci;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700323 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 char *buf;
325 unsigned long mapped_lun;
326 int ret = 0;
327
328 acl_ci = &group->cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700329 if (!acl_ci) {
330 pr_err("Unable to locatel acl_ci\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800331 return NULL;
332 }
333
334 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700335 if (!buf) {
336 pr_err("Unable to allocate memory for name buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800337 return ERR_PTR(-ENOMEM);
338 }
339 snprintf(buf, strlen(name) + 1, "%s", name);
340 /*
341 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
342 */
343 if (strstr(buf, "lun_") != buf) {
Andy Grover6708bb22011-06-08 10:36:43 -0700344 pr_err("Unable to locate \"lun_\" from buf: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345 " name: %s\n", buf, name);
346 ret = -EINVAL;
347 goto out;
348 }
349 /*
350 * Determine the Mapped LUN value. This is what the SCSI Initiator
351 * Port will actually see.
352 */
Jingoo Han57103d72013-07-19 16:22:19 +0900353 ret = kstrtoul(buf + 4, 0, &mapped_lun);
354 if (ret)
355 goto out;
356 if (mapped_lun > UINT_MAX) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 ret = -EINVAL;
358 goto out;
359 }
Nicholas Bellingerfbbf8552013-02-18 18:31:37 -0800360 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
361 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
362 "-1: %u for Target Portal Group: %u\n", mapped_lun,
363 TRANSPORT_MAX_LUNS_PER_TPG-1,
364 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
365 ret = -EINVAL;
366 goto out;
367 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800368
Nicholas Bellingerfcf29482013-02-18 18:00:33 -0800369 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
370 mapped_lun, &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700371 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700372 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700374 }
375
376 lacl_cg = &lacl->se_lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100377 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700378 GFP_KERNEL);
379 if (!lacl_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700380 pr_err("Unable to allocate lacl_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700381 ret = -ENOMEM;
382 goto out;
383 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384
385 config_group_init_type_name(&lacl->se_lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200386 &tf->tf_tpg_mappedlun_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700387 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200388 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700389 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
390 lacl_cg->default_groups[1] = NULL;
391
Andy Grovere3d6f902011-07-19 08:55:10 +0000392 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100393 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700394 GFP_KERNEL);
395 if (!ml_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700396 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700397 ret = -ENOMEM;
398 goto out;
399 }
400 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401
402 kfree(buf);
403 return &lacl->se_lun_group;
404out:
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700405 if (lacl_cg)
406 kfree(lacl_cg->default_groups);
Joern Engelda0abae2014-09-02 17:49:57 -0400407 kfree(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 kfree(buf);
409 return ERR_PTR(ret);
410}
411
412static void target_fabric_drop_mappedlun(
413 struct config_group *group,
414 struct config_item *item)
415{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700416 struct se_lun_acl *lacl = container_of(to_config_group(item),
417 struct se_lun_acl, se_lun_group);
418 struct config_item *df_item;
419 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
420 int i;
421
Andy Grovere3d6f902011-07-19 08:55:10 +0000422 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700423 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
424 df_item = &ml_stat_grp->default_groups[i]->cg_item;
425 ml_stat_grp->default_groups[i] = NULL;
426 config_item_put(df_item);
427 }
428 kfree(ml_stat_grp->default_groups);
429
430 lacl_cg = &lacl->se_lun_group;
431 for (i = 0; lacl_cg->default_groups[i]; i++) {
432 df_item = &lacl_cg->default_groups[i]->cg_item;
433 lacl_cg->default_groups[i] = NULL;
434 config_item_put(df_item);
435 }
436 kfree(lacl_cg->default_groups);
437
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800439}
440
441static void target_fabric_nacl_base_release(struct config_item *item)
442{
443 struct se_node_acl *se_nacl = container_of(to_config_group(item),
444 struct se_node_acl, acl_group);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200445 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800446
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200447 if (tf->tf_ops->fabric_cleanup_nodeacl)
448 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200449 core_tpg_del_initiator_node_acl(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450}
451
452static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800453 .release = target_fabric_nacl_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 .show_attribute = target_fabric_nacl_base_attr_show,
455 .store_attribute = target_fabric_nacl_base_attr_store,
456};
457
458static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
459 .make_group = target_fabric_make_mappedlun,
460 .drop_item = target_fabric_drop_mappedlun,
461};
462
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200463TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
464 &target_fabric_nacl_base_group_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465
466/* End of tfc_tpg_nacl_base_cit */
467
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700468/* Start of tfc_node_fabric_stats_cit */
469/*
470 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
471 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
472 */
473TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
474
475/* End of tfc_wwn_fabric_stats_cit */
476
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477/* Start of tfc_tpg_nacl_cit */
478
479static struct config_group *target_fabric_make_nodeacl(
480 struct config_group *group,
481 const char *name)
482{
483 struct se_portal_group *se_tpg = container_of(group,
484 struct se_portal_group, tpg_acl_group);
485 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
486 struct se_node_acl *se_nacl;
487 struct config_group *nacl_cg;
488
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200489 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200491 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200493 if (tf->tf_ops->fabric_init_nodeacl) {
494 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200495 if (ret) {
496 core_tpg_del_initiator_node_acl(se_nacl);
497 return ERR_PTR(ret);
498 }
499 }
500
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 nacl_cg = &se_nacl->acl_group;
502 nacl_cg->default_groups = se_nacl->acl_default_groups;
503 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
504 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
505 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700506 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
507 nacl_cg->default_groups[4] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508
509 config_group_init_type_name(&se_nacl->acl_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200510 &tf->tf_tpg_nacl_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200512 &tf->tf_tpg_nacl_attrib_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200514 &tf->tf_tpg_nacl_auth_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800515 config_group_init_type_name(&se_nacl->acl_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200516 &tf->tf_tpg_nacl_param_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700517 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200518 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800519
520 return &se_nacl->acl_group;
521}
522
523static void target_fabric_drop_nodeacl(
524 struct config_group *group,
525 struct config_item *item)
526{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 struct se_node_acl *se_nacl = container_of(to_config_group(item),
528 struct se_node_acl, acl_group);
529 struct config_item *df_item;
530 struct config_group *nacl_cg;
531 int i;
532
533 nacl_cg = &se_nacl->acl_group;
534 for (i = 0; nacl_cg->default_groups[i]; i++) {
535 df_item = &nacl_cg->default_groups[i]->cg_item;
536 nacl_cg->default_groups[i] = NULL;
537 config_item_put(df_item);
538 }
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800539 /*
540 * struct se_node_acl free is done in target_fabric_nacl_base_release()
541 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543}
544
545static struct configfs_group_operations target_fabric_nacl_group_ops = {
546 .make_group = target_fabric_make_nodeacl,
547 .drop_item = target_fabric_drop_nodeacl,
548};
549
550TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
551
552/* End of tfc_tpg_nacl_cit */
553
554/* Start of tfc_tpg_np_base_cit */
555
556CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
557
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800558static void target_fabric_np_base_release(struct config_item *item)
559{
560 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
561 struct se_tpg_np, tpg_np_group);
562 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
563 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
564
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200565 tf->tf_ops->fabric_drop_np(se_tpg_np);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800566}
567
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800568static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800569 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570 .show_attribute = target_fabric_np_base_attr_show,
571 .store_attribute = target_fabric_np_base_attr_store,
572};
573
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200574TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800575
576/* End of tfc_tpg_np_base_cit */
577
578/* Start of tfc_tpg_np_cit */
579
580static struct config_group *target_fabric_make_np(
581 struct config_group *group,
582 const char *name)
583{
584 struct se_portal_group *se_tpg = container_of(group,
585 struct se_portal_group, tpg_np_group);
586 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
587 struct se_tpg_np *se_tpg_np;
588
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200589 if (!tf->tf_ops->fabric_make_np) {
Andy Grover6708bb22011-06-08 10:36:43 -0700590 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 return ERR_PTR(-ENOSYS);
592 }
593
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200594 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700595 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596 return ERR_PTR(-EINVAL);
597
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800598 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200600 &tf->tf_tpg_np_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601
602 return &se_tpg_np->tpg_np_group;
603}
604
605static void target_fabric_drop_np(
606 struct config_group *group,
607 struct config_item *item)
608{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800609 /*
610 * struct se_tpg_np is released via target_fabric_np_base_release()
611 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613}
614
615static struct configfs_group_operations target_fabric_np_group_ops = {
616 .make_group = &target_fabric_make_np,
617 .drop_item = &target_fabric_drop_np,
618};
619
620TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
621
622/* End of tfc_tpg_np_cit */
623
624/* Start of tfc_tpg_port_cit */
625
626CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
627#define TCM_PORT_ATTR(_name, _mode) \
628static struct target_fabric_port_attribute target_fabric_port_##_name = \
629 __CONFIGFS_EATTR(_name, _mode, \
630 target_fabric_port_show_attr_##_name, \
631 target_fabric_port_store_attr_##_name);
632
633#define TCM_PORT_ATTOR_RO(_name) \
634 __CONFIGFS_EATTR_RO(_name, \
635 target_fabric_port_show_attr_##_name);
636
637/*
638 * alua_tg_pt_gp
639 */
640static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
641 struct se_lun *lun,
642 char *page)
643{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700644 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800645 return -ENODEV;
646
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700647 return core_alua_show_tg_pt_gp_info(lun, page);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800648}
649
650static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
651 struct se_lun *lun,
652 const char *page,
653 size_t count)
654{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700655 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 return -ENODEV;
657
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700658 return core_alua_store_tg_pt_gp_info(lun, page, count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800659}
660
661TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
662
663/*
664 * alua_tg_pt_offline
665 */
666static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
667 struct se_lun *lun,
668 char *page)
669{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700670 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 return -ENODEV;
672
673 return core_alua_show_offline_bit(lun, page);
674}
675
676static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
677 struct se_lun *lun,
678 const char *page,
679 size_t count)
680{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700681 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682 return -ENODEV;
683
684 return core_alua_store_offline_bit(lun, page, count);
685}
686
687TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
688
689/*
690 * alua_tg_pt_status
691 */
692static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
693 struct se_lun *lun,
694 char *page)
695{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700696 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 return -ENODEV;
698
699 return core_alua_show_secondary_status(lun, page);
700}
701
702static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
703 struct se_lun *lun,
704 const char *page,
705 size_t count)
706{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700707 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 return -ENODEV;
709
710 return core_alua_store_secondary_status(lun, page, count);
711}
712
713TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
714
715/*
716 * alua_tg_pt_write_md
717 */
718static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
719 struct se_lun *lun,
720 char *page)
721{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700722 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723 return -ENODEV;
724
725 return core_alua_show_secondary_write_metadata(lun, page);
726}
727
728static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
729 struct se_lun *lun,
730 const char *page,
731 size_t count)
732{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700733 if (!lun || !lun->lun_se_dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800734 return -ENODEV;
735
736 return core_alua_store_secondary_write_metadata(lun, page, count);
737}
738
739TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
740
741
742static struct configfs_attribute *target_fabric_port_attrs[] = {
743 &target_fabric_port_alua_tg_pt_gp.attr,
744 &target_fabric_port_alua_tg_pt_offline.attr,
745 &target_fabric_port_alua_tg_pt_status.attr,
746 &target_fabric_port_alua_tg_pt_write_md.attr,
747 NULL,
748};
749
750CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
751
752static int target_fabric_port_link(
753 struct config_item *lun_ci,
754 struct config_item *se_dev_ci)
755{
756 struct config_item *tpg_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757 struct se_lun *lun = container_of(to_config_group(lun_ci),
758 struct se_lun, lun_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759 struct se_portal_group *se_tpg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400760 struct se_device *dev =
761 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762 struct target_fabric_configfs *tf;
763 int ret;
764
Nicholas Bellinger0ff87542012-12-04 23:43:57 -0800765 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
766 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
767 " %p to struct se_device: %p\n", se_dev_ci, dev);
768 return -EFAULT;
769 }
770
Nicholas Bellingerfaa06ab2013-01-31 14:56:12 -0800771 if (!(dev->dev_flags & DF_CONFIGURED)) {
772 pr_err("se_device not configured yet, cannot port link\n");
773 return -ENODEV;
774 }
775
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
777 se_tpg = container_of(to_config_group(tpg_ci),
778 struct se_portal_group, tpg_group);
779 tf = se_tpg->se_tpg_wwn->wwn_tf;
780
781 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700782 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800783 return -EEXIST;
784 }
785
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700786 ret = core_dev_add_lun(se_tpg, dev, lun);
787 if (ret) {
788 pr_err("core_dev_add_lun() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789 goto out;
790 }
791
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200792 if (tf->tf_ops->fabric_post_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800793 /*
794 * Call the optional fabric_post_link() to allow a
795 * fabric module to setup any additional state once
796 * core_dev_add_lun() has been called..
797 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200798 tf->tf_ops->fabric_post_link(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 }
800
801 return 0;
802out:
803 return ret;
804}
805
806static int target_fabric_port_unlink(
807 struct config_item *lun_ci,
808 struct config_item *se_dev_ci)
809{
810 struct se_lun *lun = container_of(to_config_group(lun_ci),
811 struct se_lun, lun_group);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700812 struct se_portal_group *se_tpg = lun->lun_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
814
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200815 if (tf->tf_ops->fabric_pre_unlink) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816 /*
817 * Call the optional fabric_pre_unlink() to allow a
818 * fabric module to release any additional stat before
819 * core_dev_del_lun() is called.
820 */
Christoph Hellwigef0caf82015-05-03 08:50:53 +0200821 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822 }
823
Andy Grovercd9d7cb2014-06-30 16:39:44 -0700824 core_dev_del_lun(se_tpg, lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800825 return 0;
826}
827
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700828static void target_fabric_port_release(struct config_item *item)
829{
830 struct se_lun *lun = container_of(to_config_group(item),
831 struct se_lun, lun_group);
832
833 kfree_rcu(lun, rcu_head);
834}
835
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836static struct configfs_item_operations target_fabric_port_item_ops = {
837 .show_attribute = target_fabric_port_attr_show,
838 .store_attribute = target_fabric_port_attr_store,
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700839 .release = target_fabric_port_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 .allow_link = target_fabric_port_link,
841 .drop_link = target_fabric_port_unlink,
842};
843
844TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
845
846/* End of tfc_tpg_port_cit */
847
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700848/* Start of tfc_tpg_port_stat_cit */
849
850static struct config_group *target_core_port_stat_mkdir(
851 struct config_group *group,
852 const char *name)
853{
854 return ERR_PTR(-ENOSYS);
855}
856
857static void target_core_port_stat_rmdir(
858 struct config_group *group,
859 struct config_item *item)
860{
861 return;
862}
863
864static struct configfs_group_operations target_fabric_port_stat_group_ops = {
865 .make_group = target_core_port_stat_mkdir,
866 .drop_item = target_core_port_stat_rmdir,
867};
868
869TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
870
871/* End of tfc_tpg_port_stat_cit */
872
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873/* Start of tfc_tpg_lun_cit */
874
875static struct config_group *target_fabric_make_lun(
876 struct config_group *group,
877 const char *name)
878{
879 struct se_lun *lun;
880 struct se_portal_group *se_tpg = container_of(group,
881 struct se_portal_group, tpg_lun_group);
882 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700883 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800884 unsigned long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700885 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886
887 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700888 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889 " \"lun_$LUN_NUMBER\"\n");
890 return ERR_PTR(-EINVAL);
891 }
Jingoo Han57103d72013-07-19 16:22:19 +0900892 errno = kstrtoul(name + 4, 0, &unpacked_lun);
893 if (errno)
894 return ERR_PTR(errno);
895 if (unpacked_lun > UINT_MAX)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896 return ERR_PTR(-EINVAL);
897
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700898 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
899 if (IS_ERR(lun))
900 return ERR_CAST(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700902 lun_cg = &lun->lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100903 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700904 GFP_KERNEL);
905 if (!lun_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700906 pr_err("Unable to allocate lun_cg->default_groups\n");
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700907 kfree(lun);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700908 return ERR_PTR(-ENOMEM);
909 }
910
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 config_group_init_type_name(&lun->lun_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200912 &tf->tf_tpg_port_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700913 config_group_init_type_name(&lun->port_stat_grps.stat_group,
Christoph Hellwig968ebe72015-05-03 08:50:55 +0200914 "statistics", &tf->tf_tpg_port_stat_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700915 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
916 lun_cg->default_groups[1] = NULL;
917
Andy Grovere3d6f902011-07-19 08:55:10 +0000918 port_stat_grp = &lun->port_stat_grps.stat_group;
Andy Groverab6dae82013-12-09 14:27:36 -0800919 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700920 GFP_KERNEL);
921 if (!port_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700922 pr_err("Unable to allocate port_stat_grp->default_groups\n");
Joern Engel1481473b2014-09-17 15:11:28 -0700923 kfree(lun_cg->default_groups);
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700924 kfree(lun);
Joern Engel1481473b2014-09-17 15:11:28 -0700925 return ERR_PTR(-ENOMEM);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700926 }
927 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800928
929 return &lun->lun_group;
930}
931
932static void target_fabric_drop_lun(
933 struct config_group *group,
934 struct config_item *item)
935{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700936 struct se_lun *lun = container_of(to_config_group(item),
937 struct se_lun, lun_group);
938 struct config_item *df_item;
939 struct config_group *lun_cg, *port_stat_grp;
940 int i;
941
Andy Grovere3d6f902011-07-19 08:55:10 +0000942 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700943 for (i = 0; port_stat_grp->default_groups[i]; i++) {
944 df_item = &port_stat_grp->default_groups[i]->cg_item;
945 port_stat_grp->default_groups[i] = NULL;
946 config_item_put(df_item);
947 }
948 kfree(port_stat_grp->default_groups);
949
950 lun_cg = &lun->lun_group;
951 for (i = 0; lun_cg->default_groups[i]; i++) {
952 df_item = &lun_cg->default_groups[i]->cg_item;
953 lun_cg->default_groups[i] = NULL;
954 config_item_put(df_item);
955 }
956 kfree(lun_cg->default_groups);
957
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 config_item_put(item);
959}
960
961static struct configfs_group_operations target_fabric_lun_group_ops = {
962 .make_group = &target_fabric_make_lun,
963 .drop_item = &target_fabric_drop_lun,
964};
965
966TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
967
968/* End of tfc_tpg_lun_cit */
969
970/* Start of tfc_tpg_attrib_cit */
971
972CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
973
974static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
975 .show_attribute = target_fabric_tpg_attrib_attr_show,
976 .store_attribute = target_fabric_tpg_attrib_attr_store,
977};
978
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200979TF_CIT_SETUP_DRV(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800980
981/* End of tfc_tpg_attrib_cit */
982
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700983/* Start of tfc_tpg_auth_cit */
984
985CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
986
987static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
988 .show_attribute = target_fabric_tpg_auth_attr_show,
989 .store_attribute = target_fabric_tpg_auth_attr_store,
990};
991
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200992TF_CIT_SETUP_DRV(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700993
994/* End of tfc_tpg_attrib_cit */
995
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800996/* Start of tfc_tpg_param_cit */
997
998CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
999
1000static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
1001 .show_attribute = target_fabric_tpg_param_attr_show,
1002 .store_attribute = target_fabric_tpg_param_attr_store,
1003};
1004
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001005TF_CIT_SETUP_DRV(tpg_param, &target_fabric_tpg_param_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001006
1007/* End of tfc_tpg_param_cit */
1008
1009/* Start of tfc_tpg_base_cit */
1010/*
1011 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1012 */
1013CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1014
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001015static void target_fabric_tpg_release(struct config_item *item)
1016{
1017 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1018 struct se_portal_group, tpg_group);
1019 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1020 struct target_fabric_configfs *tf = wwn->wwn_tf;
1021
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001022 tf->tf_ops->fabric_drop_tpg(se_tpg);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001023}
1024
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001025static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001026 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001027 .show_attribute = target_fabric_tpg_attr_show,
1028 .store_attribute = target_fabric_tpg_attr_store,
1029};
1030
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001031TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032
1033/* End of tfc_tpg_base_cit */
1034
1035/* Start of tfc_tpg_cit */
1036
1037static struct config_group *target_fabric_make_tpg(
1038 struct config_group *group,
1039 const char *name)
1040{
1041 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1042 struct target_fabric_configfs *tf = wwn->wwn_tf;
1043 struct se_portal_group *se_tpg;
1044
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001045 if (!tf->tf_ops->fabric_make_tpg) {
1046 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047 return ERR_PTR(-ENOSYS);
1048 }
1049
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001050 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001051 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001052 return ERR_PTR(-EINVAL);
1053 /*
1054 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1055 */
1056 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1057 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1058 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1059 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1060 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001061 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1062 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1063 se_tpg->tpg_group.default_groups[6] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064
1065 config_group_init_type_name(&se_tpg->tpg_group, name,
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001066 &tf->tf_tpg_base_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001067 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001068 &tf->tf_tpg_lun_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001070 &tf->tf_tpg_np_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001072 &tf->tf_tpg_nacl_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001074 &tf->tf_tpg_attrib_cit);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001075 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001076 &tf->tf_tpg_auth_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001078 &tf->tf_tpg_param_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001079
1080 return &se_tpg->tpg_group;
1081}
1082
1083static void target_fabric_drop_tpg(
1084 struct config_group *group,
1085 struct config_item *item)
1086{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001087 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1088 struct se_portal_group, tpg_group);
1089 struct config_group *tpg_cg = &se_tpg->tpg_group;
1090 struct config_item *df_item;
1091 int i;
1092 /*
1093 * Release default groups, but do not release tpg_cg->default_groups
1094 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1095 */
1096 for (i = 0; tpg_cg->default_groups[i]; i++) {
1097 df_item = &tpg_cg->default_groups[i]->cg_item;
1098 tpg_cg->default_groups[i] = NULL;
1099 config_item_put(df_item);
1100 }
1101
1102 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103}
1104
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001105static void target_fabric_release_wwn(struct config_item *item)
1106{
1107 struct se_wwn *wwn = container_of(to_config_group(item),
1108 struct se_wwn, wwn_group);
1109 struct target_fabric_configfs *tf = wwn->wwn_tf;
1110
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001111 tf->tf_ops->fabric_drop_wwn(wwn);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001112}
1113
1114static struct configfs_item_operations target_fabric_tpg_item_ops = {
1115 .release = target_fabric_release_wwn,
1116};
1117
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118static struct configfs_group_operations target_fabric_tpg_group_ops = {
1119 .make_group = target_fabric_make_tpg,
1120 .drop_item = target_fabric_drop_tpg,
1121};
1122
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001123TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1124 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001125
1126/* End of tfc_tpg_cit */
1127
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001128/* Start of tfc_wwn_fabric_stats_cit */
1129/*
1130 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1131 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1132 */
1133TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1134
1135/* End of tfc_wwn_fabric_stats_cit */
1136
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001137/* Start of tfc_wwn_cit */
1138
1139static struct config_group *target_fabric_make_wwn(
1140 struct config_group *group,
1141 const char *name)
1142{
1143 struct target_fabric_configfs *tf = container_of(group,
1144 struct target_fabric_configfs, tf_group);
1145 struct se_wwn *wwn;
1146
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001147 if (!tf->tf_ops->fabric_make_wwn) {
Andy Grover6708bb22011-06-08 10:36:43 -07001148 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149 return ERR_PTR(-ENOSYS);
1150 }
1151
Christoph Hellwigef0caf82015-05-03 08:50:53 +02001152 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001153 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 return ERR_PTR(-EINVAL);
1155
1156 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001157 /*
1158 * Setup default groups from pre-allocated wwn->wwn_default_groups
1159 */
1160 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1161 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1162 wwn->wwn_group.default_groups[1] = NULL;
1163
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001164 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001165 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
Christoph Hellwig968ebe72015-05-03 08:50:55 +02001166 &tf->tf_wwn_fabric_stats_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167
1168 return &wwn->wwn_group;
1169}
1170
1171static void target_fabric_drop_wwn(
1172 struct config_group *group,
1173 struct config_item *item)
1174{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001175 struct se_wwn *wwn = container_of(to_config_group(item),
1176 struct se_wwn, wwn_group);
1177 struct config_item *df_item;
1178 struct config_group *cg = &wwn->wwn_group;
1179 int i;
1180
1181 for (i = 0; cg->default_groups[i]; i++) {
1182 df_item = &cg->default_groups[i]->cg_item;
1183 cg->default_groups[i] = NULL;
1184 config_item_put(df_item);
1185 }
1186
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188}
1189
1190static struct configfs_group_operations target_fabric_wwn_group_ops = {
1191 .make_group = target_fabric_make_wwn,
1192 .drop_item = target_fabric_drop_wwn,
1193};
1194/*
1195 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1196 */
1197CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1198
1199static struct configfs_item_operations target_fabric_wwn_item_ops = {
1200 .show_attribute = target_fabric_wwn_attr_show,
1201 .store_attribute = target_fabric_wwn_attr_store,
1202};
1203
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001204TF_CIT_SETUP_DRV(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205
1206/* End of tfc_wwn_cit */
1207
1208/* Start of tfc_discovery_cit */
1209
1210CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1211 tf_disc_group);
1212
1213static struct configfs_item_operations target_fabric_discovery_item_ops = {
1214 .show_attribute = target_fabric_discovery_attr_show,
1215 .store_attribute = target_fabric_discovery_attr_store,
1216};
1217
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001218TF_CIT_SETUP_DRV(discovery, &target_fabric_discovery_item_ops, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001219
1220/* End of tfc_discovery_cit */
1221
1222int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1223{
1224 target_fabric_setup_discovery_cit(tf);
1225 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001226 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 target_fabric_setup_tpg_cit(tf);
1228 target_fabric_setup_tpg_base_cit(tf);
1229 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001230 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 target_fabric_setup_tpg_lun_cit(tf);
1232 target_fabric_setup_tpg_np_cit(tf);
1233 target_fabric_setup_tpg_np_base_cit(tf);
1234 target_fabric_setup_tpg_attrib_cit(tf);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001235 target_fabric_setup_tpg_auth_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 target_fabric_setup_tpg_param_cit(tf);
1237 target_fabric_setup_tpg_nacl_cit(tf);
1238 target_fabric_setup_tpg_nacl_base_cit(tf);
1239 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1240 target_fabric_setup_tpg_nacl_auth_cit(tf);
1241 target_fabric_setup_tpg_nacl_param_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001242 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001244 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245
1246 return 0;
1247}