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