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>
39 #include "target_core_internal.h"
40 #include "target_core_alua.h"
41 #include "target_core_pr.h"
43 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
44 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
46 struct config_item_type *cit = &tf->tf_##_name##_cit; \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
51 cit->ct_owner = tf->tf_ops->module; \
52 pr_debug("Setup generic %s\n", __stringify(_name)); \
55 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
58 struct config_item_type *cit = &tf->tf_##_name##_cit; \
59 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
61 cit->ct_item_ops = _item_ops; \
62 cit->ct_group_ops = _group_ops; \
63 cit->ct_attrs = attrs; \
64 cit->ct_owner = tf->tf_ops->module; \
65 pr_debug("Setup generic %s\n", __stringify(_name)); \
68 /* Start of tfc_tpg_mappedlun_cit */
70 static int target_fabric_mappedlun_link(
71 struct config_item
*lun_acl_ci
,
72 struct config_item
*lun_ci
)
74 struct se_dev_entry
*deve
;
75 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
76 struct se_lun
, lun_group
);
77 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
78 struct se_lun_acl
, se_lun_group
);
79 struct se_portal_group
*se_tpg
;
80 struct config_item
*nacl_ci
, *tpg_ci
, *tpg_ci_s
, *wwn_ci
, *wwn_ci_s
;
83 if (lun
->lun_link_magic
!= SE_LUN_LINK_MAGIC
) {
84 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
85 " %p to struct lun: %p\n", lun_ci
, lun
);
89 * Ensure that the source port exists
91 if (!lun
->lun_se_dev
) {
92 pr_err("Source se_lun->lun_se_dev does not exist\n");
95 se_tpg
= lun
->lun_tpg
;
97 nacl_ci
= &lun_acl_ci
->ci_parent
->ci_group
->cg_item
;
98 tpg_ci
= &nacl_ci
->ci_group
->cg_item
;
99 wwn_ci
= &tpg_ci
->ci_group
->cg_item
;
100 tpg_ci_s
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
101 wwn_ci_s
= &tpg_ci_s
->ci_group
->cg_item
;
103 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
105 if (strcmp(config_item_name(wwn_ci
), config_item_name(wwn_ci_s
))) {
106 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
107 config_item_name(wwn_ci
));
110 if (strcmp(config_item_name(tpg_ci
), config_item_name(tpg_ci_s
))) {
111 pr_err("Illegal Initiator ACL Symlink outside of %s"
112 " TPGT: %s\n", config_item_name(wwn_ci
),
113 config_item_name(tpg_ci
));
117 * If this struct se_node_acl was dynamically generated with
118 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
119 * which be will write protected (READ-ONLY) when
120 * tpg_1/attrib/demo_mode_write_protect=1
123 deve
= target_nacl_find_deve(lacl
->se_lun_nacl
, lacl
->mapped_lun
);
125 lun_access
= deve
->lun_flags
;
128 (se_tpg
->se_tpg_tfo
->tpg_check_prod_mode_write_protect(
129 se_tpg
)) ? TRANSPORT_LUNFLAGS_READ_ONLY
:
130 TRANSPORT_LUNFLAGS_READ_WRITE
;
133 * Determine the actual mapped LUN value user wants..
135 * This value is what the SCSI Initiator actually sees the
136 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
138 return core_dev_add_initiator_node_lun_acl(se_tpg
, lacl
, lun
, lun_access
);
141 static int target_fabric_mappedlun_unlink(
142 struct config_item
*lun_acl_ci
,
143 struct config_item
*lun_ci
)
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
);
150 return core_dev_del_initiator_node_lun_acl(lun
, lacl
);
153 static struct se_lun_acl
*item_to_lun_acl(struct config_item
*item
)
155 return container_of(to_config_group(item
), struct se_lun_acl
,
159 static ssize_t
target_fabric_mappedlun_write_protect_show(
160 struct config_item
*item
, char *page
)
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
;
168 deve
= target_nacl_find_deve(se_nacl
, lacl
->mapped_lun
);
170 len
= sprintf(page
, "%d\n",
171 (deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
) ? 1 : 0);
178 static ssize_t
target_fabric_mappedlun_write_protect_store(
179 struct config_item
*item
, const char *page
, size_t count
)
181 struct se_lun_acl
*lacl
= item_to_lun_acl(item
);
182 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
183 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
187 ret
= kstrtoul(page
, 0, &op
);
191 if ((op
!= 1) && (op
!= 0))
194 core_update_device_list_access(lacl
->mapped_lun
, (op
) ?
195 TRANSPORT_LUNFLAGS_READ_ONLY
:
196 TRANSPORT_LUNFLAGS_READ_WRITE
,
199 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
200 " Mapped LUN: %llu Write Protect bit to %s\n",
201 se_tpg
->se_tpg_tfo
->get_fabric_name(),
202 se_nacl
->initiatorname
, lacl
->mapped_lun
, (op
) ? "ON" : "OFF");
208 CONFIGFS_ATTR(target_fabric_mappedlun_
, write_protect
);
210 static struct configfs_attribute
*target_fabric_mappedlun_attrs
[] = {
211 &target_fabric_mappedlun_attr_write_protect
,
215 static void target_fabric_mappedlun_release(struct config_item
*item
)
217 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
218 struct se_lun_acl
, se_lun_group
);
219 struct se_portal_group
*se_tpg
= lacl
->se_lun_nacl
->se_tpg
;
221 core_dev_free_initiator_node_lun_acl(se_tpg
, lacl
);
224 static struct configfs_item_operations target_fabric_mappedlun_item_ops
= {
225 .release
= target_fabric_mappedlun_release
,
226 .allow_link
= target_fabric_mappedlun_link
,
227 .drop_link
= target_fabric_mappedlun_unlink
,
230 TF_CIT_SETUP(tpg_mappedlun
, &target_fabric_mappedlun_item_ops
, NULL
,
231 target_fabric_mappedlun_attrs
);
233 /* End of tfc_tpg_mappedlun_cit */
235 /* Start of tfc_tpg_mappedlun_port_cit */
237 static struct config_group
*target_core_mappedlun_stat_mkdir(
238 struct config_group
*group
,
241 return ERR_PTR(-ENOSYS
);
244 static void target_core_mappedlun_stat_rmdir(
245 struct config_group
*group
,
246 struct config_item
*item
)
251 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops
= {
252 .make_group
= target_core_mappedlun_stat_mkdir
,
253 .drop_item
= target_core_mappedlun_stat_rmdir
,
256 TF_CIT_SETUP(tpg_mappedlun_stat
, NULL
, &target_fabric_mappedlun_stat_group_ops
,
259 /* End of tfc_tpg_mappedlun_port_cit */
261 TF_CIT_SETUP_DRV(tpg_nacl_attrib
, NULL
, NULL
);
262 TF_CIT_SETUP_DRV(tpg_nacl_auth
, NULL
, NULL
);
263 TF_CIT_SETUP_DRV(tpg_nacl_param
, NULL
, NULL
);
265 /* Start of tfc_tpg_nacl_base_cit */
267 static struct config_group
*target_fabric_make_mappedlun(
268 struct config_group
*group
,
271 struct se_node_acl
*se_nacl
= container_of(group
,
272 struct se_node_acl
, acl_group
);
273 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
274 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
275 struct se_lun_acl
*lacl
= NULL
;
276 struct config_item
*acl_ci
;
277 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
279 unsigned long long mapped_lun
;
282 acl_ci
= &group
->cg_item
;
284 pr_err("Unable to locatel acl_ci\n");
288 buf
= kzalloc(strlen(name
) + 1, GFP_KERNEL
);
290 pr_err("Unable to allocate memory for name buf\n");
291 return ERR_PTR(-ENOMEM
);
293 snprintf(buf
, strlen(name
) + 1, "%s", name
);
295 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
297 if (strstr(buf
, "lun_") != buf
) {
298 pr_err("Unable to locate \"lun_\" from buf: %s"
299 " name: %s\n", buf
, name
);
304 * Determine the Mapped LUN value. This is what the SCSI Initiator
305 * Port will actually see.
307 ret
= kstrtoull(buf
+ 4, 0, &mapped_lun
);
311 lacl
= core_dev_init_initiator_node_lun_acl(se_tpg
, se_nacl
,
318 lacl_cg
= &lacl
->se_lun_group
;
319 lacl_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
321 if (!lacl_cg
->default_groups
) {
322 pr_err("Unable to allocate lacl_cg->default_groups\n");
327 config_group_init_type_name(&lacl
->se_lun_group
, name
,
328 &tf
->tf_tpg_mappedlun_cit
);
329 config_group_init_type_name(&lacl
->ml_stat_grps
.stat_group
,
330 "statistics", &tf
->tf_tpg_mappedlun_stat_cit
);
331 lacl_cg
->default_groups
[0] = &lacl
->ml_stat_grps
.stat_group
;
332 lacl_cg
->default_groups
[1] = NULL
;
334 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
335 ml_stat_grp
->default_groups
= kmalloc(sizeof(struct config_group
*) * 3,
337 if (!ml_stat_grp
->default_groups
) {
338 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
342 target_stat_setup_mappedlun_default_groups(lacl
);
345 return &lacl
->se_lun_group
;
348 kfree(lacl_cg
->default_groups
);
354 static void target_fabric_drop_mappedlun(
355 struct config_group
*group
,
356 struct config_item
*item
)
358 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
359 struct se_lun_acl
, se_lun_group
);
360 struct config_item
*df_item
;
361 struct config_group
*lacl_cg
= NULL
, *ml_stat_grp
= NULL
;
364 ml_stat_grp
= &lacl
->ml_stat_grps
.stat_group
;
365 for (i
= 0; ml_stat_grp
->default_groups
[i
]; i
++) {
366 df_item
= &ml_stat_grp
->default_groups
[i
]->cg_item
;
367 ml_stat_grp
->default_groups
[i
] = NULL
;
368 config_item_put(df_item
);
370 kfree(ml_stat_grp
->default_groups
);
372 lacl_cg
= &lacl
->se_lun_group
;
373 for (i
= 0; lacl_cg
->default_groups
[i
]; i
++) {
374 df_item
= &lacl_cg
->default_groups
[i
]->cg_item
;
375 lacl_cg
->default_groups
[i
] = NULL
;
376 config_item_put(df_item
);
378 kfree(lacl_cg
->default_groups
);
380 config_item_put(item
);
383 static void target_fabric_nacl_base_release(struct config_item
*item
)
385 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
386 struct se_node_acl
, acl_group
);
387 struct target_fabric_configfs
*tf
= se_nacl
->se_tpg
->se_tpg_wwn
->wwn_tf
;
389 if (tf
->tf_ops
->fabric_cleanup_nodeacl
)
390 tf
->tf_ops
->fabric_cleanup_nodeacl(se_nacl
);
391 core_tpg_del_initiator_node_acl(se_nacl
);
394 static struct configfs_item_operations target_fabric_nacl_base_item_ops
= {
395 .release
= target_fabric_nacl_base_release
,
398 static struct configfs_group_operations target_fabric_nacl_base_group_ops
= {
399 .make_group
= target_fabric_make_mappedlun
,
400 .drop_item
= target_fabric_drop_mappedlun
,
403 TF_CIT_SETUP_DRV(tpg_nacl_base
, &target_fabric_nacl_base_item_ops
,
404 &target_fabric_nacl_base_group_ops
);
406 /* End of tfc_tpg_nacl_base_cit */
408 /* Start of tfc_node_fabric_stats_cit */
410 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
411 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
413 TF_CIT_SETUP(tpg_nacl_stat
, NULL
, NULL
, NULL
);
415 /* End of tfc_wwn_fabric_stats_cit */
417 /* Start of tfc_tpg_nacl_cit */
419 static struct config_group
*target_fabric_make_nodeacl(
420 struct config_group
*group
,
423 struct se_portal_group
*se_tpg
= container_of(group
,
424 struct se_portal_group
, tpg_acl_group
);
425 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
426 struct se_node_acl
*se_nacl
;
427 struct config_group
*nacl_cg
;
429 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, name
);
431 return ERR_CAST(se_nacl
);
433 if (tf
->tf_ops
->fabric_init_nodeacl
) {
434 int ret
= tf
->tf_ops
->fabric_init_nodeacl(se_nacl
, name
);
436 core_tpg_del_initiator_node_acl(se_nacl
);
441 nacl_cg
= &se_nacl
->acl_group
;
442 nacl_cg
->default_groups
= se_nacl
->acl_default_groups
;
443 nacl_cg
->default_groups
[0] = &se_nacl
->acl_attrib_group
;
444 nacl_cg
->default_groups
[1] = &se_nacl
->acl_auth_group
;
445 nacl_cg
->default_groups
[2] = &se_nacl
->acl_param_group
;
446 nacl_cg
->default_groups
[3] = &se_nacl
->acl_fabric_stat_group
;
447 nacl_cg
->default_groups
[4] = NULL
;
449 config_group_init_type_name(&se_nacl
->acl_group
, name
,
450 &tf
->tf_tpg_nacl_base_cit
);
451 config_group_init_type_name(&se_nacl
->acl_attrib_group
, "attrib",
452 &tf
->tf_tpg_nacl_attrib_cit
);
453 config_group_init_type_name(&se_nacl
->acl_auth_group
, "auth",
454 &tf
->tf_tpg_nacl_auth_cit
);
455 config_group_init_type_name(&se_nacl
->acl_param_group
, "param",
456 &tf
->tf_tpg_nacl_param_cit
);
457 config_group_init_type_name(&se_nacl
->acl_fabric_stat_group
,
458 "fabric_statistics", &tf
->tf_tpg_nacl_stat_cit
);
460 return &se_nacl
->acl_group
;
463 static void target_fabric_drop_nodeacl(
464 struct config_group
*group
,
465 struct config_item
*item
)
467 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
468 struct se_node_acl
, acl_group
);
469 struct config_item
*df_item
;
470 struct config_group
*nacl_cg
;
473 nacl_cg
= &se_nacl
->acl_group
;
474 for (i
= 0; nacl_cg
->default_groups
[i
]; i
++) {
475 df_item
= &nacl_cg
->default_groups
[i
]->cg_item
;
476 nacl_cg
->default_groups
[i
] = NULL
;
477 config_item_put(df_item
);
480 * struct se_node_acl free is done in target_fabric_nacl_base_release()
482 config_item_put(item
);
485 static struct configfs_group_operations target_fabric_nacl_group_ops
= {
486 .make_group
= target_fabric_make_nodeacl
,
487 .drop_item
= target_fabric_drop_nodeacl
,
490 TF_CIT_SETUP(tpg_nacl
, NULL
, &target_fabric_nacl_group_ops
, NULL
);
492 /* End of tfc_tpg_nacl_cit */
494 /* Start of tfc_tpg_np_base_cit */
496 static void target_fabric_np_base_release(struct config_item
*item
)
498 struct se_tpg_np
*se_tpg_np
= container_of(to_config_group(item
),
499 struct se_tpg_np
, tpg_np_group
);
500 struct se_portal_group
*se_tpg
= se_tpg_np
->tpg_np_parent
;
501 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
503 tf
->tf_ops
->fabric_drop_np(se_tpg_np
);
506 static struct configfs_item_operations target_fabric_np_base_item_ops
= {
507 .release
= target_fabric_np_base_release
,
510 TF_CIT_SETUP_DRV(tpg_np_base
, &target_fabric_np_base_item_ops
, NULL
);
512 /* End of tfc_tpg_np_base_cit */
514 /* Start of tfc_tpg_np_cit */
516 static struct config_group
*target_fabric_make_np(
517 struct config_group
*group
,
520 struct se_portal_group
*se_tpg
= container_of(group
,
521 struct se_portal_group
, tpg_np_group
);
522 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
523 struct se_tpg_np
*se_tpg_np
;
525 if (!tf
->tf_ops
->fabric_make_np
) {
526 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
527 return ERR_PTR(-ENOSYS
);
530 se_tpg_np
= tf
->tf_ops
->fabric_make_np(se_tpg
, group
, name
);
531 if (!se_tpg_np
|| IS_ERR(se_tpg_np
))
532 return ERR_PTR(-EINVAL
);
534 se_tpg_np
->tpg_np_parent
= se_tpg
;
535 config_group_init_type_name(&se_tpg_np
->tpg_np_group
, name
,
536 &tf
->tf_tpg_np_base_cit
);
538 return &se_tpg_np
->tpg_np_group
;
541 static void target_fabric_drop_np(
542 struct config_group
*group
,
543 struct config_item
*item
)
546 * struct se_tpg_np is released via target_fabric_np_base_release()
548 config_item_put(item
);
551 static struct configfs_group_operations target_fabric_np_group_ops
= {
552 .make_group
= &target_fabric_make_np
,
553 .drop_item
= &target_fabric_drop_np
,
556 TF_CIT_SETUP(tpg_np
, NULL
, &target_fabric_np_group_ops
, NULL
);
558 /* End of tfc_tpg_np_cit */
560 /* Start of tfc_tpg_port_cit */
562 static struct se_lun
*item_to_lun(struct config_item
*item
)
564 return container_of(to_config_group(item
), struct se_lun
,
568 static ssize_t
target_fabric_port_alua_tg_pt_gp_show(struct config_item
*item
,
571 struct se_lun
*lun
= item_to_lun(item
);
573 if (!lun
|| !lun
->lun_se_dev
)
576 return core_alua_show_tg_pt_gp_info(lun
, page
);
579 static ssize_t
target_fabric_port_alua_tg_pt_gp_store(struct config_item
*item
,
580 const char *page
, size_t count
)
582 struct se_lun
*lun
= item_to_lun(item
);
584 if (!lun
|| !lun
->lun_se_dev
)
587 return core_alua_store_tg_pt_gp_info(lun
, page
, count
);
590 static ssize_t
target_fabric_port_alua_tg_pt_offline_show(
591 struct config_item
*item
, char *page
)
593 struct se_lun
*lun
= item_to_lun(item
);
595 if (!lun
|| !lun
->lun_se_dev
)
598 return core_alua_show_offline_bit(lun
, page
);
601 static ssize_t
target_fabric_port_alua_tg_pt_offline_store(
602 struct config_item
*item
, const char *page
, size_t count
)
604 struct se_lun
*lun
= item_to_lun(item
);
606 if (!lun
|| !lun
->lun_se_dev
)
609 return core_alua_store_offline_bit(lun
, page
, count
);
612 static ssize_t
target_fabric_port_alua_tg_pt_status_show(
613 struct config_item
*item
, char *page
)
615 struct se_lun
*lun
= item_to_lun(item
);
617 if (!lun
|| !lun
->lun_se_dev
)
620 return core_alua_show_secondary_status(lun
, page
);
623 static ssize_t
target_fabric_port_alua_tg_pt_status_store(
624 struct config_item
*item
, const char *page
, size_t count
)
626 struct se_lun
*lun
= item_to_lun(item
);
628 if (!lun
|| !lun
->lun_se_dev
)
631 return core_alua_store_secondary_status(lun
, page
, count
);
634 static ssize_t
target_fabric_port_alua_tg_pt_write_md_show(
635 struct config_item
*item
, char *page
)
637 struct se_lun
*lun
= item_to_lun(item
);
639 if (!lun
|| !lun
->lun_se_dev
)
642 return core_alua_show_secondary_write_metadata(lun
, page
);
645 static ssize_t
target_fabric_port_alua_tg_pt_write_md_store(
646 struct config_item
*item
, const char *page
, size_t count
)
648 struct se_lun
*lun
= item_to_lun(item
);
650 if (!lun
|| !lun
->lun_se_dev
)
653 return core_alua_store_secondary_write_metadata(lun
, page
, count
);
656 CONFIGFS_ATTR(target_fabric_port_
, alua_tg_pt_gp
);
657 CONFIGFS_ATTR(target_fabric_port_
, alua_tg_pt_offline
);
658 CONFIGFS_ATTR(target_fabric_port_
, alua_tg_pt_status
);
659 CONFIGFS_ATTR(target_fabric_port_
, alua_tg_pt_write_md
);
661 static struct configfs_attribute
*target_fabric_port_attrs
[] = {
662 &target_fabric_port_attr_alua_tg_pt_gp
,
663 &target_fabric_port_attr_alua_tg_pt_offline
,
664 &target_fabric_port_attr_alua_tg_pt_status
,
665 &target_fabric_port_attr_alua_tg_pt_write_md
,
669 static int target_fabric_port_link(
670 struct config_item
*lun_ci
,
671 struct config_item
*se_dev_ci
)
673 struct config_item
*tpg_ci
;
674 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
675 struct se_lun
, lun_group
);
676 struct se_portal_group
*se_tpg
;
677 struct se_device
*dev
=
678 container_of(to_config_group(se_dev_ci
), struct se_device
, dev_group
);
679 struct target_fabric_configfs
*tf
;
682 if (dev
->dev_link_magic
!= SE_DEV_LINK_MAGIC
) {
683 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
684 " %p to struct se_device: %p\n", se_dev_ci
, dev
);
688 if (!(dev
->dev_flags
& DF_CONFIGURED
)) {
689 pr_err("se_device not configured yet, cannot port link\n");
693 tpg_ci
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
694 se_tpg
= container_of(to_config_group(tpg_ci
),
695 struct se_portal_group
, tpg_group
);
696 tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
698 if (lun
->lun_se_dev
!= NULL
) {
699 pr_err("Port Symlink already exists\n");
703 ret
= core_dev_add_lun(se_tpg
, dev
, lun
);
705 pr_err("core_dev_add_lun() failed: %d\n", ret
);
709 if (tf
->tf_ops
->fabric_post_link
) {
711 * Call the optional fabric_post_link() to allow a
712 * fabric module to setup any additional state once
713 * core_dev_add_lun() has been called..
715 tf
->tf_ops
->fabric_post_link(se_tpg
, lun
);
723 static int target_fabric_port_unlink(
724 struct config_item
*lun_ci
,
725 struct config_item
*se_dev_ci
)
727 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
728 struct se_lun
, lun_group
);
729 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
730 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
732 if (tf
->tf_ops
->fabric_pre_unlink
) {
734 * Call the optional fabric_pre_unlink() to allow a
735 * fabric module to release any additional stat before
736 * core_dev_del_lun() is called.
738 tf
->tf_ops
->fabric_pre_unlink(se_tpg
, lun
);
741 core_dev_del_lun(se_tpg
, lun
);
745 static void target_fabric_port_release(struct config_item
*item
)
747 struct se_lun
*lun
= container_of(to_config_group(item
),
748 struct se_lun
, lun_group
);
750 kfree_rcu(lun
, rcu_head
);
753 static struct configfs_item_operations target_fabric_port_item_ops
= {
754 .release
= target_fabric_port_release
,
755 .allow_link
= target_fabric_port_link
,
756 .drop_link
= target_fabric_port_unlink
,
759 TF_CIT_SETUP(tpg_port
, &target_fabric_port_item_ops
, NULL
, target_fabric_port_attrs
);
761 /* End of tfc_tpg_port_cit */
763 /* Start of tfc_tpg_port_stat_cit */
765 static struct config_group
*target_core_port_stat_mkdir(
766 struct config_group
*group
,
769 return ERR_PTR(-ENOSYS
);
772 static void target_core_port_stat_rmdir(
773 struct config_group
*group
,
774 struct config_item
*item
)
779 static struct configfs_group_operations target_fabric_port_stat_group_ops
= {
780 .make_group
= target_core_port_stat_mkdir
,
781 .drop_item
= target_core_port_stat_rmdir
,
784 TF_CIT_SETUP(tpg_port_stat
, NULL
, &target_fabric_port_stat_group_ops
, NULL
);
786 /* End of tfc_tpg_port_stat_cit */
788 /* Start of tfc_tpg_lun_cit */
790 static struct config_group
*target_fabric_make_lun(
791 struct config_group
*group
,
795 struct se_portal_group
*se_tpg
= container_of(group
,
796 struct se_portal_group
, tpg_lun_group
);
797 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
798 struct config_group
*lun_cg
= NULL
, *port_stat_grp
= NULL
;
799 unsigned long long unpacked_lun
;
802 if (strstr(name
, "lun_") != name
) {
803 pr_err("Unable to locate \'_\" in"
804 " \"lun_$LUN_NUMBER\"\n");
805 return ERR_PTR(-EINVAL
);
807 errno
= kstrtoull(name
+ 4, 0, &unpacked_lun
);
809 return ERR_PTR(errno
);
811 lun
= core_tpg_alloc_lun(se_tpg
, unpacked_lun
);
813 return ERR_CAST(lun
);
815 lun_cg
= &lun
->lun_group
;
816 lun_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
818 if (!lun_cg
->default_groups
) {
819 pr_err("Unable to allocate lun_cg->default_groups\n");
821 return ERR_PTR(-ENOMEM
);
824 config_group_init_type_name(&lun
->lun_group
, name
,
825 &tf
->tf_tpg_port_cit
);
826 config_group_init_type_name(&lun
->port_stat_grps
.stat_group
,
827 "statistics", &tf
->tf_tpg_port_stat_cit
);
828 lun_cg
->default_groups
[0] = &lun
->port_stat_grps
.stat_group
;
829 lun_cg
->default_groups
[1] = NULL
;
831 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
832 port_stat_grp
->default_groups
= kzalloc(sizeof(struct config_group
*) * 4,
834 if (!port_stat_grp
->default_groups
) {
835 pr_err("Unable to allocate port_stat_grp->default_groups\n");
836 kfree(lun_cg
->default_groups
);
838 return ERR_PTR(-ENOMEM
);
840 target_stat_setup_port_default_groups(lun
);
842 return &lun
->lun_group
;
845 static void target_fabric_drop_lun(
846 struct config_group
*group
,
847 struct config_item
*item
)
849 struct se_lun
*lun
= container_of(to_config_group(item
),
850 struct se_lun
, lun_group
);
851 struct config_item
*df_item
;
852 struct config_group
*lun_cg
, *port_stat_grp
;
855 port_stat_grp
= &lun
->port_stat_grps
.stat_group
;
856 for (i
= 0; port_stat_grp
->default_groups
[i
]; i
++) {
857 df_item
= &port_stat_grp
->default_groups
[i
]->cg_item
;
858 port_stat_grp
->default_groups
[i
] = NULL
;
859 config_item_put(df_item
);
861 kfree(port_stat_grp
->default_groups
);
863 lun_cg
= &lun
->lun_group
;
864 for (i
= 0; lun_cg
->default_groups
[i
]; i
++) {
865 df_item
= &lun_cg
->default_groups
[i
]->cg_item
;
866 lun_cg
->default_groups
[i
] = NULL
;
867 config_item_put(df_item
);
869 kfree(lun_cg
->default_groups
);
871 config_item_put(item
);
874 static struct configfs_group_operations target_fabric_lun_group_ops
= {
875 .make_group
= &target_fabric_make_lun
,
876 .drop_item
= &target_fabric_drop_lun
,
879 TF_CIT_SETUP(tpg_lun
, NULL
, &target_fabric_lun_group_ops
, NULL
);
881 /* End of tfc_tpg_lun_cit */
883 TF_CIT_SETUP_DRV(tpg_attrib
, NULL
, NULL
);
884 TF_CIT_SETUP_DRV(tpg_auth
, NULL
, NULL
);
885 TF_CIT_SETUP_DRV(tpg_param
, NULL
, NULL
);
887 /* Start of tfc_tpg_base_cit */
889 static void target_fabric_tpg_release(struct config_item
*item
)
891 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
892 struct se_portal_group
, tpg_group
);
893 struct se_wwn
*wwn
= se_tpg
->se_tpg_wwn
;
894 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
896 tf
->tf_ops
->fabric_drop_tpg(se_tpg
);
899 static struct configfs_item_operations target_fabric_tpg_base_item_ops
= {
900 .release
= target_fabric_tpg_release
,
903 TF_CIT_SETUP_DRV(tpg_base
, &target_fabric_tpg_base_item_ops
, NULL
);
905 /* End of tfc_tpg_base_cit */
907 /* Start of tfc_tpg_cit */
909 static struct config_group
*target_fabric_make_tpg(
910 struct config_group
*group
,
913 struct se_wwn
*wwn
= container_of(group
, struct se_wwn
, wwn_group
);
914 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
915 struct se_portal_group
*se_tpg
;
917 if (!tf
->tf_ops
->fabric_make_tpg
) {
918 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
919 return ERR_PTR(-ENOSYS
);
922 se_tpg
= tf
->tf_ops
->fabric_make_tpg(wwn
, group
, name
);
923 if (!se_tpg
|| IS_ERR(se_tpg
))
924 return ERR_PTR(-EINVAL
);
926 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
928 se_tpg
->tpg_group
.default_groups
= se_tpg
->tpg_default_groups
;
929 se_tpg
->tpg_group
.default_groups
[0] = &se_tpg
->tpg_lun_group
;
930 se_tpg
->tpg_group
.default_groups
[1] = &se_tpg
->tpg_np_group
;
931 se_tpg
->tpg_group
.default_groups
[2] = &se_tpg
->tpg_acl_group
;
932 se_tpg
->tpg_group
.default_groups
[3] = &se_tpg
->tpg_attrib_group
;
933 se_tpg
->tpg_group
.default_groups
[4] = &se_tpg
->tpg_auth_group
;
934 se_tpg
->tpg_group
.default_groups
[5] = &se_tpg
->tpg_param_group
;
935 se_tpg
->tpg_group
.default_groups
[6] = NULL
;
937 config_group_init_type_name(&se_tpg
->tpg_group
, name
,
938 &tf
->tf_tpg_base_cit
);
939 config_group_init_type_name(&se_tpg
->tpg_lun_group
, "lun",
940 &tf
->tf_tpg_lun_cit
);
941 config_group_init_type_name(&se_tpg
->tpg_np_group
, "np",
943 config_group_init_type_name(&se_tpg
->tpg_acl_group
, "acls",
944 &tf
->tf_tpg_nacl_cit
);
945 config_group_init_type_name(&se_tpg
->tpg_attrib_group
, "attrib",
946 &tf
->tf_tpg_attrib_cit
);
947 config_group_init_type_name(&se_tpg
->tpg_auth_group
, "auth",
948 &tf
->tf_tpg_auth_cit
);
949 config_group_init_type_name(&se_tpg
->tpg_param_group
, "param",
950 &tf
->tf_tpg_param_cit
);
952 return &se_tpg
->tpg_group
;
955 static void target_fabric_drop_tpg(
956 struct config_group
*group
,
957 struct config_item
*item
)
959 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
960 struct se_portal_group
, tpg_group
);
961 struct config_group
*tpg_cg
= &se_tpg
->tpg_group
;
962 struct config_item
*df_item
;
965 * Release default groups, but do not release tpg_cg->default_groups
966 * memory as it is statically allocated at se_tpg->tpg_default_groups.
968 for (i
= 0; tpg_cg
->default_groups
[i
]; i
++) {
969 df_item
= &tpg_cg
->default_groups
[i
]->cg_item
;
970 tpg_cg
->default_groups
[i
] = NULL
;
971 config_item_put(df_item
);
974 config_item_put(item
);
977 static void target_fabric_release_wwn(struct config_item
*item
)
979 struct se_wwn
*wwn
= container_of(to_config_group(item
),
980 struct se_wwn
, wwn_group
);
981 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
983 tf
->tf_ops
->fabric_drop_wwn(wwn
);
986 static struct configfs_item_operations target_fabric_tpg_item_ops
= {
987 .release
= target_fabric_release_wwn
,
990 static struct configfs_group_operations target_fabric_tpg_group_ops
= {
991 .make_group
= target_fabric_make_tpg
,
992 .drop_item
= target_fabric_drop_tpg
,
995 TF_CIT_SETUP(tpg
, &target_fabric_tpg_item_ops
, &target_fabric_tpg_group_ops
,
998 /* End of tfc_tpg_cit */
1000 /* Start of tfc_wwn_fabric_stats_cit */
1002 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1003 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1005 TF_CIT_SETUP(wwn_fabric_stats
, NULL
, NULL
, NULL
);
1007 /* End of tfc_wwn_fabric_stats_cit */
1009 /* Start of tfc_wwn_cit */
1011 static struct config_group
*target_fabric_make_wwn(
1012 struct config_group
*group
,
1015 struct target_fabric_configfs
*tf
= container_of(group
,
1016 struct target_fabric_configfs
, tf_group
);
1019 if (!tf
->tf_ops
->fabric_make_wwn
) {
1020 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1021 return ERR_PTR(-ENOSYS
);
1024 wwn
= tf
->tf_ops
->fabric_make_wwn(tf
, group
, name
);
1025 if (!wwn
|| IS_ERR(wwn
))
1026 return ERR_PTR(-EINVAL
);
1030 * Setup default groups from pre-allocated wwn->wwn_default_groups
1032 wwn
->wwn_group
.default_groups
= wwn
->wwn_default_groups
;
1033 wwn
->wwn_group
.default_groups
[0] = &wwn
->fabric_stat_group
;
1034 wwn
->wwn_group
.default_groups
[1] = NULL
;
1036 config_group_init_type_name(&wwn
->wwn_group
, name
, &tf
->tf_tpg_cit
);
1037 config_group_init_type_name(&wwn
->fabric_stat_group
, "fabric_statistics",
1038 &tf
->tf_wwn_fabric_stats_cit
);
1040 return &wwn
->wwn_group
;
1043 static void target_fabric_drop_wwn(
1044 struct config_group
*group
,
1045 struct config_item
*item
)
1047 struct se_wwn
*wwn
= container_of(to_config_group(item
),
1048 struct se_wwn
, wwn_group
);
1049 struct config_item
*df_item
;
1050 struct config_group
*cg
= &wwn
->wwn_group
;
1053 for (i
= 0; cg
->default_groups
[i
]; i
++) {
1054 df_item
= &cg
->default_groups
[i
]->cg_item
;
1055 cg
->default_groups
[i
] = NULL
;
1056 config_item_put(df_item
);
1059 config_item_put(item
);
1062 static struct configfs_group_operations target_fabric_wwn_group_ops
= {
1063 .make_group
= target_fabric_make_wwn
,
1064 .drop_item
= target_fabric_drop_wwn
,
1067 TF_CIT_SETUP_DRV(wwn
, NULL
, &target_fabric_wwn_group_ops
);
1068 TF_CIT_SETUP_DRV(discovery
, NULL
, NULL
);
1070 int target_fabric_setup_cits(struct target_fabric_configfs
*tf
)
1072 target_fabric_setup_discovery_cit(tf
);
1073 target_fabric_setup_wwn_cit(tf
);
1074 target_fabric_setup_wwn_fabric_stats_cit(tf
);
1075 target_fabric_setup_tpg_cit(tf
);
1076 target_fabric_setup_tpg_base_cit(tf
);
1077 target_fabric_setup_tpg_port_cit(tf
);
1078 target_fabric_setup_tpg_port_stat_cit(tf
);
1079 target_fabric_setup_tpg_lun_cit(tf
);
1080 target_fabric_setup_tpg_np_cit(tf
);
1081 target_fabric_setup_tpg_np_base_cit(tf
);
1082 target_fabric_setup_tpg_attrib_cit(tf
);
1083 target_fabric_setup_tpg_auth_cit(tf
);
1084 target_fabric_setup_tpg_param_cit(tf
);
1085 target_fabric_setup_tpg_nacl_cit(tf
);
1086 target_fabric_setup_tpg_nacl_base_cit(tf
);
1087 target_fabric_setup_tpg_nacl_attrib_cit(tf
);
1088 target_fabric_setup_tpg_nacl_auth_cit(tf
);
1089 target_fabric_setup_tpg_nacl_param_cit(tf
);
1090 target_fabric_setup_tpg_nacl_stat_cit(tf
);
1091 target_fabric_setup_tpg_mappedlun_cit(tf
);
1092 target_fabric_setup_tpg_mappedlun_stat_cit(tf
);