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