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