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_to_pool)) {
431 printk(KERN_ERR "Missing tfo->release_cmd_to_pool()\n");
434 if (!(tfo->release_cmd_direct)) {
435 printk(KERN_ERR "Missing tfo->release_cmd_direct()\n");
438 if (!(tfo->shutdown_session)) {
439 printk(KERN_ERR "Missing tfo->shutdown_session()\n");
442 if (!(tfo->close_session)) {
443 printk(KERN_ERR "Missing tfo->close_session()\n");
446 if (!(tfo->stop_session)) {
447 printk(KERN_ERR "Missing tfo->stop_session()\n");
450 if (!(tfo->fall_back_to_erl0)) {
451 printk(KERN_ERR "Missing tfo->fall_back_to_erl0()\n");
454 if (!(tfo->sess_logged_in)) {
455 printk(KERN_ERR "Missing tfo->sess_logged_in()\n");
458 if (!(tfo->sess_get_index)) {
459 printk(KERN_ERR "Missing tfo->sess_get_index()\n");
462 if (!(tfo->write_pending)) {
463 printk(KERN_ERR "Missing tfo->write_pending()\n");
466 if (!(tfo->write_pending_status)) {
467 printk(KERN_ERR "Missing tfo->write_pending_status()\n");
470 if (!(tfo->set_default_node_attributes)) {
471 printk(KERN_ERR "Missing tfo->set_default_node_attributes()\n");
474 if (!(tfo->get_task_tag)) {
475 printk(KERN_ERR "Missing tfo->get_task_tag()\n");
478 if (!(tfo->get_cmd_state)) {
479 printk(KERN_ERR "Missing tfo->get_cmd_state()\n");
482 if (!(tfo->new_cmd_failure)) {
483 printk(KERN_ERR "Missing tfo->new_cmd_failure()\n");
486 if (!(tfo->queue_data_in)) {
487 printk(KERN_ERR "Missing tfo->queue_data_in()\n");
490 if (!(tfo->queue_status)) {
491 printk(KERN_ERR "Missing tfo->queue_status()\n");
494 if (!(tfo->queue_tm_rsp)) {
495 printk(KERN_ERR "Missing tfo->queue_tm_rsp()\n");
498 if (!(tfo->set_fabric_sense_len)) {
499 printk(KERN_ERR "Missing tfo->set_fabric_sense_len()\n");
502 if (!(tfo->get_fabric_sense_len)) {
503 printk(KERN_ERR "Missing tfo->get_fabric_sense_len()\n");
506 if (!(tfo->is_state_remove)) {
507 printk(KERN_ERR "Missing tfo->is_state_remove()\n");
511 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
512 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
513 * target_core_fabric_configfs.c WWN+TPG group context code.
515 if (!(tfo->fabric_make_wwn)) {
516 printk(KERN_ERR "Missing tfo->fabric_make_wwn()\n");
519 if (!(tfo->fabric_drop_wwn)) {
520 printk(KERN_ERR "Missing tfo->fabric_drop_wwn()\n");
523 if (!(tfo->fabric_make_tpg)) {
524 printk(KERN_ERR "Missing tfo->fabric_make_tpg()\n");
527 if (!(tfo->fabric_drop_tpg)) {
528 printk(KERN_ERR "Missing tfo->fabric_drop_tpg()\n");
536 * Called 2nd from fabric module with returned parameter of
537 * struct target_fabric_configfs * from target_fabric_configfs_init().
539 * Upon a successful registration, the new fabric's struct config_item is
540 * return. Also, a pointer to this struct is set in the passed
541 * struct target_fabric_configfs.
543 int target_fabric_configfs_register(
544 struct target_fabric_configfs *tf)
549 printk(KERN_ERR "Unable to locate target_fabric_configfs"
553 if (!(tf->tf_subsys)) {
554 printk(KERN_ERR "Unable to target struct config_subsystem"
558 ret = target_fabric_tf_ops_check(tf);
562 printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
566 EXPORT_SYMBOL(target_fabric_configfs_register);
568 void target_fabric_configfs_deregister(
569 struct target_fabric_configfs *tf)
571 struct configfs_subsystem *su;
574 printk(KERN_ERR "Unable to locate passed target_fabric_"
580 printk(KERN_ERR "Unable to locate passed tf->tf_subsys"
584 printk(KERN_INFO "<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
586 mutex_lock(&g_tf_lock);
587 if (atomic_read(&tf->tf_access_cnt)) {
588 mutex_unlock(&g_tf_lock);
589 printk(KERN_ERR "Non zero tf->tf_access_cnt for fabric %s\n",
593 list_del(&tf->tf_list);
594 mutex_unlock(&g_tf_lock);
596 printk(KERN_INFO "Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
597 " %s\n", tf->tf_name);
598 tf->tf_module = NULL;
599 tf->tf_subsys = NULL;
602 printk("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
605 EXPORT_SYMBOL(target_fabric_configfs_deregister);
607 /*##############################################################################
608 // Stop functions called by external Target Fabrics Modules
609 //############################################################################*/
611 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
613 #define DEF_DEV_ATTRIB_SHOW(_name) \
614 static ssize_t target_core_dev_show_attr_##_name( \
615 struct se_dev_attrib *da, \
618 struct se_device *dev; \
619 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
622 spin_lock(&se_dev->se_dev_lock); \
623 dev = se_dev->se_dev_ptr; \
625 spin_unlock(&se_dev->se_dev_lock); \
628 rb = snprintf(page, PAGE_SIZE, "%u\n", \
629 (u32)dev->se_sub_dev->se_dev_attrib._name); \
630 spin_unlock(&se_dev->se_dev_lock); \
635 #define DEF_DEV_ATTRIB_STORE(_name) \
636 static ssize_t target_core_dev_store_attr_##_name( \
637 struct se_dev_attrib *da, \
641 struct se_device *dev; \
642 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
646 spin_lock(&se_dev->se_dev_lock); \
647 dev = se_dev->se_dev_ptr; \
649 spin_unlock(&se_dev->se_dev_lock); \
652 ret = strict_strtoul(page, 0, &val); \
654 spin_unlock(&se_dev->se_dev_lock); \
655 printk(KERN_ERR "strict_strtoul() failed with" \
656 " ret: %d\n", ret); \
659 ret = se_dev_set_##_name(dev, (u32)val); \
660 spin_unlock(&se_dev->se_dev_lock); \
662 return (!ret) ? count : -EINVAL; \
665 #define DEF_DEV_ATTRIB(_name) \
666 DEF_DEV_ATTRIB_SHOW(_name); \
667 DEF_DEV_ATTRIB_STORE(_name);
669 #define DEF_DEV_ATTRIB_RO(_name) \
670 DEF_DEV_ATTRIB_SHOW(_name);
672 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
673 #define SE_DEV_ATTR(_name, _mode) \
674 static struct target_core_dev_attrib_attribute \
675 target_core_dev_attrib_##_name = \
676 __CONFIGFS_EATTR(_name, _mode, \
677 target_core_dev_show_attr_##_name, \
678 target_core_dev_store_attr_##_name);
680 #define SE_DEV_ATTR_RO(_name); \
681 static struct target_core_dev_attrib_attribute \
682 target_core_dev_attrib_##_name = \
683 __CONFIGFS_EATTR_RO(_name, \
684 target_core_dev_show_attr_##_name);
686 DEF_DEV_ATTRIB(emulate_dpo);
687 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
689 DEF_DEV_ATTRIB(emulate_fua_write);
690 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
692 DEF_DEV_ATTRIB(emulate_fua_read);
693 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
695 DEF_DEV_ATTRIB(emulate_write_cache);
696 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
698 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
699 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
701 DEF_DEV_ATTRIB(emulate_tas);
702 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
704 DEF_DEV_ATTRIB(emulate_tpu);
705 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
707 DEF_DEV_ATTRIB(emulate_tpws);
708 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
710 DEF_DEV_ATTRIB(enforce_pr_isids);
711 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
713 DEF_DEV_ATTRIB_RO(hw_block_size);
714 SE_DEV_ATTR_RO(hw_block_size);
716 DEF_DEV_ATTRIB(block_size);
717 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
719 DEF_DEV_ATTRIB_RO(hw_max_sectors);
720 SE_DEV_ATTR_RO(hw_max_sectors);
722 DEF_DEV_ATTRIB(max_sectors);
723 SE_DEV_ATTR(max_sectors, S_IRUGO | S_IWUSR);
725 DEF_DEV_ATTRIB(optimal_sectors);
726 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
728 DEF_DEV_ATTRIB_RO(hw_queue_depth);
729 SE_DEV_ATTR_RO(hw_queue_depth);
731 DEF_DEV_ATTRIB(queue_depth);
732 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
734 DEF_DEV_ATTRIB(task_timeout);
735 SE_DEV_ATTR(task_timeout, S_IRUGO | S_IWUSR);
737 DEF_DEV_ATTRIB(max_unmap_lba_count);
738 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
740 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
741 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
743 DEF_DEV_ATTRIB(unmap_granularity);
744 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
746 DEF_DEV_ATTRIB(unmap_granularity_alignment);
747 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
749 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
751 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
752 &target_core_dev_attrib_emulate_dpo.attr,
753 &target_core_dev_attrib_emulate_fua_write.attr,
754 &target_core_dev_attrib_emulate_fua_read.attr,
755 &target_core_dev_attrib_emulate_write_cache.attr,
756 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
757 &target_core_dev_attrib_emulate_tas.attr,
758 &target_core_dev_attrib_emulate_tpu.attr,
759 &target_core_dev_attrib_emulate_tpws.attr,
760 &target_core_dev_attrib_enforce_pr_isids.attr,
761 &target_core_dev_attrib_hw_block_size.attr,
762 &target_core_dev_attrib_block_size.attr,
763 &target_core_dev_attrib_hw_max_sectors.attr,
764 &target_core_dev_attrib_max_sectors.attr,
765 &target_core_dev_attrib_optimal_sectors.attr,
766 &target_core_dev_attrib_hw_queue_depth.attr,
767 &target_core_dev_attrib_queue_depth.attr,
768 &target_core_dev_attrib_task_timeout.attr,
769 &target_core_dev_attrib_max_unmap_lba_count.attr,
770 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
771 &target_core_dev_attrib_unmap_granularity.attr,
772 &target_core_dev_attrib_unmap_granularity_alignment.attr,
776 static struct configfs_item_operations target_core_dev_attrib_ops = {
777 .show_attribute = target_core_dev_attrib_attr_show,
778 .store_attribute = target_core_dev_attrib_attr_store,
781 static struct config_item_type target_core_dev_attrib_cit = {
782 .ct_item_ops = &target_core_dev_attrib_ops,
783 .ct_attrs = target_core_dev_attrib_attrs,
784 .ct_owner = THIS_MODULE,
787 /* End functions for struct config_item_type target_core_dev_attrib_cit */
789 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
791 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
792 #define SE_DEV_WWN_ATTR(_name, _mode) \
793 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
794 __CONFIGFS_EATTR(_name, _mode, \
795 target_core_dev_wwn_show_attr_##_name, \
796 target_core_dev_wwn_store_attr_##_name);
798 #define SE_DEV_WWN_ATTR_RO(_name); \
800 static struct target_core_dev_wwn_attribute \
801 target_core_dev_wwn_##_name = \
802 __CONFIGFS_EATTR_RO(_name, \
803 target_core_dev_wwn_show_attr_##_name); \
807 * VPD page 0x80 Unit serial
809 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
810 struct t10_wwn *t10_wwn,
813 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
814 struct se_device *dev;
816 dev = se_dev->se_dev_ptr;
820 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
821 &t10_wwn->unit_serial[0]);
824 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
825 struct t10_wwn *t10_wwn,
829 struct se_subsystem_dev *su_dev = t10_wwn->t10_sub_dev;
830 struct se_device *dev;
831 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
834 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
835 * from the struct scsi_device level firmware, do not allow
836 * VPD Unit Serial to be emulated.
838 * Note this struct scsi_device could also be emulating VPD
839 * information from its drivers/scsi LLD. But for now we assume
840 * it is doing 'the right thing' wrt a world wide unique
841 * VPD Unit Serial Number that OS dependent multipath can depend on.
843 if (su_dev->su_dev_flags & SDF_FIRMWARE_VPD_UNIT_SERIAL) {
844 printk(KERN_ERR "Underlying SCSI device firmware provided VPD"
845 " Unit Serial, ignoring request\n");
849 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
850 printk(KERN_ERR "Emulated VPD Unit Serial exceeds"
851 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
855 * Check to see if any active $FABRIC_MOD exports exist. If they
856 * do exist, fail here as changing this information on the fly
857 * (underneath the initiator side OS dependent multipath code)
858 * could cause negative effects.
860 dev = su_dev->se_dev_ptr;
862 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
863 printk(KERN_ERR "Unable to set VPD Unit Serial while"
864 " active %d $FABRIC_MOD exports exist\n",
865 atomic_read(&dev->dev_export_obj.obj_access_count));
870 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
872 * Also, strip any newline added from the userspace
873 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
875 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
876 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
877 snprintf(su_dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
878 "%s", strstrip(buf));
879 su_dev->su_dev_flags |= SDF_EMULATED_VPD_UNIT_SERIAL;
881 printk(KERN_INFO "Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
882 " %s\n", su_dev->t10_wwn.unit_serial);
887 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
890 * VPD page 0x83 Protocol Identifier
892 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
893 struct t10_wwn *t10_wwn,
896 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
897 struct se_device *dev;
899 unsigned char buf[VPD_TMP_BUF_SIZE];
902 dev = se_dev->se_dev_ptr;
906 memset(buf, 0, VPD_TMP_BUF_SIZE);
908 spin_lock(&t10_wwn->t10_vpd_lock);
909 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
910 if (!(vpd->protocol_identifier_set))
913 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
915 if ((len + strlen(buf) >= PAGE_SIZE))
918 len += sprintf(page+len, "%s", buf);
920 spin_unlock(&t10_wwn->t10_vpd_lock);
925 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
926 struct t10_wwn *t10_wwn,
933 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
936 * Generic wrapper for dumping VPD identifiers by association.
938 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
939 static ssize_t target_core_dev_wwn_show_attr_##_name( \
940 struct t10_wwn *t10_wwn, \
943 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev; \
944 struct se_device *dev; \
945 struct t10_vpd *vpd; \
946 unsigned char buf[VPD_TMP_BUF_SIZE]; \
949 dev = se_dev->se_dev_ptr; \
953 spin_lock(&t10_wwn->t10_vpd_lock); \
954 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
955 if (vpd->association != _assoc) \
958 memset(buf, 0, VPD_TMP_BUF_SIZE); \
959 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
960 if ((len + strlen(buf) >= PAGE_SIZE)) \
962 len += sprintf(page+len, "%s", buf); \
964 memset(buf, 0, VPD_TMP_BUF_SIZE); \
965 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
966 if ((len + strlen(buf) >= PAGE_SIZE)) \
968 len += sprintf(page+len, "%s", buf); \
970 memset(buf, 0, VPD_TMP_BUF_SIZE); \
971 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
972 if ((len + strlen(buf) >= PAGE_SIZE)) \
974 len += sprintf(page+len, "%s", buf); \
976 spin_unlock(&t10_wwn->t10_vpd_lock); \
982 * VPD page 0x83 Assoication: Logical Unit
984 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
986 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
987 struct t10_wwn *t10_wwn,
994 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
997 * VPD page 0x83 Association: Target Port
999 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1001 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
1002 struct t10_wwn *t10_wwn,
1009 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
1012 * VPD page 0x83 Association: SCSI Target Device
1014 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1016 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
1017 struct t10_wwn *t10_wwn,
1024 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1026 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1028 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1029 &target_core_dev_wwn_vpd_unit_serial.attr,
1030 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1031 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1032 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1033 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1037 static struct configfs_item_operations target_core_dev_wwn_ops = {
1038 .show_attribute = target_core_dev_wwn_attr_show,
1039 .store_attribute = target_core_dev_wwn_attr_store,
1042 static struct config_item_type target_core_dev_wwn_cit = {
1043 .ct_item_ops = &target_core_dev_wwn_ops,
1044 .ct_attrs = target_core_dev_wwn_attrs,
1045 .ct_owner = THIS_MODULE,
1048 /* End functions for struct config_item_type target_core_dev_wwn_cit */
1050 /* Start functions for struct config_item_type target_core_dev_pr_cit */
1052 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_subsystem_dev);
1053 #define SE_DEV_PR_ATTR(_name, _mode) \
1054 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1055 __CONFIGFS_EATTR(_name, _mode, \
1056 target_core_dev_pr_show_attr_##_name, \
1057 target_core_dev_pr_store_attr_##_name);
1059 #define SE_DEV_PR_ATTR_RO(_name); \
1060 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1061 __CONFIGFS_EATTR_RO(_name, \
1062 target_core_dev_pr_show_attr_##_name);
1067 static ssize_t target_core_dev_pr_show_spc3_res(
1068 struct se_device *dev,
1072 struct se_node_acl *se_nacl;
1073 struct t10_pr_registration *pr_reg;
1074 char i_buf[PR_REG_ISID_ID_LEN];
1077 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1079 spin_lock(&dev->dev_reservation_lock);
1080 pr_reg = dev->dev_pr_res_holder;
1082 *len += sprintf(page + *len, "No SPC-3 Reservation holder\n");
1083 spin_unlock(&dev->dev_reservation_lock);
1086 se_nacl = pr_reg->pr_reg_nacl;
1087 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1088 PR_REG_ISID_ID_LEN);
1090 *len += sprintf(page + *len, "SPC-3 Reservation: %s Initiator: %s%s\n",
1091 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1092 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1093 spin_unlock(&dev->dev_reservation_lock);
1098 static ssize_t target_core_dev_pr_show_spc2_res(
1099 struct se_device *dev,
1103 struct se_node_acl *se_nacl;
1105 spin_lock(&dev->dev_reservation_lock);
1106 se_nacl = dev->dev_reserved_node_acl;
1108 *len += sprintf(page + *len, "No SPC-2 Reservation holder\n");
1109 spin_unlock(&dev->dev_reservation_lock);
1112 *len += sprintf(page + *len, "SPC-2 Reservation: %s Initiator: %s\n",
1113 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1114 se_nacl->initiatorname);
1115 spin_unlock(&dev->dev_reservation_lock);
1120 static ssize_t target_core_dev_pr_show_attr_res_holder(
1121 struct se_subsystem_dev *su_dev,
1126 if (!(su_dev->se_dev_ptr))
1129 switch (su_dev->t10_pr.res_type) {
1130 case SPC3_PERSISTENT_RESERVATIONS:
1131 target_core_dev_pr_show_spc3_res(su_dev->se_dev_ptr,
1134 case SPC2_RESERVATIONS:
1135 target_core_dev_pr_show_spc2_res(su_dev->se_dev_ptr,
1138 case SPC_PASSTHROUGH:
1139 len += sprintf(page+len, "Passthrough\n");
1142 len += sprintf(page+len, "Unknown\n");
1149 SE_DEV_PR_ATTR_RO(res_holder);
1152 * res_pr_all_tgt_pts
1154 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1155 struct se_subsystem_dev *su_dev,
1158 struct se_device *dev;
1159 struct t10_pr_registration *pr_reg;
1162 dev = su_dev->se_dev_ptr;
1166 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1169 spin_lock(&dev->dev_reservation_lock);
1170 pr_reg = dev->dev_pr_res_holder;
1172 len = sprintf(page, "No SPC-3 Reservation holder\n");
1173 spin_unlock(&dev->dev_reservation_lock);
1177 * See All Target Ports (ALL_TG_PT) bit in spcr17, section 6.14.3
1178 * Basic PERSISTENT RESERVER OUT parameter list, page 290
1180 if (pr_reg->pr_reg_all_tg_pt)
1181 len = sprintf(page, "SPC-3 Reservation: All Target"
1182 " Ports registration\n");
1184 len = sprintf(page, "SPC-3 Reservation: Single"
1185 " Target Port registration\n");
1186 spin_unlock(&dev->dev_reservation_lock);
1191 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1196 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1197 struct se_subsystem_dev *su_dev,
1200 if (!(su_dev->se_dev_ptr))
1203 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1206 return sprintf(page, "0x%08x\n", su_dev->t10_pr.pr_generation);
1209 SE_DEV_PR_ATTR_RO(res_pr_generation);
1212 * res_pr_holder_tg_port
1214 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1215 struct se_subsystem_dev *su_dev,
1218 struct se_device *dev;
1219 struct se_node_acl *se_nacl;
1221 struct se_portal_group *se_tpg;
1222 struct t10_pr_registration *pr_reg;
1223 struct target_core_fabric_ops *tfo;
1226 dev = su_dev->se_dev_ptr;
1230 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1233 spin_lock(&dev->dev_reservation_lock);
1234 pr_reg = dev->dev_pr_res_holder;
1236 len = sprintf(page, "No SPC-3 Reservation holder\n");
1237 spin_unlock(&dev->dev_reservation_lock);
1240 se_nacl = pr_reg->pr_reg_nacl;
1241 se_tpg = se_nacl->se_tpg;
1242 lun = pr_reg->pr_reg_tg_pt_lun;
1243 tfo = se_tpg->se_tpg_tfo;
1245 len += sprintf(page+len, "SPC-3 Reservation: %s"
1246 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1247 tfo->tpg_get_wwn(se_tpg));
1248 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1249 " Identifer Tag: %hu %s Portal Group Tag: %hu"
1250 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1251 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1252 tfo->get_fabric_name(), lun->unpacked_lun);
1253 spin_unlock(&dev->dev_reservation_lock);
1258 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1261 * res_pr_registered_i_pts
1263 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1264 struct se_subsystem_dev *su_dev,
1267 struct target_core_fabric_ops *tfo;
1268 struct t10_pr_registration *pr_reg;
1269 unsigned char buf[384];
1270 char i_buf[PR_REG_ISID_ID_LEN];
1272 int reg_count = 0, prf_isid;
1274 if (!(su_dev->se_dev_ptr))
1277 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1280 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1282 spin_lock(&su_dev->t10_pr.registration_lock);
1283 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1286 memset(buf, 0, 384);
1287 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1288 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1289 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1290 PR_REG_ISID_ID_LEN);
1291 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1292 tfo->get_fabric_name(),
1293 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1294 &i_buf[0] : "", pr_reg->pr_res_key,
1295 pr_reg->pr_res_generation);
1297 if ((len + strlen(buf) >= PAGE_SIZE))
1300 len += sprintf(page+len, "%s", buf);
1303 spin_unlock(&su_dev->t10_pr.registration_lock);
1306 len += sprintf(page+len, "None\n");
1311 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1316 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1317 struct se_subsystem_dev *su_dev,
1320 struct se_device *dev;
1321 struct t10_pr_registration *pr_reg;
1324 dev = su_dev->se_dev_ptr;
1328 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1331 spin_lock(&dev->dev_reservation_lock);
1332 pr_reg = dev->dev_pr_res_holder;
1334 len = sprintf(page, "No SPC-3 Reservation holder\n");
1335 spin_unlock(&dev->dev_reservation_lock);
1338 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1339 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1340 spin_unlock(&dev->dev_reservation_lock);
1345 SE_DEV_PR_ATTR_RO(res_pr_type);
1350 static ssize_t target_core_dev_pr_show_attr_res_type(
1351 struct se_subsystem_dev *su_dev,
1356 if (!(su_dev->se_dev_ptr))
1359 switch (su_dev->t10_pr.res_type) {
1360 case SPC3_PERSISTENT_RESERVATIONS:
1361 len = sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1363 case SPC2_RESERVATIONS:
1364 len = sprintf(page, "SPC2_RESERVATIONS\n");
1366 case SPC_PASSTHROUGH:
1367 len = sprintf(page, "SPC_PASSTHROUGH\n");
1370 len = sprintf(page, "UNKNOWN\n");
1377 SE_DEV_PR_ATTR_RO(res_type);
1383 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1384 struct se_subsystem_dev *su_dev,
1387 if (!(su_dev->se_dev_ptr))
1390 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1393 return sprintf(page, "APTPL Bit Status: %s\n",
1394 (su_dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1397 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1400 * res_aptpl_metadata
1402 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1403 struct se_subsystem_dev *su_dev,
1406 if (!(su_dev->se_dev_ptr))
1409 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1412 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1416 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1417 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1418 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1419 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1422 static match_table_t tokens = {
1423 {Opt_initiator_fabric, "initiator_fabric=%s"},
1424 {Opt_initiator_node, "initiator_node=%s"},
1425 {Opt_initiator_sid, "initiator_sid=%s"},
1426 {Opt_sa_res_key, "sa_res_key=%s"},
1427 {Opt_res_holder, "res_holder=%d"},
1428 {Opt_res_type, "res_type=%d"},
1429 {Opt_res_scope, "res_scope=%d"},
1430 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1431 {Opt_mapped_lun, "mapped_lun=%d"},
1432 {Opt_target_fabric, "target_fabric=%s"},
1433 {Opt_target_node, "target_node=%s"},
1434 {Opt_tpgt, "tpgt=%d"},
1435 {Opt_port_rtpi, "port_rtpi=%d"},
1436 {Opt_target_lun, "target_lun=%d"},
1440 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1441 struct se_subsystem_dev *su_dev,
1445 struct se_device *dev;
1446 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1447 unsigned char *t_fabric = NULL, *t_port = NULL;
1448 char *orig, *ptr, *arg_p, *opts;
1449 substring_t args[MAX_OPT_ARGS];
1450 unsigned long long tmp_ll;
1452 u32 mapped_lun = 0, target_lun = 0;
1453 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1454 u16 port_rpti = 0, tpgt = 0;
1457 dev = su_dev->se_dev_ptr;
1461 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1464 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1465 printk(KERN_INFO "Unable to process APTPL metadata while"
1466 " active fabric exports exist\n");
1470 opts = kstrdup(page, GFP_KERNEL);
1475 while ((ptr = strsep(&opts, ",")) != NULL) {
1479 token = match_token(ptr, tokens, args);
1481 case Opt_initiator_fabric:
1482 i_fabric = match_strdup(&args[0]);
1488 case Opt_initiator_node:
1489 i_port = match_strdup(&args[0]);
1494 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1495 printk(KERN_ERR "APTPL metadata initiator_node="
1496 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1497 PR_APTPL_MAX_IPORT_LEN);
1502 case Opt_initiator_sid:
1503 isid = match_strdup(&args[0]);
1508 if (strlen(isid) >= PR_REG_ISID_LEN) {
1509 printk(KERN_ERR "APTPL metadata initiator_isid"
1510 "= exceeds PR_REG_ISID_LEN: %d\n",
1516 case Opt_sa_res_key:
1517 arg_p = match_strdup(&args[0]);
1522 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1524 printk(KERN_ERR "strict_strtoull() failed for"
1528 sa_res_key = (u64)tmp_ll;
1531 * PR APTPL Metadata for Reservation
1533 case Opt_res_holder:
1534 match_int(args, &arg);
1538 match_int(args, &arg);
1542 match_int(args, &arg);
1545 case Opt_res_all_tg_pt:
1546 match_int(args, &arg);
1547 all_tg_pt = (int)arg;
1549 case Opt_mapped_lun:
1550 match_int(args, &arg);
1551 mapped_lun = (u32)arg;
1554 * PR APTPL Metadata for Target Port
1556 case Opt_target_fabric:
1557 t_fabric = match_strdup(&args[0]);
1563 case Opt_target_node:
1564 t_port = match_strdup(&args[0]);
1569 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1570 printk(KERN_ERR "APTPL metadata target_node="
1571 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1572 PR_APTPL_MAX_TPORT_LEN);
1578 match_int(args, &arg);
1582 match_int(args, &arg);
1583 port_rpti = (u16)arg;
1585 case Opt_target_lun:
1586 match_int(args, &arg);
1587 target_lun = (u32)arg;
1594 if (!(i_port) || !(t_port) || !(sa_res_key)) {
1595 printk(KERN_ERR "Illegal parameters for APTPL registration\n");
1600 if (res_holder && !(type)) {
1601 printk(KERN_ERR "Illegal PR type: 0x%02x for reservation"
1607 ret = core_scsi3_alloc_aptpl_registration(&su_dev->t10_pr, sa_res_key,
1608 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1609 res_holder, all_tg_pt, type);
1617 return (ret == 0) ? count : ret;
1620 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1622 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1624 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1625 &target_core_dev_pr_res_holder.attr,
1626 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1627 &target_core_dev_pr_res_pr_generation.attr,
1628 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1629 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1630 &target_core_dev_pr_res_pr_type.attr,
1631 &target_core_dev_pr_res_type.attr,
1632 &target_core_dev_pr_res_aptpl_active.attr,
1633 &target_core_dev_pr_res_aptpl_metadata.attr,
1637 static struct configfs_item_operations target_core_dev_pr_ops = {
1638 .show_attribute = target_core_dev_pr_attr_show,
1639 .store_attribute = target_core_dev_pr_attr_store,
1642 static struct config_item_type target_core_dev_pr_cit = {
1643 .ct_item_ops = &target_core_dev_pr_ops,
1644 .ct_attrs = target_core_dev_pr_attrs,
1645 .ct_owner = THIS_MODULE,
1648 /* End functions for struct config_item_type target_core_dev_pr_cit */
1650 /* Start functions for struct config_item_type target_core_dev_cit */
1652 static ssize_t target_core_show_dev_info(void *p, char *page)
1654 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1655 struct se_hba *hba = se_dev->se_dev_hba;
1656 struct se_subsystem_api *t = hba->transport;
1658 ssize_t read_bytes = 0;
1660 if (!(se_dev->se_dev_ptr))
1663 transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1665 read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1669 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1670 .attr = { .ca_owner = THIS_MODULE,
1672 .ca_mode = S_IRUGO },
1673 .show = target_core_show_dev_info,
1677 static ssize_t target_core_store_dev_control(
1682 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1683 struct se_hba *hba = se_dev->se_dev_hba;
1684 struct se_subsystem_api *t = hba->transport;
1686 if (!(se_dev->se_dev_su_ptr)) {
1687 printk(KERN_ERR "Unable to locate struct se_subsystem_dev>se"
1692 return t->set_configfs_dev_params(hba, se_dev, page, count);
1695 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1696 .attr = { .ca_owner = THIS_MODULE,
1697 .ca_name = "control",
1698 .ca_mode = S_IWUSR },
1700 .store = target_core_store_dev_control,
1703 static ssize_t target_core_show_dev_alias(void *p, char *page)
1705 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1707 if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1710 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1713 static ssize_t target_core_store_dev_alias(
1718 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1719 struct se_hba *hba = se_dev->se_dev_hba;
1722 if (count > (SE_DEV_ALIAS_LEN-1)) {
1723 printk(KERN_ERR "alias count: %d exceeds"
1724 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1725 SE_DEV_ALIAS_LEN-1);
1729 se_dev->su_dev_flags |= SDF_USING_ALIAS;
1730 read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1733 printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set alias: %s\n",
1734 config_item_name(&hba->hba_group.cg_item),
1735 config_item_name(&se_dev->se_dev_group.cg_item),
1736 se_dev->se_dev_alias);
1741 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1742 .attr = { .ca_owner = THIS_MODULE,
1744 .ca_mode = S_IRUGO | S_IWUSR },
1745 .show = target_core_show_dev_alias,
1746 .store = target_core_store_dev_alias,
1749 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1751 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1753 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH))
1756 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_udev_path);
1759 static ssize_t target_core_store_dev_udev_path(
1764 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1765 struct se_hba *hba = se_dev->se_dev_hba;
1768 if (count > (SE_UDEV_PATH_LEN-1)) {
1769 printk(KERN_ERR "udev_path count: %d exceeds"
1770 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1771 SE_UDEV_PATH_LEN-1);
1775 se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1776 read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1779 printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1780 config_item_name(&hba->hba_group.cg_item),
1781 config_item_name(&se_dev->se_dev_group.cg_item),
1782 se_dev->se_dev_udev_path);
1787 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1788 .attr = { .ca_owner = THIS_MODULE,
1789 .ca_name = "udev_path",
1790 .ca_mode = S_IRUGO | S_IWUSR },
1791 .show = target_core_show_dev_udev_path,
1792 .store = target_core_store_dev_udev_path,
1795 static ssize_t target_core_store_dev_enable(
1800 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1801 struct se_device *dev;
1802 struct se_hba *hba = se_dev->se_dev_hba;
1803 struct se_subsystem_api *t = hba->transport;
1806 ptr = strstr(page, "1");
1808 printk(KERN_ERR "For dev_enable ops, only valid value"
1812 if ((se_dev->se_dev_ptr)) {
1813 printk(KERN_ERR "se_dev->se_dev_ptr already set for storage"
1818 if (t->check_configfs_dev_params(hba, se_dev) < 0)
1821 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1823 return PTR_ERR(dev);
1827 se_dev->se_dev_ptr = dev;
1828 printk(KERN_INFO "Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1829 " %p\n", se_dev->se_dev_ptr);
1834 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1835 .attr = { .ca_owner = THIS_MODULE,
1836 .ca_name = "enable",
1837 .ca_mode = S_IWUSR },
1839 .store = target_core_store_dev_enable,
1842 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1844 struct se_device *dev;
1845 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1846 struct config_item *lu_ci;
1847 struct t10_alua_lu_gp *lu_gp;
1848 struct t10_alua_lu_gp_member *lu_gp_mem;
1851 dev = su_dev->se_dev_ptr;
1855 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED)
1858 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1860 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1865 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1866 lu_gp = lu_gp_mem->lu_gp;
1868 lu_ci = &lu_gp->lu_gp_group.cg_item;
1869 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1870 config_item_name(lu_ci), lu_gp->lu_gp_id);
1872 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1877 static ssize_t target_core_store_alua_lu_gp(
1882 struct se_device *dev;
1883 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1884 struct se_hba *hba = su_dev->se_dev_hba;
1885 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1886 struct t10_alua_lu_gp_member *lu_gp_mem;
1887 unsigned char buf[LU_GROUP_NAME_BUF];
1890 dev = su_dev->se_dev_ptr;
1894 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1895 printk(KERN_WARNING "SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1896 config_item_name(&hba->hba_group.cg_item),
1897 config_item_name(&su_dev->se_dev_group.cg_item));
1900 if (count > LU_GROUP_NAME_BUF) {
1901 printk(KERN_ERR "ALUA LU Group Alias too large!\n");
1904 memset(buf, 0, LU_GROUP_NAME_BUF);
1905 memcpy(buf, page, count);
1907 * Any ALUA logical unit alias besides "NULL" means we will be
1908 * making a new group association.
1910 if (strcmp(strstrip(buf), "NULL")) {
1912 * core_alua_get_lu_gp_by_name() will increment reference to
1913 * struct t10_alua_lu_gp. This reference is released with
1914 * core_alua_get_lu_gp_by_name below().
1916 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1920 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1923 core_alua_put_lu_gp_from_name(lu_gp_new);
1924 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1929 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1930 lu_gp = lu_gp_mem->lu_gp;
1933 * Clearing an existing lu_gp association, and replacing
1937 printk(KERN_INFO "Target_Core_ConfigFS: Releasing %s/%s"
1938 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1940 config_item_name(&hba->hba_group.cg_item),
1941 config_item_name(&su_dev->se_dev_group.cg_item),
1942 config_item_name(&lu_gp->lu_gp_group.cg_item),
1945 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1946 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1951 * Removing existing association of lu_gp_mem with lu_gp
1953 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1957 * Associate lu_gp_mem with lu_gp_new.
1959 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1960 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1962 printk(KERN_INFO "Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1963 " core/alua/lu_gps/%s, ID: %hu\n",
1964 (move) ? "Moving" : "Adding",
1965 config_item_name(&hba->hba_group.cg_item),
1966 config_item_name(&su_dev->se_dev_group.cg_item),
1967 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1968 lu_gp_new->lu_gp_id);
1970 core_alua_put_lu_gp_from_name(lu_gp_new);
1974 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1975 .attr = { .ca_owner = THIS_MODULE,
1976 .ca_name = "alua_lu_gp",
1977 .ca_mode = S_IRUGO | S_IWUSR },
1978 .show = target_core_show_alua_lu_gp,
1979 .store = target_core_store_alua_lu_gp,
1982 static struct configfs_attribute *lio_core_dev_attrs[] = {
1983 &target_core_attr_dev_info.attr,
1984 &target_core_attr_dev_control.attr,
1985 &target_core_attr_dev_alias.attr,
1986 &target_core_attr_dev_udev_path.attr,
1987 &target_core_attr_dev_enable.attr,
1988 &target_core_attr_dev_alua_lu_gp.attr,
1992 static void target_core_dev_release(struct config_item *item)
1994 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
1995 struct se_subsystem_dev, se_dev_group);
1996 struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
1997 struct se_subsystem_api *t = hba->transport;
1998 struct config_group *dev_cg = &se_dev->se_dev_group;
2000 kfree(dev_cg->default_groups);
2002 * This pointer will set when the storage is enabled with:
2003 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
2005 if (se_dev->se_dev_ptr) {
2006 printk(KERN_INFO "Target_Core_ConfigFS: Calling se_free_"
2007 "virtual_device() for se_dev_ptr: %p\n",
2008 se_dev->se_dev_ptr);
2010 se_free_virtual_device(se_dev->se_dev_ptr, hba);
2013 * Release struct se_subsystem_dev->se_dev_su_ptr..
2015 printk(KERN_INFO "Target_Core_ConfigFS: Calling t->free_"
2016 "device() for se_dev_su_ptr: %p\n",
2017 se_dev->se_dev_su_ptr);
2019 t->free_device(se_dev->se_dev_su_ptr);
2022 printk(KERN_INFO "Target_Core_ConfigFS: Deallocating se_subsystem"
2023 "_dev_t: %p\n", se_dev);
2027 static ssize_t target_core_dev_show(struct config_item *item,
2028 struct configfs_attribute *attr,
2031 struct se_subsystem_dev *se_dev = container_of(
2032 to_config_group(item), struct se_subsystem_dev,
2034 struct target_core_configfs_attribute *tc_attr = container_of(
2035 attr, struct target_core_configfs_attribute, attr);
2037 if (!(tc_attr->show))
2040 return tc_attr->show((void *)se_dev, page);
2043 static ssize_t target_core_dev_store(struct config_item *item,
2044 struct configfs_attribute *attr,
2045 const char *page, size_t count)
2047 struct se_subsystem_dev *se_dev = container_of(
2048 to_config_group(item), struct se_subsystem_dev,
2050 struct target_core_configfs_attribute *tc_attr = container_of(
2051 attr, struct target_core_configfs_attribute, attr);
2053 if (!(tc_attr->store))
2056 return tc_attr->store((void *)se_dev, page, count);
2059 static struct configfs_item_operations target_core_dev_item_ops = {
2060 .release = target_core_dev_release,
2061 .show_attribute = target_core_dev_show,
2062 .store_attribute = target_core_dev_store,
2065 static struct config_item_type target_core_dev_cit = {
2066 .ct_item_ops = &target_core_dev_item_ops,
2067 .ct_attrs = lio_core_dev_attrs,
2068 .ct_owner = THIS_MODULE,
2071 /* End functions for struct config_item_type target_core_dev_cit */
2073 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2075 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2076 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2077 static struct target_core_alua_lu_gp_attribute \
2078 target_core_alua_lu_gp_##_name = \
2079 __CONFIGFS_EATTR(_name, _mode, \
2080 target_core_alua_lu_gp_show_attr_##_name, \
2081 target_core_alua_lu_gp_store_attr_##_name);
2083 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2084 static struct target_core_alua_lu_gp_attribute \
2085 target_core_alua_lu_gp_##_name = \
2086 __CONFIGFS_EATTR_RO(_name, \
2087 target_core_alua_lu_gp_show_attr_##_name);
2092 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2093 struct t10_alua_lu_gp *lu_gp,
2096 if (!(lu_gp->lu_gp_valid_id))
2099 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2102 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2103 struct t10_alua_lu_gp *lu_gp,
2107 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2108 unsigned long lu_gp_id;
2111 ret = strict_strtoul(page, 0, &lu_gp_id);
2113 printk(KERN_ERR "strict_strtoul() returned %d for"
2114 " lu_gp_id\n", ret);
2117 if (lu_gp_id > 0x0000ffff) {
2118 printk(KERN_ERR "ALUA lu_gp_id: %lu exceeds maximum:"
2119 " 0x0000ffff\n", lu_gp_id);
2123 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2127 printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Logical Unit"
2128 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2129 config_item_name(&alua_lu_gp_cg->cg_item),
2135 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2140 static ssize_t target_core_alua_lu_gp_show_attr_members(
2141 struct t10_alua_lu_gp *lu_gp,
2144 struct se_device *dev;
2146 struct se_subsystem_dev *su_dev;
2147 struct t10_alua_lu_gp_member *lu_gp_mem;
2148 ssize_t len = 0, cur_len;
2149 unsigned char buf[LU_GROUP_NAME_BUF];
2151 memset(buf, 0, LU_GROUP_NAME_BUF);
2153 spin_lock(&lu_gp->lu_gp_lock);
2154 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2155 dev = lu_gp_mem->lu_gp_mem_dev;
2156 su_dev = dev->se_sub_dev;
2157 hba = su_dev->se_dev_hba;
2159 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2160 config_item_name(&hba->hba_group.cg_item),
2161 config_item_name(&su_dev->se_dev_group.cg_item));
2162 cur_len++; /* Extra byte for NULL terminator */
2164 if ((cur_len + len) > PAGE_SIZE) {
2165 printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2166 "_members buffer\n");
2169 memcpy(page+len, buf, cur_len);
2172 spin_unlock(&lu_gp->lu_gp_lock);
2177 SE_DEV_ALUA_LU_ATTR_RO(members);
2179 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2181 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2182 &target_core_alua_lu_gp_lu_gp_id.attr,
2183 &target_core_alua_lu_gp_members.attr,
2187 static void target_core_alua_lu_gp_release(struct config_item *item)
2189 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2190 struct t10_alua_lu_gp, lu_gp_group);
2192 core_alua_free_lu_gp(lu_gp);
2195 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2196 .release = target_core_alua_lu_gp_release,
2197 .show_attribute = target_core_alua_lu_gp_attr_show,
2198 .store_attribute = target_core_alua_lu_gp_attr_store,
2201 static struct config_item_type target_core_alua_lu_gp_cit = {
2202 .ct_item_ops = &target_core_alua_lu_gp_ops,
2203 .ct_attrs = target_core_alua_lu_gp_attrs,
2204 .ct_owner = THIS_MODULE,
2207 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2209 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2211 static struct config_group *target_core_alua_create_lu_gp(
2212 struct config_group *group,
2215 struct t10_alua_lu_gp *lu_gp;
2216 struct config_group *alua_lu_gp_cg = NULL;
2217 struct config_item *alua_lu_gp_ci = NULL;
2219 lu_gp = core_alua_allocate_lu_gp(name, 0);
2223 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2224 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2226 config_group_init_type_name(alua_lu_gp_cg, name,
2227 &target_core_alua_lu_gp_cit);
2229 printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2230 " Group: core/alua/lu_gps/%s\n",
2231 config_item_name(alua_lu_gp_ci));
2233 return alua_lu_gp_cg;
2237 static void target_core_alua_drop_lu_gp(
2238 struct config_group *group,
2239 struct config_item *item)
2241 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2242 struct t10_alua_lu_gp, lu_gp_group);
2244 printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2245 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2246 config_item_name(item), lu_gp->lu_gp_id);
2248 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2249 * -> target_core_alua_lu_gp_release()
2251 config_item_put(item);
2254 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2255 .make_group = &target_core_alua_create_lu_gp,
2256 .drop_item = &target_core_alua_drop_lu_gp,
2259 static struct config_item_type target_core_alua_lu_gps_cit = {
2260 .ct_item_ops = NULL,
2261 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2262 .ct_owner = THIS_MODULE,
2265 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2267 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2269 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2270 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2271 static struct target_core_alua_tg_pt_gp_attribute \
2272 target_core_alua_tg_pt_gp_##_name = \
2273 __CONFIGFS_EATTR(_name, _mode, \
2274 target_core_alua_tg_pt_gp_show_attr_##_name, \
2275 target_core_alua_tg_pt_gp_store_attr_##_name);
2277 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2278 static struct target_core_alua_tg_pt_gp_attribute \
2279 target_core_alua_tg_pt_gp_##_name = \
2280 __CONFIGFS_EATTR_RO(_name, \
2281 target_core_alua_tg_pt_gp_show_attr_##_name);
2286 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2287 struct t10_alua_tg_pt_gp *tg_pt_gp,
2290 return sprintf(page, "%d\n",
2291 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2294 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2295 struct t10_alua_tg_pt_gp *tg_pt_gp,
2299 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2303 if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2304 printk(KERN_ERR "Unable to do implict ALUA on non valid"
2305 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2309 ret = strict_strtoul(page, 0, &tmp);
2311 printk("Unable to extract new ALUA access state from"
2315 new_state = (int)tmp;
2317 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2318 printk(KERN_ERR "Unable to process implict configfs ALUA"
2319 " transition while TPGS_IMPLICT_ALUA is diabled\n");
2323 ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2324 NULL, NULL, new_state, 0);
2325 return (!ret) ? count : -EINVAL;
2328 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2331 * alua_access_status
2333 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2334 struct t10_alua_tg_pt_gp *tg_pt_gp,
2337 return sprintf(page, "%s\n",
2338 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2341 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2342 struct t10_alua_tg_pt_gp *tg_pt_gp,
2347 int new_status, ret;
2349 if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2350 printk(KERN_ERR "Unable to do set ALUA access status on non"
2351 " valid tg_pt_gp ID: %hu\n",
2352 tg_pt_gp->tg_pt_gp_valid_id);
2356 ret = strict_strtoul(page, 0, &tmp);
2358 printk(KERN_ERR "Unable to extract new ALUA access status"
2359 " from %s\n", page);
2362 new_status = (int)tmp;
2364 if ((new_status != ALUA_STATUS_NONE) &&
2365 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2366 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2367 printk(KERN_ERR "Illegal ALUA access status: 0x%02x\n",
2372 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2376 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2381 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2382 struct t10_alua_tg_pt_gp *tg_pt_gp,
2385 return core_alua_show_access_type(tg_pt_gp, page);
2388 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2389 struct t10_alua_tg_pt_gp *tg_pt_gp,
2393 return core_alua_store_access_type(tg_pt_gp, page, count);
2396 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2399 * alua_write_metadata
2401 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2402 struct t10_alua_tg_pt_gp *tg_pt_gp,
2405 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2408 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2409 struct t10_alua_tg_pt_gp *tg_pt_gp,
2416 ret = strict_strtoul(page, 0, &tmp);
2418 printk(KERN_ERR "Unable to extract alua_write_metadata\n");
2422 if ((tmp != 0) && (tmp != 1)) {
2423 printk(KERN_ERR "Illegal value for alua_write_metadata:"
2427 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2432 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2439 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2440 struct t10_alua_tg_pt_gp *tg_pt_gp,
2443 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2447 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2448 struct t10_alua_tg_pt_gp *tg_pt_gp,
2452 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2455 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2460 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2461 struct t10_alua_tg_pt_gp *tg_pt_gp,
2464 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2467 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2468 struct t10_alua_tg_pt_gp *tg_pt_gp,
2472 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2475 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2481 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2482 struct t10_alua_tg_pt_gp *tg_pt_gp,
2485 return core_alua_show_preferred_bit(tg_pt_gp, page);
2488 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2489 struct t10_alua_tg_pt_gp *tg_pt_gp,
2493 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2496 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2501 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2502 struct t10_alua_tg_pt_gp *tg_pt_gp,
2505 if (!(tg_pt_gp->tg_pt_gp_valid_id))
2508 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2511 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2512 struct t10_alua_tg_pt_gp *tg_pt_gp,
2516 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2517 unsigned long tg_pt_gp_id;
2520 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2522 printk(KERN_ERR "strict_strtoul() returned %d for"
2523 " tg_pt_gp_id\n", ret);
2526 if (tg_pt_gp_id > 0x0000ffff) {
2527 printk(KERN_ERR "ALUA tg_pt_gp_id: %lu exceeds maximum:"
2528 " 0x0000ffff\n", tg_pt_gp_id);
2532 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2536 printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Target Port Group: "
2537 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2538 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2539 tg_pt_gp->tg_pt_gp_id);
2544 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2549 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2550 struct t10_alua_tg_pt_gp *tg_pt_gp,
2553 struct se_port *port;
2554 struct se_portal_group *tpg;
2556 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2557 ssize_t len = 0, cur_len;
2558 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2560 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2562 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2563 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2564 tg_pt_gp_mem_list) {
2565 port = tg_pt_gp_mem->tg_pt;
2566 tpg = port->sep_tpg;
2567 lun = port->sep_lun;
2569 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2570 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2571 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2572 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2573 config_item_name(&lun->lun_group.cg_item));
2574 cur_len++; /* Extra byte for NULL terminator */
2576 if ((cur_len + len) > PAGE_SIZE) {
2577 printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2578 "_members buffer\n");
2581 memcpy(page+len, buf, cur_len);
2584 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2589 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2591 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2594 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2595 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2596 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2597 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2598 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2599 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2600 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2601 &target_core_alua_tg_pt_gp_preferred.attr,
2602 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2603 &target_core_alua_tg_pt_gp_members.attr,
2607 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2609 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2610 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2612 core_alua_free_tg_pt_gp(tg_pt_gp);
2615 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2616 .release = target_core_alua_tg_pt_gp_release,
2617 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2618 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2621 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2622 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2623 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2624 .ct_owner = THIS_MODULE,
2627 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2629 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2631 static struct config_group *target_core_alua_create_tg_pt_gp(
2632 struct config_group *group,
2635 struct t10_alua *alua = container_of(group, struct t10_alua,
2636 alua_tg_pt_gps_group);
2637 struct t10_alua_tg_pt_gp *tg_pt_gp;
2638 struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2639 struct config_group *alua_tg_pt_gp_cg = NULL;
2640 struct config_item *alua_tg_pt_gp_ci = NULL;
2642 tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2646 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2647 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2649 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2650 &target_core_alua_tg_pt_gp_cit);
2652 printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Target Port"
2653 " Group: alua/tg_pt_gps/%s\n",
2654 config_item_name(alua_tg_pt_gp_ci));
2656 return alua_tg_pt_gp_cg;
2659 static void target_core_alua_drop_tg_pt_gp(
2660 struct config_group *group,
2661 struct config_item *item)
2663 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2664 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2666 printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Target Port"
2667 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2668 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2670 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2671 * -> target_core_alua_tg_pt_gp_release().
2673 config_item_put(item);
2676 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2677 .make_group = &target_core_alua_create_tg_pt_gp,
2678 .drop_item = &target_core_alua_drop_tg_pt_gp,
2681 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2682 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2683 .ct_owner = THIS_MODULE,
2686 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2688 /* Start functions for struct config_item_type target_core_alua_cit */
2691 * target_core_alua_cit is a ConfigFS group that lives under
2692 * /sys/kernel/config/target/core/alua. There are default groups
2693 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2694 * target_core_alua_cit in target_core_init_configfs() below.
2696 static struct config_item_type target_core_alua_cit = {
2697 .ct_item_ops = NULL,
2699 .ct_owner = THIS_MODULE,
2702 /* End functions for struct config_item_type target_core_alua_cit */
2704 /* Start functions for struct config_item_type target_core_stat_cit */
2706 static struct config_group *target_core_stat_mkdir(
2707 struct config_group *group,
2710 return ERR_PTR(-ENOSYS);
2713 static void target_core_stat_rmdir(
2714 struct config_group *group,
2715 struct config_item *item)
2720 static struct configfs_group_operations target_core_stat_group_ops = {
2721 .make_group = &target_core_stat_mkdir,
2722 .drop_item = &target_core_stat_rmdir,
2725 static struct config_item_type target_core_stat_cit = {
2726 .ct_group_ops = &target_core_stat_group_ops,
2727 .ct_owner = THIS_MODULE,
2730 /* End functions for struct config_item_type target_core_stat_cit */
2732 /* Start functions for struct config_item_type target_core_hba_cit */
2734 static struct config_group *target_core_make_subdev(
2735 struct config_group *group,
2738 struct t10_alua_tg_pt_gp *tg_pt_gp;
2739 struct se_subsystem_dev *se_dev;
2740 struct se_subsystem_api *t;
2741 struct config_item *hba_ci = &group->cg_item;
2742 struct se_hba *hba = item_to_hba(hba_ci);
2743 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2744 struct config_group *dev_stat_grp = NULL;
2745 int errno = -ENOMEM, ret;
2747 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2749 return ERR_PTR(ret);
2751 * Locate the struct se_subsystem_api from parent's struct se_hba.
2755 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2757 printk(KERN_ERR "Unable to allocate memory for"
2758 " struct se_subsystem_dev\n");
2761 INIT_LIST_HEAD(&se_dev->se_dev_node);
2762 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2763 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2764 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
2765 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
2766 spin_lock_init(&se_dev->t10_pr.registration_lock);
2767 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
2768 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2769 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2770 spin_lock_init(&se_dev->se_dev_lock);
2771 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2772 se_dev->t10_wwn.t10_sub_dev = se_dev;
2773 se_dev->t10_alua.t10_sub_dev = se_dev;
2774 se_dev->se_dev_attrib.da_sub_dev = se_dev;
2776 se_dev->se_dev_hba = hba;
2777 dev_cg = &se_dev->se_dev_group;
2779 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2781 if (!(dev_cg->default_groups))
2784 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2785 * for ->allocate_virtdevice()
2787 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2788 * has been called successfully in the next level up in the
2789 * configfs tree for device object's struct config_group.
2791 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2792 if (!(se_dev->se_dev_su_ptr)) {
2793 printk(KERN_ERR "Unable to locate subsystem dependent pointer"
2794 " from allocate_virtdevice()\n");
2797 spin_lock(&se_device_lock);
2798 list_add_tail(&se_dev->se_dev_node, &se_dev_list);
2799 spin_unlock(&se_device_lock);
2801 config_group_init_type_name(&se_dev->se_dev_group, name,
2802 &target_core_dev_cit);
2803 config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2804 &target_core_dev_attrib_cit);
2805 config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2806 &target_core_dev_pr_cit);
2807 config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2808 &target_core_dev_wwn_cit);
2809 config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2810 "alua", &target_core_alua_tg_pt_gps_cit);
2811 config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2812 "statistics", &target_core_stat_cit);
2814 dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2815 dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2816 dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2817 dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2818 dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2819 dev_cg->default_groups[5] = NULL;
2821 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2823 tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2827 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2828 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2830 if (!(tg_pt_gp_cg->default_groups)) {
2831 printk(KERN_ERR "Unable to allocate tg_pt_gp_cg->"
2832 "default_groups\n");
2836 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2837 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2838 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2839 tg_pt_gp_cg->default_groups[1] = NULL;
2840 se_dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2842 * Add core/$HBA/$DEV/statistics/ default groups
2844 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2845 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2847 if (!dev_stat_grp->default_groups) {
2848 printk(KERN_ERR "Unable to allocate dev_stat_grp->default_groups\n");
2851 target_stat_setup_dev_default_groups(se_dev);
2853 printk(KERN_INFO "Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2854 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2856 mutex_unlock(&hba->hba_access_mutex);
2857 return &se_dev->se_dev_group;
2859 if (se_dev->t10_alua.default_tg_pt_gp) {
2860 core_alua_free_tg_pt_gp(se_dev->t10_alua.default_tg_pt_gp);
2861 se_dev->t10_alua.default_tg_pt_gp = NULL;
2864 kfree(dev_stat_grp->default_groups);
2866 kfree(tg_pt_gp_cg->default_groups);
2868 kfree(dev_cg->default_groups);
2869 if (se_dev->se_dev_su_ptr)
2870 t->free_device(se_dev->se_dev_su_ptr);
2873 mutex_unlock(&hba->hba_access_mutex);
2874 return ERR_PTR(errno);
2877 static void target_core_drop_subdev(
2878 struct config_group *group,
2879 struct config_item *item)
2881 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2882 struct se_subsystem_dev, se_dev_group);
2884 struct se_subsystem_api *t;
2885 struct config_item *df_item;
2886 struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2889 hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2891 mutex_lock(&hba->hba_access_mutex);
2894 spin_lock(&se_device_lock);
2895 list_del(&se_dev->se_dev_node);
2896 spin_unlock(&se_device_lock);
2898 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2899 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2900 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2901 dev_stat_grp->default_groups[i] = NULL;
2902 config_item_put(df_item);
2904 kfree(dev_stat_grp->default_groups);
2906 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2907 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2908 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2909 tg_pt_gp_cg->default_groups[i] = NULL;
2910 config_item_put(df_item);
2912 kfree(tg_pt_gp_cg->default_groups);
2914 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2915 * directly from target_core_alua_tg_pt_gp_release().
2917 se_dev->t10_alua.default_tg_pt_gp = NULL;
2919 dev_cg = &se_dev->se_dev_group;
2920 for (i = 0; dev_cg->default_groups[i]; i++) {
2921 df_item = &dev_cg->default_groups[i]->cg_item;
2922 dev_cg->default_groups[i] = NULL;
2923 config_item_put(df_item);
2926 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2927 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2929 config_item_put(item);
2930 mutex_unlock(&hba->hba_access_mutex);
2933 static struct configfs_group_operations target_core_hba_group_ops = {
2934 .make_group = target_core_make_subdev,
2935 .drop_item = target_core_drop_subdev,
2938 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2939 #define SE_HBA_ATTR(_name, _mode) \
2940 static struct target_core_hba_attribute \
2941 target_core_hba_##_name = \
2942 __CONFIGFS_EATTR(_name, _mode, \
2943 target_core_hba_show_attr_##_name, \
2944 target_core_hba_store_attr_##_name);
2946 #define SE_HBA_ATTR_RO(_name) \
2947 static struct target_core_hba_attribute \
2948 target_core_hba_##_name = \
2949 __CONFIGFS_EATTR_RO(_name, \
2950 target_core_hba_show_attr_##_name);
2952 static ssize_t target_core_hba_show_attr_hba_info(
2956 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2957 hba->hba_id, hba->transport->name,
2958 TARGET_CORE_CONFIGFS_VERSION);
2961 SE_HBA_ATTR_RO(hba_info);
2963 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2968 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2971 return sprintf(page, "%d\n", hba_mode);
2974 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2975 const char *page, size_t count)
2977 struct se_subsystem_api *transport = hba->transport;
2978 unsigned long mode_flag;
2981 if (transport->pmode_enable_hba == NULL)
2984 ret = strict_strtoul(page, 0, &mode_flag);
2986 printk(KERN_ERR "Unable to extract hba mode flag: %d\n", ret);
2990 spin_lock(&hba->device_lock);
2991 if (!(list_empty(&hba->hba_dev_list))) {
2992 printk(KERN_ERR "Unable to set hba_mode with active devices\n");
2993 spin_unlock(&hba->device_lock);
2996 spin_unlock(&hba->device_lock);
2998 ret = transport->pmode_enable_hba(hba, mode_flag);
3002 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3004 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3009 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3011 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3013 static void target_core_hba_release(struct config_item *item)
3015 struct se_hba *hba = container_of(to_config_group(item),
3016 struct se_hba, hba_group);
3017 core_delete_hba(hba);
3020 static struct configfs_attribute *target_core_hba_attrs[] = {
3021 &target_core_hba_hba_info.attr,
3022 &target_core_hba_hba_mode.attr,
3026 static struct configfs_item_operations target_core_hba_item_ops = {
3027 .release = target_core_hba_release,
3028 .show_attribute = target_core_hba_attr_show,
3029 .store_attribute = target_core_hba_attr_store,
3032 static struct config_item_type target_core_hba_cit = {
3033 .ct_item_ops = &target_core_hba_item_ops,
3034 .ct_group_ops = &target_core_hba_group_ops,
3035 .ct_attrs = target_core_hba_attrs,
3036 .ct_owner = THIS_MODULE,
3039 static struct config_group *target_core_call_addhbatotarget(
3040 struct config_group *group,
3043 char *se_plugin_str, *str, *str2;
3045 char buf[TARGET_CORE_NAME_MAX_LEN];
3046 unsigned long plugin_dep_id = 0;
3049 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3050 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3051 printk(KERN_ERR "Passed *name strlen(): %d exceeds"
3052 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3053 TARGET_CORE_NAME_MAX_LEN);
3054 return ERR_PTR(-ENAMETOOLONG);
3056 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3058 str = strstr(buf, "_");
3060 printk(KERN_ERR "Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3061 return ERR_PTR(-EINVAL);
3063 se_plugin_str = buf;
3065 * Special case for subsystem plugins that have "_" in their names.
3066 * Namely rd_direct and rd_mcp..
3068 str2 = strstr(str+1, "_");
3070 *str2 = '\0'; /* Terminate for *se_plugin_str */
3071 str2++; /* Skip to start of plugin dependent ID */
3074 *str = '\0'; /* Terminate for *se_plugin_str */
3075 str++; /* Skip to start of plugin dependent ID */
3078 ret = strict_strtoul(str, 0, &plugin_dep_id);
3080 printk(KERN_ERR "strict_strtoul() returned %d for"
3081 " plugin_dep_id\n", ret);
3082 return ERR_PTR(-EINVAL);
3085 * Load up TCM subsystem plugins if they have not already been loaded.
3087 if (transport_subsystem_check_init() < 0)
3088 return ERR_PTR(-EINVAL);
3090 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3092 return ERR_CAST(hba);
3094 config_group_init_type_name(&hba->hba_group, name,
3095 &target_core_hba_cit);
3097 return &hba->hba_group;
3100 static void target_core_call_delhbafromtarget(
3101 struct config_group *group,
3102 struct config_item *item)
3105 * core_delete_hba() is called from target_core_hba_item_ops->release()
3106 * -> target_core_hba_release()
3108 config_item_put(item);
3111 static struct configfs_group_operations target_core_group_ops = {
3112 .make_group = target_core_call_addhbatotarget,
3113 .drop_item = target_core_call_delhbafromtarget,
3116 static struct config_item_type target_core_cit = {
3117 .ct_item_ops = NULL,
3118 .ct_group_ops = &target_core_group_ops,
3120 .ct_owner = THIS_MODULE,
3123 /* Stop functions for struct config_item_type target_core_hba_cit */
3125 static int __init target_core_init_configfs(void)
3127 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3128 struct config_group *lu_gp_cg = NULL;
3129 struct configfs_subsystem *subsys;
3130 struct t10_alua_lu_gp *lu_gp;
3133 printk(KERN_INFO "TARGET_CORE[0]: Loading Generic Kernel Storage"
3134 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3135 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3137 subsys = target_core_subsystem[0];
3138 config_group_init(&subsys->su_group);
3139 mutex_init(&subsys->su_mutex);
3141 INIT_LIST_HEAD(&g_tf_list);
3142 mutex_init(&g_tf_lock);
3143 ret = init_se_kmem_caches();
3147 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3148 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3150 target_cg = &subsys->su_group;
3151 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3153 if (!(target_cg->default_groups)) {
3154 printk(KERN_ERR "Unable to allocate target_cg->default_groups\n");
3158 config_group_init_type_name(&target_core_hbagroup,
3159 "core", &target_core_cit);
3160 target_cg->default_groups[0] = &target_core_hbagroup;
3161 target_cg->default_groups[1] = NULL;
3163 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3165 hba_cg = &target_core_hbagroup;
3166 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3168 if (!(hba_cg->default_groups)) {
3169 printk(KERN_ERR "Unable to allocate hba_cg->default_groups\n");
3172 config_group_init_type_name(&alua_group,
3173 "alua", &target_core_alua_cit);
3174 hba_cg->default_groups[0] = &alua_group;
3175 hba_cg->default_groups[1] = NULL;
3177 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3178 * groups under /sys/kernel/config/target/core/alua/
3180 alua_cg = &alua_group;
3181 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3183 if (!(alua_cg->default_groups)) {
3184 printk(KERN_ERR "Unable to allocate alua_cg->default_groups\n");
3188 config_group_init_type_name(&alua_lu_gps_group,
3189 "lu_gps", &target_core_alua_lu_gps_cit);
3190 alua_cg->default_groups[0] = &alua_lu_gps_group;
3191 alua_cg->default_groups[1] = NULL;
3193 * Add core/alua/lu_gps/default_lu_gp
3195 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3199 lu_gp_cg = &alua_lu_gps_group;
3200 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3202 if (!(lu_gp_cg->default_groups)) {
3203 printk(KERN_ERR "Unable to allocate lu_gp_cg->default_groups\n");
3207 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3208 &target_core_alua_lu_gp_cit);
3209 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3210 lu_gp_cg->default_groups[1] = NULL;
3211 default_lu_gp = lu_gp;
3213 * Register the target_core_mod subsystem with configfs.
3215 ret = configfs_register_subsystem(subsys);
3217 printk(KERN_ERR "Error %d while registering subsystem %s\n",
3218 ret, subsys->su_group.cg_item.ci_namebuf);
3221 printk(KERN_INFO "TARGET_CORE[0]: Initialized ConfigFS Fabric"
3222 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3223 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3225 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3227 ret = rd_module_init();
3231 if (core_dev_setup_virtual_lun0() < 0)
3237 configfs_unregister_subsystem(subsys);
3238 core_dev_release_virtual_lun0();
3241 if (default_lu_gp) {
3242 core_alua_free_lu_gp(default_lu_gp);
3243 default_lu_gp = NULL;
3246 kfree(lu_gp_cg->default_groups);
3248 kfree(alua_cg->default_groups);
3250 kfree(hba_cg->default_groups);
3251 kfree(target_cg->default_groups);
3252 release_se_kmem_caches();
3256 static void __exit target_core_exit_configfs(void)
3258 struct configfs_subsystem *subsys;
3259 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3260 struct config_item *item;
3263 subsys = target_core_subsystem[0];
3265 lu_gp_cg = &alua_lu_gps_group;
3266 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3267 item = &lu_gp_cg->default_groups[i]->cg_item;
3268 lu_gp_cg->default_groups[i] = NULL;
3269 config_item_put(item);
3271 kfree(lu_gp_cg->default_groups);
3272 lu_gp_cg->default_groups = NULL;
3274 alua_cg = &alua_group;
3275 for (i = 0; alua_cg->default_groups[i]; i++) {
3276 item = &alua_cg->default_groups[i]->cg_item;
3277 alua_cg->default_groups[i] = NULL;
3278 config_item_put(item);
3280 kfree(alua_cg->default_groups);
3281 alua_cg->default_groups = NULL;
3283 hba_cg = &target_core_hbagroup;
3284 for (i = 0; hba_cg->default_groups[i]; i++) {
3285 item = &hba_cg->default_groups[i]->cg_item;
3286 hba_cg->default_groups[i] = NULL;
3287 config_item_put(item);
3289 kfree(hba_cg->default_groups);
3290 hba_cg->default_groups = NULL;
3292 * We expect subsys->su_group.default_groups to be released
3293 * by configfs subsystem provider logic..
3295 configfs_unregister_subsystem(subsys);
3296 kfree(subsys->su_group.default_groups);
3298 core_alua_free_lu_gp(default_lu_gp);
3299 default_lu_gp = NULL;
3301 printk(KERN_INFO "TARGET_CORE[0]: Released ConfigFS Fabric"
3302 " Infrastructure\n");
3304 core_dev_release_virtual_lun0();
3306 release_se_kmem_caches();
3309 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3310 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3311 MODULE_LICENSE("GPL");
3313 module_init(target_core_init_configfs);
3314 module_exit(target_core_exit_configfs);