target: Drop duplicate + unused se_dev_check_wce
[deliverable/linux.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 *
4c76251e 7 * (c) Copyright 2010-2013 Datera, Inc.
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{ \
968ebe75 49 struct config_item_type *cit = &tf->tf_##_name##_cit; \
c66ac9db
NB
50 \
51 cit->ct_item_ops = _item_ops; \
52 cit->ct_group_ops = _group_ops; \
53 cit->ct_attrs = _attrs; \
0dc2e8d1 54 cit->ct_owner = tf->tf_ops->module; \
6708bb27 55 pr_debug("Setup generic %s\n", __stringify(_name)); \
c66ac9db
NB
56}
57
9ac8928e
CH
58#define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
59static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
60{ \
968ebe75 61 struct config_item_type *cit = &tf->tf_##_name##_cit; \
ef0caf8d 62 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
9ac8928e
CH
63 \
64 cit->ct_item_ops = _item_ops; \
65 cit->ct_group_ops = _group_ops; \
66 cit->ct_attrs = attrs; \
0dc2e8d1 67 cit->ct_owner = tf->tf_ops->module; \
9ac8928e
CH
68 pr_debug("Setup generic %s\n", __stringify(_name)); \
69}
70
c66ac9db
NB
71/* Start of tfc_tpg_mappedlun_cit */
72
73static int target_fabric_mappedlun_link(
74 struct config_item *lun_acl_ci,
75 struct config_item *lun_ci)
76{
77 struct se_dev_entry *deve;
78 struct se_lun *lun = container_of(to_config_group(lun_ci),
79 struct se_lun, lun_group);
80 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
81 struct se_lun_acl, se_lun_group);
82 struct se_portal_group *se_tpg;
83 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
6bb82612 84 int lun_access;
0ff87549
NB
85
86 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
87 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
88 " %p to struct lun: %p\n", lun_ci, lun);
89 return -EFAULT;
90 }
c66ac9db
NB
91 /*
92 * Ensure that the source port exists
93 */
adf653f9
CH
94 if (!lun->lun_se_dev) {
95 pr_err("Source se_lun->lun_se_dev does not exist\n");
c66ac9db
NB
96 return -EINVAL;
97 }
adf653f9 98 se_tpg = lun->lun_tpg;
c66ac9db
NB
99
100 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
101 tpg_ci = &nacl_ci->ci_group->cg_item;
102 wwn_ci = &tpg_ci->ci_group->cg_item;
103 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
104 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
105 /*
106 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
107 */
108 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
6708bb27 109 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
c66ac9db
NB
110 config_item_name(wwn_ci));
111 return -EINVAL;
112 }
113 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
6708bb27 114 pr_err("Illegal Initiator ACL Symlink outside of %s"
c66ac9db
NB
115 " TPGT: %s\n", config_item_name(wwn_ci),
116 config_item_name(tpg_ci));
117 return -EINVAL;
118 }
119 /*
120 * If this struct se_node_acl was dynamically generated with
121 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
122 * which be will write protected (READ-ONLY) when
123 * tpg_1/attrib/demo_mode_write_protect=1
124 */
29a05dee
NB
125 rcu_read_lock();
126 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
127 if (deve)
c66ac9db
NB
128 lun_access = deve->lun_flags;
129 else
130 lun_access =
e3d6f909 131 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
c66ac9db
NB
132 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
133 TRANSPORT_LUNFLAGS_READ_WRITE;
29a05dee 134 rcu_read_unlock();
c66ac9db
NB
135 /*
136 * Determine the actual mapped LUN value user wants..
137 *
138 * This value is what the SCSI Initiator actually sees the
6bb82612 139 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
c66ac9db 140 */
6bb82612 141 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
c66ac9db
NB
142}
143
144static int target_fabric_mappedlun_unlink(
145 struct config_item *lun_acl_ci,
146 struct config_item *lun_ci)
147{
c66ac9db
NB
148 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
149 struct se_lun_acl, se_lun_group);
29a05dee
NB
150 struct se_lun *lun = container_of(to_config_group(lun_ci),
151 struct se_lun, lun_group);
c66ac9db 152
adf653f9 153 return core_dev_del_initiator_node_lun_acl(lun, lacl);
c66ac9db
NB
154}
155
156CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
157#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
158static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
159 __CONFIGFS_EATTR(_name, _mode, \
160 target_fabric_mappedlun_show_##_name, \
161 target_fabric_mappedlun_store_##_name);
162
163static ssize_t target_fabric_mappedlun_show_write_protect(
164 struct se_lun_acl *lacl,
165 char *page)
166{
167 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
168 struct se_dev_entry *deve;
29a05dee
NB
169 ssize_t len = 0;
170
171 rcu_read_lock();
172 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
173 if (deve) {
174 len = sprintf(page, "%d\n",
175 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
176 }
177 rcu_read_unlock();
c66ac9db
NB
178
179 return len;
180}
181
182static ssize_t target_fabric_mappedlun_store_write_protect(
183 struct se_lun_acl *lacl,
184 const char *page,
185 size_t count)
186{
187 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
188 struct se_portal_group *se_tpg = se_nacl->se_tpg;
189 unsigned long op;
57103d7f 190 int ret;
c66ac9db 191
57103d7f
JH
192 ret = kstrtoul(page, 0, &op);
193 if (ret)
194 return ret;
c66ac9db
NB
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
6708bb27 204 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
c66ac9db 205 " Mapped LUN: %u Write Protect bit to %s\n",
e3d6f909 206 se_tpg->se_tpg_tfo->get_fabric_name(),
c66ac9db
NB
207 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
1f6fe7cb
NB
217static 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
c66ac9db
NB
226static 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 = {
1f6fe7cb 232 .release = target_fabric_mappedlun_release,
c66ac9db
NB
233 .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
12d23384
NB
244/* 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
c66ac9db
NB
270/* 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
9ac8928e 279TF_CIT_SETUP_DRV(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL);
c66ac9db
NB
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
9ac8928e 292TF_CIT_SETUP_DRV(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL);
c66ac9db
NB
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
9ac8928e 305TF_CIT_SETUP_DRV(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL);
c66ac9db
NB
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;
da0abaee 321 struct se_lun_acl *lacl = NULL;
c66ac9db 322 struct config_item *acl_ci;
12d23384 323 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
c66ac9db
NB
324 char *buf;
325 unsigned long mapped_lun;
326 int ret = 0;
327
328 acl_ci = &group->cg_item;
6708bb27
AG
329 if (!acl_ci) {
330 pr_err("Unable to locatel acl_ci\n");
c66ac9db
NB
331 return NULL;
332 }
333
334 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
6708bb27
AG
335 if (!buf) {
336 pr_err("Unable to allocate memory for name buf\n");
c66ac9db
NB
337 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) {
6708bb27 344 pr_err("Unable to locate \"lun_\" from buf: %s"
c66ac9db
NB
345 " 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 */
57103d7f
JH
353 ret = kstrtoul(buf + 4, 0, &mapped_lun);
354 if (ret)
355 goto out;
356 if (mapped_lun > UINT_MAX) {
c66ac9db
NB
357 ret = -EINVAL;
358 goto out;
359 }
fbbf8555
NB
360 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
361 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
362 "-1: %u for Target Portal Group: %u\n", mapped_lun,
363 TRANSPORT_MAX_LUNS_PER_TPG-1,
364 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
365 ret = -EINVAL;
366 goto out;
367 }
c66ac9db 368
fcf29481
NB
369 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
370 mapped_lun, &ret);
6708bb27 371 if (!lacl) {
12d23384 372 ret = -EINVAL;
c66ac9db 373 goto out;
12d23384
NB
374 }
375
376 lacl_cg = &lacl->se_lun_group;
13f6a914 377 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
12d23384
NB
378 GFP_KERNEL);
379 if (!lacl_cg->default_groups) {
6708bb27 380 pr_err("Unable to allocate lacl_cg->default_groups\n");
12d23384
NB
381 ret = -ENOMEM;
382 goto out;
383 }
c66ac9db
NB
384
385 config_group_init_type_name(&lacl->se_lun_group, name,
968ebe75 386 &tf->tf_tpg_mappedlun_cit);
12d23384 387 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
968ebe75 388 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
12d23384
NB
389 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
390 lacl_cg->default_groups[1] = NULL;
391
e3d6f909 392 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
13f6a914 393 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
12d23384
NB
394 GFP_KERNEL);
395 if (!ml_stat_grp->default_groups) {
6708bb27 396 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
12d23384
NB
397 ret = -ENOMEM;
398 goto out;
399 }
400 target_stat_setup_mappedlun_default_groups(lacl);
c66ac9db
NB
401
402 kfree(buf);
403 return &lacl->se_lun_group;
404out:
12d23384
NB
405 if (lacl_cg)
406 kfree(lacl_cg->default_groups);
da0abaee 407 kfree(lacl);
c66ac9db
NB
408 kfree(buf);
409 return ERR_PTR(ret);
410}
411
412static void target_fabric_drop_mappedlun(
413 struct config_group *group,
414 struct config_item *item)
415{
12d23384
NB
416 struct se_lun_acl *lacl = container_of(to_config_group(item),
417 struct se_lun_acl, se_lun_group);
418 struct config_item *df_item;
419 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
420 int i;
421
e3d6f909 422 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
12d23384
NB
423 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
424 df_item = &ml_stat_grp->default_groups[i]->cg_item;
425 ml_stat_grp->default_groups[i] = NULL;
426 config_item_put(df_item);
427 }
428 kfree(ml_stat_grp->default_groups);
429
430 lacl_cg = &lacl->se_lun_group;
431 for (i = 0; lacl_cg->default_groups[i]; i++) {
432 df_item = &lacl_cg->default_groups[i]->cg_item;
433 lacl_cg->default_groups[i] = NULL;
434 config_item_put(df_item);
435 }
436 kfree(lacl_cg->default_groups);
437
c66ac9db 438 config_item_put(item);
1f6fe7cb
NB
439}
440
441static void target_fabric_nacl_base_release(struct config_item *item)
442{
443 struct se_node_acl *se_nacl = container_of(to_config_group(item),
444 struct se_node_acl, acl_group);
c7d6a803 445 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
1f6fe7cb 446
ef0caf8d
CH
447 if (tf->tf_ops->fabric_cleanup_nodeacl)
448 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
c7d6a803 449 core_tpg_del_initiator_node_acl(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
9ac8928e
CH
463TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
464 &target_fabric_nacl_base_group_ops);
c66ac9db
NB
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
c7d6a803 489 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
c66ac9db 490 if (IS_ERR(se_nacl))
e1750ba2 491 return ERR_CAST(se_nacl);
c66ac9db 492
ef0caf8d
CH
493 if (tf->tf_ops->fabric_init_nodeacl) {
494 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
c7d6a803
CH
495 if (ret) {
496 core_tpg_del_initiator_node_acl(se_nacl);
497 return ERR_PTR(ret);
498 }
499 }
500
c66ac9db
NB
501 nacl_cg = &se_nacl->acl_group;
502 nacl_cg->default_groups = se_nacl->acl_default_groups;
503 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
504 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
505 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
12d23384
NB
506 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
507 nacl_cg->default_groups[4] = NULL;
c66ac9db
NB
508
509 config_group_init_type_name(&se_nacl->acl_group, name,
968ebe75 510 &tf->tf_tpg_nacl_base_cit);
c66ac9db 511 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
968ebe75 512 &tf->tf_tpg_nacl_attrib_cit);
c66ac9db 513 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
968ebe75 514 &tf->tf_tpg_nacl_auth_cit);
c66ac9db 515 config_group_init_type_name(&se_nacl->acl_param_group, "param",
968ebe75 516 &tf->tf_tpg_nacl_param_cit);
12d23384 517 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
968ebe75 518 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
c66ac9db
NB
519
520 return &se_nacl->acl_group;
521}
522
523static void target_fabric_drop_nodeacl(
524 struct config_group *group,
525 struct config_item *item)
526{
c66ac9db
NB
527 struct se_node_acl *se_nacl = container_of(to_config_group(item),
528 struct se_node_acl, acl_group);
529 struct config_item *df_item;
530 struct config_group *nacl_cg;
531 int i;
532
533 nacl_cg = &se_nacl->acl_group;
534 for (i = 0; nacl_cg->default_groups[i]; i++) {
535 df_item = &nacl_cg->default_groups[i]->cg_item;
536 nacl_cg->default_groups[i] = NULL;
537 config_item_put(df_item);
538 }
1f6fe7cb
NB
539 /*
540 * struct se_node_acl free is done in target_fabric_nacl_base_release()
541 */
c66ac9db 542 config_item_put(item);
c66ac9db
NB
543}
544
545static struct configfs_group_operations target_fabric_nacl_group_ops = {
546 .make_group = target_fabric_make_nodeacl,
547 .drop_item = target_fabric_drop_nodeacl,
548};
549
550TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
551
552/* End of tfc_tpg_nacl_cit */
553
554/* Start of tfc_tpg_np_base_cit */
555
556CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
557
1f6fe7cb
NB
558static void target_fabric_np_base_release(struct config_item *item)
559{
560 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
561 struct se_tpg_np, tpg_np_group);
562 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
563 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
564
ef0caf8d 565 tf->tf_ops->fabric_drop_np(se_tpg_np);
1f6fe7cb
NB
566}
567
c66ac9db 568static struct configfs_item_operations target_fabric_np_base_item_ops = {
1f6fe7cb 569 .release = target_fabric_np_base_release,
c66ac9db
NB
570 .show_attribute = target_fabric_np_base_attr_show,
571 .store_attribute = target_fabric_np_base_attr_store,
572};
573
9ac8928e 574TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
c66ac9db
NB
575
576/* End of tfc_tpg_np_base_cit */
577
578/* Start of tfc_tpg_np_cit */
579
580static struct config_group *target_fabric_make_np(
581 struct config_group *group,
582 const char *name)
583{
584 struct se_portal_group *se_tpg = container_of(group,
585 struct se_portal_group, tpg_np_group);
586 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
587 struct se_tpg_np *se_tpg_np;
588
ef0caf8d 589 if (!tf->tf_ops->fabric_make_np) {
6708bb27 590 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
c66ac9db
NB
591 return ERR_PTR(-ENOSYS);
592 }
593
ef0caf8d 594 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
6708bb27 595 if (!se_tpg_np || IS_ERR(se_tpg_np))
c66ac9db
NB
596 return ERR_PTR(-EINVAL);
597
1f6fe7cb 598 se_tpg_np->tpg_np_parent = se_tpg;
c66ac9db 599 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
968ebe75 600 &tf->tf_tpg_np_base_cit);
c66ac9db
NB
601
602 return &se_tpg_np->tpg_np_group;
603}
604
605static void target_fabric_drop_np(
606 struct config_group *group,
607 struct config_item *item)
608{
1f6fe7cb
NB
609 /*
610 * struct se_tpg_np is released via target_fabric_np_base_release()
611 */
c66ac9db 612 config_item_put(item);
c66ac9db
NB
613}
614
615static struct configfs_group_operations target_fabric_np_group_ops = {
616 .make_group = &target_fabric_make_np,
617 .drop_item = &target_fabric_drop_np,
618};
619
620TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
621
622/* End of tfc_tpg_np_cit */
623
624/* Start of tfc_tpg_port_cit */
625
626CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
627#define TCM_PORT_ATTR(_name, _mode) \
628static struct target_fabric_port_attribute target_fabric_port_##_name = \
629 __CONFIGFS_EATTR(_name, _mode, \
630 target_fabric_port_show_attr_##_name, \
631 target_fabric_port_store_attr_##_name);
632
633#define TCM_PORT_ATTOR_RO(_name) \
634 __CONFIGFS_EATTR_RO(_name, \
635 target_fabric_port_show_attr_##_name);
636
637/*
638 * alua_tg_pt_gp
639 */
640static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
641 struct se_lun *lun,
642 char *page)
643{
adf653f9 644 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
645 return -ENODEV;
646
adf653f9 647 return core_alua_show_tg_pt_gp_info(lun, page);
c66ac9db
NB
648}
649
650static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
651 struct se_lun *lun,
652 const char *page,
653 size_t count)
654{
adf653f9 655 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
656 return -ENODEV;
657
adf653f9 658 return core_alua_store_tg_pt_gp_info(lun, page, count);
c66ac9db
NB
659}
660
661TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
662
663/*
664 * alua_tg_pt_offline
665 */
666static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
667 struct se_lun *lun,
668 char *page)
669{
adf653f9 670 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
671 return -ENODEV;
672
673 return core_alua_show_offline_bit(lun, page);
674}
675
676static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
677 struct se_lun *lun,
678 const char *page,
679 size_t count)
680{
adf653f9 681 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
682 return -ENODEV;
683
684 return core_alua_store_offline_bit(lun, page, count);
685}
686
687TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
688
689/*
690 * alua_tg_pt_status
691 */
692static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
693 struct se_lun *lun,
694 char *page)
695{
adf653f9 696 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
697 return -ENODEV;
698
699 return core_alua_show_secondary_status(lun, page);
700}
701
702static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
703 struct se_lun *lun,
704 const char *page,
705 size_t count)
706{
adf653f9 707 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
708 return -ENODEV;
709
710 return core_alua_store_secondary_status(lun, page, count);
711}
712
713TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
714
715/*
716 * alua_tg_pt_write_md
717 */
718static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
719 struct se_lun *lun,
720 char *page)
721{
adf653f9 722 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
723 return -ENODEV;
724
725 return core_alua_show_secondary_write_metadata(lun, page);
726}
727
728static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
729 struct se_lun *lun,
730 const char *page,
731 size_t count)
732{
adf653f9 733 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
734 return -ENODEV;
735
736 return core_alua_store_secondary_write_metadata(lun, page, count);
737}
738
739TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
740
741
742static struct configfs_attribute *target_fabric_port_attrs[] = {
743 &target_fabric_port_alua_tg_pt_gp.attr,
744 &target_fabric_port_alua_tg_pt_offline.attr,
745 &target_fabric_port_alua_tg_pt_status.attr,
746 &target_fabric_port_alua_tg_pt_write_md.attr,
747 NULL,
748};
749
750CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
751
752static int target_fabric_port_link(
753 struct config_item *lun_ci,
754 struct config_item *se_dev_ci)
755{
756 struct config_item *tpg_ci;
c66ac9db
NB
757 struct se_lun *lun = container_of(to_config_group(lun_ci),
758 struct se_lun, lun_group);
c66ac9db 759 struct se_portal_group *se_tpg;
0fd97ccf
CH
760 struct se_device *dev =
761 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
c66ac9db
NB
762 struct target_fabric_configfs *tf;
763 int ret;
764
0ff87549
NB
765 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
766 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
767 " %p to struct se_device: %p\n", se_dev_ci, dev);
768 return -EFAULT;
769 }
770
faa06ab9
NB
771 if (!(dev->dev_flags & DF_CONFIGURED)) {
772 pr_err("se_device not configured yet, cannot port link\n");
773 return -ENODEV;
774 }
775
c66ac9db
NB
776 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
777 se_tpg = container_of(to_config_group(tpg_ci),
778 struct se_portal_group, tpg_group);
779 tf = se_tpg->se_tpg_wwn->wwn_tf;
780
781 if (lun->lun_se_dev != NULL) {
6708bb27 782 pr_err("Port Symlink already exists\n");
c66ac9db
NB
783 return -EEXIST;
784 }
785
6bb82612
NB
786 ret = core_dev_add_lun(se_tpg, dev, lun);
787 if (ret) {
788 pr_err("core_dev_add_lun() failed: %d\n", ret);
c66ac9db
NB
789 goto out;
790 }
791
ef0caf8d 792 if (tf->tf_ops->fabric_post_link) {
c66ac9db
NB
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 */
ef0caf8d 798 tf->tf_ops->fabric_post_link(se_tpg, lun);
c66ac9db
NB
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);
adf653f9 812 struct se_portal_group *se_tpg = lun->lun_tpg;
c66ac9db
NB
813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
814
ef0caf8d 815 if (tf->tf_ops->fabric_pre_unlink) {
c66ac9db
NB
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 */
ef0caf8d 821 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
c66ac9db
NB
822 }
823
cd9d7cba 824 core_dev_del_lun(se_tpg, lun);
c66ac9db
NB
825 return 0;
826}
827
6bb82612
NB
828static void target_fabric_port_release(struct config_item *item)
829{
830 struct se_lun *lun = container_of(to_config_group(item),
831 struct se_lun, lun_group);
832
833 kfree_rcu(lun, rcu_head);
834}
835
c66ac9db
NB
836static struct configfs_item_operations target_fabric_port_item_ops = {
837 .show_attribute = target_fabric_port_attr_show,
838 .store_attribute = target_fabric_port_attr_store,
6bb82612 839 .release = target_fabric_port_release,
c66ac9db
NB
840 .allow_link = target_fabric_port_link,
841 .drop_link = target_fabric_port_unlink,
842};
843
844TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
845
846/* End of tfc_tpg_port_cit */
847
12d23384
NB
848/* Start of tfc_tpg_port_stat_cit */
849
850static struct config_group *target_core_port_stat_mkdir(
851 struct config_group *group,
852 const char *name)
853{
854 return ERR_PTR(-ENOSYS);
855}
856
857static void target_core_port_stat_rmdir(
858 struct config_group *group,
859 struct config_item *item)
860{
861 return;
862}
863
864static struct configfs_group_operations target_fabric_port_stat_group_ops = {
865 .make_group = target_core_port_stat_mkdir,
866 .drop_item = target_core_port_stat_rmdir,
867};
868
869TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
870
871/* End of tfc_tpg_port_stat_cit */
872
c66ac9db
NB
873/* Start of tfc_tpg_lun_cit */
874
875static struct config_group *target_fabric_make_lun(
876 struct config_group *group,
877 const char *name)
878{
879 struct se_lun *lun;
880 struct se_portal_group *se_tpg = container_of(group,
881 struct se_portal_group, tpg_lun_group);
882 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
12d23384 883 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
c66ac9db 884 unsigned long unpacked_lun;
12d23384 885 int errno;
c66ac9db
NB
886
887 if (strstr(name, "lun_") != name) {
6708bb27 888 pr_err("Unable to locate \'_\" in"
c66ac9db
NB
889 " \"lun_$LUN_NUMBER\"\n");
890 return ERR_PTR(-EINVAL);
891 }
57103d7f
JH
892 errno = kstrtoul(name + 4, 0, &unpacked_lun);
893 if (errno)
894 return ERR_PTR(errno);
895 if (unpacked_lun > UINT_MAX)
c66ac9db
NB
896 return ERR_PTR(-EINVAL);
897
6bb82612
NB
898 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
899 if (IS_ERR(lun))
900 return ERR_CAST(lun);
c66ac9db 901
12d23384 902 lun_cg = &lun->lun_group;
13f6a914 903 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
12d23384
NB
904 GFP_KERNEL);
905 if (!lun_cg->default_groups) {
6708bb27 906 pr_err("Unable to allocate lun_cg->default_groups\n");
6bb82612 907 kfree(lun);
12d23384
NB
908 return ERR_PTR(-ENOMEM);
909 }
910
c66ac9db 911 config_group_init_type_name(&lun->lun_group, name,
968ebe75 912 &tf->tf_tpg_port_cit);
12d23384 913 config_group_init_type_name(&lun->port_stat_grps.stat_group,
968ebe75 914 "statistics", &tf->tf_tpg_port_stat_cit);
12d23384
NB
915 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
916 lun_cg->default_groups[1] = NULL;
917
e3d6f909 918 port_stat_grp = &lun->port_stat_grps.stat_group;
ab6dae82 919 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
12d23384
NB
920 GFP_KERNEL);
921 if (!port_stat_grp->default_groups) {
6708bb27 922 pr_err("Unable to allocate port_stat_grp->default_groups\n");
1481473b 923 kfree(lun_cg->default_groups);
6bb82612 924 kfree(lun);
1481473b 925 return ERR_PTR(-ENOMEM);
12d23384
NB
926 }
927 target_stat_setup_port_default_groups(lun);
c66ac9db
NB
928
929 return &lun->lun_group;
930}
931
932static void target_fabric_drop_lun(
933 struct config_group *group,
934 struct config_item *item)
935{
12d23384
NB
936 struct se_lun *lun = container_of(to_config_group(item),
937 struct se_lun, lun_group);
938 struct config_item *df_item;
939 struct config_group *lun_cg, *port_stat_grp;
940 int i;
941
e3d6f909 942 port_stat_grp = &lun->port_stat_grps.stat_group;
12d23384
NB
943 for (i = 0; port_stat_grp->default_groups[i]; i++) {
944 df_item = &port_stat_grp->default_groups[i]->cg_item;
945 port_stat_grp->default_groups[i] = NULL;
946 config_item_put(df_item);
947 }
948 kfree(port_stat_grp->default_groups);
949
950 lun_cg = &lun->lun_group;
951 for (i = 0; lun_cg->default_groups[i]; i++) {
952 df_item = &lun_cg->default_groups[i]->cg_item;
953 lun_cg->default_groups[i] = NULL;
954 config_item_put(df_item);
955 }
956 kfree(lun_cg->default_groups);
957
c66ac9db
NB
958 config_item_put(item);
959}
960
961static struct configfs_group_operations target_fabric_lun_group_ops = {
962 .make_group = &target_fabric_make_lun,
963 .drop_item = &target_fabric_drop_lun,
964};
965
966TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
967
968/* End of tfc_tpg_lun_cit */
969
970/* Start of tfc_tpg_attrib_cit */
971
972CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
973
974static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
975 .show_attribute = target_fabric_tpg_attrib_attr_show,
976 .store_attribute = target_fabric_tpg_attrib_attr_store,
977};
978
9ac8928e 979TF_CIT_SETUP_DRV(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL);
c66ac9db
NB
980
981/* End of tfc_tpg_attrib_cit */
982
e4b512e7
NB
983/* Start of tfc_tpg_auth_cit */
984
985CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
986
987static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
988 .show_attribute = target_fabric_tpg_auth_attr_show,
989 .store_attribute = target_fabric_tpg_auth_attr_store,
990};
991
9ac8928e 992TF_CIT_SETUP_DRV(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL);
e4b512e7
NB
993
994/* End of tfc_tpg_attrib_cit */
995
c66ac9db
NB
996/* Start of tfc_tpg_param_cit */
997
998CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
999
1000static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
1001 .show_attribute = target_fabric_tpg_param_attr_show,
1002 .store_attribute = target_fabric_tpg_param_attr_store,
1003};
1004
9ac8928e 1005TF_CIT_SETUP_DRV(tpg_param, &target_fabric_tpg_param_item_ops, NULL);
c66ac9db
NB
1006
1007/* End of tfc_tpg_param_cit */
1008
1009/* Start of tfc_tpg_base_cit */
1010/*
1011 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1012 */
1013CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1014
1f6fe7cb
NB
1015static void target_fabric_tpg_release(struct config_item *item)
1016{
1017 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1018 struct se_portal_group, tpg_group);
1019 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1020 struct target_fabric_configfs *tf = wwn->wwn_tf;
1021
ef0caf8d 1022 tf->tf_ops->fabric_drop_tpg(se_tpg);
1f6fe7cb
NB
1023}
1024
c66ac9db 1025static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1f6fe7cb 1026 .release = target_fabric_tpg_release,
c66ac9db
NB
1027 .show_attribute = target_fabric_tpg_attr_show,
1028 .store_attribute = target_fabric_tpg_attr_store,
1029};
1030
9ac8928e 1031TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
c66ac9db
NB
1032
1033/* End of tfc_tpg_base_cit */
1034
1035/* Start of tfc_tpg_cit */
1036
1037static struct config_group *target_fabric_make_tpg(
1038 struct config_group *group,
1039 const char *name)
1040{
1041 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1042 struct target_fabric_configfs *tf = wwn->wwn_tf;
1043 struct se_portal_group *se_tpg;
1044
ef0caf8d
CH
1045 if (!tf->tf_ops->fabric_make_tpg) {
1046 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
c66ac9db
NB
1047 return ERR_PTR(-ENOSYS);
1048 }
1049
ef0caf8d 1050 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
6708bb27 1051 if (!se_tpg || IS_ERR(se_tpg))
c66ac9db
NB
1052 return ERR_PTR(-EINVAL);
1053 /*
1054 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1055 */
1056 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1057 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1058 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1059 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1060 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
e4b512e7
NB
1061 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1062 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1063 se_tpg->tpg_group.default_groups[6] = NULL;
c66ac9db
NB
1064
1065 config_group_init_type_name(&se_tpg->tpg_group, name,
968ebe75 1066 &tf->tf_tpg_base_cit);
c66ac9db 1067 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
968ebe75 1068 &tf->tf_tpg_lun_cit);
c66ac9db 1069 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
968ebe75 1070 &tf->tf_tpg_np_cit);
c66ac9db 1071 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
968ebe75 1072 &tf->tf_tpg_nacl_cit);
c66ac9db 1073 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
968ebe75 1074 &tf->tf_tpg_attrib_cit);
e4b512e7 1075 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
968ebe75 1076 &tf->tf_tpg_auth_cit);
c66ac9db 1077 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
968ebe75 1078 &tf->tf_tpg_param_cit);
c66ac9db
NB
1079
1080 return &se_tpg->tpg_group;
1081}
1082
1083static void target_fabric_drop_tpg(
1084 struct config_group *group,
1085 struct config_item *item)
1086{
c66ac9db
NB
1087 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1088 struct se_portal_group, tpg_group);
1089 struct config_group *tpg_cg = &se_tpg->tpg_group;
1090 struct config_item *df_item;
1091 int i;
1092 /*
1093 * Release default groups, but do not release tpg_cg->default_groups
1094 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1095 */
1096 for (i = 0; tpg_cg->default_groups[i]; i++) {
1097 df_item = &tpg_cg->default_groups[i]->cg_item;
1098 tpg_cg->default_groups[i] = NULL;
1099 config_item_put(df_item);
1100 }
1101
1102 config_item_put(item);
c66ac9db
NB
1103}
1104
1f6fe7cb
NB
1105static void target_fabric_release_wwn(struct config_item *item)
1106{
1107 struct se_wwn *wwn = container_of(to_config_group(item),
1108 struct se_wwn, wwn_group);
1109 struct target_fabric_configfs *tf = wwn->wwn_tf;
1110
ef0caf8d 1111 tf->tf_ops->fabric_drop_wwn(wwn);
1f6fe7cb
NB
1112}
1113
1114static struct configfs_item_operations target_fabric_tpg_item_ops = {
1115 .release = target_fabric_release_wwn,
1116};
1117
c66ac9db
NB
1118static struct configfs_group_operations target_fabric_tpg_group_ops = {
1119 .make_group = target_fabric_make_tpg,
1120 .drop_item = target_fabric_drop_tpg,
1121};
1122
1f6fe7cb
NB
1123TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1124 NULL);
c66ac9db
NB
1125
1126/* End of tfc_tpg_cit */
1127
12d23384
NB
1128/* Start of tfc_wwn_fabric_stats_cit */
1129/*
1130 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1131 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1132 */
1133TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1134
1135/* End of tfc_wwn_fabric_stats_cit */
1136
c66ac9db
NB
1137/* Start of tfc_wwn_cit */
1138
1139static struct config_group *target_fabric_make_wwn(
1140 struct config_group *group,
1141 const char *name)
1142{
1143 struct target_fabric_configfs *tf = container_of(group,
1144 struct target_fabric_configfs, tf_group);
1145 struct se_wwn *wwn;
1146
ef0caf8d 1147 if (!tf->tf_ops->fabric_make_wwn) {
6708bb27 1148 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
c66ac9db
NB
1149 return ERR_PTR(-ENOSYS);
1150 }
1151
ef0caf8d 1152 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
6708bb27 1153 if (!wwn || IS_ERR(wwn))
c66ac9db
NB
1154 return ERR_PTR(-EINVAL);
1155
1156 wwn->wwn_tf = tf;
12d23384
NB
1157 /*
1158 * Setup default groups from pre-allocated wwn->wwn_default_groups
1159 */
1160 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1161 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1162 wwn->wwn_group.default_groups[1] = NULL;
1163
968ebe75 1164 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
12d23384 1165 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
968ebe75 1166 &tf->tf_wwn_fabric_stats_cit);
c66ac9db
NB
1167
1168 return &wwn->wwn_group;
1169}
1170
1171static void target_fabric_drop_wwn(
1172 struct config_group *group,
1173 struct config_item *item)
1174{
12d23384
NB
1175 struct se_wwn *wwn = container_of(to_config_group(item),
1176 struct se_wwn, wwn_group);
1177 struct config_item *df_item;
1178 struct config_group *cg = &wwn->wwn_group;
1179 int i;
1180
1181 for (i = 0; cg->default_groups[i]; i++) {
1182 df_item = &cg->default_groups[i]->cg_item;
1183 cg->default_groups[i] = NULL;
1184 config_item_put(df_item);
1185 }
1186
c66ac9db 1187 config_item_put(item);
c66ac9db
NB
1188}
1189
1190static struct configfs_group_operations target_fabric_wwn_group_ops = {
1191 .make_group = target_fabric_make_wwn,
1192 .drop_item = target_fabric_drop_wwn,
1193};
1194/*
1195 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1196 */
1197CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1198
1199static struct configfs_item_operations target_fabric_wwn_item_ops = {
1200 .show_attribute = target_fabric_wwn_attr_show,
1201 .store_attribute = target_fabric_wwn_attr_store,
1202};
1203
9ac8928e 1204TF_CIT_SETUP_DRV(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops);
c66ac9db
NB
1205
1206/* End of tfc_wwn_cit */
1207
1208/* Start of tfc_discovery_cit */
1209
1210CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1211 tf_disc_group);
1212
1213static struct configfs_item_operations target_fabric_discovery_item_ops = {
1214 .show_attribute = target_fabric_discovery_attr_show,
1215 .store_attribute = target_fabric_discovery_attr_store,
1216};
1217
9ac8928e 1218TF_CIT_SETUP_DRV(discovery, &target_fabric_discovery_item_ops, NULL);
c66ac9db
NB
1219
1220/* End of tfc_discovery_cit */
1221
1222int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1223{
1224 target_fabric_setup_discovery_cit(tf);
1225 target_fabric_setup_wwn_cit(tf);
12d23384 1226 target_fabric_setup_wwn_fabric_stats_cit(tf);
c66ac9db
NB
1227 target_fabric_setup_tpg_cit(tf);
1228 target_fabric_setup_tpg_base_cit(tf);
1229 target_fabric_setup_tpg_port_cit(tf);
12d23384 1230 target_fabric_setup_tpg_port_stat_cit(tf);
c66ac9db
NB
1231 target_fabric_setup_tpg_lun_cit(tf);
1232 target_fabric_setup_tpg_np_cit(tf);
1233 target_fabric_setup_tpg_np_base_cit(tf);
1234 target_fabric_setup_tpg_attrib_cit(tf);
e4b512e7 1235 target_fabric_setup_tpg_auth_cit(tf);
c66ac9db
NB
1236 target_fabric_setup_tpg_param_cit(tf);
1237 target_fabric_setup_tpg_nacl_cit(tf);
1238 target_fabric_setup_tpg_nacl_base_cit(tf);
1239 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1240 target_fabric_setup_tpg_nacl_auth_cit(tf);
1241 target_fabric_setup_tpg_nacl_param_cit(tf);
12d23384 1242 target_fabric_setup_tpg_nacl_stat_cit(tf);
c66ac9db 1243 target_fabric_setup_tpg_mappedlun_cit(tf);
12d23384 1244 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
c66ac9db
NB
1245
1246 return 0;
1247}
This page took 0.311368 seconds and 5 git commands to generate.