Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / target / target_core_fabric_configfs.c
CommitLineData
c66ac9db
NB
1/*******************************************************************************
2* Filename: target_core_fabric_configfs.c
3 *
4 * This file contains generic fabric module configfs infrastructure for
5 * TCM v4.x code
6 *
fd9a11d7 7 * (c) Copyright 2010-2012 RisingTide Systems LLC.
c66ac9db 8 *
fd9a11d7 9 * Nicholas A. Bellinger <nab@linux-iscsi.org>
c66ac9db
NB
10*
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>
c66ac9db
NB
24#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>
c4795fb2 37#include <target/target_core_fabric.h>
c66ac9db
NB
38#include <target/target_core_fabric_configfs.h>
39#include <target/target_core_configfs.h>
40#include <target/configfs_macros.h>
41
e26d99ae 42#include "target_core_internal.h"
c66ac9db 43#include "target_core_alua.h"
c66ac9db
NB
44#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; \
6708bb27 56 pr_debug("Setup generic %s\n", __stringify(_name)); \
c66ac9db
NB
57}
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;
0ff87549
NB
73
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 }
c66ac9db
NB
79 /*
80 * Ensure that the source port exists
81 */
6708bb27
AG
82 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
83 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
c66ac9db
NB
84 "_tpg does not exist\n");
85 return -EINVAL;
86 }
003bea4a
NB
87 if (lun->lun_shutdown) {
88 pr_err("Unable to create mappedlun symlink because"
89 " lun->lun_shutdown=true\n");
90 return -EINVAL;
91 }
c66ac9db
NB
92 se_tpg = lun->lun_sep->sep_tpg;
93
94 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
95 tpg_ci = &nacl_ci->ci_group->cg_item;
96 wwn_ci = &tpg_ci->ci_group->cg_item;
97 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
98 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
99 /*
100 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
101 */
102 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
6708bb27 103 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
c66ac9db
NB
104 config_item_name(wwn_ci));
105 return -EINVAL;
106 }
107 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
6708bb27 108 pr_err("Illegal Initiator ACL Symlink outside of %s"
c66ac9db
NB
109 " TPGT: %s\n", config_item_name(wwn_ci),
110 config_item_name(tpg_ci));
111 return -EINVAL;
112 }
113 /*
114 * If this struct se_node_acl was dynamically generated with
115 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
116 * which be will write protected (READ-ONLY) when
117 * tpg_1/attrib/demo_mode_write_protect=1
118 */
119 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
f2083241 120 deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun];
c66ac9db
NB
121 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
122 lun_access = deve->lun_flags;
123 else
124 lun_access =
e3d6f909 125 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
c66ac9db
NB
126 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
127 TRANSPORT_LUNFLAGS_READ_WRITE;
128 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
129 /*
130 * Determine the actual mapped LUN value user wants..
131 *
132 * This value is what the SCSI Initiator actually sees the
133 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
134 */
135 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
136 lun->unpacked_lun, lun_access);
137
138 return (ret < 0) ? -EINVAL : 0;
139}
140
141static int target_fabric_mappedlun_unlink(
142 struct config_item *lun_acl_ci,
143 struct config_item *lun_ci)
144{
145 struct se_lun *lun;
146 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
147 struct se_lun_acl, se_lun_group);
148 struct se_node_acl *nacl = lacl->se_lun_nacl;
f2083241 149 struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun];
c66ac9db
NB
150 struct se_portal_group *se_tpg;
151 /*
152 * Determine if the underlying MappedLUN has already been released..
153 */
6708bb27 154 if (!deve->se_lun)
c66ac9db
NB
155 return 0;
156
157 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
158 se_tpg = lun->lun_sep->sep_tpg;
159
160 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
161 return 0;
162}
163
164CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
165#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
166static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
167 __CONFIGFS_EATTR(_name, _mode, \
168 target_fabric_mappedlun_show_##_name, \
169 target_fabric_mappedlun_store_##_name);
170
171static ssize_t target_fabric_mappedlun_show_write_protect(
172 struct se_lun_acl *lacl,
173 char *page)
174{
175 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
176 struct se_dev_entry *deve;
177 ssize_t len;
178
179 spin_lock_irq(&se_nacl->device_list_lock);
f2083241 180 deve = se_nacl->device_list[lacl->mapped_lun];
c66ac9db
NB
181 len = sprintf(page, "%d\n",
182 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
183 1 : 0);
184 spin_unlock_irq(&se_nacl->device_list_lock);
185
186 return len;
187}
188
189static ssize_t target_fabric_mappedlun_store_write_protect(
190 struct se_lun_acl *lacl,
191 const char *page,
192 size_t count)
193{
194 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
195 struct se_portal_group *se_tpg = se_nacl->se_tpg;
196 unsigned long op;
197
198 if (strict_strtoul(page, 0, &op))
199 return -EINVAL;
200
201 if ((op != 1) && (op != 0))
202 return -EINVAL;
203
204 core_update_device_list_access(lacl->mapped_lun, (op) ?
205 TRANSPORT_LUNFLAGS_READ_ONLY :
206 TRANSPORT_LUNFLAGS_READ_WRITE,
207 lacl->se_lun_nacl);
208
6708bb27 209 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
c66ac9db 210 " Mapped LUN: %u Write Protect bit to %s\n",
e3d6f909 211 se_tpg->se_tpg_tfo->get_fabric_name(),
c66ac9db
NB
212 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
213
214 return count;
215
216}
217
218TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
219
220CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
221
1f6fe7cb
NB
222static void target_fabric_mappedlun_release(struct config_item *item)
223{
224 struct se_lun_acl *lacl = container_of(to_config_group(item),
225 struct se_lun_acl, se_lun_group);
226 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
227
228 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
229}
230
c66ac9db
NB
231static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
232 &target_fabric_mappedlun_write_protect.attr,
233 NULL,
234};
235
236static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
1f6fe7cb 237 .release = target_fabric_mappedlun_release,
c66ac9db
NB
238 .show_attribute = target_fabric_mappedlun_attr_show,
239 .store_attribute = target_fabric_mappedlun_attr_store,
240 .allow_link = target_fabric_mappedlun_link,
241 .drop_link = target_fabric_mappedlun_unlink,
242};
243
244TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
245 target_fabric_mappedlun_attrs);
246
247/* End of tfc_tpg_mappedlun_cit */
248
12d23384
NB
249/* Start of tfc_tpg_mappedlun_port_cit */
250
251static struct config_group *target_core_mappedlun_stat_mkdir(
252 struct config_group *group,
253 const char *name)
254{
255 return ERR_PTR(-ENOSYS);
256}
257
258static void target_core_mappedlun_stat_rmdir(
259 struct config_group *group,
260 struct config_item *item)
261{
262 return;
263}
264
265static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
266 .make_group = target_core_mappedlun_stat_mkdir,
267 .drop_item = target_core_mappedlun_stat_rmdir,
268};
269
270TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
271 NULL);
272
273/* End of tfc_tpg_mappedlun_port_cit */
274
c66ac9db
NB
275/* Start of tfc_tpg_nacl_attrib_cit */
276
277CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
278
279static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
280 .show_attribute = target_fabric_nacl_attrib_attr_show,
281 .store_attribute = target_fabric_nacl_attrib_attr_store,
282};
283
284TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
285
286/* End of tfc_tpg_nacl_attrib_cit */
287
288/* Start of tfc_tpg_nacl_auth_cit */
289
290CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
291
292static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
293 .show_attribute = target_fabric_nacl_auth_attr_show,
294 .store_attribute = target_fabric_nacl_auth_attr_store,
295};
296
297TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
298
299/* End of tfc_tpg_nacl_auth_cit */
300
301/* Start of tfc_tpg_nacl_param_cit */
302
303CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
304
305static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
306 .show_attribute = target_fabric_nacl_param_attr_show,
307 .store_attribute = target_fabric_nacl_param_attr_store,
308};
309
310TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
311
312/* End of tfc_tpg_nacl_param_cit */
313
314/* Start of tfc_tpg_nacl_base_cit */
315
316CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
317
318static struct config_group *target_fabric_make_mappedlun(
319 struct config_group *group,
320 const char *name)
321{
322 struct se_node_acl *se_nacl = container_of(group,
323 struct se_node_acl, acl_group);
324 struct se_portal_group *se_tpg = se_nacl->se_tpg;
325 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
326 struct se_lun_acl *lacl;
327 struct config_item *acl_ci;
12d23384 328 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
c66ac9db
NB
329 char *buf;
330 unsigned long mapped_lun;
331 int ret = 0;
332
333 acl_ci = &group->cg_item;
6708bb27
AG
334 if (!acl_ci) {
335 pr_err("Unable to locatel acl_ci\n");
c66ac9db
NB
336 return NULL;
337 }
338
339 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
6708bb27
AG
340 if (!buf) {
341 pr_err("Unable to allocate memory for name buf\n");
c66ac9db
NB
342 return ERR_PTR(-ENOMEM);
343 }
344 snprintf(buf, strlen(name) + 1, "%s", name);
345 /*
346 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
347 */
348 if (strstr(buf, "lun_") != buf) {
6708bb27 349 pr_err("Unable to locate \"lun_\" from buf: %s"
c66ac9db
NB
350 " name: %s\n", buf, name);
351 ret = -EINVAL;
352 goto out;
353 }
354 /*
355 * Determine the Mapped LUN value. This is what the SCSI Initiator
356 * Port will actually see.
357 */
358 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
359 ret = -EINVAL;
360 goto out;
361 }
fbbf8555
NB
362 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
363 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
364 "-1: %u for Target Portal Group: %u\n", mapped_lun,
365 TRANSPORT_MAX_LUNS_PER_TPG-1,
366 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
367 ret = -EINVAL;
368 goto out;
369 }
c66ac9db 370
fcf29481
NB
371 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
372 mapped_lun, &ret);
6708bb27 373 if (!lacl) {
12d23384 374 ret = -EINVAL;
c66ac9db 375 goto out;
12d23384
NB
376 }
377
378 lacl_cg = &lacl->se_lun_group;
13f6a914 379 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
12d23384
NB
380 GFP_KERNEL);
381 if (!lacl_cg->default_groups) {
6708bb27 382 pr_err("Unable to allocate lacl_cg->default_groups\n");
12d23384
NB
383 ret = -ENOMEM;
384 goto out;
385 }
c66ac9db
NB
386
387 config_group_init_type_name(&lacl->se_lun_group, name,
388 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
12d23384
NB
389 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
390 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
391 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
392 lacl_cg->default_groups[1] = NULL;
393
e3d6f909 394 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
13f6a914 395 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
12d23384
NB
396 GFP_KERNEL);
397 if (!ml_stat_grp->default_groups) {
6708bb27 398 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
12d23384
NB
399 ret = -ENOMEM;
400 goto out;
401 }
402 target_stat_setup_mappedlun_default_groups(lacl);
c66ac9db
NB
403
404 kfree(buf);
405 return &lacl->se_lun_group;
406out:
12d23384
NB
407 if (lacl_cg)
408 kfree(lacl_cg->default_groups);
c66ac9db
NB
409 kfree(buf);
410 return ERR_PTR(ret);
411}
412
413static void target_fabric_drop_mappedlun(
414 struct config_group *group,
415 struct config_item *item)
416{
12d23384
NB
417 struct se_lun_acl *lacl = container_of(to_config_group(item),
418 struct se_lun_acl, se_lun_group);
419 struct config_item *df_item;
420 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
421 int i;
422
e3d6f909 423 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
12d23384
NB
424 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
425 df_item = &ml_stat_grp->default_groups[i]->cg_item;
426 ml_stat_grp->default_groups[i] = NULL;
427 config_item_put(df_item);
428 }
429 kfree(ml_stat_grp->default_groups);
430
431 lacl_cg = &lacl->se_lun_group;
432 for (i = 0; lacl_cg->default_groups[i]; i++) {
433 df_item = &lacl_cg->default_groups[i]->cg_item;
434 lacl_cg->default_groups[i] = NULL;
435 config_item_put(df_item);
436 }
437 kfree(lacl_cg->default_groups);
438
c66ac9db 439 config_item_put(item);
1f6fe7cb
NB
440}
441
442static void target_fabric_nacl_base_release(struct config_item *item)
443{
444 struct se_node_acl *se_nacl = container_of(to_config_group(item),
445 struct se_node_acl, acl_group);
446 struct se_portal_group *se_tpg = se_nacl->se_tpg;
447 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
448
449 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
c66ac9db
NB
450}
451
452static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
1f6fe7cb 453 .release = target_fabric_nacl_base_release,
c66ac9db
NB
454 .show_attribute = target_fabric_nacl_base_attr_show,
455 .store_attribute = target_fabric_nacl_base_attr_store,
456};
457
458static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
459 .make_group = target_fabric_make_mappedlun,
460 .drop_item = target_fabric_drop_mappedlun,
461};
462
463TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
464 &target_fabric_nacl_base_group_ops, NULL);
465
466/* End of tfc_tpg_nacl_base_cit */
467
12d23384
NB
468/* Start of tfc_node_fabric_stats_cit */
469/*
470 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
471 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
472 */
473TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
474
475/* End of tfc_wwn_fabric_stats_cit */
476
c66ac9db
NB
477/* Start of tfc_tpg_nacl_cit */
478
479static struct config_group *target_fabric_make_nodeacl(
480 struct config_group *group,
481 const char *name)
482{
483 struct se_portal_group *se_tpg = container_of(group,
484 struct se_portal_group, tpg_acl_group);
485 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
486 struct se_node_acl *se_nacl;
487 struct config_group *nacl_cg;
488
6708bb27
AG
489 if (!tf->tf_ops.fabric_make_nodeacl) {
490 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
c66ac9db
NB
491 return ERR_PTR(-ENOSYS);
492 }
493
494 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
495 if (IS_ERR(se_nacl))
e1750ba2 496 return ERR_CAST(se_nacl);
c66ac9db
NB
497
498 nacl_cg = &se_nacl->acl_group;
499 nacl_cg->default_groups = se_nacl->acl_default_groups;
500 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
501 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
502 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
12d23384
NB
503 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
504 nacl_cg->default_groups[4] = NULL;
c66ac9db
NB
505
506 config_group_init_type_name(&se_nacl->acl_group, name,
507 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
508 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
509 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
510 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
511 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
512 config_group_init_type_name(&se_nacl->acl_param_group, "param",
513 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
12d23384
NB
514 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
515 "fabric_statistics",
516 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
c66ac9db
NB
517
518 return &se_nacl->acl_group;
519}
520
521static void target_fabric_drop_nodeacl(
522 struct config_group *group,
523 struct config_item *item)
524{
c66ac9db
NB
525 struct se_node_acl *se_nacl = container_of(to_config_group(item),
526 struct se_node_acl, acl_group);
527 struct config_item *df_item;
528 struct config_group *nacl_cg;
529 int i;
530
531 nacl_cg = &se_nacl->acl_group;
532 for (i = 0; nacl_cg->default_groups[i]; i++) {
533 df_item = &nacl_cg->default_groups[i]->cg_item;
534 nacl_cg->default_groups[i] = NULL;
535 config_item_put(df_item);
536 }
1f6fe7cb
NB
537 /*
538 * struct se_node_acl free is done in target_fabric_nacl_base_release()
539 */
c66ac9db 540 config_item_put(item);
c66ac9db
NB
541}
542
543static struct configfs_group_operations target_fabric_nacl_group_ops = {
544 .make_group = target_fabric_make_nodeacl,
545 .drop_item = target_fabric_drop_nodeacl,
546};
547
548TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
549
550/* End of tfc_tpg_nacl_cit */
551
552/* Start of tfc_tpg_np_base_cit */
553
554CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
555
1f6fe7cb
NB
556static void target_fabric_np_base_release(struct config_item *item)
557{
558 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
559 struct se_tpg_np, tpg_np_group);
560 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
561 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
562
563 tf->tf_ops.fabric_drop_np(se_tpg_np);
564}
565
c66ac9db 566static struct configfs_item_operations target_fabric_np_base_item_ops = {
1f6fe7cb 567 .release = target_fabric_np_base_release,
c66ac9db
NB
568 .show_attribute = target_fabric_np_base_attr_show,
569 .store_attribute = target_fabric_np_base_attr_store,
570};
571
572TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
573
574/* End of tfc_tpg_np_base_cit */
575
576/* Start of tfc_tpg_np_cit */
577
578static struct config_group *target_fabric_make_np(
579 struct config_group *group,
580 const char *name)
581{
582 struct se_portal_group *se_tpg = container_of(group,
583 struct se_portal_group, tpg_np_group);
584 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
585 struct se_tpg_np *se_tpg_np;
586
6708bb27
AG
587 if (!tf->tf_ops.fabric_make_np) {
588 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
c66ac9db
NB
589 return ERR_PTR(-ENOSYS);
590 }
591
592 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
6708bb27 593 if (!se_tpg_np || IS_ERR(se_tpg_np))
c66ac9db
NB
594 return ERR_PTR(-EINVAL);
595
1f6fe7cb 596 se_tpg_np->tpg_np_parent = se_tpg;
c66ac9db
NB
597 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
598 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
599
600 return &se_tpg_np->tpg_np_group;
601}
602
603static void target_fabric_drop_np(
604 struct config_group *group,
605 struct config_item *item)
606{
1f6fe7cb
NB
607 /*
608 * struct se_tpg_np is released via target_fabric_np_base_release()
609 */
c66ac9db 610 config_item_put(item);
c66ac9db
NB
611}
612
613static struct configfs_group_operations target_fabric_np_group_ops = {
614 .make_group = &target_fabric_make_np,
615 .drop_item = &target_fabric_drop_np,
616};
617
618TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
619
620/* End of tfc_tpg_np_cit */
621
622/* Start of tfc_tpg_port_cit */
623
624CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
625#define TCM_PORT_ATTR(_name, _mode) \
626static struct target_fabric_port_attribute target_fabric_port_##_name = \
627 __CONFIGFS_EATTR(_name, _mode, \
628 target_fabric_port_show_attr_##_name, \
629 target_fabric_port_store_attr_##_name);
630
631#define TCM_PORT_ATTOR_RO(_name) \
632 __CONFIGFS_EATTR_RO(_name, \
633 target_fabric_port_show_attr_##_name);
634
635/*
636 * alua_tg_pt_gp
637 */
638static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
639 struct se_lun *lun,
640 char *page)
641{
6708bb27 642 if (!lun || !lun->lun_sep)
c66ac9db
NB
643 return -ENODEV;
644
645 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
646}
647
648static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
649 struct se_lun *lun,
650 const char *page,
651 size_t count)
652{
6708bb27 653 if (!lun || !lun->lun_sep)
c66ac9db
NB
654 return -ENODEV;
655
656 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
657}
658
659TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
660
661/*
662 * alua_tg_pt_offline
663 */
664static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
665 struct se_lun *lun,
666 char *page)
667{
6708bb27 668 if (!lun || !lun->lun_sep)
c66ac9db
NB
669 return -ENODEV;
670
671 return core_alua_show_offline_bit(lun, page);
672}
673
674static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
675 struct se_lun *lun,
676 const char *page,
677 size_t count)
678{
6708bb27 679 if (!lun || !lun->lun_sep)
c66ac9db
NB
680 return -ENODEV;
681
682 return core_alua_store_offline_bit(lun, page, count);
683}
684
685TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
686
687/*
688 * alua_tg_pt_status
689 */
690static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
691 struct se_lun *lun,
692 char *page)
693{
6708bb27 694 if (!lun || !lun->lun_sep)
c66ac9db
NB
695 return -ENODEV;
696
697 return core_alua_show_secondary_status(lun, page);
698}
699
700static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
701 struct se_lun *lun,
702 const char *page,
703 size_t count)
704{
6708bb27 705 if (!lun || !lun->lun_sep)
c66ac9db
NB
706 return -ENODEV;
707
708 return core_alua_store_secondary_status(lun, page, count);
709}
710
711TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
712
713/*
714 * alua_tg_pt_write_md
715 */
716static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
717 struct se_lun *lun,
718 char *page)
719{
6708bb27 720 if (!lun || !lun->lun_sep)
c66ac9db
NB
721 return -ENODEV;
722
723 return core_alua_show_secondary_write_metadata(lun, page);
724}
725
726static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
727 struct se_lun *lun,
728 const char *page,
729 size_t count)
730{
6708bb27 731 if (!lun || !lun->lun_sep)
c66ac9db
NB
732 return -ENODEV;
733
734 return core_alua_store_secondary_write_metadata(lun, page, count);
735}
736
737TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
738
739
740static struct configfs_attribute *target_fabric_port_attrs[] = {
741 &target_fabric_port_alua_tg_pt_gp.attr,
742 &target_fabric_port_alua_tg_pt_offline.attr,
743 &target_fabric_port_alua_tg_pt_status.attr,
744 &target_fabric_port_alua_tg_pt_write_md.attr,
745 NULL,
746};
747
748CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
749
750static int target_fabric_port_link(
751 struct config_item *lun_ci,
752 struct config_item *se_dev_ci)
753{
754 struct config_item *tpg_ci;
c66ac9db
NB
755 struct se_lun *lun = container_of(to_config_group(lun_ci),
756 struct se_lun, lun_group);
757 struct se_lun *lun_p;
758 struct se_portal_group *se_tpg;
0fd97ccf
CH
759 struct se_device *dev =
760 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
c66ac9db
NB
761 struct target_fabric_configfs *tf;
762 int ret;
763
0ff87549
NB
764 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
765 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
766 " %p to struct se_device: %p\n", se_dev_ci, dev);
767 return -EFAULT;
768 }
769
faa06ab9
NB
770 if (!(dev->dev_flags & DF_CONFIGURED)) {
771 pr_err("se_device not configured yet, cannot port link\n");
772 return -ENODEV;
773 }
774
c66ac9db
NB
775 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
776 se_tpg = container_of(to_config_group(tpg_ci),
777 struct se_portal_group, tpg_group);
778 tf = se_tpg->se_tpg_wwn->wwn_tf;
779
780 if (lun->lun_se_dev != NULL) {
6708bb27 781 pr_err("Port Symlink already exists\n");
c66ac9db
NB
782 return -EEXIST;
783 }
784
2dca673b 785 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
8d9efe53 786 if (IS_ERR(lun_p)) {
6708bb27 787 pr_err("core_dev_add_lun() failed\n");
8d9efe53 788 ret = PTR_ERR(lun_p);
c66ac9db
NB
789 goto out;
790 }
791
792 if (tf->tf_ops.fabric_post_link) {
793 /*
794 * Call the optional fabric_post_link() to allow a
795 * fabric module to setup any additional state once
796 * core_dev_add_lun() has been called..
797 */
798 tf->tf_ops.fabric_post_link(se_tpg, lun);
799 }
800
801 return 0;
802out:
803 return ret;
804}
805
806static int target_fabric_port_unlink(
807 struct config_item *lun_ci,
808 struct config_item *se_dev_ci)
809{
810 struct se_lun *lun = container_of(to_config_group(lun_ci),
811 struct se_lun, lun_group);
812 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
814
815 if (tf->tf_ops.fabric_pre_unlink) {
816 /*
817 * Call the optional fabric_pre_unlink() to allow a
818 * fabric module to release any additional stat before
819 * core_dev_del_lun() is called.
820 */
821 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
822 }
823
824 core_dev_del_lun(se_tpg, lun->unpacked_lun);
825 return 0;
826}
827
828static struct configfs_item_operations target_fabric_port_item_ops = {
829 .show_attribute = target_fabric_port_attr_show,
830 .store_attribute = target_fabric_port_attr_store,
831 .allow_link = target_fabric_port_link,
832 .drop_link = target_fabric_port_unlink,
833};
834
835TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
836
837/* End of tfc_tpg_port_cit */
838
12d23384
NB
839/* Start of tfc_tpg_port_stat_cit */
840
841static struct config_group *target_core_port_stat_mkdir(
842 struct config_group *group,
843 const char *name)
844{
845 return ERR_PTR(-ENOSYS);
846}
847
848static void target_core_port_stat_rmdir(
849 struct config_group *group,
850 struct config_item *item)
851{
852 return;
853}
854
855static struct configfs_group_operations target_fabric_port_stat_group_ops = {
856 .make_group = target_core_port_stat_mkdir,
857 .drop_item = target_core_port_stat_rmdir,
858};
859
860TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
861
862/* End of tfc_tpg_port_stat_cit */
863
c66ac9db
NB
864/* Start of tfc_tpg_lun_cit */
865
866static struct config_group *target_fabric_make_lun(
867 struct config_group *group,
868 const char *name)
869{
870 struct se_lun *lun;
871 struct se_portal_group *se_tpg = container_of(group,
872 struct se_portal_group, tpg_lun_group);
873 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
12d23384 874 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
c66ac9db 875 unsigned long unpacked_lun;
12d23384 876 int errno;
c66ac9db
NB
877
878 if (strstr(name, "lun_") != name) {
6708bb27 879 pr_err("Unable to locate \'_\" in"
c66ac9db
NB
880 " \"lun_$LUN_NUMBER\"\n");
881 return ERR_PTR(-EINVAL);
882 }
883 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
884 return ERR_PTR(-EINVAL);
885
886 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
6708bb27 887 if (!lun)
c66ac9db
NB
888 return ERR_PTR(-EINVAL);
889
12d23384 890 lun_cg = &lun->lun_group;
13f6a914 891 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
12d23384
NB
892 GFP_KERNEL);
893 if (!lun_cg->default_groups) {
6708bb27 894 pr_err("Unable to allocate lun_cg->default_groups\n");
12d23384
NB
895 return ERR_PTR(-ENOMEM);
896 }
897
c66ac9db
NB
898 config_group_init_type_name(&lun->lun_group, name,
899 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
12d23384
NB
900 config_group_init_type_name(&lun->port_stat_grps.stat_group,
901 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
902 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
903 lun_cg->default_groups[1] = NULL;
904
e3d6f909 905 port_stat_grp = &lun->port_stat_grps.stat_group;
12d23384
NB
906 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
907 GFP_KERNEL);
908 if (!port_stat_grp->default_groups) {
6708bb27 909 pr_err("Unable to allocate port_stat_grp->default_groups\n");
12d23384
NB
910 errno = -ENOMEM;
911 goto out;
912 }
913 target_stat_setup_port_default_groups(lun);
c66ac9db
NB
914
915 return &lun->lun_group;
12d23384
NB
916out:
917 if (lun_cg)
918 kfree(lun_cg->default_groups);
919 return ERR_PTR(errno);
c66ac9db
NB
920}
921
922static void target_fabric_drop_lun(
923 struct config_group *group,
924 struct config_item *item)
925{
12d23384
NB
926 struct se_lun *lun = container_of(to_config_group(item),
927 struct se_lun, lun_group);
928 struct config_item *df_item;
929 struct config_group *lun_cg, *port_stat_grp;
930 int i;
931
e3d6f909 932 port_stat_grp = &lun->port_stat_grps.stat_group;
12d23384
NB
933 for (i = 0; port_stat_grp->default_groups[i]; i++) {
934 df_item = &port_stat_grp->default_groups[i]->cg_item;
935 port_stat_grp->default_groups[i] = NULL;
936 config_item_put(df_item);
937 }
938 kfree(port_stat_grp->default_groups);
939
940 lun_cg = &lun->lun_group;
941 for (i = 0; lun_cg->default_groups[i]; i++) {
942 df_item = &lun_cg->default_groups[i]->cg_item;
943 lun_cg->default_groups[i] = NULL;
944 config_item_put(df_item);
945 }
946 kfree(lun_cg->default_groups);
947
c66ac9db
NB
948 config_item_put(item);
949}
950
951static struct configfs_group_operations target_fabric_lun_group_ops = {
952 .make_group = &target_fabric_make_lun,
953 .drop_item = &target_fabric_drop_lun,
954};
955
956TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
957
958/* End of tfc_tpg_lun_cit */
959
960/* Start of tfc_tpg_attrib_cit */
961
962CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
963
964static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
965 .show_attribute = target_fabric_tpg_attrib_attr_show,
966 .store_attribute = target_fabric_tpg_attrib_attr_store,
967};
968
969TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
970
971/* End of tfc_tpg_attrib_cit */
972
973/* Start of tfc_tpg_param_cit */
974
975CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
976
977static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
978 .show_attribute = target_fabric_tpg_param_attr_show,
979 .store_attribute = target_fabric_tpg_param_attr_store,
980};
981
982TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
983
984/* End of tfc_tpg_param_cit */
985
986/* Start of tfc_tpg_base_cit */
987/*
988 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
989 */
990CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
991
1f6fe7cb
NB
992static void target_fabric_tpg_release(struct config_item *item)
993{
994 struct se_portal_group *se_tpg = container_of(to_config_group(item),
995 struct se_portal_group, tpg_group);
996 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
997 struct target_fabric_configfs *tf = wwn->wwn_tf;
998
999 tf->tf_ops.fabric_drop_tpg(se_tpg);
1000}
1001
c66ac9db 1002static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1f6fe7cb 1003 .release = target_fabric_tpg_release,
c66ac9db
NB
1004 .show_attribute = target_fabric_tpg_attr_show,
1005 .store_attribute = target_fabric_tpg_attr_store,
1006};
1007
1008TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
1009
1010/* End of tfc_tpg_base_cit */
1011
1012/* Start of tfc_tpg_cit */
1013
1014static struct config_group *target_fabric_make_tpg(
1015 struct config_group *group,
1016 const char *name)
1017{
1018 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1019 struct target_fabric_configfs *tf = wwn->wwn_tf;
1020 struct se_portal_group *se_tpg;
1021
6708bb27
AG
1022 if (!tf->tf_ops.fabric_make_tpg) {
1023 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
c66ac9db
NB
1024 return ERR_PTR(-ENOSYS);
1025 }
1026
1027 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
6708bb27 1028 if (!se_tpg || IS_ERR(se_tpg))
c66ac9db
NB
1029 return ERR_PTR(-EINVAL);
1030 /*
1031 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1032 */
1033 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1034 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1035 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1036 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1037 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1038 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1039 se_tpg->tpg_group.default_groups[5] = NULL;
1040
1041 config_group_init_type_name(&se_tpg->tpg_group, name,
1042 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1043 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1044 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1045 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1046 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1047 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1048 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1049 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1050 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1051 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1052 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1053
1054 return &se_tpg->tpg_group;
1055}
1056
1057static void target_fabric_drop_tpg(
1058 struct config_group *group,
1059 struct config_item *item)
1060{
c66ac9db
NB
1061 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1062 struct se_portal_group, tpg_group);
1063 struct config_group *tpg_cg = &se_tpg->tpg_group;
1064 struct config_item *df_item;
1065 int i;
1066 /*
1067 * Release default groups, but do not release tpg_cg->default_groups
1068 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1069 */
1070 for (i = 0; tpg_cg->default_groups[i]; i++) {
1071 df_item = &tpg_cg->default_groups[i]->cg_item;
1072 tpg_cg->default_groups[i] = NULL;
1073 config_item_put(df_item);
1074 }
1075
1076 config_item_put(item);
c66ac9db
NB
1077}
1078
1f6fe7cb
NB
1079static void target_fabric_release_wwn(struct config_item *item)
1080{
1081 struct se_wwn *wwn = container_of(to_config_group(item),
1082 struct se_wwn, wwn_group);
1083 struct target_fabric_configfs *tf = wwn->wwn_tf;
1084
1085 tf->tf_ops.fabric_drop_wwn(wwn);
1086}
1087
1088static struct configfs_item_operations target_fabric_tpg_item_ops = {
1089 .release = target_fabric_release_wwn,
1090};
1091
c66ac9db
NB
1092static struct configfs_group_operations target_fabric_tpg_group_ops = {
1093 .make_group = target_fabric_make_tpg,
1094 .drop_item = target_fabric_drop_tpg,
1095};
1096
1f6fe7cb
NB
1097TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1098 NULL);
c66ac9db
NB
1099
1100/* End of tfc_tpg_cit */
1101
12d23384
NB
1102/* Start of tfc_wwn_fabric_stats_cit */
1103/*
1104 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1105 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1106 */
1107TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1108
1109/* End of tfc_wwn_fabric_stats_cit */
1110
c66ac9db
NB
1111/* Start of tfc_wwn_cit */
1112
1113static struct config_group *target_fabric_make_wwn(
1114 struct config_group *group,
1115 const char *name)
1116{
1117 struct target_fabric_configfs *tf = container_of(group,
1118 struct target_fabric_configfs, tf_group);
1119 struct se_wwn *wwn;
1120
6708bb27
AG
1121 if (!tf->tf_ops.fabric_make_wwn) {
1122 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
c66ac9db
NB
1123 return ERR_PTR(-ENOSYS);
1124 }
1125
1126 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
6708bb27 1127 if (!wwn || IS_ERR(wwn))
c66ac9db
NB
1128 return ERR_PTR(-EINVAL);
1129
1130 wwn->wwn_tf = tf;
12d23384
NB
1131 /*
1132 * Setup default groups from pre-allocated wwn->wwn_default_groups
1133 */
1134 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1135 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1136 wwn->wwn_group.default_groups[1] = NULL;
1137
c66ac9db
NB
1138 config_group_init_type_name(&wwn->wwn_group, name,
1139 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
12d23384
NB
1140 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1141 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
c66ac9db
NB
1142
1143 return &wwn->wwn_group;
1144}
1145
1146static void target_fabric_drop_wwn(
1147 struct config_group *group,
1148 struct config_item *item)
1149{
12d23384
NB
1150 struct se_wwn *wwn = container_of(to_config_group(item),
1151 struct se_wwn, wwn_group);
1152 struct config_item *df_item;
1153 struct config_group *cg = &wwn->wwn_group;
1154 int i;
1155
1156 for (i = 0; cg->default_groups[i]; i++) {
1157 df_item = &cg->default_groups[i]->cg_item;
1158 cg->default_groups[i] = NULL;
1159 config_item_put(df_item);
1160 }
1161
c66ac9db 1162 config_item_put(item);
c66ac9db
NB
1163}
1164
1165static struct configfs_group_operations target_fabric_wwn_group_ops = {
1166 .make_group = target_fabric_make_wwn,
1167 .drop_item = target_fabric_drop_wwn,
1168};
1169/*
1170 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1171 */
1172CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1173
1174static struct configfs_item_operations target_fabric_wwn_item_ops = {
1175 .show_attribute = target_fabric_wwn_attr_show,
1176 .store_attribute = target_fabric_wwn_attr_store,
1177};
1178
1179TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1180
1181/* End of tfc_wwn_cit */
1182
1183/* Start of tfc_discovery_cit */
1184
1185CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1186 tf_disc_group);
1187
1188static struct configfs_item_operations target_fabric_discovery_item_ops = {
1189 .show_attribute = target_fabric_discovery_attr_show,
1190 .store_attribute = target_fabric_discovery_attr_store,
1191};
1192
1193TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1194
1195/* End of tfc_discovery_cit */
1196
1197int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1198{
1199 target_fabric_setup_discovery_cit(tf);
1200 target_fabric_setup_wwn_cit(tf);
12d23384 1201 target_fabric_setup_wwn_fabric_stats_cit(tf);
c66ac9db
NB
1202 target_fabric_setup_tpg_cit(tf);
1203 target_fabric_setup_tpg_base_cit(tf);
1204 target_fabric_setup_tpg_port_cit(tf);
12d23384 1205 target_fabric_setup_tpg_port_stat_cit(tf);
c66ac9db
NB
1206 target_fabric_setup_tpg_lun_cit(tf);
1207 target_fabric_setup_tpg_np_cit(tf);
1208 target_fabric_setup_tpg_np_base_cit(tf);
1209 target_fabric_setup_tpg_attrib_cit(tf);
1210 target_fabric_setup_tpg_param_cit(tf);
1211 target_fabric_setup_tpg_nacl_cit(tf);
1212 target_fabric_setup_tpg_nacl_base_cit(tf);
1213 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1214 target_fabric_setup_tpg_nacl_auth_cit(tf);
1215 target_fabric_setup_tpg_nacl_param_cit(tf);
12d23384 1216 target_fabric_setup_tpg_nacl_stat_cit(tf);
c66ac9db 1217 target_fabric_setup_tpg_mappedlun_cit(tf);
12d23384 1218 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
c66ac9db
NB
1219
1220 return 0;
1221}