blob: 810263dfa4a1d9d272e16a492c5fb44365967a6c [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 Bellingerfd9a11d2012-11-09 14:51:48 -08007 * (c) Copyright 2010-2012 RisingTide Systems LLC.
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;
192
193 if (strict_strtoul(page, 0, &op))
194 return -EINVAL;
195
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
279TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
280
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
292TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
293
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
305TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
306
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;
321 struct se_lun_acl *lacl;
322 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 */
353 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
354 ret = -EINVAL;
355 goto out;
356 }
357
358 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
359 config_item_name(acl_ci), &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700360 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700361 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700363 }
364
365 lacl_cg = &lacl->se_lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100366 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700367 GFP_KERNEL);
368 if (!lacl_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700369 pr_err("Unable to allocate lacl_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700370 ret = -ENOMEM;
371 goto out;
372 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373
374 config_group_init_type_name(&lacl->se_lun_group, name,
375 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700376 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
377 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
378 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
379 lacl_cg->default_groups[1] = NULL;
380
Andy Grovere3d6f902011-07-19 08:55:10 +0000381 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100382 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700383 GFP_KERNEL);
384 if (!ml_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700385 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700386 ret = -ENOMEM;
387 goto out;
388 }
389 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390
391 kfree(buf);
392 return &lacl->se_lun_group;
393out:
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700394 if (lacl_cg)
395 kfree(lacl_cg->default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 kfree(buf);
397 return ERR_PTR(ret);
398}
399
400static void target_fabric_drop_mappedlun(
401 struct config_group *group,
402 struct config_item *item)
403{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700404 struct se_lun_acl *lacl = container_of(to_config_group(item),
405 struct se_lun_acl, se_lun_group);
406 struct config_item *df_item;
407 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
408 int i;
409
Andy Grovere3d6f902011-07-19 08:55:10 +0000410 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700411 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
412 df_item = &ml_stat_grp->default_groups[i]->cg_item;
413 ml_stat_grp->default_groups[i] = NULL;
414 config_item_put(df_item);
415 }
416 kfree(ml_stat_grp->default_groups);
417
418 lacl_cg = &lacl->se_lun_group;
419 for (i = 0; lacl_cg->default_groups[i]; i++) {
420 df_item = &lacl_cg->default_groups[i]->cg_item;
421 lacl_cg->default_groups[i] = NULL;
422 config_item_put(df_item);
423 }
424 kfree(lacl_cg->default_groups);
425
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800427}
428
429static void target_fabric_nacl_base_release(struct config_item *item)
430{
431 struct se_node_acl *se_nacl = container_of(to_config_group(item),
432 struct se_node_acl, acl_group);
433 struct se_portal_group *se_tpg = se_nacl->se_tpg;
434 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
435
436 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437}
438
439static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800440 .release = target_fabric_nacl_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441 .show_attribute = target_fabric_nacl_base_attr_show,
442 .store_attribute = target_fabric_nacl_base_attr_store,
443};
444
445static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
446 .make_group = target_fabric_make_mappedlun,
447 .drop_item = target_fabric_drop_mappedlun,
448};
449
450TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
451 &target_fabric_nacl_base_group_ops, NULL);
452
453/* End of tfc_tpg_nacl_base_cit */
454
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700455/* Start of tfc_node_fabric_stats_cit */
456/*
457 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
458 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
459 */
460TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
461
462/* End of tfc_wwn_fabric_stats_cit */
463
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464/* Start of tfc_tpg_nacl_cit */
465
466static struct config_group *target_fabric_make_nodeacl(
467 struct config_group *group,
468 const char *name)
469{
470 struct se_portal_group *se_tpg = container_of(group,
471 struct se_portal_group, tpg_acl_group);
472 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
473 struct se_node_acl *se_nacl;
474 struct config_group *nacl_cg;
475
Andy Grover6708bb22011-06-08 10:36:43 -0700476 if (!tf->tf_ops.fabric_make_nodeacl) {
477 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 return ERR_PTR(-ENOSYS);
479 }
480
481 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
482 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200483 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484
485 nacl_cg = &se_nacl->acl_group;
486 nacl_cg->default_groups = se_nacl->acl_default_groups;
487 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
488 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
489 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700490 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
491 nacl_cg->default_groups[4] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492
493 config_group_init_type_name(&se_nacl->acl_group, name,
494 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
495 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
496 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
497 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
498 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
499 config_group_init_type_name(&se_nacl->acl_param_group, "param",
500 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700501 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
502 "fabric_statistics",
503 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504
505 return &se_nacl->acl_group;
506}
507
508static void target_fabric_drop_nodeacl(
509 struct config_group *group,
510 struct config_item *item)
511{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512 struct se_node_acl *se_nacl = container_of(to_config_group(item),
513 struct se_node_acl, acl_group);
514 struct config_item *df_item;
515 struct config_group *nacl_cg;
516 int i;
517
518 nacl_cg = &se_nacl->acl_group;
519 for (i = 0; nacl_cg->default_groups[i]; i++) {
520 df_item = &nacl_cg->default_groups[i]->cg_item;
521 nacl_cg->default_groups[i] = NULL;
522 config_item_put(df_item);
523 }
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800524 /*
525 * struct se_node_acl free is done in target_fabric_nacl_base_release()
526 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800528}
529
530static struct configfs_group_operations target_fabric_nacl_group_ops = {
531 .make_group = target_fabric_make_nodeacl,
532 .drop_item = target_fabric_drop_nodeacl,
533};
534
535TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
536
537/* End of tfc_tpg_nacl_cit */
538
539/* Start of tfc_tpg_np_base_cit */
540
541CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
542
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800543static void target_fabric_np_base_release(struct config_item *item)
544{
545 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
546 struct se_tpg_np, tpg_np_group);
547 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
548 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
549
550 tf->tf_ops.fabric_drop_np(se_tpg_np);
551}
552
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800554 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 .show_attribute = target_fabric_np_base_attr_show,
556 .store_attribute = target_fabric_np_base_attr_store,
557};
558
559TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
560
561/* End of tfc_tpg_np_base_cit */
562
563/* Start of tfc_tpg_np_cit */
564
565static struct config_group *target_fabric_make_np(
566 struct config_group *group,
567 const char *name)
568{
569 struct se_portal_group *se_tpg = container_of(group,
570 struct se_portal_group, tpg_np_group);
571 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
572 struct se_tpg_np *se_tpg_np;
573
Andy Grover6708bb22011-06-08 10:36:43 -0700574 if (!tf->tf_ops.fabric_make_np) {
575 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 return ERR_PTR(-ENOSYS);
577 }
578
579 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700580 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 return ERR_PTR(-EINVAL);
582
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800583 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
585 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
586
587 return &se_tpg_np->tpg_np_group;
588}
589
590static void target_fabric_drop_np(
591 struct config_group *group,
592 struct config_item *item)
593{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800594 /*
595 * struct se_tpg_np is released via target_fabric_np_base_release()
596 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598}
599
600static struct configfs_group_operations target_fabric_np_group_ops = {
601 .make_group = &target_fabric_make_np,
602 .drop_item = &target_fabric_drop_np,
603};
604
605TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
606
607/* End of tfc_tpg_np_cit */
608
609/* Start of tfc_tpg_port_cit */
610
611CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
612#define TCM_PORT_ATTR(_name, _mode) \
613static struct target_fabric_port_attribute target_fabric_port_##_name = \
614 __CONFIGFS_EATTR(_name, _mode, \
615 target_fabric_port_show_attr_##_name, \
616 target_fabric_port_store_attr_##_name);
617
618#define TCM_PORT_ATTOR_RO(_name) \
619 __CONFIGFS_EATTR_RO(_name, \
620 target_fabric_port_show_attr_##_name);
621
622/*
623 * alua_tg_pt_gp
624 */
625static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
626 struct se_lun *lun,
627 char *page)
628{
Andy Grover6708bb22011-06-08 10:36:43 -0700629 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 return -ENODEV;
631
632 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
633}
634
635static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
636 struct se_lun *lun,
637 const char *page,
638 size_t count)
639{
Andy Grover6708bb22011-06-08 10:36:43 -0700640 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800641 return -ENODEV;
642
643 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
644}
645
646TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
647
648/*
649 * alua_tg_pt_offline
650 */
651static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
652 struct se_lun *lun,
653 char *page)
654{
Andy Grover6708bb22011-06-08 10:36:43 -0700655 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 return -ENODEV;
657
658 return core_alua_show_offline_bit(lun, page);
659}
660
661static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
662 struct se_lun *lun,
663 const char *page,
664 size_t count)
665{
Andy Grover6708bb22011-06-08 10:36:43 -0700666 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667 return -ENODEV;
668
669 return core_alua_store_offline_bit(lun, page, count);
670}
671
672TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
673
674/*
675 * alua_tg_pt_status
676 */
677static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
678 struct se_lun *lun,
679 char *page)
680{
Andy Grover6708bb22011-06-08 10:36:43 -0700681 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682 return -ENODEV;
683
684 return core_alua_show_secondary_status(lun, page);
685}
686
687static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
688 struct se_lun *lun,
689 const char *page,
690 size_t count)
691{
Andy Grover6708bb22011-06-08 10:36:43 -0700692 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 return -ENODEV;
694
695 return core_alua_store_secondary_status(lun, page, count);
696}
697
698TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
699
700/*
701 * alua_tg_pt_write_md
702 */
703static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
704 struct se_lun *lun,
705 char *page)
706{
Andy Grover6708bb22011-06-08 10:36:43 -0700707 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 return -ENODEV;
709
710 return core_alua_show_secondary_write_metadata(lun, page);
711}
712
713static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
714 struct se_lun *lun,
715 const char *page,
716 size_t count)
717{
Andy Grover6708bb22011-06-08 10:36:43 -0700718 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719 return -ENODEV;
720
721 return core_alua_store_secondary_write_metadata(lun, page, count);
722}
723
724TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
725
726
727static struct configfs_attribute *target_fabric_port_attrs[] = {
728 &target_fabric_port_alua_tg_pt_gp.attr,
729 &target_fabric_port_alua_tg_pt_offline.attr,
730 &target_fabric_port_alua_tg_pt_status.attr,
731 &target_fabric_port_alua_tg_pt_write_md.attr,
732 NULL,
733};
734
735CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
736
737static int target_fabric_port_link(
738 struct config_item *lun_ci,
739 struct config_item *se_dev_ci)
740{
741 struct config_item *tpg_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 struct se_lun *lun = container_of(to_config_group(lun_ci),
743 struct se_lun, lun_group);
744 struct se_lun *lun_p;
745 struct se_portal_group *se_tpg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400746 struct se_device *dev =
747 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 struct target_fabric_configfs *tf;
749 int ret;
750
Nicholas Bellinger0ff87542012-12-04 23:43:57 -0800751 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
752 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
753 " %p to struct se_device: %p\n", se_dev_ci, dev);
754 return -EFAULT;
755 }
756
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
758 se_tpg = container_of(to_config_group(tpg_ci),
759 struct se_portal_group, tpg_group);
760 tf = se_tpg->se_tpg_wwn->wwn_tf;
761
762 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700763 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800764 return -EEXIST;
765 }
766
Andy Grover2dca6732012-07-12 17:34:55 -0700767 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +0100768 if (IS_ERR(lun_p)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700769 pr_err("core_dev_add_lun() failed\n");
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +0100770 ret = PTR_ERR(lun_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800771 goto out;
772 }
773
774 if (tf->tf_ops.fabric_post_link) {
775 /*
776 * Call the optional fabric_post_link() to allow a
777 * fabric module to setup any additional state once
778 * core_dev_add_lun() has been called..
779 */
780 tf->tf_ops.fabric_post_link(se_tpg, lun);
781 }
782
783 return 0;
784out:
785 return ret;
786}
787
788static int target_fabric_port_unlink(
789 struct config_item *lun_ci,
790 struct config_item *se_dev_ci)
791{
792 struct se_lun *lun = container_of(to_config_group(lun_ci),
793 struct se_lun, lun_group);
794 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
795 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
796
797 if (tf->tf_ops.fabric_pre_unlink) {
798 /*
799 * Call the optional fabric_pre_unlink() to allow a
800 * fabric module to release any additional stat before
801 * core_dev_del_lun() is called.
802 */
803 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
804 }
805
806 core_dev_del_lun(se_tpg, lun->unpacked_lun);
807 return 0;
808}
809
810static struct configfs_item_operations target_fabric_port_item_ops = {
811 .show_attribute = target_fabric_port_attr_show,
812 .store_attribute = target_fabric_port_attr_store,
813 .allow_link = target_fabric_port_link,
814 .drop_link = target_fabric_port_unlink,
815};
816
817TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
818
819/* End of tfc_tpg_port_cit */
820
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700821/* Start of tfc_tpg_port_stat_cit */
822
823static struct config_group *target_core_port_stat_mkdir(
824 struct config_group *group,
825 const char *name)
826{
827 return ERR_PTR(-ENOSYS);
828}
829
830static void target_core_port_stat_rmdir(
831 struct config_group *group,
832 struct config_item *item)
833{
834 return;
835}
836
837static struct configfs_group_operations target_fabric_port_stat_group_ops = {
838 .make_group = target_core_port_stat_mkdir,
839 .drop_item = target_core_port_stat_rmdir,
840};
841
842TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
843
844/* End of tfc_tpg_port_stat_cit */
845
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846/* Start of tfc_tpg_lun_cit */
847
848static struct config_group *target_fabric_make_lun(
849 struct config_group *group,
850 const char *name)
851{
852 struct se_lun *lun;
853 struct se_portal_group *se_tpg = container_of(group,
854 struct se_portal_group, tpg_lun_group);
855 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700856 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800857 unsigned long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700858 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800859
860 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700861 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800862 " \"lun_$LUN_NUMBER\"\n");
863 return ERR_PTR(-EINVAL);
864 }
865 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
866 return ERR_PTR(-EINVAL);
867
868 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -0700869 if (!lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800870 return ERR_PTR(-EINVAL);
871
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700872 lun_cg = &lun->lun_group;
Sebastian Andrzej Siewior13f6a912012-11-27 18:54:21 +0100873 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700874 GFP_KERNEL);
875 if (!lun_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700876 pr_err("Unable to allocate lun_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700877 return ERR_PTR(-ENOMEM);
878 }
879
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880 config_group_init_type_name(&lun->lun_group, name,
881 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700882 config_group_init_type_name(&lun->port_stat_grps.stat_group,
883 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
884 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
885 lun_cg->default_groups[1] = NULL;
886
Andy Grovere3d6f902011-07-19 08:55:10 +0000887 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700888 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
889 GFP_KERNEL);
890 if (!port_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700891 pr_err("Unable to allocate port_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700892 errno = -ENOMEM;
893 goto out;
894 }
895 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896
897 return &lun->lun_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700898out:
899 if (lun_cg)
900 kfree(lun_cg->default_groups);
901 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902}
903
904static void target_fabric_drop_lun(
905 struct config_group *group,
906 struct config_item *item)
907{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700908 struct se_lun *lun = container_of(to_config_group(item),
909 struct se_lun, lun_group);
910 struct config_item *df_item;
911 struct config_group *lun_cg, *port_stat_grp;
912 int i;
913
Andy Grovere3d6f902011-07-19 08:55:10 +0000914 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700915 for (i = 0; port_stat_grp->default_groups[i]; i++) {
916 df_item = &port_stat_grp->default_groups[i]->cg_item;
917 port_stat_grp->default_groups[i] = NULL;
918 config_item_put(df_item);
919 }
920 kfree(port_stat_grp->default_groups);
921
922 lun_cg = &lun->lun_group;
923 for (i = 0; lun_cg->default_groups[i]; i++) {
924 df_item = &lun_cg->default_groups[i]->cg_item;
925 lun_cg->default_groups[i] = NULL;
926 config_item_put(df_item);
927 }
928 kfree(lun_cg->default_groups);
929
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930 config_item_put(item);
931}
932
933static struct configfs_group_operations target_fabric_lun_group_ops = {
934 .make_group = &target_fabric_make_lun,
935 .drop_item = &target_fabric_drop_lun,
936};
937
938TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
939
940/* End of tfc_tpg_lun_cit */
941
942/* Start of tfc_tpg_attrib_cit */
943
944CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
945
946static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
947 .show_attribute = target_fabric_tpg_attrib_attr_show,
948 .store_attribute = target_fabric_tpg_attrib_attr_store,
949};
950
951TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
952
953/* End of tfc_tpg_attrib_cit */
954
955/* Start of tfc_tpg_param_cit */
956
957CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
958
959static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
960 .show_attribute = target_fabric_tpg_param_attr_show,
961 .store_attribute = target_fabric_tpg_param_attr_store,
962};
963
964TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
965
966/* End of tfc_tpg_param_cit */
967
968/* Start of tfc_tpg_base_cit */
969/*
970 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
971 */
972CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
973
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800974static void target_fabric_tpg_release(struct config_item *item)
975{
976 struct se_portal_group *se_tpg = container_of(to_config_group(item),
977 struct se_portal_group, tpg_group);
978 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
979 struct target_fabric_configfs *tf = wwn->wwn_tf;
980
981 tf->tf_ops.fabric_drop_tpg(se_tpg);
982}
983
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800984static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800985 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986 .show_attribute = target_fabric_tpg_attr_show,
987 .store_attribute = target_fabric_tpg_attr_store,
988};
989
990TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
991
992/* End of tfc_tpg_base_cit */
993
994/* Start of tfc_tpg_cit */
995
996static struct config_group *target_fabric_make_tpg(
997 struct config_group *group,
998 const char *name)
999{
1000 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1001 struct target_fabric_configfs *tf = wwn->wwn_tf;
1002 struct se_portal_group *se_tpg;
1003
Andy Grover6708bb22011-06-08 10:36:43 -07001004 if (!tf->tf_ops.fabric_make_tpg) {
1005 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001006 return ERR_PTR(-ENOSYS);
1007 }
1008
1009 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001010 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011 return ERR_PTR(-EINVAL);
1012 /*
1013 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1014 */
1015 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1016 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1017 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1018 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1019 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1020 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1021 se_tpg->tpg_group.default_groups[5] = NULL;
1022
1023 config_group_init_type_name(&se_tpg->tpg_group, name,
1024 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1025 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1026 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1027 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1028 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1029 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1030 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1031 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1032 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1033 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1034 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1035
1036 return &se_tpg->tpg_group;
1037}
1038
1039static void target_fabric_drop_tpg(
1040 struct config_group *group,
1041 struct config_item *item)
1042{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1044 struct se_portal_group, tpg_group);
1045 struct config_group *tpg_cg = &se_tpg->tpg_group;
1046 struct config_item *df_item;
1047 int i;
1048 /*
1049 * Release default groups, but do not release tpg_cg->default_groups
1050 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1051 */
1052 for (i = 0; tpg_cg->default_groups[i]; i++) {
1053 df_item = &tpg_cg->default_groups[i]->cg_item;
1054 tpg_cg->default_groups[i] = NULL;
1055 config_item_put(df_item);
1056 }
1057
1058 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059}
1060
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001061static void target_fabric_release_wwn(struct config_item *item)
1062{
1063 struct se_wwn *wwn = container_of(to_config_group(item),
1064 struct se_wwn, wwn_group);
1065 struct target_fabric_configfs *tf = wwn->wwn_tf;
1066
1067 tf->tf_ops.fabric_drop_wwn(wwn);
1068}
1069
1070static struct configfs_item_operations target_fabric_tpg_item_ops = {
1071 .release = target_fabric_release_wwn,
1072};
1073
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001074static struct configfs_group_operations target_fabric_tpg_group_ops = {
1075 .make_group = target_fabric_make_tpg,
1076 .drop_item = target_fabric_drop_tpg,
1077};
1078
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001079TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1080 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001081
1082/* End of tfc_tpg_cit */
1083
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001084/* Start of tfc_wwn_fabric_stats_cit */
1085/*
1086 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1087 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1088 */
1089TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1090
1091/* End of tfc_wwn_fabric_stats_cit */
1092
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001093/* Start of tfc_wwn_cit */
1094
1095static struct config_group *target_fabric_make_wwn(
1096 struct config_group *group,
1097 const char *name)
1098{
1099 struct target_fabric_configfs *tf = container_of(group,
1100 struct target_fabric_configfs, tf_group);
1101 struct se_wwn *wwn;
1102
Andy Grover6708bb22011-06-08 10:36:43 -07001103 if (!tf->tf_ops.fabric_make_wwn) {
1104 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 return ERR_PTR(-ENOSYS);
1106 }
1107
1108 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001109 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 return ERR_PTR(-EINVAL);
1111
1112 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001113 /*
1114 * Setup default groups from pre-allocated wwn->wwn_default_groups
1115 */
1116 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1117 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1118 wwn->wwn_group.default_groups[1] = NULL;
1119
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120 config_group_init_type_name(&wwn->wwn_group, name,
1121 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001122 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1123 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124
1125 return &wwn->wwn_group;
1126}
1127
1128static void target_fabric_drop_wwn(
1129 struct config_group *group,
1130 struct config_item *item)
1131{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001132 struct se_wwn *wwn = container_of(to_config_group(item),
1133 struct se_wwn, wwn_group);
1134 struct config_item *df_item;
1135 struct config_group *cg = &wwn->wwn_group;
1136 int i;
1137
1138 for (i = 0; cg->default_groups[i]; i++) {
1139 df_item = &cg->default_groups[i]->cg_item;
1140 cg->default_groups[i] = NULL;
1141 config_item_put(df_item);
1142 }
1143
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145}
1146
1147static struct configfs_group_operations target_fabric_wwn_group_ops = {
1148 .make_group = target_fabric_make_wwn,
1149 .drop_item = target_fabric_drop_wwn,
1150};
1151/*
1152 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1153 */
1154CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1155
1156static struct configfs_item_operations target_fabric_wwn_item_ops = {
1157 .show_attribute = target_fabric_wwn_attr_show,
1158 .store_attribute = target_fabric_wwn_attr_store,
1159};
1160
1161TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1162
1163/* End of tfc_wwn_cit */
1164
1165/* Start of tfc_discovery_cit */
1166
1167CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1168 tf_disc_group);
1169
1170static struct configfs_item_operations target_fabric_discovery_item_ops = {
1171 .show_attribute = target_fabric_discovery_attr_show,
1172 .store_attribute = target_fabric_discovery_attr_store,
1173};
1174
1175TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1176
1177/* End of tfc_discovery_cit */
1178
1179int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1180{
1181 target_fabric_setup_discovery_cit(tf);
1182 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001183 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 target_fabric_setup_tpg_cit(tf);
1185 target_fabric_setup_tpg_base_cit(tf);
1186 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001187 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 target_fabric_setup_tpg_lun_cit(tf);
1189 target_fabric_setup_tpg_np_cit(tf);
1190 target_fabric_setup_tpg_np_base_cit(tf);
1191 target_fabric_setup_tpg_attrib_cit(tf);
1192 target_fabric_setup_tpg_param_cit(tf);
1193 target_fabric_setup_tpg_nacl_cit(tf);
1194 target_fabric_setup_tpg_nacl_base_cit(tf);
1195 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1196 target_fabric_setup_tpg_nacl_auth_cit(tf);
1197 target_fabric_setup_tpg_nacl_param_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001198 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001200 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201
1202 return 0;
1203}