i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / target / target_core_configfs.c
blob6e043eeb1db9c4449edfb4a4385184de1c0633fa
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 <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/namei.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/unistd.h>
35 #include <linux/string.h>
36 #include <linux/parser.h>
37 #include <linux/syscalls.h>
38 #include <linux/configfs.h>
39 #include <linux/spinlock.h>
41 #include <target/target_core_base.h>
42 #include <target/target_core_backend.h>
43 #include <target/target_core_fabric.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
48 #include "target_core_internal.h"
49 #include "target_core_alua.h"
50 #include "target_core_pr.h"
51 #include "target_core_rd.h"
53 extern struct t10_alua_lu_gp *default_lu_gp;
55 static struct list_head g_tf_list;
56 static struct mutex g_tf_lock;
58 struct target_core_configfs_attribute {
59 struct configfs_attribute attr;
60 ssize_t (*show)(void *, char *);
61 ssize_t (*store)(void *, const char *, size_t);
64 static struct config_group target_core_hbagroup;
65 static struct config_group alua_group;
66 static struct config_group alua_lu_gps_group;
68 static inline struct se_hba *
69 item_to_hba(struct config_item *item)
71 return container_of(to_config_group(item), struct se_hba, hba_group);
75 * Attributes for /sys/kernel/config/target/
77 static ssize_t target_core_attr_show(struct config_item *item,
78 struct configfs_attribute *attr,
79 char *page)
81 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
82 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
83 utsname()->sysname, utsname()->machine);
86 static struct configfs_item_operations target_core_fabric_item_ops = {
87 .show_attribute = target_core_attr_show,
90 static struct configfs_attribute target_core_item_attr_version = {
91 .ca_owner = THIS_MODULE,
92 .ca_name = "version",
93 .ca_mode = S_IRUGO,
96 static struct target_fabric_configfs *target_core_get_fabric(
97 const char *name)
99 struct target_fabric_configfs *tf;
101 if (!name)
102 return NULL;
104 mutex_lock(&g_tf_lock);
105 list_for_each_entry(tf, &g_tf_list, tf_list) {
106 if (!strcmp(tf->tf_name, name)) {
107 atomic_inc(&tf->tf_access_cnt);
108 mutex_unlock(&g_tf_lock);
109 return tf;
112 mutex_unlock(&g_tf_lock);
114 return NULL;
118 * Called from struct target_core_group_ops->make_group()
120 static struct config_group *target_core_register_fabric(
121 struct config_group *group,
122 const char *name)
124 struct target_fabric_configfs *tf;
125 int ret;
127 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
128 " %s\n", group, name);
130 * Below are some hardcoded request_module() calls to automatically
131 * local fabric modules when the following is called:
133 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
135 * Note that this does not limit which TCM fabric module can be
136 * registered, but simply provids auto loading logic for modules with
137 * mkdir(2) system calls with known TCM fabric modules.
139 if (!strncmp(name, "iscsi", 5)) {
141 * Automatically load the LIO Target fabric module when the
142 * following is called:
144 * mkdir -p $CONFIGFS/target/iscsi
146 ret = request_module("iscsi_target_mod");
147 if (ret < 0) {
148 pr_err("request_module() failed for"
149 " iscsi_target_mod.ko: %d\n", ret);
150 return ERR_PTR(-EINVAL);
152 } else if (!strncmp(name, "loopback", 8)) {
154 * Automatically load the tcm_loop fabric module when the
155 * following is called:
157 * mkdir -p $CONFIGFS/target/loopback
159 ret = request_module("tcm_loop");
160 if (ret < 0) {
161 pr_err("request_module() failed for"
162 " tcm_loop.ko: %d\n", ret);
163 return ERR_PTR(-EINVAL);
167 tf = target_core_get_fabric(name);
168 if (!tf) {
169 pr_err("target_core_get_fabric() failed for %s\n",
170 name);
171 return ERR_PTR(-EINVAL);
173 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
174 " %s\n", tf->tf_name);
176 * On a successful target_core_get_fabric() look, the returned
177 * struct target_fabric_configfs *tf will contain a usage reference.
179 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
180 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
182 tf->tf_group.default_groups = tf->tf_default_groups;
183 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
184 tf->tf_group.default_groups[1] = NULL;
186 config_group_init_type_name(&tf->tf_group, name,
187 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
188 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
189 &TF_CIT_TMPL(tf)->tfc_discovery_cit);
191 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
192 " %s\n", tf->tf_group.cg_item.ci_name);
194 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
196 tf->tf_ops.tf_subsys = tf->tf_subsys;
197 tf->tf_fabric = &tf->tf_group.cg_item;
198 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
199 " for %s\n", name);
201 return &tf->tf_group;
205 * Called from struct target_core_group_ops->drop_item()
207 static void target_core_deregister_fabric(
208 struct config_group *group,
209 struct config_item *item)
211 struct target_fabric_configfs *tf = container_of(
212 to_config_group(item), struct target_fabric_configfs, tf_group);
213 struct config_group *tf_group;
214 struct config_item *df_item;
215 int i;
217 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
218 " tf list\n", config_item_name(item));
220 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
221 " %s\n", tf->tf_name);
222 atomic_dec(&tf->tf_access_cnt);
224 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
225 " tf->tf_fabric for %s\n", tf->tf_name);
226 tf->tf_fabric = NULL;
228 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
229 " %s\n", config_item_name(item));
231 tf_group = &tf->tf_group;
232 for (i = 0; tf_group->default_groups[i]; i++) {
233 df_item = &tf_group->default_groups[i]->cg_item;
234 tf_group->default_groups[i] = NULL;
235 config_item_put(df_item);
237 config_item_put(item);
240 static struct configfs_group_operations target_core_fabric_group_ops = {
241 .make_group = &target_core_register_fabric,
242 .drop_item = &target_core_deregister_fabric,
246 * All item attributes appearing in /sys/kernel/target/ appear here.
248 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
249 &target_core_item_attr_version,
250 NULL,
254 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
256 static struct config_item_type target_core_fabrics_item = {
257 .ct_item_ops = &target_core_fabric_item_ops,
258 .ct_group_ops = &target_core_fabric_group_ops,
259 .ct_attrs = target_core_fabric_item_attrs,
260 .ct_owner = THIS_MODULE,
263 static struct configfs_subsystem target_core_fabrics = {
264 .su_group = {
265 .cg_item = {
266 .ci_namebuf = "target",
267 .ci_type = &target_core_fabrics_item,
272 static struct configfs_subsystem *target_core_subsystem[] = {
273 &target_core_fabrics,
274 NULL,
277 /*##############################################################################
278 // Start functions called by external Target Fabrics Modules
279 //############################################################################*/
282 * First function called by fabric modules to:
284 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
285 * 2) Add struct target_fabric_configfs to g_tf_list
286 * 3) Return struct target_fabric_configfs to fabric module to be passed
287 * into target_fabric_configfs_register().
289 struct target_fabric_configfs *target_fabric_configfs_init(
290 struct module *fabric_mod,
291 const char *name)
293 struct target_fabric_configfs *tf;
295 if (!(name)) {
296 pr_err("Unable to locate passed fabric name\n");
297 return ERR_PTR(-EINVAL);
299 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
300 pr_err("Passed name: %s exceeds TARGET_FABRIC"
301 "_NAME_SIZE\n", name);
302 return ERR_PTR(-EINVAL);
305 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
306 if (!tf)
307 return ERR_PTR(-ENOMEM);
309 INIT_LIST_HEAD(&tf->tf_list);
310 atomic_set(&tf->tf_access_cnt, 0);
312 * Setup the default generic struct config_item_type's (cits) in
313 * struct target_fabric_configfs->tf_cit_tmpl
315 tf->tf_module = fabric_mod;
316 target_fabric_setup_cits(tf);
318 tf->tf_subsys = target_core_subsystem[0];
319 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
321 mutex_lock(&g_tf_lock);
322 list_add_tail(&tf->tf_list, &g_tf_list);
323 mutex_unlock(&g_tf_lock);
325 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
326 ">>>>>>>>>>>>>>\n");
327 pr_debug("Initialized struct target_fabric_configfs: %p for"
328 " %s\n", tf, tf->tf_name);
329 return tf;
331 EXPORT_SYMBOL(target_fabric_configfs_init);
334 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
336 void target_fabric_configfs_free(
337 struct target_fabric_configfs *tf)
339 mutex_lock(&g_tf_lock);
340 list_del(&tf->tf_list);
341 mutex_unlock(&g_tf_lock);
343 kfree(tf);
345 EXPORT_SYMBOL(target_fabric_configfs_free);
348 * Perform a sanity check of the passed tf->tf_ops before completing
349 * TCM fabric module registration.
351 static int target_fabric_tf_ops_check(
352 struct target_fabric_configfs *tf)
354 struct target_core_fabric_ops *tfo = &tf->tf_ops;
356 if (!tfo->get_fabric_name) {
357 pr_err("Missing tfo->get_fabric_name()\n");
358 return -EINVAL;
360 if (!tfo->get_fabric_proto_ident) {
361 pr_err("Missing tfo->get_fabric_proto_ident()\n");
362 return -EINVAL;
364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
366 return -EINVAL;
368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
370 return -EINVAL;
372 if (!tfo->tpg_get_default_depth) {
373 pr_err("Missing tfo->tpg_get_default_depth()\n");
374 return -EINVAL;
376 if (!tfo->tpg_get_pr_transport_id) {
377 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
378 return -EINVAL;
380 if (!tfo->tpg_get_pr_transport_id_len) {
381 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
382 return -EINVAL;
384 if (!tfo->tpg_check_demo_mode) {
385 pr_err("Missing tfo->tpg_check_demo_mode()\n");
386 return -EINVAL;
388 if (!tfo->tpg_check_demo_mode_cache) {
389 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
390 return -EINVAL;
392 if (!tfo->tpg_check_demo_mode_write_protect) {
393 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
394 return -EINVAL;
396 if (!tfo->tpg_check_prod_mode_write_protect) {
397 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
398 return -EINVAL;
400 if (!tfo->tpg_alloc_fabric_acl) {
401 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
402 return -EINVAL;
404 if (!tfo->tpg_release_fabric_acl) {
405 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
406 return -EINVAL;
408 if (!tfo->tpg_get_inst_index) {
409 pr_err("Missing tfo->tpg_get_inst_index()\n");
410 return -EINVAL;
412 if (!tfo->release_cmd) {
413 pr_err("Missing tfo->release_cmd()\n");
414 return -EINVAL;
416 if (!tfo->shutdown_session) {
417 pr_err("Missing tfo->shutdown_session()\n");
418 return -EINVAL;
420 if (!tfo->close_session) {
421 pr_err("Missing tfo->close_session()\n");
422 return -EINVAL;
424 if (!tfo->stop_session) {
425 pr_err("Missing tfo->stop_session()\n");
426 return -EINVAL;
428 if (!tfo->fall_back_to_erl0) {
429 pr_err("Missing tfo->fall_back_to_erl0()\n");
430 return -EINVAL;
432 if (!tfo->sess_logged_in) {
433 pr_err("Missing tfo->sess_logged_in()\n");
434 return -EINVAL;
436 if (!tfo->sess_get_index) {
437 pr_err("Missing tfo->sess_get_index()\n");
438 return -EINVAL;
440 if (!tfo->write_pending) {
441 pr_err("Missing tfo->write_pending()\n");
442 return -EINVAL;
444 if (!tfo->write_pending_status) {
445 pr_err("Missing tfo->write_pending_status()\n");
446 return -EINVAL;
448 if (!tfo->set_default_node_attributes) {
449 pr_err("Missing tfo->set_default_node_attributes()\n");
450 return -EINVAL;
452 if (!tfo->get_task_tag) {
453 pr_err("Missing tfo->get_task_tag()\n");
454 return -EINVAL;
456 if (!tfo->get_cmd_state) {
457 pr_err("Missing tfo->get_cmd_state()\n");
458 return -EINVAL;
460 if (!tfo->queue_data_in) {
461 pr_err("Missing tfo->queue_data_in()\n");
462 return -EINVAL;
464 if (!tfo->queue_status) {
465 pr_err("Missing tfo->queue_status()\n");
466 return -EINVAL;
468 if (!tfo->queue_tm_rsp) {
469 pr_err("Missing tfo->queue_tm_rsp()\n");
470 return -EINVAL;
472 if (!tfo->set_fabric_sense_len) {
473 pr_err("Missing tfo->set_fabric_sense_len()\n");
474 return -EINVAL;
476 if (!tfo->get_fabric_sense_len) {
477 pr_err("Missing tfo->get_fabric_sense_len()\n");
478 return -EINVAL;
480 if (!tfo->is_state_remove) {
481 pr_err("Missing tfo->is_state_remove()\n");
482 return -EINVAL;
485 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
486 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
487 * target_core_fabric_configfs.c WWN+TPG group context code.
489 if (!tfo->fabric_make_wwn) {
490 pr_err("Missing tfo->fabric_make_wwn()\n");
491 return -EINVAL;
493 if (!tfo->fabric_drop_wwn) {
494 pr_err("Missing tfo->fabric_drop_wwn()\n");
495 return -EINVAL;
497 if (!tfo->fabric_make_tpg) {
498 pr_err("Missing tfo->fabric_make_tpg()\n");
499 return -EINVAL;
501 if (!tfo->fabric_drop_tpg) {
502 pr_err("Missing tfo->fabric_drop_tpg()\n");
503 return -EINVAL;
506 return 0;
510 * Called 2nd from fabric module with returned parameter of
511 * struct target_fabric_configfs * from target_fabric_configfs_init().
513 * Upon a successful registration, the new fabric's struct config_item is
514 * return. Also, a pointer to this struct is set in the passed
515 * struct target_fabric_configfs.
517 int target_fabric_configfs_register(
518 struct target_fabric_configfs *tf)
520 int ret;
522 if (!tf) {
523 pr_err("Unable to locate target_fabric_configfs"
524 " pointer\n");
525 return -EINVAL;
527 if (!tf->tf_subsys) {
528 pr_err("Unable to target struct config_subsystem"
529 " pointer\n");
530 return -EINVAL;
532 ret = target_fabric_tf_ops_check(tf);
533 if (ret < 0)
534 return ret;
536 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
537 ">>>>>>>>>>\n");
538 return 0;
540 EXPORT_SYMBOL(target_fabric_configfs_register);
542 void target_fabric_configfs_deregister(
543 struct target_fabric_configfs *tf)
545 struct configfs_subsystem *su;
547 if (!tf) {
548 pr_err("Unable to locate passed target_fabric_"
549 "configfs\n");
550 return;
552 su = tf->tf_subsys;
553 if (!su) {
554 pr_err("Unable to locate passed tf->tf_subsys"
555 " pointer\n");
556 return;
558 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
559 ">>>>>>>>>>>>\n");
560 mutex_lock(&g_tf_lock);
561 if (atomic_read(&tf->tf_access_cnt)) {
562 mutex_unlock(&g_tf_lock);
563 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
564 tf->tf_name);
565 BUG();
567 list_del(&tf->tf_list);
568 mutex_unlock(&g_tf_lock);
570 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
571 " %s\n", tf->tf_name);
572 tf->tf_module = NULL;
573 tf->tf_subsys = NULL;
574 kfree(tf);
576 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
577 ">>>>>\n");
579 EXPORT_SYMBOL(target_fabric_configfs_deregister);
581 /*##############################################################################
582 // Stop functions called by external Target Fabrics Modules
583 //############################################################################*/
585 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
587 #define DEF_DEV_ATTRIB_SHOW(_name) \
588 static ssize_t target_core_dev_show_attr_##_name( \
589 struct se_dev_attrib *da, \
590 char *page) \
592 struct se_device *dev; \
593 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
594 ssize_t rb; \
596 spin_lock(&se_dev->se_dev_lock); \
597 dev = se_dev->se_dev_ptr; \
598 if (!dev) { \
599 spin_unlock(&se_dev->se_dev_lock); \
600 return -ENODEV; \
602 rb = snprintf(page, PAGE_SIZE, "%u\n", \
603 (u32)dev->se_sub_dev->se_dev_attrib._name); \
604 spin_unlock(&se_dev->se_dev_lock); \
606 return rb; \
609 #define DEF_DEV_ATTRIB_STORE(_name) \
610 static ssize_t target_core_dev_store_attr_##_name( \
611 struct se_dev_attrib *da, \
612 const char *page, \
613 size_t count) \
615 struct se_device *dev; \
616 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
617 unsigned long val; \
618 int ret; \
620 spin_lock(&se_dev->se_dev_lock); \
621 dev = se_dev->se_dev_ptr; \
622 if (!dev) { \
623 spin_unlock(&se_dev->se_dev_lock); \
624 return -ENODEV; \
626 ret = strict_strtoul(page, 0, &val); \
627 if (ret < 0) { \
628 spin_unlock(&se_dev->se_dev_lock); \
629 pr_err("strict_strtoul() failed with" \
630 " ret: %d\n", ret); \
631 return -EINVAL; \
633 ret = se_dev_set_##_name(dev, (u32)val); \
634 spin_unlock(&se_dev->se_dev_lock); \
636 return (!ret) ? count : -EINVAL; \
639 #define DEF_DEV_ATTRIB(_name) \
640 DEF_DEV_ATTRIB_SHOW(_name); \
641 DEF_DEV_ATTRIB_STORE(_name);
643 #define DEF_DEV_ATTRIB_RO(_name) \
644 DEF_DEV_ATTRIB_SHOW(_name);
646 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
647 #define SE_DEV_ATTR(_name, _mode) \
648 static struct target_core_dev_attrib_attribute \
649 target_core_dev_attrib_##_name = \
650 __CONFIGFS_EATTR(_name, _mode, \
651 target_core_dev_show_attr_##_name, \
652 target_core_dev_store_attr_##_name);
654 #define SE_DEV_ATTR_RO(_name); \
655 static struct target_core_dev_attrib_attribute \
656 target_core_dev_attrib_##_name = \
657 __CONFIGFS_EATTR_RO(_name, \
658 target_core_dev_show_attr_##_name);
660 DEF_DEV_ATTRIB(emulate_dpo);
661 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
663 DEF_DEV_ATTRIB(emulate_fua_write);
664 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
666 DEF_DEV_ATTRIB(emulate_fua_read);
667 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
669 DEF_DEV_ATTRIB(emulate_write_cache);
670 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
672 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
673 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
675 DEF_DEV_ATTRIB(emulate_tas);
676 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
678 DEF_DEV_ATTRIB(emulate_tpu);
679 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
681 DEF_DEV_ATTRIB(emulate_tpws);
682 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
684 DEF_DEV_ATTRIB(enforce_pr_isids);
685 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
687 DEF_DEV_ATTRIB(is_nonrot);
688 SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
690 DEF_DEV_ATTRIB(emulate_rest_reord);
691 SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
693 DEF_DEV_ATTRIB_RO(hw_block_size);
694 SE_DEV_ATTR_RO(hw_block_size);
696 DEF_DEV_ATTRIB(block_size);
697 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
699 DEF_DEV_ATTRIB_RO(hw_max_sectors);
700 SE_DEV_ATTR_RO(hw_max_sectors);
702 DEF_DEV_ATTRIB(max_sectors);
703 SE_DEV_ATTR(max_sectors, S_IRUGO | S_IWUSR);
705 DEF_DEV_ATTRIB(optimal_sectors);
706 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
708 DEF_DEV_ATTRIB_RO(hw_queue_depth);
709 SE_DEV_ATTR_RO(hw_queue_depth);
711 DEF_DEV_ATTRIB(queue_depth);
712 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
714 DEF_DEV_ATTRIB(max_unmap_lba_count);
715 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
717 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
718 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
720 DEF_DEV_ATTRIB(unmap_granularity);
721 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
723 DEF_DEV_ATTRIB(unmap_granularity_alignment);
724 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
726 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
728 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
729 &target_core_dev_attrib_emulate_dpo.attr,
730 &target_core_dev_attrib_emulate_fua_write.attr,
731 &target_core_dev_attrib_emulate_fua_read.attr,
732 &target_core_dev_attrib_emulate_write_cache.attr,
733 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
734 &target_core_dev_attrib_emulate_tas.attr,
735 &target_core_dev_attrib_emulate_tpu.attr,
736 &target_core_dev_attrib_emulate_tpws.attr,
737 &target_core_dev_attrib_enforce_pr_isids.attr,
738 &target_core_dev_attrib_is_nonrot.attr,
739 &target_core_dev_attrib_emulate_rest_reord.attr,
740 &target_core_dev_attrib_hw_block_size.attr,
741 &target_core_dev_attrib_block_size.attr,
742 &target_core_dev_attrib_hw_max_sectors.attr,
743 &target_core_dev_attrib_max_sectors.attr,
744 &target_core_dev_attrib_optimal_sectors.attr,
745 &target_core_dev_attrib_hw_queue_depth.attr,
746 &target_core_dev_attrib_queue_depth.attr,
747 &target_core_dev_attrib_max_unmap_lba_count.attr,
748 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
749 &target_core_dev_attrib_unmap_granularity.attr,
750 &target_core_dev_attrib_unmap_granularity_alignment.attr,
751 NULL,
754 static struct configfs_item_operations target_core_dev_attrib_ops = {
755 .show_attribute = target_core_dev_attrib_attr_show,
756 .store_attribute = target_core_dev_attrib_attr_store,
759 static struct config_item_type target_core_dev_attrib_cit = {
760 .ct_item_ops = &target_core_dev_attrib_ops,
761 .ct_attrs = target_core_dev_attrib_attrs,
762 .ct_owner = THIS_MODULE,
765 /* End functions for struct config_item_type target_core_dev_attrib_cit */
767 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
769 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
770 #define SE_DEV_WWN_ATTR(_name, _mode) \
771 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
772 __CONFIGFS_EATTR(_name, _mode, \
773 target_core_dev_wwn_show_attr_##_name, \
774 target_core_dev_wwn_store_attr_##_name);
776 #define SE_DEV_WWN_ATTR_RO(_name); \
777 do { \
778 static struct target_core_dev_wwn_attribute \
779 target_core_dev_wwn_##_name = \
780 __CONFIGFS_EATTR_RO(_name, \
781 target_core_dev_wwn_show_attr_##_name); \
782 } while (0);
785 * VPD page 0x80 Unit serial
787 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
788 struct t10_wwn *t10_wwn,
789 char *page)
791 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
792 struct se_device *dev;
794 dev = se_dev->se_dev_ptr;
795 if (!dev)
796 return -ENODEV;
798 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
799 &t10_wwn->unit_serial[0]);
802 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
803 struct t10_wwn *t10_wwn,
804 const char *page,
805 size_t count)
807 struct se_subsystem_dev *su_dev = t10_wwn->t10_sub_dev;
808 struct se_device *dev;
809 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
812 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
813 * from the struct scsi_device level firmware, do not allow
814 * VPD Unit Serial to be emulated.
816 * Note this struct scsi_device could also be emulating VPD
817 * information from its drivers/scsi LLD. But for now we assume
818 * it is doing 'the right thing' wrt a world wide unique
819 * VPD Unit Serial Number that OS dependent multipath can depend on.
821 if (su_dev->su_dev_flags & SDF_FIRMWARE_VPD_UNIT_SERIAL) {
822 pr_err("Underlying SCSI device firmware provided VPD"
823 " Unit Serial, ignoring request\n");
824 return -EOPNOTSUPP;
827 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
828 pr_err("Emulated VPD Unit Serial exceeds"
829 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
830 return -EOVERFLOW;
833 * Check to see if any active $FABRIC_MOD exports exist. If they
834 * do exist, fail here as changing this information on the fly
835 * (underneath the initiator side OS dependent multipath code)
836 * could cause negative effects.
838 dev = su_dev->se_dev_ptr;
839 if (dev) {
840 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
841 pr_err("Unable to set VPD Unit Serial while"
842 " active %d $FABRIC_MOD exports exist\n",
843 atomic_read(&dev->dev_export_obj.obj_access_count));
844 return -EINVAL;
848 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
850 * Also, strip any newline added from the userspace
851 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
853 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
854 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
855 snprintf(su_dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
856 "%s", strstrip(buf));
857 su_dev->su_dev_flags |= SDF_EMULATED_VPD_UNIT_SERIAL;
859 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
860 " %s\n", su_dev->t10_wwn.unit_serial);
862 return count;
865 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
868 * VPD page 0x83 Protocol Identifier
870 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
871 struct t10_wwn *t10_wwn,
872 char *page)
874 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
875 struct se_device *dev;
876 struct t10_vpd *vpd;
877 unsigned char buf[VPD_TMP_BUF_SIZE];
878 ssize_t len = 0;
880 dev = se_dev->se_dev_ptr;
881 if (!dev)
882 return -ENODEV;
884 memset(buf, 0, VPD_TMP_BUF_SIZE);
886 spin_lock(&t10_wwn->t10_vpd_lock);
887 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
888 if (!vpd->protocol_identifier_set)
889 continue;
891 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
893 if (len + strlen(buf) >= PAGE_SIZE)
894 break;
896 len += sprintf(page+len, "%s", buf);
898 spin_unlock(&t10_wwn->t10_vpd_lock);
900 return len;
903 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
904 struct t10_wwn *t10_wwn,
905 const char *page,
906 size_t count)
908 return -ENOSYS;
911 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
914 * Generic wrapper for dumping VPD identifiers by association.
916 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
917 static ssize_t target_core_dev_wwn_show_attr_##_name( \
918 struct t10_wwn *t10_wwn, \
919 char *page) \
921 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev; \
922 struct se_device *dev; \
923 struct t10_vpd *vpd; \
924 unsigned char buf[VPD_TMP_BUF_SIZE]; \
925 ssize_t len = 0; \
927 dev = se_dev->se_dev_ptr; \
928 if (!dev) \
929 return -ENODEV; \
931 spin_lock(&t10_wwn->t10_vpd_lock); \
932 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
933 if (vpd->association != _assoc) \
934 continue; \
936 memset(buf, 0, VPD_TMP_BUF_SIZE); \
937 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
938 if (len + strlen(buf) >= PAGE_SIZE) \
939 break; \
940 len += sprintf(page+len, "%s", buf); \
942 memset(buf, 0, VPD_TMP_BUF_SIZE); \
943 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
944 if (len + strlen(buf) >= PAGE_SIZE) \
945 break; \
946 len += sprintf(page+len, "%s", buf); \
948 memset(buf, 0, VPD_TMP_BUF_SIZE); \
949 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
950 if (len + strlen(buf) >= PAGE_SIZE) \
951 break; \
952 len += sprintf(page+len, "%s", buf); \
954 spin_unlock(&t10_wwn->t10_vpd_lock); \
956 return len; \
960 * VPD page 0x83 Association: Logical Unit
962 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
964 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
965 struct t10_wwn *t10_wwn,
966 const char *page,
967 size_t count)
969 return -ENOSYS;
972 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
975 * VPD page 0x83 Association: Target Port
977 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
979 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
980 struct t10_wwn *t10_wwn,
981 const char *page,
982 size_t count)
984 return -ENOSYS;
987 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
990 * VPD page 0x83 Association: SCSI Target Device
992 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
994 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
995 struct t10_wwn *t10_wwn,
996 const char *page,
997 size_t count)
999 return -ENOSYS;
1002 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1004 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1006 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1007 &target_core_dev_wwn_vpd_unit_serial.attr,
1008 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1009 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1010 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1011 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1012 NULL,
1015 static struct configfs_item_operations target_core_dev_wwn_ops = {
1016 .show_attribute = target_core_dev_wwn_attr_show,
1017 .store_attribute = target_core_dev_wwn_attr_store,
1020 static struct config_item_type target_core_dev_wwn_cit = {
1021 .ct_item_ops = &target_core_dev_wwn_ops,
1022 .ct_attrs = target_core_dev_wwn_attrs,
1023 .ct_owner = THIS_MODULE,
1026 /* End functions for struct config_item_type target_core_dev_wwn_cit */
1028 /* Start functions for struct config_item_type target_core_dev_pr_cit */
1030 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_subsystem_dev);
1031 #define SE_DEV_PR_ATTR(_name, _mode) \
1032 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1033 __CONFIGFS_EATTR(_name, _mode, \
1034 target_core_dev_pr_show_attr_##_name, \
1035 target_core_dev_pr_store_attr_##_name);
1037 #define SE_DEV_PR_ATTR_RO(_name); \
1038 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1039 __CONFIGFS_EATTR_RO(_name, \
1040 target_core_dev_pr_show_attr_##_name);
1043 * res_holder
1045 static ssize_t target_core_dev_pr_show_spc3_res(
1046 struct se_device *dev,
1047 char *page,
1048 ssize_t *len)
1050 struct se_node_acl *se_nacl;
1051 struct t10_pr_registration *pr_reg;
1052 char i_buf[PR_REG_ISID_ID_LEN];
1053 int prf_isid;
1055 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1057 spin_lock(&dev->dev_reservation_lock);
1058 pr_reg = dev->dev_pr_res_holder;
1059 if (!pr_reg) {
1060 *len += sprintf(page + *len, "No SPC-3 Reservation holder\n");
1061 spin_unlock(&dev->dev_reservation_lock);
1062 return *len;
1064 se_nacl = pr_reg->pr_reg_nacl;
1065 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1066 PR_REG_ISID_ID_LEN);
1068 *len += sprintf(page + *len, "SPC-3 Reservation: %s Initiator: %s%s\n",
1069 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1070 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1071 spin_unlock(&dev->dev_reservation_lock);
1073 return *len;
1076 static ssize_t target_core_dev_pr_show_spc2_res(
1077 struct se_device *dev,
1078 char *page,
1079 ssize_t *len)
1081 struct se_node_acl *se_nacl;
1083 spin_lock(&dev->dev_reservation_lock);
1084 se_nacl = dev->dev_reserved_node_acl;
1085 if (!se_nacl) {
1086 *len += sprintf(page + *len, "No SPC-2 Reservation holder\n");
1087 spin_unlock(&dev->dev_reservation_lock);
1088 return *len;
1090 *len += sprintf(page + *len, "SPC-2 Reservation: %s Initiator: %s\n",
1091 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1092 se_nacl->initiatorname);
1093 spin_unlock(&dev->dev_reservation_lock);
1095 return *len;
1098 static ssize_t target_core_dev_pr_show_attr_res_holder(
1099 struct se_subsystem_dev *su_dev,
1100 char *page)
1102 ssize_t len = 0;
1104 if (!su_dev->se_dev_ptr)
1105 return -ENODEV;
1107 switch (su_dev->t10_pr.res_type) {
1108 case SPC3_PERSISTENT_RESERVATIONS:
1109 target_core_dev_pr_show_spc3_res(su_dev->se_dev_ptr,
1110 page, &len);
1111 break;
1112 case SPC2_RESERVATIONS:
1113 target_core_dev_pr_show_spc2_res(su_dev->se_dev_ptr,
1114 page, &len);
1115 break;
1116 case SPC_PASSTHROUGH:
1117 len += sprintf(page+len, "Passthrough\n");
1118 break;
1119 default:
1120 len += sprintf(page+len, "Unknown\n");
1121 break;
1124 return len;
1127 SE_DEV_PR_ATTR_RO(res_holder);
1130 * res_pr_all_tgt_pts
1132 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1133 struct se_subsystem_dev *su_dev,
1134 char *page)
1136 struct se_device *dev;
1137 struct t10_pr_registration *pr_reg;
1138 ssize_t len = 0;
1140 dev = su_dev->se_dev_ptr;
1141 if (!dev)
1142 return -ENODEV;
1144 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1145 return len;
1147 spin_lock(&dev->dev_reservation_lock);
1148 pr_reg = dev->dev_pr_res_holder;
1149 if (!pr_reg) {
1150 len = sprintf(page, "No SPC-3 Reservation holder\n");
1151 spin_unlock(&dev->dev_reservation_lock);
1152 return len;
1155 * See All Target Ports (ALL_TG_PT) bit in spcr17, section 6.14.3
1156 * Basic PERSISTENT RESERVER OUT parameter list, page 290
1158 if (pr_reg->pr_reg_all_tg_pt)
1159 len = sprintf(page, "SPC-3 Reservation: All Target"
1160 " Ports registration\n");
1161 else
1162 len = sprintf(page, "SPC-3 Reservation: Single"
1163 " Target Port registration\n");
1164 spin_unlock(&dev->dev_reservation_lock);
1166 return len;
1169 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1172 * res_pr_generation
1174 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1175 struct se_subsystem_dev *su_dev,
1176 char *page)
1178 if (!su_dev->se_dev_ptr)
1179 return -ENODEV;
1181 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1182 return 0;
1184 return sprintf(page, "0x%08x\n", su_dev->t10_pr.pr_generation);
1187 SE_DEV_PR_ATTR_RO(res_pr_generation);
1190 * res_pr_holder_tg_port
1192 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1193 struct se_subsystem_dev *su_dev,
1194 char *page)
1196 struct se_device *dev;
1197 struct se_node_acl *se_nacl;
1198 struct se_lun *lun;
1199 struct se_portal_group *se_tpg;
1200 struct t10_pr_registration *pr_reg;
1201 struct target_core_fabric_ops *tfo;
1202 ssize_t len = 0;
1204 dev = su_dev->se_dev_ptr;
1205 if (!dev)
1206 return -ENODEV;
1208 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1209 return len;
1211 spin_lock(&dev->dev_reservation_lock);
1212 pr_reg = dev->dev_pr_res_holder;
1213 if (!pr_reg) {
1214 len = sprintf(page, "No SPC-3 Reservation holder\n");
1215 spin_unlock(&dev->dev_reservation_lock);
1216 return len;
1218 se_nacl = pr_reg->pr_reg_nacl;
1219 se_tpg = se_nacl->se_tpg;
1220 lun = pr_reg->pr_reg_tg_pt_lun;
1221 tfo = se_tpg->se_tpg_tfo;
1223 len += sprintf(page+len, "SPC-3 Reservation: %s"
1224 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1225 tfo->tpg_get_wwn(se_tpg));
1226 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1227 " Identifer Tag: %hu %s Portal Group Tag: %hu"
1228 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1229 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1230 tfo->get_fabric_name(), lun->unpacked_lun);
1231 spin_unlock(&dev->dev_reservation_lock);
1233 return len;
1236 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1239 * res_pr_registered_i_pts
1241 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1242 struct se_subsystem_dev *su_dev,
1243 char *page)
1245 struct target_core_fabric_ops *tfo;
1246 struct t10_pr_registration *pr_reg;
1247 unsigned char buf[384];
1248 char i_buf[PR_REG_ISID_ID_LEN];
1249 ssize_t len = 0;
1250 int reg_count = 0, prf_isid;
1252 if (!su_dev->se_dev_ptr)
1253 return -ENODEV;
1255 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1256 return len;
1258 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1260 spin_lock(&su_dev->t10_pr.registration_lock);
1261 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1262 pr_reg_list) {
1264 memset(buf, 0, 384);
1265 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1266 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1267 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1268 PR_REG_ISID_ID_LEN);
1269 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1270 tfo->get_fabric_name(),
1271 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1272 &i_buf[0] : "", pr_reg->pr_res_key,
1273 pr_reg->pr_res_generation);
1275 if (len + strlen(buf) >= PAGE_SIZE)
1276 break;
1278 len += sprintf(page+len, "%s", buf);
1279 reg_count++;
1281 spin_unlock(&su_dev->t10_pr.registration_lock);
1283 if (!reg_count)
1284 len += sprintf(page+len, "None\n");
1286 return len;
1289 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1292 * res_pr_type
1294 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1295 struct se_subsystem_dev *su_dev,
1296 char *page)
1298 struct se_device *dev;
1299 struct t10_pr_registration *pr_reg;
1300 ssize_t len = 0;
1302 dev = su_dev->se_dev_ptr;
1303 if (!dev)
1304 return -ENODEV;
1306 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1307 return len;
1309 spin_lock(&dev->dev_reservation_lock);
1310 pr_reg = dev->dev_pr_res_holder;
1311 if (!pr_reg) {
1312 len = sprintf(page, "No SPC-3 Reservation holder\n");
1313 spin_unlock(&dev->dev_reservation_lock);
1314 return len;
1316 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1317 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1318 spin_unlock(&dev->dev_reservation_lock);
1320 return len;
1323 SE_DEV_PR_ATTR_RO(res_pr_type);
1326 * res_type
1328 static ssize_t target_core_dev_pr_show_attr_res_type(
1329 struct se_subsystem_dev *su_dev,
1330 char *page)
1332 ssize_t len = 0;
1334 if (!su_dev->se_dev_ptr)
1335 return -ENODEV;
1337 switch (su_dev->t10_pr.res_type) {
1338 case SPC3_PERSISTENT_RESERVATIONS:
1339 len = sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1340 break;
1341 case SPC2_RESERVATIONS:
1342 len = sprintf(page, "SPC2_RESERVATIONS\n");
1343 break;
1344 case SPC_PASSTHROUGH:
1345 len = sprintf(page, "SPC_PASSTHROUGH\n");
1346 break;
1347 default:
1348 len = sprintf(page, "UNKNOWN\n");
1349 break;
1352 return len;
1355 SE_DEV_PR_ATTR_RO(res_type);
1358 * res_aptpl_active
1361 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1362 struct se_subsystem_dev *su_dev,
1363 char *page)
1365 if (!su_dev->se_dev_ptr)
1366 return -ENODEV;
1368 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1369 return 0;
1371 return sprintf(page, "APTPL Bit Status: %s\n",
1372 (su_dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1375 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1378 * res_aptpl_metadata
1380 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1381 struct se_subsystem_dev *su_dev,
1382 char *page)
1384 if (!su_dev->se_dev_ptr)
1385 return -ENODEV;
1387 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1388 return 0;
1390 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1393 enum {
1394 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1395 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1396 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1397 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1400 static match_table_t tokens = {
1401 {Opt_initiator_fabric, "initiator_fabric=%s"},
1402 {Opt_initiator_node, "initiator_node=%s"},
1403 {Opt_initiator_sid, "initiator_sid=%s"},
1404 {Opt_sa_res_key, "sa_res_key=%s"},
1405 {Opt_res_holder, "res_holder=%d"},
1406 {Opt_res_type, "res_type=%d"},
1407 {Opt_res_scope, "res_scope=%d"},
1408 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1409 {Opt_mapped_lun, "mapped_lun=%d"},
1410 {Opt_target_fabric, "target_fabric=%s"},
1411 {Opt_target_node, "target_node=%s"},
1412 {Opt_tpgt, "tpgt=%d"},
1413 {Opt_port_rtpi, "port_rtpi=%d"},
1414 {Opt_target_lun, "target_lun=%d"},
1415 {Opt_err, NULL}
1418 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1419 struct se_subsystem_dev *su_dev,
1420 const char *page,
1421 size_t count)
1423 struct se_device *dev;
1424 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1425 unsigned char *t_fabric = NULL, *t_port = NULL;
1426 char *orig, *ptr, *arg_p, *opts;
1427 substring_t args[MAX_OPT_ARGS];
1428 unsigned long long tmp_ll;
1429 u64 sa_res_key = 0;
1430 u32 mapped_lun = 0, target_lun = 0;
1431 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1432 u16 port_rpti = 0, tpgt = 0;
1433 u8 type = 0, scope;
1435 dev = su_dev->se_dev_ptr;
1436 if (!dev)
1437 return -ENODEV;
1439 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1440 return 0;
1442 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1443 pr_debug("Unable to process APTPL metadata while"
1444 " active fabric exports exist\n");
1445 return -EINVAL;
1448 opts = kstrdup(page, GFP_KERNEL);
1449 if (!opts)
1450 return -ENOMEM;
1452 orig = opts;
1453 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1454 if (!*ptr)
1455 continue;
1457 token = match_token(ptr, tokens, args);
1458 switch (token) {
1459 case Opt_initiator_fabric:
1460 i_fabric = match_strdup(&args[0]);
1461 if (!i_fabric) {
1462 ret = -ENOMEM;
1463 goto out;
1465 break;
1466 case Opt_initiator_node:
1467 i_port = match_strdup(&args[0]);
1468 if (!i_port) {
1469 ret = -ENOMEM;
1470 goto out;
1472 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1473 pr_err("APTPL metadata initiator_node="
1474 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1475 PR_APTPL_MAX_IPORT_LEN);
1476 ret = -EINVAL;
1477 break;
1479 break;
1480 case Opt_initiator_sid:
1481 isid = match_strdup(&args[0]);
1482 if (!isid) {
1483 ret = -ENOMEM;
1484 goto out;
1486 if (strlen(isid) >= PR_REG_ISID_LEN) {
1487 pr_err("APTPL metadata initiator_isid"
1488 "= exceeds PR_REG_ISID_LEN: %d\n",
1489 PR_REG_ISID_LEN);
1490 ret = -EINVAL;
1491 break;
1493 break;
1494 case Opt_sa_res_key:
1495 arg_p = match_strdup(&args[0]);
1496 if (!arg_p) {
1497 ret = -ENOMEM;
1498 goto out;
1500 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1501 if (ret < 0) {
1502 pr_err("strict_strtoull() failed for"
1503 " sa_res_key=\n");
1504 goto out;
1506 sa_res_key = (u64)tmp_ll;
1507 break;
1509 * PR APTPL Metadata for Reservation
1511 case Opt_res_holder:
1512 match_int(args, &arg);
1513 res_holder = arg;
1514 break;
1515 case Opt_res_type:
1516 match_int(args, &arg);
1517 type = (u8)arg;
1518 break;
1519 case Opt_res_scope:
1520 match_int(args, &arg);
1521 scope = (u8)arg;
1522 break;
1523 case Opt_res_all_tg_pt:
1524 match_int(args, &arg);
1525 all_tg_pt = (int)arg;
1526 break;
1527 case Opt_mapped_lun:
1528 match_int(args, &arg);
1529 mapped_lun = (u32)arg;
1530 break;
1532 * PR APTPL Metadata for Target Port
1534 case Opt_target_fabric:
1535 t_fabric = match_strdup(&args[0]);
1536 if (!t_fabric) {
1537 ret = -ENOMEM;
1538 goto out;
1540 break;
1541 case Opt_target_node:
1542 t_port = match_strdup(&args[0]);
1543 if (!t_port) {
1544 ret = -ENOMEM;
1545 goto out;
1547 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1548 pr_err("APTPL metadata target_node="
1549 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1550 PR_APTPL_MAX_TPORT_LEN);
1551 ret = -EINVAL;
1552 break;
1554 break;
1555 case Opt_tpgt:
1556 match_int(args, &arg);
1557 tpgt = (u16)arg;
1558 break;
1559 case Opt_port_rtpi:
1560 match_int(args, &arg);
1561 port_rpti = (u16)arg;
1562 break;
1563 case Opt_target_lun:
1564 match_int(args, &arg);
1565 target_lun = (u32)arg;
1566 break;
1567 default:
1568 break;
1572 if (!i_port || !t_port || !sa_res_key) {
1573 pr_err("Illegal parameters for APTPL registration\n");
1574 ret = -EINVAL;
1575 goto out;
1578 if (res_holder && !(type)) {
1579 pr_err("Illegal PR type: 0x%02x for reservation"
1580 " holder\n", type);
1581 ret = -EINVAL;
1582 goto out;
1585 ret = core_scsi3_alloc_aptpl_registration(&su_dev->t10_pr, sa_res_key,
1586 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1587 res_holder, all_tg_pt, type);
1588 out:
1589 kfree(i_fabric);
1590 kfree(i_port);
1591 kfree(isid);
1592 kfree(t_fabric);
1593 kfree(t_port);
1594 kfree(orig);
1595 return (ret == 0) ? count : ret;
1598 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1600 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1602 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1603 &target_core_dev_pr_res_holder.attr,
1604 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1605 &target_core_dev_pr_res_pr_generation.attr,
1606 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1607 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1608 &target_core_dev_pr_res_pr_type.attr,
1609 &target_core_dev_pr_res_type.attr,
1610 &target_core_dev_pr_res_aptpl_active.attr,
1611 &target_core_dev_pr_res_aptpl_metadata.attr,
1612 NULL,
1615 static struct configfs_item_operations target_core_dev_pr_ops = {
1616 .show_attribute = target_core_dev_pr_attr_show,
1617 .store_attribute = target_core_dev_pr_attr_store,
1620 static struct config_item_type target_core_dev_pr_cit = {
1621 .ct_item_ops = &target_core_dev_pr_ops,
1622 .ct_attrs = target_core_dev_pr_attrs,
1623 .ct_owner = THIS_MODULE,
1626 /* End functions for struct config_item_type target_core_dev_pr_cit */
1628 /* Start functions for struct config_item_type target_core_dev_cit */
1630 static ssize_t target_core_show_dev_info(void *p, char *page)
1632 struct se_subsystem_dev *se_dev = p;
1633 struct se_hba *hba = se_dev->se_dev_hba;
1634 struct se_subsystem_api *t = hba->transport;
1635 int bl = 0;
1636 ssize_t read_bytes = 0;
1638 if (!se_dev->se_dev_ptr)
1639 return -ENODEV;
1641 transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1642 read_bytes += bl;
1643 read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1644 return read_bytes;
1647 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1648 .attr = { .ca_owner = THIS_MODULE,
1649 .ca_name = "info",
1650 .ca_mode = S_IRUGO },
1651 .show = target_core_show_dev_info,
1652 .store = NULL,
1655 static ssize_t target_core_store_dev_control(
1656 void *p,
1657 const char *page,
1658 size_t count)
1660 struct se_subsystem_dev *se_dev = p;
1661 struct se_hba *hba = se_dev->se_dev_hba;
1662 struct se_subsystem_api *t = hba->transport;
1664 if (!se_dev->se_dev_su_ptr) {
1665 pr_err("Unable to locate struct se_subsystem_dev>se"
1666 "_dev_su_ptr\n");
1667 return -EINVAL;
1670 return t->set_configfs_dev_params(hba, se_dev, page, count);
1673 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1674 .attr = { .ca_owner = THIS_MODULE,
1675 .ca_name = "control",
1676 .ca_mode = S_IWUSR },
1677 .show = NULL,
1678 .store = target_core_store_dev_control,
1681 static ssize_t target_core_show_dev_alias(void *p, char *page)
1683 struct se_subsystem_dev *se_dev = p;
1685 if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1686 return 0;
1688 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1691 static ssize_t target_core_store_dev_alias(
1692 void *p,
1693 const char *page,
1694 size_t count)
1696 struct se_subsystem_dev *se_dev = p;
1697 struct se_hba *hba = se_dev->se_dev_hba;
1698 ssize_t read_bytes;
1700 if (count > (SE_DEV_ALIAS_LEN-1)) {
1701 pr_err("alias count: %d exceeds"
1702 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1703 SE_DEV_ALIAS_LEN-1);
1704 return -EINVAL;
1707 read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1708 "%s", page);
1709 if (!read_bytes)
1710 return -EINVAL;
1711 if (se_dev->se_dev_alias[read_bytes - 1] == '\n')
1712 se_dev->se_dev_alias[read_bytes - 1] = '\0';
1714 se_dev->su_dev_flags |= SDF_USING_ALIAS;
1716 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1717 config_item_name(&hba->hba_group.cg_item),
1718 config_item_name(&se_dev->se_dev_group.cg_item),
1719 se_dev->se_dev_alias);
1721 return read_bytes;
1724 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1725 .attr = { .ca_owner = THIS_MODULE,
1726 .ca_name = "alias",
1727 .ca_mode = S_IRUGO | S_IWUSR },
1728 .show = target_core_show_dev_alias,
1729 .store = target_core_store_dev_alias,
1732 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1734 struct se_subsystem_dev *se_dev = p;
1736 if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH))
1737 return 0;
1739 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_udev_path);
1742 static ssize_t target_core_store_dev_udev_path(
1743 void *p,
1744 const char *page,
1745 size_t count)
1747 struct se_subsystem_dev *se_dev = p;
1748 struct se_hba *hba = se_dev->se_dev_hba;
1749 ssize_t read_bytes;
1751 if (count > (SE_UDEV_PATH_LEN-1)) {
1752 pr_err("udev_path count: %d exceeds"
1753 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1754 SE_UDEV_PATH_LEN-1);
1755 return -EINVAL;
1758 read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1759 "%s", page);
1760 if (!read_bytes)
1761 return -EINVAL;
1762 if (se_dev->se_dev_udev_path[read_bytes - 1] == '\n')
1763 se_dev->se_dev_udev_path[read_bytes - 1] = '\0';
1765 se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1767 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1768 config_item_name(&hba->hba_group.cg_item),
1769 config_item_name(&se_dev->se_dev_group.cg_item),
1770 se_dev->se_dev_udev_path);
1772 return read_bytes;
1775 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1776 .attr = { .ca_owner = THIS_MODULE,
1777 .ca_name = "udev_path",
1778 .ca_mode = S_IRUGO | S_IWUSR },
1779 .show = target_core_show_dev_udev_path,
1780 .store = target_core_store_dev_udev_path,
1783 static ssize_t target_core_store_dev_enable(
1784 void *p,
1785 const char *page,
1786 size_t count)
1788 struct se_subsystem_dev *se_dev = p;
1789 struct se_device *dev;
1790 struct se_hba *hba = se_dev->se_dev_hba;
1791 struct se_subsystem_api *t = hba->transport;
1792 char *ptr;
1794 ptr = strstr(page, "1");
1795 if (!ptr) {
1796 pr_err("For dev_enable ops, only valid value"
1797 " is \"1\"\n");
1798 return -EINVAL;
1800 if (se_dev->se_dev_ptr) {
1801 pr_err("se_dev->se_dev_ptr already set for storage"
1802 " object\n");
1803 return -EEXIST;
1806 if (t->check_configfs_dev_params(hba, se_dev) < 0)
1807 return -EINVAL;
1809 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1810 if (IS_ERR(dev))
1811 return PTR_ERR(dev);
1812 else if (!dev)
1813 return -EINVAL;
1815 se_dev->se_dev_ptr = dev;
1816 pr_debug("Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1817 " %p\n", se_dev->se_dev_ptr);
1819 return count;
1822 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1823 .attr = { .ca_owner = THIS_MODULE,
1824 .ca_name = "enable",
1825 .ca_mode = S_IWUSR },
1826 .show = NULL,
1827 .store = target_core_store_dev_enable,
1830 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1832 struct se_device *dev;
1833 struct se_subsystem_dev *su_dev = p;
1834 struct config_item *lu_ci;
1835 struct t10_alua_lu_gp *lu_gp;
1836 struct t10_alua_lu_gp_member *lu_gp_mem;
1837 ssize_t len = 0;
1839 dev = su_dev->se_dev_ptr;
1840 if (!dev)
1841 return -ENODEV;
1843 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED)
1844 return len;
1846 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1847 if (!lu_gp_mem) {
1848 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1849 " pointer\n");
1850 return -EINVAL;
1853 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1854 lu_gp = lu_gp_mem->lu_gp;
1855 if (lu_gp) {
1856 lu_ci = &lu_gp->lu_gp_group.cg_item;
1857 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1858 config_item_name(lu_ci), lu_gp->lu_gp_id);
1860 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1862 return len;
1865 static ssize_t target_core_store_alua_lu_gp(
1866 void *p,
1867 const char *page,
1868 size_t count)
1870 struct se_device *dev;
1871 struct se_subsystem_dev *su_dev = p;
1872 struct se_hba *hba = su_dev->se_dev_hba;
1873 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1874 struct t10_alua_lu_gp_member *lu_gp_mem;
1875 unsigned char buf[LU_GROUP_NAME_BUF];
1876 int move = 0;
1878 dev = su_dev->se_dev_ptr;
1879 if (!dev)
1880 return -ENODEV;
1882 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1883 pr_warn("SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1884 config_item_name(&hba->hba_group.cg_item),
1885 config_item_name(&su_dev->se_dev_group.cg_item));
1886 return -EINVAL;
1888 if (count > LU_GROUP_NAME_BUF) {
1889 pr_err("ALUA LU Group Alias too large!\n");
1890 return -EINVAL;
1892 memset(buf, 0, LU_GROUP_NAME_BUF);
1893 memcpy(buf, page, count);
1895 * Any ALUA logical unit alias besides "NULL" means we will be
1896 * making a new group association.
1898 if (strcmp(strstrip(buf), "NULL")) {
1900 * core_alua_get_lu_gp_by_name() will increment reference to
1901 * struct t10_alua_lu_gp. This reference is released with
1902 * core_alua_get_lu_gp_by_name below().
1904 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1905 if (!lu_gp_new)
1906 return -ENODEV;
1908 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1909 if (!lu_gp_mem) {
1910 if (lu_gp_new)
1911 core_alua_put_lu_gp_from_name(lu_gp_new);
1912 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1913 " pointer\n");
1914 return -EINVAL;
1917 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1918 lu_gp = lu_gp_mem->lu_gp;
1919 if (lu_gp) {
1921 * Clearing an existing lu_gp association, and replacing
1922 * with NULL
1924 if (!lu_gp_new) {
1925 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1926 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1927 " %hu\n",
1928 config_item_name(&hba->hba_group.cg_item),
1929 config_item_name(&su_dev->se_dev_group.cg_item),
1930 config_item_name(&lu_gp->lu_gp_group.cg_item),
1931 lu_gp->lu_gp_id);
1933 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1934 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1936 return count;
1939 * Removing existing association of lu_gp_mem with lu_gp
1941 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1942 move = 1;
1945 * Associate lu_gp_mem with lu_gp_new.
1947 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1948 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1950 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1951 " core/alua/lu_gps/%s, ID: %hu\n",
1952 (move) ? "Moving" : "Adding",
1953 config_item_name(&hba->hba_group.cg_item),
1954 config_item_name(&su_dev->se_dev_group.cg_item),
1955 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1956 lu_gp_new->lu_gp_id);
1958 core_alua_put_lu_gp_from_name(lu_gp_new);
1959 return count;
1962 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1963 .attr = { .ca_owner = THIS_MODULE,
1964 .ca_name = "alua_lu_gp",
1965 .ca_mode = S_IRUGO | S_IWUSR },
1966 .show = target_core_show_alua_lu_gp,
1967 .store = target_core_store_alua_lu_gp,
1970 static struct configfs_attribute *lio_core_dev_attrs[] = {
1971 &target_core_attr_dev_info.attr,
1972 &target_core_attr_dev_control.attr,
1973 &target_core_attr_dev_alias.attr,
1974 &target_core_attr_dev_udev_path.attr,
1975 &target_core_attr_dev_enable.attr,
1976 &target_core_attr_dev_alua_lu_gp.attr,
1977 NULL,
1980 static void target_core_dev_release(struct config_item *item)
1982 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
1983 struct se_subsystem_dev, se_dev_group);
1984 struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
1985 struct se_subsystem_api *t = hba->transport;
1986 struct config_group *dev_cg = &se_dev->se_dev_group;
1988 kfree(dev_cg->default_groups);
1990 * This pointer will set when the storage is enabled with:
1991 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
1993 if (se_dev->se_dev_ptr) {
1994 pr_debug("Target_Core_ConfigFS: Calling se_free_"
1995 "virtual_device() for se_dev_ptr: %p\n",
1996 se_dev->se_dev_ptr);
1998 se_free_virtual_device(se_dev->se_dev_ptr, hba);
1999 } else {
2001 * Release struct se_subsystem_dev->se_dev_su_ptr..
2003 pr_debug("Target_Core_ConfigFS: Calling t->free_"
2004 "device() for se_dev_su_ptr: %p\n",
2005 se_dev->se_dev_su_ptr);
2007 t->free_device(se_dev->se_dev_su_ptr);
2010 pr_debug("Target_Core_ConfigFS: Deallocating se_subsystem"
2011 "_dev_t: %p\n", se_dev);
2012 kfree(se_dev);
2015 static ssize_t target_core_dev_show(struct config_item *item,
2016 struct configfs_attribute *attr,
2017 char *page)
2019 struct se_subsystem_dev *se_dev = container_of(
2020 to_config_group(item), struct se_subsystem_dev,
2021 se_dev_group);
2022 struct target_core_configfs_attribute *tc_attr = container_of(
2023 attr, struct target_core_configfs_attribute, attr);
2025 if (!tc_attr->show)
2026 return -EINVAL;
2028 return tc_attr->show(se_dev, page);
2031 static ssize_t target_core_dev_store(struct config_item *item,
2032 struct configfs_attribute *attr,
2033 const char *page, size_t count)
2035 struct se_subsystem_dev *se_dev = container_of(
2036 to_config_group(item), struct se_subsystem_dev,
2037 se_dev_group);
2038 struct target_core_configfs_attribute *tc_attr = container_of(
2039 attr, struct target_core_configfs_attribute, attr);
2041 if (!tc_attr->store)
2042 return -EINVAL;
2044 return tc_attr->store(se_dev, page, count);
2047 static struct configfs_item_operations target_core_dev_item_ops = {
2048 .release = target_core_dev_release,
2049 .show_attribute = target_core_dev_show,
2050 .store_attribute = target_core_dev_store,
2053 static struct config_item_type target_core_dev_cit = {
2054 .ct_item_ops = &target_core_dev_item_ops,
2055 .ct_attrs = lio_core_dev_attrs,
2056 .ct_owner = THIS_MODULE,
2059 /* End functions for struct config_item_type target_core_dev_cit */
2061 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2063 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2064 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2065 static struct target_core_alua_lu_gp_attribute \
2066 target_core_alua_lu_gp_##_name = \
2067 __CONFIGFS_EATTR(_name, _mode, \
2068 target_core_alua_lu_gp_show_attr_##_name, \
2069 target_core_alua_lu_gp_store_attr_##_name);
2071 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2072 static struct target_core_alua_lu_gp_attribute \
2073 target_core_alua_lu_gp_##_name = \
2074 __CONFIGFS_EATTR_RO(_name, \
2075 target_core_alua_lu_gp_show_attr_##_name);
2078 * lu_gp_id
2080 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2081 struct t10_alua_lu_gp *lu_gp,
2082 char *page)
2084 if (!lu_gp->lu_gp_valid_id)
2085 return 0;
2087 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2090 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2091 struct t10_alua_lu_gp *lu_gp,
2092 const char *page,
2093 size_t count)
2095 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2096 unsigned long lu_gp_id;
2097 int ret;
2099 ret = strict_strtoul(page, 0, &lu_gp_id);
2100 if (ret < 0) {
2101 pr_err("strict_strtoul() returned %d for"
2102 " lu_gp_id\n", ret);
2103 return -EINVAL;
2105 if (lu_gp_id > 0x0000ffff) {
2106 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2107 " 0x0000ffff\n", lu_gp_id);
2108 return -EINVAL;
2111 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2112 if (ret < 0)
2113 return -EINVAL;
2115 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2116 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2117 config_item_name(&alua_lu_gp_cg->cg_item),
2118 lu_gp->lu_gp_id);
2120 return count;
2123 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2126 * members
2128 static ssize_t target_core_alua_lu_gp_show_attr_members(
2129 struct t10_alua_lu_gp *lu_gp,
2130 char *page)
2132 struct se_device *dev;
2133 struct se_hba *hba;
2134 struct se_subsystem_dev *su_dev;
2135 struct t10_alua_lu_gp_member *lu_gp_mem;
2136 ssize_t len = 0, cur_len;
2137 unsigned char buf[LU_GROUP_NAME_BUF];
2139 memset(buf, 0, LU_GROUP_NAME_BUF);
2141 spin_lock(&lu_gp->lu_gp_lock);
2142 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2143 dev = lu_gp_mem->lu_gp_mem_dev;
2144 su_dev = dev->se_sub_dev;
2145 hba = su_dev->se_dev_hba;
2147 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2148 config_item_name(&hba->hba_group.cg_item),
2149 config_item_name(&su_dev->se_dev_group.cg_item));
2150 cur_len++; /* Extra byte for NULL terminator */
2152 if ((cur_len + len) > PAGE_SIZE) {
2153 pr_warn("Ran out of lu_gp_show_attr"
2154 "_members buffer\n");
2155 break;
2157 memcpy(page+len, buf, cur_len);
2158 len += cur_len;
2160 spin_unlock(&lu_gp->lu_gp_lock);
2162 return len;
2165 SE_DEV_ALUA_LU_ATTR_RO(members);
2167 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2169 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2170 &target_core_alua_lu_gp_lu_gp_id.attr,
2171 &target_core_alua_lu_gp_members.attr,
2172 NULL,
2175 static void target_core_alua_lu_gp_release(struct config_item *item)
2177 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2178 struct t10_alua_lu_gp, lu_gp_group);
2180 core_alua_free_lu_gp(lu_gp);
2183 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2184 .release = target_core_alua_lu_gp_release,
2185 .show_attribute = target_core_alua_lu_gp_attr_show,
2186 .store_attribute = target_core_alua_lu_gp_attr_store,
2189 static struct config_item_type target_core_alua_lu_gp_cit = {
2190 .ct_item_ops = &target_core_alua_lu_gp_ops,
2191 .ct_attrs = target_core_alua_lu_gp_attrs,
2192 .ct_owner = THIS_MODULE,
2195 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2197 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2199 static struct config_group *target_core_alua_create_lu_gp(
2200 struct config_group *group,
2201 const char *name)
2203 struct t10_alua_lu_gp *lu_gp;
2204 struct config_group *alua_lu_gp_cg = NULL;
2205 struct config_item *alua_lu_gp_ci = NULL;
2207 lu_gp = core_alua_allocate_lu_gp(name, 0);
2208 if (IS_ERR(lu_gp))
2209 return NULL;
2211 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2212 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2214 config_group_init_type_name(alua_lu_gp_cg, name,
2215 &target_core_alua_lu_gp_cit);
2217 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2218 " Group: core/alua/lu_gps/%s\n",
2219 config_item_name(alua_lu_gp_ci));
2221 return alua_lu_gp_cg;
2225 static void target_core_alua_drop_lu_gp(
2226 struct config_group *group,
2227 struct config_item *item)
2229 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2230 struct t10_alua_lu_gp, lu_gp_group);
2232 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2233 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2234 config_item_name(item), lu_gp->lu_gp_id);
2236 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2237 * -> target_core_alua_lu_gp_release()
2239 config_item_put(item);
2242 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2243 .make_group = &target_core_alua_create_lu_gp,
2244 .drop_item = &target_core_alua_drop_lu_gp,
2247 static struct config_item_type target_core_alua_lu_gps_cit = {
2248 .ct_item_ops = NULL,
2249 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2250 .ct_owner = THIS_MODULE,
2253 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2255 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2257 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2258 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2259 static struct target_core_alua_tg_pt_gp_attribute \
2260 target_core_alua_tg_pt_gp_##_name = \
2261 __CONFIGFS_EATTR(_name, _mode, \
2262 target_core_alua_tg_pt_gp_show_attr_##_name, \
2263 target_core_alua_tg_pt_gp_store_attr_##_name);
2265 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2266 static struct target_core_alua_tg_pt_gp_attribute \
2267 target_core_alua_tg_pt_gp_##_name = \
2268 __CONFIGFS_EATTR_RO(_name, \
2269 target_core_alua_tg_pt_gp_show_attr_##_name);
2272 * alua_access_state
2274 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2275 struct t10_alua_tg_pt_gp *tg_pt_gp,
2276 char *page)
2278 return sprintf(page, "%d\n",
2279 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2282 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2283 struct t10_alua_tg_pt_gp *tg_pt_gp,
2284 const char *page,
2285 size_t count)
2287 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2288 unsigned long tmp;
2289 int new_state, ret;
2291 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2292 pr_err("Unable to do implict ALUA on non valid"
2293 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2294 return -EINVAL;
2297 ret = strict_strtoul(page, 0, &tmp);
2298 if (ret < 0) {
2299 pr_err("Unable to extract new ALUA access state from"
2300 " %s\n", page);
2301 return -EINVAL;
2303 new_state = (int)tmp;
2305 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2306 pr_err("Unable to process implict configfs ALUA"
2307 " transition while TPGS_IMPLICT_ALUA is diabled\n");
2308 return -EINVAL;
2311 ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2312 NULL, NULL, new_state, 0);
2313 return (!ret) ? count : -EINVAL;
2316 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2319 * alua_access_status
2321 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2322 struct t10_alua_tg_pt_gp *tg_pt_gp,
2323 char *page)
2325 return sprintf(page, "%s\n",
2326 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2329 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2330 struct t10_alua_tg_pt_gp *tg_pt_gp,
2331 const char *page,
2332 size_t count)
2334 unsigned long tmp;
2335 int new_status, ret;
2337 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2338 pr_err("Unable to do set ALUA access status on non"
2339 " valid tg_pt_gp ID: %hu\n",
2340 tg_pt_gp->tg_pt_gp_valid_id);
2341 return -EINVAL;
2344 ret = strict_strtoul(page, 0, &tmp);
2345 if (ret < 0) {
2346 pr_err("Unable to extract new ALUA access status"
2347 " from %s\n", page);
2348 return -EINVAL;
2350 new_status = (int)tmp;
2352 if ((new_status != ALUA_STATUS_NONE) &&
2353 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2354 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2355 pr_err("Illegal ALUA access status: 0x%02x\n",
2356 new_status);
2357 return -EINVAL;
2360 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2361 return count;
2364 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2367 * alua_access_type
2369 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2370 struct t10_alua_tg_pt_gp *tg_pt_gp,
2371 char *page)
2373 return core_alua_show_access_type(tg_pt_gp, page);
2376 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2377 struct t10_alua_tg_pt_gp *tg_pt_gp,
2378 const char *page,
2379 size_t count)
2381 return core_alua_store_access_type(tg_pt_gp, page, count);
2384 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2387 * alua_write_metadata
2389 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2390 struct t10_alua_tg_pt_gp *tg_pt_gp,
2391 char *page)
2393 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2396 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2397 struct t10_alua_tg_pt_gp *tg_pt_gp,
2398 const char *page,
2399 size_t count)
2401 unsigned long tmp;
2402 int ret;
2404 ret = strict_strtoul(page, 0, &tmp);
2405 if (ret < 0) {
2406 pr_err("Unable to extract alua_write_metadata\n");
2407 return -EINVAL;
2410 if ((tmp != 0) && (tmp != 1)) {
2411 pr_err("Illegal value for alua_write_metadata:"
2412 " %lu\n", tmp);
2413 return -EINVAL;
2415 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2417 return count;
2420 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2425 * nonop_delay_msecs
2427 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2428 struct t10_alua_tg_pt_gp *tg_pt_gp,
2429 char *page)
2431 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2435 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2436 struct t10_alua_tg_pt_gp *tg_pt_gp,
2437 const char *page,
2438 size_t count)
2440 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2443 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2446 * trans_delay_msecs
2448 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2449 struct t10_alua_tg_pt_gp *tg_pt_gp,
2450 char *page)
2452 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2455 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2456 struct t10_alua_tg_pt_gp *tg_pt_gp,
2457 const char *page,
2458 size_t count)
2460 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2463 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2466 * preferred
2469 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2470 struct t10_alua_tg_pt_gp *tg_pt_gp,
2471 char *page)
2473 return core_alua_show_preferred_bit(tg_pt_gp, page);
2476 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2477 struct t10_alua_tg_pt_gp *tg_pt_gp,
2478 const char *page,
2479 size_t count)
2481 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2484 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2487 * tg_pt_gp_id
2489 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2490 struct t10_alua_tg_pt_gp *tg_pt_gp,
2491 char *page)
2493 if (!tg_pt_gp->tg_pt_gp_valid_id)
2494 return 0;
2496 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2499 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2500 struct t10_alua_tg_pt_gp *tg_pt_gp,
2501 const char *page,
2502 size_t count)
2504 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2505 unsigned long tg_pt_gp_id;
2506 int ret;
2508 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2509 if (ret < 0) {
2510 pr_err("strict_strtoul() returned %d for"
2511 " tg_pt_gp_id\n", ret);
2512 return -EINVAL;
2514 if (tg_pt_gp_id > 0x0000ffff) {
2515 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2516 " 0x0000ffff\n", tg_pt_gp_id);
2517 return -EINVAL;
2520 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2521 if (ret < 0)
2522 return -EINVAL;
2524 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2525 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2526 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2527 tg_pt_gp->tg_pt_gp_id);
2529 return count;
2532 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2535 * members
2537 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2538 struct t10_alua_tg_pt_gp *tg_pt_gp,
2539 char *page)
2541 struct se_port *port;
2542 struct se_portal_group *tpg;
2543 struct se_lun *lun;
2544 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2545 ssize_t len = 0, cur_len;
2546 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2548 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2550 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2551 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2552 tg_pt_gp_mem_list) {
2553 port = tg_pt_gp_mem->tg_pt;
2554 tpg = port->sep_tpg;
2555 lun = port->sep_lun;
2557 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2558 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2559 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2560 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2561 config_item_name(&lun->lun_group.cg_item));
2562 cur_len++; /* Extra byte for NULL terminator */
2564 if ((cur_len + len) > PAGE_SIZE) {
2565 pr_warn("Ran out of lu_gp_show_attr"
2566 "_members buffer\n");
2567 break;
2569 memcpy(page+len, buf, cur_len);
2570 len += cur_len;
2572 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2574 return len;
2577 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2579 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2580 tg_pt_gp_group);
2582 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2583 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2584 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2585 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2586 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2587 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2588 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2589 &target_core_alua_tg_pt_gp_preferred.attr,
2590 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2591 &target_core_alua_tg_pt_gp_members.attr,
2592 NULL,
2595 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2597 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2598 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2600 core_alua_free_tg_pt_gp(tg_pt_gp);
2603 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2604 .release = target_core_alua_tg_pt_gp_release,
2605 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2606 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2609 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2610 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2611 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2612 .ct_owner = THIS_MODULE,
2615 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2617 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2619 static struct config_group *target_core_alua_create_tg_pt_gp(
2620 struct config_group *group,
2621 const char *name)
2623 struct t10_alua *alua = container_of(group, struct t10_alua,
2624 alua_tg_pt_gps_group);
2625 struct t10_alua_tg_pt_gp *tg_pt_gp;
2626 struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2627 struct config_group *alua_tg_pt_gp_cg = NULL;
2628 struct config_item *alua_tg_pt_gp_ci = NULL;
2630 tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2631 if (!tg_pt_gp)
2632 return NULL;
2634 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2635 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2637 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2638 &target_core_alua_tg_pt_gp_cit);
2640 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2641 " Group: alua/tg_pt_gps/%s\n",
2642 config_item_name(alua_tg_pt_gp_ci));
2644 return alua_tg_pt_gp_cg;
2647 static void target_core_alua_drop_tg_pt_gp(
2648 struct config_group *group,
2649 struct config_item *item)
2651 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2652 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2654 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2655 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2656 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2658 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2659 * -> target_core_alua_tg_pt_gp_release().
2661 config_item_put(item);
2664 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2665 .make_group = &target_core_alua_create_tg_pt_gp,
2666 .drop_item = &target_core_alua_drop_tg_pt_gp,
2669 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2670 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2671 .ct_owner = THIS_MODULE,
2674 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2676 /* Start functions for struct config_item_type target_core_alua_cit */
2679 * target_core_alua_cit is a ConfigFS group that lives under
2680 * /sys/kernel/config/target/core/alua. There are default groups
2681 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2682 * target_core_alua_cit in target_core_init_configfs() below.
2684 static struct config_item_type target_core_alua_cit = {
2685 .ct_item_ops = NULL,
2686 .ct_attrs = NULL,
2687 .ct_owner = THIS_MODULE,
2690 /* End functions for struct config_item_type target_core_alua_cit */
2692 /* Start functions for struct config_item_type target_core_stat_cit */
2694 static struct config_group *target_core_stat_mkdir(
2695 struct config_group *group,
2696 const char *name)
2698 return ERR_PTR(-ENOSYS);
2701 static void target_core_stat_rmdir(
2702 struct config_group *group,
2703 struct config_item *item)
2705 return;
2708 static struct configfs_group_operations target_core_stat_group_ops = {
2709 .make_group = &target_core_stat_mkdir,
2710 .drop_item = &target_core_stat_rmdir,
2713 static struct config_item_type target_core_stat_cit = {
2714 .ct_group_ops = &target_core_stat_group_ops,
2715 .ct_owner = THIS_MODULE,
2718 /* End functions for struct config_item_type target_core_stat_cit */
2720 /* Start functions for struct config_item_type target_core_hba_cit */
2722 static struct config_group *target_core_make_subdev(
2723 struct config_group *group,
2724 const char *name)
2726 struct t10_alua_tg_pt_gp *tg_pt_gp;
2727 struct se_subsystem_dev *se_dev;
2728 struct se_subsystem_api *t;
2729 struct config_item *hba_ci = &group->cg_item;
2730 struct se_hba *hba = item_to_hba(hba_ci);
2731 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2732 struct config_group *dev_stat_grp = NULL;
2733 int errno = -ENOMEM, ret;
2735 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2736 if (ret)
2737 return ERR_PTR(ret);
2739 * Locate the struct se_subsystem_api from parent's struct se_hba.
2741 t = hba->transport;
2743 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2744 if (!se_dev) {
2745 pr_err("Unable to allocate memory for"
2746 " struct se_subsystem_dev\n");
2747 goto unlock;
2749 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2750 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2751 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
2752 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
2753 spin_lock_init(&se_dev->t10_pr.registration_lock);
2754 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
2755 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2756 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2757 spin_lock_init(&se_dev->se_dev_lock);
2758 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2759 se_dev->t10_wwn.t10_sub_dev = se_dev;
2760 se_dev->t10_alua.t10_sub_dev = se_dev;
2761 se_dev->se_dev_attrib.da_sub_dev = se_dev;
2763 se_dev->se_dev_hba = hba;
2764 dev_cg = &se_dev->se_dev_group;
2766 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2767 GFP_KERNEL);
2768 if (!dev_cg->default_groups)
2769 goto out;
2771 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2772 * for ->allocate_virtdevice()
2774 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2775 * has been called successfully in the next level up in the
2776 * configfs tree for device object's struct config_group.
2778 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2779 if (!se_dev->se_dev_su_ptr) {
2780 pr_err("Unable to locate subsystem dependent pointer"
2781 " from allocate_virtdevice()\n");
2782 goto out;
2785 config_group_init_type_name(&se_dev->se_dev_group, name,
2786 &target_core_dev_cit);
2787 config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2788 &target_core_dev_attrib_cit);
2789 config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2790 &target_core_dev_pr_cit);
2791 config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2792 &target_core_dev_wwn_cit);
2793 config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2794 "alua", &target_core_alua_tg_pt_gps_cit);
2795 config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2796 "statistics", &target_core_stat_cit);
2798 dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2799 dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2800 dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2801 dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2802 dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2803 dev_cg->default_groups[5] = NULL;
2805 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2807 tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2808 if (!tg_pt_gp)
2809 goto out;
2811 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2812 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2813 GFP_KERNEL);
2814 if (!tg_pt_gp_cg->default_groups) {
2815 pr_err("Unable to allocate tg_pt_gp_cg->"
2816 "default_groups\n");
2817 goto out;
2820 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2821 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2822 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2823 tg_pt_gp_cg->default_groups[1] = NULL;
2824 se_dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2826 * Add core/$HBA/$DEV/statistics/ default groups
2828 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2829 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2830 GFP_KERNEL);
2831 if (!dev_stat_grp->default_groups) {
2832 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2833 goto out;
2835 target_stat_setup_dev_default_groups(se_dev);
2837 pr_debug("Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2838 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2840 mutex_unlock(&hba->hba_access_mutex);
2841 return &se_dev->se_dev_group;
2842 out:
2843 if (se_dev->t10_alua.default_tg_pt_gp) {
2844 core_alua_free_tg_pt_gp(se_dev->t10_alua.default_tg_pt_gp);
2845 se_dev->t10_alua.default_tg_pt_gp = NULL;
2847 if (dev_stat_grp)
2848 kfree(dev_stat_grp->default_groups);
2849 if (tg_pt_gp_cg)
2850 kfree(tg_pt_gp_cg->default_groups);
2851 if (dev_cg)
2852 kfree(dev_cg->default_groups);
2853 if (se_dev->se_dev_su_ptr)
2854 t->free_device(se_dev->se_dev_su_ptr);
2855 kfree(se_dev);
2856 unlock:
2857 mutex_unlock(&hba->hba_access_mutex);
2858 return ERR_PTR(errno);
2861 static void target_core_drop_subdev(
2862 struct config_group *group,
2863 struct config_item *item)
2865 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2866 struct se_subsystem_dev, se_dev_group);
2867 struct se_hba *hba;
2868 struct se_subsystem_api *t;
2869 struct config_item *df_item;
2870 struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2871 int i;
2873 hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2875 mutex_lock(&hba->hba_access_mutex);
2876 t = hba->transport;
2878 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2879 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2880 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2881 dev_stat_grp->default_groups[i] = NULL;
2882 config_item_put(df_item);
2884 kfree(dev_stat_grp->default_groups);
2886 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2887 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2888 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2889 tg_pt_gp_cg->default_groups[i] = NULL;
2890 config_item_put(df_item);
2892 kfree(tg_pt_gp_cg->default_groups);
2894 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2895 * directly from target_core_alua_tg_pt_gp_release().
2897 se_dev->t10_alua.default_tg_pt_gp = NULL;
2899 dev_cg = &se_dev->se_dev_group;
2900 for (i = 0; dev_cg->default_groups[i]; i++) {
2901 df_item = &dev_cg->default_groups[i]->cg_item;
2902 dev_cg->default_groups[i] = NULL;
2903 config_item_put(df_item);
2906 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2907 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2909 config_item_put(item);
2910 mutex_unlock(&hba->hba_access_mutex);
2913 static struct configfs_group_operations target_core_hba_group_ops = {
2914 .make_group = target_core_make_subdev,
2915 .drop_item = target_core_drop_subdev,
2918 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2919 #define SE_HBA_ATTR(_name, _mode) \
2920 static struct target_core_hba_attribute \
2921 target_core_hba_##_name = \
2922 __CONFIGFS_EATTR(_name, _mode, \
2923 target_core_hba_show_attr_##_name, \
2924 target_core_hba_store_attr_##_name);
2926 #define SE_HBA_ATTR_RO(_name) \
2927 static struct target_core_hba_attribute \
2928 target_core_hba_##_name = \
2929 __CONFIGFS_EATTR_RO(_name, \
2930 target_core_hba_show_attr_##_name);
2932 static ssize_t target_core_hba_show_attr_hba_info(
2933 struct se_hba *hba,
2934 char *page)
2936 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2937 hba->hba_id, hba->transport->name,
2938 TARGET_CORE_CONFIGFS_VERSION);
2941 SE_HBA_ATTR_RO(hba_info);
2943 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2944 char *page)
2946 int hba_mode = 0;
2948 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2949 hba_mode = 1;
2951 return sprintf(page, "%d\n", hba_mode);
2954 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2955 const char *page, size_t count)
2957 struct se_subsystem_api *transport = hba->transport;
2958 unsigned long mode_flag;
2959 int ret;
2961 if (transport->pmode_enable_hba == NULL)
2962 return -EINVAL;
2964 ret = strict_strtoul(page, 0, &mode_flag);
2965 if (ret < 0) {
2966 pr_err("Unable to extract hba mode flag: %d\n", ret);
2967 return -EINVAL;
2970 spin_lock(&hba->device_lock);
2971 if (!list_empty(&hba->hba_dev_list)) {
2972 pr_err("Unable to set hba_mode with active devices\n");
2973 spin_unlock(&hba->device_lock);
2974 return -EINVAL;
2976 spin_unlock(&hba->device_lock);
2978 ret = transport->pmode_enable_hba(hba, mode_flag);
2979 if (ret < 0)
2980 return -EINVAL;
2981 if (ret > 0)
2982 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2983 else if (ret == 0)
2984 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2986 return count;
2989 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2991 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2993 static void target_core_hba_release(struct config_item *item)
2995 struct se_hba *hba = container_of(to_config_group(item),
2996 struct se_hba, hba_group);
2997 core_delete_hba(hba);
3000 static struct configfs_attribute *target_core_hba_attrs[] = {
3001 &target_core_hba_hba_info.attr,
3002 &target_core_hba_hba_mode.attr,
3003 NULL,
3006 static struct configfs_item_operations target_core_hba_item_ops = {
3007 .release = target_core_hba_release,
3008 .show_attribute = target_core_hba_attr_show,
3009 .store_attribute = target_core_hba_attr_store,
3012 static struct config_item_type target_core_hba_cit = {
3013 .ct_item_ops = &target_core_hba_item_ops,
3014 .ct_group_ops = &target_core_hba_group_ops,
3015 .ct_attrs = target_core_hba_attrs,
3016 .ct_owner = THIS_MODULE,
3019 static struct config_group *target_core_call_addhbatotarget(
3020 struct config_group *group,
3021 const char *name)
3023 char *se_plugin_str, *str, *str2;
3024 struct se_hba *hba;
3025 char buf[TARGET_CORE_NAME_MAX_LEN];
3026 unsigned long plugin_dep_id = 0;
3027 int ret;
3029 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3030 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3031 pr_err("Passed *name strlen(): %d exceeds"
3032 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3033 TARGET_CORE_NAME_MAX_LEN);
3034 return ERR_PTR(-ENAMETOOLONG);
3036 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3038 str = strstr(buf, "_");
3039 if (!str) {
3040 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3041 return ERR_PTR(-EINVAL);
3043 se_plugin_str = buf;
3045 * Special case for subsystem plugins that have "_" in their names.
3046 * Namely rd_direct and rd_mcp..
3048 str2 = strstr(str+1, "_");
3049 if (str2) {
3050 *str2 = '\0'; /* Terminate for *se_plugin_str */
3051 str2++; /* Skip to start of plugin dependent ID */
3052 str = str2;
3053 } else {
3054 *str = '\0'; /* Terminate for *se_plugin_str */
3055 str++; /* Skip to start of plugin dependent ID */
3058 ret = strict_strtoul(str, 0, &plugin_dep_id);
3059 if (ret < 0) {
3060 pr_err("strict_strtoul() returned %d for"
3061 " plugin_dep_id\n", ret);
3062 return ERR_PTR(-EINVAL);
3065 * Load up TCM subsystem plugins if they have not already been loaded.
3067 transport_subsystem_check_init();
3069 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3070 if (IS_ERR(hba))
3071 return ERR_CAST(hba);
3073 config_group_init_type_name(&hba->hba_group, name,
3074 &target_core_hba_cit);
3076 return &hba->hba_group;
3079 static void target_core_call_delhbafromtarget(
3080 struct config_group *group,
3081 struct config_item *item)
3084 * core_delete_hba() is called from target_core_hba_item_ops->release()
3085 * -> target_core_hba_release()
3087 config_item_put(item);
3090 static struct configfs_group_operations target_core_group_ops = {
3091 .make_group = target_core_call_addhbatotarget,
3092 .drop_item = target_core_call_delhbafromtarget,
3095 static struct config_item_type target_core_cit = {
3096 .ct_item_ops = NULL,
3097 .ct_group_ops = &target_core_group_ops,
3098 .ct_attrs = NULL,
3099 .ct_owner = THIS_MODULE,
3102 /* Stop functions for struct config_item_type target_core_hba_cit */
3104 static int __init target_core_init_configfs(void)
3106 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3107 struct config_group *lu_gp_cg = NULL;
3108 struct configfs_subsystem *subsys;
3109 struct t10_alua_lu_gp *lu_gp;
3110 int ret;
3112 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3113 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3114 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3116 subsys = target_core_subsystem[0];
3117 config_group_init(&subsys->su_group);
3118 mutex_init(&subsys->su_mutex);
3120 INIT_LIST_HEAD(&g_tf_list);
3121 mutex_init(&g_tf_lock);
3122 ret = init_se_kmem_caches();
3123 if (ret < 0)
3124 return ret;
3126 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3127 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3129 target_cg = &subsys->su_group;
3130 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3131 GFP_KERNEL);
3132 if (!target_cg->default_groups) {
3133 pr_err("Unable to allocate target_cg->default_groups\n");
3134 goto out_global;
3137 config_group_init_type_name(&target_core_hbagroup,
3138 "core", &target_core_cit);
3139 target_cg->default_groups[0] = &target_core_hbagroup;
3140 target_cg->default_groups[1] = NULL;
3142 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3144 hba_cg = &target_core_hbagroup;
3145 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3146 GFP_KERNEL);
3147 if (!hba_cg->default_groups) {
3148 pr_err("Unable to allocate hba_cg->default_groups\n");
3149 goto out_global;
3151 config_group_init_type_name(&alua_group,
3152 "alua", &target_core_alua_cit);
3153 hba_cg->default_groups[0] = &alua_group;
3154 hba_cg->default_groups[1] = NULL;
3156 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3157 * groups under /sys/kernel/config/target/core/alua/
3159 alua_cg = &alua_group;
3160 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3161 GFP_KERNEL);
3162 if (!alua_cg->default_groups) {
3163 pr_err("Unable to allocate alua_cg->default_groups\n");
3164 goto out_global;
3167 config_group_init_type_name(&alua_lu_gps_group,
3168 "lu_gps", &target_core_alua_lu_gps_cit);
3169 alua_cg->default_groups[0] = &alua_lu_gps_group;
3170 alua_cg->default_groups[1] = NULL;
3172 * Add core/alua/lu_gps/default_lu_gp
3174 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3175 if (IS_ERR(lu_gp))
3176 goto out_global;
3178 lu_gp_cg = &alua_lu_gps_group;
3179 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3180 GFP_KERNEL);
3181 if (!lu_gp_cg->default_groups) {
3182 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
3183 goto out_global;
3186 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3187 &target_core_alua_lu_gp_cit);
3188 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3189 lu_gp_cg->default_groups[1] = NULL;
3190 default_lu_gp = lu_gp;
3192 * Register the target_core_mod subsystem with configfs.
3194 ret = configfs_register_subsystem(subsys);
3195 if (ret < 0) {
3196 pr_err("Error %d while registering subsystem %s\n",
3197 ret, subsys->su_group.cg_item.ci_namebuf);
3198 goto out_global;
3200 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3201 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3202 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3204 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3206 ret = rd_module_init();
3207 if (ret < 0)
3208 goto out;
3210 if (core_dev_setup_virtual_lun0() < 0)
3211 goto out;
3213 return 0;
3215 out:
3216 configfs_unregister_subsystem(subsys);
3217 core_dev_release_virtual_lun0();
3218 rd_module_exit();
3219 out_global:
3220 if (default_lu_gp) {
3221 core_alua_free_lu_gp(default_lu_gp);
3222 default_lu_gp = NULL;
3224 if (lu_gp_cg)
3225 kfree(lu_gp_cg->default_groups);
3226 if (alua_cg)
3227 kfree(alua_cg->default_groups);
3228 if (hba_cg)
3229 kfree(hba_cg->default_groups);
3230 kfree(target_cg->default_groups);
3231 release_se_kmem_caches();
3232 return ret;
3235 static void __exit target_core_exit_configfs(void)
3237 struct configfs_subsystem *subsys;
3238 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3239 struct config_item *item;
3240 int i;
3242 subsys = target_core_subsystem[0];
3244 lu_gp_cg = &alua_lu_gps_group;
3245 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3246 item = &lu_gp_cg->default_groups[i]->cg_item;
3247 lu_gp_cg->default_groups[i] = NULL;
3248 config_item_put(item);
3250 kfree(lu_gp_cg->default_groups);
3251 lu_gp_cg->default_groups = NULL;
3253 alua_cg = &alua_group;
3254 for (i = 0; alua_cg->default_groups[i]; i++) {
3255 item = &alua_cg->default_groups[i]->cg_item;
3256 alua_cg->default_groups[i] = NULL;
3257 config_item_put(item);
3259 kfree(alua_cg->default_groups);
3260 alua_cg->default_groups = NULL;
3262 hba_cg = &target_core_hbagroup;
3263 for (i = 0; hba_cg->default_groups[i]; i++) {
3264 item = &hba_cg->default_groups[i]->cg_item;
3265 hba_cg->default_groups[i] = NULL;
3266 config_item_put(item);
3268 kfree(hba_cg->default_groups);
3269 hba_cg->default_groups = NULL;
3271 * We expect subsys->su_group.default_groups to be released
3272 * by configfs subsystem provider logic..
3274 configfs_unregister_subsystem(subsys);
3275 kfree(subsys->su_group.default_groups);
3277 core_alua_free_lu_gp(default_lu_gp);
3278 default_lu_gp = NULL;
3280 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3281 " Infrastructure\n");
3283 core_dev_release_virtual_lun0();
3284 rd_module_exit();
3285 release_se_kmem_caches();
3288 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3289 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3290 MODULE_LICENSE("GPL");
3292 module_init(target_core_init_configfs);
3293 module_exit(target_core_exit_configfs);