USB: serial: fix whitespace issues
[zen-stable.git] / drivers / target / target_core_configfs.c
blobe0c1e8a8dd4e2140e13d7adedb4c84d414b45725
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_device.h>
43 #include <target/target_core_transport.h>
44 #include <target/target_core_fabric_ops.h>
45 #include <target/target_core_fabric_configfs.h>
46 #include <target/target_core_configfs.h>
47 #include <target/configfs_macros.h>
49 #include "target_core_alua.h"
50 #include "target_core_hba.h"
51 #include "target_core_pr.h"
52 #include "target_core_rd.h"
53 #include "target_core_stat.h"
55 extern struct t10_alua_lu_gp *default_lu_gp;
57 static struct list_head g_tf_list;
58 static struct mutex g_tf_lock;
60 struct target_core_configfs_attribute {
61 struct configfs_attribute attr;
62 ssize_t (*show)(void *, char *);
63 ssize_t (*store)(void *, const char *, size_t);
66 static struct config_group target_core_hbagroup;
67 static struct config_group alua_group;
68 static struct config_group alua_lu_gps_group;
70 static DEFINE_SPINLOCK(se_device_lock);
71 static LIST_HEAD(se_dev_list);
73 static inline struct se_hba *
74 item_to_hba(struct config_item *item)
76 return container_of(to_config_group(item), struct se_hba, hba_group);
80 * Attributes for /sys/kernel/config/target/
82 static ssize_t target_core_attr_show(struct config_item *item,
83 struct configfs_attribute *attr,
84 char *page)
86 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
87 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
88 utsname()->sysname, utsname()->machine);
91 static struct configfs_item_operations target_core_fabric_item_ops = {
92 .show_attribute = target_core_attr_show,
95 static struct configfs_attribute target_core_item_attr_version = {
96 .ca_owner = THIS_MODULE,
97 .ca_name = "version",
98 .ca_mode = S_IRUGO,
101 static struct target_fabric_configfs *target_core_get_fabric(
102 const char *name)
104 struct target_fabric_configfs *tf;
106 if (!name)
107 return NULL;
109 mutex_lock(&g_tf_lock);
110 list_for_each_entry(tf, &g_tf_list, tf_list) {
111 if (!strcmp(tf->tf_name, name)) {
112 atomic_inc(&tf->tf_access_cnt);
113 mutex_unlock(&g_tf_lock);
114 return tf;
117 mutex_unlock(&g_tf_lock);
119 return NULL;
123 * Called from struct target_core_group_ops->make_group()
125 static struct config_group *target_core_register_fabric(
126 struct config_group *group,
127 const char *name)
129 struct target_fabric_configfs *tf;
130 int ret;
132 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
133 " %s\n", group, name);
135 * Below are some hardcoded request_module() calls to automatically
136 * local fabric modules when the following is called:
138 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
140 * Note that this does not limit which TCM fabric module can be
141 * registered, but simply provids auto loading logic for modules with
142 * mkdir(2) system calls with known TCM fabric modules.
144 if (!strncmp(name, "iscsi", 5)) {
146 * Automatically load the LIO Target fabric module when the
147 * following is called:
149 * mkdir -p $CONFIGFS/target/iscsi
151 ret = request_module("iscsi_target_mod");
152 if (ret < 0) {
153 pr_err("request_module() failed for"
154 " iscsi_target_mod.ko: %d\n", ret);
155 return ERR_PTR(-EINVAL);
157 } else if (!strncmp(name, "loopback", 8)) {
159 * Automatically load the tcm_loop fabric module when the
160 * following is called:
162 * mkdir -p $CONFIGFS/target/loopback
164 ret = request_module("tcm_loop");
165 if (ret < 0) {
166 pr_err("request_module() failed for"
167 " tcm_loop.ko: %d\n", ret);
168 return ERR_PTR(-EINVAL);
172 tf = target_core_get_fabric(name);
173 if (!tf) {
174 pr_err("target_core_get_fabric() failed for %s\n",
175 name);
176 return ERR_PTR(-EINVAL);
178 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
179 " %s\n", tf->tf_name);
181 * On a successful target_core_get_fabric() look, the returned
182 * struct target_fabric_configfs *tf will contain a usage reference.
184 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
185 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
187 tf->tf_group.default_groups = tf->tf_default_groups;
188 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
189 tf->tf_group.default_groups[1] = NULL;
191 config_group_init_type_name(&tf->tf_group, name,
192 &TF_CIT_TMPL(tf)->tfc_wwn_cit);
193 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
194 &TF_CIT_TMPL(tf)->tfc_discovery_cit);
196 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
197 " %s\n", tf->tf_group.cg_item.ci_name);
199 * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
201 tf->tf_ops.tf_subsys = tf->tf_subsys;
202 tf->tf_fabric = &tf->tf_group.cg_item;
203 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
204 " for %s\n", name);
206 return &tf->tf_group;
210 * Called from struct target_core_group_ops->drop_item()
212 static void target_core_deregister_fabric(
213 struct config_group *group,
214 struct config_item *item)
216 struct target_fabric_configfs *tf = container_of(
217 to_config_group(item), struct target_fabric_configfs, tf_group);
218 struct config_group *tf_group;
219 struct config_item *df_item;
220 int i;
222 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
223 " tf list\n", config_item_name(item));
225 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
226 " %s\n", tf->tf_name);
227 atomic_dec(&tf->tf_access_cnt);
229 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
230 " tf->tf_fabric for %s\n", tf->tf_name);
231 tf->tf_fabric = NULL;
233 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
234 " %s\n", config_item_name(item));
236 tf_group = &tf->tf_group;
237 for (i = 0; tf_group->default_groups[i]; i++) {
238 df_item = &tf_group->default_groups[i]->cg_item;
239 tf_group->default_groups[i] = NULL;
240 config_item_put(df_item);
242 config_item_put(item);
245 static struct configfs_group_operations target_core_fabric_group_ops = {
246 .make_group = &target_core_register_fabric,
247 .drop_item = &target_core_deregister_fabric,
251 * All item attributes appearing in /sys/kernel/target/ appear here.
253 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
254 &target_core_item_attr_version,
255 NULL,
259 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
261 static struct config_item_type target_core_fabrics_item = {
262 .ct_item_ops = &target_core_fabric_item_ops,
263 .ct_group_ops = &target_core_fabric_group_ops,
264 .ct_attrs = target_core_fabric_item_attrs,
265 .ct_owner = THIS_MODULE,
268 static struct configfs_subsystem target_core_fabrics = {
269 .su_group = {
270 .cg_item = {
271 .ci_namebuf = "target",
272 .ci_type = &target_core_fabrics_item,
277 static struct configfs_subsystem *target_core_subsystem[] = {
278 &target_core_fabrics,
279 NULL,
282 /*##############################################################################
283 // Start functions called by external Target Fabrics Modules
284 //############################################################################*/
287 * First function called by fabric modules to:
289 * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
290 * 2) Add struct target_fabric_configfs to g_tf_list
291 * 3) Return struct target_fabric_configfs to fabric module to be passed
292 * into target_fabric_configfs_register().
294 struct target_fabric_configfs *target_fabric_configfs_init(
295 struct module *fabric_mod,
296 const char *name)
298 struct target_fabric_configfs *tf;
300 if (!(name)) {
301 pr_err("Unable to locate passed fabric name\n");
302 return ERR_PTR(-EINVAL);
304 if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
305 pr_err("Passed name: %s exceeds TARGET_FABRIC"
306 "_NAME_SIZE\n", name);
307 return ERR_PTR(-EINVAL);
310 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
311 if (!tf)
312 return ERR_PTR(-ENOMEM);
314 INIT_LIST_HEAD(&tf->tf_list);
315 atomic_set(&tf->tf_access_cnt, 0);
317 * Setup the default generic struct config_item_type's (cits) in
318 * struct target_fabric_configfs->tf_cit_tmpl
320 tf->tf_module = fabric_mod;
321 target_fabric_setup_cits(tf);
323 tf->tf_subsys = target_core_subsystem[0];
324 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
326 mutex_lock(&g_tf_lock);
327 list_add_tail(&tf->tf_list, &g_tf_list);
328 mutex_unlock(&g_tf_lock);
330 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
331 ">>>>>>>>>>>>>>\n");
332 pr_debug("Initialized struct target_fabric_configfs: %p for"
333 " %s\n", tf, tf->tf_name);
334 return tf;
336 EXPORT_SYMBOL(target_fabric_configfs_init);
339 * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
341 void target_fabric_configfs_free(
342 struct target_fabric_configfs *tf)
344 mutex_lock(&g_tf_lock);
345 list_del(&tf->tf_list);
346 mutex_unlock(&g_tf_lock);
348 kfree(tf);
350 EXPORT_SYMBOL(target_fabric_configfs_free);
353 * Perform a sanity check of the passed tf->tf_ops before completing
354 * TCM fabric module registration.
356 static int target_fabric_tf_ops_check(
357 struct target_fabric_configfs *tf)
359 struct target_core_fabric_ops *tfo = &tf->tf_ops;
361 if (!tfo->get_fabric_name) {
362 pr_err("Missing tfo->get_fabric_name()\n");
363 return -EINVAL;
365 if (!tfo->get_fabric_proto_ident) {
366 pr_err("Missing tfo->get_fabric_proto_ident()\n");
367 return -EINVAL;
369 if (!tfo->tpg_get_wwn) {
370 pr_err("Missing tfo->tpg_get_wwn()\n");
371 return -EINVAL;
373 if (!tfo->tpg_get_tag) {
374 pr_err("Missing tfo->tpg_get_tag()\n");
375 return -EINVAL;
377 if (!tfo->tpg_get_default_depth) {
378 pr_err("Missing tfo->tpg_get_default_depth()\n");
379 return -EINVAL;
381 if (!tfo->tpg_get_pr_transport_id) {
382 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
383 return -EINVAL;
385 if (!tfo->tpg_get_pr_transport_id_len) {
386 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
387 return -EINVAL;
389 if (!tfo->tpg_check_demo_mode) {
390 pr_err("Missing tfo->tpg_check_demo_mode()\n");
391 return -EINVAL;
393 if (!tfo->tpg_check_demo_mode_cache) {
394 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
395 return -EINVAL;
397 if (!tfo->tpg_check_demo_mode_write_protect) {
398 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
399 return -EINVAL;
401 if (!tfo->tpg_check_prod_mode_write_protect) {
402 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
403 return -EINVAL;
405 if (!tfo->tpg_alloc_fabric_acl) {
406 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
407 return -EINVAL;
409 if (!tfo->tpg_release_fabric_acl) {
410 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
411 return -EINVAL;
413 if (!tfo->tpg_get_inst_index) {
414 pr_err("Missing tfo->tpg_get_inst_index()\n");
415 return -EINVAL;
417 if (!tfo->release_cmd) {
418 pr_err("Missing tfo->release_cmd()\n");
419 return -EINVAL;
421 if (!tfo->shutdown_session) {
422 pr_err("Missing tfo->shutdown_session()\n");
423 return -EINVAL;
425 if (!tfo->close_session) {
426 pr_err("Missing tfo->close_session()\n");
427 return -EINVAL;
429 if (!tfo->stop_session) {
430 pr_err("Missing tfo->stop_session()\n");
431 return -EINVAL;
433 if (!tfo->fall_back_to_erl0) {
434 pr_err("Missing tfo->fall_back_to_erl0()\n");
435 return -EINVAL;
437 if (!tfo->sess_logged_in) {
438 pr_err("Missing tfo->sess_logged_in()\n");
439 return -EINVAL;
441 if (!tfo->sess_get_index) {
442 pr_err("Missing tfo->sess_get_index()\n");
443 return -EINVAL;
445 if (!tfo->write_pending) {
446 pr_err("Missing tfo->write_pending()\n");
447 return -EINVAL;
449 if (!tfo->write_pending_status) {
450 pr_err("Missing tfo->write_pending_status()\n");
451 return -EINVAL;
453 if (!tfo->set_default_node_attributes) {
454 pr_err("Missing tfo->set_default_node_attributes()\n");
455 return -EINVAL;
457 if (!tfo->get_task_tag) {
458 pr_err("Missing tfo->get_task_tag()\n");
459 return -EINVAL;
461 if (!tfo->get_cmd_state) {
462 pr_err("Missing tfo->get_cmd_state()\n");
463 return -EINVAL;
465 if (!tfo->queue_data_in) {
466 pr_err("Missing tfo->queue_data_in()\n");
467 return -EINVAL;
469 if (!tfo->queue_status) {
470 pr_err("Missing tfo->queue_status()\n");
471 return -EINVAL;
473 if (!tfo->queue_tm_rsp) {
474 pr_err("Missing tfo->queue_tm_rsp()\n");
475 return -EINVAL;
477 if (!tfo->set_fabric_sense_len) {
478 pr_err("Missing tfo->set_fabric_sense_len()\n");
479 return -EINVAL;
481 if (!tfo->get_fabric_sense_len) {
482 pr_err("Missing tfo->get_fabric_sense_len()\n");
483 return -EINVAL;
485 if (!tfo->is_state_remove) {
486 pr_err("Missing tfo->is_state_remove()\n");
487 return -EINVAL;
490 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
491 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
492 * target_core_fabric_configfs.c WWN+TPG group context code.
494 if (!tfo->fabric_make_wwn) {
495 pr_err("Missing tfo->fabric_make_wwn()\n");
496 return -EINVAL;
498 if (!tfo->fabric_drop_wwn) {
499 pr_err("Missing tfo->fabric_drop_wwn()\n");
500 return -EINVAL;
502 if (!tfo->fabric_make_tpg) {
503 pr_err("Missing tfo->fabric_make_tpg()\n");
504 return -EINVAL;
506 if (!tfo->fabric_drop_tpg) {
507 pr_err("Missing tfo->fabric_drop_tpg()\n");
508 return -EINVAL;
511 return 0;
515 * Called 2nd from fabric module with returned parameter of
516 * struct target_fabric_configfs * from target_fabric_configfs_init().
518 * Upon a successful registration, the new fabric's struct config_item is
519 * return. Also, a pointer to this struct is set in the passed
520 * struct target_fabric_configfs.
522 int target_fabric_configfs_register(
523 struct target_fabric_configfs *tf)
525 int ret;
527 if (!tf) {
528 pr_err("Unable to locate target_fabric_configfs"
529 " pointer\n");
530 return -EINVAL;
532 if (!tf->tf_subsys) {
533 pr_err("Unable to target struct config_subsystem"
534 " pointer\n");
535 return -EINVAL;
537 ret = target_fabric_tf_ops_check(tf);
538 if (ret < 0)
539 return ret;
541 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
542 ">>>>>>>>>>\n");
543 return 0;
545 EXPORT_SYMBOL(target_fabric_configfs_register);
547 void target_fabric_configfs_deregister(
548 struct target_fabric_configfs *tf)
550 struct configfs_subsystem *su;
552 if (!tf) {
553 pr_err("Unable to locate passed target_fabric_"
554 "configfs\n");
555 return;
557 su = tf->tf_subsys;
558 if (!su) {
559 pr_err("Unable to locate passed tf->tf_subsys"
560 " pointer\n");
561 return;
563 pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
564 ">>>>>>>>>>>>\n");
565 mutex_lock(&g_tf_lock);
566 if (atomic_read(&tf->tf_access_cnt)) {
567 mutex_unlock(&g_tf_lock);
568 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
569 tf->tf_name);
570 BUG();
572 list_del(&tf->tf_list);
573 mutex_unlock(&g_tf_lock);
575 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
576 " %s\n", tf->tf_name);
577 tf->tf_module = NULL;
578 tf->tf_subsys = NULL;
579 kfree(tf);
581 pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
582 ">>>>>\n");
584 EXPORT_SYMBOL(target_fabric_configfs_deregister);
586 /*##############################################################################
587 // Stop functions called by external Target Fabrics Modules
588 //############################################################################*/
590 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
592 #define DEF_DEV_ATTRIB_SHOW(_name) \
593 static ssize_t target_core_dev_show_attr_##_name( \
594 struct se_dev_attrib *da, \
595 char *page) \
597 struct se_device *dev; \
598 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
599 ssize_t rb; \
601 spin_lock(&se_dev->se_dev_lock); \
602 dev = se_dev->se_dev_ptr; \
603 if (!dev) { \
604 spin_unlock(&se_dev->se_dev_lock); \
605 return -ENODEV; \
607 rb = snprintf(page, PAGE_SIZE, "%u\n", \
608 (u32)dev->se_sub_dev->se_dev_attrib._name); \
609 spin_unlock(&se_dev->se_dev_lock); \
611 return rb; \
614 #define DEF_DEV_ATTRIB_STORE(_name) \
615 static ssize_t target_core_dev_store_attr_##_name( \
616 struct se_dev_attrib *da, \
617 const char *page, \
618 size_t count) \
620 struct se_device *dev; \
621 struct se_subsystem_dev *se_dev = da->da_sub_dev; \
622 unsigned long val; \
623 int ret; \
625 spin_lock(&se_dev->se_dev_lock); \
626 dev = se_dev->se_dev_ptr; \
627 if (!dev) { \
628 spin_unlock(&se_dev->se_dev_lock); \
629 return -ENODEV; \
631 ret = strict_strtoul(page, 0, &val); \
632 if (ret < 0) { \
633 spin_unlock(&se_dev->se_dev_lock); \
634 pr_err("strict_strtoul() failed with" \
635 " ret: %d\n", ret); \
636 return -EINVAL; \
638 ret = se_dev_set_##_name(dev, (u32)val); \
639 spin_unlock(&se_dev->se_dev_lock); \
641 return (!ret) ? count : -EINVAL; \
644 #define DEF_DEV_ATTRIB(_name) \
645 DEF_DEV_ATTRIB_SHOW(_name); \
646 DEF_DEV_ATTRIB_STORE(_name);
648 #define DEF_DEV_ATTRIB_RO(_name) \
649 DEF_DEV_ATTRIB_SHOW(_name);
651 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
652 #define SE_DEV_ATTR(_name, _mode) \
653 static struct target_core_dev_attrib_attribute \
654 target_core_dev_attrib_##_name = \
655 __CONFIGFS_EATTR(_name, _mode, \
656 target_core_dev_show_attr_##_name, \
657 target_core_dev_store_attr_##_name);
659 #define SE_DEV_ATTR_RO(_name); \
660 static struct target_core_dev_attrib_attribute \
661 target_core_dev_attrib_##_name = \
662 __CONFIGFS_EATTR_RO(_name, \
663 target_core_dev_show_attr_##_name);
665 DEF_DEV_ATTRIB(emulate_dpo);
666 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
668 DEF_DEV_ATTRIB(emulate_fua_write);
669 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
671 DEF_DEV_ATTRIB(emulate_fua_read);
672 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
674 DEF_DEV_ATTRIB(emulate_write_cache);
675 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
677 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
678 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
680 DEF_DEV_ATTRIB(emulate_tas);
681 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
683 DEF_DEV_ATTRIB(emulate_tpu);
684 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
686 DEF_DEV_ATTRIB(emulate_tpws);
687 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
689 DEF_DEV_ATTRIB(enforce_pr_isids);
690 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
692 DEF_DEV_ATTRIB(is_nonrot);
693 SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
695 DEF_DEV_ATTRIB(emulate_rest_reord);
696 SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
698 DEF_DEV_ATTRIB_RO(hw_block_size);
699 SE_DEV_ATTR_RO(hw_block_size);
701 DEF_DEV_ATTRIB(block_size);
702 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
704 DEF_DEV_ATTRIB_RO(hw_max_sectors);
705 SE_DEV_ATTR_RO(hw_max_sectors);
707 DEF_DEV_ATTRIB(max_sectors);
708 SE_DEV_ATTR(max_sectors, S_IRUGO | S_IWUSR);
710 DEF_DEV_ATTRIB(optimal_sectors);
711 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
713 DEF_DEV_ATTRIB_RO(hw_queue_depth);
714 SE_DEV_ATTR_RO(hw_queue_depth);
716 DEF_DEV_ATTRIB(queue_depth);
717 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
719 DEF_DEV_ATTRIB(max_unmap_lba_count);
720 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
722 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
723 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
725 DEF_DEV_ATTRIB(unmap_granularity);
726 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
728 DEF_DEV_ATTRIB(unmap_granularity_alignment);
729 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
731 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
733 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
734 &target_core_dev_attrib_emulate_dpo.attr,
735 &target_core_dev_attrib_emulate_fua_write.attr,
736 &target_core_dev_attrib_emulate_fua_read.attr,
737 &target_core_dev_attrib_emulate_write_cache.attr,
738 &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
739 &target_core_dev_attrib_emulate_tas.attr,
740 &target_core_dev_attrib_emulate_tpu.attr,
741 &target_core_dev_attrib_emulate_tpws.attr,
742 &target_core_dev_attrib_enforce_pr_isids.attr,
743 &target_core_dev_attrib_is_nonrot.attr,
744 &target_core_dev_attrib_emulate_rest_reord.attr,
745 &target_core_dev_attrib_hw_block_size.attr,
746 &target_core_dev_attrib_block_size.attr,
747 &target_core_dev_attrib_hw_max_sectors.attr,
748 &target_core_dev_attrib_max_sectors.attr,
749 &target_core_dev_attrib_optimal_sectors.attr,
750 &target_core_dev_attrib_hw_queue_depth.attr,
751 &target_core_dev_attrib_queue_depth.attr,
752 &target_core_dev_attrib_max_unmap_lba_count.attr,
753 &target_core_dev_attrib_max_unmap_block_desc_count.attr,
754 &target_core_dev_attrib_unmap_granularity.attr,
755 &target_core_dev_attrib_unmap_granularity_alignment.attr,
756 NULL,
759 static struct configfs_item_operations target_core_dev_attrib_ops = {
760 .show_attribute = target_core_dev_attrib_attr_show,
761 .store_attribute = target_core_dev_attrib_attr_store,
764 static struct config_item_type target_core_dev_attrib_cit = {
765 .ct_item_ops = &target_core_dev_attrib_ops,
766 .ct_attrs = target_core_dev_attrib_attrs,
767 .ct_owner = THIS_MODULE,
770 /* End functions for struct config_item_type target_core_dev_attrib_cit */
772 /* Start functions for struct config_item_type target_core_dev_wwn_cit */
774 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
775 #define SE_DEV_WWN_ATTR(_name, _mode) \
776 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
777 __CONFIGFS_EATTR(_name, _mode, \
778 target_core_dev_wwn_show_attr_##_name, \
779 target_core_dev_wwn_store_attr_##_name);
781 #define SE_DEV_WWN_ATTR_RO(_name); \
782 do { \
783 static struct target_core_dev_wwn_attribute \
784 target_core_dev_wwn_##_name = \
785 __CONFIGFS_EATTR_RO(_name, \
786 target_core_dev_wwn_show_attr_##_name); \
787 } while (0);
790 * VPD page 0x80 Unit serial
792 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
793 struct t10_wwn *t10_wwn,
794 char *page)
796 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
797 struct se_device *dev;
799 dev = se_dev->se_dev_ptr;
800 if (!dev)
801 return -ENODEV;
803 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
804 &t10_wwn->unit_serial[0]);
807 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
808 struct t10_wwn *t10_wwn,
809 const char *page,
810 size_t count)
812 struct se_subsystem_dev *su_dev = t10_wwn->t10_sub_dev;
813 struct se_device *dev;
814 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
817 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
818 * from the struct scsi_device level firmware, do not allow
819 * VPD Unit Serial to be emulated.
821 * Note this struct scsi_device could also be emulating VPD
822 * information from its drivers/scsi LLD. But for now we assume
823 * it is doing 'the right thing' wrt a world wide unique
824 * VPD Unit Serial Number that OS dependent multipath can depend on.
826 if (su_dev->su_dev_flags & SDF_FIRMWARE_VPD_UNIT_SERIAL) {
827 pr_err("Underlying SCSI device firmware provided VPD"
828 " Unit Serial, ignoring request\n");
829 return -EOPNOTSUPP;
832 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
833 pr_err("Emulated VPD Unit Serial exceeds"
834 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
835 return -EOVERFLOW;
838 * Check to see if any active $FABRIC_MOD exports exist. If they
839 * do exist, fail here as changing this information on the fly
840 * (underneath the initiator side OS dependent multipath code)
841 * could cause negative effects.
843 dev = su_dev->se_dev_ptr;
844 if (dev) {
845 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
846 pr_err("Unable to set VPD Unit Serial while"
847 " active %d $FABRIC_MOD exports exist\n",
848 atomic_read(&dev->dev_export_obj.obj_access_count));
849 return -EINVAL;
853 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
855 * Also, strip any newline added from the userspace
856 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
858 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
859 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
860 snprintf(su_dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
861 "%s", strstrip(buf));
862 su_dev->su_dev_flags |= SDF_EMULATED_VPD_UNIT_SERIAL;
864 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
865 " %s\n", su_dev->t10_wwn.unit_serial);
867 return count;
870 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
873 * VPD page 0x83 Protocol Identifier
875 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
876 struct t10_wwn *t10_wwn,
877 char *page)
879 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev;
880 struct se_device *dev;
881 struct t10_vpd *vpd;
882 unsigned char buf[VPD_TMP_BUF_SIZE];
883 ssize_t len = 0;
885 dev = se_dev->se_dev_ptr;
886 if (!dev)
887 return -ENODEV;
889 memset(buf, 0, VPD_TMP_BUF_SIZE);
891 spin_lock(&t10_wwn->t10_vpd_lock);
892 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
893 if (!vpd->protocol_identifier_set)
894 continue;
896 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
898 if (len + strlen(buf) >= PAGE_SIZE)
899 break;
901 len += sprintf(page+len, "%s", buf);
903 spin_unlock(&t10_wwn->t10_vpd_lock);
905 return len;
908 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
909 struct t10_wwn *t10_wwn,
910 const char *page,
911 size_t count)
913 return -ENOSYS;
916 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
919 * Generic wrapper for dumping VPD identifiers by association.
921 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
922 static ssize_t target_core_dev_wwn_show_attr_##_name( \
923 struct t10_wwn *t10_wwn, \
924 char *page) \
926 struct se_subsystem_dev *se_dev = t10_wwn->t10_sub_dev; \
927 struct se_device *dev; \
928 struct t10_vpd *vpd; \
929 unsigned char buf[VPD_TMP_BUF_SIZE]; \
930 ssize_t len = 0; \
932 dev = se_dev->se_dev_ptr; \
933 if (!dev) \
934 return -ENODEV; \
936 spin_lock(&t10_wwn->t10_vpd_lock); \
937 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
938 if (vpd->association != _assoc) \
939 continue; \
941 memset(buf, 0, VPD_TMP_BUF_SIZE); \
942 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
943 if (len + strlen(buf) >= PAGE_SIZE) \
944 break; \
945 len += sprintf(page+len, "%s", buf); \
947 memset(buf, 0, VPD_TMP_BUF_SIZE); \
948 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
949 if (len + strlen(buf) >= PAGE_SIZE) \
950 break; \
951 len += sprintf(page+len, "%s", buf); \
953 memset(buf, 0, VPD_TMP_BUF_SIZE); \
954 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
955 if (len + strlen(buf) >= PAGE_SIZE) \
956 break; \
957 len += sprintf(page+len, "%s", buf); \
959 spin_unlock(&t10_wwn->t10_vpd_lock); \
961 return len; \
965 * VPD page 0x83 Association: Logical Unit
967 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
969 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
970 struct t10_wwn *t10_wwn,
971 const char *page,
972 size_t count)
974 return -ENOSYS;
977 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
980 * VPD page 0x83 Association: Target Port
982 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
984 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
985 struct t10_wwn *t10_wwn,
986 const char *page,
987 size_t count)
989 return -ENOSYS;
992 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
995 * VPD page 0x83 Association: SCSI Target Device
997 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
999 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
1000 struct t10_wwn *t10_wwn,
1001 const char *page,
1002 size_t count)
1004 return -ENOSYS;
1007 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
1009 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
1011 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1012 &target_core_dev_wwn_vpd_unit_serial.attr,
1013 &target_core_dev_wwn_vpd_protocol_identifier.attr,
1014 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
1015 &target_core_dev_wwn_vpd_assoc_target_port.attr,
1016 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
1017 NULL,
1020 static struct configfs_item_operations target_core_dev_wwn_ops = {
1021 .show_attribute = target_core_dev_wwn_attr_show,
1022 .store_attribute = target_core_dev_wwn_attr_store,
1025 static struct config_item_type target_core_dev_wwn_cit = {
1026 .ct_item_ops = &target_core_dev_wwn_ops,
1027 .ct_attrs = target_core_dev_wwn_attrs,
1028 .ct_owner = THIS_MODULE,
1031 /* End functions for struct config_item_type target_core_dev_wwn_cit */
1033 /* Start functions for struct config_item_type target_core_dev_pr_cit */
1035 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_subsystem_dev);
1036 #define SE_DEV_PR_ATTR(_name, _mode) \
1037 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1038 __CONFIGFS_EATTR(_name, _mode, \
1039 target_core_dev_pr_show_attr_##_name, \
1040 target_core_dev_pr_store_attr_##_name);
1042 #define SE_DEV_PR_ATTR_RO(_name); \
1043 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
1044 __CONFIGFS_EATTR_RO(_name, \
1045 target_core_dev_pr_show_attr_##_name);
1048 * res_holder
1050 static ssize_t target_core_dev_pr_show_spc3_res(
1051 struct se_device *dev,
1052 char *page,
1053 ssize_t *len)
1055 struct se_node_acl *se_nacl;
1056 struct t10_pr_registration *pr_reg;
1057 char i_buf[PR_REG_ISID_ID_LEN];
1058 int prf_isid;
1060 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1062 spin_lock(&dev->dev_reservation_lock);
1063 pr_reg = dev->dev_pr_res_holder;
1064 if (!pr_reg) {
1065 *len += sprintf(page + *len, "No SPC-3 Reservation holder\n");
1066 spin_unlock(&dev->dev_reservation_lock);
1067 return *len;
1069 se_nacl = pr_reg->pr_reg_nacl;
1070 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1071 PR_REG_ISID_ID_LEN);
1073 *len += sprintf(page + *len, "SPC-3 Reservation: %s Initiator: %s%s\n",
1074 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1075 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1076 spin_unlock(&dev->dev_reservation_lock);
1078 return *len;
1081 static ssize_t target_core_dev_pr_show_spc2_res(
1082 struct se_device *dev,
1083 char *page,
1084 ssize_t *len)
1086 struct se_node_acl *se_nacl;
1088 spin_lock(&dev->dev_reservation_lock);
1089 se_nacl = dev->dev_reserved_node_acl;
1090 if (!se_nacl) {
1091 *len += sprintf(page + *len, "No SPC-2 Reservation holder\n");
1092 spin_unlock(&dev->dev_reservation_lock);
1093 return *len;
1095 *len += sprintf(page + *len, "SPC-2 Reservation: %s Initiator: %s\n",
1096 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1097 se_nacl->initiatorname);
1098 spin_unlock(&dev->dev_reservation_lock);
1100 return *len;
1103 static ssize_t target_core_dev_pr_show_attr_res_holder(
1104 struct se_subsystem_dev *su_dev,
1105 char *page)
1107 ssize_t len = 0;
1109 if (!su_dev->se_dev_ptr)
1110 return -ENODEV;
1112 switch (su_dev->t10_pr.res_type) {
1113 case SPC3_PERSISTENT_RESERVATIONS:
1114 target_core_dev_pr_show_spc3_res(su_dev->se_dev_ptr,
1115 page, &len);
1116 break;
1117 case SPC2_RESERVATIONS:
1118 target_core_dev_pr_show_spc2_res(su_dev->se_dev_ptr,
1119 page, &len);
1120 break;
1121 case SPC_PASSTHROUGH:
1122 len += sprintf(page+len, "Passthrough\n");
1123 break;
1124 default:
1125 len += sprintf(page+len, "Unknown\n");
1126 break;
1129 return len;
1132 SE_DEV_PR_ATTR_RO(res_holder);
1135 * res_pr_all_tgt_pts
1137 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1138 struct se_subsystem_dev *su_dev,
1139 char *page)
1141 struct se_device *dev;
1142 struct t10_pr_registration *pr_reg;
1143 ssize_t len = 0;
1145 dev = su_dev->se_dev_ptr;
1146 if (!dev)
1147 return -ENODEV;
1149 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1150 return len;
1152 spin_lock(&dev->dev_reservation_lock);
1153 pr_reg = dev->dev_pr_res_holder;
1154 if (!pr_reg) {
1155 len = sprintf(page, "No SPC-3 Reservation holder\n");
1156 spin_unlock(&dev->dev_reservation_lock);
1157 return len;
1160 * See All Target Ports (ALL_TG_PT) bit in spcr17, section 6.14.3
1161 * Basic PERSISTENT RESERVER OUT parameter list, page 290
1163 if (pr_reg->pr_reg_all_tg_pt)
1164 len = sprintf(page, "SPC-3 Reservation: All Target"
1165 " Ports registration\n");
1166 else
1167 len = sprintf(page, "SPC-3 Reservation: Single"
1168 " Target Port registration\n");
1169 spin_unlock(&dev->dev_reservation_lock);
1171 return len;
1174 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1177 * res_pr_generation
1179 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1180 struct se_subsystem_dev *su_dev,
1181 char *page)
1183 if (!su_dev->se_dev_ptr)
1184 return -ENODEV;
1186 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1187 return 0;
1189 return sprintf(page, "0x%08x\n", su_dev->t10_pr.pr_generation);
1192 SE_DEV_PR_ATTR_RO(res_pr_generation);
1195 * res_pr_holder_tg_port
1197 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1198 struct se_subsystem_dev *su_dev,
1199 char *page)
1201 struct se_device *dev;
1202 struct se_node_acl *se_nacl;
1203 struct se_lun *lun;
1204 struct se_portal_group *se_tpg;
1205 struct t10_pr_registration *pr_reg;
1206 struct target_core_fabric_ops *tfo;
1207 ssize_t len = 0;
1209 dev = su_dev->se_dev_ptr;
1210 if (!dev)
1211 return -ENODEV;
1213 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1214 return len;
1216 spin_lock(&dev->dev_reservation_lock);
1217 pr_reg = dev->dev_pr_res_holder;
1218 if (!pr_reg) {
1219 len = sprintf(page, "No SPC-3 Reservation holder\n");
1220 spin_unlock(&dev->dev_reservation_lock);
1221 return len;
1223 se_nacl = pr_reg->pr_reg_nacl;
1224 se_tpg = se_nacl->se_tpg;
1225 lun = pr_reg->pr_reg_tg_pt_lun;
1226 tfo = se_tpg->se_tpg_tfo;
1228 len += sprintf(page+len, "SPC-3 Reservation: %s"
1229 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1230 tfo->tpg_get_wwn(se_tpg));
1231 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1232 " Identifer Tag: %hu %s Portal Group Tag: %hu"
1233 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1234 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1235 tfo->get_fabric_name(), lun->unpacked_lun);
1236 spin_unlock(&dev->dev_reservation_lock);
1238 return len;
1241 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1244 * res_pr_registered_i_pts
1246 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1247 struct se_subsystem_dev *su_dev,
1248 char *page)
1250 struct target_core_fabric_ops *tfo;
1251 struct t10_pr_registration *pr_reg;
1252 unsigned char buf[384];
1253 char i_buf[PR_REG_ISID_ID_LEN];
1254 ssize_t len = 0;
1255 int reg_count = 0, prf_isid;
1257 if (!su_dev->se_dev_ptr)
1258 return -ENODEV;
1260 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1261 return len;
1263 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1265 spin_lock(&su_dev->t10_pr.registration_lock);
1266 list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
1267 pr_reg_list) {
1269 memset(buf, 0, 384);
1270 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1271 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1272 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1273 PR_REG_ISID_ID_LEN);
1274 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1275 tfo->get_fabric_name(),
1276 pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1277 &i_buf[0] : "", pr_reg->pr_res_key,
1278 pr_reg->pr_res_generation);
1280 if (len + strlen(buf) >= PAGE_SIZE)
1281 break;
1283 len += sprintf(page+len, "%s", buf);
1284 reg_count++;
1286 spin_unlock(&su_dev->t10_pr.registration_lock);
1288 if (!reg_count)
1289 len += sprintf(page+len, "None\n");
1291 return len;
1294 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1297 * res_pr_type
1299 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1300 struct se_subsystem_dev *su_dev,
1301 char *page)
1303 struct se_device *dev;
1304 struct t10_pr_registration *pr_reg;
1305 ssize_t len = 0;
1307 dev = su_dev->se_dev_ptr;
1308 if (!dev)
1309 return -ENODEV;
1311 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1312 return len;
1314 spin_lock(&dev->dev_reservation_lock);
1315 pr_reg = dev->dev_pr_res_holder;
1316 if (!pr_reg) {
1317 len = sprintf(page, "No SPC-3 Reservation holder\n");
1318 spin_unlock(&dev->dev_reservation_lock);
1319 return len;
1321 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1322 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1323 spin_unlock(&dev->dev_reservation_lock);
1325 return len;
1328 SE_DEV_PR_ATTR_RO(res_pr_type);
1331 * res_type
1333 static ssize_t target_core_dev_pr_show_attr_res_type(
1334 struct se_subsystem_dev *su_dev,
1335 char *page)
1337 ssize_t len = 0;
1339 if (!su_dev->se_dev_ptr)
1340 return -ENODEV;
1342 switch (su_dev->t10_pr.res_type) {
1343 case SPC3_PERSISTENT_RESERVATIONS:
1344 len = sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1345 break;
1346 case SPC2_RESERVATIONS:
1347 len = sprintf(page, "SPC2_RESERVATIONS\n");
1348 break;
1349 case SPC_PASSTHROUGH:
1350 len = sprintf(page, "SPC_PASSTHROUGH\n");
1351 break;
1352 default:
1353 len = sprintf(page, "UNKNOWN\n");
1354 break;
1357 return len;
1360 SE_DEV_PR_ATTR_RO(res_type);
1363 * res_aptpl_active
1366 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1367 struct se_subsystem_dev *su_dev,
1368 char *page)
1370 if (!su_dev->se_dev_ptr)
1371 return -ENODEV;
1373 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1374 return 0;
1376 return sprintf(page, "APTPL Bit Status: %s\n",
1377 (su_dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1380 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1383 * res_aptpl_metadata
1385 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1386 struct se_subsystem_dev *su_dev,
1387 char *page)
1389 if (!su_dev->se_dev_ptr)
1390 return -ENODEV;
1392 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1393 return 0;
1395 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1398 enum {
1399 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1400 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1401 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1402 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1405 static match_table_t tokens = {
1406 {Opt_initiator_fabric, "initiator_fabric=%s"},
1407 {Opt_initiator_node, "initiator_node=%s"},
1408 {Opt_initiator_sid, "initiator_sid=%s"},
1409 {Opt_sa_res_key, "sa_res_key=%s"},
1410 {Opt_res_holder, "res_holder=%d"},
1411 {Opt_res_type, "res_type=%d"},
1412 {Opt_res_scope, "res_scope=%d"},
1413 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1414 {Opt_mapped_lun, "mapped_lun=%d"},
1415 {Opt_target_fabric, "target_fabric=%s"},
1416 {Opt_target_node, "target_node=%s"},
1417 {Opt_tpgt, "tpgt=%d"},
1418 {Opt_port_rtpi, "port_rtpi=%d"},
1419 {Opt_target_lun, "target_lun=%d"},
1420 {Opt_err, NULL}
1423 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1424 struct se_subsystem_dev *su_dev,
1425 const char *page,
1426 size_t count)
1428 struct se_device *dev;
1429 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1430 unsigned char *t_fabric = NULL, *t_port = NULL;
1431 char *orig, *ptr, *arg_p, *opts;
1432 substring_t args[MAX_OPT_ARGS];
1433 unsigned long long tmp_ll;
1434 u64 sa_res_key = 0;
1435 u32 mapped_lun = 0, target_lun = 0;
1436 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1437 u16 port_rpti = 0, tpgt = 0;
1438 u8 type = 0, scope;
1440 dev = su_dev->se_dev_ptr;
1441 if (!dev)
1442 return -ENODEV;
1444 if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
1445 return 0;
1447 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1448 pr_debug("Unable to process APTPL metadata while"
1449 " active fabric exports exist\n");
1450 return -EINVAL;
1453 opts = kstrdup(page, GFP_KERNEL);
1454 if (!opts)
1455 return -ENOMEM;
1457 orig = opts;
1458 while ((ptr = strsep(&opts, ",")) != NULL) {
1459 if (!*ptr)
1460 continue;
1462 token = match_token(ptr, tokens, args);
1463 switch (token) {
1464 case Opt_initiator_fabric:
1465 i_fabric = match_strdup(&args[0]);
1466 if (!i_fabric) {
1467 ret = -ENOMEM;
1468 goto out;
1470 break;
1471 case Opt_initiator_node:
1472 i_port = match_strdup(&args[0]);
1473 if (!i_port) {
1474 ret = -ENOMEM;
1475 goto out;
1477 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1478 pr_err("APTPL metadata initiator_node="
1479 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1480 PR_APTPL_MAX_IPORT_LEN);
1481 ret = -EINVAL;
1482 break;
1484 break;
1485 case Opt_initiator_sid:
1486 isid = match_strdup(&args[0]);
1487 if (!isid) {
1488 ret = -ENOMEM;
1489 goto out;
1491 if (strlen(isid) >= PR_REG_ISID_LEN) {
1492 pr_err("APTPL metadata initiator_isid"
1493 "= exceeds PR_REG_ISID_LEN: %d\n",
1494 PR_REG_ISID_LEN);
1495 ret = -EINVAL;
1496 break;
1498 break;
1499 case Opt_sa_res_key:
1500 arg_p = match_strdup(&args[0]);
1501 if (!arg_p) {
1502 ret = -ENOMEM;
1503 goto out;
1505 ret = strict_strtoull(arg_p, 0, &tmp_ll);
1506 if (ret < 0) {
1507 pr_err("strict_strtoull() failed for"
1508 " sa_res_key=\n");
1509 goto out;
1511 sa_res_key = (u64)tmp_ll;
1512 break;
1514 * PR APTPL Metadata for Reservation
1516 case Opt_res_holder:
1517 match_int(args, &arg);
1518 res_holder = arg;
1519 break;
1520 case Opt_res_type:
1521 match_int(args, &arg);
1522 type = (u8)arg;
1523 break;
1524 case Opt_res_scope:
1525 match_int(args, &arg);
1526 scope = (u8)arg;
1527 break;
1528 case Opt_res_all_tg_pt:
1529 match_int(args, &arg);
1530 all_tg_pt = (int)arg;
1531 break;
1532 case Opt_mapped_lun:
1533 match_int(args, &arg);
1534 mapped_lun = (u32)arg;
1535 break;
1537 * PR APTPL Metadata for Target Port
1539 case Opt_target_fabric:
1540 t_fabric = match_strdup(&args[0]);
1541 if (!t_fabric) {
1542 ret = -ENOMEM;
1543 goto out;
1545 break;
1546 case Opt_target_node:
1547 t_port = match_strdup(&args[0]);
1548 if (!t_port) {
1549 ret = -ENOMEM;
1550 goto out;
1552 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1553 pr_err("APTPL metadata target_node="
1554 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1555 PR_APTPL_MAX_TPORT_LEN);
1556 ret = -EINVAL;
1557 break;
1559 break;
1560 case Opt_tpgt:
1561 match_int(args, &arg);
1562 tpgt = (u16)arg;
1563 break;
1564 case Opt_port_rtpi:
1565 match_int(args, &arg);
1566 port_rpti = (u16)arg;
1567 break;
1568 case Opt_target_lun:
1569 match_int(args, &arg);
1570 target_lun = (u32)arg;
1571 break;
1572 default:
1573 break;
1577 if (!i_port || !t_port || !sa_res_key) {
1578 pr_err("Illegal parameters for APTPL registration\n");
1579 ret = -EINVAL;
1580 goto out;
1583 if (res_holder && !(type)) {
1584 pr_err("Illegal PR type: 0x%02x for reservation"
1585 " holder\n", type);
1586 ret = -EINVAL;
1587 goto out;
1590 ret = core_scsi3_alloc_aptpl_registration(&su_dev->t10_pr, sa_res_key,
1591 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1592 res_holder, all_tg_pt, type);
1593 out:
1594 kfree(i_fabric);
1595 kfree(i_port);
1596 kfree(isid);
1597 kfree(t_fabric);
1598 kfree(t_port);
1599 kfree(orig);
1600 return (ret == 0) ? count : ret;
1603 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1605 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1607 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1608 &target_core_dev_pr_res_holder.attr,
1609 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1610 &target_core_dev_pr_res_pr_generation.attr,
1611 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1612 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1613 &target_core_dev_pr_res_pr_type.attr,
1614 &target_core_dev_pr_res_type.attr,
1615 &target_core_dev_pr_res_aptpl_active.attr,
1616 &target_core_dev_pr_res_aptpl_metadata.attr,
1617 NULL,
1620 static struct configfs_item_operations target_core_dev_pr_ops = {
1621 .show_attribute = target_core_dev_pr_attr_show,
1622 .store_attribute = target_core_dev_pr_attr_store,
1625 static struct config_item_type target_core_dev_pr_cit = {
1626 .ct_item_ops = &target_core_dev_pr_ops,
1627 .ct_attrs = target_core_dev_pr_attrs,
1628 .ct_owner = THIS_MODULE,
1631 /* End functions for struct config_item_type target_core_dev_pr_cit */
1633 /* Start functions for struct config_item_type target_core_dev_cit */
1635 static ssize_t target_core_show_dev_info(void *p, char *page)
1637 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1638 struct se_hba *hba = se_dev->se_dev_hba;
1639 struct se_subsystem_api *t = hba->transport;
1640 int bl = 0;
1641 ssize_t read_bytes = 0;
1643 if (!se_dev->se_dev_ptr)
1644 return -ENODEV;
1646 transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1647 read_bytes += bl;
1648 read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1649 return read_bytes;
1652 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1653 .attr = { .ca_owner = THIS_MODULE,
1654 .ca_name = "info",
1655 .ca_mode = S_IRUGO },
1656 .show = target_core_show_dev_info,
1657 .store = NULL,
1660 static ssize_t target_core_store_dev_control(
1661 void *p,
1662 const char *page,
1663 size_t count)
1665 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1666 struct se_hba *hba = se_dev->se_dev_hba;
1667 struct se_subsystem_api *t = hba->transport;
1669 if (!se_dev->se_dev_su_ptr) {
1670 pr_err("Unable to locate struct se_subsystem_dev>se"
1671 "_dev_su_ptr\n");
1672 return -EINVAL;
1675 return t->set_configfs_dev_params(hba, se_dev, page, count);
1678 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1679 .attr = { .ca_owner = THIS_MODULE,
1680 .ca_name = "control",
1681 .ca_mode = S_IWUSR },
1682 .show = NULL,
1683 .store = target_core_store_dev_control,
1686 static ssize_t target_core_show_dev_alias(void *p, char *page)
1688 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1690 if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1691 return 0;
1693 return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1696 static ssize_t target_core_store_dev_alias(
1697 void *p,
1698 const char *page,
1699 size_t count)
1701 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1702 struct se_hba *hba = se_dev->se_dev_hba;
1703 ssize_t read_bytes;
1705 if (count > (SE_DEV_ALIAS_LEN-1)) {
1706 pr_err("alias count: %d exceeds"
1707 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1708 SE_DEV_ALIAS_LEN-1);
1709 return -EINVAL;
1712 se_dev->su_dev_flags |= SDF_USING_ALIAS;
1713 read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1714 "%s", page);
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 = (struct se_subsystem_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 = (struct se_subsystem_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 se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1759 read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1760 "%s", page);
1762 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1763 config_item_name(&hba->hba_group.cg_item),
1764 config_item_name(&se_dev->se_dev_group.cg_item),
1765 se_dev->se_dev_udev_path);
1767 return read_bytes;
1770 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1771 .attr = { .ca_owner = THIS_MODULE,
1772 .ca_name = "udev_path",
1773 .ca_mode = S_IRUGO | S_IWUSR },
1774 .show = target_core_show_dev_udev_path,
1775 .store = target_core_store_dev_udev_path,
1778 static ssize_t target_core_store_dev_enable(
1779 void *p,
1780 const char *page,
1781 size_t count)
1783 struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1784 struct se_device *dev;
1785 struct se_hba *hba = se_dev->se_dev_hba;
1786 struct se_subsystem_api *t = hba->transport;
1787 char *ptr;
1789 ptr = strstr(page, "1");
1790 if (!ptr) {
1791 pr_err("For dev_enable ops, only valid value"
1792 " is \"1\"\n");
1793 return -EINVAL;
1795 if (se_dev->se_dev_ptr) {
1796 pr_err("se_dev->se_dev_ptr already set for storage"
1797 " object\n");
1798 return -EEXIST;
1801 if (t->check_configfs_dev_params(hba, se_dev) < 0)
1802 return -EINVAL;
1804 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1805 if (IS_ERR(dev))
1806 return PTR_ERR(dev);
1807 else if (!dev)
1808 return -EINVAL;
1810 se_dev->se_dev_ptr = dev;
1811 pr_debug("Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1812 " %p\n", se_dev->se_dev_ptr);
1814 return count;
1817 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1818 .attr = { .ca_owner = THIS_MODULE,
1819 .ca_name = "enable",
1820 .ca_mode = S_IWUSR },
1821 .show = NULL,
1822 .store = target_core_store_dev_enable,
1825 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1827 struct se_device *dev;
1828 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1829 struct config_item *lu_ci;
1830 struct t10_alua_lu_gp *lu_gp;
1831 struct t10_alua_lu_gp_member *lu_gp_mem;
1832 ssize_t len = 0;
1834 dev = su_dev->se_dev_ptr;
1835 if (!dev)
1836 return -ENODEV;
1838 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED)
1839 return len;
1841 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1842 if (!lu_gp_mem) {
1843 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1844 " pointer\n");
1845 return -EINVAL;
1848 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1849 lu_gp = lu_gp_mem->lu_gp;
1850 if (lu_gp) {
1851 lu_ci = &lu_gp->lu_gp_group.cg_item;
1852 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1853 config_item_name(lu_ci), lu_gp->lu_gp_id);
1855 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1857 return len;
1860 static ssize_t target_core_store_alua_lu_gp(
1861 void *p,
1862 const char *page,
1863 size_t count)
1865 struct se_device *dev;
1866 struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1867 struct se_hba *hba = su_dev->se_dev_hba;
1868 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1869 struct t10_alua_lu_gp_member *lu_gp_mem;
1870 unsigned char buf[LU_GROUP_NAME_BUF];
1871 int move = 0;
1873 dev = su_dev->se_dev_ptr;
1874 if (!dev)
1875 return -ENODEV;
1877 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1878 pr_warn("SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1879 config_item_name(&hba->hba_group.cg_item),
1880 config_item_name(&su_dev->se_dev_group.cg_item));
1881 return -EINVAL;
1883 if (count > LU_GROUP_NAME_BUF) {
1884 pr_err("ALUA LU Group Alias too large!\n");
1885 return -EINVAL;
1887 memset(buf, 0, LU_GROUP_NAME_BUF);
1888 memcpy(buf, page, count);
1890 * Any ALUA logical unit alias besides "NULL" means we will be
1891 * making a new group association.
1893 if (strcmp(strstrip(buf), "NULL")) {
1895 * core_alua_get_lu_gp_by_name() will increment reference to
1896 * struct t10_alua_lu_gp. This reference is released with
1897 * core_alua_get_lu_gp_by_name below().
1899 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1900 if (!lu_gp_new)
1901 return -ENODEV;
1903 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1904 if (!lu_gp_mem) {
1905 if (lu_gp_new)
1906 core_alua_put_lu_gp_from_name(lu_gp_new);
1907 pr_err("NULL struct se_device->dev_alua_lu_gp_mem"
1908 " pointer\n");
1909 return -EINVAL;
1912 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1913 lu_gp = lu_gp_mem->lu_gp;
1914 if (lu_gp) {
1916 * Clearing an existing lu_gp association, and replacing
1917 * with NULL
1919 if (!lu_gp_new) {
1920 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1921 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1922 " %hu\n",
1923 config_item_name(&hba->hba_group.cg_item),
1924 config_item_name(&su_dev->se_dev_group.cg_item),
1925 config_item_name(&lu_gp->lu_gp_group.cg_item),
1926 lu_gp->lu_gp_id);
1928 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1929 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1931 return count;
1934 * Removing existing association of lu_gp_mem with lu_gp
1936 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1937 move = 1;
1940 * Associate lu_gp_mem with lu_gp_new.
1942 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1943 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1945 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1946 " core/alua/lu_gps/%s, ID: %hu\n",
1947 (move) ? "Moving" : "Adding",
1948 config_item_name(&hba->hba_group.cg_item),
1949 config_item_name(&su_dev->se_dev_group.cg_item),
1950 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1951 lu_gp_new->lu_gp_id);
1953 core_alua_put_lu_gp_from_name(lu_gp_new);
1954 return count;
1957 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1958 .attr = { .ca_owner = THIS_MODULE,
1959 .ca_name = "alua_lu_gp",
1960 .ca_mode = S_IRUGO | S_IWUSR },
1961 .show = target_core_show_alua_lu_gp,
1962 .store = target_core_store_alua_lu_gp,
1965 static struct configfs_attribute *lio_core_dev_attrs[] = {
1966 &target_core_attr_dev_info.attr,
1967 &target_core_attr_dev_control.attr,
1968 &target_core_attr_dev_alias.attr,
1969 &target_core_attr_dev_udev_path.attr,
1970 &target_core_attr_dev_enable.attr,
1971 &target_core_attr_dev_alua_lu_gp.attr,
1972 NULL,
1975 static void target_core_dev_release(struct config_item *item)
1977 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
1978 struct se_subsystem_dev, se_dev_group);
1979 struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
1980 struct se_subsystem_api *t = hba->transport;
1981 struct config_group *dev_cg = &se_dev->se_dev_group;
1983 kfree(dev_cg->default_groups);
1985 * This pointer will set when the storage is enabled with:
1986 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
1988 if (se_dev->se_dev_ptr) {
1989 pr_debug("Target_Core_ConfigFS: Calling se_free_"
1990 "virtual_device() for se_dev_ptr: %p\n",
1991 se_dev->se_dev_ptr);
1993 se_free_virtual_device(se_dev->se_dev_ptr, hba);
1994 } else {
1996 * Release struct se_subsystem_dev->se_dev_su_ptr..
1998 pr_debug("Target_Core_ConfigFS: Calling t->free_"
1999 "device() for se_dev_su_ptr: %p\n",
2000 se_dev->se_dev_su_ptr);
2002 t->free_device(se_dev->se_dev_su_ptr);
2005 pr_debug("Target_Core_ConfigFS: Deallocating se_subsystem"
2006 "_dev_t: %p\n", se_dev);
2007 kfree(se_dev);
2010 static ssize_t target_core_dev_show(struct config_item *item,
2011 struct configfs_attribute *attr,
2012 char *page)
2014 struct se_subsystem_dev *se_dev = container_of(
2015 to_config_group(item), struct se_subsystem_dev,
2016 se_dev_group);
2017 struct target_core_configfs_attribute *tc_attr = container_of(
2018 attr, struct target_core_configfs_attribute, attr);
2020 if (!tc_attr->show)
2021 return -EINVAL;
2023 return tc_attr->show(se_dev, page);
2026 static ssize_t target_core_dev_store(struct config_item *item,
2027 struct configfs_attribute *attr,
2028 const char *page, size_t count)
2030 struct se_subsystem_dev *se_dev = container_of(
2031 to_config_group(item), struct se_subsystem_dev,
2032 se_dev_group);
2033 struct target_core_configfs_attribute *tc_attr = container_of(
2034 attr, struct target_core_configfs_attribute, attr);
2036 if (!tc_attr->store)
2037 return -EINVAL;
2039 return tc_attr->store(se_dev, page, count);
2042 static struct configfs_item_operations target_core_dev_item_ops = {
2043 .release = target_core_dev_release,
2044 .show_attribute = target_core_dev_show,
2045 .store_attribute = target_core_dev_store,
2048 static struct config_item_type target_core_dev_cit = {
2049 .ct_item_ops = &target_core_dev_item_ops,
2050 .ct_attrs = lio_core_dev_attrs,
2051 .ct_owner = THIS_MODULE,
2054 /* End functions for struct config_item_type target_core_dev_cit */
2056 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2058 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2059 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
2060 static struct target_core_alua_lu_gp_attribute \
2061 target_core_alua_lu_gp_##_name = \
2062 __CONFIGFS_EATTR(_name, _mode, \
2063 target_core_alua_lu_gp_show_attr_##_name, \
2064 target_core_alua_lu_gp_store_attr_##_name);
2066 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
2067 static struct target_core_alua_lu_gp_attribute \
2068 target_core_alua_lu_gp_##_name = \
2069 __CONFIGFS_EATTR_RO(_name, \
2070 target_core_alua_lu_gp_show_attr_##_name);
2073 * lu_gp_id
2075 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2076 struct t10_alua_lu_gp *lu_gp,
2077 char *page)
2079 if (!lu_gp->lu_gp_valid_id)
2080 return 0;
2082 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2085 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2086 struct t10_alua_lu_gp *lu_gp,
2087 const char *page,
2088 size_t count)
2090 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2091 unsigned long lu_gp_id;
2092 int ret;
2094 ret = strict_strtoul(page, 0, &lu_gp_id);
2095 if (ret < 0) {
2096 pr_err("strict_strtoul() returned %d for"
2097 " lu_gp_id\n", ret);
2098 return -EINVAL;
2100 if (lu_gp_id > 0x0000ffff) {
2101 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2102 " 0x0000ffff\n", lu_gp_id);
2103 return -EINVAL;
2106 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2107 if (ret < 0)
2108 return -EINVAL;
2110 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2111 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2112 config_item_name(&alua_lu_gp_cg->cg_item),
2113 lu_gp->lu_gp_id);
2115 return count;
2118 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2121 * members
2123 static ssize_t target_core_alua_lu_gp_show_attr_members(
2124 struct t10_alua_lu_gp *lu_gp,
2125 char *page)
2127 struct se_device *dev;
2128 struct se_hba *hba;
2129 struct se_subsystem_dev *su_dev;
2130 struct t10_alua_lu_gp_member *lu_gp_mem;
2131 ssize_t len = 0, cur_len;
2132 unsigned char buf[LU_GROUP_NAME_BUF];
2134 memset(buf, 0, LU_GROUP_NAME_BUF);
2136 spin_lock(&lu_gp->lu_gp_lock);
2137 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2138 dev = lu_gp_mem->lu_gp_mem_dev;
2139 su_dev = dev->se_sub_dev;
2140 hba = su_dev->se_dev_hba;
2142 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2143 config_item_name(&hba->hba_group.cg_item),
2144 config_item_name(&su_dev->se_dev_group.cg_item));
2145 cur_len++; /* Extra byte for NULL terminator */
2147 if ((cur_len + len) > PAGE_SIZE) {
2148 pr_warn("Ran out of lu_gp_show_attr"
2149 "_members buffer\n");
2150 break;
2152 memcpy(page+len, buf, cur_len);
2153 len += cur_len;
2155 spin_unlock(&lu_gp->lu_gp_lock);
2157 return len;
2160 SE_DEV_ALUA_LU_ATTR_RO(members);
2162 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2164 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2165 &target_core_alua_lu_gp_lu_gp_id.attr,
2166 &target_core_alua_lu_gp_members.attr,
2167 NULL,
2170 static void target_core_alua_lu_gp_release(struct config_item *item)
2172 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2173 struct t10_alua_lu_gp, lu_gp_group);
2175 core_alua_free_lu_gp(lu_gp);
2178 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2179 .release = target_core_alua_lu_gp_release,
2180 .show_attribute = target_core_alua_lu_gp_attr_show,
2181 .store_attribute = target_core_alua_lu_gp_attr_store,
2184 static struct config_item_type target_core_alua_lu_gp_cit = {
2185 .ct_item_ops = &target_core_alua_lu_gp_ops,
2186 .ct_attrs = target_core_alua_lu_gp_attrs,
2187 .ct_owner = THIS_MODULE,
2190 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2192 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2194 static struct config_group *target_core_alua_create_lu_gp(
2195 struct config_group *group,
2196 const char *name)
2198 struct t10_alua_lu_gp *lu_gp;
2199 struct config_group *alua_lu_gp_cg = NULL;
2200 struct config_item *alua_lu_gp_ci = NULL;
2202 lu_gp = core_alua_allocate_lu_gp(name, 0);
2203 if (IS_ERR(lu_gp))
2204 return NULL;
2206 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2207 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2209 config_group_init_type_name(alua_lu_gp_cg, name,
2210 &target_core_alua_lu_gp_cit);
2212 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2213 " Group: core/alua/lu_gps/%s\n",
2214 config_item_name(alua_lu_gp_ci));
2216 return alua_lu_gp_cg;
2220 static void target_core_alua_drop_lu_gp(
2221 struct config_group *group,
2222 struct config_item *item)
2224 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2225 struct t10_alua_lu_gp, lu_gp_group);
2227 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2228 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2229 config_item_name(item), lu_gp->lu_gp_id);
2231 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2232 * -> target_core_alua_lu_gp_release()
2234 config_item_put(item);
2237 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2238 .make_group = &target_core_alua_create_lu_gp,
2239 .drop_item = &target_core_alua_drop_lu_gp,
2242 static struct config_item_type target_core_alua_lu_gps_cit = {
2243 .ct_item_ops = NULL,
2244 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2245 .ct_owner = THIS_MODULE,
2248 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2250 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2252 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2253 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
2254 static struct target_core_alua_tg_pt_gp_attribute \
2255 target_core_alua_tg_pt_gp_##_name = \
2256 __CONFIGFS_EATTR(_name, _mode, \
2257 target_core_alua_tg_pt_gp_show_attr_##_name, \
2258 target_core_alua_tg_pt_gp_store_attr_##_name);
2260 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
2261 static struct target_core_alua_tg_pt_gp_attribute \
2262 target_core_alua_tg_pt_gp_##_name = \
2263 __CONFIGFS_EATTR_RO(_name, \
2264 target_core_alua_tg_pt_gp_show_attr_##_name);
2267 * alua_access_state
2269 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2270 struct t10_alua_tg_pt_gp *tg_pt_gp,
2271 char *page)
2273 return sprintf(page, "%d\n",
2274 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2277 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2278 struct t10_alua_tg_pt_gp *tg_pt_gp,
2279 const char *page,
2280 size_t count)
2282 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2283 unsigned long tmp;
2284 int new_state, ret;
2286 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2287 pr_err("Unable to do implict ALUA on non valid"
2288 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2289 return -EINVAL;
2292 ret = strict_strtoul(page, 0, &tmp);
2293 if (ret < 0) {
2294 pr_err("Unable to extract new ALUA access state from"
2295 " %s\n", page);
2296 return -EINVAL;
2298 new_state = (int)tmp;
2300 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2301 pr_err("Unable to process implict configfs ALUA"
2302 " transition while TPGS_IMPLICT_ALUA is diabled\n");
2303 return -EINVAL;
2306 ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2307 NULL, NULL, new_state, 0);
2308 return (!ret) ? count : -EINVAL;
2311 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2314 * alua_access_status
2316 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2317 struct t10_alua_tg_pt_gp *tg_pt_gp,
2318 char *page)
2320 return sprintf(page, "%s\n",
2321 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2324 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2325 struct t10_alua_tg_pt_gp *tg_pt_gp,
2326 const char *page,
2327 size_t count)
2329 unsigned long tmp;
2330 int new_status, ret;
2332 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2333 pr_err("Unable to do set ALUA access status on non"
2334 " valid tg_pt_gp ID: %hu\n",
2335 tg_pt_gp->tg_pt_gp_valid_id);
2336 return -EINVAL;
2339 ret = strict_strtoul(page, 0, &tmp);
2340 if (ret < 0) {
2341 pr_err("Unable to extract new ALUA access status"
2342 " from %s\n", page);
2343 return -EINVAL;
2345 new_status = (int)tmp;
2347 if ((new_status != ALUA_STATUS_NONE) &&
2348 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2349 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2350 pr_err("Illegal ALUA access status: 0x%02x\n",
2351 new_status);
2352 return -EINVAL;
2355 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2356 return count;
2359 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2362 * alua_access_type
2364 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2365 struct t10_alua_tg_pt_gp *tg_pt_gp,
2366 char *page)
2368 return core_alua_show_access_type(tg_pt_gp, page);
2371 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2372 struct t10_alua_tg_pt_gp *tg_pt_gp,
2373 const char *page,
2374 size_t count)
2376 return core_alua_store_access_type(tg_pt_gp, page, count);
2379 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2382 * alua_write_metadata
2384 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2385 struct t10_alua_tg_pt_gp *tg_pt_gp,
2386 char *page)
2388 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2391 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2392 struct t10_alua_tg_pt_gp *tg_pt_gp,
2393 const char *page,
2394 size_t count)
2396 unsigned long tmp;
2397 int ret;
2399 ret = strict_strtoul(page, 0, &tmp);
2400 if (ret < 0) {
2401 pr_err("Unable to extract alua_write_metadata\n");
2402 return -EINVAL;
2405 if ((tmp != 0) && (tmp != 1)) {
2406 pr_err("Illegal value for alua_write_metadata:"
2407 " %lu\n", tmp);
2408 return -EINVAL;
2410 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2412 return count;
2415 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2420 * nonop_delay_msecs
2422 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2423 struct t10_alua_tg_pt_gp *tg_pt_gp,
2424 char *page)
2426 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2430 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2431 struct t10_alua_tg_pt_gp *tg_pt_gp,
2432 const char *page,
2433 size_t count)
2435 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2438 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2441 * trans_delay_msecs
2443 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2444 struct t10_alua_tg_pt_gp *tg_pt_gp,
2445 char *page)
2447 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2450 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2451 struct t10_alua_tg_pt_gp *tg_pt_gp,
2452 const char *page,
2453 size_t count)
2455 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2458 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2461 * preferred
2464 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2465 struct t10_alua_tg_pt_gp *tg_pt_gp,
2466 char *page)
2468 return core_alua_show_preferred_bit(tg_pt_gp, page);
2471 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2472 struct t10_alua_tg_pt_gp *tg_pt_gp,
2473 const char *page,
2474 size_t count)
2476 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2479 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2482 * tg_pt_gp_id
2484 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2485 struct t10_alua_tg_pt_gp *tg_pt_gp,
2486 char *page)
2488 if (!tg_pt_gp->tg_pt_gp_valid_id)
2489 return 0;
2491 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2494 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2495 struct t10_alua_tg_pt_gp *tg_pt_gp,
2496 const char *page,
2497 size_t count)
2499 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2500 unsigned long tg_pt_gp_id;
2501 int ret;
2503 ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2504 if (ret < 0) {
2505 pr_err("strict_strtoul() returned %d for"
2506 " tg_pt_gp_id\n", ret);
2507 return -EINVAL;
2509 if (tg_pt_gp_id > 0x0000ffff) {
2510 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2511 " 0x0000ffff\n", tg_pt_gp_id);
2512 return -EINVAL;
2515 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2516 if (ret < 0)
2517 return -EINVAL;
2519 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2520 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2521 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2522 tg_pt_gp->tg_pt_gp_id);
2524 return count;
2527 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2530 * members
2532 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2533 struct t10_alua_tg_pt_gp *tg_pt_gp,
2534 char *page)
2536 struct se_port *port;
2537 struct se_portal_group *tpg;
2538 struct se_lun *lun;
2539 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2540 ssize_t len = 0, cur_len;
2541 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2543 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2545 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2546 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2547 tg_pt_gp_mem_list) {
2548 port = tg_pt_gp_mem->tg_pt;
2549 tpg = port->sep_tpg;
2550 lun = port->sep_lun;
2552 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2553 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2554 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2555 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2556 config_item_name(&lun->lun_group.cg_item));
2557 cur_len++; /* Extra byte for NULL terminator */
2559 if ((cur_len + len) > PAGE_SIZE) {
2560 pr_warn("Ran out of lu_gp_show_attr"
2561 "_members buffer\n");
2562 break;
2564 memcpy(page+len, buf, cur_len);
2565 len += cur_len;
2567 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2569 return len;
2572 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2574 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2575 tg_pt_gp_group);
2577 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2578 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2579 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2580 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2581 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2582 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2583 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2584 &target_core_alua_tg_pt_gp_preferred.attr,
2585 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2586 &target_core_alua_tg_pt_gp_members.attr,
2587 NULL,
2590 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2592 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2593 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2595 core_alua_free_tg_pt_gp(tg_pt_gp);
2598 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2599 .release = target_core_alua_tg_pt_gp_release,
2600 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2601 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2604 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2605 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2606 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2607 .ct_owner = THIS_MODULE,
2610 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2612 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2614 static struct config_group *target_core_alua_create_tg_pt_gp(
2615 struct config_group *group,
2616 const char *name)
2618 struct t10_alua *alua = container_of(group, struct t10_alua,
2619 alua_tg_pt_gps_group);
2620 struct t10_alua_tg_pt_gp *tg_pt_gp;
2621 struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2622 struct config_group *alua_tg_pt_gp_cg = NULL;
2623 struct config_item *alua_tg_pt_gp_ci = NULL;
2625 tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2626 if (!tg_pt_gp)
2627 return NULL;
2629 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2630 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2632 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2633 &target_core_alua_tg_pt_gp_cit);
2635 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2636 " Group: alua/tg_pt_gps/%s\n",
2637 config_item_name(alua_tg_pt_gp_ci));
2639 return alua_tg_pt_gp_cg;
2642 static void target_core_alua_drop_tg_pt_gp(
2643 struct config_group *group,
2644 struct config_item *item)
2646 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2647 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2649 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2650 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2651 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2653 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2654 * -> target_core_alua_tg_pt_gp_release().
2656 config_item_put(item);
2659 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2660 .make_group = &target_core_alua_create_tg_pt_gp,
2661 .drop_item = &target_core_alua_drop_tg_pt_gp,
2664 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2665 .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
2666 .ct_owner = THIS_MODULE,
2669 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2671 /* Start functions for struct config_item_type target_core_alua_cit */
2674 * target_core_alua_cit is a ConfigFS group that lives under
2675 * /sys/kernel/config/target/core/alua. There are default groups
2676 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2677 * target_core_alua_cit in target_core_init_configfs() below.
2679 static struct config_item_type target_core_alua_cit = {
2680 .ct_item_ops = NULL,
2681 .ct_attrs = NULL,
2682 .ct_owner = THIS_MODULE,
2685 /* End functions for struct config_item_type target_core_alua_cit */
2687 /* Start functions for struct config_item_type target_core_stat_cit */
2689 static struct config_group *target_core_stat_mkdir(
2690 struct config_group *group,
2691 const char *name)
2693 return ERR_PTR(-ENOSYS);
2696 static void target_core_stat_rmdir(
2697 struct config_group *group,
2698 struct config_item *item)
2700 return;
2703 static struct configfs_group_operations target_core_stat_group_ops = {
2704 .make_group = &target_core_stat_mkdir,
2705 .drop_item = &target_core_stat_rmdir,
2708 static struct config_item_type target_core_stat_cit = {
2709 .ct_group_ops = &target_core_stat_group_ops,
2710 .ct_owner = THIS_MODULE,
2713 /* End functions for struct config_item_type target_core_stat_cit */
2715 /* Start functions for struct config_item_type target_core_hba_cit */
2717 static struct config_group *target_core_make_subdev(
2718 struct config_group *group,
2719 const char *name)
2721 struct t10_alua_tg_pt_gp *tg_pt_gp;
2722 struct se_subsystem_dev *se_dev;
2723 struct se_subsystem_api *t;
2724 struct config_item *hba_ci = &group->cg_item;
2725 struct se_hba *hba = item_to_hba(hba_ci);
2726 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2727 struct config_group *dev_stat_grp = NULL;
2728 int errno = -ENOMEM, ret;
2730 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2731 if (ret)
2732 return ERR_PTR(ret);
2734 * Locate the struct se_subsystem_api from parent's struct se_hba.
2736 t = hba->transport;
2738 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2739 if (!se_dev) {
2740 pr_err("Unable to allocate memory for"
2741 " struct se_subsystem_dev\n");
2742 goto unlock;
2744 INIT_LIST_HEAD(&se_dev->se_dev_node);
2745 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2746 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2747 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
2748 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
2749 spin_lock_init(&se_dev->t10_pr.registration_lock);
2750 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
2751 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2752 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2753 spin_lock_init(&se_dev->se_dev_lock);
2754 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2755 se_dev->t10_wwn.t10_sub_dev = se_dev;
2756 se_dev->t10_alua.t10_sub_dev = se_dev;
2757 se_dev->se_dev_attrib.da_sub_dev = se_dev;
2759 se_dev->se_dev_hba = hba;
2760 dev_cg = &se_dev->se_dev_group;
2762 dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2763 GFP_KERNEL);
2764 if (!dev_cg->default_groups)
2765 goto out;
2767 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2768 * for ->allocate_virtdevice()
2770 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2771 * has been called successfully in the next level up in the
2772 * configfs tree for device object's struct config_group.
2774 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2775 if (!se_dev->se_dev_su_ptr) {
2776 pr_err("Unable to locate subsystem dependent pointer"
2777 " from allocate_virtdevice()\n");
2778 goto out;
2780 spin_lock(&se_device_lock);
2781 list_add_tail(&se_dev->se_dev_node, &se_dev_list);
2782 spin_unlock(&se_device_lock);
2784 config_group_init_type_name(&se_dev->se_dev_group, name,
2785 &target_core_dev_cit);
2786 config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2787 &target_core_dev_attrib_cit);
2788 config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2789 &target_core_dev_pr_cit);
2790 config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2791 &target_core_dev_wwn_cit);
2792 config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2793 "alua", &target_core_alua_tg_pt_gps_cit);
2794 config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2795 "statistics", &target_core_stat_cit);
2797 dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2798 dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2799 dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2800 dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2801 dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2802 dev_cg->default_groups[5] = NULL;
2804 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2806 tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2807 if (!tg_pt_gp)
2808 goto out;
2810 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2811 tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2812 GFP_KERNEL);
2813 if (!tg_pt_gp_cg->default_groups) {
2814 pr_err("Unable to allocate tg_pt_gp_cg->"
2815 "default_groups\n");
2816 goto out;
2819 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2820 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2821 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2822 tg_pt_gp_cg->default_groups[1] = NULL;
2823 se_dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2825 * Add core/$HBA/$DEV/statistics/ default groups
2827 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2828 dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2829 GFP_KERNEL);
2830 if (!dev_stat_grp->default_groups) {
2831 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2832 goto out;
2834 target_stat_setup_dev_default_groups(se_dev);
2836 pr_debug("Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2837 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2839 mutex_unlock(&hba->hba_access_mutex);
2840 return &se_dev->se_dev_group;
2841 out:
2842 if (se_dev->t10_alua.default_tg_pt_gp) {
2843 core_alua_free_tg_pt_gp(se_dev->t10_alua.default_tg_pt_gp);
2844 se_dev->t10_alua.default_tg_pt_gp = NULL;
2846 if (dev_stat_grp)
2847 kfree(dev_stat_grp->default_groups);
2848 if (tg_pt_gp_cg)
2849 kfree(tg_pt_gp_cg->default_groups);
2850 if (dev_cg)
2851 kfree(dev_cg->default_groups);
2852 if (se_dev->se_dev_su_ptr)
2853 t->free_device(se_dev->se_dev_su_ptr);
2854 kfree(se_dev);
2855 unlock:
2856 mutex_unlock(&hba->hba_access_mutex);
2857 return ERR_PTR(errno);
2860 static void target_core_drop_subdev(
2861 struct config_group *group,
2862 struct config_item *item)
2864 struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2865 struct se_subsystem_dev, se_dev_group);
2866 struct se_hba *hba;
2867 struct se_subsystem_api *t;
2868 struct config_item *df_item;
2869 struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2870 int i;
2872 hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2874 mutex_lock(&hba->hba_access_mutex);
2875 t = hba->transport;
2877 spin_lock(&se_device_lock);
2878 list_del(&se_dev->se_dev_node);
2879 spin_unlock(&se_device_lock);
2881 dev_stat_grp = &se_dev->dev_stat_grps.stat_group;
2882 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2883 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2884 dev_stat_grp->default_groups[i] = NULL;
2885 config_item_put(df_item);
2887 kfree(dev_stat_grp->default_groups);
2889 tg_pt_gp_cg = &se_dev->t10_alua.alua_tg_pt_gps_group;
2890 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2891 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2892 tg_pt_gp_cg->default_groups[i] = NULL;
2893 config_item_put(df_item);
2895 kfree(tg_pt_gp_cg->default_groups);
2897 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2898 * directly from target_core_alua_tg_pt_gp_release().
2900 se_dev->t10_alua.default_tg_pt_gp = NULL;
2902 dev_cg = &se_dev->se_dev_group;
2903 for (i = 0; dev_cg->default_groups[i]; i++) {
2904 df_item = &dev_cg->default_groups[i]->cg_item;
2905 dev_cg->default_groups[i] = NULL;
2906 config_item_put(df_item);
2909 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2910 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2912 config_item_put(item);
2913 mutex_unlock(&hba->hba_access_mutex);
2916 static struct configfs_group_operations target_core_hba_group_ops = {
2917 .make_group = target_core_make_subdev,
2918 .drop_item = target_core_drop_subdev,
2921 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2922 #define SE_HBA_ATTR(_name, _mode) \
2923 static struct target_core_hba_attribute \
2924 target_core_hba_##_name = \
2925 __CONFIGFS_EATTR(_name, _mode, \
2926 target_core_hba_show_attr_##_name, \
2927 target_core_hba_store_attr_##_name);
2929 #define SE_HBA_ATTR_RO(_name) \
2930 static struct target_core_hba_attribute \
2931 target_core_hba_##_name = \
2932 __CONFIGFS_EATTR_RO(_name, \
2933 target_core_hba_show_attr_##_name);
2935 static ssize_t target_core_hba_show_attr_hba_info(
2936 struct se_hba *hba,
2937 char *page)
2939 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2940 hba->hba_id, hba->transport->name,
2941 TARGET_CORE_CONFIGFS_VERSION);
2944 SE_HBA_ATTR_RO(hba_info);
2946 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2947 char *page)
2949 int hba_mode = 0;
2951 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2952 hba_mode = 1;
2954 return sprintf(page, "%d\n", hba_mode);
2957 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2958 const char *page, size_t count)
2960 struct se_subsystem_api *transport = hba->transport;
2961 unsigned long mode_flag;
2962 int ret;
2964 if (transport->pmode_enable_hba == NULL)
2965 return -EINVAL;
2967 ret = strict_strtoul(page, 0, &mode_flag);
2968 if (ret < 0) {
2969 pr_err("Unable to extract hba mode flag: %d\n", ret);
2970 return -EINVAL;
2973 spin_lock(&hba->device_lock);
2974 if (!list_empty(&hba->hba_dev_list)) {
2975 pr_err("Unable to set hba_mode with active devices\n");
2976 spin_unlock(&hba->device_lock);
2977 return -EINVAL;
2979 spin_unlock(&hba->device_lock);
2981 ret = transport->pmode_enable_hba(hba, mode_flag);
2982 if (ret < 0)
2983 return -EINVAL;
2984 if (ret > 0)
2985 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2986 else if (ret == 0)
2987 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2989 return count;
2992 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2994 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2996 static void target_core_hba_release(struct config_item *item)
2998 struct se_hba *hba = container_of(to_config_group(item),
2999 struct se_hba, hba_group);
3000 core_delete_hba(hba);
3003 static struct configfs_attribute *target_core_hba_attrs[] = {
3004 &target_core_hba_hba_info.attr,
3005 &target_core_hba_hba_mode.attr,
3006 NULL,
3009 static struct configfs_item_operations target_core_hba_item_ops = {
3010 .release = target_core_hba_release,
3011 .show_attribute = target_core_hba_attr_show,
3012 .store_attribute = target_core_hba_attr_store,
3015 static struct config_item_type target_core_hba_cit = {
3016 .ct_item_ops = &target_core_hba_item_ops,
3017 .ct_group_ops = &target_core_hba_group_ops,
3018 .ct_attrs = target_core_hba_attrs,
3019 .ct_owner = THIS_MODULE,
3022 static struct config_group *target_core_call_addhbatotarget(
3023 struct config_group *group,
3024 const char *name)
3026 char *se_plugin_str, *str, *str2;
3027 struct se_hba *hba;
3028 char buf[TARGET_CORE_NAME_MAX_LEN];
3029 unsigned long plugin_dep_id = 0;
3030 int ret;
3032 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3033 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3034 pr_err("Passed *name strlen(): %d exceeds"
3035 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3036 TARGET_CORE_NAME_MAX_LEN);
3037 return ERR_PTR(-ENAMETOOLONG);
3039 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3041 str = strstr(buf, "_");
3042 if (!str) {
3043 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3044 return ERR_PTR(-EINVAL);
3046 se_plugin_str = buf;
3048 * Special case for subsystem plugins that have "_" in their names.
3049 * Namely rd_direct and rd_mcp..
3051 str2 = strstr(str+1, "_");
3052 if (str2) {
3053 *str2 = '\0'; /* Terminate for *se_plugin_str */
3054 str2++; /* Skip to start of plugin dependent ID */
3055 str = str2;
3056 } else {
3057 *str = '\0'; /* Terminate for *se_plugin_str */
3058 str++; /* Skip to start of plugin dependent ID */
3061 ret = strict_strtoul(str, 0, &plugin_dep_id);
3062 if (ret < 0) {
3063 pr_err("strict_strtoul() returned %d for"
3064 " plugin_dep_id\n", ret);
3065 return ERR_PTR(-EINVAL);
3068 * Load up TCM subsystem plugins if they have not already been loaded.
3070 transport_subsystem_check_init();
3072 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3073 if (IS_ERR(hba))
3074 return ERR_CAST(hba);
3076 config_group_init_type_name(&hba->hba_group, name,
3077 &target_core_hba_cit);
3079 return &hba->hba_group;
3082 static void target_core_call_delhbafromtarget(
3083 struct config_group *group,
3084 struct config_item *item)
3087 * core_delete_hba() is called from target_core_hba_item_ops->release()
3088 * -> target_core_hba_release()
3090 config_item_put(item);
3093 static struct configfs_group_operations target_core_group_ops = {
3094 .make_group = target_core_call_addhbatotarget,
3095 .drop_item = target_core_call_delhbafromtarget,
3098 static struct config_item_type target_core_cit = {
3099 .ct_item_ops = NULL,
3100 .ct_group_ops = &target_core_group_ops,
3101 .ct_attrs = NULL,
3102 .ct_owner = THIS_MODULE,
3105 /* Stop functions for struct config_item_type target_core_hba_cit */
3107 static int __init target_core_init_configfs(void)
3109 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3110 struct config_group *lu_gp_cg = NULL;
3111 struct configfs_subsystem *subsys;
3112 struct t10_alua_lu_gp *lu_gp;
3113 int ret;
3115 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3116 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3117 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3119 subsys = target_core_subsystem[0];
3120 config_group_init(&subsys->su_group);
3121 mutex_init(&subsys->su_mutex);
3123 INIT_LIST_HEAD(&g_tf_list);
3124 mutex_init(&g_tf_lock);
3125 ret = init_se_kmem_caches();
3126 if (ret < 0)
3127 return ret;
3129 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3130 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3132 target_cg = &subsys->su_group;
3133 target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3134 GFP_KERNEL);
3135 if (!target_cg->default_groups) {
3136 pr_err("Unable to allocate target_cg->default_groups\n");
3137 goto out_global;
3140 config_group_init_type_name(&target_core_hbagroup,
3141 "core", &target_core_cit);
3142 target_cg->default_groups[0] = &target_core_hbagroup;
3143 target_cg->default_groups[1] = NULL;
3145 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3147 hba_cg = &target_core_hbagroup;
3148 hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3149 GFP_KERNEL);
3150 if (!hba_cg->default_groups) {
3151 pr_err("Unable to allocate hba_cg->default_groups\n");
3152 goto out_global;
3154 config_group_init_type_name(&alua_group,
3155 "alua", &target_core_alua_cit);
3156 hba_cg->default_groups[0] = &alua_group;
3157 hba_cg->default_groups[1] = NULL;
3159 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3160 * groups under /sys/kernel/config/target/core/alua/
3162 alua_cg = &alua_group;
3163 alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3164 GFP_KERNEL);
3165 if (!alua_cg->default_groups) {
3166 pr_err("Unable to allocate alua_cg->default_groups\n");
3167 goto out_global;
3170 config_group_init_type_name(&alua_lu_gps_group,
3171 "lu_gps", &target_core_alua_lu_gps_cit);
3172 alua_cg->default_groups[0] = &alua_lu_gps_group;
3173 alua_cg->default_groups[1] = NULL;
3175 * Add core/alua/lu_gps/default_lu_gp
3177 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3178 if (IS_ERR(lu_gp))
3179 goto out_global;
3181 lu_gp_cg = &alua_lu_gps_group;
3182 lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3183 GFP_KERNEL);
3184 if (!lu_gp_cg->default_groups) {
3185 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
3186 goto out_global;
3189 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3190 &target_core_alua_lu_gp_cit);
3191 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3192 lu_gp_cg->default_groups[1] = NULL;
3193 default_lu_gp = lu_gp;
3195 * Register the target_core_mod subsystem with configfs.
3197 ret = configfs_register_subsystem(subsys);
3198 if (ret < 0) {
3199 pr_err("Error %d while registering subsystem %s\n",
3200 ret, subsys->su_group.cg_item.ci_namebuf);
3201 goto out_global;
3203 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3204 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3205 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3207 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3209 ret = rd_module_init();
3210 if (ret < 0)
3211 goto out;
3213 if (core_dev_setup_virtual_lun0() < 0)
3214 goto out;
3216 return 0;
3218 out:
3219 configfs_unregister_subsystem(subsys);
3220 core_dev_release_virtual_lun0();
3221 rd_module_exit();
3222 out_global:
3223 if (default_lu_gp) {
3224 core_alua_free_lu_gp(default_lu_gp);
3225 default_lu_gp = NULL;
3227 if (lu_gp_cg)
3228 kfree(lu_gp_cg->default_groups);
3229 if (alua_cg)
3230 kfree(alua_cg->default_groups);
3231 if (hba_cg)
3232 kfree(hba_cg->default_groups);
3233 kfree(target_cg->default_groups);
3234 release_se_kmem_caches();
3235 return ret;
3238 static void __exit target_core_exit_configfs(void)
3240 struct configfs_subsystem *subsys;
3241 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3242 struct config_item *item;
3243 int i;
3245 subsys = target_core_subsystem[0];
3247 lu_gp_cg = &alua_lu_gps_group;
3248 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3249 item = &lu_gp_cg->default_groups[i]->cg_item;
3250 lu_gp_cg->default_groups[i] = NULL;
3251 config_item_put(item);
3253 kfree(lu_gp_cg->default_groups);
3254 lu_gp_cg->default_groups = NULL;
3256 alua_cg = &alua_group;
3257 for (i = 0; alua_cg->default_groups[i]; i++) {
3258 item = &alua_cg->default_groups[i]->cg_item;
3259 alua_cg->default_groups[i] = NULL;
3260 config_item_put(item);
3262 kfree(alua_cg->default_groups);
3263 alua_cg->default_groups = NULL;
3265 hba_cg = &target_core_hbagroup;
3266 for (i = 0; hba_cg->default_groups[i]; i++) {
3267 item = &hba_cg->default_groups[i]->cg_item;
3268 hba_cg->default_groups[i] = NULL;
3269 config_item_put(item);
3271 kfree(hba_cg->default_groups);
3272 hba_cg->default_groups = NULL;
3274 * We expect subsys->su_group.default_groups to be released
3275 * by configfs subsystem provider logic..
3277 configfs_unregister_subsystem(subsys);
3278 kfree(subsys->su_group.default_groups);
3280 core_alua_free_lu_gp(default_lu_gp);
3281 default_lu_gp = NULL;
3283 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3284 " Infrastructure\n");
3286 core_dev_release_virtual_lun0();
3287 rd_module_exit();
3288 release_se_kmem_caches();
3291 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3292 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3293 MODULE_LICENSE("GPL");
3295 module_init(target_core_init_configfs);
3296 module_exit(target_core_exit_configfs);