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