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