cris: add arch/cris/include/asm/serial.h
[linux-2.6/next.git] / drivers / target / target_core_configfs.c
blobb2575d8568cc174ceadb7648c6fa9deb0a438a3f
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>
30 #include <linux/fs.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,
85 char *page)
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,
98 .ca_name = "version",
99 .ca_mode = S_IRUGO,
102 static struct target_fabric_configfs *target_core_get_fabric(
103 const char *name)
105 struct target_fabric_configfs *tf;
107 if (!name)
108 return NULL;
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);
115 return tf;
118 mutex_unlock(&g_tf_lock);
120 return NULL;
124 * Called from struct target_core_group_ops->make_group()
126 static struct config_group *target_core_register_fabric(
127 struct config_group *group,
128 const char *name)
130 struct target_fabric_configfs *tf;
131 int ret;
133 pr_debug("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
138 * LUN symlinks.
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");
161 if (ret < 0) {
162 pr_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");
174 if (ret < 0) {
175 pr_err("request_module() failed for"
176 " tcm_loop.ko: %d\n", ret);
177 return ERR_PTR(-EINVAL);
181 tf = target_core_get_fabric(name);
182 if (!tf) {
183 pr_err("target_core_get_fabric() failed for %s\n",
184 name);
185 return ERR_PTR(-EINVAL);
187 pr_debug("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 pr_debug("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 pr_debug("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 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
213 " for %s\n", name);
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;
229 int i;
231 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
232 " tf list\n", config_item_name(item));
234 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
235 " %s\n", tf->tf_name);
236 atomic_dec(&tf->tf_access_cnt);
238 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
239 " tf->tf_fabric for %s\n", tf->tf_name);
240 tf->tf_fabric = NULL;
242 pr_debug("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,
264 NULL,
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 = {
278 .su_group = {
279 .cg_item = {
280 .ci_namebuf = "target",
281 .ci_type = &target_core_fabrics_item,
286 static struct configfs_subsystem *target_core_subsystem[] = {
287 &target_core_fabrics,
288 NULL,
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,
305 const char *name)
307 struct target_fabric_configfs *tf;
309 if (!(name)) {
310 pr_err("Unable to locate passed fabric name\n");
311 return ERR_PTR(-EINVAL);
313 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
314 pr_err("Passed name: %s exceeds TARGET_FABRIC"
315 "_NAME_SIZE\n", name);
316 return ERR_PTR(-EINVAL);
319 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
320 if (!tf)
321 return ERR_PTR(-ENOMEM);
323 INIT_LIST_HEAD(&tf->tf_list);
324 atomic_set(&tf->tf_access_cnt, 0);
326 * Setup the default generic struct config_item_type's (cits) in
327 * struct target_fabric_configfs->tf_cit_tmpl
329 tf->tf_module = fabric_mod;
330 target_fabric_setup_cits(tf);
332 tf->tf_subsys = target_core_subsystem[0];
333 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
335 mutex_lock(&g_tf_lock);
336 list_add_tail(&tf->tf_list, &g_tf_list);
337 mutex_unlock(&g_tf_lock);
339 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
340 ">>>>>>>>>>>>>>\n");
341 pr_debug("Initialized struct target_fabric_configfs: %p for"
342 " %s\n", tf, tf->tf_name);
343 return tf;
345 EXPORT_SYMBOL(target_fabric_configfs_init);
348 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
350 void target_fabric_configfs_free(
351 struct target_fabric_configfs *tf)
353 mutex_lock(&g_tf_lock);
354 list_del(&tf->tf_list);
355 mutex_unlock(&g_tf_lock);
357 kfree(tf);
359 EXPORT_SYMBOL(target_fabric_configfs_free);
362 * Perform a sanity check of the passed tf->tf_ops before completing
363 * TCM fabric module registration.
365 static int target_fabric_tf_ops_check(
366 struct target_fabric_configfs *tf)
368 struct target_core_fabric_ops *tfo = &tf->tf_ops;
370 if (!tfo->get_fabric_name) {
371 pr_err("Missing tfo->get_fabric_name()\n");
372 return -EINVAL;
374 if (!tfo->get_fabric_proto_ident) {
375 pr_err("Missing tfo->get_fabric_proto_ident()\n");
376 return -EINVAL;
378 if (!tfo->tpg_get_wwn) {
379 pr_err("Missing tfo->tpg_get_wwn()\n");
380 return -EINVAL;
382 if (!tfo->tpg_get_tag) {
383 pr_err("Missing tfo->tpg_get_tag()\n");
384 return -EINVAL;
386 if (!tfo->tpg_get_default_depth) {
387 pr_err("Missing tfo->tpg_get_default_depth()\n");
388 return -EINVAL;
390 if (!tfo->tpg_get_pr_transport_id) {
391 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
392 return -EINVAL;
394 if (!tfo->tpg_get_pr_transport_id_len) {
395 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
396 return -EINVAL;
398 if (!tfo->tpg_check_demo_mode) {
399 pr_err("Missing tfo->tpg_check_demo_mode()\n");
400 return -EINVAL;
402 if (!tfo->tpg_check_demo_mode_cache) {
403 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
404 return -EINVAL;
406 if (!tfo->tpg_check_demo_mode_write_protect) {
407 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
408 return -EINVAL;
410 if (!tfo->tpg_check_prod_mode_write_protect) {
411 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
412 return -EINVAL;
414 if (!tfo->tpg_alloc_fabric_acl) {
415 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
416 return -EINVAL;
418 if (!tfo->tpg_release_fabric_acl) {
419 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
420 return -EINVAL;
422 if (!tfo->tpg_get_inst_index) {
423 pr_err("Missing tfo->tpg_get_inst_index()\n");
424 return -EINVAL;
426 if (!tfo->release_cmd) {
427 pr_err("Missing tfo->release_cmd()\n");
428 return -EINVAL;
430 if (!tfo->shutdown_session) {
431 pr_err("Missing tfo->shutdown_session()\n");
432 return -EINVAL;
434 if (!tfo->close_session) {
435 pr_err("Missing tfo->close_session()\n");
436 return -EINVAL;
438 if (!tfo->stop_session) {
439 pr_err("Missing tfo->stop_session()\n");
440 return -EINVAL;
442 if (!tfo->fall_back_to_erl0) {
443 pr_err("Missing tfo->fall_back_to_erl0()\n");
444 return -EINVAL;
446 if (!tfo->sess_logged_in) {
447 pr_err("Missing tfo->sess_logged_in()\n");
448 return -EINVAL;
450 if (!tfo->sess_get_index) {
451 pr_err("Missing tfo->sess_get_index()\n");
452 return -EINVAL;
454 if (!tfo->write_pending) {
455 pr_err("Missing tfo->write_pending()\n");
456 return -EINVAL;
458 if (!tfo->write_pending_status) {
459 pr_err("Missing tfo->write_pending_status()\n");
460 return -EINVAL;
462 if (!tfo->set_default_node_attributes) {
463 pr_err("Missing tfo->set_default_node_attributes()\n");
464 return -EINVAL;
466 if (!tfo->get_task_tag) {
467 pr_err("Missing tfo->get_task_tag()\n");
468 return -EINVAL;
470 if (!tfo->get_cmd_state) {
471 pr_err("Missing tfo->get_cmd_state()\n");
472 return -EINVAL;
474 if (!tfo->queue_data_in) {
475 pr_err("Missing tfo->queue_data_in()\n");
476 return -EINVAL;
478 if (!tfo->queue_status) {
479 pr_err("Missing tfo->queue_status()\n");
480 return -EINVAL;
482 if (!tfo->queue_tm_rsp) {
483 pr_err("Missing tfo->queue_tm_rsp()\n");
484 return -EINVAL;
486 if (!tfo->set_fabric_sense_len) {
487 pr_err("Missing tfo->set_fabric_sense_len()\n");
488 return -EINVAL;
490 if (!tfo->get_fabric_sense_len) {
491 pr_err("Missing tfo->get_fabric_sense_len()\n");
492 return -EINVAL;
494 if (!tfo->is_state_remove) {
495 pr_err("Missing tfo->is_state_remove()\n");
496 return -EINVAL;
499 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
500 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
501 * target_core_fabric_configfs.c WWN+TPG group context code.
503 if (!tfo->fabric_make_wwn) {
504 pr_err("Missing tfo->fabric_make_wwn()\n");
505 return -EINVAL;
507 if (!tfo->fabric_drop_wwn) {
508 pr_err("Missing tfo->fabric_drop_wwn()\n");
509 return -EINVAL;
511 if (!tfo->fabric_make_tpg) {
512 pr_err("Missing tfo->fabric_make_tpg()\n");
513 return -EINVAL;
515 if (!tfo->fabric_drop_tpg) {
516 pr_err("Missing tfo->fabric_drop_tpg()\n");
517 return -EINVAL;
520 return 0;
524 * Called 2nd from fabric module with returned parameter of
525 * struct target_fabric_configfs * from target_fabric_configfs_init().
527 * Upon a successful registration, the new fabric's struct config_item is
528 * return. Also, a pointer to this struct is set in the passed
529 * struct target_fabric_configfs.
531 int target_fabric_configfs_register(
532 struct target_fabric_configfs *tf)
534 int ret;
536 if (!tf) {
537 pr_err("Unable to locate target_fabric_configfs"
538 " pointer\n");
539 return -EINVAL;
541 if (!tf->tf_subsys) {
542 pr_err("Unable to target struct config_subsystem"
543 " pointer\n");
544 return -EINVAL;
546 ret = target_fabric_tf_ops_check(tf);
547 if (ret < 0)
548 return ret;
550 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
551 ">>>>>>>>>>\n");
552 return 0;
554 EXPORT_SYMBOL(target_fabric_configfs_register);
556 void target_fabric_configfs_deregister(
557 struct target_fabric_configfs *tf)
559 struct configfs_subsystem *su;
561 if (!tf) {
562 pr_err("Unable to locate passed target_fabric_"
563 "configfs\n");
564 return;
566 su = tf->tf_subsys;
567 if (!su) {
568 pr_err("Unable to locate passed tf->tf_subsys"
569 " pointer\n");
570 return;
572 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
573 ">>>>>>>>>>>>\n");
574 mutex_lock(&g_tf_lock);
575 if (atomic_read(&tf->tf_access_cnt)) {
576 mutex_unlock(&g_tf_lock);
577 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
578 tf->tf_name);
579 BUG();
581 list_del(&tf->tf_list);
582 mutex_unlock(&g_tf_lock);
584 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
585 " %s\n", tf->tf_name);
586 tf->tf_module = NULL;
587 tf->tf_subsys = NULL;
588 kfree(tf);
590 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
591 ">>>>>\n");
593 EXPORT_SYMBOL(target_fabric_configfs_deregister);
595 /*##############################################################################
596 // Stop functions called by external Target Fabrics Modules
597 //############################################################################*/
599 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
601 #define DEF_DEV_ATTRIB_SHOW(_name) \
602 static ssize_t target_core_dev_show_attr_##_name( \
603 struct se_dev_attrib *da, \
604 char *page) \
606 struct se_device *dev; \
607 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
608 ssize_t rb; \
610 spin_lock(&se_dev->se_dev_lock); \
611 dev = se_dev->se_dev_ptr; \
612 if (!dev) { \
613 spin_unlock(&se_dev->se_dev_lock); \
614 return -ENODEV; \
616 rb = snprintf(page, PAGE_SIZE, "%u\n", \
617 (u32)dev->se_sub_dev->se_dev_attrib._name); \
618 spin_unlock(&se_dev->se_dev_lock); \
620 return rb; \
623 #define DEF_DEV_ATTRIB_STORE(_name) \
624 static ssize_t target_core_dev_store_attr_##_name( \
625 struct se_dev_attrib *da, \
626 const char *page, \
627 size_t count) \
629 struct se_device *dev; \
630 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
631 unsigned long val; \
632 int ret; \
634 spin_lock(&se_dev->se_dev_lock); \
635 dev = se_dev->se_dev_ptr; \
636 if (!dev) { \
637 spin_unlock(&se_dev->se_dev_lock); \
638 return -ENODEV; \
640 ret = strict_strtoul(page, 0, &val); \
641 if (ret < 0) { \
642 spin_unlock(&se_dev->se_dev_lock); \
643 pr_err("strict_strtoul() failed with" \
644 " ret: %d\n", ret); \
645 return -EINVAL; \
647 ret = se_dev_set_##_name(dev, (u32)val); \
648 spin_unlock(&se_dev->se_dev_lock); \
650 return (!ret) ? count : -EINVAL; \
653 #define DEF_DEV_ATTRIB(_name) \
654 DEF_DEV_ATTRIB_SHOW(_name); \
655 DEF_DEV_ATTRIB_STORE(_name);
657 #define DEF_DEV_ATTRIB_RO(_name) \
658 DEF_DEV_ATTRIB_SHOW(_name);
660 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
661 #define SE_DEV_ATTR(_name, _mode) \
662 static struct target_core_dev_attrib_attribute \
663 target_core_dev_attrib_##_name = \
664 __CONFIGFS_EATTR(_name, _mode, \
665 target_core_dev_show_attr_##_name, \
666 target_core_dev_store_attr_##_name);
668 #define SE_DEV_ATTR_RO(_name); \
669 static struct target_core_dev_attrib_attribute \
670 target_core_dev_attrib_##_name = \
671 __CONFIGFS_EATTR_RO(_name, \
672 target_core_dev_show_attr_##_name);
674 DEF_DEV_ATTRIB(emulate_dpo);
675 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
677 DEF_DEV_ATTRIB(emulate_fua_write);
678 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
680 DEF_DEV_ATTRIB(emulate_fua_read);
681 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
683 DEF_DEV_ATTRIB(emulate_write_cache);
684 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
686 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
687 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
689 DEF_DEV_ATTRIB(emulate_tas);
690 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
692 DEF_DEV_ATTRIB(emulate_tpu);
693 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
695 DEF_DEV_ATTRIB(emulate_tpws);
696 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
698 DEF_DEV_ATTRIB(enforce_pr_isids);
699 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
701 DEF_DEV_ATTRIB(is_nonrot);
702 SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
704 DEF_DEV_ATTRIB(emulate_rest_reord);
705 SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
707 DEF_DEV_ATTRIB_RO(hw_block_size);
708 SE_DEV_ATTR_RO(hw_block_size);
710 DEF_DEV_ATTRIB(block_size);
711 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
713 DEF_DEV_ATTRIB_RO(hw_max_sectors);
714 SE_DEV_ATTR_RO(hw_max_sectors);
716 DEF_DEV_ATTRIB(max_sectors);
717 SE_DEV_ATTR(max_sectors, S_IRUGO | S_IWUSR);
719 DEF_DEV_ATTRIB(optimal_sectors);
720 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
722 DEF_DEV_ATTRIB_RO(hw_queue_depth);
723 SE_DEV_ATTR_RO(hw_queue_depth);
725 DEF_DEV_ATTRIB(queue_depth);
726 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
728 DEF_DEV_ATTRIB(task_timeout);
729 SE_DEV_ATTR(task_timeout, S_IRUGO | S_IWUSR);
731 DEF_DEV_ATTRIB(max_unmap_lba_count);
732 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
734 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
735 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
737 DEF_DEV_ATTRIB(unmap_granularity);
738 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
740 DEF_DEV_ATTRIB(unmap_granularity_alignment);
741 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
743 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
745 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
746 &target_core_dev_attrib_emulate_dpo.attr,
747 &target_core_dev_attrib_emulate_fua_write.attr,
748 &target_core_dev_attrib_emulate_fua_read.attr,
749 &target_core_dev_attrib_emulate_write_cache.attr,
750 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
751 &target_core_dev_attrib_emulate_tas.attr,
752 &target_core_dev_attrib_emulate_tpu.attr,
753 &target_core_dev_attrib_emulate_tpws.attr,
754 &target_core_dev_attrib_enforce_pr_isids.attr,
755 &target_core_dev_attrib_is_nonrot.attr,
756 &target_core_dev_attrib_emulate_rest_reord.attr,
757 &target_core_dev_attrib_hw_block_size.attr,
758 &target_core_dev_attrib_block_size.attr,
759 &target_core_dev_attrib_hw_max_sectors.attr,
760 &target_core_dev_attrib_max_sectors.attr,
761 &target_core_dev_attrib_optimal_sectors.attr,
762 &target_core_dev_attrib_hw_queue_depth.attr,
763 &target_core_dev_attrib_queue_depth.attr,
764 &target_core_dev_attrib_task_timeout.attr,
765 &target_core_dev_attrib_max_unmap_lba_count.attr,
766 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
767 &target_core_dev_attrib_unmap_granularity.attr,
768 &target_core_dev_attrib_unmap_granularity_alignment.attr,
769 NULL,
772 static struct configfs_item_operations target_core_dev_attrib_ops = {
773 .show_attribute = target_core_dev_attrib_attr_show,
774 .store_attribute = target_core_dev_attrib_attr_store,
777 static struct config_item_type target_core_dev_attrib_cit = {
778 .ct_item_ops = &target_core_dev_attrib_ops,
779 .ct_attrs = target_core_dev_attrib_attrs,
780 .ct_owner = THIS_MODULE,
783 /* End functions for struct config_item_type target_core_dev_attrib_cit */
785 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
787 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
788 #define SE_DEV_WWN_ATTR(_name, _mode) \
789 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
790 __CONFIGFS_EATTR(_name, _mode, \
791 target_core_dev_wwn_show_attr_##_name, \
792 target_core_dev_wwn_store_attr_##_name);
794 #define SE_DEV_WWN_ATTR_RO(_name); \
795 do { \
796 static struct target_core_dev_wwn_attribute \
797 target_core_dev_wwn_##_name = \
798 __CONFIGFS_EATTR_RO(_name, \
799 target_core_dev_wwn_show_attr_##_name); \
800 } while (0);
803 * VPD page 0x80 Unit serial
805 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
806 struct t10_wwn *t10_wwn,
807 char *page)
809 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
810 struct se_device *dev;
812 dev = se_dev->se_dev_ptr;
813 if (!dev)
814 return -ENODEV;
816 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
817 &t10_wwn->unit_serial[0]);
820 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
821 struct t10_wwn *t10_wwn,
822 const char *page,
823 size_t count)
825 struct se_subsystem_dev *su_dev = t10_wwn->t10_sub_dev;
826 struct se_device *dev;
827 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
830 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
831 * from the struct scsi_device level firmware, do not allow
832 * VPD Unit Serial to be emulated.
834 * Note this struct scsi_device could also be emulating VPD
835 * information from its drivers/scsi LLD. But for now we assume
836 * it is doing 'the right thing' wrt a world wide unique
837 * VPD Unit Serial Number that OS dependent multipath can depend on.
839 if (su_dev->su_dev_flags & SDF_FIRMWARE_VPD_UNIT_SERIAL) {
840 pr_err("Underlying SCSI device firmware provided VPD"
841 " Unit Serial, ignoring request\n");
842 return -EOPNOTSUPP;
845 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
846 pr_err("Emulated VPD Unit Serial exceeds"
847 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
848 return -EOVERFLOW;
851 * Check to see if any active $FABRIC_MOD exports exist. If they
852 * do exist, fail here as changing this information on the fly
853 * (underneath the initiator side OS dependent multipath code)
854 * could cause negative effects.
856 dev = su_dev->se_dev_ptr;
857 if (dev) {
858 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
859 pr_err("Unable to set VPD Unit Serial while"
860 " active %d $FABRIC_MOD exports exist\n",
861 atomic_read(&dev->dev_export_obj.obj_access_count));
862 return -EINVAL;
866 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
868 * Also, strip any newline added from the userspace
869 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
871 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
872 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
873 snprintf(su_dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
874 "%s", strstrip(buf));
875 su_dev->su_dev_flags |= SDF_EMULATED_VPD_UNIT_SERIAL;
877 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
878 " %s\n", su_dev->t10_wwn.unit_serial);
880 return count;
883 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
886 * VPD page 0x83 Protocol Identifier
888 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
889 struct t10_wwn *t10_wwn,
890 char *page)
892 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
893 struct se_device *dev;
894 struct t10_vpd *vpd;
895 unsigned char buf[VPD_TMP_BUF_SIZE];
896 ssize_t len = 0;
898 dev = se_dev->se_dev_ptr;
899 if (!dev)
900 return -ENODEV;
902 memset(buf, 0, VPD_TMP_BUF_SIZE);
904 spin_lock(&t10_wwn->t10_vpd_lock);
905 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
906 if (!vpd->protocol_identifier_set)
907 continue;
909 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
911 if (len + strlen(buf) >= PAGE_SIZE)
912 break;
914 len += sprintf(page+len, "%s", buf);
916 spin_unlock(&t10_wwn->t10_vpd_lock);
918 return len;
921 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
922 struct t10_wwn *t10_wwn,
923 const char *page,
924 size_t count)
926 return -ENOSYS;
929 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
932 * Generic wrapper for dumping VPD identifiers by association.
934 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
935 static ssize_t target_core_dev_wwn_show_attr_##_name( \
936 struct t10_wwn *t10_wwn, \
937 char *page) \
939 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev; \
940 struct se_device *dev; \
941 struct t10_vpd *vpd; \
942 unsigned char buf[VPD_TMP_BUF_SIZE]; \
943 ssize_t len = 0; \
945 dev = se_dev->se_dev_ptr; \
946 if (!dev) \
947 return -ENODEV; \
949 spin_lock(&t10_wwn->t10_vpd_lock); \
950 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
951 if (vpd->association != _assoc) \
952 continue; \
954 memset(buf, 0, VPD_TMP_BUF_SIZE); \
955 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
956 if (len + strlen(buf) >= PAGE_SIZE) \
957 break; \
958 len += sprintf(page+len, "%s", buf); \
960 memset(buf, 0, VPD_TMP_BUF_SIZE); \
961 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
962 if (len + strlen(buf) >= PAGE_SIZE) \
963 break; \
964 len += sprintf(page+len, "%s", buf); \
966 memset(buf, 0, VPD_TMP_BUF_SIZE); \
967 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
968 if (len + strlen(buf) >= PAGE_SIZE) \
969 break; \
970 len += sprintf(page+len, "%s", buf); \
972 spin_unlock(&t10_wwn->t10_vpd_lock); \
974 return len; \
978 * VPD page 0x83 Association: Logical Unit
980 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
982 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
983 struct t10_wwn *t10_wwn,
984 const char *page,
985 size_t count)
987 return -ENOSYS;
990 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
993 * VPD page 0x83 Association: Target Port
995 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
997 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
998 struct t10_wwn *t10_wwn,
999 const char *page,
1000 size_t count)
1002 return -ENOSYS;
1005 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
1008 * VPD page 0x83 Association: SCSI Target Device
1010 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1012 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
1013 struct t10_wwn *t10_wwn,
1014 const char *page,
1015 size_t count)
1017 return -ENOSYS;
1020 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1022 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1024 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1025 &target_core_dev_wwn_vpd_unit_serial.attr,
1026 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1027 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1028 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1029 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1030 NULL,
1033 static struct configfs_item_operations target_core_dev_wwn_ops = {
1034 .show_attribute = target_core_dev_wwn_attr_show,
1035 .store_attribute = target_core_dev_wwn_attr_store,
1038 static struct config_item_type target_core_dev_wwn_cit = {
1039 .ct_item_ops = &target_core_dev_wwn_ops,
1040 .ct_attrs = target_core_dev_wwn_attrs,
1041 .ct_owner = THIS_MODULE,
1044 /* End functions for struct config_item_type target_core_dev_wwn_cit */
1046 /* Start functions for struct config_item_type target_core_dev_pr_cit */
1048 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_subsystem_dev);
1049 #define SE_DEV_PR_ATTR(_name, _mode) \
1050 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1051 __CONFIGFS_EATTR(_name, _mode, \
1052 target_core_dev_pr_show_attr_##_name, \
1053 target_core_dev_pr_store_attr_##_name);
1055 #define SE_DEV_PR_ATTR_RO(_name); \
1056 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1057 __CONFIGFS_EATTR_RO(_name, \
1058 target_core_dev_pr_show_attr_##_name);
1061 * res_holder
1063 static ssize_t target_core_dev_pr_show_spc3_res(
1064 struct se_device *dev,
1065 char *page,
1066 ssize_t *len)
1068 struct se_node_acl *se_nacl;
1069 struct t10_pr_registration *pr_reg;
1070 char i_buf[PR_REG_ISID_ID_LEN];
1071 int prf_isid;
1073 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1075 spin_lock(&dev->dev_reservation_lock);
1076 pr_reg = dev->dev_pr_res_holder;
1077 if (!pr_reg) {
1078 *len += sprintf(page + *len, "No SPC-3 Reservation holder\n");
1079 spin_unlock(&dev->dev_reservation_lock);
1080 return *len;
1082 se_nacl = pr_reg->pr_reg_nacl;
1083 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1084 PR_REG_ISID_ID_LEN);
1086 *len += sprintf(page + *len, "SPC-3 Reservation: %s Initiator: %s%s\n",
1087 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1088 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1089 spin_unlock(&dev->dev_reservation_lock);
1091 return *len;
1094 static ssize_t target_core_dev_pr_show_spc2_res(
1095 struct se_device *dev,
1096 char *page,
1097 ssize_t *len)
1099 struct se_node_acl *se_nacl;
1101 spin_lock(&dev->dev_reservation_lock);
1102 se_nacl = dev->dev_reserved_node_acl;
1103 if (!se_nacl) {
1104 *len += sprintf(page + *len, "No SPC-2 Reservation holder\n");
1105 spin_unlock(&dev->dev_reservation_lock);
1106 return *len;
1108 *len += sprintf(page + *len, "SPC-2 Reservation: %s Initiator: %s\n",
1109 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1110 se_nacl->initiatorname);
1111 spin_unlock(&dev->dev_reservation_lock);
1113 return *len;
1116 static ssize_t target_core_dev_pr_show_attr_res_holder(
1117 struct se_subsystem_dev *su_dev,
1118 char *page)
1120 ssize_t len = 0;
1122 if (!su_dev->se_dev_ptr)
1123 return -ENODEV;
1125 switch (su_dev->t10_pr.res_type) {
1126 case SPC3_PERSISTENT_RESERVATIONS:
1127 target_core_dev_pr_show_spc3_res(su_dev->se_dev_ptr,
1128 page, &len);
1129 break;
1130 case SPC2_RESERVATIONS:
1131 target_core_dev_pr_show_spc2_res(su_dev->se_dev_ptr,
1132 page, &len);
1133 break;
1134 case SPC_PASSTHROUGH:
1135 len += sprintf(page+len, "Passthrough\n");
1136 break;
1137 default:
1138 len += sprintf(page+len, "Unknown\n");
1139 break;
1142 return len;
1145 SE_DEV_PR_ATTR_RO(res_holder);
1148 * res_pr_all_tgt_pts
1150 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1151 struct se_subsystem_dev *su_dev,
1152 char *page)
1154 struct se_device *dev;
1155 struct t10_pr_registration *pr_reg;
1156 ssize_t len = 0;
1158 dev = su_dev->se_dev_ptr;
1159 if (!dev)
1160 return -ENODEV;
1162 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1163 return len;
1165 spin_lock(&dev->dev_reservation_lock);
1166 pr_reg = dev->dev_pr_res_holder;
1167 if (!pr_reg) {
1168 len = sprintf(page, "No SPC-3 Reservation holder\n");
1169 spin_unlock(&dev->dev_reservation_lock);
1170 return len;
1173 * See All Target Ports (ALL_TG_PT) bit in spcr17, section 6.14.3
1174 * Basic PERSISTENT RESERVER OUT parameter list, page 290
1176 if (pr_reg->pr_reg_all_tg_pt)
1177 len = sprintf(page, "SPC-3 Reservation: All Target"
1178 " Ports registration\n");
1179 else
1180 len = sprintf(page, "SPC-3 Reservation: Single"
1181 " Target Port registration\n");
1182 spin_unlock(&dev->dev_reservation_lock);
1184 return len;
1187 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1190 * res_pr_generation
1192 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1193 struct se_subsystem_dev *su_dev,
1194 char *page)
1196 if (!su_dev->se_dev_ptr)
1197 return -ENODEV;
1199 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1200 return 0;
1202 return sprintf(page, "0x%08x\n", su_dev->t10_pr.pr_generation);
1205 SE_DEV_PR_ATTR_RO(res_pr_generation);
1208 * res_pr_holder_tg_port
1210 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1211 struct se_subsystem_dev *su_dev,
1212 char *page)
1214 struct se_device *dev;
1215 struct se_node_acl *se_nacl;
1216 struct se_lun *lun;
1217 struct se_portal_group *se_tpg;
1218 struct t10_pr_registration *pr_reg;
1219 struct target_core_fabric_ops *tfo;
1220 ssize_t len = 0;
1222 dev = su_dev->se_dev_ptr;
1223 if (!dev)
1224 return -ENODEV;
1226 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1227 return len;
1229 spin_lock(&dev->dev_reservation_lock);
1230 pr_reg = dev->dev_pr_res_holder;
1231 if (!pr_reg) {
1232 len = sprintf(page, "No SPC-3 Reservation holder\n");
1233 spin_unlock(&dev->dev_reservation_lock);
1234 return len;
1236 se_nacl = pr_reg->pr_reg_nacl;
1237 se_tpg = se_nacl->se_tpg;
1238 lun = pr_reg->pr_reg_tg_pt_lun;
1239 tfo = se_tpg->se_tpg_tfo;
1241 len += sprintf(page+len, "SPC-3 Reservation: %s"
1242 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1243 tfo->tpg_get_wwn(se_tpg));
1244 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1245 " Identifer Tag: %hu %s Portal Group Tag: %hu"
1246 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1247 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1248 tfo->get_fabric_name(), lun->unpacked_lun);
1249 spin_unlock(&dev->dev_reservation_lock);
1251 return len;
1254 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1257 * res_pr_registered_i_pts
1259 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1260 struct se_subsystem_dev *su_dev,
1261 char *page)
1263 struct target_core_fabric_ops *tfo;
1264 struct t10_pr_registration *pr_reg;
1265 unsigned char buf[384];
1266 char i_buf[PR_REG_ISID_ID_LEN];
1267 ssize_t len = 0;
1268 int reg_count = 0, prf_isid;
1270 if (!su_dev->se_dev_ptr)
1271 return -ENODEV;
1273 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1274 return len;
1276 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1278 spin_lock(&su_dev->t10_pr.registration_lock);
1279 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1280 pr_reg_list) {
1282 memset(buf, 0, 384);
1283 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1284 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1285 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1286 PR_REG_ISID_ID_LEN);
1287 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1288 tfo->get_fabric_name(),
1289 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1290 &i_buf[0] : "", pr_reg->pr_res_key,
1291 pr_reg->pr_res_generation);
1293 if (len + strlen(buf) >= PAGE_SIZE)
1294 break;
1296 len += sprintf(page+len, "%s", buf);
1297 reg_count++;
1299 spin_unlock(&su_dev->t10_pr.registration_lock);
1301 if (!reg_count)
1302 len += sprintf(page+len, "None\n");
1304 return len;
1307 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1310 * res_pr_type
1312 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1313 struct se_subsystem_dev *su_dev,
1314 char *page)
1316 struct se_device *dev;
1317 struct t10_pr_registration *pr_reg;
1318 ssize_t len = 0;
1320 dev = su_dev->se_dev_ptr;
1321 if (!dev)
1322 return -ENODEV;
1324 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1325 return len;
1327 spin_lock(&dev->dev_reservation_lock);
1328 pr_reg = dev->dev_pr_res_holder;
1329 if (!pr_reg) {
1330 len = sprintf(page, "No SPC-3 Reservation holder\n");
1331 spin_unlock(&dev->dev_reservation_lock);
1332 return len;
1334 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1335 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1336 spin_unlock(&dev->dev_reservation_lock);
1338 return len;
1341 SE_DEV_PR_ATTR_RO(res_pr_type);
1344 * res_type
1346 static ssize_t target_core_dev_pr_show_attr_res_type(
1347 struct se_subsystem_dev *su_dev,
1348 char *page)
1350 ssize_t len = 0;
1352 if (!su_dev->se_dev_ptr)
1353 return -ENODEV;
1355 switch (su_dev->t10_pr.res_type) {
1356 case SPC3_PERSISTENT_RESERVATIONS:
1357 len = sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1358 break;
1359 case SPC2_RESERVATIONS:
1360 len = sprintf(page, "SPC2_RESERVATIONS\n");
1361 break;
1362 case SPC_PASSTHROUGH:
1363 len = sprintf(page, "SPC_PASSTHROUGH\n");
1364 break;
1365 default:
1366 len = sprintf(page, "UNKNOWN\n");
1367 break;
1370 return len;
1373 SE_DEV_PR_ATTR_RO(res_type);
1376 * res_aptpl_active
1379 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1380 struct se_subsystem_dev *su_dev,
1381 char *page)
1383 if (!su_dev->se_dev_ptr)
1384 return -ENODEV;
1386 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1387 return 0;
1389 return sprintf(page, "APTPL Bit Status: %s\n",
1390 (su_dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1393 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1396 * res_aptpl_metadata
1398 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1399 struct se_subsystem_dev *su_dev,
1400 char *page)
1402 if (!su_dev->se_dev_ptr)
1403 return -ENODEV;
1405 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1406 return 0;
1408 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1411 enum {
1412 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1413 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1414 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1415 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1418 static match_table_t tokens = {
1419 {Opt_initiator_fabric, "initiator_fabric=%s"},
1420 {Opt_initiator_node, "initiator_node=%s"},
1421 {Opt_initiator_sid, "initiator_sid=%s"},
1422 {Opt_sa_res_key, "sa_res_key=%s"},
1423 {Opt_res_holder, "res_holder=%d"},
1424 {Opt_res_type, "res_type=%d"},
1425 {Opt_res_scope, "res_scope=%d"},
1426 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1427 {Opt_mapped_lun, "mapped_lun=%d"},
1428 {Opt_target_fabric, "target_fabric=%s"},
1429 {Opt_target_node, "target_node=%s"},
1430 {Opt_tpgt, "tpgt=%d"},
1431 {Opt_port_rtpi, "port_rtpi=%d"},
1432 {Opt_target_lun, "target_lun=%d"},
1433 {Opt_err, NULL}
1436 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1437 struct se_subsystem_dev *su_dev,
1438 const char *page,
1439 size_t count)
1441 struct se_device *dev;
1442 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1443 unsigned char *t_fabric = NULL, *t_port = NULL;
1444 char *orig, *ptr, *arg_p, *opts;
1445 substring_t args[MAX_OPT_ARGS];
1446 unsigned long long tmp_ll;
1447 u64 sa_res_key = 0;
1448 u32 mapped_lun = 0, target_lun = 0;
1449 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1450 u16 port_rpti = 0, tpgt = 0;
1451 u8 type = 0, scope;
1453 dev = su_dev->se_dev_ptr;
1454 if (!dev)
1455 return -ENODEV;
1457 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1458 return 0;
1460 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1461 pr_debug("Unable to process APTPL metadata while"
1462 " active fabric exports exist\n");
1463 return -EINVAL;
1466 opts = kstrdup(page, GFP_KERNEL);
1467 if (!opts)
1468 return -ENOMEM;
1470 orig = opts;
1471 while ((ptr = strsep(&opts, ",")) != NULL) {
1472 if (!*ptr)
1473 continue;
1475 token = match_token(ptr, tokens, args);
1476 switch (token) {
1477 case Opt_initiator_fabric:
1478 i_fabric = match_strdup(&args[0]);
1479 if (!i_fabric) {
1480 ret = -ENOMEM;
1481 goto out;
1483 break;
1484 case Opt_initiator_node:
1485 i_port = match_strdup(&args[0]);
1486 if (!i_port) {
1487 ret = -ENOMEM;
1488 goto out;
1490 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1491 pr_err("APTPL metadata initiator_node="
1492 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1493 PR_APTPL_MAX_IPORT_LEN);
1494 ret = -EINVAL;
1495 break;
1497 break;
1498 case Opt_initiator_sid:
1499 isid = match_strdup(&args[0]);
1500 if (!isid) {
1501 ret = -ENOMEM;
1502 goto out;
1504 if (strlen(isid) >= PR_REG_ISID_LEN) {
1505 pr_err("APTPL metadata initiator_isid"
1506 "= exceeds PR_REG_ISID_LEN: %d\n",
1507 PR_REG_ISID_LEN);
1508 ret = -EINVAL;
1509 break;
1511 break;
1512 case Opt_sa_res_key:
1513 arg_p = match_strdup(&args[0]);
1514 if (!arg_p) {
1515 ret = -ENOMEM;
1516 goto out;
1518 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1519 if (ret < 0) {
1520 pr_err("strict_strtoull() failed for"
1521 " sa_res_key=\n");
1522 goto out;
1524 sa_res_key = (u64)tmp_ll;
1525 break;
1527 * PR APTPL Metadata for Reservation
1529 case Opt_res_holder:
1530 match_int(args, &arg);
1531 res_holder = arg;
1532 break;
1533 case Opt_res_type:
1534 match_int(args, &arg);
1535 type = (u8)arg;
1536 break;
1537 case Opt_res_scope:
1538 match_int(args, &arg);
1539 scope = (u8)arg;
1540 break;
1541 case Opt_res_all_tg_pt:
1542 match_int(args, &arg);
1543 all_tg_pt = (int)arg;
1544 break;
1545 case Opt_mapped_lun:
1546 match_int(args, &arg);
1547 mapped_lun = (u32)arg;
1548 break;
1550 * PR APTPL Metadata for Target Port
1552 case Opt_target_fabric:
1553 t_fabric = match_strdup(&args[0]);
1554 if (!t_fabric) {
1555 ret = -ENOMEM;
1556 goto out;
1558 break;
1559 case Opt_target_node:
1560 t_port = match_strdup(&args[0]);
1561 if (!t_port) {
1562 ret = -ENOMEM;
1563 goto out;
1565 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1566 pr_err("APTPL metadata target_node="
1567 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1568 PR_APTPL_MAX_TPORT_LEN);
1569 ret = -EINVAL;
1570 break;
1572 break;
1573 case Opt_tpgt:
1574 match_int(args, &arg);
1575 tpgt = (u16)arg;
1576 break;
1577 case Opt_port_rtpi:
1578 match_int(args, &arg);
1579 port_rpti = (u16)arg;
1580 break;
1581 case Opt_target_lun:
1582 match_int(args, &arg);
1583 target_lun = (u32)arg;
1584 break;
1585 default:
1586 break;
1590 if (!i_port || !t_port || !sa_res_key) {
1591 pr_err("Illegal parameters for APTPL registration\n");
1592 ret = -EINVAL;
1593 goto out;
1596 if (res_holder && !(type)) {
1597 pr_err("Illegal PR type: 0x%02x for reservation"
1598 " holder\n", type);
1599 ret = -EINVAL;
1600 goto out;
1603 ret = core_scsi3_alloc_aptpl_registration(&su_dev->t10_pr, sa_res_key,
1604 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1605 res_holder, all_tg_pt, type);
1606 out:
1607 kfree(i_fabric);
1608 kfree(i_port);
1609 kfree(isid);
1610 kfree(t_fabric);
1611 kfree(t_port);
1612 kfree(orig);
1613 return (ret == 0) ? count : ret;
1616 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1618 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1620 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1621 &target_core_dev_pr_res_holder.attr,
1622 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1623 &target_core_dev_pr_res_pr_generation.attr,
1624 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1625 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1626 &target_core_dev_pr_res_pr_type.attr,
1627 &target_core_dev_pr_res_type.attr,
1628 &target_core_dev_pr_res_aptpl_active.attr,
1629 &target_core_dev_pr_res_aptpl_metadata.attr,
1630 NULL,
1633 static struct configfs_item_operations target_core_dev_pr_ops = {
1634 .show_attribute = target_core_dev_pr_attr_show,
1635 .store_attribute = target_core_dev_pr_attr_store,
1638 static struct config_item_type target_core_dev_pr_cit = {
1639 .ct_item_ops = &target_core_dev_pr_ops,
1640 .ct_attrs = target_core_dev_pr_attrs,
1641 .ct_owner = THIS_MODULE,
1644 /* End functions for struct config_item_type target_core_dev_pr_cit */
1646 /* Start functions for struct config_item_type target_core_dev_cit */
1648 static ssize_t target_core_show_dev_info(void *p, char *page)
1650 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1651 struct se_hba *hba = se_dev->se_dev_hba;
1652 struct se_subsystem_api *t = hba->transport;
1653 int bl = 0;
1654 ssize_t read_bytes = 0;
1656 if (!se_dev->se_dev_ptr)
1657 return -ENODEV;
1659 transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1660 read_bytes += bl;
1661 read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1662 return read_bytes;
1665 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1666 .attr = { .ca_owner = THIS_MODULE,
1667 .ca_name = "info",
1668 .ca_mode = S_IRUGO },
1669 .show = target_core_show_dev_info,
1670 .store = NULL,
1673 static ssize_t target_core_store_dev_control(
1674 void *p,
1675 const char *page,
1676 size_t count)
1678 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1679 struct se_hba *hba = se_dev->se_dev_hba;
1680 struct se_subsystem_api *t = hba->transport;
1682 if (!se_dev->se_dev_su_ptr) {
1683 pr_err("Unable to locate struct se_subsystem_dev>se"
1684 "_dev_su_ptr\n");
1685 return -EINVAL;
1688 return t->set_configfs_dev_params(hba, se_dev, page, count);
1691 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1692 .attr = { .ca_owner = THIS_MODULE,
1693 .ca_name = "control",
1694 .ca_mode = S_IWUSR },
1695 .show = NULL,
1696 .store = target_core_store_dev_control,
1699 static ssize_t target_core_show_dev_alias(void *p, char *page)
1701 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1703 if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1704 return 0;
1706 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1709 static ssize_t target_core_store_dev_alias(
1710 void *p,
1711 const char *page,
1712 size_t count)
1714 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1715 struct se_hba *hba = se_dev->se_dev_hba;
1716 ssize_t read_bytes;
1718 if (count > (SE_DEV_ALIAS_LEN-1)) {
1719 pr_err("alias count: %d exceeds"
1720 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1721 SE_DEV_ALIAS_LEN-1);
1722 return -EINVAL;
1725 se_dev->su_dev_flags |= SDF_USING_ALIAS;
1726 read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1727 "%s", page);
1729 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1730 config_item_name(&hba->hba_group.cg_item),
1731 config_item_name(&se_dev->se_dev_group.cg_item),
1732 se_dev->se_dev_alias);
1734 return read_bytes;
1737 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1738 .attr = { .ca_owner = THIS_MODULE,
1739 .ca_name = "alias",
1740 .ca_mode = S_IRUGO | S_IWUSR },
1741 .show = target_core_show_dev_alias,
1742 .store = target_core_store_dev_alias,
1745 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1747 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1749 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH))
1750 return 0;
1752 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_udev_path);
1755 static ssize_t target_core_store_dev_udev_path(
1756 void *p,
1757 const char *page,
1758 size_t count)
1760 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1761 struct se_hba *hba = se_dev->se_dev_hba;
1762 ssize_t read_bytes;
1764 if (count > (SE_UDEV_PATH_LEN-1)) {
1765 pr_err("udev_path count: %d exceeds"
1766 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1767 SE_UDEV_PATH_LEN-1);
1768 return -EINVAL;
1771 se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1772 read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1773 "%s", page);
1775 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1776 config_item_name(&hba->hba_group.cg_item),
1777 config_item_name(&se_dev->se_dev_group.cg_item),
1778 se_dev->se_dev_udev_path);
1780 return read_bytes;
1783 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1784 .attr = { .ca_owner = THIS_MODULE,
1785 .ca_name = "udev_path",
1786 .ca_mode = S_IRUGO | S_IWUSR },
1787 .show = target_core_show_dev_udev_path,
1788 .store = target_core_store_dev_udev_path,
1791 static ssize_t target_core_store_dev_enable(
1792 void *p,
1793 const char *page,
1794 size_t count)
1796 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1797 struct se_device *dev;
1798 struct se_hba *hba = se_dev->se_dev_hba;
1799 struct se_subsystem_api *t = hba->transport;
1800 char *ptr;
1802 ptr = strstr(page, "1");
1803 if (!ptr) {
1804 pr_err("For dev_enable ops, only valid value"
1805 " is \"1\"\n");
1806 return -EINVAL;
1808 if (se_dev->se_dev_ptr) {
1809 pr_err("se_dev->se_dev_ptr already set for storage"
1810 " object\n");
1811 return -EEXIST;
1814 if (t->check_configfs_dev_params(hba, se_dev) < 0)
1815 return -EINVAL;
1817 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1818 if (IS_ERR(dev))
1819 return PTR_ERR(dev);
1820 else if (!dev)
1821 return -EINVAL;
1823 se_dev->se_dev_ptr = dev;
1824 pr_debug("Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1825 " %p\n", se_dev->se_dev_ptr);
1827 return count;
1830 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1831 .attr = { .ca_owner = THIS_MODULE,
1832 .ca_name = "enable",
1833 .ca_mode = S_IWUSR },
1834 .show = NULL,
1835 .store = target_core_store_dev_enable,
1838 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1840 struct se_device *dev;
1841 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1842 struct config_item *lu_ci;
1843 struct t10_alua_lu_gp *lu_gp;
1844 struct t10_alua_lu_gp_member *lu_gp_mem;
1845 ssize_t len = 0;
1847 dev = su_dev->se_dev_ptr;
1848 if (!dev)
1849 return -ENODEV;
1851 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED)
1852 return len;
1854 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1855 if (!lu_gp_mem) {
1856 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1857 " pointer\n");
1858 return -EINVAL;
1861 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1862 lu_gp = lu_gp_mem->lu_gp;
1863 if (lu_gp) {
1864 lu_ci = &lu_gp->lu_gp_group.cg_item;
1865 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1866 config_item_name(lu_ci), lu_gp->lu_gp_id);
1868 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1870 return len;
1873 static ssize_t target_core_store_alua_lu_gp(
1874 void *p,
1875 const char *page,
1876 size_t count)
1878 struct se_device *dev;
1879 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1880 struct se_hba *hba = su_dev->se_dev_hba;
1881 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1882 struct t10_alua_lu_gp_member *lu_gp_mem;
1883 unsigned char buf[LU_GROUP_NAME_BUF];
1884 int move = 0;
1886 dev = su_dev->se_dev_ptr;
1887 if (!dev)
1888 return -ENODEV;
1890 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1891 pr_warn("SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1892 config_item_name(&hba->hba_group.cg_item),
1893 config_item_name(&su_dev->se_dev_group.cg_item));
1894 return -EINVAL;
1896 if (count > LU_GROUP_NAME_BUF) {
1897 pr_err("ALUA LU Group Alias too large!\n");
1898 return -EINVAL;
1900 memset(buf, 0, LU_GROUP_NAME_BUF);
1901 memcpy(buf, page, count);
1903 * Any ALUA logical unit alias besides "NULL" means we will be
1904 * making a new group association.
1906 if (strcmp(strstrip(buf), "NULL")) {
1908 * core_alua_get_lu_gp_by_name() will increment reference to
1909 * struct t10_alua_lu_gp. This reference is released with
1910 * core_alua_get_lu_gp_by_name below().
1912 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1913 if (!lu_gp_new)
1914 return -ENODEV;
1916 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1917 if (!lu_gp_mem) {
1918 if (lu_gp_new)
1919 core_alua_put_lu_gp_from_name(lu_gp_new);
1920 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1921 " pointer\n");
1922 return -EINVAL;
1925 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1926 lu_gp = lu_gp_mem->lu_gp;
1927 if (lu_gp) {
1929 * Clearing an existing lu_gp association, and replacing
1930 * with NULL
1932 if (!lu_gp_new) {
1933 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1934 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1935 " %hu\n",
1936 config_item_name(&hba->hba_group.cg_item),
1937 config_item_name(&su_dev->se_dev_group.cg_item),
1938 config_item_name(&lu_gp->lu_gp_group.cg_item),
1939 lu_gp->lu_gp_id);
1941 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1942 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1944 return count;
1947 * Removing existing association of lu_gp_mem with lu_gp
1949 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1950 move = 1;
1953 * Associate lu_gp_mem with lu_gp_new.
1955 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1956 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1958 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1959 " core/alua/lu_gps/%s, ID: %hu\n",
1960 (move) ? "Moving" : "Adding",
1961 config_item_name(&hba->hba_group.cg_item),
1962 config_item_name(&su_dev->se_dev_group.cg_item),
1963 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1964 lu_gp_new->lu_gp_id);
1966 core_alua_put_lu_gp_from_name(lu_gp_new);
1967 return count;
1970 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1971 .attr = { .ca_owner = THIS_MODULE,
1972 .ca_name = "alua_lu_gp",
1973 .ca_mode = S_IRUGO | S_IWUSR },
1974 .show = target_core_show_alua_lu_gp,
1975 .store = target_core_store_alua_lu_gp,
1978 static struct configfs_attribute *lio_core_dev_attrs[] = {
1979 &target_core_attr_dev_info.attr,
1980 &target_core_attr_dev_control.attr,
1981 &target_core_attr_dev_alias.attr,
1982 &target_core_attr_dev_udev_path.attr,
1983 &target_core_attr_dev_enable.attr,
1984 &target_core_attr_dev_alua_lu_gp.attr,
1985 NULL,
1988 static void target_core_dev_release(struct config_item *item)
1990 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
1991 struct se_subsystem_dev, se_dev_group);
1992 struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
1993 struct se_subsystem_api *t = hba->transport;
1994 struct config_group *dev_cg = &se_dev->se_dev_group;
1996 kfree(dev_cg->default_groups);
1998 * This pointer will set when the storage is enabled with:
1999 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
2001 if (se_dev->se_dev_ptr) {
2002 pr_debug("Target_Core_ConfigFS: Calling se_free_"
2003 "virtual_device() for se_dev_ptr: %p\n",
2004 se_dev->se_dev_ptr);
2006 se_free_virtual_device(se_dev->se_dev_ptr, hba);
2007 } else {
2009 * Release struct se_subsystem_dev->se_dev_su_ptr..
2011 pr_debug("Target_Core_ConfigFS: Calling t->free_"
2012 "device() for se_dev_su_ptr: %p\n",
2013 se_dev->se_dev_su_ptr);
2015 t->free_device(se_dev->se_dev_su_ptr);
2018 pr_debug("Target_Core_ConfigFS: Deallocating se_subsystem"
2019 "_dev_t: %p\n", se_dev);
2020 kfree(se_dev);
2023 static ssize_t target_core_dev_show(struct config_item *item,
2024 struct configfs_attribute *attr,
2025 char *page)
2027 struct se_subsystem_dev *se_dev = container_of(
2028 to_config_group(item), struct se_subsystem_dev,
2029 se_dev_group);
2030 struct target_core_configfs_attribute *tc_attr = container_of(
2031 attr, struct target_core_configfs_attribute, attr);
2033 if (!tc_attr->show)
2034 return -EINVAL;
2036 return tc_attr->show(se_dev, page);
2039 static ssize_t target_core_dev_store(struct config_item *item,
2040 struct configfs_attribute *attr,
2041 const char *page, size_t count)
2043 struct se_subsystem_dev *se_dev = container_of(
2044 to_config_group(item), struct se_subsystem_dev,
2045 se_dev_group);
2046 struct target_core_configfs_attribute *tc_attr = container_of(
2047 attr, struct target_core_configfs_attribute, attr);
2049 if (!tc_attr->store)
2050 return -EINVAL;
2052 return tc_attr->store(se_dev, page, count);
2055 static struct configfs_item_operations target_core_dev_item_ops = {
2056 .release = target_core_dev_release,
2057 .show_attribute = target_core_dev_show,
2058 .store_attribute = target_core_dev_store,
2061 static struct config_item_type target_core_dev_cit = {
2062 .ct_item_ops = &target_core_dev_item_ops,
2063 .ct_attrs = lio_core_dev_attrs,
2064 .ct_owner = THIS_MODULE,
2067 /* End functions for struct config_item_type target_core_dev_cit */
2069 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2071 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2072 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2073 static struct target_core_alua_lu_gp_attribute \
2074 target_core_alua_lu_gp_##_name = \
2075 __CONFIGFS_EATTR(_name, _mode, \
2076 target_core_alua_lu_gp_show_attr_##_name, \
2077 target_core_alua_lu_gp_store_attr_##_name);
2079 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2080 static struct target_core_alua_lu_gp_attribute \
2081 target_core_alua_lu_gp_##_name = \
2082 __CONFIGFS_EATTR_RO(_name, \
2083 target_core_alua_lu_gp_show_attr_##_name);
2086 * lu_gp_id
2088 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2089 struct t10_alua_lu_gp *lu_gp,
2090 char *page)
2092 if (!lu_gp->lu_gp_valid_id)
2093 return 0;
2095 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2098 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2099 struct t10_alua_lu_gp *lu_gp,
2100 const char *page,
2101 size_t count)
2103 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2104 unsigned long lu_gp_id;
2105 int ret;
2107 ret = strict_strtoul(page, 0, &lu_gp_id);
2108 if (ret < 0) {
2109 pr_err("strict_strtoul() returned %d for"
2110 " lu_gp_id\n", ret);
2111 return -EINVAL;
2113 if (lu_gp_id > 0x0000ffff) {
2114 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2115 " 0x0000ffff\n", lu_gp_id);
2116 return -EINVAL;
2119 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2120 if (ret < 0)
2121 return -EINVAL;
2123 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2124 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2125 config_item_name(&alua_lu_gp_cg->cg_item),
2126 lu_gp->lu_gp_id);
2128 return count;
2131 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2134 * members
2136 static ssize_t target_core_alua_lu_gp_show_attr_members(
2137 struct t10_alua_lu_gp *lu_gp,
2138 char *page)
2140 struct se_device *dev;
2141 struct se_hba *hba;
2142 struct se_subsystem_dev *su_dev;
2143 struct t10_alua_lu_gp_member *lu_gp_mem;
2144 ssize_t len = 0, cur_len;
2145 unsigned char buf[LU_GROUP_NAME_BUF];
2147 memset(buf, 0, LU_GROUP_NAME_BUF);
2149 spin_lock(&lu_gp->lu_gp_lock);
2150 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2151 dev = lu_gp_mem->lu_gp_mem_dev;
2152 su_dev = dev->se_sub_dev;
2153 hba = su_dev->se_dev_hba;
2155 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2156 config_item_name(&hba->hba_group.cg_item),
2157 config_item_name(&su_dev->se_dev_group.cg_item));
2158 cur_len++; /* Extra byte for NULL terminator */
2160 if ((cur_len + len) > PAGE_SIZE) {
2161 pr_warn("Ran out of lu_gp_show_attr"
2162 "_members buffer\n");
2163 break;
2165 memcpy(page+len, buf, cur_len);
2166 len += cur_len;
2168 spin_unlock(&lu_gp->lu_gp_lock);
2170 return len;
2173 SE_DEV_ALUA_LU_ATTR_RO(members);
2175 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2177 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2178 &target_core_alua_lu_gp_lu_gp_id.attr,
2179 &target_core_alua_lu_gp_members.attr,
2180 NULL,
2183 static void target_core_alua_lu_gp_release(struct config_item *item)
2185 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2186 struct t10_alua_lu_gp, lu_gp_group);
2188 core_alua_free_lu_gp(lu_gp);
2191 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2192 .release = target_core_alua_lu_gp_release,
2193 .show_attribute = target_core_alua_lu_gp_attr_show,
2194 .store_attribute = target_core_alua_lu_gp_attr_store,
2197 static struct config_item_type target_core_alua_lu_gp_cit = {
2198 .ct_item_ops = &target_core_alua_lu_gp_ops,
2199 .ct_attrs = target_core_alua_lu_gp_attrs,
2200 .ct_owner = THIS_MODULE,
2203 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2205 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2207 static struct config_group *target_core_alua_create_lu_gp(
2208 struct config_group *group,
2209 const char *name)
2211 struct t10_alua_lu_gp *lu_gp;
2212 struct config_group *alua_lu_gp_cg = NULL;
2213 struct config_item *alua_lu_gp_ci = NULL;
2215 lu_gp = core_alua_allocate_lu_gp(name, 0);
2216 if (IS_ERR(lu_gp))
2217 return NULL;
2219 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2220 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2222 config_group_init_type_name(alua_lu_gp_cg, name,
2223 &target_core_alua_lu_gp_cit);
2225 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2226 " Group: core/alua/lu_gps/%s\n",
2227 config_item_name(alua_lu_gp_ci));
2229 return alua_lu_gp_cg;
2233 static void target_core_alua_drop_lu_gp(
2234 struct config_group *group,
2235 struct config_item *item)
2237 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2238 struct t10_alua_lu_gp, lu_gp_group);
2240 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2241 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2242 config_item_name(item), lu_gp->lu_gp_id);
2244 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2245 * -> target_core_alua_lu_gp_release()
2247 config_item_put(item);
2250 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2251 .make_group = &target_core_alua_create_lu_gp,
2252 .drop_item = &target_core_alua_drop_lu_gp,
2255 static struct config_item_type target_core_alua_lu_gps_cit = {
2256 .ct_item_ops = NULL,
2257 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2258 .ct_owner = THIS_MODULE,
2261 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2263 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2265 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2266 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2267 static struct target_core_alua_tg_pt_gp_attribute \
2268 target_core_alua_tg_pt_gp_##_name = \
2269 __CONFIGFS_EATTR(_name, _mode, \
2270 target_core_alua_tg_pt_gp_show_attr_##_name, \
2271 target_core_alua_tg_pt_gp_store_attr_##_name);
2273 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2274 static struct target_core_alua_tg_pt_gp_attribute \
2275 target_core_alua_tg_pt_gp_##_name = \
2276 __CONFIGFS_EATTR_RO(_name, \
2277 target_core_alua_tg_pt_gp_show_attr_##_name);
2280 * alua_access_state
2282 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2283 struct t10_alua_tg_pt_gp *tg_pt_gp,
2284 char *page)
2286 return sprintf(page, "%d\n",
2287 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2290 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2291 struct t10_alua_tg_pt_gp *tg_pt_gp,
2292 const char *page,
2293 size_t count)
2295 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2296 unsigned long tmp;
2297 int new_state, ret;
2299 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2300 pr_err("Unable to do implict ALUA on non valid"
2301 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2302 return -EINVAL;
2305 ret = strict_strtoul(page, 0, &tmp);
2306 if (ret < 0) {
2307 pr_err("Unable to extract new ALUA access state from"
2308 " %s\n", page);
2309 return -EINVAL;
2311 new_state = (int)tmp;
2313 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2314 pr_err("Unable to process implict configfs ALUA"
2315 " transition while TPGS_IMPLICT_ALUA is diabled\n");
2316 return -EINVAL;
2319 ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2320 NULL, NULL, new_state, 0);
2321 return (!ret) ? count : -EINVAL;
2324 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2327 * alua_access_status
2329 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2330 struct t10_alua_tg_pt_gp *tg_pt_gp,
2331 char *page)
2333 return sprintf(page, "%s\n",
2334 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2337 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2338 struct t10_alua_tg_pt_gp *tg_pt_gp,
2339 const char *page,
2340 size_t count)
2342 unsigned long tmp;
2343 int new_status, ret;
2345 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2346 pr_err("Unable to do set ALUA access status on non"
2347 " valid tg_pt_gp ID: %hu\n",
2348 tg_pt_gp->tg_pt_gp_valid_id);
2349 return -EINVAL;
2352 ret = strict_strtoul(page, 0, &tmp);
2353 if (ret < 0) {
2354 pr_err("Unable to extract new ALUA access status"
2355 " from %s\n", page);
2356 return -EINVAL;
2358 new_status = (int)tmp;
2360 if ((new_status != ALUA_STATUS_NONE) &&
2361 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2362 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2363 pr_err("Illegal ALUA access status: 0x%02x\n",
2364 new_status);
2365 return -EINVAL;
2368 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2369 return count;
2372 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2375 * alua_access_type
2377 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2378 struct t10_alua_tg_pt_gp *tg_pt_gp,
2379 char *page)
2381 return core_alua_show_access_type(tg_pt_gp, page);
2384 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2385 struct t10_alua_tg_pt_gp *tg_pt_gp,
2386 const char *page,
2387 size_t count)
2389 return core_alua_store_access_type(tg_pt_gp, page, count);
2392 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2395 * alua_write_metadata
2397 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2398 struct t10_alua_tg_pt_gp *tg_pt_gp,
2399 char *page)
2401 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2404 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2405 struct t10_alua_tg_pt_gp *tg_pt_gp,
2406 const char *page,
2407 size_t count)
2409 unsigned long tmp;
2410 int ret;
2412 ret = strict_strtoul(page, 0, &tmp);
2413 if (ret < 0) {
2414 pr_err("Unable to extract alua_write_metadata\n");
2415 return -EINVAL;
2418 if ((tmp != 0) && (tmp != 1)) {
2419 pr_err("Illegal value for alua_write_metadata:"
2420 " %lu\n", tmp);
2421 return -EINVAL;
2423 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2425 return count;
2428 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2433 * nonop_delay_msecs
2435 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2436 struct t10_alua_tg_pt_gp *tg_pt_gp,
2437 char *page)
2439 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2443 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2444 struct t10_alua_tg_pt_gp *tg_pt_gp,
2445 const char *page,
2446 size_t count)
2448 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2451 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2454 * trans_delay_msecs
2456 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2457 struct t10_alua_tg_pt_gp *tg_pt_gp,
2458 char *page)
2460 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2463 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2464 struct t10_alua_tg_pt_gp *tg_pt_gp,
2465 const char *page,
2466 size_t count)
2468 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2471 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2474 * preferred
2477 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2478 struct t10_alua_tg_pt_gp *tg_pt_gp,
2479 char *page)
2481 return core_alua_show_preferred_bit(tg_pt_gp, page);
2484 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2485 struct t10_alua_tg_pt_gp *tg_pt_gp,
2486 const char *page,
2487 size_t count)
2489 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2492 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2495 * tg_pt_gp_id
2497 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2498 struct t10_alua_tg_pt_gp *tg_pt_gp,
2499 char *page)
2501 if (!tg_pt_gp->tg_pt_gp_valid_id)
2502 return 0;
2504 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2507 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2508 struct t10_alua_tg_pt_gp *tg_pt_gp,
2509 const char *page,
2510 size_t count)
2512 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2513 unsigned long tg_pt_gp_id;
2514 int ret;
2516 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2517 if (ret < 0) {
2518 pr_err("strict_strtoul() returned %d for"
2519 " tg_pt_gp_id\n", ret);
2520 return -EINVAL;
2522 if (tg_pt_gp_id > 0x0000ffff) {
2523 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2524 " 0x0000ffff\n", tg_pt_gp_id);
2525 return -EINVAL;
2528 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2529 if (ret < 0)
2530 return -EINVAL;
2532 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2533 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2534 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2535 tg_pt_gp->tg_pt_gp_id);
2537 return count;
2540 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2543 * members
2545 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2546 struct t10_alua_tg_pt_gp *tg_pt_gp,
2547 char *page)
2549 struct se_port *port;
2550 struct se_portal_group *tpg;
2551 struct se_lun *lun;
2552 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2553 ssize_t len = 0, cur_len;
2554 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2556 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2558 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2559 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2560 tg_pt_gp_mem_list) {
2561 port = tg_pt_gp_mem->tg_pt;
2562 tpg = port->sep_tpg;
2563 lun = port->sep_lun;
2565 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2566 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2567 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2568 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2569 config_item_name(&lun->lun_group.cg_item));
2570 cur_len++; /* Extra byte for NULL terminator */
2572 if ((cur_len + len) > PAGE_SIZE) {
2573 pr_warn("Ran out of lu_gp_show_attr"
2574 "_members buffer\n");
2575 break;
2577 memcpy(page+len, buf, cur_len);
2578 len += cur_len;
2580 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2582 return len;
2585 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2587 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2588 tg_pt_gp_group);
2590 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2591 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2592 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2593 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2594 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2595 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2596 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2597 &target_core_alua_tg_pt_gp_preferred.attr,
2598 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2599 &target_core_alua_tg_pt_gp_members.attr,
2600 NULL,
2603 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2605 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2606 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2608 core_alua_free_tg_pt_gp(tg_pt_gp);
2611 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2612 .release = target_core_alua_tg_pt_gp_release,
2613 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2614 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2617 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2618 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2619 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2620 .ct_owner = THIS_MODULE,
2623 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2625 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2627 static struct config_group *target_core_alua_create_tg_pt_gp(
2628 struct config_group *group,
2629 const char *name)
2631 struct t10_alua *alua = container_of(group, struct t10_alua,
2632 alua_tg_pt_gps_group);
2633 struct t10_alua_tg_pt_gp *tg_pt_gp;
2634 struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2635 struct config_group *alua_tg_pt_gp_cg = NULL;
2636 struct config_item *alua_tg_pt_gp_ci = NULL;
2638 tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2639 if (!tg_pt_gp)
2640 return NULL;
2642 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2643 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2645 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2646 &target_core_alua_tg_pt_gp_cit);
2648 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2649 " Group: alua/tg_pt_gps/%s\n",
2650 config_item_name(alua_tg_pt_gp_ci));
2652 return alua_tg_pt_gp_cg;
2655 static void target_core_alua_drop_tg_pt_gp(
2656 struct config_group *group,
2657 struct config_item *item)
2659 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2660 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2662 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2663 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2664 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2666 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2667 * -> target_core_alua_tg_pt_gp_release().
2669 config_item_put(item);
2672 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2673 .make_group = &target_core_alua_create_tg_pt_gp,
2674 .drop_item = &target_core_alua_drop_tg_pt_gp,
2677 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2678 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2679 .ct_owner = THIS_MODULE,
2682 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2684 /* Start functions for struct config_item_type target_core_alua_cit */
2687 * target_core_alua_cit is a ConfigFS group that lives under
2688 * /sys/kernel/config/target/core/alua. There are default groups
2689 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2690 * target_core_alua_cit in target_core_init_configfs() below.
2692 static struct config_item_type target_core_alua_cit = {
2693 .ct_item_ops = NULL,
2694 .ct_attrs = NULL,
2695 .ct_owner = THIS_MODULE,
2698 /* End functions for struct config_item_type target_core_alua_cit */
2700 /* Start functions for struct config_item_type target_core_stat_cit */
2702 static struct config_group *target_core_stat_mkdir(
2703 struct config_group *group,
2704 const char *name)
2706 return ERR_PTR(-ENOSYS);
2709 static void target_core_stat_rmdir(
2710 struct config_group *group,
2711 struct config_item *item)
2713 return;
2716 static struct configfs_group_operations target_core_stat_group_ops = {
2717 .make_group = &target_core_stat_mkdir,
2718 .drop_item = &target_core_stat_rmdir,
2721 static struct config_item_type target_core_stat_cit = {
2722 .ct_group_ops = &target_core_stat_group_ops,
2723 .ct_owner = THIS_MODULE,
2726 /* End functions for struct config_item_type target_core_stat_cit */
2728 /* Start functions for struct config_item_type target_core_hba_cit */
2730 static struct config_group *target_core_make_subdev(
2731 struct config_group *group,
2732 const char *name)
2734 struct t10_alua_tg_pt_gp *tg_pt_gp;
2735 struct se_subsystem_dev *se_dev;
2736 struct se_subsystem_api *t;
2737 struct config_item *hba_ci = &group->cg_item;
2738 struct se_hba *hba = item_to_hba(hba_ci);
2739 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2740 struct config_group *dev_stat_grp = NULL;
2741 int errno = -ENOMEM, ret;
2743 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2744 if (ret)
2745 return ERR_PTR(ret);
2747 * Locate the struct se_subsystem_api from parent's struct se_hba.
2749 t = hba->transport;
2751 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2752 if (!se_dev) {
2753 pr_err("Unable to allocate memory for"
2754 " struct se_subsystem_dev\n");
2755 goto unlock;
2757 INIT_LIST_HEAD(&se_dev->se_dev_node);
2758 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2759 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2760 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
2761 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
2762 spin_lock_init(&se_dev->t10_pr.registration_lock);
2763 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
2764 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2765 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2766 spin_lock_init(&se_dev->se_dev_lock);
2767 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2768 se_dev->t10_wwn.t10_sub_dev = se_dev;
2769 se_dev->t10_alua.t10_sub_dev = se_dev;
2770 se_dev->se_dev_attrib.da_sub_dev = se_dev;
2772 se_dev->se_dev_hba = hba;
2773 dev_cg = &se_dev->se_dev_group;
2775 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2776 GFP_KERNEL);
2777 if (!dev_cg->default_groups)
2778 goto out;
2780 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2781 * for ->allocate_virtdevice()
2783 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2784 * has been called successfully in the next level up in the
2785 * configfs tree for device object's struct config_group.
2787 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2788 if (!se_dev->se_dev_su_ptr) {
2789 pr_err("Unable to locate subsystem dependent pointer"
2790 " from allocate_virtdevice()\n");
2791 goto out;
2793 spin_lock(&se_device_lock);
2794 list_add_tail(&se_dev->se_dev_node, &se_dev_list);
2795 spin_unlock(&se_device_lock);
2797 config_group_init_type_name(&se_dev->se_dev_group, name,
2798 &target_core_dev_cit);
2799 config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2800 &target_core_dev_attrib_cit);
2801 config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2802 &target_core_dev_pr_cit);
2803 config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2804 &target_core_dev_wwn_cit);
2805 config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2806 "alua", &target_core_alua_tg_pt_gps_cit);
2807 config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2808 "statistics", &target_core_stat_cit);
2810 dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2811 dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2812 dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2813 dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2814 dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2815 dev_cg->default_groups[5] = NULL;
2817 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2819 tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2820 if (!tg_pt_gp)
2821 goto out;
2823 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2824 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2825 GFP_KERNEL);
2826 if (!tg_pt_gp_cg->default_groups) {
2827 pr_err("Unable to allocate tg_pt_gp_cg->"
2828 "default_groups\n");
2829 goto out;
2832 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2833 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2834 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2835 tg_pt_gp_cg->default_groups[1] = NULL;
2836 se_dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2838 * Add core/$HBA/$DEV/statistics/ default groups
2840 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2841 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2842 GFP_KERNEL);
2843 if (!dev_stat_grp->default_groups) {
2844 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2845 goto out;
2847 target_stat_setup_dev_default_groups(se_dev);
2849 pr_debug("Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2850 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2852 mutex_unlock(&hba->hba_access_mutex);
2853 return &se_dev->se_dev_group;
2854 out:
2855 if (se_dev->t10_alua.default_tg_pt_gp) {
2856 core_alua_free_tg_pt_gp(se_dev->t10_alua.default_tg_pt_gp);
2857 se_dev->t10_alua.default_tg_pt_gp = NULL;
2859 if (dev_stat_grp)
2860 kfree(dev_stat_grp->default_groups);
2861 if (tg_pt_gp_cg)
2862 kfree(tg_pt_gp_cg->default_groups);
2863 if (dev_cg)
2864 kfree(dev_cg->default_groups);
2865 if (se_dev->se_dev_su_ptr)
2866 t->free_device(se_dev->se_dev_su_ptr);
2867 kfree(se_dev);
2868 unlock:
2869 mutex_unlock(&hba->hba_access_mutex);
2870 return ERR_PTR(errno);
2873 static void target_core_drop_subdev(
2874 struct config_group *group,
2875 struct config_item *item)
2877 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2878 struct se_subsystem_dev, se_dev_group);
2879 struct se_hba *hba;
2880 struct se_subsystem_api *t;
2881 struct config_item *df_item;
2882 struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2883 int i;
2885 hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2887 mutex_lock(&hba->hba_access_mutex);
2888 t = hba->transport;
2890 spin_lock(&se_device_lock);
2891 list_del(&se_dev->se_dev_node);
2892 spin_unlock(&se_device_lock);
2894 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2895 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2896 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2897 dev_stat_grp->default_groups[i] = NULL;
2898 config_item_put(df_item);
2900 kfree(dev_stat_grp->default_groups);
2902 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2903 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2904 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2905 tg_pt_gp_cg->default_groups[i] = NULL;
2906 config_item_put(df_item);
2908 kfree(tg_pt_gp_cg->default_groups);
2910 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2911 * directly from target_core_alua_tg_pt_gp_release().
2913 se_dev->t10_alua.default_tg_pt_gp = NULL;
2915 dev_cg = &se_dev->se_dev_group;
2916 for (i = 0; dev_cg->default_groups[i]; i++) {
2917 df_item = &dev_cg->default_groups[i]->cg_item;
2918 dev_cg->default_groups[i] = NULL;
2919 config_item_put(df_item);
2922 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2923 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2925 config_item_put(item);
2926 mutex_unlock(&hba->hba_access_mutex);
2929 static struct configfs_group_operations target_core_hba_group_ops = {
2930 .make_group = target_core_make_subdev,
2931 .drop_item = target_core_drop_subdev,
2934 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2935 #define SE_HBA_ATTR(_name, _mode) \
2936 static struct target_core_hba_attribute \
2937 target_core_hba_##_name = \
2938 __CONFIGFS_EATTR(_name, _mode, \
2939 target_core_hba_show_attr_##_name, \
2940 target_core_hba_store_attr_##_name);
2942 #define SE_HBA_ATTR_RO(_name) \
2943 static struct target_core_hba_attribute \
2944 target_core_hba_##_name = \
2945 __CONFIGFS_EATTR_RO(_name, \
2946 target_core_hba_show_attr_##_name);
2948 static ssize_t target_core_hba_show_attr_hba_info(
2949 struct se_hba *hba,
2950 char *page)
2952 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2953 hba->hba_id, hba->transport->name,
2954 TARGET_CORE_CONFIGFS_VERSION);
2957 SE_HBA_ATTR_RO(hba_info);
2959 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2960 char *page)
2962 int hba_mode = 0;
2964 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2965 hba_mode = 1;
2967 return sprintf(page, "%d\n", hba_mode);
2970 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2971 const char *page, size_t count)
2973 struct se_subsystem_api *transport = hba->transport;
2974 unsigned long mode_flag;
2975 int ret;
2977 if (transport->pmode_enable_hba == NULL)
2978 return -EINVAL;
2980 ret = strict_strtoul(page, 0, &mode_flag);
2981 if (ret < 0) {
2982 pr_err("Unable to extract hba mode flag: %d\n", ret);
2983 return -EINVAL;
2986 spin_lock(&hba->device_lock);
2987 if (!list_empty(&hba->hba_dev_list)) {
2988 pr_err("Unable to set hba_mode with active devices\n");
2989 spin_unlock(&hba->device_lock);
2990 return -EINVAL;
2992 spin_unlock(&hba->device_lock);
2994 ret = transport->pmode_enable_hba(hba, mode_flag);
2995 if (ret < 0)
2996 return -EINVAL;
2997 if (ret > 0)
2998 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2999 else if (ret == 0)
3000 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3002 return count;
3005 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3007 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3009 static void target_core_hba_release(struct config_item *item)
3011 struct se_hba *hba = container_of(to_config_group(item),
3012 struct se_hba, hba_group);
3013 core_delete_hba(hba);
3016 static struct configfs_attribute *target_core_hba_attrs[] = {
3017 &target_core_hba_hba_info.attr,
3018 &target_core_hba_hba_mode.attr,
3019 NULL,
3022 static struct configfs_item_operations target_core_hba_item_ops = {
3023 .release = target_core_hba_release,
3024 .show_attribute = target_core_hba_attr_show,
3025 .store_attribute = target_core_hba_attr_store,
3028 static struct config_item_type target_core_hba_cit = {
3029 .ct_item_ops = &target_core_hba_item_ops,
3030 .ct_group_ops = &target_core_hba_group_ops,
3031 .ct_attrs = target_core_hba_attrs,
3032 .ct_owner = THIS_MODULE,
3035 static struct config_group *target_core_call_addhbatotarget(
3036 struct config_group *group,
3037 const char *name)
3039 char *se_plugin_str, *str, *str2;
3040 struct se_hba *hba;
3041 char buf[TARGET_CORE_NAME_MAX_LEN];
3042 unsigned long plugin_dep_id = 0;
3043 int ret;
3045 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3046 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3047 pr_err("Passed *name strlen(): %d exceeds"
3048 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3049 TARGET_CORE_NAME_MAX_LEN);
3050 return ERR_PTR(-ENAMETOOLONG);
3052 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3054 str = strstr(buf, "_");
3055 if (!str) {
3056 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3057 return ERR_PTR(-EINVAL);
3059 se_plugin_str = buf;
3061 * Special case for subsystem plugins that have "_" in their names.
3062 * Namely rd_direct and rd_mcp..
3064 str2 = strstr(str+1, "_");
3065 if (str2) {
3066 *str2 = '\0'; /* Terminate for *se_plugin_str */
3067 str2++; /* Skip to start of plugin dependent ID */
3068 str = str2;
3069 } else {
3070 *str = '\0'; /* Terminate for *se_plugin_str */
3071 str++; /* Skip to start of plugin dependent ID */
3074 ret = strict_strtoul(str, 0, &plugin_dep_id);
3075 if (ret < 0) {
3076 pr_err("strict_strtoul() returned %d for"
3077 " plugin_dep_id\n", ret);
3078 return ERR_PTR(-EINVAL);
3081 * Load up TCM subsystem plugins if they have not already been loaded.
3083 if (transport_subsystem_check_init() < 0)
3084 return ERR_PTR(-EINVAL);
3086 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3087 if (IS_ERR(hba))
3088 return ERR_CAST(hba);
3090 config_group_init_type_name(&hba->hba_group, name,
3091 &target_core_hba_cit);
3093 return &hba->hba_group;
3096 static void target_core_call_delhbafromtarget(
3097 struct config_group *group,
3098 struct config_item *item)
3101 * core_delete_hba() is called from target_core_hba_item_ops->release()
3102 * -> target_core_hba_release()
3104 config_item_put(item);
3107 static struct configfs_group_operations target_core_group_ops = {
3108 .make_group = target_core_call_addhbatotarget,
3109 .drop_item = target_core_call_delhbafromtarget,
3112 static struct config_item_type target_core_cit = {
3113 .ct_item_ops = NULL,
3114 .ct_group_ops = &target_core_group_ops,
3115 .ct_attrs = NULL,
3116 .ct_owner = THIS_MODULE,
3119 /* Stop functions for struct config_item_type target_core_hba_cit */
3121 static int __init target_core_init_configfs(void)
3123 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3124 struct config_group *lu_gp_cg = NULL;
3125 struct configfs_subsystem *subsys;
3126 struct t10_alua_lu_gp *lu_gp;
3127 int ret;
3129 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3130 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3131 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3133 subsys = target_core_subsystem[0];
3134 config_group_init(&subsys->su_group);
3135 mutex_init(&subsys->su_mutex);
3137 INIT_LIST_HEAD(&g_tf_list);
3138 mutex_init(&g_tf_lock);
3139 ret = init_se_kmem_caches();
3140 if (ret < 0)
3141 return ret;
3143 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3144 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3146 target_cg = &subsys->su_group;
3147 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3148 GFP_KERNEL);
3149 if (!target_cg->default_groups) {
3150 pr_err("Unable to allocate target_cg->default_groups\n");
3151 goto out_global;
3154 config_group_init_type_name(&target_core_hbagroup,
3155 "core", &target_core_cit);
3156 target_cg->default_groups[0] = &target_core_hbagroup;
3157 target_cg->default_groups[1] = NULL;
3159 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3161 hba_cg = &target_core_hbagroup;
3162 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3163 GFP_KERNEL);
3164 if (!hba_cg->default_groups) {
3165 pr_err("Unable to allocate hba_cg->default_groups\n");
3166 goto out_global;
3168 config_group_init_type_name(&alua_group,
3169 "alua", &target_core_alua_cit);
3170 hba_cg->default_groups[0] = &alua_group;
3171 hba_cg->default_groups[1] = NULL;
3173 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3174 * groups under /sys/kernel/config/target/core/alua/
3176 alua_cg = &alua_group;
3177 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3178 GFP_KERNEL);
3179 if (!alua_cg->default_groups) {
3180 pr_err("Unable to allocate alua_cg->default_groups\n");
3181 goto out_global;
3184 config_group_init_type_name(&alua_lu_gps_group,
3185 "lu_gps", &target_core_alua_lu_gps_cit);
3186 alua_cg->default_groups[0] = &alua_lu_gps_group;
3187 alua_cg->default_groups[1] = NULL;
3189 * Add core/alua/lu_gps/default_lu_gp
3191 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3192 if (IS_ERR(lu_gp))
3193 goto out_global;
3195 lu_gp_cg = &alua_lu_gps_group;
3196 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3197 GFP_KERNEL);
3198 if (!lu_gp_cg->default_groups) {
3199 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
3200 goto out_global;
3203 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3204 &target_core_alua_lu_gp_cit);
3205 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3206 lu_gp_cg->default_groups[1] = NULL;
3207 default_lu_gp = lu_gp;
3209 * Register the target_core_mod subsystem with configfs.
3211 ret = configfs_register_subsystem(subsys);
3212 if (ret < 0) {
3213 pr_err("Error %d while registering subsystem %s\n",
3214 ret, subsys->su_group.cg_item.ci_namebuf);
3215 goto out_global;
3217 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3218 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3219 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3221 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3223 ret = rd_module_init();
3224 if (ret < 0)
3225 goto out;
3227 if (core_dev_setup_virtual_lun0() < 0)
3228 goto out;
3230 return 0;
3232 out:
3233 configfs_unregister_subsystem(subsys);
3234 core_dev_release_virtual_lun0();
3235 rd_module_exit();
3236 out_global:
3237 if (default_lu_gp) {
3238 core_alua_free_lu_gp(default_lu_gp);
3239 default_lu_gp = NULL;
3241 if (lu_gp_cg)
3242 kfree(lu_gp_cg->default_groups);
3243 if (alua_cg)
3244 kfree(alua_cg->default_groups);
3245 if (hba_cg)
3246 kfree(hba_cg->default_groups);
3247 kfree(target_cg->default_groups);
3248 release_se_kmem_caches();
3249 return ret;
3252 static void __exit target_core_exit_configfs(void)
3254 struct configfs_subsystem *subsys;
3255 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3256 struct config_item *item;
3257 int i;
3259 subsys = target_core_subsystem[0];
3261 lu_gp_cg = &alua_lu_gps_group;
3262 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3263 item = &lu_gp_cg->default_groups[i]->cg_item;
3264 lu_gp_cg->default_groups[i] = NULL;
3265 config_item_put(item);
3267 kfree(lu_gp_cg->default_groups);
3268 lu_gp_cg->default_groups = NULL;
3270 alua_cg = &alua_group;
3271 for (i = 0; alua_cg->default_groups[i]; i++) {
3272 item = &alua_cg->default_groups[i]->cg_item;
3273 alua_cg->default_groups[i] = NULL;
3274 config_item_put(item);
3276 kfree(alua_cg->default_groups);
3277 alua_cg->default_groups = NULL;
3279 hba_cg = &target_core_hbagroup;
3280 for (i = 0; hba_cg->default_groups[i]; i++) {
3281 item = &hba_cg->default_groups[i]->cg_item;
3282 hba_cg->default_groups[i] = NULL;
3283 config_item_put(item);
3285 kfree(hba_cg->default_groups);
3286 hba_cg->default_groups = NULL;
3288 * We expect subsys->su_group.default_groups to be released
3289 * by configfs subsystem provider logic..
3291 configfs_unregister_subsystem(subsys);
3292 kfree(subsys->su_group.default_groups);
3294 core_alua_free_lu_gp(default_lu_gp);
3295 default_lu_gp = NULL;
3297 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3298 " Infrastructure\n");
3300 core_dev_release_virtual_lun0();
3301 rd_module_exit();
3302 release_se_kmem_caches();
3305 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3306 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3307 MODULE_LICENSE("GPL");
3309 module_init(target_core_init_configfs);
3310 module_exit(target_core_exit_configfs);