]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/target/target_core_fabric_configfs.c
Bluetooth: hci_bcm: Support platform enumeration
[mirror_ubuntu-bionic-kernel.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 38
e26d99ae 39#include "target_core_internal.h"
c66ac9db 40#include "target_core_alua.h"
c66ac9db
NB
41#include "target_core_pr.h"
42
43#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
44static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
45{ \
968ebe75 46 struct config_item_type *cit = &tf->tf_##_name##_cit; \
c66ac9db
NB
47 \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
0dc2e8d1 51 cit->ct_owner = tf->tf_ops->module; \
6708bb27 52 pr_debug("Setup generic %s\n", __stringify(_name)); \
c66ac9db
NB
53}
54
9ac8928e
CH
55#define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
57{ \
968ebe75 58 struct config_item_type *cit = &tf->tf_##_name##_cit; \
ef0caf8d 59 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
9ac8928e
CH
60 \
61 cit->ct_item_ops = _item_ops; \
62 cit->ct_group_ops = _group_ops; \
63 cit->ct_attrs = attrs; \
0dc2e8d1 64 cit->ct_owner = tf->tf_ops->module; \
9ac8928e
CH
65 pr_debug("Setup generic %s\n", __stringify(_name)); \
66}
67
c66ac9db
NB
68/* Start of tfc_tpg_mappedlun_cit */
69
70static int target_fabric_mappedlun_link(
71 struct config_item *lun_acl_ci,
72 struct config_item *lun_ci)
73{
74 struct se_dev_entry *deve;
75 struct se_lun *lun = container_of(to_config_group(lun_ci),
76 struct se_lun, lun_group);
77 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
78 struct se_lun_acl, se_lun_group);
79 struct se_portal_group *se_tpg;
80 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
03a68b44 81 bool lun_access_ro;
0ff87549
NB
82
83 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
84 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
85 " %p to struct lun: %p\n", lun_ci, lun);
86 return -EFAULT;
87 }
c66ac9db
NB
88 /*
89 * Ensure that the source port exists
90 */
adf653f9
CH
91 if (!lun->lun_se_dev) {
92 pr_err("Source se_lun->lun_se_dev does not exist\n");
c66ac9db
NB
93 return -EINVAL;
94 }
adf653f9 95 se_tpg = lun->lun_tpg;
c66ac9db
NB
96
97 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
98 tpg_ci = &nacl_ci->ci_group->cg_item;
99 wwn_ci = &tpg_ci->ci_group->cg_item;
100 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
101 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
102 /*
103 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
104 */
105 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
6708bb27 106 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
c66ac9db
NB
107 config_item_name(wwn_ci));
108 return -EINVAL;
109 }
110 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
6708bb27 111 pr_err("Illegal Initiator ACL Symlink outside of %s"
c66ac9db
NB
112 " TPGT: %s\n", config_item_name(wwn_ci),
113 config_item_name(tpg_ci));
114 return -EINVAL;
115 }
116 /*
117 * If this struct se_node_acl was dynamically generated with
03a68b44
AG
118 * tpg_1/attrib/generate_node_acls=1, use the existing
119 * deve->lun_access_ro value, which will be true when
c66ac9db
NB
120 * tpg_1/attrib/demo_mode_write_protect=1
121 */
29a05dee
NB
122 rcu_read_lock();
123 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
124 if (deve)
03a68b44 125 lun_access_ro = deve->lun_access_ro;
c66ac9db 126 else
03a68b44 127 lun_access_ro =
e3d6f909 128 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
03a68b44 129 se_tpg)) ? true : false;
29a05dee 130 rcu_read_unlock();
c66ac9db
NB
131 /*
132 * Determine the actual mapped LUN value user wants..
133 *
134 * This value is what the SCSI Initiator actually sees the
6bb82612 135 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
c66ac9db 136 */
03a68b44 137 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
c66ac9db
NB
138}
139
e16769d4 140static void target_fabric_mappedlun_unlink(
c66ac9db
NB
141 struct config_item *lun_acl_ci,
142 struct config_item *lun_ci)
143{
c66ac9db
NB
144 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
145 struct se_lun_acl, se_lun_group);
29a05dee
NB
146 struct se_lun *lun = container_of(to_config_group(lun_ci),
147 struct se_lun, lun_group);
c66ac9db 148
e16769d4 149 core_dev_del_initiator_node_lun_acl(lun, lacl);
c66ac9db
NB
150}
151
2eafd729
CH
152static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
153{
154 return container_of(to_config_group(item), struct se_lun_acl,
155 se_lun_group);
156}
c66ac9db 157
2eafd729
CH
158static ssize_t target_fabric_mappedlun_write_protect_show(
159 struct config_item *item, char *page)
c66ac9db 160{
2eafd729 161 struct se_lun_acl *lacl = item_to_lun_acl(item);
c66ac9db
NB
162 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
163 struct se_dev_entry *deve;
29a05dee
NB
164 ssize_t len = 0;
165
166 rcu_read_lock();
167 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
168 if (deve) {
03a68b44 169 len = sprintf(page, "%d\n", deve->lun_access_ro);
29a05dee
NB
170 }
171 rcu_read_unlock();
c66ac9db
NB
172
173 return len;
174}
175
2eafd729
CH
176static ssize_t target_fabric_mappedlun_write_protect_store(
177 struct config_item *item, const char *page, size_t count)
c66ac9db 178{
2eafd729 179 struct se_lun_acl *lacl = item_to_lun_acl(item);
c66ac9db
NB
180 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
181 struct se_portal_group *se_tpg = se_nacl->se_tpg;
03a68b44 182 unsigned long wp;
57103d7f 183 int ret;
c66ac9db 184
03a68b44 185 ret = kstrtoul(page, 0, &wp);
57103d7f
JH
186 if (ret)
187 return ret;
c66ac9db 188
03a68b44 189 if ((wp != 1) && (wp != 0))
c66ac9db
NB
190 return -EINVAL;
191
03a68b44
AG
192 /* wp=1 means lun_access_ro=true */
193 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
c66ac9db 194
6708bb27 195 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
f2d30680 196 " Mapped LUN: %llu Write Protect bit to %s\n",
e3d6f909 197 se_tpg->se_tpg_tfo->get_fabric_name(),
03a68b44 198 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
c66ac9db
NB
199
200 return count;
201
202}
203
2eafd729 204CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
c66ac9db 205
2eafd729
CH
206static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
207 &target_fabric_mappedlun_attr_write_protect,
208 NULL,
209};
c66ac9db 210
1f6fe7cb
NB
211static void target_fabric_mappedlun_release(struct config_item *item)
212{
213 struct se_lun_acl *lacl = container_of(to_config_group(item),
214 struct se_lun_acl, se_lun_group);
215 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
216
217 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
218}
219
c66ac9db 220static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
1f6fe7cb 221 .release = target_fabric_mappedlun_release,
c66ac9db
NB
222 .allow_link = target_fabric_mappedlun_link,
223 .drop_link = target_fabric_mappedlun_unlink,
224};
225
226TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
227 target_fabric_mappedlun_attrs);
228
229/* End of tfc_tpg_mappedlun_cit */
230
12d23384
NB
231/* Start of tfc_tpg_mappedlun_port_cit */
232
233static struct config_group *target_core_mappedlun_stat_mkdir(
234 struct config_group *group,
235 const char *name)
236{
237 return ERR_PTR(-ENOSYS);
238}
239
240static void target_core_mappedlun_stat_rmdir(
241 struct config_group *group,
242 struct config_item *item)
243{
244 return;
245}
246
247static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
248 .make_group = target_core_mappedlun_stat_mkdir,
249 .drop_item = target_core_mappedlun_stat_rmdir,
250};
251
252TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
253 NULL);
254
255/* End of tfc_tpg_mappedlun_port_cit */
256
2eafd729
CH
257TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
258TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
259TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
c66ac9db
NB
260
261/* Start of tfc_tpg_nacl_base_cit */
262
c66ac9db
NB
263static struct config_group *target_fabric_make_mappedlun(
264 struct config_group *group,
265 const char *name)
266{
267 struct se_node_acl *se_nacl = container_of(group,
268 struct se_node_acl, acl_group);
269 struct se_portal_group *se_tpg = se_nacl->se_tpg;
270 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
da0abaee 271 struct se_lun_acl *lacl = NULL;
c66ac9db 272 char *buf;
f2d30680 273 unsigned long long mapped_lun;
c66ac9db
NB
274 int ret = 0;
275
c66ac9db 276 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
6708bb27
AG
277 if (!buf) {
278 pr_err("Unable to allocate memory for name buf\n");
c66ac9db
NB
279 return ERR_PTR(-ENOMEM);
280 }
281 snprintf(buf, strlen(name) + 1, "%s", name);
282 /*
283 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
284 */
285 if (strstr(buf, "lun_") != buf) {
6708bb27 286 pr_err("Unable to locate \"lun_\" from buf: %s"
c66ac9db
NB
287 " name: %s\n", buf, name);
288 ret = -EINVAL;
289 goto out;
290 }
291 /*
292 * Determine the Mapped LUN value. This is what the SCSI Initiator
293 * Port will actually see.
294 */
f2d30680 295 ret = kstrtoull(buf + 4, 0, &mapped_lun);
57103d7f
JH
296 if (ret)
297 goto out;
c66ac9db 298
fcf29481
NB
299 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
300 mapped_lun, &ret);
6708bb27 301 if (!lacl) {
12d23384 302 ret = -EINVAL;
c66ac9db 303 goto out;
12d23384
NB
304 }
305
c66ac9db 306 config_group_init_type_name(&lacl->se_lun_group, name,
968ebe75 307 &tf->tf_tpg_mappedlun_cit);
1ae1602d 308
12d23384 309 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
968ebe75 310 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
1ae1602d
CH
311 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
312 &lacl->se_lun_group);
313
12d23384 314 target_stat_setup_mappedlun_default_groups(lacl);
c66ac9db
NB
315
316 kfree(buf);
317 return &lacl->se_lun_group;
318out:
da0abaee 319 kfree(lacl);
c66ac9db
NB
320 kfree(buf);
321 return ERR_PTR(ret);
322}
323
324static void target_fabric_drop_mappedlun(
325 struct config_group *group,
326 struct config_item *item)
327{
12d23384
NB
328 struct se_lun_acl *lacl = container_of(to_config_group(item),
329 struct se_lun_acl, se_lun_group);
12d23384 330
1ae1602d
CH
331 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
332 configfs_remove_default_groups(&lacl->se_lun_group);
12d23384 333
c66ac9db 334 config_item_put(item);
1f6fe7cb
NB
335}
336
337static void target_fabric_nacl_base_release(struct config_item *item)
338{
339 struct se_node_acl *se_nacl = container_of(to_config_group(item),
340 struct se_node_acl, acl_group);
1f6fe7cb 341
ce7043fd 342 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
c7d6a803 343 core_tpg_del_initiator_node_acl(se_nacl);
c66ac9db
NB
344}
345
346static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
1f6fe7cb 347 .release = target_fabric_nacl_base_release,
c66ac9db
NB
348};
349
350static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
351 .make_group = target_fabric_make_mappedlun,
352 .drop_item = target_fabric_drop_mappedlun,
353};
354
9ac8928e
CH
355TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
356 &target_fabric_nacl_base_group_ops);
c66ac9db
NB
357
358/* End of tfc_tpg_nacl_base_cit */
359
12d23384
NB
360/* Start of tfc_node_fabric_stats_cit */
361/*
362 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
363 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
364 */
365TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
366
367/* End of tfc_wwn_fabric_stats_cit */
368
c66ac9db
NB
369/* Start of tfc_tpg_nacl_cit */
370
371static struct config_group *target_fabric_make_nodeacl(
372 struct config_group *group,
373 const char *name)
374{
375 struct se_portal_group *se_tpg = container_of(group,
376 struct se_portal_group, tpg_acl_group);
377 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
378 struct se_node_acl *se_nacl;
c66ac9db 379
c7d6a803 380 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
c66ac9db 381 if (IS_ERR(se_nacl))
e1750ba2 382 return ERR_CAST(se_nacl);
c66ac9db 383
c66ac9db 384 config_group_init_type_name(&se_nacl->acl_group, name,
968ebe75 385 &tf->tf_tpg_nacl_base_cit);
1ae1602d 386
c66ac9db 387 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
968ebe75 388 &tf->tf_tpg_nacl_attrib_cit);
1ae1602d
CH
389 configfs_add_default_group(&se_nacl->acl_attrib_group,
390 &se_nacl->acl_group);
391
c66ac9db 392 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
968ebe75 393 &tf->tf_tpg_nacl_auth_cit);
1ae1602d
CH
394 configfs_add_default_group(&se_nacl->acl_auth_group,
395 &se_nacl->acl_group);
396
c66ac9db 397 config_group_init_type_name(&se_nacl->acl_param_group, "param",
968ebe75 398 &tf->tf_tpg_nacl_param_cit);
1ae1602d
CH
399 configfs_add_default_group(&se_nacl->acl_param_group,
400 &se_nacl->acl_group);
401
12d23384 402 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
968ebe75 403 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
1ae1602d
CH
404 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
405 &se_nacl->acl_group);
c66ac9db 406
e6e202ed
CH
407 if (tf->tf_ops->fabric_init_nodeacl) {
408 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
409 if (ret) {
410 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
411 core_tpg_del_initiator_node_acl(se_nacl);
412 return ERR_PTR(ret);
413 }
414 }
415
c66ac9db
NB
416 return &se_nacl->acl_group;
417}
418
419static void target_fabric_drop_nodeacl(
420 struct config_group *group,
421 struct config_item *item)
422{
c66ac9db
NB
423 struct se_node_acl *se_nacl = container_of(to_config_group(item),
424 struct se_node_acl, acl_group);
1ae1602d
CH
425
426 configfs_remove_default_groups(&se_nacl->acl_group);
427
1f6fe7cb
NB
428 /*
429 * struct se_node_acl free is done in target_fabric_nacl_base_release()
430 */
c66ac9db 431 config_item_put(item);
c66ac9db
NB
432}
433
434static struct configfs_group_operations target_fabric_nacl_group_ops = {
435 .make_group = target_fabric_make_nodeacl,
436 .drop_item = target_fabric_drop_nodeacl,
437};
438
439TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
440
441/* End of tfc_tpg_nacl_cit */
442
443/* Start of tfc_tpg_np_base_cit */
444
1f6fe7cb
NB
445static void target_fabric_np_base_release(struct config_item *item)
446{
447 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
448 struct se_tpg_np, tpg_np_group);
449 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
450 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
451
ef0caf8d 452 tf->tf_ops->fabric_drop_np(se_tpg_np);
1f6fe7cb
NB
453}
454
c66ac9db 455static struct configfs_item_operations target_fabric_np_base_item_ops = {
1f6fe7cb 456 .release = target_fabric_np_base_release,
c66ac9db
NB
457};
458
9ac8928e 459TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
c66ac9db
NB
460
461/* End of tfc_tpg_np_base_cit */
462
463/* Start of tfc_tpg_np_cit */
464
465static struct config_group *target_fabric_make_np(
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_np_group);
471 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
472 struct se_tpg_np *se_tpg_np;
473
ef0caf8d 474 if (!tf->tf_ops->fabric_make_np) {
6708bb27 475 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
c66ac9db
NB
476 return ERR_PTR(-ENOSYS);
477 }
478
ef0caf8d 479 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
6708bb27 480 if (!se_tpg_np || IS_ERR(se_tpg_np))
c66ac9db
NB
481 return ERR_PTR(-EINVAL);
482
1f6fe7cb 483 se_tpg_np->tpg_np_parent = se_tpg;
c66ac9db 484 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
968ebe75 485 &tf->tf_tpg_np_base_cit);
c66ac9db
NB
486
487 return &se_tpg_np->tpg_np_group;
488}
489
490static void target_fabric_drop_np(
491 struct config_group *group,
492 struct config_item *item)
493{
1f6fe7cb
NB
494 /*
495 * struct se_tpg_np is released via target_fabric_np_base_release()
496 */
c66ac9db 497 config_item_put(item);
c66ac9db
NB
498}
499
500static struct configfs_group_operations target_fabric_np_group_ops = {
501 .make_group = &target_fabric_make_np,
502 .drop_item = &target_fabric_drop_np,
503};
504
505TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
506
507/* End of tfc_tpg_np_cit */
508
509/* Start of tfc_tpg_port_cit */
510
2eafd729
CH
511static struct se_lun *item_to_lun(struct config_item *item)
512{
513 return container_of(to_config_group(item), struct se_lun,
514 lun_group);
515}
c66ac9db 516
2eafd729
CH
517static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
518 char *page)
c66ac9db 519{
2eafd729
CH
520 struct se_lun *lun = item_to_lun(item);
521
adf653f9 522 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
523 return -ENODEV;
524
adf653f9 525 return core_alua_show_tg_pt_gp_info(lun, page);
c66ac9db
NB
526}
527
2eafd729
CH
528static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
529 const char *page, size_t count)
c66ac9db 530{
2eafd729
CH
531 struct se_lun *lun = item_to_lun(item);
532
adf653f9 533 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
534 return -ENODEV;
535
adf653f9 536 return core_alua_store_tg_pt_gp_info(lun, page, count);
c66ac9db
NB
537}
538
2eafd729
CH
539static ssize_t target_fabric_port_alua_tg_pt_offline_show(
540 struct config_item *item, char *page)
c66ac9db 541{
2eafd729
CH
542 struct se_lun *lun = item_to_lun(item);
543
adf653f9 544 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
545 return -ENODEV;
546
547 return core_alua_show_offline_bit(lun, page);
548}
549
2eafd729
CH
550static ssize_t target_fabric_port_alua_tg_pt_offline_store(
551 struct config_item *item, const char *page, size_t count)
c66ac9db 552{
2eafd729
CH
553 struct se_lun *lun = item_to_lun(item);
554
adf653f9 555 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
556 return -ENODEV;
557
558 return core_alua_store_offline_bit(lun, page, count);
559}
560
2eafd729
CH
561static ssize_t target_fabric_port_alua_tg_pt_status_show(
562 struct config_item *item, char *page)
c66ac9db 563{
2eafd729
CH
564 struct se_lun *lun = item_to_lun(item);
565
adf653f9 566 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
567 return -ENODEV;
568
569 return core_alua_show_secondary_status(lun, page);
570}
571
2eafd729
CH
572static ssize_t target_fabric_port_alua_tg_pt_status_store(
573 struct config_item *item, const char *page, size_t count)
c66ac9db 574{
2eafd729
CH
575 struct se_lun *lun = item_to_lun(item);
576
adf653f9 577 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
578 return -ENODEV;
579
580 return core_alua_store_secondary_status(lun, page, count);
581}
582
2eafd729
CH
583static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
584 struct config_item *item, char *page)
c66ac9db 585{
2eafd729
CH
586 struct se_lun *lun = item_to_lun(item);
587
adf653f9 588 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
589 return -ENODEV;
590
591 return core_alua_show_secondary_write_metadata(lun, page);
592}
593
2eafd729
CH
594static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
595 struct config_item *item, const char *page, size_t count)
c66ac9db 596{
2eafd729
CH
597 struct se_lun *lun = item_to_lun(item);
598
adf653f9 599 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
600 return -ENODEV;
601
602 return core_alua_store_secondary_write_metadata(lun, page, count);
603}
604
2eafd729
CH
605CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
606CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
607CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
608CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
c66ac9db
NB
609
610static struct configfs_attribute *target_fabric_port_attrs[] = {
2eafd729
CH
611 &target_fabric_port_attr_alua_tg_pt_gp,
612 &target_fabric_port_attr_alua_tg_pt_offline,
613 &target_fabric_port_attr_alua_tg_pt_status,
614 &target_fabric_port_attr_alua_tg_pt_write_md,
c66ac9db
NB
615 NULL,
616};
617
c66ac9db
NB
618static int target_fabric_port_link(
619 struct config_item *lun_ci,
620 struct config_item *se_dev_ci)
621{
622 struct config_item *tpg_ci;
c66ac9db
NB
623 struct se_lun *lun = container_of(to_config_group(lun_ci),
624 struct se_lun, lun_group);
c66ac9db 625 struct se_portal_group *se_tpg;
0fd97ccf
CH
626 struct se_device *dev =
627 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
c66ac9db
NB
628 struct target_fabric_configfs *tf;
629 int ret;
630
0ff87549
NB
631 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
632 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
633 " %p to struct se_device: %p\n", se_dev_ci, dev);
634 return -EFAULT;
635 }
636
faa06ab9
NB
637 if (!(dev->dev_flags & DF_CONFIGURED)) {
638 pr_err("se_device not configured yet, cannot port link\n");
639 return -ENODEV;
640 }
641
c66ac9db
NB
642 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
643 se_tpg = container_of(to_config_group(tpg_ci),
644 struct se_portal_group, tpg_group);
645 tf = se_tpg->se_tpg_wwn->wwn_tf;
646
647 if (lun->lun_se_dev != NULL) {
6708bb27 648 pr_err("Port Symlink already exists\n");
c66ac9db
NB
649 return -EEXIST;
650 }
651
6bb82612
NB
652 ret = core_dev_add_lun(se_tpg, dev, lun);
653 if (ret) {
654 pr_err("core_dev_add_lun() failed: %d\n", ret);
c66ac9db
NB
655 goto out;
656 }
657
ef0caf8d 658 if (tf->tf_ops->fabric_post_link) {
c66ac9db
NB
659 /*
660 * Call the optional fabric_post_link() to allow a
661 * fabric module to setup any additional state once
662 * core_dev_add_lun() has been called..
663 */
ef0caf8d 664 tf->tf_ops->fabric_post_link(se_tpg, lun);
c66ac9db
NB
665 }
666
667 return 0;
668out:
669 return ret;
670}
671
e16769d4 672static void target_fabric_port_unlink(
c66ac9db
NB
673 struct config_item *lun_ci,
674 struct config_item *se_dev_ci)
675{
676 struct se_lun *lun = container_of(to_config_group(lun_ci),
677 struct se_lun, lun_group);
adf653f9 678 struct se_portal_group *se_tpg = lun->lun_tpg;
c66ac9db
NB
679 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
680
ef0caf8d 681 if (tf->tf_ops->fabric_pre_unlink) {
c66ac9db
NB
682 /*
683 * Call the optional fabric_pre_unlink() to allow a
684 * fabric module to release any additional stat before
685 * core_dev_del_lun() is called.
686 */
ef0caf8d 687 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
c66ac9db
NB
688 }
689
cd9d7cba 690 core_dev_del_lun(se_tpg, lun);
c66ac9db
NB
691}
692
6bb82612
NB
693static void target_fabric_port_release(struct config_item *item)
694{
695 struct se_lun *lun = container_of(to_config_group(item),
696 struct se_lun, lun_group);
697
698 kfree_rcu(lun, rcu_head);
699}
700
c66ac9db 701static struct configfs_item_operations target_fabric_port_item_ops = {
6bb82612 702 .release = target_fabric_port_release,
c66ac9db
NB
703 .allow_link = target_fabric_port_link,
704 .drop_link = target_fabric_port_unlink,
705};
706
707TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
708
709/* End of tfc_tpg_port_cit */
710
12d23384
NB
711/* Start of tfc_tpg_port_stat_cit */
712
713static struct config_group *target_core_port_stat_mkdir(
714 struct config_group *group,
715 const char *name)
716{
717 return ERR_PTR(-ENOSYS);
718}
719
720static void target_core_port_stat_rmdir(
721 struct config_group *group,
722 struct config_item *item)
723{
724 return;
725}
726
727static struct configfs_group_operations target_fabric_port_stat_group_ops = {
728 .make_group = target_core_port_stat_mkdir,
729 .drop_item = target_core_port_stat_rmdir,
730};
731
732TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
733
734/* End of tfc_tpg_port_stat_cit */
735
c66ac9db
NB
736/* Start of tfc_tpg_lun_cit */
737
738static struct config_group *target_fabric_make_lun(
739 struct config_group *group,
740 const char *name)
741{
742 struct se_lun *lun;
743 struct se_portal_group *se_tpg = container_of(group,
744 struct se_portal_group, tpg_lun_group);
745 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
f2d30680 746 unsigned long long unpacked_lun;
12d23384 747 int errno;
c66ac9db
NB
748
749 if (strstr(name, "lun_") != name) {
6708bb27 750 pr_err("Unable to locate \'_\" in"
c66ac9db
NB
751 " \"lun_$LUN_NUMBER\"\n");
752 return ERR_PTR(-EINVAL);
753 }
f2d30680 754 errno = kstrtoull(name + 4, 0, &unpacked_lun);
57103d7f
JH
755 if (errno)
756 return ERR_PTR(errno);
c66ac9db 757
6bb82612
NB
758 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
759 if (IS_ERR(lun))
760 return ERR_CAST(lun);
c66ac9db
NB
761
762 config_group_init_type_name(&lun->lun_group, name,
968ebe75 763 &tf->tf_tpg_port_cit);
1ae1602d 764
12d23384 765 config_group_init_type_name(&lun->port_stat_grps.stat_group,
968ebe75 766 "statistics", &tf->tf_tpg_port_stat_cit);
1ae1602d
CH
767 configfs_add_default_group(&lun->port_stat_grps.stat_group,
768 &lun->lun_group);
769
12d23384 770 target_stat_setup_port_default_groups(lun);
c66ac9db
NB
771
772 return &lun->lun_group;
773}
774
775static void target_fabric_drop_lun(
776 struct config_group *group,
777 struct config_item *item)
778{
12d23384
NB
779 struct se_lun *lun = container_of(to_config_group(item),
780 struct se_lun, lun_group);
12d23384 781
1ae1602d
CH
782 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
783 configfs_remove_default_groups(&lun->lun_group);
12d23384 784
c66ac9db
NB
785 config_item_put(item);
786}
787
788static struct configfs_group_operations target_fabric_lun_group_ops = {
789 .make_group = &target_fabric_make_lun,
790 .drop_item = &target_fabric_drop_lun,
791};
792
793TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
794
795/* End of tfc_tpg_lun_cit */
796
2eafd729
CH
797TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
798TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
799TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
c66ac9db
NB
800
801/* Start of tfc_tpg_base_cit */
c66ac9db 802
1f6fe7cb
NB
803static void target_fabric_tpg_release(struct config_item *item)
804{
805 struct se_portal_group *se_tpg = container_of(to_config_group(item),
806 struct se_portal_group, tpg_group);
807 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
808 struct target_fabric_configfs *tf = wwn->wwn_tf;
809
ef0caf8d 810 tf->tf_ops->fabric_drop_tpg(se_tpg);
1f6fe7cb
NB
811}
812
c66ac9db 813static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1f6fe7cb 814 .release = target_fabric_tpg_release,
c66ac9db
NB
815};
816
9ac8928e 817TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
c66ac9db
NB
818
819/* End of tfc_tpg_base_cit */
820
821/* Start of tfc_tpg_cit */
822
823static struct config_group *target_fabric_make_tpg(
824 struct config_group *group,
825 const char *name)
826{
827 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
828 struct target_fabric_configfs *tf = wwn->wwn_tf;
829 struct se_portal_group *se_tpg;
830
ef0caf8d
CH
831 if (!tf->tf_ops->fabric_make_tpg) {
832 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
c66ac9db
NB
833 return ERR_PTR(-ENOSYS);
834 }
835
ef0caf8d 836 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
6708bb27 837 if (!se_tpg || IS_ERR(se_tpg))
c66ac9db 838 return ERR_PTR(-EINVAL);
c66ac9db
NB
839
840 config_group_init_type_name(&se_tpg->tpg_group, name,
968ebe75 841 &tf->tf_tpg_base_cit);
1ae1602d 842
c66ac9db 843 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
968ebe75 844 &tf->tf_tpg_lun_cit);
1ae1602d
CH
845 configfs_add_default_group(&se_tpg->tpg_lun_group,
846 &se_tpg->tpg_group);
847
c66ac9db 848 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
968ebe75 849 &tf->tf_tpg_np_cit);
1ae1602d
CH
850 configfs_add_default_group(&se_tpg->tpg_np_group,
851 &se_tpg->tpg_group);
852
c66ac9db 853 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
968ebe75 854 &tf->tf_tpg_nacl_cit);
1ae1602d
CH
855 configfs_add_default_group(&se_tpg->tpg_acl_group,
856 &se_tpg->tpg_group);
857
c66ac9db 858 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
968ebe75 859 &tf->tf_tpg_attrib_cit);
1ae1602d
CH
860 configfs_add_default_group(&se_tpg->tpg_attrib_group,
861 &se_tpg->tpg_group);
862
e4b512e7 863 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
968ebe75 864 &tf->tf_tpg_auth_cit);
1ae1602d
CH
865 configfs_add_default_group(&se_tpg->tpg_auth_group,
866 &se_tpg->tpg_group);
867
c66ac9db 868 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
968ebe75 869 &tf->tf_tpg_param_cit);
1ae1602d
CH
870 configfs_add_default_group(&se_tpg->tpg_param_group,
871 &se_tpg->tpg_group);
c66ac9db
NB
872
873 return &se_tpg->tpg_group;
874}
875
876static void target_fabric_drop_tpg(
877 struct config_group *group,
878 struct config_item *item)
879{
c66ac9db
NB
880 struct se_portal_group *se_tpg = container_of(to_config_group(item),
881 struct se_portal_group, tpg_group);
c66ac9db 882
1ae1602d 883 configfs_remove_default_groups(&se_tpg->tpg_group);
c66ac9db 884 config_item_put(item);
c66ac9db
NB
885}
886
1f6fe7cb
NB
887static void target_fabric_release_wwn(struct config_item *item)
888{
889 struct se_wwn *wwn = container_of(to_config_group(item),
890 struct se_wwn, wwn_group);
891 struct target_fabric_configfs *tf = wwn->wwn_tf;
892
839559e1 893 configfs_remove_default_groups(&wwn->fabric_stat_group);
ef0caf8d 894 tf->tf_ops->fabric_drop_wwn(wwn);
1f6fe7cb
NB
895}
896
897static struct configfs_item_operations target_fabric_tpg_item_ops = {
898 .release = target_fabric_release_wwn,
899};
900
c66ac9db
NB
901static struct configfs_group_operations target_fabric_tpg_group_ops = {
902 .make_group = target_fabric_make_tpg,
903 .drop_item = target_fabric_drop_tpg,
904};
905
1f6fe7cb
NB
906TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
907 NULL);
c66ac9db
NB
908
909/* End of tfc_tpg_cit */
910
12d23384
NB
911/* Start of tfc_wwn_fabric_stats_cit */
912/*
913 * This is used as a placeholder for struct se_wwn->fabric_stat_group
914 * to allow fabrics access to ->fabric_stat_group->default_groups[]
915 */
916TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
917
918/* End of tfc_wwn_fabric_stats_cit */
919
c66ac9db
NB
920/* Start of tfc_wwn_cit */
921
922static struct config_group *target_fabric_make_wwn(
923 struct config_group *group,
924 const char *name)
925{
926 struct target_fabric_configfs *tf = container_of(group,
927 struct target_fabric_configfs, tf_group);
928 struct se_wwn *wwn;
929
ef0caf8d 930 if (!tf->tf_ops->fabric_make_wwn) {
6708bb27 931 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
c66ac9db
NB
932 return ERR_PTR(-ENOSYS);
933 }
934
ef0caf8d 935 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
6708bb27 936 if (!wwn || IS_ERR(wwn))
c66ac9db
NB
937 return ERR_PTR(-EINVAL);
938
939 wwn->wwn_tf = tf;
12d23384 940
968ebe75 941 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1ae1602d 942
12d23384 943 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
968ebe75 944 &tf->tf_wwn_fabric_stats_cit);
1ae1602d 945 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
c66ac9db 946
839559e1
CH
947 if (tf->tf_ops->add_wwn_groups)
948 tf->tf_ops->add_wwn_groups(wwn);
c66ac9db
NB
949 return &wwn->wwn_group;
950}
951
952static void target_fabric_drop_wwn(
953 struct config_group *group,
954 struct config_item *item)
955{
12d23384
NB
956 struct se_wwn *wwn = container_of(to_config_group(item),
957 struct se_wwn, wwn_group);
12d23384 958
1ae1602d 959 configfs_remove_default_groups(&wwn->wwn_group);
c66ac9db 960 config_item_put(item);
c66ac9db
NB
961}
962
963static struct configfs_group_operations target_fabric_wwn_group_ops = {
964 .make_group = target_fabric_make_wwn,
965 .drop_item = target_fabric_drop_wwn,
966};
c66ac9db 967
2eafd729
CH
968TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
969TF_CIT_SETUP_DRV(discovery, NULL, NULL);
c66ac9db
NB
970
971int target_fabric_setup_cits(struct target_fabric_configfs *tf)
972{
973 target_fabric_setup_discovery_cit(tf);
974 target_fabric_setup_wwn_cit(tf);
12d23384 975 target_fabric_setup_wwn_fabric_stats_cit(tf);
c66ac9db
NB
976 target_fabric_setup_tpg_cit(tf);
977 target_fabric_setup_tpg_base_cit(tf);
978 target_fabric_setup_tpg_port_cit(tf);
12d23384 979 target_fabric_setup_tpg_port_stat_cit(tf);
c66ac9db
NB
980 target_fabric_setup_tpg_lun_cit(tf);
981 target_fabric_setup_tpg_np_cit(tf);
982 target_fabric_setup_tpg_np_base_cit(tf);
983 target_fabric_setup_tpg_attrib_cit(tf);
e4b512e7 984 target_fabric_setup_tpg_auth_cit(tf);
c66ac9db
NB
985 target_fabric_setup_tpg_param_cit(tf);
986 target_fabric_setup_tpg_nacl_cit(tf);
987 target_fabric_setup_tpg_nacl_base_cit(tf);
988 target_fabric_setup_tpg_nacl_attrib_cit(tf);
989 target_fabric_setup_tpg_nacl_auth_cit(tf);
990 target_fabric_setup_tpg_nacl_param_cit(tf);
12d23384 991 target_fabric_setup_tpg_nacl_stat_cit(tf);
c66ac9db 992 target_fabric_setup_tpg_mappedlun_cit(tf);
12d23384 993 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
c66ac9db
NB
994
995 return 0;
996}