blob: e070ce2de60abf7767d2992ab217252d97a8af3a [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
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>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050022#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000023#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000024
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
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080034#include <target/iscsi/iscsi_transport.h>
35
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036struct 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);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -070052 sema_init(&tpg->np_login_sem, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000053 spin_lock_init(&tpg->tpg_state_lock);
54 spin_lock_init(&tpg->tpg_np_lock);
55
56 return tpg;
57}
58
59static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
60
61int 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,
Jörn Engel8359cf42011-11-24 02:05:51 +010075 NULL, &tpg->tpg_se_tpg, tpg,
Nicholas Bellingere48354c2011-07-23 06:43:04 +000076 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;
110out:
111 if (tpg->sid == 1)
112 core_tpg_deregister(&tpg->tpg_se_tpg);
113 kfree(tpg);
114 return -1;
115}
116
117void 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
130struct iscsi_portal_group *iscsit_get_tpg_from_np(
131 struct iscsi_tiqn *tiqn,
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700132 struct iscsi_np *np,
133 struct iscsi_tpg_np **tpg_np_out)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000134{
135 struct iscsi_portal_group *tpg = NULL;
136 struct iscsi_tpg_np *tpg_np;
137
138 spin_lock(&tiqn->tiqn_tpg_lock);
139 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
140
141 spin_lock(&tpg->tpg_state_lock);
142 if (tpg->tpg_state == TPG_STATE_FREE) {
143 spin_unlock(&tpg->tpg_state_lock);
144 continue;
145 }
146 spin_unlock(&tpg->tpg_state_lock);
147
148 spin_lock(&tpg->tpg_np_lock);
149 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
150 if (tpg_np->tpg_np == np) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700151 *tpg_np_out = tpg_np;
152 kref_get(&tpg_np->tpg_np_kref);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 spin_unlock(&tpg->tpg_np_lock);
154 spin_unlock(&tiqn->tiqn_tpg_lock);
155 return tpg;
156 }
157 }
158 spin_unlock(&tpg->tpg_np_lock);
159 }
160 spin_unlock(&tiqn->tiqn_tpg_lock);
161
162 return NULL;
163}
164
165int iscsit_get_tpg(
166 struct iscsi_portal_group *tpg)
167{
168 int ret;
169
170 ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
171 return ((ret != 0) || signal_pending(current)) ? -1 : 0;
172}
173
174void iscsit_put_tpg(struct iscsi_portal_group *tpg)
175{
176 mutex_unlock(&tpg->tpg_access_lock);
177}
178
179static void iscsit_clear_tpg_np_login_thread(
180 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700181 struct iscsi_portal_group *tpg,
182 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000183{
184 if (!tpg_np->tpg_np) {
185 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
186 return;
187 }
188
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700189 iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000190}
191
192void iscsit_clear_tpg_np_login_threads(
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700193 struct iscsi_portal_group *tpg,
194 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000195{
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);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700205 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000206 spin_lock(&tpg->tpg_np_lock);
207 }
208 spin_unlock(&tpg->tpg_np_lock);
209}
210
211void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
212{
213 iscsi_print_params(tpg->param_list);
214}
215
216static 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}
229
230int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
231{
232 if (tpg->tpg_state != TPG_STATE_FREE) {
233 pr_err("Unable to add iSCSI Target Portal Group: %d"
234 " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
235 return -EEXIST;
236 }
237 iscsit_set_default_tpg_attribs(tpg);
238
239 if (iscsi_create_default_params(&tpg->param_list) < 0)
240 goto err_out;
241
242 ISCSI_TPG_ATTRIB(tpg)->tpg = tpg;
243
244 spin_lock(&tpg->tpg_state_lock);
245 tpg->tpg_state = TPG_STATE_INACTIVE;
246 spin_unlock(&tpg->tpg_state_lock);
247
248 spin_lock(&tiqn->tiqn_tpg_lock);
249 list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
250 tiqn->tiqn_ntpgs++;
251 pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
252 tiqn->tiqn, tpg->tpgt);
253 spin_unlock(&tiqn->tiqn_tpg_lock);
254
255 return 0;
256err_out:
257 if (tpg->param_list) {
258 iscsi_release_param_list(tpg->param_list);
259 tpg->param_list = NULL;
260 }
261 kfree(tpg);
262 return -ENOMEM;
263}
264
265int iscsit_tpg_del_portal_group(
266 struct iscsi_tiqn *tiqn,
267 struct iscsi_portal_group *tpg,
268 int force)
269{
270 u8 old_state = tpg->tpg_state;
271
272 spin_lock(&tpg->tpg_state_lock);
273 tpg->tpg_state = TPG_STATE_INACTIVE;
274 spin_unlock(&tpg->tpg_state_lock);
275
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700276 iscsit_clear_tpg_np_login_threads(tpg, true);
277
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000278 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
279 pr_err("Unable to delete iSCSI Target Portal Group:"
280 " %hu while active sessions exist, and force=0\n",
281 tpg->tpgt);
282 tpg->tpg_state = old_state;
283 return -EPERM;
284 }
285
286 core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
287
288 if (tpg->param_list) {
289 iscsi_release_param_list(tpg->param_list);
290 tpg->param_list = NULL;
291 }
292
293 core_tpg_deregister(&tpg->tpg_se_tpg);
294
295 spin_lock(&tpg->tpg_state_lock);
296 tpg->tpg_state = TPG_STATE_FREE;
297 spin_unlock(&tpg->tpg_state_lock);
298
299 spin_lock(&tiqn->tiqn_tpg_lock);
300 tiqn->tiqn_ntpgs--;
301 list_del(&tpg->tpg_list);
302 spin_unlock(&tiqn->tiqn_tpg_lock);
303
304 pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
305 tiqn->tiqn, tpg->tpgt);
306
307 kfree(tpg);
308 return 0;
309}
310
311int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
312{
313 struct iscsi_param *param;
314 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
Andy Grover617a0c22012-07-12 17:34:56 -0700315 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000316
317 spin_lock(&tpg->tpg_state_lock);
318 if (tpg->tpg_state == TPG_STATE_ACTIVE) {
319 pr_err("iSCSI target portal group: %hu is already"
320 " active, ignoring request.\n", tpg->tpgt);
321 spin_unlock(&tpg->tpg_state_lock);
322 return -EINVAL;
323 }
324 /*
325 * Make sure that AuthMethod does not contain None as an option
326 * unless explictly disabled. Set the default to CHAP if authentication
327 * is enforced (as per default), and remove the NONE option.
328 */
329 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
330 if (!param) {
331 spin_unlock(&tpg->tpg_state_lock);
Andy Grover617a0c22012-07-12 17:34:56 -0700332 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000333 }
334
335 if (ISCSI_TPG_ATTRIB(tpg)->authentication) {
Andy Grover617a0c22012-07-12 17:34:56 -0700336 if (!strcmp(param->value, NONE)) {
337 ret = iscsi_update_param_value(param, CHAP);
338 if (ret)
339 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340 }
Andy Grover617a0c22012-07-12 17:34:56 -0700341
342 ret = iscsit_ta_authentication(tpg, 1);
343 if (ret < 0)
344 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000345 }
346
347 tpg->tpg_state = TPG_STATE_ACTIVE;
348 spin_unlock(&tpg->tpg_state_lock);
349
350 spin_lock(&tiqn->tiqn_tpg_lock);
351 tiqn->tiqn_active_tpgs++;
352 pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
353 tpg->tpgt);
354 spin_unlock(&tiqn->tiqn_tpg_lock);
355
356 return 0;
Andy Grover617a0c22012-07-12 17:34:56 -0700357
358err:
359 spin_unlock(&tpg->tpg_state_lock);
360 return ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000361}
362
363int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
364{
365 struct iscsi_tiqn *tiqn;
366 u8 old_state = tpg->tpg_state;
367
368 spin_lock(&tpg->tpg_state_lock);
369 if (tpg->tpg_state == TPG_STATE_INACTIVE) {
370 pr_err("iSCSI Target Portal Group: %hu is already"
371 " inactive, ignoring request.\n", tpg->tpgt);
372 spin_unlock(&tpg->tpg_state_lock);
373 return -EINVAL;
374 }
375 tpg->tpg_state = TPG_STATE_INACTIVE;
376 spin_unlock(&tpg->tpg_state_lock);
377
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700378 iscsit_clear_tpg_np_login_threads(tpg, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379
380 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
381 spin_lock(&tpg->tpg_state_lock);
382 tpg->tpg_state = old_state;
383 spin_unlock(&tpg->tpg_state_lock);
384 pr_err("Unable to disable iSCSI Target Portal Group:"
385 " %hu while active sessions exist, and force=0\n",
386 tpg->tpgt);
387 return -EPERM;
388 }
389
390 tiqn = tpg->tpg_tiqn;
391 if (!tiqn || (tpg == iscsit_global->discovery_tpg))
392 return 0;
393
394 spin_lock(&tiqn->tiqn_tpg_lock);
395 tiqn->tiqn_active_tpgs--;
396 pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
397 tpg->tpgt);
398 spin_unlock(&tiqn->tiqn_tpg_lock);
399
400 return 0;
401}
402
403struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
404 struct iscsi_session *sess)
405{
406 struct se_session *se_sess = sess->se_sess;
407 struct se_node_acl *se_nacl = se_sess->se_node_acl;
408 struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
409 se_node_acl);
410
411 return &acl->node_attrib;
412}
413
414struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
415 struct iscsi_tpg_np *tpg_np,
416 int network_transport)
417{
418 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
419
420 spin_lock(&tpg_np->tpg_np_parent_lock);
421 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
422 &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
423 if (tpg_np_child->tpg_np->np_network_transport ==
424 network_transport) {
425 spin_unlock(&tpg_np->tpg_np_parent_lock);
426 return tpg_np_child;
427 }
428 }
429 spin_unlock(&tpg_np->tpg_np_parent_lock);
430
431 return NULL;
432}
433
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800434static bool iscsit_tpg_check_network_portal(
435 struct iscsi_tiqn *tiqn,
436 struct __kernel_sockaddr_storage *sockaddr,
437 int network_transport)
438{
439 struct iscsi_portal_group *tpg;
440 struct iscsi_tpg_np *tpg_np;
441 struct iscsi_np *np;
442 bool match = false;
443
444 spin_lock(&tiqn->tiqn_tpg_lock);
445 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
446
447 spin_lock(&tpg->tpg_np_lock);
448 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
449 np = tpg_np->tpg_np;
450
451 match = iscsit_check_np_match(sockaddr, np,
452 network_transport);
453 if (match == true)
454 break;
455 }
456 spin_unlock(&tpg->tpg_np_lock);
457 }
458 spin_unlock(&tiqn->tiqn_tpg_lock);
459
460 return match;
461}
462
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000463struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
464 struct iscsi_portal_group *tpg,
465 struct __kernel_sockaddr_storage *sockaddr,
466 char *ip_str,
467 struct iscsi_tpg_np *tpg_np_parent,
468 int network_transport)
469{
470 struct iscsi_np *np;
471 struct iscsi_tpg_np *tpg_np;
472
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800473 if (!tpg_np_parent) {
474 if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
475 network_transport) == true) {
476 pr_err("Network Portal: %s already exists on a"
477 " different TPG on %s\n", ip_str,
478 tpg->tpg_tiqn->tiqn);
479 return ERR_PTR(-EEXIST);
480 }
481 }
482
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483 tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
484 if (!tpg_np) {
485 pr_err("Unable to allocate memory for"
486 " struct iscsi_tpg_np.\n");
487 return ERR_PTR(-ENOMEM);
488 }
489
490 np = iscsit_add_np(sockaddr, ip_str, network_transport);
491 if (IS_ERR(np)) {
492 kfree(tpg_np);
493 return ERR_CAST(np);
494 }
495
496 INIT_LIST_HEAD(&tpg_np->tpg_np_list);
497 INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
498 INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
499 spin_lock_init(&tpg_np->tpg_np_parent_lock);
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700500 init_completion(&tpg_np->tpg_np_comp);
501 kref_init(&tpg_np->tpg_np_kref);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000502 tpg_np->tpg_np = np;
503 tpg_np->tpg = tpg;
504
505 spin_lock(&tpg->tpg_np_lock);
506 list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
507 tpg->num_tpg_nps++;
508 if (tpg->tpg_tiqn)
509 tpg->tpg_tiqn->tiqn_num_tpg_nps++;
510 spin_unlock(&tpg->tpg_np_lock);
511
512 if (tpg_np_parent) {
513 tpg_np->tpg_np_parent = tpg_np_parent;
514 spin_lock(&tpg_np_parent->tpg_np_parent_lock);
515 list_add_tail(&tpg_np->tpg_np_child_list,
516 &tpg_np_parent->tpg_np_parent_list);
517 spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
518 }
519
520 pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
521 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800522 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000523
524 return tpg_np;
525}
526
527static int iscsit_tpg_release_np(
528 struct iscsi_tpg_np *tpg_np,
529 struct iscsi_portal_group *tpg,
530 struct iscsi_np *np)
531{
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700532 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000533
534 pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
535 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800536 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000537
538 tpg_np->tpg_np = NULL;
539 tpg_np->tpg = NULL;
540 kfree(tpg_np);
541 /*
542 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
543 */
544 return iscsit_del_np(np);
545}
546
547int iscsit_tpg_del_network_portal(
548 struct iscsi_portal_group *tpg,
549 struct iscsi_tpg_np *tpg_np)
550{
551 struct iscsi_np *np;
552 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
553 int ret = 0;
554
555 np = tpg_np->tpg_np;
556 if (!np) {
557 pr_err("Unable to locate struct iscsi_np from"
558 " struct iscsi_tpg_np\n");
559 return -EINVAL;
560 }
561
562 if (!tpg_np->tpg_np_parent) {
563 /*
564 * We are the parent tpg network portal. Release all of the
565 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
566 * list first.
567 */
568 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
569 &tpg_np->tpg_np_parent_list,
570 tpg_np_child_list) {
571 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
572 if (ret < 0)
573 pr_err("iscsit_tpg_del_network_portal()"
574 " failed: %d\n", ret);
575 }
576 } else {
577 /*
578 * We are not the parent ISCSI_TCP tpg network portal. Release
579 * our own network portals from the child list.
580 */
581 spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
582 list_del(&tpg_np->tpg_np_child_list);
583 spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
584 }
585
586 spin_lock(&tpg->tpg_np_lock);
587 list_del(&tpg_np->tpg_np_list);
588 tpg->num_tpg_nps--;
589 if (tpg->tpg_tiqn)
590 tpg->tpg_tiqn->tiqn_num_tpg_nps--;
591 spin_unlock(&tpg->tpg_np_lock);
592
593 return iscsit_tpg_release_np(tpg_np, tpg, np);
594}
595
596int iscsit_tpg_set_initiator_node_queue_depth(
597 struct iscsi_portal_group *tpg,
598 unsigned char *initiatorname,
599 u32 queue_depth,
600 int force)
601{
602 return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
603 initiatorname, queue_depth, force);
604}
605
606int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
607{
608 unsigned char buf1[256], buf2[256], *none = NULL;
609 int len;
610 struct iscsi_param *param;
611 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
612
613 if ((authentication != 1) && (authentication != 0)) {
614 pr_err("Illegal value for authentication parameter:"
615 " %u, ignoring request.\n", authentication);
Andy Grover617a0c22012-07-12 17:34:56 -0700616 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617 }
618
619 memset(buf1, 0, sizeof(buf1));
620 memset(buf2, 0, sizeof(buf2));
621
622 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
623 if (!param)
624 return -EINVAL;
625
626 if (authentication) {
627 snprintf(buf1, sizeof(buf1), "%s", param->value);
628 none = strstr(buf1, NONE);
629 if (!none)
630 goto out;
631 if (!strncmp(none + 4, ",", 1)) {
632 if (!strcmp(buf1, none))
633 sprintf(buf2, "%s", none+5);
634 else {
635 none--;
636 *none = '\0';
637 len = sprintf(buf2, "%s", buf1);
638 none += 5;
639 sprintf(buf2 + len, "%s", none);
640 }
641 } else {
642 none--;
643 *none = '\0';
644 sprintf(buf2, "%s", buf1);
645 }
646 if (iscsi_update_param_value(param, buf2) < 0)
647 return -EINVAL;
648 } else {
649 snprintf(buf1, sizeof(buf1), "%s", param->value);
650 none = strstr(buf1, NONE);
Andy Groveree1b1b92012-07-12 17:34:54 -0700651 if (none)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652 goto out;
653 strncat(buf1, ",", strlen(","));
654 strncat(buf1, NONE, strlen(NONE));
655 if (iscsi_update_param_value(param, buf1) < 0)
656 return -EINVAL;
657 }
658
659out:
660 a->authentication = authentication;
661 pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
662 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
663
664 return 0;
665}
666
667int iscsit_ta_login_timeout(
668 struct iscsi_portal_group *tpg,
669 u32 login_timeout)
670{
671 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
672
673 if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
674 pr_err("Requested Login Timeout %u larger than maximum"
675 " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
676 return -EINVAL;
677 } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
678 pr_err("Requested Logout Timeout %u smaller than"
679 " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
680 return -EINVAL;
681 }
682
683 a->login_timeout = login_timeout;
684 pr_debug("Set Logout Timeout to %u for Target Portal Group"
685 " %hu\n", a->login_timeout, tpg->tpgt);
686
687 return 0;
688}
689
690int iscsit_ta_netif_timeout(
691 struct iscsi_portal_group *tpg,
692 u32 netif_timeout)
693{
694 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
695
696 if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
697 pr_err("Requested Network Interface Timeout %u larger"
698 " than maximum %u\n", netif_timeout,
699 TA_NETIF_TIMEOUT_MAX);
700 return -EINVAL;
701 } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
702 pr_err("Requested Network Interface Timeout %u smaller"
703 " than minimum %u\n", netif_timeout,
704 TA_NETIF_TIMEOUT_MIN);
705 return -EINVAL;
706 }
707
708 a->netif_timeout = netif_timeout;
709 pr_debug("Set Network Interface Timeout to %u for"
710 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
711
712 return 0;
713}
714
715int iscsit_ta_generate_node_acls(
716 struct iscsi_portal_group *tpg,
717 u32 flag)
718{
719 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
720
721 if ((flag != 0) && (flag != 1)) {
722 pr_err("Illegal value %d\n", flag);
723 return -EINVAL;
724 }
725
726 a->generate_node_acls = flag;
727 pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
728 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
729
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700730 if (flag == 1 && a->cache_dynamic_acls == 0) {
731 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
732 "generate_node_acls=1\n");
733 a->cache_dynamic_acls = 1;
734 }
735
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000736 return 0;
737}
738
739int iscsit_ta_default_cmdsn_depth(
740 struct iscsi_portal_group *tpg,
741 u32 tcq_depth)
742{
743 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
744
745 if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
746 pr_err("Requested Default Queue Depth: %u larger"
747 " than maximum %u\n", tcq_depth,
748 TA_DEFAULT_CMDSN_DEPTH_MAX);
749 return -EINVAL;
750 } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
751 pr_err("Requested Default Queue Depth: %u smaller"
752 " than minimum %u\n", tcq_depth,
753 TA_DEFAULT_CMDSN_DEPTH_MIN);
754 return -EINVAL;
755 }
756
757 a->default_cmdsn_depth = tcq_depth;
758 pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
759 tpg->tpgt, a->default_cmdsn_depth);
760
761 return 0;
762}
763
764int iscsit_ta_cache_dynamic_acls(
765 struct iscsi_portal_group *tpg,
766 u32 flag)
767{
768 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
769
770 if ((flag != 0) && (flag != 1)) {
771 pr_err("Illegal value %d\n", flag);
772 return -EINVAL;
773 }
774
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700775 if (a->generate_node_acls == 1 && flag == 0) {
776 pr_debug("Skipping cache_dynamic_acls=0 when"
777 " generate_node_acls=1\n");
778 return 0;
779 }
780
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 a->cache_dynamic_acls = flag;
782 pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
783 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
784 "Enabled" : "Disabled");
785
786 return 0;
787}
788
789int iscsit_ta_demo_mode_write_protect(
790 struct iscsi_portal_group *tpg,
791 u32 flag)
792{
793 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
794
795 if ((flag != 0) && (flag != 1)) {
796 pr_err("Illegal value %d\n", flag);
797 return -EINVAL;
798 }
799
800 a->demo_mode_write_protect = flag;
801 pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
802 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
803
804 return 0;
805}
806
807int iscsit_ta_prod_mode_write_protect(
808 struct iscsi_portal_group *tpg,
809 u32 flag)
810{
811 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
812
813 if ((flag != 0) && (flag != 1)) {
814 pr_err("Illegal value %d\n", flag);
815 return -EINVAL;
816 }
817
818 a->prod_mode_write_protect = flag;
819 pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
820 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
821 "ON" : "OFF");
822
823 return 0;
824}