xref: /linux/drivers/target/iscsi/iscsi_target_configfs.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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 <target/target_core_base.h>
25 #include <target/target_core_fabric.h>
26 #include <target/target_core_fabric_configfs.h>
27 #include <target/configfs_macros.h>
28 #include <target/iscsi/iscsi_transport.h>
29 
30 #include <target/iscsi/iscsi_target_core.h>
31 #include "iscsi_target_parameters.h"
32 #include "iscsi_target_device.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_nodeattrib.h"
35 #include "iscsi_target_tpg.h"
36 #include "iscsi_target_util.h"
37 #include "iscsi_target.h"
38 #include <target/iscsi/iscsi_target_stat.h>
39 
40 struct lio_target_configfs_attribute {
41 	struct configfs_attribute attr;
42 	ssize_t (*show)(void *, char *);
43 	ssize_t (*store)(void *, const char *, size_t);
44 };
45 
46 /* Start items for lio_target_portal_cit */
47 
48 static ssize_t lio_target_np_show_sctp(
49 	struct se_tpg_np *se_tpg_np,
50 	char *page)
51 {
52 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
53 				struct iscsi_tpg_np, se_tpg_np);
54 	struct iscsi_tpg_np *tpg_np_sctp;
55 	ssize_t rb;
56 
57 	tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
58 	if (tpg_np_sctp)
59 		rb = sprintf(page, "1\n");
60 	else
61 		rb = sprintf(page, "0\n");
62 
63 	return rb;
64 }
65 
66 static ssize_t lio_target_np_store_sctp(
67 	struct se_tpg_np *se_tpg_np,
68 	const char *page,
69 	size_t count)
70 {
71 	struct iscsi_np *np;
72 	struct iscsi_portal_group *tpg;
73 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
74 				struct iscsi_tpg_np, se_tpg_np);
75 	struct iscsi_tpg_np *tpg_np_sctp = NULL;
76 	u32 op;
77 	int ret;
78 
79 	ret = kstrtou32(page, 0, &op);
80 	if (ret)
81 		return ret;
82 	if ((op != 1) && (op != 0)) {
83 		pr_err("Illegal value for tpg_enable: %u\n", op);
84 		return -EINVAL;
85 	}
86 	np = tpg_np->tpg_np;
87 	if (!np) {
88 		pr_err("Unable to locate struct iscsi_np from"
89 				" struct iscsi_tpg_np\n");
90 		return -EINVAL;
91 	}
92 
93 	tpg = tpg_np->tpg;
94 	if (iscsit_get_tpg(tpg) < 0)
95 		return -EINVAL;
96 
97 	if (op) {
98 		/*
99 		 * Use existing np->np_sockaddr for SCTP network portal reference
100 		 */
101 		tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
102 					tpg_np, ISCSI_SCTP_TCP);
103 		if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
104 			goto out;
105 	} else {
106 		tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
107 		if (!tpg_np_sctp)
108 			goto out;
109 
110 		ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
111 		if (ret < 0)
112 			goto out;
113 	}
114 
115 	iscsit_put_tpg(tpg);
116 	return count;
117 out:
118 	iscsit_put_tpg(tpg);
119 	return -EINVAL;
120 }
121 
122 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
123 
124 static ssize_t lio_target_np_show_iser(
125 	struct se_tpg_np *se_tpg_np,
126 	char *page)
127 {
128 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
129 				struct iscsi_tpg_np, se_tpg_np);
130 	struct iscsi_tpg_np *tpg_np_iser;
131 	ssize_t rb;
132 
133 	tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
134 	if (tpg_np_iser)
135 		rb = sprintf(page, "1\n");
136 	else
137 		rb = sprintf(page, "0\n");
138 
139 	return rb;
140 }
141 
142 static ssize_t lio_target_np_store_iser(
143 	struct se_tpg_np *se_tpg_np,
144 	const char *page,
145 	size_t count)
146 {
147 	struct iscsi_np *np;
148 	struct iscsi_portal_group *tpg;
149 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
150 				struct iscsi_tpg_np, se_tpg_np);
151 	struct iscsi_tpg_np *tpg_np_iser = NULL;
152 	char *endptr;
153 	u32 op;
154 	int rc = 0;
155 
156 	op = simple_strtoul(page, &endptr, 0);
157 	if ((op != 1) && (op != 0)) {
158 		pr_err("Illegal value for tpg_enable: %u\n", op);
159 		return -EINVAL;
160 	}
161 	np = tpg_np->tpg_np;
162 	if (!np) {
163 		pr_err("Unable to locate struct iscsi_np from"
164 				" struct iscsi_tpg_np\n");
165 		return -EINVAL;
166 	}
167 
168 	tpg = tpg_np->tpg;
169 	if (iscsit_get_tpg(tpg) < 0)
170 		return -EINVAL;
171 
172 	if (op) {
173 		rc = request_module("ib_isert");
174 		if (rc != 0) {
175 			pr_warn("Unable to request_module for ib_isert\n");
176 			rc = 0;
177 		}
178 
179 		tpg_np_iser = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
180 				tpg_np, ISCSI_INFINIBAND);
181 		if (IS_ERR(tpg_np_iser)) {
182 			rc = PTR_ERR(tpg_np_iser);
183 			goto out;
184 		}
185 	} else {
186 		tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
187 		if (tpg_np_iser) {
188 			rc = iscsit_tpg_del_network_portal(tpg, tpg_np_iser);
189 			if (rc < 0)
190 				goto out;
191 		}
192 	}
193 
194 	iscsit_put_tpg(tpg);
195 	return count;
196 out:
197 	iscsit_put_tpg(tpg);
198 	return rc;
199 }
200 
201 TF_NP_BASE_ATTR(lio_target, iser, S_IRUGO | S_IWUSR);
202 
203 static struct configfs_attribute *lio_target_portal_attrs[] = {
204 	&lio_target_np_sctp.attr,
205 	&lio_target_np_iser.attr,
206 	NULL,
207 };
208 
209 /* Stop items for lio_target_portal_cit */
210 
211 /* Start items for lio_target_np_cit */
212 
213 #define MAX_PORTAL_LEN		256
214 
215 static struct se_tpg_np *lio_target_call_addnptotpg(
216 	struct se_portal_group *se_tpg,
217 	struct config_group *group,
218 	const char *name)
219 {
220 	struct iscsi_portal_group *tpg;
221 	struct iscsi_tpg_np *tpg_np;
222 	char *str, *str2, *ip_str, *port_str;
223 	struct sockaddr_storage sockaddr;
224 	struct sockaddr_in *sock_in;
225 	struct sockaddr_in6 *sock_in6;
226 	unsigned long port;
227 	int ret;
228 	char buf[MAX_PORTAL_LEN + 1];
229 
230 	if (strlen(name) > MAX_PORTAL_LEN) {
231 		pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
232 			(int)strlen(name), MAX_PORTAL_LEN);
233 		return ERR_PTR(-EOVERFLOW);
234 	}
235 	memset(buf, 0, MAX_PORTAL_LEN + 1);
236 	snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
237 
238 	memset(&sockaddr, 0, sizeof(struct sockaddr_storage));
239 
240 	str = strstr(buf, "[");
241 	if (str) {
242 		const char *end;
243 
244 		str2 = strstr(str, "]");
245 		if (!str2) {
246 			pr_err("Unable to locate trailing \"]\""
247 				" in IPv6 iSCSI network portal address\n");
248 			return ERR_PTR(-EINVAL);
249 		}
250 		str++; /* Skip over leading "[" */
251 		*str2 = '\0'; /* Terminate the unbracketed IPv6 address */
252 		str2++; /* Skip over the \0 */
253 		port_str = strstr(str2, ":");
254 		if (!port_str) {
255 			pr_err("Unable to locate \":port\""
256 				" in IPv6 iSCSI network portal address\n");
257 			return ERR_PTR(-EINVAL);
258 		}
259 		*port_str = '\0'; /* Terminate string for IP */
260 		port_str++; /* Skip over ":" */
261 
262 		ret = kstrtoul(port_str, 0, &port);
263 		if (ret < 0) {
264 			pr_err("kstrtoul() failed for port_str: %d\n", ret);
265 			return ERR_PTR(ret);
266 		}
267 		sock_in6 = (struct sockaddr_in6 *)&sockaddr;
268 		sock_in6->sin6_family = AF_INET6;
269 		sock_in6->sin6_port = htons((unsigned short)port);
270 		ret = in6_pton(str, -1,
271 				(void *)&sock_in6->sin6_addr.in6_u, -1, &end);
272 		if (ret <= 0) {
273 			pr_err("in6_pton returned: %d\n", ret);
274 			return ERR_PTR(-EINVAL);
275 		}
276 	} else {
277 		str = ip_str = &buf[0];
278 		port_str = strstr(ip_str, ":");
279 		if (!port_str) {
280 			pr_err("Unable to locate \":port\""
281 				" in IPv4 iSCSI network portal address\n");
282 			return ERR_PTR(-EINVAL);
283 		}
284 		*port_str = '\0'; /* Terminate string for IP */
285 		port_str++; /* Skip over ":" */
286 
287 		ret = kstrtoul(port_str, 0, &port);
288 		if (ret < 0) {
289 			pr_err("kstrtoul() failed for port_str: %d\n", ret);
290 			return ERR_PTR(ret);
291 		}
292 		sock_in = (struct sockaddr_in *)&sockaddr;
293 		sock_in->sin_family = AF_INET;
294 		sock_in->sin_port = htons((unsigned short)port);
295 		sock_in->sin_addr.s_addr = in_aton(ip_str);
296 	}
297 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
298 	ret = iscsit_get_tpg(tpg);
299 	if (ret < 0)
300 		return ERR_PTR(-EINVAL);
301 
302 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
303 		" PORTAL: %s\n",
304 		config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
305 		tpg->tpgt, name);
306 	/*
307 	 * Assume ISCSI_TCP by default.  Other network portals for other
308 	 * iSCSI fabrics:
309 	 *
310 	 * Traditional iSCSI over SCTP (initial support)
311 	 * iSER/TCP (TODO, hardware available)
312 	 * iSER/SCTP (TODO, software emulation with osc-iwarp)
313 	 * iSER/IB (TODO, hardware available)
314 	 *
315 	 * can be enabled with attributes under
316 	 * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
317 	 *
318 	 */
319 	tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
320 				ISCSI_TCP);
321 	if (IS_ERR(tpg_np)) {
322 		iscsit_put_tpg(tpg);
323 		return ERR_CAST(tpg_np);
324 	}
325 	pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
326 
327 	iscsit_put_tpg(tpg);
328 	return &tpg_np->se_tpg_np;
329 }
330 
331 static void lio_target_call_delnpfromtpg(
332 	struct se_tpg_np *se_tpg_np)
333 {
334 	struct iscsi_portal_group *tpg;
335 	struct iscsi_tpg_np *tpg_np;
336 	struct se_portal_group *se_tpg;
337 	int ret;
338 
339 	tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
340 	tpg = tpg_np->tpg;
341 	ret = iscsit_get_tpg(tpg);
342 	if (ret < 0)
343 		return;
344 
345 	se_tpg = &tpg->tpg_se_tpg;
346 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
347 		" PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
348 		tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
349 
350 	ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
351 	if (ret < 0)
352 		goto out;
353 
354 	pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
355 out:
356 	iscsit_put_tpg(tpg);
357 }
358 
359 /* End items for lio_target_np_cit */
360 
361 /* Start items for lio_target_nacl_attrib_cit */
362 
363 #define DEF_NACL_ATTRIB(name)						\
364 static ssize_t iscsi_nacl_attrib_show_##name(				\
365 	struct se_node_acl *se_nacl,					\
366 	char *page)							\
367 {									\
368 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
369 					se_node_acl);			\
370 									\
371 	return sprintf(page, "%u\n", nacl->node_attrib.name);		\
372 }									\
373 									\
374 static ssize_t iscsi_nacl_attrib_store_##name(				\
375 	struct se_node_acl *se_nacl,					\
376 	const char *page,						\
377 	size_t count)							\
378 {									\
379 	struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
380 					se_node_acl);			\
381 	u32 val;							\
382 	int ret;							\
383 									\
384 	ret = kstrtou32(page, 0, &val);					\
385 	if (ret)							\
386 		return ret;						\
387 	ret = iscsit_na_##name(nacl, val);				\
388 	if (ret < 0)							\
389 		return ret;						\
390 									\
391 	return count;							\
392 }
393 
394 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
395 /*
396  * Define iscsi_node_attrib_s_dataout_timeout
397  */
398 DEF_NACL_ATTRIB(dataout_timeout);
399 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
400 /*
401  * Define iscsi_node_attrib_s_dataout_timeout_retries
402  */
403 DEF_NACL_ATTRIB(dataout_timeout_retries);
404 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
405 /*
406  * Define iscsi_node_attrib_s_default_erl
407  */
408 DEF_NACL_ATTRIB(default_erl);
409 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
410 /*
411  * Define iscsi_node_attrib_s_nopin_timeout
412  */
413 DEF_NACL_ATTRIB(nopin_timeout);
414 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
415 /*
416  * Define iscsi_node_attrib_s_nopin_response_timeout
417  */
418 DEF_NACL_ATTRIB(nopin_response_timeout);
419 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
420 /*
421  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
422  */
423 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
424 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
425 /*
426  * Define iscsi_node_attrib_s_random_datain_seq_offsets
427  */
428 DEF_NACL_ATTRIB(random_datain_seq_offsets);
429 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
430 /*
431  * Define iscsi_node_attrib_s_random_r2t_offsets
432  */
433 DEF_NACL_ATTRIB(random_r2t_offsets);
434 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
435 
436 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
437 	&iscsi_nacl_attrib_dataout_timeout.attr,
438 	&iscsi_nacl_attrib_dataout_timeout_retries.attr,
439 	&iscsi_nacl_attrib_default_erl.attr,
440 	&iscsi_nacl_attrib_nopin_timeout.attr,
441 	&iscsi_nacl_attrib_nopin_response_timeout.attr,
442 	&iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
443 	&iscsi_nacl_attrib_random_datain_seq_offsets.attr,
444 	&iscsi_nacl_attrib_random_r2t_offsets.attr,
445 	NULL,
446 };
447 
448 /* End items for lio_target_nacl_attrib_cit */
449 
450 /* Start items for lio_target_nacl_auth_cit */
451 
452 #define __DEF_NACL_AUTH_STR(prefix, name, flags)			\
453 static ssize_t __iscsi_##prefix##_show_##name(				\
454 	struct iscsi_node_acl *nacl,					\
455 	char *page)							\
456 {									\
457 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
458 									\
459 	if (!capable(CAP_SYS_ADMIN))					\
460 		return -EPERM;						\
461 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);		\
462 }									\
463 									\
464 static ssize_t __iscsi_##prefix##_store_##name(				\
465 	struct iscsi_node_acl *nacl,					\
466 	const char *page,						\
467 	size_t count)							\
468 {									\
469 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
470 									\
471 	if (!capable(CAP_SYS_ADMIN))					\
472 		return -EPERM;						\
473 	if (count >= sizeof(auth->name))				\
474 		return -EINVAL;						\
475 	snprintf(auth->name, sizeof(auth->name), "%s", page);		\
476 	if (!strncmp("NULL", auth->name, 4))				\
477 		auth->naf_flags &= ~flags;				\
478 	else								\
479 		auth->naf_flags |= flags;				\
480 									\
481 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&			\
482 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))			\
483 		auth->authenticate_target = 1;				\
484 	else								\
485 		auth->authenticate_target = 0;				\
486 									\
487 	return count;							\
488 }
489 
490 #define __DEF_NACL_AUTH_INT(prefix, name)				\
491 static ssize_t __iscsi_##prefix##_show_##name(				\
492 	struct iscsi_node_acl *nacl,					\
493 	char *page)							\
494 {									\
495 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
496 									\
497 	if (!capable(CAP_SYS_ADMIN))					\
498 		return -EPERM;						\
499 									\
500 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);		\
501 }
502 
503 #define DEF_NACL_AUTH_STR(name, flags)					\
504 	__DEF_NACL_AUTH_STR(nacl_auth, name, flags)			\
505 static ssize_t iscsi_nacl_auth_show_##name(				\
506 	struct se_node_acl *nacl,					\
507 	char *page)							\
508 {									\
509 	return __iscsi_nacl_auth_show_##name(container_of(nacl,		\
510 			struct iscsi_node_acl, se_node_acl), page);		\
511 }									\
512 static ssize_t iscsi_nacl_auth_store_##name(				\
513 	struct se_node_acl *nacl,					\
514 	const char *page,						\
515 	size_t count)							\
516 {									\
517 	return __iscsi_nacl_auth_store_##name(container_of(nacl,	\
518 			struct iscsi_node_acl, se_node_acl), page, count);	\
519 }
520 
521 #define DEF_NACL_AUTH_INT(name)						\
522 	__DEF_NACL_AUTH_INT(nacl_auth, name)				\
523 static ssize_t iscsi_nacl_auth_show_##name(				\
524 	struct se_node_acl *nacl,					\
525 	char *page)							\
526 {									\
527 	return __iscsi_nacl_auth_show_##name(container_of(nacl,		\
528 			struct iscsi_node_acl, se_node_acl), page);		\
529 }
530 
531 #define AUTH_ATTR(_name, _mode)	TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
532 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
533 
534 /*
535  * One-way authentication userid
536  */
537 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
538 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
539 /*
540  * One-way authentication password
541  */
542 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
543 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
544 /*
545  * Enforce mutual authentication
546  */
547 DEF_NACL_AUTH_INT(authenticate_target);
548 AUTH_ATTR_RO(authenticate_target);
549 /*
550  * Mutual authentication userid
551  */
552 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
553 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
554 /*
555  * Mutual authentication password
556  */
557 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
558 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
559 
560 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
561 	&iscsi_nacl_auth_userid.attr,
562 	&iscsi_nacl_auth_password.attr,
563 	&iscsi_nacl_auth_authenticate_target.attr,
564 	&iscsi_nacl_auth_userid_mutual.attr,
565 	&iscsi_nacl_auth_password_mutual.attr,
566 	NULL,
567 };
568 
569 /* End items for lio_target_nacl_auth_cit */
570 
571 /* Start items for lio_target_nacl_param_cit */
572 
573 #define DEF_NACL_PARAM(name)						\
574 static ssize_t iscsi_nacl_param_show_##name(				\
575 	struct se_node_acl *se_nacl,					\
576 	char *page)							\
577 {									\
578 	struct iscsi_session *sess;					\
579 	struct se_session *se_sess;					\
580 	ssize_t rb;							\
581 									\
582 	spin_lock_bh(&se_nacl->nacl_sess_lock);				\
583 	se_sess = se_nacl->nacl_sess;					\
584 	if (!se_sess) {							\
585 		rb = snprintf(page, PAGE_SIZE,				\
586 			"No Active iSCSI Session\n");			\
587 	} else {							\
588 		sess = se_sess->fabric_sess_ptr;			\
589 		rb = snprintf(page, PAGE_SIZE, "%u\n",			\
590 			(u32)sess->sess_ops->name);			\
591 	}								\
592 	spin_unlock_bh(&se_nacl->nacl_sess_lock);			\
593 									\
594 	return rb;							\
595 }
596 
597 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
598 
599 DEF_NACL_PARAM(MaxConnections);
600 NACL_PARAM_ATTR(MaxConnections);
601 
602 DEF_NACL_PARAM(InitialR2T);
603 NACL_PARAM_ATTR(InitialR2T);
604 
605 DEF_NACL_PARAM(ImmediateData);
606 NACL_PARAM_ATTR(ImmediateData);
607 
608 DEF_NACL_PARAM(MaxBurstLength);
609 NACL_PARAM_ATTR(MaxBurstLength);
610 
611 DEF_NACL_PARAM(FirstBurstLength);
612 NACL_PARAM_ATTR(FirstBurstLength);
613 
614 DEF_NACL_PARAM(DefaultTime2Wait);
615 NACL_PARAM_ATTR(DefaultTime2Wait);
616 
617 DEF_NACL_PARAM(DefaultTime2Retain);
618 NACL_PARAM_ATTR(DefaultTime2Retain);
619 
620 DEF_NACL_PARAM(MaxOutstandingR2T);
621 NACL_PARAM_ATTR(MaxOutstandingR2T);
622 
623 DEF_NACL_PARAM(DataPDUInOrder);
624 NACL_PARAM_ATTR(DataPDUInOrder);
625 
626 DEF_NACL_PARAM(DataSequenceInOrder);
627 NACL_PARAM_ATTR(DataSequenceInOrder);
628 
629 DEF_NACL_PARAM(ErrorRecoveryLevel);
630 NACL_PARAM_ATTR(ErrorRecoveryLevel);
631 
632 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
633 	&iscsi_nacl_param_MaxConnections.attr,
634 	&iscsi_nacl_param_InitialR2T.attr,
635 	&iscsi_nacl_param_ImmediateData.attr,
636 	&iscsi_nacl_param_MaxBurstLength.attr,
637 	&iscsi_nacl_param_FirstBurstLength.attr,
638 	&iscsi_nacl_param_DefaultTime2Wait.attr,
639 	&iscsi_nacl_param_DefaultTime2Retain.attr,
640 	&iscsi_nacl_param_MaxOutstandingR2T.attr,
641 	&iscsi_nacl_param_DataPDUInOrder.attr,
642 	&iscsi_nacl_param_DataSequenceInOrder.attr,
643 	&iscsi_nacl_param_ErrorRecoveryLevel.attr,
644 	NULL,
645 };
646 
647 /* End items for lio_target_nacl_param_cit */
648 
649 /* Start items for lio_target_acl_cit */
650 
651 static ssize_t lio_target_nacl_show_info(
652 	struct se_node_acl *se_nacl,
653 	char *page)
654 {
655 	struct iscsi_session *sess;
656 	struct iscsi_conn *conn;
657 	struct se_session *se_sess;
658 	ssize_t rb = 0;
659 	u32 max_cmd_sn;
660 
661 	spin_lock_bh(&se_nacl->nacl_sess_lock);
662 	se_sess = se_nacl->nacl_sess;
663 	if (!se_sess) {
664 		rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
665 			" Endpoint: %s\n", se_nacl->initiatorname);
666 	} else {
667 		sess = se_sess->fabric_sess_ptr;
668 
669 		rb += sprintf(page+rb, "InitiatorName: %s\n",
670 			sess->sess_ops->InitiatorName);
671 		rb += sprintf(page+rb, "InitiatorAlias: %s\n",
672 			sess->sess_ops->InitiatorAlias);
673 
674 		rb += sprintf(page+rb,
675 			      "LIO Session ID: %u   ISID: 0x%6ph  TSIH: %hu  ",
676 			      sess->sid, sess->isid, sess->tsih);
677 		rb += sprintf(page+rb, "SessionType: %s\n",
678 				(sess->sess_ops->SessionType) ?
679 				"Discovery" : "Normal");
680 		rb += sprintf(page+rb, "Session State: ");
681 		switch (sess->session_state) {
682 		case TARG_SESS_STATE_FREE:
683 			rb += sprintf(page+rb, "TARG_SESS_FREE\n");
684 			break;
685 		case TARG_SESS_STATE_ACTIVE:
686 			rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
687 			break;
688 		case TARG_SESS_STATE_LOGGED_IN:
689 			rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
690 			break;
691 		case TARG_SESS_STATE_FAILED:
692 			rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
693 			break;
694 		case TARG_SESS_STATE_IN_CONTINUE:
695 			rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
696 			break;
697 		default:
698 			rb += sprintf(page+rb, "ERROR: Unknown Session"
699 					" State!\n");
700 			break;
701 		}
702 
703 		rb += sprintf(page+rb, "---------------------[iSCSI Session"
704 				" Values]-----------------------\n");
705 		rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
706 				"  :  MaxCmdSN  :     ITT    :     TTT\n");
707 		max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
708 		rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
709 				"   0x%08x   0x%08x\n",
710 			sess->cmdsn_window,
711 			(max_cmd_sn - sess->exp_cmd_sn) + 1,
712 			sess->exp_cmd_sn, max_cmd_sn,
713 			sess->init_task_tag, sess->targ_xfer_tag);
714 		rb += sprintf(page+rb, "----------------------[iSCSI"
715 				" Connections]-------------------------\n");
716 
717 		spin_lock(&sess->conn_lock);
718 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
719 			rb += sprintf(page+rb, "CID: %hu  Connection"
720 					" State: ", conn->cid);
721 			switch (conn->conn_state) {
722 			case TARG_CONN_STATE_FREE:
723 				rb += sprintf(page+rb,
724 					"TARG_CONN_STATE_FREE\n");
725 				break;
726 			case TARG_CONN_STATE_XPT_UP:
727 				rb += sprintf(page+rb,
728 					"TARG_CONN_STATE_XPT_UP\n");
729 				break;
730 			case TARG_CONN_STATE_IN_LOGIN:
731 				rb += sprintf(page+rb,
732 					"TARG_CONN_STATE_IN_LOGIN\n");
733 				break;
734 			case TARG_CONN_STATE_LOGGED_IN:
735 				rb += sprintf(page+rb,
736 					"TARG_CONN_STATE_LOGGED_IN\n");
737 				break;
738 			case TARG_CONN_STATE_IN_LOGOUT:
739 				rb += sprintf(page+rb,
740 					"TARG_CONN_STATE_IN_LOGOUT\n");
741 				break;
742 			case TARG_CONN_STATE_LOGOUT_REQUESTED:
743 				rb += sprintf(page+rb,
744 					"TARG_CONN_STATE_LOGOUT_REQUESTED\n");
745 				break;
746 			case TARG_CONN_STATE_CLEANUP_WAIT:
747 				rb += sprintf(page+rb,
748 					"TARG_CONN_STATE_CLEANUP_WAIT\n");
749 				break;
750 			default:
751 				rb += sprintf(page+rb,
752 					"ERROR: Unknown Connection State!\n");
753 				break;
754 			}
755 
756 			rb += sprintf(page+rb, "   Address %pISc %s", &conn->login_sockaddr,
757 				(conn->network_transport == ISCSI_TCP) ?
758 				"TCP" : "SCTP");
759 			rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
760 				conn->stat_sn);
761 		}
762 		spin_unlock(&sess->conn_lock);
763 	}
764 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
765 
766 	return rb;
767 }
768 
769 TF_NACL_BASE_ATTR_RO(lio_target, info);
770 
771 static ssize_t lio_target_nacl_show_cmdsn_depth(
772 	struct se_node_acl *se_nacl,
773 	char *page)
774 {
775 	return sprintf(page, "%u\n", se_nacl->queue_depth);
776 }
777 
778 static ssize_t lio_target_nacl_store_cmdsn_depth(
779 	struct se_node_acl *se_nacl,
780 	const char *page,
781 	size_t count)
782 {
783 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
784 	struct iscsi_portal_group *tpg = container_of(se_tpg,
785 			struct iscsi_portal_group, tpg_se_tpg);
786 	struct config_item *acl_ci, *tpg_ci, *wwn_ci;
787 	u32 cmdsn_depth = 0;
788 	int ret;
789 
790 	ret = kstrtou32(page, 0, &cmdsn_depth);
791 	if (ret)
792 		return ret;
793 	if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
794 		pr_err("Passed cmdsn_depth: %u exceeds"
795 			" TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
796 			TA_DEFAULT_CMDSN_DEPTH_MAX);
797 		return -EINVAL;
798 	}
799 	acl_ci = &se_nacl->acl_group.cg_item;
800 	if (!acl_ci) {
801 		pr_err("Unable to locatel acl_ci\n");
802 		return -EINVAL;
803 	}
804 	tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
805 	if (!tpg_ci) {
806 		pr_err("Unable to locate tpg_ci\n");
807 		return -EINVAL;
808 	}
809 	wwn_ci = &tpg_ci->ci_group->cg_item;
810 	if (!wwn_ci) {
811 		pr_err("Unable to locate config_item wwn_ci\n");
812 		return -EINVAL;
813 	}
814 
815 	if (iscsit_get_tpg(tpg) < 0)
816 		return -EINVAL;
817 	/*
818 	 * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
819 	 */
820 	ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
821 				config_item_name(acl_ci), cmdsn_depth, 1);
822 
823 	pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
824 		"InitiatorName: %s\n", config_item_name(wwn_ci),
825 		config_item_name(tpg_ci), cmdsn_depth,
826 		config_item_name(acl_ci));
827 
828 	iscsit_put_tpg(tpg);
829 	return (!ret) ? count : (ssize_t)ret;
830 }
831 
832 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
833 
834 static ssize_t lio_target_nacl_show_tag(
835 	struct se_node_acl *se_nacl,
836 	char *page)
837 {
838 	return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
839 }
840 
841 static ssize_t lio_target_nacl_store_tag(
842 	struct se_node_acl *se_nacl,
843 	const char *page,
844 	size_t count)
845 {
846 	int ret;
847 
848 	ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
849 
850 	if (ret < 0)
851 		return ret;
852 	return count;
853 }
854 
855 TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
856 
857 static struct configfs_attribute *lio_target_initiator_attrs[] = {
858 	&lio_target_nacl_info.attr,
859 	&lio_target_nacl_cmdsn_depth.attr,
860 	&lio_target_nacl_tag.attr,
861 	NULL,
862 };
863 
864 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
865 		const char *name)
866 {
867 	struct iscsi_node_acl *acl =
868 		container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
869 	struct config_group *stats_cg = &se_nacl->acl_fabric_stat_group;
870 
871 	stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
872 				GFP_KERNEL);
873 	if (!stats_cg->default_groups) {
874 		pr_err("Unable to allocate memory for"
875 				" stats_cg->default_groups\n");
876 		return -ENOMEM;
877 	}
878 
879 	stats_cg->default_groups[0] = &acl->node_stat_grps.iscsi_sess_stats_group;
880 	stats_cg->default_groups[1] = NULL;
881 	config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
882 			"iscsi_sess_stats", &iscsi_stat_sess_cit);
883 
884 	return 0;
885 }
886 
887 static void lio_target_cleanup_nodeacl( struct se_node_acl *se_nacl)
888 {
889 	struct iscsi_node_acl *acl = container_of(se_nacl,
890 			struct iscsi_node_acl, se_node_acl);
891 	struct config_item *df_item;
892 	struct config_group *stats_cg;
893 	int i;
894 
895 	stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
896 	for (i = 0; stats_cg->default_groups[i]; i++) {
897 		df_item = &stats_cg->default_groups[i]->cg_item;
898 		stats_cg->default_groups[i] = NULL;
899 		config_item_put(df_item);
900 	}
901 	kfree(stats_cg->default_groups);
902 }
903 
904 /* End items for lio_target_acl_cit */
905 
906 /* Start items for lio_target_tpg_attrib_cit */
907 
908 #define DEF_TPG_ATTRIB(name)						\
909 									\
910 static ssize_t iscsi_tpg_attrib_show_##name(				\
911 	struct se_portal_group *se_tpg,				\
912 	char *page)							\
913 {									\
914 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
915 			struct iscsi_portal_group, tpg_se_tpg);	\
916 	ssize_t rb;							\
917 									\
918 	if (iscsit_get_tpg(tpg) < 0)					\
919 		return -EINVAL;						\
920 									\
921 	rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);		\
922 	iscsit_put_tpg(tpg);						\
923 	return rb;							\
924 }									\
925 									\
926 static ssize_t iscsi_tpg_attrib_store_##name(				\
927 	struct se_portal_group *se_tpg,				\
928 	const char *page,						\
929 	size_t count)							\
930 {									\
931 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
932 			struct iscsi_portal_group, tpg_se_tpg);	\
933 	u32 val;							\
934 	int ret;							\
935 									\
936 	if (iscsit_get_tpg(tpg) < 0)					\
937 		return -EINVAL;						\
938 									\
939 	ret = kstrtou32(page, 0, &val);					\
940 	if (ret)							\
941 		goto out;						\
942 	ret = iscsit_ta_##name(tpg, val);				\
943 	if (ret < 0)							\
944 		goto out;						\
945 									\
946 	iscsit_put_tpg(tpg);						\
947 	return count;							\
948 out:									\
949 	iscsit_put_tpg(tpg);						\
950 	return ret;							\
951 }
952 
953 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
954 
955 /*
956  * Define iscsi_tpg_attrib_s_authentication
957  */
958 DEF_TPG_ATTRIB(authentication);
959 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
960 /*
961  * Define iscsi_tpg_attrib_s_login_timeout
962  */
963 DEF_TPG_ATTRIB(login_timeout);
964 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
965 /*
966  * Define iscsi_tpg_attrib_s_netif_timeout
967  */
968 DEF_TPG_ATTRIB(netif_timeout);
969 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
970 /*
971  * Define iscsi_tpg_attrib_s_generate_node_acls
972  */
973 DEF_TPG_ATTRIB(generate_node_acls);
974 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
975 /*
976  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
977  */
978 DEF_TPG_ATTRIB(default_cmdsn_depth);
979 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
980 /*
981  Define iscsi_tpg_attrib_s_cache_dynamic_acls
982  */
983 DEF_TPG_ATTRIB(cache_dynamic_acls);
984 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
985 /*
986  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
987  */
988 DEF_TPG_ATTRIB(demo_mode_write_protect);
989 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
990 /*
991  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
992  */
993 DEF_TPG_ATTRIB(prod_mode_write_protect);
994 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
995 /*
996  * Define iscsi_tpg_attrib_s_demo_mode_discovery,
997  */
998 DEF_TPG_ATTRIB(demo_mode_discovery);
999 TPG_ATTR(demo_mode_discovery, S_IRUGO | S_IWUSR);
1000 /*
1001  * Define iscsi_tpg_attrib_s_default_erl
1002  */
1003 DEF_TPG_ATTRIB(default_erl);
1004 TPG_ATTR(default_erl, S_IRUGO | S_IWUSR);
1005 /*
1006  * Define iscsi_tpg_attrib_s_t10_pi
1007  */
1008 DEF_TPG_ATTRIB(t10_pi);
1009 TPG_ATTR(t10_pi, S_IRUGO | S_IWUSR);
1010 /*
1011  * Define iscsi_tpg_attrib_s_fabric_prot_type
1012  */
1013 DEF_TPG_ATTRIB(fabric_prot_type);
1014 TPG_ATTR(fabric_prot_type, S_IRUGO | S_IWUSR);
1015 /*
1016  * Define iscsi_tpg_attrib_s_tpg_enabled_sendtargets
1017  */
1018 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
1019 TPG_ATTR(tpg_enabled_sendtargets, S_IRUGO | S_IWUSR);
1020 
1021 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
1022 	&iscsi_tpg_attrib_authentication.attr,
1023 	&iscsi_tpg_attrib_login_timeout.attr,
1024 	&iscsi_tpg_attrib_netif_timeout.attr,
1025 	&iscsi_tpg_attrib_generate_node_acls.attr,
1026 	&iscsi_tpg_attrib_default_cmdsn_depth.attr,
1027 	&iscsi_tpg_attrib_cache_dynamic_acls.attr,
1028 	&iscsi_tpg_attrib_demo_mode_write_protect.attr,
1029 	&iscsi_tpg_attrib_prod_mode_write_protect.attr,
1030 	&iscsi_tpg_attrib_demo_mode_discovery.attr,
1031 	&iscsi_tpg_attrib_default_erl.attr,
1032 	&iscsi_tpg_attrib_t10_pi.attr,
1033 	&iscsi_tpg_attrib_fabric_prot_type.attr,
1034 	&iscsi_tpg_attrib_tpg_enabled_sendtargets.attr,
1035 	NULL,
1036 };
1037 
1038 /* End items for lio_target_tpg_attrib_cit */
1039 
1040 /* Start items for lio_target_tpg_auth_cit */
1041 
1042 #define __DEF_TPG_AUTH_STR(prefix, name, flags)					\
1043 static ssize_t __iscsi_##prefix##_show_##name(					\
1044 	struct se_portal_group *se_tpg,						\
1045 	char *page)								\
1046 {										\
1047 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
1048 				struct iscsi_portal_group, tpg_se_tpg);		\
1049 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
1050 										\
1051 	if (!capable(CAP_SYS_ADMIN))						\
1052 		return -EPERM;							\
1053 										\
1054 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);			\
1055 }										\
1056 										\
1057 static ssize_t __iscsi_##prefix##_store_##name(					\
1058 	struct se_portal_group *se_tpg,						\
1059 	const char *page,							\
1060 	size_t count)								\
1061 {										\
1062 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
1063 				struct iscsi_portal_group, tpg_se_tpg);		\
1064 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
1065 										\
1066 	if (!capable(CAP_SYS_ADMIN))						\
1067 		return -EPERM;							\
1068 										\
1069 	snprintf(auth->name, sizeof(auth->name), "%s", page);			\
1070 	if (!(strncmp("NULL", auth->name, 4)))					\
1071 		auth->naf_flags &= ~flags;					\
1072 	else									\
1073 		auth->naf_flags |= flags;					\
1074 										\
1075 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&				\
1076 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))				\
1077 		auth->authenticate_target = 1;					\
1078 	else									\
1079 		auth->authenticate_target = 0;					\
1080 										\
1081 	return count;								\
1082 }
1083 
1084 #define __DEF_TPG_AUTH_INT(prefix, name)					\
1085 static ssize_t __iscsi_##prefix##_show_##name(					\
1086 	struct se_portal_group *se_tpg,						\
1087 	char *page)								\
1088 {										\
1089 	struct iscsi_portal_group *tpg = container_of(se_tpg,			\
1090 				struct iscsi_portal_group, tpg_se_tpg);		\
1091 	struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;			\
1092 										\
1093 	if (!capable(CAP_SYS_ADMIN))						\
1094 		return -EPERM;							\
1095 										\
1096 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);			\
1097 }
1098 
1099 #define DEF_TPG_AUTH_STR(name, flags)						\
1100 	__DEF_TPG_AUTH_STR(tpg_auth, name, flags)				\
1101 static ssize_t iscsi_tpg_auth_show_##name(					\
1102 	struct se_portal_group *se_tpg,						\
1103 	char *page)								\
1104 {										\
1105 	return __iscsi_tpg_auth_show_##name(se_tpg, page);			\
1106 }										\
1107 										\
1108 static ssize_t iscsi_tpg_auth_store_##name(					\
1109 	struct se_portal_group *se_tpg,						\
1110 	const char *page,							\
1111 	size_t count)								\
1112 {										\
1113 	return __iscsi_tpg_auth_store_##name(se_tpg, page, count);		\
1114 }
1115 
1116 #define DEF_TPG_AUTH_INT(name)							\
1117 	__DEF_TPG_AUTH_INT(tpg_auth, name)					\
1118 static ssize_t iscsi_tpg_auth_show_##name(					\
1119 	struct se_portal_group *se_tpg,						\
1120 	char *page)								\
1121 {										\
1122 	return __iscsi_tpg_auth_show_##name(se_tpg, page);			\
1123 }
1124 
1125 #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
1126 #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
1127 
1128 /*
1129  *  * One-way authentication userid
1130  *   */
1131 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
1132 TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1133 /*
1134  *  * One-way authentication password
1135  *   */
1136 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
1137 TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1138 /*
1139  *  * Enforce mutual authentication
1140  *   */
1141 DEF_TPG_AUTH_INT(authenticate_target);
1142 TPG_AUTH_ATTR_RO(authenticate_target);
1143 /*
1144  *  * Mutual authentication userid
1145  *   */
1146 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1147 TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1148 /*
1149  *  * Mutual authentication password
1150  *   */
1151 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1152 TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1153 
1154 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
1155 	&iscsi_tpg_auth_userid.attr,
1156 	&iscsi_tpg_auth_password.attr,
1157 	&iscsi_tpg_auth_authenticate_target.attr,
1158 	&iscsi_tpg_auth_userid_mutual.attr,
1159 	&iscsi_tpg_auth_password_mutual.attr,
1160 	NULL,
1161 };
1162 
1163 /* End items for lio_target_tpg_auth_cit */
1164 
1165 /* Start items for lio_target_tpg_param_cit */
1166 
1167 #define DEF_TPG_PARAM(name)						\
1168 static ssize_t iscsi_tpg_param_show_##name(				\
1169 	struct se_portal_group *se_tpg,					\
1170 	char *page)							\
1171 {									\
1172 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
1173 			struct iscsi_portal_group, tpg_se_tpg);		\
1174 	struct iscsi_param *param;					\
1175 	ssize_t rb;							\
1176 									\
1177 	if (iscsit_get_tpg(tpg) < 0)					\
1178 		return -EINVAL;						\
1179 									\
1180 	param = iscsi_find_param_from_key(__stringify(name),		\
1181 				tpg->param_list);			\
1182 	if (!param) {							\
1183 		iscsit_put_tpg(tpg);					\
1184 		return -EINVAL;						\
1185 	}								\
1186 	rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);		\
1187 									\
1188 	iscsit_put_tpg(tpg);						\
1189 	return rb;							\
1190 }									\
1191 static ssize_t iscsi_tpg_param_store_##name(				\
1192 	struct se_portal_group *se_tpg,				\
1193 	const char *page,						\
1194 	size_t count)							\
1195 {									\
1196 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
1197 			struct iscsi_portal_group, tpg_se_tpg);		\
1198 	char *buf;							\
1199 	int ret, len;							\
1200 									\
1201 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);				\
1202 	if (!buf)							\
1203 		return -ENOMEM;						\
1204 	len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);	\
1205 	if (isspace(buf[len-1]))					\
1206 		buf[len-1] = '\0'; /* Kill newline */			\
1207 									\
1208 	if (iscsit_get_tpg(tpg) < 0) {					\
1209 		kfree(buf);						\
1210 		return -EINVAL;						\
1211 	}								\
1212 									\
1213 	ret = iscsi_change_param_value(buf, tpg->param_list, 1);	\
1214 	if (ret < 0)							\
1215 		goto out;						\
1216 									\
1217 	kfree(buf);							\
1218 	iscsit_put_tpg(tpg);						\
1219 	return count;							\
1220 out:									\
1221 	kfree(buf);							\
1222 	iscsit_put_tpg(tpg);						\
1223 	return -EINVAL;						\
1224 }
1225 
1226 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1227 
1228 DEF_TPG_PARAM(AuthMethod);
1229 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1230 
1231 DEF_TPG_PARAM(HeaderDigest);
1232 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1233 
1234 DEF_TPG_PARAM(DataDigest);
1235 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1236 
1237 DEF_TPG_PARAM(MaxConnections);
1238 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1239 
1240 DEF_TPG_PARAM(TargetAlias);
1241 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1242 
1243 DEF_TPG_PARAM(InitialR2T);
1244 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1245 
1246 DEF_TPG_PARAM(ImmediateData);
1247 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1248 
1249 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1250 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1251 
1252 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1253 TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
1254 
1255 DEF_TPG_PARAM(MaxBurstLength);
1256 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1257 
1258 DEF_TPG_PARAM(FirstBurstLength);
1259 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1260 
1261 DEF_TPG_PARAM(DefaultTime2Wait);
1262 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1263 
1264 DEF_TPG_PARAM(DefaultTime2Retain);
1265 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1266 
1267 DEF_TPG_PARAM(MaxOutstandingR2T);
1268 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1269 
1270 DEF_TPG_PARAM(DataPDUInOrder);
1271 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1272 
1273 DEF_TPG_PARAM(DataSequenceInOrder);
1274 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1275 
1276 DEF_TPG_PARAM(ErrorRecoveryLevel);
1277 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1278 
1279 DEF_TPG_PARAM(IFMarker);
1280 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1281 
1282 DEF_TPG_PARAM(OFMarker);
1283 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1284 
1285 DEF_TPG_PARAM(IFMarkInt);
1286 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1287 
1288 DEF_TPG_PARAM(OFMarkInt);
1289 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1290 
1291 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1292 	&iscsi_tpg_param_AuthMethod.attr,
1293 	&iscsi_tpg_param_HeaderDigest.attr,
1294 	&iscsi_tpg_param_DataDigest.attr,
1295 	&iscsi_tpg_param_MaxConnections.attr,
1296 	&iscsi_tpg_param_TargetAlias.attr,
1297 	&iscsi_tpg_param_InitialR2T.attr,
1298 	&iscsi_tpg_param_ImmediateData.attr,
1299 	&iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1300 	&iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
1301 	&iscsi_tpg_param_MaxBurstLength.attr,
1302 	&iscsi_tpg_param_FirstBurstLength.attr,
1303 	&iscsi_tpg_param_DefaultTime2Wait.attr,
1304 	&iscsi_tpg_param_DefaultTime2Retain.attr,
1305 	&iscsi_tpg_param_MaxOutstandingR2T.attr,
1306 	&iscsi_tpg_param_DataPDUInOrder.attr,
1307 	&iscsi_tpg_param_DataSequenceInOrder.attr,
1308 	&iscsi_tpg_param_ErrorRecoveryLevel.attr,
1309 	&iscsi_tpg_param_IFMarker.attr,
1310 	&iscsi_tpg_param_OFMarker.attr,
1311 	&iscsi_tpg_param_IFMarkInt.attr,
1312 	&iscsi_tpg_param_OFMarkInt.attr,
1313 	NULL,
1314 };
1315 
1316 /* End items for lio_target_tpg_param_cit */
1317 
1318 /* Start items for lio_target_tpg_cit */
1319 
1320 static ssize_t lio_target_tpg_show_enable(
1321 	struct se_portal_group *se_tpg,
1322 	char *page)
1323 {
1324 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1325 			struct iscsi_portal_group, tpg_se_tpg);
1326 	ssize_t len;
1327 
1328 	spin_lock(&tpg->tpg_state_lock);
1329 	len = sprintf(page, "%d\n",
1330 			(tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1331 	spin_unlock(&tpg->tpg_state_lock);
1332 
1333 	return len;
1334 }
1335 
1336 static ssize_t lio_target_tpg_store_enable(
1337 	struct se_portal_group *se_tpg,
1338 	const char *page,
1339 	size_t count)
1340 {
1341 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1342 			struct iscsi_portal_group, tpg_se_tpg);
1343 	u32 op;
1344 	int ret;
1345 
1346 	ret = kstrtou32(page, 0, &op);
1347 	if (ret)
1348 		return ret;
1349 	if ((op != 1) && (op != 0)) {
1350 		pr_err("Illegal value for tpg_enable: %u\n", op);
1351 		return -EINVAL;
1352 	}
1353 
1354 	ret = iscsit_get_tpg(tpg);
1355 	if (ret < 0)
1356 		return -EINVAL;
1357 
1358 	if (op) {
1359 		ret = iscsit_tpg_enable_portal_group(tpg);
1360 		if (ret < 0)
1361 			goto out;
1362 	} else {
1363 		/*
1364 		 * iscsit_tpg_disable_portal_group() assumes force=1
1365 		 */
1366 		ret = iscsit_tpg_disable_portal_group(tpg, 1);
1367 		if (ret < 0)
1368 			goto out;
1369 	}
1370 
1371 	iscsit_put_tpg(tpg);
1372 	return count;
1373 out:
1374 	iscsit_put_tpg(tpg);
1375 	return -EINVAL;
1376 }
1377 
1378 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1379 
1380 static ssize_t lio_target_tpg_show_dynamic_sessions(
1381 	struct se_portal_group *se_tpg,
1382 	char *page)
1383 {
1384 	return target_show_dynamic_sessions(se_tpg, page);
1385 }
1386 
1387 TF_TPG_BASE_ATTR_RO(lio_target, dynamic_sessions);
1388 
1389 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1390 	&lio_target_tpg_enable.attr,
1391 	&lio_target_tpg_dynamic_sessions.attr,
1392 	NULL,
1393 };
1394 
1395 /* End items for lio_target_tpg_cit */
1396 
1397 /* Start items for lio_target_tiqn_cit */
1398 
1399 static struct se_portal_group *lio_target_tiqn_addtpg(
1400 	struct se_wwn *wwn,
1401 	struct config_group *group,
1402 	const char *name)
1403 {
1404 	struct iscsi_portal_group *tpg;
1405 	struct iscsi_tiqn *tiqn;
1406 	char *tpgt_str;
1407 	int ret;
1408 	u16 tpgt;
1409 
1410 	tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1411 	/*
1412 	 * Only tpgt_# directory groups can be created below
1413 	 * target/iscsi/iqn.superturodiskarry/
1414 	 */
1415 	tpgt_str = strstr(name, "tpgt_");
1416 	if (!tpgt_str) {
1417 		pr_err("Unable to locate \"tpgt_#\" directory"
1418 				" group\n");
1419 		return NULL;
1420 	}
1421 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
1422 	ret = kstrtou16(tpgt_str, 0, &tpgt);
1423 	if (ret)
1424 		return NULL;
1425 
1426 	tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1427 	if (!tpg)
1428 		return NULL;
1429 
1430 	ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1431 	if (ret < 0)
1432 		return NULL;
1433 
1434 	ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1435 	if (ret != 0)
1436 		goto out;
1437 
1438 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1439 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1440 			name);
1441 	return &tpg->tpg_se_tpg;
1442 out:
1443 	core_tpg_deregister(&tpg->tpg_se_tpg);
1444 	kfree(tpg);
1445 	return NULL;
1446 }
1447 
1448 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1449 {
1450 	struct iscsi_portal_group *tpg;
1451 	struct iscsi_tiqn *tiqn;
1452 
1453 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1454 	tiqn = tpg->tpg_tiqn;
1455 	/*
1456 	 * iscsit_tpg_del_portal_group() assumes force=1
1457 	 */
1458 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1459 	iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1460 }
1461 
1462 /* End items for lio_target_tiqn_cit */
1463 
1464 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1465 
1466 static ssize_t lio_target_wwn_show_attr_lio_version(
1467 	struct target_fabric_configfs *tf,
1468 	char *page)
1469 {
1470 	return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1471 }
1472 
1473 TF_WWN_ATTR_RO(lio_target, lio_version);
1474 
1475 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1476 	&lio_target_wwn_lio_version.attr,
1477 	NULL,
1478 };
1479 
1480 static struct se_wwn *lio_target_call_coreaddtiqn(
1481 	struct target_fabric_configfs *tf,
1482 	struct config_group *group,
1483 	const char *name)
1484 {
1485 	struct config_group *stats_cg;
1486 	struct iscsi_tiqn *tiqn;
1487 
1488 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1489 	if (IS_ERR(tiqn))
1490 		return ERR_CAST(tiqn);
1491 	/*
1492 	 * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1493 	 */
1494 	stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1495 
1496 	stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
1497 				GFP_KERNEL);
1498 	if (!stats_cg->default_groups) {
1499 		pr_err("Unable to allocate memory for"
1500 				" stats_cg->default_groups\n");
1501 		iscsit_del_tiqn(tiqn);
1502 		return ERR_PTR(-ENOMEM);
1503 	}
1504 
1505 	stats_cg->default_groups[0] = &tiqn->tiqn_stat_grps.iscsi_instance_group;
1506 	stats_cg->default_groups[1] = &tiqn->tiqn_stat_grps.iscsi_sess_err_group;
1507 	stats_cg->default_groups[2] = &tiqn->tiqn_stat_grps.iscsi_tgt_attr_group;
1508 	stats_cg->default_groups[3] = &tiqn->tiqn_stat_grps.iscsi_login_stats_group;
1509 	stats_cg->default_groups[4] = &tiqn->tiqn_stat_grps.iscsi_logout_stats_group;
1510 	stats_cg->default_groups[5] = NULL;
1511 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1512 			"iscsi_instance", &iscsi_stat_instance_cit);
1513 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1514 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
1515 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1516 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1517 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1518 			"iscsi_login_stats", &iscsi_stat_login_cit);
1519 	config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1520 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
1521 
1522 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1523 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1524 			" %s\n", name);
1525 	return &tiqn->tiqn_wwn;
1526 }
1527 
1528 static void lio_target_call_coredeltiqn(
1529 	struct se_wwn *wwn)
1530 {
1531 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1532 	struct config_item *df_item;
1533 	struct config_group *stats_cg;
1534 	int i;
1535 
1536 	stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1537 	for (i = 0; stats_cg->default_groups[i]; i++) {
1538 		df_item = &stats_cg->default_groups[i]->cg_item;
1539 		stats_cg->default_groups[i] = NULL;
1540 		config_item_put(df_item);
1541 	}
1542 	kfree(stats_cg->default_groups);
1543 
1544 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1545 			tiqn->tiqn);
1546 	iscsit_del_tiqn(tiqn);
1547 }
1548 
1549 /* End LIO-Target TIQN struct contig_lio_target_cit */
1550 
1551 /* Start lio_target_discovery_auth_cit */
1552 
1553 #define DEF_DISC_AUTH_STR(name, flags)					\
1554 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
1555 static ssize_t iscsi_disc_show_##name(					\
1556 	struct target_fabric_configfs *tf,				\
1557 	char *page)							\
1558 {									\
1559 	return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,	\
1560 		page);							\
1561 }									\
1562 static ssize_t iscsi_disc_store_##name(					\
1563 	struct target_fabric_configfs *tf,				\
1564 	const char *page,						\
1565 	size_t count)							\
1566 {									\
1567 	return __iscsi_disc_store_##name(&iscsit_global->discovery_acl,	\
1568 		page, count);						\
1569 }
1570 
1571 #define DEF_DISC_AUTH_INT(name)						\
1572 	__DEF_NACL_AUTH_INT(disc, name)					\
1573 static ssize_t iscsi_disc_show_##name(					\
1574 	struct target_fabric_configfs *tf,				\
1575 	char *page)							\
1576 {									\
1577 	return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,	\
1578 			page);						\
1579 }
1580 
1581 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1582 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1583 
1584 /*
1585  * One-way authentication userid
1586  */
1587 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1588 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1589 /*
1590  * One-way authentication password
1591  */
1592 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1593 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1594 /*
1595  * Enforce mutual authentication
1596  */
1597 DEF_DISC_AUTH_INT(authenticate_target);
1598 DISC_AUTH_ATTR_RO(authenticate_target);
1599 /*
1600  * Mutual authentication userid
1601  */
1602 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1603 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1604 /*
1605  * Mutual authentication password
1606  */
1607 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1608 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1609 
1610 /*
1611  * enforce_discovery_auth
1612  */
1613 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1614 	struct target_fabric_configfs *tf,
1615 	char *page)
1616 {
1617 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1618 
1619 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1620 }
1621 
1622 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1623 	struct target_fabric_configfs *tf,
1624 	const char *page,
1625 	size_t count)
1626 {
1627 	struct iscsi_param *param;
1628 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1629 	u32 op;
1630 	int err;
1631 
1632 	err = kstrtou32(page, 0, &op);
1633 	if (err)
1634 		return -EINVAL;
1635 	if ((op != 1) && (op != 0)) {
1636 		pr_err("Illegal value for enforce_discovery_auth:"
1637 				" %u\n", op);
1638 		return -EINVAL;
1639 	}
1640 
1641 	if (!discovery_tpg) {
1642 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1643 		return -EINVAL;
1644 	}
1645 
1646 	param = iscsi_find_param_from_key(AUTHMETHOD,
1647 				discovery_tpg->param_list);
1648 	if (!param)
1649 		return -EINVAL;
1650 
1651 	if (op) {
1652 		/*
1653 		 * Reset the AuthMethod key to CHAP.
1654 		 */
1655 		if (iscsi_update_param_value(param, CHAP) < 0)
1656 			return -EINVAL;
1657 
1658 		discovery_tpg->tpg_attrib.authentication = 1;
1659 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1660 		pr_debug("LIO-CORE[0] Successfully enabled"
1661 			" authentication enforcement for iSCSI"
1662 			" Discovery TPG\n");
1663 	} else {
1664 		/*
1665 		 * Reset the AuthMethod key to CHAP,None
1666 		 */
1667 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1668 			return -EINVAL;
1669 
1670 		discovery_tpg->tpg_attrib.authentication = 0;
1671 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1672 		pr_debug("LIO-CORE[0] Successfully disabled"
1673 			" authentication enforcement for iSCSI"
1674 			" Discovery TPG\n");
1675 	}
1676 
1677 	return count;
1678 }
1679 
1680 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1681 
1682 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1683 	&iscsi_disc_userid.attr,
1684 	&iscsi_disc_password.attr,
1685 	&iscsi_disc_authenticate_target.attr,
1686 	&iscsi_disc_userid_mutual.attr,
1687 	&iscsi_disc_password_mutual.attr,
1688 	&iscsi_disc_enforce_discovery_auth.attr,
1689 	NULL,
1690 };
1691 
1692 /* End lio_target_discovery_auth_cit */
1693 
1694 /* Start functions for target_core_fabric_ops */
1695 
1696 static char *iscsi_get_fabric_name(void)
1697 {
1698 	return "iSCSI";
1699 }
1700 
1701 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1702 {
1703 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1704 
1705 	return cmd->i_state;
1706 }
1707 
1708 static u32 lio_sess_get_index(struct se_session *se_sess)
1709 {
1710 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1711 
1712 	return sess->session_index;
1713 }
1714 
1715 static u32 lio_sess_get_initiator_sid(
1716 	struct se_session *se_sess,
1717 	unsigned char *buf,
1718 	u32 size)
1719 {
1720 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1721 	/*
1722 	 * iSCSI Initiator Session Identifier from RFC-3720.
1723 	 */
1724 	return snprintf(buf, size, "%6phN", sess->isid);
1725 }
1726 
1727 static int lio_queue_data_in(struct se_cmd *se_cmd)
1728 {
1729 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1730 
1731 	cmd->i_state = ISTATE_SEND_DATAIN;
1732 	cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1733 
1734 	return 0;
1735 }
1736 
1737 static int lio_write_pending(struct se_cmd *se_cmd)
1738 {
1739 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1740 	struct iscsi_conn *conn = cmd->conn;
1741 
1742 	if (!cmd->immediate_data && !cmd->unsolicited_data)
1743 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1744 
1745 	return 0;
1746 }
1747 
1748 static int lio_write_pending_status(struct se_cmd *se_cmd)
1749 {
1750 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1751 	int ret;
1752 
1753 	spin_lock_bh(&cmd->istate_lock);
1754 	ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1755 	spin_unlock_bh(&cmd->istate_lock);
1756 
1757 	return ret;
1758 }
1759 
1760 static int lio_queue_status(struct se_cmd *se_cmd)
1761 {
1762 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1763 
1764 	cmd->i_state = ISTATE_SEND_STATUS;
1765 
1766 	if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1767 		iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1768 		return 0;
1769 	}
1770 	cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1771 
1772 	return 0;
1773 }
1774 
1775 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1776 {
1777 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1778 
1779 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1780 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1781 }
1782 
1783 static void lio_aborted_task(struct se_cmd *se_cmd)
1784 {
1785 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1786 
1787 	cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1788 }
1789 
1790 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1791 {
1792 	return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1793 }
1794 
1795 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1796 {
1797 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1798 }
1799 
1800 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1801 {
1802 	return iscsi_tpg(se_tpg)->tpgt;
1803 }
1804 
1805 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1806 {
1807 	return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1808 }
1809 
1810 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1811 {
1812 	return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1813 }
1814 
1815 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1816 {
1817 	return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1818 }
1819 
1820 static int lio_tpg_check_demo_mode_write_protect(
1821 	struct se_portal_group *se_tpg)
1822 {
1823 	return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1824 }
1825 
1826 static int lio_tpg_check_prod_mode_write_protect(
1827 	struct se_portal_group *se_tpg)
1828 {
1829 	return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1830 }
1831 
1832 static int lio_tpg_check_prot_fabric_only(
1833 	struct se_portal_group *se_tpg)
1834 {
1835 	/*
1836 	 * Only report fabric_prot_type if t10_pi has also been enabled
1837 	 * for incoming ib_isert sessions.
1838 	 */
1839 	if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1840 		return 0;
1841 	return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1842 }
1843 
1844 /*
1845  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1846  *
1847  * Also, this function calls iscsit_inc_session_usage_count() on the
1848  * struct iscsi_session in question.
1849  */
1850 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1851 {
1852 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1853 
1854 	spin_lock(&sess->conn_lock);
1855 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1856 	    atomic_read(&sess->session_logout) ||
1857 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1858 		spin_unlock(&sess->conn_lock);
1859 		return 0;
1860 	}
1861 	atomic_set(&sess->session_reinstatement, 1);
1862 	spin_unlock(&sess->conn_lock);
1863 
1864 	iscsit_stop_time2retain_timer(sess);
1865 	iscsit_stop_session(sess, 1, 1);
1866 
1867 	return 1;
1868 }
1869 
1870 /*
1871  * Calls iscsit_dec_session_usage_count() as inverse of
1872  * lio_tpg_shutdown_session()
1873  */
1874 static void lio_tpg_close_session(struct se_session *se_sess)
1875 {
1876 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1877 	/*
1878 	 * If the iSCSI Session for the iSCSI Initiator Node exists,
1879 	 * forcefully shutdown the iSCSI NEXUS.
1880 	 */
1881 	iscsit_close_session(sess);
1882 }
1883 
1884 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1885 {
1886 	return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1887 }
1888 
1889 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1890 {
1891 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1892 				se_node_acl);
1893 	struct se_portal_group *se_tpg = se_acl->se_tpg;
1894 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1895 				struct iscsi_portal_group, tpg_se_tpg);
1896 
1897 	acl->node_attrib.nacl = acl;
1898 	iscsit_set_default_node_attribues(acl, tpg);
1899 }
1900 
1901 static int lio_check_stop_free(struct se_cmd *se_cmd)
1902 {
1903 	return target_put_sess_cmd(se_cmd);
1904 }
1905 
1906 static void lio_release_cmd(struct se_cmd *se_cmd)
1907 {
1908 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1909 
1910 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1911 	iscsit_release_cmd(cmd);
1912 }
1913 
1914 const struct target_core_fabric_ops iscsi_ops = {
1915 	.module				= THIS_MODULE,
1916 	.name				= "iscsi",
1917 	.node_acl_size			= sizeof(struct iscsi_node_acl),
1918 	.get_fabric_name		= iscsi_get_fabric_name,
1919 	.tpg_get_wwn			= lio_tpg_get_endpoint_wwn,
1920 	.tpg_get_tag			= lio_tpg_get_tag,
1921 	.tpg_get_default_depth		= lio_tpg_get_default_depth,
1922 	.tpg_check_demo_mode		= lio_tpg_check_demo_mode,
1923 	.tpg_check_demo_mode_cache	= lio_tpg_check_demo_mode_cache,
1924 	.tpg_check_demo_mode_write_protect =
1925 			lio_tpg_check_demo_mode_write_protect,
1926 	.tpg_check_prod_mode_write_protect =
1927 			lio_tpg_check_prod_mode_write_protect,
1928 	.tpg_check_prot_fabric_only	= &lio_tpg_check_prot_fabric_only,
1929 	.tpg_get_inst_index		= lio_tpg_get_inst_index,
1930 	.check_stop_free		= lio_check_stop_free,
1931 	.release_cmd			= lio_release_cmd,
1932 	.shutdown_session		= lio_tpg_shutdown_session,
1933 	.close_session			= lio_tpg_close_session,
1934 	.sess_get_index			= lio_sess_get_index,
1935 	.sess_get_initiator_sid		= lio_sess_get_initiator_sid,
1936 	.write_pending			= lio_write_pending,
1937 	.write_pending_status		= lio_write_pending_status,
1938 	.set_default_node_attributes	= lio_set_default_node_attributes,
1939 	.get_cmd_state			= iscsi_get_cmd_state,
1940 	.queue_data_in			= lio_queue_data_in,
1941 	.queue_status			= lio_queue_status,
1942 	.queue_tm_rsp			= lio_queue_tm_rsp,
1943 	.aborted_task			= lio_aborted_task,
1944 	.fabric_make_wwn		= lio_target_call_coreaddtiqn,
1945 	.fabric_drop_wwn		= lio_target_call_coredeltiqn,
1946 	.fabric_make_tpg		= lio_target_tiqn_addtpg,
1947 	.fabric_drop_tpg		= lio_target_tiqn_deltpg,
1948 	.fabric_make_np			= lio_target_call_addnptotpg,
1949 	.fabric_drop_np			= lio_target_call_delnpfromtpg,
1950 	.fabric_init_nodeacl		= lio_target_init_nodeacl,
1951 	.fabric_cleanup_nodeacl		= lio_target_cleanup_nodeacl,
1952 
1953 	.tfc_discovery_attrs		= lio_target_discovery_auth_attrs,
1954 	.tfc_wwn_attrs			= lio_target_wwn_attrs,
1955 	.tfc_tpg_base_attrs		= lio_target_tpg_attrs,
1956 	.tfc_tpg_attrib_attrs		= lio_target_tpg_attrib_attrs,
1957 	.tfc_tpg_auth_attrs		= lio_target_tpg_auth_attrs,
1958 	.tfc_tpg_param_attrs		= lio_target_tpg_param_attrs,
1959 	.tfc_tpg_np_base_attrs		= lio_target_portal_attrs,
1960 	.tfc_tpg_nacl_base_attrs	= lio_target_initiator_attrs,
1961 	.tfc_tpg_nacl_attrib_attrs	= lio_target_nacl_attrib_attrs,
1962 	.tfc_tpg_nacl_auth_attrs	= lio_target_nacl_auth_attrs,
1963 	.tfc_tpg_nacl_param_attrs	= lio_target_nacl_param_attrs,
1964 };
1965