]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/target/iscsi/iscsi_target_configfs.c
scsi: qedf: Fix a potential NULL pointer dereference
[mirror_ubuntu-artful-kernel.git] / drivers / target / iscsi / iscsi_target_configfs.c
1 /*******************************************************************************
2 * This file contains the configfs implementation for iSCSI Target mode
3 * from the LIO-Target Project.
4 *
5 * (c) Copyright 2007-2013 Datera, Inc.
6 *
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 ****************************************************************************/
19
20 #include <linux/configfs.h>
21 #include <linux/ctype.h>
22 #include <linux/export.h>
23 #include <linux/inet.h>
24 #include <linux/module.h>
25 #include <net/ipv6.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
28 #include <target/iscsi/iscsi_transport.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_nodeattrib.h"
34 #include "iscsi_target_tpg.h"
35 #include "iscsi_target_util.h"
36 #include "iscsi_target.h"
37 #include <target/iscsi/iscsi_target_stat.h>
38
39
40 /* Start items for lio_target_portal_cit */
41
42 static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
43 {
44 return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
45 }
46
47 static ssize_t lio_target_np_driver_show(struct config_item *item, char *page,
48 enum iscsit_transport_type type)
49 {
50 struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
51 struct iscsi_tpg_np *tpg_np_new;
52 ssize_t rb;
53
54 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
55 if (tpg_np_new)
56 rb = sprintf(page, "1\n");
57 else
58 rb = sprintf(page, "0\n");
59
60 return rb;
61 }
62
63 static ssize_t lio_target_np_driver_store(struct config_item *item,
64 const char *page, size_t count, enum iscsit_transport_type type,
65 const char *mod_name)
66 {
67 struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
68 struct iscsi_np *np;
69 struct iscsi_portal_group *tpg;
70 struct iscsi_tpg_np *tpg_np_new = NULL;
71 u32 op;
72 int rc;
73
74 rc = kstrtou32(page, 0, &op);
75 if (rc)
76 return rc;
77 if ((op != 1) && (op != 0)) {
78 pr_err("Illegal value for tpg_enable: %u\n", op);
79 return -EINVAL;
80 }
81 np = tpg_np->tpg_np;
82 if (!np) {
83 pr_err("Unable to locate struct iscsi_np from"
84 " struct iscsi_tpg_np\n");
85 return -EINVAL;
86 }
87
88 tpg = tpg_np->tpg;
89 if (iscsit_get_tpg(tpg) < 0)
90 return -EINVAL;
91
92 if (op) {
93 if (strlen(mod_name)) {
94 rc = request_module(mod_name);
95 if (rc != 0) {
96 pr_warn("Unable to request_module for %s\n",
97 mod_name);
98 rc = 0;
99 }
100 }
101
102 tpg_np_new = iscsit_tpg_add_network_portal(tpg,
103 &np->np_sockaddr, tpg_np, type);
104 if (IS_ERR(tpg_np_new)) {
105 rc = PTR_ERR(tpg_np_new);
106 goto out;
107 }
108 } else {
109 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
110 if (tpg_np_new) {
111 rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
112 if (rc < 0)
113 goto out;
114 }
115 }
116
117 iscsit_put_tpg(tpg);
118 return count;
119 out:
120 iscsit_put_tpg(tpg);
121 return rc;
122 }
123
124 static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
125 {
126 return lio_target_np_driver_show(item, page, ISCSI_INFINIBAND);
127 }
128
129 static ssize_t lio_target_np_iser_store(struct config_item *item,
130 const char *page, size_t count)
131 {
132 return lio_target_np_driver_store(item, page, count,
133 ISCSI_INFINIBAND, "ib_isert");
134 }
135 CONFIGFS_ATTR(lio_target_np_, iser);
136
137 static ssize_t lio_target_np_cxgbit_show(struct config_item *item, char *page)
138 {
139 return lio_target_np_driver_show(item, page, ISCSI_CXGBIT);
140 }
141
142 static ssize_t lio_target_np_cxgbit_store(struct config_item *item,
143 const char *page, size_t count)
144 {
145 return lio_target_np_driver_store(item, page, count,
146 ISCSI_CXGBIT, "cxgbit");
147 }
148 CONFIGFS_ATTR(lio_target_np_, cxgbit);
149
150 static struct configfs_attribute *lio_target_portal_attrs[] = {
151 &lio_target_np_attr_iser,
152 &lio_target_np_attr_cxgbit,
153 NULL,
154 };
155
156 /* Stop items for lio_target_portal_cit */
157
158 /* Start items for lio_target_np_cit */
159
160 #define MAX_PORTAL_LEN 256
161
162 static struct se_tpg_np *lio_target_call_addnptotpg(
163 struct se_portal_group *se_tpg,
164 struct config_group *group,
165 const char *name)
166 {
167 struct iscsi_portal_group *tpg;
168 struct iscsi_tpg_np *tpg_np;
169 char *str, *str2, *ip_str, *port_str;
170 struct sockaddr_storage sockaddr = { };
171 int ret;
172 char buf[MAX_PORTAL_LEN + 1];
173
174 if (strlen(name) > MAX_PORTAL_LEN) {
175 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
176 (int)strlen(name), MAX_PORTAL_LEN);
177 return ERR_PTR(-EOVERFLOW);
178 }
179 memset(buf, 0, MAX_PORTAL_LEN + 1);
180 snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
181
182 str = strstr(buf, "[");
183 if (str) {
184 str2 = strstr(str, "]");
185 if (!str2) {
186 pr_err("Unable to locate trailing \"]\""
187 " in IPv6 iSCSI network portal address\n");
188 return ERR_PTR(-EINVAL);
189 }
190
191 ip_str = str + 1; /* Skip over leading "[" */
192 *str2 = '\0'; /* Terminate the unbracketed IPv6 address */
193 str2++; /* Skip over the \0 */
194
195 port_str = strstr(str2, ":");
196 if (!port_str) {
197 pr_err("Unable to locate \":port\""
198 " in IPv6 iSCSI network portal address\n");
199 return ERR_PTR(-EINVAL);
200 }
201 *port_str = '\0'; /* Terminate string for IP */
202 port_str++; /* Skip over ":" */
203 } else {
204 ip_str = &buf[0];
205 port_str = strstr(ip_str, ":");
206 if (!port_str) {
207 pr_err("Unable to locate \":port\""
208 " in IPv4 iSCSI network portal address\n");
209 return ERR_PTR(-EINVAL);
210 }
211 *port_str = '\0'; /* Terminate string for IP */
212 port_str++; /* Skip over ":" */
213 }
214
215 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, ip_str,
216 port_str, &sockaddr);
217 if (ret) {
218 pr_err("malformed ip/port passed: %s\n", name);
219 return ERR_PTR(ret);
220 }
221
222 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
223 ret = iscsit_get_tpg(tpg);
224 if (ret < 0)
225 return ERR_PTR(-EINVAL);
226
227 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
228 " PORTAL: %s\n",
229 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
230 tpg->tpgt, name);
231 /*
232 * Assume ISCSI_TCP by default. Other network portals for other
233 * iSCSI fabrics:
234 *
235 * Traditional iSCSI over SCTP (initial support)
236 * iSER/TCP (TODO, hardware available)
237 * iSER/SCTP (TODO, software emulation with osc-iwarp)
238 * iSER/IB (TODO, hardware available)
239 *
240 * can be enabled with attributes under
241 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
242 *
243 */
244 tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
245 ISCSI_TCP);
246 if (IS_ERR(tpg_np)) {
247 iscsit_put_tpg(tpg);
248 return ERR_CAST(tpg_np);
249 }
250 pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
251
252 iscsit_put_tpg(tpg);
253 return &tpg_np->se_tpg_np;
254 }
255
256 static void lio_target_call_delnpfromtpg(
257 struct se_tpg_np *se_tpg_np)
258 {
259 struct iscsi_portal_group *tpg;
260 struct iscsi_tpg_np *tpg_np;
261 struct se_portal_group *se_tpg;
262 int ret;
263
264 tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
265 tpg = tpg_np->tpg;
266 ret = iscsit_get_tpg(tpg);
267 if (ret < 0)
268 return;
269
270 se_tpg = &tpg->tpg_se_tpg;
271 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
272 " PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
273 tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
274
275 ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
276 if (ret < 0)
277 goto out;
278
279 pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
280 out:
281 iscsit_put_tpg(tpg);
282 }
283
284 /* End items for lio_target_np_cit */
285
286 /* Start items for lio_target_nacl_attrib_cit */
287
288 #define ISCSI_NACL_ATTR(name) \
289 static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
290 char *page) \
291 { \
292 struct se_node_acl *se_nacl = attrib_to_nacl(item); \
293 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
294 se_node_acl); \
295 \
296 return sprintf(page, "%u\n", nacl->node_attrib.name); \
297 } \
298 \
299 static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
300 const char *page, size_t count) \
301 { \
302 struct se_node_acl *se_nacl = attrib_to_nacl(item); \
303 struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
304 se_node_acl); \
305 u32 val; \
306 int ret; \
307 \
308 ret = kstrtou32(page, 0, &val); \
309 if (ret) \
310 return ret; \
311 ret = iscsit_na_##name(nacl, val); \
312 if (ret < 0) \
313 return ret; \
314 \
315 return count; \
316 } \
317 \
318 CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
319
320 ISCSI_NACL_ATTR(dataout_timeout);
321 ISCSI_NACL_ATTR(dataout_timeout_retries);
322 ISCSI_NACL_ATTR(default_erl);
323 ISCSI_NACL_ATTR(nopin_timeout);
324 ISCSI_NACL_ATTR(nopin_response_timeout);
325 ISCSI_NACL_ATTR(random_datain_pdu_offsets);
326 ISCSI_NACL_ATTR(random_datain_seq_offsets);
327 ISCSI_NACL_ATTR(random_r2t_offsets);
328
329 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
330 &iscsi_nacl_attrib_attr_dataout_timeout,
331 &iscsi_nacl_attrib_attr_dataout_timeout_retries,
332 &iscsi_nacl_attrib_attr_default_erl,
333 &iscsi_nacl_attrib_attr_nopin_timeout,
334 &iscsi_nacl_attrib_attr_nopin_response_timeout,
335 &iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
336 &iscsi_nacl_attrib_attr_random_datain_seq_offsets,
337 &iscsi_nacl_attrib_attr_random_r2t_offsets,
338 NULL,
339 };
340
341 /* End items for lio_target_nacl_attrib_cit */
342
343 /* Start items for lio_target_nacl_auth_cit */
344
345 #define __DEF_NACL_AUTH_STR(prefix, name, flags) \
346 static ssize_t __iscsi_##prefix##_##name##_show( \
347 struct iscsi_node_acl *nacl, \
348 char *page) \
349 { \
350 struct iscsi_node_auth *auth = &nacl->node_auth; \
351 \
352 if (!capable(CAP_SYS_ADMIN)) \
353 return -EPERM; \
354 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
355 } \
356 \
357 static ssize_t __iscsi_##prefix##_##name##_store( \
358 struct iscsi_node_acl *nacl, \
359 const char *page, \
360 size_t count) \
361 { \
362 struct iscsi_node_auth *auth = &nacl->node_auth; \
363 \
364 if (!capable(CAP_SYS_ADMIN)) \
365 return -EPERM; \
366 if (count >= sizeof(auth->name)) \
367 return -EINVAL; \
368 snprintf(auth->name, sizeof(auth->name), "%s", page); \
369 if (!strncmp("NULL", auth->name, 4)) \
370 auth->naf_flags &= ~flags; \
371 else \
372 auth->naf_flags |= flags; \
373 \
374 if ((auth->naf_flags & NAF_USERID_IN_SET) && \
375 (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
376 auth->authenticate_target = 1; \
377 else \
378 auth->authenticate_target = 0; \
379 \
380 return count; \
381 }
382
383 #define DEF_NACL_AUTH_STR(name, flags) \
384 __DEF_NACL_AUTH_STR(nacl_auth, name, flags) \
385 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item, \
386 char *page) \
387 { \
388 struct se_node_acl *nacl = auth_to_nacl(item); \
389 return __iscsi_nacl_auth_##name##_show(container_of(nacl, \
390 struct iscsi_node_acl, se_node_acl), page); \
391 } \
392 static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item, \
393 const char *page, size_t count) \
394 { \
395 struct se_node_acl *nacl = auth_to_nacl(item); \
396 return __iscsi_nacl_auth_##name##_store(container_of(nacl, \
397 struct iscsi_node_acl, se_node_acl), page, count); \
398 } \
399 \
400 CONFIGFS_ATTR(iscsi_nacl_auth_, name)
401
402 /*
403 * One-way authentication userid
404 */
405 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
406 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
407 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
408 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
409
410 #define __DEF_NACL_AUTH_INT(prefix, name) \
411 static ssize_t __iscsi_##prefix##_##name##_show( \
412 struct iscsi_node_acl *nacl, \
413 char *page) \
414 { \
415 struct iscsi_node_auth *auth = &nacl->node_auth; \
416 \
417 if (!capable(CAP_SYS_ADMIN)) \
418 return -EPERM; \
419 \
420 return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
421 }
422
423 #define DEF_NACL_AUTH_INT(name) \
424 __DEF_NACL_AUTH_INT(nacl_auth, name) \
425 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item, \
426 char *page) \
427 { \
428 struct se_node_acl *nacl = auth_to_nacl(item); \
429 return __iscsi_nacl_auth_##name##_show(container_of(nacl, \
430 struct iscsi_node_acl, se_node_acl), page); \
431 } \
432 \
433 CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
434
435 DEF_NACL_AUTH_INT(authenticate_target);
436
437 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
438 &iscsi_nacl_auth_attr_userid,
439 &iscsi_nacl_auth_attr_password,
440 &iscsi_nacl_auth_attr_authenticate_target,
441 &iscsi_nacl_auth_attr_userid_mutual,
442 &iscsi_nacl_auth_attr_password_mutual,
443 NULL,
444 };
445
446 /* End items for lio_target_nacl_auth_cit */
447
448 /* Start items for lio_target_nacl_param_cit */
449
450 #define ISCSI_NACL_PARAM(name) \
451 static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item, \
452 char *page) \
453 { \
454 struct se_node_acl *se_nacl = param_to_nacl(item); \
455 struct iscsi_session *sess; \
456 struct se_session *se_sess; \
457 ssize_t rb; \
458 \
459 spin_lock_bh(&se_nacl->nacl_sess_lock); \
460 se_sess = se_nacl->nacl_sess; \
461 if (!se_sess) { \
462 rb = snprintf(page, PAGE_SIZE, \
463 "No Active iSCSI Session\n"); \
464 } else { \
465 sess = se_sess->fabric_sess_ptr; \
466 rb = snprintf(page, PAGE_SIZE, "%u\n", \
467 (u32)sess->sess_ops->name); \
468 } \
469 spin_unlock_bh(&se_nacl->nacl_sess_lock); \
470 \
471 return rb; \
472 } \
473 \
474 CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
475
476 ISCSI_NACL_PARAM(MaxConnections);
477 ISCSI_NACL_PARAM(InitialR2T);
478 ISCSI_NACL_PARAM(ImmediateData);
479 ISCSI_NACL_PARAM(MaxBurstLength);
480 ISCSI_NACL_PARAM(FirstBurstLength);
481 ISCSI_NACL_PARAM(DefaultTime2Wait);
482 ISCSI_NACL_PARAM(DefaultTime2Retain);
483 ISCSI_NACL_PARAM(MaxOutstandingR2T);
484 ISCSI_NACL_PARAM(DataPDUInOrder);
485 ISCSI_NACL_PARAM(DataSequenceInOrder);
486 ISCSI_NACL_PARAM(ErrorRecoveryLevel);
487
488 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
489 &iscsi_nacl_param_attr_MaxConnections,
490 &iscsi_nacl_param_attr_InitialR2T,
491 &iscsi_nacl_param_attr_ImmediateData,
492 &iscsi_nacl_param_attr_MaxBurstLength,
493 &iscsi_nacl_param_attr_FirstBurstLength,
494 &iscsi_nacl_param_attr_DefaultTime2Wait,
495 &iscsi_nacl_param_attr_DefaultTime2Retain,
496 &iscsi_nacl_param_attr_MaxOutstandingR2T,
497 &iscsi_nacl_param_attr_DataPDUInOrder,
498 &iscsi_nacl_param_attr_DataSequenceInOrder,
499 &iscsi_nacl_param_attr_ErrorRecoveryLevel,
500 NULL,
501 };
502
503 /* End items for lio_target_nacl_param_cit */
504
505 /* Start items for lio_target_acl_cit */
506
507 static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
508 {
509 struct se_node_acl *se_nacl = acl_to_nacl(item);
510 struct iscsi_session *sess;
511 struct iscsi_conn *conn;
512 struct se_session *se_sess;
513 ssize_t rb = 0;
514 u32 max_cmd_sn;
515
516 spin_lock_bh(&se_nacl->nacl_sess_lock);
517 se_sess = se_nacl->nacl_sess;
518 if (!se_sess) {
519 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
520 " Endpoint: %s\n", se_nacl->initiatorname);
521 } else {
522 sess = se_sess->fabric_sess_ptr;
523
524 rb += sprintf(page+rb, "InitiatorName: %s\n",
525 sess->sess_ops->InitiatorName);
526 rb += sprintf(page+rb, "InitiatorAlias: %s\n",
527 sess->sess_ops->InitiatorAlias);
528
529 rb += sprintf(page+rb,
530 "LIO Session ID: %u ISID: 0x%6ph TSIH: %hu ",
531 sess->sid, sess->isid, sess->tsih);
532 rb += sprintf(page+rb, "SessionType: %s\n",
533 (sess->sess_ops->SessionType) ?
534 "Discovery" : "Normal");
535 rb += sprintf(page+rb, "Session State: ");
536 switch (sess->session_state) {
537 case TARG_SESS_STATE_FREE:
538 rb += sprintf(page+rb, "TARG_SESS_FREE\n");
539 break;
540 case TARG_SESS_STATE_ACTIVE:
541 rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
542 break;
543 case TARG_SESS_STATE_LOGGED_IN:
544 rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
545 break;
546 case TARG_SESS_STATE_FAILED:
547 rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
548 break;
549 case TARG_SESS_STATE_IN_CONTINUE:
550 rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
551 break;
552 default:
553 rb += sprintf(page+rb, "ERROR: Unknown Session"
554 " State!\n");
555 break;
556 }
557
558 rb += sprintf(page+rb, "---------------------[iSCSI Session"
559 " Values]-----------------------\n");
560 rb += sprintf(page+rb, " CmdSN/WR : CmdSN/WC : ExpCmdSN"
561 " : MaxCmdSN : ITT : TTT\n");
562 max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
563 rb += sprintf(page+rb, " 0x%08x 0x%08x 0x%08x 0x%08x"
564 " 0x%08x 0x%08x\n",
565 sess->cmdsn_window,
566 (max_cmd_sn - sess->exp_cmd_sn) + 1,
567 sess->exp_cmd_sn, max_cmd_sn,
568 sess->init_task_tag, sess->targ_xfer_tag);
569 rb += sprintf(page+rb, "----------------------[iSCSI"
570 " Connections]-------------------------\n");
571
572 spin_lock(&sess->conn_lock);
573 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
574 rb += sprintf(page+rb, "CID: %hu Connection"
575 " State: ", conn->cid);
576 switch (conn->conn_state) {
577 case TARG_CONN_STATE_FREE:
578 rb += sprintf(page+rb,
579 "TARG_CONN_STATE_FREE\n");
580 break;
581 case TARG_CONN_STATE_XPT_UP:
582 rb += sprintf(page+rb,
583 "TARG_CONN_STATE_XPT_UP\n");
584 break;
585 case TARG_CONN_STATE_IN_LOGIN:
586 rb += sprintf(page+rb,
587 "TARG_CONN_STATE_IN_LOGIN\n");
588 break;
589 case TARG_CONN_STATE_LOGGED_IN:
590 rb += sprintf(page+rb,
591 "TARG_CONN_STATE_LOGGED_IN\n");
592 break;
593 case TARG_CONN_STATE_IN_LOGOUT:
594 rb += sprintf(page+rb,
595 "TARG_CONN_STATE_IN_LOGOUT\n");
596 break;
597 case TARG_CONN_STATE_LOGOUT_REQUESTED:
598 rb += sprintf(page+rb,
599 "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
600 break;
601 case TARG_CONN_STATE_CLEANUP_WAIT:
602 rb += sprintf(page+rb,
603 "TARG_CONN_STATE_CLEANUP_WAIT\n");
604 break;
605 default:
606 rb += sprintf(page+rb,
607 "ERROR: Unknown Connection State!\n");
608 break;
609 }
610
611 rb += sprintf(page+rb, " Address %pISc %s", &conn->login_sockaddr,
612 (conn->network_transport == ISCSI_TCP) ?
613 "TCP" : "SCTP");
614 rb += sprintf(page+rb, " StatSN: 0x%08x\n",
615 conn->stat_sn);
616 }
617 spin_unlock(&sess->conn_lock);
618 }
619 spin_unlock_bh(&se_nacl->nacl_sess_lock);
620
621 return rb;
622 }
623
624 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
625 char *page)
626 {
627 return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
628 }
629
630 static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
631 const char *page, size_t count)
632 {
633 struct se_node_acl *se_nacl = acl_to_nacl(item);
634 struct se_portal_group *se_tpg = se_nacl->se_tpg;
635 struct iscsi_portal_group *tpg = container_of(se_tpg,
636 struct iscsi_portal_group, tpg_se_tpg);
637 struct config_item *acl_ci, *tpg_ci, *wwn_ci;
638 u32 cmdsn_depth = 0;
639 int ret;
640
641 ret = kstrtou32(page, 0, &cmdsn_depth);
642 if (ret)
643 return ret;
644 if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
645 pr_err("Passed cmdsn_depth: %u exceeds"
646 " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
647 TA_DEFAULT_CMDSN_DEPTH_MAX);
648 return -EINVAL;
649 }
650 acl_ci = &se_nacl->acl_group.cg_item;
651 if (!acl_ci) {
652 pr_err("Unable to locatel acl_ci\n");
653 return -EINVAL;
654 }
655 tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
656 if (!tpg_ci) {
657 pr_err("Unable to locate tpg_ci\n");
658 return -EINVAL;
659 }
660 wwn_ci = &tpg_ci->ci_group->cg_item;
661 if (!wwn_ci) {
662 pr_err("Unable to locate config_item wwn_ci\n");
663 return -EINVAL;
664 }
665
666 if (iscsit_get_tpg(tpg) < 0)
667 return -EINVAL;
668
669 ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
670
671 pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
672 "InitiatorName: %s\n", config_item_name(wwn_ci),
673 config_item_name(tpg_ci), cmdsn_depth,
674 config_item_name(acl_ci));
675
676 iscsit_put_tpg(tpg);
677 return (!ret) ? count : (ssize_t)ret;
678 }
679
680 static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
681 {
682 return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
683 }
684
685 static ssize_t lio_target_nacl_tag_store(struct config_item *item,
686 const char *page, size_t count)
687 {
688 struct se_node_acl *se_nacl = acl_to_nacl(item);
689 int ret;
690
691 ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
692
693 if (ret < 0)
694 return ret;
695 return count;
696 }
697
698 CONFIGFS_ATTR_RO(lio_target_nacl_, info);
699 CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
700 CONFIGFS_ATTR(lio_target_nacl_, tag);
701
702 static struct configfs_attribute *lio_target_initiator_attrs[] = {
703 &lio_target_nacl_attr_info,
704 &lio_target_nacl_attr_cmdsn_depth,
705 &lio_target_nacl_attr_tag,
706 NULL,
707 };
708
709 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
710 const char *name)
711 {
712 struct iscsi_node_acl *acl =
713 container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
714
715 config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
716 "iscsi_sess_stats", &iscsi_stat_sess_cit);
717 configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
718 &se_nacl->acl_fabric_stat_group);
719 return 0;
720 }
721
722 /* End items for lio_target_acl_cit */
723
724 /* Start items for lio_target_tpg_attrib_cit */
725
726 #define DEF_TPG_ATTRIB(name) \
727 \
728 static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item, \
729 char *page) \
730 { \
731 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
732 struct iscsi_portal_group *tpg = container_of(se_tpg, \
733 struct iscsi_portal_group, tpg_se_tpg); \
734 ssize_t rb; \
735 \
736 if (iscsit_get_tpg(tpg) < 0) \
737 return -EINVAL; \
738 \
739 rb = sprintf(page, "%u\n", tpg->tpg_attrib.name); \
740 iscsit_put_tpg(tpg); \
741 return rb; \
742 } \
743 \
744 static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
745 const char *page, size_t count) \
746 { \
747 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
748 struct iscsi_portal_group *tpg = container_of(se_tpg, \
749 struct iscsi_portal_group, tpg_se_tpg); \
750 u32 val; \
751 int ret; \
752 \
753 if (iscsit_get_tpg(tpg) < 0) \
754 return -EINVAL; \
755 \
756 ret = kstrtou32(page, 0, &val); \
757 if (ret) \
758 goto out; \
759 ret = iscsit_ta_##name(tpg, val); \
760 if (ret < 0) \
761 goto out; \
762 \
763 iscsit_put_tpg(tpg); \
764 return count; \
765 out: \
766 iscsit_put_tpg(tpg); \
767 return ret; \
768 } \
769 CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
770
771 DEF_TPG_ATTRIB(authentication);
772 DEF_TPG_ATTRIB(login_timeout);
773 DEF_TPG_ATTRIB(netif_timeout);
774 DEF_TPG_ATTRIB(generate_node_acls);
775 DEF_TPG_ATTRIB(default_cmdsn_depth);
776 DEF_TPG_ATTRIB(cache_dynamic_acls);
777 DEF_TPG_ATTRIB(demo_mode_write_protect);
778 DEF_TPG_ATTRIB(prod_mode_write_protect);
779 DEF_TPG_ATTRIB(demo_mode_discovery);
780 DEF_TPG_ATTRIB(default_erl);
781 DEF_TPG_ATTRIB(t10_pi);
782 DEF_TPG_ATTRIB(fabric_prot_type);
783 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
784
785 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
786 &iscsi_tpg_attrib_attr_authentication,
787 &iscsi_tpg_attrib_attr_login_timeout,
788 &iscsi_tpg_attrib_attr_netif_timeout,
789 &iscsi_tpg_attrib_attr_generate_node_acls,
790 &iscsi_tpg_attrib_attr_default_cmdsn_depth,
791 &iscsi_tpg_attrib_attr_cache_dynamic_acls,
792 &iscsi_tpg_attrib_attr_demo_mode_write_protect,
793 &iscsi_tpg_attrib_attr_prod_mode_write_protect,
794 &iscsi_tpg_attrib_attr_demo_mode_discovery,
795 &iscsi_tpg_attrib_attr_default_erl,
796 &iscsi_tpg_attrib_attr_t10_pi,
797 &iscsi_tpg_attrib_attr_fabric_prot_type,
798 &iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
799 NULL,
800 };
801
802 /* End items for lio_target_tpg_attrib_cit */
803
804 /* Start items for lio_target_tpg_auth_cit */
805
806 #define __DEF_TPG_AUTH_STR(prefix, name, flags) \
807 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
808 char *page) \
809 { \
810 struct iscsi_portal_group *tpg = container_of(se_tpg, \
811 struct iscsi_portal_group, tpg_se_tpg); \
812 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
813 \
814 if (!capable(CAP_SYS_ADMIN)) \
815 return -EPERM; \
816 \
817 return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
818 } \
819 \
820 static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
821 const char *page, size_t count) \
822 { \
823 struct iscsi_portal_group *tpg = container_of(se_tpg, \
824 struct iscsi_portal_group, tpg_se_tpg); \
825 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
826 \
827 if (!capable(CAP_SYS_ADMIN)) \
828 return -EPERM; \
829 \
830 snprintf(auth->name, sizeof(auth->name), "%s", page); \
831 if (!(strncmp("NULL", auth->name, 4))) \
832 auth->naf_flags &= ~flags; \
833 else \
834 auth->naf_flags |= flags; \
835 \
836 if ((auth->naf_flags & NAF_USERID_IN_SET) && \
837 (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
838 auth->authenticate_target = 1; \
839 else \
840 auth->authenticate_target = 0; \
841 \
842 return count; \
843 }
844
845 #define DEF_TPG_AUTH_STR(name, flags) \
846 __DEF_TPG_AUTH_STR(tpg_auth, name, flags) \
847 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item, \
848 char *page) \
849 { \
850 return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page); \
851 } \
852 \
853 static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item, \
854 const char *page, size_t count) \
855 { \
856 return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count); \
857 } \
858 \
859 CONFIGFS_ATTR(iscsi_tpg_auth_, name);
860
861
862 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
863 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
864 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
865 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
866
867 #define __DEF_TPG_AUTH_INT(prefix, name) \
868 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
869 char *page) \
870 { \
871 struct iscsi_portal_group *tpg = container_of(se_tpg, \
872 struct iscsi_portal_group, tpg_se_tpg); \
873 struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
874 \
875 if (!capable(CAP_SYS_ADMIN)) \
876 return -EPERM; \
877 \
878 return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
879 }
880
881 #define DEF_TPG_AUTH_INT(name) \
882 __DEF_TPG_AUTH_INT(tpg_auth, name) \
883 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item, \
884 char *page) \
885 { \
886 return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page); \
887 } \
888 CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
889
890 DEF_TPG_AUTH_INT(authenticate_target);
891
892 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
893 &iscsi_tpg_auth_attr_userid,
894 &iscsi_tpg_auth_attr_password,
895 &iscsi_tpg_auth_attr_authenticate_target,
896 &iscsi_tpg_auth_attr_userid_mutual,
897 &iscsi_tpg_auth_attr_password_mutual,
898 NULL,
899 };
900
901 /* End items for lio_target_tpg_auth_cit */
902
903 /* Start items for lio_target_tpg_param_cit */
904
905 #define DEF_TPG_PARAM(name) \
906 static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item, \
907 char *page) \
908 { \
909 struct se_portal_group *se_tpg = param_to_tpg(item); \
910 struct iscsi_portal_group *tpg = container_of(se_tpg, \
911 struct iscsi_portal_group, tpg_se_tpg); \
912 struct iscsi_param *param; \
913 ssize_t rb; \
914 \
915 if (iscsit_get_tpg(tpg) < 0) \
916 return -EINVAL; \
917 \
918 param = iscsi_find_param_from_key(__stringify(name), \
919 tpg->param_list); \
920 if (!param) { \
921 iscsit_put_tpg(tpg); \
922 return -EINVAL; \
923 } \
924 rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
925 \
926 iscsit_put_tpg(tpg); \
927 return rb; \
928 } \
929 static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
930 const char *page, size_t count) \
931 { \
932 struct se_portal_group *se_tpg = param_to_tpg(item); \
933 struct iscsi_portal_group *tpg = container_of(se_tpg, \
934 struct iscsi_portal_group, tpg_se_tpg); \
935 char *buf; \
936 int ret, len; \
937 \
938 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
939 if (!buf) \
940 return -ENOMEM; \
941 len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
942 if (isspace(buf[len-1])) \
943 buf[len-1] = '\0'; /* Kill newline */ \
944 \
945 if (iscsit_get_tpg(tpg) < 0) { \
946 kfree(buf); \
947 return -EINVAL; \
948 } \
949 \
950 ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
951 if (ret < 0) \
952 goto out; \
953 \
954 kfree(buf); \
955 iscsit_put_tpg(tpg); \
956 return count; \
957 out: \
958 kfree(buf); \
959 iscsit_put_tpg(tpg); \
960 return -EINVAL; \
961 } \
962 CONFIGFS_ATTR(iscsi_tpg_param_, name)
963
964 DEF_TPG_PARAM(AuthMethod);
965 DEF_TPG_PARAM(HeaderDigest);
966 DEF_TPG_PARAM(DataDigest);
967 DEF_TPG_PARAM(MaxConnections);
968 DEF_TPG_PARAM(TargetAlias);
969 DEF_TPG_PARAM(InitialR2T);
970 DEF_TPG_PARAM(ImmediateData);
971 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
972 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
973 DEF_TPG_PARAM(MaxBurstLength);
974 DEF_TPG_PARAM(FirstBurstLength);
975 DEF_TPG_PARAM(DefaultTime2Wait);
976 DEF_TPG_PARAM(DefaultTime2Retain);
977 DEF_TPG_PARAM(MaxOutstandingR2T);
978 DEF_TPG_PARAM(DataPDUInOrder);
979 DEF_TPG_PARAM(DataSequenceInOrder);
980 DEF_TPG_PARAM(ErrorRecoveryLevel);
981 DEF_TPG_PARAM(IFMarker);
982 DEF_TPG_PARAM(OFMarker);
983 DEF_TPG_PARAM(IFMarkInt);
984 DEF_TPG_PARAM(OFMarkInt);
985
986 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
987 &iscsi_tpg_param_attr_AuthMethod,
988 &iscsi_tpg_param_attr_HeaderDigest,
989 &iscsi_tpg_param_attr_DataDigest,
990 &iscsi_tpg_param_attr_MaxConnections,
991 &iscsi_tpg_param_attr_TargetAlias,
992 &iscsi_tpg_param_attr_InitialR2T,
993 &iscsi_tpg_param_attr_ImmediateData,
994 &iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
995 &iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
996 &iscsi_tpg_param_attr_MaxBurstLength,
997 &iscsi_tpg_param_attr_FirstBurstLength,
998 &iscsi_tpg_param_attr_DefaultTime2Wait,
999 &iscsi_tpg_param_attr_DefaultTime2Retain,
1000 &iscsi_tpg_param_attr_MaxOutstandingR2T,
1001 &iscsi_tpg_param_attr_DataPDUInOrder,
1002 &iscsi_tpg_param_attr_DataSequenceInOrder,
1003 &iscsi_tpg_param_attr_ErrorRecoveryLevel,
1004 &iscsi_tpg_param_attr_IFMarker,
1005 &iscsi_tpg_param_attr_OFMarker,
1006 &iscsi_tpg_param_attr_IFMarkInt,
1007 &iscsi_tpg_param_attr_OFMarkInt,
1008 NULL,
1009 };
1010
1011 /* End items for lio_target_tpg_param_cit */
1012
1013 /* Start items for lio_target_tpg_cit */
1014
1015 static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1016 {
1017 struct se_portal_group *se_tpg = to_tpg(item);
1018 struct iscsi_portal_group *tpg = container_of(se_tpg,
1019 struct iscsi_portal_group, tpg_se_tpg);
1020 ssize_t len;
1021
1022 spin_lock(&tpg->tpg_state_lock);
1023 len = sprintf(page, "%d\n",
1024 (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1025 spin_unlock(&tpg->tpg_state_lock);
1026
1027 return len;
1028 }
1029
1030 static ssize_t lio_target_tpg_enable_store(struct config_item *item,
1031 const char *page, size_t count)
1032 {
1033 struct se_portal_group *se_tpg = to_tpg(item);
1034 struct iscsi_portal_group *tpg = container_of(se_tpg,
1035 struct iscsi_portal_group, tpg_se_tpg);
1036 u32 op;
1037 int ret;
1038
1039 ret = kstrtou32(page, 0, &op);
1040 if (ret)
1041 return ret;
1042 if ((op != 1) && (op != 0)) {
1043 pr_err("Illegal value for tpg_enable: %u\n", op);
1044 return -EINVAL;
1045 }
1046
1047 ret = iscsit_get_tpg(tpg);
1048 if (ret < 0)
1049 return -EINVAL;
1050
1051 if (op) {
1052 ret = iscsit_tpg_enable_portal_group(tpg);
1053 if (ret < 0)
1054 goto out;
1055 } else {
1056 /*
1057 * iscsit_tpg_disable_portal_group() assumes force=1
1058 */
1059 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1060 if (ret < 0)
1061 goto out;
1062 }
1063
1064 iscsit_put_tpg(tpg);
1065 return count;
1066 out:
1067 iscsit_put_tpg(tpg);
1068 return -EINVAL;
1069 }
1070
1071
1072 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1073 char *page)
1074 {
1075 return target_show_dynamic_sessions(to_tpg(item), page);
1076 }
1077
1078 CONFIGFS_ATTR(lio_target_tpg_, enable);
1079 CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1080
1081 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1082 &lio_target_tpg_attr_enable,
1083 &lio_target_tpg_attr_dynamic_sessions,
1084 NULL,
1085 };
1086
1087 /* End items for lio_target_tpg_cit */
1088
1089 /* Start items for lio_target_tiqn_cit */
1090
1091 static struct se_portal_group *lio_target_tiqn_addtpg(
1092 struct se_wwn *wwn,
1093 struct config_group *group,
1094 const char *name)
1095 {
1096 struct iscsi_portal_group *tpg;
1097 struct iscsi_tiqn *tiqn;
1098 char *tpgt_str;
1099 int ret;
1100 u16 tpgt;
1101
1102 tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1103 /*
1104 * Only tpgt_# directory groups can be created below
1105 * target/iscsi/iqn.superturodiskarry/
1106 */
1107 tpgt_str = strstr(name, "tpgt_");
1108 if (!tpgt_str) {
1109 pr_err("Unable to locate \"tpgt_#\" directory"
1110 " group\n");
1111 return NULL;
1112 }
1113 tpgt_str += 5; /* Skip ahead of "tpgt_" */
1114 ret = kstrtou16(tpgt_str, 0, &tpgt);
1115 if (ret)
1116 return NULL;
1117
1118 tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1119 if (!tpg)
1120 return NULL;
1121
1122 ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1123 if (ret < 0)
1124 return NULL;
1125
1126 ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1127 if (ret != 0)
1128 goto out;
1129
1130 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1131 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1132 name);
1133 return &tpg->tpg_se_tpg;
1134 out:
1135 core_tpg_deregister(&tpg->tpg_se_tpg);
1136 kfree(tpg);
1137 return NULL;
1138 }
1139
1140 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1141 {
1142 struct iscsi_portal_group *tpg;
1143 struct iscsi_tiqn *tiqn;
1144
1145 tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1146 tiqn = tpg->tpg_tiqn;
1147 /*
1148 * iscsit_tpg_del_portal_group() assumes force=1
1149 */
1150 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1151 iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1152 }
1153
1154 /* End items for lio_target_tiqn_cit */
1155
1156 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1157
1158 static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1159 char *page)
1160 {
1161 return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1162 }
1163
1164 CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1165
1166 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1167 &lio_target_wwn_attr_lio_version,
1168 NULL,
1169 };
1170
1171 static struct se_wwn *lio_target_call_coreaddtiqn(
1172 struct target_fabric_configfs *tf,
1173 struct config_group *group,
1174 const char *name)
1175 {
1176 struct iscsi_tiqn *tiqn;
1177
1178 tiqn = iscsit_add_tiqn((unsigned char *)name);
1179 if (IS_ERR(tiqn))
1180 return ERR_CAST(tiqn);
1181
1182 pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1183 pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1184 " %s\n", name);
1185 return &tiqn->tiqn_wwn;
1186 }
1187
1188 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1189 {
1190 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1191
1192 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1193 "iscsi_instance", &iscsi_stat_instance_cit);
1194 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1195 &tiqn->tiqn_wwn.fabric_stat_group);
1196
1197 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1198 "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1199 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1200 &tiqn->tiqn_wwn.fabric_stat_group);
1201
1202 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1203 "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1204 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1205 &tiqn->tiqn_wwn.fabric_stat_group);
1206
1207 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1208 "iscsi_login_stats", &iscsi_stat_login_cit);
1209 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1210 &tiqn->tiqn_wwn.fabric_stat_group);
1211
1212 config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1213 "iscsi_logout_stats", &iscsi_stat_logout_cit);
1214 configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1215 &tiqn->tiqn_wwn.fabric_stat_group);
1216 }
1217
1218 static void lio_target_call_coredeltiqn(
1219 struct se_wwn *wwn)
1220 {
1221 struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1222
1223 pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1224 tiqn->tiqn);
1225 iscsit_del_tiqn(tiqn);
1226 }
1227
1228 /* End LIO-Target TIQN struct contig_lio_target_cit */
1229
1230 /* Start lio_target_discovery_auth_cit */
1231
1232 #define DEF_DISC_AUTH_STR(name, flags) \
1233 __DEF_NACL_AUTH_STR(disc, name, flags) \
1234 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1235 { \
1236 return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1237 page); \
1238 } \
1239 static ssize_t iscsi_disc_##name##_store(struct config_item *item, \
1240 const char *page, size_t count) \
1241 { \
1242 return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl, \
1243 page, count); \
1244 \
1245 } \
1246 CONFIGFS_ATTR(iscsi_disc_, name)
1247
1248 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1249 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1250 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1251 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1252
1253 #define DEF_DISC_AUTH_INT(name) \
1254 __DEF_NACL_AUTH_INT(disc, name) \
1255 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1256 { \
1257 return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1258 page); \
1259 } \
1260 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1261
1262 DEF_DISC_AUTH_INT(authenticate_target);
1263
1264
1265 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1266 char *page)
1267 {
1268 struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1269
1270 return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1271 }
1272
1273 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1274 const char *page, size_t count)
1275 {
1276 struct iscsi_param *param;
1277 struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1278 u32 op;
1279 int err;
1280
1281 err = kstrtou32(page, 0, &op);
1282 if (err)
1283 return -EINVAL;
1284 if ((op != 1) && (op != 0)) {
1285 pr_err("Illegal value for enforce_discovery_auth:"
1286 " %u\n", op);
1287 return -EINVAL;
1288 }
1289
1290 if (!discovery_tpg) {
1291 pr_err("iscsit_global->discovery_tpg is NULL\n");
1292 return -EINVAL;
1293 }
1294
1295 param = iscsi_find_param_from_key(AUTHMETHOD,
1296 discovery_tpg->param_list);
1297 if (!param)
1298 return -EINVAL;
1299
1300 if (op) {
1301 /*
1302 * Reset the AuthMethod key to CHAP.
1303 */
1304 if (iscsi_update_param_value(param, CHAP) < 0)
1305 return -EINVAL;
1306
1307 discovery_tpg->tpg_attrib.authentication = 1;
1308 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1309 pr_debug("LIO-CORE[0] Successfully enabled"
1310 " authentication enforcement for iSCSI"
1311 " Discovery TPG\n");
1312 } else {
1313 /*
1314 * Reset the AuthMethod key to CHAP,None
1315 */
1316 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1317 return -EINVAL;
1318
1319 discovery_tpg->tpg_attrib.authentication = 0;
1320 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1321 pr_debug("LIO-CORE[0] Successfully disabled"
1322 " authentication enforcement for iSCSI"
1323 " Discovery TPG\n");
1324 }
1325
1326 return count;
1327 }
1328
1329 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1330
1331 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1332 &iscsi_disc_attr_userid,
1333 &iscsi_disc_attr_password,
1334 &iscsi_disc_attr_authenticate_target,
1335 &iscsi_disc_attr_userid_mutual,
1336 &iscsi_disc_attr_password_mutual,
1337 &iscsi_disc_attr_enforce_discovery_auth,
1338 NULL,
1339 };
1340
1341 /* End lio_target_discovery_auth_cit */
1342
1343 /* Start functions for target_core_fabric_ops */
1344
1345 static char *iscsi_get_fabric_name(void)
1346 {
1347 return "iSCSI";
1348 }
1349
1350 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1351 {
1352 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1353
1354 return cmd->i_state;
1355 }
1356
1357 static u32 lio_sess_get_index(struct se_session *se_sess)
1358 {
1359 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1360
1361 return sess->session_index;
1362 }
1363
1364 static u32 lio_sess_get_initiator_sid(
1365 struct se_session *se_sess,
1366 unsigned char *buf,
1367 u32 size)
1368 {
1369 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1370 /*
1371 * iSCSI Initiator Session Identifier from RFC-3720.
1372 */
1373 return snprintf(buf, size, "%6phN", sess->isid);
1374 }
1375
1376 static int lio_queue_data_in(struct se_cmd *se_cmd)
1377 {
1378 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1379 struct iscsi_conn *conn = cmd->conn;
1380
1381 cmd->i_state = ISTATE_SEND_DATAIN;
1382 return conn->conn_transport->iscsit_queue_data_in(conn, cmd);
1383 }
1384
1385 static int lio_write_pending(struct se_cmd *se_cmd)
1386 {
1387 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1388 struct iscsi_conn *conn = cmd->conn;
1389
1390 if (!cmd->immediate_data && !cmd->unsolicited_data)
1391 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1392
1393 return 0;
1394 }
1395
1396 static int lio_write_pending_status(struct se_cmd *se_cmd)
1397 {
1398 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1399 int ret;
1400
1401 spin_lock_bh(&cmd->istate_lock);
1402 ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1403 spin_unlock_bh(&cmd->istate_lock);
1404
1405 return ret;
1406 }
1407
1408 static int lio_queue_status(struct se_cmd *se_cmd)
1409 {
1410 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1411 struct iscsi_conn *conn = cmd->conn;
1412
1413 cmd->i_state = ISTATE_SEND_STATUS;
1414
1415 if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1416 return iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1417 }
1418 return conn->conn_transport->iscsit_queue_status(conn, cmd);
1419 }
1420
1421 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1422 {
1423 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1424
1425 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1426 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1427 }
1428
1429 static void lio_aborted_task(struct se_cmd *se_cmd)
1430 {
1431 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1432
1433 cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1434 }
1435
1436 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1437 {
1438 return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1439 }
1440
1441 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1442 {
1443 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1444 }
1445
1446 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1447 {
1448 return iscsi_tpg(se_tpg)->tpgt;
1449 }
1450
1451 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1452 {
1453 return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1454 }
1455
1456 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1457 {
1458 return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1459 }
1460
1461 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1462 {
1463 return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1464 }
1465
1466 static int lio_tpg_check_demo_mode_write_protect(
1467 struct se_portal_group *se_tpg)
1468 {
1469 return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1470 }
1471
1472 static int lio_tpg_check_prod_mode_write_protect(
1473 struct se_portal_group *se_tpg)
1474 {
1475 return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1476 }
1477
1478 static int lio_tpg_check_prot_fabric_only(
1479 struct se_portal_group *se_tpg)
1480 {
1481 /*
1482 * Only report fabric_prot_type if t10_pi has also been enabled
1483 * for incoming ib_isert sessions.
1484 */
1485 if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1486 return 0;
1487 return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1488 }
1489
1490 /*
1491 * This function calls iscsit_inc_session_usage_count() on the
1492 * struct iscsi_session in question.
1493 */
1494 static void lio_tpg_close_session(struct se_session *se_sess)
1495 {
1496 struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1497 struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1498
1499 spin_lock_bh(&se_tpg->session_lock);
1500 spin_lock(&sess->conn_lock);
1501 if (atomic_read(&sess->session_fall_back_to_erl0) ||
1502 atomic_read(&sess->session_logout) ||
1503 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1504 spin_unlock(&sess->conn_lock);
1505 spin_unlock_bh(&se_tpg->session_lock);
1506 return;
1507 }
1508 atomic_set(&sess->session_reinstatement, 1);
1509 atomic_set(&sess->session_fall_back_to_erl0, 1);
1510 spin_unlock(&sess->conn_lock);
1511
1512 iscsit_stop_time2retain_timer(sess);
1513 spin_unlock_bh(&se_tpg->session_lock);
1514
1515 iscsit_stop_session(sess, 1, 1);
1516 iscsit_close_session(sess);
1517 }
1518
1519 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1520 {
1521 return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1522 }
1523
1524 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1525 {
1526 struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1527 se_node_acl);
1528 struct se_portal_group *se_tpg = se_acl->se_tpg;
1529 struct iscsi_portal_group *tpg = container_of(se_tpg,
1530 struct iscsi_portal_group, tpg_se_tpg);
1531
1532 acl->node_attrib.nacl = acl;
1533 iscsit_set_default_node_attribues(acl, tpg);
1534 }
1535
1536 static int lio_check_stop_free(struct se_cmd *se_cmd)
1537 {
1538 return target_put_sess_cmd(se_cmd);
1539 }
1540
1541 static void lio_release_cmd(struct se_cmd *se_cmd)
1542 {
1543 struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1544
1545 pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1546 iscsit_release_cmd(cmd);
1547 }
1548
1549 const struct target_core_fabric_ops iscsi_ops = {
1550 .module = THIS_MODULE,
1551 .name = "iscsi",
1552 .node_acl_size = sizeof(struct iscsi_node_acl),
1553 .get_fabric_name = iscsi_get_fabric_name,
1554 .tpg_get_wwn = lio_tpg_get_endpoint_wwn,
1555 .tpg_get_tag = lio_tpg_get_tag,
1556 .tpg_get_default_depth = lio_tpg_get_default_depth,
1557 .tpg_check_demo_mode = lio_tpg_check_demo_mode,
1558 .tpg_check_demo_mode_cache = lio_tpg_check_demo_mode_cache,
1559 .tpg_check_demo_mode_write_protect =
1560 lio_tpg_check_demo_mode_write_protect,
1561 .tpg_check_prod_mode_write_protect =
1562 lio_tpg_check_prod_mode_write_protect,
1563 .tpg_check_prot_fabric_only = &lio_tpg_check_prot_fabric_only,
1564 .tpg_get_inst_index = lio_tpg_get_inst_index,
1565 .check_stop_free = lio_check_stop_free,
1566 .release_cmd = lio_release_cmd,
1567 .close_session = lio_tpg_close_session,
1568 .sess_get_index = lio_sess_get_index,
1569 .sess_get_initiator_sid = lio_sess_get_initiator_sid,
1570 .write_pending = lio_write_pending,
1571 .write_pending_status = lio_write_pending_status,
1572 .set_default_node_attributes = lio_set_default_node_attributes,
1573 .get_cmd_state = iscsi_get_cmd_state,
1574 .queue_data_in = lio_queue_data_in,
1575 .queue_status = lio_queue_status,
1576 .queue_tm_rsp = lio_queue_tm_rsp,
1577 .aborted_task = lio_aborted_task,
1578 .fabric_make_wwn = lio_target_call_coreaddtiqn,
1579 .fabric_drop_wwn = lio_target_call_coredeltiqn,
1580 .add_wwn_groups = lio_target_add_wwn_groups,
1581 .fabric_make_tpg = lio_target_tiqn_addtpg,
1582 .fabric_drop_tpg = lio_target_tiqn_deltpg,
1583 .fabric_make_np = lio_target_call_addnptotpg,
1584 .fabric_drop_np = lio_target_call_delnpfromtpg,
1585 .fabric_init_nodeacl = lio_target_init_nodeacl,
1586
1587 .tfc_discovery_attrs = lio_target_discovery_auth_attrs,
1588 .tfc_wwn_attrs = lio_target_wwn_attrs,
1589 .tfc_tpg_base_attrs = lio_target_tpg_attrs,
1590 .tfc_tpg_attrib_attrs = lio_target_tpg_attrib_attrs,
1591 .tfc_tpg_auth_attrs = lio_target_tpg_auth_attrs,
1592 .tfc_tpg_param_attrs = lio_target_tpg_param_attrs,
1593 .tfc_tpg_np_base_attrs = lio_target_portal_attrs,
1594 .tfc_tpg_nacl_base_attrs = lio_target_initiator_attrs,
1595 .tfc_tpg_nacl_attrib_attrs = lio_target_nacl_attrib_attrs,
1596 .tfc_tpg_nacl_auth_attrs = lio_target_nacl_auth_attrs,
1597 .tfc_tpg_nacl_param_attrs = lio_target_nacl_param_attrs,
1598 };