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