target: Add missing mapped_lun bounds checking during make_mappedlun setup
[pandora-kernel.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         cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
820         /*
821          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
822          * when converting a NdoeACL from demo mode -> explict
823          */
824         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
825                                 name, cmdsn_depth);
826         if (IS_ERR(se_nacl))
827                 return se_nacl;
828
829         acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
830         stats_cg = &se_nacl->acl_fabric_stat_group;
831
832         stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
833                                 GFP_KERNEL);
834         if (!stats_cg->default_groups) {
835                 pr_err("Unable to allocate memory for"
836                                 " stats_cg->default_groups\n");
837                 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
838                 kfree(acl);
839                 return ERR_PTR(-ENOMEM);
840         }
841
842         stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
843         stats_cg->default_groups[1] = NULL;
844         config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
845                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
846
847         return se_nacl;
848 }
849
850 static void lio_target_drop_nodeacl(
851         struct se_node_acl *se_nacl)
852 {
853         struct se_portal_group *se_tpg = se_nacl->se_tpg;
854         struct iscsi_node_acl *acl = container_of(se_nacl,
855                         struct iscsi_node_acl, se_node_acl);
856         struct config_item *df_item;
857         struct config_group *stats_cg;
858         int i;
859
860         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
861         for (i = 0; stats_cg->default_groups[i]; i++) {
862                 df_item = &stats_cg->default_groups[i]->cg_item;
863                 stats_cg->default_groups[i] = NULL;
864                 config_item_put(df_item);
865         }
866         kfree(stats_cg->default_groups);
867
868         core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
869         kfree(acl);
870 }
871
872 /* End items for lio_target_acl_cit */
873
874 /* Start items for lio_target_tpg_attrib_cit */
875
876 #define DEF_TPG_ATTRIB(name)                                            \
877                                                                         \
878 static ssize_t iscsi_tpg_attrib_show_##name(                            \
879         struct se_portal_group *se_tpg,                         \
880         char *page)                                                     \
881 {                                                                       \
882         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
883                         struct iscsi_portal_group, tpg_se_tpg); \
884         ssize_t rb;                                                     \
885                                                                         \
886         if (iscsit_get_tpg(tpg) < 0)                                    \
887                 return -EINVAL;                                         \
888                                                                         \
889         rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name);        \
890         iscsit_put_tpg(tpg);                                            \
891         return rb;                                                      \
892 }                                                                       \
893                                                                         \
894 static ssize_t iscsi_tpg_attrib_store_##name(                           \
895         struct se_portal_group *se_tpg,                         \
896         const char *page,                                               \
897         size_t count)                                                   \
898 {                                                                       \
899         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
900                         struct iscsi_portal_group, tpg_se_tpg); \
901         char *endptr;                                                   \
902         u32 val;                                                        \
903         int ret;                                                        \
904                                                                         \
905         if (iscsit_get_tpg(tpg) < 0)                                    \
906                 return -EINVAL;                                         \
907                                                                         \
908         val = simple_strtoul(page, &endptr, 0);                         \
909         ret = iscsit_ta_##name(tpg, val);                               \
910         if (ret < 0)                                                    \
911                 goto out;                                               \
912                                                                         \
913         iscsit_put_tpg(tpg);                                            \
914         return count;                                                   \
915 out:                                                                    \
916         iscsit_put_tpg(tpg);                                            \
917         return ret;                                                     \
918 }
919
920 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
921
922 /*
923  * Define iscsi_tpg_attrib_s_authentication
924  */
925 DEF_TPG_ATTRIB(authentication);
926 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
927 /*
928  * Define iscsi_tpg_attrib_s_login_timeout
929  */
930 DEF_TPG_ATTRIB(login_timeout);
931 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
932 /*
933  * Define iscsi_tpg_attrib_s_netif_timeout
934  */
935 DEF_TPG_ATTRIB(netif_timeout);
936 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
937 /*
938  * Define iscsi_tpg_attrib_s_generate_node_acls
939  */
940 DEF_TPG_ATTRIB(generate_node_acls);
941 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
942 /*
943  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
944  */
945 DEF_TPG_ATTRIB(default_cmdsn_depth);
946 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
947 /*
948  Define iscsi_tpg_attrib_s_cache_dynamic_acls
949  */
950 DEF_TPG_ATTRIB(cache_dynamic_acls);
951 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
952 /*
953  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
954  */
955 DEF_TPG_ATTRIB(demo_mode_write_protect);
956 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
957 /*
958  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
959  */
960 DEF_TPG_ATTRIB(prod_mode_write_protect);
961 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
962
963 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
964         &iscsi_tpg_attrib_authentication.attr,
965         &iscsi_tpg_attrib_login_timeout.attr,
966         &iscsi_tpg_attrib_netif_timeout.attr,
967         &iscsi_tpg_attrib_generate_node_acls.attr,
968         &iscsi_tpg_attrib_default_cmdsn_depth.attr,
969         &iscsi_tpg_attrib_cache_dynamic_acls.attr,
970         &iscsi_tpg_attrib_demo_mode_write_protect.attr,
971         &iscsi_tpg_attrib_prod_mode_write_protect.attr,
972         NULL,
973 };
974
975 /* End items for lio_target_tpg_attrib_cit */
976
977 /* Start items for lio_target_tpg_param_cit */
978
979 #define DEF_TPG_PARAM(name)                                             \
980 static ssize_t iscsi_tpg_param_show_##name(                             \
981         struct se_portal_group *se_tpg,                                 \
982         char *page)                                                     \
983 {                                                                       \
984         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
985                         struct iscsi_portal_group, tpg_se_tpg);         \
986         struct iscsi_param *param;                                      \
987         ssize_t rb;                                                     \
988                                                                         \
989         if (iscsit_get_tpg(tpg) < 0)                                    \
990                 return -EINVAL;                                         \
991                                                                         \
992         param = iscsi_find_param_from_key(__stringify(name),            \
993                                 tpg->param_list);                       \
994         if (!param) {                                                   \
995                 iscsit_put_tpg(tpg);                                    \
996                 return -EINVAL;                                         \
997         }                                                               \
998         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
999                                                                         \
1000         iscsit_put_tpg(tpg);                                            \
1001         return rb;                                                      \
1002 }                                                                       \
1003 static ssize_t iscsi_tpg_param_store_##name(                            \
1004         struct se_portal_group *se_tpg,                         \
1005         const char *page,                                               \
1006         size_t count)                                                   \
1007 {                                                                       \
1008         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
1009                         struct iscsi_portal_group, tpg_se_tpg);         \
1010         char *buf;                                                      \
1011         int ret;                                                        \
1012                                                                         \
1013         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
1014         if (!buf)                                                       \
1015                 return -ENOMEM;                                         \
1016         snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);     \
1017         buf[strlen(buf)-1] = '\0'; /* Kill newline */                   \
1018                                                                         \
1019         if (iscsit_get_tpg(tpg) < 0) {                                  \
1020                 kfree(buf);                                             \
1021                 return -EINVAL;                                         \
1022         }                                                               \
1023                                                                         \
1024         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
1025         if (ret < 0)                                                    \
1026                 goto out;                                               \
1027                                                                         \
1028         kfree(buf);                                                     \
1029         iscsit_put_tpg(tpg);                                            \
1030         return count;                                                   \
1031 out:                                                                    \
1032         kfree(buf);                                                     \
1033         iscsit_put_tpg(tpg);                                            \
1034         return -EINVAL;                                         \
1035 }
1036
1037 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1038
1039 DEF_TPG_PARAM(AuthMethod);
1040 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1041
1042 DEF_TPG_PARAM(HeaderDigest);
1043 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1044
1045 DEF_TPG_PARAM(DataDigest);
1046 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1047
1048 DEF_TPG_PARAM(MaxConnections);
1049 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1050
1051 DEF_TPG_PARAM(TargetAlias);
1052 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1053
1054 DEF_TPG_PARAM(InitialR2T);
1055 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1056
1057 DEF_TPG_PARAM(ImmediateData);
1058 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1059
1060 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1061 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1062
1063 DEF_TPG_PARAM(MaxBurstLength);
1064 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1065
1066 DEF_TPG_PARAM(FirstBurstLength);
1067 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1068
1069 DEF_TPG_PARAM(DefaultTime2Wait);
1070 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1071
1072 DEF_TPG_PARAM(DefaultTime2Retain);
1073 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1074
1075 DEF_TPG_PARAM(MaxOutstandingR2T);
1076 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1077
1078 DEF_TPG_PARAM(DataPDUInOrder);
1079 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1080
1081 DEF_TPG_PARAM(DataSequenceInOrder);
1082 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1083
1084 DEF_TPG_PARAM(ErrorRecoveryLevel);
1085 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1086
1087 DEF_TPG_PARAM(IFMarker);
1088 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1089
1090 DEF_TPG_PARAM(OFMarker);
1091 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1092
1093 DEF_TPG_PARAM(IFMarkInt);
1094 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1095
1096 DEF_TPG_PARAM(OFMarkInt);
1097 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1098
1099 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1100         &iscsi_tpg_param_AuthMethod.attr,
1101         &iscsi_tpg_param_HeaderDigest.attr,
1102         &iscsi_tpg_param_DataDigest.attr,
1103         &iscsi_tpg_param_MaxConnections.attr,
1104         &iscsi_tpg_param_TargetAlias.attr,
1105         &iscsi_tpg_param_InitialR2T.attr,
1106         &iscsi_tpg_param_ImmediateData.attr,
1107         &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1108         &iscsi_tpg_param_MaxBurstLength.attr,
1109         &iscsi_tpg_param_FirstBurstLength.attr,
1110         &iscsi_tpg_param_DefaultTime2Wait.attr,
1111         &iscsi_tpg_param_DefaultTime2Retain.attr,
1112         &iscsi_tpg_param_MaxOutstandingR2T.attr,
1113         &iscsi_tpg_param_DataPDUInOrder.attr,
1114         &iscsi_tpg_param_DataSequenceInOrder.attr,
1115         &iscsi_tpg_param_ErrorRecoveryLevel.attr,
1116         &iscsi_tpg_param_IFMarker.attr,
1117         &iscsi_tpg_param_OFMarker.attr,
1118         &iscsi_tpg_param_IFMarkInt.attr,
1119         &iscsi_tpg_param_OFMarkInt.attr,
1120         NULL,
1121 };
1122
1123 /* End items for lio_target_tpg_param_cit */
1124
1125 /* Start items for lio_target_tpg_cit */
1126
1127 static ssize_t lio_target_tpg_show_enable(
1128         struct se_portal_group *se_tpg,
1129         char *page)
1130 {
1131         struct iscsi_portal_group *tpg = container_of(se_tpg,
1132                         struct iscsi_portal_group, tpg_se_tpg);
1133         ssize_t len;
1134
1135         spin_lock(&tpg->tpg_state_lock);
1136         len = sprintf(page, "%d\n",
1137                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1138         spin_unlock(&tpg->tpg_state_lock);
1139
1140         return len;
1141 }
1142
1143 static ssize_t lio_target_tpg_store_enable(
1144         struct se_portal_group *se_tpg,
1145         const char *page,
1146         size_t count)
1147 {
1148         struct iscsi_portal_group *tpg = container_of(se_tpg,
1149                         struct iscsi_portal_group, tpg_se_tpg);
1150         char *endptr;
1151         u32 op;
1152         int ret = 0;
1153
1154         op = simple_strtoul(page, &endptr, 0);
1155         if ((op != 1) && (op != 0)) {
1156                 pr_err("Illegal value for tpg_enable: %u\n", op);
1157                 return -EINVAL;
1158         }
1159
1160         ret = iscsit_get_tpg(tpg);
1161         if (ret < 0)
1162                 return -EINVAL;
1163
1164         if (op) {
1165                 ret = iscsit_tpg_enable_portal_group(tpg);
1166                 if (ret < 0)
1167                         goto out;
1168         } else {
1169                 /*
1170                  * iscsit_tpg_disable_portal_group() assumes force=1
1171                  */
1172                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1173                 if (ret < 0)
1174                         goto out;
1175         }
1176
1177         iscsit_put_tpg(tpg);
1178         return count;
1179 out:
1180         iscsit_put_tpg(tpg);
1181         return -EINVAL;
1182 }
1183
1184 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1185
1186 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1187         &lio_target_tpg_enable.attr,
1188         NULL,
1189 };
1190
1191 /* End items for lio_target_tpg_cit */
1192
1193 /* Start items for lio_target_tiqn_cit */
1194
1195 struct se_portal_group *lio_target_tiqn_addtpg(
1196         struct se_wwn *wwn,
1197         struct config_group *group,
1198         const char *name)
1199 {
1200         struct iscsi_portal_group *tpg;
1201         struct iscsi_tiqn *tiqn;
1202         char *tpgt_str, *end_ptr;
1203         int ret = 0;
1204         unsigned short int tpgt;
1205
1206         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1207         /*
1208          * Only tpgt_# directory groups can be created below
1209          * target/iscsi/iqn.superturodiskarry/
1210         */
1211         tpgt_str = strstr(name, "tpgt_");
1212         if (!tpgt_str) {
1213                 pr_err("Unable to locate \"tpgt_#\" directory"
1214                                 " group\n");
1215                 return NULL;
1216         }
1217         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1218         tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1219
1220         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1221         if (!tpg)
1222                 return NULL;
1223
1224         ret = core_tpg_register(
1225                         &lio_target_fabric_configfs->tf_ops,
1226                         wwn, &tpg->tpg_se_tpg, (void *)tpg,
1227                         TRANSPORT_TPG_TYPE_NORMAL);
1228         if (ret < 0)
1229                 return NULL;
1230
1231         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1232         if (ret != 0)
1233                 goto out;
1234
1235         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1236         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1237                         name);
1238         return &tpg->tpg_se_tpg;
1239 out:
1240         core_tpg_deregister(&tpg->tpg_se_tpg);
1241         kfree(tpg);
1242         return NULL;
1243 }
1244
1245 void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1246 {
1247         struct iscsi_portal_group *tpg;
1248         struct iscsi_tiqn *tiqn;
1249
1250         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1251         tiqn = tpg->tpg_tiqn;
1252         /*
1253          * iscsit_tpg_del_portal_group() assumes force=1
1254          */
1255         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1256         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1257 }
1258
1259 /* End items for lio_target_tiqn_cit */
1260
1261 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1262
1263 static ssize_t lio_target_wwn_show_attr_lio_version(
1264         struct target_fabric_configfs *tf,
1265         char *page)
1266 {
1267         return sprintf(page, "RisingTide Systems Linux-iSCSI Target "ISCSIT_VERSION"\n");
1268 }
1269
1270 TF_WWN_ATTR_RO(lio_target, lio_version);
1271
1272 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1273         &lio_target_wwn_lio_version.attr,
1274         NULL,
1275 };
1276
1277 struct se_wwn *lio_target_call_coreaddtiqn(
1278         struct target_fabric_configfs *tf,
1279         struct config_group *group,
1280         const char *name)
1281 {
1282         struct config_group *stats_cg;
1283         struct iscsi_tiqn *tiqn;
1284
1285         tiqn = iscsit_add_tiqn((unsigned char *)name);
1286         if (IS_ERR(tiqn))
1287                 return ERR_CAST(tiqn);
1288         /*
1289          * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1290          */
1291         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1292
1293         stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 6,
1294                                 GFP_KERNEL);
1295         if (!stats_cg->default_groups) {
1296                 pr_err("Unable to allocate memory for"
1297                                 " stats_cg->default_groups\n");
1298                 iscsit_del_tiqn(tiqn);
1299                 return ERR_PTR(-ENOMEM);
1300         }
1301
1302         stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
1303         stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
1304         stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
1305         stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
1306         stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
1307         stats_cg->default_groups[5] = NULL;
1308         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
1309                         "iscsi_instance", &iscsi_stat_instance_cit);
1310         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
1311                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1312         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
1313                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1314         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
1315                         "iscsi_login_stats", &iscsi_stat_login_cit);
1316         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
1317                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1318
1319         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1320         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1321                         " %s\n", name);
1322         return &tiqn->tiqn_wwn;
1323 }
1324
1325 void lio_target_call_coredeltiqn(
1326         struct se_wwn *wwn)
1327 {
1328         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1329         struct config_item *df_item;
1330         struct config_group *stats_cg;
1331         int i;
1332
1333         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1334         for (i = 0; stats_cg->default_groups[i]; i++) {
1335                 df_item = &stats_cg->default_groups[i]->cg_item;
1336                 stats_cg->default_groups[i] = NULL;
1337                 config_item_put(df_item);
1338         }
1339         kfree(stats_cg->default_groups);
1340
1341         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1342                         tiqn->tiqn);
1343         iscsit_del_tiqn(tiqn);
1344 }
1345
1346 /* End LIO-Target TIQN struct contig_lio_target_cit */
1347
1348 /* Start lio_target_discovery_auth_cit */
1349
1350 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1351         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1352 static ssize_t iscsi_disc_show_##name(                                  \
1353         struct target_fabric_configfs *tf,                              \
1354         char *page)                                                     \
1355 {                                                                       \
1356         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1357                 page);                                                  \
1358 }                                                                       \
1359 static ssize_t iscsi_disc_store_##name(                                 \
1360         struct target_fabric_configfs *tf,                              \
1361         const char *page,                                               \
1362         size_t count)                                                   \
1363 {                                                                       \
1364         return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
1365                 page, count);                                           \
1366 }
1367
1368 #define DEF_DISC_AUTH_INT(name)                                         \
1369         __DEF_NACL_AUTH_INT(disc, name)                                 \
1370 static ssize_t iscsi_disc_show_##name(                                  \
1371         struct target_fabric_configfs *tf,                              \
1372         char *page)                                                     \
1373 {                                                                       \
1374         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1375                         page);                                          \
1376 }
1377
1378 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1379 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1380
1381 /*
1382  * One-way authentication userid
1383  */
1384 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1385 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1386 /*
1387  * One-way authentication password
1388  */
1389 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1390 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1391 /*
1392  * Enforce mutual authentication
1393  */
1394 DEF_DISC_AUTH_INT(authenticate_target);
1395 DISC_AUTH_ATTR_RO(authenticate_target);
1396 /*
1397  * Mutual authentication userid
1398  */
1399 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1400 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1401 /*
1402  * Mutual authentication password
1403  */
1404 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1405 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1406
1407 /*
1408  * enforce_discovery_auth
1409  */
1410 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1411         struct target_fabric_configfs *tf,
1412         char *page)
1413 {
1414         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1415
1416         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1417 }
1418
1419 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1420         struct target_fabric_configfs *tf,
1421         const char *page,
1422         size_t count)
1423 {
1424         struct iscsi_param *param;
1425         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1426         char *endptr;
1427         u32 op;
1428
1429         op = simple_strtoul(page, &endptr, 0);
1430         if ((op != 1) && (op != 0)) {
1431                 pr_err("Illegal value for enforce_discovery_auth:"
1432                                 " %u\n", op);
1433                 return -EINVAL;
1434         }
1435
1436         if (!discovery_tpg) {
1437                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1438                 return -EINVAL;
1439         }
1440
1441         param = iscsi_find_param_from_key(AUTHMETHOD,
1442                                 discovery_tpg->param_list);
1443         if (!param)
1444                 return -EINVAL;
1445
1446         if (op) {
1447                 /*
1448                  * Reset the AuthMethod key to CHAP.
1449                  */
1450                 if (iscsi_update_param_value(param, CHAP) < 0)
1451                         return -EINVAL;
1452
1453                 discovery_tpg->tpg_attrib.authentication = 1;
1454                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1455                 pr_debug("LIO-CORE[0] Successfully enabled"
1456                         " authentication enforcement for iSCSI"
1457                         " Discovery TPG\n");
1458         } else {
1459                 /*
1460                  * Reset the AuthMethod key to CHAP,None
1461                  */
1462                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1463                         return -EINVAL;
1464
1465                 discovery_tpg->tpg_attrib.authentication = 0;
1466                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1467                 pr_debug("LIO-CORE[0] Successfully disabled"
1468                         " authentication enforcement for iSCSI"
1469                         " Discovery TPG\n");
1470         }
1471
1472         return count;
1473 }
1474
1475 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1476
1477 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1478         &iscsi_disc_userid.attr,
1479         &iscsi_disc_password.attr,
1480         &iscsi_disc_authenticate_target.attr,
1481         &iscsi_disc_userid_mutual.attr,
1482         &iscsi_disc_password_mutual.attr,
1483         &iscsi_disc_enforce_discovery_auth.attr,
1484         NULL,
1485 };
1486
1487 /* End lio_target_discovery_auth_cit */
1488
1489 /* Start functions for target_core_fabric_ops */
1490
1491 static char *iscsi_get_fabric_name(void)
1492 {
1493         return "iSCSI";
1494 }
1495
1496 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1497 {
1498         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1499
1500         return cmd->init_task_tag;
1501 }
1502
1503 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1504 {
1505         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1506
1507         return cmd->i_state;
1508 }
1509
1510 static int iscsi_is_state_remove(struct se_cmd *se_cmd)
1511 {
1512         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1513
1514         return (cmd->i_state == ISTATE_REMOVE);
1515 }
1516
1517 static int lio_sess_logged_in(struct se_session *se_sess)
1518 {
1519         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1520         int ret;
1521         /*
1522          * Called with spin_lock_bh(&tpg_lock); and
1523          * spin_lock(&se_tpg->session_lock); held.
1524          */
1525         spin_lock(&sess->conn_lock);
1526         ret = (sess->session_state != TARG_SESS_STATE_LOGGED_IN);
1527         spin_unlock(&sess->conn_lock);
1528
1529         return ret;
1530 }
1531
1532 static u32 lio_sess_get_index(struct se_session *se_sess)
1533 {
1534         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1535
1536         return sess->session_index;
1537 }
1538
1539 static u32 lio_sess_get_initiator_sid(
1540         struct se_session *se_sess,
1541         unsigned char *buf,
1542         u32 size)
1543 {
1544         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1545         /*
1546          * iSCSI Initiator Session Identifier from RFC-3720.
1547          */
1548         return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1549                 sess->isid[0], sess->isid[1], sess->isid[2],
1550                 sess->isid[3], sess->isid[4], sess->isid[5]);
1551 }
1552
1553 static int lio_queue_data_in(struct se_cmd *se_cmd)
1554 {
1555         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1556
1557         cmd->i_state = ISTATE_SEND_DATAIN;
1558         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1559         return 0;
1560 }
1561
1562 static int lio_write_pending(struct se_cmd *se_cmd)
1563 {
1564         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1565
1566         if (!cmd->immediate_data && !cmd->unsolicited_data)
1567                 return iscsit_build_r2ts_for_cmd(cmd, cmd->conn, 1);
1568
1569         return 0;
1570 }
1571
1572 static int lio_write_pending_status(struct se_cmd *se_cmd)
1573 {
1574         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1575         int ret;
1576
1577         spin_lock_bh(&cmd->istate_lock);
1578         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1579         spin_unlock_bh(&cmd->istate_lock);
1580
1581         return ret;
1582 }
1583
1584 static int lio_queue_status(struct se_cmd *se_cmd)
1585 {
1586         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1587
1588         cmd->i_state = ISTATE_SEND_STATUS;
1589         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1590         return 0;
1591 }
1592
1593 static u16 lio_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
1594 {
1595         unsigned char *buffer = se_cmd->sense_buffer;
1596         /*
1597          * From RFC-3720 10.4.7.  Data Segment - Sense and Response Data Segment
1598          * 16-bit SenseLength.
1599          */
1600         buffer[0] = ((sense_length >> 8) & 0xff);
1601         buffer[1] = (sense_length & 0xff);
1602         /*
1603          * Return two byte offset into allocated sense_buffer.
1604          */
1605         return 2;
1606 }
1607
1608 static u16 lio_get_fabric_sense_len(void)
1609 {
1610         /*
1611          * Return two byte offset into allocated sense_buffer.
1612          */
1613         return 2;
1614 }
1615
1616 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1617 {
1618         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1619
1620         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1621         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1622         return 0;
1623 }
1624
1625 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1626 {
1627         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1628
1629         return &tpg->tpg_tiqn->tiqn[0];
1630 }
1631
1632 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1633 {
1634         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1635
1636         return tpg->tpgt;
1637 }
1638
1639 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1640 {
1641         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1642
1643         return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1644 }
1645
1646 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1647 {
1648         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1649
1650         return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1651 }
1652
1653 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1654 {
1655         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1656
1657         return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1658 }
1659
1660 static int lio_tpg_check_demo_mode_write_protect(
1661         struct se_portal_group *se_tpg)
1662 {
1663         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1664
1665         return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1666 }
1667
1668 static int lio_tpg_check_prod_mode_write_protect(
1669         struct se_portal_group *se_tpg)
1670 {
1671         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1672
1673         return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1674 }
1675
1676 static void lio_tpg_release_fabric_acl(
1677         struct se_portal_group *se_tpg,
1678         struct se_node_acl *se_acl)
1679 {
1680         struct iscsi_node_acl *acl = container_of(se_acl,
1681                                 struct iscsi_node_acl, se_node_acl);
1682         kfree(acl);
1683 }
1684
1685 /*
1686  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1687  *
1688  * Also, this function calls iscsit_inc_session_usage_count() on the
1689  * struct iscsi_session in question.
1690  */
1691 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1692 {
1693         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1694
1695         spin_lock(&sess->conn_lock);
1696         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1697             atomic_read(&sess->session_logout) ||
1698             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1699                 spin_unlock(&sess->conn_lock);
1700                 return 0;
1701         }
1702         atomic_set(&sess->session_reinstatement, 1);
1703         spin_unlock(&sess->conn_lock);
1704
1705         iscsit_inc_session_usage_count(sess);
1706         iscsit_stop_time2retain_timer(sess);
1707
1708         return 1;
1709 }
1710
1711 /*
1712  * Calls iscsit_dec_session_usage_count() as inverse of
1713  * lio_tpg_shutdown_session()
1714  */
1715 static void lio_tpg_close_session(struct se_session *se_sess)
1716 {
1717         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1718         /*
1719          * If the iSCSI Session for the iSCSI Initiator Node exists,
1720          * forcefully shutdown the iSCSI NEXUS.
1721          */
1722         iscsit_stop_session(sess, 1, 1);
1723         iscsit_dec_session_usage_count(sess);
1724         iscsit_close_session(sess);
1725 }
1726
1727 static void lio_tpg_stop_session(
1728         struct se_session *se_sess,
1729         int sess_sleep,
1730         int conn_sleep)
1731 {
1732         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1733
1734         iscsit_stop_session(sess, sess_sleep, conn_sleep);
1735 }
1736
1737 static void lio_tpg_fall_back_to_erl0(struct se_session *se_sess)
1738 {
1739         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1740
1741         iscsit_fall_back_to_erl0(sess);
1742 }
1743
1744 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1745 {
1746         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1747
1748         return tpg->tpg_tiqn->tiqn_index;
1749 }
1750
1751 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1752 {
1753         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1754                                 se_node_acl);
1755
1756         ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1757         iscsit_set_default_node_attribues(acl);
1758 }
1759
1760 static void lio_release_cmd(struct se_cmd *se_cmd)
1761 {
1762         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1763
1764         iscsit_release_cmd(cmd);
1765 }
1766
1767 /* End functions for target_core_fabric_ops */
1768
1769 int iscsi_target_register_configfs(void)
1770 {
1771         struct target_fabric_configfs *fabric;
1772         int ret;
1773
1774         lio_target_fabric_configfs = NULL;
1775         fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1776         if (IS_ERR(fabric)) {
1777                 pr_err("target_fabric_configfs_init() for"
1778                                 " LIO-Target failed!\n");
1779                 return PTR_ERR(fabric);
1780         }
1781         /*
1782          * Setup the fabric API of function pointers used by target_core_mod..
1783          */
1784         fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1785         fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1786         fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1787         fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1788         fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1789         fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1790         fabric->tf_ops.tpg_get_pr_transport_id_len =
1791                                 &iscsi_get_pr_transport_id_len;
1792         fabric->tf_ops.tpg_parse_pr_out_transport_id =
1793                                 &iscsi_parse_pr_out_transport_id;
1794         fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1795         fabric->tf_ops.tpg_check_demo_mode_cache =
1796                                 &lio_tpg_check_demo_mode_cache;
1797         fabric->tf_ops.tpg_check_demo_mode_write_protect =
1798                                 &lio_tpg_check_demo_mode_write_protect;
1799         fabric->tf_ops.tpg_check_prod_mode_write_protect =
1800                                 &lio_tpg_check_prod_mode_write_protect;
1801         fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1802         fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1803         fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1804         fabric->tf_ops.release_cmd = &lio_release_cmd;
1805         fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1806         fabric->tf_ops.close_session = &lio_tpg_close_session;
1807         fabric->tf_ops.stop_session = &lio_tpg_stop_session;
1808         fabric->tf_ops.fall_back_to_erl0 = &lio_tpg_fall_back_to_erl0;
1809         fabric->tf_ops.sess_logged_in = &lio_sess_logged_in;
1810         fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1811         fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1812         fabric->tf_ops.write_pending = &lio_write_pending;
1813         fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1814         fabric->tf_ops.set_default_node_attributes =
1815                                 &lio_set_default_node_attributes;
1816         fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1817         fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1818         fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1819         fabric->tf_ops.queue_status = &lio_queue_status;
1820         fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1821         fabric->tf_ops.set_fabric_sense_len = &lio_set_fabric_sense_len;
1822         fabric->tf_ops.get_fabric_sense_len = &lio_get_fabric_sense_len;
1823         fabric->tf_ops.is_state_remove = &iscsi_is_state_remove;
1824         /*
1825          * Setup function pointers for generic logic in target_core_fabric_configfs.c
1826          */
1827         fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1828         fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1829         fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1830         fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1831         fabric->tf_ops.fabric_post_link = NULL;
1832         fabric->tf_ops.fabric_pre_unlink = NULL;
1833         fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1834         fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1835         fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1836         fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1837         /*
1838          * Setup default attribute lists for various fabric->tf_cit_tmpl
1839          * sturct config_item_type's
1840          */
1841         TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1842         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1843         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1844         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1845         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1846         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1847         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1848         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1849         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1850         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
1851
1852         ret = target_fabric_configfs_register(fabric);
1853         if (ret < 0) {
1854                 pr_err("target_fabric_configfs_register() for"
1855                                 " LIO-Target failed!\n");
1856                 target_fabric_configfs_free(fabric);
1857                 return ret;
1858         }
1859
1860         lio_target_fabric_configfs = fabric;
1861         pr_debug("LIO_TARGET[0] - Set fabric ->"
1862                         " lio_target_fabric_configfs\n");
1863         return 0;
1864 }
1865
1866
1867 void iscsi_target_deregister_configfs(void)
1868 {
1869         if (!lio_target_fabric_configfs)
1870                 return;
1871         /*
1872          * Shutdown discovery sessions and disable discovery TPG
1873          */
1874         if (iscsit_global->discovery_tpg)
1875                 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
1876
1877         target_fabric_configfs_deregister(lio_target_fabric_configfs);
1878         lio_target_fabric_configfs = NULL;
1879         pr_debug("LIO_TARGET[0] - Cleared"
1880                                 " lio_target_fabric_configfs\n");
1881 }