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