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