SHM_UNLOCK: fix Unevictable pages stranded after swap
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / target_core_fabric_configfs.c
blob09b6f8729f918edbcd913770ef73c16485594839
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 <generated/utsrelease.h>
26 #include <linux/utsname.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/namei.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <linux/unistd.h>
34 #include <linux/string.h>
35 #include <linux/syscalls.h>
36 #include <linux/configfs.h>
38 #include <target/target_core_base.h>
39 #include <target/target_core_device.h>
40 #include <target/target_core_tpg.h>
41 #include <target/target_core_transport.h>
42 #include <target/target_core_fabric_ops.h>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
47 #include "target_core_alua.h"
48 #include "target_core_hba.h"
49 #include "target_core_pr.h"
50 #include "target_core_stat.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) \
54 { \
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 pr_debug("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 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
84 "_tpg does not exist\n");
85 return -EINVAL;
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 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
99 config_item_name(wwn_ci));
100 return -EINVAL;
102 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
103 pr_err("Illegal Initiator ACL Symlink outside of %s"
104 " TPGT: %s\n", config_item_name(wwn_ci),
105 config_item_name(tpg_ci));
106 return -EINVAL;
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;
118 else
119 lun_access =
120 (se_tpg->se_tpg_tfo->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)
140 struct se_lun *lun;
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..
149 if (!deve->se_lun)
150 return 0;
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);
156 return 0;
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,
168 char *page)
170 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
171 struct se_dev_entry *deve;
172 ssize_t len;
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) ?
178 1 : 0);
179 spin_unlock_irq(&se_nacl->device_list_lock);
181 return len;
184 static ssize_t target_fabric_mappedlun_store_write_protect(
185 struct se_lun_acl *lacl,
186 const char *page,
187 size_t count)
189 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
190 struct se_portal_group *se_tpg = se_nacl->se_tpg;
191 unsigned long op;
193 if (strict_strtoul(page, 0, &op))
194 return -EINVAL;
196 if ((op != 1) && (op != 0))
197 return -EINVAL;
199 core_update_device_list_access(lacl->mapped_lun, (op) ?
200 TRANSPORT_LUNFLAGS_READ_ONLY :
201 TRANSPORT_LUNFLAGS_READ_WRITE,
202 lacl->se_lun_nacl);
204 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
205 " Mapped LUN: %u Write Protect bit to %s\n",
206 se_tpg->se_tpg_tfo->get_fabric_name(),
207 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
209 return count;
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 void target_fabric_mappedlun_release(struct config_item *item)
219 struct se_lun_acl *lacl = container_of(to_config_group(item),
220 struct se_lun_acl, se_lun_group);
221 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
223 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
226 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
227 &target_fabric_mappedlun_write_protect.attr,
228 NULL,
231 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
232 .release = target_fabric_mappedlun_release,
233 .show_attribute = target_fabric_mappedlun_attr_show,
234 .store_attribute = target_fabric_mappedlun_attr_store,
235 .allow_link = target_fabric_mappedlun_link,
236 .drop_link = target_fabric_mappedlun_unlink,
239 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
240 target_fabric_mappedlun_attrs);
242 /* End of tfc_tpg_mappedlun_cit */
244 /* Start of tfc_tpg_mappedlun_port_cit */
246 static struct config_group *target_core_mappedlun_stat_mkdir(
247 struct config_group *group,
248 const char *name)
250 return ERR_PTR(-ENOSYS);
253 static void target_core_mappedlun_stat_rmdir(
254 struct config_group *group,
255 struct config_item *item)
257 return;
260 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
261 .make_group = target_core_mappedlun_stat_mkdir,
262 .drop_item = target_core_mappedlun_stat_rmdir,
265 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
266 NULL);
268 /* End of tfc_tpg_mappedlun_port_cit */
270 /* Start of tfc_tpg_nacl_attrib_cit */
272 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
274 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
275 .show_attribute = target_fabric_nacl_attrib_attr_show,
276 .store_attribute = target_fabric_nacl_attrib_attr_store,
279 TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
281 /* End of tfc_tpg_nacl_attrib_cit */
283 /* Start of tfc_tpg_nacl_auth_cit */
285 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
287 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
288 .show_attribute = target_fabric_nacl_auth_attr_show,
289 .store_attribute = target_fabric_nacl_auth_attr_store,
292 TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
294 /* End of tfc_tpg_nacl_auth_cit */
296 /* Start of tfc_tpg_nacl_param_cit */
298 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
300 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
301 .show_attribute = target_fabric_nacl_param_attr_show,
302 .store_attribute = target_fabric_nacl_param_attr_store,
305 TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
307 /* End of tfc_tpg_nacl_param_cit */
309 /* Start of tfc_tpg_nacl_base_cit */
311 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
313 static struct config_group *target_fabric_make_mappedlun(
314 struct config_group *group,
315 const char *name)
317 struct se_node_acl *se_nacl = container_of(group,
318 struct se_node_acl, acl_group);
319 struct se_portal_group *se_tpg = se_nacl->se_tpg;
320 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
321 struct se_lun_acl *lacl;
322 struct config_item *acl_ci;
323 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
324 char *buf;
325 unsigned long mapped_lun;
326 int ret = 0;
328 acl_ci = &group->cg_item;
329 if (!acl_ci) {
330 pr_err("Unable to locatel acl_ci\n");
331 return NULL;
334 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
335 if (!buf) {
336 pr_err("Unable to allocate memory for name buf\n");
337 return ERR_PTR(-ENOMEM);
339 snprintf(buf, strlen(name) + 1, "%s", name);
341 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
343 if (strstr(buf, "lun_") != buf) {
344 pr_err("Unable to locate \"lun_\" from buf: %s"
345 " name: %s\n", buf, name);
346 ret = -EINVAL;
347 goto out;
350 * Determine the Mapped LUN value. This is what the SCSI Initiator
351 * Port will actually see.
353 if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
354 ret = -EINVAL;
355 goto out;
358 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
359 config_item_name(acl_ci), &ret);
360 if (!lacl) {
361 ret = -EINVAL;
362 goto out;
365 lacl_cg = &lacl->se_lun_group;
366 lacl_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
367 GFP_KERNEL);
368 if (!lacl_cg->default_groups) {
369 pr_err("Unable to allocate lacl_cg->default_groups\n");
370 ret = -ENOMEM;
371 goto out;
374 config_group_init_type_name(&lacl->se_lun_group, name,
375 &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
376 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
377 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
378 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
379 lacl_cg->default_groups[1] = NULL;
381 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
382 ml_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
383 GFP_KERNEL);
384 if (!ml_stat_grp->default_groups) {
385 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
386 ret = -ENOMEM;
387 goto out;
389 target_stat_setup_mappedlun_default_groups(lacl);
391 kfree(buf);
392 return &lacl->se_lun_group;
393 out:
394 if (lacl_cg)
395 kfree(lacl_cg->default_groups);
396 kfree(buf);
397 return ERR_PTR(ret);
400 static void target_fabric_drop_mappedlun(
401 struct config_group *group,
402 struct config_item *item)
404 struct se_lun_acl *lacl = container_of(to_config_group(item),
405 struct se_lun_acl, se_lun_group);
406 struct config_item *df_item;
407 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
408 int i;
410 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
411 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
412 df_item = &ml_stat_grp->default_groups[i]->cg_item;
413 ml_stat_grp->default_groups[i] = NULL;
414 config_item_put(df_item);
416 kfree(ml_stat_grp->default_groups);
418 lacl_cg = &lacl->se_lun_group;
419 for (i = 0; lacl_cg->default_groups[i]; i++) {
420 df_item = &lacl_cg->default_groups[i]->cg_item;
421 lacl_cg->default_groups[i] = NULL;
422 config_item_put(df_item);
424 kfree(lacl_cg->default_groups);
426 config_item_put(item);
429 static void target_fabric_nacl_base_release(struct config_item *item)
431 struct se_node_acl *se_nacl = container_of(to_config_group(item),
432 struct se_node_acl, acl_group);
433 struct se_portal_group *se_tpg = se_nacl->se_tpg;
434 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
436 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
439 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
440 .release = target_fabric_nacl_base_release,
441 .show_attribute = target_fabric_nacl_base_attr_show,
442 .store_attribute = target_fabric_nacl_base_attr_store,
445 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
446 .make_group = target_fabric_make_mappedlun,
447 .drop_item = target_fabric_drop_mappedlun,
450 TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
451 &target_fabric_nacl_base_group_ops, NULL);
453 /* End of tfc_tpg_nacl_base_cit */
455 /* Start of tfc_node_fabric_stats_cit */
457 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
458 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
460 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
462 /* End of tfc_wwn_fabric_stats_cit */
464 /* Start of tfc_tpg_nacl_cit */
466 static struct config_group *target_fabric_make_nodeacl(
467 struct config_group *group,
468 const char *name)
470 struct se_portal_group *se_tpg = container_of(group,
471 struct se_portal_group, tpg_acl_group);
472 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
473 struct se_node_acl *se_nacl;
474 struct config_group *nacl_cg;
476 if (!tf->tf_ops.fabric_make_nodeacl) {
477 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
478 return ERR_PTR(-ENOSYS);
481 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
482 if (IS_ERR(se_nacl))
483 return ERR_CAST(se_nacl);
485 nacl_cg = &se_nacl->acl_group;
486 nacl_cg->default_groups = se_nacl->acl_default_groups;
487 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
488 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
489 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
490 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
491 nacl_cg->default_groups[4] = NULL;
493 config_group_init_type_name(&se_nacl->acl_group, name,
494 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
495 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
496 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
497 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
498 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
499 config_group_init_type_name(&se_nacl->acl_param_group, "param",
500 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
501 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
502 "fabric_statistics",
503 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
505 return &se_nacl->acl_group;
508 static void target_fabric_drop_nodeacl(
509 struct config_group *group,
510 struct config_item *item)
512 struct se_node_acl *se_nacl = container_of(to_config_group(item),
513 struct se_node_acl, acl_group);
514 struct config_item *df_item;
515 struct config_group *nacl_cg;
516 int i;
518 nacl_cg = &se_nacl->acl_group;
519 for (i = 0; nacl_cg->default_groups[i]; i++) {
520 df_item = &nacl_cg->default_groups[i]->cg_item;
521 nacl_cg->default_groups[i] = NULL;
522 config_item_put(df_item);
525 * struct se_node_acl free is done in target_fabric_nacl_base_release()
527 config_item_put(item);
530 static struct configfs_group_operations target_fabric_nacl_group_ops = {
531 .make_group = target_fabric_make_nodeacl,
532 .drop_item = target_fabric_drop_nodeacl,
535 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
537 /* End of tfc_tpg_nacl_cit */
539 /* Start of tfc_tpg_np_base_cit */
541 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
543 static void target_fabric_np_base_release(struct config_item *item)
545 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
546 struct se_tpg_np, tpg_np_group);
547 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
548 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
550 tf->tf_ops.fabric_drop_np(se_tpg_np);
553 static struct configfs_item_operations target_fabric_np_base_item_ops = {
554 .release = target_fabric_np_base_release,
555 .show_attribute = target_fabric_np_base_attr_show,
556 .store_attribute = target_fabric_np_base_attr_store,
559 TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
561 /* End of tfc_tpg_np_base_cit */
563 /* Start of tfc_tpg_np_cit */
565 static struct config_group *target_fabric_make_np(
566 struct config_group *group,
567 const char *name)
569 struct se_portal_group *se_tpg = container_of(group,
570 struct se_portal_group, tpg_np_group);
571 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
572 struct se_tpg_np *se_tpg_np;
574 if (!tf->tf_ops.fabric_make_np) {
575 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
576 return ERR_PTR(-ENOSYS);
579 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
580 if (!se_tpg_np || IS_ERR(se_tpg_np))
581 return ERR_PTR(-EINVAL);
583 se_tpg_np->tpg_np_parent = se_tpg;
584 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
585 &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
587 return &se_tpg_np->tpg_np_group;
590 static void target_fabric_drop_np(
591 struct config_group *group,
592 struct config_item *item)
595 * struct se_tpg_np is released via target_fabric_np_base_release()
597 config_item_put(item);
600 static struct configfs_group_operations target_fabric_np_group_ops = {
601 .make_group = &target_fabric_make_np,
602 .drop_item = &target_fabric_drop_np,
605 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
607 /* End of tfc_tpg_np_cit */
609 /* Start of tfc_tpg_port_cit */
611 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
612 #define TCM_PORT_ATTR(_name, _mode) \
613 static struct target_fabric_port_attribute target_fabric_port_##_name = \
614 __CONFIGFS_EATTR(_name, _mode, \
615 target_fabric_port_show_attr_##_name, \
616 target_fabric_port_store_attr_##_name);
618 #define TCM_PORT_ATTOR_RO(_name) \
619 __CONFIGFS_EATTR_RO(_name, \
620 target_fabric_port_show_attr_##_name);
623 * alua_tg_pt_gp
625 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
626 struct se_lun *lun,
627 char *page)
629 if (!lun || !lun->lun_sep)
630 return -ENODEV;
632 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
635 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
636 struct se_lun *lun,
637 const char *page,
638 size_t count)
640 if (!lun || !lun->lun_sep)
641 return -ENODEV;
643 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
646 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
649 * alua_tg_pt_offline
651 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
652 struct se_lun *lun,
653 char *page)
655 if (!lun || !lun->lun_sep)
656 return -ENODEV;
658 return core_alua_show_offline_bit(lun, page);
661 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
662 struct se_lun *lun,
663 const char *page,
664 size_t count)
666 if (!lun || !lun->lun_sep)
667 return -ENODEV;
669 return core_alua_store_offline_bit(lun, page, count);
672 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
675 * alua_tg_pt_status
677 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
678 struct se_lun *lun,
679 char *page)
681 if (!lun || !lun->lun_sep)
682 return -ENODEV;
684 return core_alua_show_secondary_status(lun, page);
687 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
688 struct se_lun *lun,
689 const char *page,
690 size_t count)
692 if (!lun || !lun->lun_sep)
693 return -ENODEV;
695 return core_alua_store_secondary_status(lun, page, count);
698 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
701 * alua_tg_pt_write_md
703 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
704 struct se_lun *lun,
705 char *page)
707 if (!lun || !lun->lun_sep)
708 return -ENODEV;
710 return core_alua_show_secondary_write_metadata(lun, page);
713 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
714 struct se_lun *lun,
715 const char *page,
716 size_t count)
718 if (!lun || !lun->lun_sep)
719 return -ENODEV;
721 return core_alua_store_secondary_write_metadata(lun, page, count);
724 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
727 static struct configfs_attribute *target_fabric_port_attrs[] = {
728 &target_fabric_port_alua_tg_pt_gp.attr,
729 &target_fabric_port_alua_tg_pt_offline.attr,
730 &target_fabric_port_alua_tg_pt_status.attr,
731 &target_fabric_port_alua_tg_pt_write_md.attr,
732 NULL,
735 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
737 static int target_fabric_port_link(
738 struct config_item *lun_ci,
739 struct config_item *se_dev_ci)
741 struct config_item *tpg_ci;
742 struct se_device *dev;
743 struct se_lun *lun = container_of(to_config_group(lun_ci),
744 struct se_lun, lun_group);
745 struct se_lun *lun_p;
746 struct se_portal_group *se_tpg;
747 struct se_subsystem_dev *se_dev = container_of(
748 to_config_group(se_dev_ci), struct se_subsystem_dev,
749 se_dev_group);
750 struct target_fabric_configfs *tf;
751 int ret;
753 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
754 se_tpg = container_of(to_config_group(tpg_ci),
755 struct se_portal_group, tpg_group);
756 tf = se_tpg->se_tpg_wwn->wwn_tf;
758 if (lun->lun_se_dev != NULL) {
759 pr_err("Port Symlink already exists\n");
760 return -EEXIST;
763 dev = se_dev->se_dev_ptr;
764 if (!dev) {
765 pr_err("Unable to locate struct se_device pointer from"
766 " %s\n", config_item_name(se_dev_ci));
767 ret = -ENODEV;
768 goto out;
771 lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
772 lun->unpacked_lun);
773 if (IS_ERR(lun_p) || !lun_p) {
774 pr_err("core_dev_add_lun() failed\n");
775 ret = -EINVAL;
776 goto out;
779 if (tf->tf_ops.fabric_post_link) {
781 * Call the optional fabric_post_link() to allow a
782 * fabric module to setup any additional state once
783 * core_dev_add_lun() has been called..
785 tf->tf_ops.fabric_post_link(se_tpg, lun);
788 return 0;
789 out:
790 return ret;
793 static int target_fabric_port_unlink(
794 struct config_item *lun_ci,
795 struct config_item *se_dev_ci)
797 struct se_lun *lun = container_of(to_config_group(lun_ci),
798 struct se_lun, lun_group);
799 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
800 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
802 if (tf->tf_ops.fabric_pre_unlink) {
804 * Call the optional fabric_pre_unlink() to allow a
805 * fabric module to release any additional stat before
806 * core_dev_del_lun() is called.
808 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
811 core_dev_del_lun(se_tpg, lun->unpacked_lun);
812 return 0;
815 static struct configfs_item_operations target_fabric_port_item_ops = {
816 .show_attribute = target_fabric_port_attr_show,
817 .store_attribute = target_fabric_port_attr_store,
818 .allow_link = target_fabric_port_link,
819 .drop_link = target_fabric_port_unlink,
822 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
824 /* End of tfc_tpg_port_cit */
826 /* Start of tfc_tpg_port_stat_cit */
828 static struct config_group *target_core_port_stat_mkdir(
829 struct config_group *group,
830 const char *name)
832 return ERR_PTR(-ENOSYS);
835 static void target_core_port_stat_rmdir(
836 struct config_group *group,
837 struct config_item *item)
839 return;
842 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
843 .make_group = target_core_port_stat_mkdir,
844 .drop_item = target_core_port_stat_rmdir,
847 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
849 /* End of tfc_tpg_port_stat_cit */
851 /* Start of tfc_tpg_lun_cit */
853 static struct config_group *target_fabric_make_lun(
854 struct config_group *group,
855 const char *name)
857 struct se_lun *lun;
858 struct se_portal_group *se_tpg = container_of(group,
859 struct se_portal_group, tpg_lun_group);
860 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
861 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
862 unsigned long unpacked_lun;
863 int errno;
865 if (strstr(name, "lun_") != name) {
866 pr_err("Unable to locate \'_\" in"
867 " \"lun_$LUN_NUMBER\"\n");
868 return ERR_PTR(-EINVAL);
870 if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
871 return ERR_PTR(-EINVAL);
873 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
874 if (!lun)
875 return ERR_PTR(-EINVAL);
877 lun_cg = &lun->lun_group;
878 lun_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
879 GFP_KERNEL);
880 if (!lun_cg->default_groups) {
881 pr_err("Unable to allocate lun_cg->default_groups\n");
882 return ERR_PTR(-ENOMEM);
885 config_group_init_type_name(&lun->lun_group, name,
886 &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
887 config_group_init_type_name(&lun->port_stat_grps.stat_group,
888 "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
889 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
890 lun_cg->default_groups[1] = NULL;
892 port_stat_grp = &lun->port_stat_grps.stat_group;
893 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
894 GFP_KERNEL);
895 if (!port_stat_grp->default_groups) {
896 pr_err("Unable to allocate port_stat_grp->default_groups\n");
897 errno = -ENOMEM;
898 goto out;
900 target_stat_setup_port_default_groups(lun);
902 return &lun->lun_group;
903 out:
904 if (lun_cg)
905 kfree(lun_cg->default_groups);
906 return ERR_PTR(errno);
909 static void target_fabric_drop_lun(
910 struct config_group *group,
911 struct config_item *item)
913 struct se_lun *lun = container_of(to_config_group(item),
914 struct se_lun, lun_group);
915 struct config_item *df_item;
916 struct config_group *lun_cg, *port_stat_grp;
917 int i;
919 port_stat_grp = &lun->port_stat_grps.stat_group;
920 for (i = 0; port_stat_grp->default_groups[i]; i++) {
921 df_item = &port_stat_grp->default_groups[i]->cg_item;
922 port_stat_grp->default_groups[i] = NULL;
923 config_item_put(df_item);
925 kfree(port_stat_grp->default_groups);
927 lun_cg = &lun->lun_group;
928 for (i = 0; lun_cg->default_groups[i]; i++) {
929 df_item = &lun_cg->default_groups[i]->cg_item;
930 lun_cg->default_groups[i] = NULL;
931 config_item_put(df_item);
933 kfree(lun_cg->default_groups);
935 config_item_put(item);
938 static struct configfs_group_operations target_fabric_lun_group_ops = {
939 .make_group = &target_fabric_make_lun,
940 .drop_item = &target_fabric_drop_lun,
943 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
945 /* End of tfc_tpg_lun_cit */
947 /* Start of tfc_tpg_attrib_cit */
949 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
951 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
952 .show_attribute = target_fabric_tpg_attrib_attr_show,
953 .store_attribute = target_fabric_tpg_attrib_attr_store,
956 TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
958 /* End of tfc_tpg_attrib_cit */
960 /* Start of tfc_tpg_param_cit */
962 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
964 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
965 .show_attribute = target_fabric_tpg_param_attr_show,
966 .store_attribute = target_fabric_tpg_param_attr_store,
969 TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
971 /* End of tfc_tpg_param_cit */
973 /* Start of tfc_tpg_base_cit */
975 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
977 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
979 static void target_fabric_tpg_release(struct config_item *item)
981 struct se_portal_group *se_tpg = container_of(to_config_group(item),
982 struct se_portal_group, tpg_group);
983 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
984 struct target_fabric_configfs *tf = wwn->wwn_tf;
986 tf->tf_ops.fabric_drop_tpg(se_tpg);
989 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
990 .release = target_fabric_tpg_release,
991 .show_attribute = target_fabric_tpg_attr_show,
992 .store_attribute = target_fabric_tpg_attr_store,
995 TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
997 /* End of tfc_tpg_base_cit */
999 /* Start of tfc_tpg_cit */
1001 static struct config_group *target_fabric_make_tpg(
1002 struct config_group *group,
1003 const char *name)
1005 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1006 struct target_fabric_configfs *tf = wwn->wwn_tf;
1007 struct se_portal_group *se_tpg;
1009 if (!tf->tf_ops.fabric_make_tpg) {
1010 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
1011 return ERR_PTR(-ENOSYS);
1014 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
1015 if (!se_tpg || IS_ERR(se_tpg))
1016 return ERR_PTR(-EINVAL);
1018 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1020 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1021 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1022 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1023 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1024 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1025 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
1026 se_tpg->tpg_group.default_groups[5] = NULL;
1028 config_group_init_type_name(&se_tpg->tpg_group, name,
1029 &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
1030 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1031 &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
1032 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1033 &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
1034 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1035 &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
1036 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1037 &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
1038 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1039 &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
1041 return &se_tpg->tpg_group;
1044 static void target_fabric_drop_tpg(
1045 struct config_group *group,
1046 struct config_item *item)
1048 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1049 struct se_portal_group, tpg_group);
1050 struct config_group *tpg_cg = &se_tpg->tpg_group;
1051 struct config_item *df_item;
1052 int i;
1054 * Release default groups, but do not release tpg_cg->default_groups
1055 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1057 for (i = 0; tpg_cg->default_groups[i]; i++) {
1058 df_item = &tpg_cg->default_groups[i]->cg_item;
1059 tpg_cg->default_groups[i] = NULL;
1060 config_item_put(df_item);
1063 config_item_put(item);
1066 static void target_fabric_release_wwn(struct config_item *item)
1068 struct se_wwn *wwn = container_of(to_config_group(item),
1069 struct se_wwn, wwn_group);
1070 struct target_fabric_configfs *tf = wwn->wwn_tf;
1072 tf->tf_ops.fabric_drop_wwn(wwn);
1075 static struct configfs_item_operations target_fabric_tpg_item_ops = {
1076 .release = target_fabric_release_wwn,
1079 static struct configfs_group_operations target_fabric_tpg_group_ops = {
1080 .make_group = target_fabric_make_tpg,
1081 .drop_item = target_fabric_drop_tpg,
1084 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1085 NULL);
1087 /* End of tfc_tpg_cit */
1089 /* Start of tfc_wwn_fabric_stats_cit */
1091 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1092 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1094 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1096 /* End of tfc_wwn_fabric_stats_cit */
1098 /* Start of tfc_wwn_cit */
1100 static struct config_group *target_fabric_make_wwn(
1101 struct config_group *group,
1102 const char *name)
1104 struct target_fabric_configfs *tf = container_of(group,
1105 struct target_fabric_configfs, tf_group);
1106 struct se_wwn *wwn;
1108 if (!tf->tf_ops.fabric_make_wwn) {
1109 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1110 return ERR_PTR(-ENOSYS);
1113 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
1114 if (!wwn || IS_ERR(wwn))
1115 return ERR_PTR(-EINVAL);
1117 wwn->wwn_tf = tf;
1119 * Setup default groups from pre-allocated wwn->wwn_default_groups
1121 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1122 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1123 wwn->wwn_group.default_groups[1] = NULL;
1125 config_group_init_type_name(&wwn->wwn_group, name,
1126 &TF_CIT_TMPL(tf)->tfc_tpg_cit);
1127 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1128 &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
1130 return &wwn->wwn_group;
1133 static void target_fabric_drop_wwn(
1134 struct config_group *group,
1135 struct config_item *item)
1137 struct se_wwn *wwn = container_of(to_config_group(item),
1138 struct se_wwn, wwn_group);
1139 struct config_item *df_item;
1140 struct config_group *cg = &wwn->wwn_group;
1141 int i;
1143 for (i = 0; cg->default_groups[i]; i++) {
1144 df_item = &cg->default_groups[i]->cg_item;
1145 cg->default_groups[i] = NULL;
1146 config_item_put(df_item);
1149 config_item_put(item);
1152 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1153 .make_group = target_fabric_make_wwn,
1154 .drop_item = target_fabric_drop_wwn,
1157 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1159 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1161 static struct configfs_item_operations target_fabric_wwn_item_ops = {
1162 .show_attribute = target_fabric_wwn_attr_show,
1163 .store_attribute = target_fabric_wwn_attr_store,
1166 TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1168 /* End of tfc_wwn_cit */
1170 /* Start of tfc_discovery_cit */
1172 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1173 tf_disc_group);
1175 static struct configfs_item_operations target_fabric_discovery_item_ops = {
1176 .show_attribute = target_fabric_discovery_attr_show,
1177 .store_attribute = target_fabric_discovery_attr_store,
1180 TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1182 /* End of tfc_discovery_cit */
1184 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1186 target_fabric_setup_discovery_cit(tf);
1187 target_fabric_setup_wwn_cit(tf);
1188 target_fabric_setup_wwn_fabric_stats_cit(tf);
1189 target_fabric_setup_tpg_cit(tf);
1190 target_fabric_setup_tpg_base_cit(tf);
1191 target_fabric_setup_tpg_port_cit(tf);
1192 target_fabric_setup_tpg_port_stat_cit(tf);
1193 target_fabric_setup_tpg_lun_cit(tf);
1194 target_fabric_setup_tpg_np_cit(tf);
1195 target_fabric_setup_tpg_np_base_cit(tf);
1196 target_fabric_setup_tpg_attrib_cit(tf);
1197 target_fabric_setup_tpg_param_cit(tf);
1198 target_fabric_setup_tpg_nacl_cit(tf);
1199 target_fabric_setup_tpg_nacl_base_cit(tf);
1200 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1201 target_fabric_setup_tpg_nacl_auth_cit(tf);
1202 target_fabric_setup_tpg_nacl_param_cit(tf);
1203 target_fabric_setup_tpg_nacl_stat_cit(tf);
1204 target_fabric_setup_tpg_mappedlun_cit(tf);
1205 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1207 return 0;