1 /*******************************************************************************
2 * This file contains the configfs implementation for iSCSI Target mode
3 * from the LIO-Target Project.
5 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 ****************************************************************************/
22 #include <linux/configfs.h>
23 #include <linux/export.h>
24 #include <linux/inet.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
27 #include <target/target_core_fabric_configfs.h>
28 #include <target/target_core_configfs.h>
29 #include <target/configfs_macros.h>
31 #include "iscsi_target_core.h"
32 #include "iscsi_target_parameters.h"
33 #include "iscsi_target_device.h"
34 #include "iscsi_target_erl0.h"
35 #include "iscsi_target_nodeattrib.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_stat.h"
40 #include "iscsi_target_configfs.h"
42 struct target_fabric_configfs *lio_target_fabric_configfs;
44 struct lio_target_configfs_attribute {
45 struct configfs_attribute attr;
46 ssize_t (*show)(void *, char *);
47 ssize_t (*store)(void *, const char *, size_t);
50 struct iscsi_portal_group *lio_get_tpg_from_tpg_item(
51 struct config_item *item,
52 struct iscsi_tiqn **tiqn_out)
54 struct se_portal_group *se_tpg = container_of(to_config_group(item),
55 struct se_portal_group, tpg_group);
56 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
60 pr_err("Unable to locate struct iscsi_portal_group "
64 ret = iscsit_get_tpg(tpg);
68 *tiqn_out = tpg->tpg_tiqn;
72 /* Start items for lio_target_portal_cit */
74 static ssize_t lio_target_np_show_sctp(
75 struct se_tpg_np *se_tpg_np,
78 struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
79 struct iscsi_tpg_np, se_tpg_np);
80 struct iscsi_tpg_np *tpg_np_sctp;
83 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
85 rb = sprintf(page, "1\n");
87 rb = sprintf(page, "0\n");
92 static ssize_t lio_target_np_store_sctp(
93 struct se_tpg_np *se_tpg_np,
98 struct iscsi_portal_group *tpg;
99 struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
100 struct iscsi_tpg_np, se_tpg_np);
101 struct iscsi_tpg_np *tpg_np_sctp = NULL;
106 op = simple_strtoul(page, &endptr, 0);
107 if ((op != 1) && (op != 0)) {
108 pr_err("Illegal value for tpg_enable: %u\n", op);
113 pr_err("Unable to locate struct iscsi_np from"
114 " struct iscsi_tpg_np\n");
119 if (iscsit_get_tpg(tpg) < 0)
124 * Use existing np->np_sockaddr for SCTP network portal reference
126 tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
127 np->np_ip, tpg_np, ISCSI_SCTP_TCP);
128 if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
131 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
135 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
147 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
149 static struct configfs_attribute *lio_target_portal_attrs[] = {
150 &lio_target_np_sctp.attr,
154 /* Stop items for lio_target_portal_cit */
156 /* Start items for lio_target_np_cit */
158 #define MAX_PORTAL_LEN 256
160 struct se_tpg_np *lio_target_call_addnptotpg(
161 struct se_portal_group *se_tpg,
162 struct config_group *group,
165 struct iscsi_portal_group *tpg;
166 struct iscsi_tpg_np *tpg_np;
167 char *str, *str2, *ip_str, *port_str;
168 struct __kernel_sockaddr_storage sockaddr;
169 struct sockaddr_in *sock_in;
170 struct sockaddr_in6 *sock_in6;
173 char buf[MAX_PORTAL_LEN + 1];
175 if (strlen(name) > MAX_PORTAL_LEN) {
176 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
177 (int)strlen(name), MAX_PORTAL_LEN);
178 return ERR_PTR(-EOVERFLOW);
180 memset(buf, 0, MAX_PORTAL_LEN + 1);
181 snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
183 memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
185 str = strstr(buf, "[");
189 str2 = strstr(str, "]");
191 pr_err("Unable to locate trailing \"]\""
192 " in IPv6 iSCSI network portal address\n");
193 return ERR_PTR(-EINVAL);
195 str++; /* Skip over leading "[" */
196 *str2 = '\0'; /* Terminate the IPv6 address */
197 str2++; /* Skip over the "]" */
198 port_str = strstr(str2, ":");
200 pr_err("Unable to locate \":port\""
201 " in IPv6 iSCSI network portal address\n");
202 return ERR_PTR(-EINVAL);
204 *port_str = '\0'; /* Terminate string for IP */
205 port_str++; /* Skip over ":" */
207 ret = strict_strtoul(port_str, 0, &port);
209 pr_err("strict_strtoul() failed for port_str: %d\n", ret);
212 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
213 sock_in6->sin6_family = AF_INET6;
214 sock_in6->sin6_port = htons((unsigned short)port);
215 ret = in6_pton(str, IPV6_ADDRESS_SPACE,
216 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
218 pr_err("in6_pton returned: %d\n", ret);
219 return ERR_PTR(-EINVAL);
222 str = ip_str = &buf[0];
223 port_str = strstr(ip_str, ":");
225 pr_err("Unable to locate \":port\""
226 " in IPv4 iSCSI network portal address\n");
227 return ERR_PTR(-EINVAL);
229 *port_str = '\0'; /* Terminate string for IP */
230 port_str++; /* Skip over ":" */
232 ret = strict_strtoul(port_str, 0, &port);
234 pr_err("strict_strtoul() failed for port_str: %d\n", ret);
237 sock_in = (struct sockaddr_in *)&sockaddr;
238 sock_in->sin_family = AF_INET;
239 sock_in->sin_port = htons((unsigned short)port);
240 sock_in->sin_addr.s_addr = in_aton(ip_str);
242 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
243 ret = iscsit_get_tpg(tpg);
245 return ERR_PTR(-EINVAL);
247 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
249 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
252 * Assume ISCSI_TCP by default. Other network portals for other
255 * Traditional iSCSI over SCTP (initial support)
256 * iSER/TCP (TODO, hardware available)
257 * iSER/SCTP (TODO, software emulation with osc-iwarp)
258 * iSER/IB (TODO, hardware available)
260 * can be enabled with atributes under
261 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
264 tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
266 if (IS_ERR(tpg_np)) {
268 return ERR_CAST(tpg_np);
270 pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
273 return &tpg_np->se_tpg_np;
276 static void lio_target_call_delnpfromtpg(
277 struct se_tpg_np *se_tpg_np)
279 struct iscsi_portal_group *tpg;
280 struct iscsi_tpg_np *tpg_np;
281 struct se_portal_group *se_tpg;
284 tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
286 ret = iscsit_get_tpg(tpg);
290 se_tpg = &tpg->tpg_se_tpg;
291 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
292 " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
293 tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
295 ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
299 pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
304 /* End items for lio_target_np_cit */
306 /* Start items for lio_target_nacl_attrib_cit */
308 #define DEF_NACL_ATTRIB(name) \
309 static ssize_t iscsi_nacl_attrib_show_##name( \
310 struct se_node_acl *se_nacl, \
313 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
316 return sprintf(page, "%u\n", ISCSI_NODE_ATTRIB(nacl)->name); \
319 static ssize_t iscsi_nacl_attrib_store_##name( \
320 struct se_node_acl *se_nacl, \
324 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
330 val = simple_strtoul(page, &endptr, 0); \
331 ret = iscsit_na_##name(nacl, val); \
338 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
340 * Define iscsi_node_attrib_s_dataout_timeout
342 DEF_NACL_ATTRIB(dataout_timeout);
343 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
345 * Define iscsi_node_attrib_s_dataout_timeout_retries
347 DEF_NACL_ATTRIB(dataout_timeout_retries);
348 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
350 * Define iscsi_node_attrib_s_default_erl
352 DEF_NACL_ATTRIB(default_erl);
353 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
355 * Define iscsi_node_attrib_s_nopin_timeout
357 DEF_NACL_ATTRIB(nopin_timeout);
358 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
360 * Define iscsi_node_attrib_s_nopin_response_timeout
362 DEF_NACL_ATTRIB(nopin_response_timeout);
363 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
365 * Define iscsi_node_attrib_s_random_datain_pdu_offsets
367 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
368 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
370 * Define iscsi_node_attrib_s_random_datain_seq_offsets
372 DEF_NACL_ATTRIB(random_datain_seq_offsets);
373 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
375 * Define iscsi_node_attrib_s_random_r2t_offsets
377 DEF_NACL_ATTRIB(random_r2t_offsets);
378 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
380 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
381 &iscsi_nacl_attrib_dataout_timeout.attr,
382 &iscsi_nacl_attrib_dataout_timeout_retries.attr,
383 &iscsi_nacl_attrib_default_erl.attr,
384 &iscsi_nacl_attrib_nopin_timeout.attr,
385 &iscsi_nacl_attrib_nopin_response_timeout.attr,
386 &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
387 &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
388 &iscsi_nacl_attrib_random_r2t_offsets.attr,
392 /* End items for lio_target_nacl_attrib_cit */
394 /* Start items for lio_target_nacl_auth_cit */
396 #define __DEF_NACL_AUTH_STR(prefix, name, flags) \
397 static ssize_t __iscsi_##prefix##_show_##name( \
398 struct iscsi_node_acl *nacl, \
401 struct iscsi_node_auth *auth = &nacl->node_auth; \
403 if (!capable(CAP_SYS_ADMIN)) \
405 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
408 static ssize_t __iscsi_##prefix##_store_##name( \
409 struct iscsi_node_acl *nacl, \
413 struct iscsi_node_auth *auth = &nacl->node_auth; \
415 if (!capable(CAP_SYS_ADMIN)) \
418 snprintf(auth->name, PAGE_SIZE, "%s", page); \
419 if (!strncmp("NULL", auth->name, 4)) \
420 auth->naf_flags &= ~flags; \
422 auth->naf_flags |= flags; \
424 if ((auth->naf_flags & NAF_USERID_IN_SET) && \
425 (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
426 auth->authenticate_target = 1; \
428 auth->authenticate_target = 0; \
433 #define __DEF_NACL_AUTH_INT(prefix, name) \
434 static ssize_t __iscsi_##prefix##_show_##name( \
435 struct iscsi_node_acl *nacl, \
438 struct iscsi_node_auth *auth = &nacl->node_auth; \
440 if (!capable(CAP_SYS_ADMIN)) \
443 return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
446 #define DEF_NACL_AUTH_STR(name, flags) \
447 __DEF_NACL_AUTH_STR(nacl_auth, name, flags) \
448 static ssize_t iscsi_nacl_auth_show_##name( \
449 struct se_node_acl *nacl, \
452 return __iscsi_nacl_auth_show_##name(container_of(nacl, \
453 struct iscsi_node_acl, se_node_acl), page); \
455 static ssize_t iscsi_nacl_auth_store_##name( \
456 struct se_node_acl *nacl, \
460 return __iscsi_nacl_auth_store_##name(container_of(nacl, \
461 struct iscsi_node_acl, se_node_acl), page, count); \
464 #define DEF_NACL_AUTH_INT(name) \
465 __DEF_NACL_AUTH_INT(nacl_auth, name) \
466 static ssize_t iscsi_nacl_auth_show_##name( \
467 struct se_node_acl *nacl, \
470 return __iscsi_nacl_auth_show_##name(container_of(nacl, \
471 struct iscsi_node_acl, se_node_acl), page); \
474 #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
475 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
478 * One-way authentication userid
480 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
481 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
483 * One-way authentication password
485 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
486 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
488 * Enforce mutual authentication
490 DEF_NACL_AUTH_INT(authenticate_target);
491 AUTH_ATTR_RO(authenticate_target);
493 * Mutual authentication userid
495 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
496 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
498 * Mutual authentication password
500 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
501 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
503 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
504 &iscsi_nacl_auth_userid.attr,
505 &iscsi_nacl_auth_password.attr,
506 &iscsi_nacl_auth_authenticate_target.attr,
507 &iscsi_nacl_auth_userid_mutual.attr,
508 &iscsi_nacl_auth_password_mutual.attr,
512 /* End items for lio_target_nacl_auth_cit */
514 /* Start items for lio_target_nacl_param_cit */
516 #define DEF_NACL_PARAM(name) \
517 static ssize_t iscsi_nacl_param_show_##name( \
518 struct se_node_acl *se_nacl, \
521 struct iscsi_session *sess; \
522 struct se_session *se_sess; \
525 spin_lock_bh(&se_nacl->nacl_sess_lock); \
526 se_sess = se_nacl->nacl_sess; \
528 rb = snprintf(page, PAGE_SIZE, \
529 "No Active iSCSI Session\n"); \
531 sess = se_sess->fabric_sess_ptr; \
532 rb = snprintf(page, PAGE_SIZE, "%u\n", \
533 (u32)sess->sess_ops->name); \
535 spin_unlock_bh(&se_nacl->nacl_sess_lock); \
540 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
542 DEF_NACL_PARAM(MaxConnections);
543 NACL_PARAM_ATTR(MaxConnections);
545 DEF_NACL_PARAM(InitialR2T);
546 NACL_PARAM_ATTR(InitialR2T);
548 DEF_NACL_PARAM(ImmediateData);
549 NACL_PARAM_ATTR(ImmediateData);
551 DEF_NACL_PARAM(MaxBurstLength);
552 NACL_PARAM_ATTR(MaxBurstLength);
554 DEF_NACL_PARAM(FirstBurstLength);
555 NACL_PARAM_ATTR(FirstBurstLength);
557 DEF_NACL_PARAM(DefaultTime2Wait);
558 NACL_PARAM_ATTR(DefaultTime2Wait);
560 DEF_NACL_PARAM(DefaultTime2Retain);
561 NACL_PARAM_ATTR(DefaultTime2Retain);
563 DEF_NACL_PARAM(MaxOutstandingR2T);
564 NACL_PARAM_ATTR(MaxOutstandingR2T);
566 DEF_NACL_PARAM(DataPDUInOrder);
567 NACL_PARAM_ATTR(DataPDUInOrder);
569 DEF_NACL_PARAM(DataSequenceInOrder);
570 NACL_PARAM_ATTR(DataSequenceInOrder);
572 DEF_NACL_PARAM(ErrorRecoveryLevel);
573 NACL_PARAM_ATTR(ErrorRecoveryLevel);
575 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
576 &iscsi_nacl_param_MaxConnections.attr,
577 &iscsi_nacl_param_InitialR2T.attr,
578 &iscsi_nacl_param_ImmediateData.attr,
579 &iscsi_nacl_param_MaxBurstLength.attr,
580 &iscsi_nacl_param_FirstBurstLength.attr,
581 &iscsi_nacl_param_DefaultTime2Wait.attr,
582 &iscsi_nacl_param_DefaultTime2Retain.attr,
583 &iscsi_nacl_param_MaxOutstandingR2T.attr,
584 &iscsi_nacl_param_DataPDUInOrder.attr,
585 &iscsi_nacl_param_DataSequenceInOrder.attr,
586 &iscsi_nacl_param_ErrorRecoveryLevel.attr,
590 /* End items for lio_target_nacl_param_cit */
592 /* Start items for lio_target_acl_cit */
594 static ssize_t lio_target_nacl_show_info(
595 struct se_node_acl *se_nacl,
598 struct iscsi_session *sess;
599 struct iscsi_conn *conn;
600 struct se_session *se_sess;
603 spin_lock_bh(&se_nacl->nacl_sess_lock);
604 se_sess = se_nacl->nacl_sess;
606 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
607 " Endpoint: %s\n", se_nacl->initiatorname);
609 sess = se_sess->fabric_sess_ptr;
611 if (sess->sess_ops->InitiatorName)
612 rb += sprintf(page+rb, "InitiatorName: %s\n",
613 sess->sess_ops->InitiatorName);
614 if (sess->sess_ops->InitiatorAlias)
615 rb += sprintf(page+rb, "InitiatorAlias: %s\n",
616 sess->sess_ops->InitiatorAlias);
618 rb += sprintf(page+rb, "LIO Session ID: %u "
619 "ISID: 0x%02x %02x %02x %02x %02x %02x "
620 "TSIH: %hu ", sess->sid,
621 sess->isid[0], sess->isid[1], sess->isid[2],
622 sess->isid[3], sess->isid[4], sess->isid[5],
624 rb += sprintf(page+rb, "SessionType: %s\n",
625 (sess->sess_ops->SessionType) ?
626 "Discovery" : "Normal");
627 rb += sprintf(page+rb, "Session State: ");
628 switch (sess->session_state) {
629 case TARG_SESS_STATE_FREE:
630 rb += sprintf(page+rb, "TARG_SESS_FREE\n");
632 case TARG_SESS_STATE_ACTIVE:
633 rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
635 case TARG_SESS_STATE_LOGGED_IN:
636 rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
638 case TARG_SESS_STATE_FAILED:
639 rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
641 case TARG_SESS_STATE_IN_CONTINUE:
642 rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
645 rb += sprintf(page+rb, "ERROR: Unknown Session"
650 rb += sprintf(page+rb, "---------------------[iSCSI Session"
651 " Values]-----------------------\n");
652 rb += sprintf(page+rb, " CmdSN/WR : CmdSN/WC : ExpCmdSN"
653 " : MaxCmdSN : ITT : TTT\n");
654 rb += sprintf(page+rb, " 0x%08x 0x%08x 0x%08x 0x%08x"
657 (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
658 sess->exp_cmd_sn, sess->max_cmd_sn,
659 sess->init_task_tag, sess->targ_xfer_tag);
660 rb += sprintf(page+rb, "----------------------[iSCSI"
661 " Connections]-------------------------\n");
663 spin_lock(&sess->conn_lock);
664 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
665 rb += sprintf(page+rb, "CID: %hu Connection"
666 " State: ", conn->cid);
667 switch (conn->conn_state) {
668 case TARG_CONN_STATE_FREE:
669 rb += sprintf(page+rb,
670 "TARG_CONN_STATE_FREE\n");
672 case TARG_CONN_STATE_XPT_UP:
673 rb += sprintf(page+rb,
674 "TARG_CONN_STATE_XPT_UP\n");
676 case TARG_CONN_STATE_IN_LOGIN:
677 rb += sprintf(page+rb,
678 "TARG_CONN_STATE_IN_LOGIN\n");
680 case TARG_CONN_STATE_LOGGED_IN:
681 rb += sprintf(page+rb,
682 "TARG_CONN_STATE_LOGGED_IN\n");
684 case TARG_CONN_STATE_IN_LOGOUT:
685 rb += sprintf(page+rb,
686 "TARG_CONN_STATE_IN_LOGOUT\n");
688 case TARG_CONN_STATE_LOGOUT_REQUESTED:
689 rb += sprintf(page+rb,
690 "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
692 case TARG_CONN_STATE_CLEANUP_WAIT:
693 rb += sprintf(page+rb,
694 "TARG_CONN_STATE_CLEANUP_WAIT\n");
697 rb += sprintf(page+rb,
698 "ERROR: Unknown Connection State!\n");
702 rb += sprintf(page+rb, " Address %s %s", conn->login_ip,
703 (conn->network_transport == ISCSI_TCP) ?
705 rb += sprintf(page+rb, " StatSN: 0x%08x\n",
708 spin_unlock(&sess->conn_lock);
710 spin_unlock_bh(&se_nacl->nacl_sess_lock);
715 TF_NACL_BASE_ATTR_RO(lio_target, info);
717 static ssize_t lio_target_nacl_show_cmdsn_depth(
718 struct se_node_acl *se_nacl,
721 return sprintf(page, "%u\n", se_nacl->queue_depth);
724 static ssize_t lio_target_nacl_store_cmdsn_depth(
725 struct se_node_acl *se_nacl,
729 struct se_portal_group *se_tpg = se_nacl->se_tpg;
730 struct iscsi_portal_group *tpg = container_of(se_tpg,
731 struct iscsi_portal_group, tpg_se_tpg);
732 struct config_item *acl_ci, *tpg_ci, *wwn_ci;
737 cmdsn_depth = simple_strtoul(page, &endptr, 0);
738 if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
739 pr_err("Passed cmdsn_depth: %u exceeds"
740 " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
741 TA_DEFAULT_CMDSN_DEPTH_MAX);
744 acl_ci = &se_nacl->acl_group.cg_item;
746 pr_err("Unable to locatel acl_ci\n");
749 tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
751 pr_err("Unable to locate tpg_ci\n");
754 wwn_ci = &tpg_ci->ci_group->cg_item;
756 pr_err("Unable to locate config_item wwn_ci\n");
760 if (iscsit_get_tpg(tpg) < 0)
763 * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
765 ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
766 config_item_name(acl_ci), cmdsn_depth, 1);
768 pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
769 "InitiatorName: %s\n", config_item_name(wwn_ci),
770 config_item_name(tpg_ci), cmdsn_depth,
771 config_item_name(acl_ci));
774 return (!ret) ? count : (ssize_t)ret;
777 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
779 static struct configfs_attribute *lio_target_initiator_attrs[] = {
780 &lio_target_nacl_info.attr,
781 &lio_target_nacl_cmdsn_depth.attr,
785 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
786 struct se_portal_group *se_tpg)
788 struct iscsi_node_acl *acl;
790 acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
792 pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
796 return &acl->se_node_acl;
799 static struct se_node_acl *lio_target_make_nodeacl(
800 struct se_portal_group *se_tpg,
801 struct config_group *group,
804 struct config_group *stats_cg;
805 struct iscsi_node_acl *acl;
806 struct se_node_acl *se_nacl_new, *se_nacl;
807 struct iscsi_portal_group *tpg = container_of(se_tpg,
808 struct iscsi_portal_group, tpg_se_tpg);
811 se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
813 return ERR_PTR(-ENOMEM);
815 acl = container_of(se_nacl_new, struct iscsi_node_acl,
818 cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
820 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
821 * when converting a NdoeACL from demo mode -> explict
823 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
828 stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
830 stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
832 if (!stats_cg->default_groups) {
833 pr_err("Unable to allocate memory for"
834 " stats_cg->default_groups\n");
835 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
837 return ERR_PTR(-ENOMEM);
840 stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
841 stats_cg->default_groups[1] = NULL;
842 config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
843 "iscsi_sess_stats", &iscsi_stat_sess_cit);
848 static void lio_target_drop_nodeacl(
849 struct se_node_acl *se_nacl)
851 struct se_portal_group *se_tpg = se_nacl->se_tpg;
852 struct iscsi_node_acl *acl = container_of(se_nacl,
853 struct iscsi_node_acl, se_node_acl);
854 struct config_item *df_item;
855 struct config_group *stats_cg;
858 stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
859 for (i = 0; stats_cg->default_groups[i]; i++) {
860 df_item = &stats_cg->default_groups[i]->cg_item;
861 stats_cg->default_groups[i] = NULL;
862 config_item_put(df_item);
864 kfree(stats_cg->default_groups);
866 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
870 /* End items for lio_target_acl_cit */
872 /* Start items for lio_target_tpg_attrib_cit */
874 #define DEF_TPG_ATTRIB(name) \
876 static ssize_t iscsi_tpg_attrib_show_##name( \
877 struct se_portal_group *se_tpg, \
880 struct iscsi_portal_group *tpg = container_of(se_tpg, \
881 struct iscsi_portal_group, tpg_se_tpg); \
884 if (iscsit_get_tpg(tpg) < 0) \
887 rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name); \
888 iscsit_put_tpg(tpg); \
892 static ssize_t iscsi_tpg_attrib_store_##name( \
893 struct se_portal_group *se_tpg, \
897 struct iscsi_portal_group *tpg = container_of(se_tpg, \
898 struct iscsi_portal_group, tpg_se_tpg); \
903 if (iscsit_get_tpg(tpg) < 0) \
906 val = simple_strtoul(page, &endptr, 0); \
907 ret = iscsit_ta_##name(tpg, val); \
911 iscsit_put_tpg(tpg); \
914 iscsit_put_tpg(tpg); \
918 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
921 * Define iscsi_tpg_attrib_s_authentication
923 DEF_TPG_ATTRIB(authentication);
924 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
926 * Define iscsi_tpg_attrib_s_login_timeout
928 DEF_TPG_ATTRIB(login_timeout);
929 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
931 * Define iscsi_tpg_attrib_s_netif_timeout
933 DEF_TPG_ATTRIB(netif_timeout);
934 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
936 * Define iscsi_tpg_attrib_s_generate_node_acls
938 DEF_TPG_ATTRIB(generate_node_acls);
939 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
941 * Define iscsi_tpg_attrib_s_default_cmdsn_depth
943 DEF_TPG_ATTRIB(default_cmdsn_depth);
944 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
946 Define iscsi_tpg_attrib_s_cache_dynamic_acls
948 DEF_TPG_ATTRIB(cache_dynamic_acls);
949 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
951 * Define iscsi_tpg_attrib_s_demo_mode_write_protect
953 DEF_TPG_ATTRIB(demo_mode_write_protect);
954 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
956 * Define iscsi_tpg_attrib_s_prod_mode_write_protect
958 DEF_TPG_ATTRIB(prod_mode_write_protect);
959 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
961 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
962 &iscsi_tpg_attrib_authentication.attr,
963 &iscsi_tpg_attrib_login_timeout.attr,
964 &iscsi_tpg_attrib_netif_timeout.attr,
965 &iscsi_tpg_attrib_generate_node_acls.attr,
966 &iscsi_tpg_attrib_default_cmdsn_depth.attr,
967 &iscsi_tpg_attrib_cache_dynamic_acls.attr,
968 &iscsi_tpg_attrib_demo_mode_write_protect.attr,
969 &iscsi_tpg_attrib_prod_mode_write_protect.attr,
973 /* End items for lio_target_tpg_attrib_cit */
975 /* Start items for lio_target_tpg_param_cit */
977 #define DEF_TPG_PARAM(name) \
978 static ssize_t iscsi_tpg_param_show_##name( \
979 struct se_portal_group *se_tpg, \
982 struct iscsi_portal_group *tpg = container_of(se_tpg, \
983 struct iscsi_portal_group, tpg_se_tpg); \
984 struct iscsi_param *param; \
987 if (iscsit_get_tpg(tpg) < 0) \
990 param = iscsi_find_param_from_key(__stringify(name), \
993 iscsit_put_tpg(tpg); \
996 rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
998 iscsit_put_tpg(tpg); \
1001 static ssize_t iscsi_tpg_param_store_##name( \
1002 struct se_portal_group *se_tpg, \
1006 struct iscsi_portal_group *tpg = container_of(se_tpg, \
1007 struct iscsi_portal_group, tpg_se_tpg); \
1011 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
1014 snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
1015 buf[strlen(buf)-1] = '\0'; /* Kill newline */ \
1017 if (iscsit_get_tpg(tpg) < 0) { \
1022 ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
1027 iscsit_put_tpg(tpg); \
1031 iscsit_put_tpg(tpg); \
1035 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1037 DEF_TPG_PARAM(AuthMethod);
1038 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1040 DEF_TPG_PARAM(HeaderDigest);
1041 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1043 DEF_TPG_PARAM(DataDigest);
1044 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1046 DEF_TPG_PARAM(MaxConnections);
1047 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1049 DEF_TPG_PARAM(TargetAlias);
1050 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1052 DEF_TPG_PARAM(InitialR2T);
1053 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1055 DEF_TPG_PARAM(ImmediateData);
1056 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1058 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1059 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1061 DEF_TPG_PARAM(MaxBurstLength);
1062 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1064 DEF_TPG_PARAM(FirstBurstLength);
1065 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1067 DEF_TPG_PARAM(DefaultTime2Wait);
1068 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1070 DEF_TPG_PARAM(DefaultTime2Retain);
1071 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1073 DEF_TPG_PARAM(MaxOutstandingR2T);
1074 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1076 DEF_TPG_PARAM(DataPDUInOrder);
1077 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1079 DEF_TPG_PARAM(DataSequenceInOrder);
1080 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1082 DEF_TPG_PARAM(ErrorRecoveryLevel);
1083 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1085 DEF_TPG_PARAM(IFMarker);
1086 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1088 DEF_TPG_PARAM(OFMarker);
1089 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1091 DEF_TPG_PARAM(IFMarkInt);
1092 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1094 DEF_TPG_PARAM(OFMarkInt);
1095 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1097 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1098 &iscsi_tpg_param_AuthMethod.attr,
1099 &iscsi_tpg_param_HeaderDigest.attr,
1100 &iscsi_tpg_param_DataDigest.attr,
1101 &iscsi_tpg_param_MaxConnections.attr,
1102 &iscsi_tpg_param_TargetAlias.attr,
1103 &iscsi_tpg_param_InitialR2T.attr,
1104 &iscsi_tpg_param_ImmediateData.attr,
1105 &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1106 &iscsi_tpg_param_MaxBurstLength.attr,
1107 &iscsi_tpg_param_FirstBurstLength.attr,
1108 &iscsi_tpg_param_DefaultTime2Wait.attr,
1109 &iscsi_tpg_param_DefaultTime2Retain.attr,
1110 &iscsi_tpg_param_MaxOutstandingR2T.attr,
1111 &iscsi_tpg_param_DataPDUInOrder.attr,
1112 &iscsi_tpg_param_DataSequenceInOrder.attr,
1113 &iscsi_tpg_param_ErrorRecoveryLevel.attr,
1114 &iscsi_tpg_param_IFMarker.attr,
1115 &iscsi_tpg_param_OFMarker.attr,
1116 &iscsi_tpg_param_IFMarkInt.attr,
1117 &iscsi_tpg_param_OFMarkInt.attr,
1121 /* End items for lio_target_tpg_param_cit */
1123 /* Start items for lio_target_tpg_cit */
1125 static ssize_t lio_target_tpg_show_enable(
1126 struct se_portal_group *se_tpg,
1129 struct iscsi_portal_group *tpg = container_of(se_tpg,
1130 struct iscsi_portal_group, tpg_se_tpg);
1133 spin_lock(&tpg->tpg_state_lock);
1134 len = sprintf(page, "%d\n",
1135 (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1136 spin_unlock(&tpg->tpg_state_lock);
1141 static ssize_t lio_target_tpg_store_enable(
1142 struct se_portal_group *se_tpg,
1146 struct iscsi_portal_group *tpg = container_of(se_tpg,
1147 struct iscsi_portal_group, tpg_se_tpg);
1152 op = simple_strtoul(page, &endptr, 0);
1153 if ((op != 1) && (op != 0)) {
1154 pr_err("Illegal value for tpg_enable: %u\n", op);
1158 ret = iscsit_get_tpg(tpg);
1163 ret = iscsit_tpg_enable_portal_group(tpg);
1168 * iscsit_tpg_disable_portal_group() assumes force=1
1170 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1175 iscsit_put_tpg(tpg);
1178 iscsit_put_tpg(tpg);
1182 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1184 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1185 &lio_target_tpg_enable.attr,
1189 /* End items for lio_target_tpg_cit */
1191 /* Start items for lio_target_tiqn_cit */
1193 struct se_portal_group *lio_target_tiqn_addtpg(
1195 struct config_group *group,
1198 struct iscsi_portal_group *tpg;
1199 struct iscsi_tiqn *tiqn;
1200 char *tpgt_str, *end_ptr;
1202 unsigned short int tpgt;
1204 tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1206 * Only tpgt_# directory groups can be created below
1207 * target/iscsi/iqn.superturodiskarry/
1209 tpgt_str = strstr(name, "tpgt_");
1211 pr_err("Unable to locate \"tpgt_#\" directory"
1215 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1216 tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1218 tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1222 ret = core_tpg_register(
1223 &lio_target_fabric_configfs->tf_ops,
1224 wwn, &tpg->tpg_se_tpg, tpg,
1225 TRANSPORT_TPG_TYPE_NORMAL);
1229 ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1233 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1234 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1236 return &tpg->tpg_se_tpg;
1238 core_tpg_deregister(&tpg->tpg_se_tpg);
1243 void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1245 struct iscsi_portal_group *tpg;
1246 struct iscsi_tiqn *tiqn;
1248 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1249 tiqn = tpg->tpg_tiqn;
1251 * iscsit_tpg_del_portal_group() assumes force=1
1253 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1254 iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1257 /* End items for lio_target_tiqn_cit */
1259 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1261 static ssize_t lio_target_wwn_show_attr_lio_version(
1262 struct target_fabric_configfs *tf,
1265 return sprintf(page, "RisingTide Systems Linux-iSCSI Target "ISCSIT_VERSION"\n");
1268 TF_WWN_ATTR_RO(lio_target, lio_version);
1270 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1271 &lio_target_wwn_lio_version.attr,
1275 struct se_wwn *lio_target_call_coreaddtiqn(
1276 struct target_fabric_configfs *tf,
1277 struct config_group *group,
1280 struct config_group *stats_cg;
1281 struct iscsi_tiqn *tiqn;
1283 tiqn = iscsit_add_tiqn((unsigned char *)name);
1285 return ERR_CAST(tiqn);
1287 * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1289 stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1291 stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 6,
1293 if (!stats_cg->default_groups) {
1294 pr_err("Unable to allocate memory for"
1295 " stats_cg->default_groups\n");
1296 iscsit_del_tiqn(tiqn);
1297 return ERR_PTR(-ENOMEM);
1300 stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
1301 stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
1302 stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
1303 stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
1304 stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
1305 stats_cg->default_groups[5] = NULL;
1306 config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
1307 "iscsi_instance", &iscsi_stat_instance_cit);
1308 config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
1309 "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1310 config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
1311 "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1312 config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
1313 "iscsi_login_stats", &iscsi_stat_login_cit);
1314 config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
1315 "iscsi_logout_stats", &iscsi_stat_logout_cit);
1317 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1318 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1320 return &tiqn->tiqn_wwn;
1323 void lio_target_call_coredeltiqn(
1326 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1327 struct config_item *df_item;
1328 struct config_group *stats_cg;
1331 stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1332 for (i = 0; stats_cg->default_groups[i]; i++) {
1333 df_item = &stats_cg->default_groups[i]->cg_item;
1334 stats_cg->default_groups[i] = NULL;
1335 config_item_put(df_item);
1337 kfree(stats_cg->default_groups);
1339 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1341 iscsit_del_tiqn(tiqn);
1344 /* End LIO-Target TIQN struct contig_lio_target_cit */
1346 /* Start lio_target_discovery_auth_cit */
1348 #define DEF_DISC_AUTH_STR(name, flags) \
1349 __DEF_NACL_AUTH_STR(disc, name, flags) \
1350 static ssize_t iscsi_disc_show_##name( \
1351 struct target_fabric_configfs *tf, \
1354 return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
1357 static ssize_t iscsi_disc_store_##name( \
1358 struct target_fabric_configfs *tf, \
1362 return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
1366 #define DEF_DISC_AUTH_INT(name) \
1367 __DEF_NACL_AUTH_INT(disc, name) \
1368 static ssize_t iscsi_disc_show_##name( \
1369 struct target_fabric_configfs *tf, \
1372 return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
1376 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1377 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1380 * One-way authentication userid
1382 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1383 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1385 * One-way authentication password
1387 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1388 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1390 * Enforce mutual authentication
1392 DEF_DISC_AUTH_INT(authenticate_target);
1393 DISC_AUTH_ATTR_RO(authenticate_target);
1395 * Mutual authentication userid
1397 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1398 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1400 * Mutual authentication password
1402 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1403 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1406 * enforce_discovery_auth
1408 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1409 struct target_fabric_configfs *tf,
1412 struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1414 return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1417 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1418 struct target_fabric_configfs *tf,
1422 struct iscsi_param *param;
1423 struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1427 op = simple_strtoul(page, &endptr, 0);
1428 if ((op != 1) && (op != 0)) {
1429 pr_err("Illegal value for enforce_discovery_auth:"
1434 if (!discovery_tpg) {
1435 pr_err("iscsit_global->discovery_tpg is NULL\n");
1439 param = iscsi_find_param_from_key(AUTHMETHOD,
1440 discovery_tpg->param_list);
1446 * Reset the AuthMethod key to CHAP.
1448 if (iscsi_update_param_value(param, CHAP) < 0)
1451 discovery_tpg->tpg_attrib.authentication = 1;
1452 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1453 pr_debug("LIO-CORE[0] Successfully enabled"
1454 " authentication enforcement for iSCSI"
1455 " Discovery TPG\n");
1458 * Reset the AuthMethod key to CHAP,None
1460 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1463 discovery_tpg->tpg_attrib.authentication = 0;
1464 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1465 pr_debug("LIO-CORE[0] Successfully disabled"
1466 " authentication enforcement for iSCSI"
1467 " Discovery TPG\n");
1473 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1475 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1476 &iscsi_disc_userid.attr,
1477 &iscsi_disc_password.attr,
1478 &iscsi_disc_authenticate_target.attr,
1479 &iscsi_disc_userid_mutual.attr,
1480 &iscsi_disc_password_mutual.attr,
1481 &iscsi_disc_enforce_discovery_auth.attr,
1485 /* End lio_target_discovery_auth_cit */
1487 /* Start functions for target_core_fabric_ops */
1489 static char *iscsi_get_fabric_name(void)
1494 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1496 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1498 return cmd->init_task_tag;
1501 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1503 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1505 return cmd->i_state;
1508 static int iscsi_is_state_remove(struct se_cmd *se_cmd)
1510 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1512 return (cmd->i_state == ISTATE_REMOVE);
1515 static int lio_sess_logged_in(struct se_session *se_sess)
1517 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1520 * Called with spin_lock_bh(&tpg_lock); and
1521 * spin_lock(&se_tpg->session_lock); held.
1523 spin_lock(&sess->conn_lock);
1524 ret = (sess->session_state != TARG_SESS_STATE_LOGGED_IN);
1525 spin_unlock(&sess->conn_lock);
1530 static u32 lio_sess_get_index(struct se_session *se_sess)
1532 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1534 return sess->session_index;
1537 static u32 lio_sess_get_initiator_sid(
1538 struct se_session *se_sess,
1542 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1544 * iSCSI Initiator Session Identifier from RFC-3720.
1546 return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1547 sess->isid[0], sess->isid[1], sess->isid[2],
1548 sess->isid[3], sess->isid[4], sess->isid[5]);
1551 static int lio_queue_data_in(struct se_cmd *se_cmd)
1553 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1555 cmd->i_state = ISTATE_SEND_DATAIN;
1556 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1560 static int lio_write_pending(struct se_cmd *se_cmd)
1562 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1564 if (!cmd->immediate_data && !cmd->unsolicited_data)
1565 return iscsit_build_r2ts_for_cmd(cmd, cmd->conn, 1);
1570 static int lio_write_pending_status(struct se_cmd *se_cmd)
1572 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1575 spin_lock_bh(&cmd->istate_lock);
1576 ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1577 spin_unlock_bh(&cmd->istate_lock);
1582 static int lio_queue_status(struct se_cmd *se_cmd)
1584 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1586 cmd->i_state = ISTATE_SEND_STATUS;
1587 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1591 static u16 lio_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
1593 unsigned char *buffer = se_cmd->sense_buffer;
1595 * From RFC-3720 10.4.7. Data Segment - Sense and Response Data Segment
1596 * 16-bit SenseLength.
1598 buffer[0] = ((sense_length >> 8) & 0xff);
1599 buffer[1] = (sense_length & 0xff);
1601 * Return two byte offset into allocated sense_buffer.
1606 static u16 lio_get_fabric_sense_len(void)
1609 * Return two byte offset into allocated sense_buffer.
1614 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1616 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1618 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1619 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1623 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1625 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1627 return &tpg->tpg_tiqn->tiqn[0];
1630 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1632 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1637 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1639 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1641 return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1644 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1646 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1648 return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1651 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1653 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1655 return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1658 static int lio_tpg_check_demo_mode_write_protect(
1659 struct se_portal_group *se_tpg)
1661 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1663 return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1666 static int lio_tpg_check_prod_mode_write_protect(
1667 struct se_portal_group *se_tpg)
1669 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1671 return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1674 static void lio_tpg_release_fabric_acl(
1675 struct se_portal_group *se_tpg,
1676 struct se_node_acl *se_acl)
1678 struct iscsi_node_acl *acl = container_of(se_acl,
1679 struct iscsi_node_acl, se_node_acl);
1684 * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1686 * Also, this function calls iscsit_inc_session_usage_count() on the
1687 * struct iscsi_session in question.
1689 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1691 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1693 spin_lock(&sess->conn_lock);
1694 if (atomic_read(&sess->session_fall_back_to_erl0) ||
1695 atomic_read(&sess->session_logout) ||
1696 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1697 spin_unlock(&sess->conn_lock);
1700 atomic_set(&sess->session_reinstatement, 1);
1701 spin_unlock(&sess->conn_lock);
1703 iscsit_inc_session_usage_count(sess);
1704 iscsit_stop_time2retain_timer(sess);
1710 * Calls iscsit_dec_session_usage_count() as inverse of
1711 * lio_tpg_shutdown_session()
1713 static void lio_tpg_close_session(struct se_session *se_sess)
1715 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1717 * If the iSCSI Session for the iSCSI Initiator Node exists,
1718 * forcefully shutdown the iSCSI NEXUS.
1720 iscsit_stop_session(sess, 1, 1);
1721 iscsit_dec_session_usage_count(sess);
1722 iscsit_close_session(sess);
1725 static void lio_tpg_stop_session(
1726 struct se_session *se_sess,
1730 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1732 iscsit_stop_session(sess, sess_sleep, conn_sleep);
1735 static void lio_tpg_fall_back_to_erl0(struct se_session *se_sess)
1737 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1739 iscsit_fall_back_to_erl0(sess);
1742 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1744 struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1746 return tpg->tpg_tiqn->tiqn_index;
1749 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1751 struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1754 ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1755 iscsit_set_default_node_attribues(acl);
1758 static void lio_release_cmd(struct se_cmd *se_cmd)
1760 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1762 iscsit_release_cmd(cmd);
1765 /* End functions for target_core_fabric_ops */
1767 int iscsi_target_register_configfs(void)
1769 struct target_fabric_configfs *fabric;
1772 lio_target_fabric_configfs = NULL;
1773 fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1774 if (IS_ERR(fabric)) {
1775 pr_err("target_fabric_configfs_init() for"
1776 " LIO-Target failed!\n");
1777 return PTR_ERR(fabric);
1780 * Setup the fabric API of function pointers used by target_core_mod..
1782 fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1783 fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1784 fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1785 fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1786 fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1787 fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1788 fabric->tf_ops.tpg_get_pr_transport_id_len =
1789 &iscsi_get_pr_transport_id_len;
1790 fabric->tf_ops.tpg_parse_pr_out_transport_id =
1791 &iscsi_parse_pr_out_transport_id;
1792 fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1793 fabric->tf_ops.tpg_check_demo_mode_cache =
1794 &lio_tpg_check_demo_mode_cache;
1795 fabric->tf_ops.tpg_check_demo_mode_write_protect =
1796 &lio_tpg_check_demo_mode_write_protect;
1797 fabric->tf_ops.tpg_check_prod_mode_write_protect =
1798 &lio_tpg_check_prod_mode_write_protect;
1799 fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1800 fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1801 fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1802 fabric->tf_ops.release_cmd = &lio_release_cmd;
1803 fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1804 fabric->tf_ops.close_session = &lio_tpg_close_session;
1805 fabric->tf_ops.stop_session = &lio_tpg_stop_session;
1806 fabric->tf_ops.fall_back_to_erl0 = &lio_tpg_fall_back_to_erl0;
1807 fabric->tf_ops.sess_logged_in = &lio_sess_logged_in;
1808 fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1809 fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1810 fabric->tf_ops.write_pending = &lio_write_pending;
1811 fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1812 fabric->tf_ops.set_default_node_attributes =
1813 &lio_set_default_node_attributes;
1814 fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1815 fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1816 fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1817 fabric->tf_ops.queue_status = &lio_queue_status;
1818 fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1819 fabric->tf_ops.set_fabric_sense_len = &lio_set_fabric_sense_len;
1820 fabric->tf_ops.get_fabric_sense_len = &lio_get_fabric_sense_len;
1821 fabric->tf_ops.is_state_remove = &iscsi_is_state_remove;
1823 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1825 fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1826 fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1827 fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1828 fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1829 fabric->tf_ops.fabric_post_link = NULL;
1830 fabric->tf_ops.fabric_pre_unlink = NULL;
1831 fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1832 fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1833 fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1834 fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1836 * Setup default attribute lists for various fabric->tf_cit_tmpl
1837 * sturct config_item_type's
1839 TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1840 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1841 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1842 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1843 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1844 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1845 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1846 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1847 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1848 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
1850 ret = target_fabric_configfs_register(fabric);
1852 pr_err("target_fabric_configfs_register() for"
1853 " LIO-Target failed!\n");
1854 target_fabric_configfs_free(fabric);
1858 lio_target_fabric_configfs = fabric;
1859 pr_debug("LIO_TARGET[0] - Set fabric ->"
1860 " lio_target_fabric_configfs\n");
1865 void iscsi_target_deregister_configfs(void)
1867 if (!lio_target_fabric_configfs)
1870 * Shutdown discovery sessions and disable discovery TPG
1872 if (iscsit_global->discovery_tpg)
1873 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
1875 target_fabric_configfs_deregister(lio_target_fabric_configfs);
1876 lio_target_fabric_configfs = NULL;
1877 pr_debug("LIO_TARGET[0] - Cleared"
1878 " lio_target_fabric_configfs\n");