Linux 5.1.15
[linux/fpc-iii.git] / drivers / target / target_core_configfs.c
blobfc5ef31f5ba82b6b20e793f42464ea1bbe2d95ac
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>
28 #include <linux/fs.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) \
52 { \
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) \
64 { \
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,
93 char *page)
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 char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
103 static char db_root_stage[DB_ROOT_LEN];
105 static ssize_t target_core_item_dbroot_show(struct config_item *item,
106 char *page)
108 return sprintf(page, "%s\n", db_root);
111 static ssize_t target_core_item_dbroot_store(struct config_item *item,
112 const char *page, size_t count)
114 ssize_t read_bytes;
115 struct file *fp;
117 mutex_lock(&g_tf_lock);
118 if (!list_empty(&g_tf_list)) {
119 mutex_unlock(&g_tf_lock);
120 pr_err("db_root: cannot be changed: target drivers registered");
121 return -EINVAL;
124 if (count > (DB_ROOT_LEN - 1)) {
125 mutex_unlock(&g_tf_lock);
126 pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
127 (int)count, DB_ROOT_LEN - 1);
128 return -EINVAL;
131 read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
132 if (!read_bytes) {
133 mutex_unlock(&g_tf_lock);
134 return -EINVAL;
136 if (db_root_stage[read_bytes - 1] == '\n')
137 db_root_stage[read_bytes - 1] = '\0';
139 /* validate new db root before accepting it */
140 fp = filp_open(db_root_stage, O_RDONLY, 0);
141 if (IS_ERR(fp)) {
142 mutex_unlock(&g_tf_lock);
143 pr_err("db_root: cannot open: %s\n", db_root_stage);
144 return -EINVAL;
146 if (!S_ISDIR(file_inode(fp)->i_mode)) {
147 filp_close(fp, NULL);
148 mutex_unlock(&g_tf_lock);
149 pr_err("db_root: not a directory: %s\n", db_root_stage);
150 return -EINVAL;
152 filp_close(fp, NULL);
154 strncpy(db_root, db_root_stage, read_bytes);
156 mutex_unlock(&g_tf_lock);
158 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
160 return read_bytes;
163 CONFIGFS_ATTR(target_core_item_, dbroot);
165 static struct target_fabric_configfs *target_core_get_fabric(
166 const char *name)
168 struct target_fabric_configfs *tf;
170 if (!name)
171 return NULL;
173 mutex_lock(&g_tf_lock);
174 list_for_each_entry(tf, &g_tf_list, tf_list) {
175 const char *cmp_name = tf->tf_ops->fabric_alias;
176 if (!cmp_name)
177 cmp_name = tf->tf_ops->fabric_name;
178 if (!strcmp(cmp_name, name)) {
179 atomic_inc(&tf->tf_access_cnt);
180 mutex_unlock(&g_tf_lock);
181 return tf;
184 mutex_unlock(&g_tf_lock);
186 return NULL;
190 * Called from struct target_core_group_ops->make_group()
192 static struct config_group *target_core_register_fabric(
193 struct config_group *group,
194 const char *name)
196 struct target_fabric_configfs *tf;
197 int ret;
199 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
200 " %s\n", group, name);
202 tf = target_core_get_fabric(name);
203 if (!tf) {
204 pr_debug("target_core_register_fabric() trying autoload for %s\n",
205 name);
208 * Below are some hardcoded request_module() calls to automatically
209 * local fabric modules when the following is called:
211 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
213 * Note that this does not limit which TCM fabric module can be
214 * registered, but simply provids auto loading logic for modules with
215 * mkdir(2) system calls with known TCM fabric modules.
218 if (!strncmp(name, "iscsi", 5)) {
220 * Automatically load the LIO Target fabric module when the
221 * following is called:
223 * mkdir -p $CONFIGFS/target/iscsi
225 ret = request_module("iscsi_target_mod");
226 if (ret < 0) {
227 pr_debug("request_module() failed for"
228 " iscsi_target_mod.ko: %d\n", ret);
229 return ERR_PTR(-EINVAL);
231 } else if (!strncmp(name, "loopback", 8)) {
233 * Automatically load the tcm_loop fabric module when the
234 * following is called:
236 * mkdir -p $CONFIGFS/target/loopback
238 ret = request_module("tcm_loop");
239 if (ret < 0) {
240 pr_debug("request_module() failed for"
241 " tcm_loop.ko: %d\n", ret);
242 return ERR_PTR(-EINVAL);
246 tf = target_core_get_fabric(name);
249 if (!tf) {
250 pr_debug("target_core_get_fabric() failed for %s\n",
251 name);
252 return ERR_PTR(-EINVAL);
254 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
255 " %s\n", tf->tf_ops->fabric_name);
257 * On a successful target_core_get_fabric() look, the returned
258 * struct target_fabric_configfs *tf will contain a usage reference.
260 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
261 &tf->tf_wwn_cit);
263 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
265 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
266 &tf->tf_discovery_cit);
267 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
269 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
270 config_item_name(&tf->tf_group.cg_item));
271 return &tf->tf_group;
275 * Called from struct target_core_group_ops->drop_item()
277 static void target_core_deregister_fabric(
278 struct config_group *group,
279 struct config_item *item)
281 struct target_fabric_configfs *tf = container_of(
282 to_config_group(item), struct target_fabric_configfs, tf_group);
284 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
285 " tf list\n", config_item_name(item));
287 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
288 " %s\n", tf->tf_ops->fabric_name);
289 atomic_dec(&tf->tf_access_cnt);
291 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
292 " %s\n", config_item_name(item));
294 configfs_remove_default_groups(&tf->tf_group);
295 config_item_put(item);
298 static struct configfs_group_operations target_core_fabric_group_ops = {
299 .make_group = &target_core_register_fabric,
300 .drop_item = &target_core_deregister_fabric,
304 * All item attributes appearing in /sys/kernel/target/ appear here.
306 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
307 &target_core_item_attr_version,
308 &target_core_item_attr_dbroot,
309 NULL,
313 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
315 static const struct config_item_type target_core_fabrics_item = {
316 .ct_group_ops = &target_core_fabric_group_ops,
317 .ct_attrs = target_core_fabric_item_attrs,
318 .ct_owner = THIS_MODULE,
321 static struct configfs_subsystem target_core_fabrics = {
322 .su_group = {
323 .cg_item = {
324 .ci_namebuf = "target",
325 .ci_type = &target_core_fabrics_item,
330 int target_depend_item(struct config_item *item)
332 return configfs_depend_item(&target_core_fabrics, item);
334 EXPORT_SYMBOL(target_depend_item);
336 void target_undepend_item(struct config_item *item)
338 return configfs_undepend_item(item);
340 EXPORT_SYMBOL(target_undepend_item);
342 /*##############################################################################
343 // Start functions called by external Target Fabrics Modules
344 //############################################################################*/
346 static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
348 if (tfo->fabric_alias) {
349 if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
350 pr_err("Passed alias: %s exceeds "
351 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
352 return -EINVAL;
355 if (!tfo->fabric_name) {
356 pr_err("Missing tfo->fabric_name\n");
357 return -EINVAL;
359 if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
360 pr_err("Passed name: %s exceeds "
361 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
362 return -EINVAL;
364 if (!tfo->tpg_get_wwn) {
365 pr_err("Missing tfo->tpg_get_wwn()\n");
366 return -EINVAL;
368 if (!tfo->tpg_get_tag) {
369 pr_err("Missing tfo->tpg_get_tag()\n");
370 return -EINVAL;
372 if (!tfo->tpg_check_demo_mode) {
373 pr_err("Missing tfo->tpg_check_demo_mode()\n");
374 return -EINVAL;
376 if (!tfo->tpg_check_demo_mode_cache) {
377 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
378 return -EINVAL;
380 if (!tfo->tpg_check_demo_mode_write_protect) {
381 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
382 return -EINVAL;
384 if (!tfo->tpg_check_prod_mode_write_protect) {
385 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
386 return -EINVAL;
388 if (!tfo->tpg_get_inst_index) {
389 pr_err("Missing tfo->tpg_get_inst_index()\n");
390 return -EINVAL;
392 if (!tfo->release_cmd) {
393 pr_err("Missing tfo->release_cmd()\n");
394 return -EINVAL;
396 if (!tfo->sess_get_index) {
397 pr_err("Missing tfo->sess_get_index()\n");
398 return -EINVAL;
400 if (!tfo->write_pending) {
401 pr_err("Missing tfo->write_pending()\n");
402 return -EINVAL;
404 if (!tfo->set_default_node_attributes) {
405 pr_err("Missing tfo->set_default_node_attributes()\n");
406 return -EINVAL;
408 if (!tfo->get_cmd_state) {
409 pr_err("Missing tfo->get_cmd_state()\n");
410 return -EINVAL;
412 if (!tfo->queue_data_in) {
413 pr_err("Missing tfo->queue_data_in()\n");
414 return -EINVAL;
416 if (!tfo->queue_status) {
417 pr_err("Missing tfo->queue_status()\n");
418 return -EINVAL;
420 if (!tfo->queue_tm_rsp) {
421 pr_err("Missing tfo->queue_tm_rsp()\n");
422 return -EINVAL;
424 if (!tfo->aborted_task) {
425 pr_err("Missing tfo->aborted_task()\n");
426 return -EINVAL;
428 if (!tfo->check_stop_free) {
429 pr_err("Missing tfo->check_stop_free()\n");
430 return -EINVAL;
433 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
434 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
435 * target_core_fabric_configfs.c WWN+TPG group context code.
437 if (!tfo->fabric_make_wwn) {
438 pr_err("Missing tfo->fabric_make_wwn()\n");
439 return -EINVAL;
441 if (!tfo->fabric_drop_wwn) {
442 pr_err("Missing tfo->fabric_drop_wwn()\n");
443 return -EINVAL;
445 if (!tfo->fabric_make_tpg) {
446 pr_err("Missing tfo->fabric_make_tpg()\n");
447 return -EINVAL;
449 if (!tfo->fabric_drop_tpg) {
450 pr_err("Missing tfo->fabric_drop_tpg()\n");
451 return -EINVAL;
454 return 0;
457 int target_register_template(const struct target_core_fabric_ops *fo)
459 struct target_fabric_configfs *tf;
460 int ret;
462 ret = target_fabric_tf_ops_check(fo);
463 if (ret)
464 return ret;
466 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
467 if (!tf) {
468 pr_err("%s: could not allocate memory!\n", __func__);
469 return -ENOMEM;
472 INIT_LIST_HEAD(&tf->tf_list);
473 atomic_set(&tf->tf_access_cnt, 0);
474 tf->tf_ops = fo;
475 target_fabric_setup_cits(tf);
477 mutex_lock(&g_tf_lock);
478 list_add_tail(&tf->tf_list, &g_tf_list);
479 mutex_unlock(&g_tf_lock);
481 return 0;
483 EXPORT_SYMBOL(target_register_template);
485 void target_unregister_template(const struct target_core_fabric_ops *fo)
487 struct target_fabric_configfs *t;
489 mutex_lock(&g_tf_lock);
490 list_for_each_entry(t, &g_tf_list, tf_list) {
491 if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
492 BUG_ON(atomic_read(&t->tf_access_cnt));
493 list_del(&t->tf_list);
494 mutex_unlock(&g_tf_lock);
496 * Wait for any outstanding fabric se_deve_entry->rcu_head
497 * callbacks to complete post kfree_rcu(), before allowing
498 * fabric driver unload of TFO->module to proceed.
500 rcu_barrier();
501 kfree(t);
502 return;
505 mutex_unlock(&g_tf_lock);
507 EXPORT_SYMBOL(target_unregister_template);
509 /*##############################################################################
510 // Stop functions called by external Target Fabrics Modules
511 //############################################################################*/
513 static inline struct se_dev_attrib *to_attrib(struct config_item *item)
515 return container_of(to_config_group(item), struct se_dev_attrib,
516 da_group);
519 /* Start functions for struct config_item_type tb_dev_attrib_cit */
520 #define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
521 static ssize_t _name##_show(struct config_item *item, char *page) \
523 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
526 DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
527 DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
528 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
529 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
530 DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
531 DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
532 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
533 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
534 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
535 DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
536 DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
537 DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
538 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
539 DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
540 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
541 DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
542 DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
543 DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
544 DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
545 DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
546 DEF_CONFIGFS_ATTRIB_SHOW(block_size);
547 DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
548 DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
549 DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
550 DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
551 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
552 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
553 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
554 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
555 DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
556 DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
558 #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
559 static ssize_t _name##_store(struct config_item *item, const char *page,\
560 size_t count) \
562 struct se_dev_attrib *da = to_attrib(item); \
563 u32 val; \
564 int ret; \
566 ret = kstrtou32(page, 0, &val); \
567 if (ret < 0) \
568 return ret; \
569 da->_name = val; \
570 return count; \
573 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
574 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
575 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
576 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
577 DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
579 #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
580 static ssize_t _name##_store(struct config_item *item, const char *page, \
581 size_t count) \
583 struct se_dev_attrib *da = to_attrib(item); \
584 bool flag; \
585 int ret; \
587 ret = strtobool(page, &flag); \
588 if (ret < 0) \
589 return ret; \
590 da->_name = flag; \
591 return count; \
594 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
595 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
596 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
597 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
598 DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
599 DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
601 #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
602 static ssize_t _name##_store(struct config_item *item, const char *page,\
603 size_t count) \
605 printk_once(KERN_WARNING \
606 "ignoring deprecated %s attribute\n", \
607 __stringify(_name)); \
608 return count; \
611 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
612 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
614 static void dev_set_t10_wwn_model_alias(struct se_device *dev)
616 const char *configname;
618 configname = config_item_name(&dev->dev_group.cg_item);
619 if (strlen(configname) >= INQUIRY_MODEL_LEN) {
620 pr_warn("dev[%p]: Backstore name '%s' is too long for "
621 "INQUIRY_MODEL, truncating to 15 characters\n", dev,
622 configname);
625 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
626 * here without potentially breaking existing setups, so continue to
627 * truncate one byte shorter than what can be carried in INQUIRY.
629 strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
632 static ssize_t emulate_model_alias_store(struct config_item *item,
633 const char *page, size_t count)
635 struct se_dev_attrib *da = to_attrib(item);
636 struct se_device *dev = da->da_dev;
637 bool flag;
638 int ret;
640 if (dev->export_count) {
641 pr_err("dev[%p]: Unable to change model alias"
642 " while export_count is %d\n",
643 dev, dev->export_count);
644 return -EINVAL;
647 ret = strtobool(page, &flag);
648 if (ret < 0)
649 return ret;
651 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
652 if (flag) {
653 dev_set_t10_wwn_model_alias(dev);
654 } else {
655 strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
656 sizeof(dev->t10_wwn.model));
658 da->emulate_model_alias = flag;
659 return count;
662 static ssize_t emulate_write_cache_store(struct config_item *item,
663 const char *page, size_t count)
665 struct se_dev_attrib *da = to_attrib(item);
666 bool flag;
667 int ret;
669 ret = strtobool(page, &flag);
670 if (ret < 0)
671 return ret;
673 if (flag && da->da_dev->transport->get_write_cache) {
674 pr_err("emulate_write_cache not supported for this device\n");
675 return -EINVAL;
678 da->emulate_write_cache = flag;
679 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
680 da->da_dev, flag);
681 return count;
684 static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
685 const char *page, size_t count)
687 struct se_dev_attrib *da = to_attrib(item);
688 u32 val;
689 int ret;
691 ret = kstrtou32(page, 0, &val);
692 if (ret < 0)
693 return ret;
695 if (val != 0 && val != 1 && val != 2) {
696 pr_err("Illegal value %d\n", val);
697 return -EINVAL;
700 if (da->da_dev->export_count) {
701 pr_err("dev[%p]: Unable to change SE Device"
702 " UA_INTRLCK_CTRL while export_count is %d\n",
703 da->da_dev, da->da_dev->export_count);
704 return -EINVAL;
706 da->emulate_ua_intlck_ctrl = val;
707 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
708 da->da_dev, val);
709 return count;
712 static ssize_t emulate_tas_store(struct config_item *item,
713 const char *page, size_t count)
715 struct se_dev_attrib *da = to_attrib(item);
716 bool flag;
717 int ret;
719 ret = strtobool(page, &flag);
720 if (ret < 0)
721 return ret;
723 if (da->da_dev->export_count) {
724 pr_err("dev[%p]: Unable to change SE Device TAS while"
725 " export_count is %d\n",
726 da->da_dev, da->da_dev->export_count);
727 return -EINVAL;
729 da->emulate_tas = flag;
730 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
731 da->da_dev, flag ? "Enabled" : "Disabled");
733 return count;
736 static ssize_t emulate_tpu_store(struct config_item *item,
737 const char *page, size_t count)
739 struct se_dev_attrib *da = to_attrib(item);
740 bool flag;
741 int ret;
743 ret = strtobool(page, &flag);
744 if (ret < 0)
745 return ret;
748 * We expect this value to be non-zero when generic Block Layer
749 * Discard supported is detected iblock_create_virtdevice().
751 if (flag && !da->max_unmap_block_desc_count) {
752 pr_err("Generic Block Discard not supported\n");
753 return -ENOSYS;
756 da->emulate_tpu = flag;
757 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
758 da->da_dev, flag);
759 return count;
762 static ssize_t emulate_tpws_store(struct config_item *item,
763 const char *page, size_t count)
765 struct se_dev_attrib *da = to_attrib(item);
766 bool flag;
767 int ret;
769 ret = strtobool(page, &flag);
770 if (ret < 0)
771 return ret;
774 * We expect this value to be non-zero when generic Block Layer
775 * Discard supported is detected iblock_create_virtdevice().
777 if (flag && !da->max_unmap_block_desc_count) {
778 pr_err("Generic Block Discard not supported\n");
779 return -ENOSYS;
782 da->emulate_tpws = flag;
783 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
784 da->da_dev, flag);
785 return count;
788 static ssize_t pi_prot_type_store(struct config_item *item,
789 const char *page, size_t count)
791 struct se_dev_attrib *da = to_attrib(item);
792 int old_prot = da->pi_prot_type, ret;
793 struct se_device *dev = da->da_dev;
794 u32 flag;
796 ret = kstrtou32(page, 0, &flag);
797 if (ret < 0)
798 return ret;
800 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
801 pr_err("Illegal value %d for pi_prot_type\n", flag);
802 return -EINVAL;
804 if (flag == 2) {
805 pr_err("DIF TYPE2 protection currently not supported\n");
806 return -ENOSYS;
808 if (da->hw_pi_prot_type) {
809 pr_warn("DIF protection enabled on underlying hardware,"
810 " ignoring\n");
811 return count;
813 if (!dev->transport->init_prot || !dev->transport->free_prot) {
814 /* 0 is only allowed value for non-supporting backends */
815 if (flag == 0)
816 return count;
818 pr_err("DIF protection not supported by backend: %s\n",
819 dev->transport->name);
820 return -ENOSYS;
822 if (!target_dev_configured(dev)) {
823 pr_err("DIF protection requires device to be configured\n");
824 return -ENODEV;
826 if (dev->export_count) {
827 pr_err("dev[%p]: Unable to change SE Device PROT type while"
828 " export_count is %d\n", dev, dev->export_count);
829 return -EINVAL;
832 da->pi_prot_type = flag;
834 if (flag && !old_prot) {
835 ret = dev->transport->init_prot(dev);
836 if (ret) {
837 da->pi_prot_type = old_prot;
838 da->pi_prot_verify = (bool) da->pi_prot_type;
839 return ret;
842 } else if (!flag && old_prot) {
843 dev->transport->free_prot(dev);
846 da->pi_prot_verify = (bool) da->pi_prot_type;
847 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
848 return count;
851 /* always zero, but attr needs to remain RW to avoid userspace breakage */
852 static ssize_t pi_prot_format_show(struct config_item *item, char *page)
854 return snprintf(page, PAGE_SIZE, "0\n");
857 static ssize_t pi_prot_format_store(struct config_item *item,
858 const char *page, size_t count)
860 struct se_dev_attrib *da = to_attrib(item);
861 struct se_device *dev = da->da_dev;
862 bool flag;
863 int ret;
865 ret = strtobool(page, &flag);
866 if (ret < 0)
867 return ret;
869 if (!flag)
870 return count;
872 if (!dev->transport->format_prot) {
873 pr_err("DIF protection format not supported by backend %s\n",
874 dev->transport->name);
875 return -ENOSYS;
877 if (!target_dev_configured(dev)) {
878 pr_err("DIF protection format requires device to be configured\n");
879 return -ENODEV;
881 if (dev->export_count) {
882 pr_err("dev[%p]: Unable to format SE Device PROT type while"
883 " export_count is %d\n", dev, dev->export_count);
884 return -EINVAL;
887 ret = dev->transport->format_prot(dev);
888 if (ret)
889 return ret;
891 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
892 return count;
895 static ssize_t pi_prot_verify_store(struct config_item *item,
896 const char *page, size_t count)
898 struct se_dev_attrib *da = to_attrib(item);
899 bool flag;
900 int ret;
902 ret = strtobool(page, &flag);
903 if (ret < 0)
904 return ret;
906 if (!flag) {
907 da->pi_prot_verify = flag;
908 return count;
910 if (da->hw_pi_prot_type) {
911 pr_warn("DIF protection enabled on underlying hardware,"
912 " ignoring\n");
913 return count;
915 if (!da->pi_prot_type) {
916 pr_warn("DIF protection not supported by backend, ignoring\n");
917 return count;
919 da->pi_prot_verify = flag;
921 return count;
924 static ssize_t force_pr_aptpl_store(struct config_item *item,
925 const char *page, size_t count)
927 struct se_dev_attrib *da = to_attrib(item);
928 bool flag;
929 int ret;
931 ret = strtobool(page, &flag);
932 if (ret < 0)
933 return ret;
934 if (da->da_dev->export_count) {
935 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
936 " export_count is %d\n",
937 da->da_dev, da->da_dev->export_count);
938 return -EINVAL;
941 da->force_pr_aptpl = flag;
942 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
943 return count;
946 static ssize_t emulate_rest_reord_store(struct config_item *item,
947 const char *page, size_t count)
949 struct se_dev_attrib *da = to_attrib(item);
950 bool flag;
951 int ret;
953 ret = strtobool(page, &flag);
954 if (ret < 0)
955 return ret;
957 if (flag != 0) {
958 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
959 " reordering not implemented\n", da->da_dev);
960 return -ENOSYS;
962 da->emulate_rest_reord = flag;
963 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
964 da->da_dev, flag);
965 return count;
968 static ssize_t unmap_zeroes_data_store(struct config_item *item,
969 const char *page, size_t count)
971 struct se_dev_attrib *da = to_attrib(item);
972 bool flag;
973 int ret;
975 ret = strtobool(page, &flag);
976 if (ret < 0)
977 return ret;
979 if (da->da_dev->export_count) {
980 pr_err("dev[%p]: Unable to change SE Device"
981 " unmap_zeroes_data while export_count is %d\n",
982 da->da_dev, da->da_dev->export_count);
983 return -EINVAL;
986 * We expect this value to be non-zero when generic Block Layer
987 * Discard supported is detected iblock_configure_device().
989 if (flag && !da->max_unmap_block_desc_count) {
990 pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
991 " because max_unmap_block_desc_count is zero\n",
992 da->da_dev);
993 return -ENOSYS;
995 da->unmap_zeroes_data = flag;
996 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
997 da->da_dev, flag);
998 return count;
1002 * Note, this can only be called on unexported SE Device Object.
1004 static ssize_t queue_depth_store(struct config_item *item,
1005 const char *page, size_t count)
1007 struct se_dev_attrib *da = to_attrib(item);
1008 struct se_device *dev = da->da_dev;
1009 u32 val;
1010 int ret;
1012 ret = kstrtou32(page, 0, &val);
1013 if (ret < 0)
1014 return ret;
1016 if (dev->export_count) {
1017 pr_err("dev[%p]: Unable to change SE Device TCQ while"
1018 " export_count is %d\n",
1019 dev, dev->export_count);
1020 return -EINVAL;
1022 if (!val) {
1023 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
1024 return -EINVAL;
1027 if (val > dev->dev_attrib.queue_depth) {
1028 if (val > dev->dev_attrib.hw_queue_depth) {
1029 pr_err("dev[%p]: Passed queue_depth:"
1030 " %u exceeds TCM/SE_Device MAX"
1031 " TCQ: %u\n", dev, val,
1032 dev->dev_attrib.hw_queue_depth);
1033 return -EINVAL;
1036 da->queue_depth = dev->queue_depth = val;
1037 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
1038 return count;
1041 static ssize_t optimal_sectors_store(struct config_item *item,
1042 const char *page, size_t count)
1044 struct se_dev_attrib *da = to_attrib(item);
1045 u32 val;
1046 int ret;
1048 ret = kstrtou32(page, 0, &val);
1049 if (ret < 0)
1050 return ret;
1052 if (da->da_dev->export_count) {
1053 pr_err("dev[%p]: Unable to change SE Device"
1054 " optimal_sectors while export_count is %d\n",
1055 da->da_dev, da->da_dev->export_count);
1056 return -EINVAL;
1058 if (val > da->hw_max_sectors) {
1059 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1060 " greater than hw_max_sectors: %u\n",
1061 da->da_dev, val, da->hw_max_sectors);
1062 return -EINVAL;
1065 da->optimal_sectors = val;
1066 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1067 da->da_dev, val);
1068 return count;
1071 static ssize_t block_size_store(struct config_item *item,
1072 const char *page, size_t count)
1074 struct se_dev_attrib *da = to_attrib(item);
1075 u32 val;
1076 int ret;
1078 ret = kstrtou32(page, 0, &val);
1079 if (ret < 0)
1080 return ret;
1082 if (da->da_dev->export_count) {
1083 pr_err("dev[%p]: Unable to change SE Device block_size"
1084 " while export_count is %d\n",
1085 da->da_dev, da->da_dev->export_count);
1086 return -EINVAL;
1089 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1090 pr_err("dev[%p]: Illegal value for block_device: %u"
1091 " for SE device, must be 512, 1024, 2048 or 4096\n",
1092 da->da_dev, val);
1093 return -EINVAL;
1096 da->block_size = val;
1097 if (da->max_bytes_per_io)
1098 da->hw_max_sectors = da->max_bytes_per_io / val;
1100 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1101 da->da_dev, val);
1102 return count;
1105 static ssize_t alua_support_show(struct config_item *item, char *page)
1107 struct se_dev_attrib *da = to_attrib(item);
1108 u8 flags = da->da_dev->transport->transport_flags;
1110 return snprintf(page, PAGE_SIZE, "%d\n",
1111 flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1114 static ssize_t pgr_support_show(struct config_item *item, char *page)
1116 struct se_dev_attrib *da = to_attrib(item);
1117 u8 flags = da->da_dev->transport->transport_flags;
1119 return snprintf(page, PAGE_SIZE, "%d\n",
1120 flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1123 CONFIGFS_ATTR(, emulate_model_alias);
1124 CONFIGFS_ATTR(, emulate_dpo);
1125 CONFIGFS_ATTR(, emulate_fua_write);
1126 CONFIGFS_ATTR(, emulate_fua_read);
1127 CONFIGFS_ATTR(, emulate_write_cache);
1128 CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1129 CONFIGFS_ATTR(, emulate_tas);
1130 CONFIGFS_ATTR(, emulate_tpu);
1131 CONFIGFS_ATTR(, emulate_tpws);
1132 CONFIGFS_ATTR(, emulate_caw);
1133 CONFIGFS_ATTR(, emulate_3pc);
1134 CONFIGFS_ATTR(, emulate_pr);
1135 CONFIGFS_ATTR(, pi_prot_type);
1136 CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1137 CONFIGFS_ATTR(, pi_prot_format);
1138 CONFIGFS_ATTR(, pi_prot_verify);
1139 CONFIGFS_ATTR(, enforce_pr_isids);
1140 CONFIGFS_ATTR(, is_nonrot);
1141 CONFIGFS_ATTR(, emulate_rest_reord);
1142 CONFIGFS_ATTR(, force_pr_aptpl);
1143 CONFIGFS_ATTR_RO(, hw_block_size);
1144 CONFIGFS_ATTR(, block_size);
1145 CONFIGFS_ATTR_RO(, hw_max_sectors);
1146 CONFIGFS_ATTR(, optimal_sectors);
1147 CONFIGFS_ATTR_RO(, hw_queue_depth);
1148 CONFIGFS_ATTR(, queue_depth);
1149 CONFIGFS_ATTR(, max_unmap_lba_count);
1150 CONFIGFS_ATTR(, max_unmap_block_desc_count);
1151 CONFIGFS_ATTR(, unmap_granularity);
1152 CONFIGFS_ATTR(, unmap_granularity_alignment);
1153 CONFIGFS_ATTR(, unmap_zeroes_data);
1154 CONFIGFS_ATTR(, max_write_same_len);
1155 CONFIGFS_ATTR_RO(, alua_support);
1156 CONFIGFS_ATTR_RO(, pgr_support);
1159 * dev_attrib attributes for devices using the target core SBC/SPC
1160 * interpreter. Any backend using spc_parse_cdb should be using
1161 * these.
1163 struct configfs_attribute *sbc_attrib_attrs[] = {
1164 &attr_emulate_model_alias,
1165 &attr_emulate_dpo,
1166 &attr_emulate_fua_write,
1167 &attr_emulate_fua_read,
1168 &attr_emulate_write_cache,
1169 &attr_emulate_ua_intlck_ctrl,
1170 &attr_emulate_tas,
1171 &attr_emulate_tpu,
1172 &attr_emulate_tpws,
1173 &attr_emulate_caw,
1174 &attr_emulate_3pc,
1175 &attr_emulate_pr,
1176 &attr_pi_prot_type,
1177 &attr_hw_pi_prot_type,
1178 &attr_pi_prot_format,
1179 &attr_pi_prot_verify,
1180 &attr_enforce_pr_isids,
1181 &attr_is_nonrot,
1182 &attr_emulate_rest_reord,
1183 &attr_force_pr_aptpl,
1184 &attr_hw_block_size,
1185 &attr_block_size,
1186 &attr_hw_max_sectors,
1187 &attr_optimal_sectors,
1188 &attr_hw_queue_depth,
1189 &attr_queue_depth,
1190 &attr_max_unmap_lba_count,
1191 &attr_max_unmap_block_desc_count,
1192 &attr_unmap_granularity,
1193 &attr_unmap_granularity_alignment,
1194 &attr_unmap_zeroes_data,
1195 &attr_max_write_same_len,
1196 &attr_alua_support,
1197 &attr_pgr_support,
1198 NULL,
1200 EXPORT_SYMBOL(sbc_attrib_attrs);
1203 * Minimal dev_attrib attributes for devices passing through CDBs.
1204 * In this case we only provide a few read-only attributes for
1205 * backwards compatibility.
1207 struct configfs_attribute *passthrough_attrib_attrs[] = {
1208 &attr_hw_pi_prot_type,
1209 &attr_hw_block_size,
1210 &attr_hw_max_sectors,
1211 &attr_hw_queue_depth,
1212 &attr_alua_support,
1213 &attr_pgr_support,
1214 NULL,
1216 EXPORT_SYMBOL(passthrough_attrib_attrs);
1218 TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1219 TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1221 /* End functions for struct config_item_type tb_dev_attrib_cit */
1223 /* Start functions for struct config_item_type tb_dev_wwn_cit */
1225 static struct t10_wwn *to_t10_wwn(struct config_item *item)
1227 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1231 * STANDARD and VPD page 0x83 T10 Vendor Identification
1233 static ssize_t target_wwn_vendor_id_show(struct config_item *item,
1234 char *page)
1236 return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
1239 static ssize_t target_wwn_vendor_id_store(struct config_item *item,
1240 const char *page, size_t count)
1242 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1243 struct se_device *dev = t10_wwn->t10_dev;
1244 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1245 unsigned char buf[INQUIRY_VENDOR_LEN + 2];
1246 char *stripped = NULL;
1247 size_t len;
1248 int i;
1250 len = strlcpy(buf, page, sizeof(buf));
1251 if (len < sizeof(buf)) {
1252 /* Strip any newline added from userspace. */
1253 stripped = strstrip(buf);
1254 len = strlen(stripped);
1256 if (len > INQUIRY_VENDOR_LEN) {
1257 pr_err("Emulated T10 Vendor Identification exceeds"
1258 " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
1259 "\n");
1260 return -EOVERFLOW;
1264 * SPC 4.3.1:
1265 * ASCII data fields shall contain only ASCII printable characters (i.e.,
1266 * code values 20h to 7Eh) and may be terminated with one or more ASCII
1267 * null (00h) characters.
1269 for (i = 0; i < len; i++) {
1270 if ((stripped[i] < 0x20) || (stripped[i] > 0x7E)) {
1271 pr_err("Emulated T10 Vendor Identification contains"
1272 " non-ASCII-printable characters\n");
1273 return -EINVAL;
1278 * Check to see if any active exports exist. If they do exist, fail
1279 * here as changing this information on the fly (underneath the
1280 * initiator side OS dependent multipath code) could cause negative
1281 * effects.
1283 if (dev->export_count) {
1284 pr_err("Unable to set T10 Vendor Identification while"
1285 " active %d exports exist\n", dev->export_count);
1286 return -EINVAL;
1289 BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
1290 strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
1292 pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
1293 " %s\n", dev->t10_wwn.vendor);
1295 return count;
1299 * VPD page 0x80 Unit serial
1301 static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1302 char *page)
1304 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1305 &to_t10_wwn(item)->unit_serial[0]);
1308 static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1309 const char *page, size_t count)
1311 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1312 struct se_device *dev = t10_wwn->t10_dev;
1313 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
1316 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1317 * from the struct scsi_device level firmware, do not allow
1318 * VPD Unit Serial to be emulated.
1320 * Note this struct scsi_device could also be emulating VPD
1321 * information from its drivers/scsi LLD. But for now we assume
1322 * it is doing 'the right thing' wrt a world wide unique
1323 * VPD Unit Serial Number that OS dependent multipath can depend on.
1325 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
1326 pr_err("Underlying SCSI device firmware provided VPD"
1327 " Unit Serial, ignoring request\n");
1328 return -EOPNOTSUPP;
1331 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
1332 pr_err("Emulated VPD Unit Serial exceeds"
1333 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1334 return -EOVERFLOW;
1337 * Check to see if any active $FABRIC_MOD exports exist. If they
1338 * do exist, fail here as changing this information on the fly
1339 * (underneath the initiator side OS dependent multipath code)
1340 * could cause negative effects.
1342 if (dev->export_count) {
1343 pr_err("Unable to set VPD Unit Serial while"
1344 " active %d $FABRIC_MOD exports exist\n",
1345 dev->export_count);
1346 return -EINVAL;
1350 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1352 * Also, strip any newline added from the userspace
1353 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1355 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
1356 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
1357 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1358 "%s", strstrip(buf));
1359 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1361 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1362 " %s\n", dev->t10_wwn.unit_serial);
1364 return count;
1368 * VPD page 0x83 Protocol Identifier
1370 static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1371 char *page)
1373 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1374 struct t10_vpd *vpd;
1375 unsigned char buf[VPD_TMP_BUF_SIZE];
1376 ssize_t len = 0;
1378 memset(buf, 0, VPD_TMP_BUF_SIZE);
1380 spin_lock(&t10_wwn->t10_vpd_lock);
1381 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
1382 if (!vpd->protocol_identifier_set)
1383 continue;
1385 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1387 if (len + strlen(buf) >= PAGE_SIZE)
1388 break;
1390 len += sprintf(page+len, "%s", buf);
1392 spin_unlock(&t10_wwn->t10_vpd_lock);
1394 return len;
1398 * Generic wrapper for dumping VPD identifiers by association.
1400 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
1401 static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1402 char *page) \
1404 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1405 struct t10_vpd *vpd; \
1406 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1407 ssize_t len = 0; \
1409 spin_lock(&t10_wwn->t10_vpd_lock); \
1410 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1411 if (vpd->association != _assoc) \
1412 continue; \
1414 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1415 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
1416 if (len + strlen(buf) >= PAGE_SIZE) \
1417 break; \
1418 len += sprintf(page+len, "%s", buf); \
1420 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1421 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1422 if (len + strlen(buf) >= PAGE_SIZE) \
1423 break; \
1424 len += sprintf(page+len, "%s", buf); \
1426 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1427 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1428 if (len + strlen(buf) >= PAGE_SIZE) \
1429 break; \
1430 len += sprintf(page+len, "%s", buf); \
1432 spin_unlock(&t10_wwn->t10_vpd_lock); \
1434 return len; \
1437 /* VPD page 0x83 Association: Logical Unit */
1438 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1439 /* VPD page 0x83 Association: Target Port */
1440 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1441 /* VPD page 0x83 Association: SCSI Target Device */
1442 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1444 CONFIGFS_ATTR(target_wwn_, vendor_id);
1445 CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1446 CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1447 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1448 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1449 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1451 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1452 &target_wwn_attr_vendor_id,
1453 &target_wwn_attr_vpd_unit_serial,
1454 &target_wwn_attr_vpd_protocol_identifier,
1455 &target_wwn_attr_vpd_assoc_logical_unit,
1456 &target_wwn_attr_vpd_assoc_target_port,
1457 &target_wwn_attr_vpd_assoc_scsi_target_device,
1458 NULL,
1461 TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1463 /* End functions for struct config_item_type tb_dev_wwn_cit */
1465 /* Start functions for struct config_item_type tb_dev_pr_cit */
1467 static struct se_device *pr_to_dev(struct config_item *item)
1469 return container_of(to_config_group(item), struct se_device,
1470 dev_pr_group);
1473 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1474 char *page)
1476 struct se_node_acl *se_nacl;
1477 struct t10_pr_registration *pr_reg;
1478 char i_buf[PR_REG_ISID_ID_LEN];
1480 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1482 pr_reg = dev->dev_pr_res_holder;
1483 if (!pr_reg)
1484 return sprintf(page, "No SPC-3 Reservation holder\n");
1486 se_nacl = pr_reg->pr_reg_nacl;
1487 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1489 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1490 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1491 se_nacl->initiatorname, i_buf);
1494 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1495 char *page)
1497 struct se_node_acl *se_nacl;
1498 ssize_t len;
1500 se_nacl = dev->dev_reserved_node_acl;
1501 if (se_nacl) {
1502 len = sprintf(page,
1503 "SPC-2 Reservation: %s Initiator: %s\n",
1504 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1505 se_nacl->initiatorname);
1506 } else {
1507 len = sprintf(page, "No SPC-2 Reservation holder\n");
1509 return len;
1512 static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1514 struct se_device *dev = pr_to_dev(item);
1515 int ret;
1517 if (!dev->dev_attrib.emulate_pr)
1518 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1520 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1521 return sprintf(page, "Passthrough\n");
1523 spin_lock(&dev->dev_reservation_lock);
1524 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1525 ret = target_core_dev_pr_show_spc2_res(dev, page);
1526 else
1527 ret = target_core_dev_pr_show_spc3_res(dev, page);
1528 spin_unlock(&dev->dev_reservation_lock);
1529 return ret;
1532 static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1533 char *page)
1535 struct se_device *dev = pr_to_dev(item);
1536 ssize_t len = 0;
1538 spin_lock(&dev->dev_reservation_lock);
1539 if (!dev->dev_pr_res_holder) {
1540 len = sprintf(page, "No SPC-3 Reservation holder\n");
1541 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1542 len = sprintf(page, "SPC-3 Reservation: All Target"
1543 " Ports registration\n");
1544 } else {
1545 len = sprintf(page, "SPC-3 Reservation: Single"
1546 " Target Port registration\n");
1549 spin_unlock(&dev->dev_reservation_lock);
1550 return len;
1553 static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1554 char *page)
1556 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1560 static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1561 char *page)
1563 struct se_device *dev = pr_to_dev(item);
1564 struct se_node_acl *se_nacl;
1565 struct se_portal_group *se_tpg;
1566 struct t10_pr_registration *pr_reg;
1567 const struct target_core_fabric_ops *tfo;
1568 ssize_t len = 0;
1570 spin_lock(&dev->dev_reservation_lock);
1571 pr_reg = dev->dev_pr_res_holder;
1572 if (!pr_reg) {
1573 len = sprintf(page, "No SPC-3 Reservation holder\n");
1574 goto out_unlock;
1577 se_nacl = pr_reg->pr_reg_nacl;
1578 se_tpg = se_nacl->se_tpg;
1579 tfo = se_tpg->se_tpg_tfo;
1581 len += sprintf(page+len, "SPC-3 Reservation: %s"
1582 " Target Node Endpoint: %s\n", tfo->fabric_name,
1583 tfo->tpg_get_wwn(se_tpg));
1584 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1585 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1586 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1587 tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1588 tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1590 out_unlock:
1591 spin_unlock(&dev->dev_reservation_lock);
1592 return len;
1596 static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1597 char *page)
1599 struct se_device *dev = pr_to_dev(item);
1600 const struct target_core_fabric_ops *tfo;
1601 struct t10_pr_registration *pr_reg;
1602 unsigned char buf[384];
1603 char i_buf[PR_REG_ISID_ID_LEN];
1604 ssize_t len = 0;
1605 int reg_count = 0;
1607 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1609 spin_lock(&dev->t10_pr.registration_lock);
1610 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1611 pr_reg_list) {
1613 memset(buf, 0, 384);
1614 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1615 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1616 core_pr_dump_initiator_port(pr_reg, i_buf,
1617 PR_REG_ISID_ID_LEN);
1618 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1619 tfo->fabric_name,
1620 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1621 pr_reg->pr_res_generation);
1623 if (len + strlen(buf) >= PAGE_SIZE)
1624 break;
1626 len += sprintf(page+len, "%s", buf);
1627 reg_count++;
1629 spin_unlock(&dev->t10_pr.registration_lock);
1631 if (!reg_count)
1632 len += sprintf(page+len, "None\n");
1634 return len;
1637 static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1639 struct se_device *dev = pr_to_dev(item);
1640 struct t10_pr_registration *pr_reg;
1641 ssize_t len = 0;
1643 spin_lock(&dev->dev_reservation_lock);
1644 pr_reg = dev->dev_pr_res_holder;
1645 if (pr_reg) {
1646 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1647 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1648 } else {
1649 len = sprintf(page, "No SPC-3 Reservation holder\n");
1652 spin_unlock(&dev->dev_reservation_lock);
1653 return len;
1656 static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1658 struct se_device *dev = pr_to_dev(item);
1660 if (!dev->dev_attrib.emulate_pr)
1661 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1662 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1663 return sprintf(page, "SPC_PASSTHROUGH\n");
1664 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1665 return sprintf(page, "SPC2_RESERVATIONS\n");
1667 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1670 static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1671 char *page)
1673 struct se_device *dev = pr_to_dev(item);
1675 if (!dev->dev_attrib.emulate_pr ||
1676 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1677 return 0;
1679 return sprintf(page, "APTPL Bit Status: %s\n",
1680 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1683 static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1684 char *page)
1686 struct se_device *dev = pr_to_dev(item);
1688 if (!dev->dev_attrib.emulate_pr ||
1689 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1690 return 0;
1692 return sprintf(page, "Ready to process PR APTPL metadata..\n");
1695 enum {
1696 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1697 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1698 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1699 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1702 static match_table_t tokens = {
1703 {Opt_initiator_fabric, "initiator_fabric=%s"},
1704 {Opt_initiator_node, "initiator_node=%s"},
1705 {Opt_initiator_sid, "initiator_sid=%s"},
1706 {Opt_sa_res_key, "sa_res_key=%s"},
1707 {Opt_res_holder, "res_holder=%d"},
1708 {Opt_res_type, "res_type=%d"},
1709 {Opt_res_scope, "res_scope=%d"},
1710 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1711 {Opt_mapped_lun, "mapped_lun=%u"},
1712 {Opt_target_fabric, "target_fabric=%s"},
1713 {Opt_target_node, "target_node=%s"},
1714 {Opt_tpgt, "tpgt=%d"},
1715 {Opt_port_rtpi, "port_rtpi=%d"},
1716 {Opt_target_lun, "target_lun=%u"},
1717 {Opt_err, NULL}
1720 static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1721 const char *page, size_t count)
1723 struct se_device *dev = pr_to_dev(item);
1724 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1725 unsigned char *t_fabric = NULL, *t_port = NULL;
1726 char *orig, *ptr, *opts;
1727 substring_t args[MAX_OPT_ARGS];
1728 unsigned long long tmp_ll;
1729 u64 sa_res_key = 0;
1730 u64 mapped_lun = 0, target_lun = 0;
1731 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1732 u16 tpgt = 0;
1733 u8 type = 0;
1735 if (!dev->dev_attrib.emulate_pr ||
1736 (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1737 return count;
1738 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1739 return count;
1741 if (dev->export_count) {
1742 pr_debug("Unable to process APTPL metadata while"
1743 " active fabric exports exist\n");
1744 return -EINVAL;
1747 opts = kstrdup(page, GFP_KERNEL);
1748 if (!opts)
1749 return -ENOMEM;
1751 orig = opts;
1752 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1753 if (!*ptr)
1754 continue;
1756 token = match_token(ptr, tokens, args);
1757 switch (token) {
1758 case Opt_initiator_fabric:
1759 i_fabric = match_strdup(args);
1760 if (!i_fabric) {
1761 ret = -ENOMEM;
1762 goto out;
1764 break;
1765 case Opt_initiator_node:
1766 i_port = match_strdup(args);
1767 if (!i_port) {
1768 ret = -ENOMEM;
1769 goto out;
1771 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1772 pr_err("APTPL metadata initiator_node="
1773 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1774 PR_APTPL_MAX_IPORT_LEN);
1775 ret = -EINVAL;
1776 break;
1778 break;
1779 case Opt_initiator_sid:
1780 isid = match_strdup(args);
1781 if (!isid) {
1782 ret = -ENOMEM;
1783 goto out;
1785 if (strlen(isid) >= PR_REG_ISID_LEN) {
1786 pr_err("APTPL metadata initiator_isid"
1787 "= exceeds PR_REG_ISID_LEN: %d\n",
1788 PR_REG_ISID_LEN);
1789 ret = -EINVAL;
1790 break;
1792 break;
1793 case Opt_sa_res_key:
1794 ret = match_u64(args, &tmp_ll);
1795 if (ret < 0) {
1796 pr_err("kstrtoull() failed for sa_res_key=\n");
1797 goto out;
1799 sa_res_key = (u64)tmp_ll;
1800 break;
1802 * PR APTPL Metadata for Reservation
1804 case Opt_res_holder:
1805 ret = match_int(args, &arg);
1806 if (ret)
1807 goto out;
1808 res_holder = arg;
1809 break;
1810 case Opt_res_type:
1811 ret = match_int(args, &arg);
1812 if (ret)
1813 goto out;
1814 type = (u8)arg;
1815 break;
1816 case Opt_res_scope:
1817 ret = match_int(args, &arg);
1818 if (ret)
1819 goto out;
1820 break;
1821 case Opt_res_all_tg_pt:
1822 ret = match_int(args, &arg);
1823 if (ret)
1824 goto out;
1825 all_tg_pt = (int)arg;
1826 break;
1827 case Opt_mapped_lun:
1828 ret = match_u64(args, &tmp_ll);
1829 if (ret)
1830 goto out;
1831 mapped_lun = (u64)tmp_ll;
1832 break;
1834 * PR APTPL Metadata for Target Port
1836 case Opt_target_fabric:
1837 t_fabric = match_strdup(args);
1838 if (!t_fabric) {
1839 ret = -ENOMEM;
1840 goto out;
1842 break;
1843 case Opt_target_node:
1844 t_port = match_strdup(args);
1845 if (!t_port) {
1846 ret = -ENOMEM;
1847 goto out;
1849 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1850 pr_err("APTPL metadata target_node="
1851 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1852 PR_APTPL_MAX_TPORT_LEN);
1853 ret = -EINVAL;
1854 break;
1856 break;
1857 case Opt_tpgt:
1858 ret = match_int(args, &arg);
1859 if (ret)
1860 goto out;
1861 tpgt = (u16)arg;
1862 break;
1863 case Opt_port_rtpi:
1864 ret = match_int(args, &arg);
1865 if (ret)
1866 goto out;
1867 break;
1868 case Opt_target_lun:
1869 ret = match_u64(args, &tmp_ll);
1870 if (ret)
1871 goto out;
1872 target_lun = (u64)tmp_ll;
1873 break;
1874 default:
1875 break;
1879 if (!i_port || !t_port || !sa_res_key) {
1880 pr_err("Illegal parameters for APTPL registration\n");
1881 ret = -EINVAL;
1882 goto out;
1885 if (res_holder && !(type)) {
1886 pr_err("Illegal PR type: 0x%02x for reservation"
1887 " holder\n", type);
1888 ret = -EINVAL;
1889 goto out;
1892 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1893 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1894 res_holder, all_tg_pt, type);
1895 out:
1896 kfree(i_fabric);
1897 kfree(i_port);
1898 kfree(isid);
1899 kfree(t_fabric);
1900 kfree(t_port);
1901 kfree(orig);
1902 return (ret == 0) ? count : ret;
1906 CONFIGFS_ATTR_RO(target_pr_, res_holder);
1907 CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
1908 CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
1909 CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
1910 CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
1911 CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
1912 CONFIGFS_ATTR_RO(target_pr_, res_type);
1913 CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
1914 CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1916 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1917 &target_pr_attr_res_holder,
1918 &target_pr_attr_res_pr_all_tgt_pts,
1919 &target_pr_attr_res_pr_generation,
1920 &target_pr_attr_res_pr_holder_tg_port,
1921 &target_pr_attr_res_pr_registered_i_pts,
1922 &target_pr_attr_res_pr_type,
1923 &target_pr_attr_res_type,
1924 &target_pr_attr_res_aptpl_active,
1925 &target_pr_attr_res_aptpl_metadata,
1926 NULL,
1929 TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1931 /* End functions for struct config_item_type tb_dev_pr_cit */
1933 /* Start functions for struct config_item_type tb_dev_cit */
1935 static inline struct se_device *to_device(struct config_item *item)
1937 return container_of(to_config_group(item), struct se_device, dev_group);
1940 static ssize_t target_dev_info_show(struct config_item *item, char *page)
1942 struct se_device *dev = to_device(item);
1943 int bl = 0;
1944 ssize_t read_bytes = 0;
1946 transport_dump_dev_state(dev, page, &bl);
1947 read_bytes += bl;
1948 read_bytes += dev->transport->show_configfs_dev_params(dev,
1949 page+read_bytes);
1950 return read_bytes;
1953 static ssize_t target_dev_control_store(struct config_item *item,
1954 const char *page, size_t count)
1956 struct se_device *dev = to_device(item);
1958 return dev->transport->set_configfs_dev_params(dev, page, count);
1961 static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1963 struct se_device *dev = to_device(item);
1965 if (!(dev->dev_flags & DF_USING_ALIAS))
1966 return 0;
1968 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1971 static ssize_t target_dev_alias_store(struct config_item *item,
1972 const char *page, size_t count)
1974 struct se_device *dev = to_device(item);
1975 struct se_hba *hba = dev->se_hba;
1976 ssize_t read_bytes;
1978 if (count > (SE_DEV_ALIAS_LEN-1)) {
1979 pr_err("alias count: %d exceeds"
1980 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1981 SE_DEV_ALIAS_LEN-1);
1982 return -EINVAL;
1985 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1986 if (!read_bytes)
1987 return -EINVAL;
1988 if (dev->dev_alias[read_bytes - 1] == '\n')
1989 dev->dev_alias[read_bytes - 1] = '\0';
1991 dev->dev_flags |= DF_USING_ALIAS;
1993 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1994 config_item_name(&hba->hba_group.cg_item),
1995 config_item_name(&dev->dev_group.cg_item),
1996 dev->dev_alias);
1998 return read_bytes;
2001 static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2003 struct se_device *dev = to_device(item);
2005 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2006 return 0;
2008 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2011 static ssize_t target_dev_udev_path_store(struct config_item *item,
2012 const char *page, size_t count)
2014 struct se_device *dev = to_device(item);
2015 struct se_hba *hba = dev->se_hba;
2016 ssize_t read_bytes;
2018 if (count > (SE_UDEV_PATH_LEN-1)) {
2019 pr_err("udev_path count: %d exceeds"
2020 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2021 SE_UDEV_PATH_LEN-1);
2022 return -EINVAL;
2025 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2026 "%s", page);
2027 if (!read_bytes)
2028 return -EINVAL;
2029 if (dev->udev_path[read_bytes - 1] == '\n')
2030 dev->udev_path[read_bytes - 1] = '\0';
2032 dev->dev_flags |= DF_USING_UDEV_PATH;
2034 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2035 config_item_name(&hba->hba_group.cg_item),
2036 config_item_name(&dev->dev_group.cg_item),
2037 dev->udev_path);
2039 return read_bytes;
2042 static ssize_t target_dev_enable_show(struct config_item *item, char *page)
2044 struct se_device *dev = to_device(item);
2046 return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
2049 static ssize_t target_dev_enable_store(struct config_item *item,
2050 const char *page, size_t count)
2052 struct se_device *dev = to_device(item);
2053 char *ptr;
2054 int ret;
2056 ptr = strstr(page, "1");
2057 if (!ptr) {
2058 pr_err("For dev_enable ops, only valid value"
2059 " is \"1\"\n");
2060 return -EINVAL;
2063 ret = target_configure_device(dev);
2064 if (ret)
2065 return ret;
2066 return count;
2069 static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2071 struct se_device *dev = to_device(item);
2072 struct config_item *lu_ci;
2073 struct t10_alua_lu_gp *lu_gp;
2074 struct t10_alua_lu_gp_member *lu_gp_mem;
2075 ssize_t len = 0;
2077 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2078 if (!lu_gp_mem)
2079 return 0;
2081 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2082 lu_gp = lu_gp_mem->lu_gp;
2083 if (lu_gp) {
2084 lu_ci = &lu_gp->lu_gp_group.cg_item;
2085 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2086 config_item_name(lu_ci), lu_gp->lu_gp_id);
2088 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2090 return len;
2093 static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2094 const char *page, size_t count)
2096 struct se_device *dev = to_device(item);
2097 struct se_hba *hba = dev->se_hba;
2098 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2099 struct t10_alua_lu_gp_member *lu_gp_mem;
2100 unsigned char buf[LU_GROUP_NAME_BUF];
2101 int move = 0;
2103 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2104 if (!lu_gp_mem)
2105 return count;
2107 if (count > LU_GROUP_NAME_BUF) {
2108 pr_err("ALUA LU Group Alias too large!\n");
2109 return -EINVAL;
2111 memset(buf, 0, LU_GROUP_NAME_BUF);
2112 memcpy(buf, page, count);
2114 * Any ALUA logical unit alias besides "NULL" means we will be
2115 * making a new group association.
2117 if (strcmp(strstrip(buf), "NULL")) {
2119 * core_alua_get_lu_gp_by_name() will increment reference to
2120 * struct t10_alua_lu_gp. This reference is released with
2121 * core_alua_get_lu_gp_by_name below().
2123 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
2124 if (!lu_gp_new)
2125 return -ENODEV;
2128 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2129 lu_gp = lu_gp_mem->lu_gp;
2130 if (lu_gp) {
2132 * Clearing an existing lu_gp association, and replacing
2133 * with NULL
2135 if (!lu_gp_new) {
2136 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2137 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2138 " %hu\n",
2139 config_item_name(&hba->hba_group.cg_item),
2140 config_item_name(&dev->dev_group.cg_item),
2141 config_item_name(&lu_gp->lu_gp_group.cg_item),
2142 lu_gp->lu_gp_id);
2144 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2145 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2147 return count;
2150 * Removing existing association of lu_gp_mem with lu_gp
2152 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2153 move = 1;
2156 * Associate lu_gp_mem with lu_gp_new.
2158 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2159 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2161 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2162 " core/alua/lu_gps/%s, ID: %hu\n",
2163 (move) ? "Moving" : "Adding",
2164 config_item_name(&hba->hba_group.cg_item),
2165 config_item_name(&dev->dev_group.cg_item),
2166 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2167 lu_gp_new->lu_gp_id);
2169 core_alua_put_lu_gp_from_name(lu_gp_new);
2170 return count;
2173 static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2175 struct se_device *dev = to_device(item);
2176 struct t10_alua_lba_map *map;
2177 struct t10_alua_lba_map_member *mem;
2178 char *b = page;
2179 int bl = 0;
2180 char state;
2182 spin_lock(&dev->t10_alua.lba_map_lock);
2183 if (!list_empty(&dev->t10_alua.lba_map_list))
2184 bl += sprintf(b + bl, "%u %u\n",
2185 dev->t10_alua.lba_map_segment_size,
2186 dev->t10_alua.lba_map_segment_multiplier);
2187 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2188 bl += sprintf(b + bl, "%llu %llu",
2189 map->lba_map_first_lba, map->lba_map_last_lba);
2190 list_for_each_entry(mem, &map->lba_map_mem_list,
2191 lba_map_mem_list) {
2192 switch (mem->lba_map_mem_alua_state) {
2193 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2194 state = 'O';
2195 break;
2196 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2197 state = 'A';
2198 break;
2199 case ALUA_ACCESS_STATE_STANDBY:
2200 state = 'S';
2201 break;
2202 case ALUA_ACCESS_STATE_UNAVAILABLE:
2203 state = 'U';
2204 break;
2205 default:
2206 state = '.';
2207 break;
2209 bl += sprintf(b + bl, " %d:%c",
2210 mem->lba_map_mem_alua_pg_id, state);
2212 bl += sprintf(b + bl, "\n");
2214 spin_unlock(&dev->t10_alua.lba_map_lock);
2215 return bl;
2218 static ssize_t target_dev_lba_map_store(struct config_item *item,
2219 const char *page, size_t count)
2221 struct se_device *dev = to_device(item);
2222 struct t10_alua_lba_map *lba_map = NULL;
2223 struct list_head lba_list;
2224 char *map_entries, *orig, *ptr;
2225 char state;
2226 int pg_num = -1, pg;
2227 int ret = 0, num = 0, pg_id, alua_state;
2228 unsigned long start_lba = -1, end_lba = -1;
2229 unsigned long segment_size = -1, segment_mult = -1;
2231 orig = map_entries = kstrdup(page, GFP_KERNEL);
2232 if (!map_entries)
2233 return -ENOMEM;
2235 INIT_LIST_HEAD(&lba_list);
2236 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2237 if (!*ptr)
2238 continue;
2240 if (num == 0) {
2241 if (sscanf(ptr, "%lu %lu\n",
2242 &segment_size, &segment_mult) != 2) {
2243 pr_err("Invalid line %d\n", num);
2244 ret = -EINVAL;
2245 break;
2247 num++;
2248 continue;
2250 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2251 pr_err("Invalid line %d\n", num);
2252 ret = -EINVAL;
2253 break;
2255 ptr = strchr(ptr, ' ');
2256 if (!ptr) {
2257 pr_err("Invalid line %d, missing end lba\n", num);
2258 ret = -EINVAL;
2259 break;
2261 ptr++;
2262 ptr = strchr(ptr, ' ');
2263 if (!ptr) {
2264 pr_err("Invalid line %d, missing state definitions\n",
2265 num);
2266 ret = -EINVAL;
2267 break;
2269 ptr++;
2270 lba_map = core_alua_allocate_lba_map(&lba_list,
2271 start_lba, end_lba);
2272 if (IS_ERR(lba_map)) {
2273 ret = PTR_ERR(lba_map);
2274 break;
2276 pg = 0;
2277 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2278 switch (state) {
2279 case 'O':
2280 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2281 break;
2282 case 'A':
2283 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2284 break;
2285 case 'S':
2286 alua_state = ALUA_ACCESS_STATE_STANDBY;
2287 break;
2288 case 'U':
2289 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2290 break;
2291 default:
2292 pr_err("Invalid ALUA state '%c'\n", state);
2293 ret = -EINVAL;
2294 goto out;
2297 ret = core_alua_allocate_lba_map_mem(lba_map,
2298 pg_id, alua_state);
2299 if (ret) {
2300 pr_err("Invalid target descriptor %d:%c "
2301 "at line %d\n",
2302 pg_id, state, num);
2303 break;
2305 pg++;
2306 ptr = strchr(ptr, ' ');
2307 if (ptr)
2308 ptr++;
2309 else
2310 break;
2312 if (pg_num == -1)
2313 pg_num = pg;
2314 else if (pg != pg_num) {
2315 pr_err("Only %d from %d port groups definitions "
2316 "at line %d\n", pg, pg_num, num);
2317 ret = -EINVAL;
2318 break;
2320 num++;
2322 out:
2323 if (ret) {
2324 core_alua_free_lba_map(&lba_list);
2325 count = ret;
2326 } else
2327 core_alua_set_lba_map(dev, &lba_list,
2328 segment_size, segment_mult);
2329 kfree(orig);
2330 return count;
2333 CONFIGFS_ATTR_RO(target_dev_, info);
2334 CONFIGFS_ATTR_WO(target_dev_, control);
2335 CONFIGFS_ATTR(target_dev_, alias);
2336 CONFIGFS_ATTR(target_dev_, udev_path);
2337 CONFIGFS_ATTR(target_dev_, enable);
2338 CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2339 CONFIGFS_ATTR(target_dev_, lba_map);
2341 static struct configfs_attribute *target_core_dev_attrs[] = {
2342 &target_dev_attr_info,
2343 &target_dev_attr_control,
2344 &target_dev_attr_alias,
2345 &target_dev_attr_udev_path,
2346 &target_dev_attr_enable,
2347 &target_dev_attr_alua_lu_gp,
2348 &target_dev_attr_lba_map,
2349 NULL,
2352 static void target_core_dev_release(struct config_item *item)
2354 struct config_group *dev_cg = to_config_group(item);
2355 struct se_device *dev =
2356 container_of(dev_cg, struct se_device, dev_group);
2358 target_free_device(dev);
2362 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2363 * within target_fabric_port_link()
2365 struct configfs_item_operations target_core_dev_item_ops = {
2366 .release = target_core_dev_release,
2369 TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2371 /* End functions for struct config_item_type tb_dev_cit */
2373 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2375 static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2377 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2378 lu_gp_group);
2381 static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2383 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2385 if (!lu_gp->lu_gp_valid_id)
2386 return 0;
2387 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2390 static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2391 const char *page, size_t count)
2393 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2394 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2395 unsigned long lu_gp_id;
2396 int ret;
2398 ret = kstrtoul(page, 0, &lu_gp_id);
2399 if (ret < 0) {
2400 pr_err("kstrtoul() returned %d for"
2401 " lu_gp_id\n", ret);
2402 return ret;
2404 if (lu_gp_id > 0x0000ffff) {
2405 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2406 " 0x0000ffff\n", lu_gp_id);
2407 return -EINVAL;
2410 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2411 if (ret < 0)
2412 return -EINVAL;
2414 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2415 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2416 config_item_name(&alua_lu_gp_cg->cg_item),
2417 lu_gp->lu_gp_id);
2419 return count;
2422 static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2424 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2425 struct se_device *dev;
2426 struct se_hba *hba;
2427 struct t10_alua_lu_gp_member *lu_gp_mem;
2428 ssize_t len = 0, cur_len;
2429 unsigned char buf[LU_GROUP_NAME_BUF];
2431 memset(buf, 0, LU_GROUP_NAME_BUF);
2433 spin_lock(&lu_gp->lu_gp_lock);
2434 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2435 dev = lu_gp_mem->lu_gp_mem_dev;
2436 hba = dev->se_hba;
2438 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2439 config_item_name(&hba->hba_group.cg_item),
2440 config_item_name(&dev->dev_group.cg_item));
2441 cur_len++; /* Extra byte for NULL terminator */
2443 if ((cur_len + len) > PAGE_SIZE) {
2444 pr_warn("Ran out of lu_gp_show_attr"
2445 "_members buffer\n");
2446 break;
2448 memcpy(page+len, buf, cur_len);
2449 len += cur_len;
2451 spin_unlock(&lu_gp->lu_gp_lock);
2453 return len;
2456 CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2457 CONFIGFS_ATTR_RO(target_lu_gp_, members);
2459 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2460 &target_lu_gp_attr_lu_gp_id,
2461 &target_lu_gp_attr_members,
2462 NULL,
2465 static void target_core_alua_lu_gp_release(struct config_item *item)
2467 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2468 struct t10_alua_lu_gp, lu_gp_group);
2470 core_alua_free_lu_gp(lu_gp);
2473 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2474 .release = target_core_alua_lu_gp_release,
2477 static const struct config_item_type target_core_alua_lu_gp_cit = {
2478 .ct_item_ops = &target_core_alua_lu_gp_ops,
2479 .ct_attrs = target_core_alua_lu_gp_attrs,
2480 .ct_owner = THIS_MODULE,
2483 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2485 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2487 static struct config_group *target_core_alua_create_lu_gp(
2488 struct config_group *group,
2489 const char *name)
2491 struct t10_alua_lu_gp *lu_gp;
2492 struct config_group *alua_lu_gp_cg = NULL;
2493 struct config_item *alua_lu_gp_ci = NULL;
2495 lu_gp = core_alua_allocate_lu_gp(name, 0);
2496 if (IS_ERR(lu_gp))
2497 return NULL;
2499 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2500 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2502 config_group_init_type_name(alua_lu_gp_cg, name,
2503 &target_core_alua_lu_gp_cit);
2505 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2506 " Group: core/alua/lu_gps/%s\n",
2507 config_item_name(alua_lu_gp_ci));
2509 return alua_lu_gp_cg;
2513 static void target_core_alua_drop_lu_gp(
2514 struct config_group *group,
2515 struct config_item *item)
2517 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2518 struct t10_alua_lu_gp, lu_gp_group);
2520 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2521 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2522 config_item_name(item), lu_gp->lu_gp_id);
2524 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2525 * -> target_core_alua_lu_gp_release()
2527 config_item_put(item);
2530 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2531 .make_group = &target_core_alua_create_lu_gp,
2532 .drop_item = &target_core_alua_drop_lu_gp,
2535 static const struct config_item_type target_core_alua_lu_gps_cit = {
2536 .ct_item_ops = NULL,
2537 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2538 .ct_owner = THIS_MODULE,
2541 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2543 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2545 static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2547 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2548 tg_pt_gp_group);
2551 static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2552 char *page)
2554 return sprintf(page, "%d\n",
2555 to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2558 static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2559 const char *page, size_t count)
2561 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2562 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2563 unsigned long tmp;
2564 int new_state, ret;
2566 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2567 pr_err("Unable to do implicit ALUA on non valid"
2568 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2569 return -EINVAL;
2571 if (!target_dev_configured(dev)) {
2572 pr_err("Unable to set alua_access_state while device is"
2573 " not configured\n");
2574 return -ENODEV;
2577 ret = kstrtoul(page, 0, &tmp);
2578 if (ret < 0) {
2579 pr_err("Unable to extract new ALUA access state from"
2580 " %s\n", page);
2581 return ret;
2583 new_state = (int)tmp;
2585 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2586 pr_err("Unable to process implicit configfs ALUA"
2587 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2588 return -EINVAL;
2590 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2591 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2592 /* LBA DEPENDENT is only allowed with implicit ALUA */
2593 pr_err("Unable to process implicit configfs ALUA transition"
2594 " while explicit ALUA management is enabled\n");
2595 return -EINVAL;
2598 ret = core_alua_do_port_transition(tg_pt_gp, dev,
2599 NULL, NULL, new_state, 0);
2600 return (!ret) ? count : -EINVAL;
2603 static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2604 char *page)
2606 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2607 return sprintf(page, "%s\n",
2608 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2611 static ssize_t target_tg_pt_gp_alua_access_status_store(
2612 struct config_item *item, const char *page, size_t count)
2614 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2615 unsigned long tmp;
2616 int new_status, ret;
2618 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2619 pr_err("Unable to do set ALUA access status on non"
2620 " valid tg_pt_gp ID: %hu\n",
2621 tg_pt_gp->tg_pt_gp_valid_id);
2622 return -EINVAL;
2625 ret = kstrtoul(page, 0, &tmp);
2626 if (ret < 0) {
2627 pr_err("Unable to extract new ALUA access status"
2628 " from %s\n", page);
2629 return ret;
2631 new_status = (int)tmp;
2633 if ((new_status != ALUA_STATUS_NONE) &&
2634 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2635 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2636 pr_err("Illegal ALUA access status: 0x%02x\n",
2637 new_status);
2638 return -EINVAL;
2641 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2642 return count;
2645 static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2646 char *page)
2648 return core_alua_show_access_type(to_tg_pt_gp(item), page);
2651 static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2652 const char *page, size_t count)
2654 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2657 #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2658 static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2659 struct config_item *item, char *p) \
2661 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2662 return sprintf(p, "%d\n", \
2663 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
2666 static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
2667 struct config_item *item, const char *p, size_t c) \
2669 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2670 unsigned long tmp; \
2671 int ret; \
2673 if (!t->tg_pt_gp_valid_id) { \
2674 pr_err("Unable to do set " #_name " ALUA state on non" \
2675 " valid tg_pt_gp ID: %hu\n", \
2676 t->tg_pt_gp_valid_id); \
2677 return -EINVAL; \
2680 ret = kstrtoul(p, 0, &tmp); \
2681 if (ret < 0) { \
2682 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2683 return -EINVAL; \
2685 if (tmp > 1) { \
2686 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2687 return -EINVAL; \
2689 if (tmp) \
2690 t->tg_pt_gp_alua_supported_states |= _bit; \
2691 else \
2692 t->tg_pt_gp_alua_supported_states &= ~_bit; \
2694 return c; \
2697 ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2698 ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2699 ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2700 ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2701 ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2702 ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2703 ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
2705 static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2706 struct config_item *item, char *page)
2708 return sprintf(page, "%d\n",
2709 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2712 static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2713 struct config_item *item, const char *page, size_t count)
2715 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2716 unsigned long tmp;
2717 int ret;
2719 ret = kstrtoul(page, 0, &tmp);
2720 if (ret < 0) {
2721 pr_err("Unable to extract alua_write_metadata\n");
2722 return ret;
2725 if ((tmp != 0) && (tmp != 1)) {
2726 pr_err("Illegal value for alua_write_metadata:"
2727 " %lu\n", tmp);
2728 return -EINVAL;
2730 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2732 return count;
2735 static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2736 char *page)
2738 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2741 static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2742 const char *page, size_t count)
2744 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2745 count);
2748 static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2749 char *page)
2751 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2754 static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
2755 const char *page, size_t count)
2757 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
2758 count);
2761 static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
2762 struct config_item *item, char *page)
2764 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
2767 static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
2768 struct config_item *item, const char *page, size_t count)
2770 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
2771 count);
2774 static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
2775 char *page)
2777 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
2780 static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
2781 const char *page, size_t count)
2783 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
2786 static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
2787 char *page)
2789 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2791 if (!tg_pt_gp->tg_pt_gp_valid_id)
2792 return 0;
2793 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2796 static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
2797 const char *page, size_t count)
2799 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2800 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2801 unsigned long tg_pt_gp_id;
2802 int ret;
2804 ret = kstrtoul(page, 0, &tg_pt_gp_id);
2805 if (ret < 0) {
2806 pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
2807 page);
2808 return ret;
2810 if (tg_pt_gp_id > 0x0000ffff) {
2811 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
2812 tg_pt_gp_id);
2813 return -EINVAL;
2816 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2817 if (ret < 0)
2818 return -EINVAL;
2820 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2821 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2822 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2823 tg_pt_gp->tg_pt_gp_id);
2825 return count;
2828 static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
2829 char *page)
2831 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2832 struct se_lun *lun;
2833 ssize_t len = 0, cur_len;
2834 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2836 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2838 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2839 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2840 lun_tg_pt_gp_link) {
2841 struct se_portal_group *tpg = lun->lun_tpg;
2843 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2844 "/%s\n", tpg->se_tpg_tfo->fabric_name,
2845 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2846 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2847 config_item_name(&lun->lun_group.cg_item));
2848 cur_len++; /* Extra byte for NULL terminator */
2850 if ((cur_len + len) > PAGE_SIZE) {
2851 pr_warn("Ran out of lu_gp_show_attr"
2852 "_members buffer\n");
2853 break;
2855 memcpy(page+len, buf, cur_len);
2856 len += cur_len;
2858 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2860 return len;
2863 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
2864 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
2865 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
2866 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
2867 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
2868 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
2869 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
2870 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
2871 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
2872 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
2873 CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
2874 CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
2875 CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
2876 CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
2877 CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
2878 CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
2879 CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2881 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2882 &target_tg_pt_gp_attr_alua_access_state,
2883 &target_tg_pt_gp_attr_alua_access_status,
2884 &target_tg_pt_gp_attr_alua_access_type,
2885 &target_tg_pt_gp_attr_alua_support_transitioning,
2886 &target_tg_pt_gp_attr_alua_support_offline,
2887 &target_tg_pt_gp_attr_alua_support_lba_dependent,
2888 &target_tg_pt_gp_attr_alua_support_unavailable,
2889 &target_tg_pt_gp_attr_alua_support_standby,
2890 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
2891 &target_tg_pt_gp_attr_alua_support_active_optimized,
2892 &target_tg_pt_gp_attr_alua_write_metadata,
2893 &target_tg_pt_gp_attr_nonop_delay_msecs,
2894 &target_tg_pt_gp_attr_trans_delay_msecs,
2895 &target_tg_pt_gp_attr_implicit_trans_secs,
2896 &target_tg_pt_gp_attr_preferred,
2897 &target_tg_pt_gp_attr_tg_pt_gp_id,
2898 &target_tg_pt_gp_attr_members,
2899 NULL,
2902 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2904 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2905 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2907 core_alua_free_tg_pt_gp(tg_pt_gp);
2910 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2911 .release = target_core_alua_tg_pt_gp_release,
2914 static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
2915 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2916 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2917 .ct_owner = THIS_MODULE,
2920 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2922 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2924 static struct config_group *target_core_alua_create_tg_pt_gp(
2925 struct config_group *group,
2926 const char *name)
2928 struct t10_alua *alua = container_of(group, struct t10_alua,
2929 alua_tg_pt_gps_group);
2930 struct t10_alua_tg_pt_gp *tg_pt_gp;
2931 struct config_group *alua_tg_pt_gp_cg = NULL;
2932 struct config_item *alua_tg_pt_gp_ci = NULL;
2934 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2935 if (!tg_pt_gp)
2936 return NULL;
2938 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2939 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2941 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2942 &target_core_alua_tg_pt_gp_cit);
2944 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2945 " Group: alua/tg_pt_gps/%s\n",
2946 config_item_name(alua_tg_pt_gp_ci));
2948 return alua_tg_pt_gp_cg;
2951 static void target_core_alua_drop_tg_pt_gp(
2952 struct config_group *group,
2953 struct config_item *item)
2955 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2956 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2958 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2959 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2960 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2962 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2963 * -> target_core_alua_tg_pt_gp_release().
2965 config_item_put(item);
2968 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2969 .make_group = &target_core_alua_create_tg_pt_gp,
2970 .drop_item = &target_core_alua_drop_tg_pt_gp,
2973 TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2975 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2977 /* Start functions for struct config_item_type target_core_alua_cit */
2980 * target_core_alua_cit is a ConfigFS group that lives under
2981 * /sys/kernel/config/target/core/alua. There are default groups
2982 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2983 * target_core_alua_cit in target_core_init_configfs() below.
2985 static const struct config_item_type target_core_alua_cit = {
2986 .ct_item_ops = NULL,
2987 .ct_attrs = NULL,
2988 .ct_owner = THIS_MODULE,
2991 /* End functions for struct config_item_type target_core_alua_cit */
2993 /* Start functions for struct config_item_type tb_dev_stat_cit */
2995 static struct config_group *target_core_stat_mkdir(
2996 struct config_group *group,
2997 const char *name)
2999 return ERR_PTR(-ENOSYS);
3002 static void target_core_stat_rmdir(
3003 struct config_group *group,
3004 struct config_item *item)
3006 return;
3009 static struct configfs_group_operations target_core_stat_group_ops = {
3010 .make_group = &target_core_stat_mkdir,
3011 .drop_item = &target_core_stat_rmdir,
3014 TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
3016 /* End functions for struct config_item_type tb_dev_stat_cit */
3018 /* Start functions for struct config_item_type target_core_hba_cit */
3020 static struct config_group *target_core_make_subdev(
3021 struct config_group *group,
3022 const char *name)
3024 struct t10_alua_tg_pt_gp *tg_pt_gp;
3025 struct config_item *hba_ci = &group->cg_item;
3026 struct se_hba *hba = item_to_hba(hba_ci);
3027 struct target_backend *tb = hba->backend;
3028 struct se_device *dev;
3029 int errno = -ENOMEM, ret;
3031 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3032 if (ret)
3033 return ERR_PTR(ret);
3035 dev = target_alloc_device(hba, name);
3036 if (!dev)
3037 goto out_unlock;
3039 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3041 config_group_init_type_name(&dev->dev_action_group, "action",
3042 &tb->tb_dev_action_cit);
3043 configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3045 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
3046 &tb->tb_dev_attrib_cit);
3047 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3049 config_group_init_type_name(&dev->dev_pr_group, "pr",
3050 &tb->tb_dev_pr_cit);
3051 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3053 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
3054 &tb->tb_dev_wwn_cit);
3055 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3056 &dev->dev_group);
3058 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
3059 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
3060 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3061 &dev->dev_group);
3063 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
3064 "statistics", &tb->tb_dev_stat_cit);
3065 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3066 &dev->dev_group);
3069 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3071 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
3072 if (!tg_pt_gp)
3073 goto out_free_device;
3074 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3076 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3077 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
3078 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3079 &dev->t10_alua.alua_tg_pt_gps_group);
3082 * Add core/$HBA/$DEV/statistics/ default groups
3084 target_stat_setup_dev_default_groups(dev);
3086 mutex_unlock(&hba->hba_access_mutex);
3087 return &dev->dev_group;
3089 out_free_device:
3090 target_free_device(dev);
3091 out_unlock:
3092 mutex_unlock(&hba->hba_access_mutex);
3093 return ERR_PTR(errno);
3096 static void target_core_drop_subdev(
3097 struct config_group *group,
3098 struct config_item *item)
3100 struct config_group *dev_cg = to_config_group(item);
3101 struct se_device *dev =
3102 container_of(dev_cg, struct se_device, dev_group);
3103 struct se_hba *hba;
3105 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3107 mutex_lock(&hba->hba_access_mutex);
3109 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3110 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
3113 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3114 * directly from target_core_alua_tg_pt_gp_release().
3116 dev->t10_alua.default_tg_pt_gp = NULL;
3118 configfs_remove_default_groups(dev_cg);
3121 * se_dev is released from target_core_dev_item_ops->release()
3123 config_item_put(item);
3124 mutex_unlock(&hba->hba_access_mutex);
3127 static struct configfs_group_operations target_core_hba_group_ops = {
3128 .make_group = target_core_make_subdev,
3129 .drop_item = target_core_drop_subdev,
3133 static inline struct se_hba *to_hba(struct config_item *item)
3135 return container_of(to_config_group(item), struct se_hba, hba_group);
3138 static ssize_t target_hba_info_show(struct config_item *item, char *page)
3140 struct se_hba *hba = to_hba(item);
3142 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
3143 hba->hba_id, hba->backend->ops->name,
3144 TARGET_CORE_VERSION);
3147 static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3149 struct se_hba *hba = to_hba(item);
3150 int hba_mode = 0;
3152 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3153 hba_mode = 1;
3155 return sprintf(page, "%d\n", hba_mode);
3158 static ssize_t target_hba_mode_store(struct config_item *item,
3159 const char *page, size_t count)
3161 struct se_hba *hba = to_hba(item);
3162 unsigned long mode_flag;
3163 int ret;
3165 if (hba->backend->ops->pmode_enable_hba == NULL)
3166 return -EINVAL;
3168 ret = kstrtoul(page, 0, &mode_flag);
3169 if (ret < 0) {
3170 pr_err("Unable to extract hba mode flag: %d\n", ret);
3171 return ret;
3174 if (hba->dev_count) {
3175 pr_err("Unable to set hba_mode with active devices\n");
3176 return -EINVAL;
3179 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3180 if (ret < 0)
3181 return -EINVAL;
3182 if (ret > 0)
3183 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3184 else if (ret == 0)
3185 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3187 return count;
3190 CONFIGFS_ATTR_RO(target_, hba_info);
3191 CONFIGFS_ATTR(target_, hba_mode);
3193 static void target_core_hba_release(struct config_item *item)
3195 struct se_hba *hba = container_of(to_config_group(item),
3196 struct se_hba, hba_group);
3197 core_delete_hba(hba);
3200 static struct configfs_attribute *target_core_hba_attrs[] = {
3201 &target_attr_hba_info,
3202 &target_attr_hba_mode,
3203 NULL,
3206 static struct configfs_item_operations target_core_hba_item_ops = {
3207 .release = target_core_hba_release,
3210 static const struct config_item_type target_core_hba_cit = {
3211 .ct_item_ops = &target_core_hba_item_ops,
3212 .ct_group_ops = &target_core_hba_group_ops,
3213 .ct_attrs = target_core_hba_attrs,
3214 .ct_owner = THIS_MODULE,
3217 static struct config_group *target_core_call_addhbatotarget(
3218 struct config_group *group,
3219 const char *name)
3221 char *se_plugin_str, *str, *str2;
3222 struct se_hba *hba;
3223 char buf[TARGET_CORE_NAME_MAX_LEN];
3224 unsigned long plugin_dep_id = 0;
3225 int ret;
3227 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3228 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3229 pr_err("Passed *name strlen(): %d exceeds"
3230 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3231 TARGET_CORE_NAME_MAX_LEN);
3232 return ERR_PTR(-ENAMETOOLONG);
3234 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3236 str = strstr(buf, "_");
3237 if (!str) {
3238 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3239 return ERR_PTR(-EINVAL);
3241 se_plugin_str = buf;
3243 * Special case for subsystem plugins that have "_" in their names.
3244 * Namely rd_direct and rd_mcp..
3246 str2 = strstr(str+1, "_");
3247 if (str2) {
3248 *str2 = '\0'; /* Terminate for *se_plugin_str */
3249 str2++; /* Skip to start of plugin dependent ID */
3250 str = str2;
3251 } else {
3252 *str = '\0'; /* Terminate for *se_plugin_str */
3253 str++; /* Skip to start of plugin dependent ID */
3256 ret = kstrtoul(str, 0, &plugin_dep_id);
3257 if (ret < 0) {
3258 pr_err("kstrtoul() returned %d for"
3259 " plugin_dep_id\n", ret);
3260 return ERR_PTR(ret);
3263 * Load up TCM subsystem plugins if they have not already been loaded.
3265 transport_subsystem_check_init();
3267 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3268 if (IS_ERR(hba))
3269 return ERR_CAST(hba);
3271 config_group_init_type_name(&hba->hba_group, name,
3272 &target_core_hba_cit);
3274 return &hba->hba_group;
3277 static void target_core_call_delhbafromtarget(
3278 struct config_group *group,
3279 struct config_item *item)
3282 * core_delete_hba() is called from target_core_hba_item_ops->release()
3283 * -> target_core_hba_release()
3285 config_item_put(item);
3288 static struct configfs_group_operations target_core_group_ops = {
3289 .make_group = target_core_call_addhbatotarget,
3290 .drop_item = target_core_call_delhbafromtarget,
3293 static const struct config_item_type target_core_cit = {
3294 .ct_item_ops = NULL,
3295 .ct_group_ops = &target_core_group_ops,
3296 .ct_attrs = NULL,
3297 .ct_owner = THIS_MODULE,
3300 /* Stop functions for struct config_item_type target_core_hba_cit */
3302 void target_setup_backend_cits(struct target_backend *tb)
3304 target_core_setup_dev_cit(tb);
3305 target_core_setup_dev_action_cit(tb);
3306 target_core_setup_dev_attrib_cit(tb);
3307 target_core_setup_dev_pr_cit(tb);
3308 target_core_setup_dev_wwn_cit(tb);
3309 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3310 target_core_setup_dev_stat_cit(tb);
3313 static void target_init_dbroot(void)
3315 struct file *fp;
3317 snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3318 fp = filp_open(db_root_stage, O_RDONLY, 0);
3319 if (IS_ERR(fp)) {
3320 pr_err("db_root: cannot open: %s\n", db_root_stage);
3321 return;
3323 if (!S_ISDIR(file_inode(fp)->i_mode)) {
3324 filp_close(fp, NULL);
3325 pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3326 return;
3328 filp_close(fp, NULL);
3330 strncpy(db_root, db_root_stage, DB_ROOT_LEN);
3331 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3334 static int __init target_core_init_configfs(void)
3336 struct configfs_subsystem *subsys = &target_core_fabrics;
3337 struct t10_alua_lu_gp *lu_gp;
3338 int ret;
3340 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3341 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3342 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3344 config_group_init(&subsys->su_group);
3345 mutex_init(&subsys->su_mutex);
3347 ret = init_se_kmem_caches();
3348 if (ret < 0)
3349 return ret;
3351 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3352 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3354 config_group_init_type_name(&target_core_hbagroup, "core",
3355 &target_core_cit);
3356 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3359 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3361 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3362 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3365 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3366 * groups under /sys/kernel/config/target/core/alua/
3368 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3369 &target_core_alua_lu_gps_cit);
3370 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3373 * Add core/alua/lu_gps/default_lu_gp
3375 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3376 if (IS_ERR(lu_gp)) {
3377 ret = -ENOMEM;
3378 goto out_global;
3381 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3382 &target_core_alua_lu_gp_cit);
3383 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3385 default_lu_gp = lu_gp;
3388 * Register the target_core_mod subsystem with configfs.
3390 ret = configfs_register_subsystem(subsys);
3391 if (ret < 0) {
3392 pr_err("Error %d while registering subsystem %s\n",
3393 ret, subsys->su_group.cg_item.ci_namebuf);
3394 goto out_global;
3396 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3397 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3398 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3400 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3402 ret = rd_module_init();
3403 if (ret < 0)
3404 goto out;
3406 ret = core_dev_setup_virtual_lun0();
3407 if (ret < 0)
3408 goto out;
3410 ret = target_xcopy_setup_pt();
3411 if (ret < 0)
3412 goto out;
3414 target_init_dbroot();
3416 return 0;
3418 out:
3419 configfs_unregister_subsystem(subsys);
3420 core_dev_release_virtual_lun0();
3421 rd_module_exit();
3422 out_global:
3423 if (default_lu_gp) {
3424 core_alua_free_lu_gp(default_lu_gp);
3425 default_lu_gp = NULL;
3427 release_se_kmem_caches();
3428 return ret;
3431 static void __exit target_core_exit_configfs(void)
3433 configfs_remove_default_groups(&alua_lu_gps_group);
3434 configfs_remove_default_groups(&alua_group);
3435 configfs_remove_default_groups(&target_core_hbagroup);
3438 * We expect subsys->su_group.default_groups to be released
3439 * by configfs subsystem provider logic..
3441 configfs_unregister_subsystem(&target_core_fabrics);
3443 core_alua_free_lu_gp(default_lu_gp);
3444 default_lu_gp = NULL;
3446 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3447 " Infrastructure\n");
3449 core_dev_release_virtual_lun0();
3450 rd_module_exit();
3451 target_xcopy_release_pt();
3452 release_se_kmem_caches();
3455 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3456 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3457 MODULE_LICENSE("GPL");
3459 module_init(target_core_init_configfs);
3460 module_exit(target_core_exit_configfs);