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