gma500: begin adding CDV specific code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / target_core_fabric_configfs.c
blob07ab5a3bb8e8d56a97a76c3c21af3fa8a8e96792
1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
4 * This file contains generic fabric module configfs infrastructure for
5 * TCM v4.x code
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 <linux/version.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/namei.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/unistd.h>
35 #include <linux/string.h>
36 #include <linux/syscalls.h>
37 #include <linux/configfs.h>
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"
51 #include "target_core_stat.h"
53 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
54 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
55 { \
56 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
57 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
59 cit->ct_item_ops = _item_ops; \
60 cit->ct_group_ops = _group_ops; \
61 cit->ct_attrs = _attrs; \
62 cit->ct_owner = tf->tf_module; \
63 printk("Setup generic %s\n", __stringify(_name)); \
66 /* Start of tfc_tpg_mappedlun_cit */
68 static int target_fabric_mappedlun_link(
69 struct config_item *lun_acl_ci,
70 struct config_item *lun_ci)
72 struct se_dev_entry *deve;
73 struct se_lun *lun = container_of(to_config_group(lun_ci),
74 struct se_lun, lun_group);
75 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
76 struct se_lun_acl, se_lun_group);
77 struct se_portal_group *se_tpg;
78 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
79 int ret = 0, lun_access;
81 * Ensure that the source port exists
83 if (!(lun->lun_sep) || !(lun->lun_sep->sep_tpg)) {
84 printk(KERN_ERR "Source se_lun->lun_sep or lun->lun_sep->sep"
85 "_tpg does not exist\n");
86 return -EINVAL;
88 se_tpg = lun->lun_sep->sep_tpg;
90 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
91 tpg_ci = &nacl_ci->ci_group->cg_item;
92 wwn_ci = &tpg_ci->ci_group->cg_item;
93 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
94 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
96 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
98 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
99 printk(KERN_ERR "Illegal Initiator ACL SymLink outside of %s\n",
100 config_item_name(wwn_ci));
101 return -EINVAL;
103 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
104 printk(KERN_ERR "Illegal Initiator ACL Symlink outside of %s"
105 " TPGT: %s\n", config_item_name(wwn_ci),
106 config_item_name(tpg_ci));
107 return -EINVAL;
110 * If this struct se_node_acl was dynamically generated with
111 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
112 * which be will write protected (READ-ONLY) when
113 * tpg_1/attrib/demo_mode_write_protect=1
115 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
116 deve = &lacl->se_lun_nacl->device_list[lacl->mapped_lun];
117 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
118 lun_access = deve->lun_flags;
119 else
120 lun_access =
121 (TPG_TFO(se_tpg)->tpg_check_prod_mode_write_protect(
122 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
123 TRANSPORT_LUNFLAGS_READ_WRITE;
124 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
126 * Determine the actual mapped LUN value user wants..
128 * This value is what the SCSI Initiator actually sees the
129 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
131 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
132 lun->unpacked_lun, lun_access);
134 return (ret < 0) ? -EINVAL : 0;
137 static int target_fabric_mappedlun_unlink(
138 struct config_item *lun_acl_ci,
139 struct config_item *lun_ci)
141 struct se_lun *lun;
142 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
143 struct se_lun_acl, se_lun_group);
144 struct se_node_acl *nacl = lacl->se_lun_nacl;
145 struct se_dev_entry *deve = &nacl->device_list[lacl->mapped_lun];
146 struct se_portal_group *se_tpg;
148 * Determine if the underlying MappedLUN has already been released..
150 if (!(deve->se_lun))
151 return 0;
153 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
154 se_tpg = lun->lun_sep->sep_tpg;
156 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
157 return 0;
160 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
161 #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
162 static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
163 __CONFIGFS_EATTR(_name, _mode, \
164 target_fabric_mappedlun_show_##_name, \
165 target_fabric_mappedlun_store_##_name);
167 static ssize_t target_fabric_mappedlun_show_write_protect(
168 struct se_lun_acl *lacl,
169 char *page)
171 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
172 struct se_dev_entry *deve;
173 ssize_t len;
175 spin_lock_irq(&se_nacl->device_list_lock);
176 deve = &se_nacl->device_list[lacl->mapped_lun];
177 len = sprintf(page, "%d\n",
178 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
179 1 : 0);
180 spin_unlock_irq(&se_nacl->device_list_lock);
182 return len;
185 static ssize_t target_fabric_mappedlun_store_write_protect(
186 struct se_lun_acl *lacl,
187 const char *page,
188 size_t count)
190 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
191 struct se_portal_group *se_tpg = se_nacl->se_tpg;
192 unsigned long op;
194 if (strict_strtoul(page, 0, &op))
195 return -EINVAL;
197 if ((op != 1) && (op != 0))
198 return -EINVAL;
200 core_update_device_list_access(lacl->mapped_lun, (op) ?
201 TRANSPORT_LUNFLAGS_READ_ONLY :
202 TRANSPORT_LUNFLAGS_READ_WRITE,
203 lacl->se_lun_nacl);
205 printk(KERN_INFO "%s_ConfigFS: Changed Initiator ACL: %s"
206 " Mapped LUN: %u Write Protect bit to %s\n",
207 TPG_TFO(se_tpg)->get_fabric_name(),
208 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
210 return count;
214 TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
216 CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
218 static void target_fabric_mappedlun_release(struct config_item *item)
220 struct se_lun_acl *lacl = container_of(to_config_group(item),
221 struct se_lun_acl, se_lun_group);
222 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
224 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
227 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
228 &target_fabric_mappedlun_write_protect.attr,
229 NULL,
232 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
233 .release = target_fabric_mappedlun_release,
234 .show_attribute = target_fabric_mappedlun_attr_show,
235 .store_attribute = target_fabric_mappedlun_attr_store,
236 .allow_link = target_fabric_mappedlun_link,
237 .drop_link = target_fabric_mappedlun_unlink,
240 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
241 target_fabric_mappedlun_attrs);
243 /* End of tfc_tpg_mappedlun_cit */
245 /* Start of tfc_tpg_mappedlun_port_cit */
247 static struct config_group *target_core_mappedlun_stat_mkdir(
248 struct config_group *group,
249 const char *name)
251 return ERR_PTR(-ENOSYS);
254 static void target_core_mappedlun_stat_rmdir(
255 struct config_group *group,
256 struct config_item *item)
258 return;
261 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
262 .make_group = target_core_mappedlun_stat_mkdir,
263 .drop_item = target_core_mappedlun_stat_rmdir,
266 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
267 NULL);
269 /* End of tfc_tpg_mappedlun_port_cit */
271 /* Start of tfc_tpg_nacl_attrib_cit */
273 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
275 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
276 .show_attribute = target_fabric_nacl_attrib_attr_show,
277 .store_attribute = target_fabric_nacl_attrib_attr_store,
280 TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
282 /* End of tfc_tpg_nacl_attrib_cit */
284 /* Start of tfc_tpg_nacl_auth_cit */
286 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
288 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
289 .show_attribute = target_fabric_nacl_auth_attr_show,
290 .store_attribute = target_fabric_nacl_auth_attr_store,
293 TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
295 /* End of tfc_tpg_nacl_auth_cit */
297 /* Start of tfc_tpg_nacl_param_cit */
299 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
301 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
302 .show_attribute = target_fabric_nacl_param_attr_show,
303 .store_attribute = target_fabric_nacl_param_attr_store,
306 TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
308 /* End of tfc_tpg_nacl_param_cit */
310 /* Start of tfc_tpg_nacl_base_cit */
312 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
314 static struct config_group *target_fabric_make_mappedlun(
315 struct config_group *group,
316 const char *name)
318 struct se_node_acl *se_nacl = container_of(group,
319 struct se_node_acl, acl_group);
320 struct se_portal_group *se_tpg = se_nacl->se_tpg;
321 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
322 struct se_lun_acl *lacl;
323 struct config_item *acl_ci;
324 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
325 char *buf;
326 unsigned long mapped_lun;
327 int ret = 0;
329 acl_ci = &group->cg_item;
330 if (!(acl_ci)) {
331 printk(KERN_ERR "Unable to locatel acl_ci\n");
332 return NULL;
335 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
336 if (!(buf)) {
337 printk(KERN_ERR "Unable to allocate memory for name buf\n");
338 return ERR_PTR(-ENOMEM);
340 snprintf(buf, strlen(name) + 1, "%s", name);
342 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
344 if (strstr(buf, "lun_") != buf) {
345 printk(KERN_ERR "Unable to locate \"lun_\" from buf: %s"
346 " name: %s\n", buf, name);
347 ret = -EINVAL;
348 goto out;
351 * Determine the Mapped LUN value. This is what the SCSI Initiator
352 * Port will actually see.
354 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
355 ret = -EINVAL;
356 goto out;
359 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
360 config_item_name(acl_ci), &ret);
361 if (!(lacl)) {
362 ret = -EINVAL;
363 goto out;
366 lacl_cg = &lacl->se_lun_group;
367 lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
368 GFP_KERNEL);
369 if (!lacl_cg->default_groups) {
370 printk(KERN_ERR "Unable to allocate lacl_cg->default_groups\n");
371 ret = -ENOMEM;
372 goto out;
375 config_group_init_type_name(&lacl->se_lun_group, name,
376 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
377 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
378 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
379 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
380 lacl_cg->default_groups[1] = NULL;
382 ml_stat_grp = &ML_STAT_GRPS(lacl)->stat_group;
383 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
384 GFP_KERNEL);
385 if (!ml_stat_grp->default_groups) {
386 printk(KERN_ERR "Unable to allocate ml_stat_grp->default_groups\n");
387 ret = -ENOMEM;
388 goto out;
390 target_stat_setup_mappedlun_default_groups(lacl);
392 kfree(buf);
393 return &lacl->se_lun_group;
394 out:
395 if (lacl_cg)
396 kfree(lacl_cg->default_groups);
397 kfree(buf);
398 return ERR_PTR(ret);
401 static void target_fabric_drop_mappedlun(
402 struct config_group *group,
403 struct config_item *item)
405 struct se_lun_acl *lacl = container_of(to_config_group(item),
406 struct se_lun_acl, se_lun_group);
407 struct config_item *df_item;
408 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
409 int i;
411 ml_stat_grp = &ML_STAT_GRPS(lacl)->stat_group;
412 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
413 df_item = &ml_stat_grp->default_groups[i]->cg_item;
414 ml_stat_grp->default_groups[i] = NULL;
415 config_item_put(df_item);
417 kfree(ml_stat_grp->default_groups);
419 lacl_cg = &lacl->se_lun_group;
420 for (i = 0; lacl_cg->default_groups[i]; i++) {
421 df_item = &lacl_cg->default_groups[i]->cg_item;
422 lacl_cg->default_groups[i] = NULL;
423 config_item_put(df_item);
425 kfree(lacl_cg->default_groups);
427 config_item_put(item);
430 static void target_fabric_nacl_base_release(struct config_item *item)
432 struct se_node_acl *se_nacl = container_of(to_config_group(item),
433 struct se_node_acl, acl_group);
434 struct se_portal_group *se_tpg = se_nacl->se_tpg;
435 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
437 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
440 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
441 .release = target_fabric_nacl_base_release,
442 .show_attribute = target_fabric_nacl_base_attr_show,
443 .store_attribute = target_fabric_nacl_base_attr_store,
446 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
447 .make_group = target_fabric_make_mappedlun,
448 .drop_item = target_fabric_drop_mappedlun,
451 TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
452 &target_fabric_nacl_base_group_ops, NULL);
454 /* End of tfc_tpg_nacl_base_cit */
456 /* Start of tfc_node_fabric_stats_cit */
458 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
459 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
461 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
463 /* End of tfc_wwn_fabric_stats_cit */
465 /* Start of tfc_tpg_nacl_cit */
467 static struct config_group *target_fabric_make_nodeacl(
468 struct config_group *group,
469 const char *name)
471 struct se_portal_group *se_tpg = container_of(group,
472 struct se_portal_group, tpg_acl_group);
473 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
474 struct se_node_acl *se_nacl;
475 struct config_group *nacl_cg;
477 if (!(tf->tf_ops.fabric_make_nodeacl)) {
478 printk(KERN_ERR "tf->tf_ops.fabric_make_nodeacl is NULL\n");
479 return ERR_PTR(-ENOSYS);
482 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
483 if (IS_ERR(se_nacl))
484 return ERR_PTR(PTR_ERR(se_nacl));
486 nacl_cg = &se_nacl->acl_group;
487 nacl_cg->default_groups = se_nacl->acl_default_groups;
488 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
489 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
490 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
491 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
492 nacl_cg->default_groups[4] = NULL;
494 config_group_init_type_name(&se_nacl->acl_group, name,
495 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
496 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
497 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
498 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
499 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
500 config_group_init_type_name(&se_nacl->acl_param_group, "param",
501 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
502 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
503 "fabric_statistics",
504 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
506 return &se_nacl->acl_group;
509 static void target_fabric_drop_nodeacl(
510 struct config_group *group,
511 struct config_item *item)
513 struct se_node_acl *se_nacl = container_of(to_config_group(item),
514 struct se_node_acl, acl_group);
515 struct config_item *df_item;
516 struct config_group *nacl_cg;
517 int i;
519 nacl_cg = &se_nacl->acl_group;
520 for (i = 0; nacl_cg->default_groups[i]; i++) {
521 df_item = &nacl_cg->default_groups[i]->cg_item;
522 nacl_cg->default_groups[i] = NULL;
523 config_item_put(df_item);
526 * struct se_node_acl free is done in target_fabric_nacl_base_release()
528 config_item_put(item);
531 static struct configfs_group_operations target_fabric_nacl_group_ops = {
532 .make_group = target_fabric_make_nodeacl,
533 .drop_item = target_fabric_drop_nodeacl,
536 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
538 /* End of tfc_tpg_nacl_cit */
540 /* Start of tfc_tpg_np_base_cit */
542 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
544 static void target_fabric_np_base_release(struct config_item *item)
546 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
547 struct se_tpg_np, tpg_np_group);
548 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
549 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
551 tf->tf_ops.fabric_drop_np(se_tpg_np);
554 static struct configfs_item_operations target_fabric_np_base_item_ops = {
555 .release = target_fabric_np_base_release,
556 .show_attribute = target_fabric_np_base_attr_show,
557 .store_attribute = target_fabric_np_base_attr_store,
560 TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
562 /* End of tfc_tpg_np_base_cit */
564 /* Start of tfc_tpg_np_cit */
566 static struct config_group *target_fabric_make_np(
567 struct config_group *group,
568 const char *name)
570 struct se_portal_group *se_tpg = container_of(group,
571 struct se_portal_group, tpg_np_group);
572 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
573 struct se_tpg_np *se_tpg_np;
575 if (!(tf->tf_ops.fabric_make_np)) {
576 printk(KERN_ERR "tf->tf_ops.fabric_make_np is NULL\n");
577 return ERR_PTR(-ENOSYS);
580 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
581 if (!(se_tpg_np) || IS_ERR(se_tpg_np))
582 return ERR_PTR(-EINVAL);
584 se_tpg_np->tpg_np_parent = se_tpg;
585 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
586 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
588 return &se_tpg_np->tpg_np_group;
591 static void target_fabric_drop_np(
592 struct config_group *group,
593 struct config_item *item)
596 * struct se_tpg_np is released via target_fabric_np_base_release()
598 config_item_put(item);
601 static struct configfs_group_operations target_fabric_np_group_ops = {
602 .make_group = &target_fabric_make_np,
603 .drop_item = &target_fabric_drop_np,
606 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
608 /* End of tfc_tpg_np_cit */
610 /* Start of tfc_tpg_port_cit */
612 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
613 #define TCM_PORT_ATTR(_name, _mode) \
614 static struct target_fabric_port_attribute target_fabric_port_##_name = \
615 __CONFIGFS_EATTR(_name, _mode, \
616 target_fabric_port_show_attr_##_name, \
617 target_fabric_port_store_attr_##_name);
619 #define TCM_PORT_ATTOR_RO(_name) \
620 __CONFIGFS_EATTR_RO(_name, \
621 target_fabric_port_show_attr_##_name);
624 * alua_tg_pt_gp
626 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
627 struct se_lun *lun,
628 char *page)
630 if (!(lun))
631 return -ENODEV;
633 if (!(lun->lun_sep))
634 return -ENODEV;
636 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
639 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
640 struct se_lun *lun,
641 const char *page,
642 size_t count)
644 if (!(lun))
645 return -ENODEV;
647 if (!(lun->lun_sep))
648 return -ENODEV;
650 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
653 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
656 * alua_tg_pt_offline
658 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
659 struct se_lun *lun,
660 char *page)
662 if (!(lun))
663 return -ENODEV;
665 if (!(lun->lun_sep))
666 return -ENODEV;
668 return core_alua_show_offline_bit(lun, page);
671 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
672 struct se_lun *lun,
673 const char *page,
674 size_t count)
676 if (!(lun))
677 return -ENODEV;
679 if (!(lun->lun_sep))
680 return -ENODEV;
682 return core_alua_store_offline_bit(lun, page, count);
685 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
688 * alua_tg_pt_status
690 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
691 struct se_lun *lun,
692 char *page)
694 if (!(lun))
695 return -ENODEV;
697 if (!(lun->lun_sep))
698 return -ENODEV;
700 return core_alua_show_secondary_status(lun, page);
703 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
704 struct se_lun *lun,
705 const char *page,
706 size_t count)
708 if (!(lun))
709 return -ENODEV;
711 if (!(lun->lun_sep))
712 return -ENODEV;
714 return core_alua_store_secondary_status(lun, page, count);
717 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
720 * alua_tg_pt_write_md
722 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
723 struct se_lun *lun,
724 char *page)
726 if (!(lun))
727 return -ENODEV;
729 if (!(lun->lun_sep))
730 return -ENODEV;
732 return core_alua_show_secondary_write_metadata(lun, page);
735 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
736 struct se_lun *lun,
737 const char *page,
738 size_t count)
740 if (!(lun))
741 return -ENODEV;
743 if (!(lun->lun_sep))
744 return -ENODEV;
746 return core_alua_store_secondary_write_metadata(lun, page, count);
749 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
752 static struct configfs_attribute *target_fabric_port_attrs[] = {
753 &target_fabric_port_alua_tg_pt_gp.attr,
754 &target_fabric_port_alua_tg_pt_offline.attr,
755 &target_fabric_port_alua_tg_pt_status.attr,
756 &target_fabric_port_alua_tg_pt_write_md.attr,
757 NULL,
760 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
762 static int target_fabric_port_link(
763 struct config_item *lun_ci,
764 struct config_item *se_dev_ci)
766 struct config_item *tpg_ci;
767 struct se_device *dev;
768 struct se_lun *lun = container_of(to_config_group(lun_ci),
769 struct se_lun, lun_group);
770 struct se_lun *lun_p;
771 struct se_portal_group *se_tpg;
772 struct se_subsystem_dev *se_dev = container_of(
773 to_config_group(se_dev_ci), struct se_subsystem_dev,
774 se_dev_group);
775 struct target_fabric_configfs *tf;
776 int ret;
778 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
779 se_tpg = container_of(to_config_group(tpg_ci),
780 struct se_portal_group, tpg_group);
781 tf = se_tpg->se_tpg_wwn->wwn_tf;
783 if (lun->lun_se_dev != NULL) {
784 printk(KERN_ERR "Port Symlink already exists\n");
785 return -EEXIST;
788 dev = se_dev->se_dev_ptr;
789 if (!(dev)) {
790 printk(KERN_ERR "Unable to locate struct se_device pointer from"
791 " %s\n", config_item_name(se_dev_ci));
792 ret = -ENODEV;
793 goto out;
796 lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
797 lun->unpacked_lun);
798 if ((IS_ERR(lun_p)) || !(lun_p)) {
799 printk(KERN_ERR "core_dev_add_lun() failed\n");
800 ret = -EINVAL;
801 goto out;
804 if (tf->tf_ops.fabric_post_link) {
806 * Call the optional fabric_post_link() to allow a
807 * fabric module to setup any additional state once
808 * core_dev_add_lun() has been called..
810 tf->tf_ops.fabric_post_link(se_tpg, lun);
813 return 0;
814 out:
815 return ret;
818 static int target_fabric_port_unlink(
819 struct config_item *lun_ci,
820 struct config_item *se_dev_ci)
822 struct se_lun *lun = container_of(to_config_group(lun_ci),
823 struct se_lun, lun_group);
824 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
825 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
827 if (tf->tf_ops.fabric_pre_unlink) {
829 * Call the optional fabric_pre_unlink() to allow a
830 * fabric module to release any additional stat before
831 * core_dev_del_lun() is called.
833 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
836 core_dev_del_lun(se_tpg, lun->unpacked_lun);
837 return 0;
840 static struct configfs_item_operations target_fabric_port_item_ops = {
841 .show_attribute = target_fabric_port_attr_show,
842 .store_attribute = target_fabric_port_attr_store,
843 .allow_link = target_fabric_port_link,
844 .drop_link = target_fabric_port_unlink,
847 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
849 /* End of tfc_tpg_port_cit */
851 /* Start of tfc_tpg_port_stat_cit */
853 static struct config_group *target_core_port_stat_mkdir(
854 struct config_group *group,
855 const char *name)
857 return ERR_PTR(-ENOSYS);
860 static void target_core_port_stat_rmdir(
861 struct config_group *group,
862 struct config_item *item)
864 return;
867 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
868 .make_group = target_core_port_stat_mkdir,
869 .drop_item = target_core_port_stat_rmdir,
872 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
874 /* End of tfc_tpg_port_stat_cit */
876 /* Start of tfc_tpg_lun_cit */
878 static struct config_group *target_fabric_make_lun(
879 struct config_group *group,
880 const char *name)
882 struct se_lun *lun;
883 struct se_portal_group *se_tpg = container_of(group,
884 struct se_portal_group, tpg_lun_group);
885 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
886 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
887 unsigned long unpacked_lun;
888 int errno;
890 if (strstr(name, "lun_") != name) {
891 printk(KERN_ERR "Unable to locate \'_\" in"
892 " \"lun_$LUN_NUMBER\"\n");
893 return ERR_PTR(-EINVAL);
895 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
896 return ERR_PTR(-EINVAL);
898 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
899 if (!(lun))
900 return ERR_PTR(-EINVAL);
902 lun_cg = &lun->lun_group;
903 lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
904 GFP_KERNEL);
905 if (!lun_cg->default_groups) {
906 printk(KERN_ERR "Unable to allocate lun_cg->default_groups\n");
907 return ERR_PTR(-ENOMEM);
910 config_group_init_type_name(&lun->lun_group, name,
911 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
912 config_group_init_type_name(&lun->port_stat_grps.stat_group,
913 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
914 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
915 lun_cg->default_groups[1] = NULL;
917 port_stat_grp = &PORT_STAT_GRP(lun)->stat_group;
918 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
919 GFP_KERNEL);
920 if (!port_stat_grp->default_groups) {
921 printk(KERN_ERR "Unable to allocate port_stat_grp->default_groups\n");
922 errno = -ENOMEM;
923 goto out;
925 target_stat_setup_port_default_groups(lun);
927 return &lun->lun_group;
928 out:
929 if (lun_cg)
930 kfree(lun_cg->default_groups);
931 return ERR_PTR(errno);
934 static void target_fabric_drop_lun(
935 struct config_group *group,
936 struct config_item *item)
938 struct se_lun *lun = container_of(to_config_group(item),
939 struct se_lun, lun_group);
940 struct config_item *df_item;
941 struct config_group *lun_cg, *port_stat_grp;
942 int i;
944 port_stat_grp = &PORT_STAT_GRP(lun)->stat_group;
945 for (i = 0; port_stat_grp->default_groups[i]; i++) {
946 df_item = &port_stat_grp->default_groups[i]->cg_item;
947 port_stat_grp->default_groups[i] = NULL;
948 config_item_put(df_item);
950 kfree(port_stat_grp->default_groups);
952 lun_cg = &lun->lun_group;
953 for (i = 0; lun_cg->default_groups[i]; i++) {
954 df_item = &lun_cg->default_groups[i]->cg_item;
955 lun_cg->default_groups[i] = NULL;
956 config_item_put(df_item);
958 kfree(lun_cg->default_groups);
960 config_item_put(item);
963 static struct configfs_group_operations target_fabric_lun_group_ops = {
964 .make_group = &target_fabric_make_lun,
965 .drop_item = &target_fabric_drop_lun,
968 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
970 /* End of tfc_tpg_lun_cit */
972 /* Start of tfc_tpg_attrib_cit */
974 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
976 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
977 .show_attribute = target_fabric_tpg_attrib_attr_show,
978 .store_attribute = target_fabric_tpg_attrib_attr_store,
981 TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
983 /* End of tfc_tpg_attrib_cit */
985 /* Start of tfc_tpg_param_cit */
987 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
989 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
990 .show_attribute = target_fabric_tpg_param_attr_show,
991 .store_attribute = target_fabric_tpg_param_attr_store,
994 TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
996 /* End of tfc_tpg_param_cit */
998 /* Start of tfc_tpg_base_cit */
1000 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1002 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1004 static void target_fabric_tpg_release(struct config_item *item)
1006 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1007 struct se_portal_group, tpg_group);
1008 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1009 struct target_fabric_configfs *tf = wwn->wwn_tf;
1011 tf->tf_ops.fabric_drop_tpg(se_tpg);
1014 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1015 .release = target_fabric_tpg_release,
1016 .show_attribute = target_fabric_tpg_attr_show,
1017 .store_attribute = target_fabric_tpg_attr_store,
1020 TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
1022 /* End of tfc_tpg_base_cit */
1024 /* Start of tfc_tpg_cit */
1026 static struct config_group *target_fabric_make_tpg(
1027 struct config_group *group,
1028 const char *name)
1030 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1031 struct target_fabric_configfs *tf = wwn->wwn_tf;
1032 struct se_portal_group *se_tpg;
1034 if (!(tf->tf_ops.fabric_make_tpg)) {
1035 printk(KERN_ERR "tf->tf_ops.fabric_make_tpg is NULL\n");
1036 return ERR_PTR(-ENOSYS);
1039 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
1040 if (!(se_tpg) || IS_ERR(se_tpg))
1041 return ERR_PTR(-EINVAL);
1043 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1045 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1046 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1047 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1048 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1049 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1050 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1051 se_tpg->tpg_group.default_groups[5] = NULL;
1053 config_group_init_type_name(&se_tpg->tpg_group, name,
1054 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1055 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1056 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1057 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1058 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1059 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1060 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1061 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1062 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1063 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1064 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1066 return &se_tpg->tpg_group;
1069 static void target_fabric_drop_tpg(
1070 struct config_group *group,
1071 struct config_item *item)
1073 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1074 struct se_portal_group, tpg_group);
1075 struct config_group *tpg_cg = &se_tpg->tpg_group;
1076 struct config_item *df_item;
1077 int i;
1079 * Release default groups, but do not release tpg_cg->default_groups
1080 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1082 for (i = 0; tpg_cg->default_groups[i]; i++) {
1083 df_item = &tpg_cg->default_groups[i]->cg_item;
1084 tpg_cg->default_groups[i] = NULL;
1085 config_item_put(df_item);
1088 config_item_put(item);
1091 static void target_fabric_release_wwn(struct config_item *item)
1093 struct se_wwn *wwn = container_of(to_config_group(item),
1094 struct se_wwn, wwn_group);
1095 struct target_fabric_configfs *tf = wwn->wwn_tf;
1097 tf->tf_ops.fabric_drop_wwn(wwn);
1100 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1101 .release = target_fabric_release_wwn,
1104 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1105 .make_group = target_fabric_make_tpg,
1106 .drop_item = target_fabric_drop_tpg,
1109 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1110 NULL);
1112 /* End of tfc_tpg_cit */
1114 /* Start of tfc_wwn_fabric_stats_cit */
1116 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1117 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1119 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1121 /* End of tfc_wwn_fabric_stats_cit */
1123 /* Start of tfc_wwn_cit */
1125 static struct config_group *target_fabric_make_wwn(
1126 struct config_group *group,
1127 const char *name)
1129 struct target_fabric_configfs *tf = container_of(group,
1130 struct target_fabric_configfs, tf_group);
1131 struct se_wwn *wwn;
1133 if (!(tf->tf_ops.fabric_make_wwn)) {
1134 printk(KERN_ERR "tf->tf_ops.fabric_make_wwn is NULL\n");
1135 return ERR_PTR(-ENOSYS);
1138 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
1139 if (!(wwn) || IS_ERR(wwn))
1140 return ERR_PTR(-EINVAL);
1142 wwn->wwn_tf = tf;
1144 * Setup default groups from pre-allocated wwn->wwn_default_groups
1146 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1147 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1148 wwn->wwn_group.default_groups[1] = NULL;
1150 config_group_init_type_name(&wwn->wwn_group, name,
1151 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
1152 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1153 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
1155 return &wwn->wwn_group;
1158 static void target_fabric_drop_wwn(
1159 struct config_group *group,
1160 struct config_item *item)
1162 struct se_wwn *wwn = container_of(to_config_group(item),
1163 struct se_wwn, wwn_group);
1164 struct config_item *df_item;
1165 struct config_group *cg = &wwn->wwn_group;
1166 int i;
1168 for (i = 0; cg->default_groups[i]; i++) {
1169 df_item = &cg->default_groups[i]->cg_item;
1170 cg->default_groups[i] = NULL;
1171 config_item_put(df_item);
1174 config_item_put(item);
1177 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1178 .make_group = target_fabric_make_wwn,
1179 .drop_item = target_fabric_drop_wwn,
1182 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1184 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1186 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1187 .show_attribute = target_fabric_wwn_attr_show,
1188 .store_attribute = target_fabric_wwn_attr_store,
1191 TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1193 /* End of tfc_wwn_cit */
1195 /* Start of tfc_discovery_cit */
1197 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1198 tf_disc_group);
1200 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1201 .show_attribute = target_fabric_discovery_attr_show,
1202 .store_attribute = target_fabric_discovery_attr_store,
1205 TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1207 /* End of tfc_discovery_cit */
1209 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1211 target_fabric_setup_discovery_cit(tf);
1212 target_fabric_setup_wwn_cit(tf);
1213 target_fabric_setup_wwn_fabric_stats_cit(tf);
1214 target_fabric_setup_tpg_cit(tf);
1215 target_fabric_setup_tpg_base_cit(tf);
1216 target_fabric_setup_tpg_port_cit(tf);
1217 target_fabric_setup_tpg_port_stat_cit(tf);
1218 target_fabric_setup_tpg_lun_cit(tf);
1219 target_fabric_setup_tpg_np_cit(tf);
1220 target_fabric_setup_tpg_np_base_cit(tf);
1221 target_fabric_setup_tpg_attrib_cit(tf);
1222 target_fabric_setup_tpg_param_cit(tf);
1223 target_fabric_setup_tpg_nacl_cit(tf);
1224 target_fabric_setup_tpg_nacl_base_cit(tf);
1225 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1226 target_fabric_setup_tpg_nacl_auth_cit(tf);
1227 target_fabric_setup_tpg_nacl_param_cit(tf);
1228 target_fabric_setup_tpg_nacl_stat_cit(tf);
1229 target_fabric_setup_tpg_mappedlun_cit(tf);
1230 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1232 return 0;