1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * (c) Copyright 2010-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/utsname.h>
25 #include <linux/init.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>
36 #include <target/target_core_base.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_fabric_configfs.h>
39 #include <target/target_core_configfs.h>
40 #include <target/configfs_macros.h>
42 #include "target_core_internal.h"
43 #include "target_core_alua.h"
44 #include "target_core_pr.h"
46 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
47 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
49 struct config_item_type *cit = &tf->tf_##_name##_cit; \
51 cit->ct_item_ops = _item_ops; \
52 cit->ct_group_ops = _group_ops; \
53 cit->ct_attrs = _attrs; \
54 cit->ct_owner = tf->tf_ops->module; \
55 pr_debug("Setup generic %s\n", __stringify(_name)); \
58 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
59 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
61 struct config_item_type *cit = &tf->tf_##_name##_cit; \
62 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
64 cit->ct_item_ops = _item_ops; \
65 cit->ct_group_ops = _group_ops; \
66 cit->ct_attrs = attrs; \
67 cit->ct_owner = tf->tf_ops->module; \
68 pr_debug("Setup generic %s\n", __stringify(_name)); \
71 /* Start of tfc_tpg_mappedlun_cit */
73 static int target_fabric_mappedlun_link(
74 struct config_item
*lun_acl_ci
,
75 struct config_item
*lun_ci
)
77 struct se_dev_entry
*deve
;
78 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
79 struct se_lun
, lun_group
);
80 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
81 struct se_lun_acl
, se_lun_group
);
82 struct se_portal_group
*se_tpg
;
83 struct config_item
*nacl_ci
, *tpg_ci
, *tpg_ci_s
, *wwn_ci
, *wwn_ci_s
;
84 int ret
= 0, lun_access
;
86 if (lun
->lun_link_magic
!= SE_LUN_LINK_MAGIC
) {
87 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
88 " %p to struct lun: %p\n", lun_ci
, lun
);
92 * Ensure that the source port exists
94 if (!lun
->lun_sep
|| !lun
->lun_sep
->sep_tpg
) {
95 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
96 "_tpg does not exist\n");
99 se_tpg
= lun
->lun_sep
->sep_tpg
;
101 nacl_ci
= &lun_acl_ci
->ci_parent
->ci_group
->cg_item
;
102 tpg_ci
= &nacl_ci
->ci_group
->cg_item
;
103 wwn_ci
= &tpg_ci
->ci_group
->cg_item
;
104 tpg_ci_s
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
105 wwn_ci_s
= &tpg_ci_s
->ci_group
->cg_item
;
107 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
109 if (strcmp(config_item_name(wwn_ci
), config_item_name(wwn_ci_s
))) {
110 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
111 config_item_name(wwn_ci
));
114 if (strcmp(config_item_name(tpg_ci
), config_item_name(tpg_ci_s
))) {
115 pr_err("Illegal Initiator ACL Symlink outside of %s"
116 " TPGT: %s\n", config_item_name(wwn_ci
),
117 config_item_name(tpg_ci
));
121 * If this struct se_node_acl was dynamically generated with
122 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
123 * which be will write protected (READ-ONLY) when
124 * tpg_1/attrib/demo_mode_write_protect=1
126 spin_lock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
127 deve
= lacl
->se_lun_nacl
->device_list
[lacl
->mapped_lun
];
128 if (deve
->lun_flags
& TRANSPORT_LUNFLAGS_INITIATOR_ACCESS
)
129 lun_access
= deve
->lun_flags
;
132 (se_tpg
->se_tpg_tfo
->tpg_check_prod_mode_write_protect(
133 se_tpg
)) ? TRANSPORT_LUNFLAGS_READ_ONLY
:
134 TRANSPORT_LUNFLAGS_READ_WRITE
;
135 spin_unlock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
137 * Determine the actual mapped LUN value user wants..
139 * This value is what the SCSI Initiator actually sees the
140 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
142 ret
= core_dev_add_initiator_node_lun_acl(se_tpg
, lacl
,
143 lun
->unpacked_lun
, lun_access
);
145 return (ret
< 0) ? -EINVAL
: 0;
148 static int target_fabric_mappedlun_unlink(
149 struct config_item
*lun_acl_ci
,
150 struct config_item
*lun_ci
)
153 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
154 struct se_lun_acl
, se_lun_group
);
155 struct se_node_acl
*nacl
= lacl
->se_lun_nacl
;
156 struct se_dev_entry
*deve
= nacl
->device_list
[lacl
->mapped_lun
];
157 struct se_portal_group
*se_tpg
;
159 * Determine if the underlying MappedLUN has already been released..
164 lun
= container_of(to_config_group(lun_ci
), struct se_lun
, lun_group
);
165 se_tpg
= lun
->lun_sep
->sep_tpg
;
167 core_dev_del_initiator_node_lun_acl(se_tpg
, lun
, lacl
);
171 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun
, se_lun_acl
);
172 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
173 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
174 __CONFIGFS_EATTR(_name, _mode, \
175 target_fabric_mappedlun_show_##_name, \
176 target_fabric_mappedlun_store_##_name);
178 static ssize_t
target_fabric_mappedlun_show_write_protect(
179 struct se_lun_acl
*lacl
,
182 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
183 struct se_dev_entry
*deve
;
186 spin_lock_irq(&se_nacl
->device_list_lock
);
187 deve
= se_nacl
->device_list
[lacl
->mapped_lun
];
188 len
= sprintf(page
, "%d\n",
189 (deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
) ?
191 spin_unlock_irq(&se_nacl
->device_list_lock
);
196 static ssize_t
target_fabric_mappedlun_store_write_protect(
197 struct se_lun_acl
*lacl
,
201 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
202 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
206 ret
= kstrtoul(page
, 0, &op
);
210 if ((op
!= 1) && (op
!= 0))
213 core_update_device_list_access(lacl
->mapped_lun
, (op
) ?
214 TRANSPORT_LUNFLAGS_READ_ONLY
:
215 TRANSPORT_LUNFLAGS_READ_WRITE
,
218 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
219 " Mapped LUN: %u Write Protect bit to %s\n",
220 se_tpg
->se_tpg_tfo
->get_fabric_name(),
221 lacl
->initiatorname
, lacl
->mapped_lun
, (op
) ? "ON" : "OFF");
227 TCM_MAPPEDLUN_ATTR(write_protect
, S_IRUGO
| S_IWUSR
);
229 CONFIGFS_EATTR_OPS(target_fabric_mappedlun
, se_lun_acl
, se_lun_group
);
231 static void target_fabric_mappedlun_release(struct config_item
*item
)
233 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
234 struct se_lun_acl
, se_lun_group
);
235 struct se_portal_group
*se_tpg
= lacl
->se_lun_nacl
->se_tpg
;
237 core_dev_free_initiator_node_lun_acl(se_tpg
, lacl
);
240 static struct configfs_attribute
*target_fabric_mappedlun_attrs
[] = {
241 &target_fabric_mappedlun_write_protect
.attr
,
245 static struct configfs_item_operations target_fabric_mappedlun_item_ops
= {
246 .release
= target_fabric_mappedlun_release
,
247 .show_attribute
= target_fabric_mappedlun_attr_show
,
248 .store_attribute
= target_fabric_mappedlun_attr_store
,
249 .allow_link
= target_fabric_mappedlun_link
,
250 .drop_link
= target_fabric_mappedlun_unlink
,
253 TF_CIT_SETUP(tpg_mappedlun
, &target_fabric_mappedlun_item_ops
, NULL
,
254 target_fabric_mappedlun_attrs
);
256 /* End of tfc_tpg_mappedlun_cit */
258 /* Start of tfc_tpg_mappedlun_port_cit */
260 static struct config_group
*target_core_mappedlun_stat_mkdir(
261 struct config_group
*group
,
264 return ERR_PTR(-ENOSYS
);
267 static void target_core_mappedlun_stat_rmdir(
268 struct config_group
*group
,
269 struct config_item
*item
)
274 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops
= {
275 .make_group
= target_core_mappedlun_stat_mkdir
,
276 .drop_item
= target_core_mappedlun_stat_rmdir
,
279 TF_CIT_SETUP(tpg_mappedlun_stat
, NULL
, &target_fabric_mappedlun_stat_group_ops
,
282 /* End of tfc_tpg_mappedlun_port_cit */
284 /* Start of tfc_tpg_nacl_attrib_cit */
286 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib
, se_node_acl
, acl_attrib_group
);
288 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops
= {
289 .show_attribute
= target_fabric_nacl_attrib_attr_show
,
290 .store_attribute
= target_fabric_nacl_attrib_attr_store
,
293 TF_CIT_SETUP_DRV(tpg_nacl_attrib
, &target_fabric_nacl_attrib_item_ops
, NULL
);
295 /* End of tfc_tpg_nacl_attrib_cit */
297 /* Start of tfc_tpg_nacl_auth_cit */
299 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth
, se_node_acl
, acl_auth_group
);
301 static struct configfs_item_operations target_fabric_nacl_auth_item_ops
= {
302 .show_attribute
= target_fabric_nacl_auth_attr_show
,
303 .store_attribute
= target_fabric_nacl_auth_attr_store
,
306 TF_CIT_SETUP_DRV(tpg_nacl_auth
, &target_fabric_nacl_auth_item_ops
, NULL
);
308 /* End of tfc_tpg_nacl_auth_cit */
310 /* Start of tfc_tpg_nacl_param_cit */
312 CONFIGFS_EATTR_OPS(target_fabric_nacl_param
, se_node_acl
, acl_param_group
);
314 static struct configfs_item_operations target_fabric_nacl_param_item_ops
= {
315 .show_attribute
= target_fabric_nacl_param_attr_show
,
316 .store_attribute
= target_fabric_nacl_param_attr_store
,
319 TF_CIT_SETUP_DRV(tpg_nacl_param
, &target_fabric_nacl_param_item_ops
, NULL
);
321 /* End of tfc_tpg_nacl_param_cit */
323 /* Start of tfc_tpg_nacl_base_cit */
325 CONFIGFS_EATTR_OPS(target_fabric_nacl_base
, se_node_acl
, acl_group
);
327 static struct config_group
*target_fabric_make_mappedlun(
328 struct config_group
*group
,
331 struct se_node_acl
*se_nacl
= container_of(group
,
332 struct se_node_acl
, acl_group
);
333 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
334 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
335 struct se_lun_acl
*lacl
= NULL
;
336 struct config_item
*acl_ci
;
337 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
339 unsigned long mapped_lun
;
342 acl_ci
= &group
->cg_item
;
344 pr_err("Unable to locatel acl_ci\n");
348 buf
= kzalloc(strlen(name
) + 1, GFP_KERNEL
);
350 pr_err("Unable to allocate memory for name buf\n");
351 return ERR_PTR(-ENOMEM
);
353 snprintf(buf
, strlen(name
) + 1, "%s", name
);
355 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
357 if (strstr(buf
, "lun_") != buf
) {
358 pr_err("Unable to locate \"lun_\" from buf: %s"
359 " name: %s\n", buf
, name
);
364 * Determine the Mapped LUN value. This is what the SCSI Initiator
365 * Port will actually see.
367 ret
= kstrtoul(buf
+ 4, 0, &mapped_lun
);
370 if (mapped_lun
> UINT_MAX
) {
374 if (mapped_lun
> (TRANSPORT_MAX_LUNS_PER_TPG
-1)) {
375 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
376 "-1: %u for Target Portal Group: %u\n", mapped_lun
,
377 TRANSPORT_MAX_LUNS_PER_TPG
-1,
378 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
383 lacl
= core_dev_init_initiator_node_lun_acl(se_tpg
, se_nacl
,
390 lacl_cg
= &lacl
->se_lun_group
;
391 lacl_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
393 if (!lacl_cg
->default_groups
) {
394 pr_err("Unable to allocate lacl_cg->default_groups\n");
399 config_group_init_type_name(&lacl
->se_lun_group
, name
,
400 &tf
->tf_tpg_mappedlun_cit
);
401 config_group_init_type_name(&lacl
->ml_stat_grps
.stat_group
,
402 "statistics", &tf
->tf_tpg_mappedlun_stat_cit
);
403 lacl_cg
->default_groups
[0] = &lacl
->ml_stat_grps
.stat_group
;
404 lacl_cg
->default_groups
[1] = NULL
;
406 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
407 ml_stat_grp
->default_groups
= kmalloc(sizeof(struct config_group
*) * 3,
409 if (!ml_stat_grp
->default_groups
) {
410 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
414 target_stat_setup_mappedlun_default_groups(lacl
);
417 return &lacl
->se_lun_group
;
420 kfree(lacl_cg
->default_groups
);
426 static void target_fabric_drop_mappedlun(
427 struct config_group
*group
,
428 struct config_item
*item
)
430 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
431 struct se_lun_acl
, se_lun_group
);
432 struct config_item
*df_item
;
433 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
436 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
437 for (i
= 0; ml_stat_grp
->default_groups
[i
]; i
++) {
438 df_item
= &ml_stat_grp
->default_groups
[i
]->cg_item
;
439 ml_stat_grp
->default_groups
[i
] = NULL
;
440 config_item_put(df_item
);
442 kfree(ml_stat_grp
->default_groups
);
444 lacl_cg
= &lacl
->se_lun_group
;
445 for (i
= 0; lacl_cg
->default_groups
[i
]; i
++) {
446 df_item
= &lacl_cg
->default_groups
[i
]->cg_item
;
447 lacl_cg
->default_groups
[i
] = NULL
;
448 config_item_put(df_item
);
450 kfree(lacl_cg
->default_groups
);
452 config_item_put(item
);
455 static void target_fabric_nacl_base_release(struct config_item
*item
)
457 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
458 struct se_node_acl
, acl_group
);
459 struct target_fabric_configfs
*tf
= se_nacl
->se_tpg
->se_tpg_wwn
->wwn_tf
;
461 if (tf
->tf_ops
->fabric_cleanup_nodeacl
)
462 tf
->tf_ops
->fabric_cleanup_nodeacl(se_nacl
);
463 core_tpg_del_initiator_node_acl(se_nacl
);
466 static struct configfs_item_operations target_fabric_nacl_base_item_ops
= {
467 .release
= target_fabric_nacl_base_release
,
468 .show_attribute
= target_fabric_nacl_base_attr_show
,
469 .store_attribute
= target_fabric_nacl_base_attr_store
,
472 static struct configfs_group_operations target_fabric_nacl_base_group_ops
= {
473 .make_group
= target_fabric_make_mappedlun
,
474 .drop_item
= target_fabric_drop_mappedlun
,
477 TF_CIT_SETUP_DRV(tpg_nacl_base
, &target_fabric_nacl_base_item_ops
,
478 &target_fabric_nacl_base_group_ops
);
480 /* End of tfc_tpg_nacl_base_cit */
482 /* Start of tfc_node_fabric_stats_cit */
484 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
485 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
487 TF_CIT_SETUP(tpg_nacl_stat
, NULL
, NULL
, NULL
);
489 /* End of tfc_wwn_fabric_stats_cit */
491 /* Start of tfc_tpg_nacl_cit */
493 static struct config_group
*target_fabric_make_nodeacl(
494 struct config_group
*group
,
497 struct se_portal_group
*se_tpg
= container_of(group
,
498 struct se_portal_group
, tpg_acl_group
);
499 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
500 struct se_node_acl
*se_nacl
;
501 struct config_group
*nacl_cg
;
503 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, name
);
505 return ERR_CAST(se_nacl
);
507 if (tf
->tf_ops
->fabric_init_nodeacl
) {
508 int ret
= tf
->tf_ops
->fabric_init_nodeacl(se_nacl
, name
);
510 core_tpg_del_initiator_node_acl(se_nacl
);
515 nacl_cg
= &se_nacl
->acl_group
;
516 nacl_cg
->default_groups
= se_nacl
->acl_default_groups
;
517 nacl_cg
->default_groups
[0] = &se_nacl
->acl_attrib_group
;
518 nacl_cg
->default_groups
[1] = &se_nacl
->acl_auth_group
;
519 nacl_cg
->default_groups
[2] = &se_nacl
->acl_param_group
;
520 nacl_cg
->default_groups
[3] = &se_nacl
->acl_fabric_stat_group
;
521 nacl_cg
->default_groups
[4] = NULL
;
523 config_group_init_type_name(&se_nacl
->acl_group
, name
,
524 &tf
->tf_tpg_nacl_base_cit
);
525 config_group_init_type_name(&se_nacl
->acl_attrib_group
, "attrib",
526 &tf
->tf_tpg_nacl_attrib_cit
);
527 config_group_init_type_name(&se_nacl
->acl_auth_group
, "auth",
528 &tf
->tf_tpg_nacl_auth_cit
);
529 config_group_init_type_name(&se_nacl
->acl_param_group
, "param",
530 &tf
->tf_tpg_nacl_param_cit
);
531 config_group_init_type_name(&se_nacl
->acl_fabric_stat_group
,
532 "fabric_statistics", &tf
->tf_tpg_nacl_stat_cit
);
534 return &se_nacl
->acl_group
;
537 static void target_fabric_drop_nodeacl(
538 struct config_group
*group
,
539 struct config_item
*item
)
541 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
542 struct se_node_acl
, acl_group
);
543 struct config_item
*df_item
;
544 struct config_group
*nacl_cg
;
547 nacl_cg
= &se_nacl
->acl_group
;
548 for (i
= 0; nacl_cg
->default_groups
[i
]; i
++) {
549 df_item
= &nacl_cg
->default_groups
[i
]->cg_item
;
550 nacl_cg
->default_groups
[i
] = NULL
;
551 config_item_put(df_item
);
554 * struct se_node_acl free is done in target_fabric_nacl_base_release()
556 config_item_put(item
);
559 static struct configfs_group_operations target_fabric_nacl_group_ops
= {
560 .make_group
= target_fabric_make_nodeacl
,
561 .drop_item
= target_fabric_drop_nodeacl
,
564 TF_CIT_SETUP(tpg_nacl
, NULL
, &target_fabric_nacl_group_ops
, NULL
);
566 /* End of tfc_tpg_nacl_cit */
568 /* Start of tfc_tpg_np_base_cit */
570 CONFIGFS_EATTR_OPS(target_fabric_np_base
, se_tpg_np
, tpg_np_group
);
572 static void target_fabric_np_base_release(struct config_item
*item
)
574 struct se_tpg_np
*se_tpg_np
= container_of(to_config_group(item
),
575 struct se_tpg_np
, tpg_np_group
);
576 struct se_portal_group
*se_tpg
= se_tpg_np
->tpg_np_parent
;
577 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
579 tf
->tf_ops
->fabric_drop_np(se_tpg_np
);
582 static struct configfs_item_operations target_fabric_np_base_item_ops
= {
583 .release
= target_fabric_np_base_release
,
584 .show_attribute
= target_fabric_np_base_attr_show
,
585 .store_attribute
= target_fabric_np_base_attr_store
,
588 TF_CIT_SETUP_DRV(tpg_np_base
, &target_fabric_np_base_item_ops
, NULL
);
590 /* End of tfc_tpg_np_base_cit */
592 /* Start of tfc_tpg_np_cit */
594 static struct config_group
*target_fabric_make_np(
595 struct config_group
*group
,
598 struct se_portal_group
*se_tpg
= container_of(group
,
599 struct se_portal_group
, tpg_np_group
);
600 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
601 struct se_tpg_np
*se_tpg_np
;
603 if (!tf
->tf_ops
->fabric_make_np
) {
604 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
605 return ERR_PTR(-ENOSYS
);
608 se_tpg_np
= tf
->tf_ops
->fabric_make_np(se_tpg
, group
, name
);
609 if (!se_tpg_np
|| IS_ERR(se_tpg_np
))
610 return ERR_PTR(-EINVAL
);
612 se_tpg_np
->tpg_np_parent
= se_tpg
;
613 config_group_init_type_name(&se_tpg_np
->tpg_np_group
, name
,
614 &tf
->tf_tpg_np_base_cit
);
616 return &se_tpg_np
->tpg_np_group
;
619 static void target_fabric_drop_np(
620 struct config_group
*group
,
621 struct config_item
*item
)
624 * struct se_tpg_np is released via target_fabric_np_base_release()
626 config_item_put(item
);
629 static struct configfs_group_operations target_fabric_np_group_ops
= {
630 .make_group
= &target_fabric_make_np
,
631 .drop_item
= &target_fabric_drop_np
,
634 TF_CIT_SETUP(tpg_np
, NULL
, &target_fabric_np_group_ops
, NULL
);
636 /* End of tfc_tpg_np_cit */
638 /* Start of tfc_tpg_port_cit */
640 CONFIGFS_EATTR_STRUCT(target_fabric_port
, se_lun
);
641 #define TCM_PORT_ATTR(_name, _mode) \
642 static struct target_fabric_port_attribute target_fabric_port_##_name = \
643 __CONFIGFS_EATTR(_name, _mode, \
644 target_fabric_port_show_attr_##_name, \
645 target_fabric_port_store_attr_##_name);
647 #define TCM_PORT_ATTOR_RO(_name) \
648 __CONFIGFS_EATTR_RO(_name, \
649 target_fabric_port_show_attr_##_name);
654 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_gp(
658 if (!lun
|| !lun
->lun_sep
)
661 return core_alua_show_tg_pt_gp_info(lun
->lun_sep
, page
);
664 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_gp(
669 if (!lun
|| !lun
->lun_sep
)
672 return core_alua_store_tg_pt_gp_info(lun
->lun_sep
, page
, count
);
675 TCM_PORT_ATTR(alua_tg_pt_gp
, S_IRUGO
| S_IWUSR
);
680 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_offline(
684 if (!lun
|| !lun
->lun_sep
)
687 return core_alua_show_offline_bit(lun
, page
);
690 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_offline(
695 if (!lun
|| !lun
->lun_sep
)
698 return core_alua_store_offline_bit(lun
, page
, count
);
701 TCM_PORT_ATTR(alua_tg_pt_offline
, S_IRUGO
| S_IWUSR
);
706 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_status(
710 if (!lun
|| !lun
->lun_sep
)
713 return core_alua_show_secondary_status(lun
, page
);
716 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_status(
721 if (!lun
|| !lun
->lun_sep
)
724 return core_alua_store_secondary_status(lun
, page
, count
);
727 TCM_PORT_ATTR(alua_tg_pt_status
, S_IRUGO
| S_IWUSR
);
730 * alua_tg_pt_write_md
732 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_write_md(
736 if (!lun
|| !lun
->lun_sep
)
739 return core_alua_show_secondary_write_metadata(lun
, page
);
742 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_write_md(
747 if (!lun
|| !lun
->lun_sep
)
750 return core_alua_store_secondary_write_metadata(lun
, page
, count
);
753 TCM_PORT_ATTR(alua_tg_pt_write_md
, S_IRUGO
| S_IWUSR
);
756 static struct configfs_attribute
*target_fabric_port_attrs
[] = {
757 &target_fabric_port_alua_tg_pt_gp
.attr
,
758 &target_fabric_port_alua_tg_pt_offline
.attr
,
759 &target_fabric_port_alua_tg_pt_status
.attr
,
760 &target_fabric_port_alua_tg_pt_write_md
.attr
,
764 CONFIGFS_EATTR_OPS(target_fabric_port
, se_lun
, lun_group
);
766 static int target_fabric_port_link(
767 struct config_item
*lun_ci
,
768 struct config_item
*se_dev_ci
)
770 struct config_item
*tpg_ci
;
771 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
772 struct se_lun
, lun_group
);
773 struct se_lun
*lun_p
;
774 struct se_portal_group
*se_tpg
;
775 struct se_device
*dev
=
776 container_of(to_config_group(se_dev_ci
), struct se_device
, dev_group
);
777 struct target_fabric_configfs
*tf
;
780 if (dev
->dev_link_magic
!= SE_DEV_LINK_MAGIC
) {
781 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
782 " %p to struct se_device: %p\n", se_dev_ci
, dev
);
786 if (!(dev
->dev_flags
& DF_CONFIGURED
)) {
787 pr_err("se_device not configured yet, cannot port link\n");
791 tpg_ci
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
792 se_tpg
= container_of(to_config_group(tpg_ci
),
793 struct se_portal_group
, tpg_group
);
794 tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
796 if (lun
->lun_se_dev
!= NULL
) {
797 pr_err("Port Symlink already exists\n");
801 lun_p
= core_dev_add_lun(se_tpg
, dev
, lun
->unpacked_lun
);
803 pr_err("core_dev_add_lun() failed\n");
804 ret
= PTR_ERR(lun_p
);
808 if (tf
->tf_ops
->fabric_post_link
) {
810 * Call the optional fabric_post_link() to allow a
811 * fabric module to setup any additional state once
812 * core_dev_add_lun() has been called..
814 tf
->tf_ops
->fabric_post_link(se_tpg
, lun
);
822 static int target_fabric_port_unlink(
823 struct config_item
*lun_ci
,
824 struct config_item
*se_dev_ci
)
826 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
827 struct se_lun
, lun_group
);
828 struct se_portal_group
*se_tpg
= lun
->lun_sep
->sep_tpg
;
829 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
831 if (tf
->tf_ops
->fabric_pre_unlink
) {
833 * Call the optional fabric_pre_unlink() to allow a
834 * fabric module to release any additional stat before
835 * core_dev_del_lun() is called.
837 tf
->tf_ops
->fabric_pre_unlink(se_tpg
, lun
);
840 core_dev_del_lun(se_tpg
, lun
);
844 static struct configfs_item_operations target_fabric_port_item_ops
= {
845 .show_attribute
= target_fabric_port_attr_show
,
846 .store_attribute
= target_fabric_port_attr_store
,
847 .allow_link
= target_fabric_port_link
,
848 .drop_link
= target_fabric_port_unlink
,
851 TF_CIT_SETUP(tpg_port
, &target_fabric_port_item_ops
, NULL
, target_fabric_port_attrs
);
853 /* End of tfc_tpg_port_cit */
855 /* Start of tfc_tpg_port_stat_cit */
857 static struct config_group
*target_core_port_stat_mkdir(
858 struct config_group
*group
,
861 return ERR_PTR(-ENOSYS
);
864 static void target_core_port_stat_rmdir(
865 struct config_group
*group
,
866 struct config_item
*item
)
871 static struct configfs_group_operations target_fabric_port_stat_group_ops
= {
872 .make_group
= target_core_port_stat_mkdir
,
873 .drop_item
= target_core_port_stat_rmdir
,
876 TF_CIT_SETUP(tpg_port_stat
, NULL
, &target_fabric_port_stat_group_ops
, NULL
);
878 /* End of tfc_tpg_port_stat_cit */
880 /* Start of tfc_tpg_lun_cit */
882 static struct config_group
*target_fabric_make_lun(
883 struct config_group
*group
,
887 struct se_portal_group
*se_tpg
= container_of(group
,
888 struct se_portal_group
, tpg_lun_group
);
889 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
890 struct config_group
*lun_cg
= NULL
, *port_stat_grp
= NULL
;
891 unsigned long unpacked_lun
;
894 if (strstr(name
, "lun_") != name
) {
895 pr_err("Unable to locate \'_\" in"
896 " \"lun_$LUN_NUMBER\"\n");
897 return ERR_PTR(-EINVAL
);
899 errno
= kstrtoul(name
+ 4, 0, &unpacked_lun
);
901 return ERR_PTR(errno
);
902 if (unpacked_lun
> UINT_MAX
)
903 return ERR_PTR(-EINVAL
);
905 lun
= core_get_lun_from_tpg(se_tpg
, unpacked_lun
);
907 return ERR_PTR(-EINVAL
);
909 lun_cg
= &lun
->lun_group
;
910 lun_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
912 if (!lun_cg
->default_groups
) {
913 pr_err("Unable to allocate lun_cg->default_groups\n");
914 return ERR_PTR(-ENOMEM
);
917 config_group_init_type_name(&lun
->lun_group
, name
,
918 &tf
->tf_tpg_port_cit
);
919 config_group_init_type_name(&lun
->port_stat_grps
.stat_group
,
920 "statistics", &tf
->tf_tpg_port_stat_cit
);
921 lun_cg
->default_groups
[0] = &lun
->port_stat_grps
.stat_group
;
922 lun_cg
->default_groups
[1] = NULL
;
924 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
925 port_stat_grp
->default_groups
= kzalloc(sizeof(struct config_group
*) * 4,
927 if (!port_stat_grp
->default_groups
) {
928 pr_err("Unable to allocate port_stat_grp->default_groups\n");
929 kfree(lun_cg
->default_groups
);
930 return ERR_PTR(-ENOMEM
);
932 target_stat_setup_port_default_groups(lun
);
934 return &lun
->lun_group
;
937 static void target_fabric_drop_lun(
938 struct config_group
*group
,
939 struct config_item
*item
)
941 struct se_lun
*lun
= container_of(to_config_group(item
),
942 struct se_lun
, lun_group
);
943 struct config_item
*df_item
;
944 struct config_group
*lun_cg
, *port_stat_grp
;
947 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
948 for (i
= 0; port_stat_grp
->default_groups
[i
]; i
++) {
949 df_item
= &port_stat_grp
->default_groups
[i
]->cg_item
;
950 port_stat_grp
->default_groups
[i
] = NULL
;
951 config_item_put(df_item
);
953 kfree(port_stat_grp
->default_groups
);
955 lun_cg
= &lun
->lun_group
;
956 for (i
= 0; lun_cg
->default_groups
[i
]; i
++) {
957 df_item
= &lun_cg
->default_groups
[i
]->cg_item
;
958 lun_cg
->default_groups
[i
] = NULL
;
959 config_item_put(df_item
);
961 kfree(lun_cg
->default_groups
);
963 config_item_put(item
);
966 static struct configfs_group_operations target_fabric_lun_group_ops
= {
967 .make_group
= &target_fabric_make_lun
,
968 .drop_item
= &target_fabric_drop_lun
,
971 TF_CIT_SETUP(tpg_lun
, NULL
, &target_fabric_lun_group_ops
, NULL
);
973 /* End of tfc_tpg_lun_cit */
975 /* Start of tfc_tpg_attrib_cit */
977 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib
, se_portal_group
, tpg_attrib_group
);
979 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops
= {
980 .show_attribute
= target_fabric_tpg_attrib_attr_show
,
981 .store_attribute
= target_fabric_tpg_attrib_attr_store
,
984 TF_CIT_SETUP_DRV(tpg_attrib
, &target_fabric_tpg_attrib_item_ops
, NULL
);
986 /* End of tfc_tpg_attrib_cit */
988 /* Start of tfc_tpg_auth_cit */
990 CONFIGFS_EATTR_OPS(target_fabric_tpg_auth
, se_portal_group
, tpg_auth_group
);
992 static struct configfs_item_operations target_fabric_tpg_auth_item_ops
= {
993 .show_attribute
= target_fabric_tpg_auth_attr_show
,
994 .store_attribute
= target_fabric_tpg_auth_attr_store
,
997 TF_CIT_SETUP_DRV(tpg_auth
, &target_fabric_tpg_auth_item_ops
, NULL
);
999 /* End of tfc_tpg_attrib_cit */
1001 /* Start of tfc_tpg_param_cit */
1003 CONFIGFS_EATTR_OPS(target_fabric_tpg_param
, se_portal_group
, tpg_param_group
);
1005 static struct configfs_item_operations target_fabric_tpg_param_item_ops
= {
1006 .show_attribute
= target_fabric_tpg_param_attr_show
,
1007 .store_attribute
= target_fabric_tpg_param_attr_store
,
1010 TF_CIT_SETUP_DRV(tpg_param
, &target_fabric_tpg_param_item_ops
, NULL
);
1012 /* End of tfc_tpg_param_cit */
1014 /* Start of tfc_tpg_base_cit */
1016 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1018 CONFIGFS_EATTR_OPS(target_fabric_tpg
, se_portal_group
, tpg_group
);
1020 static void target_fabric_tpg_release(struct config_item
*item
)
1022 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
1023 struct se_portal_group
, tpg_group
);
1024 struct se_wwn
*wwn
= se_tpg
->se_tpg_wwn
;
1025 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
1027 tf
->tf_ops
->fabric_drop_tpg(se_tpg
);
1030 static struct configfs_item_operations target_fabric_tpg_base_item_ops
= {
1031 .release
= target_fabric_tpg_release
,
1032 .show_attribute
= target_fabric_tpg_attr_show
,
1033 .store_attribute
= target_fabric_tpg_attr_store
,
1036 TF_CIT_SETUP_DRV(tpg_base
, &target_fabric_tpg_base_item_ops
, NULL
);
1038 /* End of tfc_tpg_base_cit */
1040 /* Start of tfc_tpg_cit */
1042 static struct config_group
*target_fabric_make_tpg(
1043 struct config_group
*group
,
1046 struct se_wwn
*wwn
= container_of(group
, struct se_wwn
, wwn_group
);
1047 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
1048 struct se_portal_group
*se_tpg
;
1050 if (!tf
->tf_ops
->fabric_make_tpg
) {
1051 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
1052 return ERR_PTR(-ENOSYS
);
1055 se_tpg
= tf
->tf_ops
->fabric_make_tpg(wwn
, group
, name
);
1056 if (!se_tpg
|| IS_ERR(se_tpg
))
1057 return ERR_PTR(-EINVAL
);
1059 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1061 se_tpg
->tpg_group
.default_groups
= se_tpg
->tpg_default_groups
;
1062 se_tpg
->tpg_group
.default_groups
[0] = &se_tpg
->tpg_lun_group
;
1063 se_tpg
->tpg_group
.default_groups
[1] = &se_tpg
->tpg_np_group
;
1064 se_tpg
->tpg_group
.default_groups
[2] = &se_tpg
->tpg_acl_group
;
1065 se_tpg
->tpg_group
.default_groups
[3] = &se_tpg
->tpg_attrib_group
;
1066 se_tpg
->tpg_group
.default_groups
[4] = &se_tpg
->tpg_auth_group
;
1067 se_tpg
->tpg_group
.default_groups
[5] = &se_tpg
->tpg_param_group
;
1068 se_tpg
->tpg_group
.default_groups
[6] = NULL
;
1070 config_group_init_type_name(&se_tpg
->tpg_group
, name
,
1071 &tf
->tf_tpg_base_cit
);
1072 config_group_init_type_name(&se_tpg
->tpg_lun_group
, "lun",
1073 &tf
->tf_tpg_lun_cit
);
1074 config_group_init_type_name(&se_tpg
->tpg_np_group
, "np",
1075 &tf
->tf_tpg_np_cit
);
1076 config_group_init_type_name(&se_tpg
->tpg_acl_group
, "acls",
1077 &tf
->tf_tpg_nacl_cit
);
1078 config_group_init_type_name(&se_tpg
->tpg_attrib_group
, "attrib",
1079 &tf
->tf_tpg_attrib_cit
);
1080 config_group_init_type_name(&se_tpg
->tpg_auth_group
, "auth",
1081 &tf
->tf_tpg_auth_cit
);
1082 config_group_init_type_name(&se_tpg
->tpg_param_group
, "param",
1083 &tf
->tf_tpg_param_cit
);
1085 return &se_tpg
->tpg_group
;
1088 static void target_fabric_drop_tpg(
1089 struct config_group
*group
,
1090 struct config_item
*item
)
1092 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
1093 struct se_portal_group
, tpg_group
);
1094 struct config_group
*tpg_cg
= &se_tpg
->tpg_group
;
1095 struct config_item
*df_item
;
1098 * Release default groups, but do not release tpg_cg->default_groups
1099 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1101 for (i
= 0; tpg_cg
->default_groups
[i
]; i
++) {
1102 df_item
= &tpg_cg
->default_groups
[i
]->cg_item
;
1103 tpg_cg
->default_groups
[i
] = NULL
;
1104 config_item_put(df_item
);
1107 config_item_put(item
);
1110 static void target_fabric_release_wwn(struct config_item
*item
)
1112 struct se_wwn
*wwn
= container_of(to_config_group(item
),
1113 struct se_wwn
, wwn_group
);
1114 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
1116 tf
->tf_ops
->fabric_drop_wwn(wwn
);
1119 static struct configfs_item_operations target_fabric_tpg_item_ops
= {
1120 .release
= target_fabric_release_wwn
,
1123 static struct configfs_group_operations target_fabric_tpg_group_ops
= {
1124 .make_group
= target_fabric_make_tpg
,
1125 .drop_item
= target_fabric_drop_tpg
,
1128 TF_CIT_SETUP(tpg
, &target_fabric_tpg_item_ops
, &target_fabric_tpg_group_ops
,
1131 /* End of tfc_tpg_cit */
1133 /* Start of tfc_wwn_fabric_stats_cit */
1135 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1136 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1138 TF_CIT_SETUP(wwn_fabric_stats
, NULL
, NULL
, NULL
);
1140 /* End of tfc_wwn_fabric_stats_cit */
1142 /* Start of tfc_wwn_cit */
1144 static struct config_group
*target_fabric_make_wwn(
1145 struct config_group
*group
,
1148 struct target_fabric_configfs
*tf
= container_of(group
,
1149 struct target_fabric_configfs
, tf_group
);
1152 if (!tf
->tf_ops
->fabric_make_wwn
) {
1153 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1154 return ERR_PTR(-ENOSYS
);
1157 wwn
= tf
->tf_ops
->fabric_make_wwn(tf
, group
, name
);
1158 if (!wwn
|| IS_ERR(wwn
))
1159 return ERR_PTR(-EINVAL
);
1163 * Setup default groups from pre-allocated wwn->wwn_default_groups
1165 wwn
->wwn_group
.default_groups
= wwn
->wwn_default_groups
;
1166 wwn
->wwn_group
.default_groups
[0] = &wwn
->fabric_stat_group
;
1167 wwn
->wwn_group
.default_groups
[1] = NULL
;
1169 config_group_init_type_name(&wwn
->wwn_group
, name
, &tf
->tf_tpg_cit
);
1170 config_group_init_type_name(&wwn
->fabric_stat_group
, "fabric_statistics",
1171 &tf
->tf_wwn_fabric_stats_cit
);
1173 return &wwn
->wwn_group
;
1176 static void target_fabric_drop_wwn(
1177 struct config_group
*group
,
1178 struct config_item
*item
)
1180 struct se_wwn
*wwn
= container_of(to_config_group(item
),
1181 struct se_wwn
, wwn_group
);
1182 struct config_item
*df_item
;
1183 struct config_group
*cg
= &wwn
->wwn_group
;
1186 for (i
= 0; cg
->default_groups
[i
]; i
++) {
1187 df_item
= &cg
->default_groups
[i
]->cg_item
;
1188 cg
->default_groups
[i
] = NULL
;
1189 config_item_put(df_item
);
1192 config_item_put(item
);
1195 static struct configfs_group_operations target_fabric_wwn_group_ops
= {
1196 .make_group
= target_fabric_make_wwn
,
1197 .drop_item
= target_fabric_drop_wwn
,
1200 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1202 CONFIGFS_EATTR_OPS(target_fabric_wwn
, target_fabric_configfs
, tf_group
);
1204 static struct configfs_item_operations target_fabric_wwn_item_ops
= {
1205 .show_attribute
= target_fabric_wwn_attr_show
,
1206 .store_attribute
= target_fabric_wwn_attr_store
,
1209 TF_CIT_SETUP_DRV(wwn
, &target_fabric_wwn_item_ops
, &target_fabric_wwn_group_ops
);
1211 /* End of tfc_wwn_cit */
1213 /* Start of tfc_discovery_cit */
1215 CONFIGFS_EATTR_OPS(target_fabric_discovery
, target_fabric_configfs
,
1218 static struct configfs_item_operations target_fabric_discovery_item_ops
= {
1219 .show_attribute
= target_fabric_discovery_attr_show
,
1220 .store_attribute
= target_fabric_discovery_attr_store
,
1223 TF_CIT_SETUP_DRV(discovery
, &target_fabric_discovery_item_ops
, NULL
);
1225 /* End of tfc_discovery_cit */
1227 int target_fabric_setup_cits(struct target_fabric_configfs
*tf
)
1229 target_fabric_setup_discovery_cit(tf
);
1230 target_fabric_setup_wwn_cit(tf
);
1231 target_fabric_setup_wwn_fabric_stats_cit(tf
);
1232 target_fabric_setup_tpg_cit(tf
);
1233 target_fabric_setup_tpg_base_cit(tf
);
1234 target_fabric_setup_tpg_port_cit(tf
);
1235 target_fabric_setup_tpg_port_stat_cit(tf
);
1236 target_fabric_setup_tpg_lun_cit(tf
);
1237 target_fabric_setup_tpg_np_cit(tf
);
1238 target_fabric_setup_tpg_np_base_cit(tf
);
1239 target_fabric_setup_tpg_attrib_cit(tf
);
1240 target_fabric_setup_tpg_auth_cit(tf
);
1241 target_fabric_setup_tpg_param_cit(tf
);
1242 target_fabric_setup_tpg_nacl_cit(tf
);
1243 target_fabric_setup_tpg_nacl_base_cit(tf
);
1244 target_fabric_setup_tpg_nacl_attrib_cit(tf
);
1245 target_fabric_setup_tpg_nacl_auth_cit(tf
);
1246 target_fabric_setup_tpg_nacl_param_cit(tf
);
1247 target_fabric_setup_tpg_nacl_stat_cit(tf
);
1248 target_fabric_setup_tpg_mappedlun_cit(tf
);
1249 target_fabric_setup_tpg_mappedlun_stat_cit(tf
);