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/export.h>
24 #include <linux/inet.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
27 #include <target/target_core_fabric_configfs.h>
28 #include <target/target_core_configfs.h>
29 #include <target/configfs_macros.h>
30 #include <target/iscsi/iscsi_transport.h>
31 
32 #include "iscsi_target_core.h"
33 #include "iscsi_target_parameters.h"
34 #include "iscsi_target_device.h"
35 #include "iscsi_target_erl0.h"
36 #include "iscsi_target_nodeattrib.h"
37 #include "iscsi_target_tpg.h"
38 #include "iscsi_target_util.h"
39 #include "iscsi_target.h"
40 #include "iscsi_target_stat.h"
41 #include "iscsi_target_configfs.h"
42 
43 struct target_fabric_configfs *lio_target_fabric_configfs;
44 
45 struct lio_target_configfs_attribute {
46 	struct configfs_attribute attr;
47 	ssize_t (*show)(void *, char *);
48 	ssize_t (*store)(void *, const char *, size_t);
49 };
50 
51 /* Start items for lio_target_portal_cit */
52 
53 static ssize_t lio_target_np_show_sctp(
54 	struct se_tpg_np *se_tpg_np,
55 	char *page)
56 {
57 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
58 				struct iscsi_tpg_np, se_tpg_np);
59 	struct iscsi_tpg_np *tpg_np_sctp;
60 	ssize_t rb;
61 
62 	tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
63 	if (tpg_np_sctp)
64 		rb = sprintf(page, "1\n");
65 	else
66 		rb = sprintf(page, "0\n");
67 
68 	return rb;
69 }
70 
71 static ssize_t lio_target_np_store_sctp(
72 	struct se_tpg_np *se_tpg_np,
73 	const char *page,
74 	size_t count)
75 {
76 	struct iscsi_np *np;
77 	struct iscsi_portal_group *tpg;
78 	struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
79 				struct iscsi_tpg_np, se_tpg_np);
80 	struct iscsi_tpg_np *tpg_np_sctp = NULL;
81 	char *endptr;
82 	u32 op;
83 	int ret;
84 
85 	op = simple_strtoul(page, &endptr, 0);
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 = strict_strtoul(port_str, 0, &port);
267 		if (ret < 0) {
268 			pr_err("strict_strtoul() 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 = strict_strtoul(port_str, 0, &port);
292 		if (ret < 0) {
293 			pr_err("strict_strtoul() 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", ISCSI_NODE_ATTRIB(nacl)->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 	char *endptr;							\
386 	u32 val;							\
387 	int ret;							\
388 									\
389 	val = simple_strtoul(page, &endptr, 0);				\
390 	ret = iscsit_na_##name(nacl, val);				\
391 	if (ret < 0)							\
392 		return ret;						\
393 									\
394 	return count;							\
395 }
396 
397 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
398 /*
399  * Define iscsi_node_attrib_s_dataout_timeout
400  */
401 DEF_NACL_ATTRIB(dataout_timeout);
402 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
403 /*
404  * Define iscsi_node_attrib_s_dataout_timeout_retries
405  */
406 DEF_NACL_ATTRIB(dataout_timeout_retries);
407 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
408 /*
409  * Define iscsi_node_attrib_s_default_erl
410  */
411 DEF_NACL_ATTRIB(default_erl);
412 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
413 /*
414  * Define iscsi_node_attrib_s_nopin_timeout
415  */
416 DEF_NACL_ATTRIB(nopin_timeout);
417 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
418 /*
419  * Define iscsi_node_attrib_s_nopin_response_timeout
420  */
421 DEF_NACL_ATTRIB(nopin_response_timeout);
422 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
423 /*
424  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
425  */
426 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
427 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
428 /*
429  * Define iscsi_node_attrib_s_random_datain_seq_offsets
430  */
431 DEF_NACL_ATTRIB(random_datain_seq_offsets);
432 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
433 /*
434  * Define iscsi_node_attrib_s_random_r2t_offsets
435  */
436 DEF_NACL_ATTRIB(random_r2t_offsets);
437 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
438 
439 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
440 	&iscsi_nacl_attrib_dataout_timeout.attr,
441 	&iscsi_nacl_attrib_dataout_timeout_retries.attr,
442 	&iscsi_nacl_attrib_default_erl.attr,
443 	&iscsi_nacl_attrib_nopin_timeout.attr,
444 	&iscsi_nacl_attrib_nopin_response_timeout.attr,
445 	&iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
446 	&iscsi_nacl_attrib_random_datain_seq_offsets.attr,
447 	&iscsi_nacl_attrib_random_r2t_offsets.attr,
448 	NULL,
449 };
450 
451 /* End items for lio_target_nacl_attrib_cit */
452 
453 /* Start items for lio_target_nacl_auth_cit */
454 
455 #define __DEF_NACL_AUTH_STR(prefix, name, flags)			\
456 static ssize_t __iscsi_##prefix##_show_##name(				\
457 	struct iscsi_node_acl *nacl,					\
458 	char *page)							\
459 {									\
460 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
461 									\
462 	if (!capable(CAP_SYS_ADMIN))					\
463 		return -EPERM;						\
464 	return snprintf(page, PAGE_SIZE, "%s\n", auth->name);		\
465 }									\
466 									\
467 static ssize_t __iscsi_##prefix##_store_##name(				\
468 	struct iscsi_node_acl *nacl,					\
469 	const char *page,						\
470 	size_t count)							\
471 {									\
472 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
473 									\
474 	if (!capable(CAP_SYS_ADMIN))					\
475 		return -EPERM;						\
476 									\
477 	snprintf(auth->name, PAGE_SIZE, "%s", page);			\
478 	if (!strncmp("NULL", auth->name, 4))				\
479 		auth->naf_flags &= ~flags;				\
480 	else								\
481 		auth->naf_flags |= flags;				\
482 									\
483 	if ((auth->naf_flags & NAF_USERID_IN_SET) &&			\
484 	    (auth->naf_flags & NAF_PASSWORD_IN_SET))			\
485 		auth->authenticate_target = 1;				\
486 	else								\
487 		auth->authenticate_target = 0;				\
488 									\
489 	return count;							\
490 }
491 
492 #define __DEF_NACL_AUTH_INT(prefix, name)				\
493 static ssize_t __iscsi_##prefix##_show_##name(				\
494 	struct iscsi_node_acl *nacl,					\
495 	char *page)							\
496 {									\
497 	struct iscsi_node_auth *auth = &nacl->node_auth;		\
498 									\
499 	if (!capable(CAP_SYS_ADMIN))					\
500 		return -EPERM;						\
501 									\
502 	return snprintf(page, PAGE_SIZE, "%d\n", auth->name);		\
503 }
504 
505 #define DEF_NACL_AUTH_STR(name, flags)					\
506 	__DEF_NACL_AUTH_STR(nacl_auth, name, flags)			\
507 static ssize_t iscsi_nacl_auth_show_##name(				\
508 	struct se_node_acl *nacl,					\
509 	char *page)							\
510 {									\
511 	return __iscsi_nacl_auth_show_##name(container_of(nacl,		\
512 			struct iscsi_node_acl, se_node_acl), page);		\
513 }									\
514 static ssize_t iscsi_nacl_auth_store_##name(				\
515 	struct se_node_acl *nacl,					\
516 	const char *page,						\
517 	size_t count)							\
518 {									\
519 	return __iscsi_nacl_auth_store_##name(container_of(nacl,	\
520 			struct iscsi_node_acl, se_node_acl), page, count);	\
521 }
522 
523 #define DEF_NACL_AUTH_INT(name)						\
524 	__DEF_NACL_AUTH_INT(nacl_auth, name)				\
525 static ssize_t iscsi_nacl_auth_show_##name(				\
526 	struct se_node_acl *nacl,					\
527 	char *page)							\
528 {									\
529 	return __iscsi_nacl_auth_show_##name(container_of(nacl,		\
530 			struct iscsi_node_acl, se_node_acl), page);		\
531 }
532 
533 #define AUTH_ATTR(_name, _mode)	TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
534 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
535 
536 /*
537  * One-way authentication userid
538  */
539 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
540 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
541 /*
542  * One-way authentication password
543  */
544 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
545 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
546 /*
547  * Enforce mutual authentication
548  */
549 DEF_NACL_AUTH_INT(authenticate_target);
550 AUTH_ATTR_RO(authenticate_target);
551 /*
552  * Mutual authentication userid
553  */
554 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
555 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
556 /*
557  * Mutual authentication password
558  */
559 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
560 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
561 
562 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
563 	&iscsi_nacl_auth_userid.attr,
564 	&iscsi_nacl_auth_password.attr,
565 	&iscsi_nacl_auth_authenticate_target.attr,
566 	&iscsi_nacl_auth_userid_mutual.attr,
567 	&iscsi_nacl_auth_password_mutual.attr,
568 	NULL,
569 };
570 
571 /* End items for lio_target_nacl_auth_cit */
572 
573 /* Start items for lio_target_nacl_param_cit */
574 
575 #define DEF_NACL_PARAM(name)						\
576 static ssize_t iscsi_nacl_param_show_##name(				\
577 	struct se_node_acl *se_nacl,					\
578 	char *page)							\
579 {									\
580 	struct iscsi_session *sess;					\
581 	struct se_session *se_sess;					\
582 	ssize_t rb;							\
583 									\
584 	spin_lock_bh(&se_nacl->nacl_sess_lock);				\
585 	se_sess = se_nacl->nacl_sess;					\
586 	if (!se_sess) {							\
587 		rb = snprintf(page, PAGE_SIZE,				\
588 			"No Active iSCSI Session\n");			\
589 	} else {							\
590 		sess = se_sess->fabric_sess_ptr;			\
591 		rb = snprintf(page, PAGE_SIZE, "%u\n",			\
592 			(u32)sess->sess_ops->name);			\
593 	}								\
594 	spin_unlock_bh(&se_nacl->nacl_sess_lock);			\
595 									\
596 	return rb;							\
597 }
598 
599 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
600 
601 DEF_NACL_PARAM(MaxConnections);
602 NACL_PARAM_ATTR(MaxConnections);
603 
604 DEF_NACL_PARAM(InitialR2T);
605 NACL_PARAM_ATTR(InitialR2T);
606 
607 DEF_NACL_PARAM(ImmediateData);
608 NACL_PARAM_ATTR(ImmediateData);
609 
610 DEF_NACL_PARAM(MaxBurstLength);
611 NACL_PARAM_ATTR(MaxBurstLength);
612 
613 DEF_NACL_PARAM(FirstBurstLength);
614 NACL_PARAM_ATTR(FirstBurstLength);
615 
616 DEF_NACL_PARAM(DefaultTime2Wait);
617 NACL_PARAM_ATTR(DefaultTime2Wait);
618 
619 DEF_NACL_PARAM(DefaultTime2Retain);
620 NACL_PARAM_ATTR(DefaultTime2Retain);
621 
622 DEF_NACL_PARAM(MaxOutstandingR2T);
623 NACL_PARAM_ATTR(MaxOutstandingR2T);
624 
625 DEF_NACL_PARAM(DataPDUInOrder);
626 NACL_PARAM_ATTR(DataPDUInOrder);
627 
628 DEF_NACL_PARAM(DataSequenceInOrder);
629 NACL_PARAM_ATTR(DataSequenceInOrder);
630 
631 DEF_NACL_PARAM(ErrorRecoveryLevel);
632 NACL_PARAM_ATTR(ErrorRecoveryLevel);
633 
634 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
635 	&iscsi_nacl_param_MaxConnections.attr,
636 	&iscsi_nacl_param_InitialR2T.attr,
637 	&iscsi_nacl_param_ImmediateData.attr,
638 	&iscsi_nacl_param_MaxBurstLength.attr,
639 	&iscsi_nacl_param_FirstBurstLength.attr,
640 	&iscsi_nacl_param_DefaultTime2Wait.attr,
641 	&iscsi_nacl_param_DefaultTime2Retain.attr,
642 	&iscsi_nacl_param_MaxOutstandingR2T.attr,
643 	&iscsi_nacl_param_DataPDUInOrder.attr,
644 	&iscsi_nacl_param_DataSequenceInOrder.attr,
645 	&iscsi_nacl_param_ErrorRecoveryLevel.attr,
646 	NULL,
647 };
648 
649 /* End items for lio_target_nacl_param_cit */
650 
651 /* Start items for lio_target_acl_cit */
652 
653 static ssize_t lio_target_nacl_show_info(
654 	struct se_node_acl *se_nacl,
655 	char *page)
656 {
657 	struct iscsi_session *sess;
658 	struct iscsi_conn *conn;
659 	struct se_session *se_sess;
660 	ssize_t rb = 0;
661 
662 	spin_lock_bh(&se_nacl->nacl_sess_lock);
663 	se_sess = se_nacl->nacl_sess;
664 	if (!se_sess) {
665 		rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
666 			" Endpoint: %s\n", se_nacl->initiatorname);
667 	} else {
668 		sess = se_sess->fabric_sess_ptr;
669 
670 		if (sess->sess_ops->InitiatorName)
671 			rb += sprintf(page+rb, "InitiatorName: %s\n",
672 				sess->sess_ops->InitiatorName);
673 		if (sess->sess_ops->InitiatorAlias)
674 			rb += sprintf(page+rb, "InitiatorAlias: %s\n",
675 				sess->sess_ops->InitiatorAlias);
676 
677 		rb += sprintf(page+rb, "LIO Session ID: %u   "
678 			"ISID: 0x%02x %02x %02x %02x %02x %02x  "
679 			"TSIH: %hu  ", sess->sid,
680 			sess->isid[0], sess->isid[1], sess->isid[2],
681 			sess->isid[3], sess->isid[4], sess->isid[5],
682 			sess->tsih);
683 		rb += sprintf(page+rb, "SessionType: %s\n",
684 				(sess->sess_ops->SessionType) ?
685 				"Discovery" : "Normal");
686 		rb += sprintf(page+rb, "Session State: ");
687 		switch (sess->session_state) {
688 		case TARG_SESS_STATE_FREE:
689 			rb += sprintf(page+rb, "TARG_SESS_FREE\n");
690 			break;
691 		case TARG_SESS_STATE_ACTIVE:
692 			rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
693 			break;
694 		case TARG_SESS_STATE_LOGGED_IN:
695 			rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
696 			break;
697 		case TARG_SESS_STATE_FAILED:
698 			rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
699 			break;
700 		case TARG_SESS_STATE_IN_CONTINUE:
701 			rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
702 			break;
703 		default:
704 			rb += sprintf(page+rb, "ERROR: Unknown Session"
705 					" State!\n");
706 			break;
707 		}
708 
709 		rb += sprintf(page+rb, "---------------------[iSCSI Session"
710 				" Values]-----------------------\n");
711 		rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
712 				"  :  MaxCmdSN  :     ITT    :     TTT\n");
713 		rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
714 				"   0x%08x   0x%08x\n",
715 			sess->cmdsn_window,
716 			(sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
717 			sess->exp_cmd_sn, sess->max_cmd_sn,
718 			sess->init_task_tag, sess->targ_xfer_tag);
719 		rb += sprintf(page+rb, "----------------------[iSCSI"
720 				" Connections]-------------------------\n");
721 
722 		spin_lock(&sess->conn_lock);
723 		list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
724 			rb += sprintf(page+rb, "CID: %hu  Connection"
725 					" State: ", conn->cid);
726 			switch (conn->conn_state) {
727 			case TARG_CONN_STATE_FREE:
728 				rb += sprintf(page+rb,
729 					"TARG_CONN_STATE_FREE\n");
730 				break;
731 			case TARG_CONN_STATE_XPT_UP:
732 				rb += sprintf(page+rb,
733 					"TARG_CONN_STATE_XPT_UP\n");
734 				break;
735 			case TARG_CONN_STATE_IN_LOGIN:
736 				rb += sprintf(page+rb,
737 					"TARG_CONN_STATE_IN_LOGIN\n");
738 				break;
739 			case TARG_CONN_STATE_LOGGED_IN:
740 				rb += sprintf(page+rb,
741 					"TARG_CONN_STATE_LOGGED_IN\n");
742 				break;
743 			case TARG_CONN_STATE_IN_LOGOUT:
744 				rb += sprintf(page+rb,
745 					"TARG_CONN_STATE_IN_LOGOUT\n");
746 				break;
747 			case TARG_CONN_STATE_LOGOUT_REQUESTED:
748 				rb += sprintf(page+rb,
749 					"TARG_CONN_STATE_LOGOUT_REQUESTED\n");
750 				break;
751 			case TARG_CONN_STATE_CLEANUP_WAIT:
752 				rb += sprintf(page+rb,
753 					"TARG_CONN_STATE_CLEANUP_WAIT\n");
754 				break;
755 			default:
756 				rb += sprintf(page+rb,
757 					"ERROR: Unknown Connection State!\n");
758 				break;
759 			}
760 
761 			rb += sprintf(page+rb, "   Address %s %s", conn->login_ip,
762 				(conn->network_transport == ISCSI_TCP) ?
763 				"TCP" : "SCTP");
764 			rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
765 				conn->stat_sn);
766 		}
767 		spin_unlock(&sess->conn_lock);
768 	}
769 	spin_unlock_bh(&se_nacl->nacl_sess_lock);
770 
771 	return rb;
772 }
773 
774 TF_NACL_BASE_ATTR_RO(lio_target, info);
775 
776 static ssize_t lio_target_nacl_show_cmdsn_depth(
777 	struct se_node_acl *se_nacl,
778 	char *page)
779 {
780 	return sprintf(page, "%u\n", se_nacl->queue_depth);
781 }
782 
783 static ssize_t lio_target_nacl_store_cmdsn_depth(
784 	struct se_node_acl *se_nacl,
785 	const char *page,
786 	size_t count)
787 {
788 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
789 	struct iscsi_portal_group *tpg = container_of(se_tpg,
790 			struct iscsi_portal_group, tpg_se_tpg);
791 	struct config_item *acl_ci, *tpg_ci, *wwn_ci;
792 	char *endptr;
793 	u32 cmdsn_depth = 0;
794 	int ret;
795 
796 	cmdsn_depth = simple_strtoul(page, &endptr, 0);
797 	if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
798 		pr_err("Passed cmdsn_depth: %u exceeds"
799 			" TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
800 			TA_DEFAULT_CMDSN_DEPTH_MAX);
801 		return -EINVAL;
802 	}
803 	acl_ci = &se_nacl->acl_group.cg_item;
804 	if (!acl_ci) {
805 		pr_err("Unable to locatel acl_ci\n");
806 		return -EINVAL;
807 	}
808 	tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
809 	if (!tpg_ci) {
810 		pr_err("Unable to locate tpg_ci\n");
811 		return -EINVAL;
812 	}
813 	wwn_ci = &tpg_ci->ci_group->cg_item;
814 	if (!wwn_ci) {
815 		pr_err("Unable to locate config_item wwn_ci\n");
816 		return -EINVAL;
817 	}
818 
819 	if (iscsit_get_tpg(tpg) < 0)
820 		return -EINVAL;
821 	/*
822 	 * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
823 	 */
824 	ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
825 				config_item_name(acl_ci), cmdsn_depth, 1);
826 
827 	pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
828 		"InitiatorName: %s\n", config_item_name(wwn_ci),
829 		config_item_name(tpg_ci), cmdsn_depth,
830 		config_item_name(acl_ci));
831 
832 	iscsit_put_tpg(tpg);
833 	return (!ret) ? count : (ssize_t)ret;
834 }
835 
836 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
837 
838 static ssize_t lio_target_nacl_show_tag(
839 	struct se_node_acl *se_nacl,
840 	char *page)
841 {
842 	return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
843 }
844 
845 static ssize_t lio_target_nacl_store_tag(
846 	struct se_node_acl *se_nacl,
847 	const char *page,
848 	size_t count)
849 {
850 	int ret;
851 
852 	ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
853 
854 	if (ret < 0)
855 		return ret;
856 	return count;
857 }
858 
859 TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
860 
861 static struct configfs_attribute *lio_target_initiator_attrs[] = {
862 	&lio_target_nacl_info.attr,
863 	&lio_target_nacl_cmdsn_depth.attr,
864 	&lio_target_nacl_tag.attr,
865 	NULL,
866 };
867 
868 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
869 	struct se_portal_group *se_tpg)
870 {
871 	struct iscsi_node_acl *acl;
872 
873 	acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
874 	if (!acl) {
875 		pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
876 		return NULL;
877 	}
878 
879 	return &acl->se_node_acl;
880 }
881 
882 static struct se_node_acl *lio_target_make_nodeacl(
883 	struct se_portal_group *se_tpg,
884 	struct config_group *group,
885 	const char *name)
886 {
887 	struct config_group *stats_cg;
888 	struct iscsi_node_acl *acl;
889 	struct se_node_acl *se_nacl_new, *se_nacl;
890 	struct iscsi_portal_group *tpg = container_of(se_tpg,
891 			struct iscsi_portal_group, tpg_se_tpg);
892 	u32 cmdsn_depth;
893 
894 	se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
895 	if (!se_nacl_new)
896 		return ERR_PTR(-ENOMEM);
897 
898 	cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
899 	/*
900 	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
901 	 * when converting a NdoeACL from demo mode -> explict
902 	 */
903 	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
904 				name, cmdsn_depth);
905 	if (IS_ERR(se_nacl))
906 		return se_nacl;
907 
908 	acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
909 	stats_cg = &se_nacl->acl_fabric_stat_group;
910 
911 	stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
912 				GFP_KERNEL);
913 	if (!stats_cg->default_groups) {
914 		pr_err("Unable to allocate memory for"
915 				" stats_cg->default_groups\n");
916 		core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
917 		kfree(acl);
918 		return ERR_PTR(-ENOMEM);
919 	}
920 
921 	stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
922 	stats_cg->default_groups[1] = NULL;
923 	config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
924 			"iscsi_sess_stats", &iscsi_stat_sess_cit);
925 
926 	return se_nacl;
927 }
928 
929 static void lio_target_drop_nodeacl(
930 	struct se_node_acl *se_nacl)
931 {
932 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
933 	struct iscsi_node_acl *acl = container_of(se_nacl,
934 			struct iscsi_node_acl, se_node_acl);
935 	struct config_item *df_item;
936 	struct config_group *stats_cg;
937 	int i;
938 
939 	stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
940 	for (i = 0; stats_cg->default_groups[i]; i++) {
941 		df_item = &stats_cg->default_groups[i]->cg_item;
942 		stats_cg->default_groups[i] = NULL;
943 		config_item_put(df_item);
944 	}
945 	kfree(stats_cg->default_groups);
946 
947 	core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
948 	kfree(acl);
949 }
950 
951 /* End items for lio_target_acl_cit */
952 
953 /* Start items for lio_target_tpg_attrib_cit */
954 
955 #define DEF_TPG_ATTRIB(name)						\
956 									\
957 static ssize_t iscsi_tpg_attrib_show_##name(				\
958 	struct se_portal_group *se_tpg,				\
959 	char *page)							\
960 {									\
961 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
962 			struct iscsi_portal_group, tpg_se_tpg);	\
963 	ssize_t rb;							\
964 									\
965 	if (iscsit_get_tpg(tpg) < 0)					\
966 		return -EINVAL;						\
967 									\
968 	rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name);	\
969 	iscsit_put_tpg(tpg);						\
970 	return rb;							\
971 }									\
972 									\
973 static ssize_t iscsi_tpg_attrib_store_##name(				\
974 	struct se_portal_group *se_tpg,				\
975 	const char *page,						\
976 	size_t count)							\
977 {									\
978 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
979 			struct iscsi_portal_group, tpg_se_tpg);	\
980 	char *endptr;							\
981 	u32 val;							\
982 	int ret;							\
983 									\
984 	if (iscsit_get_tpg(tpg) < 0)					\
985 		return -EINVAL;						\
986 									\
987 	val = simple_strtoul(page, &endptr, 0);				\
988 	ret = iscsit_ta_##name(tpg, val);				\
989 	if (ret < 0)							\
990 		goto out;						\
991 									\
992 	iscsit_put_tpg(tpg);						\
993 	return count;							\
994 out:									\
995 	iscsit_put_tpg(tpg);						\
996 	return ret;							\
997 }
998 
999 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
1000 
1001 /*
1002  * Define iscsi_tpg_attrib_s_authentication
1003  */
1004 DEF_TPG_ATTRIB(authentication);
1005 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
1006 /*
1007  * Define iscsi_tpg_attrib_s_login_timeout
1008  */
1009 DEF_TPG_ATTRIB(login_timeout);
1010 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
1011 /*
1012  * Define iscsi_tpg_attrib_s_netif_timeout
1013  */
1014 DEF_TPG_ATTRIB(netif_timeout);
1015 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
1016 /*
1017  * Define iscsi_tpg_attrib_s_generate_node_acls
1018  */
1019 DEF_TPG_ATTRIB(generate_node_acls);
1020 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
1021 /*
1022  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
1023  */
1024 DEF_TPG_ATTRIB(default_cmdsn_depth);
1025 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
1026 /*
1027  Define iscsi_tpg_attrib_s_cache_dynamic_acls
1028  */
1029 DEF_TPG_ATTRIB(cache_dynamic_acls);
1030 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
1031 /*
1032  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
1033  */
1034 DEF_TPG_ATTRIB(demo_mode_write_protect);
1035 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
1036 /*
1037  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
1038  */
1039 DEF_TPG_ATTRIB(prod_mode_write_protect);
1040 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
1041 
1042 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
1043 	&iscsi_tpg_attrib_authentication.attr,
1044 	&iscsi_tpg_attrib_login_timeout.attr,
1045 	&iscsi_tpg_attrib_netif_timeout.attr,
1046 	&iscsi_tpg_attrib_generate_node_acls.attr,
1047 	&iscsi_tpg_attrib_default_cmdsn_depth.attr,
1048 	&iscsi_tpg_attrib_cache_dynamic_acls.attr,
1049 	&iscsi_tpg_attrib_demo_mode_write_protect.attr,
1050 	&iscsi_tpg_attrib_prod_mode_write_protect.attr,
1051 	NULL,
1052 };
1053 
1054 /* End items for lio_target_tpg_attrib_cit */
1055 
1056 /* Start items for lio_target_tpg_param_cit */
1057 
1058 #define DEF_TPG_PARAM(name)						\
1059 static ssize_t iscsi_tpg_param_show_##name(				\
1060 	struct se_portal_group *se_tpg,					\
1061 	char *page)							\
1062 {									\
1063 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
1064 			struct iscsi_portal_group, tpg_se_tpg);		\
1065 	struct iscsi_param *param;					\
1066 	ssize_t rb;							\
1067 									\
1068 	if (iscsit_get_tpg(tpg) < 0)					\
1069 		return -EINVAL;						\
1070 									\
1071 	param = iscsi_find_param_from_key(__stringify(name),		\
1072 				tpg->param_list);			\
1073 	if (!param) {							\
1074 		iscsit_put_tpg(tpg);					\
1075 		return -EINVAL;						\
1076 	}								\
1077 	rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);		\
1078 									\
1079 	iscsit_put_tpg(tpg);						\
1080 	return rb;							\
1081 }									\
1082 static ssize_t iscsi_tpg_param_store_##name(				\
1083 	struct se_portal_group *se_tpg,				\
1084 	const char *page,						\
1085 	size_t count)							\
1086 {									\
1087 	struct iscsi_portal_group *tpg = container_of(se_tpg,		\
1088 			struct iscsi_portal_group, tpg_se_tpg);		\
1089 	char *buf;							\
1090 	int ret;							\
1091 									\
1092 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);				\
1093 	if (!buf)							\
1094 		return -ENOMEM;						\
1095 	snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);	\
1096 	buf[strlen(buf)-1] = '\0'; /* Kill newline */			\
1097 									\
1098 	if (iscsit_get_tpg(tpg) < 0) {					\
1099 		kfree(buf);						\
1100 		return -EINVAL;						\
1101 	}								\
1102 									\
1103 	ret = iscsi_change_param_value(buf, tpg->param_list, 1);	\
1104 	if (ret < 0)							\
1105 		goto out;						\
1106 									\
1107 	kfree(buf);							\
1108 	iscsit_put_tpg(tpg);						\
1109 	return count;							\
1110 out:									\
1111 	kfree(buf);							\
1112 	iscsit_put_tpg(tpg);						\
1113 	return -EINVAL;						\
1114 }
1115 
1116 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1117 
1118 DEF_TPG_PARAM(AuthMethod);
1119 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1120 
1121 DEF_TPG_PARAM(HeaderDigest);
1122 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1123 
1124 DEF_TPG_PARAM(DataDigest);
1125 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1126 
1127 DEF_TPG_PARAM(MaxConnections);
1128 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1129 
1130 DEF_TPG_PARAM(TargetAlias);
1131 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1132 
1133 DEF_TPG_PARAM(InitialR2T);
1134 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1135 
1136 DEF_TPG_PARAM(ImmediateData);
1137 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1138 
1139 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1140 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1141 
1142 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1143 TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
1144 
1145 DEF_TPG_PARAM(MaxBurstLength);
1146 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1147 
1148 DEF_TPG_PARAM(FirstBurstLength);
1149 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1150 
1151 DEF_TPG_PARAM(DefaultTime2Wait);
1152 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1153 
1154 DEF_TPG_PARAM(DefaultTime2Retain);
1155 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1156 
1157 DEF_TPG_PARAM(MaxOutstandingR2T);
1158 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1159 
1160 DEF_TPG_PARAM(DataPDUInOrder);
1161 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1162 
1163 DEF_TPG_PARAM(DataSequenceInOrder);
1164 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1165 
1166 DEF_TPG_PARAM(ErrorRecoveryLevel);
1167 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1168 
1169 DEF_TPG_PARAM(IFMarker);
1170 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1171 
1172 DEF_TPG_PARAM(OFMarker);
1173 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1174 
1175 DEF_TPG_PARAM(IFMarkInt);
1176 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1177 
1178 DEF_TPG_PARAM(OFMarkInt);
1179 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1180 
1181 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1182 	&iscsi_tpg_param_AuthMethod.attr,
1183 	&iscsi_tpg_param_HeaderDigest.attr,
1184 	&iscsi_tpg_param_DataDigest.attr,
1185 	&iscsi_tpg_param_MaxConnections.attr,
1186 	&iscsi_tpg_param_TargetAlias.attr,
1187 	&iscsi_tpg_param_InitialR2T.attr,
1188 	&iscsi_tpg_param_ImmediateData.attr,
1189 	&iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1190 	&iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
1191 	&iscsi_tpg_param_MaxBurstLength.attr,
1192 	&iscsi_tpg_param_FirstBurstLength.attr,
1193 	&iscsi_tpg_param_DefaultTime2Wait.attr,
1194 	&iscsi_tpg_param_DefaultTime2Retain.attr,
1195 	&iscsi_tpg_param_MaxOutstandingR2T.attr,
1196 	&iscsi_tpg_param_DataPDUInOrder.attr,
1197 	&iscsi_tpg_param_DataSequenceInOrder.attr,
1198 	&iscsi_tpg_param_ErrorRecoveryLevel.attr,
1199 	&iscsi_tpg_param_IFMarker.attr,
1200 	&iscsi_tpg_param_OFMarker.attr,
1201 	&iscsi_tpg_param_IFMarkInt.attr,
1202 	&iscsi_tpg_param_OFMarkInt.attr,
1203 	NULL,
1204 };
1205 
1206 /* End items for lio_target_tpg_param_cit */
1207 
1208 /* Start items for lio_target_tpg_cit */
1209 
1210 static ssize_t lio_target_tpg_show_enable(
1211 	struct se_portal_group *se_tpg,
1212 	char *page)
1213 {
1214 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1215 			struct iscsi_portal_group, tpg_se_tpg);
1216 	ssize_t len;
1217 
1218 	spin_lock(&tpg->tpg_state_lock);
1219 	len = sprintf(page, "%d\n",
1220 			(tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1221 	spin_unlock(&tpg->tpg_state_lock);
1222 
1223 	return len;
1224 }
1225 
1226 static ssize_t lio_target_tpg_store_enable(
1227 	struct se_portal_group *se_tpg,
1228 	const char *page,
1229 	size_t count)
1230 {
1231 	struct iscsi_portal_group *tpg = container_of(se_tpg,
1232 			struct iscsi_portal_group, tpg_se_tpg);
1233 	char *endptr;
1234 	u32 op;
1235 	int ret = 0;
1236 
1237 	op = simple_strtoul(page, &endptr, 0);
1238 	if ((op != 1) && (op != 0)) {
1239 		pr_err("Illegal value for tpg_enable: %u\n", op);
1240 		return -EINVAL;
1241 	}
1242 
1243 	ret = iscsit_get_tpg(tpg);
1244 	if (ret < 0)
1245 		return -EINVAL;
1246 
1247 	if (op) {
1248 		ret = iscsit_tpg_enable_portal_group(tpg);
1249 		if (ret < 0)
1250 			goto out;
1251 	} else {
1252 		/*
1253 		 * iscsit_tpg_disable_portal_group() assumes force=1
1254 		 */
1255 		ret = iscsit_tpg_disable_portal_group(tpg, 1);
1256 		if (ret < 0)
1257 			goto out;
1258 	}
1259 
1260 	iscsit_put_tpg(tpg);
1261 	return count;
1262 out:
1263 	iscsit_put_tpg(tpg);
1264 	return -EINVAL;
1265 }
1266 
1267 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1268 
1269 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1270 	&lio_target_tpg_enable.attr,
1271 	NULL,
1272 };
1273 
1274 /* End items for lio_target_tpg_cit */
1275 
1276 /* Start items for lio_target_tiqn_cit */
1277 
1278 static struct se_portal_group *lio_target_tiqn_addtpg(
1279 	struct se_wwn *wwn,
1280 	struct config_group *group,
1281 	const char *name)
1282 {
1283 	struct iscsi_portal_group *tpg;
1284 	struct iscsi_tiqn *tiqn;
1285 	char *tpgt_str, *end_ptr;
1286 	int ret = 0;
1287 	unsigned short int tpgt;
1288 
1289 	tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1290 	/*
1291 	 * Only tpgt_# directory groups can be created below
1292 	 * target/iscsi/iqn.superturodiskarry/
1293 	*/
1294 	tpgt_str = strstr(name, "tpgt_");
1295 	if (!tpgt_str) {
1296 		pr_err("Unable to locate \"tpgt_#\" directory"
1297 				" group\n");
1298 		return NULL;
1299 	}
1300 	tpgt_str += 5; /* Skip ahead of "tpgt_" */
1301 	tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1302 
1303 	tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1304 	if (!tpg)
1305 		return NULL;
1306 
1307 	ret = core_tpg_register(
1308 			&lio_target_fabric_configfs->tf_ops,
1309 			wwn, &tpg->tpg_se_tpg, tpg,
1310 			TRANSPORT_TPG_TYPE_NORMAL);
1311 	if (ret < 0)
1312 		return NULL;
1313 
1314 	ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1315 	if (ret != 0)
1316 		goto out;
1317 
1318 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1319 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1320 			name);
1321 	return &tpg->tpg_se_tpg;
1322 out:
1323 	core_tpg_deregister(&tpg->tpg_se_tpg);
1324 	kfree(tpg);
1325 	return NULL;
1326 }
1327 
1328 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1329 {
1330 	struct iscsi_portal_group *tpg;
1331 	struct iscsi_tiqn *tiqn;
1332 
1333 	tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1334 	tiqn = tpg->tpg_tiqn;
1335 	/*
1336 	 * iscsit_tpg_del_portal_group() assumes force=1
1337 	 */
1338 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1339 	iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1340 }
1341 
1342 /* End items for lio_target_tiqn_cit */
1343 
1344 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1345 
1346 static ssize_t lio_target_wwn_show_attr_lio_version(
1347 	struct target_fabric_configfs *tf,
1348 	char *page)
1349 {
1350 	return sprintf(page, "RisingTide Systems Linux-iSCSI Target "ISCSIT_VERSION"\n");
1351 }
1352 
1353 TF_WWN_ATTR_RO(lio_target, lio_version);
1354 
1355 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1356 	&lio_target_wwn_lio_version.attr,
1357 	NULL,
1358 };
1359 
1360 static struct se_wwn *lio_target_call_coreaddtiqn(
1361 	struct target_fabric_configfs *tf,
1362 	struct config_group *group,
1363 	const char *name)
1364 {
1365 	struct config_group *stats_cg;
1366 	struct iscsi_tiqn *tiqn;
1367 
1368 	tiqn = iscsit_add_tiqn((unsigned char *)name);
1369 	if (IS_ERR(tiqn))
1370 		return ERR_CAST(tiqn);
1371 	/*
1372 	 * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1373 	 */
1374 	stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1375 
1376 	stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
1377 				GFP_KERNEL);
1378 	if (!stats_cg->default_groups) {
1379 		pr_err("Unable to allocate memory for"
1380 				" stats_cg->default_groups\n");
1381 		iscsit_del_tiqn(tiqn);
1382 		return ERR_PTR(-ENOMEM);
1383 	}
1384 
1385 	stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
1386 	stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
1387 	stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
1388 	stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
1389 	stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
1390 	stats_cg->default_groups[5] = NULL;
1391 	config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
1392 			"iscsi_instance", &iscsi_stat_instance_cit);
1393 	config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
1394 			"iscsi_sess_err", &iscsi_stat_sess_err_cit);
1395 	config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
1396 			"iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1397 	config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
1398 			"iscsi_login_stats", &iscsi_stat_login_cit);
1399 	config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
1400 			"iscsi_logout_stats", &iscsi_stat_logout_cit);
1401 
1402 	pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1403 	pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1404 			" %s\n", name);
1405 	return &tiqn->tiqn_wwn;
1406 }
1407 
1408 static void lio_target_call_coredeltiqn(
1409 	struct se_wwn *wwn)
1410 {
1411 	struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1412 	struct config_item *df_item;
1413 	struct config_group *stats_cg;
1414 	int i;
1415 
1416 	stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1417 	for (i = 0; stats_cg->default_groups[i]; i++) {
1418 		df_item = &stats_cg->default_groups[i]->cg_item;
1419 		stats_cg->default_groups[i] = NULL;
1420 		config_item_put(df_item);
1421 	}
1422 	kfree(stats_cg->default_groups);
1423 
1424 	pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1425 			tiqn->tiqn);
1426 	iscsit_del_tiqn(tiqn);
1427 }
1428 
1429 /* End LIO-Target TIQN struct contig_lio_target_cit */
1430 
1431 /* Start lio_target_discovery_auth_cit */
1432 
1433 #define DEF_DISC_AUTH_STR(name, flags)					\
1434 	__DEF_NACL_AUTH_STR(disc, name, flags)				\
1435 static ssize_t iscsi_disc_show_##name(					\
1436 	struct target_fabric_configfs *tf,				\
1437 	char *page)							\
1438 {									\
1439 	return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,	\
1440 		page);							\
1441 }									\
1442 static ssize_t iscsi_disc_store_##name(					\
1443 	struct target_fabric_configfs *tf,				\
1444 	const char *page,						\
1445 	size_t count)							\
1446 {									\
1447 	return __iscsi_disc_store_##name(&iscsit_global->discovery_acl,	\
1448 		page, count);						\
1449 }
1450 
1451 #define DEF_DISC_AUTH_INT(name)						\
1452 	__DEF_NACL_AUTH_INT(disc, name)					\
1453 static ssize_t iscsi_disc_show_##name(					\
1454 	struct target_fabric_configfs *tf,				\
1455 	char *page)							\
1456 {									\
1457 	return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,	\
1458 			page);						\
1459 }
1460 
1461 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1462 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1463 
1464 /*
1465  * One-way authentication userid
1466  */
1467 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1468 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1469 /*
1470  * One-way authentication password
1471  */
1472 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1473 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1474 /*
1475  * Enforce mutual authentication
1476  */
1477 DEF_DISC_AUTH_INT(authenticate_target);
1478 DISC_AUTH_ATTR_RO(authenticate_target);
1479 /*
1480  * Mutual authentication userid
1481  */
1482 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1483 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1484 /*
1485  * Mutual authentication password
1486  */
1487 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1488 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1489 
1490 /*
1491  * enforce_discovery_auth
1492  */
1493 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1494 	struct target_fabric_configfs *tf,
1495 	char *page)
1496 {
1497 	struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1498 
1499 	return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1500 }
1501 
1502 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1503 	struct target_fabric_configfs *tf,
1504 	const char *page,
1505 	size_t count)
1506 {
1507 	struct iscsi_param *param;
1508 	struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1509 	char *endptr;
1510 	u32 op;
1511 
1512 	op = simple_strtoul(page, &endptr, 0);
1513 	if ((op != 1) && (op != 0)) {
1514 		pr_err("Illegal value for enforce_discovery_auth:"
1515 				" %u\n", op);
1516 		return -EINVAL;
1517 	}
1518 
1519 	if (!discovery_tpg) {
1520 		pr_err("iscsit_global->discovery_tpg is NULL\n");
1521 		return -EINVAL;
1522 	}
1523 
1524 	param = iscsi_find_param_from_key(AUTHMETHOD,
1525 				discovery_tpg->param_list);
1526 	if (!param)
1527 		return -EINVAL;
1528 
1529 	if (op) {
1530 		/*
1531 		 * Reset the AuthMethod key to CHAP.
1532 		 */
1533 		if (iscsi_update_param_value(param, CHAP) < 0)
1534 			return -EINVAL;
1535 
1536 		discovery_tpg->tpg_attrib.authentication = 1;
1537 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1538 		pr_debug("LIO-CORE[0] Successfully enabled"
1539 			" authentication enforcement for iSCSI"
1540 			" Discovery TPG\n");
1541 	} else {
1542 		/*
1543 		 * Reset the AuthMethod key to CHAP,None
1544 		 */
1545 		if (iscsi_update_param_value(param, "CHAP,None") < 0)
1546 			return -EINVAL;
1547 
1548 		discovery_tpg->tpg_attrib.authentication = 0;
1549 		iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1550 		pr_debug("LIO-CORE[0] Successfully disabled"
1551 			" authentication enforcement for iSCSI"
1552 			" Discovery TPG\n");
1553 	}
1554 
1555 	return count;
1556 }
1557 
1558 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1559 
1560 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1561 	&iscsi_disc_userid.attr,
1562 	&iscsi_disc_password.attr,
1563 	&iscsi_disc_authenticate_target.attr,
1564 	&iscsi_disc_userid_mutual.attr,
1565 	&iscsi_disc_password_mutual.attr,
1566 	&iscsi_disc_enforce_discovery_auth.attr,
1567 	NULL,
1568 };
1569 
1570 /* End lio_target_discovery_auth_cit */
1571 
1572 /* Start functions for target_core_fabric_ops */
1573 
1574 static char *iscsi_get_fabric_name(void)
1575 {
1576 	return "iSCSI";
1577 }
1578 
1579 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1580 {
1581 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1582 
1583 	/* only used for printks or comparism with ->ref_task_tag */
1584 	return (__force u32)cmd->init_task_tag;
1585 }
1586 
1587 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1588 {
1589 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1590 
1591 	return cmd->i_state;
1592 }
1593 
1594 static u32 lio_sess_get_index(struct se_session *se_sess)
1595 {
1596 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1597 
1598 	return sess->session_index;
1599 }
1600 
1601 static u32 lio_sess_get_initiator_sid(
1602 	struct se_session *se_sess,
1603 	unsigned char *buf,
1604 	u32 size)
1605 {
1606 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1607 	/*
1608 	 * iSCSI Initiator Session Identifier from RFC-3720.
1609 	 */
1610 	return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1611 		sess->isid[0], sess->isid[1], sess->isid[2],
1612 		sess->isid[3], sess->isid[4], sess->isid[5]);
1613 }
1614 
1615 static int lio_queue_data_in(struct se_cmd *se_cmd)
1616 {
1617 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1618 
1619 	cmd->i_state = ISTATE_SEND_DATAIN;
1620 	cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
1621 
1622 	return 0;
1623 }
1624 
1625 static int lio_write_pending(struct se_cmd *se_cmd)
1626 {
1627 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1628 	struct iscsi_conn *conn = cmd->conn;
1629 
1630 	if (!cmd->immediate_data && !cmd->unsolicited_data)
1631 		return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1632 
1633 	return 0;
1634 }
1635 
1636 static int lio_write_pending_status(struct se_cmd *se_cmd)
1637 {
1638 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1639 	int ret;
1640 
1641 	spin_lock_bh(&cmd->istate_lock);
1642 	ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1643 	spin_unlock_bh(&cmd->istate_lock);
1644 
1645 	return ret;
1646 }
1647 
1648 static int lio_queue_status(struct se_cmd *se_cmd)
1649 {
1650 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1651 
1652 	cmd->i_state = ISTATE_SEND_STATUS;
1653 	cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
1654 
1655 	return 0;
1656 }
1657 
1658 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1659 {
1660 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1661 
1662 	cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1663 	iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1664 	return 0;
1665 }
1666 
1667 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1668 {
1669 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1670 
1671 	return &tpg->tpg_tiqn->tiqn[0];
1672 }
1673 
1674 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1675 {
1676 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1677 
1678 	return tpg->tpgt;
1679 }
1680 
1681 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1682 {
1683 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1684 
1685 	return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1686 }
1687 
1688 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1689 {
1690 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1691 
1692 	return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1693 }
1694 
1695 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1696 {
1697 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1698 
1699 	return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1700 }
1701 
1702 static int lio_tpg_check_demo_mode_write_protect(
1703 	struct se_portal_group *se_tpg)
1704 {
1705 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1706 
1707 	return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1708 }
1709 
1710 static int lio_tpg_check_prod_mode_write_protect(
1711 	struct se_portal_group *se_tpg)
1712 {
1713 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1714 
1715 	return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1716 }
1717 
1718 static void lio_tpg_release_fabric_acl(
1719 	struct se_portal_group *se_tpg,
1720 	struct se_node_acl *se_acl)
1721 {
1722 	struct iscsi_node_acl *acl = container_of(se_acl,
1723 				struct iscsi_node_acl, se_node_acl);
1724 	kfree(acl);
1725 }
1726 
1727 /*
1728  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1729  *
1730  * Also, this function calls iscsit_inc_session_usage_count() on the
1731  * struct iscsi_session in question.
1732  */
1733 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1734 {
1735 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1736 
1737 	spin_lock(&sess->conn_lock);
1738 	if (atomic_read(&sess->session_fall_back_to_erl0) ||
1739 	    atomic_read(&sess->session_logout) ||
1740 	    (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1741 		spin_unlock(&sess->conn_lock);
1742 		return 0;
1743 	}
1744 	atomic_set(&sess->session_reinstatement, 1);
1745 	spin_unlock(&sess->conn_lock);
1746 
1747 	iscsit_stop_time2retain_timer(sess);
1748 	iscsit_stop_session(sess, 1, 1);
1749 
1750 	return 1;
1751 }
1752 
1753 /*
1754  * Calls iscsit_dec_session_usage_count() as inverse of
1755  * lio_tpg_shutdown_session()
1756  */
1757 static void lio_tpg_close_session(struct se_session *se_sess)
1758 {
1759 	struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1760 	/*
1761 	 * If the iSCSI Session for the iSCSI Initiator Node exists,
1762 	 * forcefully shutdown the iSCSI NEXUS.
1763 	 */
1764 	iscsit_close_session(sess);
1765 }
1766 
1767 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1768 {
1769 	struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1770 
1771 	return tpg->tpg_tiqn->tiqn_index;
1772 }
1773 
1774 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1775 {
1776 	struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1777 				se_node_acl);
1778 
1779 	ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1780 	iscsit_set_default_node_attribues(acl);
1781 }
1782 
1783 static int lio_check_stop_free(struct se_cmd *se_cmd)
1784 {
1785 	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
1786 }
1787 
1788 static void lio_release_cmd(struct se_cmd *se_cmd)
1789 {
1790 	struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1791 
1792 	pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1793 	cmd->release_cmd(cmd);
1794 }
1795 
1796 /* End functions for target_core_fabric_ops */
1797 
1798 int iscsi_target_register_configfs(void)
1799 {
1800 	struct target_fabric_configfs *fabric;
1801 	int ret;
1802 
1803 	lio_target_fabric_configfs = NULL;
1804 	fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1805 	if (IS_ERR(fabric)) {
1806 		pr_err("target_fabric_configfs_init() for"
1807 				" LIO-Target failed!\n");
1808 		return PTR_ERR(fabric);
1809 	}
1810 	/*
1811 	 * Setup the fabric API of function pointers used by target_core_mod..
1812 	 */
1813 	fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1814 	fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1815 	fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1816 	fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1817 	fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1818 	fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1819 	fabric->tf_ops.tpg_get_pr_transport_id_len =
1820 				&iscsi_get_pr_transport_id_len;
1821 	fabric->tf_ops.tpg_parse_pr_out_transport_id =
1822 				&iscsi_parse_pr_out_transport_id;
1823 	fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1824 	fabric->tf_ops.tpg_check_demo_mode_cache =
1825 				&lio_tpg_check_demo_mode_cache;
1826 	fabric->tf_ops.tpg_check_demo_mode_write_protect =
1827 				&lio_tpg_check_demo_mode_write_protect;
1828 	fabric->tf_ops.tpg_check_prod_mode_write_protect =
1829 				&lio_tpg_check_prod_mode_write_protect;
1830 	fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1831 	fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1832 	fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1833 	fabric->tf_ops.check_stop_free = &lio_check_stop_free,
1834 	fabric->tf_ops.release_cmd = &lio_release_cmd;
1835 	fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1836 	fabric->tf_ops.close_session = &lio_tpg_close_session;
1837 	fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1838 	fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1839 	fabric->tf_ops.write_pending = &lio_write_pending;
1840 	fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1841 	fabric->tf_ops.set_default_node_attributes =
1842 				&lio_set_default_node_attributes;
1843 	fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1844 	fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1845 	fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1846 	fabric->tf_ops.queue_status = &lio_queue_status;
1847 	fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1848 	/*
1849 	 * Setup function pointers for generic logic in target_core_fabric_configfs.c
1850 	 */
1851 	fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1852 	fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1853 	fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1854 	fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1855 	fabric->tf_ops.fabric_post_link	= NULL;
1856 	fabric->tf_ops.fabric_pre_unlink = NULL;
1857 	fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1858 	fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1859 	fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1860 	fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1861 	/*
1862 	 * Setup default attribute lists for various fabric->tf_cit_tmpl
1863 	 * sturct config_item_type's
1864 	 */
1865 	TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1866 	TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1867 	TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1868 	TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1869 	TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1870 	TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1871 	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1872 	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1873 	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1874 	TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
1875 
1876 	ret = target_fabric_configfs_register(fabric);
1877 	if (ret < 0) {
1878 		pr_err("target_fabric_configfs_register() for"
1879 				" LIO-Target failed!\n");
1880 		target_fabric_configfs_free(fabric);
1881 		return ret;
1882 	}
1883 
1884 	lio_target_fabric_configfs = fabric;
1885 	pr_debug("LIO_TARGET[0] - Set fabric ->"
1886 			" lio_target_fabric_configfs\n");
1887 	return 0;
1888 }
1889 
1890 
1891 void iscsi_target_deregister_configfs(void)
1892 {
1893 	if (!lio_target_fabric_configfs)
1894 		return;
1895 	/*
1896 	 * Shutdown discovery sessions and disable discovery TPG
1897 	 */
1898 	if (iscsit_global->discovery_tpg)
1899 		iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
1900 
1901 	target_fabric_configfs_deregister(lio_target_fabric_configfs);
1902 	lio_target_fabric_configfs = NULL;
1903 	pr_debug("LIO_TARGET[0] - Cleared"
1904 				" lio_target_fabric_configfs\n");
1905 }
1906