blob: fa1e4eb01bc7f6ce9386bee51561673962b8bdab [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 Bellinger12d2338422011-03-14 04:06:11 -07007 * Copyright (c) 2010,2011 Rising Tide Systems
8 * Copyright (c) 2010,2011 Linux-iSCSI.org
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08009 *
Nicholas Bellinger12d2338422011-03-14 04:06:11 -070010 * Copyright (c) Nicholas A. Bellinger <nab@linux-iscsi.org>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080011*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080025#include <generated/utsrelease.h>
26#include <linux/utsname.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/namei.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32#include <linux/delay.h>
33#include <linux/unistd.h>
34#include <linux/string.h>
35#include <linux/syscalls.h>
36#include <linux/configfs.h>
37
38#include <target/target_core_base.h>
39#include <target/target_core_device.h>
40#include <target/target_core_tpg.h>
41#include <target/target_core_transport.h>
42#include <target/target_core_fabric_ops.h>
43#include <target/target_core_fabric_configfs.h>
44#include <target/target_core_configfs.h>
45#include <target/configfs_macros.h>
46
Christoph Hellwige26d99a2011-11-14 12:30:30 -050047#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080048#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_pr.h"
50
51#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
52static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
53{ \
54 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
55 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
56 \
57 cit->ct_item_ops = _item_ops; \
58 cit->ct_group_ops = _group_ops; \
59 cit->ct_attrs = _attrs; \
60 cit->ct_owner = tf->tf_module; \
Andy Grover6708bb22011-06-08 10:36:43 -070061 pr_debug("Setup generic %s\n", __stringify(_name)); \
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062}
63
64/* Start of tfc_tpg_mappedlun_cit */
65
66static int target_fabric_mappedlun_link(
67 struct config_item *lun_acl_ci,
68 struct config_item *lun_ci)
69{
70 struct se_dev_entry *deve;
71 struct se_lun *lun = container_of(to_config_group(lun_ci),
72 struct se_lun, lun_group);
73 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
74 struct se_lun_acl, se_lun_group);
75 struct se_portal_group *se_tpg;
76 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
77 int ret = 0, lun_access;
78 /*
79 * Ensure that the source port exists
80 */
Andy Grover6708bb22011-06-08 10:36:43 -070081 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
82 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 "_tpg does not exist\n");
84 return -EINVAL;
85 }
86 se_tpg = lun->lun_sep->sep_tpg;
87
88 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
89 tpg_ci = &nacl_ci->ci_group->cg_item;
90 wwn_ci = &tpg_ci->ci_group->cg_item;
91 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
92 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
93 /*
94 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
95 */
96 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -070097 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 config_item_name(wwn_ci));
99 return -EINVAL;
100 }
101 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
Andy Grover6708bb22011-06-08 10:36:43 -0700102 pr_err("Illegal Initiator ACL Symlink outside of %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103 " TPGT: %s\n", config_item_name(wwn_ci),
104 config_item_name(tpg_ci));
105 return -EINVAL;
106 }
107 /*
108 * If this struct se_node_acl was dynamically generated with
109 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
110 * which be will write protected (READ-ONLY) when
111 * tpg_1/attrib/demo_mode_write_protect=1
112 */
113 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
114 deve = &lacl->se_lun_nacl->device_list[lacl->mapped_lun];
115 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
116 lun_access = deve->lun_flags;
117 else
118 lun_access =
Andy Grovere3d6f902011-07-19 08:55:10 +0000119 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
121 TRANSPORT_LUNFLAGS_READ_WRITE;
122 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
123 /*
124 * Determine the actual mapped LUN value user wants..
125 *
126 * This value is what the SCSI Initiator actually sees the
127 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
128 */
129 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
130 lun->unpacked_lun, lun_access);
131
132 return (ret < 0) ? -EINVAL : 0;
133}
134
135static int target_fabric_mappedlun_unlink(
136 struct config_item *lun_acl_ci,
137 struct config_item *lun_ci)
138{
139 struct se_lun *lun;
140 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
141 struct se_lun_acl, se_lun_group);
142 struct se_node_acl *nacl = lacl->se_lun_nacl;
143 struct se_dev_entry *deve = &nacl->device_list[lacl->mapped_lun];
144 struct se_portal_group *se_tpg;
145 /*
146 * Determine if the underlying MappedLUN has already been released..
147 */
Andy Grover6708bb22011-06-08 10:36:43 -0700148 if (!deve->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149 return 0;
150
151 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
152 se_tpg = lun->lun_sep->sep_tpg;
153
154 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
155 return 0;
156}
157
158CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
159#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
160static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
161 __CONFIGFS_EATTR(_name, _mode, \
162 target_fabric_mappedlun_show_##_name, \
163 target_fabric_mappedlun_store_##_name);
164
165static ssize_t target_fabric_mappedlun_show_write_protect(
166 struct se_lun_acl *lacl,
167 char *page)
168{
169 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
170 struct se_dev_entry *deve;
171 ssize_t len;
172
173 spin_lock_irq(&se_nacl->device_list_lock);
174 deve = &se_nacl->device_list[lacl->mapped_lun];
175 len = sprintf(page, "%d\n",
176 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
177 1 : 0);
178 spin_unlock_irq(&se_nacl->device_list_lock);
179
180 return len;
181}
182
183static ssize_t target_fabric_mappedlun_store_write_protect(
184 struct se_lun_acl *lacl,
185 const char *page,
186 size_t count)
187{
188 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
189 struct se_portal_group *se_tpg = se_nacl->se_tpg;
190 unsigned long op;
191
192 if (strict_strtoul(page, 0, &op))
193 return -EINVAL;
194
195 if ((op != 1) && (op != 0))
196 return -EINVAL;
197
198 core_update_device_list_access(lacl->mapped_lun, (op) ?
199 TRANSPORT_LUNFLAGS_READ_ONLY :
200 TRANSPORT_LUNFLAGS_READ_WRITE,
201 lacl->se_lun_nacl);
202
Andy Grover6708bb22011-06-08 10:36:43 -0700203 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 " Mapped LUN: %u Write Protect bit to %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000205 se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
207
208 return count;
209
210}
211
212TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
213
214CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
215
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800216static void target_fabric_mappedlun_release(struct config_item *item)
217{
218 struct se_lun_acl *lacl = container_of(to_config_group(item),
219 struct se_lun_acl, se_lun_group);
220 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
221
222 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
223}
224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
226 &target_fabric_mappedlun_write_protect.attr,
227 NULL,
228};
229
230static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800231 .release = target_fabric_mappedlun_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232 .show_attribute = target_fabric_mappedlun_attr_show,
233 .store_attribute = target_fabric_mappedlun_attr_store,
234 .allow_link = target_fabric_mappedlun_link,
235 .drop_link = target_fabric_mappedlun_unlink,
236};
237
238TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
239 target_fabric_mappedlun_attrs);
240
241/* End of tfc_tpg_mappedlun_cit */
242
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700243/* Start of tfc_tpg_mappedlun_port_cit */
244
245static struct config_group *target_core_mappedlun_stat_mkdir(
246 struct config_group *group,
247 const char *name)
248{
249 return ERR_PTR(-ENOSYS);
250}
251
252static void target_core_mappedlun_stat_rmdir(
253 struct config_group *group,
254 struct config_item *item)
255{
256 return;
257}
258
259static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
260 .make_group = target_core_mappedlun_stat_mkdir,
261 .drop_item = target_core_mappedlun_stat_rmdir,
262};
263
264TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
265 NULL);
266
267/* End of tfc_tpg_mappedlun_port_cit */
268
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800269/* Start of tfc_tpg_nacl_attrib_cit */
270
271CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
272
273static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
274 .show_attribute = target_fabric_nacl_attrib_attr_show,
275 .store_attribute = target_fabric_nacl_attrib_attr_store,
276};
277
278TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
279
280/* End of tfc_tpg_nacl_attrib_cit */
281
282/* Start of tfc_tpg_nacl_auth_cit */
283
284CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
285
286static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
287 .show_attribute = target_fabric_nacl_auth_attr_show,
288 .store_attribute = target_fabric_nacl_auth_attr_store,
289};
290
291TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
292
293/* End of tfc_tpg_nacl_auth_cit */
294
295/* Start of tfc_tpg_nacl_param_cit */
296
297CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
298
299static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
300 .show_attribute = target_fabric_nacl_param_attr_show,
301 .store_attribute = target_fabric_nacl_param_attr_store,
302};
303
304TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
305
306/* End of tfc_tpg_nacl_param_cit */
307
308/* Start of tfc_tpg_nacl_base_cit */
309
310CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
311
312static struct config_group *target_fabric_make_mappedlun(
313 struct config_group *group,
314 const char *name)
315{
316 struct se_node_acl *se_nacl = container_of(group,
317 struct se_node_acl, acl_group);
318 struct se_portal_group *se_tpg = se_nacl->se_tpg;
319 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
320 struct se_lun_acl *lacl;
321 struct config_item *acl_ci;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700322 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 char *buf;
324 unsigned long mapped_lun;
325 int ret = 0;
326
327 acl_ci = &group->cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -0700328 if (!acl_ci) {
329 pr_err("Unable to locatel acl_ci\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 return NULL;
331 }
332
333 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700334 if (!buf) {
335 pr_err("Unable to allocate memory for name buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336 return ERR_PTR(-ENOMEM);
337 }
338 snprintf(buf, strlen(name) + 1, "%s", name);
339 /*
340 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
341 */
342 if (strstr(buf, "lun_") != buf) {
Andy Grover6708bb22011-06-08 10:36:43 -0700343 pr_err("Unable to locate \"lun_\" from buf: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344 " name: %s\n", buf, name);
345 ret = -EINVAL;
346 goto out;
347 }
348 /*
349 * Determine the Mapped LUN value. This is what the SCSI Initiator
350 * Port will actually see.
351 */
352 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
353 ret = -EINVAL;
354 goto out;
355 }
356
357 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
358 config_item_name(acl_ci), &ret);
Andy Grover6708bb22011-06-08 10:36:43 -0700359 if (!lacl) {
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700360 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361 goto out;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700362 }
363
364 lacl_cg = &lacl->se_lun_group;
365 lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
366 GFP_KERNEL);
367 if (!lacl_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700368 pr_err("Unable to allocate lacl_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700369 ret = -ENOMEM;
370 goto out;
371 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372
373 config_group_init_type_name(&lacl->se_lun_group, name,
374 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700375 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
376 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
377 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
378 lacl_cg->default_groups[1] = NULL;
379
Andy Grovere3d6f902011-07-19 08:55:10 +0000380 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700381 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
382 GFP_KERNEL);
383 if (!ml_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700384 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700385 ret = -ENOMEM;
386 goto out;
387 }
388 target_stat_setup_mappedlun_default_groups(lacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389
390 kfree(buf);
391 return &lacl->se_lun_group;
392out:
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700393 if (lacl_cg)
394 kfree(lacl_cg->default_groups);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 kfree(buf);
396 return ERR_PTR(ret);
397}
398
399static void target_fabric_drop_mappedlun(
400 struct config_group *group,
401 struct config_item *item)
402{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700403 struct se_lun_acl *lacl = container_of(to_config_group(item),
404 struct se_lun_acl, se_lun_group);
405 struct config_item *df_item;
406 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
407 int i;
408
Andy Grovere3d6f902011-07-19 08:55:10 +0000409 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700410 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
411 df_item = &ml_stat_grp->default_groups[i]->cg_item;
412 ml_stat_grp->default_groups[i] = NULL;
413 config_item_put(df_item);
414 }
415 kfree(ml_stat_grp->default_groups);
416
417 lacl_cg = &lacl->se_lun_group;
418 for (i = 0; lacl_cg->default_groups[i]; i++) {
419 df_item = &lacl_cg->default_groups[i]->cg_item;
420 lacl_cg->default_groups[i] = NULL;
421 config_item_put(df_item);
422 }
423 kfree(lacl_cg->default_groups);
424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 config_item_put(item);
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800426}
427
428static void target_fabric_nacl_base_release(struct config_item *item)
429{
430 struct se_node_acl *se_nacl = container_of(to_config_group(item),
431 struct se_node_acl, acl_group);
432 struct se_portal_group *se_tpg = se_nacl->se_tpg;
433 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
434
435 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800436}
437
438static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800439 .release = target_fabric_nacl_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440 .show_attribute = target_fabric_nacl_base_attr_show,
441 .store_attribute = target_fabric_nacl_base_attr_store,
442};
443
444static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
445 .make_group = target_fabric_make_mappedlun,
446 .drop_item = target_fabric_drop_mappedlun,
447};
448
449TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
450 &target_fabric_nacl_base_group_ops, NULL);
451
452/* End of tfc_tpg_nacl_base_cit */
453
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700454/* Start of tfc_node_fabric_stats_cit */
455/*
456 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
457 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
458 */
459TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
460
461/* End of tfc_wwn_fabric_stats_cit */
462
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463/* Start of tfc_tpg_nacl_cit */
464
465static struct config_group *target_fabric_make_nodeacl(
466 struct config_group *group,
467 const char *name)
468{
469 struct se_portal_group *se_tpg = container_of(group,
470 struct se_portal_group, tpg_acl_group);
471 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
472 struct se_node_acl *se_nacl;
473 struct config_group *nacl_cg;
474
Andy Grover6708bb22011-06-08 10:36:43 -0700475 if (!tf->tf_ops.fabric_make_nodeacl) {
476 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477 return ERR_PTR(-ENOSYS);
478 }
479
480 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
481 if (IS_ERR(se_nacl))
Thomas Meyere1750ba2011-08-01 23:58:18 +0200482 return ERR_CAST(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483
484 nacl_cg = &se_nacl->acl_group;
485 nacl_cg->default_groups = se_nacl->acl_default_groups;
486 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
487 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
488 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700489 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
490 nacl_cg->default_groups[4] = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491
492 config_group_init_type_name(&se_nacl->acl_group, name,
493 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
494 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
495 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
496 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
497 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
498 config_group_init_type_name(&se_nacl->acl_param_group, "param",
499 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700500 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
501 "fabric_statistics",
502 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503
504 return &se_nacl->acl_group;
505}
506
507static void target_fabric_drop_nodeacl(
508 struct config_group *group,
509 struct config_item *item)
510{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 struct se_node_acl *se_nacl = container_of(to_config_group(item),
512 struct se_node_acl, acl_group);
513 struct config_item *df_item;
514 struct config_group *nacl_cg;
515 int i;
516
517 nacl_cg = &se_nacl->acl_group;
518 for (i = 0; nacl_cg->default_groups[i]; i++) {
519 df_item = &nacl_cg->default_groups[i]->cg_item;
520 nacl_cg->default_groups[i] = NULL;
521 config_item_put(df_item);
522 }
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800523 /*
524 * struct se_node_acl free is done in target_fabric_nacl_base_release()
525 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527}
528
529static struct configfs_group_operations target_fabric_nacl_group_ops = {
530 .make_group = target_fabric_make_nodeacl,
531 .drop_item = target_fabric_drop_nodeacl,
532};
533
534TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
535
536/* End of tfc_tpg_nacl_cit */
537
538/* Start of tfc_tpg_np_base_cit */
539
540CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
541
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800542static void target_fabric_np_base_release(struct config_item *item)
543{
544 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
545 struct se_tpg_np, tpg_np_group);
546 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
547 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
548
549 tf->tf_ops.fabric_drop_np(se_tpg_np);
550}
551
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552static struct configfs_item_operations target_fabric_np_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800553 .release = target_fabric_np_base_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 .show_attribute = target_fabric_np_base_attr_show,
555 .store_attribute = target_fabric_np_base_attr_store,
556};
557
558TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
559
560/* End of tfc_tpg_np_base_cit */
561
562/* Start of tfc_tpg_np_cit */
563
564static struct config_group *target_fabric_make_np(
565 struct config_group *group,
566 const char *name)
567{
568 struct se_portal_group *se_tpg = container_of(group,
569 struct se_portal_group, tpg_np_group);
570 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
571 struct se_tpg_np *se_tpg_np;
572
Andy Grover6708bb22011-06-08 10:36:43 -0700573 if (!tf->tf_ops.fabric_make_np) {
574 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800575 return ERR_PTR(-ENOSYS);
576 }
577
578 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -0700579 if (!se_tpg_np || IS_ERR(se_tpg_np))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 return ERR_PTR(-EINVAL);
581
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800582 se_tpg_np->tpg_np_parent = se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
584 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
585
586 return &se_tpg_np->tpg_np_group;
587}
588
589static void target_fabric_drop_np(
590 struct config_group *group,
591 struct config_item *item)
592{
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800593 /*
594 * struct se_tpg_np is released via target_fabric_np_base_release()
595 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597}
598
599static struct configfs_group_operations target_fabric_np_group_ops = {
600 .make_group = &target_fabric_make_np,
601 .drop_item = &target_fabric_drop_np,
602};
603
604TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
605
606/* End of tfc_tpg_np_cit */
607
608/* Start of tfc_tpg_port_cit */
609
610CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
611#define TCM_PORT_ATTR(_name, _mode) \
612static struct target_fabric_port_attribute target_fabric_port_##_name = \
613 __CONFIGFS_EATTR(_name, _mode, \
614 target_fabric_port_show_attr_##_name, \
615 target_fabric_port_store_attr_##_name);
616
617#define TCM_PORT_ATTOR_RO(_name) \
618 __CONFIGFS_EATTR_RO(_name, \
619 target_fabric_port_show_attr_##_name);
620
621/*
622 * alua_tg_pt_gp
623 */
624static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
625 struct se_lun *lun,
626 char *page)
627{
Andy Grover6708bb22011-06-08 10:36:43 -0700628 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629 return -ENODEV;
630
631 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
632}
633
634static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
635 struct se_lun *lun,
636 const char *page,
637 size_t count)
638{
Andy Grover6708bb22011-06-08 10:36:43 -0700639 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 return -ENODEV;
641
642 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
643}
644
645TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
646
647/*
648 * alua_tg_pt_offline
649 */
650static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
651 struct se_lun *lun,
652 char *page)
653{
Andy Grover6708bb22011-06-08 10:36:43 -0700654 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 return -ENODEV;
656
657 return core_alua_show_offline_bit(lun, page);
658}
659
660static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
661 struct se_lun *lun,
662 const char *page,
663 size_t count)
664{
Andy Grover6708bb22011-06-08 10:36:43 -0700665 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 return -ENODEV;
667
668 return core_alua_store_offline_bit(lun, page, count);
669}
670
671TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
672
673/*
674 * alua_tg_pt_status
675 */
676static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
677 struct se_lun *lun,
678 char *page)
679{
Andy Grover6708bb22011-06-08 10:36:43 -0700680 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800681 return -ENODEV;
682
683 return core_alua_show_secondary_status(lun, page);
684}
685
686static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
687 struct se_lun *lun,
688 const char *page,
689 size_t count)
690{
Andy Grover6708bb22011-06-08 10:36:43 -0700691 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692 return -ENODEV;
693
694 return core_alua_store_secondary_status(lun, page, count);
695}
696
697TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
698
699/*
700 * alua_tg_pt_write_md
701 */
702static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
703 struct se_lun *lun,
704 char *page)
705{
Andy Grover6708bb22011-06-08 10:36:43 -0700706 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800707 return -ENODEV;
708
709 return core_alua_show_secondary_write_metadata(lun, page);
710}
711
712static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
713 struct se_lun *lun,
714 const char *page,
715 size_t count)
716{
Andy Grover6708bb22011-06-08 10:36:43 -0700717 if (!lun || !lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718 return -ENODEV;
719
720 return core_alua_store_secondary_write_metadata(lun, page, count);
721}
722
723TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
724
725
726static struct configfs_attribute *target_fabric_port_attrs[] = {
727 &target_fabric_port_alua_tg_pt_gp.attr,
728 &target_fabric_port_alua_tg_pt_offline.attr,
729 &target_fabric_port_alua_tg_pt_status.attr,
730 &target_fabric_port_alua_tg_pt_write_md.attr,
731 NULL,
732};
733
734CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
735
736static int target_fabric_port_link(
737 struct config_item *lun_ci,
738 struct config_item *se_dev_ci)
739{
740 struct config_item *tpg_ci;
741 struct se_device *dev;
742 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;
746 struct se_subsystem_dev *se_dev = container_of(
747 to_config_group(se_dev_ci), struct se_subsystem_dev,
748 se_dev_group);
749 struct target_fabric_configfs *tf;
750 int ret;
751
752 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
753 se_tpg = container_of(to_config_group(tpg_ci),
754 struct se_portal_group, tpg_group);
755 tf = se_tpg->se_tpg_wwn->wwn_tf;
756
757 if (lun->lun_se_dev != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700758 pr_err("Port Symlink already exists\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759 return -EEXIST;
760 }
761
762 dev = se_dev->se_dev_ptr;
Andy Grover6708bb22011-06-08 10:36:43 -0700763 if (!dev) {
764 pr_err("Unable to locate struct se_device pointer from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800765 " %s\n", config_item_name(se_dev_ci));
766 ret = -ENODEV;
767 goto out;
768 }
769
770 lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
771 lun->unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -0700772 if (IS_ERR(lun_p) || !lun_p) {
773 pr_err("core_dev_add_lun() failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774 ret = -EINVAL;
775 goto out;
776 }
777
778 if (tf->tf_ops.fabric_post_link) {
779 /*
780 * Call the optional fabric_post_link() to allow a
781 * fabric module to setup any additional state once
782 * core_dev_add_lun() has been called..
783 */
784 tf->tf_ops.fabric_post_link(se_tpg, lun);
785 }
786
787 return 0;
788out:
789 return ret;
790}
791
792static int target_fabric_port_unlink(
793 struct config_item *lun_ci,
794 struct config_item *se_dev_ci)
795{
796 struct se_lun *lun = container_of(to_config_group(lun_ci),
797 struct se_lun, lun_group);
798 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
799 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
800
801 if (tf->tf_ops.fabric_pre_unlink) {
802 /*
803 * Call the optional fabric_pre_unlink() to allow a
804 * fabric module to release any additional stat before
805 * core_dev_del_lun() is called.
806 */
807 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
808 }
809
810 core_dev_del_lun(se_tpg, lun->unpacked_lun);
811 return 0;
812}
813
814static struct configfs_item_operations target_fabric_port_item_ops = {
815 .show_attribute = target_fabric_port_attr_show,
816 .store_attribute = target_fabric_port_attr_store,
817 .allow_link = target_fabric_port_link,
818 .drop_link = target_fabric_port_unlink,
819};
820
821TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
822
823/* End of tfc_tpg_port_cit */
824
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700825/* Start of tfc_tpg_port_stat_cit */
826
827static struct config_group *target_core_port_stat_mkdir(
828 struct config_group *group,
829 const char *name)
830{
831 return ERR_PTR(-ENOSYS);
832}
833
834static void target_core_port_stat_rmdir(
835 struct config_group *group,
836 struct config_item *item)
837{
838 return;
839}
840
841static struct configfs_group_operations target_fabric_port_stat_group_ops = {
842 .make_group = target_core_port_stat_mkdir,
843 .drop_item = target_core_port_stat_rmdir,
844};
845
846TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
847
848/* End of tfc_tpg_port_stat_cit */
849
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850/* Start of tfc_tpg_lun_cit */
851
852static struct config_group *target_fabric_make_lun(
853 struct config_group *group,
854 const char *name)
855{
856 struct se_lun *lun;
857 struct se_portal_group *se_tpg = container_of(group,
858 struct se_portal_group, tpg_lun_group);
859 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700860 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800861 unsigned long unpacked_lun;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700862 int errno;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863
864 if (strstr(name, "lun_") != name) {
Andy Grover6708bb22011-06-08 10:36:43 -0700865 pr_err("Unable to locate \'_\" in"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 " \"lun_$LUN_NUMBER\"\n");
867 return ERR_PTR(-EINVAL);
868 }
869 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
870 return ERR_PTR(-EINVAL);
871
872 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -0700873 if (!lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874 return ERR_PTR(-EINVAL);
875
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700876 lun_cg = &lun->lun_group;
877 lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
878 GFP_KERNEL);
879 if (!lun_cg->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700880 pr_err("Unable to allocate lun_cg->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700881 return ERR_PTR(-ENOMEM);
882 }
883
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800884 config_group_init_type_name(&lun->lun_group, name,
885 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700886 config_group_init_type_name(&lun->port_stat_grps.stat_group,
887 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
888 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
889 lun_cg->default_groups[1] = NULL;
890
Andy Grovere3d6f902011-07-19 08:55:10 +0000891 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700892 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
893 GFP_KERNEL);
894 if (!port_stat_grp->default_groups) {
Andy Grover6708bb22011-06-08 10:36:43 -0700895 pr_err("Unable to allocate port_stat_grp->default_groups\n");
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700896 errno = -ENOMEM;
897 goto out;
898 }
899 target_stat_setup_port_default_groups(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800900
901 return &lun->lun_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700902out:
903 if (lun_cg)
904 kfree(lun_cg->default_groups);
905 return ERR_PTR(errno);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906}
907
908static void target_fabric_drop_lun(
909 struct config_group *group,
910 struct config_item *item)
911{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700912 struct se_lun *lun = container_of(to_config_group(item),
913 struct se_lun, lun_group);
914 struct config_item *df_item;
915 struct config_group *lun_cg, *port_stat_grp;
916 int i;
917
Andy Grovere3d6f902011-07-19 08:55:10 +0000918 port_stat_grp = &lun->port_stat_grps.stat_group;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -0700919 for (i = 0; port_stat_grp->default_groups[i]; i++) {
920 df_item = &port_stat_grp->default_groups[i]->cg_item;
921 port_stat_grp->default_groups[i] = NULL;
922 config_item_put(df_item);
923 }
924 kfree(port_stat_grp->default_groups);
925
926 lun_cg = &lun->lun_group;
927 for (i = 0; lun_cg->default_groups[i]; i++) {
928 df_item = &lun_cg->default_groups[i]->cg_item;
929 lun_cg->default_groups[i] = NULL;
930 config_item_put(df_item);
931 }
932 kfree(lun_cg->default_groups);
933
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800934 config_item_put(item);
935}
936
937static struct configfs_group_operations target_fabric_lun_group_ops = {
938 .make_group = &target_fabric_make_lun,
939 .drop_item = &target_fabric_drop_lun,
940};
941
942TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
943
944/* End of tfc_tpg_lun_cit */
945
946/* Start of tfc_tpg_attrib_cit */
947
948CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
949
950static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
951 .show_attribute = target_fabric_tpg_attrib_attr_show,
952 .store_attribute = target_fabric_tpg_attrib_attr_store,
953};
954
955TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
956
957/* End of tfc_tpg_attrib_cit */
958
959/* Start of tfc_tpg_param_cit */
960
961CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
962
963static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
964 .show_attribute = target_fabric_tpg_param_attr_show,
965 .store_attribute = target_fabric_tpg_param_attr_store,
966};
967
968TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
969
970/* End of tfc_tpg_param_cit */
971
972/* Start of tfc_tpg_base_cit */
973/*
974 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
975 */
976CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
977
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800978static void target_fabric_tpg_release(struct config_item *item)
979{
980 struct se_portal_group *se_tpg = container_of(to_config_group(item),
981 struct se_portal_group, tpg_group);
982 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
983 struct target_fabric_configfs *tf = wwn->wwn_tf;
984
985 tf->tf_ops.fabric_drop_tpg(se_tpg);
986}
987
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800988static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -0800989 .release = target_fabric_tpg_release,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800990 .show_attribute = target_fabric_tpg_attr_show,
991 .store_attribute = target_fabric_tpg_attr_store,
992};
993
994TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
995
996/* End of tfc_tpg_base_cit */
997
998/* Start of tfc_tpg_cit */
999
1000static struct config_group *target_fabric_make_tpg(
1001 struct config_group *group,
1002 const char *name)
1003{
1004 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1005 struct target_fabric_configfs *tf = wwn->wwn_tf;
1006 struct se_portal_group *se_tpg;
1007
Andy Grover6708bb22011-06-08 10:36:43 -07001008 if (!tf->tf_ops.fabric_make_tpg) {
1009 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010 return ERR_PTR(-ENOSYS);
1011 }
1012
1013 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001014 if (!se_tpg || IS_ERR(se_tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015 return ERR_PTR(-EINVAL);
1016 /*
1017 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1018 */
1019 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1020 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1021 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1022 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1023 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1024 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1025 se_tpg->tpg_group.default_groups[5] = NULL;
1026
1027 config_group_init_type_name(&se_tpg->tpg_group, name,
1028 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1029 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1030 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1031 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1032 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1033 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1034 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1035 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1036 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1037 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1038 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1039
1040 return &se_tpg->tpg_group;
1041}
1042
1043static void target_fabric_drop_tpg(
1044 struct config_group *group,
1045 struct config_item *item)
1046{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001047 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1048 struct se_portal_group, tpg_group);
1049 struct config_group *tpg_cg = &se_tpg->tpg_group;
1050 struct config_item *df_item;
1051 int i;
1052 /*
1053 * Release default groups, but do not release tpg_cg->default_groups
1054 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1055 */
1056 for (i = 0; tpg_cg->default_groups[i]; i++) {
1057 df_item = &tpg_cg->default_groups[i]->cg_item;
1058 tpg_cg->default_groups[i] = NULL;
1059 config_item_put(df_item);
1060 }
1061
1062 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001063}
1064
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001065static void target_fabric_release_wwn(struct config_item *item)
1066{
1067 struct se_wwn *wwn = container_of(to_config_group(item),
1068 struct se_wwn, wwn_group);
1069 struct target_fabric_configfs *tf = wwn->wwn_tf;
1070
1071 tf->tf_ops.fabric_drop_wwn(wwn);
1072}
1073
1074static struct configfs_item_operations target_fabric_tpg_item_ops = {
1075 .release = target_fabric_release_wwn,
1076};
1077
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078static struct configfs_group_operations target_fabric_tpg_group_ops = {
1079 .make_group = target_fabric_make_tpg,
1080 .drop_item = target_fabric_drop_tpg,
1081};
1082
Nicholas Bellinger1f6fe7c2011-02-09 15:34:54 -08001083TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1084 NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085
1086/* End of tfc_tpg_cit */
1087
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001088/* Start of tfc_wwn_fabric_stats_cit */
1089/*
1090 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1091 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1092 */
1093TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1094
1095/* End of tfc_wwn_fabric_stats_cit */
1096
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097/* Start of tfc_wwn_cit */
1098
1099static struct config_group *target_fabric_make_wwn(
1100 struct config_group *group,
1101 const char *name)
1102{
1103 struct target_fabric_configfs *tf = container_of(group,
1104 struct target_fabric_configfs, tf_group);
1105 struct se_wwn *wwn;
1106
Andy Grover6708bb22011-06-08 10:36:43 -07001107 if (!tf->tf_ops.fabric_make_wwn) {
1108 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 return ERR_PTR(-ENOSYS);
1110 }
1111
1112 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
Andy Grover6708bb22011-06-08 10:36:43 -07001113 if (!wwn || IS_ERR(wwn))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001114 return ERR_PTR(-EINVAL);
1115
1116 wwn->wwn_tf = tf;
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001117 /*
1118 * Setup default groups from pre-allocated wwn->wwn_default_groups
1119 */
1120 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1121 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1122 wwn->wwn_group.default_groups[1] = NULL;
1123
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124 config_group_init_type_name(&wwn->wwn_group, name,
1125 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001126 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1127 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001128
1129 return &wwn->wwn_group;
1130}
1131
1132static void target_fabric_drop_wwn(
1133 struct config_group *group,
1134 struct config_item *item)
1135{
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001136 struct se_wwn *wwn = container_of(to_config_group(item),
1137 struct se_wwn, wwn_group);
1138 struct config_item *df_item;
1139 struct config_group *cg = &wwn->wwn_group;
1140 int i;
1141
1142 for (i = 0; cg->default_groups[i]; i++) {
1143 df_item = &cg->default_groups[i]->cg_item;
1144 cg->default_groups[i] = NULL;
1145 config_item_put(df_item);
1146 }
1147
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 config_item_put(item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149}
1150
1151static struct configfs_group_operations target_fabric_wwn_group_ops = {
1152 .make_group = target_fabric_make_wwn,
1153 .drop_item = target_fabric_drop_wwn,
1154};
1155/*
1156 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1157 */
1158CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1159
1160static struct configfs_item_operations target_fabric_wwn_item_ops = {
1161 .show_attribute = target_fabric_wwn_attr_show,
1162 .store_attribute = target_fabric_wwn_attr_store,
1163};
1164
1165TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1166
1167/* End of tfc_wwn_cit */
1168
1169/* Start of tfc_discovery_cit */
1170
1171CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1172 tf_disc_group);
1173
1174static struct configfs_item_operations target_fabric_discovery_item_ops = {
1175 .show_attribute = target_fabric_discovery_attr_show,
1176 .store_attribute = target_fabric_discovery_attr_store,
1177};
1178
1179TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1180
1181/* End of tfc_discovery_cit */
1182
1183int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1184{
1185 target_fabric_setup_discovery_cit(tf);
1186 target_fabric_setup_wwn_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001187 target_fabric_setup_wwn_fabric_stats_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001188 target_fabric_setup_tpg_cit(tf);
1189 target_fabric_setup_tpg_base_cit(tf);
1190 target_fabric_setup_tpg_port_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001191 target_fabric_setup_tpg_port_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 target_fabric_setup_tpg_lun_cit(tf);
1193 target_fabric_setup_tpg_np_cit(tf);
1194 target_fabric_setup_tpg_np_base_cit(tf);
1195 target_fabric_setup_tpg_attrib_cit(tf);
1196 target_fabric_setup_tpg_param_cit(tf);
1197 target_fabric_setup_tpg_nacl_cit(tf);
1198 target_fabric_setup_tpg_nacl_base_cit(tf);
1199 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1200 target_fabric_setup_tpg_nacl_auth_cit(tf);
1201 target_fabric_setup_tpg_nacl_param_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001202 target_fabric_setup_tpg_nacl_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203 target_fabric_setup_tpg_mappedlun_cit(tf);
Nicholas Bellinger12d2338422011-03-14 04:06:11 -07001204 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205
1206 return 0;
1207}