md/raid0: convert some printks to pr_debug.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / target_core_tpg.c
blob4f1ba4c5ef1196e43e0d5ff4b52836c02e0dc9fe
1 /*******************************************************************************
2 * Filename: target_core_tpg.c
4 * This file contains generic Target Portal Group related functions.
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 * Nicholas A. Bellinger <nab@kernel.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
29 #include <linux/net.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/in.h>
35 #include <net/sock.h>
36 #include <net/tcp.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_cmnd.h>
40 #include <target/target_core_base.h>
41 #include <target/target_core_device.h>
42 #include <target/target_core_tpg.h>
43 #include <target/target_core_transport.h>
44 #include <target/target_core_fabric_ops.h>
46 #include "target_core_hba.h"
47 #include "target_core_stat.h"
49 extern struct se_device *g_lun0_dev;
51 static DEFINE_SPINLOCK(tpg_lock);
52 static LIST_HEAD(tpg_list);
54 /* core_clear_initiator_node_from_tpg():
58 static void core_clear_initiator_node_from_tpg(
59 struct se_node_acl *nacl,
60 struct se_portal_group *tpg)
62 int i;
63 struct se_dev_entry *deve;
64 struct se_lun *lun;
65 struct se_lun_acl *acl, *acl_tmp;
67 spin_lock_irq(&nacl->device_list_lock);
68 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
69 deve = &nacl->device_list[i];
71 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
72 continue;
74 if (!deve->se_lun) {
75 pr_err("%s device entries device pointer is"
76 " NULL, but Initiator has access.\n",
77 tpg->se_tpg_tfo->get_fabric_name());
78 continue;
81 lun = deve->se_lun;
82 spin_unlock_irq(&nacl->device_list_lock);
83 core_update_device_list_for_node(lun, NULL, deve->mapped_lun,
84 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
86 spin_lock(&lun->lun_acl_lock);
87 list_for_each_entry_safe(acl, acl_tmp,
88 &lun->lun_acl_list, lacl_list) {
89 if (!strcmp(acl->initiatorname, nacl->initiatorname) &&
90 (acl->mapped_lun == deve->mapped_lun))
91 break;
94 if (!acl) {
95 pr_err("Unable to locate struct se_lun_acl for %s,"
96 " mapped_lun: %u\n", nacl->initiatorname,
97 deve->mapped_lun);
98 spin_unlock(&lun->lun_acl_lock);
99 spin_lock_irq(&nacl->device_list_lock);
100 continue;
103 list_del(&acl->lacl_list);
104 spin_unlock(&lun->lun_acl_lock);
106 spin_lock_irq(&nacl->device_list_lock);
107 kfree(acl);
109 spin_unlock_irq(&nacl->device_list_lock);
112 /* __core_tpg_get_initiator_node_acl():
114 * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
116 struct se_node_acl *__core_tpg_get_initiator_node_acl(
117 struct se_portal_group *tpg,
118 const char *initiatorname)
120 struct se_node_acl *acl;
122 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
123 if (!strcmp(acl->initiatorname, initiatorname))
124 return acl;
127 return NULL;
130 /* core_tpg_get_initiator_node_acl():
134 struct se_node_acl *core_tpg_get_initiator_node_acl(
135 struct se_portal_group *tpg,
136 unsigned char *initiatorname)
138 struct se_node_acl *acl;
140 spin_lock_bh(&tpg->acl_node_lock);
141 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
142 if (!strcmp(acl->initiatorname, initiatorname) &&
143 !acl->dynamic_node_acl) {
144 spin_unlock_bh(&tpg->acl_node_lock);
145 return acl;
148 spin_unlock_bh(&tpg->acl_node_lock);
150 return NULL;
153 /* core_tpg_add_node_to_devs():
157 void core_tpg_add_node_to_devs(
158 struct se_node_acl *acl,
159 struct se_portal_group *tpg)
161 int i = 0;
162 u32 lun_access = 0;
163 struct se_lun *lun;
164 struct se_device *dev;
166 spin_lock(&tpg->tpg_lun_lock);
167 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
168 lun = &tpg->tpg_lun_list[i];
169 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
170 continue;
172 spin_unlock(&tpg->tpg_lun_lock);
174 dev = lun->lun_se_dev;
176 * By default in LIO-Target $FABRIC_MOD,
177 * demo_mode_write_protect is ON, or READ_ONLY;
179 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
180 if (dev->dev_flags & DF_READ_ONLY)
181 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
182 else
183 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
184 } else {
186 * Allow only optical drives to issue R/W in default RO
187 * demo mode.
189 if (dev->transport->get_device_type(dev) == TYPE_DISK)
190 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
191 else
192 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
195 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
196 " access for LUN in Demo Mode\n",
197 tpg->se_tpg_tfo->get_fabric_name(),
198 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
199 (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
200 "READ-WRITE" : "READ-ONLY");
202 core_update_device_list_for_node(lun, NULL, lun->unpacked_lun,
203 lun_access, acl, tpg, 1);
204 spin_lock(&tpg->tpg_lun_lock);
206 spin_unlock(&tpg->tpg_lun_lock);
209 /* core_set_queue_depth_for_node():
213 static int core_set_queue_depth_for_node(
214 struct se_portal_group *tpg,
215 struct se_node_acl *acl)
217 if (!acl->queue_depth) {
218 pr_err("Queue depth for %s Initiator Node: %s is 0,"
219 "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
220 acl->initiatorname);
221 acl->queue_depth = 1;
224 return 0;
227 /* core_create_device_list_for_node():
231 static int core_create_device_list_for_node(struct se_node_acl *nacl)
233 struct se_dev_entry *deve;
234 int i;
236 nacl->device_list = kzalloc(sizeof(struct se_dev_entry) *
237 TRANSPORT_MAX_LUNS_PER_TPG, GFP_KERNEL);
238 if (!nacl->device_list) {
239 pr_err("Unable to allocate memory for"
240 " struct se_node_acl->device_list\n");
241 return -ENOMEM;
243 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
244 deve = &nacl->device_list[i];
246 atomic_set(&deve->ua_count, 0);
247 atomic_set(&deve->pr_ref_count, 0);
248 spin_lock_init(&deve->ua_lock);
249 INIT_LIST_HEAD(&deve->alua_port_list);
250 INIT_LIST_HEAD(&deve->ua_list);
253 return 0;
256 /* core_tpg_check_initiator_node_acl()
260 struct se_node_acl *core_tpg_check_initiator_node_acl(
261 struct se_portal_group *tpg,
262 unsigned char *initiatorname)
264 struct se_node_acl *acl;
266 acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
267 if (acl)
268 return acl;
270 if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
271 return NULL;
273 acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
274 if (!acl)
275 return NULL;
277 INIT_LIST_HEAD(&acl->acl_list);
278 INIT_LIST_HEAD(&acl->acl_sess_list);
279 spin_lock_init(&acl->device_list_lock);
280 spin_lock_init(&acl->nacl_sess_lock);
281 atomic_set(&acl->acl_pr_ref_count, 0);
282 acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
283 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
284 acl->se_tpg = tpg;
285 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
286 spin_lock_init(&acl->stats_lock);
287 acl->dynamic_node_acl = 1;
289 tpg->se_tpg_tfo->set_default_node_attributes(acl);
291 if (core_create_device_list_for_node(acl) < 0) {
292 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
293 return NULL;
296 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
297 core_free_device_list_for_node(acl, tpg);
298 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
299 return NULL;
302 core_tpg_add_node_to_devs(acl, tpg);
304 spin_lock_bh(&tpg->acl_node_lock);
305 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
306 tpg->num_node_acls++;
307 spin_unlock_bh(&tpg->acl_node_lock);
309 pr_debug("%s_TPG[%u] - Added DYNAMIC ACL with TCQ Depth: %d for %s"
310 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
311 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
312 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
314 return acl;
316 EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
318 void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
320 while (atomic_read(&nacl->acl_pr_ref_count) != 0)
321 cpu_relax();
324 void core_tpg_clear_object_luns(struct se_portal_group *tpg)
326 int i, ret;
327 struct se_lun *lun;
329 spin_lock(&tpg->tpg_lun_lock);
330 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
331 lun = &tpg->tpg_lun_list[i];
333 if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) ||
334 (lun->lun_se_dev == NULL))
335 continue;
337 spin_unlock(&tpg->tpg_lun_lock);
338 ret = core_dev_del_lun(tpg, lun->unpacked_lun);
339 spin_lock(&tpg->tpg_lun_lock);
341 spin_unlock(&tpg->tpg_lun_lock);
343 EXPORT_SYMBOL(core_tpg_clear_object_luns);
345 /* core_tpg_add_initiator_node_acl():
349 struct se_node_acl *core_tpg_add_initiator_node_acl(
350 struct se_portal_group *tpg,
351 struct se_node_acl *se_nacl,
352 const char *initiatorname,
353 u32 queue_depth)
355 struct se_node_acl *acl = NULL;
357 spin_lock_bh(&tpg->acl_node_lock);
358 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
359 if (acl) {
360 if (acl->dynamic_node_acl) {
361 acl->dynamic_node_acl = 0;
362 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
363 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
364 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
365 spin_unlock_bh(&tpg->acl_node_lock);
367 * Release the locally allocated struct se_node_acl
368 * because * core_tpg_add_initiator_node_acl() returned
369 * a pointer to an existing demo mode node ACL.
371 if (se_nacl)
372 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg,
373 se_nacl);
374 goto done;
377 pr_err("ACL entry for %s Initiator"
378 " Node %s already exists for TPG %u, ignoring"
379 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
380 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
381 spin_unlock_bh(&tpg->acl_node_lock);
382 return ERR_PTR(-EEXIST);
384 spin_unlock_bh(&tpg->acl_node_lock);
386 if (!se_nacl) {
387 pr_err("struct se_node_acl pointer is NULL\n");
388 return ERR_PTR(-EINVAL);
391 * For v4.x logic the se_node_acl_s is hanging off a fabric
392 * dependent structure allocated via
393 * struct target_core_fabric_ops->fabric_make_nodeacl()
395 acl = se_nacl;
397 INIT_LIST_HEAD(&acl->acl_list);
398 INIT_LIST_HEAD(&acl->acl_sess_list);
399 spin_lock_init(&acl->device_list_lock);
400 spin_lock_init(&acl->nacl_sess_lock);
401 atomic_set(&acl->acl_pr_ref_count, 0);
402 acl->queue_depth = queue_depth;
403 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
404 acl->se_tpg = tpg;
405 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
406 spin_lock_init(&acl->stats_lock);
408 tpg->se_tpg_tfo->set_default_node_attributes(acl);
410 if (core_create_device_list_for_node(acl) < 0) {
411 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
412 return ERR_PTR(-ENOMEM);
415 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
416 core_free_device_list_for_node(acl, tpg);
417 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
418 return ERR_PTR(-EINVAL);
421 spin_lock_bh(&tpg->acl_node_lock);
422 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
423 tpg->num_node_acls++;
424 spin_unlock_bh(&tpg->acl_node_lock);
426 done:
427 pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
428 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
429 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
430 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
432 return acl;
434 EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
436 /* core_tpg_del_initiator_node_acl():
440 int core_tpg_del_initiator_node_acl(
441 struct se_portal_group *tpg,
442 struct se_node_acl *acl,
443 int force)
445 struct se_session *sess, *sess_tmp;
446 int dynamic_acl = 0;
448 spin_lock_bh(&tpg->acl_node_lock);
449 if (acl->dynamic_node_acl) {
450 acl->dynamic_node_acl = 0;
451 dynamic_acl = 1;
453 list_del(&acl->acl_list);
454 tpg->num_node_acls--;
455 spin_unlock_bh(&tpg->acl_node_lock);
457 spin_lock_bh(&tpg->session_lock);
458 list_for_each_entry_safe(sess, sess_tmp,
459 &tpg->tpg_sess_list, sess_list) {
460 if (sess->se_node_acl != acl)
461 continue;
463 * Determine if the session needs to be closed by our context.
465 if (!tpg->se_tpg_tfo->shutdown_session(sess))
466 continue;
468 spin_unlock_bh(&tpg->session_lock);
470 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
471 * forcefully shutdown the $FABRIC_MOD session/nexus.
473 tpg->se_tpg_tfo->close_session(sess);
475 spin_lock_bh(&tpg->session_lock);
477 spin_unlock_bh(&tpg->session_lock);
479 core_tpg_wait_for_nacl_pr_ref(acl);
480 core_clear_initiator_node_from_tpg(acl, tpg);
481 core_free_device_list_for_node(acl, tpg);
483 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
484 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
485 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
486 tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
488 return 0;
490 EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
492 /* core_tpg_set_initiator_node_queue_depth():
496 int core_tpg_set_initiator_node_queue_depth(
497 struct se_portal_group *tpg,
498 unsigned char *initiatorname,
499 u32 queue_depth,
500 int force)
502 struct se_session *sess, *init_sess = NULL;
503 struct se_node_acl *acl;
504 int dynamic_acl = 0;
506 spin_lock_bh(&tpg->acl_node_lock);
507 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
508 if (!acl) {
509 pr_err("Access Control List entry for %s Initiator"
510 " Node %s does not exists for TPG %hu, ignoring"
511 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
512 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
513 spin_unlock_bh(&tpg->acl_node_lock);
514 return -ENODEV;
516 if (acl->dynamic_node_acl) {
517 acl->dynamic_node_acl = 0;
518 dynamic_acl = 1;
520 spin_unlock_bh(&tpg->acl_node_lock);
522 spin_lock_bh(&tpg->session_lock);
523 list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
524 if (sess->se_node_acl != acl)
525 continue;
527 if (!force) {
528 pr_err("Unable to change queue depth for %s"
529 " Initiator Node: %s while session is"
530 " operational. To forcefully change the queue"
531 " depth and force session reinstatement"
532 " use the \"force=1\" parameter.\n",
533 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
534 spin_unlock_bh(&tpg->session_lock);
536 spin_lock_bh(&tpg->acl_node_lock);
537 if (dynamic_acl)
538 acl->dynamic_node_acl = 1;
539 spin_unlock_bh(&tpg->acl_node_lock);
540 return -EEXIST;
543 * Determine if the session needs to be closed by our context.
545 if (!tpg->se_tpg_tfo->shutdown_session(sess))
546 continue;
548 init_sess = sess;
549 break;
553 * User has requested to change the queue depth for a Initiator Node.
554 * Change the value in the Node's struct se_node_acl, and call
555 * core_set_queue_depth_for_node() to add the requested queue depth.
557 * Finally call tpg->se_tpg_tfo->close_session() to force session
558 * reinstatement to occur if there is an active session for the
559 * $FABRIC_MOD Initiator Node in question.
561 acl->queue_depth = queue_depth;
563 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
564 spin_unlock_bh(&tpg->session_lock);
566 * Force session reinstatement if
567 * core_set_queue_depth_for_node() failed, because we assume
568 * the $FABRIC_MOD has already the set session reinstatement
569 * bit from tpg->se_tpg_tfo->shutdown_session() called above.
571 if (init_sess)
572 tpg->se_tpg_tfo->close_session(init_sess);
574 spin_lock_bh(&tpg->acl_node_lock);
575 if (dynamic_acl)
576 acl->dynamic_node_acl = 1;
577 spin_unlock_bh(&tpg->acl_node_lock);
578 return -EINVAL;
580 spin_unlock_bh(&tpg->session_lock);
582 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
583 * forcefully shutdown the $FABRIC_MOD session/nexus.
585 if (init_sess)
586 tpg->se_tpg_tfo->close_session(init_sess);
588 pr_debug("Successfuly changed queue depth to: %d for Initiator"
589 " Node: %s on %s Target Portal Group: %u\n", queue_depth,
590 initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
591 tpg->se_tpg_tfo->tpg_get_tag(tpg));
593 spin_lock_bh(&tpg->acl_node_lock);
594 if (dynamic_acl)
595 acl->dynamic_node_acl = 1;
596 spin_unlock_bh(&tpg->acl_node_lock);
598 return 0;
600 EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
602 static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
604 /* Set in core_dev_setup_virtual_lun0() */
605 struct se_device *dev = g_lun0_dev;
606 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
607 u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
608 int ret;
610 lun->unpacked_lun = 0;
611 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
612 atomic_set(&lun->lun_acl_count, 0);
613 init_completion(&lun->lun_shutdown_comp);
614 INIT_LIST_HEAD(&lun->lun_acl_list);
615 INIT_LIST_HEAD(&lun->lun_cmd_list);
616 spin_lock_init(&lun->lun_acl_lock);
617 spin_lock_init(&lun->lun_cmd_lock);
618 spin_lock_init(&lun->lun_sep_lock);
620 ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
621 if (ret < 0)
622 return ret;
624 return 0;
627 static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
629 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
631 core_tpg_post_dellun(se_tpg, lun);
634 int core_tpg_register(
635 struct target_core_fabric_ops *tfo,
636 struct se_wwn *se_wwn,
637 struct se_portal_group *se_tpg,
638 void *tpg_fabric_ptr,
639 int se_tpg_type)
641 struct se_lun *lun;
642 u32 i;
644 se_tpg->tpg_lun_list = kzalloc((sizeof(struct se_lun) *
645 TRANSPORT_MAX_LUNS_PER_TPG), GFP_KERNEL);
646 if (!se_tpg->tpg_lun_list) {
647 pr_err("Unable to allocate struct se_portal_group->"
648 "tpg_lun_list\n");
649 return -ENOMEM;
652 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
653 lun = &se_tpg->tpg_lun_list[i];
654 lun->unpacked_lun = i;
655 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
656 atomic_set(&lun->lun_acl_count, 0);
657 init_completion(&lun->lun_shutdown_comp);
658 INIT_LIST_HEAD(&lun->lun_acl_list);
659 INIT_LIST_HEAD(&lun->lun_cmd_list);
660 spin_lock_init(&lun->lun_acl_lock);
661 spin_lock_init(&lun->lun_cmd_lock);
662 spin_lock_init(&lun->lun_sep_lock);
665 se_tpg->se_tpg_type = se_tpg_type;
666 se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
667 se_tpg->se_tpg_tfo = tfo;
668 se_tpg->se_tpg_wwn = se_wwn;
669 atomic_set(&se_tpg->tpg_pr_ref_count, 0);
670 INIT_LIST_HEAD(&se_tpg->acl_node_list);
671 INIT_LIST_HEAD(&se_tpg->se_tpg_node);
672 INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
673 spin_lock_init(&se_tpg->acl_node_lock);
674 spin_lock_init(&se_tpg->session_lock);
675 spin_lock_init(&se_tpg->tpg_lun_lock);
677 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
678 if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
679 kfree(se_tpg);
680 return -ENOMEM;
684 spin_lock_bh(&tpg_lock);
685 list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
686 spin_unlock_bh(&tpg_lock);
688 pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
689 " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
690 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
691 "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
692 "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
694 return 0;
696 EXPORT_SYMBOL(core_tpg_register);
698 int core_tpg_deregister(struct se_portal_group *se_tpg)
700 struct se_node_acl *nacl, *nacl_tmp;
702 pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
703 " for endpoint: %s Portal Tag %u\n",
704 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
705 "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
706 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
707 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
709 spin_lock_bh(&tpg_lock);
710 list_del(&se_tpg->se_tpg_node);
711 spin_unlock_bh(&tpg_lock);
713 while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
714 cpu_relax();
716 * Release any remaining demo-mode generated se_node_acl that have
717 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
718 * in transport_deregister_session().
720 spin_lock_bh(&se_tpg->acl_node_lock);
721 list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
722 acl_list) {
723 list_del(&nacl->acl_list);
724 se_tpg->num_node_acls--;
725 spin_unlock_bh(&se_tpg->acl_node_lock);
727 core_tpg_wait_for_nacl_pr_ref(nacl);
728 core_free_device_list_for_node(nacl, se_tpg);
729 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
731 spin_lock_bh(&se_tpg->acl_node_lock);
733 spin_unlock_bh(&se_tpg->acl_node_lock);
735 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
736 core_tpg_release_virtual_lun0(se_tpg);
738 se_tpg->se_tpg_fabric_ptr = NULL;
739 kfree(se_tpg->tpg_lun_list);
740 return 0;
742 EXPORT_SYMBOL(core_tpg_deregister);
744 struct se_lun *core_tpg_pre_addlun(
745 struct se_portal_group *tpg,
746 u32 unpacked_lun)
748 struct se_lun *lun;
750 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
751 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
752 "-1: %u for Target Portal Group: %u\n",
753 tpg->se_tpg_tfo->get_fabric_name(),
754 unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
755 tpg->se_tpg_tfo->tpg_get_tag(tpg));
756 return ERR_PTR(-EOVERFLOW);
759 spin_lock(&tpg->tpg_lun_lock);
760 lun = &tpg->tpg_lun_list[unpacked_lun];
761 if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
762 pr_err("TPG Logical Unit Number: %u is already active"
763 " on %s Target Portal Group: %u, ignoring request.\n",
764 unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
765 tpg->se_tpg_tfo->tpg_get_tag(tpg));
766 spin_unlock(&tpg->tpg_lun_lock);
767 return ERR_PTR(-EINVAL);
769 spin_unlock(&tpg->tpg_lun_lock);
771 return lun;
774 int core_tpg_post_addlun(
775 struct se_portal_group *tpg,
776 struct se_lun *lun,
777 u32 lun_access,
778 void *lun_ptr)
780 int ret;
782 ret = core_dev_export(lun_ptr, tpg, lun);
783 if (ret < 0)
784 return ret;
786 spin_lock(&tpg->tpg_lun_lock);
787 lun->lun_access = lun_access;
788 lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
789 spin_unlock(&tpg->tpg_lun_lock);
791 return 0;
794 static void core_tpg_shutdown_lun(
795 struct se_portal_group *tpg,
796 struct se_lun *lun)
798 core_clear_lun_from_tpg(lun, tpg);
799 transport_clear_lun_from_sessions(lun);
802 struct se_lun *core_tpg_pre_dellun(
803 struct se_portal_group *tpg,
804 u32 unpacked_lun,
805 int *ret)
807 struct se_lun *lun;
809 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
810 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
811 "-1: %u for Target Portal Group: %u\n",
812 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
813 TRANSPORT_MAX_LUNS_PER_TPG-1,
814 tpg->se_tpg_tfo->tpg_get_tag(tpg));
815 return ERR_PTR(-EOVERFLOW);
818 spin_lock(&tpg->tpg_lun_lock);
819 lun = &tpg->tpg_lun_list[unpacked_lun];
820 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
821 pr_err("%s Logical Unit Number: %u is not active on"
822 " Target Portal Group: %u, ignoring request.\n",
823 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
824 tpg->se_tpg_tfo->tpg_get_tag(tpg));
825 spin_unlock(&tpg->tpg_lun_lock);
826 return ERR_PTR(-ENODEV);
828 spin_unlock(&tpg->tpg_lun_lock);
830 return lun;
833 int core_tpg_post_dellun(
834 struct se_portal_group *tpg,
835 struct se_lun *lun)
837 core_tpg_shutdown_lun(tpg, lun);
839 core_dev_unexport(lun->lun_se_dev, tpg, lun);
841 spin_lock(&tpg->tpg_lun_lock);
842 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
843 spin_unlock(&tpg->tpg_lun_lock);
845 return 0;