1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
7 * Copyright (c) 2010 Rising Tide Systems
8 * Copyright (c) 2010 Linux-iSCSI.org
10 * Copyright (c) 2010 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 <linux/version.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
30 #include <linux/namei.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/unistd.h>
35 #include <linux/string.h>
36 #include <linux/syscalls.h>
37 #include <linux/configfs.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_device.h>
41 #include <target/target_core_tpg.h>
42 #include <target/target_core_transport.h>
43 #include <target/target_core_fabric_ops.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
48 #include "target_core_alua.h"
49 #include "target_core_hba.h"
50 #include "target_core_pr.h"
52 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
53 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
55 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
56 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
58 cit->ct_item_ops = _item_ops; \
59 cit->ct_group_ops = _group_ops; \
60 cit->ct_attrs = _attrs; \
61 cit->ct_owner = tf->tf_module; \
62 printk("Setup generic %s\n", __stringify(_name)); \
65 /* Start of tfc_tpg_mappedlun_cit */
67 static int target_fabric_mappedlun_link(
68 struct config_item
*lun_acl_ci
,
69 struct config_item
*lun_ci
)
71 struct se_dev_entry
*deve
;
72 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
73 struct se_lun
, lun_group
);
74 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
75 struct se_lun_acl
, se_lun_group
);
76 struct se_portal_group
*se_tpg
;
77 struct config_item
*nacl_ci
, *tpg_ci
, *tpg_ci_s
, *wwn_ci
, *wwn_ci_s
;
78 int ret
= 0, lun_access
;
80 * Ensure that the source port exists
82 if (!(lun
->lun_sep
) || !(lun
->lun_sep
->sep_tpg
)) {
83 printk(KERN_ERR
"Source se_lun->lun_sep or lun->lun_sep->sep"
84 "_tpg does not exist\n");
87 se_tpg
= lun
->lun_sep
->sep_tpg
;
89 nacl_ci
= &lun_acl_ci
->ci_parent
->ci_group
->cg_item
;
90 tpg_ci
= &nacl_ci
->ci_group
->cg_item
;
91 wwn_ci
= &tpg_ci
->ci_group
->cg_item
;
92 tpg_ci_s
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
93 wwn_ci_s
= &tpg_ci_s
->ci_group
->cg_item
;
95 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
97 if (strcmp(config_item_name(wwn_ci
), config_item_name(wwn_ci_s
))) {
98 printk(KERN_ERR
"Illegal Initiator ACL SymLink outside of %s\n",
99 config_item_name(wwn_ci
));
102 if (strcmp(config_item_name(tpg_ci
), config_item_name(tpg_ci_s
))) {
103 printk(KERN_ERR
"Illegal Initiator ACL Symlink outside of %s"
104 " TPGT: %s\n", config_item_name(wwn_ci
),
105 config_item_name(tpg_ci
));
109 * If this struct se_node_acl was dynamically generated with
110 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
111 * which be will write protected (READ-ONLY) when
112 * tpg_1/attrib/demo_mode_write_protect=1
114 spin_lock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
115 deve
= &lacl
->se_lun_nacl
->device_list
[lacl
->mapped_lun
];
116 if (deve
->lun_flags
& TRANSPORT_LUNFLAGS_INITIATOR_ACCESS
)
117 lun_access
= deve
->lun_flags
;
120 (TPG_TFO(se_tpg
)->tpg_check_prod_mode_write_protect(
121 se_tpg
)) ? TRANSPORT_LUNFLAGS_READ_ONLY
:
122 TRANSPORT_LUNFLAGS_READ_WRITE
;
123 spin_unlock_irq(&lacl
->se_lun_nacl
->device_list_lock
);
125 * Determine the actual mapped LUN value user wants..
127 * This value is what the SCSI Initiator actually sees the
128 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
130 ret
= core_dev_add_initiator_node_lun_acl(se_tpg
, lacl
,
131 lun
->unpacked_lun
, lun_access
);
133 return (ret
< 0) ? -EINVAL
: 0;
136 static int target_fabric_mappedlun_unlink(
137 struct config_item
*lun_acl_ci
,
138 struct config_item
*lun_ci
)
141 struct se_lun_acl
*lacl
= container_of(to_config_group(lun_acl_ci
),
142 struct se_lun_acl
, se_lun_group
);
143 struct se_node_acl
*nacl
= lacl
->se_lun_nacl
;
144 struct se_dev_entry
*deve
= &nacl
->device_list
[lacl
->mapped_lun
];
145 struct se_portal_group
*se_tpg
;
147 * Determine if the underlying MappedLUN has already been released..
152 lun
= container_of(to_config_group(lun_ci
), struct se_lun
, lun_group
);
153 se_tpg
= lun
->lun_sep
->sep_tpg
;
155 core_dev_del_initiator_node_lun_acl(se_tpg
, lun
, lacl
);
159 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun
, se_lun_acl
);
160 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
161 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
162 __CONFIGFS_EATTR(_name, _mode, \
163 target_fabric_mappedlun_show_##_name, \
164 target_fabric_mappedlun_store_##_name);
166 static ssize_t
target_fabric_mappedlun_show_write_protect(
167 struct se_lun_acl
*lacl
,
170 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
171 struct se_dev_entry
*deve
;
174 spin_lock_irq(&se_nacl
->device_list_lock
);
175 deve
= &se_nacl
->device_list
[lacl
->mapped_lun
];
176 len
= sprintf(page
, "%d\n",
177 (deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
) ?
179 spin_unlock_irq(&se_nacl
->device_list_lock
);
184 static ssize_t
target_fabric_mappedlun_store_write_protect(
185 struct se_lun_acl
*lacl
,
189 struct se_node_acl
*se_nacl
= lacl
->se_lun_nacl
;
190 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
193 if (strict_strtoul(page
, 0, &op
))
196 if ((op
!= 1) && (op
!= 0))
199 core_update_device_list_access(lacl
->mapped_lun
, (op
) ?
200 TRANSPORT_LUNFLAGS_READ_ONLY
:
201 TRANSPORT_LUNFLAGS_READ_WRITE
,
204 printk(KERN_INFO
"%s_ConfigFS: Changed Initiator ACL: %s"
205 " Mapped LUN: %u Write Protect bit to %s\n",
206 TPG_TFO(se_tpg
)->get_fabric_name(),
207 lacl
->initiatorname
, lacl
->mapped_lun
, (op
) ? "ON" : "OFF");
213 TCM_MAPPEDLUN_ATTR(write_protect
, S_IRUGO
| S_IWUSR
);
215 CONFIGFS_EATTR_OPS(target_fabric_mappedlun
, se_lun_acl
, se_lun_group
);
217 static struct configfs_attribute
*target_fabric_mappedlun_attrs
[] = {
218 &target_fabric_mappedlun_write_protect
.attr
,
222 static struct configfs_item_operations target_fabric_mappedlun_item_ops
= {
223 .show_attribute
= target_fabric_mappedlun_attr_show
,
224 .store_attribute
= target_fabric_mappedlun_attr_store
,
225 .allow_link
= target_fabric_mappedlun_link
,
226 .drop_link
= target_fabric_mappedlun_unlink
,
229 TF_CIT_SETUP(tpg_mappedlun
, &target_fabric_mappedlun_item_ops
, NULL
,
230 target_fabric_mappedlun_attrs
);
232 /* End of tfc_tpg_mappedlun_cit */
234 /* Start of tfc_tpg_nacl_attrib_cit */
236 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib
, se_node_acl
, acl_attrib_group
);
238 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops
= {
239 .show_attribute
= target_fabric_nacl_attrib_attr_show
,
240 .store_attribute
= target_fabric_nacl_attrib_attr_store
,
243 TF_CIT_SETUP(tpg_nacl_attrib
, &target_fabric_nacl_attrib_item_ops
, NULL
, NULL
);
245 /* End of tfc_tpg_nacl_attrib_cit */
247 /* Start of tfc_tpg_nacl_auth_cit */
249 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth
, se_node_acl
, acl_auth_group
);
251 static struct configfs_item_operations target_fabric_nacl_auth_item_ops
= {
252 .show_attribute
= target_fabric_nacl_auth_attr_show
,
253 .store_attribute
= target_fabric_nacl_auth_attr_store
,
256 TF_CIT_SETUP(tpg_nacl_auth
, &target_fabric_nacl_auth_item_ops
, NULL
, NULL
);
258 /* End of tfc_tpg_nacl_auth_cit */
260 /* Start of tfc_tpg_nacl_param_cit */
262 CONFIGFS_EATTR_OPS(target_fabric_nacl_param
, se_node_acl
, acl_param_group
);
264 static struct configfs_item_operations target_fabric_nacl_param_item_ops
= {
265 .show_attribute
= target_fabric_nacl_param_attr_show
,
266 .store_attribute
= target_fabric_nacl_param_attr_store
,
269 TF_CIT_SETUP(tpg_nacl_param
, &target_fabric_nacl_param_item_ops
, NULL
, NULL
);
271 /* End of tfc_tpg_nacl_param_cit */
273 /* Start of tfc_tpg_nacl_base_cit */
275 CONFIGFS_EATTR_OPS(target_fabric_nacl_base
, se_node_acl
, acl_group
);
277 static struct config_group
*target_fabric_make_mappedlun(
278 struct config_group
*group
,
281 struct se_node_acl
*se_nacl
= container_of(group
,
282 struct se_node_acl
, acl_group
);
283 struct se_portal_group
*se_tpg
= se_nacl
->se_tpg
;
284 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
285 struct se_lun_acl
*lacl
;
286 struct config_item
*acl_ci
;
288 unsigned long mapped_lun
;
291 acl_ci
= &group
->cg_item
;
293 printk(KERN_ERR
"Unable to locatel acl_ci\n");
297 buf
= kzalloc(strlen(name
) + 1, GFP_KERNEL
);
299 printk(KERN_ERR
"Unable to allocate memory for name buf\n");
300 return ERR_PTR(-ENOMEM
);
302 snprintf(buf
, strlen(name
) + 1, "%s", name
);
304 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
306 if (strstr(buf
, "lun_") != buf
) {
307 printk(KERN_ERR
"Unable to locate \"lun_\" from buf: %s"
308 " name: %s\n", buf
, name
);
313 * Determine the Mapped LUN value. This is what the SCSI Initiator
314 * Port will actually see.
316 if (strict_strtoul(buf
+ 4, 0, &mapped_lun
) || mapped_lun
> UINT_MAX
) {
321 lacl
= core_dev_init_initiator_node_lun_acl(se_tpg
, mapped_lun
,
322 config_item_name(acl_ci
), &ret
);
326 config_group_init_type_name(&lacl
->se_lun_group
, name
,
327 &TF_CIT_TMPL(tf
)->tfc_tpg_mappedlun_cit
);
330 return &lacl
->se_lun_group
;
336 static void target_fabric_drop_mappedlun(
337 struct config_group
*group
,
338 struct config_item
*item
)
340 struct se_lun_acl
*lacl
= container_of(to_config_group(item
),
341 struct se_lun_acl
, se_lun_group
);
342 struct se_portal_group
*se_tpg
= lacl
->se_lun_nacl
->se_tpg
;
344 config_item_put(item
);
345 core_dev_free_initiator_node_lun_acl(se_tpg
, lacl
);
348 static struct configfs_item_operations target_fabric_nacl_base_item_ops
= {
349 .show_attribute
= target_fabric_nacl_base_attr_show
,
350 .store_attribute
= target_fabric_nacl_base_attr_store
,
353 static struct configfs_group_operations target_fabric_nacl_base_group_ops
= {
354 .make_group
= target_fabric_make_mappedlun
,
355 .drop_item
= target_fabric_drop_mappedlun
,
358 TF_CIT_SETUP(tpg_nacl_base
, &target_fabric_nacl_base_item_ops
,
359 &target_fabric_nacl_base_group_ops
, NULL
);
361 /* End of tfc_tpg_nacl_base_cit */
363 /* Start of tfc_tpg_nacl_cit */
365 static struct config_group
*target_fabric_make_nodeacl(
366 struct config_group
*group
,
369 struct se_portal_group
*se_tpg
= container_of(group
,
370 struct se_portal_group
, tpg_acl_group
);
371 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
372 struct se_node_acl
*se_nacl
;
373 struct config_group
*nacl_cg
;
375 if (!(tf
->tf_ops
.fabric_make_nodeacl
)) {
376 printk(KERN_ERR
"tf->tf_ops.fabric_make_nodeacl is NULL\n");
377 return ERR_PTR(-ENOSYS
);
380 se_nacl
= tf
->tf_ops
.fabric_make_nodeacl(se_tpg
, group
, name
);
382 return ERR_PTR(PTR_ERR(se_nacl
));
384 nacl_cg
= &se_nacl
->acl_group
;
385 nacl_cg
->default_groups
= se_nacl
->acl_default_groups
;
386 nacl_cg
->default_groups
[0] = &se_nacl
->acl_attrib_group
;
387 nacl_cg
->default_groups
[1] = &se_nacl
->acl_auth_group
;
388 nacl_cg
->default_groups
[2] = &se_nacl
->acl_param_group
;
389 nacl_cg
->default_groups
[3] = NULL
;
391 config_group_init_type_name(&se_nacl
->acl_group
, name
,
392 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_base_cit
);
393 config_group_init_type_name(&se_nacl
->acl_attrib_group
, "attrib",
394 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_attrib_cit
);
395 config_group_init_type_name(&se_nacl
->acl_auth_group
, "auth",
396 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_auth_cit
);
397 config_group_init_type_name(&se_nacl
->acl_param_group
, "param",
398 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_param_cit
);
400 return &se_nacl
->acl_group
;
403 static void target_fabric_drop_nodeacl(
404 struct config_group
*group
,
405 struct config_item
*item
)
407 struct se_portal_group
*se_tpg
= container_of(group
,
408 struct se_portal_group
, tpg_acl_group
);
409 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
410 struct se_node_acl
*se_nacl
= container_of(to_config_group(item
),
411 struct se_node_acl
, acl_group
);
412 struct config_item
*df_item
;
413 struct config_group
*nacl_cg
;
416 nacl_cg
= &se_nacl
->acl_group
;
417 for (i
= 0; nacl_cg
->default_groups
[i
]; i
++) {
418 df_item
= &nacl_cg
->default_groups
[i
]->cg_item
;
419 nacl_cg
->default_groups
[i
] = NULL
;
420 config_item_put(df_item
);
423 config_item_put(item
);
424 tf
->tf_ops
.fabric_drop_nodeacl(se_nacl
);
427 static struct configfs_group_operations target_fabric_nacl_group_ops
= {
428 .make_group
= target_fabric_make_nodeacl
,
429 .drop_item
= target_fabric_drop_nodeacl
,
432 TF_CIT_SETUP(tpg_nacl
, NULL
, &target_fabric_nacl_group_ops
, NULL
);
434 /* End of tfc_tpg_nacl_cit */
436 /* Start of tfc_tpg_np_base_cit */
438 CONFIGFS_EATTR_OPS(target_fabric_np_base
, se_tpg_np
, tpg_np_group
);
440 static struct configfs_item_operations target_fabric_np_base_item_ops
= {
441 .show_attribute
= target_fabric_np_base_attr_show
,
442 .store_attribute
= target_fabric_np_base_attr_store
,
445 TF_CIT_SETUP(tpg_np_base
, &target_fabric_np_base_item_ops
, NULL
, NULL
);
447 /* End of tfc_tpg_np_base_cit */
449 /* Start of tfc_tpg_np_cit */
451 static struct config_group
*target_fabric_make_np(
452 struct config_group
*group
,
455 struct se_portal_group
*se_tpg
= container_of(group
,
456 struct se_portal_group
, tpg_np_group
);
457 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
458 struct se_tpg_np
*se_tpg_np
;
460 if (!(tf
->tf_ops
.fabric_make_np
)) {
461 printk(KERN_ERR
"tf->tf_ops.fabric_make_np is NULL\n");
462 return ERR_PTR(-ENOSYS
);
465 se_tpg_np
= tf
->tf_ops
.fabric_make_np(se_tpg
, group
, name
);
466 if (!(se_tpg_np
) || IS_ERR(se_tpg_np
))
467 return ERR_PTR(-EINVAL
);
469 config_group_init_type_name(&se_tpg_np
->tpg_np_group
, name
,
470 &TF_CIT_TMPL(tf
)->tfc_tpg_np_base_cit
);
472 return &se_tpg_np
->tpg_np_group
;
475 static void target_fabric_drop_np(
476 struct config_group
*group
,
477 struct config_item
*item
)
479 struct se_portal_group
*se_tpg
= container_of(group
,
480 struct se_portal_group
, tpg_np_group
);
481 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
482 struct se_tpg_np
*se_tpg_np
= container_of(to_config_group(item
),
483 struct se_tpg_np
, tpg_np_group
);
485 config_item_put(item
);
486 tf
->tf_ops
.fabric_drop_np(se_tpg_np
);
489 static struct configfs_group_operations target_fabric_np_group_ops
= {
490 .make_group
= &target_fabric_make_np
,
491 .drop_item
= &target_fabric_drop_np
,
494 TF_CIT_SETUP(tpg_np
, NULL
, &target_fabric_np_group_ops
, NULL
);
496 /* End of tfc_tpg_np_cit */
498 /* Start of tfc_tpg_port_cit */
500 CONFIGFS_EATTR_STRUCT(target_fabric_port
, se_lun
);
501 #define TCM_PORT_ATTR(_name, _mode) \
502 static struct target_fabric_port_attribute target_fabric_port_##_name = \
503 __CONFIGFS_EATTR(_name, _mode, \
504 target_fabric_port_show_attr_##_name, \
505 target_fabric_port_store_attr_##_name);
507 #define TCM_PORT_ATTOR_RO(_name) \
508 __CONFIGFS_EATTR_RO(_name, \
509 target_fabric_port_show_attr_##_name);
514 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_gp(
524 return core_alua_show_tg_pt_gp_info(lun
->lun_sep
, page
);
527 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_gp(
538 return core_alua_store_tg_pt_gp_info(lun
->lun_sep
, page
, count
);
541 TCM_PORT_ATTR(alua_tg_pt_gp
, S_IRUGO
| S_IWUSR
);
546 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_offline(
556 return core_alua_show_offline_bit(lun
, page
);
559 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_offline(
570 return core_alua_store_offline_bit(lun
, page
, count
);
573 TCM_PORT_ATTR(alua_tg_pt_offline
, S_IRUGO
| S_IWUSR
);
578 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_status(
588 return core_alua_show_secondary_status(lun
, page
);
591 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_status(
602 return core_alua_store_secondary_status(lun
, page
, count
);
605 TCM_PORT_ATTR(alua_tg_pt_status
, S_IRUGO
| S_IWUSR
);
608 * alua_tg_pt_write_md
610 static ssize_t
target_fabric_port_show_attr_alua_tg_pt_write_md(
620 return core_alua_show_secondary_write_metadata(lun
, page
);
623 static ssize_t
target_fabric_port_store_attr_alua_tg_pt_write_md(
634 return core_alua_store_secondary_write_metadata(lun
, page
, count
);
637 TCM_PORT_ATTR(alua_tg_pt_write_md
, S_IRUGO
| S_IWUSR
);
640 static struct configfs_attribute
*target_fabric_port_attrs
[] = {
641 &target_fabric_port_alua_tg_pt_gp
.attr
,
642 &target_fabric_port_alua_tg_pt_offline
.attr
,
643 &target_fabric_port_alua_tg_pt_status
.attr
,
644 &target_fabric_port_alua_tg_pt_write_md
.attr
,
648 CONFIGFS_EATTR_OPS(target_fabric_port
, se_lun
, lun_group
);
650 static int target_fabric_port_link(
651 struct config_item
*lun_ci
,
652 struct config_item
*se_dev_ci
)
654 struct config_item
*tpg_ci
;
655 struct se_device
*dev
;
656 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
657 struct se_lun
, lun_group
);
658 struct se_lun
*lun_p
;
659 struct se_portal_group
*se_tpg
;
660 struct se_subsystem_dev
*se_dev
= container_of(
661 to_config_group(se_dev_ci
), struct se_subsystem_dev
,
663 struct target_fabric_configfs
*tf
;
666 tpg_ci
= &lun_ci
->ci_parent
->ci_group
->cg_item
;
667 se_tpg
= container_of(to_config_group(tpg_ci
),
668 struct se_portal_group
, tpg_group
);
669 tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
671 if (lun
->lun_se_dev
!= NULL
) {
672 printk(KERN_ERR
"Port Symlink already exists\n");
676 dev
= se_dev
->se_dev_ptr
;
678 printk(KERN_ERR
"Unable to locate struct se_device pointer from"
679 " %s\n", config_item_name(se_dev_ci
));
684 lun_p
= core_dev_add_lun(se_tpg
, dev
->se_hba
, dev
,
686 if ((IS_ERR(lun_p
)) || !(lun_p
)) {
687 printk(KERN_ERR
"core_dev_add_lun() failed\n");
692 if (tf
->tf_ops
.fabric_post_link
) {
694 * Call the optional fabric_post_link() to allow a
695 * fabric module to setup any additional state once
696 * core_dev_add_lun() has been called..
698 tf
->tf_ops
.fabric_post_link(se_tpg
, lun
);
706 static int target_fabric_port_unlink(
707 struct config_item
*lun_ci
,
708 struct config_item
*se_dev_ci
)
710 struct se_lun
*lun
= container_of(to_config_group(lun_ci
),
711 struct se_lun
, lun_group
);
712 struct se_portal_group
*se_tpg
= lun
->lun_sep
->sep_tpg
;
713 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
715 if (tf
->tf_ops
.fabric_pre_unlink
) {
717 * Call the optional fabric_pre_unlink() to allow a
718 * fabric module to release any additional stat before
719 * core_dev_del_lun() is called.
721 tf
->tf_ops
.fabric_pre_unlink(se_tpg
, lun
);
724 core_dev_del_lun(se_tpg
, lun
->unpacked_lun
);
728 static struct configfs_item_operations target_fabric_port_item_ops
= {
729 .show_attribute
= target_fabric_port_attr_show
,
730 .store_attribute
= target_fabric_port_attr_store
,
731 .allow_link
= target_fabric_port_link
,
732 .drop_link
= target_fabric_port_unlink
,
735 TF_CIT_SETUP(tpg_port
, &target_fabric_port_item_ops
, NULL
, target_fabric_port_attrs
);
737 /* End of tfc_tpg_port_cit */
739 /* Start of tfc_tpg_lun_cit */
741 static struct config_group
*target_fabric_make_lun(
742 struct config_group
*group
,
746 struct se_portal_group
*se_tpg
= container_of(group
,
747 struct se_portal_group
, tpg_lun_group
);
748 struct target_fabric_configfs
*tf
= se_tpg
->se_tpg_wwn
->wwn_tf
;
749 unsigned long unpacked_lun
;
751 if (strstr(name
, "lun_") != name
) {
752 printk(KERN_ERR
"Unable to locate \'_\" in"
753 " \"lun_$LUN_NUMBER\"\n");
754 return ERR_PTR(-EINVAL
);
756 if (strict_strtoul(name
+ 4, 0, &unpacked_lun
) || unpacked_lun
> UINT_MAX
)
757 return ERR_PTR(-EINVAL
);
759 lun
= core_get_lun_from_tpg(se_tpg
, unpacked_lun
);
761 return ERR_PTR(-EINVAL
);
763 config_group_init_type_name(&lun
->lun_group
, name
,
764 &TF_CIT_TMPL(tf
)->tfc_tpg_port_cit
);
766 return &lun
->lun_group
;
769 static void target_fabric_drop_lun(
770 struct config_group
*group
,
771 struct config_item
*item
)
773 config_item_put(item
);
776 static struct configfs_group_operations target_fabric_lun_group_ops
= {
777 .make_group
= &target_fabric_make_lun
,
778 .drop_item
= &target_fabric_drop_lun
,
781 TF_CIT_SETUP(tpg_lun
, NULL
, &target_fabric_lun_group_ops
, NULL
);
783 /* End of tfc_tpg_lun_cit */
785 /* Start of tfc_tpg_attrib_cit */
787 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib
, se_portal_group
, tpg_attrib_group
);
789 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops
= {
790 .show_attribute
= target_fabric_tpg_attrib_attr_show
,
791 .store_attribute
= target_fabric_tpg_attrib_attr_store
,
794 TF_CIT_SETUP(tpg_attrib
, &target_fabric_tpg_attrib_item_ops
, NULL
, NULL
);
796 /* End of tfc_tpg_attrib_cit */
798 /* Start of tfc_tpg_param_cit */
800 CONFIGFS_EATTR_OPS(target_fabric_tpg_param
, se_portal_group
, tpg_param_group
);
802 static struct configfs_item_operations target_fabric_tpg_param_item_ops
= {
803 .show_attribute
= target_fabric_tpg_param_attr_show
,
804 .store_attribute
= target_fabric_tpg_param_attr_store
,
807 TF_CIT_SETUP(tpg_param
, &target_fabric_tpg_param_item_ops
, NULL
, NULL
);
809 /* End of tfc_tpg_param_cit */
811 /* Start of tfc_tpg_base_cit */
813 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
815 CONFIGFS_EATTR_OPS(target_fabric_tpg
, se_portal_group
, tpg_group
);
817 static struct configfs_item_operations target_fabric_tpg_base_item_ops
= {
818 .show_attribute
= target_fabric_tpg_attr_show
,
819 .store_attribute
= target_fabric_tpg_attr_store
,
822 TF_CIT_SETUP(tpg_base
, &target_fabric_tpg_base_item_ops
, NULL
, NULL
);
824 /* End of tfc_tpg_base_cit */
826 /* Start of tfc_tpg_cit */
828 static struct config_group
*target_fabric_make_tpg(
829 struct config_group
*group
,
832 struct se_wwn
*wwn
= container_of(group
, struct se_wwn
, wwn_group
);
833 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
834 struct se_portal_group
*se_tpg
;
836 if (!(tf
->tf_ops
.fabric_make_tpg
)) {
837 printk(KERN_ERR
"tf->tf_ops.fabric_make_tpg is NULL\n");
838 return ERR_PTR(-ENOSYS
);
841 se_tpg
= tf
->tf_ops
.fabric_make_tpg(wwn
, group
, name
);
842 if (!(se_tpg
) || IS_ERR(se_tpg
))
843 return ERR_PTR(-EINVAL
);
845 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
847 se_tpg
->tpg_group
.default_groups
= se_tpg
->tpg_default_groups
;
848 se_tpg
->tpg_group
.default_groups
[0] = &se_tpg
->tpg_lun_group
;
849 se_tpg
->tpg_group
.default_groups
[1] = &se_tpg
->tpg_np_group
;
850 se_tpg
->tpg_group
.default_groups
[2] = &se_tpg
->tpg_acl_group
;
851 se_tpg
->tpg_group
.default_groups
[3] = &se_tpg
->tpg_attrib_group
;
852 se_tpg
->tpg_group
.default_groups
[4] = &se_tpg
->tpg_param_group
;
853 se_tpg
->tpg_group
.default_groups
[5] = NULL
;
855 config_group_init_type_name(&se_tpg
->tpg_group
, name
,
856 &TF_CIT_TMPL(tf
)->tfc_tpg_base_cit
);
857 config_group_init_type_name(&se_tpg
->tpg_lun_group
, "lun",
858 &TF_CIT_TMPL(tf
)->tfc_tpg_lun_cit
);
859 config_group_init_type_name(&se_tpg
->tpg_np_group
, "np",
860 &TF_CIT_TMPL(tf
)->tfc_tpg_np_cit
);
861 config_group_init_type_name(&se_tpg
->tpg_acl_group
, "acls",
862 &TF_CIT_TMPL(tf
)->tfc_tpg_nacl_cit
);
863 config_group_init_type_name(&se_tpg
->tpg_attrib_group
, "attrib",
864 &TF_CIT_TMPL(tf
)->tfc_tpg_attrib_cit
);
865 config_group_init_type_name(&se_tpg
->tpg_param_group
, "param",
866 &TF_CIT_TMPL(tf
)->tfc_tpg_param_cit
);
868 return &se_tpg
->tpg_group
;
871 static void target_fabric_drop_tpg(
872 struct config_group
*group
,
873 struct config_item
*item
)
875 struct se_wwn
*wwn
= container_of(group
, struct se_wwn
, wwn_group
);
876 struct target_fabric_configfs
*tf
= wwn
->wwn_tf
;
877 struct se_portal_group
*se_tpg
= container_of(to_config_group(item
),
878 struct se_portal_group
, tpg_group
);
879 struct config_group
*tpg_cg
= &se_tpg
->tpg_group
;
880 struct config_item
*df_item
;
883 * Release default groups, but do not release tpg_cg->default_groups
884 * memory as it is statically allocated at se_tpg->tpg_default_groups.
886 for (i
= 0; tpg_cg
->default_groups
[i
]; i
++) {
887 df_item
= &tpg_cg
->default_groups
[i
]->cg_item
;
888 tpg_cg
->default_groups
[i
] = NULL
;
889 config_item_put(df_item
);
892 config_item_put(item
);
893 tf
->tf_ops
.fabric_drop_tpg(se_tpg
);
896 static struct configfs_group_operations target_fabric_tpg_group_ops
= {
897 .make_group
= target_fabric_make_tpg
,
898 .drop_item
= target_fabric_drop_tpg
,
901 TF_CIT_SETUP(tpg
, NULL
, &target_fabric_tpg_group_ops
, NULL
);
903 /* End of tfc_tpg_cit */
905 /* Start of tfc_wwn_cit */
907 static struct config_group
*target_fabric_make_wwn(
908 struct config_group
*group
,
911 struct target_fabric_configfs
*tf
= container_of(group
,
912 struct target_fabric_configfs
, tf_group
);
915 if (!(tf
->tf_ops
.fabric_make_wwn
)) {
916 printk(KERN_ERR
"tf->tf_ops.fabric_make_wwn is NULL\n");
917 return ERR_PTR(-ENOSYS
);
920 wwn
= tf
->tf_ops
.fabric_make_wwn(tf
, group
, name
);
921 if (!(wwn
) || IS_ERR(wwn
))
922 return ERR_PTR(-EINVAL
);
925 config_group_init_type_name(&wwn
->wwn_group
, name
,
926 &TF_CIT_TMPL(tf
)->tfc_tpg_cit
);
928 return &wwn
->wwn_group
;
931 static void target_fabric_drop_wwn(
932 struct config_group
*group
,
933 struct config_item
*item
)
935 struct target_fabric_configfs
*tf
= container_of(group
,
936 struct target_fabric_configfs
, tf_group
);
937 struct se_wwn
*wwn
= container_of(to_config_group(item
),
938 struct se_wwn
, wwn_group
);
940 config_item_put(item
);
941 tf
->tf_ops
.fabric_drop_wwn(wwn
);
944 static struct configfs_group_operations target_fabric_wwn_group_ops
= {
945 .make_group
= target_fabric_make_wwn
,
946 .drop_item
= target_fabric_drop_wwn
,
949 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
951 CONFIGFS_EATTR_OPS(target_fabric_wwn
, target_fabric_configfs
, tf_group
);
953 static struct configfs_item_operations target_fabric_wwn_item_ops
= {
954 .show_attribute
= target_fabric_wwn_attr_show
,
955 .store_attribute
= target_fabric_wwn_attr_store
,
958 TF_CIT_SETUP(wwn
, &target_fabric_wwn_item_ops
, &target_fabric_wwn_group_ops
, NULL
);
960 /* End of tfc_wwn_cit */
962 /* Start of tfc_discovery_cit */
964 CONFIGFS_EATTR_OPS(target_fabric_discovery
, target_fabric_configfs
,
967 static struct configfs_item_operations target_fabric_discovery_item_ops
= {
968 .show_attribute
= target_fabric_discovery_attr_show
,
969 .store_attribute
= target_fabric_discovery_attr_store
,
972 TF_CIT_SETUP(discovery
, &target_fabric_discovery_item_ops
, NULL
, NULL
);
974 /* End of tfc_discovery_cit */
976 int target_fabric_setup_cits(struct target_fabric_configfs
*tf
)
978 target_fabric_setup_discovery_cit(tf
);
979 target_fabric_setup_wwn_cit(tf
);
980 target_fabric_setup_tpg_cit(tf
);
981 target_fabric_setup_tpg_base_cit(tf
);
982 target_fabric_setup_tpg_port_cit(tf
);
983 target_fabric_setup_tpg_lun_cit(tf
);
984 target_fabric_setup_tpg_np_cit(tf
);
985 target_fabric_setup_tpg_np_base_cit(tf
);
986 target_fabric_setup_tpg_attrib_cit(tf
);
987 target_fabric_setup_tpg_param_cit(tf
);
988 target_fabric_setup_tpg_nacl_cit(tf
);
989 target_fabric_setup_tpg_nacl_base_cit(tf
);
990 target_fabric_setup_tpg_nacl_attrib_cit(tf
);
991 target_fabric_setup_tpg_nacl_auth_cit(tf
);
992 target_fabric_setup_tpg_nacl_param_cit(tf
);
993 target_fabric_setup_tpg_mappedlun_cit(tf
);