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