drivers/target: Add export.h to files as required.
[cascardo/linux.git] / drivers / target / iscsi / iscsi_target_configfs.c
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 <target/target_core_base.h>
25 #include <target/target_core_transport.h>
26 #include <target/target_core_fabric_ops.h>
27 #include <target/target_core_fabric_configfs.h>
28 #include <target/target_core_fabric_lib.h>
29 #include <target/target_core_device.h>
30 #include <target/target_core_tpg.h>
31 #include <target/target_core_configfs.h>
32 #include <target/configfs_macros.h>
33
34 #include "iscsi_target_core.h"
35 #include "iscsi_target_parameters.h"
36 #include "iscsi_target_device.h"
37 #include "iscsi_target_erl0.h"
38 #include "iscsi_target_nodeattrib.h"
39 #include "iscsi_target_tpg.h"
40 #include "iscsi_target_util.h"
41 #include "iscsi_target.h"
42 #include "iscsi_target_stat.h"
43 #include "iscsi_target_configfs.h"
44
45 struct target_fabric_configfs *lio_target_fabric_configfs;
46
47 struct lio_target_configfs_attribute {
48         struct configfs_attribute attr;
49         ssize_t (*show)(void *, char *);
50         ssize_t (*store)(void *, const char *, size_t);
51 };
52
53 struct iscsi_portal_group *lio_get_tpg_from_tpg_item(
54         struct config_item *item,
55         struct iscsi_tiqn **tiqn_out)
56 {
57         struct se_portal_group *se_tpg = container_of(to_config_group(item),
58                                         struct se_portal_group, tpg_group);
59         struct iscsi_portal_group *tpg =
60                         (struct iscsi_portal_group *)se_tpg->se_tpg_fabric_ptr;
61         int ret;
62
63         if (!tpg) {
64                 pr_err("Unable to locate struct iscsi_portal_group "
65                         "pointer\n");
66                 return NULL;
67         }
68         ret = iscsit_get_tpg(tpg);
69         if (ret < 0)
70                 return NULL;
71
72         *tiqn_out = tpg->tpg_tiqn;
73         return tpg;
74 }
75
76 /* Start items for lio_target_portal_cit */
77
78 static ssize_t lio_target_np_show_sctp(
79         struct se_tpg_np *se_tpg_np,
80         char *page)
81 {
82         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
83                                 struct iscsi_tpg_np, se_tpg_np);
84         struct iscsi_tpg_np *tpg_np_sctp;
85         ssize_t rb;
86
87         tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
88         if (tpg_np_sctp)
89                 rb = sprintf(page, "1\n");
90         else
91                 rb = sprintf(page, "0\n");
92
93         return rb;
94 }
95
96 static ssize_t lio_target_np_store_sctp(
97         struct se_tpg_np *se_tpg_np,
98         const char *page,
99         size_t count)
100 {
101         struct iscsi_np *np;
102         struct iscsi_portal_group *tpg;
103         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
104                                 struct iscsi_tpg_np, se_tpg_np);
105         struct iscsi_tpg_np *tpg_np_sctp = NULL;
106         char *endptr;
107         u32 op;
108         int ret;
109
110         op = simple_strtoul(page, &endptr, 0);
111         if ((op != 1) && (op != 0)) {
112                 pr_err("Illegal value for tpg_enable: %u\n", op);
113                 return -EINVAL;
114         }
115         np = tpg_np->tpg_np;
116         if (!np) {
117                 pr_err("Unable to locate struct iscsi_np from"
118                                 " struct iscsi_tpg_np\n");
119                 return -EINVAL;
120         }
121
122         tpg = tpg_np->tpg;
123         if (iscsit_get_tpg(tpg) < 0)
124                 return -EINVAL;
125
126         if (op) {
127                 /*
128                  * Use existing np->np_sockaddr for SCTP network portal reference
129                  */
130                 tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
131                                         np->np_ip, tpg_np, ISCSI_SCTP_TCP);
132                 if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
133                         goto out;
134         } else {
135                 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
136                 if (!tpg_np_sctp)
137                         goto out;
138
139                 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
140                 if (ret < 0)
141                         goto out;
142         }
143
144         iscsit_put_tpg(tpg);
145         return count;
146 out:
147         iscsit_put_tpg(tpg);
148         return -EINVAL;
149 }
150
151 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
152
153 static struct configfs_attribute *lio_target_portal_attrs[] = {
154         &lio_target_np_sctp.attr,
155         NULL,
156 };
157
158 /* Stop items for lio_target_portal_cit */
159
160 /* Start items for lio_target_np_cit */
161
162 #define MAX_PORTAL_LEN          256
163
164 struct se_tpg_np *lio_target_call_addnptotpg(
165         struct se_portal_group *se_tpg,
166         struct config_group *group,
167         const char *name)
168 {
169         struct iscsi_portal_group *tpg;
170         struct iscsi_tpg_np *tpg_np;
171         char *str, *str2, *ip_str, *port_str;
172         struct __kernel_sockaddr_storage sockaddr;
173         struct sockaddr_in *sock_in;
174         struct sockaddr_in6 *sock_in6;
175         unsigned long port;
176         int ret;
177         char buf[MAX_PORTAL_LEN + 1];
178
179         if (strlen(name) > MAX_PORTAL_LEN) {
180                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
181                         (int)strlen(name), MAX_PORTAL_LEN);
182                 return ERR_PTR(-EOVERFLOW);
183         }
184         memset(buf, 0, MAX_PORTAL_LEN + 1);
185         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
186
187         memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
188
189         str = strstr(buf, "[");
190         if (str) {
191                 const char *end;
192
193                 str2 = strstr(str, "]");
194                 if (!str2) {
195                         pr_err("Unable to locate trailing \"]\""
196                                 " in IPv6 iSCSI network portal address\n");
197                         return ERR_PTR(-EINVAL);
198                 }
199                 str++; /* Skip over leading "[" */
200                 *str2 = '\0'; /* Terminate the IPv6 address */
201                 str2++; /* Skip over the "]" */
202                 port_str = strstr(str2, ":");
203                 if (!port_str) {
204                         pr_err("Unable to locate \":port\""
205                                 " in IPv6 iSCSI network portal address\n");
206                         return ERR_PTR(-EINVAL);
207                 }
208                 *port_str = '\0'; /* Terminate string for IP */
209                 port_str++; /* Skip over ":" */
210
211                 ret = strict_strtoul(port_str, 0, &port);
212                 if (ret < 0) {
213                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
214                         return ERR_PTR(ret);
215                 }
216                 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
217                 sock_in6->sin6_family = AF_INET6;
218                 sock_in6->sin6_port = htons((unsigned short)port);
219                 ret = in6_pton(str, IPV6_ADDRESS_SPACE,
220                                 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
221                 if (ret <= 0) {
222                         pr_err("in6_pton returned: %d\n", ret);
223                         return ERR_PTR(-EINVAL);
224                 }
225         } else {
226                 str = ip_str = &buf[0];
227                 port_str = strstr(ip_str, ":");
228                 if (!port_str) {
229                         pr_err("Unable to locate \":port\""
230                                 " in IPv4 iSCSI network portal address\n");
231                         return ERR_PTR(-EINVAL);
232                 }
233                 *port_str = '\0'; /* Terminate string for IP */
234                 port_str++; /* Skip over ":" */
235
236                 ret = strict_strtoul(port_str, 0, &port);
237                 if (ret < 0) {
238                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
239                         return ERR_PTR(ret);
240                 }
241                 sock_in = (struct sockaddr_in *)&sockaddr;
242                 sock_in->sin_family = AF_INET;
243                 sock_in->sin_port = htons((unsigned short)port);
244                 sock_in->sin_addr.s_addr = in_aton(ip_str);
245         }
246         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
247         ret = iscsit_get_tpg(tpg);
248         if (ret < 0)
249                 return ERR_PTR(-EINVAL);
250
251         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
252                 " PORTAL: %s\n",
253                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
254                 tpg->tpgt, name);
255         /*
256          * Assume ISCSI_TCP by default.  Other network portals for other
257          * iSCSI fabrics:
258          *
259          * Traditional iSCSI over SCTP (initial support)
260          * iSER/TCP (TODO, hardware available)
261          * iSER/SCTP (TODO, software emulation with osc-iwarp)
262          * iSER/IB (TODO, hardware available)
263          *
264          * can be enabled with atributes under
265          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
266          *
267          */
268         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
269                                 ISCSI_TCP);
270         if (IS_ERR(tpg_np)) {
271                 iscsit_put_tpg(tpg);
272                 return ERR_CAST(tpg_np);
273         }
274         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
275
276         iscsit_put_tpg(tpg);
277         return &tpg_np->se_tpg_np;
278 }
279
280 static void lio_target_call_delnpfromtpg(
281         struct se_tpg_np *se_tpg_np)
282 {
283         struct iscsi_portal_group *tpg;
284         struct iscsi_tpg_np *tpg_np;
285         struct se_portal_group *se_tpg;
286         int ret;
287
288         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
289         tpg = tpg_np->tpg;
290         ret = iscsit_get_tpg(tpg);
291         if (ret < 0)
292                 return;
293
294         se_tpg = &tpg->tpg_se_tpg;
295         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
296                 " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
297                 tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
298
299         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
300         if (ret < 0)
301                 goto out;
302
303         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
304 out:
305         iscsit_put_tpg(tpg);
306 }
307
308 /* End items for lio_target_np_cit */
309
310 /* Start items for lio_target_nacl_attrib_cit */
311
312 #define DEF_NACL_ATTRIB(name)                                           \
313 static ssize_t iscsi_nacl_attrib_show_##name(                           \
314         struct se_node_acl *se_nacl,                                    \
315         char *page)                                                     \
316 {                                                                       \
317         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
318                                         se_node_acl);                   \
319                                                                         \
320         return sprintf(page, "%u\n", ISCSI_NODE_ATTRIB(nacl)->name);    \
321 }                                                                       \
322                                                                         \
323 static ssize_t iscsi_nacl_attrib_store_##name(                          \
324         struct se_node_acl *se_nacl,                                    \
325         const char *page,                                               \
326         size_t count)                                                   \
327 {                                                                       \
328         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
329                                         se_node_acl);                   \
330         char *endptr;                                                   \
331         u32 val;                                                        \
332         int ret;                                                        \
333                                                                         \
334         val = simple_strtoul(page, &endptr, 0);                         \
335         ret = iscsit_na_##name(nacl, val);                              \
336         if (ret < 0)                                                    \
337                 return ret;                                             \
338                                                                         \
339         return count;                                                   \
340 }
341
342 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
343 /*
344  * Define iscsi_node_attrib_s_dataout_timeout
345  */
346 DEF_NACL_ATTRIB(dataout_timeout);
347 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
348 /*
349  * Define iscsi_node_attrib_s_dataout_timeout_retries
350  */
351 DEF_NACL_ATTRIB(dataout_timeout_retries);
352 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
353 /*
354  * Define iscsi_node_attrib_s_default_erl
355  */
356 DEF_NACL_ATTRIB(default_erl);
357 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
358 /*
359  * Define iscsi_node_attrib_s_nopin_timeout
360  */
361 DEF_NACL_ATTRIB(nopin_timeout);
362 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
363 /*
364  * Define iscsi_node_attrib_s_nopin_response_timeout
365  */
366 DEF_NACL_ATTRIB(nopin_response_timeout);
367 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
368 /*
369  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
370  */
371 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
372 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
373 /*
374  * Define iscsi_node_attrib_s_random_datain_seq_offsets
375  */
376 DEF_NACL_ATTRIB(random_datain_seq_offsets);
377 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
378 /*
379  * Define iscsi_node_attrib_s_random_r2t_offsets
380  */
381 DEF_NACL_ATTRIB(random_r2t_offsets);
382 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
383
384 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
385         &iscsi_nacl_attrib_dataout_timeout.attr,
386         &iscsi_nacl_attrib_dataout_timeout_retries.attr,
387         &iscsi_nacl_attrib_default_erl.attr,
388         &iscsi_nacl_attrib_nopin_timeout.attr,
389         &iscsi_nacl_attrib_nopin_response_timeout.attr,
390         &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
391         &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
392         &iscsi_nacl_attrib_random_r2t_offsets.attr,
393         NULL,
394 };
395
396 /* End items for lio_target_nacl_attrib_cit */
397
398 /* Start items for lio_target_nacl_auth_cit */
399
400 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
401 static ssize_t __iscsi_##prefix##_show_##name(                          \
402         struct iscsi_node_acl *nacl,                                    \
403         char *page)                                                     \
404 {                                                                       \
405         struct iscsi_node_auth *auth = &nacl->node_auth;                \
406                                                                         \
407         if (!capable(CAP_SYS_ADMIN))                                    \
408                 return -EPERM;                                          \
409         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
410 }                                                                       \
411                                                                         \
412 static ssize_t __iscsi_##prefix##_store_##name(                         \
413         struct iscsi_node_acl *nacl,                                    \
414         const char *page,                                               \
415         size_t count)                                                   \
416 {                                                                       \
417         struct iscsi_node_auth *auth = &nacl->node_auth;                \
418                                                                         \
419         if (!capable(CAP_SYS_ADMIN))                                    \
420                 return -EPERM;                                          \
421                                                                         \
422         snprintf(auth->name, PAGE_SIZE, "%s", page);                    \
423         if (!strncmp("NULL", auth->name, 4))                            \
424                 auth->naf_flags &= ~flags;                              \
425         else                                                            \
426                 auth->naf_flags |= flags;                               \
427                                                                         \
428         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
429             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
430                 auth->authenticate_target = 1;                          \
431         else                                                            \
432                 auth->authenticate_target = 0;                          \
433                                                                         \
434         return count;                                                   \
435 }
436
437 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
438 static ssize_t __iscsi_##prefix##_show_##name(                          \
439         struct iscsi_node_acl *nacl,                                    \
440         char *page)                                                     \
441 {                                                                       \
442         struct iscsi_node_auth *auth = &nacl->node_auth;                \
443                                                                         \
444         if (!capable(CAP_SYS_ADMIN))                                    \
445                 return -EPERM;                                          \
446                                                                         \
447         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
448 }
449
450 #define DEF_NACL_AUTH_STR(name, flags)                                  \
451         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
452 static ssize_t iscsi_nacl_auth_show_##name(                             \
453         struct se_node_acl *nacl,                                       \
454         char *page)                                                     \
455 {                                                                       \
456         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
457                         struct iscsi_node_acl, se_node_acl), page);             \
458 }                                                                       \
459 static ssize_t iscsi_nacl_auth_store_##name(                            \
460         struct se_node_acl *nacl,                                       \
461         const char *page,                                               \
462         size_t count)                                                   \
463 {                                                                       \
464         return __iscsi_nacl_auth_store_##name(container_of(nacl,        \
465                         struct iscsi_node_acl, se_node_acl), page, count);      \
466 }
467
468 #define DEF_NACL_AUTH_INT(name)                                         \
469         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
470 static ssize_t iscsi_nacl_auth_show_##name(                             \
471         struct se_node_acl *nacl,                                       \
472         char *page)                                                     \
473 {                                                                       \
474         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
475                         struct iscsi_node_acl, se_node_acl), page);             \
476 }
477
478 #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
479 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
480
481 /*
482  * One-way authentication userid
483  */
484 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
485 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
486 /*
487  * One-way authentication password
488  */
489 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
490 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
491 /*
492  * Enforce mutual authentication
493  */
494 DEF_NACL_AUTH_INT(authenticate_target);
495 AUTH_ATTR_RO(authenticate_target);
496 /*
497  * Mutual authentication userid
498  */
499 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
500 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
501 /*
502  * Mutual authentication password
503  */
504 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
505 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
506
507 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
508         &iscsi_nacl_auth_userid.attr,
509         &iscsi_nacl_auth_password.attr,
510         &iscsi_nacl_auth_authenticate_target.attr,
511         &iscsi_nacl_auth_userid_mutual.attr,
512         &iscsi_nacl_auth_password_mutual.attr,
513         NULL,
514 };
515
516 /* End items for lio_target_nacl_auth_cit */
517
518 /* Start items for lio_target_nacl_param_cit */
519
520 #define DEF_NACL_PARAM(name)                                            \
521 static ssize_t iscsi_nacl_param_show_##name(                            \
522         struct se_node_acl *se_nacl,                                    \
523         char *page)                                                     \
524 {                                                                       \
525         struct iscsi_session *sess;                                     \
526         struct se_session *se_sess;                                     \
527         ssize_t rb;                                                     \
528                                                                         \
529         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
530         se_sess = se_nacl->nacl_sess;                                   \
531         if (!se_sess) {                                                 \
532                 rb = snprintf(page, PAGE_SIZE,                          \
533                         "No Active iSCSI Session\n");                   \
534         } else {                                                        \
535                 sess = se_sess->fabric_sess_ptr;                        \
536                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
537                         (u32)sess->sess_ops->name);                     \
538         }                                                               \
539         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
540                                                                         \
541         return rb;                                                      \
542 }
543
544 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
545
546 DEF_NACL_PARAM(MaxConnections);
547 NACL_PARAM_ATTR(MaxConnections);
548
549 DEF_NACL_PARAM(InitialR2T);
550 NACL_PARAM_ATTR(InitialR2T);
551
552 DEF_NACL_PARAM(ImmediateData);
553 NACL_PARAM_ATTR(ImmediateData);
554
555 DEF_NACL_PARAM(MaxBurstLength);
556 NACL_PARAM_ATTR(MaxBurstLength);
557
558 DEF_NACL_PARAM(FirstBurstLength);
559 NACL_PARAM_ATTR(FirstBurstLength);
560
561 DEF_NACL_PARAM(DefaultTime2Wait);
562 NACL_PARAM_ATTR(DefaultTime2Wait);
563
564 DEF_NACL_PARAM(DefaultTime2Retain);
565 NACL_PARAM_ATTR(DefaultTime2Retain);
566
567 DEF_NACL_PARAM(MaxOutstandingR2T);
568 NACL_PARAM_ATTR(MaxOutstandingR2T);
569
570 DEF_NACL_PARAM(DataPDUInOrder);
571 NACL_PARAM_ATTR(DataPDUInOrder);
572
573 DEF_NACL_PARAM(DataSequenceInOrder);
574 NACL_PARAM_ATTR(DataSequenceInOrder);
575
576 DEF_NACL_PARAM(ErrorRecoveryLevel);
577 NACL_PARAM_ATTR(ErrorRecoveryLevel);
578
579 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
580         &iscsi_nacl_param_MaxConnections.attr,
581         &iscsi_nacl_param_InitialR2T.attr,
582         &iscsi_nacl_param_ImmediateData.attr,
583         &iscsi_nacl_param_MaxBurstLength.attr,
584         &iscsi_nacl_param_FirstBurstLength.attr,
585         &iscsi_nacl_param_DefaultTime2Wait.attr,
586         &iscsi_nacl_param_DefaultTime2Retain.attr,
587         &iscsi_nacl_param_MaxOutstandingR2T.attr,
588         &iscsi_nacl_param_DataPDUInOrder.attr,
589         &iscsi_nacl_param_DataSequenceInOrder.attr,
590         &iscsi_nacl_param_ErrorRecoveryLevel.attr,
591         NULL,
592 };
593
594 /* End items for lio_target_nacl_param_cit */
595
596 /* Start items for lio_target_acl_cit */
597
598 static ssize_t lio_target_nacl_show_info(
599         struct se_node_acl *se_nacl,
600         char *page)
601 {
602         struct iscsi_session *sess;
603         struct iscsi_conn *conn;
604         struct se_session *se_sess;
605         ssize_t rb = 0;
606
607         spin_lock_bh(&se_nacl->nacl_sess_lock);
608         se_sess = se_nacl->nacl_sess;
609         if (!se_sess) {
610                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
611                         " Endpoint: %s\n", se_nacl->initiatorname);
612         } else {
613                 sess = se_sess->fabric_sess_ptr;
614
615                 if (sess->sess_ops->InitiatorName)
616                         rb += sprintf(page+rb, "InitiatorName: %s\n",
617                                 sess->sess_ops->InitiatorName);
618                 if (sess->sess_ops->InitiatorAlias)
619                         rb += sprintf(page+rb, "InitiatorAlias: %s\n",
620                                 sess->sess_ops->InitiatorAlias);
621
622                 rb += sprintf(page+rb, "LIO Session ID: %u   "
623                         "ISID: 0x%02x %02x %02x %02x %02x %02x  "
624                         "TSIH: %hu  ", sess->sid,
625                         sess->isid[0], sess->isid[1], sess->isid[2],
626                         sess->isid[3], sess->isid[4], sess->isid[5],
627                         sess->tsih);
628                 rb += sprintf(page+rb, "SessionType: %s\n",
629                                 (sess->sess_ops->SessionType) ?
630                                 "Discovery" : "Normal");
631                 rb += sprintf(page+rb, "Session State: ");
632                 switch (sess->session_state) {
633                 case TARG_SESS_STATE_FREE:
634                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
635                         break;
636                 case TARG_SESS_STATE_ACTIVE:
637                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
638                         break;
639                 case TARG_SESS_STATE_LOGGED_IN:
640                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
641                         break;
642                 case TARG_SESS_STATE_FAILED:
643                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
644                         break;
645                 case TARG_SESS_STATE_IN_CONTINUE:
646                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
647                         break;
648                 default:
649                         rb += sprintf(page+rb, "ERROR: Unknown Session"
650                                         " State!\n");
651                         break;
652                 }
653
654                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
655                                 " Values]-----------------------\n");
656                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
657                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
658                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
659                                 "   0x%08x   0x%08x\n",
660                         sess->cmdsn_window,
661                         (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
662                         sess->exp_cmd_sn, sess->max_cmd_sn,
663                         sess->init_task_tag, sess->targ_xfer_tag);
664                 rb += sprintf(page+rb, "----------------------[iSCSI"
665                                 " Connections]-------------------------\n");
666
667                 spin_lock(&sess->conn_lock);
668                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
669                         rb += sprintf(page+rb, "CID: %hu  Connection"
670                                         " State: ", conn->cid);
671                         switch (conn->conn_state) {
672                         case TARG_CONN_STATE_FREE:
673                                 rb += sprintf(page+rb,
674                                         "TARG_CONN_STATE_FREE\n");
675                                 break;
676                         case TARG_CONN_STATE_XPT_UP:
677                                 rb += sprintf(page+rb,
678                                         "TARG_CONN_STATE_XPT_UP\n");
679                                 break;
680                         case TARG_CONN_STATE_IN_LOGIN:
681                                 rb += sprintf(page+rb,
682                                         "TARG_CONN_STATE_IN_LOGIN\n");
683                                 break;
684                         case TARG_CONN_STATE_LOGGED_IN:
685                                 rb += sprintf(page+rb,
686                                         "TARG_CONN_STATE_LOGGED_IN\n");
687                                 break;
688                         case TARG_CONN_STATE_IN_LOGOUT:
689                                 rb += sprintf(page+rb,
690                                         "TARG_CONN_STATE_IN_LOGOUT\n");
691                                 break;
692                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
693                                 rb += sprintf(page+rb,
694                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
695                                 break;
696                         case TARG_CONN_STATE_CLEANUP_WAIT:
697                                 rb += sprintf(page+rb,
698                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
699                                 break;
700                         default:
701                                 rb += sprintf(page+rb,
702                                         "ERROR: Unknown Connection State!\n");
703                                 break;
704                         }
705
706                         rb += sprintf(page+rb, "   Address %s %s", conn->login_ip,
707                                 (conn->network_transport == ISCSI_TCP) ?
708                                 "TCP" : "SCTP");
709                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
710                                 conn->stat_sn);
711                 }
712                 spin_unlock(&sess->conn_lock);
713         }
714         spin_unlock_bh(&se_nacl->nacl_sess_lock);
715
716         return rb;
717 }
718
719 TF_NACL_BASE_ATTR_RO(lio_target, info);
720
721 static ssize_t lio_target_nacl_show_cmdsn_depth(
722         struct se_node_acl *se_nacl,
723         char *page)
724 {
725         return sprintf(page, "%u\n", se_nacl->queue_depth);
726 }
727
728 static ssize_t lio_target_nacl_store_cmdsn_depth(
729         struct se_node_acl *se_nacl,
730         const char *page,
731         size_t count)
732 {
733         struct se_portal_group *se_tpg = se_nacl->se_tpg;
734         struct iscsi_portal_group *tpg = container_of(se_tpg,
735                         struct iscsi_portal_group, tpg_se_tpg);
736         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
737         char *endptr;
738         u32 cmdsn_depth = 0;
739         int ret;
740
741         cmdsn_depth = simple_strtoul(page, &endptr, 0);
742         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
743                 pr_err("Passed cmdsn_depth: %u exceeds"
744                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
745                         TA_DEFAULT_CMDSN_DEPTH_MAX);
746                 return -EINVAL;
747         }
748         acl_ci = &se_nacl->acl_group.cg_item;
749         if (!acl_ci) {
750                 pr_err("Unable to locatel acl_ci\n");
751                 return -EINVAL;
752         }
753         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
754         if (!tpg_ci) {
755                 pr_err("Unable to locate tpg_ci\n");
756                 return -EINVAL;
757         }
758         wwn_ci = &tpg_ci->ci_group->cg_item;
759         if (!wwn_ci) {
760                 pr_err("Unable to locate config_item wwn_ci\n");
761                 return -EINVAL;
762         }
763
764         if (iscsit_get_tpg(tpg) < 0)
765                 return -EINVAL;
766         /*
767          * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
768          */
769         ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
770                                 config_item_name(acl_ci), cmdsn_depth, 1);
771
772         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
773                 "InitiatorName: %s\n", config_item_name(wwn_ci),
774                 config_item_name(tpg_ci), cmdsn_depth,
775                 config_item_name(acl_ci));
776
777         iscsit_put_tpg(tpg);
778         return (!ret) ? count : (ssize_t)ret;
779 }
780
781 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
782
783 static struct configfs_attribute *lio_target_initiator_attrs[] = {
784         &lio_target_nacl_info.attr,
785         &lio_target_nacl_cmdsn_depth.attr,
786         NULL,
787 };
788
789 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
790         struct se_portal_group *se_tpg)
791 {
792         struct iscsi_node_acl *acl;
793
794         acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
795         if (!acl) {
796                 pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
797                 return NULL;
798         }
799
800         return &acl->se_node_acl;
801 }
802
803 static struct se_node_acl *lio_target_make_nodeacl(
804         struct se_portal_group *se_tpg,
805         struct config_group *group,
806         const char *name)
807 {
808         struct config_group *stats_cg;
809         struct iscsi_node_acl *acl;
810         struct se_node_acl *se_nacl_new, *se_nacl;
811         struct iscsi_portal_group *tpg = container_of(se_tpg,
812                         struct iscsi_portal_group, tpg_se_tpg);
813         u32 cmdsn_depth;
814
815         se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
816         if (!se_nacl_new)
817                 return ERR_PTR(-ENOMEM);
818
819         acl = container_of(se_nacl_new, struct iscsi_node_acl,
820                                 se_node_acl);
821
822         cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
823         /*
824          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
825          * when converting a NdoeACL from demo mode -> explict
826          */
827         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
828                                 name, cmdsn_depth);
829         if (IS_ERR(se_nacl))
830                 return se_nacl;
831
832         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
833
834         stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
835                                 GFP_KERNEL);
836         if (!stats_cg->default_groups) {
837                 pr_err("Unable to allocate memory for"
838                                 " stats_cg->default_groups\n");
839                 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
840                 kfree(acl);
841                 return ERR_PTR(-ENOMEM);
842         }
843
844         stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
845         stats_cg->default_groups[1] = NULL;
846         config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
847                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
848
849         return se_nacl;
850 }
851
852 static void lio_target_drop_nodeacl(
853         struct se_node_acl *se_nacl)
854 {
855         struct se_portal_group *se_tpg = se_nacl->se_tpg;
856         struct iscsi_node_acl *acl = container_of(se_nacl,
857                         struct iscsi_node_acl, se_node_acl);
858         struct config_item *df_item;
859         struct config_group *stats_cg;
860         int i;
861
862         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
863         for (i = 0; stats_cg->default_groups[i]; i++) {
864                 df_item = &stats_cg->default_groups[i]->cg_item;
865                 stats_cg->default_groups[i] = NULL;
866                 config_item_put(df_item);
867         }
868         kfree(stats_cg->default_groups);
869
870         core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
871         kfree(acl);
872 }
873
874 /* End items for lio_target_acl_cit */
875
876 /* Start items for lio_target_tpg_attrib_cit */
877
878 #define DEF_TPG_ATTRIB(name)                                            \
879                                                                         \
880 static ssize_t iscsi_tpg_attrib_show_##name(                            \
881         struct se_portal_group *se_tpg,                         \
882         char *page)                                                     \
883 {                                                                       \
884         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
885                         struct iscsi_portal_group, tpg_se_tpg); \
886         ssize_t rb;                                                     \
887                                                                         \
888         if (iscsit_get_tpg(tpg) < 0)                                    \
889                 return -EINVAL;                                         \
890                                                                         \
891         rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name);        \
892         iscsit_put_tpg(tpg);                                            \
893         return rb;                                                      \
894 }                                                                       \
895                                                                         \
896 static ssize_t iscsi_tpg_attrib_store_##name(                           \
897         struct se_portal_group *se_tpg,                         \
898         const char *page,                                               \
899         size_t count)                                                   \
900 {                                                                       \
901         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
902                         struct iscsi_portal_group, tpg_se_tpg); \
903         char *endptr;                                                   \
904         u32 val;                                                        \
905         int ret;                                                        \
906                                                                         \
907         if (iscsit_get_tpg(tpg) < 0)                                    \
908                 return -EINVAL;                                         \
909                                                                         \
910         val = simple_strtoul(page, &endptr, 0);                         \
911         ret = iscsit_ta_##name(tpg, val);                               \
912         if (ret < 0)                                                    \
913                 goto out;                                               \
914                                                                         \
915         iscsit_put_tpg(tpg);                                            \
916         return count;                                                   \
917 out:                                                                    \
918         iscsit_put_tpg(tpg);                                            \
919         return ret;                                                     \
920 }
921
922 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
923
924 /*
925  * Define iscsi_tpg_attrib_s_authentication
926  */
927 DEF_TPG_ATTRIB(authentication);
928 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
929 /*
930  * Define iscsi_tpg_attrib_s_login_timeout
931  */
932 DEF_TPG_ATTRIB(login_timeout);
933 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
934 /*
935  * Define iscsi_tpg_attrib_s_netif_timeout
936  */
937 DEF_TPG_ATTRIB(netif_timeout);
938 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
939 /*
940  * Define iscsi_tpg_attrib_s_generate_node_acls
941  */
942 DEF_TPG_ATTRIB(generate_node_acls);
943 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
944 /*
945  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
946  */
947 DEF_TPG_ATTRIB(default_cmdsn_depth);
948 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
949 /*
950  Define iscsi_tpg_attrib_s_cache_dynamic_acls
951  */
952 DEF_TPG_ATTRIB(cache_dynamic_acls);
953 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
954 /*
955  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
956  */
957 DEF_TPG_ATTRIB(demo_mode_write_protect);
958 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
959 /*
960  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
961  */
962 DEF_TPG_ATTRIB(prod_mode_write_protect);
963 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
964
965 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
966         &iscsi_tpg_attrib_authentication.attr,
967         &iscsi_tpg_attrib_login_timeout.attr,
968         &iscsi_tpg_attrib_netif_timeout.attr,
969         &iscsi_tpg_attrib_generate_node_acls.attr,
970         &iscsi_tpg_attrib_default_cmdsn_depth.attr,
971         &iscsi_tpg_attrib_cache_dynamic_acls.attr,
972         &iscsi_tpg_attrib_demo_mode_write_protect.attr,
973         &iscsi_tpg_attrib_prod_mode_write_protect.attr,
974         NULL,
975 };
976
977 /* End items for lio_target_tpg_attrib_cit */
978
979 /* Start items for lio_target_tpg_param_cit */
980
981 #define DEF_TPG_PARAM(name)                                             \
982 static ssize_t iscsi_tpg_param_show_##name(                             \
983         struct se_portal_group *se_tpg,                                 \
984         char *page)                                                     \
985 {                                                                       \
986         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
987                         struct iscsi_portal_group, tpg_se_tpg);         \
988         struct iscsi_param *param;                                      \
989         ssize_t rb;                                                     \
990                                                                         \
991         if (iscsit_get_tpg(tpg) < 0)                                    \
992                 return -EINVAL;                                         \
993                                                                         \
994         param = iscsi_find_param_from_key(__stringify(name),            \
995                                 tpg->param_list);                       \
996         if (!param) {                                                   \
997                 iscsit_put_tpg(tpg);                                    \
998                 return -EINVAL;                                         \
999         }                                                               \
1000         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
1001                                                                         \
1002         iscsit_put_tpg(tpg);                                            \
1003         return rb;                                                      \
1004 }                                                                       \
1005 static ssize_t iscsi_tpg_param_store_##name(                            \
1006         struct se_portal_group *se_tpg,                         \
1007         const char *page,                                               \
1008         size_t count)                                                   \
1009 {                                                                       \
1010         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1011                         struct iscsi_portal_group, tpg_se_tpg);         \
1012         char *buf;                                                      \
1013         int ret;                                                        \
1014                                                                         \
1015         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
1016         if (!buf)                                                       \
1017                 return -ENOMEM;                                         \
1018         snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);     \
1019         buf[strlen(buf)-1] = '\0'; /* Kill newline */                   \
1020                                                                         \
1021         if (iscsit_get_tpg(tpg) < 0) {                                  \
1022                 kfree(buf);                                             \
1023                 return -EINVAL;                                         \
1024         }                                                               \
1025                                                                         \
1026         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
1027         if (ret < 0)                                                    \
1028                 goto out;                                               \
1029                                                                         \
1030         kfree(buf);                                                     \
1031         iscsit_put_tpg(tpg);                                            \
1032         return count;                                                   \
1033 out:                                                                    \
1034         kfree(buf);                                                     \
1035         iscsit_put_tpg(tpg);                                            \
1036         return -EINVAL;                                         \
1037 }
1038
1039 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1040
1041 DEF_TPG_PARAM(AuthMethod);
1042 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1043
1044 DEF_TPG_PARAM(HeaderDigest);
1045 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1046
1047 DEF_TPG_PARAM(DataDigest);
1048 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1049
1050 DEF_TPG_PARAM(MaxConnections);
1051 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1052
1053 DEF_TPG_PARAM(TargetAlias);
1054 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1055
1056 DEF_TPG_PARAM(InitialR2T);
1057 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1058
1059 DEF_TPG_PARAM(ImmediateData);
1060 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1061
1062 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1063 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1064
1065 DEF_TPG_PARAM(MaxBurstLength);
1066 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1067
1068 DEF_TPG_PARAM(FirstBurstLength);
1069 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1070
1071 DEF_TPG_PARAM(DefaultTime2Wait);
1072 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1073
1074 DEF_TPG_PARAM(DefaultTime2Retain);
1075 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1076
1077 DEF_TPG_PARAM(MaxOutstandingR2T);
1078 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1079
1080 DEF_TPG_PARAM(DataPDUInOrder);
1081 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1082
1083 DEF_TPG_PARAM(DataSequenceInOrder);
1084 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1085
1086 DEF_TPG_PARAM(ErrorRecoveryLevel);
1087 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1088
1089 DEF_TPG_PARAM(IFMarker);
1090 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1091
1092 DEF_TPG_PARAM(OFMarker);
1093 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1094
1095 DEF_TPG_PARAM(IFMarkInt);
1096 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1097
1098 DEF_TPG_PARAM(OFMarkInt);
1099 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1100
1101 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1102         &iscsi_tpg_param_AuthMethod.attr,
1103         &iscsi_tpg_param_HeaderDigest.attr,
1104         &iscsi_tpg_param_DataDigest.attr,
1105         &iscsi_tpg_param_MaxConnections.attr,
1106         &iscsi_tpg_param_TargetAlias.attr,
1107         &iscsi_tpg_param_InitialR2T.attr,
1108         &iscsi_tpg_param_ImmediateData.attr,
1109         &iscsi_tpg_param_MaxRecvDataSegmentLength.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 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, (void *)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 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 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 = kzalloc(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 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         return cmd->init_task_tag;
1503 }
1504
1505 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1506 {
1507         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1508
1509         return cmd->i_state;
1510 }
1511
1512 static int iscsi_is_state_remove(struct se_cmd *se_cmd)
1513 {
1514         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1515
1516         return (cmd->i_state == ISTATE_REMOVE);
1517 }
1518
1519 static int lio_sess_logged_in(struct se_session *se_sess)
1520 {
1521         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1522         int ret;
1523         /*
1524          * Called with spin_lock_bh(&tpg_lock); and
1525          * spin_lock(&se_tpg->session_lock); held.
1526          */
1527         spin_lock(&sess->conn_lock);
1528         ret = (sess->session_state != TARG_SESS_STATE_LOGGED_IN);
1529         spin_unlock(&sess->conn_lock);
1530
1531         return ret;
1532 }
1533
1534 static u32 lio_sess_get_index(struct se_session *se_sess)
1535 {
1536         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1537
1538         return sess->session_index;
1539 }
1540
1541 static u32 lio_sess_get_initiator_sid(
1542         struct se_session *se_sess,
1543         unsigned char *buf,
1544         u32 size)
1545 {
1546         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1547         /*
1548          * iSCSI Initiator Session Identifier from RFC-3720.
1549          */
1550         return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1551                 sess->isid[0], sess->isid[1], sess->isid[2],
1552                 sess->isid[3], sess->isid[4], sess->isid[5]);
1553 }
1554
1555 static int lio_queue_data_in(struct se_cmd *se_cmd)
1556 {
1557         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1558
1559         cmd->i_state = ISTATE_SEND_DATAIN;
1560         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1561         return 0;
1562 }
1563
1564 static int lio_write_pending(struct se_cmd *se_cmd)
1565 {
1566         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1567
1568         if (!cmd->immediate_data && !cmd->unsolicited_data)
1569                 return iscsit_build_r2ts_for_cmd(cmd, cmd->conn, 1);
1570
1571         return 0;
1572 }
1573
1574 static int lio_write_pending_status(struct se_cmd *se_cmd)
1575 {
1576         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1577         int ret;
1578
1579         spin_lock_bh(&cmd->istate_lock);
1580         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1581         spin_unlock_bh(&cmd->istate_lock);
1582
1583         return ret;
1584 }
1585
1586 static int lio_queue_status(struct se_cmd *se_cmd)
1587 {
1588         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1589
1590         cmd->i_state = ISTATE_SEND_STATUS;
1591         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1592         return 0;
1593 }
1594
1595 static u16 lio_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
1596 {
1597         unsigned char *buffer = se_cmd->sense_buffer;
1598         /*
1599          * From RFC-3720 10.4.7.  Data Segment - Sense and Response Data Segment
1600          * 16-bit SenseLength.
1601          */
1602         buffer[0] = ((sense_length >> 8) & 0xff);
1603         buffer[1] = (sense_length & 0xff);
1604         /*
1605          * Return two byte offset into allocated sense_buffer.
1606          */
1607         return 2;
1608 }
1609
1610 static u16 lio_get_fabric_sense_len(void)
1611 {
1612         /*
1613          * Return two byte offset into allocated sense_buffer.
1614          */
1615         return 2;
1616 }
1617
1618 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1619 {
1620         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1621
1622         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1623         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1624         return 0;
1625 }
1626
1627 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1628 {
1629         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1630
1631         return &tpg->tpg_tiqn->tiqn[0];
1632 }
1633
1634 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1635 {
1636         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1637
1638         return tpg->tpgt;
1639 }
1640
1641 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1642 {
1643         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1644
1645         return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1646 }
1647
1648 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1649 {
1650         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1651
1652         return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1653 }
1654
1655 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1656 {
1657         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1658
1659         return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1660 }
1661
1662 static int lio_tpg_check_demo_mode_write_protect(
1663         struct se_portal_group *se_tpg)
1664 {
1665         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1666
1667         return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1668 }
1669
1670 static int lio_tpg_check_prod_mode_write_protect(
1671         struct se_portal_group *se_tpg)
1672 {
1673         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1674
1675         return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1676 }
1677
1678 static void lio_tpg_release_fabric_acl(
1679         struct se_portal_group *se_tpg,
1680         struct se_node_acl *se_acl)
1681 {
1682         struct iscsi_node_acl *acl = container_of(se_acl,
1683                                 struct iscsi_node_acl, se_node_acl);
1684         kfree(acl);
1685 }
1686
1687 /*
1688  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1689  *
1690  * Also, this function calls iscsit_inc_session_usage_count() on the
1691  * struct iscsi_session in question.
1692  */
1693 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1694 {
1695         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1696
1697         spin_lock(&sess->conn_lock);
1698         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1699             atomic_read(&sess->session_logout) ||
1700             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1701                 spin_unlock(&sess->conn_lock);
1702                 return 0;
1703         }
1704         atomic_set(&sess->session_reinstatement, 1);
1705         spin_unlock(&sess->conn_lock);
1706
1707         iscsit_inc_session_usage_count(sess);
1708         iscsit_stop_time2retain_timer(sess);
1709
1710         return 1;
1711 }
1712
1713 /*
1714  * Calls iscsit_dec_session_usage_count() as inverse of
1715  * lio_tpg_shutdown_session()
1716  */
1717 static void lio_tpg_close_session(struct se_session *se_sess)
1718 {
1719         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1720         /*
1721          * If the iSCSI Session for the iSCSI Initiator Node exists,
1722          * forcefully shutdown the iSCSI NEXUS.
1723          */
1724         iscsit_stop_session(sess, 1, 1);
1725         iscsit_dec_session_usage_count(sess);
1726         iscsit_close_session(sess);
1727 }
1728
1729 static void lio_tpg_stop_session(
1730         struct se_session *se_sess,
1731         int sess_sleep,
1732         int conn_sleep)
1733 {
1734         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1735
1736         iscsit_stop_session(sess, sess_sleep, conn_sleep);
1737 }
1738
1739 static void lio_tpg_fall_back_to_erl0(struct se_session *se_sess)
1740 {
1741         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1742
1743         iscsit_fall_back_to_erl0(sess);
1744 }
1745
1746 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1747 {
1748         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1749
1750         return tpg->tpg_tiqn->tiqn_index;
1751 }
1752
1753 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1754 {
1755         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1756                                 se_node_acl);
1757
1758         ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1759         iscsit_set_default_node_attribues(acl);
1760 }
1761
1762 static void lio_release_cmd(struct se_cmd *se_cmd)
1763 {
1764         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1765
1766         iscsit_release_cmd(cmd);
1767 }
1768
1769 /* End functions for target_core_fabric_ops */
1770
1771 int iscsi_target_register_configfs(void)
1772 {
1773         struct target_fabric_configfs *fabric;
1774         int ret;
1775
1776         lio_target_fabric_configfs = NULL;
1777         fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1778         if (IS_ERR(fabric)) {
1779                 pr_err("target_fabric_configfs_init() for"
1780                                 " LIO-Target failed!\n");
1781                 return PTR_ERR(fabric);
1782         }
1783         /*
1784          * Setup the fabric API of function pointers used by target_core_mod..
1785          */
1786         fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1787         fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1788         fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1789         fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1790         fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1791         fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1792         fabric->tf_ops.tpg_get_pr_transport_id_len =
1793                                 &iscsi_get_pr_transport_id_len;
1794         fabric->tf_ops.tpg_parse_pr_out_transport_id =
1795                                 &iscsi_parse_pr_out_transport_id;
1796         fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1797         fabric->tf_ops.tpg_check_demo_mode_cache =
1798                                 &lio_tpg_check_demo_mode_cache;
1799         fabric->tf_ops.tpg_check_demo_mode_write_protect =
1800                                 &lio_tpg_check_demo_mode_write_protect;
1801         fabric->tf_ops.tpg_check_prod_mode_write_protect =
1802                                 &lio_tpg_check_prod_mode_write_protect;
1803         fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1804         fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1805         fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1806         fabric->tf_ops.release_cmd = &lio_release_cmd;
1807         fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1808         fabric->tf_ops.close_session = &lio_tpg_close_session;
1809         fabric->tf_ops.stop_session = &lio_tpg_stop_session;
1810         fabric->tf_ops.fall_back_to_erl0 = &lio_tpg_fall_back_to_erl0;
1811         fabric->tf_ops.sess_logged_in = &lio_sess_logged_in;
1812         fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1813         fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1814         fabric->tf_ops.write_pending = &lio_write_pending;
1815         fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1816         fabric->tf_ops.set_default_node_attributes =
1817                                 &lio_set_default_node_attributes;
1818         fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1819         fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1820         fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1821         fabric->tf_ops.queue_status = &lio_queue_status;
1822         fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1823         fabric->tf_ops.set_fabric_sense_len = &lio_set_fabric_sense_len;
1824         fabric->tf_ops.get_fabric_sense_len = &lio_get_fabric_sense_len;
1825         fabric->tf_ops.is_state_remove = &iscsi_is_state_remove;
1826         /*
1827          * Setup function pointers for generic logic in target_core_fabric_configfs.c
1828          */
1829         fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1830         fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1831         fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1832         fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1833         fabric->tf_ops.fabric_post_link = NULL;
1834         fabric->tf_ops.fabric_pre_unlink = NULL;
1835         fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1836         fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1837         fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1838         fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1839         /*
1840          * Setup default attribute lists for various fabric->tf_cit_tmpl
1841          * sturct config_item_type's
1842          */
1843         TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1844         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1845         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1846         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1847         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1848         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1849         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1850         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1851         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1852         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
1853
1854         ret = target_fabric_configfs_register(fabric);
1855         if (ret < 0) {
1856                 pr_err("target_fabric_configfs_register() for"
1857                                 " LIO-Target failed!\n");
1858                 target_fabric_configfs_free(fabric);
1859                 return ret;
1860         }
1861
1862         lio_target_fabric_configfs = fabric;
1863         pr_debug("LIO_TARGET[0] - Set fabric ->"
1864                         " lio_target_fabric_configfs\n");
1865         return 0;
1866 }
1867
1868
1869 void iscsi_target_deregister_configfs(void)
1870 {
1871         if (!lio_target_fabric_configfs)
1872                 return;
1873         /*
1874          * Shutdown discovery sessions and disable discovery TPG
1875          */
1876         if (iscsit_global->discovery_tpg)
1877                 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
1878
1879         target_fabric_configfs_deregister(lio_target_fabric_configfs);
1880         lio_target_fabric_configfs = NULL;
1881         pr_debug("LIO_TARGET[0] - Cleared"
1882                                 " lio_target_fabric_configfs\n");
1883 }