gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / target / target_core_configfs.c
blobe7b0430a0575d0403dbb38b0fd4d41df1ccce79d
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>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
47 #include "target_core_internal.h"
48 #include "target_core_alua.h"
49 #include "target_core_pr.h"
50 #include "target_core_rd.h"
51 #include "target_core_xcopy.h"
53 #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
54 static void target_core_setup_##_name##_cit(struct se_subsystem_api *sa) \
55 { \
56 struct target_backend_cits *tbc = &sa->tb_cits; \
57 struct config_item_type *cit = &tbc->tb_##_name##_cit; \
59 cit->ct_item_ops = _item_ops; \
60 cit->ct_group_ops = _group_ops; \
61 cit->ct_attrs = _attrs; \
62 cit->ct_owner = sa->owner; \
63 pr_debug("Setup generic %s\n", __stringify(_name)); \
66 extern struct t10_alua_lu_gp *default_lu_gp;
68 static LIST_HEAD(g_tf_list);
69 static DEFINE_MUTEX(g_tf_lock);
71 struct target_core_configfs_attribute {
72 struct configfs_attribute attr;
73 ssize_t (*show)(void *, char *);
74 ssize_t (*store)(void *, const char *, size_t);
77 static struct config_group target_core_hbagroup;
78 static struct config_group alua_group;
79 static struct config_group alua_lu_gps_group;
81 static inline struct se_hba *
82 item_to_hba(struct config_item *item)
84 return container_of(to_config_group(item), struct se_hba, hba_group);
88 * Attributes for /sys/kernel/config/target/
90 static ssize_t target_core_attr_show(struct config_item *item,
91 struct configfs_attribute *attr,
92 char *page)
94 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
95 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
96 utsname()->sysname, utsname()->machine);
99 static struct configfs_item_operations target_core_fabric_item_ops = {
100 .show_attribute = target_core_attr_show,
103 static struct configfs_attribute target_core_item_attr_version = {
104 .ca_owner = THIS_MODULE,
105 .ca_name = "version",
106 .ca_mode = S_IRUGO,
109 static struct target_fabric_configfs *target_core_get_fabric(
110 const char *name)
112 struct target_fabric_configfs *tf;
114 if (!name)
115 return NULL;
117 mutex_lock(&g_tf_lock);
118 list_for_each_entry(tf, &g_tf_list, tf_list) {
119 if (!strcmp(tf->tf_name, name)) {
120 atomic_inc(&tf->tf_access_cnt);
121 mutex_unlock(&g_tf_lock);
122 return tf;
125 mutex_unlock(&g_tf_lock);
127 return NULL;
131 * Called from struct target_core_group_ops->make_group()
133 static struct config_group *target_core_register_fabric(
134 struct config_group *group,
135 const char *name)
137 struct target_fabric_configfs *tf;
138 int ret;
140 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
141 " %s\n", group, name);
143 tf = target_core_get_fabric(name);
144 if (!tf) {
145 pr_debug("target_core_register_fabric() trying autoload for %s\n",
146 name);
149 * Below are some hardcoded request_module() calls to automatically
150 * local fabric modules when the following is called:
152 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
154 * Note that this does not limit which TCM fabric module can be
155 * registered, but simply provids auto loading logic for modules with
156 * mkdir(2) system calls with known TCM fabric modules.
159 if (!strncmp(name, "iscsi", 5)) {
161 * Automatically load the LIO Target fabric module when the
162 * following is called:
164 * mkdir -p $CONFIGFS/target/iscsi
166 ret = request_module("iscsi_target_mod");
167 if (ret < 0) {
168 pr_debug("request_module() failed for"
169 " iscsi_target_mod.ko: %d\n", ret);
170 return ERR_PTR(-EINVAL);
172 } else if (!strncmp(name, "loopback", 8)) {
174 * Automatically load the tcm_loop fabric module when the
175 * following is called:
177 * mkdir -p $CONFIGFS/target/loopback
179 ret = request_module("tcm_loop");
180 if (ret < 0) {
181 pr_debug("request_module() failed for"
182 " tcm_loop.ko: %d\n", ret);
183 return ERR_PTR(-EINVAL);
187 tf = target_core_get_fabric(name);
190 if (!tf) {
191 pr_debug("target_core_get_fabric() failed for %s\n",
192 name);
193 return ERR_PTR(-EINVAL);
195 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
196 " %s\n", tf->tf_name);
198 * On a successful target_core_get_fabric() look, the returned
199 * struct target_fabric_configfs *tf will contain a usage reference.
201 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
202 &tf->tf_cit_tmpl.tfc_wwn_cit);
204 tf->tf_group.default_groups = tf->tf_default_groups;
205 tf->tf_group.default_groups[0] = &tf->tf_disc_group;
206 tf->tf_group.default_groups[1] = NULL;
208 config_group_init_type_name(&tf->tf_group, name,
209 &tf->tf_cit_tmpl.tfc_wwn_cit);
210 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
211 &tf->tf_cit_tmpl.tfc_discovery_cit);
213 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
214 " %s\n", tf->tf_group.cg_item.ci_name);
215 tf->tf_fabric = &tf->tf_group.cg_item;
216 pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
217 " for %s\n", name);
219 return &tf->tf_group;
223 * Called from struct target_core_group_ops->drop_item()
225 static void target_core_deregister_fabric(
226 struct config_group *group,
227 struct config_item *item)
229 struct target_fabric_configfs *tf = container_of(
230 to_config_group(item), struct target_fabric_configfs, tf_group);
231 struct config_group *tf_group;
232 struct config_item *df_item;
233 int i;
235 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
236 " tf list\n", config_item_name(item));
238 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
239 " %s\n", tf->tf_name);
240 atomic_dec(&tf->tf_access_cnt);
242 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
243 " tf->tf_fabric for %s\n", tf->tf_name);
244 tf->tf_fabric = NULL;
246 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
247 " %s\n", config_item_name(item));
249 tf_group = &tf->tf_group;
250 for (i = 0; tf_group->default_groups[i]; i++) {
251 df_item = &tf_group->default_groups[i]->cg_item;
252 tf_group->default_groups[i] = NULL;
253 config_item_put(df_item);
255 config_item_put(item);
258 static struct configfs_group_operations target_core_fabric_group_ops = {
259 .make_group = &target_core_register_fabric,
260 .drop_item = &target_core_deregister_fabric,
264 * All item attributes appearing in /sys/kernel/target/ appear here.
266 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
267 &target_core_item_attr_version,
268 NULL,
272 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
274 static struct config_item_type target_core_fabrics_item = {
275 .ct_item_ops = &target_core_fabric_item_ops,
276 .ct_group_ops = &target_core_fabric_group_ops,
277 .ct_attrs = target_core_fabric_item_attrs,
278 .ct_owner = THIS_MODULE,
281 static struct configfs_subsystem target_core_fabrics = {
282 .su_group = {
283 .cg_item = {
284 .ci_namebuf = "target",
285 .ci_type = &target_core_fabrics_item,
290 int target_depend_item(struct config_item *item)
292 return configfs_depend_item(&target_core_fabrics, item);
294 EXPORT_SYMBOL(target_depend_item);
296 void target_undepend_item(struct config_item *item)
298 return configfs_undepend_item(&target_core_fabrics, item);
300 EXPORT_SYMBOL(target_undepend_item);
302 /*##############################################################################
303 // Start functions called by external Target Fabrics Modules
304 //############################################################################*/
306 static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
308 if (!tfo->name) {
309 pr_err("Missing tfo->name\n");
310 return -EINVAL;
312 if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
313 pr_err("Passed name: %s exceeds TARGET_FABRIC"
314 "_NAME_SIZE\n", tfo->name);
315 return -EINVAL;
317 if (!tfo->get_fabric_name) {
318 pr_err("Missing tfo->get_fabric_name()\n");
319 return -EINVAL;
321 if (!tfo->get_fabric_proto_ident) {
322 pr_err("Missing tfo->get_fabric_proto_ident()\n");
323 return -EINVAL;
325 if (!tfo->tpg_get_wwn) {
326 pr_err("Missing tfo->tpg_get_wwn()\n");
327 return -EINVAL;
329 if (!tfo->tpg_get_tag) {
330 pr_err("Missing tfo->tpg_get_tag()\n");
331 return -EINVAL;
333 if (!tfo->tpg_get_default_depth) {
334 pr_err("Missing tfo->tpg_get_default_depth()\n");
335 return -EINVAL;
337 if (!tfo->tpg_get_pr_transport_id) {
338 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
339 return -EINVAL;
341 if (!tfo->tpg_get_pr_transport_id_len) {
342 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
343 return -EINVAL;
345 if (!tfo->tpg_check_demo_mode) {
346 pr_err("Missing tfo->tpg_check_demo_mode()\n");
347 return -EINVAL;
349 if (!tfo->tpg_check_demo_mode_cache) {
350 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
351 return -EINVAL;
353 if (!tfo->tpg_check_demo_mode_write_protect) {
354 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
355 return -EINVAL;
357 if (!tfo->tpg_check_prod_mode_write_protect) {
358 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
359 return -EINVAL;
361 if (!tfo->tpg_alloc_fabric_acl) {
362 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
363 return -EINVAL;
365 if (!tfo->tpg_release_fabric_acl) {
366 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
367 return -EINVAL;
369 if (!tfo->tpg_get_inst_index) {
370 pr_err("Missing tfo->tpg_get_inst_index()\n");
371 return -EINVAL;
373 if (!tfo->release_cmd) {
374 pr_err("Missing tfo->release_cmd()\n");
375 return -EINVAL;
377 if (!tfo->shutdown_session) {
378 pr_err("Missing tfo->shutdown_session()\n");
379 return -EINVAL;
381 if (!tfo->close_session) {
382 pr_err("Missing tfo->close_session()\n");
383 return -EINVAL;
385 if (!tfo->sess_get_index) {
386 pr_err("Missing tfo->sess_get_index()\n");
387 return -EINVAL;
389 if (!tfo->write_pending) {
390 pr_err("Missing tfo->write_pending()\n");
391 return -EINVAL;
393 if (!tfo->write_pending_status) {
394 pr_err("Missing tfo->write_pending_status()\n");
395 return -EINVAL;
397 if (!tfo->set_default_node_attributes) {
398 pr_err("Missing tfo->set_default_node_attributes()\n");
399 return -EINVAL;
401 if (!tfo->get_task_tag) {
402 pr_err("Missing tfo->get_task_tag()\n");
403 return -EINVAL;
405 if (!tfo->get_cmd_state) {
406 pr_err("Missing tfo->get_cmd_state()\n");
407 return -EINVAL;
409 if (!tfo->queue_data_in) {
410 pr_err("Missing tfo->queue_data_in()\n");
411 return -EINVAL;
413 if (!tfo->queue_status) {
414 pr_err("Missing tfo->queue_status()\n");
415 return -EINVAL;
417 if (!tfo->queue_tm_rsp) {
418 pr_err("Missing tfo->queue_tm_rsp()\n");
419 return -EINVAL;
421 if (!tfo->aborted_task) {
422 pr_err("Missing tfo->aborted_task()\n");
423 return -EINVAL;
426 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
427 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
428 * target_core_fabric_configfs.c WWN+TPG group context code.
430 if (!tfo->fabric_make_wwn) {
431 pr_err("Missing tfo->fabric_make_wwn()\n");
432 return -EINVAL;
434 if (!tfo->fabric_drop_wwn) {
435 pr_err("Missing tfo->fabric_drop_wwn()\n");
436 return -EINVAL;
438 if (!tfo->fabric_make_tpg) {
439 pr_err("Missing tfo->fabric_make_tpg()\n");
440 return -EINVAL;
442 if (!tfo->fabric_drop_tpg) {
443 pr_err("Missing tfo->fabric_drop_tpg()\n");
444 return -EINVAL;
447 return 0;
450 int target_register_template(const struct target_core_fabric_ops *fo)
452 struct target_fabric_configfs *tf;
453 int ret;
455 ret = target_fabric_tf_ops_check(fo);
456 if (ret)
457 return ret;
459 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
460 if (!tf) {
461 pr_err("%s: could not allocate memory!\n", __func__);
462 return -ENOMEM;
465 INIT_LIST_HEAD(&tf->tf_list);
466 atomic_set(&tf->tf_access_cnt, 0);
469 * Setup the default generic struct config_item_type's (cits) in
470 * struct target_fabric_configfs->tf_cit_tmpl
472 tf->tf_module = fo->module;
473 snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", fo->name);
475 tf->tf_ops = *fo;
476 target_fabric_setup_cits(tf);
478 mutex_lock(&g_tf_lock);
479 list_add_tail(&tf->tf_list, &g_tf_list);
480 mutex_unlock(&g_tf_lock);
482 return 0;
484 EXPORT_SYMBOL(target_register_template);
486 void target_unregister_template(const struct target_core_fabric_ops *fo)
488 struct target_fabric_configfs *t;
490 mutex_lock(&g_tf_lock);
491 list_for_each_entry(t, &g_tf_list, tf_list) {
492 if (!strcmp(t->tf_name, fo->name)) {
493 BUG_ON(atomic_read(&t->tf_access_cnt));
494 list_del(&t->tf_list);
495 kfree(t);
496 break;
499 mutex_unlock(&g_tf_lock);
501 EXPORT_SYMBOL(target_unregister_template);
503 /*##############################################################################
504 // Stop functions called by external Target Fabrics Modules
505 //############################################################################*/
507 /* Start functions for struct config_item_type tb_dev_attrib_cit */
509 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
510 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
512 static struct configfs_item_operations target_core_dev_attrib_ops = {
513 .show_attribute = target_core_dev_attrib_attr_show,
514 .store_attribute = target_core_dev_attrib_attr_store,
517 TB_CIT_SETUP(dev_attrib, &target_core_dev_attrib_ops, NULL, NULL);
519 /* End functions for struct config_item_type tb_dev_attrib_cit */
521 /* Start functions for struct config_item_type tb_dev_wwn_cit */
523 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
524 #define SE_DEV_WWN_ATTR(_name, _mode) \
525 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
526 __CONFIGFS_EATTR(_name, _mode, \
527 target_core_dev_wwn_show_attr_##_name, \
528 target_core_dev_wwn_store_attr_##_name);
530 #define SE_DEV_WWN_ATTR_RO(_name); \
531 do { \
532 static struct target_core_dev_wwn_attribute \
533 target_core_dev_wwn_##_name = \
534 __CONFIGFS_EATTR_RO(_name, \
535 target_core_dev_wwn_show_attr_##_name); \
536 } while (0);
539 * VPD page 0x80 Unit serial
541 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
542 struct t10_wwn *t10_wwn,
543 char *page)
545 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
546 &t10_wwn->unit_serial[0]);
549 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
550 struct t10_wwn *t10_wwn,
551 const char *page,
552 size_t count)
554 struct se_device *dev = t10_wwn->t10_dev;
555 unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
558 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
559 * from the struct scsi_device level firmware, do not allow
560 * VPD Unit Serial to be emulated.
562 * Note this struct scsi_device could also be emulating VPD
563 * information from its drivers/scsi LLD. But for now we assume
564 * it is doing 'the right thing' wrt a world wide unique
565 * VPD Unit Serial Number that OS dependent multipath can depend on.
567 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
568 pr_err("Underlying SCSI device firmware provided VPD"
569 " Unit Serial, ignoring request\n");
570 return -EOPNOTSUPP;
573 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
574 pr_err("Emulated VPD Unit Serial exceeds"
575 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
576 return -EOVERFLOW;
579 * Check to see if any active $FABRIC_MOD exports exist. If they
580 * do exist, fail here as changing this information on the fly
581 * (underneath the initiator side OS dependent multipath code)
582 * could cause negative effects.
584 if (dev->export_count) {
585 pr_err("Unable to set VPD Unit Serial while"
586 " active %d $FABRIC_MOD exports exist\n",
587 dev->export_count);
588 return -EINVAL;
592 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
594 * Also, strip any newline added from the userspace
595 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
597 memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
598 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
599 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
600 "%s", strstrip(buf));
601 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
603 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
604 " %s\n", dev->t10_wwn.unit_serial);
606 return count;
609 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
612 * VPD page 0x83 Protocol Identifier
614 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
615 struct t10_wwn *t10_wwn,
616 char *page)
618 struct t10_vpd *vpd;
619 unsigned char buf[VPD_TMP_BUF_SIZE];
620 ssize_t len = 0;
622 memset(buf, 0, VPD_TMP_BUF_SIZE);
624 spin_lock(&t10_wwn->t10_vpd_lock);
625 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
626 if (!vpd->protocol_identifier_set)
627 continue;
629 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
631 if (len + strlen(buf) >= PAGE_SIZE)
632 break;
634 len += sprintf(page+len, "%s", buf);
636 spin_unlock(&t10_wwn->t10_vpd_lock);
638 return len;
641 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
642 struct t10_wwn *t10_wwn,
643 const char *page,
644 size_t count)
646 return -ENOSYS;
649 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
652 * Generic wrapper for dumping VPD identifiers by association.
654 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
655 static ssize_t target_core_dev_wwn_show_attr_##_name( \
656 struct t10_wwn *t10_wwn, \
657 char *page) \
659 struct t10_vpd *vpd; \
660 unsigned char buf[VPD_TMP_BUF_SIZE]; \
661 ssize_t len = 0; \
663 spin_lock(&t10_wwn->t10_vpd_lock); \
664 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
665 if (vpd->association != _assoc) \
666 continue; \
668 memset(buf, 0, VPD_TMP_BUF_SIZE); \
669 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
670 if (len + strlen(buf) >= PAGE_SIZE) \
671 break; \
672 len += sprintf(page+len, "%s", buf); \
674 memset(buf, 0, VPD_TMP_BUF_SIZE); \
675 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
676 if (len + strlen(buf) >= PAGE_SIZE) \
677 break; \
678 len += sprintf(page+len, "%s", buf); \
680 memset(buf, 0, VPD_TMP_BUF_SIZE); \
681 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
682 if (len + strlen(buf) >= PAGE_SIZE) \
683 break; \
684 len += sprintf(page+len, "%s", buf); \
686 spin_unlock(&t10_wwn->t10_vpd_lock); \
688 return len; \
692 * VPD page 0x83 Association: Logical Unit
694 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
696 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
697 struct t10_wwn *t10_wwn,
698 const char *page,
699 size_t count)
701 return -ENOSYS;
704 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
707 * VPD page 0x83 Association: Target Port
709 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
711 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
712 struct t10_wwn *t10_wwn,
713 const char *page,
714 size_t count)
716 return -ENOSYS;
719 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
722 * VPD page 0x83 Association: SCSI Target Device
724 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
726 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
727 struct t10_wwn *t10_wwn,
728 const char *page,
729 size_t count)
731 return -ENOSYS;
734 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
736 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
738 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
739 &target_core_dev_wwn_vpd_unit_serial.attr,
740 &target_core_dev_wwn_vpd_protocol_identifier.attr,
741 &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
742 &target_core_dev_wwn_vpd_assoc_target_port.attr,
743 &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
744 NULL,
747 static struct configfs_item_operations target_core_dev_wwn_ops = {
748 .show_attribute = target_core_dev_wwn_attr_show,
749 .store_attribute = target_core_dev_wwn_attr_store,
752 TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
754 /* End functions for struct config_item_type tb_dev_wwn_cit */
756 /* Start functions for struct config_item_type tb_dev_pr_cit */
758 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
759 #define SE_DEV_PR_ATTR(_name, _mode) \
760 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
761 __CONFIGFS_EATTR(_name, _mode, \
762 target_core_dev_pr_show_attr_##_name, \
763 target_core_dev_pr_store_attr_##_name);
765 #define SE_DEV_PR_ATTR_RO(_name); \
766 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
767 __CONFIGFS_EATTR_RO(_name, \
768 target_core_dev_pr_show_attr_##_name);
770 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
771 char *page)
773 struct se_node_acl *se_nacl;
774 struct t10_pr_registration *pr_reg;
775 char i_buf[PR_REG_ISID_ID_LEN];
777 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
779 pr_reg = dev->dev_pr_res_holder;
780 if (!pr_reg)
781 return sprintf(page, "No SPC-3 Reservation holder\n");
783 se_nacl = pr_reg->pr_reg_nacl;
784 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
786 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
787 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
788 se_nacl->initiatorname, i_buf);
791 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
792 char *page)
794 struct se_node_acl *se_nacl;
795 ssize_t len;
797 se_nacl = dev->dev_reserved_node_acl;
798 if (se_nacl) {
799 len = sprintf(page,
800 "SPC-2 Reservation: %s Initiator: %s\n",
801 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
802 se_nacl->initiatorname);
803 } else {
804 len = sprintf(page, "No SPC-2 Reservation holder\n");
806 return len;
809 static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
810 char *page)
812 int ret;
814 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
815 return sprintf(page, "Passthrough\n");
817 spin_lock(&dev->dev_reservation_lock);
818 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
819 ret = target_core_dev_pr_show_spc2_res(dev, page);
820 else
821 ret = target_core_dev_pr_show_spc3_res(dev, page);
822 spin_unlock(&dev->dev_reservation_lock);
823 return ret;
826 SE_DEV_PR_ATTR_RO(res_holder);
828 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
829 struct se_device *dev, char *page)
831 ssize_t len = 0;
833 spin_lock(&dev->dev_reservation_lock);
834 if (!dev->dev_pr_res_holder) {
835 len = sprintf(page, "No SPC-3 Reservation holder\n");
836 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
837 len = sprintf(page, "SPC-3 Reservation: All Target"
838 " Ports registration\n");
839 } else {
840 len = sprintf(page, "SPC-3 Reservation: Single"
841 " Target Port registration\n");
844 spin_unlock(&dev->dev_reservation_lock);
845 return len;
848 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
850 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
851 struct se_device *dev, char *page)
853 return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
856 SE_DEV_PR_ATTR_RO(res_pr_generation);
859 * res_pr_holder_tg_port
861 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
862 struct se_device *dev, char *page)
864 struct se_node_acl *se_nacl;
865 struct se_lun *lun;
866 struct se_portal_group *se_tpg;
867 struct t10_pr_registration *pr_reg;
868 const struct target_core_fabric_ops *tfo;
869 ssize_t len = 0;
871 spin_lock(&dev->dev_reservation_lock);
872 pr_reg = dev->dev_pr_res_holder;
873 if (!pr_reg) {
874 len = sprintf(page, "No SPC-3 Reservation holder\n");
875 goto out_unlock;
878 se_nacl = pr_reg->pr_reg_nacl;
879 se_tpg = se_nacl->se_tpg;
880 lun = pr_reg->pr_reg_tg_pt_lun;
881 tfo = se_tpg->se_tpg_tfo;
883 len += sprintf(page+len, "SPC-3 Reservation: %s"
884 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
885 tfo->tpg_get_wwn(se_tpg));
886 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
887 " Identifier Tag: %hu %s Portal Group Tag: %hu"
888 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
889 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
890 tfo->get_fabric_name(), lun->unpacked_lun);
892 out_unlock:
893 spin_unlock(&dev->dev_reservation_lock);
894 return len;
897 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
899 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
900 struct se_device *dev, char *page)
902 const struct target_core_fabric_ops *tfo;
903 struct t10_pr_registration *pr_reg;
904 unsigned char buf[384];
905 char i_buf[PR_REG_ISID_ID_LEN];
906 ssize_t len = 0;
907 int reg_count = 0;
909 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
911 spin_lock(&dev->t10_pr.registration_lock);
912 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
913 pr_reg_list) {
915 memset(buf, 0, 384);
916 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
917 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
918 core_pr_dump_initiator_port(pr_reg, i_buf,
919 PR_REG_ISID_ID_LEN);
920 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
921 tfo->get_fabric_name(),
922 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
923 pr_reg->pr_res_generation);
925 if (len + strlen(buf) >= PAGE_SIZE)
926 break;
928 len += sprintf(page+len, "%s", buf);
929 reg_count++;
931 spin_unlock(&dev->t10_pr.registration_lock);
933 if (!reg_count)
934 len += sprintf(page+len, "None\n");
936 return len;
939 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
941 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
942 struct se_device *dev, char *page)
944 struct t10_pr_registration *pr_reg;
945 ssize_t len = 0;
947 spin_lock(&dev->dev_reservation_lock);
948 pr_reg = dev->dev_pr_res_holder;
949 if (pr_reg) {
950 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
951 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
952 } else {
953 len = sprintf(page, "No SPC-3 Reservation holder\n");
956 spin_unlock(&dev->dev_reservation_lock);
957 return len;
960 SE_DEV_PR_ATTR_RO(res_pr_type);
962 static ssize_t target_core_dev_pr_show_attr_res_type(
963 struct se_device *dev, char *page)
965 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
966 return sprintf(page, "SPC_PASSTHROUGH\n");
967 else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
968 return sprintf(page, "SPC2_RESERVATIONS\n");
969 else
970 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
973 SE_DEV_PR_ATTR_RO(res_type);
975 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
976 struct se_device *dev, char *page)
978 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
979 return 0;
981 return sprintf(page, "APTPL Bit Status: %s\n",
982 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
985 SE_DEV_PR_ATTR_RO(res_aptpl_active);
988 * res_aptpl_metadata
990 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
991 struct se_device *dev, char *page)
993 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
994 return 0;
996 return sprintf(page, "Ready to process PR APTPL metadata..\n");
999 enum {
1000 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1001 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1002 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1003 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1006 static match_table_t tokens = {
1007 {Opt_initiator_fabric, "initiator_fabric=%s"},
1008 {Opt_initiator_node, "initiator_node=%s"},
1009 {Opt_initiator_sid, "initiator_sid=%s"},
1010 {Opt_sa_res_key, "sa_res_key=%s"},
1011 {Opt_res_holder, "res_holder=%d"},
1012 {Opt_res_type, "res_type=%d"},
1013 {Opt_res_scope, "res_scope=%d"},
1014 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1015 {Opt_mapped_lun, "mapped_lun=%d"},
1016 {Opt_target_fabric, "target_fabric=%s"},
1017 {Opt_target_node, "target_node=%s"},
1018 {Opt_tpgt, "tpgt=%d"},
1019 {Opt_port_rtpi, "port_rtpi=%d"},
1020 {Opt_target_lun, "target_lun=%d"},
1021 {Opt_err, NULL}
1024 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1025 struct se_device *dev,
1026 const char *page,
1027 size_t count)
1029 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1030 unsigned char *t_fabric = NULL, *t_port = NULL;
1031 char *orig, *ptr, *opts;
1032 substring_t args[MAX_OPT_ARGS];
1033 unsigned long long tmp_ll;
1034 u64 sa_res_key = 0;
1035 u32 mapped_lun = 0, target_lun = 0;
1036 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1037 u16 port_rpti = 0, tpgt = 0;
1038 u8 type = 0, scope;
1040 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1041 return 0;
1042 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1043 return 0;
1045 if (dev->export_count) {
1046 pr_debug("Unable to process APTPL metadata while"
1047 " active fabric exports exist\n");
1048 return -EINVAL;
1051 opts = kstrdup(page, GFP_KERNEL);
1052 if (!opts)
1053 return -ENOMEM;
1055 orig = opts;
1056 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1057 if (!*ptr)
1058 continue;
1060 token = match_token(ptr, tokens, args);
1061 switch (token) {
1062 case Opt_initiator_fabric:
1063 i_fabric = match_strdup(args);
1064 if (!i_fabric) {
1065 ret = -ENOMEM;
1066 goto out;
1068 break;
1069 case Opt_initiator_node:
1070 i_port = match_strdup(args);
1071 if (!i_port) {
1072 ret = -ENOMEM;
1073 goto out;
1075 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1076 pr_err("APTPL metadata initiator_node="
1077 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1078 PR_APTPL_MAX_IPORT_LEN);
1079 ret = -EINVAL;
1080 break;
1082 break;
1083 case Opt_initiator_sid:
1084 isid = match_strdup(args);
1085 if (!isid) {
1086 ret = -ENOMEM;
1087 goto out;
1089 if (strlen(isid) >= PR_REG_ISID_LEN) {
1090 pr_err("APTPL metadata initiator_isid"
1091 "= exceeds PR_REG_ISID_LEN: %d\n",
1092 PR_REG_ISID_LEN);
1093 ret = -EINVAL;
1094 break;
1096 break;
1097 case Opt_sa_res_key:
1098 ret = kstrtoull(args->from, 0, &tmp_ll);
1099 if (ret < 0) {
1100 pr_err("kstrtoull() failed for sa_res_key=\n");
1101 goto out;
1103 sa_res_key = (u64)tmp_ll;
1104 break;
1106 * PR APTPL Metadata for Reservation
1108 case Opt_res_holder:
1109 match_int(args, &arg);
1110 res_holder = arg;
1111 break;
1112 case Opt_res_type:
1113 match_int(args, &arg);
1114 type = (u8)arg;
1115 break;
1116 case Opt_res_scope:
1117 match_int(args, &arg);
1118 scope = (u8)arg;
1119 break;
1120 case Opt_res_all_tg_pt:
1121 match_int(args, &arg);
1122 all_tg_pt = (int)arg;
1123 break;
1124 case Opt_mapped_lun:
1125 match_int(args, &arg);
1126 mapped_lun = (u32)arg;
1127 break;
1129 * PR APTPL Metadata for Target Port
1131 case Opt_target_fabric:
1132 t_fabric = match_strdup(args);
1133 if (!t_fabric) {
1134 ret = -ENOMEM;
1135 goto out;
1137 break;
1138 case Opt_target_node:
1139 t_port = match_strdup(args);
1140 if (!t_port) {
1141 ret = -ENOMEM;
1142 goto out;
1144 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1145 pr_err("APTPL metadata target_node="
1146 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1147 PR_APTPL_MAX_TPORT_LEN);
1148 ret = -EINVAL;
1149 break;
1151 break;
1152 case Opt_tpgt:
1153 match_int(args, &arg);
1154 tpgt = (u16)arg;
1155 break;
1156 case Opt_port_rtpi:
1157 match_int(args, &arg);
1158 port_rpti = (u16)arg;
1159 break;
1160 case Opt_target_lun:
1161 match_int(args, &arg);
1162 target_lun = (u32)arg;
1163 break;
1164 default:
1165 break;
1169 if (!i_port || !t_port || !sa_res_key) {
1170 pr_err("Illegal parameters for APTPL registration\n");
1171 ret = -EINVAL;
1172 goto out;
1175 if (res_holder && !(type)) {
1176 pr_err("Illegal PR type: 0x%02x for reservation"
1177 " holder\n", type);
1178 ret = -EINVAL;
1179 goto out;
1182 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1183 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1184 res_holder, all_tg_pt, type);
1185 out:
1186 kfree(i_fabric);
1187 kfree(i_port);
1188 kfree(isid);
1189 kfree(t_fabric);
1190 kfree(t_port);
1191 kfree(orig);
1192 return (ret == 0) ? count : ret;
1195 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1197 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1199 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1200 &target_core_dev_pr_res_holder.attr,
1201 &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1202 &target_core_dev_pr_res_pr_generation.attr,
1203 &target_core_dev_pr_res_pr_holder_tg_port.attr,
1204 &target_core_dev_pr_res_pr_registered_i_pts.attr,
1205 &target_core_dev_pr_res_pr_type.attr,
1206 &target_core_dev_pr_res_type.attr,
1207 &target_core_dev_pr_res_aptpl_active.attr,
1208 &target_core_dev_pr_res_aptpl_metadata.attr,
1209 NULL,
1212 static struct configfs_item_operations target_core_dev_pr_ops = {
1213 .show_attribute = target_core_dev_pr_attr_show,
1214 .store_attribute = target_core_dev_pr_attr_store,
1217 TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
1219 /* End functions for struct config_item_type tb_dev_pr_cit */
1221 /* Start functions for struct config_item_type tb_dev_cit */
1223 static ssize_t target_core_show_dev_info(void *p, char *page)
1225 struct se_device *dev = p;
1226 struct se_subsystem_api *t = dev->transport;
1227 int bl = 0;
1228 ssize_t read_bytes = 0;
1230 transport_dump_dev_state(dev, page, &bl);
1231 read_bytes += bl;
1232 read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1233 return read_bytes;
1236 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1237 .attr = { .ca_owner = THIS_MODULE,
1238 .ca_name = "info",
1239 .ca_mode = S_IRUGO },
1240 .show = target_core_show_dev_info,
1241 .store = NULL,
1244 static ssize_t target_core_store_dev_control(
1245 void *p,
1246 const char *page,
1247 size_t count)
1249 struct se_device *dev = p;
1250 struct se_subsystem_api *t = dev->transport;
1252 return t->set_configfs_dev_params(dev, page, count);
1255 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1256 .attr = { .ca_owner = THIS_MODULE,
1257 .ca_name = "control",
1258 .ca_mode = S_IWUSR },
1259 .show = NULL,
1260 .store = target_core_store_dev_control,
1263 static ssize_t target_core_show_dev_alias(void *p, char *page)
1265 struct se_device *dev = p;
1267 if (!(dev->dev_flags & DF_USING_ALIAS))
1268 return 0;
1270 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1273 static ssize_t target_core_store_dev_alias(
1274 void *p,
1275 const char *page,
1276 size_t count)
1278 struct se_device *dev = p;
1279 struct se_hba *hba = dev->se_hba;
1280 ssize_t read_bytes;
1282 if (count > (SE_DEV_ALIAS_LEN-1)) {
1283 pr_err("alias count: %d exceeds"
1284 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1285 SE_DEV_ALIAS_LEN-1);
1286 return -EINVAL;
1289 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1290 if (!read_bytes)
1291 return -EINVAL;
1292 if (dev->dev_alias[read_bytes - 1] == '\n')
1293 dev->dev_alias[read_bytes - 1] = '\0';
1295 dev->dev_flags |= DF_USING_ALIAS;
1297 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1298 config_item_name(&hba->hba_group.cg_item),
1299 config_item_name(&dev->dev_group.cg_item),
1300 dev->dev_alias);
1302 return read_bytes;
1305 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1306 .attr = { .ca_owner = THIS_MODULE,
1307 .ca_name = "alias",
1308 .ca_mode = S_IRUGO | S_IWUSR },
1309 .show = target_core_show_dev_alias,
1310 .store = target_core_store_dev_alias,
1313 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1315 struct se_device *dev = p;
1317 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1318 return 0;
1320 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1323 static ssize_t target_core_store_dev_udev_path(
1324 void *p,
1325 const char *page,
1326 size_t count)
1328 struct se_device *dev = p;
1329 struct se_hba *hba = dev->se_hba;
1330 ssize_t read_bytes;
1332 if (count > (SE_UDEV_PATH_LEN-1)) {
1333 pr_err("udev_path count: %d exceeds"
1334 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1335 SE_UDEV_PATH_LEN-1);
1336 return -EINVAL;
1339 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1340 "%s", page);
1341 if (!read_bytes)
1342 return -EINVAL;
1343 if (dev->udev_path[read_bytes - 1] == '\n')
1344 dev->udev_path[read_bytes - 1] = '\0';
1346 dev->dev_flags |= DF_USING_UDEV_PATH;
1348 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1349 config_item_name(&hba->hba_group.cg_item),
1350 config_item_name(&dev->dev_group.cg_item),
1351 dev->udev_path);
1353 return read_bytes;
1356 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1357 .attr = { .ca_owner = THIS_MODULE,
1358 .ca_name = "udev_path",
1359 .ca_mode = S_IRUGO | S_IWUSR },
1360 .show = target_core_show_dev_udev_path,
1361 .store = target_core_store_dev_udev_path,
1364 static ssize_t target_core_show_dev_enable(void *p, char *page)
1366 struct se_device *dev = p;
1368 return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1371 static ssize_t target_core_store_dev_enable(
1372 void *p,
1373 const char *page,
1374 size_t count)
1376 struct se_device *dev = p;
1377 char *ptr;
1378 int ret;
1380 ptr = strstr(page, "1");
1381 if (!ptr) {
1382 pr_err("For dev_enable ops, only valid value"
1383 " is \"1\"\n");
1384 return -EINVAL;
1387 ret = target_configure_device(dev);
1388 if (ret)
1389 return ret;
1390 return count;
1393 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1394 .attr = { .ca_owner = THIS_MODULE,
1395 .ca_name = "enable",
1396 .ca_mode = S_IRUGO | S_IWUSR },
1397 .show = target_core_show_dev_enable,
1398 .store = target_core_store_dev_enable,
1401 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1403 struct se_device *dev = p;
1404 struct config_item *lu_ci;
1405 struct t10_alua_lu_gp *lu_gp;
1406 struct t10_alua_lu_gp_member *lu_gp_mem;
1407 ssize_t len = 0;
1409 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1410 if (!lu_gp_mem)
1411 return 0;
1413 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1414 lu_gp = lu_gp_mem->lu_gp;
1415 if (lu_gp) {
1416 lu_ci = &lu_gp->lu_gp_group.cg_item;
1417 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1418 config_item_name(lu_ci), lu_gp->lu_gp_id);
1420 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1422 return len;
1425 static ssize_t target_core_store_alua_lu_gp(
1426 void *p,
1427 const char *page,
1428 size_t count)
1430 struct se_device *dev = p;
1431 struct se_hba *hba = dev->se_hba;
1432 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1433 struct t10_alua_lu_gp_member *lu_gp_mem;
1434 unsigned char buf[LU_GROUP_NAME_BUF];
1435 int move = 0;
1437 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1438 if (!lu_gp_mem)
1439 return 0;
1441 if (count > LU_GROUP_NAME_BUF) {
1442 pr_err("ALUA LU Group Alias too large!\n");
1443 return -EINVAL;
1445 memset(buf, 0, LU_GROUP_NAME_BUF);
1446 memcpy(buf, page, count);
1448 * Any ALUA logical unit alias besides "NULL" means we will be
1449 * making a new group association.
1451 if (strcmp(strstrip(buf), "NULL")) {
1453 * core_alua_get_lu_gp_by_name() will increment reference to
1454 * struct t10_alua_lu_gp. This reference is released with
1455 * core_alua_get_lu_gp_by_name below().
1457 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1458 if (!lu_gp_new)
1459 return -ENODEV;
1462 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1463 lu_gp = lu_gp_mem->lu_gp;
1464 if (lu_gp) {
1466 * Clearing an existing lu_gp association, and replacing
1467 * with NULL
1469 if (!lu_gp_new) {
1470 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1471 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1472 " %hu\n",
1473 config_item_name(&hba->hba_group.cg_item),
1474 config_item_name(&dev->dev_group.cg_item),
1475 config_item_name(&lu_gp->lu_gp_group.cg_item),
1476 lu_gp->lu_gp_id);
1478 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1479 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1481 return count;
1484 * Removing existing association of lu_gp_mem with lu_gp
1486 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1487 move = 1;
1490 * Associate lu_gp_mem with lu_gp_new.
1492 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1493 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1495 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1496 " core/alua/lu_gps/%s, ID: %hu\n",
1497 (move) ? "Moving" : "Adding",
1498 config_item_name(&hba->hba_group.cg_item),
1499 config_item_name(&dev->dev_group.cg_item),
1500 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1501 lu_gp_new->lu_gp_id);
1503 core_alua_put_lu_gp_from_name(lu_gp_new);
1504 return count;
1507 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1508 .attr = { .ca_owner = THIS_MODULE,
1509 .ca_name = "alua_lu_gp",
1510 .ca_mode = S_IRUGO | S_IWUSR },
1511 .show = target_core_show_alua_lu_gp,
1512 .store = target_core_store_alua_lu_gp,
1515 static ssize_t target_core_show_dev_lba_map(void *p, char *page)
1517 struct se_device *dev = p;
1518 struct t10_alua_lba_map *map;
1519 struct t10_alua_lba_map_member *mem;
1520 char *b = page;
1521 int bl = 0;
1522 char state;
1524 spin_lock(&dev->t10_alua.lba_map_lock);
1525 if (!list_empty(&dev->t10_alua.lba_map_list))
1526 bl += sprintf(b + bl, "%u %u\n",
1527 dev->t10_alua.lba_map_segment_size,
1528 dev->t10_alua.lba_map_segment_multiplier);
1529 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
1530 bl += sprintf(b + bl, "%llu %llu",
1531 map->lba_map_first_lba, map->lba_map_last_lba);
1532 list_for_each_entry(mem, &map->lba_map_mem_list,
1533 lba_map_mem_list) {
1534 switch (mem->lba_map_mem_alua_state) {
1535 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
1536 state = 'O';
1537 break;
1538 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1539 state = 'A';
1540 break;
1541 case ALUA_ACCESS_STATE_STANDBY:
1542 state = 'S';
1543 break;
1544 case ALUA_ACCESS_STATE_UNAVAILABLE:
1545 state = 'U';
1546 break;
1547 default:
1548 state = '.';
1549 break;
1551 bl += sprintf(b + bl, " %d:%c",
1552 mem->lba_map_mem_alua_pg_id, state);
1554 bl += sprintf(b + bl, "\n");
1556 spin_unlock(&dev->t10_alua.lba_map_lock);
1557 return bl;
1560 static ssize_t target_core_store_dev_lba_map(
1561 void *p,
1562 const char *page,
1563 size_t count)
1565 struct se_device *dev = p;
1566 struct t10_alua_lba_map *lba_map = NULL;
1567 struct list_head lba_list;
1568 char *map_entries, *ptr;
1569 char state;
1570 int pg_num = -1, pg;
1571 int ret = 0, num = 0, pg_id, alua_state;
1572 unsigned long start_lba = -1, end_lba = -1;
1573 unsigned long segment_size = -1, segment_mult = -1;
1575 map_entries = kstrdup(page, GFP_KERNEL);
1576 if (!map_entries)
1577 return -ENOMEM;
1579 INIT_LIST_HEAD(&lba_list);
1580 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
1581 if (!*ptr)
1582 continue;
1584 if (num == 0) {
1585 if (sscanf(ptr, "%lu %lu\n",
1586 &segment_size, &segment_mult) != 2) {
1587 pr_err("Invalid line %d\n", num);
1588 ret = -EINVAL;
1589 break;
1591 num++;
1592 continue;
1594 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
1595 pr_err("Invalid line %d\n", num);
1596 ret = -EINVAL;
1597 break;
1599 ptr = strchr(ptr, ' ');
1600 if (!ptr) {
1601 pr_err("Invalid line %d, missing end lba\n", num);
1602 ret = -EINVAL;
1603 break;
1605 ptr++;
1606 ptr = strchr(ptr, ' ');
1607 if (!ptr) {
1608 pr_err("Invalid line %d, missing state definitions\n",
1609 num);
1610 ret = -EINVAL;
1611 break;
1613 ptr++;
1614 lba_map = core_alua_allocate_lba_map(&lba_list,
1615 start_lba, end_lba);
1616 if (IS_ERR(lba_map)) {
1617 ret = PTR_ERR(lba_map);
1618 break;
1620 pg = 0;
1621 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
1622 switch (state) {
1623 case 'O':
1624 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1625 break;
1626 case 'A':
1627 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
1628 break;
1629 case 'S':
1630 alua_state = ALUA_ACCESS_STATE_STANDBY;
1631 break;
1632 case 'U':
1633 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
1634 break;
1635 default:
1636 pr_err("Invalid ALUA state '%c'\n", state);
1637 ret = -EINVAL;
1638 goto out;
1641 ret = core_alua_allocate_lba_map_mem(lba_map,
1642 pg_id, alua_state);
1643 if (ret) {
1644 pr_err("Invalid target descriptor %d:%c "
1645 "at line %d\n",
1646 pg_id, state, num);
1647 break;
1649 pg++;
1650 ptr = strchr(ptr, ' ');
1651 if (ptr)
1652 ptr++;
1653 else
1654 break;
1656 if (pg_num == -1)
1657 pg_num = pg;
1658 else if (pg != pg_num) {
1659 pr_err("Only %d from %d port groups definitions "
1660 "at line %d\n", pg, pg_num, num);
1661 ret = -EINVAL;
1662 break;
1664 num++;
1666 out:
1667 if (ret) {
1668 core_alua_free_lba_map(&lba_list);
1669 count = ret;
1670 } else
1671 core_alua_set_lba_map(dev, &lba_list,
1672 segment_size, segment_mult);
1673 kfree(map_entries);
1674 return count;
1677 static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
1678 .attr = { .ca_owner = THIS_MODULE,
1679 .ca_name = "lba_map",
1680 .ca_mode = S_IRUGO | S_IWUSR },
1681 .show = target_core_show_dev_lba_map,
1682 .store = target_core_store_dev_lba_map,
1685 static struct configfs_attribute *target_core_dev_attrs[] = {
1686 &target_core_attr_dev_info.attr,
1687 &target_core_attr_dev_control.attr,
1688 &target_core_attr_dev_alias.attr,
1689 &target_core_attr_dev_udev_path.attr,
1690 &target_core_attr_dev_enable.attr,
1691 &target_core_attr_dev_alua_lu_gp.attr,
1692 &target_core_attr_dev_lba_map.attr,
1693 NULL,
1696 static void target_core_dev_release(struct config_item *item)
1698 struct config_group *dev_cg = to_config_group(item);
1699 struct se_device *dev =
1700 container_of(dev_cg, struct se_device, dev_group);
1702 kfree(dev_cg->default_groups);
1703 target_free_device(dev);
1706 static ssize_t target_core_dev_show(struct config_item *item,
1707 struct configfs_attribute *attr,
1708 char *page)
1710 struct config_group *dev_cg = to_config_group(item);
1711 struct se_device *dev =
1712 container_of(dev_cg, struct se_device, dev_group);
1713 struct target_core_configfs_attribute *tc_attr = container_of(
1714 attr, struct target_core_configfs_attribute, attr);
1716 if (!tc_attr->show)
1717 return -EINVAL;
1719 return tc_attr->show(dev, page);
1722 static ssize_t target_core_dev_store(struct config_item *item,
1723 struct configfs_attribute *attr,
1724 const char *page, size_t count)
1726 struct config_group *dev_cg = to_config_group(item);
1727 struct se_device *dev =
1728 container_of(dev_cg, struct se_device, dev_group);
1729 struct target_core_configfs_attribute *tc_attr = container_of(
1730 attr, struct target_core_configfs_attribute, attr);
1732 if (!tc_attr->store)
1733 return -EINVAL;
1735 return tc_attr->store(dev, page, count);
1738 static struct configfs_item_operations target_core_dev_item_ops = {
1739 .release = target_core_dev_release,
1740 .show_attribute = target_core_dev_show,
1741 .store_attribute = target_core_dev_store,
1744 TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
1746 /* End functions for struct config_item_type tb_dev_cit */
1748 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1750 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1751 #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
1752 static struct target_core_alua_lu_gp_attribute \
1753 target_core_alua_lu_gp_##_name = \
1754 __CONFIGFS_EATTR(_name, _mode, \
1755 target_core_alua_lu_gp_show_attr_##_name, \
1756 target_core_alua_lu_gp_store_attr_##_name);
1758 #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
1759 static struct target_core_alua_lu_gp_attribute \
1760 target_core_alua_lu_gp_##_name = \
1761 __CONFIGFS_EATTR_RO(_name, \
1762 target_core_alua_lu_gp_show_attr_##_name);
1765 * lu_gp_id
1767 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1768 struct t10_alua_lu_gp *lu_gp,
1769 char *page)
1771 if (!lu_gp->lu_gp_valid_id)
1772 return 0;
1774 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1777 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1778 struct t10_alua_lu_gp *lu_gp,
1779 const char *page,
1780 size_t count)
1782 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1783 unsigned long lu_gp_id;
1784 int ret;
1786 ret = kstrtoul(page, 0, &lu_gp_id);
1787 if (ret < 0) {
1788 pr_err("kstrtoul() returned %d for"
1789 " lu_gp_id\n", ret);
1790 return ret;
1792 if (lu_gp_id > 0x0000ffff) {
1793 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1794 " 0x0000ffff\n", lu_gp_id);
1795 return -EINVAL;
1798 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1799 if (ret < 0)
1800 return -EINVAL;
1802 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1803 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1804 config_item_name(&alua_lu_gp_cg->cg_item),
1805 lu_gp->lu_gp_id);
1807 return count;
1810 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1813 * members
1815 static ssize_t target_core_alua_lu_gp_show_attr_members(
1816 struct t10_alua_lu_gp *lu_gp,
1817 char *page)
1819 struct se_device *dev;
1820 struct se_hba *hba;
1821 struct t10_alua_lu_gp_member *lu_gp_mem;
1822 ssize_t len = 0, cur_len;
1823 unsigned char buf[LU_GROUP_NAME_BUF];
1825 memset(buf, 0, LU_GROUP_NAME_BUF);
1827 spin_lock(&lu_gp->lu_gp_lock);
1828 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1829 dev = lu_gp_mem->lu_gp_mem_dev;
1830 hba = dev->se_hba;
1832 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1833 config_item_name(&hba->hba_group.cg_item),
1834 config_item_name(&dev->dev_group.cg_item));
1835 cur_len++; /* Extra byte for NULL terminator */
1837 if ((cur_len + len) > PAGE_SIZE) {
1838 pr_warn("Ran out of lu_gp_show_attr"
1839 "_members buffer\n");
1840 break;
1842 memcpy(page+len, buf, cur_len);
1843 len += cur_len;
1845 spin_unlock(&lu_gp->lu_gp_lock);
1847 return len;
1850 SE_DEV_ALUA_LU_ATTR_RO(members);
1852 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1854 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1855 &target_core_alua_lu_gp_lu_gp_id.attr,
1856 &target_core_alua_lu_gp_members.attr,
1857 NULL,
1860 static void target_core_alua_lu_gp_release(struct config_item *item)
1862 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1863 struct t10_alua_lu_gp, lu_gp_group);
1865 core_alua_free_lu_gp(lu_gp);
1868 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
1869 .release = target_core_alua_lu_gp_release,
1870 .show_attribute = target_core_alua_lu_gp_attr_show,
1871 .store_attribute = target_core_alua_lu_gp_attr_store,
1874 static struct config_item_type target_core_alua_lu_gp_cit = {
1875 .ct_item_ops = &target_core_alua_lu_gp_ops,
1876 .ct_attrs = target_core_alua_lu_gp_attrs,
1877 .ct_owner = THIS_MODULE,
1880 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1882 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1884 static struct config_group *target_core_alua_create_lu_gp(
1885 struct config_group *group,
1886 const char *name)
1888 struct t10_alua_lu_gp *lu_gp;
1889 struct config_group *alua_lu_gp_cg = NULL;
1890 struct config_item *alua_lu_gp_ci = NULL;
1892 lu_gp = core_alua_allocate_lu_gp(name, 0);
1893 if (IS_ERR(lu_gp))
1894 return NULL;
1896 alua_lu_gp_cg = &lu_gp->lu_gp_group;
1897 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1899 config_group_init_type_name(alua_lu_gp_cg, name,
1900 &target_core_alua_lu_gp_cit);
1902 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1903 " Group: core/alua/lu_gps/%s\n",
1904 config_item_name(alua_lu_gp_ci));
1906 return alua_lu_gp_cg;
1910 static void target_core_alua_drop_lu_gp(
1911 struct config_group *group,
1912 struct config_item *item)
1914 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1915 struct t10_alua_lu_gp, lu_gp_group);
1917 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1918 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1919 config_item_name(item), lu_gp->lu_gp_id);
1921 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1922 * -> target_core_alua_lu_gp_release()
1924 config_item_put(item);
1927 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1928 .make_group = &target_core_alua_create_lu_gp,
1929 .drop_item = &target_core_alua_drop_lu_gp,
1932 static struct config_item_type target_core_alua_lu_gps_cit = {
1933 .ct_item_ops = NULL,
1934 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
1935 .ct_owner = THIS_MODULE,
1938 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1940 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1942 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1943 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
1944 static struct target_core_alua_tg_pt_gp_attribute \
1945 target_core_alua_tg_pt_gp_##_name = \
1946 __CONFIGFS_EATTR(_name, _mode, \
1947 target_core_alua_tg_pt_gp_show_attr_##_name, \
1948 target_core_alua_tg_pt_gp_store_attr_##_name);
1950 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
1951 static struct target_core_alua_tg_pt_gp_attribute \
1952 target_core_alua_tg_pt_gp_##_name = \
1953 __CONFIGFS_EATTR_RO(_name, \
1954 target_core_alua_tg_pt_gp_show_attr_##_name);
1957 * alua_access_state
1959 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
1960 struct t10_alua_tg_pt_gp *tg_pt_gp,
1961 char *page)
1963 return sprintf(page, "%d\n",
1964 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
1967 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
1968 struct t10_alua_tg_pt_gp *tg_pt_gp,
1969 const char *page,
1970 size_t count)
1972 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1973 unsigned long tmp;
1974 int new_state, ret;
1976 if (!tg_pt_gp->tg_pt_gp_valid_id) {
1977 pr_err("Unable to do implicit ALUA on non valid"
1978 " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
1979 return -EINVAL;
1981 if (!(dev->dev_flags & DF_CONFIGURED)) {
1982 pr_err("Unable to set alua_access_state while device is"
1983 " not configured\n");
1984 return -ENODEV;
1987 ret = kstrtoul(page, 0, &tmp);
1988 if (ret < 0) {
1989 pr_err("Unable to extract new ALUA access state from"
1990 " %s\n", page);
1991 return ret;
1993 new_state = (int)tmp;
1995 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
1996 pr_err("Unable to process implicit configfs ALUA"
1997 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
1998 return -EINVAL;
2000 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2001 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2002 /* LBA DEPENDENT is only allowed with implicit ALUA */
2003 pr_err("Unable to process implicit configfs ALUA transition"
2004 " while explicit ALUA management is enabled\n");
2005 return -EINVAL;
2008 ret = core_alua_do_port_transition(tg_pt_gp, dev,
2009 NULL, NULL, new_state, 0);
2010 return (!ret) ? count : -EINVAL;
2013 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2016 * alua_access_status
2018 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2019 struct t10_alua_tg_pt_gp *tg_pt_gp,
2020 char *page)
2022 return sprintf(page, "%s\n",
2023 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2026 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2027 struct t10_alua_tg_pt_gp *tg_pt_gp,
2028 const char *page,
2029 size_t count)
2031 unsigned long tmp;
2032 int new_status, ret;
2034 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2035 pr_err("Unable to do set ALUA access status on non"
2036 " valid tg_pt_gp ID: %hu\n",
2037 tg_pt_gp->tg_pt_gp_valid_id);
2038 return -EINVAL;
2041 ret = kstrtoul(page, 0, &tmp);
2042 if (ret < 0) {
2043 pr_err("Unable to extract new ALUA access status"
2044 " from %s\n", page);
2045 return ret;
2047 new_status = (int)tmp;
2049 if ((new_status != ALUA_STATUS_NONE) &&
2050 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2051 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2052 pr_err("Illegal ALUA access status: 0x%02x\n",
2053 new_status);
2054 return -EINVAL;
2057 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2058 return count;
2061 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2064 * alua_access_type
2066 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2067 struct t10_alua_tg_pt_gp *tg_pt_gp,
2068 char *page)
2070 return core_alua_show_access_type(tg_pt_gp, page);
2073 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2074 struct t10_alua_tg_pt_gp *tg_pt_gp,
2075 const char *page,
2076 size_t count)
2078 return core_alua_store_access_type(tg_pt_gp, page, count);
2081 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2084 * alua_supported_states
2087 #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
2088 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
2089 struct t10_alua_tg_pt_gp *t, char *p) \
2091 return sprintf(p, "%d\n", !!(t->_var & _bit)); \
2094 #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
2095 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
2096 struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
2098 unsigned long tmp; \
2099 int ret; \
2101 if (!t->tg_pt_gp_valid_id) { \
2102 pr_err("Unable to do set ##_name ALUA state on non" \
2103 " valid tg_pt_gp ID: %hu\n", \
2104 t->tg_pt_gp_valid_id); \
2105 return -EINVAL; \
2108 ret = kstrtoul(p, 0, &tmp); \
2109 if (ret < 0) { \
2110 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
2111 return -EINVAL; \
2113 if (tmp > 1) { \
2114 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2115 return -EINVAL; \
2117 if (tmp) \
2118 t->_var |= _bit; \
2119 else \
2120 t->_var &= ~_bit; \
2122 return c; \
2125 SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
2126 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2127 SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
2128 tg_pt_gp_alua_supported_states, ALUA_T_SUP);
2129 SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
2131 SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
2132 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2133 SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
2134 tg_pt_gp_alua_supported_states, ALUA_O_SUP);
2135 SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
2137 SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
2138 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2139 SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
2140 tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
2141 SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
2143 SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
2144 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2145 SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
2146 tg_pt_gp_alua_supported_states, ALUA_U_SUP);
2147 SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
2149 SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
2150 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2151 SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
2152 tg_pt_gp_alua_supported_states, ALUA_S_SUP);
2153 SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
2155 SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
2156 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2157 SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
2158 tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
2159 SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
2161 SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
2162 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2163 SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
2164 tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
2165 SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
2168 * alua_write_metadata
2170 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2171 struct t10_alua_tg_pt_gp *tg_pt_gp,
2172 char *page)
2174 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2177 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2178 struct t10_alua_tg_pt_gp *tg_pt_gp,
2179 const char *page,
2180 size_t count)
2182 unsigned long tmp;
2183 int ret;
2185 ret = kstrtoul(page, 0, &tmp);
2186 if (ret < 0) {
2187 pr_err("Unable to extract alua_write_metadata\n");
2188 return ret;
2191 if ((tmp != 0) && (tmp != 1)) {
2192 pr_err("Illegal value for alua_write_metadata:"
2193 " %lu\n", tmp);
2194 return -EINVAL;
2196 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2198 return count;
2201 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2206 * nonop_delay_msecs
2208 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2209 struct t10_alua_tg_pt_gp *tg_pt_gp,
2210 char *page)
2212 return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2216 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2217 struct t10_alua_tg_pt_gp *tg_pt_gp,
2218 const char *page,
2219 size_t count)
2221 return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2224 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2227 * trans_delay_msecs
2229 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2230 struct t10_alua_tg_pt_gp *tg_pt_gp,
2231 char *page)
2233 return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2236 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2237 struct t10_alua_tg_pt_gp *tg_pt_gp,
2238 const char *page,
2239 size_t count)
2241 return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2244 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2247 * implicit_trans_secs
2249 static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
2250 struct t10_alua_tg_pt_gp *tg_pt_gp,
2251 char *page)
2253 return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
2256 static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
2257 struct t10_alua_tg_pt_gp *tg_pt_gp,
2258 const char *page,
2259 size_t count)
2261 return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
2264 SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
2267 * preferred
2270 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2271 struct t10_alua_tg_pt_gp *tg_pt_gp,
2272 char *page)
2274 return core_alua_show_preferred_bit(tg_pt_gp, page);
2277 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2278 struct t10_alua_tg_pt_gp *tg_pt_gp,
2279 const char *page,
2280 size_t count)
2282 return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2285 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2288 * tg_pt_gp_id
2290 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2291 struct t10_alua_tg_pt_gp *tg_pt_gp,
2292 char *page)
2294 if (!tg_pt_gp->tg_pt_gp_valid_id)
2295 return 0;
2297 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2300 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2301 struct t10_alua_tg_pt_gp *tg_pt_gp,
2302 const char *page,
2303 size_t count)
2305 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2306 unsigned long tg_pt_gp_id;
2307 int ret;
2309 ret = kstrtoul(page, 0, &tg_pt_gp_id);
2310 if (ret < 0) {
2311 pr_err("kstrtoul() returned %d for"
2312 " tg_pt_gp_id\n", ret);
2313 return ret;
2315 if (tg_pt_gp_id > 0x0000ffff) {
2316 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2317 " 0x0000ffff\n", tg_pt_gp_id);
2318 return -EINVAL;
2321 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2322 if (ret < 0)
2323 return -EINVAL;
2325 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2326 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2327 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2328 tg_pt_gp->tg_pt_gp_id);
2330 return count;
2333 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2336 * members
2338 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2339 struct t10_alua_tg_pt_gp *tg_pt_gp,
2340 char *page)
2342 struct se_port *port;
2343 struct se_portal_group *tpg;
2344 struct se_lun *lun;
2345 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2346 ssize_t len = 0, cur_len;
2347 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2349 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2351 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2352 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2353 tg_pt_gp_mem_list) {
2354 port = tg_pt_gp_mem->tg_pt;
2355 tpg = port->sep_tpg;
2356 lun = port->sep_lun;
2358 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2359 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2360 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2361 tpg->se_tpg_tfo->tpg_get_tag(tpg),
2362 config_item_name(&lun->lun_group.cg_item));
2363 cur_len++; /* Extra byte for NULL terminator */
2365 if ((cur_len + len) > PAGE_SIZE) {
2366 pr_warn("Ran out of lu_gp_show_attr"
2367 "_members buffer\n");
2368 break;
2370 memcpy(page+len, buf, cur_len);
2371 len += cur_len;
2373 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2375 return len;
2378 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2380 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2381 tg_pt_gp_group);
2383 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2384 &target_core_alua_tg_pt_gp_alua_access_state.attr,
2385 &target_core_alua_tg_pt_gp_alua_access_status.attr,
2386 &target_core_alua_tg_pt_gp_alua_access_type.attr,
2387 &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
2388 &target_core_alua_tg_pt_gp_alua_support_offline.attr,
2389 &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
2390 &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
2391 &target_core_alua_tg_pt_gp_alua_support_standby.attr,
2392 &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
2393 &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
2394 &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2395 &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2396 &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2397 &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
2398 &target_core_alua_tg_pt_gp_preferred.attr,
2399 &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2400 &target_core_alua_tg_pt_gp_members.attr,
2401 NULL,
2404 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2406 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2407 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2409 core_alua_free_tg_pt_gp(tg_pt_gp);
2412 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2413 .release = target_core_alua_tg_pt_gp_release,
2414 .show_attribute = target_core_alua_tg_pt_gp_attr_show,
2415 .store_attribute = target_core_alua_tg_pt_gp_attr_store,
2418 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2419 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
2420 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
2421 .ct_owner = THIS_MODULE,
2424 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2426 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2428 static struct config_group *target_core_alua_create_tg_pt_gp(
2429 struct config_group *group,
2430 const char *name)
2432 struct t10_alua *alua = container_of(group, struct t10_alua,
2433 alua_tg_pt_gps_group);
2434 struct t10_alua_tg_pt_gp *tg_pt_gp;
2435 struct config_group *alua_tg_pt_gp_cg = NULL;
2436 struct config_item *alua_tg_pt_gp_ci = NULL;
2438 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2439 if (!tg_pt_gp)
2440 return NULL;
2442 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2443 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2445 config_group_init_type_name(alua_tg_pt_gp_cg, name,
2446 &target_core_alua_tg_pt_gp_cit);
2448 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2449 " Group: alua/tg_pt_gps/%s\n",
2450 config_item_name(alua_tg_pt_gp_ci));
2452 return alua_tg_pt_gp_cg;
2455 static void target_core_alua_drop_tg_pt_gp(
2456 struct config_group *group,
2457 struct config_item *item)
2459 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2460 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2462 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2463 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2464 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2466 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2467 * -> target_core_alua_tg_pt_gp_release().
2469 config_item_put(item);
2472 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2473 .make_group = &target_core_alua_create_tg_pt_gp,
2474 .drop_item = &target_core_alua_drop_tg_pt_gp,
2477 TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2479 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2481 /* Start functions for struct config_item_type target_core_alua_cit */
2484 * target_core_alua_cit is a ConfigFS group that lives under
2485 * /sys/kernel/config/target/core/alua. There are default groups
2486 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2487 * target_core_alua_cit in target_core_init_configfs() below.
2489 static struct config_item_type target_core_alua_cit = {
2490 .ct_item_ops = NULL,
2491 .ct_attrs = NULL,
2492 .ct_owner = THIS_MODULE,
2495 /* End functions for struct config_item_type target_core_alua_cit */
2497 /* Start functions for struct config_item_type tb_dev_stat_cit */
2499 static struct config_group *target_core_stat_mkdir(
2500 struct config_group *group,
2501 const char *name)
2503 return ERR_PTR(-ENOSYS);
2506 static void target_core_stat_rmdir(
2507 struct config_group *group,
2508 struct config_item *item)
2510 return;
2513 static struct configfs_group_operations target_core_stat_group_ops = {
2514 .make_group = &target_core_stat_mkdir,
2515 .drop_item = &target_core_stat_rmdir,
2518 TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
2520 /* End functions for struct config_item_type tb_dev_stat_cit */
2522 /* Start functions for struct config_item_type target_core_hba_cit */
2524 static struct config_group *target_core_make_subdev(
2525 struct config_group *group,
2526 const char *name)
2528 struct t10_alua_tg_pt_gp *tg_pt_gp;
2529 struct se_subsystem_api *t;
2530 struct config_item *hba_ci = &group->cg_item;
2531 struct se_hba *hba = item_to_hba(hba_ci);
2532 struct se_device *dev;
2533 struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2534 struct config_group *dev_stat_grp = NULL;
2535 int errno = -ENOMEM, ret;
2537 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2538 if (ret)
2539 return ERR_PTR(ret);
2541 * Locate the struct se_subsystem_api from parent's struct se_hba.
2543 t = hba->transport;
2545 dev = target_alloc_device(hba, name);
2546 if (!dev)
2547 goto out_unlock;
2549 dev_cg = &dev->dev_group;
2551 dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2552 GFP_KERNEL);
2553 if (!dev_cg->default_groups)
2554 goto out_free_device;
2556 config_group_init_type_name(dev_cg, name, &t->tb_cits.tb_dev_cit);
2557 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2558 &t->tb_cits.tb_dev_attrib_cit);
2559 config_group_init_type_name(&dev->dev_pr_group, "pr",
2560 &t->tb_cits.tb_dev_pr_cit);
2561 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2562 &t->tb_cits.tb_dev_wwn_cit);
2563 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
2564 "alua", &t->tb_cits.tb_dev_alua_tg_pt_gps_cit);
2565 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2566 "statistics", &t->tb_cits.tb_dev_stat_cit);
2568 dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2569 dev_cg->default_groups[1] = &dev->dev_pr_group;
2570 dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2571 dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2572 dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
2573 dev_cg->default_groups[5] = NULL;
2575 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2577 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
2578 if (!tg_pt_gp)
2579 goto out_free_dev_cg_default_groups;
2580 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2582 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2583 tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2584 GFP_KERNEL);
2585 if (!tg_pt_gp_cg->default_groups) {
2586 pr_err("Unable to allocate tg_pt_gp_cg->"
2587 "default_groups\n");
2588 goto out_free_tg_pt_gp;
2591 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2592 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2593 tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2594 tg_pt_gp_cg->default_groups[1] = NULL;
2596 * Add core/$HBA/$DEV/statistics/ default groups
2598 dev_stat_grp = &dev->dev_stat_grps.stat_group;
2599 dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
2600 GFP_KERNEL);
2601 if (!dev_stat_grp->default_groups) {
2602 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2603 goto out_free_tg_pt_gp_cg_default_groups;
2605 target_stat_setup_dev_default_groups(dev);
2607 mutex_unlock(&hba->hba_access_mutex);
2608 return dev_cg;
2610 out_free_tg_pt_gp_cg_default_groups:
2611 kfree(tg_pt_gp_cg->default_groups);
2612 out_free_tg_pt_gp:
2613 core_alua_free_tg_pt_gp(tg_pt_gp);
2614 out_free_dev_cg_default_groups:
2615 kfree(dev_cg->default_groups);
2616 out_free_device:
2617 target_free_device(dev);
2618 out_unlock:
2619 mutex_unlock(&hba->hba_access_mutex);
2620 return ERR_PTR(errno);
2623 static void target_core_drop_subdev(
2624 struct config_group *group,
2625 struct config_item *item)
2627 struct config_group *dev_cg = to_config_group(item);
2628 struct se_device *dev =
2629 container_of(dev_cg, struct se_device, dev_group);
2630 struct se_hba *hba;
2631 struct config_item *df_item;
2632 struct config_group *tg_pt_gp_cg, *dev_stat_grp;
2633 int i;
2635 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2637 mutex_lock(&hba->hba_access_mutex);
2639 dev_stat_grp = &dev->dev_stat_grps.stat_group;
2640 for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2641 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2642 dev_stat_grp->default_groups[i] = NULL;
2643 config_item_put(df_item);
2645 kfree(dev_stat_grp->default_groups);
2647 tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2648 for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2649 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2650 tg_pt_gp_cg->default_groups[i] = NULL;
2651 config_item_put(df_item);
2653 kfree(tg_pt_gp_cg->default_groups);
2655 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2656 * directly from target_core_alua_tg_pt_gp_release().
2658 dev->t10_alua.default_tg_pt_gp = NULL;
2660 for (i = 0; dev_cg->default_groups[i]; i++) {
2661 df_item = &dev_cg->default_groups[i]->cg_item;
2662 dev_cg->default_groups[i] = NULL;
2663 config_item_put(df_item);
2666 * se_dev is released from target_core_dev_item_ops->release()
2668 config_item_put(item);
2669 mutex_unlock(&hba->hba_access_mutex);
2672 static struct configfs_group_operations target_core_hba_group_ops = {
2673 .make_group = target_core_make_subdev,
2674 .drop_item = target_core_drop_subdev,
2677 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2678 #define SE_HBA_ATTR(_name, _mode) \
2679 static struct target_core_hba_attribute \
2680 target_core_hba_##_name = \
2681 __CONFIGFS_EATTR(_name, _mode, \
2682 target_core_hba_show_attr_##_name, \
2683 target_core_hba_store_attr_##_name);
2685 #define SE_HBA_ATTR_RO(_name) \
2686 static struct target_core_hba_attribute \
2687 target_core_hba_##_name = \
2688 __CONFIGFS_EATTR_RO(_name, \
2689 target_core_hba_show_attr_##_name);
2691 static ssize_t target_core_hba_show_attr_hba_info(
2692 struct se_hba *hba,
2693 char *page)
2695 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2696 hba->hba_id, hba->transport->name,
2697 TARGET_CORE_CONFIGFS_VERSION);
2700 SE_HBA_ATTR_RO(hba_info);
2702 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2703 char *page)
2705 int hba_mode = 0;
2707 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2708 hba_mode = 1;
2710 return sprintf(page, "%d\n", hba_mode);
2713 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2714 const char *page, size_t count)
2716 struct se_subsystem_api *transport = hba->transport;
2717 unsigned long mode_flag;
2718 int ret;
2720 if (transport->pmode_enable_hba == NULL)
2721 return -EINVAL;
2723 ret = kstrtoul(page, 0, &mode_flag);
2724 if (ret < 0) {
2725 pr_err("Unable to extract hba mode flag: %d\n", ret);
2726 return ret;
2729 if (hba->dev_count) {
2730 pr_err("Unable to set hba_mode with active devices\n");
2731 return -EINVAL;
2734 ret = transport->pmode_enable_hba(hba, mode_flag);
2735 if (ret < 0)
2736 return -EINVAL;
2737 if (ret > 0)
2738 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2739 else if (ret == 0)
2740 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2742 return count;
2745 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2747 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2749 static void target_core_hba_release(struct config_item *item)
2751 struct se_hba *hba = container_of(to_config_group(item),
2752 struct se_hba, hba_group);
2753 core_delete_hba(hba);
2756 static struct configfs_attribute *target_core_hba_attrs[] = {
2757 &target_core_hba_hba_info.attr,
2758 &target_core_hba_hba_mode.attr,
2759 NULL,
2762 static struct configfs_item_operations target_core_hba_item_ops = {
2763 .release = target_core_hba_release,
2764 .show_attribute = target_core_hba_attr_show,
2765 .store_attribute = target_core_hba_attr_store,
2768 static struct config_item_type target_core_hba_cit = {
2769 .ct_item_ops = &target_core_hba_item_ops,
2770 .ct_group_ops = &target_core_hba_group_ops,
2771 .ct_attrs = target_core_hba_attrs,
2772 .ct_owner = THIS_MODULE,
2775 static struct config_group *target_core_call_addhbatotarget(
2776 struct config_group *group,
2777 const char *name)
2779 char *se_plugin_str, *str, *str2;
2780 struct se_hba *hba;
2781 char buf[TARGET_CORE_NAME_MAX_LEN];
2782 unsigned long plugin_dep_id = 0;
2783 int ret;
2785 memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
2786 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
2787 pr_err("Passed *name strlen(): %d exceeds"
2788 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2789 TARGET_CORE_NAME_MAX_LEN);
2790 return ERR_PTR(-ENAMETOOLONG);
2792 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2794 str = strstr(buf, "_");
2795 if (!str) {
2796 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2797 return ERR_PTR(-EINVAL);
2799 se_plugin_str = buf;
2801 * Special case for subsystem plugins that have "_" in their names.
2802 * Namely rd_direct and rd_mcp..
2804 str2 = strstr(str+1, "_");
2805 if (str2) {
2806 *str2 = '\0'; /* Terminate for *se_plugin_str */
2807 str2++; /* Skip to start of plugin dependent ID */
2808 str = str2;
2809 } else {
2810 *str = '\0'; /* Terminate for *se_plugin_str */
2811 str++; /* Skip to start of plugin dependent ID */
2814 ret = kstrtoul(str, 0, &plugin_dep_id);
2815 if (ret < 0) {
2816 pr_err("kstrtoul() returned %d for"
2817 " plugin_dep_id\n", ret);
2818 return ERR_PTR(ret);
2821 * Load up TCM subsystem plugins if they have not already been loaded.
2823 transport_subsystem_check_init();
2825 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2826 if (IS_ERR(hba))
2827 return ERR_CAST(hba);
2829 config_group_init_type_name(&hba->hba_group, name,
2830 &target_core_hba_cit);
2832 return &hba->hba_group;
2835 static void target_core_call_delhbafromtarget(
2836 struct config_group *group,
2837 struct config_item *item)
2840 * core_delete_hba() is called from target_core_hba_item_ops->release()
2841 * -> target_core_hba_release()
2843 config_item_put(item);
2846 static struct configfs_group_operations target_core_group_ops = {
2847 .make_group = target_core_call_addhbatotarget,
2848 .drop_item = target_core_call_delhbafromtarget,
2851 static struct config_item_type target_core_cit = {
2852 .ct_item_ops = NULL,
2853 .ct_group_ops = &target_core_group_ops,
2854 .ct_attrs = NULL,
2855 .ct_owner = THIS_MODULE,
2858 /* Stop functions for struct config_item_type target_core_hba_cit */
2860 void target_core_setup_sub_cits(struct se_subsystem_api *sa)
2862 target_core_setup_dev_cit(sa);
2863 target_core_setup_dev_attrib_cit(sa);
2864 target_core_setup_dev_pr_cit(sa);
2865 target_core_setup_dev_wwn_cit(sa);
2866 target_core_setup_dev_alua_tg_pt_gps_cit(sa);
2867 target_core_setup_dev_stat_cit(sa);
2869 EXPORT_SYMBOL(target_core_setup_sub_cits);
2871 static int __init target_core_init_configfs(void)
2873 struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2874 struct config_group *lu_gp_cg = NULL;
2875 struct configfs_subsystem *subsys = &target_core_fabrics;
2876 struct t10_alua_lu_gp *lu_gp;
2877 int ret;
2879 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2880 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2881 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2883 config_group_init(&subsys->su_group);
2884 mutex_init(&subsys->su_mutex);
2886 ret = init_se_kmem_caches();
2887 if (ret < 0)
2888 return ret;
2890 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2891 * and ALUA Logical Unit Group and Target Port Group infrastructure.
2893 target_cg = &subsys->su_group;
2894 target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2895 GFP_KERNEL);
2896 if (!target_cg->default_groups) {
2897 pr_err("Unable to allocate target_cg->default_groups\n");
2898 ret = -ENOMEM;
2899 goto out_global;
2902 config_group_init_type_name(&target_core_hbagroup,
2903 "core", &target_core_cit);
2904 target_cg->default_groups[0] = &target_core_hbagroup;
2905 target_cg->default_groups[1] = NULL;
2907 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2909 hba_cg = &target_core_hbagroup;
2910 hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2911 GFP_KERNEL);
2912 if (!hba_cg->default_groups) {
2913 pr_err("Unable to allocate hba_cg->default_groups\n");
2914 ret = -ENOMEM;
2915 goto out_global;
2917 config_group_init_type_name(&alua_group,
2918 "alua", &target_core_alua_cit);
2919 hba_cg->default_groups[0] = &alua_group;
2920 hba_cg->default_groups[1] = NULL;
2922 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2923 * groups under /sys/kernel/config/target/core/alua/
2925 alua_cg = &alua_group;
2926 alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2927 GFP_KERNEL);
2928 if (!alua_cg->default_groups) {
2929 pr_err("Unable to allocate alua_cg->default_groups\n");
2930 ret = -ENOMEM;
2931 goto out_global;
2934 config_group_init_type_name(&alua_lu_gps_group,
2935 "lu_gps", &target_core_alua_lu_gps_cit);
2936 alua_cg->default_groups[0] = &alua_lu_gps_group;
2937 alua_cg->default_groups[1] = NULL;
2939 * Add core/alua/lu_gps/default_lu_gp
2941 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
2942 if (IS_ERR(lu_gp)) {
2943 ret = -ENOMEM;
2944 goto out_global;
2947 lu_gp_cg = &alua_lu_gps_group;
2948 lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2949 GFP_KERNEL);
2950 if (!lu_gp_cg->default_groups) {
2951 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
2952 ret = -ENOMEM;
2953 goto out_global;
2956 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2957 &target_core_alua_lu_gp_cit);
2958 lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2959 lu_gp_cg->default_groups[1] = NULL;
2960 default_lu_gp = lu_gp;
2962 * Register the target_core_mod subsystem with configfs.
2964 ret = configfs_register_subsystem(subsys);
2965 if (ret < 0) {
2966 pr_err("Error %d while registering subsystem %s\n",
2967 ret, subsys->su_group.cg_item.ci_namebuf);
2968 goto out_global;
2970 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
2971 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2972 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2974 * Register built-in RAMDISK subsystem logic for virtual LUN 0
2976 ret = rd_module_init();
2977 if (ret < 0)
2978 goto out;
2980 ret = core_dev_setup_virtual_lun0();
2981 if (ret < 0)
2982 goto out;
2984 ret = target_xcopy_setup_pt();
2985 if (ret < 0)
2986 goto out;
2988 return 0;
2990 out:
2991 configfs_unregister_subsystem(subsys);
2992 core_dev_release_virtual_lun0();
2993 rd_module_exit();
2994 out_global:
2995 if (default_lu_gp) {
2996 core_alua_free_lu_gp(default_lu_gp);
2997 default_lu_gp = NULL;
2999 if (lu_gp_cg)
3000 kfree(lu_gp_cg->default_groups);
3001 if (alua_cg)
3002 kfree(alua_cg->default_groups);
3003 if (hba_cg)
3004 kfree(hba_cg->default_groups);
3005 kfree(target_cg->default_groups);
3006 release_se_kmem_caches();
3007 return ret;
3010 static void __exit target_core_exit_configfs(void)
3012 struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3013 struct config_item *item;
3014 int i;
3016 lu_gp_cg = &alua_lu_gps_group;
3017 for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3018 item = &lu_gp_cg->default_groups[i]->cg_item;
3019 lu_gp_cg->default_groups[i] = NULL;
3020 config_item_put(item);
3022 kfree(lu_gp_cg->default_groups);
3023 lu_gp_cg->default_groups = NULL;
3025 alua_cg = &alua_group;
3026 for (i = 0; alua_cg->default_groups[i]; i++) {
3027 item = &alua_cg->default_groups[i]->cg_item;
3028 alua_cg->default_groups[i] = NULL;
3029 config_item_put(item);
3031 kfree(alua_cg->default_groups);
3032 alua_cg->default_groups = NULL;
3034 hba_cg = &target_core_hbagroup;
3035 for (i = 0; hba_cg->default_groups[i]; i++) {
3036 item = &hba_cg->default_groups[i]->cg_item;
3037 hba_cg->default_groups[i] = NULL;
3038 config_item_put(item);
3040 kfree(hba_cg->default_groups);
3041 hba_cg->default_groups = NULL;
3043 * We expect subsys->su_group.default_groups to be released
3044 * by configfs subsystem provider logic..
3046 configfs_unregister_subsystem(&target_core_fabrics);
3047 kfree(target_core_fabrics.su_group.default_groups);
3049 core_alua_free_lu_gp(default_lu_gp);
3050 default_lu_gp = NULL;
3052 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3053 " Infrastructure\n");
3055 core_dev_release_virtual_lun0();
3056 rd_module_exit();
3057 target_xcopy_release_pt();
3058 release_se_kmem_caches();
3061 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3062 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3063 MODULE_LICENSE("GPL");
3065 module_init(target_core_init_configfs);
3066 module_exit(target_core_exit_configfs);