1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * Copyright (c) 2010,2011 Rising Tide Systems
8 * Copyright (c) 2010,2011 Linux-iSCSI.org
10 * Copyright (c) Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ****************************************************************************/
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <generated/utsrelease.h>
26 #include <linux/utsname.h>
27 #include <linux/init.h>
29 #include <linux/namei.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <linux/unistd.h>
34 #include <linux/string.h>
35 #include <linux/syscalls.h>
36 #include <linux/configfs.h>
38 #include <target/target_core_base.h>
39 #include <target/target_core_fabric.h>
40 #include <target/target_core_fabric_configfs.h>
41 #include <target/target_core_configfs.h>
42 #include <target/configfs_macros.h>
44 #include "target_core_internal.h"
45 #include "target_core_alua.h"
46 #include "target_core_pr.h"
48 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
49 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
51 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
52 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
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_module; \
58 pr_debug("Setup generic %s\n", __stringify(_name)); \
61 /* Start of tfc_tpg_mappedlun_cit */
63 static int target_fabric_mappedlun_link(
64 struct config_item
*lun_acl_ci
,
65 struct config_item
*lun_ci
)
67 struct se_dev_entry
*deve
;
68 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
69 struct se_lun
, lun_group
);
70 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
71 struct se_lun_acl
, se_lun_group
);
72 struct se_portal_group
*se_tpg
;
73 struct config_item
*nacl_ci
, *tpg_ci
, *tpg_ci_s
, *wwn_ci
, *wwn_ci_s
;
74 int ret
= 0, lun_access
;
76 * Ensure that the source port exists
78 if (!lun
->lun_sep
|| !lun
->lun_sep
->sep_tpg
) {
79 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
80 "_tpg does not exist\n");
83 se_tpg
= lun
->lun_sep
->sep_tpg
;
85 nacl_ci
= &lun_acl_ci
->ci_parent
->ci_group
->cg_item
;
86 tpg_ci
= &nacl_ci
->ci_group
->cg_item
;
87 wwn_ci
= &tpg_ci
->ci_group
->cg_item
;
88 tpg_ci_s
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
89 wwn_ci_s
= &tpg_ci_s
->ci_group
->cg_item
;
91 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
93 if (strcmp(config_item_name(wwn_ci
), config_item_name(wwn_ci_s
))) {
94 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
95 config_item_name(wwn_ci
));
98 if (strcmp(config_item_name(tpg_ci
), config_item_name(tpg_ci_s
))) {
99 pr_err("Illegal Initiator ACL Symlink outside of %s"
100 " TPGT: %s\n", config_item_name(wwn_ci
),
101 config_item_name(tpg_ci
));
105 * If this struct se_node_acl was dynamically generated with
106 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
107 * which be will write protected (READ-ONLY) when
108 * tpg_1/attrib/demo_mode_write_protect=1
110 spin_lock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
111 deve
= lacl
->se_lun_nacl
->device_list
[lacl
->mapped_lun
];
112 if (deve
->lun_flags
& TRANSPORT_LUNFLAGS_INITIATOR_ACCESS
)
113 lun_access
= deve
->lun_flags
;
116 (se_tpg
->se_tpg_tfo
->tpg_check_prod_mode_write_protect(
117 se_tpg
)) ? TRANSPORT_LUNFLAGS_READ_ONLY
:
118 TRANSPORT_LUNFLAGS_READ_WRITE
;
119 spin_unlock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
121 * Determine the actual mapped LUN value user wants..
123 * This value is what the SCSI Initiator actually sees the
124 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
126 ret
= core_dev_add_initiator_node_lun_acl(se_tpg
, lacl
,
127 lun
->unpacked_lun
, lun_access
);
129 return (ret
< 0) ? -EINVAL
: 0;
132 static int target_fabric_mappedlun_unlink(
133 struct config_item
*lun_acl_ci
,
134 struct config_item
*lun_ci
)
137 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
138 struct se_lun_acl
, se_lun_group
);
139 struct se_node_acl
*nacl
= lacl
->se_lun_nacl
;
140 struct se_dev_entry
*deve
= nacl
->device_list
[lacl
->mapped_lun
];
141 struct se_portal_group
*se_tpg
;
143 * Determine if the underlying MappedLUN has already been released..
148 lun
= container_of(to_config_group(lun_ci
), struct se_lun
, lun_group
);
149 se_tpg
= lun
->lun_sep
->sep_tpg
;
151 core_dev_del_initiator_node_lun_acl(se_tpg
, lun
, lacl
);
155 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun
, se_lun_acl
);
156 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
157 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
158 __CONFIGFS_EATTR(_name, _mode, \
159 target_fabric_mappedlun_show_##_name, \
160 target_fabric_mappedlun_store_##_name);
162 static ssize_t
target_fabric_mappedlun_show_write_protect(
163 struct se_lun_acl
*lacl
,
166 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
167 struct se_dev_entry
*deve
;
170 spin_lock_irq(&se_nacl
->device_list_lock
);
171 deve
= se_nacl
->device_list
[lacl
->mapped_lun
];
172 len
= sprintf(page
, "%d\n",
173 (deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
) ?
175 spin_unlock_irq(&se_nacl
->device_list_lock
);
180 static ssize_t
target_fabric_mappedlun_store_write_protect(
181 struct se_lun_acl
*lacl
,
185 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
186 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
189 if (strict_strtoul(page
, 0, &op
))
192 if ((op
!= 1) && (op
!= 0))
195 core_update_device_list_access(lacl
->mapped_lun
, (op
) ?
196 TRANSPORT_LUNFLAGS_READ_ONLY
:
197 TRANSPORT_LUNFLAGS_READ_WRITE
,
200 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
201 " Mapped LUN: %u Write Protect bit to %s\n",
202 se_tpg
->se_tpg_tfo
->get_fabric_name(),
203 lacl
->initiatorname
, lacl
->mapped_lun
, (op
) ? "ON" : "OFF");
209 TCM_MAPPEDLUN_ATTR(write_protect
, S_IRUGO
| S_IWUSR
);
211 CONFIGFS_EATTR_OPS(target_fabric_mappedlun
, se_lun_acl
, se_lun_group
);
213 static void target_fabric_mappedlun_release(struct config_item
*item
)
215 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
216 struct se_lun_acl
, se_lun_group
);
217 struct se_portal_group
*se_tpg
= lacl
->se_lun_nacl
->se_tpg
;
219 core_dev_free_initiator_node_lun_acl(se_tpg
, lacl
);
222 static struct configfs_attribute
*target_fabric_mappedlun_attrs
[] = {
223 &target_fabric_mappedlun_write_protect
.attr
,
227 static struct configfs_item_operations target_fabric_mappedlun_item_ops
= {
228 .release
= target_fabric_mappedlun_release
,
229 .show_attribute
= target_fabric_mappedlun_attr_show
,
230 .store_attribute
= target_fabric_mappedlun_attr_store
,
231 .allow_link
= target_fabric_mappedlun_link
,
232 .drop_link
= target_fabric_mappedlun_unlink
,
235 TF_CIT_SETUP(tpg_mappedlun
, &target_fabric_mappedlun_item_ops
, NULL
,
236 target_fabric_mappedlun_attrs
);
238 /* End of tfc_tpg_mappedlun_cit */
240 /* Start of tfc_tpg_mappedlun_port_cit */
242 static struct config_group
*target_core_mappedlun_stat_mkdir(
243 struct config_group
*group
,
246 return ERR_PTR(-ENOSYS
);
249 static void target_core_mappedlun_stat_rmdir(
250 struct config_group
*group
,
251 struct config_item
*item
)
256 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops
= {
257 .make_group
= target_core_mappedlun_stat_mkdir
,
258 .drop_item
= target_core_mappedlun_stat_rmdir
,
261 TF_CIT_SETUP(tpg_mappedlun_stat
, NULL
, &target_fabric_mappedlun_stat_group_ops
,
264 /* End of tfc_tpg_mappedlun_port_cit */
266 /* Start of tfc_tpg_nacl_attrib_cit */
268 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib
, se_node_acl
, acl_attrib_group
);
270 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops
= {
271 .show_attribute
= target_fabric_nacl_attrib_attr_show
,
272 .store_attribute
= target_fabric_nacl_attrib_attr_store
,
275 TF_CIT_SETUP(tpg_nacl_attrib
, &target_fabric_nacl_attrib_item_ops
, NULL
, NULL
);
277 /* End of tfc_tpg_nacl_attrib_cit */
279 /* Start of tfc_tpg_nacl_auth_cit */
281 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth
, se_node_acl
, acl_auth_group
);
283 static struct configfs_item_operations target_fabric_nacl_auth_item_ops
= {
284 .show_attribute
= target_fabric_nacl_auth_attr_show
,
285 .store_attribute
= target_fabric_nacl_auth_attr_store
,
288 TF_CIT_SETUP(tpg_nacl_auth
, &target_fabric_nacl_auth_item_ops
, NULL
, NULL
);
290 /* End of tfc_tpg_nacl_auth_cit */
292 /* Start of tfc_tpg_nacl_param_cit */
294 CONFIGFS_EATTR_OPS(target_fabric_nacl_param
, se_node_acl
, acl_param_group
);
296 static struct configfs_item_operations target_fabric_nacl_param_item_ops
= {
297 .show_attribute
= target_fabric_nacl_param_attr_show
,
298 .store_attribute
= target_fabric_nacl_param_attr_store
,
301 TF_CIT_SETUP(tpg_nacl_param
, &target_fabric_nacl_param_item_ops
, NULL
, NULL
);
303 /* End of tfc_tpg_nacl_param_cit */
305 /* Start of tfc_tpg_nacl_base_cit */
307 CONFIGFS_EATTR_OPS(target_fabric_nacl_base
, se_node_acl
, acl_group
);
309 static struct config_group
*target_fabric_make_mappedlun(
310 struct config_group
*group
,
313 struct se_node_acl
*se_nacl
= container_of(group
,
314 struct se_node_acl
, acl_group
);
315 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
316 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
317 struct se_lun_acl
*lacl
;
318 struct config_item
*acl_ci
;
319 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
321 unsigned long mapped_lun
;
324 acl_ci
= &group
->cg_item
;
326 pr_err("Unable to locatel acl_ci\n");
330 buf
= kzalloc(strlen(name
) + 1, GFP_KERNEL
);
332 pr_err("Unable to allocate memory for name buf\n");
333 return ERR_PTR(-ENOMEM
);
335 snprintf(buf
, strlen(name
) + 1, "%s", name
);
337 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
339 if (strstr(buf
, "lun_") != buf
) {
340 pr_err("Unable to locate \"lun_\" from buf: %s"
341 " name: %s\n", buf
, name
);
346 * Determine the Mapped LUN value. This is what the SCSI Initiator
347 * Port will actually see.
349 if (strict_strtoul(buf
+ 4, 0, &mapped_lun
) || mapped_lun
> UINT_MAX
) {
354 lacl
= core_dev_init_initiator_node_lun_acl(se_tpg
, mapped_lun
,
355 config_item_name(acl_ci
), &ret
);
361 lacl_cg
= &lacl
->se_lun_group
;
362 lacl_cg
->default_groups
= kzalloc(sizeof(struct config_group
) * 2,
364 if (!lacl_cg
->default_groups
) {
365 pr_err("Unable to allocate lacl_cg->default_groups\n");
370 config_group_init_type_name(&lacl
->se_lun_group
, name
,
371 &TF_CIT_TMPL(tf
)->tfc_tpg_mappedlun_cit
);
372 config_group_init_type_name(&lacl
->ml_stat_grps
.stat_group
,
373 "statistics", &TF_CIT_TMPL(tf
)->tfc_tpg_mappedlun_stat_cit
);
374 lacl_cg
->default_groups
[0] = &lacl
->ml_stat_grps
.stat_group
;
375 lacl_cg
->default_groups
[1] = NULL
;
377 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
378 ml_stat_grp
->default_groups
= kzalloc(sizeof(struct config_group
) * 3,
380 if (!ml_stat_grp
->default_groups
) {
381 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
385 target_stat_setup_mappedlun_default_groups(lacl
);
388 return &lacl
->se_lun_group
;
391 kfree(lacl_cg
->default_groups
);
396 static void target_fabric_drop_mappedlun(
397 struct config_group
*group
,
398 struct config_item
*item
)
400 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
401 struct se_lun_acl
, se_lun_group
);
402 struct config_item
*df_item
;
403 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
406 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
407 for (i
= 0; ml_stat_grp
->default_groups
[i
]; i
++) {
408 df_item
= &ml_stat_grp
->default_groups
[i
]->cg_item
;
409 ml_stat_grp
->default_groups
[i
] = NULL
;
410 config_item_put(df_item
);
412 kfree(ml_stat_grp
->default_groups
);
414 lacl_cg
= &lacl
->se_lun_group
;
415 for (i
= 0; lacl_cg
->default_groups
[i
]; i
++) {
416 df_item
= &lacl_cg
->default_groups
[i
]->cg_item
;
417 lacl_cg
->default_groups
[i
] = NULL
;
418 config_item_put(df_item
);
420 kfree(lacl_cg
->default_groups
);
422 config_item_put(item
);
425 static void target_fabric_nacl_base_release(struct config_item
*item
)
427 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
428 struct se_node_acl
, acl_group
);
429 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
430 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
432 tf
->tf_ops
.fabric_drop_nodeacl(se_nacl
);
435 static struct configfs_item_operations target_fabric_nacl_base_item_ops
= {
436 .release
= target_fabric_nacl_base_release
,
437 .show_attribute
= target_fabric_nacl_base_attr_show
,
438 .store_attribute
= target_fabric_nacl_base_attr_store
,
441 static struct configfs_group_operations target_fabric_nacl_base_group_ops
= {
442 .make_group
= target_fabric_make_mappedlun
,
443 .drop_item
= target_fabric_drop_mappedlun
,
446 TF_CIT_SETUP(tpg_nacl_base
, &target_fabric_nacl_base_item_ops
,
447 &target_fabric_nacl_base_group_ops
, NULL
);
449 /* End of tfc_tpg_nacl_base_cit */
451 /* Start of tfc_node_fabric_stats_cit */
453 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
454 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
456 TF_CIT_SETUP(tpg_nacl_stat
, NULL
, NULL
, NULL
);
458 /* End of tfc_wwn_fabric_stats_cit */
460 /* Start of tfc_tpg_nacl_cit */
462 static struct config_group
*target_fabric_make_nodeacl(
463 struct config_group
*group
,
466 struct se_portal_group
*se_tpg
= container_of(group
,
467 struct se_portal_group
, tpg_acl_group
);
468 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
469 struct se_node_acl
*se_nacl
;
470 struct config_group
*nacl_cg
;
472 if (!tf
->tf_ops
.fabric_make_nodeacl
) {
473 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
474 return ERR_PTR(-ENOSYS
);
477 se_nacl
= tf
->tf_ops
.fabric_make_nodeacl(se_tpg
, group
, name
);
479 return ERR_CAST(se_nacl
);
481 nacl_cg
= &se_nacl
->acl_group
;
482 nacl_cg
->default_groups
= se_nacl
->acl_default_groups
;
483 nacl_cg
->default_groups
[0] = &se_nacl
->acl_attrib_group
;
484 nacl_cg
->default_groups
[1] = &se_nacl
->acl_auth_group
;
485 nacl_cg
->default_groups
[2] = &se_nacl
->acl_param_group
;
486 nacl_cg
->default_groups
[3] = &se_nacl
->acl_fabric_stat_group
;
487 nacl_cg
->default_groups
[4] = NULL
;
489 config_group_init_type_name(&se_nacl
->acl_group
, name
,
490 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_base_cit
);
491 config_group_init_type_name(&se_nacl
->acl_attrib_group
, "attrib",
492 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_attrib_cit
);
493 config_group_init_type_name(&se_nacl
->acl_auth_group
, "auth",
494 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_auth_cit
);
495 config_group_init_type_name(&se_nacl
->acl_param_group
, "param",
496 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_param_cit
);
497 config_group_init_type_name(&se_nacl
->acl_fabric_stat_group
,
499 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_stat_cit
);
501 return &se_nacl
->acl_group
;
504 static void target_fabric_drop_nodeacl(
505 struct config_group
*group
,
506 struct config_item
*item
)
508 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
509 struct se_node_acl
, acl_group
);
510 struct config_item
*df_item
;
511 struct config_group
*nacl_cg
;
514 nacl_cg
= &se_nacl
->acl_group
;
515 for (i
= 0; nacl_cg
->default_groups
[i
]; i
++) {
516 df_item
= &nacl_cg
->default_groups
[i
]->cg_item
;
517 nacl_cg
->default_groups
[i
] = NULL
;
518 config_item_put(df_item
);
521 * struct se_node_acl free is done in target_fabric_nacl_base_release()
523 config_item_put(item
);
526 static struct configfs_group_operations target_fabric_nacl_group_ops
= {
527 .make_group
= target_fabric_make_nodeacl
,
528 .drop_item
= target_fabric_drop_nodeacl
,
531 TF_CIT_SETUP(tpg_nacl
, NULL
, &target_fabric_nacl_group_ops
, NULL
);
533 /* End of tfc_tpg_nacl_cit */
535 /* Start of tfc_tpg_np_base_cit */
537 CONFIGFS_EATTR_OPS(target_fabric_np_base
, se_tpg_np
, tpg_np_group
);
539 static void target_fabric_np_base_release(struct config_item
*item
)
541 struct se_tpg_np
*se_tpg_np
= container_of(to_config_group(item
),
542 struct se_tpg_np
, tpg_np_group
);
543 struct se_portal_group
*se_tpg
= se_tpg_np
->tpg_np_parent
;
544 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
546 tf
->tf_ops
.fabric_drop_np(se_tpg_np
);
549 static struct configfs_item_operations target_fabric_np_base_item_ops
= {
550 .release
= target_fabric_np_base_release
,
551 .show_attribute
= target_fabric_np_base_attr_show
,
552 .store_attribute
= target_fabric_np_base_attr_store
,
555 TF_CIT_SETUP(tpg_np_base
, &target_fabric_np_base_item_ops
, NULL
, NULL
);
557 /* End of tfc_tpg_np_base_cit */
559 /* Start of tfc_tpg_np_cit */
561 static struct config_group
*target_fabric_make_np(
562 struct config_group
*group
,
565 struct se_portal_group
*se_tpg
= container_of(group
,
566 struct se_portal_group
, tpg_np_group
);
567 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
568 struct se_tpg_np
*se_tpg_np
;
570 if (!tf
->tf_ops
.fabric_make_np
) {
571 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
572 return ERR_PTR(-ENOSYS
);
575 se_tpg_np
= tf
->tf_ops
.fabric_make_np(se_tpg
, group
, name
);
576 if (!se_tpg_np
|| IS_ERR(se_tpg_np
))
577 return ERR_PTR(-EINVAL
);
579 se_tpg_np
->tpg_np_parent
= se_tpg
;
580 config_group_init_type_name(&se_tpg_np
->tpg_np_group
, name
,
581 &TF_CIT_TMPL(tf
)->tfc_tpg_np_base_cit
);
583 return &se_tpg_np
->tpg_np_group
;
586 static void target_fabric_drop_np(
587 struct config_group
*group
,
588 struct config_item
*item
)
591 * struct se_tpg_np is released via target_fabric_np_base_release()
593 config_item_put(item
);
596 static struct configfs_group_operations target_fabric_np_group_ops
= {
597 .make_group
= &target_fabric_make_np
,
598 .drop_item
= &target_fabric_drop_np
,
601 TF_CIT_SETUP(tpg_np
, NULL
, &target_fabric_np_group_ops
, NULL
);
603 /* End of tfc_tpg_np_cit */
605 /* Start of tfc_tpg_port_cit */
607 CONFIGFS_EATTR_STRUCT(target_fabric_port
, se_lun
);
608 #define TCM_PORT_ATTR(_name, _mode) \
609 static struct target_fabric_port_attribute target_fabric_port_##_name = \
610 __CONFIGFS_EATTR(_name, _mode, \
611 target_fabric_port_show_attr_##_name, \
612 target_fabric_port_store_attr_##_name);
614 #define TCM_PORT_ATTOR_RO(_name) \
615 __CONFIGFS_EATTR_RO(_name, \
616 target_fabric_port_show_attr_##_name);
621 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_gp(
625 if (!lun
|| !lun
->lun_sep
)
628 return core_alua_show_tg_pt_gp_info(lun
->lun_sep
, page
);
631 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_gp(
636 if (!lun
|| !lun
->lun_sep
)
639 return core_alua_store_tg_pt_gp_info(lun
->lun_sep
, page
, count
);
642 TCM_PORT_ATTR(alua_tg_pt_gp
, S_IRUGO
| S_IWUSR
);
647 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_offline(
651 if (!lun
|| !lun
->lun_sep
)
654 return core_alua_show_offline_bit(lun
, page
);
657 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_offline(
662 if (!lun
|| !lun
->lun_sep
)
665 return core_alua_store_offline_bit(lun
, page
, count
);
668 TCM_PORT_ATTR(alua_tg_pt_offline
, S_IRUGO
| S_IWUSR
);
673 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_status(
677 if (!lun
|| !lun
->lun_sep
)
680 return core_alua_show_secondary_status(lun
, page
);
683 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_status(
688 if (!lun
|| !lun
->lun_sep
)
691 return core_alua_store_secondary_status(lun
, page
, count
);
694 TCM_PORT_ATTR(alua_tg_pt_status
, S_IRUGO
| S_IWUSR
);
697 * alua_tg_pt_write_md
699 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_write_md(
703 if (!lun
|| !lun
->lun_sep
)
706 return core_alua_show_secondary_write_metadata(lun
, page
);
709 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_write_md(
714 if (!lun
|| !lun
->lun_sep
)
717 return core_alua_store_secondary_write_metadata(lun
, page
, count
);
720 TCM_PORT_ATTR(alua_tg_pt_write_md
, S_IRUGO
| S_IWUSR
);
723 static struct configfs_attribute
*target_fabric_port_attrs
[] = {
724 &target_fabric_port_alua_tg_pt_gp
.attr
,
725 &target_fabric_port_alua_tg_pt_offline
.attr
,
726 &target_fabric_port_alua_tg_pt_status
.attr
,
727 &target_fabric_port_alua_tg_pt_write_md
.attr
,
731 CONFIGFS_EATTR_OPS(target_fabric_port
, se_lun
, lun_group
);
733 static int target_fabric_port_link(
734 struct config_item
*lun_ci
,
735 struct config_item
*se_dev_ci
)
737 struct config_item
*tpg_ci
;
738 struct se_device
*dev
;
739 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
740 struct se_lun
, lun_group
);
741 struct se_lun
*lun_p
;
742 struct se_portal_group
*se_tpg
;
743 struct se_subsystem_dev
*se_dev
= container_of(
744 to_config_group(se_dev_ci
), struct se_subsystem_dev
,
746 struct target_fabric_configfs
*tf
;
749 tpg_ci
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
750 se_tpg
= container_of(to_config_group(tpg_ci
),
751 struct se_portal_group
, tpg_group
);
752 tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
754 if (lun
->lun_se_dev
!= NULL
) {
755 pr_err("Port Symlink already exists\n");
759 dev
= se_dev
->se_dev_ptr
;
761 pr_err("Unable to locate struct se_device pointer from"
762 " %s\n", config_item_name(se_dev_ci
));
767 lun_p
= core_dev_add_lun(se_tpg
, dev
, lun
->unpacked_lun
);
769 pr_err("core_dev_add_lun() failed\n");
770 ret
= PTR_ERR(lun_p
);
774 if (tf
->tf_ops
.fabric_post_link
) {
776 * Call the optional fabric_post_link() to allow a
777 * fabric module to setup any additional state once
778 * core_dev_add_lun() has been called..
780 tf
->tf_ops
.fabric_post_link(se_tpg
, lun
);
788 static int target_fabric_port_unlink(
789 struct config_item
*lun_ci
,
790 struct config_item
*se_dev_ci
)
792 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
793 struct se_lun
, lun_group
);
794 struct se_portal_group
*se_tpg
= lun
->lun_sep
->sep_tpg
;
795 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
797 if (tf
->tf_ops
.fabric_pre_unlink
) {
799 * Call the optional fabric_pre_unlink() to allow a
800 * fabric module to release any additional stat before
801 * core_dev_del_lun() is called.
803 tf
->tf_ops
.fabric_pre_unlink(se_tpg
, lun
);
806 core_dev_del_lun(se_tpg
, lun
->unpacked_lun
);
810 static struct configfs_item_operations target_fabric_port_item_ops
= {
811 .show_attribute
= target_fabric_port_attr_show
,
812 .store_attribute
= target_fabric_port_attr_store
,
813 .allow_link
= target_fabric_port_link
,
814 .drop_link
= target_fabric_port_unlink
,
817 TF_CIT_SETUP(tpg_port
, &target_fabric_port_item_ops
, NULL
, target_fabric_port_attrs
);
819 /* End of tfc_tpg_port_cit */
821 /* Start of tfc_tpg_port_stat_cit */
823 static struct config_group
*target_core_port_stat_mkdir(
824 struct config_group
*group
,
827 return ERR_PTR(-ENOSYS
);
830 static void target_core_port_stat_rmdir(
831 struct config_group
*group
,
832 struct config_item
*item
)
837 static struct configfs_group_operations target_fabric_port_stat_group_ops
= {
838 .make_group
= target_core_port_stat_mkdir
,
839 .drop_item
= target_core_port_stat_rmdir
,
842 TF_CIT_SETUP(tpg_port_stat
, NULL
, &target_fabric_port_stat_group_ops
, NULL
);
844 /* End of tfc_tpg_port_stat_cit */
846 /* Start of tfc_tpg_lun_cit */
848 static struct config_group
*target_fabric_make_lun(
849 struct config_group
*group
,
853 struct se_portal_group
*se_tpg
= container_of(group
,
854 struct se_portal_group
, tpg_lun_group
);
855 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
856 struct config_group
*lun_cg
= NULL
, *port_stat_grp
= NULL
;
857 unsigned long unpacked_lun
;
860 if (strstr(name
, "lun_") != name
) {
861 pr_err("Unable to locate \'_\" in"
862 " \"lun_$LUN_NUMBER\"\n");
863 return ERR_PTR(-EINVAL
);
865 if (strict_strtoul(name
+ 4, 0, &unpacked_lun
) || unpacked_lun
> UINT_MAX
)
866 return ERR_PTR(-EINVAL
);
868 lun
= core_get_lun_from_tpg(se_tpg
, unpacked_lun
);
870 return ERR_PTR(-EINVAL
);
872 lun_cg
= &lun
->lun_group
;
873 lun_cg
->default_groups
= kzalloc(sizeof(struct config_group
) * 2,
875 if (!lun_cg
->default_groups
) {
876 pr_err("Unable to allocate lun_cg->default_groups\n");
877 return ERR_PTR(-ENOMEM
);
880 config_group_init_type_name(&lun
->lun_group
, name
,
881 &TF_CIT_TMPL(tf
)->tfc_tpg_port_cit
);
882 config_group_init_type_name(&lun
->port_stat_grps
.stat_group
,
883 "statistics", &TF_CIT_TMPL(tf
)->tfc_tpg_port_stat_cit
);
884 lun_cg
->default_groups
[0] = &lun
->port_stat_grps
.stat_group
;
885 lun_cg
->default_groups
[1] = NULL
;
887 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
888 port_stat_grp
->default_groups
= kzalloc(sizeof(struct config_group
) * 3,
890 if (!port_stat_grp
->default_groups
) {
891 pr_err("Unable to allocate port_stat_grp->default_groups\n");
895 target_stat_setup_port_default_groups(lun
);
897 return &lun
->lun_group
;
900 kfree(lun_cg
->default_groups
);
901 return ERR_PTR(errno
);
904 static void target_fabric_drop_lun(
905 struct config_group
*group
,
906 struct config_item
*item
)
908 struct se_lun
*lun
= container_of(to_config_group(item
),
909 struct se_lun
, lun_group
);
910 struct config_item
*df_item
;
911 struct config_group
*lun_cg
, *port_stat_grp
;
914 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
915 for (i
= 0; port_stat_grp
->default_groups
[i
]; i
++) {
916 df_item
= &port_stat_grp
->default_groups
[i
]->cg_item
;
917 port_stat_grp
->default_groups
[i
] = NULL
;
918 config_item_put(df_item
);
920 kfree(port_stat_grp
->default_groups
);
922 lun_cg
= &lun
->lun_group
;
923 for (i
= 0; lun_cg
->default_groups
[i
]; i
++) {
924 df_item
= &lun_cg
->default_groups
[i
]->cg_item
;
925 lun_cg
->default_groups
[i
] = NULL
;
926 config_item_put(df_item
);
928 kfree(lun_cg
->default_groups
);
930 config_item_put(item
);
933 static struct configfs_group_operations target_fabric_lun_group_ops
= {
934 .make_group
= &target_fabric_make_lun
,
935 .drop_item
= &target_fabric_drop_lun
,
938 TF_CIT_SETUP(tpg_lun
, NULL
, &target_fabric_lun_group_ops
, NULL
);
940 /* End of tfc_tpg_lun_cit */
942 /* Start of tfc_tpg_attrib_cit */
944 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib
, se_portal_group
, tpg_attrib_group
);
946 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops
= {
947 .show_attribute
= target_fabric_tpg_attrib_attr_show
,
948 .store_attribute
= target_fabric_tpg_attrib_attr_store
,
951 TF_CIT_SETUP(tpg_attrib
, &target_fabric_tpg_attrib_item_ops
, NULL
, NULL
);
953 /* End of tfc_tpg_attrib_cit */
955 /* Start of tfc_tpg_param_cit */
957 CONFIGFS_EATTR_OPS(target_fabric_tpg_param
, se_portal_group
, tpg_param_group
);
959 static struct configfs_item_operations target_fabric_tpg_param_item_ops
= {
960 .show_attribute
= target_fabric_tpg_param_attr_show
,
961 .store_attribute
= target_fabric_tpg_param_attr_store
,
964 TF_CIT_SETUP(tpg_param
, &target_fabric_tpg_param_item_ops
, NULL
, NULL
);
966 /* End of tfc_tpg_param_cit */
968 /* Start of tfc_tpg_base_cit */
970 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
972 CONFIGFS_EATTR_OPS(target_fabric_tpg
, se_portal_group
, tpg_group
);
974 static void target_fabric_tpg_release(struct config_item
*item
)
976 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
977 struct se_portal_group
, tpg_group
);
978 struct se_wwn
*wwn
= se_tpg
->se_tpg_wwn
;
979 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
981 tf
->tf_ops
.fabric_drop_tpg(se_tpg
);
984 static struct configfs_item_operations target_fabric_tpg_base_item_ops
= {
985 .release
= target_fabric_tpg_release
,
986 .show_attribute
= target_fabric_tpg_attr_show
,
987 .store_attribute
= target_fabric_tpg_attr_store
,
990 TF_CIT_SETUP(tpg_base
, &target_fabric_tpg_base_item_ops
, NULL
, NULL
);
992 /* End of tfc_tpg_base_cit */
994 /* Start of tfc_tpg_cit */
996 static struct config_group
*target_fabric_make_tpg(
997 struct config_group
*group
,
1000 struct se_wwn
*wwn
= container_of(group
, struct se_wwn
, wwn_group
);
1001 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
1002 struct se_portal_group
*se_tpg
;
1004 if (!tf
->tf_ops
.fabric_make_tpg
) {
1005 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
1006 return ERR_PTR(-ENOSYS
);
1009 se_tpg
= tf
->tf_ops
.fabric_make_tpg(wwn
, group
, name
);
1010 if (!se_tpg
|| IS_ERR(se_tpg
))
1011 return ERR_PTR(-EINVAL
);
1013 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1015 se_tpg
->tpg_group
.default_groups
= se_tpg
->tpg_default_groups
;
1016 se_tpg
->tpg_group
.default_groups
[0] = &se_tpg
->tpg_lun_group
;
1017 se_tpg
->tpg_group
.default_groups
[1] = &se_tpg
->tpg_np_group
;
1018 se_tpg
->tpg_group
.default_groups
[2] = &se_tpg
->tpg_acl_group
;
1019 se_tpg
->tpg_group
.default_groups
[3] = &se_tpg
->tpg_attrib_group
;
1020 se_tpg
->tpg_group
.default_groups
[4] = &se_tpg
->tpg_param_group
;
1021 se_tpg
->tpg_group
.default_groups
[5] = NULL
;
1023 config_group_init_type_name(&se_tpg
->tpg_group
, name
,
1024 &TF_CIT_TMPL(tf
)->tfc_tpg_base_cit
);
1025 config_group_init_type_name(&se_tpg
->tpg_lun_group
, "lun",
1026 &TF_CIT_TMPL(tf
)->tfc_tpg_lun_cit
);
1027 config_group_init_type_name(&se_tpg
->tpg_np_group
, "np",
1028 &TF_CIT_TMPL(tf
)->tfc_tpg_np_cit
);
1029 config_group_init_type_name(&se_tpg
->tpg_acl_group
, "acls",
1030 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_cit
);
1031 config_group_init_type_name(&se_tpg
->tpg_attrib_group
, "attrib",
1032 &TF_CIT_TMPL(tf
)->tfc_tpg_attrib_cit
);
1033 config_group_init_type_name(&se_tpg
->tpg_param_group
, "param",
1034 &TF_CIT_TMPL(tf
)->tfc_tpg_param_cit
);
1036 return &se_tpg
->tpg_group
;
1039 static void target_fabric_drop_tpg(
1040 struct config_group
*group
,
1041 struct config_item
*item
)
1043 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
1044 struct se_portal_group
, tpg_group
);
1045 struct config_group
*tpg_cg
= &se_tpg
->tpg_group
;
1046 struct config_item
*df_item
;
1049 * Release default groups, but do not release tpg_cg->default_groups
1050 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1052 for (i
= 0; tpg_cg
->default_groups
[i
]; i
++) {
1053 df_item
= &tpg_cg
->default_groups
[i
]->cg_item
;
1054 tpg_cg
->default_groups
[i
] = NULL
;
1055 config_item_put(df_item
);
1058 config_item_put(item
);
1061 static void target_fabric_release_wwn(struct config_item
*item
)
1063 struct se_wwn
*wwn
= container_of(to_config_group(item
),
1064 struct se_wwn
, wwn_group
);
1065 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
1067 tf
->tf_ops
.fabric_drop_wwn(wwn
);
1070 static struct configfs_item_operations target_fabric_tpg_item_ops
= {
1071 .release
= target_fabric_release_wwn
,
1074 static struct configfs_group_operations target_fabric_tpg_group_ops
= {
1075 .make_group
= target_fabric_make_tpg
,
1076 .drop_item
= target_fabric_drop_tpg
,
1079 TF_CIT_SETUP(tpg
, &target_fabric_tpg_item_ops
, &target_fabric_tpg_group_ops
,
1082 /* End of tfc_tpg_cit */
1084 /* Start of tfc_wwn_fabric_stats_cit */
1086 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1087 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1089 TF_CIT_SETUP(wwn_fabric_stats
, NULL
, NULL
, NULL
);
1091 /* End of tfc_wwn_fabric_stats_cit */
1093 /* Start of tfc_wwn_cit */
1095 static struct config_group
*target_fabric_make_wwn(
1096 struct config_group
*group
,
1099 struct target_fabric_configfs
*tf
= container_of(group
,
1100 struct target_fabric_configfs
, tf_group
);
1103 if (!tf
->tf_ops
.fabric_make_wwn
) {
1104 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1105 return ERR_PTR(-ENOSYS
);
1108 wwn
= tf
->tf_ops
.fabric_make_wwn(tf
, group
, name
);
1109 if (!wwn
|| IS_ERR(wwn
))
1110 return ERR_PTR(-EINVAL
);
1114 * Setup default groups from pre-allocated wwn->wwn_default_groups
1116 wwn
->wwn_group
.default_groups
= wwn
->wwn_default_groups
;
1117 wwn
->wwn_group
.default_groups
[0] = &wwn
->fabric_stat_group
;
1118 wwn
->wwn_group
.default_groups
[1] = NULL
;
1120 config_group_init_type_name(&wwn
->wwn_group
, name
,
1121 &TF_CIT_TMPL(tf
)->tfc_tpg_cit
);
1122 config_group_init_type_name(&wwn
->fabric_stat_group
, "fabric_statistics",
1123 &TF_CIT_TMPL(tf
)->tfc_wwn_fabric_stats_cit
);
1125 return &wwn
->wwn_group
;
1128 static void target_fabric_drop_wwn(
1129 struct config_group
*group
,
1130 struct config_item
*item
)
1132 struct se_wwn
*wwn
= container_of(to_config_group(item
),
1133 struct se_wwn
, wwn_group
);
1134 struct config_item
*df_item
;
1135 struct config_group
*cg
= &wwn
->wwn_group
;
1138 for (i
= 0; cg
->default_groups
[i
]; i
++) {
1139 df_item
= &cg
->default_groups
[i
]->cg_item
;
1140 cg
->default_groups
[i
] = NULL
;
1141 config_item_put(df_item
);
1144 config_item_put(item
);
1147 static struct configfs_group_operations target_fabric_wwn_group_ops
= {
1148 .make_group
= target_fabric_make_wwn
,
1149 .drop_item
= target_fabric_drop_wwn
,
1152 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1154 CONFIGFS_EATTR_OPS(target_fabric_wwn
, target_fabric_configfs
, tf_group
);
1156 static struct configfs_item_operations target_fabric_wwn_item_ops
= {
1157 .show_attribute
= target_fabric_wwn_attr_show
,
1158 .store_attribute
= target_fabric_wwn_attr_store
,
1161 TF_CIT_SETUP(wwn
, &target_fabric_wwn_item_ops
, &target_fabric_wwn_group_ops
, NULL
);
1163 /* End of tfc_wwn_cit */
1165 /* Start of tfc_discovery_cit */
1167 CONFIGFS_EATTR_OPS(target_fabric_discovery
, target_fabric_configfs
,
1170 static struct configfs_item_operations target_fabric_discovery_item_ops
= {
1171 .show_attribute
= target_fabric_discovery_attr_show
,
1172 .store_attribute
= target_fabric_discovery_attr_store
,
1175 TF_CIT_SETUP(discovery
, &target_fabric_discovery_item_ops
, NULL
, NULL
);
1177 /* End of tfc_discovery_cit */
1179 int target_fabric_setup_cits(struct target_fabric_configfs
*tf
)
1181 target_fabric_setup_discovery_cit(tf
);
1182 target_fabric_setup_wwn_cit(tf
);
1183 target_fabric_setup_wwn_fabric_stats_cit(tf
);
1184 target_fabric_setup_tpg_cit(tf
);
1185 target_fabric_setup_tpg_base_cit(tf
);
1186 target_fabric_setup_tpg_port_cit(tf
);
1187 target_fabric_setup_tpg_port_stat_cit(tf
);
1188 target_fabric_setup_tpg_lun_cit(tf
);
1189 target_fabric_setup_tpg_np_cit(tf
);
1190 target_fabric_setup_tpg_np_base_cit(tf
);
1191 target_fabric_setup_tpg_attrib_cit(tf
);
1192 target_fabric_setup_tpg_param_cit(tf
);
1193 target_fabric_setup_tpg_nacl_cit(tf
);
1194 target_fabric_setup_tpg_nacl_base_cit(tf
);
1195 target_fabric_setup_tpg_nacl_attrib_cit(tf
);
1196 target_fabric_setup_tpg_nacl_auth_cit(tf
);
1197 target_fabric_setup_tpg_nacl_param_cit(tf
);
1198 target_fabric_setup_tpg_nacl_stat_cit(tf
);
1199 target_fabric_setup_tpg_mappedlun_cit(tf
);
1200 target_fabric_setup_tpg_mappedlun_stat_cit(tf
);