1 /*******************************************************************************
2 * Filename: target_core_configfs.c
4 * This file contains ConfigFS logic for the Generic Target Engine project.
6 * (c) Copyright 2008-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 ****************************************************************************/
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <generated/utsrelease.h>
26 #include <linux/utsname.h>
27 #include <linux/init.h>
29 #include <linux/namei.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <linux/unistd.h>
34 #include <linux/string.h>
35 #include <linux/parser.h>
36 #include <linux/syscalls.h>
37 #include <linux/configfs.h>
38 #include <linux/spinlock.h>
40 #include <target/target_core_base.h>
41 #include <target/target_core_backend.h>
42 #include <target/target_core_fabric.h>
44 #include "target_core_internal.h"
45 #include "target_core_alua.h"
46 #include "target_core_pr.h"
47 #include "target_core_rd.h"
48 #include "target_core_xcopy.h"
50 #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
51 static void target_core_setup_##_name##_cit(struct target_backend *tb) \
53 struct config_item_type *cit = &tb->tb_##_name##_cit; \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = _attrs; \
58 cit->ct_owner = tb->ops->owner; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
62 #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
63 static void target_core_setup_##_name##_cit(struct target_backend *tb) \
65 struct config_item_type *cit = &tb->tb_##_name##_cit; \
67 cit->ct_item_ops = _item_ops; \
68 cit->ct_group_ops = _group_ops; \
69 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
70 cit->ct_owner = tb->ops->owner; \
71 pr_debug("Setup generic %s\n", __stringify(_name)); \
74 extern struct t10_alua_lu_gp
*default_lu_gp
;
76 static LIST_HEAD(g_tf_list
);
77 static DEFINE_MUTEX(g_tf_lock
);
79 static struct config_group target_core_hbagroup
;
80 static struct config_group alua_group
;
81 static struct config_group alua_lu_gps_group
;
83 static inline struct se_hba
*
84 item_to_hba(struct config_item
*item
)
86 return container_of(to_config_group(item
), struct se_hba
, hba_group
);
90 * Attributes for /sys/kernel/config/target/
92 static ssize_t
target_core_item_version_show(struct config_item
*item
,
95 return sprintf(page
, "Target Engine Core ConfigFS Infrastructure %s"
96 " on %s/%s on "UTS_RELEASE
"\n", TARGET_CORE_VERSION
,
97 utsname()->sysname
, utsname()->machine
);
100 CONFIGFS_ATTR_RO(target_core_item_
, version
);
102 static struct target_fabric_configfs
*target_core_get_fabric(
105 struct target_fabric_configfs
*tf
;
110 mutex_lock(&g_tf_lock
);
111 list_for_each_entry(tf
, &g_tf_list
, tf_list
) {
112 if (!strcmp(tf
->tf_ops
->name
, name
)) {
113 atomic_inc(&tf
->tf_access_cnt
);
114 mutex_unlock(&g_tf_lock
);
118 mutex_unlock(&g_tf_lock
);
124 * Called from struct target_core_group_ops->make_group()
126 static struct config_group
*target_core_register_fabric(
127 struct config_group
*group
,
130 struct target_fabric_configfs
*tf
;
133 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
134 " %s\n", group
, name
);
136 tf
= target_core_get_fabric(name
);
138 pr_debug("target_core_register_fabric() trying autoload for %s\n",
142 * Below are some hardcoded request_module() calls to automatically
143 * local fabric modules when the following is called:
145 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
147 * Note that this does not limit which TCM fabric module can be
148 * registered, but simply provids auto loading logic for modules with
149 * mkdir(2) system calls with known TCM fabric modules.
152 if (!strncmp(name
, "iscsi", 5)) {
154 * Automatically load the LIO Target fabric module when the
155 * following is called:
157 * mkdir -p $CONFIGFS/target/iscsi
159 ret
= request_module("iscsi_target_mod");
161 pr_debug("request_module() failed for"
162 " iscsi_target_mod.ko: %d\n", ret
);
163 return ERR_PTR(-EINVAL
);
165 } else if (!strncmp(name
, "loopback", 8)) {
167 * Automatically load the tcm_loop fabric module when the
168 * following is called:
170 * mkdir -p $CONFIGFS/target/loopback
172 ret
= request_module("tcm_loop");
174 pr_debug("request_module() failed for"
175 " tcm_loop.ko: %d\n", ret
);
176 return ERR_PTR(-EINVAL
);
180 tf
= target_core_get_fabric(name
);
184 pr_debug("target_core_get_fabric() failed for %s\n",
186 return ERR_PTR(-EINVAL
);
188 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
189 " %s\n", tf
->tf_ops
->name
);
191 * On a successful target_core_get_fabric() look, the returned
192 * struct target_fabric_configfs *tf will contain a usage reference.
194 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
197 tf
->tf_group
.default_groups
= tf
->tf_default_groups
;
198 tf
->tf_group
.default_groups
[0] = &tf
->tf_disc_group
;
199 tf
->tf_group
.default_groups
[1] = NULL
;
201 config_group_init_type_name(&tf
->tf_group
, name
, &tf
->tf_wwn_cit
);
202 config_group_init_type_name(&tf
->tf_disc_group
, "discovery_auth",
203 &tf
->tf_discovery_cit
);
205 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
206 " %s\n", tf
->tf_group
.cg_item
.ci_name
);
207 return &tf
->tf_group
;
211 * Called from struct target_core_group_ops->drop_item()
213 static void target_core_deregister_fabric(
214 struct config_group
*group
,
215 struct config_item
*item
)
217 struct target_fabric_configfs
*tf
= container_of(
218 to_config_group(item
), struct target_fabric_configfs
, tf_group
);
219 struct config_group
*tf_group
;
220 struct config_item
*df_item
;
223 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
224 " tf list\n", config_item_name(item
));
226 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
227 " %s\n", tf
->tf_ops
->name
);
228 atomic_dec(&tf
->tf_access_cnt
);
230 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
231 " %s\n", config_item_name(item
));
233 tf_group
= &tf
->tf_group
;
234 for (i
= 0; tf_group
->default_groups
[i
]; i
++) {
235 df_item
= &tf_group
->default_groups
[i
]->cg_item
;
236 tf_group
->default_groups
[i
] = NULL
;
237 config_item_put(df_item
);
239 config_item_put(item
);
242 static struct configfs_group_operations target_core_fabric_group_ops
= {
243 .make_group
= &target_core_register_fabric
,
244 .drop_item
= &target_core_deregister_fabric
,
248 * All item attributes appearing in /sys/kernel/target/ appear here.
250 static struct configfs_attribute
*target_core_fabric_item_attrs
[] = {
251 &target_core_item_attr_version
,
256 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
258 static struct config_item_type target_core_fabrics_item
= {
259 .ct_group_ops
= &target_core_fabric_group_ops
,
260 .ct_attrs
= target_core_fabric_item_attrs
,
261 .ct_owner
= THIS_MODULE
,
264 static struct configfs_subsystem target_core_fabrics
= {
267 .ci_namebuf
= "target",
268 .ci_type
= &target_core_fabrics_item
,
273 int target_depend_item(struct config_item
*item
)
275 return configfs_depend_item(&target_core_fabrics
, item
);
277 EXPORT_SYMBOL(target_depend_item
);
279 void target_undepend_item(struct config_item
*item
)
281 return configfs_undepend_item(&target_core_fabrics
, item
);
283 EXPORT_SYMBOL(target_undepend_item
);
285 /*##############################################################################
286 // Start functions called by external Target Fabrics Modules
287 //############################################################################*/
289 static int target_fabric_tf_ops_check(const struct target_core_fabric_ops
*tfo
)
292 pr_err("Missing tfo->name\n");
295 if (strlen(tfo
->name
) >= TARGET_FABRIC_NAME_SIZE
) {
296 pr_err("Passed name: %s exceeds TARGET_FABRIC"
297 "_NAME_SIZE\n", tfo
->name
);
300 if (!tfo
->get_fabric_name
) {
301 pr_err("Missing tfo->get_fabric_name()\n");
304 if (!tfo
->tpg_get_wwn
) {
305 pr_err("Missing tfo->tpg_get_wwn()\n");
308 if (!tfo
->tpg_get_tag
) {
309 pr_err("Missing tfo->tpg_get_tag()\n");
312 if (!tfo
->tpg_check_demo_mode
) {
313 pr_err("Missing tfo->tpg_check_demo_mode()\n");
316 if (!tfo
->tpg_check_demo_mode_cache
) {
317 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
320 if (!tfo
->tpg_check_demo_mode_write_protect
) {
321 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
324 if (!tfo
->tpg_check_prod_mode_write_protect
) {
325 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
328 if (!tfo
->tpg_get_inst_index
) {
329 pr_err("Missing tfo->tpg_get_inst_index()\n");
332 if (!tfo
->release_cmd
) {
333 pr_err("Missing tfo->release_cmd()\n");
336 if (!tfo
->shutdown_session
) {
337 pr_err("Missing tfo->shutdown_session()\n");
340 if (!tfo
->close_session
) {
341 pr_err("Missing tfo->close_session()\n");
344 if (!tfo
->sess_get_index
) {
345 pr_err("Missing tfo->sess_get_index()\n");
348 if (!tfo
->write_pending
) {
349 pr_err("Missing tfo->write_pending()\n");
352 if (!tfo
->write_pending_status
) {
353 pr_err("Missing tfo->write_pending_status()\n");
356 if (!tfo
->set_default_node_attributes
) {
357 pr_err("Missing tfo->set_default_node_attributes()\n");
360 if (!tfo
->get_cmd_state
) {
361 pr_err("Missing tfo->get_cmd_state()\n");
364 if (!tfo
->queue_data_in
) {
365 pr_err("Missing tfo->queue_data_in()\n");
368 if (!tfo
->queue_status
) {
369 pr_err("Missing tfo->queue_status()\n");
372 if (!tfo
->queue_tm_rsp
) {
373 pr_err("Missing tfo->queue_tm_rsp()\n");
376 if (!tfo
->aborted_task
) {
377 pr_err("Missing tfo->aborted_task()\n");
381 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
382 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
383 * target_core_fabric_configfs.c WWN+TPG group context code.
385 if (!tfo
->fabric_make_wwn
) {
386 pr_err("Missing tfo->fabric_make_wwn()\n");
389 if (!tfo
->fabric_drop_wwn
) {
390 pr_err("Missing tfo->fabric_drop_wwn()\n");
393 if (!tfo
->fabric_make_tpg
) {
394 pr_err("Missing tfo->fabric_make_tpg()\n");
397 if (!tfo
->fabric_drop_tpg
) {
398 pr_err("Missing tfo->fabric_drop_tpg()\n");
405 int target_register_template(const struct target_core_fabric_ops
*fo
)
407 struct target_fabric_configfs
*tf
;
410 ret
= target_fabric_tf_ops_check(fo
);
414 tf
= kzalloc(sizeof(struct target_fabric_configfs
), GFP_KERNEL
);
416 pr_err("%s: could not allocate memory!\n", __func__
);
420 INIT_LIST_HEAD(&tf
->tf_list
);
421 atomic_set(&tf
->tf_access_cnt
, 0);
423 target_fabric_setup_cits(tf
);
425 mutex_lock(&g_tf_lock
);
426 list_add_tail(&tf
->tf_list
, &g_tf_list
);
427 mutex_unlock(&g_tf_lock
);
431 EXPORT_SYMBOL(target_register_template
);
433 void target_unregister_template(const struct target_core_fabric_ops
*fo
)
435 struct target_fabric_configfs
*t
;
437 mutex_lock(&g_tf_lock
);
438 list_for_each_entry(t
, &g_tf_list
, tf_list
) {
439 if (!strcmp(t
->tf_ops
->name
, fo
->name
)) {
440 BUG_ON(atomic_read(&t
->tf_access_cnt
));
441 list_del(&t
->tf_list
);
442 mutex_unlock(&g_tf_lock
);
444 * Wait for any outstanding fabric se_deve_entry->rcu_head
445 * callbacks to complete post kfree_rcu(), before allowing
446 * fabric driver unload of TFO->module to proceed.
453 mutex_unlock(&g_tf_lock
);
455 EXPORT_SYMBOL(target_unregister_template
);
457 /*##############################################################################
458 // Stop functions called by external Target Fabrics Modules
459 //############################################################################*/
461 static inline struct se_dev_attrib
*to_attrib(struct config_item
*item
)
463 return container_of(to_config_group(item
), struct se_dev_attrib
,
467 /* Start functions for struct config_item_type tb_dev_attrib_cit */
468 #define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
469 static ssize_t _name##_show(struct config_item *item, char *page) \
471 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
474 DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias
);
475 DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo
);
476 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write
);
477 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read
);
478 DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache
);
479 DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl
);
480 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas
);
481 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu
);
482 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws
);
483 DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw
);
484 DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc
);
485 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type
);
486 DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type
);
487 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format
);
488 DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids
);
489 DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot
);
490 DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord
);
491 DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl
);
492 DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size
);
493 DEF_CONFIGFS_ATTRIB_SHOW(block_size
);
494 DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors
);
495 DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors
);
496 DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth
);
497 DEF_CONFIGFS_ATTRIB_SHOW(queue_depth
);
498 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count
);
499 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count
);
500 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity
);
501 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment
);
502 DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len
);
504 #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
505 static ssize_t _name##_store(struct config_item *item, const char *page,\
508 struct se_dev_attrib *da = to_attrib(item); \
512 ret = kstrtou32(page, 0, &val); \
519 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count
);
520 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count
);
521 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity
);
522 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment
);
523 DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len
);
525 #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
526 static ssize_t _name##_store(struct config_item *item, const char *page, \
529 struct se_dev_attrib *da = to_attrib(item); \
533 ret = strtobool(page, &flag); \
540 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write
);
541 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw
);
542 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc
);
543 DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids
);
544 DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot
);
546 #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
547 static ssize_t _name##_store(struct config_item *item, const char *page,\
550 printk_once(KERN_WARNING \
551 "ignoring deprecated ##_name## attribute\n"); \
555 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo
);
556 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read
);
558 static void dev_set_t10_wwn_model_alias(struct se_device
*dev
)
560 const char *configname
;
562 configname
= config_item_name(&dev
->dev_group
.cg_item
);
563 if (strlen(configname
) >= 16) {
564 pr_warn("dev[%p]: Backstore name '%s' is too long for "
565 "INQUIRY_MODEL, truncating to 16 bytes\n", dev
,
568 snprintf(&dev
->t10_wwn
.model
[0], 16, "%s", configname
);
571 static ssize_t
emulate_model_alias_store(struct config_item
*item
,
572 const char *page
, size_t count
)
574 struct se_dev_attrib
*da
= to_attrib(item
);
575 struct se_device
*dev
= da
->da_dev
;
579 if (dev
->export_count
) {
580 pr_err("dev[%p]: Unable to change model alias"
581 " while export_count is %d\n",
582 dev
, dev
->export_count
);
586 ret
= strtobool(page
, &flag
);
591 dev_set_t10_wwn_model_alias(dev
);
593 strncpy(&dev
->t10_wwn
.model
[0],
594 dev
->transport
->inquiry_prod
, 16);
596 da
->emulate_model_alias
= flag
;
600 static ssize_t
emulate_write_cache_store(struct config_item
*item
,
601 const char *page
, size_t count
)
603 struct se_dev_attrib
*da
= to_attrib(item
);
607 ret
= strtobool(page
, &flag
);
611 if (flag
&& da
->da_dev
->transport
->get_write_cache
) {
612 pr_err("emulate_write_cache not supported for this device\n");
616 da
->emulate_write_cache
= flag
;
617 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
622 static ssize_t
emulate_ua_intlck_ctrl_store(struct config_item
*item
,
623 const char *page
, size_t count
)
625 struct se_dev_attrib
*da
= to_attrib(item
);
629 ret
= kstrtou32(page
, 0, &val
);
633 if (val
!= 0 && val
!= 1 && val
!= 2) {
634 pr_err("Illegal value %d\n", val
);
638 if (da
->da_dev
->export_count
) {
639 pr_err("dev[%p]: Unable to change SE Device"
640 " UA_INTRLCK_CTRL while export_count is %d\n",
641 da
->da_dev
, da
->da_dev
->export_count
);
644 da
->emulate_ua_intlck_ctrl
= val
;
645 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
650 static ssize_t
emulate_tas_store(struct config_item
*item
,
651 const char *page
, size_t count
)
653 struct se_dev_attrib
*da
= to_attrib(item
);
657 ret
= strtobool(page
, &flag
);
661 if (da
->da_dev
->export_count
) {
662 pr_err("dev[%p]: Unable to change SE Device TAS while"
663 " export_count is %d\n",
664 da
->da_dev
, da
->da_dev
->export_count
);
667 da
->emulate_tas
= flag
;
668 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
669 da
->da_dev
, flag
? "Enabled" : "Disabled");
674 static ssize_t
emulate_tpu_store(struct config_item
*item
,
675 const char *page
, size_t count
)
677 struct se_dev_attrib
*da
= to_attrib(item
);
681 ret
= strtobool(page
, &flag
);
686 * We expect this value to be non-zero when generic Block Layer
687 * Discard supported is detected iblock_create_virtdevice().
689 if (flag
&& !da
->max_unmap_block_desc_count
) {
690 pr_err("Generic Block Discard not supported\n");
694 da
->emulate_tpu
= flag
;
695 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
700 static ssize_t
emulate_tpws_store(struct config_item
*item
,
701 const char *page
, size_t count
)
703 struct se_dev_attrib
*da
= to_attrib(item
);
707 ret
= strtobool(page
, &flag
);
712 * We expect this value to be non-zero when generic Block Layer
713 * Discard supported is detected iblock_create_virtdevice().
715 if (flag
&& !da
->max_unmap_block_desc_count
) {
716 pr_err("Generic Block Discard not supported\n");
720 da
->emulate_tpws
= flag
;
721 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
726 static ssize_t
pi_prot_type_store(struct config_item
*item
,
727 const char *page
, size_t count
)
729 struct se_dev_attrib
*da
= to_attrib(item
);
730 int old_prot
= da
->pi_prot_type
, ret
;
731 struct se_device
*dev
= da
->da_dev
;
734 ret
= kstrtou32(page
, 0, &flag
);
738 if (flag
!= 0 && flag
!= 1 && flag
!= 2 && flag
!= 3) {
739 pr_err("Illegal value %d for pi_prot_type\n", flag
);
743 pr_err("DIF TYPE2 protection currently not supported\n");
746 if (da
->hw_pi_prot_type
) {
747 pr_warn("DIF protection enabled on underlying hardware,"
751 if (!dev
->transport
->init_prot
|| !dev
->transport
->free_prot
) {
752 /* 0 is only allowed value for non-supporting backends */
756 pr_err("DIF protection not supported by backend: %s\n",
757 dev
->transport
->name
);
760 if (!(dev
->dev_flags
& DF_CONFIGURED
)) {
761 pr_err("DIF protection requires device to be configured\n");
764 if (dev
->export_count
) {
765 pr_err("dev[%p]: Unable to change SE Device PROT type while"
766 " export_count is %d\n", dev
, dev
->export_count
);
770 da
->pi_prot_type
= flag
;
772 if (flag
&& !old_prot
) {
773 ret
= dev
->transport
->init_prot(dev
);
775 da
->pi_prot_type
= old_prot
;
779 } else if (!flag
&& old_prot
) {
780 dev
->transport
->free_prot(dev
);
783 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev
, flag
);
787 static ssize_t
pi_prot_format_store(struct config_item
*item
,
788 const char *page
, size_t count
)
790 struct se_dev_attrib
*da
= to_attrib(item
);
791 struct se_device
*dev
= da
->da_dev
;
795 ret
= strtobool(page
, &flag
);
802 if (!dev
->transport
->format_prot
) {
803 pr_err("DIF protection format not supported by backend %s\n",
804 dev
->transport
->name
);
807 if (!(dev
->dev_flags
& DF_CONFIGURED
)) {
808 pr_err("DIF protection format requires device to be configured\n");
811 if (dev
->export_count
) {
812 pr_err("dev[%p]: Unable to format SE Device PROT type while"
813 " export_count is %d\n", dev
, dev
->export_count
);
817 ret
= dev
->transport
->format_prot(dev
);
821 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev
);
825 static ssize_t
force_pr_aptpl_store(struct config_item
*item
,
826 const char *page
, size_t count
)
828 struct se_dev_attrib
*da
= to_attrib(item
);
832 ret
= strtobool(page
, &flag
);
835 if (da
->da_dev
->export_count
) {
836 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
837 " export_count is %d\n",
838 da
->da_dev
, da
->da_dev
->export_count
);
842 da
->force_pr_aptpl
= flag
;
843 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da
->da_dev
, flag
);
847 static ssize_t
emulate_rest_reord_store(struct config_item
*item
,
848 const char *page
, size_t count
)
850 struct se_dev_attrib
*da
= to_attrib(item
);
854 ret
= strtobool(page
, &flag
);
859 printk(KERN_ERR
"dev[%p]: SE Device emulation of restricted"
860 " reordering not implemented\n", da
->da_dev
);
863 da
->emulate_rest_reord
= flag
;
864 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
870 * Note, this can only be called on unexported SE Device Object.
872 static ssize_t
queue_depth_store(struct config_item
*item
,
873 const char *page
, size_t count
)
875 struct se_dev_attrib
*da
= to_attrib(item
);
876 struct se_device
*dev
= da
->da_dev
;
880 ret
= kstrtou32(page
, 0, &val
);
884 if (dev
->export_count
) {
885 pr_err("dev[%p]: Unable to change SE Device TCQ while"
886 " export_count is %d\n",
887 dev
, dev
->export_count
);
891 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev
);
895 if (val
> dev
->dev_attrib
.queue_depth
) {
896 if (val
> dev
->dev_attrib
.hw_queue_depth
) {
897 pr_err("dev[%p]: Passed queue_depth:"
898 " %u exceeds TCM/SE_Device MAX"
899 " TCQ: %u\n", dev
, val
,
900 dev
->dev_attrib
.hw_queue_depth
);
904 da
->queue_depth
= dev
->queue_depth
= val
;
905 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev
, val
);
909 static ssize_t
optimal_sectors_store(struct config_item
*item
,
910 const char *page
, size_t count
)
912 struct se_dev_attrib
*da
= to_attrib(item
);
916 ret
= kstrtou32(page
, 0, &val
);
920 if (da
->da_dev
->export_count
) {
921 pr_err("dev[%p]: Unable to change SE Device"
922 " optimal_sectors while export_count is %d\n",
923 da
->da_dev
, da
->da_dev
->export_count
);
926 if (val
> da
->hw_max_sectors
) {
927 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
928 " greater than hw_max_sectors: %u\n",
929 da
->da_dev
, val
, da
->hw_max_sectors
);
933 da
->optimal_sectors
= val
;
934 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
939 static ssize_t
block_size_store(struct config_item
*item
,
940 const char *page
, size_t count
)
942 struct se_dev_attrib
*da
= to_attrib(item
);
946 ret
= kstrtou32(page
, 0, &val
);
950 if (da
->da_dev
->export_count
) {
951 pr_err("dev[%p]: Unable to change SE Device block_size"
952 " while export_count is %d\n",
953 da
->da_dev
, da
->da_dev
->export_count
);
957 if (val
!= 512 && val
!= 1024 && val
!= 2048 && val
!= 4096) {
958 pr_err("dev[%p]: Illegal value for block_device: %u"
959 " for SE device, must be 512, 1024, 2048 or 4096\n",
964 da
->block_size
= val
;
965 if (da
->max_bytes_per_io
)
966 da
->hw_max_sectors
= da
->max_bytes_per_io
/ val
;
968 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
973 CONFIGFS_ATTR(, emulate_model_alias
);
974 CONFIGFS_ATTR(, emulate_dpo
);
975 CONFIGFS_ATTR(, emulate_fua_write
);
976 CONFIGFS_ATTR(, emulate_fua_read
);
977 CONFIGFS_ATTR(, emulate_write_cache
);
978 CONFIGFS_ATTR(, emulate_ua_intlck_ctrl
);
979 CONFIGFS_ATTR(, emulate_tas
);
980 CONFIGFS_ATTR(, emulate_tpu
);
981 CONFIGFS_ATTR(, emulate_tpws
);
982 CONFIGFS_ATTR(, emulate_caw
);
983 CONFIGFS_ATTR(, emulate_3pc
);
984 CONFIGFS_ATTR(, pi_prot_type
);
985 CONFIGFS_ATTR_RO(, hw_pi_prot_type
);
986 CONFIGFS_ATTR(, pi_prot_format
);
987 CONFIGFS_ATTR(, enforce_pr_isids
);
988 CONFIGFS_ATTR(, is_nonrot
);
989 CONFIGFS_ATTR(, emulate_rest_reord
);
990 CONFIGFS_ATTR(, force_pr_aptpl
);
991 CONFIGFS_ATTR_RO(, hw_block_size
);
992 CONFIGFS_ATTR(, block_size
);
993 CONFIGFS_ATTR_RO(, hw_max_sectors
);
994 CONFIGFS_ATTR(, optimal_sectors
);
995 CONFIGFS_ATTR_RO(, hw_queue_depth
);
996 CONFIGFS_ATTR(, queue_depth
);
997 CONFIGFS_ATTR(, max_unmap_lba_count
);
998 CONFIGFS_ATTR(, max_unmap_block_desc_count
);
999 CONFIGFS_ATTR(, unmap_granularity
);
1000 CONFIGFS_ATTR(, unmap_granularity_alignment
);
1001 CONFIGFS_ATTR(, max_write_same_len
);
1004 * dev_attrib attributes for devices using the target core SBC/SPC
1005 * interpreter. Any backend using spc_parse_cdb should be using
1008 struct configfs_attribute
*sbc_attrib_attrs
[] = {
1009 &attr_emulate_model_alias
,
1011 &attr_emulate_fua_write
,
1012 &attr_emulate_fua_read
,
1013 &attr_emulate_write_cache
,
1014 &attr_emulate_ua_intlck_ctrl
,
1021 &attr_hw_pi_prot_type
,
1022 &attr_pi_prot_format
,
1023 &attr_enforce_pr_isids
,
1025 &attr_emulate_rest_reord
,
1026 &attr_force_pr_aptpl
,
1027 &attr_hw_block_size
,
1029 &attr_hw_max_sectors
,
1030 &attr_optimal_sectors
,
1031 &attr_hw_queue_depth
,
1033 &attr_max_unmap_lba_count
,
1034 &attr_max_unmap_block_desc_count
,
1035 &attr_unmap_granularity
,
1036 &attr_unmap_granularity_alignment
,
1037 &attr_max_write_same_len
,
1040 EXPORT_SYMBOL(sbc_attrib_attrs
);
1043 * Minimal dev_attrib attributes for devices passing through CDBs.
1044 * In this case we only provide a few read-only attributes for
1045 * backwards compatibility.
1047 struct configfs_attribute
*passthrough_attrib_attrs
[] = {
1048 &attr_hw_pi_prot_type
,
1049 &attr_hw_block_size
,
1050 &attr_hw_max_sectors
,
1051 &attr_hw_queue_depth
,
1054 EXPORT_SYMBOL(passthrough_attrib_attrs
);
1056 TB_CIT_SETUP_DRV(dev_attrib
, NULL
, NULL
);
1058 /* End functions for struct config_item_type tb_dev_attrib_cit */
1060 /* Start functions for struct config_item_type tb_dev_wwn_cit */
1062 static struct t10_wwn
*to_t10_wwn(struct config_item
*item
)
1064 return container_of(to_config_group(item
), struct t10_wwn
, t10_wwn_group
);
1068 * VPD page 0x80 Unit serial
1070 static ssize_t
target_wwn_vpd_unit_serial_show(struct config_item
*item
,
1073 return sprintf(page
, "T10 VPD Unit Serial Number: %s\n",
1074 &to_t10_wwn(item
)->unit_serial
[0]);
1077 static ssize_t
target_wwn_vpd_unit_serial_store(struct config_item
*item
,
1078 const char *page
, size_t count
)
1080 struct t10_wwn
*t10_wwn
= to_t10_wwn(item
);
1081 struct se_device
*dev
= t10_wwn
->t10_dev
;
1082 unsigned char buf
[INQUIRY_VPD_SERIAL_LEN
];
1085 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1086 * from the struct scsi_device level firmware, do not allow
1087 * VPD Unit Serial to be emulated.
1089 * Note this struct scsi_device could also be emulating VPD
1090 * information from its drivers/scsi LLD. But for now we assume
1091 * it is doing 'the right thing' wrt a world wide unique
1092 * VPD Unit Serial Number that OS dependent multipath can depend on.
1094 if (dev
->dev_flags
& DF_FIRMWARE_VPD_UNIT_SERIAL
) {
1095 pr_err("Underlying SCSI device firmware provided VPD"
1096 " Unit Serial, ignoring request\n");
1100 if (strlen(page
) >= INQUIRY_VPD_SERIAL_LEN
) {
1101 pr_err("Emulated VPD Unit Serial exceeds"
1102 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN
);
1106 * Check to see if any active $FABRIC_MOD exports exist. If they
1107 * do exist, fail here as changing this information on the fly
1108 * (underneath the initiator side OS dependent multipath code)
1109 * could cause negative effects.
1111 if (dev
->export_count
) {
1112 pr_err("Unable to set VPD Unit Serial while"
1113 " active %d $FABRIC_MOD exports exist\n",
1119 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1121 * Also, strip any newline added from the userspace
1122 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1124 memset(buf
, 0, INQUIRY_VPD_SERIAL_LEN
);
1125 snprintf(buf
, INQUIRY_VPD_SERIAL_LEN
, "%s", page
);
1126 snprintf(dev
->t10_wwn
.unit_serial
, INQUIRY_VPD_SERIAL_LEN
,
1127 "%s", strstrip(buf
));
1128 dev
->dev_flags
|= DF_EMULATED_VPD_UNIT_SERIAL
;
1130 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1131 " %s\n", dev
->t10_wwn
.unit_serial
);
1137 * VPD page 0x83 Protocol Identifier
1139 static ssize_t
target_wwn_vpd_protocol_identifier_show(struct config_item
*item
,
1142 struct t10_wwn
*t10_wwn
= to_t10_wwn(item
);
1143 struct t10_vpd
*vpd
;
1144 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1147 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1149 spin_lock(&t10_wwn
->t10_vpd_lock
);
1150 list_for_each_entry(vpd
, &t10_wwn
->t10_vpd_list
, vpd_list
) {
1151 if (!vpd
->protocol_identifier_set
)
1154 transport_dump_vpd_proto_id(vpd
, buf
, VPD_TMP_BUF_SIZE
);
1156 if (len
+ strlen(buf
) >= PAGE_SIZE
)
1159 len
+= sprintf(page
+len
, "%s", buf
);
1161 spin_unlock(&t10_wwn
->t10_vpd_lock
);
1167 * Generic wrapper for dumping VPD identifiers by association.
1169 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
1170 static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1173 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1174 struct t10_vpd *vpd; \
1175 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1178 spin_lock(&t10_wwn->t10_vpd_lock); \
1179 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1180 if (vpd->association != _assoc) \
1183 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1184 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
1185 if (len + strlen(buf) >= PAGE_SIZE) \
1187 len += sprintf(page+len, "%s", buf); \
1189 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1190 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1191 if (len + strlen(buf) >= PAGE_SIZE) \
1193 len += sprintf(page+len, "%s", buf); \
1195 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1196 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1197 if (len + strlen(buf) >= PAGE_SIZE) \
1199 len += sprintf(page+len, "%s", buf); \
1201 spin_unlock(&t10_wwn->t10_vpd_lock); \
1206 /* VPD page 0x83 Association: Logical Unit */
1207 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit
, 0x00);
1208 /* VPD page 0x83 Association: Target Port */
1209 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port
, 0x10);
1210 /* VPD page 0x83 Association: SCSI Target Device */
1211 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device
, 0x20);
1213 CONFIGFS_ATTR(target_wwn_
, vpd_unit_serial
);
1214 CONFIGFS_ATTR_RO(target_wwn_
, vpd_protocol_identifier
);
1215 CONFIGFS_ATTR_RO(target_wwn_
, vpd_assoc_logical_unit
);
1216 CONFIGFS_ATTR_RO(target_wwn_
, vpd_assoc_target_port
);
1217 CONFIGFS_ATTR_RO(target_wwn_
, vpd_assoc_scsi_target_device
);
1219 static struct configfs_attribute
*target_core_dev_wwn_attrs
[] = {
1220 &target_wwn_attr_vpd_unit_serial
,
1221 &target_wwn_attr_vpd_protocol_identifier
,
1222 &target_wwn_attr_vpd_assoc_logical_unit
,
1223 &target_wwn_attr_vpd_assoc_target_port
,
1224 &target_wwn_attr_vpd_assoc_scsi_target_device
,
1228 TB_CIT_SETUP(dev_wwn
, NULL
, NULL
, target_core_dev_wwn_attrs
);
1230 /* End functions for struct config_item_type tb_dev_wwn_cit */
1232 /* Start functions for struct config_item_type tb_dev_pr_cit */
1234 static struct se_device
*pr_to_dev(struct config_item
*item
)
1236 return container_of(to_config_group(item
), struct se_device
,
1240 static ssize_t
target_core_dev_pr_show_spc3_res(struct se_device
*dev
,
1243 struct se_node_acl
*se_nacl
;
1244 struct t10_pr_registration
*pr_reg
;
1245 char i_buf
[PR_REG_ISID_ID_LEN
];
1247 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1249 pr_reg
= dev
->dev_pr_res_holder
;
1251 return sprintf(page
, "No SPC-3 Reservation holder\n");
1253 se_nacl
= pr_reg
->pr_reg_nacl
;
1254 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1256 return sprintf(page
, "SPC-3 Reservation: %s Initiator: %s%s\n",
1257 se_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
1258 se_nacl
->initiatorname
, i_buf
);
1261 static ssize_t
target_core_dev_pr_show_spc2_res(struct se_device
*dev
,
1264 struct se_node_acl
*se_nacl
;
1267 se_nacl
= dev
->dev_reserved_node_acl
;
1270 "SPC-2 Reservation: %s Initiator: %s\n",
1271 se_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
1272 se_nacl
->initiatorname
);
1274 len
= sprintf(page
, "No SPC-2 Reservation holder\n");
1279 static ssize_t
target_pr_res_holder_show(struct config_item
*item
, char *page
)
1281 struct se_device
*dev
= pr_to_dev(item
);
1284 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
1285 return sprintf(page
, "Passthrough\n");
1287 spin_lock(&dev
->dev_reservation_lock
);
1288 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
1289 ret
= target_core_dev_pr_show_spc2_res(dev
, page
);
1291 ret
= target_core_dev_pr_show_spc3_res(dev
, page
);
1292 spin_unlock(&dev
->dev_reservation_lock
);
1296 static ssize_t
target_pr_res_pr_all_tgt_pts_show(struct config_item
*item
,
1299 struct se_device
*dev
= pr_to_dev(item
);
1302 spin_lock(&dev
->dev_reservation_lock
);
1303 if (!dev
->dev_pr_res_holder
) {
1304 len
= sprintf(page
, "No SPC-3 Reservation holder\n");
1305 } else if (dev
->dev_pr_res_holder
->pr_reg_all_tg_pt
) {
1306 len
= sprintf(page
, "SPC-3 Reservation: All Target"
1307 " Ports registration\n");
1309 len
= sprintf(page
, "SPC-3 Reservation: Single"
1310 " Target Port registration\n");
1313 spin_unlock(&dev
->dev_reservation_lock
);
1317 static ssize_t
target_pr_res_pr_generation_show(struct config_item
*item
,
1320 return sprintf(page
, "0x%08x\n", pr_to_dev(item
)->t10_pr
.pr_generation
);
1324 static ssize_t
target_pr_res_pr_holder_tg_port_show(struct config_item
*item
,
1327 struct se_device
*dev
= pr_to_dev(item
);
1328 struct se_node_acl
*se_nacl
;
1329 struct se_portal_group
*se_tpg
;
1330 struct t10_pr_registration
*pr_reg
;
1331 const struct target_core_fabric_ops
*tfo
;
1334 spin_lock(&dev
->dev_reservation_lock
);
1335 pr_reg
= dev
->dev_pr_res_holder
;
1337 len
= sprintf(page
, "No SPC-3 Reservation holder\n");
1341 se_nacl
= pr_reg
->pr_reg_nacl
;
1342 se_tpg
= se_nacl
->se_tpg
;
1343 tfo
= se_tpg
->se_tpg_tfo
;
1345 len
+= sprintf(page
+len
, "SPC-3 Reservation: %s"
1346 " Target Node Endpoint: %s\n", tfo
->get_fabric_name(),
1347 tfo
->tpg_get_wwn(se_tpg
));
1348 len
+= sprintf(page
+len
, "SPC-3 Reservation: Relative Port"
1349 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1350 " %s Logical Unit: %llu\n", pr_reg
->tg_pt_sep_rtpi
,
1351 tfo
->get_fabric_name(), tfo
->tpg_get_tag(se_tpg
),
1352 tfo
->get_fabric_name(), pr_reg
->pr_aptpl_target_lun
);
1355 spin_unlock(&dev
->dev_reservation_lock
);
1360 static ssize_t
target_pr_res_pr_registered_i_pts_show(struct config_item
*item
,
1363 struct se_device
*dev
= pr_to_dev(item
);
1364 const struct target_core_fabric_ops
*tfo
;
1365 struct t10_pr_registration
*pr_reg
;
1366 unsigned char buf
[384];
1367 char i_buf
[PR_REG_ISID_ID_LEN
];
1371 len
+= sprintf(page
+len
, "SPC-3 PR Registrations:\n");
1373 spin_lock(&dev
->t10_pr
.registration_lock
);
1374 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1377 memset(buf
, 0, 384);
1378 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1379 tfo
= pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1380 core_pr_dump_initiator_port(pr_reg
, i_buf
,
1381 PR_REG_ISID_ID_LEN
);
1382 sprintf(buf
, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1383 tfo
->get_fabric_name(),
1384 pr_reg
->pr_reg_nacl
->initiatorname
, i_buf
, pr_reg
->pr_res_key
,
1385 pr_reg
->pr_res_generation
);
1387 if (len
+ strlen(buf
) >= PAGE_SIZE
)
1390 len
+= sprintf(page
+len
, "%s", buf
);
1393 spin_unlock(&dev
->t10_pr
.registration_lock
);
1396 len
+= sprintf(page
+len
, "None\n");
1401 static ssize_t
target_pr_res_pr_type_show(struct config_item
*item
, char *page
)
1403 struct se_device
*dev
= pr_to_dev(item
);
1404 struct t10_pr_registration
*pr_reg
;
1407 spin_lock(&dev
->dev_reservation_lock
);
1408 pr_reg
= dev
->dev_pr_res_holder
;
1410 len
= sprintf(page
, "SPC-3 Reservation Type: %s\n",
1411 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
));
1413 len
= sprintf(page
, "No SPC-3 Reservation holder\n");
1416 spin_unlock(&dev
->dev_reservation_lock
);
1420 static ssize_t
target_pr_res_type_show(struct config_item
*item
, char *page
)
1422 struct se_device
*dev
= pr_to_dev(item
);
1424 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
1425 return sprintf(page
, "SPC_PASSTHROUGH\n");
1426 else if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
1427 return sprintf(page
, "SPC2_RESERVATIONS\n");
1429 return sprintf(page
, "SPC3_PERSISTENT_RESERVATIONS\n");
1432 static ssize_t
target_pr_res_aptpl_active_show(struct config_item
*item
,
1435 struct se_device
*dev
= pr_to_dev(item
);
1437 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
1440 return sprintf(page
, "APTPL Bit Status: %s\n",
1441 (dev
->t10_pr
.pr_aptpl_active
) ? "Activated" : "Disabled");
1444 static ssize_t
target_pr_res_aptpl_metadata_show(struct config_item
*item
,
1447 struct se_device
*dev
= pr_to_dev(item
);
1449 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
1452 return sprintf(page
, "Ready to process PR APTPL metadata..\n");
1456 Opt_initiator_fabric
, Opt_initiator_node
, Opt_initiator_sid
,
1457 Opt_sa_res_key
, Opt_res_holder
, Opt_res_type
, Opt_res_scope
,
1458 Opt_res_all_tg_pt
, Opt_mapped_lun
, Opt_target_fabric
,
1459 Opt_target_node
, Opt_tpgt
, Opt_port_rtpi
, Opt_target_lun
, Opt_err
1462 static match_table_t tokens
= {
1463 {Opt_initiator_fabric
, "initiator_fabric=%s"},
1464 {Opt_initiator_node
, "initiator_node=%s"},
1465 {Opt_initiator_sid
, "initiator_sid=%s"},
1466 {Opt_sa_res_key
, "sa_res_key=%s"},
1467 {Opt_res_holder
, "res_holder=%d"},
1468 {Opt_res_type
, "res_type=%d"},
1469 {Opt_res_scope
, "res_scope=%d"},
1470 {Opt_res_all_tg_pt
, "res_all_tg_pt=%d"},
1471 {Opt_mapped_lun
, "mapped_lun=%lld"},
1472 {Opt_target_fabric
, "target_fabric=%s"},
1473 {Opt_target_node
, "target_node=%s"},
1474 {Opt_tpgt
, "tpgt=%d"},
1475 {Opt_port_rtpi
, "port_rtpi=%d"},
1476 {Opt_target_lun
, "target_lun=%lld"},
1480 static ssize_t
target_pr_res_aptpl_metadata_store(struct config_item
*item
,
1481 const char *page
, size_t count
)
1483 struct se_device
*dev
= pr_to_dev(item
);
1484 unsigned char *i_fabric
= NULL
, *i_port
= NULL
, *isid
= NULL
;
1485 unsigned char *t_fabric
= NULL
, *t_port
= NULL
;
1486 char *orig
, *ptr
, *opts
;
1487 substring_t args
[MAX_OPT_ARGS
];
1488 unsigned long long tmp_ll
;
1490 u64 mapped_lun
= 0, target_lun
= 0;
1491 int ret
= -1, res_holder
= 0, all_tg_pt
= 0, arg
, token
;
1495 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
1497 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
1500 if (dev
->export_count
) {
1501 pr_debug("Unable to process APTPL metadata while"
1502 " active fabric exports exist\n");
1506 opts
= kstrdup(page
, GFP_KERNEL
);
1511 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
1515 token
= match_token(ptr
, tokens
, args
);
1517 case Opt_initiator_fabric
:
1518 i_fabric
= match_strdup(args
);
1524 case Opt_initiator_node
:
1525 i_port
= match_strdup(args
);
1530 if (strlen(i_port
) >= PR_APTPL_MAX_IPORT_LEN
) {
1531 pr_err("APTPL metadata initiator_node="
1532 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1533 PR_APTPL_MAX_IPORT_LEN
);
1538 case Opt_initiator_sid
:
1539 isid
= match_strdup(args
);
1544 if (strlen(isid
) >= PR_REG_ISID_LEN
) {
1545 pr_err("APTPL metadata initiator_isid"
1546 "= exceeds PR_REG_ISID_LEN: %d\n",
1552 case Opt_sa_res_key
:
1553 ret
= kstrtoull(args
->from
, 0, &tmp_ll
);
1555 pr_err("kstrtoull() failed for sa_res_key=\n");
1558 sa_res_key
= (u64
)tmp_ll
;
1561 * PR APTPL Metadata for Reservation
1563 case Opt_res_holder
:
1564 ret
= match_int(args
, &arg
);
1570 ret
= match_int(args
, &arg
);
1576 ret
= match_int(args
, &arg
);
1580 case Opt_res_all_tg_pt
:
1581 ret
= match_int(args
, &arg
);
1584 all_tg_pt
= (int)arg
;
1586 case Opt_mapped_lun
:
1587 ret
= match_int(args
, &arg
);
1590 mapped_lun
= (u64
)arg
;
1593 * PR APTPL Metadata for Target Port
1595 case Opt_target_fabric
:
1596 t_fabric
= match_strdup(args
);
1602 case Opt_target_node
:
1603 t_port
= match_strdup(args
);
1608 if (strlen(t_port
) >= PR_APTPL_MAX_TPORT_LEN
) {
1609 pr_err("APTPL metadata target_node="
1610 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1611 PR_APTPL_MAX_TPORT_LEN
);
1617 ret
= match_int(args
, &arg
);
1623 ret
= match_int(args
, &arg
);
1627 case Opt_target_lun
:
1628 ret
= match_int(args
, &arg
);
1631 target_lun
= (u64
)arg
;
1638 if (!i_port
|| !t_port
|| !sa_res_key
) {
1639 pr_err("Illegal parameters for APTPL registration\n");
1644 if (res_holder
&& !(type
)) {
1645 pr_err("Illegal PR type: 0x%02x for reservation"
1651 ret
= core_scsi3_alloc_aptpl_registration(&dev
->t10_pr
, sa_res_key
,
1652 i_port
, isid
, mapped_lun
, t_port
, tpgt
, target_lun
,
1653 res_holder
, all_tg_pt
, type
);
1661 return (ret
== 0) ? count
: ret
;
1665 CONFIGFS_ATTR_RO(target_pr_
, res_holder
);
1666 CONFIGFS_ATTR_RO(target_pr_
, res_pr_all_tgt_pts
);
1667 CONFIGFS_ATTR_RO(target_pr_
, res_pr_generation
);
1668 CONFIGFS_ATTR_RO(target_pr_
, res_pr_holder_tg_port
);
1669 CONFIGFS_ATTR_RO(target_pr_
, res_pr_registered_i_pts
);
1670 CONFIGFS_ATTR_RO(target_pr_
, res_pr_type
);
1671 CONFIGFS_ATTR_RO(target_pr_
, res_type
);
1672 CONFIGFS_ATTR_RO(target_pr_
, res_aptpl_active
);
1673 CONFIGFS_ATTR(target_pr_
, res_aptpl_metadata
);
1675 static struct configfs_attribute
*target_core_dev_pr_attrs
[] = {
1676 &target_pr_attr_res_holder
,
1677 &target_pr_attr_res_pr_all_tgt_pts
,
1678 &target_pr_attr_res_pr_generation
,
1679 &target_pr_attr_res_pr_holder_tg_port
,
1680 &target_pr_attr_res_pr_registered_i_pts
,
1681 &target_pr_attr_res_pr_type
,
1682 &target_pr_attr_res_type
,
1683 &target_pr_attr_res_aptpl_active
,
1684 &target_pr_attr_res_aptpl_metadata
,
1688 TB_CIT_SETUP(dev_pr
, NULL
, NULL
, target_core_dev_pr_attrs
);
1690 /* End functions for struct config_item_type tb_dev_pr_cit */
1692 /* Start functions for struct config_item_type tb_dev_cit */
1694 static inline struct se_device
*to_device(struct config_item
*item
)
1696 return container_of(to_config_group(item
), struct se_device
, dev_group
);
1699 static ssize_t
target_dev_info_show(struct config_item
*item
, char *page
)
1701 struct se_device
*dev
= to_device(item
);
1703 ssize_t read_bytes
= 0;
1705 transport_dump_dev_state(dev
, page
, &bl
);
1707 read_bytes
+= dev
->transport
->show_configfs_dev_params(dev
,
1712 static ssize_t
target_dev_control_store(struct config_item
*item
,
1713 const char *page
, size_t count
)
1715 struct se_device
*dev
= to_device(item
);
1717 return dev
->transport
->set_configfs_dev_params(dev
, page
, count
);
1720 static ssize_t
target_dev_alias_show(struct config_item
*item
, char *page
)
1722 struct se_device
*dev
= to_device(item
);
1724 if (!(dev
->dev_flags
& DF_USING_ALIAS
))
1727 return snprintf(page
, PAGE_SIZE
, "%s\n", dev
->dev_alias
);
1730 static ssize_t
target_dev_alias_store(struct config_item
*item
,
1731 const char *page
, size_t count
)
1733 struct se_device
*dev
= to_device(item
);
1734 struct se_hba
*hba
= dev
->se_hba
;
1737 if (count
> (SE_DEV_ALIAS_LEN
-1)) {
1738 pr_err("alias count: %d exceeds"
1739 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count
,
1740 SE_DEV_ALIAS_LEN
-1);
1744 read_bytes
= snprintf(&dev
->dev_alias
[0], SE_DEV_ALIAS_LEN
, "%s", page
);
1747 if (dev
->dev_alias
[read_bytes
- 1] == '\n')
1748 dev
->dev_alias
[read_bytes
- 1] = '\0';
1750 dev
->dev_flags
|= DF_USING_ALIAS
;
1752 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1753 config_item_name(&hba
->hba_group
.cg_item
),
1754 config_item_name(&dev
->dev_group
.cg_item
),
1760 static ssize_t
target_dev_udev_path_show(struct config_item
*item
, char *page
)
1762 struct se_device
*dev
= to_device(item
);
1764 if (!(dev
->dev_flags
& DF_USING_UDEV_PATH
))
1767 return snprintf(page
, PAGE_SIZE
, "%s\n", dev
->udev_path
);
1770 static ssize_t
target_dev_udev_path_store(struct config_item
*item
,
1771 const char *page
, size_t count
)
1773 struct se_device
*dev
= to_device(item
);
1774 struct se_hba
*hba
= dev
->se_hba
;
1777 if (count
> (SE_UDEV_PATH_LEN
-1)) {
1778 pr_err("udev_path count: %d exceeds"
1779 " SE_UDEV_PATH_LEN-1: %u\n", (int)count
,
1780 SE_UDEV_PATH_LEN
-1);
1784 read_bytes
= snprintf(&dev
->udev_path
[0], SE_UDEV_PATH_LEN
,
1788 if (dev
->udev_path
[read_bytes
- 1] == '\n')
1789 dev
->udev_path
[read_bytes
- 1] = '\0';
1791 dev
->dev_flags
|= DF_USING_UDEV_PATH
;
1793 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1794 config_item_name(&hba
->hba_group
.cg_item
),
1795 config_item_name(&dev
->dev_group
.cg_item
),
1801 static ssize_t
target_dev_enable_show(struct config_item
*item
, char *page
)
1803 struct se_device
*dev
= to_device(item
);
1805 return snprintf(page
, PAGE_SIZE
, "%d\n", !!(dev
->dev_flags
& DF_CONFIGURED
));
1808 static ssize_t
target_dev_enable_store(struct config_item
*item
,
1809 const char *page
, size_t count
)
1811 struct se_device
*dev
= to_device(item
);
1815 ptr
= strstr(page
, "1");
1817 pr_err("For dev_enable ops, only valid value"
1822 ret
= target_configure_device(dev
);
1828 static ssize_t
target_dev_alua_lu_gp_show(struct config_item
*item
, char *page
)
1830 struct se_device
*dev
= to_device(item
);
1831 struct config_item
*lu_ci
;
1832 struct t10_alua_lu_gp
*lu_gp
;
1833 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1836 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1840 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1841 lu_gp
= lu_gp_mem
->lu_gp
;
1843 lu_ci
= &lu_gp
->lu_gp_group
.cg_item
;
1844 len
+= sprintf(page
, "LU Group Alias: %s\nLU Group ID: %hu\n",
1845 config_item_name(lu_ci
), lu_gp
->lu_gp_id
);
1847 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1852 static ssize_t
target_dev_alua_lu_gp_store(struct config_item
*item
,
1853 const char *page
, size_t count
)
1855 struct se_device
*dev
= to_device(item
);
1856 struct se_hba
*hba
= dev
->se_hba
;
1857 struct t10_alua_lu_gp
*lu_gp
= NULL
, *lu_gp_new
= NULL
;
1858 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1859 unsigned char buf
[LU_GROUP_NAME_BUF
];
1862 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1866 if (count
> LU_GROUP_NAME_BUF
) {
1867 pr_err("ALUA LU Group Alias too large!\n");
1870 memset(buf
, 0, LU_GROUP_NAME_BUF
);
1871 memcpy(buf
, page
, count
);
1873 * Any ALUA logical unit alias besides "NULL" means we will be
1874 * making a new group association.
1876 if (strcmp(strstrip(buf
), "NULL")) {
1878 * core_alua_get_lu_gp_by_name() will increment reference to
1879 * struct t10_alua_lu_gp. This reference is released with
1880 * core_alua_get_lu_gp_by_name below().
1882 lu_gp_new
= core_alua_get_lu_gp_by_name(strstrip(buf
));
1887 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1888 lu_gp
= lu_gp_mem
->lu_gp
;
1891 * Clearing an existing lu_gp association, and replacing
1895 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1896 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1898 config_item_name(&hba
->hba_group
.cg_item
),
1899 config_item_name(&dev
->dev_group
.cg_item
),
1900 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1903 __core_alua_drop_lu_gp_mem(lu_gp_mem
, lu_gp
);
1904 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1909 * Removing existing association of lu_gp_mem with lu_gp
1911 __core_alua_drop_lu_gp_mem(lu_gp_mem
, lu_gp
);
1915 * Associate lu_gp_mem with lu_gp_new.
1917 __core_alua_attach_lu_gp_mem(lu_gp_mem
, lu_gp_new
);
1918 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1920 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1921 " core/alua/lu_gps/%s, ID: %hu\n",
1922 (move
) ? "Moving" : "Adding",
1923 config_item_name(&hba
->hba_group
.cg_item
),
1924 config_item_name(&dev
->dev_group
.cg_item
),
1925 config_item_name(&lu_gp_new
->lu_gp_group
.cg_item
),
1926 lu_gp_new
->lu_gp_id
);
1928 core_alua_put_lu_gp_from_name(lu_gp_new
);
1932 static ssize_t
target_dev_lba_map_show(struct config_item
*item
, char *page
)
1934 struct se_device
*dev
= to_device(item
);
1935 struct t10_alua_lba_map
*map
;
1936 struct t10_alua_lba_map_member
*mem
;
1941 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1942 if (!list_empty(&dev
->t10_alua
.lba_map_list
))
1943 bl
+= sprintf(b
+ bl
, "%u %u\n",
1944 dev
->t10_alua
.lba_map_segment_size
,
1945 dev
->t10_alua
.lba_map_segment_multiplier
);
1946 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
, lba_map_list
) {
1947 bl
+= sprintf(b
+ bl
, "%llu %llu",
1948 map
->lba_map_first_lba
, map
->lba_map_last_lba
);
1949 list_for_each_entry(mem
, &map
->lba_map_mem_list
,
1951 switch (mem
->lba_map_mem_alua_state
) {
1952 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
1955 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
1958 case ALUA_ACCESS_STATE_STANDBY
:
1961 case ALUA_ACCESS_STATE_UNAVAILABLE
:
1968 bl
+= sprintf(b
+ bl
, " %d:%c",
1969 mem
->lba_map_mem_alua_pg_id
, state
);
1971 bl
+= sprintf(b
+ bl
, "\n");
1973 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1977 static ssize_t
target_dev_lba_map_store(struct config_item
*item
,
1978 const char *page
, size_t count
)
1980 struct se_device
*dev
= to_device(item
);
1981 struct t10_alua_lba_map
*lba_map
= NULL
;
1982 struct list_head lba_list
;
1983 char *map_entries
, *ptr
;
1985 int pg_num
= -1, pg
;
1986 int ret
= 0, num
= 0, pg_id
, alua_state
;
1987 unsigned long start_lba
= -1, end_lba
= -1;
1988 unsigned long segment_size
= -1, segment_mult
= -1;
1990 map_entries
= kstrdup(page
, GFP_KERNEL
);
1994 INIT_LIST_HEAD(&lba_list
);
1995 while ((ptr
= strsep(&map_entries
, "\n")) != NULL
) {
2000 if (sscanf(ptr
, "%lu %lu\n",
2001 &segment_size
, &segment_mult
) != 2) {
2002 pr_err("Invalid line %d\n", num
);
2009 if (sscanf(ptr
, "%lu %lu", &start_lba
, &end_lba
) != 2) {
2010 pr_err("Invalid line %d\n", num
);
2014 ptr
= strchr(ptr
, ' ');
2016 pr_err("Invalid line %d, missing end lba\n", num
);
2021 ptr
= strchr(ptr
, ' ');
2023 pr_err("Invalid line %d, missing state definitions\n",
2029 lba_map
= core_alua_allocate_lba_map(&lba_list
,
2030 start_lba
, end_lba
);
2031 if (IS_ERR(lba_map
)) {
2032 ret
= PTR_ERR(lba_map
);
2036 while (sscanf(ptr
, "%d:%c", &pg_id
, &state
) == 2) {
2039 alua_state
= ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
;
2042 alua_state
= ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
;
2045 alua_state
= ALUA_ACCESS_STATE_STANDBY
;
2048 alua_state
= ALUA_ACCESS_STATE_UNAVAILABLE
;
2051 pr_err("Invalid ALUA state '%c'\n", state
);
2056 ret
= core_alua_allocate_lba_map_mem(lba_map
,
2059 pr_err("Invalid target descriptor %d:%c "
2065 ptr
= strchr(ptr
, ' ');
2073 else if (pg
!= pg_num
) {
2074 pr_err("Only %d from %d port groups definitions "
2075 "at line %d\n", pg
, pg_num
, num
);
2083 core_alua_free_lba_map(&lba_list
);
2086 core_alua_set_lba_map(dev
, &lba_list
,
2087 segment_size
, segment_mult
);
2092 CONFIGFS_ATTR_RO(target_dev_
, info
);
2093 CONFIGFS_ATTR_WO(target_dev_
, control
);
2094 CONFIGFS_ATTR(target_dev_
, alias
);
2095 CONFIGFS_ATTR(target_dev_
, udev_path
);
2096 CONFIGFS_ATTR(target_dev_
, enable
);
2097 CONFIGFS_ATTR(target_dev_
, alua_lu_gp
);
2098 CONFIGFS_ATTR(target_dev_
, lba_map
);
2100 static struct configfs_attribute
*target_core_dev_attrs
[] = {
2101 &target_dev_attr_info
,
2102 &target_dev_attr_control
,
2103 &target_dev_attr_alias
,
2104 &target_dev_attr_udev_path
,
2105 &target_dev_attr_enable
,
2106 &target_dev_attr_alua_lu_gp
,
2107 &target_dev_attr_lba_map
,
2111 static void target_core_dev_release(struct config_item
*item
)
2113 struct config_group
*dev_cg
= to_config_group(item
);
2114 struct se_device
*dev
=
2115 container_of(dev_cg
, struct se_device
, dev_group
);
2117 kfree(dev_cg
->default_groups
);
2118 target_free_device(dev
);
2121 static struct configfs_item_operations target_core_dev_item_ops
= {
2122 .release
= target_core_dev_release
,
2125 TB_CIT_SETUP(dev
, &target_core_dev_item_ops
, NULL
, target_core_dev_attrs
);
2127 /* End functions for struct config_item_type tb_dev_cit */
2129 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2131 static inline struct t10_alua_lu_gp
*to_lu_gp(struct config_item
*item
)
2133 return container_of(to_config_group(item
), struct t10_alua_lu_gp
,
2137 static ssize_t
target_lu_gp_lu_gp_id_show(struct config_item
*item
, char *page
)
2139 struct t10_alua_lu_gp
*lu_gp
= to_lu_gp(item
);
2141 if (!lu_gp
->lu_gp_valid_id
)
2143 return sprintf(page
, "%hu\n", lu_gp
->lu_gp_id
);
2146 static ssize_t
target_lu_gp_lu_gp_id_store(struct config_item
*item
,
2147 const char *page
, size_t count
)
2149 struct t10_alua_lu_gp
*lu_gp
= to_lu_gp(item
);
2150 struct config_group
*alua_lu_gp_cg
= &lu_gp
->lu_gp_group
;
2151 unsigned long lu_gp_id
;
2154 ret
= kstrtoul(page
, 0, &lu_gp_id
);
2156 pr_err("kstrtoul() returned %d for"
2157 " lu_gp_id\n", ret
);
2160 if (lu_gp_id
> 0x0000ffff) {
2161 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2162 " 0x0000ffff\n", lu_gp_id
);
2166 ret
= core_alua_set_lu_gp_id(lu_gp
, (u16
)lu_gp_id
);
2170 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2171 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2172 config_item_name(&alua_lu_gp_cg
->cg_item
),
2178 static ssize_t
target_lu_gp_members_show(struct config_item
*item
, char *page
)
2180 struct t10_alua_lu_gp
*lu_gp
= to_lu_gp(item
);
2181 struct se_device
*dev
;
2183 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2184 ssize_t len
= 0, cur_len
;
2185 unsigned char buf
[LU_GROUP_NAME_BUF
];
2187 memset(buf
, 0, LU_GROUP_NAME_BUF
);
2189 spin_lock(&lu_gp
->lu_gp_lock
);
2190 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
2191 dev
= lu_gp_mem
->lu_gp_mem_dev
;
2194 cur_len
= snprintf(buf
, LU_GROUP_NAME_BUF
, "%s/%s\n",
2195 config_item_name(&hba
->hba_group
.cg_item
),
2196 config_item_name(&dev
->dev_group
.cg_item
));
2197 cur_len
++; /* Extra byte for NULL terminator */
2199 if ((cur_len
+ len
) > PAGE_SIZE
) {
2200 pr_warn("Ran out of lu_gp_show_attr"
2201 "_members buffer\n");
2204 memcpy(page
+len
, buf
, cur_len
);
2207 spin_unlock(&lu_gp
->lu_gp_lock
);
2212 CONFIGFS_ATTR(target_lu_gp_
, lu_gp_id
);
2213 CONFIGFS_ATTR_RO(target_lu_gp_
, members
);
2215 static struct configfs_attribute
*target_core_alua_lu_gp_attrs
[] = {
2216 &target_lu_gp_attr_lu_gp_id
,
2217 &target_lu_gp_attr_members
,
2221 static void target_core_alua_lu_gp_release(struct config_item
*item
)
2223 struct t10_alua_lu_gp
*lu_gp
= container_of(to_config_group(item
),
2224 struct t10_alua_lu_gp
, lu_gp_group
);
2226 core_alua_free_lu_gp(lu_gp
);
2229 static struct configfs_item_operations target_core_alua_lu_gp_ops
= {
2230 .release
= target_core_alua_lu_gp_release
,
2233 static struct config_item_type target_core_alua_lu_gp_cit
= {
2234 .ct_item_ops
= &target_core_alua_lu_gp_ops
,
2235 .ct_attrs
= target_core_alua_lu_gp_attrs
,
2236 .ct_owner
= THIS_MODULE
,
2239 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2241 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2243 static struct config_group
*target_core_alua_create_lu_gp(
2244 struct config_group
*group
,
2247 struct t10_alua_lu_gp
*lu_gp
;
2248 struct config_group
*alua_lu_gp_cg
= NULL
;
2249 struct config_item
*alua_lu_gp_ci
= NULL
;
2251 lu_gp
= core_alua_allocate_lu_gp(name
, 0);
2255 alua_lu_gp_cg
= &lu_gp
->lu_gp_group
;
2256 alua_lu_gp_ci
= &alua_lu_gp_cg
->cg_item
;
2258 config_group_init_type_name(alua_lu_gp_cg
, name
,
2259 &target_core_alua_lu_gp_cit
);
2261 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2262 " Group: core/alua/lu_gps/%s\n",
2263 config_item_name(alua_lu_gp_ci
));
2265 return alua_lu_gp_cg
;
2269 static void target_core_alua_drop_lu_gp(
2270 struct config_group
*group
,
2271 struct config_item
*item
)
2273 struct t10_alua_lu_gp
*lu_gp
= container_of(to_config_group(item
),
2274 struct t10_alua_lu_gp
, lu_gp_group
);
2276 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2277 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2278 config_item_name(item
), lu_gp
->lu_gp_id
);
2280 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2281 * -> target_core_alua_lu_gp_release()
2283 config_item_put(item
);
2286 static struct configfs_group_operations target_core_alua_lu_gps_group_ops
= {
2287 .make_group
= &target_core_alua_create_lu_gp
,
2288 .drop_item
= &target_core_alua_drop_lu_gp
,
2291 static struct config_item_type target_core_alua_lu_gps_cit
= {
2292 .ct_item_ops
= NULL
,
2293 .ct_group_ops
= &target_core_alua_lu_gps_group_ops
,
2294 .ct_owner
= THIS_MODULE
,
2297 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2299 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2301 static inline struct t10_alua_tg_pt_gp
*to_tg_pt_gp(struct config_item
*item
)
2303 return container_of(to_config_group(item
), struct t10_alua_tg_pt_gp
,
2307 static ssize_t
target_tg_pt_gp_alua_access_state_show(struct config_item
*item
,
2310 return sprintf(page
, "%d\n",
2311 atomic_read(&to_tg_pt_gp(item
)->tg_pt_gp_alua_access_state
));
2314 static ssize_t
target_tg_pt_gp_alua_access_state_store(struct config_item
*item
,
2315 const char *page
, size_t count
)
2317 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2318 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
2322 if (!tg_pt_gp
->tg_pt_gp_valid_id
) {
2323 pr_err("Unable to do implicit ALUA on non valid"
2324 " tg_pt_gp ID: %hu\n", tg_pt_gp
->tg_pt_gp_valid_id
);
2327 if (!(dev
->dev_flags
& DF_CONFIGURED
)) {
2328 pr_err("Unable to set alua_access_state while device is"
2329 " not configured\n");
2333 ret
= kstrtoul(page
, 0, &tmp
);
2335 pr_err("Unable to extract new ALUA access state from"
2339 new_state
= (int)tmp
;
2341 if (!(tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)) {
2342 pr_err("Unable to process implicit configfs ALUA"
2343 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2346 if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
&&
2347 new_state
== ALUA_ACCESS_STATE_LBA_DEPENDENT
) {
2348 /* LBA DEPENDENT is only allowed with implicit ALUA */
2349 pr_err("Unable to process implicit configfs ALUA transition"
2350 " while explicit ALUA management is enabled\n");
2354 ret
= core_alua_do_port_transition(tg_pt_gp
, dev
,
2355 NULL
, NULL
, new_state
, 0);
2356 return (!ret
) ? count
: -EINVAL
;
2359 static ssize_t
target_tg_pt_gp_alua_access_status_show(struct config_item
*item
,
2362 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2363 return sprintf(page
, "%s\n",
2364 core_alua_dump_status(tg_pt_gp
->tg_pt_gp_alua_access_status
));
2367 static ssize_t
target_tg_pt_gp_alua_access_status_store(
2368 struct config_item
*item
, const char *page
, size_t count
)
2370 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2372 int new_status
, ret
;
2374 if (!tg_pt_gp
->tg_pt_gp_valid_id
) {
2375 pr_err("Unable to do set ALUA access status on non"
2376 " valid tg_pt_gp ID: %hu\n",
2377 tg_pt_gp
->tg_pt_gp_valid_id
);
2381 ret
= kstrtoul(page
, 0, &tmp
);
2383 pr_err("Unable to extract new ALUA access status"
2384 " from %s\n", page
);
2387 new_status
= (int)tmp
;
2389 if ((new_status
!= ALUA_STATUS_NONE
) &&
2390 (new_status
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2391 (new_status
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2392 pr_err("Illegal ALUA access status: 0x%02x\n",
2397 tg_pt_gp
->tg_pt_gp_alua_access_status
= new_status
;
2401 static ssize_t
target_tg_pt_gp_alua_access_type_show(struct config_item
*item
,
2404 return core_alua_show_access_type(to_tg_pt_gp(item
), page
);
2407 static ssize_t
target_tg_pt_gp_alua_access_type_store(struct config_item
*item
,
2408 const char *page
, size_t count
)
2410 return core_alua_store_access_type(to_tg_pt_gp(item
), page
, count
);
2413 #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2414 static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2415 struct config_item *item, char *p) \
2417 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2418 return sprintf(p, "%d\n", \
2419 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2422 static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2423 struct config_item *item, const char *p, size_t c) \
2425 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2426 unsigned long tmp; \
2429 if (!t->tg_pt_gp_valid_id) { \
2430 pr_err("Unable to do set ##_name ALUA state on non" \
2431 " valid tg_pt_gp ID: %hu\n", \
2432 t->tg_pt_gp_valid_id); \
2436 ret = kstrtoul(p, 0, &tmp); \
2438 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2442 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2446 t->tg_pt_gp_alua_supported_states |= _bit; \
2448 t->tg_pt_gp_alua_supported_states &= ~_bit; \
2453 ALUA_SUPPORTED_STATE_ATTR(transitioning
, ALUA_T_SUP
);
2454 ALUA_SUPPORTED_STATE_ATTR(offline
, ALUA_O_SUP
);
2455 ALUA_SUPPORTED_STATE_ATTR(lba_dependent
, ALUA_LBD_SUP
);
2456 ALUA_SUPPORTED_STATE_ATTR(unavailable
, ALUA_U_SUP
);
2457 ALUA_SUPPORTED_STATE_ATTR(standby
, ALUA_S_SUP
);
2458 ALUA_SUPPORTED_STATE_ATTR(active_optimized
, ALUA_AO_SUP
);
2459 ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized
, ALUA_AN_SUP
);
2461 static ssize_t
target_tg_pt_gp_alua_write_metadata_show(
2462 struct config_item
*item
, char *page
)
2464 return sprintf(page
, "%d\n",
2465 to_tg_pt_gp(item
)->tg_pt_gp_write_metadata
);
2468 static ssize_t
target_tg_pt_gp_alua_write_metadata_store(
2469 struct config_item
*item
, const char *page
, size_t count
)
2471 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2475 ret
= kstrtoul(page
, 0, &tmp
);
2477 pr_err("Unable to extract alua_write_metadata\n");
2481 if ((tmp
!= 0) && (tmp
!= 1)) {
2482 pr_err("Illegal value for alua_write_metadata:"
2486 tg_pt_gp
->tg_pt_gp_write_metadata
= (int)tmp
;
2491 static ssize_t
target_tg_pt_gp_nonop_delay_msecs_show(struct config_item
*item
,
2494 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item
), page
);
2497 static ssize_t
target_tg_pt_gp_nonop_delay_msecs_store(struct config_item
*item
,
2498 const char *page
, size_t count
)
2500 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item
), page
,
2504 static ssize_t
target_tg_pt_gp_trans_delay_msecs_show(struct config_item
*item
,
2507 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item
), page
);
2510 static ssize_t
target_tg_pt_gp_trans_delay_msecs_store(struct config_item
*item
,
2511 const char *page
, size_t count
)
2513 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item
), page
,
2517 static ssize_t
target_tg_pt_gp_implicit_trans_secs_show(
2518 struct config_item
*item
, char *page
)
2520 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item
), page
);
2523 static ssize_t
target_tg_pt_gp_implicit_trans_secs_store(
2524 struct config_item
*item
, const char *page
, size_t count
)
2526 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item
), page
,
2530 static ssize_t
target_tg_pt_gp_preferred_show(struct config_item
*item
,
2533 return core_alua_show_preferred_bit(to_tg_pt_gp(item
), page
);
2536 static ssize_t
target_tg_pt_gp_preferred_store(struct config_item
*item
,
2537 const char *page
, size_t count
)
2539 return core_alua_store_preferred_bit(to_tg_pt_gp(item
), page
, count
);
2542 static ssize_t
target_tg_pt_gp_tg_pt_gp_id_show(struct config_item
*item
,
2545 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2547 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
2549 return sprintf(page
, "%hu\n", tg_pt_gp
->tg_pt_gp_id
);
2552 static ssize_t
target_tg_pt_gp_tg_pt_gp_id_store(struct config_item
*item
,
2553 const char *page
, size_t count
)
2555 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2556 struct config_group
*alua_tg_pt_gp_cg
= &tg_pt_gp
->tg_pt_gp_group
;
2557 unsigned long tg_pt_gp_id
;
2560 ret
= kstrtoul(page
, 0, &tg_pt_gp_id
);
2562 pr_err("kstrtoul() returned %d for"
2563 " tg_pt_gp_id\n", ret
);
2566 if (tg_pt_gp_id
> 0x0000ffff) {
2567 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2568 " 0x0000ffff\n", tg_pt_gp_id
);
2572 ret
= core_alua_set_tg_pt_gp_id(tg_pt_gp
, (u16
)tg_pt_gp_id
);
2576 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2577 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2578 config_item_name(&alua_tg_pt_gp_cg
->cg_item
),
2579 tg_pt_gp
->tg_pt_gp_id
);
2584 static ssize_t
target_tg_pt_gp_members_show(struct config_item
*item
,
2587 struct t10_alua_tg_pt_gp
*tg_pt_gp
= to_tg_pt_gp(item
);
2589 ssize_t len
= 0, cur_len
;
2590 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
2592 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
2594 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
2595 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
2596 lun_tg_pt_gp_link
) {
2597 struct se_portal_group
*tpg
= lun
->lun_tpg
;
2599 cur_len
= snprintf(buf
, TG_PT_GROUP_NAME_BUF
, "%s/%s/tpgt_%hu"
2600 "/%s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
2601 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2602 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2603 config_item_name(&lun
->lun_group
.cg_item
));
2604 cur_len
++; /* Extra byte for NULL terminator */
2606 if ((cur_len
+ len
) > PAGE_SIZE
) {
2607 pr_warn("Ran out of lu_gp_show_attr"
2608 "_members buffer\n");
2611 memcpy(page
+len
, buf
, cur_len
);
2614 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
2619 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_access_state
);
2620 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_access_status
);
2621 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_access_type
);
2622 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_transitioning
);
2623 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_offline
);
2624 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_lba_dependent
);
2625 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_unavailable
);
2626 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_standby
);
2627 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_active_optimized
);
2628 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_support_active_nonoptimized
);
2629 CONFIGFS_ATTR(target_tg_pt_gp_
, alua_write_metadata
);
2630 CONFIGFS_ATTR(target_tg_pt_gp_
, nonop_delay_msecs
);
2631 CONFIGFS_ATTR(target_tg_pt_gp_
, trans_delay_msecs
);
2632 CONFIGFS_ATTR(target_tg_pt_gp_
, implicit_trans_secs
);
2633 CONFIGFS_ATTR(target_tg_pt_gp_
, preferred
);
2634 CONFIGFS_ATTR(target_tg_pt_gp_
, tg_pt_gp_id
);
2635 CONFIGFS_ATTR_RO(target_tg_pt_gp_
, members
);
2637 static struct configfs_attribute
*target_core_alua_tg_pt_gp_attrs
[] = {
2638 &target_tg_pt_gp_attr_alua_access_state
,
2639 &target_tg_pt_gp_attr_alua_access_status
,
2640 &target_tg_pt_gp_attr_alua_access_type
,
2641 &target_tg_pt_gp_attr_alua_support_transitioning
,
2642 &target_tg_pt_gp_attr_alua_support_offline
,
2643 &target_tg_pt_gp_attr_alua_support_lba_dependent
,
2644 &target_tg_pt_gp_attr_alua_support_unavailable
,
2645 &target_tg_pt_gp_attr_alua_support_standby
,
2646 &target_tg_pt_gp_attr_alua_support_active_nonoptimized
,
2647 &target_tg_pt_gp_attr_alua_support_active_optimized
,
2648 &target_tg_pt_gp_attr_alua_write_metadata
,
2649 &target_tg_pt_gp_attr_nonop_delay_msecs
,
2650 &target_tg_pt_gp_attr_trans_delay_msecs
,
2651 &target_tg_pt_gp_attr_implicit_trans_secs
,
2652 &target_tg_pt_gp_attr_preferred
,
2653 &target_tg_pt_gp_attr_tg_pt_gp_id
,
2654 &target_tg_pt_gp_attr_members
,
2658 static void target_core_alua_tg_pt_gp_release(struct config_item
*item
)
2660 struct t10_alua_tg_pt_gp
*tg_pt_gp
= container_of(to_config_group(item
),
2661 struct t10_alua_tg_pt_gp
, tg_pt_gp_group
);
2663 core_alua_free_tg_pt_gp(tg_pt_gp
);
2666 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops
= {
2667 .release
= target_core_alua_tg_pt_gp_release
,
2670 static struct config_item_type target_core_alua_tg_pt_gp_cit
= {
2671 .ct_item_ops
= &target_core_alua_tg_pt_gp_ops
,
2672 .ct_attrs
= target_core_alua_tg_pt_gp_attrs
,
2673 .ct_owner
= THIS_MODULE
,
2676 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2678 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2680 static struct config_group
*target_core_alua_create_tg_pt_gp(
2681 struct config_group
*group
,
2684 struct t10_alua
*alua
= container_of(group
, struct t10_alua
,
2685 alua_tg_pt_gps_group
);
2686 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
2687 struct config_group
*alua_tg_pt_gp_cg
= NULL
;
2688 struct config_item
*alua_tg_pt_gp_ci
= NULL
;
2690 tg_pt_gp
= core_alua_allocate_tg_pt_gp(alua
->t10_dev
, name
, 0);
2694 alua_tg_pt_gp_cg
= &tg_pt_gp
->tg_pt_gp_group
;
2695 alua_tg_pt_gp_ci
= &alua_tg_pt_gp_cg
->cg_item
;
2697 config_group_init_type_name(alua_tg_pt_gp_cg
, name
,
2698 &target_core_alua_tg_pt_gp_cit
);
2700 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2701 " Group: alua/tg_pt_gps/%s\n",
2702 config_item_name(alua_tg_pt_gp_ci
));
2704 return alua_tg_pt_gp_cg
;
2707 static void target_core_alua_drop_tg_pt_gp(
2708 struct config_group
*group
,
2709 struct config_item
*item
)
2711 struct t10_alua_tg_pt_gp
*tg_pt_gp
= container_of(to_config_group(item
),
2712 struct t10_alua_tg_pt_gp
, tg_pt_gp_group
);
2714 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2715 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2716 config_item_name(item
), tg_pt_gp
->tg_pt_gp_id
);
2718 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2719 * -> target_core_alua_tg_pt_gp_release().
2721 config_item_put(item
);
2724 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops
= {
2725 .make_group
= &target_core_alua_create_tg_pt_gp
,
2726 .drop_item
= &target_core_alua_drop_tg_pt_gp
,
2729 TB_CIT_SETUP(dev_alua_tg_pt_gps
, NULL
, &target_core_alua_tg_pt_gps_group_ops
, NULL
);
2731 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2733 /* Start functions for struct config_item_type target_core_alua_cit */
2736 * target_core_alua_cit is a ConfigFS group that lives under
2737 * /sys/kernel/config/target/core/alua. There are default groups
2738 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2739 * target_core_alua_cit in target_core_init_configfs() below.
2741 static struct config_item_type target_core_alua_cit
= {
2742 .ct_item_ops
= NULL
,
2744 .ct_owner
= THIS_MODULE
,
2747 /* End functions for struct config_item_type target_core_alua_cit */
2749 /* Start functions for struct config_item_type tb_dev_stat_cit */
2751 static struct config_group
*target_core_stat_mkdir(
2752 struct config_group
*group
,
2755 return ERR_PTR(-ENOSYS
);
2758 static void target_core_stat_rmdir(
2759 struct config_group
*group
,
2760 struct config_item
*item
)
2765 static struct configfs_group_operations target_core_stat_group_ops
= {
2766 .make_group
= &target_core_stat_mkdir
,
2767 .drop_item
= &target_core_stat_rmdir
,
2770 TB_CIT_SETUP(dev_stat
, NULL
, &target_core_stat_group_ops
, NULL
);
2772 /* End functions for struct config_item_type tb_dev_stat_cit */
2774 /* Start functions for struct config_item_type target_core_hba_cit */
2776 static struct config_group
*target_core_make_subdev(
2777 struct config_group
*group
,
2780 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
2781 struct config_item
*hba_ci
= &group
->cg_item
;
2782 struct se_hba
*hba
= item_to_hba(hba_ci
);
2783 struct target_backend
*tb
= hba
->backend
;
2784 struct se_device
*dev
;
2785 struct config_group
*dev_cg
= NULL
, *tg_pt_gp_cg
= NULL
;
2786 struct config_group
*dev_stat_grp
= NULL
;
2787 int errno
= -ENOMEM
, ret
;
2789 ret
= mutex_lock_interruptible(&hba
->hba_access_mutex
);
2791 return ERR_PTR(ret
);
2793 dev
= target_alloc_device(hba
, name
);
2797 dev_cg
= &dev
->dev_group
;
2799 dev_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 6,
2801 if (!dev_cg
->default_groups
)
2802 goto out_free_device
;
2804 config_group_init_type_name(dev_cg
, name
, &tb
->tb_dev_cit
);
2805 config_group_init_type_name(&dev
->dev_attrib
.da_group
, "attrib",
2806 &tb
->tb_dev_attrib_cit
);
2807 config_group_init_type_name(&dev
->dev_pr_group
, "pr",
2808 &tb
->tb_dev_pr_cit
);
2809 config_group_init_type_name(&dev
->t10_wwn
.t10_wwn_group
, "wwn",
2810 &tb
->tb_dev_wwn_cit
);
2811 config_group_init_type_name(&dev
->t10_alua
.alua_tg_pt_gps_group
,
2812 "alua", &tb
->tb_dev_alua_tg_pt_gps_cit
);
2813 config_group_init_type_name(&dev
->dev_stat_grps
.stat_group
,
2814 "statistics", &tb
->tb_dev_stat_cit
);
2816 dev_cg
->default_groups
[0] = &dev
->dev_attrib
.da_group
;
2817 dev_cg
->default_groups
[1] = &dev
->dev_pr_group
;
2818 dev_cg
->default_groups
[2] = &dev
->t10_wwn
.t10_wwn_group
;
2819 dev_cg
->default_groups
[3] = &dev
->t10_alua
.alua_tg_pt_gps_group
;
2820 dev_cg
->default_groups
[4] = &dev
->dev_stat_grps
.stat_group
;
2821 dev_cg
->default_groups
[5] = NULL
;
2823 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2825 tg_pt_gp
= core_alua_allocate_tg_pt_gp(dev
, "default_tg_pt_gp", 1);
2827 goto out_free_dev_cg_default_groups
;
2828 dev
->t10_alua
.default_tg_pt_gp
= tg_pt_gp
;
2830 tg_pt_gp_cg
= &dev
->t10_alua
.alua_tg_pt_gps_group
;
2831 tg_pt_gp_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
2833 if (!tg_pt_gp_cg
->default_groups
) {
2834 pr_err("Unable to allocate tg_pt_gp_cg->"
2835 "default_groups\n");
2836 goto out_free_tg_pt_gp
;
2839 config_group_init_type_name(&tg_pt_gp
->tg_pt_gp_group
,
2840 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit
);
2841 tg_pt_gp_cg
->default_groups
[0] = &tg_pt_gp
->tg_pt_gp_group
;
2842 tg_pt_gp_cg
->default_groups
[1] = NULL
;
2844 * Add core/$HBA/$DEV/statistics/ default groups
2846 dev_stat_grp
= &dev
->dev_stat_grps
.stat_group
;
2847 dev_stat_grp
->default_groups
= kmalloc(sizeof(struct config_group
*) * 4,
2849 if (!dev_stat_grp
->default_groups
) {
2850 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2851 goto out_free_tg_pt_gp_cg_default_groups
;
2853 target_stat_setup_dev_default_groups(dev
);
2855 mutex_unlock(&hba
->hba_access_mutex
);
2858 out_free_tg_pt_gp_cg_default_groups
:
2859 kfree(tg_pt_gp_cg
->default_groups
);
2861 core_alua_free_tg_pt_gp(tg_pt_gp
);
2862 out_free_dev_cg_default_groups
:
2863 kfree(dev_cg
->default_groups
);
2865 target_free_device(dev
);
2867 mutex_unlock(&hba
->hba_access_mutex
);
2868 return ERR_PTR(errno
);
2871 static void target_core_drop_subdev(
2872 struct config_group
*group
,
2873 struct config_item
*item
)
2875 struct config_group
*dev_cg
= to_config_group(item
);
2876 struct se_device
*dev
=
2877 container_of(dev_cg
, struct se_device
, dev_group
);
2879 struct config_item
*df_item
;
2880 struct config_group
*tg_pt_gp_cg
, *dev_stat_grp
;
2883 hba
= item_to_hba(&dev
->se_hba
->hba_group
.cg_item
);
2885 mutex_lock(&hba
->hba_access_mutex
);
2887 dev_stat_grp
= &dev
->dev_stat_grps
.stat_group
;
2888 for (i
= 0; dev_stat_grp
->default_groups
[i
]; i
++) {
2889 df_item
= &dev_stat_grp
->default_groups
[i
]->cg_item
;
2890 dev_stat_grp
->default_groups
[i
] = NULL
;
2891 config_item_put(df_item
);
2893 kfree(dev_stat_grp
->default_groups
);
2895 tg_pt_gp_cg
= &dev
->t10_alua
.alua_tg_pt_gps_group
;
2896 for (i
= 0; tg_pt_gp_cg
->default_groups
[i
]; i
++) {
2897 df_item
= &tg_pt_gp_cg
->default_groups
[i
]->cg_item
;
2898 tg_pt_gp_cg
->default_groups
[i
] = NULL
;
2899 config_item_put(df_item
);
2901 kfree(tg_pt_gp_cg
->default_groups
);
2903 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2904 * directly from target_core_alua_tg_pt_gp_release().
2906 dev
->t10_alua
.default_tg_pt_gp
= NULL
;
2908 for (i
= 0; dev_cg
->default_groups
[i
]; i
++) {
2909 df_item
= &dev_cg
->default_groups
[i
]->cg_item
;
2910 dev_cg
->default_groups
[i
] = NULL
;
2911 config_item_put(df_item
);
2914 * se_dev is released from target_core_dev_item_ops->release()
2916 config_item_put(item
);
2917 mutex_unlock(&hba
->hba_access_mutex
);
2920 static struct configfs_group_operations target_core_hba_group_ops
= {
2921 .make_group
= target_core_make_subdev
,
2922 .drop_item
= target_core_drop_subdev
,
2926 static inline struct se_hba
*to_hba(struct config_item
*item
)
2928 return container_of(to_config_group(item
), struct se_hba
, hba_group
);
2931 static ssize_t
target_hba_info_show(struct config_item
*item
, char *page
)
2933 struct se_hba
*hba
= to_hba(item
);
2935 return sprintf(page
, "HBA Index: %d plugin: %s version: %s\n",
2936 hba
->hba_id
, hba
->backend
->ops
->name
,
2937 TARGET_CORE_VERSION
);
2940 static ssize_t
target_hba_mode_show(struct config_item
*item
, char *page
)
2942 struct se_hba
*hba
= to_hba(item
);
2945 if (hba
->hba_flags
& HBA_FLAGS_PSCSI_MODE
)
2948 return sprintf(page
, "%d\n", hba_mode
);
2951 static ssize_t
target_hba_mode_store(struct config_item
*item
,
2952 const char *page
, size_t count
)
2954 struct se_hba
*hba
= to_hba(item
);
2955 unsigned long mode_flag
;
2958 if (hba
->backend
->ops
->pmode_enable_hba
== NULL
)
2961 ret
= kstrtoul(page
, 0, &mode_flag
);
2963 pr_err("Unable to extract hba mode flag: %d\n", ret
);
2967 if (hba
->dev_count
) {
2968 pr_err("Unable to set hba_mode with active devices\n");
2972 ret
= hba
->backend
->ops
->pmode_enable_hba(hba
, mode_flag
);
2976 hba
->hba_flags
|= HBA_FLAGS_PSCSI_MODE
;
2978 hba
->hba_flags
&= ~HBA_FLAGS_PSCSI_MODE
;
2983 CONFIGFS_ATTR_RO(target_
, hba_info
);
2984 CONFIGFS_ATTR(target_
, hba_mode
);
2986 static void target_core_hba_release(struct config_item
*item
)
2988 struct se_hba
*hba
= container_of(to_config_group(item
),
2989 struct se_hba
, hba_group
);
2990 core_delete_hba(hba
);
2993 static struct configfs_attribute
*target_core_hba_attrs
[] = {
2994 &target_attr_hba_info
,
2995 &target_attr_hba_mode
,
2999 static struct configfs_item_operations target_core_hba_item_ops
= {
3000 .release
= target_core_hba_release
,
3003 static struct config_item_type target_core_hba_cit
= {
3004 .ct_item_ops
= &target_core_hba_item_ops
,
3005 .ct_group_ops
= &target_core_hba_group_ops
,
3006 .ct_attrs
= target_core_hba_attrs
,
3007 .ct_owner
= THIS_MODULE
,
3010 static struct config_group
*target_core_call_addhbatotarget(
3011 struct config_group
*group
,
3014 char *se_plugin_str
, *str
, *str2
;
3016 char buf
[TARGET_CORE_NAME_MAX_LEN
];
3017 unsigned long plugin_dep_id
= 0;
3020 memset(buf
, 0, TARGET_CORE_NAME_MAX_LEN
);
3021 if (strlen(name
) >= TARGET_CORE_NAME_MAX_LEN
) {
3022 pr_err("Passed *name strlen(): %d exceeds"
3023 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name
),
3024 TARGET_CORE_NAME_MAX_LEN
);
3025 return ERR_PTR(-ENAMETOOLONG
);
3027 snprintf(buf
, TARGET_CORE_NAME_MAX_LEN
, "%s", name
);
3029 str
= strstr(buf
, "_");
3031 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3032 return ERR_PTR(-EINVAL
);
3034 se_plugin_str
= buf
;
3036 * Special case for subsystem plugins that have "_" in their names.
3037 * Namely rd_direct and rd_mcp..
3039 str2
= strstr(str
+1, "_");
3041 *str2
= '\0'; /* Terminate for *se_plugin_str */
3042 str2
++; /* Skip to start of plugin dependent ID */
3045 *str
= '\0'; /* Terminate for *se_plugin_str */
3046 str
++; /* Skip to start of plugin dependent ID */
3049 ret
= kstrtoul(str
, 0, &plugin_dep_id
);
3051 pr_err("kstrtoul() returned %d for"
3052 " plugin_dep_id\n", ret
);
3053 return ERR_PTR(ret
);
3056 * Load up TCM subsystem plugins if they have not already been loaded.
3058 transport_subsystem_check_init();
3060 hba
= core_alloc_hba(se_plugin_str
, plugin_dep_id
, 0);
3062 return ERR_CAST(hba
);
3064 config_group_init_type_name(&hba
->hba_group
, name
,
3065 &target_core_hba_cit
);
3067 return &hba
->hba_group
;
3070 static void target_core_call_delhbafromtarget(
3071 struct config_group
*group
,
3072 struct config_item
*item
)
3075 * core_delete_hba() is called from target_core_hba_item_ops->release()
3076 * -> target_core_hba_release()
3078 config_item_put(item
);
3081 static struct configfs_group_operations target_core_group_ops
= {
3082 .make_group
= target_core_call_addhbatotarget
,
3083 .drop_item
= target_core_call_delhbafromtarget
,
3086 static struct config_item_type target_core_cit
= {
3087 .ct_item_ops
= NULL
,
3088 .ct_group_ops
= &target_core_group_ops
,
3090 .ct_owner
= THIS_MODULE
,
3093 /* Stop functions for struct config_item_type target_core_hba_cit */
3095 void target_setup_backend_cits(struct target_backend
*tb
)
3097 target_core_setup_dev_cit(tb
);
3098 target_core_setup_dev_attrib_cit(tb
);
3099 target_core_setup_dev_pr_cit(tb
);
3100 target_core_setup_dev_wwn_cit(tb
);
3101 target_core_setup_dev_alua_tg_pt_gps_cit(tb
);
3102 target_core_setup_dev_stat_cit(tb
);
3105 static int __init
target_core_init_configfs(void)
3107 struct config_group
*target_cg
, *hba_cg
= NULL
, *alua_cg
= NULL
;
3108 struct config_group
*lu_gp_cg
= NULL
;
3109 struct configfs_subsystem
*subsys
= &target_core_fabrics
;
3110 struct t10_alua_lu_gp
*lu_gp
;
3113 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3114 " Engine: %s on %s/%s on "UTS_RELEASE
"\n",
3115 TARGET_CORE_VERSION
, utsname()->sysname
, utsname()->machine
);
3117 config_group_init(&subsys
->su_group
);
3118 mutex_init(&subsys
->su_mutex
);
3120 ret
= init_se_kmem_caches();
3124 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3125 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3127 target_cg
= &subsys
->su_group
;
3128 target_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
3130 if (!target_cg
->default_groups
) {
3131 pr_err("Unable to allocate target_cg->default_groups\n");
3136 config_group_init_type_name(&target_core_hbagroup
,
3137 "core", &target_core_cit
);
3138 target_cg
->default_groups
[0] = &target_core_hbagroup
;
3139 target_cg
->default_groups
[1] = NULL
;
3141 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3143 hba_cg
= &target_core_hbagroup
;
3144 hba_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
3146 if (!hba_cg
->default_groups
) {
3147 pr_err("Unable to allocate hba_cg->default_groups\n");
3151 config_group_init_type_name(&alua_group
,
3152 "alua", &target_core_alua_cit
);
3153 hba_cg
->default_groups
[0] = &alua_group
;
3154 hba_cg
->default_groups
[1] = NULL
;
3156 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3157 * groups under /sys/kernel/config/target/core/alua/
3159 alua_cg
= &alua_group
;
3160 alua_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
3162 if (!alua_cg
->default_groups
) {
3163 pr_err("Unable to allocate alua_cg->default_groups\n");
3168 config_group_init_type_name(&alua_lu_gps_group
,
3169 "lu_gps", &target_core_alua_lu_gps_cit
);
3170 alua_cg
->default_groups
[0] = &alua_lu_gps_group
;
3171 alua_cg
->default_groups
[1] = NULL
;
3173 * Add core/alua/lu_gps/default_lu_gp
3175 lu_gp
= core_alua_allocate_lu_gp("default_lu_gp", 1);
3176 if (IS_ERR(lu_gp
)) {
3181 lu_gp_cg
= &alua_lu_gps_group
;
3182 lu_gp_cg
->default_groups
= kmalloc(sizeof(struct config_group
*) * 2,
3184 if (!lu_gp_cg
->default_groups
) {
3185 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
3190 config_group_init_type_name(&lu_gp
->lu_gp_group
, "default_lu_gp",
3191 &target_core_alua_lu_gp_cit
);
3192 lu_gp_cg
->default_groups
[0] = &lu_gp
->lu_gp_group
;
3193 lu_gp_cg
->default_groups
[1] = NULL
;
3194 default_lu_gp
= lu_gp
;
3196 * Register the target_core_mod subsystem with configfs.
3198 ret
= configfs_register_subsystem(subsys
);
3200 pr_err("Error %d while registering subsystem %s\n",
3201 ret
, subsys
->su_group
.cg_item
.ci_namebuf
);
3204 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3205 " Infrastructure: "TARGET_CORE_VERSION
" on %s/%s"
3206 " on "UTS_RELEASE
"\n", utsname()->sysname
, utsname()->machine
);
3208 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3210 ret
= rd_module_init();
3214 ret
= core_dev_setup_virtual_lun0();
3218 ret
= target_xcopy_setup_pt();
3225 configfs_unregister_subsystem(subsys
);
3226 core_dev_release_virtual_lun0();
3229 if (default_lu_gp
) {
3230 core_alua_free_lu_gp(default_lu_gp
);
3231 default_lu_gp
= NULL
;
3234 kfree(lu_gp_cg
->default_groups
);
3236 kfree(alua_cg
->default_groups
);
3238 kfree(hba_cg
->default_groups
);
3239 kfree(target_cg
->default_groups
);
3240 release_se_kmem_caches();
3244 static void __exit
target_core_exit_configfs(void)
3246 struct config_group
*hba_cg
, *alua_cg
, *lu_gp_cg
;
3247 struct config_item
*item
;
3250 lu_gp_cg
= &alua_lu_gps_group
;
3251 for (i
= 0; lu_gp_cg
->default_groups
[i
]; i
++) {
3252 item
= &lu_gp_cg
->default_groups
[i
]->cg_item
;
3253 lu_gp_cg
->default_groups
[i
] = NULL
;
3254 config_item_put(item
);
3256 kfree(lu_gp_cg
->default_groups
);
3257 lu_gp_cg
->default_groups
= NULL
;
3259 alua_cg
= &alua_group
;
3260 for (i
= 0; alua_cg
->default_groups
[i
]; i
++) {
3261 item
= &alua_cg
->default_groups
[i
]->cg_item
;
3262 alua_cg
->default_groups
[i
] = NULL
;
3263 config_item_put(item
);
3265 kfree(alua_cg
->default_groups
);
3266 alua_cg
->default_groups
= NULL
;
3268 hba_cg
= &target_core_hbagroup
;
3269 for (i
= 0; hba_cg
->default_groups
[i
]; i
++) {
3270 item
= &hba_cg
->default_groups
[i
]->cg_item
;
3271 hba_cg
->default_groups
[i
] = NULL
;
3272 config_item_put(item
);
3274 kfree(hba_cg
->default_groups
);
3275 hba_cg
->default_groups
= NULL
;
3277 * We expect subsys->su_group.default_groups to be released
3278 * by configfs subsystem provider logic..
3280 configfs_unregister_subsystem(&target_core_fabrics
);
3281 kfree(target_core_fabrics
.su_group
.default_groups
);
3283 core_alua_free_lu_gp(default_lu_gp
);
3284 default_lu_gp
= NULL
;
3286 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3287 " Infrastructure\n");
3289 core_dev_release_virtual_lun0();
3291 target_xcopy_release_pt();
3292 release_se_kmem_caches();
3295 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3296 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3297 MODULE_LICENSE("GPL");
3299 module_init(target_core_init_configfs
);
3300 module_exit(target_core_exit_configfs
);