blob: 3503996d7d10f667c4328c20ce8101e82b2b5c8e [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{ \
49 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
50 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
51 \
52 cit->ct_item_ops = _item_ops; \
53 cit->ct_group_ops = _group_ops; \
54 cit->ct_attrs = _attrs; \
55 cit->ct_owner = tf->tf_module; \
Andy Grover6708bb22011-06-08 10:36:43 -070056 pr_debug("Setup generic %s\n", __stringify(_name)); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057}
58
59/* Start of tfc_tpg_mappedlun_cit */
60
61static int target_fabric_mappedlun_link(
62 struct config_item *lun_acl_ci,
63 struct config_item *lun_ci)
64{
65 struct se_dev_entry *deve;
66 struct se_lun *lun = container_of(to_config_group(lun_ci),
67 struct se_lun, lun_group);
68 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
69 struct se_lun_acl, se_lun_group);
70 struct se_portal_group *se_tpg;
71 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
72 int ret = 0, lun_access;
Nicholas Bellinger0ff87542012-12-04 23:43:57 -080073
74 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
75 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
76 " %p to struct lun: %p\n", lun_ci, lun);
77 return -EFAULT;
78 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079 /*
80 * Ensure that the source port exists
81 */
Andy Grover6708bb22011-06-08 10:36:43 -070082 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
83 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084 "_tpg does not exist\n");
85 return -EINVAL;
86 }
87 se_tpg = lun->lun_sep->sep_tpg;
88
89 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
90 tpg_ci = &nacl_ci->ci_group->cg_item;
91 wwn_ci = &tpg_ci->ci_group->cg_item;
92 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
93 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
94 /*
95 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
96 */
97 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -070098 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 config_item_name(wwn_ci));
100 return -EINVAL;
101 }
102 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700103 pr_err("Illegal Initiator ACL Symlink outside of %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 " TPGT: %s\n", config_item_name(wwn_ci),
105 config_item_name(tpg_ci));
106 return -EINVAL;
107 }
108 /*
109 * If this struct se_node_acl was dynamically generated with
110 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
111 * which be will write protected (READ-ONLY) when
112 * tpg_1/attrib/demo_mode_write_protect=1
113 */
114 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
Jörn Engelf2083242012-03-15 15:05:40 -0400115 deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800116 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
117 lun_access = deve->lun_flags;
118 else
119 lun_access =
Andy Grovere3d6f902011-07-19 08:55:10 +0000120 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800121 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
122 TRANSPORT_LUNFLAGS_READ_WRITE;
123 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
124 /*
125 * Determine the actual mapped LUN value user wants..
126 *
127 * This value is what the SCSI Initiator actually sees the
128 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
129 */
130 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
131 lun->unpacked_lun, lun_access);
132
133 return (ret < 0) ? -EINVAL : 0;
134}
135
136static int target_fabric_mappedlun_unlink(
137 struct config_item *lun_acl_ci,
138 struct config_item *lun_ci)
139{
140 struct se_lun *lun;
141 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
142 struct se_lun_acl, se_lun_group);
143 struct se_node_acl *nacl = lacl->se_lun_nacl;
Jörn Engelf2083242012-03-15 15:05:40 -0400144 struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800145 struct se_portal_group *se_tpg;
146 /*
147 * Determine if the underlying MappedLUN has already been released..
148 */
Andy Grover6708bb22011-06-08 10:36:43 -0700149 if (!deve->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800150 return 0;
151
152 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
153 se_tpg = lun->lun_sep->sep_tpg;
154
155 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
156 return 0;
157}
158
159CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
160#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
161static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
162 __CONFIGFS_EATTR(_name, _mode, \
163 target_fabric_mappedlun_show_##_name, \
164 target_fabric_mappedlun_store_##_name);
165
166static ssize_t target_fabric_mappedlun_show_write_protect(
167 struct se_lun_acl *lacl,
168 char *page)
169{
170 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
171 struct se_dev_entry *deve;
172 ssize_t len;
173
174 spin_lock_irq(&se_nacl->device_list_lock);
Jörn Engelf2083242012-03-15 15:05:40 -0400175 deve = se_nacl->device_list[lacl->mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176 len = sprintf(page, "%d\n",
177 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
178 1 : 0);
179 spin_unlock_irq(&se_nacl->device_list_lock);
180
181 return len;
182}
183
184static ssize_t target_fabric_mappedlun_store_write_protect(
185 struct se_lun_acl *lacl,
186 const char *page,
187 size_t count)
188{
189 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
190 struct se_portal_group *se_tpg = se_nacl->se_tpg;
191 unsigned long op;
Jingoo Han57103d72013-07-19 16:22:19 +0900192 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193
Jingoo Han57103d72013-07-19 16:22:19 +0900194 ret = kstrtoul(page, 0, &op);
195 if (ret)
196 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197
198 if ((op != 1) && (op != 0))
199 return -EINVAL;
200
201 core_update_device_list_access(lacl->mapped_lun, (op) ?
202 TRANSPORT_LUNFLAGS_READ_ONLY :
203 TRANSPORT_LUNFLAGS_READ_WRITE,
204 lacl->se_lun_nacl);
205
Andy Grover6708bb22011-06-08 10:36:43 -0700206 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207 " Mapped LUN: %u Write Protect bit to %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000208 se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800209 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
210
211 return count;
212
213}
214
215TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
216
217CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
218
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800219static void target_fabric_mappedlun_release(struct config_item *item)
220{
221 struct se_lun_acl *lacl = container_of(to_config_group(item),
222 struct se_lun_acl, se_lun_group);
223 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
224
225 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
226}
227
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
229 &target_fabric_mappedlun_write_protect.attr,
230 NULL,
231};
232
233static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800234 .release = target_fabric_mappedlun_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800235 .show_attribute = target_fabric_mappedlun_attr_show,
236 .store_attribute = target_fabric_mappedlun_attr_store,
237 .allow_link = target_fabric_mappedlun_link,
238 .drop_link = target_fabric_mappedlun_unlink,
239};
240
241TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
242 target_fabric_mappedlun_attrs);
243
244/* End of tfc_tpg_mappedlun_cit */
245
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700246/* Start of tfc_tpg_mappedlun_port_cit */
247
248static struct config_group *target_core_mappedlun_stat_mkdir(
249 struct config_group *group,
250 const char *name)
251{
252 return ERR_PTR(-ENOSYS);
253}
254
255static void target_core_mappedlun_stat_rmdir(
256 struct config_group *group,
257 struct config_item *item)
258{
259 return;
260}
261
262static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
263 .make_group = target_core_mappedlun_stat_mkdir,
264 .drop_item = target_core_mappedlun_stat_rmdir,
265};
266
267TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
268 NULL);
269
270/* End of tfc_tpg_mappedlun_port_cit */
271
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800272/* Start of tfc_tpg_nacl_attrib_cit */
273
274CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
275
276static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
277 .show_attribute = target_fabric_nacl_attrib_attr_show,
278 .store_attribute = target_fabric_nacl_attrib_attr_store,
279};
280
281TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
282
283/* End of tfc_tpg_nacl_attrib_cit */
284
285/* Start of tfc_tpg_nacl_auth_cit */
286
287CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
288
289static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
290 .show_attribute = target_fabric_nacl_auth_attr_show,
291 .store_attribute = target_fabric_nacl_auth_attr_store,
292};
293
294TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
295
296/* End of tfc_tpg_nacl_auth_cit */
297
298/* Start of tfc_tpg_nacl_param_cit */
299
300CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
301
302static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
303 .show_attribute = target_fabric_nacl_param_attr_show,
304 .store_attribute = target_fabric_nacl_param_attr_store,
305};
306
307TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
308
309/* End of tfc_tpg_nacl_param_cit */
310
311/* Start of tfc_tpg_nacl_base_cit */
312
313CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
314
315static struct config_group *target_fabric_make_mappedlun(
316 struct config_group *group,
317 const char *name)
318{
319 struct se_node_acl *se_nacl = container_of(group,
320 struct se_node_acl, acl_group);
321 struct se_portal_group *se_tpg = se_nacl->se_tpg;
322 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
323 struct se_lun_acl *lacl;
324 struct config_item *acl_ci;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700325 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 char *buf;
327 unsigned long mapped_lun;
328 int ret = 0;
329
330 acl_ci = &group->cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700331 if (!acl_ci) {
332 pr_err("Unable to locatel acl_ci\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800333 return NULL;
334 }
335
336 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700337 if (!buf) {
338 pr_err("Unable to allocate memory for name buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 return ERR_PTR(-ENOMEM);
340 }
341 snprintf(buf, strlen(name) + 1, "%s", name);
342 /*
343 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
344 */
345 if (strstr(buf, "lun_") != buf) {
Andy Grover6708bb22011-06-08 10:36:43 -0700346 pr_err("Unable to locate \"lun_\" from buf: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347 " name: %s\n", buf, name);
348 ret = -EINVAL;
349 goto out;
350 }
351 /*
352 * Determine the Mapped LUN value. This is what the SCSI Initiator
353 * Port will actually see.
354 */
Jingoo Han57103d72013-07-19 16:22:19 +0900355 ret = kstrtoul(buf + 4, 0, &mapped_lun);
356 if (ret)
357 goto out;
358 if (mapped_lun > UINT_MAX) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800359 ret = -EINVAL;
360 goto out;
361 }
Nicholas Bellingerfbbf8552013-02-18 18:31:37 -0800362 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
363 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
364 "-1: %u for Target Portal Group: %u\n", mapped_lun,
365 TRANSPORT_MAX_LUNS_PER_TPG-1,
366 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
367 ret = -EINVAL;
368 goto out;
369 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370
Nicholas Bellingerfcf29482013-02-18 18:00:33 -0800371 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
372 mapped_lun, &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700373 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700374 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700376 }
377
378 lacl_cg = &lacl->se_lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100379 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700380 GFP_KERNEL);
381 if (!lacl_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700382 pr_err("Unable to allocate lacl_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700383 ret = -ENOMEM;
384 goto out;
385 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386
387 config_group_init_type_name(&lacl->se_lun_group, name,
388 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700389 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
390 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
391 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
392 lacl_cg->default_groups[1] = NULL;
393
Andy Grovere3d6f902011-07-19 08:55:10 +0000394 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100395 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700396 GFP_KERNEL);
397 if (!ml_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700398 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700399 ret = -ENOMEM;
400 goto out;
401 }
402 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800403
404 kfree(buf);
405 return &lacl->se_lun_group;
406out:
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700407 if (lacl_cg)
408 kfree(lacl_cg->default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 kfree(buf);
410 return ERR_PTR(ret);
411}
412
413static void target_fabric_drop_mappedlun(
414 struct config_group *group,
415 struct config_item *item)
416{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700417 struct se_lun_acl *lacl = container_of(to_config_group(item),
418 struct se_lun_acl, se_lun_group);
419 struct config_item *df_item;
420 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
421 int i;
422
Andy Grovere3d6f902011-07-19 08:55:10 +0000423 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700424 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
425 df_item = &ml_stat_grp->default_groups[i]->cg_item;
426 ml_stat_grp->default_groups[i] = NULL;
427 config_item_put(df_item);
428 }
429 kfree(ml_stat_grp->default_groups);
430
431 lacl_cg = &lacl->se_lun_group;
432 for (i = 0; lacl_cg->default_groups[i]; i++) {
433 df_item = &lacl_cg->default_groups[i]->cg_item;
434 lacl_cg->default_groups[i] = NULL;
435 config_item_put(df_item);
436 }
437 kfree(lacl_cg->default_groups);
438
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800440}
441
442static void target_fabric_nacl_base_release(struct config_item *item)
443{
444 struct se_node_acl *se_nacl = container_of(to_config_group(item),
445 struct se_node_acl, acl_group);
446 struct se_portal_group *se_tpg = se_nacl->se_tpg;
447 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
448
449 tf->tf_ops.fabric_drop_nodeacl(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
463TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
464 &target_fabric_nacl_base_group_ops, NULL);
465
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
Andy Grover6708bb22011-06-08 10:36:43 -0700489 if (!tf->tf_ops.fabric_make_nodeacl) {
490 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 return ERR_PTR(-ENOSYS);
492 }
493
494 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
495 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200496 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497
498 nacl_cg = &se_nacl->acl_group;
499 nacl_cg->default_groups = se_nacl->acl_default_groups;
500 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
501 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
502 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700503 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
504 nacl_cg->default_groups[4] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505
506 config_group_init_type_name(&se_nacl->acl_group, name,
507 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
508 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
509 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
510 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
511 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
512 config_group_init_type_name(&se_nacl->acl_param_group, "param",
513 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700514 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
515 "fabric_statistics",
516 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517
518 return &se_nacl->acl_group;
519}
520
521static void target_fabric_drop_nodeacl(
522 struct config_group *group,
523 struct config_item *item)
524{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 struct se_node_acl *se_nacl = container_of(to_config_group(item),
526 struct se_node_acl, acl_group);
527 struct config_item *df_item;
528 struct config_group *nacl_cg;
529 int i;
530
531 nacl_cg = &se_nacl->acl_group;
532 for (i = 0; nacl_cg->default_groups[i]; i++) {
533 df_item = &nacl_cg->default_groups[i]->cg_item;
534 nacl_cg->default_groups[i] = NULL;
535 config_item_put(df_item);
536 }
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800537 /*
538 * struct se_node_acl free is done in target_fabric_nacl_base_release()
539 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541}
542
543static struct configfs_group_operations target_fabric_nacl_group_ops = {
544 .make_group = target_fabric_make_nodeacl,
545 .drop_item = target_fabric_drop_nodeacl,
546};
547
548TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
549
550/* End of tfc_tpg_nacl_cit */
551
552/* Start of tfc_tpg_np_base_cit */
553
554CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
555
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800556static void target_fabric_np_base_release(struct config_item *item)
557{
558 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
559 struct se_tpg_np, tpg_np_group);
560 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
561 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
562
563 tf->tf_ops.fabric_drop_np(se_tpg_np);
564}
565
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800567 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800568 .show_attribute = target_fabric_np_base_attr_show,
569 .store_attribute = target_fabric_np_base_attr_store,
570};
571
572TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
573
574/* End of tfc_tpg_np_base_cit */
575
576/* Start of tfc_tpg_np_cit */
577
578static struct config_group *target_fabric_make_np(
579 struct config_group *group,
580 const char *name)
581{
582 struct se_portal_group *se_tpg = container_of(group,
583 struct se_portal_group, tpg_np_group);
584 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
585 struct se_tpg_np *se_tpg_np;
586
Andy Grover6708bb22011-06-08 10:36:43 -0700587 if (!tf->tf_ops.fabric_make_np) {
588 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800589 return ERR_PTR(-ENOSYS);
590 }
591
592 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700593 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800594 return ERR_PTR(-EINVAL);
595
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800596 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
598 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
599
600 return &se_tpg_np->tpg_np_group;
601}
602
603static void target_fabric_drop_np(
604 struct config_group *group,
605 struct config_item *item)
606{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800607 /*
608 * struct se_tpg_np is released via target_fabric_np_base_release()
609 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611}
612
613static struct configfs_group_operations target_fabric_np_group_ops = {
614 .make_group = &target_fabric_make_np,
615 .drop_item = &target_fabric_drop_np,
616};
617
618TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
619
620/* End of tfc_tpg_np_cit */
621
622/* Start of tfc_tpg_port_cit */
623
624CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
625#define TCM_PORT_ATTR(_name, _mode) \
626static struct target_fabric_port_attribute target_fabric_port_##_name = \
627 __CONFIGFS_EATTR(_name, _mode, \
628 target_fabric_port_show_attr_##_name, \
629 target_fabric_port_store_attr_##_name);
630
631#define TCM_PORT_ATTOR_RO(_name) \
632 __CONFIGFS_EATTR_RO(_name, \
633 target_fabric_port_show_attr_##_name);
634
635/*
636 * alua_tg_pt_gp
637 */
638static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
639 struct se_lun *lun,
640 char *page)
641{
Andy Grover6708bb22011-06-08 10:36:43 -0700642 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 return -ENODEV;
644
645 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
646}
647
648static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
649 struct se_lun *lun,
650 const char *page,
651 size_t count)
652{
Andy Grover6708bb22011-06-08 10:36:43 -0700653 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654 return -ENODEV;
655
656 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
657}
658
659TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
660
661/*
662 * alua_tg_pt_offline
663 */
664static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
665 struct se_lun *lun,
666 char *page)
667{
Andy Grover6708bb22011-06-08 10:36:43 -0700668 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669 return -ENODEV;
670
671 return core_alua_show_offline_bit(lun, page);
672}
673
674static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
675 struct se_lun *lun,
676 const char *page,
677 size_t count)
678{
Andy Grover6708bb22011-06-08 10:36:43 -0700679 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680 return -ENODEV;
681
682 return core_alua_store_offline_bit(lun, page, count);
683}
684
685TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
686
687/*
688 * alua_tg_pt_status
689 */
690static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
691 struct se_lun *lun,
692 char *page)
693{
Andy Grover6708bb22011-06-08 10:36:43 -0700694 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 return -ENODEV;
696
697 return core_alua_show_secondary_status(lun, page);
698}
699
700static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
701 struct se_lun *lun,
702 const char *page,
703 size_t count)
704{
Andy Grover6708bb22011-06-08 10:36:43 -0700705 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800706 return -ENODEV;
707
708 return core_alua_store_secondary_status(lun, page, count);
709}
710
711TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
712
713/*
714 * alua_tg_pt_write_md
715 */
716static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
717 struct se_lun *lun,
718 char *page)
719{
Andy Grover6708bb22011-06-08 10:36:43 -0700720 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800721 return -ENODEV;
722
723 return core_alua_show_secondary_write_metadata(lun, page);
724}
725
726static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
727 struct se_lun *lun,
728 const char *page,
729 size_t count)
730{
Andy Grover6708bb22011-06-08 10:36:43 -0700731 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800732 return -ENODEV;
733
734 return core_alua_store_secondary_write_metadata(lun, page, count);
735}
736
737TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
738
739
740static struct configfs_attribute *target_fabric_port_attrs[] = {
741 &target_fabric_port_alua_tg_pt_gp.attr,
742 &target_fabric_port_alua_tg_pt_offline.attr,
743 &target_fabric_port_alua_tg_pt_status.attr,
744 &target_fabric_port_alua_tg_pt_write_md.attr,
745 NULL,
746};
747
748CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
749
750static int target_fabric_port_link(
751 struct config_item *lun_ci,
752 struct config_item *se_dev_ci)
753{
754 struct config_item *tpg_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755 struct se_lun *lun = container_of(to_config_group(lun_ci),
756 struct se_lun, lun_group);
757 struct se_lun *lun_p;
758 struct se_portal_group *se_tpg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400759 struct se_device *dev =
760 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761 struct target_fabric_configfs *tf;
762 int ret;
763
Nicholas Bellinger0ff87542012-12-04 23:43:57 -0800764 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
765 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
766 " %p to struct se_device: %p\n", se_dev_ci, dev);
767 return -EFAULT;
768 }
769
Nicholas Bellingerfaa06ab2013-01-31 14:56:12 -0800770 if (!(dev->dev_flags & DF_CONFIGURED)) {
771 pr_err("se_device not configured yet, cannot port link\n");
772 return -ENODEV;
773 }
774
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800775 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
776 se_tpg = container_of(to_config_group(tpg_ci),
777 struct se_portal_group, tpg_group);
778 tf = se_tpg->se_tpg_wwn->wwn_tf;
779
780 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700781 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800782 return -EEXIST;
783 }
784
Andy Grover2dca6732012-07-12 17:34:55 -0700785 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +0100786 if (IS_ERR(lun_p)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700787 pr_err("core_dev_add_lun() failed\n");
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +0100788 ret = PTR_ERR(lun_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789 goto out;
790 }
791
792 if (tf->tf_ops.fabric_post_link) {
793 /*
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 */
798 tf->tf_ops.fabric_post_link(se_tpg, lun);
799 }
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);
812 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
814
815 if (tf->tf_ops.fabric_pre_unlink) {
816 /*
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 */
821 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
822 }
823
824 core_dev_del_lun(se_tpg, lun->unpacked_lun);
825 return 0;
826}
827
828static struct configfs_item_operations target_fabric_port_item_ops = {
829 .show_attribute = target_fabric_port_attr_show,
830 .store_attribute = target_fabric_port_attr_store,
831 .allow_link = target_fabric_port_link,
832 .drop_link = target_fabric_port_unlink,
833};
834
835TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
836
837/* End of tfc_tpg_port_cit */
838
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700839/* Start of tfc_tpg_port_stat_cit */
840
841static struct config_group *target_core_port_stat_mkdir(
842 struct config_group *group,
843 const char *name)
844{
845 return ERR_PTR(-ENOSYS);
846}
847
848static void target_core_port_stat_rmdir(
849 struct config_group *group,
850 struct config_item *item)
851{
852 return;
853}
854
855static struct configfs_group_operations target_fabric_port_stat_group_ops = {
856 .make_group = target_core_port_stat_mkdir,
857 .drop_item = target_core_port_stat_rmdir,
858};
859
860TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
861
862/* End of tfc_tpg_port_stat_cit */
863
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800864/* Start of tfc_tpg_lun_cit */
865
866static struct config_group *target_fabric_make_lun(
867 struct config_group *group,
868 const char *name)
869{
870 struct se_lun *lun;
871 struct se_portal_group *se_tpg = container_of(group,
872 struct se_portal_group, tpg_lun_group);
873 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700874 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800875 unsigned long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700876 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800877
878 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700879 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880 " \"lun_$LUN_NUMBER\"\n");
881 return ERR_PTR(-EINVAL);
882 }
Jingoo Han57103d72013-07-19 16:22:19 +0900883 errno = kstrtoul(name + 4, 0, &unpacked_lun);
884 if (errno)
885 return ERR_PTR(errno);
886 if (unpacked_lun > UINT_MAX)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887 return ERR_PTR(-EINVAL);
888
889 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -0700890 if (!lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891 return ERR_PTR(-EINVAL);
892
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700893 lun_cg = &lun->lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100894 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700895 GFP_KERNEL);
896 if (!lun_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700897 pr_err("Unable to allocate lun_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700898 return ERR_PTR(-ENOMEM);
899 }
900
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901 config_group_init_type_name(&lun->lun_group, name,
902 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700903 config_group_init_type_name(&lun->port_stat_grps.stat_group,
904 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
905 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
906 lun_cg->default_groups[1] = NULL;
907
Andy Grovere3d6f902011-07-19 08:55:10 +0000908 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700909 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
910 GFP_KERNEL);
911 if (!port_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700912 pr_err("Unable to allocate port_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700913 errno = -ENOMEM;
914 goto out;
915 }
916 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800917
918 return &lun->lun_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700919out:
920 if (lun_cg)
921 kfree(lun_cg->default_groups);
922 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800923}
924
925static void target_fabric_drop_lun(
926 struct config_group *group,
927 struct config_item *item)
928{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700929 struct se_lun *lun = container_of(to_config_group(item),
930 struct se_lun, lun_group);
931 struct config_item *df_item;
932 struct config_group *lun_cg, *port_stat_grp;
933 int i;
934
Andy Grovere3d6f902011-07-19 08:55:10 +0000935 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700936 for (i = 0; port_stat_grp->default_groups[i]; i++) {
937 df_item = &port_stat_grp->default_groups[i]->cg_item;
938 port_stat_grp->default_groups[i] = NULL;
939 config_item_put(df_item);
940 }
941 kfree(port_stat_grp->default_groups);
942
943 lun_cg = &lun->lun_group;
944 for (i = 0; lun_cg->default_groups[i]; i++) {
945 df_item = &lun_cg->default_groups[i]->cg_item;
946 lun_cg->default_groups[i] = NULL;
947 config_item_put(df_item);
948 }
949 kfree(lun_cg->default_groups);
950
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951 config_item_put(item);
952}
953
954static struct configfs_group_operations target_fabric_lun_group_ops = {
955 .make_group = &target_fabric_make_lun,
956 .drop_item = &target_fabric_drop_lun,
957};
958
959TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
960
961/* End of tfc_tpg_lun_cit */
962
963/* Start of tfc_tpg_attrib_cit */
964
965CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
966
967static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
968 .show_attribute = target_fabric_tpg_attrib_attr_show,
969 .store_attribute = target_fabric_tpg_attrib_attr_store,
970};
971
972TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
973
974/* End of tfc_tpg_attrib_cit */
975
Nicholas Bellingere4b512e2013-06-19 18:37:00 -0700976/* Start of tfc_tpg_auth_cit */
977
978CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
979
980static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
981 .show_attribute = target_fabric_tpg_auth_attr_show,
982 .store_attribute = target_fabric_tpg_auth_attr_store,
983};
984
985TF_CIT_SETUP(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL, NULL);
986
987/* End of tfc_tpg_attrib_cit */
988
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800989/* Start of tfc_tpg_param_cit */
990
991CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
992
993static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
994 .show_attribute = target_fabric_tpg_param_attr_show,
995 .store_attribute = target_fabric_tpg_param_attr_store,
996};
997
998TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
999
1000/* End of tfc_tpg_param_cit */
1001
1002/* Start of tfc_tpg_base_cit */
1003/*
1004 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1005 */
1006CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1007
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001008static void target_fabric_tpg_release(struct config_item *item)
1009{
1010 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1011 struct se_portal_group, tpg_group);
1012 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1013 struct target_fabric_configfs *tf = wwn->wwn_tf;
1014
1015 tf->tf_ops.fabric_drop_tpg(se_tpg);
1016}
1017
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001019 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020 .show_attribute = target_fabric_tpg_attr_show,
1021 .store_attribute = target_fabric_tpg_attr_store,
1022};
1023
1024TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
1025
1026/* End of tfc_tpg_base_cit */
1027
1028/* Start of tfc_tpg_cit */
1029
1030static struct config_group *target_fabric_make_tpg(
1031 struct config_group *group,
1032 const char *name)
1033{
1034 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1035 struct target_fabric_configfs *tf = wwn->wwn_tf;
1036 struct se_portal_group *se_tpg;
1037
Andy Grover6708bb22011-06-08 10:36:43 -07001038 if (!tf->tf_ops.fabric_make_tpg) {
1039 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 return ERR_PTR(-ENOSYS);
1041 }
1042
1043 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001044 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001045 return ERR_PTR(-EINVAL);
1046 /*
1047 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1048 */
1049 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1050 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1051 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1052 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1053 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001054 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1055 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1056 se_tpg->tpg_group.default_groups[6] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057
1058 config_group_init_type_name(&se_tpg->tpg_group, name,
1059 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1060 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1061 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1062 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1063 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1064 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1065 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1066 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1067 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001068 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
1069 &TF_CIT_TMPL(tf)->tfc_tpg_auth_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1071 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1072
1073 return &se_tpg->tpg_group;
1074}
1075
1076static void target_fabric_drop_tpg(
1077 struct config_group *group,
1078 struct config_item *item)
1079{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1081 struct se_portal_group, tpg_group);
1082 struct config_group *tpg_cg = &se_tpg->tpg_group;
1083 struct config_item *df_item;
1084 int i;
1085 /*
1086 * Release default groups, but do not release tpg_cg->default_groups
1087 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1088 */
1089 for (i = 0; tpg_cg->default_groups[i]; i++) {
1090 df_item = &tpg_cg->default_groups[i]->cg_item;
1091 tpg_cg->default_groups[i] = NULL;
1092 config_item_put(df_item);
1093 }
1094
1095 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096}
1097
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001098static void target_fabric_release_wwn(struct config_item *item)
1099{
1100 struct se_wwn *wwn = container_of(to_config_group(item),
1101 struct se_wwn, wwn_group);
1102 struct target_fabric_configfs *tf = wwn->wwn_tf;
1103
1104 tf->tf_ops.fabric_drop_wwn(wwn);
1105}
1106
1107static struct configfs_item_operations target_fabric_tpg_item_ops = {
1108 .release = target_fabric_release_wwn,
1109};
1110
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111static struct configfs_group_operations target_fabric_tpg_group_ops = {
1112 .make_group = target_fabric_make_tpg,
1113 .drop_item = target_fabric_drop_tpg,
1114};
1115
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001116TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1117 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001118
1119/* End of tfc_tpg_cit */
1120
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001121/* Start of tfc_wwn_fabric_stats_cit */
1122/*
1123 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1124 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1125 */
1126TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1127
1128/* End of tfc_wwn_fabric_stats_cit */
1129
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130/* Start of tfc_wwn_cit */
1131
1132static struct config_group *target_fabric_make_wwn(
1133 struct config_group *group,
1134 const char *name)
1135{
1136 struct target_fabric_configfs *tf = container_of(group,
1137 struct target_fabric_configfs, tf_group);
1138 struct se_wwn *wwn;
1139
Andy Grover6708bb22011-06-08 10:36:43 -07001140 if (!tf->tf_ops.fabric_make_wwn) {
1141 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142 return ERR_PTR(-ENOSYS);
1143 }
1144
1145 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001146 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147 return ERR_PTR(-EINVAL);
1148
1149 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001150 /*
1151 * Setup default groups from pre-allocated wwn->wwn_default_groups
1152 */
1153 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1154 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1155 wwn->wwn_group.default_groups[1] = NULL;
1156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 config_group_init_type_name(&wwn->wwn_group, name,
1158 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001159 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1160 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161
1162 return &wwn->wwn_group;
1163}
1164
1165static void target_fabric_drop_wwn(
1166 struct config_group *group,
1167 struct config_item *item)
1168{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001169 struct se_wwn *wwn = container_of(to_config_group(item),
1170 struct se_wwn, wwn_group);
1171 struct config_item *df_item;
1172 struct config_group *cg = &wwn->wwn_group;
1173 int i;
1174
1175 for (i = 0; cg->default_groups[i]; i++) {
1176 df_item = &cg->default_groups[i]->cg_item;
1177 cg->default_groups[i] = NULL;
1178 config_item_put(df_item);
1179 }
1180
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182}
1183
1184static struct configfs_group_operations target_fabric_wwn_group_ops = {
1185 .make_group = target_fabric_make_wwn,
1186 .drop_item = target_fabric_drop_wwn,
1187};
1188/*
1189 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1190 */
1191CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1192
1193static struct configfs_item_operations target_fabric_wwn_item_ops = {
1194 .show_attribute = target_fabric_wwn_attr_show,
1195 .store_attribute = target_fabric_wwn_attr_store,
1196};
1197
1198TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1199
1200/* End of tfc_wwn_cit */
1201
1202/* Start of tfc_discovery_cit */
1203
1204CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1205 tf_disc_group);
1206
1207static struct configfs_item_operations target_fabric_discovery_item_ops = {
1208 .show_attribute = target_fabric_discovery_attr_show,
1209 .store_attribute = target_fabric_discovery_attr_store,
1210};
1211
1212TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1213
1214/* End of tfc_discovery_cit */
1215
1216int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1217{
1218 target_fabric_setup_discovery_cit(tf);
1219 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001220 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221 target_fabric_setup_tpg_cit(tf);
1222 target_fabric_setup_tpg_base_cit(tf);
1223 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001224 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 target_fabric_setup_tpg_lun_cit(tf);
1226 target_fabric_setup_tpg_np_cit(tf);
1227 target_fabric_setup_tpg_np_base_cit(tf);
1228 target_fabric_setup_tpg_attrib_cit(tf);
Nicholas Bellingere4b512e2013-06-19 18:37:00 -07001229 target_fabric_setup_tpg_auth_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230 target_fabric_setup_tpg_param_cit(tf);
1231 target_fabric_setup_tpg_nacl_cit(tf);
1232 target_fabric_setup_tpg_nacl_base_cit(tf);
1233 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1234 target_fabric_setup_tpg_nacl_auth_cit(tf);
1235 target_fabric_setup_tpg_nacl_param_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001236 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001237 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001238 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001239
1240 return 0;
1241}