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