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>
35 #include <linux/export.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
41 #include <target/target_core_base.h>
42 #include <target/target_core_device.h>
43 #include <target/target_core_tpg.h>
44 #include <target/target_core_transport.h>
45 #include <target/target_core_fabric_ops.h>
47 #include "target_core_hba.h"
48 #include "target_core_stat.h"
50 extern struct se_device *g_lun0_dev;
52 static DEFINE_SPINLOCK(tpg_lock);
53 static LIST_HEAD(tpg_list);
55 /* core_clear_initiator_node_from_tpg():
59 static void core_clear_initiator_node_from_tpg(
60 struct se_node_acl *nacl,
61 struct se_portal_group *tpg)
64 struct se_dev_entry *deve;
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))
75 pr_err("%s device entries device pointer is"
76 " NULL, but Initiator has access.\n",
77 tpg->se_tpg_tfo->get_fabric_name());
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_irq(&nacl->device_list_lock);
88 spin_unlock_irq(&nacl->device_list_lock);
91 /* __core_tpg_get_initiator_node_acl():
93 * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
95 struct se_node_acl *__core_tpg_get_initiator_node_acl(
96 struct se_portal_group *tpg,
97 const char *initiatorname)
99 struct se_node_acl *acl;
101 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
102 if (!strcmp(acl->initiatorname, initiatorname))
109 /* core_tpg_get_initiator_node_acl():
113 struct se_node_acl *core_tpg_get_initiator_node_acl(
114 struct se_portal_group *tpg,
115 unsigned char *initiatorname)
117 struct se_node_acl *acl;
119 spin_lock_irq(&tpg->acl_node_lock);
120 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
121 if (!strcmp(acl->initiatorname, initiatorname) &&
122 !acl->dynamic_node_acl) {
123 spin_unlock_irq(&tpg->acl_node_lock);
127 spin_unlock_irq(&tpg->acl_node_lock);
132 /* core_tpg_add_node_to_devs():
136 void core_tpg_add_node_to_devs(
137 struct se_node_acl *acl,
138 struct se_portal_group *tpg)
143 struct se_device *dev;
145 spin_lock(&tpg->tpg_lun_lock);
146 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
147 lun = &tpg->tpg_lun_list[i];
148 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
151 spin_unlock(&tpg->tpg_lun_lock);
153 dev = lun->lun_se_dev;
155 * By default in LIO-Target $FABRIC_MOD,
156 * demo_mode_write_protect is ON, or READ_ONLY;
158 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
159 if (dev->dev_flags & DF_READ_ONLY)
160 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
162 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
165 * Allow only optical drives to issue R/W in default RO
168 if (dev->transport->get_device_type(dev) == TYPE_DISK)
169 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
171 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
174 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
175 " access for LUN in Demo Mode\n",
176 tpg->se_tpg_tfo->get_fabric_name(),
177 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
178 (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
179 "READ-WRITE" : "READ-ONLY");
181 core_update_device_list_for_node(lun, NULL, lun->unpacked_lun,
182 lun_access, acl, tpg, 1);
183 spin_lock(&tpg->tpg_lun_lock);
185 spin_unlock(&tpg->tpg_lun_lock);
188 /* core_set_queue_depth_for_node():
192 static int core_set_queue_depth_for_node(
193 struct se_portal_group *tpg,
194 struct se_node_acl *acl)
196 if (!acl->queue_depth) {
197 pr_err("Queue depth for %s Initiator Node: %s is 0,"
198 "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
200 acl->queue_depth = 1;
206 /* core_create_device_list_for_node():
210 static int core_create_device_list_for_node(struct se_node_acl *nacl)
212 struct se_dev_entry *deve;
215 nacl->device_list = kzalloc(sizeof(struct se_dev_entry) *
216 TRANSPORT_MAX_LUNS_PER_TPG, GFP_KERNEL);
217 if (!nacl->device_list) {
218 pr_err("Unable to allocate memory for"
219 " struct se_node_acl->device_list\n");
222 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
223 deve = &nacl->device_list[i];
225 atomic_set(&deve->ua_count, 0);
226 atomic_set(&deve->pr_ref_count, 0);
227 spin_lock_init(&deve->ua_lock);
228 INIT_LIST_HEAD(&deve->alua_port_list);
229 INIT_LIST_HEAD(&deve->ua_list);
235 /* core_tpg_check_initiator_node_acl()
239 struct se_node_acl *core_tpg_check_initiator_node_acl(
240 struct se_portal_group *tpg,
241 unsigned char *initiatorname)
243 struct se_node_acl *acl;
245 acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
249 if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
252 acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
256 INIT_LIST_HEAD(&acl->acl_list);
257 INIT_LIST_HEAD(&acl->acl_sess_list);
258 spin_lock_init(&acl->device_list_lock);
259 spin_lock_init(&acl->nacl_sess_lock);
260 atomic_set(&acl->acl_pr_ref_count, 0);
261 acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
262 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
264 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
265 spin_lock_init(&acl->stats_lock);
266 acl->dynamic_node_acl = 1;
268 tpg->se_tpg_tfo->set_default_node_attributes(acl);
270 if (core_create_device_list_for_node(acl) < 0) {
271 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
275 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
276 core_free_device_list_for_node(acl, tpg);
277 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
281 * Here we only create demo-mode MappedLUNs from the active
282 * TPG LUNs if the fabric is not explictly asking for
283 * tpg_check_demo_mode_login_only() == 1.
285 if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only != NULL) &&
286 (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) == 1))
289 core_tpg_add_node_to_devs(acl, tpg);
291 spin_lock_irq(&tpg->acl_node_lock);
292 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
293 tpg->num_node_acls++;
294 spin_unlock_irq(&tpg->acl_node_lock);
296 pr_debug("%s_TPG[%u] - Added DYNAMIC ACL with TCQ Depth: %d for %s"
297 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
298 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
299 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
303 EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
305 void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
307 while (atomic_read(&nacl->acl_pr_ref_count) != 0)
311 void core_tpg_clear_object_luns(struct se_portal_group *tpg)
316 spin_lock(&tpg->tpg_lun_lock);
317 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
318 lun = &tpg->tpg_lun_list[i];
320 if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) ||
321 (lun->lun_se_dev == NULL))
324 spin_unlock(&tpg->tpg_lun_lock);
325 ret = core_dev_del_lun(tpg, lun->unpacked_lun);
326 spin_lock(&tpg->tpg_lun_lock);
328 spin_unlock(&tpg->tpg_lun_lock);
330 EXPORT_SYMBOL(core_tpg_clear_object_luns);
332 /* core_tpg_add_initiator_node_acl():
336 struct se_node_acl *core_tpg_add_initiator_node_acl(
337 struct se_portal_group *tpg,
338 struct se_node_acl *se_nacl,
339 const char *initiatorname,
342 struct se_node_acl *acl = NULL;
344 spin_lock_irq(&tpg->acl_node_lock);
345 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
347 if (acl->dynamic_node_acl) {
348 acl->dynamic_node_acl = 0;
349 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
350 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
351 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
352 spin_unlock_irq(&tpg->acl_node_lock);
354 * Release the locally allocated struct se_node_acl
355 * because * core_tpg_add_initiator_node_acl() returned
356 * a pointer to an existing demo mode node ACL.
359 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg,
364 pr_err("ACL entry for %s Initiator"
365 " Node %s already exists for TPG %u, ignoring"
366 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
367 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
368 spin_unlock_irq(&tpg->acl_node_lock);
369 return ERR_PTR(-EEXIST);
371 spin_unlock_irq(&tpg->acl_node_lock);
374 pr_err("struct se_node_acl pointer is NULL\n");
375 return ERR_PTR(-EINVAL);
378 * For v4.x logic the se_node_acl_s is hanging off a fabric
379 * dependent structure allocated via
380 * struct target_core_fabric_ops->fabric_make_nodeacl()
384 INIT_LIST_HEAD(&acl->acl_list);
385 INIT_LIST_HEAD(&acl->acl_sess_list);
386 spin_lock_init(&acl->device_list_lock);
387 spin_lock_init(&acl->nacl_sess_lock);
388 atomic_set(&acl->acl_pr_ref_count, 0);
389 acl->queue_depth = queue_depth;
390 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
392 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
393 spin_lock_init(&acl->stats_lock);
395 tpg->se_tpg_tfo->set_default_node_attributes(acl);
397 if (core_create_device_list_for_node(acl) < 0) {
398 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
399 return ERR_PTR(-ENOMEM);
402 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
403 core_free_device_list_for_node(acl, tpg);
404 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
405 return ERR_PTR(-EINVAL);
408 spin_lock_irq(&tpg->acl_node_lock);
409 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
410 tpg->num_node_acls++;
411 spin_unlock_irq(&tpg->acl_node_lock);
414 pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
415 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
416 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
417 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
421 EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
423 /* core_tpg_del_initiator_node_acl():
427 int core_tpg_del_initiator_node_acl(
428 struct se_portal_group *tpg,
429 struct se_node_acl *acl,
432 struct se_session *sess, *sess_tmp;
435 spin_lock_irq(&tpg->acl_node_lock);
436 if (acl->dynamic_node_acl) {
437 acl->dynamic_node_acl = 0;
440 list_del(&acl->acl_list);
441 tpg->num_node_acls--;
442 spin_unlock_irq(&tpg->acl_node_lock);
444 spin_lock_bh(&tpg->session_lock);
445 list_for_each_entry_safe(sess, sess_tmp,
446 &tpg->tpg_sess_list, sess_list) {
447 if (sess->se_node_acl != acl)
450 * Determine if the session needs to be closed by our context.
452 if (!tpg->se_tpg_tfo->shutdown_session(sess))
455 spin_unlock_bh(&tpg->session_lock);
457 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
458 * forcefully shutdown the $FABRIC_MOD session/nexus.
460 tpg->se_tpg_tfo->close_session(sess);
462 spin_lock_bh(&tpg->session_lock);
464 spin_unlock_bh(&tpg->session_lock);
466 core_tpg_wait_for_nacl_pr_ref(acl);
467 core_clear_initiator_node_from_tpg(acl, tpg);
468 core_free_device_list_for_node(acl, tpg);
470 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
471 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
472 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
473 tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
477 EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
479 /* core_tpg_set_initiator_node_queue_depth():
483 int core_tpg_set_initiator_node_queue_depth(
484 struct se_portal_group *tpg,
485 unsigned char *initiatorname,
489 struct se_session *sess, *init_sess = NULL;
490 struct se_node_acl *acl;
493 spin_lock_irq(&tpg->acl_node_lock);
494 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
496 pr_err("Access Control List entry for %s Initiator"
497 " Node %s does not exists for TPG %hu, ignoring"
498 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
499 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
500 spin_unlock_irq(&tpg->acl_node_lock);
503 if (acl->dynamic_node_acl) {
504 acl->dynamic_node_acl = 0;
507 spin_unlock_irq(&tpg->acl_node_lock);
509 spin_lock_bh(&tpg->session_lock);
510 list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
511 if (sess->se_node_acl != acl)
515 pr_err("Unable to change queue depth for %s"
516 " Initiator Node: %s while session is"
517 " operational. To forcefully change the queue"
518 " depth and force session reinstatement"
519 " use the \"force=1\" parameter.\n",
520 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
521 spin_unlock_bh(&tpg->session_lock);
523 spin_lock_irq(&tpg->acl_node_lock);
525 acl->dynamic_node_acl = 1;
526 spin_unlock_irq(&tpg->acl_node_lock);
530 * Determine if the session needs to be closed by our context.
532 if (!tpg->se_tpg_tfo->shutdown_session(sess))
540 * User has requested to change the queue depth for a Initiator Node.
541 * Change the value in the Node's struct se_node_acl, and call
542 * core_set_queue_depth_for_node() to add the requested queue depth.
544 * Finally call tpg->se_tpg_tfo->close_session() to force session
545 * reinstatement to occur if there is an active session for the
546 * $FABRIC_MOD Initiator Node in question.
548 acl->queue_depth = queue_depth;
550 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
551 spin_unlock_bh(&tpg->session_lock);
553 * Force session reinstatement if
554 * core_set_queue_depth_for_node() failed, because we assume
555 * the $FABRIC_MOD has already the set session reinstatement
556 * bit from tpg->se_tpg_tfo->shutdown_session() called above.
559 tpg->se_tpg_tfo->close_session(init_sess);
561 spin_lock_irq(&tpg->acl_node_lock);
563 acl->dynamic_node_acl = 1;
564 spin_unlock_irq(&tpg->acl_node_lock);
567 spin_unlock_bh(&tpg->session_lock);
569 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
570 * forcefully shutdown the $FABRIC_MOD session/nexus.
573 tpg->se_tpg_tfo->close_session(init_sess);
575 pr_debug("Successfully changed queue depth to: %d for Initiator"
576 " Node: %s on %s Target Portal Group: %u\n", queue_depth,
577 initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
578 tpg->se_tpg_tfo->tpg_get_tag(tpg));
580 spin_lock_irq(&tpg->acl_node_lock);
582 acl->dynamic_node_acl = 1;
583 spin_unlock_irq(&tpg->acl_node_lock);
587 EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
589 static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
591 /* Set in core_dev_setup_virtual_lun0() */
592 struct se_device *dev = g_lun0_dev;
593 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
594 u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
597 lun->unpacked_lun = 0;
598 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
599 atomic_set(&lun->lun_acl_count, 0);
600 init_completion(&lun->lun_shutdown_comp);
601 INIT_LIST_HEAD(&lun->lun_acl_list);
602 INIT_LIST_HEAD(&lun->lun_cmd_list);
603 spin_lock_init(&lun->lun_acl_lock);
604 spin_lock_init(&lun->lun_cmd_lock);
605 spin_lock_init(&lun->lun_sep_lock);
607 ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
614 static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
616 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
618 core_tpg_post_dellun(se_tpg, lun);
621 int core_tpg_register(
622 struct target_core_fabric_ops *tfo,
623 struct se_wwn *se_wwn,
624 struct se_portal_group *se_tpg,
625 void *tpg_fabric_ptr,
631 se_tpg->tpg_lun_list = kzalloc((sizeof(struct se_lun) *
632 TRANSPORT_MAX_LUNS_PER_TPG), GFP_KERNEL);
633 if (!se_tpg->tpg_lun_list) {
634 pr_err("Unable to allocate struct se_portal_group->"
639 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
640 lun = &se_tpg->tpg_lun_list[i];
641 lun->unpacked_lun = i;
642 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
643 atomic_set(&lun->lun_acl_count, 0);
644 init_completion(&lun->lun_shutdown_comp);
645 INIT_LIST_HEAD(&lun->lun_acl_list);
646 INIT_LIST_HEAD(&lun->lun_cmd_list);
647 spin_lock_init(&lun->lun_acl_lock);
648 spin_lock_init(&lun->lun_cmd_lock);
649 spin_lock_init(&lun->lun_sep_lock);
652 se_tpg->se_tpg_type = se_tpg_type;
653 se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
654 se_tpg->se_tpg_tfo = tfo;
655 se_tpg->se_tpg_wwn = se_wwn;
656 atomic_set(&se_tpg->tpg_pr_ref_count, 0);
657 INIT_LIST_HEAD(&se_tpg->acl_node_list);
658 INIT_LIST_HEAD(&se_tpg->se_tpg_node);
659 INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
660 spin_lock_init(&se_tpg->acl_node_lock);
661 spin_lock_init(&se_tpg->session_lock);
662 spin_lock_init(&se_tpg->tpg_lun_lock);
664 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
665 if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
671 spin_lock_bh(&tpg_lock);
672 list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
673 spin_unlock_bh(&tpg_lock);
675 pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
676 " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
677 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
678 "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
679 "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
683 EXPORT_SYMBOL(core_tpg_register);
685 int core_tpg_deregister(struct se_portal_group *se_tpg)
687 struct se_node_acl *nacl, *nacl_tmp;
689 pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
690 " for endpoint: %s Portal Tag %u\n",
691 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
692 "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
693 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
694 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
696 spin_lock_bh(&tpg_lock);
697 list_del(&se_tpg->se_tpg_node);
698 spin_unlock_bh(&tpg_lock);
700 while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
703 * Release any remaining demo-mode generated se_node_acl that have
704 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
705 * in transport_deregister_session().
707 spin_lock_irq(&se_tpg->acl_node_lock);
708 list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
710 list_del(&nacl->acl_list);
711 se_tpg->num_node_acls--;
712 spin_unlock_irq(&se_tpg->acl_node_lock);
714 core_tpg_wait_for_nacl_pr_ref(nacl);
715 core_free_device_list_for_node(nacl, se_tpg);
716 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
718 spin_lock_irq(&se_tpg->acl_node_lock);
720 spin_unlock_irq(&se_tpg->acl_node_lock);
722 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
723 core_tpg_release_virtual_lun0(se_tpg);
725 se_tpg->se_tpg_fabric_ptr = NULL;
726 kfree(se_tpg->tpg_lun_list);
729 EXPORT_SYMBOL(core_tpg_deregister);
731 struct se_lun *core_tpg_pre_addlun(
732 struct se_portal_group *tpg,
737 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
738 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
739 "-1: %u for Target Portal Group: %u\n",
740 tpg->se_tpg_tfo->get_fabric_name(),
741 unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
742 tpg->se_tpg_tfo->tpg_get_tag(tpg));
743 return ERR_PTR(-EOVERFLOW);
746 spin_lock(&tpg->tpg_lun_lock);
747 lun = &tpg->tpg_lun_list[unpacked_lun];
748 if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
749 pr_err("TPG Logical Unit Number: %u is already active"
750 " on %s Target Portal Group: %u, ignoring request.\n",
751 unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
752 tpg->se_tpg_tfo->tpg_get_tag(tpg));
753 spin_unlock(&tpg->tpg_lun_lock);
754 return ERR_PTR(-EINVAL);
756 spin_unlock(&tpg->tpg_lun_lock);
761 int core_tpg_post_addlun(
762 struct se_portal_group *tpg,
769 ret = core_dev_export(lun_ptr, tpg, lun);
773 spin_lock(&tpg->tpg_lun_lock);
774 lun->lun_access = lun_access;
775 lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
776 spin_unlock(&tpg->tpg_lun_lock);
781 static void core_tpg_shutdown_lun(
782 struct se_portal_group *tpg,
785 core_clear_lun_from_tpg(lun, tpg);
786 transport_clear_lun_from_sessions(lun);
789 struct se_lun *core_tpg_pre_dellun(
790 struct se_portal_group *tpg,
796 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
797 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
798 "-1: %u for Target Portal Group: %u\n",
799 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
800 TRANSPORT_MAX_LUNS_PER_TPG-1,
801 tpg->se_tpg_tfo->tpg_get_tag(tpg));
802 return ERR_PTR(-EOVERFLOW);
805 spin_lock(&tpg->tpg_lun_lock);
806 lun = &tpg->tpg_lun_list[unpacked_lun];
807 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
808 pr_err("%s Logical Unit Number: %u is not active on"
809 " Target Portal Group: %u, ignoring request.\n",
810 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
811 tpg->se_tpg_tfo->tpg_get_tag(tpg));
812 spin_unlock(&tpg->tpg_lun_lock);
813 return ERR_PTR(-ENODEV);
815 spin_unlock(&tpg->tpg_lun_lock);
820 int core_tpg_post_dellun(
821 struct se_portal_group *tpg,
824 core_tpg_shutdown_lun(tpg, lun);
826 core_dev_unexport(lun->lun_se_dev, tpg, lun);
828 spin_lock(&tpg->tpg_lun_lock);
829 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
830 spin_unlock(&tpg->tpg_lun_lock);