Merge tag 'imx-clk-fixes-4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/shawng...
[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 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
140static int target_fabric_mappedlun_unlink(
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
adf653f9 149 return 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);
c7d6a803 341 struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
1f6fe7cb 342
ef0caf8d
CH
343 if (tf->tf_ops->fabric_cleanup_nodeacl)
344 tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
c7d6a803 345 core_tpg_del_initiator_node_acl(se_nacl);
c66ac9db
NB
346}
347
348static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
1f6fe7cb 349 .release = target_fabric_nacl_base_release,
c66ac9db
NB
350};
351
352static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
353 .make_group = target_fabric_make_mappedlun,
354 .drop_item = target_fabric_drop_mappedlun,
355};
356
9ac8928e
CH
357TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
358 &target_fabric_nacl_base_group_ops);
c66ac9db
NB
359
360/* End of tfc_tpg_nacl_base_cit */
361
12d23384
NB
362/* Start of tfc_node_fabric_stats_cit */
363/*
364 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
365 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
366 */
367TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
368
369/* End of tfc_wwn_fabric_stats_cit */
370
c66ac9db
NB
371/* Start of tfc_tpg_nacl_cit */
372
373static struct config_group *target_fabric_make_nodeacl(
374 struct config_group *group,
375 const char *name)
376{
377 struct se_portal_group *se_tpg = container_of(group,
378 struct se_portal_group, tpg_acl_group);
379 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
380 struct se_node_acl *se_nacl;
c66ac9db 381
c7d6a803 382 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
c66ac9db 383 if (IS_ERR(se_nacl))
e1750ba2 384 return ERR_CAST(se_nacl);
c66ac9db 385
ef0caf8d
CH
386 if (tf->tf_ops->fabric_init_nodeacl) {
387 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
c7d6a803
CH
388 if (ret) {
389 core_tpg_del_initiator_node_acl(se_nacl);
390 return ERR_PTR(ret);
391 }
392 }
393
c66ac9db 394 config_group_init_type_name(&se_nacl->acl_group, name,
968ebe75 395 &tf->tf_tpg_nacl_base_cit);
1ae1602d 396
c66ac9db 397 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
968ebe75 398 &tf->tf_tpg_nacl_attrib_cit);
1ae1602d
CH
399 configfs_add_default_group(&se_nacl->acl_attrib_group,
400 &se_nacl->acl_group);
401
c66ac9db 402 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
968ebe75 403 &tf->tf_tpg_nacl_auth_cit);
1ae1602d
CH
404 configfs_add_default_group(&se_nacl->acl_auth_group,
405 &se_nacl->acl_group);
406
c66ac9db 407 config_group_init_type_name(&se_nacl->acl_param_group, "param",
968ebe75 408 &tf->tf_tpg_nacl_param_cit);
1ae1602d
CH
409 configfs_add_default_group(&se_nacl->acl_param_group,
410 &se_nacl->acl_group);
411
12d23384 412 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
968ebe75 413 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
1ae1602d
CH
414 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
415 &se_nacl->acl_group);
c66ac9db
NB
416
417 return &se_nacl->acl_group;
418}
419
420static void target_fabric_drop_nodeacl(
421 struct config_group *group,
422 struct config_item *item)
423{
c66ac9db
NB
424 struct se_node_acl *se_nacl = container_of(to_config_group(item),
425 struct se_node_acl, acl_group);
1ae1602d
CH
426
427 configfs_remove_default_groups(&se_nacl->acl_group);
428
1f6fe7cb
NB
429 /*
430 * struct se_node_acl free is done in target_fabric_nacl_base_release()
431 */
c66ac9db 432 config_item_put(item);
c66ac9db
NB
433}
434
435static struct configfs_group_operations target_fabric_nacl_group_ops = {
436 .make_group = target_fabric_make_nodeacl,
437 .drop_item = target_fabric_drop_nodeacl,
438};
439
440TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
441
442/* End of tfc_tpg_nacl_cit */
443
444/* Start of tfc_tpg_np_base_cit */
445
1f6fe7cb
NB
446static void target_fabric_np_base_release(struct config_item *item)
447{
448 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
449 struct se_tpg_np, tpg_np_group);
450 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
451 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
452
ef0caf8d 453 tf->tf_ops->fabric_drop_np(se_tpg_np);
1f6fe7cb
NB
454}
455
c66ac9db 456static struct configfs_item_operations target_fabric_np_base_item_ops = {
1f6fe7cb 457 .release = target_fabric_np_base_release,
c66ac9db
NB
458};
459
9ac8928e 460TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
c66ac9db
NB
461
462/* End of tfc_tpg_np_base_cit */
463
464/* Start of tfc_tpg_np_cit */
465
466static struct config_group *target_fabric_make_np(
467 struct config_group *group,
468 const char *name)
469{
470 struct se_portal_group *se_tpg = container_of(group,
471 struct se_portal_group, tpg_np_group);
472 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
473 struct se_tpg_np *se_tpg_np;
474
ef0caf8d 475 if (!tf->tf_ops->fabric_make_np) {
6708bb27 476 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
c66ac9db
NB
477 return ERR_PTR(-ENOSYS);
478 }
479
ef0caf8d 480 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
6708bb27 481 if (!se_tpg_np || IS_ERR(se_tpg_np))
c66ac9db
NB
482 return ERR_PTR(-EINVAL);
483
1f6fe7cb 484 se_tpg_np->tpg_np_parent = se_tpg;
c66ac9db 485 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
968ebe75 486 &tf->tf_tpg_np_base_cit);
c66ac9db
NB
487
488 return &se_tpg_np->tpg_np_group;
489}
490
491static void target_fabric_drop_np(
492 struct config_group *group,
493 struct config_item *item)
494{
1f6fe7cb
NB
495 /*
496 * struct se_tpg_np is released via target_fabric_np_base_release()
497 */
c66ac9db 498 config_item_put(item);
c66ac9db
NB
499}
500
501static struct configfs_group_operations target_fabric_np_group_ops = {
502 .make_group = &target_fabric_make_np,
503 .drop_item = &target_fabric_drop_np,
504};
505
506TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
507
508/* End of tfc_tpg_np_cit */
509
510/* Start of tfc_tpg_port_cit */
511
2eafd729
CH
512static struct se_lun *item_to_lun(struct config_item *item)
513{
514 return container_of(to_config_group(item), struct se_lun,
515 lun_group);
516}
c66ac9db 517
2eafd729
CH
518static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
519 char *page)
c66ac9db 520{
2eafd729
CH
521 struct se_lun *lun = item_to_lun(item);
522
adf653f9 523 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
524 return -ENODEV;
525
adf653f9 526 return core_alua_show_tg_pt_gp_info(lun, page);
c66ac9db
NB
527}
528
2eafd729
CH
529static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
530 const char *page, size_t count)
c66ac9db 531{
2eafd729
CH
532 struct se_lun *lun = item_to_lun(item);
533
adf653f9 534 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
535 return -ENODEV;
536
adf653f9 537 return core_alua_store_tg_pt_gp_info(lun, page, count);
c66ac9db
NB
538}
539
2eafd729
CH
540static ssize_t target_fabric_port_alua_tg_pt_offline_show(
541 struct config_item *item, char *page)
c66ac9db 542{
2eafd729
CH
543 struct se_lun *lun = item_to_lun(item);
544
adf653f9 545 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
546 return -ENODEV;
547
548 return core_alua_show_offline_bit(lun, page);
549}
550
2eafd729
CH
551static ssize_t target_fabric_port_alua_tg_pt_offline_store(
552 struct config_item *item, const char *page, size_t count)
c66ac9db 553{
2eafd729
CH
554 struct se_lun *lun = item_to_lun(item);
555
adf653f9 556 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
557 return -ENODEV;
558
559 return core_alua_store_offline_bit(lun, page, count);
560}
561
2eafd729
CH
562static ssize_t target_fabric_port_alua_tg_pt_status_show(
563 struct config_item *item, char *page)
c66ac9db 564{
2eafd729
CH
565 struct se_lun *lun = item_to_lun(item);
566
adf653f9 567 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
568 return -ENODEV;
569
570 return core_alua_show_secondary_status(lun, page);
571}
572
2eafd729
CH
573static ssize_t target_fabric_port_alua_tg_pt_status_store(
574 struct config_item *item, const char *page, size_t count)
c66ac9db 575{
2eafd729
CH
576 struct se_lun *lun = item_to_lun(item);
577
adf653f9 578 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
579 return -ENODEV;
580
581 return core_alua_store_secondary_status(lun, page, count);
582}
583
2eafd729
CH
584static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
585 struct config_item *item, char *page)
c66ac9db 586{
2eafd729
CH
587 struct se_lun *lun = item_to_lun(item);
588
adf653f9 589 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
590 return -ENODEV;
591
592 return core_alua_show_secondary_write_metadata(lun, page);
593}
594
2eafd729
CH
595static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
596 struct config_item *item, const char *page, size_t count)
c66ac9db 597{
2eafd729
CH
598 struct se_lun *lun = item_to_lun(item);
599
adf653f9 600 if (!lun || !lun->lun_se_dev)
c66ac9db
NB
601 return -ENODEV;
602
603 return core_alua_store_secondary_write_metadata(lun, page, count);
604}
605
2eafd729
CH
606CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
607CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
608CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
609CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
c66ac9db
NB
610
611static struct configfs_attribute *target_fabric_port_attrs[] = {
2eafd729
CH
612 &target_fabric_port_attr_alua_tg_pt_gp,
613 &target_fabric_port_attr_alua_tg_pt_offline,
614 &target_fabric_port_attr_alua_tg_pt_status,
615 &target_fabric_port_attr_alua_tg_pt_write_md,
c66ac9db
NB
616 NULL,
617};
618
c66ac9db
NB
619static int target_fabric_port_link(
620 struct config_item *lun_ci,
621 struct config_item *se_dev_ci)
622{
623 struct config_item *tpg_ci;
c66ac9db
NB
624 struct se_lun *lun = container_of(to_config_group(lun_ci),
625 struct se_lun, lun_group);
c66ac9db 626 struct se_portal_group *se_tpg;
0fd97ccf
CH
627 struct se_device *dev =
628 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
c66ac9db
NB
629 struct target_fabric_configfs *tf;
630 int ret;
631
0ff87549
NB
632 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
633 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
634 " %p to struct se_device: %p\n", se_dev_ci, dev);
635 return -EFAULT;
636 }
637
faa06ab9
NB
638 if (!(dev->dev_flags & DF_CONFIGURED)) {
639 pr_err("se_device not configured yet, cannot port link\n");
640 return -ENODEV;
641 }
642
c66ac9db
NB
643 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
644 se_tpg = container_of(to_config_group(tpg_ci),
645 struct se_portal_group, tpg_group);
646 tf = se_tpg->se_tpg_wwn->wwn_tf;
647
648 if (lun->lun_se_dev != NULL) {
6708bb27 649 pr_err("Port Symlink already exists\n");
c66ac9db
NB
650 return -EEXIST;
651 }
652
6bb82612
NB
653 ret = core_dev_add_lun(se_tpg, dev, lun);
654 if (ret) {
655 pr_err("core_dev_add_lun() failed: %d\n", ret);
c66ac9db
NB
656 goto out;
657 }
658
ef0caf8d 659 if (tf->tf_ops->fabric_post_link) {
c66ac9db
NB
660 /*
661 * Call the optional fabric_post_link() to allow a
662 * fabric module to setup any additional state once
663 * core_dev_add_lun() has been called..
664 */
ef0caf8d 665 tf->tf_ops->fabric_post_link(se_tpg, lun);
c66ac9db
NB
666 }
667
668 return 0;
669out:
670 return ret;
671}
672
673static int target_fabric_port_unlink(
674 struct config_item *lun_ci,
675 struct config_item *se_dev_ci)
676{
677 struct se_lun *lun = container_of(to_config_group(lun_ci),
678 struct se_lun, lun_group);
adf653f9 679 struct se_portal_group *se_tpg = lun->lun_tpg;
c66ac9db
NB
680 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
681
ef0caf8d 682 if (tf->tf_ops->fabric_pre_unlink) {
c66ac9db
NB
683 /*
684 * Call the optional fabric_pre_unlink() to allow a
685 * fabric module to release any additional stat before
686 * core_dev_del_lun() is called.
687 */
ef0caf8d 688 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
c66ac9db
NB
689 }
690
cd9d7cba 691 core_dev_del_lun(se_tpg, lun);
c66ac9db
NB
692 return 0;
693}
694
6bb82612
NB
695static void target_fabric_port_release(struct config_item *item)
696{
697 struct se_lun *lun = container_of(to_config_group(item),
698 struct se_lun, lun_group);
699
700 kfree_rcu(lun, rcu_head);
701}
702
c66ac9db 703static struct configfs_item_operations target_fabric_port_item_ops = {
6bb82612 704 .release = target_fabric_port_release,
c66ac9db
NB
705 .allow_link = target_fabric_port_link,
706 .drop_link = target_fabric_port_unlink,
707};
708
709TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
710
711/* End of tfc_tpg_port_cit */
712
12d23384
NB
713/* Start of tfc_tpg_port_stat_cit */
714
715static struct config_group *target_core_port_stat_mkdir(
716 struct config_group *group,
717 const char *name)
718{
719 return ERR_PTR(-ENOSYS);
720}
721
722static void target_core_port_stat_rmdir(
723 struct config_group *group,
724 struct config_item *item)
725{
726 return;
727}
728
729static struct configfs_group_operations target_fabric_port_stat_group_ops = {
730 .make_group = target_core_port_stat_mkdir,
731 .drop_item = target_core_port_stat_rmdir,
732};
733
734TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
735
736/* End of tfc_tpg_port_stat_cit */
737
c66ac9db
NB
738/* Start of tfc_tpg_lun_cit */
739
740static struct config_group *target_fabric_make_lun(
741 struct config_group *group,
742 const char *name)
743{
744 struct se_lun *lun;
745 struct se_portal_group *se_tpg = container_of(group,
746 struct se_portal_group, tpg_lun_group);
747 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
f2d30680 748 unsigned long long unpacked_lun;
12d23384 749 int errno;
c66ac9db
NB
750
751 if (strstr(name, "lun_") != name) {
6708bb27 752 pr_err("Unable to locate \'_\" in"
c66ac9db
NB
753 " \"lun_$LUN_NUMBER\"\n");
754 return ERR_PTR(-EINVAL);
755 }
f2d30680 756 errno = kstrtoull(name + 4, 0, &unpacked_lun);
57103d7f
JH
757 if (errno)
758 return ERR_PTR(errno);
c66ac9db 759
6bb82612
NB
760 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
761 if (IS_ERR(lun))
762 return ERR_CAST(lun);
c66ac9db
NB
763
764 config_group_init_type_name(&lun->lun_group, name,
968ebe75 765 &tf->tf_tpg_port_cit);
1ae1602d 766
12d23384 767 config_group_init_type_name(&lun->port_stat_grps.stat_group,
968ebe75 768 "statistics", &tf->tf_tpg_port_stat_cit);
1ae1602d
CH
769 configfs_add_default_group(&lun->port_stat_grps.stat_group,
770 &lun->lun_group);
771
12d23384 772 target_stat_setup_port_default_groups(lun);
c66ac9db
NB
773
774 return &lun->lun_group;
775}
776
777static void target_fabric_drop_lun(
778 struct config_group *group,
779 struct config_item *item)
780{
12d23384
NB
781 struct se_lun *lun = container_of(to_config_group(item),
782 struct se_lun, lun_group);
12d23384 783
1ae1602d
CH
784 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
785 configfs_remove_default_groups(&lun->lun_group);
12d23384 786
c66ac9db
NB
787 config_item_put(item);
788}
789
790static struct configfs_group_operations target_fabric_lun_group_ops = {
791 .make_group = &target_fabric_make_lun,
792 .drop_item = &target_fabric_drop_lun,
793};
794
795TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
796
797/* End of tfc_tpg_lun_cit */
798
2eafd729
CH
799TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
800TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
801TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
c66ac9db
NB
802
803/* Start of tfc_tpg_base_cit */
c66ac9db 804
1f6fe7cb
NB
805static void target_fabric_tpg_release(struct config_item *item)
806{
807 struct se_portal_group *se_tpg = container_of(to_config_group(item),
808 struct se_portal_group, tpg_group);
809 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
810 struct target_fabric_configfs *tf = wwn->wwn_tf;
811
ef0caf8d 812 tf->tf_ops->fabric_drop_tpg(se_tpg);
1f6fe7cb
NB
813}
814
c66ac9db 815static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1f6fe7cb 816 .release = target_fabric_tpg_release,
c66ac9db
NB
817};
818
9ac8928e 819TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
c66ac9db
NB
820
821/* End of tfc_tpg_base_cit */
822
823/* Start of tfc_tpg_cit */
824
825static struct config_group *target_fabric_make_tpg(
826 struct config_group *group,
827 const char *name)
828{
829 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
830 struct target_fabric_configfs *tf = wwn->wwn_tf;
831 struct se_portal_group *se_tpg;
832
ef0caf8d
CH
833 if (!tf->tf_ops->fabric_make_tpg) {
834 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
c66ac9db
NB
835 return ERR_PTR(-ENOSYS);
836 }
837
ef0caf8d 838 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
6708bb27 839 if (!se_tpg || IS_ERR(se_tpg))
c66ac9db 840 return ERR_PTR(-EINVAL);
c66ac9db
NB
841
842 config_group_init_type_name(&se_tpg->tpg_group, name,
968ebe75 843 &tf->tf_tpg_base_cit);
1ae1602d 844
c66ac9db 845 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
968ebe75 846 &tf->tf_tpg_lun_cit);
1ae1602d
CH
847 configfs_add_default_group(&se_tpg->tpg_lun_group,
848 &se_tpg->tpg_group);
849
c66ac9db 850 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
968ebe75 851 &tf->tf_tpg_np_cit);
1ae1602d
CH
852 configfs_add_default_group(&se_tpg->tpg_np_group,
853 &se_tpg->tpg_group);
854
c66ac9db 855 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
968ebe75 856 &tf->tf_tpg_nacl_cit);
1ae1602d
CH
857 configfs_add_default_group(&se_tpg->tpg_acl_group,
858 &se_tpg->tpg_group);
859
c66ac9db 860 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
968ebe75 861 &tf->tf_tpg_attrib_cit);
1ae1602d
CH
862 configfs_add_default_group(&se_tpg->tpg_attrib_group,
863 &se_tpg->tpg_group);
864
e4b512e7 865 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
968ebe75 866 &tf->tf_tpg_auth_cit);
1ae1602d
CH
867 configfs_add_default_group(&se_tpg->tpg_auth_group,
868 &se_tpg->tpg_group);
869
c66ac9db 870 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
968ebe75 871 &tf->tf_tpg_param_cit);
1ae1602d
CH
872 configfs_add_default_group(&se_tpg->tpg_param_group,
873 &se_tpg->tpg_group);
c66ac9db
NB
874
875 return &se_tpg->tpg_group;
876}
877
878static void target_fabric_drop_tpg(
879 struct config_group *group,
880 struct config_item *item)
881{
c66ac9db
NB
882 struct se_portal_group *se_tpg = container_of(to_config_group(item),
883 struct se_portal_group, tpg_group);
c66ac9db 884
1ae1602d 885 configfs_remove_default_groups(&se_tpg->tpg_group);
c66ac9db 886 config_item_put(item);
c66ac9db
NB
887}
888
1f6fe7cb
NB
889static void target_fabric_release_wwn(struct config_item *item)
890{
891 struct se_wwn *wwn = container_of(to_config_group(item),
892 struct se_wwn, wwn_group);
893 struct target_fabric_configfs *tf = wwn->wwn_tf;
894
ef0caf8d 895 tf->tf_ops->fabric_drop_wwn(wwn);
1f6fe7cb
NB
896}
897
898static struct configfs_item_operations target_fabric_tpg_item_ops = {
899 .release = target_fabric_release_wwn,
900};
901
c66ac9db
NB
902static struct configfs_group_operations target_fabric_tpg_group_ops = {
903 .make_group = target_fabric_make_tpg,
904 .drop_item = target_fabric_drop_tpg,
905};
906
1f6fe7cb
NB
907TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
908 NULL);
c66ac9db
NB
909
910/* End of tfc_tpg_cit */
911
12d23384
NB
912/* Start of tfc_wwn_fabric_stats_cit */
913/*
914 * This is used as a placeholder for struct se_wwn->fabric_stat_group
915 * to allow fabrics access to ->fabric_stat_group->default_groups[]
916 */
917TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
918
919/* End of tfc_wwn_fabric_stats_cit */
920
c66ac9db
NB
921/* Start of tfc_wwn_cit */
922
923static struct config_group *target_fabric_make_wwn(
924 struct config_group *group,
925 const char *name)
926{
927 struct target_fabric_configfs *tf = container_of(group,
928 struct target_fabric_configfs, tf_group);
929 struct se_wwn *wwn;
930
ef0caf8d 931 if (!tf->tf_ops->fabric_make_wwn) {
6708bb27 932 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
c66ac9db
NB
933 return ERR_PTR(-ENOSYS);
934 }
935
ef0caf8d 936 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
6708bb27 937 if (!wwn || IS_ERR(wwn))
c66ac9db
NB
938 return ERR_PTR(-EINVAL);
939
940 wwn->wwn_tf = tf;
12d23384 941
968ebe75 942 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1ae1602d 943
12d23384 944 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
968ebe75 945 &tf->tf_wwn_fabric_stats_cit);
1ae1602d 946 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
c66ac9db
NB
947
948 return &wwn->wwn_group;
949}
950
951static void target_fabric_drop_wwn(
952 struct config_group *group,
953 struct config_item *item)
954{
12d23384
NB
955 struct se_wwn *wwn = container_of(to_config_group(item),
956 struct se_wwn, wwn_group);
12d23384 957
1ae1602d 958 configfs_remove_default_groups(&wwn->wwn_group);
c66ac9db 959 config_item_put(item);
c66ac9db
NB
960}
961
962static struct configfs_group_operations target_fabric_wwn_group_ops = {
963 .make_group = target_fabric_make_wwn,
964 .drop_item = target_fabric_drop_wwn,
965};
c66ac9db 966
2eafd729
CH
967TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
968TF_CIT_SETUP_DRV(discovery, NULL, NULL);
c66ac9db
NB
969
970int target_fabric_setup_cits(struct target_fabric_configfs *tf)
971{
972 target_fabric_setup_discovery_cit(tf);
973 target_fabric_setup_wwn_cit(tf);
12d23384 974 target_fabric_setup_wwn_fabric_stats_cit(tf);
c66ac9db
NB
975 target_fabric_setup_tpg_cit(tf);
976 target_fabric_setup_tpg_base_cit(tf);
977 target_fabric_setup_tpg_port_cit(tf);
12d23384 978 target_fabric_setup_tpg_port_stat_cit(tf);
c66ac9db
NB
979 target_fabric_setup_tpg_lun_cit(tf);
980 target_fabric_setup_tpg_np_cit(tf);
981 target_fabric_setup_tpg_np_base_cit(tf);
982 target_fabric_setup_tpg_attrib_cit(tf);
e4b512e7 983 target_fabric_setup_tpg_auth_cit(tf);
c66ac9db
NB
984 target_fabric_setup_tpg_param_cit(tf);
985 target_fabric_setup_tpg_nacl_cit(tf);
986 target_fabric_setup_tpg_nacl_base_cit(tf);
987 target_fabric_setup_tpg_nacl_attrib_cit(tf);
988 target_fabric_setup_tpg_nacl_auth_cit(tf);
989 target_fabric_setup_tpg_nacl_param_cit(tf);
12d23384 990 target_fabric_setup_tpg_nacl_stat_cit(tf);
c66ac9db 991 target_fabric_setup_tpg_mappedlun_cit(tf);
12d23384 992 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
c66ac9db
NB
993
994 return 0;
995}
This page took 0.287978 seconds and 5 git commands to generate.