1 /*******************************************************************************
2 * Filename: target_core_configfs.c
4 * This file contains ConfigFS logic for the Generic Target Engine project.
6 * Copyright (c) 2008-2011 Rising Tide Systems
7 * Copyright (c) 2008-2011 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 ****************************************************************************/
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/version.h>
27 #include <generated/utsrelease.h>
28 #include <linux/utsname.h>
29 #include <linux/init.h>
31 #include <linux/namei.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/delay.h>
35 #include <linux/unistd.h>
36 #include <linux/string.h>
37 #include <linux/parser.h>
38 #include <linux/syscalls.h>
39 #include <linux/configfs.h>
40 #include <linux/spinlock.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_device.h>
44 #include <target/target_core_transport.h>
45 #include <target/target_core_fabric_ops.h>
46 #include <target/target_core_fabric_configfs.h>
47 #include <target/target_core_configfs.h>
48 #include <target/configfs_macros.h>
50 #include "target_core_alua.h"
51 #include "target_core_hba.h"
52 #include "target_core_pr.h"
53 #include "target_core_rd.h"
54 #include "target_core_stat.h"
56 extern struct t10_alua_lu_gp *default_lu_gp;
58 static struct list_head g_tf_list;
59 static struct mutex g_tf_lock;
61 struct target_core_configfs_attribute {
62 struct configfs_attribute attr;
63 ssize_t (*show)(void *, char *);
64 ssize_t (*store)(void *, const char *, size_t);
67 static struct config_group target_core_hbagroup;
68 static struct config_group alua_group;
69 static struct config_group alua_lu_gps_group;
71 static DEFINE_SPINLOCK(se_device_lock);
72 static LIST_HEAD(se_dev_list);
74 static inline struct se_hba *
75 item_to_hba(struct config_item *item)
77 return container_of(to_config_group(item), struct se_hba, hba_group);
81 * Attributes for /sys/kernel/config/target/
83 static ssize_t target_core_attr_show(struct config_item *item,
84 struct configfs_attribute *attr,
87 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
88 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
89 utsname()->sysname, utsname()->machine);
92 static struct configfs_item_operations target_core_fabric_item_ops = {
93 .show_attribute = target_core_attr_show,
96 static struct configfs_attribute target_core_item_attr_version = {
97 .ca_owner = THIS_MODULE,
102 static struct target_fabric_configfs *target_core_get_fabric(
105 struct target_fabric_configfs *tf;
110 mutex_lock(&g_tf_lock);
111 list_for_each_entry(tf, &g_tf_list, tf_list) {
112 if (!(strcmp(tf->tf_name, name))) {
113 atomic_inc(&tf->tf_access_cnt);
114 mutex_unlock(&g_tf_lock);
118 mutex_unlock(&g_tf_lock);
124 * Called from struct target_core_group_ops->make_group()
126 static struct config_group *target_core_register_fabric(
127 struct config_group *group,
130 struct target_fabric_configfs *tf;
133 printk(KERN_INFO "Target_Core_ConfigFS: REGISTER -> group: %p name:"
134 " %s\n", group, name);
136 * Ensure that TCM subsystem plugins are loaded at this point for
137 * using the RAMDISK_DR virtual LUN 0 and all other struct se_port
140 if (transport_subsystem_check_init() < 0)
141 return ERR_PTR(-EINVAL);
144 * Below are some hardcoded request_module() calls to automatically
145 * local fabric modules when the following is called:
147 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
149 * Note that this does not limit which TCM fabric module can be
150 * registered, but simply provids auto loading logic for modules with
151 * mkdir(2) system calls with known TCM fabric modules.
153 if (!(strncmp(name, "iscsi", 5))) {
155 * Automatically load the LIO Target fabric module when the
156 * following is called:
158 * mkdir -p $CONFIGFS/target/iscsi
160 ret = request_module("iscsi_target_mod");
162 printk(KERN_ERR "request_module() failed for"
163 " iscsi_target_mod.ko: %d\n", ret);
164 return ERR_PTR(-EINVAL);
166 } else if (!(strncmp(name, "loopback", 8))) {
168 * Automatically load the tcm_loop fabric module when the
169 * following is called:
171 * mkdir -p $CONFIGFS/target/loopback
173 ret = request_module("tcm_loop");
175 printk(KERN_ERR "request_module() failed for"
176 " tcm_loop.ko: %d\n", ret);
177 return ERR_PTR(-EINVAL);
181 tf = target_core_get_fabric(name);
183 printk(KERN_ERR "target_core_get_fabric() failed for %s\n",
185 return ERR_PTR(-EINVAL);
187 printk(KERN_INFO "Target_Core_ConfigFS: REGISTER -> Located fabric:"
188 " %s\n", tf->tf_name);
190 * On a successful target_core_get_fabric() look, the returned
191 * struct target_fabric_configfs *tf will contain a usage reference.
193 printk(KERN_INFO "Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
194 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
196 tf->tf_group.default_groups = tf->tf_default_groups;
197 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
198 tf->tf_group.default_groups[1] = NULL;
200 config_group_init_type_name(&tf->tf_group, name,
201 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
202 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
203 &TF_CIT_TMPL(tf)->tfc_discovery_cit);
205 printk(KERN_INFO "Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
206 " %s\n", tf->tf_group.cg_item.ci_name);
208 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
210 tf->tf_ops.tf_subsys = tf->tf_subsys;
211 tf->tf_fabric = &tf->tf_group.cg_item;
212 printk(KERN_INFO "Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
215 return &tf->tf_group;
219 * Called from struct target_core_group_ops->drop_item()
221 static void target_core_deregister_fabric(
222 struct config_group *group,
223 struct config_item *item)
225 struct target_fabric_configfs *tf = container_of(
226 to_config_group(item), struct target_fabric_configfs, tf_group);
227 struct config_group *tf_group;
228 struct config_item *df_item;
231 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
232 " tf list\n", config_item_name(item));
234 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> located fabric:"
235 " %s\n", tf->tf_name);
236 atomic_dec(&tf->tf_access_cnt);
238 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> Releasing"
239 " tf->tf_fabric for %s\n", tf->tf_name);
240 tf->tf_fabric = NULL;
242 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
243 " %s\n", config_item_name(item));
245 tf_group = &tf->tf_group;
246 for (i = 0; tf_group->default_groups[i]; i++) {
247 df_item = &tf_group->default_groups[i]->cg_item;
248 tf_group->default_groups[i] = NULL;
249 config_item_put(df_item);
251 config_item_put(item);
254 static struct configfs_group_operations target_core_fabric_group_ops = {
255 .make_group = &target_core_register_fabric,
256 .drop_item = &target_core_deregister_fabric,
260 * All item attributes appearing in /sys/kernel/target/ appear here.
262 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
263 &target_core_item_attr_version,
268 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
270 static struct config_item_type target_core_fabrics_item = {
271 .ct_item_ops = &target_core_fabric_item_ops,
272 .ct_group_ops = &target_core_fabric_group_ops,
273 .ct_attrs = target_core_fabric_item_attrs,
274 .ct_owner = THIS_MODULE,
277 static struct configfs_subsystem target_core_fabrics = {
280 .ci_namebuf = "target",
281 .ci_type = &target_core_fabrics_item,
286 static struct configfs_subsystem *target_core_subsystem[] = {
287 &target_core_fabrics,
291 /*##############################################################################
292 // Start functions called by external Target Fabrics Modules
293 //############################################################################*/
296 * First function called by fabric modules to:
298 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
299 * 2) Add struct target_fabric_configfs to g_tf_list
300 * 3) Return struct target_fabric_configfs to fabric module to be passed
301 * into target_fabric_configfs_register().
303 struct target_fabric_configfs *target_fabric_configfs_init(
304 struct module *fabric_mod,
307 struct target_fabric_configfs *tf;
310 printk(KERN_ERR "Missing struct module *fabric_mod pointer\n");
311 return ERR_PTR(-EINVAL);
314 printk(KERN_ERR "Unable to locate passed fabric name\n");
315 return ERR_PTR(-EINVAL);
317 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
318 printk(KERN_ERR "Passed name: %s exceeds TARGET_FABRIC"
319 "_NAME_SIZE\n", name);
320 return ERR_PTR(-EINVAL);
323 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
325 return ERR_PTR(-ENOMEM);
327 INIT_LIST_HEAD(&tf->tf_list);
328 atomic_set(&tf->tf_access_cnt, 0);
330 * Setup the default generic struct config_item_type's (cits) in
331 * struct target_fabric_configfs->tf_cit_tmpl
333 tf->tf_module = fabric_mod;
334 target_fabric_setup_cits(tf);
336 tf->tf_subsys = target_core_subsystem[0];
337 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
339 mutex_lock(&g_tf_lock);
340 list_add_tail(&tf->tf_list, &g_tf_list);
341 mutex_unlock(&g_tf_lock);
343 printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
345 printk(KERN_INFO "Initialized struct target_fabric_configfs: %p for"
346 " %s\n", tf, tf->tf_name);
349 EXPORT_SYMBOL(target_fabric_configfs_init);
352 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
354 void target_fabric_configfs_free(
355 struct target_fabric_configfs *tf)
357 mutex_lock(&g_tf_lock);
358 list_del(&tf->tf_list);
359 mutex_unlock(&g_tf_lock);
363 EXPORT_SYMBOL(target_fabric_configfs_free);
366 * Perform a sanity check of the passed tf->tf_ops before completing
367 * TCM fabric module registration.
369 static int target_fabric_tf_ops_check(
370 struct target_fabric_configfs *tf)
372 struct target_core_fabric_ops *tfo = &tf->tf_ops;
374 if (!(tfo->get_fabric_name)) {
375 printk(KERN_ERR "Missing tfo->get_fabric_name()\n");
378 if (!(tfo->get_fabric_proto_ident)) {
379 printk(KERN_ERR "Missing tfo->get_fabric_proto_ident()\n");
382 if (!(tfo->tpg_get_wwn)) {
383 printk(KERN_ERR "Missing tfo->tpg_get_wwn()\n");
386 if (!(tfo->tpg_get_tag)) {
387 printk(KERN_ERR "Missing tfo->tpg_get_tag()\n");
390 if (!(tfo->tpg_get_default_depth)) {
391 printk(KERN_ERR "Missing tfo->tpg_get_default_depth()\n");
394 if (!(tfo->tpg_get_pr_transport_id)) {
395 printk(KERN_ERR "Missing tfo->tpg_get_pr_transport_id()\n");
398 if (!(tfo->tpg_get_pr_transport_id_len)) {
399 printk(KERN_ERR "Missing tfo->tpg_get_pr_transport_id_len()\n");
402 if (!(tfo->tpg_check_demo_mode)) {
403 printk(KERN_ERR "Missing tfo->tpg_check_demo_mode()\n");
406 if (!(tfo->tpg_check_demo_mode_cache)) {
407 printk(KERN_ERR "Missing tfo->tpg_check_demo_mode_cache()\n");
410 if (!(tfo->tpg_check_demo_mode_write_protect)) {
411 printk(KERN_ERR "Missing tfo->tpg_check_demo_mode_write_protect()\n");
414 if (!(tfo->tpg_check_prod_mode_write_protect)) {
415 printk(KERN_ERR "Missing tfo->tpg_check_prod_mode_write_protect()\n");
418 if (!(tfo->tpg_alloc_fabric_acl)) {
419 printk(KERN_ERR "Missing tfo->tpg_alloc_fabric_acl()\n");
422 if (!(tfo->tpg_release_fabric_acl)) {
423 printk(KERN_ERR "Missing tfo->tpg_release_fabric_acl()\n");
426 if (!(tfo->tpg_get_inst_index)) {
427 printk(KERN_ERR "Missing tfo->tpg_get_inst_index()\n");
430 if (!tfo->release_cmd) {
431 printk(KERN_ERR "Missing tfo->release_cmd()\n");
434 if (!(tfo->shutdown_session)) {
435 printk(KERN_ERR "Missing tfo->shutdown_session()\n");
438 if (!(tfo->close_session)) {
439 printk(KERN_ERR "Missing tfo->close_session()\n");
442 if (!(tfo->stop_session)) {
443 printk(KERN_ERR "Missing tfo->stop_session()\n");
446 if (!(tfo->fall_back_to_erl0)) {
447 printk(KERN_ERR "Missing tfo->fall_back_to_erl0()\n");
450 if (!(tfo->sess_logged_in)) {
451 printk(KERN_ERR "Missing tfo->sess_logged_in()\n");
454 if (!(tfo->sess_get_index)) {
455 printk(KERN_ERR "Missing tfo->sess_get_index()\n");
458 if (!(tfo->write_pending)) {
459 printk(KERN_ERR "Missing tfo->write_pending()\n");
462 if (!(tfo->write_pending_status)) {
463 printk(KERN_ERR "Missing tfo->write_pending_status()\n");
466 if (!(tfo->set_default_node_attributes)) {
467 printk(KERN_ERR "Missing tfo->set_default_node_attributes()\n");
470 if (!(tfo->get_task_tag)) {
471 printk(KERN_ERR "Missing tfo->get_task_tag()\n");
474 if (!(tfo->get_cmd_state)) {
475 printk(KERN_ERR "Missing tfo->get_cmd_state()\n");
478 if (!(tfo->queue_data_in)) {
479 printk(KERN_ERR "Missing tfo->queue_data_in()\n");
482 if (!(tfo->queue_status)) {
483 printk(KERN_ERR "Missing tfo->queue_status()\n");
486 if (!(tfo->queue_tm_rsp)) {
487 printk(KERN_ERR "Missing tfo->queue_tm_rsp()\n");
490 if (!(tfo->set_fabric_sense_len)) {
491 printk(KERN_ERR "Missing tfo->set_fabric_sense_len()\n");
494 if (!(tfo->get_fabric_sense_len)) {
495 printk(KERN_ERR "Missing tfo->get_fabric_sense_len()\n");
498 if (!(tfo->is_state_remove)) {
499 printk(KERN_ERR "Missing tfo->is_state_remove()\n");
503 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
504 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
505 * target_core_fabric_configfs.c WWN+TPG group context code.
507 if (!(tfo->fabric_make_wwn)) {
508 printk(KERN_ERR "Missing tfo->fabric_make_wwn()\n");
511 if (!(tfo->fabric_drop_wwn)) {
512 printk(KERN_ERR "Missing tfo->fabric_drop_wwn()\n");
515 if (!(tfo->fabric_make_tpg)) {
516 printk(KERN_ERR "Missing tfo->fabric_make_tpg()\n");
519 if (!(tfo->fabric_drop_tpg)) {
520 printk(KERN_ERR "Missing tfo->fabric_drop_tpg()\n");
528 * Called 2nd from fabric module with returned parameter of
529 * struct target_fabric_configfs * from target_fabric_configfs_init().
531 * Upon a successful registration, the new fabric's struct config_item is
532 * return. Also, a pointer to this struct is set in the passed
533 * struct target_fabric_configfs.
535 int target_fabric_configfs_register(
536 struct target_fabric_configfs *tf)
541 printk(KERN_ERR "Unable to locate target_fabric_configfs"
545 if (!(tf->tf_subsys)) {
546 printk(KERN_ERR "Unable to target struct config_subsystem"
550 ret = target_fabric_tf_ops_check(tf);
554 printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
558 EXPORT_SYMBOL(target_fabric_configfs_register);
560 void target_fabric_configfs_deregister(
561 struct target_fabric_configfs *tf)
563 struct configfs_subsystem *su;
566 printk(KERN_ERR "Unable to locate passed target_fabric_"
572 printk(KERN_ERR "Unable to locate passed tf->tf_subsys"
576 printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
578 mutex_lock(&g_tf_lock);
579 if (atomic_read(&tf->tf_access_cnt)) {
580 mutex_unlock(&g_tf_lock);
581 printk(KERN_ERR "Non zero tf->tf_access_cnt for fabric %s\n",
585 list_del(&tf->tf_list);
586 mutex_unlock(&g_tf_lock);
588 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
589 " %s\n", tf->tf_name);
590 tf->tf_module = NULL;
591 tf->tf_subsys = NULL;
594 printk("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
597 EXPORT_SYMBOL(target_fabric_configfs_deregister);
599 /*##############################################################################
600 // Stop functions called by external Target Fabrics Modules
601 //############################################################################*/
603 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
605 #define DEF_DEV_ATTRIB_SHOW(_name) \
606 static ssize_t target_core_dev_show_attr_##_name( \
607 struct se_dev_attrib *da, \
610 struct se_device *dev; \
611 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
614 spin_lock(&se_dev->se_dev_lock); \
615 dev = se_dev->se_dev_ptr; \
617 spin_unlock(&se_dev->se_dev_lock); \
620 rb = snprintf(page, PAGE_SIZE, "%u\n", \
621 (u32)dev->se_sub_dev->se_dev_attrib._name); \
622 spin_unlock(&se_dev->se_dev_lock); \
627 #define DEF_DEV_ATTRIB_STORE(_name) \
628 static ssize_t target_core_dev_store_attr_##_name( \
629 struct se_dev_attrib *da, \
633 struct se_device *dev; \
634 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
638 spin_lock(&se_dev->se_dev_lock); \
639 dev = se_dev->se_dev_ptr; \
641 spin_unlock(&se_dev->se_dev_lock); \
644 ret = strict_strtoul(page, 0, &val); \
646 spin_unlock(&se_dev->se_dev_lock); \
647 printk(KERN_ERR "strict_strtoul() failed with" \
648 " ret: %d\n", ret); \
651 ret = se_dev_set_##_name(dev, (u32)val); \
652 spin_unlock(&se_dev->se_dev_lock); \
654 return (!ret) ? count : -EINVAL; \
657 #define DEF_DEV_ATTRIB(_name) \
658 DEF_DEV_ATTRIB_SHOW(_name); \
659 DEF_DEV_ATTRIB_STORE(_name);
661 #define DEF_DEV_ATTRIB_RO(_name) \
662 DEF_DEV_ATTRIB_SHOW(_name);
664 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
665 #define SE_DEV_ATTR(_name, _mode) \
666 static struct target_core_dev_attrib_attribute \
667 target_core_dev_attrib_##_name = \
668 __CONFIGFS_EATTR(_name, _mode, \
669 target_core_dev_show_attr_##_name, \
670 target_core_dev_store_attr_##_name);
672 #define SE_DEV_ATTR_RO(_name); \
673 static struct target_core_dev_attrib_attribute \
674 target_core_dev_attrib_##_name = \
675 __CONFIGFS_EATTR_RO(_name, \
676 target_core_dev_show_attr_##_name);
678 DEF_DEV_ATTRIB(emulate_dpo);
679 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
681 DEF_DEV_ATTRIB(emulate_fua_write);
682 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
684 DEF_DEV_ATTRIB(emulate_fua_read);
685 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
687 DEF_DEV_ATTRIB(emulate_write_cache);
688 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
690 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
691 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
693 DEF_DEV_ATTRIB(emulate_tas);
694 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
696 DEF_DEV_ATTRIB(emulate_tpu);
697 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
699 DEF_DEV_ATTRIB(emulate_tpws);
700 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
702 DEF_DEV_ATTRIB(enforce_pr_isids);
703 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
705 DEF_DEV_ATTRIB_RO(hw_block_size);
706 SE_DEV_ATTR_RO(hw_block_size);
708 DEF_DEV_ATTRIB(block_size);
709 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
711 DEF_DEV_ATTRIB_RO(hw_max_sectors);
712 SE_DEV_ATTR_RO(hw_max_sectors);
714 DEF_DEV_ATTRIB(max_sectors);
715 SE_DEV_ATTR(max_sectors, S_IRUGO | S_IWUSR);
717 DEF_DEV_ATTRIB(optimal_sectors);
718 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
720 DEF_DEV_ATTRIB_RO(hw_queue_depth);
721 SE_DEV_ATTR_RO(hw_queue_depth);
723 DEF_DEV_ATTRIB(queue_depth);
724 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
726 DEF_DEV_ATTRIB(task_timeout);
727 SE_DEV_ATTR(task_timeout, S_IRUGO | S_IWUSR);
729 DEF_DEV_ATTRIB(max_unmap_lba_count);
730 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
732 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
733 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
735 DEF_DEV_ATTRIB(unmap_granularity);
736 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
738 DEF_DEV_ATTRIB(unmap_granularity_alignment);
739 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
741 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
743 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
744 &target_core_dev_attrib_emulate_dpo.attr,
745 &target_core_dev_attrib_emulate_fua_write.attr,
746 &target_core_dev_attrib_emulate_fua_read.attr,
747 &target_core_dev_attrib_emulate_write_cache.attr,
748 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
749 &target_core_dev_attrib_emulate_tas.attr,
750 &target_core_dev_attrib_emulate_tpu.attr,
751 &target_core_dev_attrib_emulate_tpws.attr,
752 &target_core_dev_attrib_enforce_pr_isids.attr,
753 &target_core_dev_attrib_hw_block_size.attr,
754 &target_core_dev_attrib_block_size.attr,
755 &target_core_dev_attrib_hw_max_sectors.attr,
756 &target_core_dev_attrib_max_sectors.attr,
757 &target_core_dev_attrib_optimal_sectors.attr,
758 &target_core_dev_attrib_hw_queue_depth.attr,
759 &target_core_dev_attrib_queue_depth.attr,
760 &target_core_dev_attrib_task_timeout.attr,
761 &target_core_dev_attrib_max_unmap_lba_count.attr,
762 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
763 &target_core_dev_attrib_unmap_granularity.attr,
764 &target_core_dev_attrib_unmap_granularity_alignment.attr,
768 static struct configfs_item_operations target_core_dev_attrib_ops = {
769 .show_attribute = target_core_dev_attrib_attr_show,
770 .store_attribute = target_core_dev_attrib_attr_store,
773 static struct config_item_type target_core_dev_attrib_cit = {
774 .ct_item_ops = &target_core_dev_attrib_ops,
775 .ct_attrs = target_core_dev_attrib_attrs,
776 .ct_owner = THIS_MODULE,
779 /* End functions for struct config_item_type target_core_dev_attrib_cit */
781 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
783 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
784 #define SE_DEV_WWN_ATTR(_name, _mode) \
785 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
786 __CONFIGFS_EATTR(_name, _mode, \
787 target_core_dev_wwn_show_attr_##_name, \
788 target_core_dev_wwn_store_attr_##_name);
790 #define SE_DEV_WWN_ATTR_RO(_name); \
792 static struct target_core_dev_wwn_attribute \
793 target_core_dev_wwn_##_name = \
794 __CONFIGFS_EATTR_RO(_name, \
795 target_core_dev_wwn_show_attr_##_name); \
799 * VPD page 0x80 Unit serial
801 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
802 struct t10_wwn *t10_wwn,
805 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
806 struct se_device *dev;
808 dev = se_dev->se_dev_ptr;
812 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
813 &t10_wwn->unit_serial[0]);
816 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
817 struct t10_wwn *t10_wwn,
821 struct se_subsystem_dev *su_dev = t10_wwn->t10_sub_dev;
822 struct se_device *dev;
823 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
826 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
827 * from the struct scsi_device level firmware, do not allow
828 * VPD Unit Serial to be emulated.
830 * Note this struct scsi_device could also be emulating VPD
831 * information from its drivers/scsi LLD. But for now we assume
832 * it is doing 'the right thing' wrt a world wide unique
833 * VPD Unit Serial Number that OS dependent multipath can depend on.
835 if (su_dev->su_dev_flags & SDF_FIRMWARE_VPD_UNIT_SERIAL) {
836 printk(KERN_ERR "Underlying SCSI device firmware provided VPD"
837 " Unit Serial, ignoring request\n");
841 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
842 printk(KERN_ERR "Emulated VPD Unit Serial exceeds"
843 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
847 * Check to see if any active $FABRIC_MOD exports exist. If they
848 * do exist, fail here as changing this information on the fly
849 * (underneath the initiator side OS dependent multipath code)
850 * could cause negative effects.
852 dev = su_dev->se_dev_ptr;
854 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
855 printk(KERN_ERR "Unable to set VPD Unit Serial while"
856 " active %d $FABRIC_MOD exports exist\n",
857 atomic_read(&dev->dev_export_obj.obj_access_count));
862 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
864 * Also, strip any newline added from the userspace
865 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
867 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
868 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
869 snprintf(su_dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
870 "%s", strstrip(buf));
871 su_dev->su_dev_flags |= SDF_EMULATED_VPD_UNIT_SERIAL;
873 printk(KERN_INFO "Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
874 " %s\n", su_dev->t10_wwn.unit_serial);
879 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
882 * VPD page 0x83 Protocol Identifier
884 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
885 struct t10_wwn *t10_wwn,
888 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
889 struct se_device *dev;
891 unsigned char buf[VPD_TMP_BUF_SIZE];
894 dev = se_dev->se_dev_ptr;
898 memset(buf, 0, VPD_TMP_BUF_SIZE);
900 spin_lock(&t10_wwn->t10_vpd_lock);
901 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
902 if (!(vpd->protocol_identifier_set))
905 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
907 if ((len + strlen(buf) >= PAGE_SIZE))
910 len += sprintf(page+len, "%s", buf);
912 spin_unlock(&t10_wwn->t10_vpd_lock);
917 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
918 struct t10_wwn *t10_wwn,
925 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
928 * Generic wrapper for dumping VPD identifiers by association.
930 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
931 static ssize_t target_core_dev_wwn_show_attr_##_name( \
932 struct t10_wwn *t10_wwn, \
935 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev; \
936 struct se_device *dev; \
937 struct t10_vpd *vpd; \
938 unsigned char buf[VPD_TMP_BUF_SIZE]; \
941 dev = se_dev->se_dev_ptr; \
945 spin_lock(&t10_wwn->t10_vpd_lock); \
946 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
947 if (vpd->association != _assoc) \
950 memset(buf, 0, VPD_TMP_BUF_SIZE); \
951 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
952 if ((len + strlen(buf) >= PAGE_SIZE)) \
954 len += sprintf(page+len, "%s", buf); \
956 memset(buf, 0, VPD_TMP_BUF_SIZE); \
957 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
958 if ((len + strlen(buf) >= PAGE_SIZE)) \
960 len += sprintf(page+len, "%s", buf); \
962 memset(buf, 0, VPD_TMP_BUF_SIZE); \
963 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
964 if ((len + strlen(buf) >= PAGE_SIZE)) \
966 len += sprintf(page+len, "%s", buf); \
968 spin_unlock(&t10_wwn->t10_vpd_lock); \
974 * VPD page 0x83 Assoication: Logical Unit
976 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
978 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
979 struct t10_wwn *t10_wwn,
986 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
989 * VPD page 0x83 Association: Target Port
991 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
993 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
994 struct t10_wwn *t10_wwn,
1001 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
1004 * VPD page 0x83 Association: SCSI Target Device
1006 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1008 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
1009 struct t10_wwn *t10_wwn,
1016 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1018 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1020 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1021 &target_core_dev_wwn_vpd_unit_serial.attr,
1022 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1023 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1024 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1025 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1029 static struct configfs_item_operations target_core_dev_wwn_ops = {
1030 .show_attribute = target_core_dev_wwn_attr_show,
1031 .store_attribute = target_core_dev_wwn_attr_store,
1034 static struct config_item_type target_core_dev_wwn_cit = {
1035 .ct_item_ops = &target_core_dev_wwn_ops,
1036 .ct_attrs = target_core_dev_wwn_attrs,
1037 .ct_owner = THIS_MODULE,
1040 /* End functions for struct config_item_type target_core_dev_wwn_cit */
1042 /* Start functions for struct config_item_type target_core_dev_pr_cit */
1044 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_subsystem_dev);
1045 #define SE_DEV_PR_ATTR(_name, _mode) \
1046 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1047 __CONFIGFS_EATTR(_name, _mode, \
1048 target_core_dev_pr_show_attr_##_name, \
1049 target_core_dev_pr_store_attr_##_name);
1051 #define SE_DEV_PR_ATTR_RO(_name); \
1052 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1053 __CONFIGFS_EATTR_RO(_name, \
1054 target_core_dev_pr_show_attr_##_name);
1059 static ssize_t target_core_dev_pr_show_spc3_res(
1060 struct se_device *dev,
1064 struct se_node_acl *se_nacl;
1065 struct t10_pr_registration *pr_reg;
1066 char i_buf[PR_REG_ISID_ID_LEN];
1069 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1071 spin_lock(&dev->dev_reservation_lock);
1072 pr_reg = dev->dev_pr_res_holder;
1074 *len += sprintf(page + *len, "No SPC-3 Reservation holder\n");
1075 spin_unlock(&dev->dev_reservation_lock);
1078 se_nacl = pr_reg->pr_reg_nacl;
1079 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1080 PR_REG_ISID_ID_LEN);
1082 *len += sprintf(page + *len, "SPC-3 Reservation: %s Initiator: %s%s\n",
1083 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1084 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1085 spin_unlock(&dev->dev_reservation_lock);
1090 static ssize_t target_core_dev_pr_show_spc2_res(
1091 struct se_device *dev,
1095 struct se_node_acl *se_nacl;
1097 spin_lock(&dev->dev_reservation_lock);
1098 se_nacl = dev->dev_reserved_node_acl;
1100 *len += sprintf(page + *len, "No SPC-2 Reservation holder\n");
1101 spin_unlock(&dev->dev_reservation_lock);
1104 *len += sprintf(page + *len, "SPC-2 Reservation: %s Initiator: %s\n",
1105 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1106 se_nacl->initiatorname);
1107 spin_unlock(&dev->dev_reservation_lock);
1112 static ssize_t target_core_dev_pr_show_attr_res_holder(
1113 struct se_subsystem_dev *su_dev,
1118 if (!(su_dev->se_dev_ptr))
1121 switch (su_dev->t10_pr.res_type) {
1122 case SPC3_PERSISTENT_RESERVATIONS:
1123 target_core_dev_pr_show_spc3_res(su_dev->se_dev_ptr,
1126 case SPC2_RESERVATIONS:
1127 target_core_dev_pr_show_spc2_res(su_dev->se_dev_ptr,
1130 case SPC_PASSTHROUGH:
1131 len += sprintf(page+len, "Passthrough\n");
1134 len += sprintf(page+len, "Unknown\n");
1141 SE_DEV_PR_ATTR_RO(res_holder);
1144 * res_pr_all_tgt_pts
1146 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1147 struct se_subsystem_dev *su_dev,
1150 struct se_device *dev;
1151 struct t10_pr_registration *pr_reg;
1154 dev = su_dev->se_dev_ptr;
1158 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1161 spin_lock(&dev->dev_reservation_lock);
1162 pr_reg = dev->dev_pr_res_holder;
1164 len = sprintf(page, "No SPC-3 Reservation holder\n");
1165 spin_unlock(&dev->dev_reservation_lock);
1169 * See All Target Ports (ALL_TG_PT) bit in spcr17, section 6.14.3
1170 * Basic PERSISTENT RESERVER OUT parameter list, page 290
1172 if (pr_reg->pr_reg_all_tg_pt)
1173 len = sprintf(page, "SPC-3 Reservation: All Target"
1174 " Ports registration\n");
1176 len = sprintf(page, "SPC-3 Reservation: Single"
1177 " Target Port registration\n");
1178 spin_unlock(&dev->dev_reservation_lock);
1183 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1188 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1189 struct se_subsystem_dev *su_dev,
1192 if (!(su_dev->se_dev_ptr))
1195 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1198 return sprintf(page, "0x%08x\n", su_dev->t10_pr.pr_generation);
1201 SE_DEV_PR_ATTR_RO(res_pr_generation);
1204 * res_pr_holder_tg_port
1206 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1207 struct se_subsystem_dev *su_dev,
1210 struct se_device *dev;
1211 struct se_node_acl *se_nacl;
1213 struct se_portal_group *se_tpg;
1214 struct t10_pr_registration *pr_reg;
1215 struct target_core_fabric_ops *tfo;
1218 dev = su_dev->se_dev_ptr;
1222 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1225 spin_lock(&dev->dev_reservation_lock);
1226 pr_reg = dev->dev_pr_res_holder;
1228 len = sprintf(page, "No SPC-3 Reservation holder\n");
1229 spin_unlock(&dev->dev_reservation_lock);
1232 se_nacl = pr_reg->pr_reg_nacl;
1233 se_tpg = se_nacl->se_tpg;
1234 lun = pr_reg->pr_reg_tg_pt_lun;
1235 tfo = se_tpg->se_tpg_tfo;
1237 len += sprintf(page+len, "SPC-3 Reservation: %s"
1238 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1239 tfo->tpg_get_wwn(se_tpg));
1240 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1241 " Identifer Tag: %hu %s Portal Group Tag: %hu"
1242 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1243 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1244 tfo->get_fabric_name(), lun->unpacked_lun);
1245 spin_unlock(&dev->dev_reservation_lock);
1250 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1253 * res_pr_registered_i_pts
1255 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1256 struct se_subsystem_dev *su_dev,
1259 struct target_core_fabric_ops *tfo;
1260 struct t10_pr_registration *pr_reg;
1261 unsigned char buf[384];
1262 char i_buf[PR_REG_ISID_ID_LEN];
1264 int reg_count = 0, prf_isid;
1266 if (!(su_dev->se_dev_ptr))
1269 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1272 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1274 spin_lock(&su_dev->t10_pr.registration_lock);
1275 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1278 memset(buf, 0, 384);
1279 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1280 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1281 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1282 PR_REG_ISID_ID_LEN);
1283 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1284 tfo->get_fabric_name(),
1285 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1286 &i_buf[0] : "", pr_reg->pr_res_key,
1287 pr_reg->pr_res_generation);
1289 if ((len + strlen(buf) >= PAGE_SIZE))
1292 len += sprintf(page+len, "%s", buf);
1295 spin_unlock(&su_dev->t10_pr.registration_lock);
1298 len += sprintf(page+len, "None\n");
1303 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1308 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1309 struct se_subsystem_dev *su_dev,
1312 struct se_device *dev;
1313 struct t10_pr_registration *pr_reg;
1316 dev = su_dev->se_dev_ptr;
1320 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1323 spin_lock(&dev->dev_reservation_lock);
1324 pr_reg = dev->dev_pr_res_holder;
1326 len = sprintf(page, "No SPC-3 Reservation holder\n");
1327 spin_unlock(&dev->dev_reservation_lock);
1330 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1331 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1332 spin_unlock(&dev->dev_reservation_lock);
1337 SE_DEV_PR_ATTR_RO(res_pr_type);
1342 static ssize_t target_core_dev_pr_show_attr_res_type(
1343 struct se_subsystem_dev *su_dev,
1348 if (!(su_dev->se_dev_ptr))
1351 switch (su_dev->t10_pr.res_type) {
1352 case SPC3_PERSISTENT_RESERVATIONS:
1353 len = sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1355 case SPC2_RESERVATIONS:
1356 len = sprintf(page, "SPC2_RESERVATIONS\n");
1358 case SPC_PASSTHROUGH:
1359 len = sprintf(page, "SPC_PASSTHROUGH\n");
1362 len = sprintf(page, "UNKNOWN\n");
1369 SE_DEV_PR_ATTR_RO(res_type);
1375 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1376 struct se_subsystem_dev *su_dev,
1379 if (!(su_dev->se_dev_ptr))
1382 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1385 return sprintf(page, "APTPL Bit Status: %s\n",
1386 (su_dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1389 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1392 * res_aptpl_metadata
1394 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1395 struct se_subsystem_dev *su_dev,
1398 if (!(su_dev->se_dev_ptr))
1401 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1404 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1408 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1409 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1410 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1411 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1414 static match_table_t tokens = {
1415 {Opt_initiator_fabric, "initiator_fabric=%s"},
1416 {Opt_initiator_node, "initiator_node=%s"},
1417 {Opt_initiator_sid, "initiator_sid=%s"},
1418 {Opt_sa_res_key, "sa_res_key=%s"},
1419 {Opt_res_holder, "res_holder=%d"},
1420 {Opt_res_type, "res_type=%d"},
1421 {Opt_res_scope, "res_scope=%d"},
1422 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1423 {Opt_mapped_lun, "mapped_lun=%d"},
1424 {Opt_target_fabric, "target_fabric=%s"},
1425 {Opt_target_node, "target_node=%s"},
1426 {Opt_tpgt, "tpgt=%d"},
1427 {Opt_port_rtpi, "port_rtpi=%d"},
1428 {Opt_target_lun, "target_lun=%d"},
1432 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1433 struct se_subsystem_dev *su_dev,
1437 struct se_device *dev;
1438 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1439 unsigned char *t_fabric = NULL, *t_port = NULL;
1440 char *orig, *ptr, *arg_p, *opts;
1441 substring_t args[MAX_OPT_ARGS];
1442 unsigned long long tmp_ll;
1444 u32 mapped_lun = 0, target_lun = 0;
1445 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1446 u16 port_rpti = 0, tpgt = 0;
1449 dev = su_dev->se_dev_ptr;
1453 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1456 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1457 printk(KERN_INFO "Unable to process APTPL metadata while"
1458 " active fabric exports exist\n");
1462 opts = kstrdup(page, GFP_KERNEL);
1467 while ((ptr = strsep(&opts, ",")) != NULL) {
1471 token = match_token(ptr, tokens, args);
1473 case Opt_initiator_fabric:
1474 i_fabric = match_strdup(&args[0]);
1480 case Opt_initiator_node:
1481 i_port = match_strdup(&args[0]);
1486 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1487 printk(KERN_ERR "APTPL metadata initiator_node="
1488 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1489 PR_APTPL_MAX_IPORT_LEN);
1494 case Opt_initiator_sid:
1495 isid = match_strdup(&args[0]);
1500 if (strlen(isid) >= PR_REG_ISID_LEN) {
1501 printk(KERN_ERR "APTPL metadata initiator_isid"
1502 "= exceeds PR_REG_ISID_LEN: %d\n",
1508 case Opt_sa_res_key:
1509 arg_p = match_strdup(&args[0]);
1514 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1516 printk(KERN_ERR "strict_strtoull() failed for"
1520 sa_res_key = (u64)tmp_ll;
1523 * PR APTPL Metadata for Reservation
1525 case Opt_res_holder:
1526 match_int(args, &arg);
1530 match_int(args, &arg);
1534 match_int(args, &arg);
1537 case Opt_res_all_tg_pt:
1538 match_int(args, &arg);
1539 all_tg_pt = (int)arg;
1541 case Opt_mapped_lun:
1542 match_int(args, &arg);
1543 mapped_lun = (u32)arg;
1546 * PR APTPL Metadata for Target Port
1548 case Opt_target_fabric:
1549 t_fabric = match_strdup(&args[0]);
1555 case Opt_target_node:
1556 t_port = match_strdup(&args[0]);
1561 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1562 printk(KERN_ERR "APTPL metadata target_node="
1563 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1564 PR_APTPL_MAX_TPORT_LEN);
1570 match_int(args, &arg);
1574 match_int(args, &arg);
1575 port_rpti = (u16)arg;
1577 case Opt_target_lun:
1578 match_int(args, &arg);
1579 target_lun = (u32)arg;
1586 if (!(i_port) || !(t_port) || !(sa_res_key)) {
1587 printk(KERN_ERR "Illegal parameters for APTPL registration\n");
1592 if (res_holder && !(type)) {
1593 printk(KERN_ERR "Illegal PR type: 0x%02x for reservation"
1599 ret = core_scsi3_alloc_aptpl_registration(&su_dev->t10_pr, sa_res_key,
1600 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1601 res_holder, all_tg_pt, type);
1609 return (ret == 0) ? count : ret;
1612 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1614 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1616 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1617 &target_core_dev_pr_res_holder.attr,
1618 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1619 &target_core_dev_pr_res_pr_generation.attr,
1620 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1621 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1622 &target_core_dev_pr_res_pr_type.attr,
1623 &target_core_dev_pr_res_type.attr,
1624 &target_core_dev_pr_res_aptpl_active.attr,
1625 &target_core_dev_pr_res_aptpl_metadata.attr,
1629 static struct configfs_item_operations target_core_dev_pr_ops = {
1630 .show_attribute = target_core_dev_pr_attr_show,
1631 .store_attribute = target_core_dev_pr_attr_store,
1634 static struct config_item_type target_core_dev_pr_cit = {
1635 .ct_item_ops = &target_core_dev_pr_ops,
1636 .ct_attrs = target_core_dev_pr_attrs,
1637 .ct_owner = THIS_MODULE,
1640 /* End functions for struct config_item_type target_core_dev_pr_cit */
1642 /* Start functions for struct config_item_type target_core_dev_cit */
1644 static ssize_t target_core_show_dev_info(void *p, char *page)
1646 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1647 struct se_hba *hba = se_dev->se_dev_hba;
1648 struct se_subsystem_api *t = hba->transport;
1650 ssize_t read_bytes = 0;
1652 if (!(se_dev->se_dev_ptr))
1655 transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1657 read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1661 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1662 .attr = { .ca_owner = THIS_MODULE,
1664 .ca_mode = S_IRUGO },
1665 .show = target_core_show_dev_info,
1669 static ssize_t target_core_store_dev_control(
1674 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1675 struct se_hba *hba = se_dev->se_dev_hba;
1676 struct se_subsystem_api *t = hba->transport;
1678 if (!(se_dev->se_dev_su_ptr)) {
1679 printk(KERN_ERR "Unable to locate struct se_subsystem_dev>se"
1684 return t->set_configfs_dev_params(hba, se_dev, page, count);
1687 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1688 .attr = { .ca_owner = THIS_MODULE,
1689 .ca_name = "control",
1690 .ca_mode = S_IWUSR },
1692 .store = target_core_store_dev_control,
1695 static ssize_t target_core_show_dev_alias(void *p, char *page)
1697 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1699 if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1702 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1705 static ssize_t target_core_store_dev_alias(
1710 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1711 struct se_hba *hba = se_dev->se_dev_hba;
1714 if (count > (SE_DEV_ALIAS_LEN-1)) {
1715 printk(KERN_ERR "alias count: %d exceeds"
1716 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1717 SE_DEV_ALIAS_LEN-1);
1721 se_dev->su_dev_flags |= SDF_USING_ALIAS;
1722 read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1725 printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set alias: %s\n",
1726 config_item_name(&hba->hba_group.cg_item),
1727 config_item_name(&se_dev->se_dev_group.cg_item),
1728 se_dev->se_dev_alias);
1733 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1734 .attr = { .ca_owner = THIS_MODULE,
1736 .ca_mode = S_IRUGO | S_IWUSR },
1737 .show = target_core_show_dev_alias,
1738 .store = target_core_store_dev_alias,
1741 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1743 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1745 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH))
1748 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_udev_path);
1751 static ssize_t target_core_store_dev_udev_path(
1756 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1757 struct se_hba *hba = se_dev->se_dev_hba;
1760 if (count > (SE_UDEV_PATH_LEN-1)) {
1761 printk(KERN_ERR "udev_path count: %d exceeds"
1762 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1763 SE_UDEV_PATH_LEN-1);
1767 se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1768 read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1771 printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1772 config_item_name(&hba->hba_group.cg_item),
1773 config_item_name(&se_dev->se_dev_group.cg_item),
1774 se_dev->se_dev_udev_path);
1779 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1780 .attr = { .ca_owner = THIS_MODULE,
1781 .ca_name = "udev_path",
1782 .ca_mode = S_IRUGO | S_IWUSR },
1783 .show = target_core_show_dev_udev_path,
1784 .store = target_core_store_dev_udev_path,
1787 static ssize_t target_core_store_dev_enable(
1792 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1793 struct se_device *dev;
1794 struct se_hba *hba = se_dev->se_dev_hba;
1795 struct se_subsystem_api *t = hba->transport;
1798 ptr = strstr(page, "1");
1800 printk(KERN_ERR "For dev_enable ops, only valid value"
1804 if ((se_dev->se_dev_ptr)) {
1805 printk(KERN_ERR "se_dev->se_dev_ptr already set for storage"
1810 if (t->check_configfs_dev_params(hba, se_dev) < 0)
1813 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1815 return PTR_ERR(dev);
1819 se_dev->se_dev_ptr = dev;
1820 printk(KERN_INFO "Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1821 " %p\n", se_dev->se_dev_ptr);
1826 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1827 .attr = { .ca_owner = THIS_MODULE,
1828 .ca_name = "enable",
1829 .ca_mode = S_IWUSR },
1831 .store = target_core_store_dev_enable,
1834 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1836 struct se_device *dev;
1837 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1838 struct config_item *lu_ci;
1839 struct t10_alua_lu_gp *lu_gp;
1840 struct t10_alua_lu_gp_member *lu_gp_mem;
1843 dev = su_dev->se_dev_ptr;
1847 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED)
1850 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1852 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1857 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1858 lu_gp = lu_gp_mem->lu_gp;
1860 lu_ci = &lu_gp->lu_gp_group.cg_item;
1861 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1862 config_item_name(lu_ci), lu_gp->lu_gp_id);
1864 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1869 static ssize_t target_core_store_alua_lu_gp(
1874 struct se_device *dev;
1875 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1876 struct se_hba *hba = su_dev->se_dev_hba;
1877 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1878 struct t10_alua_lu_gp_member *lu_gp_mem;
1879 unsigned char buf[LU_GROUP_NAME_BUF];
1882 dev = su_dev->se_dev_ptr;
1886 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1887 printk(KERN_WARNING "SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1888 config_item_name(&hba->hba_group.cg_item),
1889 config_item_name(&su_dev->se_dev_group.cg_item));
1892 if (count > LU_GROUP_NAME_BUF) {
1893 printk(KERN_ERR "ALUA LU Group Alias too large!\n");
1896 memset(buf, 0, LU_GROUP_NAME_BUF);
1897 memcpy(buf, page, count);
1899 * Any ALUA logical unit alias besides "NULL" means we will be
1900 * making a new group association.
1902 if (strcmp(strstrip(buf), "NULL")) {
1904 * core_alua_get_lu_gp_by_name() will increment reference to
1905 * struct t10_alua_lu_gp. This reference is released with
1906 * core_alua_get_lu_gp_by_name below().
1908 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1912 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1915 core_alua_put_lu_gp_from_name(lu_gp_new);
1916 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1921 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1922 lu_gp = lu_gp_mem->lu_gp;
1925 * Clearing an existing lu_gp association, and replacing
1929 printk(KERN_INFO "Target_Core_ConfigFS: Releasing %s/%s"
1930 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1932 config_item_name(&hba->hba_group.cg_item),
1933 config_item_name(&su_dev->se_dev_group.cg_item),
1934 config_item_name(&lu_gp->lu_gp_group.cg_item),
1937 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1938 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1943 * Removing existing association of lu_gp_mem with lu_gp
1945 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1949 * Associate lu_gp_mem with lu_gp_new.
1951 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1952 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1954 printk(KERN_INFO "Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1955 " core/alua/lu_gps/%s, ID: %hu\n",
1956 (move) ? "Moving" : "Adding",
1957 config_item_name(&hba->hba_group.cg_item),
1958 config_item_name(&su_dev->se_dev_group.cg_item),
1959 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1960 lu_gp_new->lu_gp_id);
1962 core_alua_put_lu_gp_from_name(lu_gp_new);
1966 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1967 .attr = { .ca_owner = THIS_MODULE,
1968 .ca_name = "alua_lu_gp",
1969 .ca_mode = S_IRUGO | S_IWUSR },
1970 .show = target_core_show_alua_lu_gp,
1971 .store = target_core_store_alua_lu_gp,
1974 static struct configfs_attribute *lio_core_dev_attrs[] = {
1975 &target_core_attr_dev_info.attr,
1976 &target_core_attr_dev_control.attr,
1977 &target_core_attr_dev_alias.attr,
1978 &target_core_attr_dev_udev_path.attr,
1979 &target_core_attr_dev_enable.attr,
1980 &target_core_attr_dev_alua_lu_gp.attr,
1984 static void target_core_dev_release(struct config_item *item)
1986 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
1987 struct se_subsystem_dev, se_dev_group);
1988 struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
1989 struct se_subsystem_api *t = hba->transport;
1990 struct config_group *dev_cg = &se_dev->se_dev_group;
1992 kfree(dev_cg->default_groups);
1994 * This pointer will set when the storage is enabled with:
1995 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
1997 if (se_dev->se_dev_ptr) {
1998 printk(KERN_INFO "Target_Core_ConfigFS: Calling se_free_"
1999 "virtual_device() for se_dev_ptr: %p\n",
2000 se_dev->se_dev_ptr);
2002 se_free_virtual_device(se_dev->se_dev_ptr, hba);
2005 * Release struct se_subsystem_dev->se_dev_su_ptr..
2007 printk(KERN_INFO "Target_Core_ConfigFS: Calling t->free_"
2008 "device() for se_dev_su_ptr: %p\n",
2009 se_dev->se_dev_su_ptr);
2011 t->free_device(se_dev->se_dev_su_ptr);
2014 printk(KERN_INFO "Target_Core_ConfigFS: Deallocating se_subsystem"
2015 "_dev_t: %p\n", se_dev);
2019 static ssize_t target_core_dev_show(struct config_item *item,
2020 struct configfs_attribute *attr,
2023 struct se_subsystem_dev *se_dev = container_of(
2024 to_config_group(item), struct se_subsystem_dev,
2026 struct target_core_configfs_attribute *tc_attr = container_of(
2027 attr, struct target_core_configfs_attribute, attr);
2029 if (!(tc_attr->show))
2032 return tc_attr->show(se_dev, page);
2035 static ssize_t target_core_dev_store(struct config_item *item,
2036 struct configfs_attribute *attr,
2037 const char *page, size_t count)
2039 struct se_subsystem_dev *se_dev = container_of(
2040 to_config_group(item), struct se_subsystem_dev,
2042 struct target_core_configfs_attribute *tc_attr = container_of(
2043 attr, struct target_core_configfs_attribute, attr);
2045 if (!(tc_attr->store))
2048 return tc_attr->store(se_dev, page, count);
2051 static struct configfs_item_operations target_core_dev_item_ops = {
2052 .release = target_core_dev_release,
2053 .show_attribute = target_core_dev_show,
2054 .store_attribute = target_core_dev_store,
2057 static struct config_item_type target_core_dev_cit = {
2058 .ct_item_ops = &target_core_dev_item_ops,
2059 .ct_attrs = lio_core_dev_attrs,
2060 .ct_owner = THIS_MODULE,
2063 /* End functions for struct config_item_type target_core_dev_cit */
2065 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2067 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2068 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2069 static struct target_core_alua_lu_gp_attribute \
2070 target_core_alua_lu_gp_##_name = \
2071 __CONFIGFS_EATTR(_name, _mode, \
2072 target_core_alua_lu_gp_show_attr_##_name, \
2073 target_core_alua_lu_gp_store_attr_##_name);
2075 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2076 static struct target_core_alua_lu_gp_attribute \
2077 target_core_alua_lu_gp_##_name = \
2078 __CONFIGFS_EATTR_RO(_name, \
2079 target_core_alua_lu_gp_show_attr_##_name);
2084 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2085 struct t10_alua_lu_gp *lu_gp,
2088 if (!(lu_gp->lu_gp_valid_id))
2091 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2094 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2095 struct t10_alua_lu_gp *lu_gp,
2099 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2100 unsigned long lu_gp_id;
2103 ret = strict_strtoul(page, 0, &lu_gp_id);
2105 printk(KERN_ERR "strict_strtoul() returned %d for"
2106 " lu_gp_id\n", ret);
2109 if (lu_gp_id > 0x0000ffff) {
2110 printk(KERN_ERR "ALUA lu_gp_id: %lu exceeds maximum:"
2111 " 0x0000ffff\n", lu_gp_id);
2115 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2119 printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Logical Unit"
2120 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2121 config_item_name(&alua_lu_gp_cg->cg_item),
2127 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2132 static ssize_t target_core_alua_lu_gp_show_attr_members(
2133 struct t10_alua_lu_gp *lu_gp,
2136 struct se_device *dev;
2138 struct se_subsystem_dev *su_dev;
2139 struct t10_alua_lu_gp_member *lu_gp_mem;
2140 ssize_t len = 0, cur_len;
2141 unsigned char buf[LU_GROUP_NAME_BUF];
2143 memset(buf, 0, LU_GROUP_NAME_BUF);
2145 spin_lock(&lu_gp->lu_gp_lock);
2146 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2147 dev = lu_gp_mem->lu_gp_mem_dev;
2148 su_dev = dev->se_sub_dev;
2149 hba = su_dev->se_dev_hba;
2151 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2152 config_item_name(&hba->hba_group.cg_item),
2153 config_item_name(&su_dev->se_dev_group.cg_item));
2154 cur_len++; /* Extra byte for NULL terminator */
2156 if ((cur_len + len) > PAGE_SIZE) {
2157 printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2158 "_members buffer\n");
2161 memcpy(page+len, buf, cur_len);
2164 spin_unlock(&lu_gp->lu_gp_lock);
2169 SE_DEV_ALUA_LU_ATTR_RO(members);
2171 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2173 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2174 &target_core_alua_lu_gp_lu_gp_id.attr,
2175 &target_core_alua_lu_gp_members.attr,
2179 static void target_core_alua_lu_gp_release(struct config_item *item)
2181 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2182 struct t10_alua_lu_gp, lu_gp_group);
2184 core_alua_free_lu_gp(lu_gp);
2187 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2188 .release = target_core_alua_lu_gp_release,
2189 .show_attribute = target_core_alua_lu_gp_attr_show,
2190 .store_attribute = target_core_alua_lu_gp_attr_store,
2193 static struct config_item_type target_core_alua_lu_gp_cit = {
2194 .ct_item_ops = &target_core_alua_lu_gp_ops,
2195 .ct_attrs = target_core_alua_lu_gp_attrs,
2196 .ct_owner = THIS_MODULE,
2199 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2201 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2203 static struct config_group *target_core_alua_create_lu_gp(
2204 struct config_group *group,
2207 struct t10_alua_lu_gp *lu_gp;
2208 struct config_group *alua_lu_gp_cg = NULL;
2209 struct config_item *alua_lu_gp_ci = NULL;
2211 lu_gp = core_alua_allocate_lu_gp(name, 0);
2215 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2216 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2218 config_group_init_type_name(alua_lu_gp_cg, name,
2219 &target_core_alua_lu_gp_cit);
2221 printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2222 " Group: core/alua/lu_gps/%s\n",
2223 config_item_name(alua_lu_gp_ci));
2225 return alua_lu_gp_cg;
2229 static void target_core_alua_drop_lu_gp(
2230 struct config_group *group,
2231 struct config_item *item)
2233 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2234 struct t10_alua_lu_gp, lu_gp_group);
2236 printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2237 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2238 config_item_name(item), lu_gp->lu_gp_id);
2240 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2241 * -> target_core_alua_lu_gp_release()
2243 config_item_put(item);
2246 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2247 .make_group = &target_core_alua_create_lu_gp,
2248 .drop_item = &target_core_alua_drop_lu_gp,
2251 static struct config_item_type target_core_alua_lu_gps_cit = {
2252 .ct_item_ops = NULL,
2253 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2254 .ct_owner = THIS_MODULE,
2257 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2259 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2261 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2262 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2263 static struct target_core_alua_tg_pt_gp_attribute \
2264 target_core_alua_tg_pt_gp_##_name = \
2265 __CONFIGFS_EATTR(_name, _mode, \
2266 target_core_alua_tg_pt_gp_show_attr_##_name, \
2267 target_core_alua_tg_pt_gp_store_attr_##_name);
2269 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2270 static struct target_core_alua_tg_pt_gp_attribute \
2271 target_core_alua_tg_pt_gp_##_name = \
2272 __CONFIGFS_EATTR_RO(_name, \
2273 target_core_alua_tg_pt_gp_show_attr_##_name);
2278 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2279 struct t10_alua_tg_pt_gp *tg_pt_gp,
2282 return sprintf(page, "%d\n",
2283 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2286 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2287 struct t10_alua_tg_pt_gp *tg_pt_gp,
2291 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2295 if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2296 printk(KERN_ERR "Unable to do implict ALUA on non valid"
2297 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2301 ret = strict_strtoul(page, 0, &tmp);
2303 printk("Unable to extract new ALUA access state from"
2307 new_state = (int)tmp;
2309 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2310 printk(KERN_ERR "Unable to process implict configfs ALUA"
2311 " transition while TPGS_IMPLICT_ALUA is diabled\n");
2315 ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2316 NULL, NULL, new_state, 0);
2317 return (!ret) ? count : -EINVAL;
2320 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2323 * alua_access_status
2325 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2326 struct t10_alua_tg_pt_gp *tg_pt_gp,
2329 return sprintf(page, "%s\n",
2330 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2333 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2334 struct t10_alua_tg_pt_gp *tg_pt_gp,
2339 int new_status, ret;
2341 if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2342 printk(KERN_ERR "Unable to do set ALUA access status on non"
2343 " valid tg_pt_gp ID: %hu\n",
2344 tg_pt_gp->tg_pt_gp_valid_id);
2348 ret = strict_strtoul(page, 0, &tmp);
2350 printk(KERN_ERR "Unable to extract new ALUA access status"
2351 " from %s\n", page);
2354 new_status = (int)tmp;
2356 if ((new_status != ALUA_STATUS_NONE) &&
2357 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2358 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2359 printk(KERN_ERR "Illegal ALUA access status: 0x%02x\n",
2364 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2368 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2373 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2374 struct t10_alua_tg_pt_gp *tg_pt_gp,
2377 return core_alua_show_access_type(tg_pt_gp, page);
2380 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2381 struct t10_alua_tg_pt_gp *tg_pt_gp,
2385 return core_alua_store_access_type(tg_pt_gp, page, count);
2388 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2391 * alua_write_metadata
2393 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2394 struct t10_alua_tg_pt_gp *tg_pt_gp,
2397 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2400 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2401 struct t10_alua_tg_pt_gp *tg_pt_gp,
2408 ret = strict_strtoul(page, 0, &tmp);
2410 printk(KERN_ERR "Unable to extract alua_write_metadata\n");
2414 if ((tmp != 0) && (tmp != 1)) {
2415 printk(KERN_ERR "Illegal value for alua_write_metadata:"
2419 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2424 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2431 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2432 struct t10_alua_tg_pt_gp *tg_pt_gp,
2435 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2439 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2440 struct t10_alua_tg_pt_gp *tg_pt_gp,
2444 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2447 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2452 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2453 struct t10_alua_tg_pt_gp *tg_pt_gp,
2456 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2459 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2460 struct t10_alua_tg_pt_gp *tg_pt_gp,
2464 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2467 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2473 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2474 struct t10_alua_tg_pt_gp *tg_pt_gp,
2477 return core_alua_show_preferred_bit(tg_pt_gp, page);
2480 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2481 struct t10_alua_tg_pt_gp *tg_pt_gp,
2485 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2488 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2493 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2494 struct t10_alua_tg_pt_gp *tg_pt_gp,
2497 if (!(tg_pt_gp->tg_pt_gp_valid_id))
2500 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2503 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2504 struct t10_alua_tg_pt_gp *tg_pt_gp,
2508 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2509 unsigned long tg_pt_gp_id;
2512 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2514 printk(KERN_ERR "strict_strtoul() returned %d for"
2515 " tg_pt_gp_id\n", ret);
2518 if (tg_pt_gp_id > 0x0000ffff) {
2519 printk(KERN_ERR "ALUA tg_pt_gp_id: %lu exceeds maximum:"
2520 " 0x0000ffff\n", tg_pt_gp_id);
2524 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2528 printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Target Port Group: "
2529 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2530 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2531 tg_pt_gp->tg_pt_gp_id);
2536 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2541 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2542 struct t10_alua_tg_pt_gp *tg_pt_gp,
2545 struct se_port *port;
2546 struct se_portal_group *tpg;
2548 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2549 ssize_t len = 0, cur_len;
2550 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2552 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2554 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2555 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2556 tg_pt_gp_mem_list) {
2557 port = tg_pt_gp_mem->tg_pt;
2558 tpg = port->sep_tpg;
2559 lun = port->sep_lun;
2561 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2562 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2563 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2564 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2565 config_item_name(&lun->lun_group.cg_item));
2566 cur_len++; /* Extra byte for NULL terminator */
2568 if ((cur_len + len) > PAGE_SIZE) {
2569 printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2570 "_members buffer\n");
2573 memcpy(page+len, buf, cur_len);
2576 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2581 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2583 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2586 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2587 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2588 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2589 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2590 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2591 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2592 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2593 &target_core_alua_tg_pt_gp_preferred.attr,
2594 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2595 &target_core_alua_tg_pt_gp_members.attr,
2599 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2601 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2602 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2604 core_alua_free_tg_pt_gp(tg_pt_gp);
2607 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2608 .release = target_core_alua_tg_pt_gp_release,
2609 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2610 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2613 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2614 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2615 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2616 .ct_owner = THIS_MODULE,
2619 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2621 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2623 static struct config_group *target_core_alua_create_tg_pt_gp(
2624 struct config_group *group,
2627 struct t10_alua *alua = container_of(group, struct t10_alua,
2628 alua_tg_pt_gps_group);
2629 struct t10_alua_tg_pt_gp *tg_pt_gp;
2630 struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2631 struct config_group *alua_tg_pt_gp_cg = NULL;
2632 struct config_item *alua_tg_pt_gp_ci = NULL;
2634 tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2638 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2639 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2641 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2642 &target_core_alua_tg_pt_gp_cit);
2644 printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Target Port"
2645 " Group: alua/tg_pt_gps/%s\n",
2646 config_item_name(alua_tg_pt_gp_ci));
2648 return alua_tg_pt_gp_cg;
2651 static void target_core_alua_drop_tg_pt_gp(
2652 struct config_group *group,
2653 struct config_item *item)
2655 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2656 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2658 printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Target Port"
2659 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2660 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2662 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2663 * -> target_core_alua_tg_pt_gp_release().
2665 config_item_put(item);
2668 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2669 .make_group = &target_core_alua_create_tg_pt_gp,
2670 .drop_item = &target_core_alua_drop_tg_pt_gp,
2673 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2674 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2675 .ct_owner = THIS_MODULE,
2678 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2680 /* Start functions for struct config_item_type target_core_alua_cit */
2683 * target_core_alua_cit is a ConfigFS group that lives under
2684 * /sys/kernel/config/target/core/alua. There are default groups
2685 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2686 * target_core_alua_cit in target_core_init_configfs() below.
2688 static struct config_item_type target_core_alua_cit = {
2689 .ct_item_ops = NULL,
2691 .ct_owner = THIS_MODULE,
2694 /* End functions for struct config_item_type target_core_alua_cit */
2696 /* Start functions for struct config_item_type target_core_stat_cit */
2698 static struct config_group *target_core_stat_mkdir(
2699 struct config_group *group,
2702 return ERR_PTR(-ENOSYS);
2705 static void target_core_stat_rmdir(
2706 struct config_group *group,
2707 struct config_item *item)
2712 static struct configfs_group_operations target_core_stat_group_ops = {
2713 .make_group = &target_core_stat_mkdir,
2714 .drop_item = &target_core_stat_rmdir,
2717 static struct config_item_type target_core_stat_cit = {
2718 .ct_group_ops = &target_core_stat_group_ops,
2719 .ct_owner = THIS_MODULE,
2722 /* End functions for struct config_item_type target_core_stat_cit */
2724 /* Start functions for struct config_item_type target_core_hba_cit */
2726 static struct config_group *target_core_make_subdev(
2727 struct config_group *group,
2730 struct t10_alua_tg_pt_gp *tg_pt_gp;
2731 struct se_subsystem_dev *se_dev;
2732 struct se_subsystem_api *t;
2733 struct config_item *hba_ci = &group->cg_item;
2734 struct se_hba *hba = item_to_hba(hba_ci);
2735 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2736 struct config_group *dev_stat_grp = NULL;
2737 int errno = -ENOMEM, ret;
2739 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2741 return ERR_PTR(ret);
2743 * Locate the struct se_subsystem_api from parent's struct se_hba.
2747 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2749 printk(KERN_ERR "Unable to allocate memory for"
2750 " struct se_subsystem_dev\n");
2753 INIT_LIST_HEAD(&se_dev->se_dev_node);
2754 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2755 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2756 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
2757 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
2758 spin_lock_init(&se_dev->t10_pr.registration_lock);
2759 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
2760 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2761 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2762 spin_lock_init(&se_dev->se_dev_lock);
2763 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2764 se_dev->t10_wwn.t10_sub_dev = se_dev;
2765 se_dev->t10_alua.t10_sub_dev = se_dev;
2766 se_dev->se_dev_attrib.da_sub_dev = se_dev;
2768 se_dev->se_dev_hba = hba;
2769 dev_cg = &se_dev->se_dev_group;
2771 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2773 if (!(dev_cg->default_groups))
2776 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2777 * for ->allocate_virtdevice()
2779 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2780 * has been called successfully in the next level up in the
2781 * configfs tree for device object's struct config_group.
2783 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2784 if (!(se_dev->se_dev_su_ptr)) {
2785 printk(KERN_ERR "Unable to locate subsystem dependent pointer"
2786 " from allocate_virtdevice()\n");
2789 spin_lock(&se_device_lock);
2790 list_add_tail(&se_dev->se_dev_node, &se_dev_list);
2791 spin_unlock(&se_device_lock);
2793 config_group_init_type_name(&se_dev->se_dev_group, name,
2794 &target_core_dev_cit);
2795 config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2796 &target_core_dev_attrib_cit);
2797 config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2798 &target_core_dev_pr_cit);
2799 config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2800 &target_core_dev_wwn_cit);
2801 config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2802 "alua", &target_core_alua_tg_pt_gps_cit);
2803 config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2804 "statistics", &target_core_stat_cit);
2806 dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2807 dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2808 dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2809 dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2810 dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2811 dev_cg->default_groups[5] = NULL;
2813 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2815 tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2819 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2820 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2822 if (!(tg_pt_gp_cg->default_groups)) {
2823 printk(KERN_ERR "Unable to allocate tg_pt_gp_cg->"
2824 "default_groups\n");
2828 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2829 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2830 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2831 tg_pt_gp_cg->default_groups[1] = NULL;
2832 se_dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2834 * Add core/$HBA/$DEV/statistics/ default groups
2836 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2837 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2839 if (!dev_stat_grp->default_groups) {
2840 printk(KERN_ERR "Unable to allocate dev_stat_grp->default_groups\n");
2843 target_stat_setup_dev_default_groups(se_dev);
2845 printk(KERN_INFO "Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2846 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2848 mutex_unlock(&hba->hba_access_mutex);
2849 return &se_dev->se_dev_group;
2851 if (se_dev->t10_alua.default_tg_pt_gp) {
2852 core_alua_free_tg_pt_gp(se_dev->t10_alua.default_tg_pt_gp);
2853 se_dev->t10_alua.default_tg_pt_gp = NULL;
2856 kfree(dev_stat_grp->default_groups);
2858 kfree(tg_pt_gp_cg->default_groups);
2860 kfree(dev_cg->default_groups);
2861 if (se_dev->se_dev_su_ptr)
2862 t->free_device(se_dev->se_dev_su_ptr);
2865 mutex_unlock(&hba->hba_access_mutex);
2866 return ERR_PTR(errno);
2869 static void target_core_drop_subdev(
2870 struct config_group *group,
2871 struct config_item *item)
2873 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2874 struct se_subsystem_dev, se_dev_group);
2876 struct se_subsystem_api *t;
2877 struct config_item *df_item;
2878 struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2881 hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2883 mutex_lock(&hba->hba_access_mutex);
2886 spin_lock(&se_device_lock);
2887 list_del(&se_dev->se_dev_node);
2888 spin_unlock(&se_device_lock);
2890 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2891 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2892 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2893 dev_stat_grp->default_groups[i] = NULL;
2894 config_item_put(df_item);
2896 kfree(dev_stat_grp->default_groups);
2898 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2899 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2900 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2901 tg_pt_gp_cg->default_groups[i] = NULL;
2902 config_item_put(df_item);
2904 kfree(tg_pt_gp_cg->default_groups);
2906 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2907 * directly from target_core_alua_tg_pt_gp_release().
2909 se_dev->t10_alua.default_tg_pt_gp = NULL;
2911 dev_cg = &se_dev->se_dev_group;
2912 for (i = 0; dev_cg->default_groups[i]; i++) {
2913 df_item = &dev_cg->default_groups[i]->cg_item;
2914 dev_cg->default_groups[i] = NULL;
2915 config_item_put(df_item);
2918 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2919 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2921 config_item_put(item);
2922 mutex_unlock(&hba->hba_access_mutex);
2925 static struct configfs_group_operations target_core_hba_group_ops = {
2926 .make_group = target_core_make_subdev,
2927 .drop_item = target_core_drop_subdev,
2930 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2931 #define SE_HBA_ATTR(_name, _mode) \
2932 static struct target_core_hba_attribute \
2933 target_core_hba_##_name = \
2934 __CONFIGFS_EATTR(_name, _mode, \
2935 target_core_hba_show_attr_##_name, \
2936 target_core_hba_store_attr_##_name);
2938 #define SE_HBA_ATTR_RO(_name) \
2939 static struct target_core_hba_attribute \
2940 target_core_hba_##_name = \
2941 __CONFIGFS_EATTR_RO(_name, \
2942 target_core_hba_show_attr_##_name);
2944 static ssize_t target_core_hba_show_attr_hba_info(
2948 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2949 hba->hba_id, hba->transport->name,
2950 TARGET_CORE_CONFIGFS_VERSION);
2953 SE_HBA_ATTR_RO(hba_info);
2955 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2960 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2963 return sprintf(page, "%d\n", hba_mode);
2966 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2967 const char *page, size_t count)
2969 struct se_subsystem_api *transport = hba->transport;
2970 unsigned long mode_flag;
2973 if (transport->pmode_enable_hba == NULL)
2976 ret = strict_strtoul(page, 0, &mode_flag);
2978 printk(KERN_ERR "Unable to extract hba mode flag: %d\n", ret);
2982 spin_lock(&hba->device_lock);
2983 if (!(list_empty(&hba->hba_dev_list))) {
2984 printk(KERN_ERR "Unable to set hba_mode with active devices\n");
2985 spin_unlock(&hba->device_lock);
2988 spin_unlock(&hba->device_lock);
2990 ret = transport->pmode_enable_hba(hba, mode_flag);
2994 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2996 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3001 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3003 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3005 static void target_core_hba_release(struct config_item *item)
3007 struct se_hba *hba = container_of(to_config_group(item),
3008 struct se_hba, hba_group);
3009 core_delete_hba(hba);
3012 static struct configfs_attribute *target_core_hba_attrs[] = {
3013 &target_core_hba_hba_info.attr,
3014 &target_core_hba_hba_mode.attr,
3018 static struct configfs_item_operations target_core_hba_item_ops = {
3019 .release = target_core_hba_release,
3020 .show_attribute = target_core_hba_attr_show,
3021 .store_attribute = target_core_hba_attr_store,
3024 static struct config_item_type target_core_hba_cit = {
3025 .ct_item_ops = &target_core_hba_item_ops,
3026 .ct_group_ops = &target_core_hba_group_ops,
3027 .ct_attrs = target_core_hba_attrs,
3028 .ct_owner = THIS_MODULE,
3031 static struct config_group *target_core_call_addhbatotarget(
3032 struct config_group *group,
3035 char *se_plugin_str, *str, *str2;
3037 char buf[TARGET_CORE_NAME_MAX_LEN];
3038 unsigned long plugin_dep_id = 0;
3041 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3042 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3043 printk(KERN_ERR "Passed *name strlen(): %d exceeds"
3044 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3045 TARGET_CORE_NAME_MAX_LEN);
3046 return ERR_PTR(-ENAMETOOLONG);
3048 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3050 str = strstr(buf, "_");
3052 printk(KERN_ERR "Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3053 return ERR_PTR(-EINVAL);
3055 se_plugin_str = buf;
3057 * Special case for subsystem plugins that have "_" in their names.
3058 * Namely rd_direct and rd_mcp..
3060 str2 = strstr(str+1, "_");
3062 *str2 = '\0'; /* Terminate for *se_plugin_str */
3063 str2++; /* Skip to start of plugin dependent ID */
3066 *str = '\0'; /* Terminate for *se_plugin_str */
3067 str++; /* Skip to start of plugin dependent ID */
3070 ret = strict_strtoul(str, 0, &plugin_dep_id);
3072 printk(KERN_ERR "strict_strtoul() returned %d for"
3073 " plugin_dep_id\n", ret);
3074 return ERR_PTR(-EINVAL);
3077 * Load up TCM subsystem plugins if they have not already been loaded.
3079 if (transport_subsystem_check_init() < 0)
3080 return ERR_PTR(-EINVAL);
3082 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3084 return ERR_CAST(hba);
3086 config_group_init_type_name(&hba->hba_group, name,
3087 &target_core_hba_cit);
3089 return &hba->hba_group;
3092 static void target_core_call_delhbafromtarget(
3093 struct config_group *group,
3094 struct config_item *item)
3097 * core_delete_hba() is called from target_core_hba_item_ops->release()
3098 * -> target_core_hba_release()
3100 config_item_put(item);
3103 static struct configfs_group_operations target_core_group_ops = {
3104 .make_group = target_core_call_addhbatotarget,
3105 .drop_item = target_core_call_delhbafromtarget,
3108 static struct config_item_type target_core_cit = {
3109 .ct_item_ops = NULL,
3110 .ct_group_ops = &target_core_group_ops,
3112 .ct_owner = THIS_MODULE,
3115 /* Stop functions for struct config_item_type target_core_hba_cit */
3117 static int __init target_core_init_configfs(void)
3119 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3120 struct config_group *lu_gp_cg = NULL;
3121 struct configfs_subsystem *subsys;
3122 struct t10_alua_lu_gp *lu_gp;
3125 printk(KERN_INFO "TARGET_CORE[0]: Loading Generic Kernel Storage"
3126 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3127 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3129 subsys = target_core_subsystem[0];
3130 config_group_init(&subsys->su_group);
3131 mutex_init(&subsys->su_mutex);
3133 INIT_LIST_HEAD(&g_tf_list);
3134 mutex_init(&g_tf_lock);
3135 ret = init_se_kmem_caches();
3139 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3140 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3142 target_cg = &subsys->su_group;
3143 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3145 if (!(target_cg->default_groups)) {
3146 printk(KERN_ERR "Unable to allocate target_cg->default_groups\n");
3150 config_group_init_type_name(&target_core_hbagroup,
3151 "core", &target_core_cit);
3152 target_cg->default_groups[0] = &target_core_hbagroup;
3153 target_cg->default_groups[1] = NULL;
3155 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3157 hba_cg = &target_core_hbagroup;
3158 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3160 if (!(hba_cg->default_groups)) {
3161 printk(KERN_ERR "Unable to allocate hba_cg->default_groups\n");
3164 config_group_init_type_name(&alua_group,
3165 "alua", &target_core_alua_cit);
3166 hba_cg->default_groups[0] = &alua_group;
3167 hba_cg->default_groups[1] = NULL;
3169 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3170 * groups under /sys/kernel/config/target/core/alua/
3172 alua_cg = &alua_group;
3173 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3175 if (!(alua_cg->default_groups)) {
3176 printk(KERN_ERR "Unable to allocate alua_cg->default_groups\n");
3180 config_group_init_type_name(&alua_lu_gps_group,
3181 "lu_gps", &target_core_alua_lu_gps_cit);
3182 alua_cg->default_groups[0] = &alua_lu_gps_group;
3183 alua_cg->default_groups[1] = NULL;
3185 * Add core/alua/lu_gps/default_lu_gp
3187 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3191 lu_gp_cg = &alua_lu_gps_group;
3192 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3194 if (!(lu_gp_cg->default_groups)) {
3195 printk(KERN_ERR "Unable to allocate lu_gp_cg->default_groups\n");
3199 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3200 &target_core_alua_lu_gp_cit);
3201 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3202 lu_gp_cg->default_groups[1] = NULL;
3203 default_lu_gp = lu_gp;
3205 * Register the target_core_mod subsystem with configfs.
3207 ret = configfs_register_subsystem(subsys);
3209 printk(KERN_ERR "Error %d while registering subsystem %s\n",
3210 ret, subsys->su_group.cg_item.ci_namebuf);
3213 printk(KERN_INFO "TARGET_CORE[0]: Initialized ConfigFS Fabric"
3214 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3215 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3217 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3219 ret = rd_module_init();
3223 if (core_dev_setup_virtual_lun0() < 0)
3229 configfs_unregister_subsystem(subsys);
3230 core_dev_release_virtual_lun0();
3233 if (default_lu_gp) {
3234 core_alua_free_lu_gp(default_lu_gp);
3235 default_lu_gp = NULL;
3238 kfree(lu_gp_cg->default_groups);
3240 kfree(alua_cg->default_groups);
3242 kfree(hba_cg->default_groups);
3243 kfree(target_cg->default_groups);
3244 release_se_kmem_caches();
3248 static void __exit target_core_exit_configfs(void)
3250 struct configfs_subsystem *subsys;
3251 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3252 struct config_item *item;
3255 subsys = target_core_subsystem[0];
3257 lu_gp_cg = &alua_lu_gps_group;
3258 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3259 item = &lu_gp_cg->default_groups[i]->cg_item;
3260 lu_gp_cg->default_groups[i] = NULL;
3261 config_item_put(item);
3263 kfree(lu_gp_cg->default_groups);
3264 lu_gp_cg->default_groups = NULL;
3266 alua_cg = &alua_group;
3267 for (i = 0; alua_cg->default_groups[i]; i++) {
3268 item = &alua_cg->default_groups[i]->cg_item;
3269 alua_cg->default_groups[i] = NULL;
3270 config_item_put(item);
3272 kfree(alua_cg->default_groups);
3273 alua_cg->default_groups = NULL;
3275 hba_cg = &target_core_hbagroup;
3276 for (i = 0; hba_cg->default_groups[i]; i++) {
3277 item = &hba_cg->default_groups[i]->cg_item;
3278 hba_cg->default_groups[i] = NULL;
3279 config_item_put(item);
3281 kfree(hba_cg->default_groups);
3282 hba_cg->default_groups = NULL;
3284 * We expect subsys->su_group.default_groups to be released
3285 * by configfs subsystem provider logic..
3287 configfs_unregister_subsystem(subsys);
3288 kfree(subsys->su_group.default_groups);
3290 core_alua_free_lu_gp(default_lu_gp);
3291 default_lu_gp = NULL;
3293 printk(KERN_INFO "TARGET_CORE[0]: Released ConfigFS Fabric"
3294 " Infrastructure\n");
3296 core_dev_release_virtual_lun0();
3298 release_se_kmem_caches();
3301 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3302 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3303 MODULE_LICENSE("GPL");
3305 module_init(target_core_init_configfs);
3306 module_exit(target_core_exit_configfs);