iser-target: Fix multi network portal shutdown regression
[firefly-linux-kernel-4.4.55.git] / drivers / target / iscsi / iscsi_target_tpg.c
1 /*******************************************************************************
2  * This file contains iSCSI Target Portal Group related functions.
3  *
4  * (c) Copyright 2007-2013 Datera, Inc.
5  *
6  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  ******************************************************************************/
18
19 #include <target/target_core_base.h>
20 #include <target/target_core_fabric.h>
21 #include <target/target_core_configfs.h>
22
23 #include "iscsi_target_core.h"
24 #include "iscsi_target_erl0.h"
25 #include "iscsi_target_login.h"
26 #include "iscsi_target_nodeattrib.h"
27 #include "iscsi_target_tpg.h"
28 #include "iscsi_target_util.h"
29 #include "iscsi_target.h"
30 #include "iscsi_target_parameters.h"
31
32 #include <target/iscsi/iscsi_transport.h>
33
34 struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
35 {
36         struct iscsi_portal_group *tpg;
37
38         tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
39         if (!tpg) {
40                 pr_err("Unable to allocate struct iscsi_portal_group\n");
41                 return NULL;
42         }
43
44         tpg->tpgt = tpgt;
45         tpg->tpg_state = TPG_STATE_FREE;
46         tpg->tpg_tiqn = tiqn;
47         INIT_LIST_HEAD(&tpg->tpg_gnp_list);
48         INIT_LIST_HEAD(&tpg->tpg_list);
49         mutex_init(&tpg->tpg_access_lock);
50         sema_init(&tpg->np_login_sem, 1);
51         spin_lock_init(&tpg->tpg_state_lock);
52         spin_lock_init(&tpg->tpg_np_lock);
53
54         return tpg;
55 }
56
57 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
58
59 int iscsit_load_discovery_tpg(void)
60 {
61         struct iscsi_param *param;
62         struct iscsi_portal_group *tpg;
63         int ret;
64
65         tpg = iscsit_alloc_portal_group(NULL, 1);
66         if (!tpg) {
67                 pr_err("Unable to allocate struct iscsi_portal_group\n");
68                 return -1;
69         }
70
71         ret = core_tpg_register(
72                         &lio_target_fabric_configfs->tf_ops,
73                         NULL, &tpg->tpg_se_tpg, tpg,
74                         TRANSPORT_TPG_TYPE_DISCOVERY);
75         if (ret < 0) {
76                 kfree(tpg);
77                 return -1;
78         }
79
80         tpg->sid = 1; /* First Assigned LIO Session ID */
81         iscsit_set_default_tpg_attribs(tpg);
82
83         if (iscsi_create_default_params(&tpg->param_list) < 0)
84                 goto out;
85         /*
86          * By default we disable authentication for discovery sessions,
87          * this can be changed with:
88          *
89          * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
90          */
91         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
92         if (!param)
93                 goto out;
94
95         if (iscsi_update_param_value(param, "CHAP,None") < 0)
96                 goto out;
97
98         tpg->tpg_attrib.authentication = 0;
99
100         spin_lock(&tpg->tpg_state_lock);
101         tpg->tpg_state  = TPG_STATE_ACTIVE;
102         spin_unlock(&tpg->tpg_state_lock);
103
104         iscsit_global->discovery_tpg = tpg;
105         pr_debug("CORE[0] - Allocated Discovery TPG\n");
106
107         return 0;
108 out:
109         if (tpg->sid == 1)
110                 core_tpg_deregister(&tpg->tpg_se_tpg);
111         kfree(tpg);
112         return -1;
113 }
114
115 void iscsit_release_discovery_tpg(void)
116 {
117         struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
118
119         if (!tpg)
120                 return;
121
122         core_tpg_deregister(&tpg->tpg_se_tpg);
123
124         kfree(tpg);
125         iscsit_global->discovery_tpg = NULL;
126 }
127
128 struct iscsi_portal_group *iscsit_get_tpg_from_np(
129         struct iscsi_tiqn *tiqn,
130         struct iscsi_np *np,
131         struct iscsi_tpg_np **tpg_np_out)
132 {
133         struct iscsi_portal_group *tpg = NULL;
134         struct iscsi_tpg_np *tpg_np;
135
136         spin_lock(&tiqn->tiqn_tpg_lock);
137         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
138
139                 spin_lock(&tpg->tpg_state_lock);
140                 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
141                         spin_unlock(&tpg->tpg_state_lock);
142                         continue;
143                 }
144                 spin_unlock(&tpg->tpg_state_lock);
145
146                 spin_lock(&tpg->tpg_np_lock);
147                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
148                         if (tpg_np->tpg_np == np) {
149                                 *tpg_np_out = tpg_np;
150                                 kref_get(&tpg_np->tpg_np_kref);
151                                 spin_unlock(&tpg->tpg_np_lock);
152                                 spin_unlock(&tiqn->tiqn_tpg_lock);
153                                 return tpg;
154                         }
155                 }
156                 spin_unlock(&tpg->tpg_np_lock);
157         }
158         spin_unlock(&tiqn->tiqn_tpg_lock);
159
160         return NULL;
161 }
162
163 int iscsit_get_tpg(
164         struct iscsi_portal_group *tpg)
165 {
166         int ret;
167
168         ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
169         return ((ret != 0) || signal_pending(current)) ? -1 : 0;
170 }
171
172 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
173 {
174         mutex_unlock(&tpg->tpg_access_lock);
175 }
176
177 static void iscsit_clear_tpg_np_login_thread(
178         struct iscsi_tpg_np *tpg_np,
179         struct iscsi_portal_group *tpg,
180         bool shutdown)
181 {
182         if (!tpg_np->tpg_np) {
183                 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
184                 return;
185         }
186
187         if (shutdown)
188                 tpg_np->tpg_np->enabled = false;
189         iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
190 }
191
192 void iscsit_clear_tpg_np_login_threads(
193         struct iscsi_portal_group *tpg,
194         bool shutdown)
195 {
196         struct iscsi_tpg_np *tpg_np;
197
198         spin_lock(&tpg->tpg_np_lock);
199         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
200                 if (!tpg_np->tpg_np) {
201                         pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
202                         continue;
203                 }
204                 spin_unlock(&tpg->tpg_np_lock);
205                 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
206                 spin_lock(&tpg->tpg_np_lock);
207         }
208         spin_unlock(&tpg->tpg_np_lock);
209 }
210
211 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
212 {
213         iscsi_print_params(tpg->param_list);
214 }
215
216 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
217 {
218         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
219
220         a->authentication = TA_AUTHENTICATION;
221         a->login_timeout = TA_LOGIN_TIMEOUT;
222         a->netif_timeout = TA_NETIF_TIMEOUT;
223         a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
224         a->generate_node_acls = TA_GENERATE_NODE_ACLS;
225         a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
226         a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
227         a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
228         a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
229         a->default_erl = TA_DEFAULT_ERL;
230         a->t10_pi = TA_DEFAULT_T10_PI;
231 }
232
233 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
234 {
235         if (tpg->tpg_state != TPG_STATE_FREE) {
236                 pr_err("Unable to add iSCSI Target Portal Group: %d"
237                         " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
238                 return -EEXIST;
239         }
240         iscsit_set_default_tpg_attribs(tpg);
241
242         if (iscsi_create_default_params(&tpg->param_list) < 0)
243                 goto err_out;
244
245         tpg->tpg_attrib.tpg = tpg;
246
247         spin_lock(&tpg->tpg_state_lock);
248         tpg->tpg_state  = TPG_STATE_INACTIVE;
249         spin_unlock(&tpg->tpg_state_lock);
250
251         spin_lock(&tiqn->tiqn_tpg_lock);
252         list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
253         tiqn->tiqn_ntpgs++;
254         pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
255                         tiqn->tiqn, tpg->tpgt);
256         spin_unlock(&tiqn->tiqn_tpg_lock);
257
258         return 0;
259 err_out:
260         if (tpg->param_list) {
261                 iscsi_release_param_list(tpg->param_list);
262                 tpg->param_list = NULL;
263         }
264         kfree(tpg);
265         return -ENOMEM;
266 }
267
268 int iscsit_tpg_del_portal_group(
269         struct iscsi_tiqn *tiqn,
270         struct iscsi_portal_group *tpg,
271         int force)
272 {
273         u8 old_state = tpg->tpg_state;
274
275         spin_lock(&tpg->tpg_state_lock);
276         tpg->tpg_state = TPG_STATE_INACTIVE;
277         spin_unlock(&tpg->tpg_state_lock);
278
279         iscsit_clear_tpg_np_login_threads(tpg, true);
280
281         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
282                 pr_err("Unable to delete iSCSI Target Portal Group:"
283                         " %hu while active sessions exist, and force=0\n",
284                         tpg->tpgt);
285                 tpg->tpg_state = old_state;
286                 return -EPERM;
287         }
288
289         core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
290
291         if (tpg->param_list) {
292                 iscsi_release_param_list(tpg->param_list);
293                 tpg->param_list = NULL;
294         }
295
296         core_tpg_deregister(&tpg->tpg_se_tpg);
297
298         spin_lock(&tpg->tpg_state_lock);
299         tpg->tpg_state = TPG_STATE_FREE;
300         spin_unlock(&tpg->tpg_state_lock);
301
302         spin_lock(&tiqn->tiqn_tpg_lock);
303         tiqn->tiqn_ntpgs--;
304         list_del(&tpg->tpg_list);
305         spin_unlock(&tiqn->tiqn_tpg_lock);
306
307         pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
308                         tiqn->tiqn, tpg->tpgt);
309
310         kfree(tpg);
311         return 0;
312 }
313
314 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
315 {
316         struct iscsi_param *param;
317         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
318         int ret;
319
320         spin_lock(&tpg->tpg_state_lock);
321         if (tpg->tpg_state == TPG_STATE_ACTIVE) {
322                 pr_err("iSCSI target portal group: %hu is already"
323                         " active, ignoring request.\n", tpg->tpgt);
324                 spin_unlock(&tpg->tpg_state_lock);
325                 return -EINVAL;
326         }
327         /*
328          * Make sure that AuthMethod does not contain None as an option
329          * unless explictly disabled.  Set the default to CHAP if authentication
330          * is enforced (as per default), and remove the NONE option.
331          */
332         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
333         if (!param) {
334                 spin_unlock(&tpg->tpg_state_lock);
335                 return -EINVAL;
336         }
337
338         if (tpg->tpg_attrib.authentication) {
339                 if (!strcmp(param->value, NONE)) {
340                         ret = iscsi_update_param_value(param, CHAP);
341                         if (ret)
342                                 goto err;
343                 }
344
345                 ret = iscsit_ta_authentication(tpg, 1);
346                 if (ret < 0)
347                         goto err;
348         }
349
350         tpg->tpg_state = TPG_STATE_ACTIVE;
351         spin_unlock(&tpg->tpg_state_lock);
352
353         spin_lock(&tiqn->tiqn_tpg_lock);
354         tiqn->tiqn_active_tpgs++;
355         pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
356                         tpg->tpgt);
357         spin_unlock(&tiqn->tiqn_tpg_lock);
358
359         return 0;
360
361 err:
362         spin_unlock(&tpg->tpg_state_lock);
363         return ret;
364 }
365
366 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
367 {
368         struct iscsi_tiqn *tiqn;
369         u8 old_state = tpg->tpg_state;
370
371         spin_lock(&tpg->tpg_state_lock);
372         if (tpg->tpg_state == TPG_STATE_INACTIVE) {
373                 pr_err("iSCSI Target Portal Group: %hu is already"
374                         " inactive, ignoring request.\n", tpg->tpgt);
375                 spin_unlock(&tpg->tpg_state_lock);
376                 return -EINVAL;
377         }
378         tpg->tpg_state = TPG_STATE_INACTIVE;
379         spin_unlock(&tpg->tpg_state_lock);
380
381         iscsit_clear_tpg_np_login_threads(tpg, false);
382
383         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
384                 spin_lock(&tpg->tpg_state_lock);
385                 tpg->tpg_state = old_state;
386                 spin_unlock(&tpg->tpg_state_lock);
387                 pr_err("Unable to disable iSCSI Target Portal Group:"
388                         " %hu while active sessions exist, and force=0\n",
389                         tpg->tpgt);
390                 return -EPERM;
391         }
392
393         tiqn = tpg->tpg_tiqn;
394         if (!tiqn || (tpg == iscsit_global->discovery_tpg))
395                 return 0;
396
397         spin_lock(&tiqn->tiqn_tpg_lock);
398         tiqn->tiqn_active_tpgs--;
399         pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
400                         tpg->tpgt);
401         spin_unlock(&tiqn->tiqn_tpg_lock);
402
403         return 0;
404 }
405
406 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
407         struct iscsi_session *sess)
408 {
409         struct se_session *se_sess = sess->se_sess;
410         struct se_node_acl *se_nacl = se_sess->se_node_acl;
411         struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
412                                         se_node_acl);
413
414         return &acl->node_attrib;
415 }
416
417 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
418         struct iscsi_tpg_np *tpg_np,
419         int network_transport)
420 {
421         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
422
423         spin_lock(&tpg_np->tpg_np_parent_lock);
424         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
425                         &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
426                 if (tpg_np_child->tpg_np->np_network_transport ==
427                                 network_transport) {
428                         spin_unlock(&tpg_np->tpg_np_parent_lock);
429                         return tpg_np_child;
430                 }
431         }
432         spin_unlock(&tpg_np->tpg_np_parent_lock);
433
434         return NULL;
435 }
436
437 static bool iscsit_tpg_check_network_portal(
438         struct iscsi_tiqn *tiqn,
439         struct __kernel_sockaddr_storage *sockaddr,
440         int network_transport)
441 {
442         struct iscsi_portal_group *tpg;
443         struct iscsi_tpg_np *tpg_np;
444         struct iscsi_np *np;
445         bool match = false;
446
447         spin_lock(&tiqn->tiqn_tpg_lock);
448         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
449
450                 spin_lock(&tpg->tpg_np_lock);
451                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
452                         np = tpg_np->tpg_np;
453
454                         match = iscsit_check_np_match(sockaddr, np,
455                                                 network_transport);
456                         if (match == true)
457                                 break;
458                 }
459                 spin_unlock(&tpg->tpg_np_lock);
460         }
461         spin_unlock(&tiqn->tiqn_tpg_lock);
462
463         return match;
464 }
465
466 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
467         struct iscsi_portal_group *tpg,
468         struct __kernel_sockaddr_storage *sockaddr,
469         char *ip_str,
470         struct iscsi_tpg_np *tpg_np_parent,
471         int network_transport)
472 {
473         struct iscsi_np *np;
474         struct iscsi_tpg_np *tpg_np;
475
476         if (!tpg_np_parent) {
477                 if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
478                                 network_transport) == true) {
479                         pr_err("Network Portal: %s already exists on a"
480                                 " different TPG on %s\n", ip_str,
481                                 tpg->tpg_tiqn->tiqn);
482                         return ERR_PTR(-EEXIST);
483                 }
484         }
485
486         tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
487         if (!tpg_np) {
488                 pr_err("Unable to allocate memory for"
489                                 " struct iscsi_tpg_np.\n");
490                 return ERR_PTR(-ENOMEM);
491         }
492
493         np = iscsit_add_np(sockaddr, ip_str, network_transport);
494         if (IS_ERR(np)) {
495                 kfree(tpg_np);
496                 return ERR_CAST(np);
497         }
498
499         INIT_LIST_HEAD(&tpg_np->tpg_np_list);
500         INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
501         INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
502         spin_lock_init(&tpg_np->tpg_np_parent_lock);
503         init_completion(&tpg_np->tpg_np_comp);
504         kref_init(&tpg_np->tpg_np_kref);
505         tpg_np->tpg_np          = np;
506         np->tpg_np              = tpg_np;
507         tpg_np->tpg             = tpg;
508
509         spin_lock(&tpg->tpg_np_lock);
510         list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
511         tpg->num_tpg_nps++;
512         if (tpg->tpg_tiqn)
513                 tpg->tpg_tiqn->tiqn_num_tpg_nps++;
514         spin_unlock(&tpg->tpg_np_lock);
515
516         if (tpg_np_parent) {
517                 tpg_np->tpg_np_parent = tpg_np_parent;
518                 spin_lock(&tpg_np_parent->tpg_np_parent_lock);
519                 list_add_tail(&tpg_np->tpg_np_child_list,
520                         &tpg_np_parent->tpg_np_parent_list);
521                 spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
522         }
523
524         pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
525                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
526                 np->np_transport->name);
527
528         return tpg_np;
529 }
530
531 static int iscsit_tpg_release_np(
532         struct iscsi_tpg_np *tpg_np,
533         struct iscsi_portal_group *tpg,
534         struct iscsi_np *np)
535 {
536         iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
537
538         pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
539                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
540                 np->np_transport->name);
541
542         tpg_np->tpg_np = NULL;
543         tpg_np->tpg = NULL;
544         kfree(tpg_np);
545         /*
546          * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
547          */
548         return iscsit_del_np(np);
549 }
550
551 int iscsit_tpg_del_network_portal(
552         struct iscsi_portal_group *tpg,
553         struct iscsi_tpg_np *tpg_np)
554 {
555         struct iscsi_np *np;
556         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
557         int ret = 0;
558
559         np = tpg_np->tpg_np;
560         if (!np) {
561                 pr_err("Unable to locate struct iscsi_np from"
562                                 " struct iscsi_tpg_np\n");
563                 return -EINVAL;
564         }
565
566         if (!tpg_np->tpg_np_parent) {
567                 /*
568                  * We are the parent tpg network portal.  Release all of the
569                  * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
570                  * list first.
571                  */
572                 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
573                                 &tpg_np->tpg_np_parent_list,
574                                 tpg_np_child_list) {
575                         ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
576                         if (ret < 0)
577                                 pr_err("iscsit_tpg_del_network_portal()"
578                                         " failed: %d\n", ret);
579                 }
580         } else {
581                 /*
582                  * We are not the parent ISCSI_TCP tpg network portal.  Release
583                  * our own network portals from the child list.
584                  */
585                 spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
586                 list_del(&tpg_np->tpg_np_child_list);
587                 spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
588         }
589
590         spin_lock(&tpg->tpg_np_lock);
591         list_del(&tpg_np->tpg_np_list);
592         tpg->num_tpg_nps--;
593         if (tpg->tpg_tiqn)
594                 tpg->tpg_tiqn->tiqn_num_tpg_nps--;
595         spin_unlock(&tpg->tpg_np_lock);
596
597         return iscsit_tpg_release_np(tpg_np, tpg, np);
598 }
599
600 int iscsit_tpg_set_initiator_node_queue_depth(
601         struct iscsi_portal_group *tpg,
602         unsigned char *initiatorname,
603         u32 queue_depth,
604         int force)
605 {
606         return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
607                 initiatorname, queue_depth, force);
608 }
609
610 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
611 {
612         unsigned char buf1[256], buf2[256], *none = NULL;
613         int len;
614         struct iscsi_param *param;
615         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
616
617         if ((authentication != 1) && (authentication != 0)) {
618                 pr_err("Illegal value for authentication parameter:"
619                         " %u, ignoring request.\n", authentication);
620                 return -EINVAL;
621         }
622
623         memset(buf1, 0, sizeof(buf1));
624         memset(buf2, 0, sizeof(buf2));
625
626         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
627         if (!param)
628                 return -EINVAL;
629
630         if (authentication) {
631                 snprintf(buf1, sizeof(buf1), "%s", param->value);
632                 none = strstr(buf1, NONE);
633                 if (!none)
634                         goto out;
635                 if (!strncmp(none + 4, ",", 1)) {
636                         if (!strcmp(buf1, none))
637                                 sprintf(buf2, "%s", none+5);
638                         else {
639                                 none--;
640                                 *none = '\0';
641                                 len = sprintf(buf2, "%s", buf1);
642                                 none += 5;
643                                 sprintf(buf2 + len, "%s", none);
644                         }
645                 } else {
646                         none--;
647                         *none = '\0';
648                         sprintf(buf2, "%s", buf1);
649                 }
650                 if (iscsi_update_param_value(param, buf2) < 0)
651                         return -EINVAL;
652         } else {
653                 snprintf(buf1, sizeof(buf1), "%s", param->value);
654                 none = strstr(buf1, NONE);
655                 if (none)
656                         goto out;
657                 strncat(buf1, ",", strlen(","));
658                 strncat(buf1, NONE, strlen(NONE));
659                 if (iscsi_update_param_value(param, buf1) < 0)
660                         return -EINVAL;
661         }
662
663 out:
664         a->authentication = authentication;
665         pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
666                 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
667
668         return 0;
669 }
670
671 int iscsit_ta_login_timeout(
672         struct iscsi_portal_group *tpg,
673         u32 login_timeout)
674 {
675         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
676
677         if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
678                 pr_err("Requested Login Timeout %u larger than maximum"
679                         " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
680                 return -EINVAL;
681         } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
682                 pr_err("Requested Logout Timeout %u smaller than"
683                         " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
684                 return -EINVAL;
685         }
686
687         a->login_timeout = login_timeout;
688         pr_debug("Set Logout Timeout to %u for Target Portal Group"
689                 " %hu\n", a->login_timeout, tpg->tpgt);
690
691         return 0;
692 }
693
694 int iscsit_ta_netif_timeout(
695         struct iscsi_portal_group *tpg,
696         u32 netif_timeout)
697 {
698         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
699
700         if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
701                 pr_err("Requested Network Interface Timeout %u larger"
702                         " than maximum %u\n", netif_timeout,
703                                 TA_NETIF_TIMEOUT_MAX);
704                 return -EINVAL;
705         } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
706                 pr_err("Requested Network Interface Timeout %u smaller"
707                         " than minimum %u\n", netif_timeout,
708                                 TA_NETIF_TIMEOUT_MIN);
709                 return -EINVAL;
710         }
711
712         a->netif_timeout = netif_timeout;
713         pr_debug("Set Network Interface Timeout to %u for"
714                 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
715
716         return 0;
717 }
718
719 int iscsit_ta_generate_node_acls(
720         struct iscsi_portal_group *tpg,
721         u32 flag)
722 {
723         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
724
725         if ((flag != 0) && (flag != 1)) {
726                 pr_err("Illegal value %d\n", flag);
727                 return -EINVAL;
728         }
729
730         a->generate_node_acls = flag;
731         pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
732                 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
733
734         if (flag == 1 && a->cache_dynamic_acls == 0) {
735                 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
736                         "generate_node_acls=1\n");
737                 a->cache_dynamic_acls = 1;
738         }
739
740         return 0;
741 }
742
743 int iscsit_ta_default_cmdsn_depth(
744         struct iscsi_portal_group *tpg,
745         u32 tcq_depth)
746 {
747         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
748
749         if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
750                 pr_err("Requested Default Queue Depth: %u larger"
751                         " than maximum %u\n", tcq_depth,
752                                 TA_DEFAULT_CMDSN_DEPTH_MAX);
753                 return -EINVAL;
754         } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
755                 pr_err("Requested Default Queue Depth: %u smaller"
756                         " than minimum %u\n", tcq_depth,
757                                 TA_DEFAULT_CMDSN_DEPTH_MIN);
758                 return -EINVAL;
759         }
760
761         a->default_cmdsn_depth = tcq_depth;
762         pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
763                 tpg->tpgt, a->default_cmdsn_depth);
764
765         return 0;
766 }
767
768 int iscsit_ta_cache_dynamic_acls(
769         struct iscsi_portal_group *tpg,
770         u32 flag)
771 {
772         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
773
774         if ((flag != 0) && (flag != 1)) {
775                 pr_err("Illegal value %d\n", flag);
776                 return -EINVAL;
777         }
778
779         if (a->generate_node_acls == 1 && flag == 0) {
780                 pr_debug("Skipping cache_dynamic_acls=0 when"
781                         " generate_node_acls=1\n");
782                 return 0;
783         }
784
785         a->cache_dynamic_acls = flag;
786         pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
787                 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
788                 "Enabled" : "Disabled");
789
790         return 0;
791 }
792
793 int iscsit_ta_demo_mode_write_protect(
794         struct iscsi_portal_group *tpg,
795         u32 flag)
796 {
797         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
798
799         if ((flag != 0) && (flag != 1)) {
800                 pr_err("Illegal value %d\n", flag);
801                 return -EINVAL;
802         }
803
804         a->demo_mode_write_protect = flag;
805         pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
806                 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
807
808         return 0;
809 }
810
811 int iscsit_ta_prod_mode_write_protect(
812         struct iscsi_portal_group *tpg,
813         u32 flag)
814 {
815         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
816
817         if ((flag != 0) && (flag != 1)) {
818                 pr_err("Illegal value %d\n", flag);
819                 return -EINVAL;
820         }
821
822         a->prod_mode_write_protect = flag;
823         pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
824                 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
825                 "ON" : "OFF");
826
827         return 0;
828 }
829
830 int iscsit_ta_demo_mode_discovery(
831         struct iscsi_portal_group *tpg,
832         u32 flag)
833 {
834         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
835
836         if ((flag != 0) && (flag != 1)) {
837                 pr_err("Illegal value %d\n", flag);
838                 return -EINVAL;
839         }
840
841         a->demo_mode_discovery = flag;
842         pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
843                 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
844                 "ON" : "OFF");
845
846         return 0;
847 }
848
849 int iscsit_ta_default_erl(
850         struct iscsi_portal_group *tpg,
851         u32 default_erl)
852 {
853         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
854
855         if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
856                 pr_err("Illegal value for default_erl: %u\n", default_erl);
857                 return -EINVAL;
858         }
859
860         a->default_erl = default_erl;
861         pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
862
863         return 0;
864 }
865
866 int iscsit_ta_t10_pi(
867         struct iscsi_portal_group *tpg,
868         u32 flag)
869 {
870         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
871
872         if ((flag != 0) && (flag != 1)) {
873                 pr_err("Illegal value %d\n", flag);
874                 return -EINVAL;
875         }
876
877         a->t10_pi = flag;
878         pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
879                 " %s\n", tpg->tpgt, (a->t10_pi) ?
880                 "ON" : "OFF");
881
882         return 0;
883 }