1c66ac9dbSNicholas Bellinger /******************************************************************************* 2c66ac9dbSNicholas Bellinger * Filename: target_core_tpg.c 3c66ac9dbSNicholas Bellinger * 4c66ac9dbSNicholas Bellinger * This file contains generic Target Portal Group related functions. 5c66ac9dbSNicholas Bellinger * 64c76251eSNicholas Bellinger * (c) Copyright 2002-2013 Datera, Inc. 7c66ac9dbSNicholas Bellinger * 8c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 9c66ac9dbSNicholas Bellinger * 10c66ac9dbSNicholas Bellinger * This program is free software; you can redistribute it and/or modify 11c66ac9dbSNicholas Bellinger * it under the terms of the GNU General Public License as published by 12c66ac9dbSNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 13c66ac9dbSNicholas Bellinger * (at your option) any later version. 14c66ac9dbSNicholas Bellinger * 15c66ac9dbSNicholas Bellinger * This program is distributed in the hope that it will be useful, 16c66ac9dbSNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 17c66ac9dbSNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18c66ac9dbSNicholas Bellinger * GNU General Public License for more details. 19c66ac9dbSNicholas Bellinger * 20c66ac9dbSNicholas Bellinger * You should have received a copy of the GNU General Public License 21c66ac9dbSNicholas Bellinger * along with this program; if not, write to the Free Software 22c66ac9dbSNicholas Bellinger * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23c66ac9dbSNicholas Bellinger * 24c66ac9dbSNicholas Bellinger ******************************************************************************/ 25c66ac9dbSNicholas Bellinger 26c66ac9dbSNicholas Bellinger #include <linux/net.h> 27c66ac9dbSNicholas Bellinger #include <linux/string.h> 28c66ac9dbSNicholas Bellinger #include <linux/timer.h> 29c66ac9dbSNicholas Bellinger #include <linux/slab.h> 30c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 31c66ac9dbSNicholas Bellinger #include <linux/in.h> 32c53181afSPaul Gortmaker #include <linux/export.h> 33c66ac9dbSNicholas Bellinger #include <net/sock.h> 34c66ac9dbSNicholas Bellinger #include <net/tcp.h> 35ba929992SBart Van Assche #include <scsi/scsi_proto.h> 36c66ac9dbSNicholas Bellinger 37c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 38c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 39c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h> 40c66ac9dbSNicholas Bellinger 41e26d99aeSChristoph Hellwig #include "target_core_internal.h" 42adf653f9SChristoph Hellwig #include "target_core_alua.h" 43e2480563SNicholas Bellinger #include "target_core_pr.h" 44e986a35aSHannes Reinecke #include "target_core_ua.h" 45e3d6f909SAndy Grover 46e3d6f909SAndy Grover extern struct se_device *g_lun0_dev; 47e3d6f909SAndy Grover 48e3d6f909SAndy Grover static DEFINE_SPINLOCK(tpg_lock); 49e3d6f909SAndy Grover static LIST_HEAD(tpg_list); 50c66ac9dbSNicholas Bellinger 51c66ac9dbSNicholas Bellinger /* __core_tpg_get_initiator_node_acl(): 52c66ac9dbSNicholas Bellinger * 53403edd78SNicholas Bellinger * mutex_lock(&tpg->acl_node_mutex); must be held when calling 54c66ac9dbSNicholas Bellinger */ 55c66ac9dbSNicholas Bellinger struct se_node_acl *__core_tpg_get_initiator_node_acl( 56c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 57c66ac9dbSNicholas Bellinger const char *initiatorname) 58c66ac9dbSNicholas Bellinger { 59c66ac9dbSNicholas Bellinger struct se_node_acl *acl; 60c66ac9dbSNicholas Bellinger 61c66ac9dbSNicholas Bellinger list_for_each_entry(acl, &tpg->acl_node_list, acl_list) { 626708bb27SAndy Grover if (!strcmp(acl->initiatorname, initiatorname)) 63c66ac9dbSNicholas Bellinger return acl; 64c66ac9dbSNicholas Bellinger } 65c66ac9dbSNicholas Bellinger 66c66ac9dbSNicholas Bellinger return NULL; 67c66ac9dbSNicholas Bellinger } 68c66ac9dbSNicholas Bellinger 69c66ac9dbSNicholas Bellinger /* core_tpg_get_initiator_node_acl(): 70c66ac9dbSNicholas Bellinger * 71c66ac9dbSNicholas Bellinger * 72c66ac9dbSNicholas Bellinger */ 73c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_get_initiator_node_acl( 74c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 75c66ac9dbSNicholas Bellinger unsigned char *initiatorname) 76c66ac9dbSNicholas Bellinger { 77c66ac9dbSNicholas Bellinger struct se_node_acl *acl; 7821aaa23bSNicholas Bellinger /* 7921aaa23bSNicholas Bellinger * Obtain se_node_acl->acl_kref using fabric driver provided 8021aaa23bSNicholas Bellinger * initiatorname[] during node acl endpoint lookup driven by 8121aaa23bSNicholas Bellinger * new se_session login. 8221aaa23bSNicholas Bellinger * 8321aaa23bSNicholas Bellinger * The reference is held until se_session shutdown -> release 8421aaa23bSNicholas Bellinger * occurs via fabric driver invoked transport_deregister_session() 8521aaa23bSNicholas Bellinger * or transport_free_session() code. 8621aaa23bSNicholas Bellinger */ 87403edd78SNicholas Bellinger mutex_lock(&tpg->acl_node_mutex); 88fcf29481SNicholas Bellinger acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); 8921aaa23bSNicholas Bellinger if (acl) { 9021aaa23bSNicholas Bellinger if (!kref_get_unless_zero(&acl->acl_kref)) 9121aaa23bSNicholas Bellinger acl = NULL; 9221aaa23bSNicholas Bellinger } 93403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 94c66ac9dbSNicholas Bellinger 95fcf29481SNicholas Bellinger return acl; 96c66ac9dbSNicholas Bellinger } 97b3fde035SThomas Glanzmann EXPORT_SYMBOL(core_tpg_get_initiator_node_acl); 98c66ac9dbSNicholas Bellinger 99e986a35aSHannes Reinecke void core_allocate_nexus_loss_ua( 100e986a35aSHannes Reinecke struct se_node_acl *nacl) 101e986a35aSHannes Reinecke { 102e986a35aSHannes Reinecke struct se_dev_entry *deve; 103e986a35aSHannes Reinecke 104e986a35aSHannes Reinecke if (!nacl) 105e986a35aSHannes Reinecke return; 106e986a35aSHannes Reinecke 107e986a35aSHannes Reinecke rcu_read_lock(); 108e986a35aSHannes Reinecke hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) 109e986a35aSHannes Reinecke core_scsi3_ua_allocate(deve, 0x29, 110e986a35aSHannes Reinecke ASCQ_29H_NEXUS_LOSS_OCCURRED); 111e986a35aSHannes Reinecke rcu_read_unlock(); 112e986a35aSHannes Reinecke } 113e986a35aSHannes Reinecke EXPORT_SYMBOL(core_allocate_nexus_loss_ua); 114e986a35aSHannes Reinecke 115c66ac9dbSNicholas Bellinger /* core_tpg_add_node_to_devs(): 116c66ac9dbSNicholas Bellinger * 117c66ac9dbSNicholas Bellinger * 118c66ac9dbSNicholas Bellinger */ 119c66ac9dbSNicholas Bellinger void core_tpg_add_node_to_devs( 120c66ac9dbSNicholas Bellinger struct se_node_acl *acl, 121df9766caSNicholas Bellinger struct se_portal_group *tpg, 122df9766caSNicholas Bellinger struct se_lun *lun_orig) 123c66ac9dbSNicholas Bellinger { 12403a68b44SAndy Grover bool lun_access_ro = true; 125c66ac9dbSNicholas Bellinger struct se_lun *lun; 126c66ac9dbSNicholas Bellinger struct se_device *dev; 127c66ac9dbSNicholas Bellinger 1286bb82612SNicholas Bellinger mutex_lock(&tpg->tpg_lun_mutex); 1296bb82612SNicholas Bellinger hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) { 130df9766caSNicholas Bellinger if (lun_orig && lun != lun_orig) 131c66ac9dbSNicholas Bellinger continue; 132c66ac9dbSNicholas Bellinger 1334cc987eaSNicholas Bellinger dev = rcu_dereference_check(lun->lun_se_dev, 1344cc987eaSNicholas Bellinger lockdep_is_held(&tpg->tpg_lun_mutex)); 135c66ac9dbSNicholas Bellinger /* 136c66ac9dbSNicholas Bellinger * By default in LIO-Target $FABRIC_MOD, 137c66ac9dbSNicholas Bellinger * demo_mode_write_protect is ON, or READ_ONLY; 138c66ac9dbSNicholas Bellinger */ 1396708bb27SAndy Grover if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) { 14003a68b44SAndy Grover lun_access_ro = false; 141c66ac9dbSNicholas Bellinger } else { 142c66ac9dbSNicholas Bellinger /* 143c66ac9dbSNicholas Bellinger * Allow only optical drives to issue R/W in default RO 144c66ac9dbSNicholas Bellinger * demo mode. 145c66ac9dbSNicholas Bellinger */ 146e3d6f909SAndy Grover if (dev->transport->get_device_type(dev) == TYPE_DISK) 14703a68b44SAndy Grover lun_access_ro = true; 148c66ac9dbSNicholas Bellinger else 14903a68b44SAndy Grover lun_access_ro = false; 150c66ac9dbSNicholas Bellinger } 151c66ac9dbSNicholas Bellinger 152f2d30680SHannes Reinecke pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s" 153c66ac9dbSNicholas Bellinger " access for LUN in Demo Mode\n", 154e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), 155e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, 15603a68b44SAndy Grover lun_access_ro ? "READ-ONLY" : "READ-WRITE"); 157c66ac9dbSNicholas Bellinger 158e80ac6c4SAndy Grover core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun, 15903a68b44SAndy Grover lun_access_ro, acl, tpg); 160e2480563SNicholas Bellinger /* 161e2480563SNicholas Bellinger * Check to see if there are any existing persistent reservation 162e2480563SNicholas Bellinger * APTPL pre-registrations that need to be enabled for this dynamic 163e2480563SNicholas Bellinger * LUN ACL now.. 164e2480563SNicholas Bellinger */ 165e2480563SNicholas Bellinger core_scsi3_check_aptpl_registration(dev, tpg, lun, acl, 166e2480563SNicholas Bellinger lun->unpacked_lun); 167c66ac9dbSNicholas Bellinger } 1686bb82612SNicholas Bellinger mutex_unlock(&tpg->tpg_lun_mutex); 169c66ac9dbSNicholas Bellinger } 170c66ac9dbSNicholas Bellinger 171d36ad77fSNicholas Bellinger static void 172d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(struct se_portal_group *tpg, 173d36ad77fSNicholas Bellinger struct se_node_acl *acl, u32 queue_depth) 174c66ac9dbSNicholas Bellinger { 175d36ad77fSNicholas Bellinger acl->queue_depth = queue_depth; 176d36ad77fSNicholas Bellinger 177c66ac9dbSNicholas Bellinger if (!acl->queue_depth) { 178d36ad77fSNicholas Bellinger pr_warn("Queue depth for %s Initiator Node: %s is 0," 179e3d6f909SAndy Grover "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(), 180c66ac9dbSNicholas Bellinger acl->initiatorname); 181c66ac9dbSNicholas Bellinger acl->queue_depth = 1; 182c66ac9dbSNicholas Bellinger } 183c66ac9dbSNicholas Bellinger } 184c66ac9dbSNicholas Bellinger 185e413f472SChristoph Hellwig static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg, 186e413f472SChristoph Hellwig const unsigned char *initiatorname) 1874a5a75f3SJörn Engel { 188c66ac9dbSNicholas Bellinger struct se_node_acl *acl; 189d36ad77fSNicholas Bellinger u32 queue_depth; 190c66ac9dbSNicholas Bellinger 191144bc4c2SChristoph Hellwig acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size), 192144bc4c2SChristoph Hellwig GFP_KERNEL); 1936708bb27SAndy Grover if (!acl) 194c66ac9dbSNicholas Bellinger return NULL; 195c66ac9dbSNicholas Bellinger 196c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&acl->acl_list); 197c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&acl->acl_sess_list); 19829a05deeSNicholas Bellinger INIT_HLIST_HEAD(&acl->lun_entry_hlist); 199afb999ffSNicholas Bellinger kref_init(&acl->acl_kref); 20001468346SNicholas Bellinger init_completion(&acl->acl_free_comp); 201c66ac9dbSNicholas Bellinger spin_lock_init(&acl->nacl_sess_lock); 20229a05deeSNicholas Bellinger mutex_init(&acl->lun_entry_mutex); 203c66ac9dbSNicholas Bellinger atomic_set(&acl->acl_pr_ref_count, 0); 204d36ad77fSNicholas Bellinger 205e1750d20SChristoph Hellwig if (tpg->se_tpg_tfo->tpg_get_default_depth) 206d36ad77fSNicholas Bellinger queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg); 207e1750d20SChristoph Hellwig else 208d36ad77fSNicholas Bellinger queue_depth = 1; 209d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(tpg, acl, queue_depth); 210d36ad77fSNicholas Bellinger 211c66ac9dbSNicholas Bellinger snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname); 212c66ac9dbSNicholas Bellinger acl->se_tpg = tpg; 213c66ac9dbSNicholas Bellinger acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX); 214c66ac9dbSNicholas Bellinger 215e3d6f909SAndy Grover tpg->se_tpg_tfo->set_default_node_attributes(acl); 216c66ac9dbSNicholas Bellinger 217e413f472SChristoph Hellwig return acl; 218c66ac9dbSNicholas Bellinger } 219c66ac9dbSNicholas Bellinger 220e413f472SChristoph Hellwig static void target_add_node_acl(struct se_node_acl *acl) 221c66ac9dbSNicholas Bellinger { 222e413f472SChristoph Hellwig struct se_portal_group *tpg = acl->se_tpg; 223c66ac9dbSNicholas Bellinger 224403edd78SNicholas Bellinger mutex_lock(&tpg->acl_node_mutex); 225e413f472SChristoph Hellwig list_add_tail(&acl->acl_list, &tpg->acl_node_list); 226403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 227c66ac9dbSNicholas Bellinger 228e413f472SChristoph Hellwig pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s" 229e413f472SChristoph Hellwig " Initiator Node: %s\n", 230e413f472SChristoph Hellwig tpg->se_tpg_tfo->get_fabric_name(), 231e413f472SChristoph Hellwig tpg->se_tpg_tfo->tpg_get_tag(tpg), 232e413f472SChristoph Hellwig acl->dynamic_node_acl ? "DYNAMIC" : "", 233e413f472SChristoph Hellwig acl->queue_depth, 234e413f472SChristoph Hellwig tpg->se_tpg_tfo->get_fabric_name(), 235e413f472SChristoph Hellwig acl->initiatorname); 236c66ac9dbSNicholas Bellinger } 237c66ac9dbSNicholas Bellinger 23821aaa23bSNicholas Bellinger bool target_tpg_has_node_acl(struct se_portal_group *tpg, 23921aaa23bSNicholas Bellinger const char *initiatorname) 24021aaa23bSNicholas Bellinger { 24121aaa23bSNicholas Bellinger struct se_node_acl *acl; 24221aaa23bSNicholas Bellinger bool found = false; 24321aaa23bSNicholas Bellinger 24421aaa23bSNicholas Bellinger mutex_lock(&tpg->acl_node_mutex); 24521aaa23bSNicholas Bellinger list_for_each_entry(acl, &tpg->acl_node_list, acl_list) { 24621aaa23bSNicholas Bellinger if (!strcmp(acl->initiatorname, initiatorname)) { 24721aaa23bSNicholas Bellinger found = true; 24821aaa23bSNicholas Bellinger break; 24921aaa23bSNicholas Bellinger } 25021aaa23bSNicholas Bellinger } 25121aaa23bSNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 25221aaa23bSNicholas Bellinger 25321aaa23bSNicholas Bellinger return found; 25421aaa23bSNicholas Bellinger } 25521aaa23bSNicholas Bellinger EXPORT_SYMBOL(target_tpg_has_node_acl); 25621aaa23bSNicholas Bellinger 257c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_check_initiator_node_acl( 258c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 259c66ac9dbSNicholas Bellinger unsigned char *initiatorname) 260c66ac9dbSNicholas Bellinger { 261c66ac9dbSNicholas Bellinger struct se_node_acl *acl; 262c66ac9dbSNicholas Bellinger 263c66ac9dbSNicholas Bellinger acl = core_tpg_get_initiator_node_acl(tpg, initiatorname); 264c66ac9dbSNicholas Bellinger if (acl) 265c66ac9dbSNicholas Bellinger return acl; 266c66ac9dbSNicholas Bellinger 267c66ac9dbSNicholas Bellinger if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) 268c66ac9dbSNicholas Bellinger return NULL; 269c66ac9dbSNicholas Bellinger 270e413f472SChristoph Hellwig acl = target_alloc_node_acl(tpg, initiatorname); 271c66ac9dbSNicholas Bellinger if (!acl) 272c66ac9dbSNicholas Bellinger return NULL; 27321aaa23bSNicholas Bellinger /* 27421aaa23bSNicholas Bellinger * When allocating a dynamically generated node_acl, go ahead 27521aaa23bSNicholas Bellinger * and take the extra kref now before returning to the fabric 27621aaa23bSNicholas Bellinger * driver caller. 27721aaa23bSNicholas Bellinger * 27821aaa23bSNicholas Bellinger * Note this reference will be released at session shutdown 27921aaa23bSNicholas Bellinger * time within transport_free_session() code. 28021aaa23bSNicholas Bellinger */ 28121aaa23bSNicholas Bellinger kref_get(&acl->acl_kref); 282c66ac9dbSNicholas Bellinger acl->dynamic_node_acl = 1; 283c66ac9dbSNicholas Bellinger 284052605c6SNicholas Bellinger /* 285052605c6SNicholas Bellinger * Here we only create demo-mode MappedLUNs from the active 28635d1efe8SMasanari Iida * TPG LUNs if the fabric is not explicitly asking for 287052605c6SNicholas Bellinger * tpg_check_demo_mode_login_only() == 1. 288052605c6SNicholas Bellinger */ 289cdf88a2fSAndy Grover if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) || 290cdf88a2fSAndy Grover (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1)) 291df9766caSNicholas Bellinger core_tpg_add_node_to_devs(acl, tpg, NULL); 292c66ac9dbSNicholas Bellinger 293e413f472SChristoph Hellwig target_add_node_acl(acl); 294c66ac9dbSNicholas Bellinger return acl; 295c66ac9dbSNicholas Bellinger } 296c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_check_initiator_node_acl); 297c66ac9dbSNicholas Bellinger 298c66ac9dbSNicholas Bellinger void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl) 299c66ac9dbSNicholas Bellinger { 300c66ac9dbSNicholas Bellinger while (atomic_read(&nacl->acl_pr_ref_count) != 0) 301c66ac9dbSNicholas Bellinger cpu_relax(); 302c66ac9dbSNicholas Bellinger } 303c66ac9dbSNicholas Bellinger 304c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_add_initiator_node_acl( 305c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 306c7d6a803SChristoph Hellwig const char *initiatorname) 307c66ac9dbSNicholas Bellinger { 308c7d6a803SChristoph Hellwig struct se_node_acl *acl; 309c66ac9dbSNicholas Bellinger 310403edd78SNicholas Bellinger mutex_lock(&tpg->acl_node_mutex); 311c66ac9dbSNicholas Bellinger acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); 3126708bb27SAndy Grover if (acl) { 313c66ac9dbSNicholas Bellinger if (acl->dynamic_node_acl) { 314c66ac9dbSNicholas Bellinger acl->dynamic_node_acl = 0; 3156708bb27SAndy Grover pr_debug("%s_TPG[%u] - Replacing dynamic ACL" 316e3d6f909SAndy Grover " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), 317e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname); 318403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 319e413f472SChristoph Hellwig return acl; 320c66ac9dbSNicholas Bellinger } 321c66ac9dbSNicholas Bellinger 3226708bb27SAndy Grover pr_err("ACL entry for %s Initiator" 323c66ac9dbSNicholas Bellinger " Node %s already exists for TPG %u, ignoring" 324e3d6f909SAndy Grover " request.\n", tpg->se_tpg_tfo->get_fabric_name(), 325e3d6f909SAndy Grover initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg)); 326403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 327c66ac9dbSNicholas Bellinger return ERR_PTR(-EEXIST); 328c66ac9dbSNicholas Bellinger } 329403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 330c66ac9dbSNicholas Bellinger 331e413f472SChristoph Hellwig acl = target_alloc_node_acl(tpg, initiatorname); 332e413f472SChristoph Hellwig if (!acl) 333c66ac9dbSNicholas Bellinger return ERR_PTR(-ENOMEM); 334c66ac9dbSNicholas Bellinger 335e413f472SChristoph Hellwig target_add_node_acl(acl); 336c66ac9dbSNicholas Bellinger return acl; 337c66ac9dbSNicholas Bellinger } 338c66ac9dbSNicholas Bellinger 339fba81f88SChristoph Hellwig static void target_shutdown_sessions(struct se_node_acl *acl) 340bc6e6bb4SChristoph Hellwig { 341bc6e6bb4SChristoph Hellwig struct se_session *sess; 342bc6e6bb4SChristoph Hellwig unsigned long flags; 343bc6e6bb4SChristoph Hellwig 344bc6e6bb4SChristoph Hellwig restart: 345fba81f88SChristoph Hellwig spin_lock_irqsave(&acl->nacl_sess_lock, flags); 346bc6e6bb4SChristoph Hellwig list_for_each_entry(sess, &acl->acl_sess_list, sess_acl_list) { 347bc6e6bb4SChristoph Hellwig if (sess->sess_tearing_down) 348bc6e6bb4SChristoph Hellwig continue; 349bc6e6bb4SChristoph Hellwig 350fba81f88SChristoph Hellwig list_del_init(&sess->sess_acl_list); 351bc6e6bb4SChristoph Hellwig spin_unlock_irqrestore(&acl->nacl_sess_lock, flags); 352d94331faSChristoph Hellwig 353d94331faSChristoph Hellwig if (acl->se_tpg->se_tpg_tfo->close_session) 354d94331faSChristoph Hellwig acl->se_tpg->se_tpg_tfo->close_session(sess); 355bc6e6bb4SChristoph Hellwig goto restart; 356bc6e6bb4SChristoph Hellwig } 357bc6e6bb4SChristoph Hellwig spin_unlock_irqrestore(&acl->nacl_sess_lock, flags); 358bc6e6bb4SChristoph Hellwig } 359bc6e6bb4SChristoph Hellwig 360c7d6a803SChristoph Hellwig void core_tpg_del_initiator_node_acl(struct se_node_acl *acl) 361c66ac9dbSNicholas Bellinger { 362c7d6a803SChristoph Hellwig struct se_portal_group *tpg = acl->se_tpg; 363c66ac9dbSNicholas Bellinger 364403edd78SNicholas Bellinger mutex_lock(&tpg->acl_node_mutex); 365bc6e6bb4SChristoph Hellwig if (acl->dynamic_node_acl) 366c66ac9dbSNicholas Bellinger acl->dynamic_node_acl = 0; 367c66ac9dbSNicholas Bellinger list_del(&acl->acl_list); 368403edd78SNicholas Bellinger mutex_unlock(&tpg->acl_node_mutex); 369c66ac9dbSNicholas Bellinger 370fba81f88SChristoph Hellwig target_shutdown_sessions(acl); 371337c0607SNicholas Bellinger 372337c0607SNicholas Bellinger target_put_nacl(acl); 373337c0607SNicholas Bellinger /* 374337c0607SNicholas Bellinger * Wait for last target_put_nacl() to complete in target_complete_nacl() 375337c0607SNicholas Bellinger * for active fabric session transport_deregister_session() callbacks. 376337c0607SNicholas Bellinger */ 377337c0607SNicholas Bellinger wait_for_completion(&acl->acl_free_comp); 378c66ac9dbSNicholas Bellinger 379c66ac9dbSNicholas Bellinger core_tpg_wait_for_nacl_pr_ref(acl); 380c66ac9dbSNicholas Bellinger core_free_device_list_for_node(acl, tpg); 381c66ac9dbSNicholas Bellinger 3826708bb27SAndy Grover pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s" 383e3d6f909SAndy Grover " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(), 384e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth, 385e3d6f909SAndy Grover tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname); 386c66ac9dbSNicholas Bellinger 387144bc4c2SChristoph Hellwig kfree(acl); 388c66ac9dbSNicholas Bellinger } 389c66ac9dbSNicholas Bellinger 390c66ac9dbSNicholas Bellinger /* core_tpg_set_initiator_node_queue_depth(): 391c66ac9dbSNicholas Bellinger * 392c66ac9dbSNicholas Bellinger * 393c66ac9dbSNicholas Bellinger */ 394c66ac9dbSNicholas Bellinger int core_tpg_set_initiator_node_queue_depth( 395d36ad77fSNicholas Bellinger struct se_node_acl *acl, 396d36ad77fSNicholas Bellinger u32 queue_depth) 397c66ac9dbSNicholas Bellinger { 398d36ad77fSNicholas Bellinger struct se_portal_group *tpg = acl->se_tpg; 399c66ac9dbSNicholas Bellinger 400c66ac9dbSNicholas Bellinger /* 40146861cddSNicholas Bellinger * Allow the setting of se_node_acl queue_depth to be idempotent, 40246861cddSNicholas Bellinger * and not force a session shutdown event if the value is not 40346861cddSNicholas Bellinger * changing. 40446861cddSNicholas Bellinger */ 40546861cddSNicholas Bellinger if (acl->queue_depth == queue_depth) 40646861cddSNicholas Bellinger return 0; 40746861cddSNicholas Bellinger /* 408c66ac9dbSNicholas Bellinger * User has requested to change the queue depth for a Initiator Node. 409c66ac9dbSNicholas Bellinger * Change the value in the Node's struct se_node_acl, and call 410d36ad77fSNicholas Bellinger * target_set_nacl_queue_depth() to set the new queue depth. 411d36ad77fSNicholas Bellinger */ 412d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(tpg, acl, queue_depth); 413d36ad77fSNicholas Bellinger 414d36ad77fSNicholas Bellinger /* 415bc6e6bb4SChristoph Hellwig * Shutdown all pending sessions to force session reinstatement. 416c66ac9dbSNicholas Bellinger */ 417fba81f88SChristoph Hellwig target_shutdown_sessions(acl); 418c66ac9dbSNicholas Bellinger 419bfb9035cSJoe Perches pr_debug("Successfully changed queue depth to: %d for Initiator" 420d36ad77fSNicholas Bellinger " Node: %s on %s Target Portal Group: %u\n", acl->queue_depth, 421d36ad77fSNicholas Bellinger acl->initiatorname, tpg->se_tpg_tfo->get_fabric_name(), 422e3d6f909SAndy Grover tpg->se_tpg_tfo->tpg_get_tag(tpg)); 423c66ac9dbSNicholas Bellinger 424c66ac9dbSNicholas Bellinger return 0; 425c66ac9dbSNicholas Bellinger } 426c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth); 427c66ac9dbSNicholas Bellinger 42879e62fc3SAndy Grover /* core_tpg_set_initiator_node_tag(): 42979e62fc3SAndy Grover * 43079e62fc3SAndy Grover * Initiator nodeacl tags are not used internally, but may be used by 43179e62fc3SAndy Grover * userspace to emulate aliases or groups. 43279e62fc3SAndy Grover * Returns length of newly-set tag or -EINVAL. 43379e62fc3SAndy Grover */ 43479e62fc3SAndy Grover int core_tpg_set_initiator_node_tag( 43579e62fc3SAndy Grover struct se_portal_group *tpg, 43679e62fc3SAndy Grover struct se_node_acl *acl, 43779e62fc3SAndy Grover const char *new_tag) 43879e62fc3SAndy Grover { 43979e62fc3SAndy Grover if (strlen(new_tag) >= MAX_ACL_TAG_SIZE) 44079e62fc3SAndy Grover return -EINVAL; 44179e62fc3SAndy Grover 44279e62fc3SAndy Grover if (!strncmp("NULL", new_tag, 4)) { 44379e62fc3SAndy Grover acl->acl_tag[0] = '\0'; 44479e62fc3SAndy Grover return 0; 44579e62fc3SAndy Grover } 44679e62fc3SAndy Grover 44779e62fc3SAndy Grover return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag); 44879e62fc3SAndy Grover } 44979e62fc3SAndy Grover EXPORT_SYMBOL(core_tpg_set_initiator_node_tag); 45079e62fc3SAndy Grover 4515277797dSNicholas Bellinger static void core_tpg_lun_ref_release(struct percpu_ref *ref) 4525277797dSNicholas Bellinger { 4535277797dSNicholas Bellinger struct se_lun *lun = container_of(ref, struct se_lun, lun_ref); 4545277797dSNicholas Bellinger 455bd4e2d29SNicholas Bellinger complete(&lun->lun_shutdown_comp); 4565277797dSNicholas Bellinger } 4575277797dSNicholas Bellinger 4582bce1a6dSBart Van Assche /* Does not change se_wwn->priv. */ 459c66ac9dbSNicholas Bellinger int core_tpg_register( 460c66ac9dbSNicholas Bellinger struct se_wwn *se_wwn, 461c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg, 462e4aae5afSChristoph Hellwig int proto_id) 463c66ac9dbSNicholas Bellinger { 464adf653f9SChristoph Hellwig int ret; 465c66ac9dbSNicholas Bellinger 466bc0c94b1SNicholas Bellinger if (!se_tpg) 467bc0c94b1SNicholas Bellinger return -EINVAL; 468bc0c94b1SNicholas Bellinger /* 469bc0c94b1SNicholas Bellinger * For the typical case where core_tpg_register() is called by a 470bc0c94b1SNicholas Bellinger * fabric driver from target_core_fabric_ops->fabric_make_tpg() 471bc0c94b1SNicholas Bellinger * configfs context, use the original tf_ops pointer already saved 472bc0c94b1SNicholas Bellinger * by target-core in target_fabric_make_wwn(). 473bc0c94b1SNicholas Bellinger * 474bc0c94b1SNicholas Bellinger * Otherwise, for special cases like iscsi-target discovery TPGs 475bc0c94b1SNicholas Bellinger * the caller is responsible for setting ->se_tpg_tfo ahead of 476bc0c94b1SNicholas Bellinger * calling core_tpg_register(). 477bc0c94b1SNicholas Bellinger */ 478bc0c94b1SNicholas Bellinger if (se_wwn) 479bc0c94b1SNicholas Bellinger se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops; 480bc0c94b1SNicholas Bellinger 481bc0c94b1SNicholas Bellinger if (!se_tpg->se_tpg_tfo) { 482bc0c94b1SNicholas Bellinger pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n"); 483bc0c94b1SNicholas Bellinger return -EINVAL; 484c66ac9dbSNicholas Bellinger } 485c66ac9dbSNicholas Bellinger 4866bb82612SNicholas Bellinger INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist); 487e4aae5afSChristoph Hellwig se_tpg->proto_id = proto_id; 488c66ac9dbSNicholas Bellinger se_tpg->se_tpg_wwn = se_wwn; 489c66ac9dbSNicholas Bellinger atomic_set(&se_tpg->tpg_pr_ref_count, 0); 490c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&se_tpg->acl_node_list); 491e3d6f909SAndy Grover INIT_LIST_HEAD(&se_tpg->se_tpg_node); 492c66ac9dbSNicholas Bellinger INIT_LIST_HEAD(&se_tpg->tpg_sess_list); 493c66ac9dbSNicholas Bellinger spin_lock_init(&se_tpg->session_lock); 4946bb82612SNicholas Bellinger mutex_init(&se_tpg->tpg_lun_mutex); 495403edd78SNicholas Bellinger mutex_init(&se_tpg->acl_node_mutex); 496c66ac9dbSNicholas Bellinger 497e4aae5afSChristoph Hellwig if (se_tpg->proto_id >= 0) { 498adf653f9SChristoph Hellwig se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0); 499adf653f9SChristoph Hellwig if (IS_ERR(se_tpg->tpg_virt_lun0)) 500adf653f9SChristoph Hellwig return PTR_ERR(se_tpg->tpg_virt_lun0); 501adf653f9SChristoph Hellwig 502adf653f9SChristoph Hellwig ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0, 50303a68b44SAndy Grover true, g_lun0_dev); 504adf653f9SChristoph Hellwig if (ret < 0) { 505adf653f9SChristoph Hellwig kfree(se_tpg->tpg_virt_lun0); 506adf653f9SChristoph Hellwig return ret; 507c66ac9dbSNicholas Bellinger } 508c66ac9dbSNicholas Bellinger } 509c66ac9dbSNicholas Bellinger 510e3d6f909SAndy Grover spin_lock_bh(&tpg_lock); 511e3d6f909SAndy Grover list_add_tail(&se_tpg->se_tpg_node, &tpg_list); 512e3d6f909SAndy Grover spin_unlock_bh(&tpg_lock); 513c66ac9dbSNicholas Bellinger 514e4aae5afSChristoph Hellwig pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, " 515bc0c94b1SNicholas Bellinger "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->get_fabric_name(), 516bc0c94b1SNicholas Bellinger se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ? 517bc0c94b1SNicholas Bellinger se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL, 518bc0c94b1SNicholas Bellinger se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg)); 519c66ac9dbSNicholas Bellinger 520c66ac9dbSNicholas Bellinger return 0; 521c66ac9dbSNicholas Bellinger } 522c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_register); 523c66ac9dbSNicholas Bellinger 524c66ac9dbSNicholas Bellinger int core_tpg_deregister(struct se_portal_group *se_tpg) 525c66ac9dbSNicholas Bellinger { 526e4aae5afSChristoph Hellwig const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo; 527e89d15eeSNicholas Bellinger struct se_node_acl *nacl, *nacl_tmp; 52822793de5SNicholas Bellinger LIST_HEAD(node_list); 529e89d15eeSNicholas Bellinger 530e4aae5afSChristoph Hellwig pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, " 531e4aae5afSChristoph Hellwig "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(), 532e4aae5afSChristoph Hellwig tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL, 533e4aae5afSChristoph Hellwig se_tpg->proto_id, tfo->tpg_get_tag(se_tpg)); 534c66ac9dbSNicholas Bellinger 535e3d6f909SAndy Grover spin_lock_bh(&tpg_lock); 536e3d6f909SAndy Grover list_del(&se_tpg->se_tpg_node); 537e3d6f909SAndy Grover spin_unlock_bh(&tpg_lock); 538c66ac9dbSNicholas Bellinger 539c66ac9dbSNicholas Bellinger while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0) 540c66ac9dbSNicholas Bellinger cpu_relax(); 541403edd78SNicholas Bellinger 54222793de5SNicholas Bellinger mutex_lock(&se_tpg->acl_node_mutex); 54322793de5SNicholas Bellinger list_splice_init(&se_tpg->acl_node_list, &node_list); 54422793de5SNicholas Bellinger mutex_unlock(&se_tpg->acl_node_mutex); 545e89d15eeSNicholas Bellinger /* 546e89d15eeSNicholas Bellinger * Release any remaining demo-mode generated se_node_acl that have 547e89d15eeSNicholas Bellinger * not been released because of TFO->tpg_check_demo_mode_cache() == 1 548e89d15eeSNicholas Bellinger * in transport_deregister_session(). 549e89d15eeSNicholas Bellinger */ 55022793de5SNicholas Bellinger list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) { 551e89d15eeSNicholas Bellinger list_del(&nacl->acl_list); 552e89d15eeSNicholas Bellinger 553e89d15eeSNicholas Bellinger core_tpg_wait_for_nacl_pr_ref(nacl); 554e89d15eeSNicholas Bellinger core_free_device_list_for_node(nacl, se_tpg); 555144bc4c2SChristoph Hellwig kfree(nacl); 556e89d15eeSNicholas Bellinger } 557c66ac9dbSNicholas Bellinger 558adf653f9SChristoph Hellwig if (se_tpg->proto_id >= 0) { 559adf653f9SChristoph Hellwig core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0); 560adf653f9SChristoph Hellwig kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head); 561adf653f9SChristoph Hellwig } 562c66ac9dbSNicholas Bellinger 563c66ac9dbSNicholas Bellinger return 0; 564c66ac9dbSNicholas Bellinger } 565c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_deregister); 566c66ac9dbSNicholas Bellinger 567d344f8a1SAndy Grover struct se_lun *core_tpg_alloc_lun( 568c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 569f2d30680SHannes Reinecke u64 unpacked_lun) 570c66ac9dbSNicholas Bellinger { 571c66ac9dbSNicholas Bellinger struct se_lun *lun; 572c66ac9dbSNicholas Bellinger 5736bb82612SNicholas Bellinger lun = kzalloc(sizeof(*lun), GFP_KERNEL); 5746bb82612SNicholas Bellinger if (!lun) { 5756bb82612SNicholas Bellinger pr_err("Unable to allocate se_lun memory\n"); 5766bb82612SNicholas Bellinger return ERR_PTR(-ENOMEM); 577c66ac9dbSNicholas Bellinger } 5786bb82612SNicholas Bellinger lun->unpacked_lun = unpacked_lun; 5796bb82612SNicholas Bellinger lun->lun_link_magic = SE_LUN_LINK_MAGIC; 5806bb82612SNicholas Bellinger atomic_set(&lun->lun_acl_count, 0); 5816bb82612SNicholas Bellinger init_completion(&lun->lun_ref_comp); 582bd4e2d29SNicholas Bellinger init_completion(&lun->lun_shutdown_comp); 583adf653f9SChristoph Hellwig INIT_LIST_HEAD(&lun->lun_deve_list); 584adf653f9SChristoph Hellwig INIT_LIST_HEAD(&lun->lun_dev_link); 585adf653f9SChristoph Hellwig atomic_set(&lun->lun_tg_pt_secondary_offline, 0); 586adf653f9SChristoph Hellwig spin_lock_init(&lun->lun_deve_lock); 587adf653f9SChristoph Hellwig mutex_init(&lun->lun_tg_pt_md_mutex); 588adf653f9SChristoph Hellwig INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link); 589adf653f9SChristoph Hellwig spin_lock_init(&lun->lun_tg_pt_gp_lock); 590adf653f9SChristoph Hellwig lun->lun_tpg = tpg; 591c66ac9dbSNicholas Bellinger 592c66ac9dbSNicholas Bellinger return lun; 593c66ac9dbSNicholas Bellinger } 594c66ac9dbSNicholas Bellinger 595d344f8a1SAndy Grover int core_tpg_add_lun( 596c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 597c66ac9dbSNicholas Bellinger struct se_lun *lun, 59803a68b44SAndy Grover bool lun_access_ro, 599340dbf72SAndy Grover struct se_device *dev) 600c66ac9dbSNicholas Bellinger { 601e3d6f909SAndy Grover int ret; 602e3d6f909SAndy Grover 6032aad2a86STejun Heo ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0, 604a34375efSTejun Heo GFP_KERNEL); 605e3d6f909SAndy Grover if (ret < 0) 606adf653f9SChristoph Hellwig goto out; 607c66ac9dbSNicholas Bellinger 608adf653f9SChristoph Hellwig ret = core_alloc_rtpi(lun, dev); 609adf653f9SChristoph Hellwig if (ret) 610adf653f9SChristoph Hellwig goto out_kill_ref; 6115277797dSNicholas Bellinger 612530c6891SMike Christie if (!(dev->transport->transport_flags & 613530c6891SMike Christie TRANSPORT_FLAG_PASSTHROUGH_ALUA) && 614adf653f9SChristoph Hellwig !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) 615adf653f9SChristoph Hellwig target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp); 616c66ac9dbSNicholas Bellinger 6176bb82612SNicholas Bellinger mutex_lock(&tpg->tpg_lun_mutex); 618adf653f9SChristoph Hellwig 619adf653f9SChristoph Hellwig spin_lock(&dev->se_port_lock); 6204cc987eaSNicholas Bellinger lun->lun_index = dev->dev_index; 621adf653f9SChristoph Hellwig rcu_assign_pointer(lun->lun_se_dev, dev); 622adf653f9SChristoph Hellwig dev->export_count++; 623adf653f9SChristoph Hellwig list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list); 624adf653f9SChristoph Hellwig spin_unlock(&dev->se_port_lock); 625adf653f9SChristoph Hellwig 626eeeb9522SNicholas Bellinger if (dev->dev_flags & DF_READ_ONLY) 62703a68b44SAndy Grover lun->lun_access_ro = true; 628eeeb9522SNicholas Bellinger else 62903a68b44SAndy Grover lun->lun_access_ro = lun_access_ro; 6306bb82612SNicholas Bellinger if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) 6316bb82612SNicholas Bellinger hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist); 6326bb82612SNicholas Bellinger mutex_unlock(&tpg->tpg_lun_mutex); 633c66ac9dbSNicholas Bellinger 634c66ac9dbSNicholas Bellinger return 0; 635adf653f9SChristoph Hellwig 636adf653f9SChristoph Hellwig out_kill_ref: 637adf653f9SChristoph Hellwig percpu_ref_exit(&lun->lun_ref); 638adf653f9SChristoph Hellwig out: 639adf653f9SChristoph Hellwig return ret; 640c66ac9dbSNicholas Bellinger } 641c66ac9dbSNicholas Bellinger 642cd9d7cbaSAndy Grover void core_tpg_remove_lun( 643c66ac9dbSNicholas Bellinger struct se_portal_group *tpg, 644c66ac9dbSNicholas Bellinger struct se_lun *lun) 645c66ac9dbSNicholas Bellinger { 6464cc987eaSNicholas Bellinger /* 6474cc987eaSNicholas Bellinger * rcu_dereference_raw protected by se_lun->lun_group symlink 6484cc987eaSNicholas Bellinger * reference to se_device->dev_group. 6494cc987eaSNicholas Bellinger */ 6504cc987eaSNicholas Bellinger struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev); 6516bb82612SNicholas Bellinger 65249cb77e2SNicholas Bellinger lun->lun_shutdown = true; 65349cb77e2SNicholas Bellinger 6544a9a6c8dSNicholas Bellinger core_clear_lun_from_tpg(lun, tpg); 6559e37d042SNicholas Bellinger /* 6569e37d042SNicholas Bellinger * Wait for any active I/O references to percpu se_lun->lun_ref to 6579e37d042SNicholas Bellinger * be released. Also, se_lun->lun_ref is now used by PR and ALUA 6589e37d042SNicholas Bellinger * logic when referencing a remote target port during ALL_TGT_PT=1 6599e37d042SNicholas Bellinger * and generating UNIT_ATTENTIONs for ALUA access state transition. 6609e37d042SNicholas Bellinger */ 6614a9a6c8dSNicholas Bellinger transport_clear_lun_ref(lun); 662c66ac9dbSNicholas Bellinger 6636bb82612SNicholas Bellinger mutex_lock(&tpg->tpg_lun_mutex); 664adf653f9SChristoph Hellwig if (lun->lun_se_dev) { 665adf653f9SChristoph Hellwig target_detach_tg_pt_gp(lun); 666c66ac9dbSNicholas Bellinger 667adf653f9SChristoph Hellwig spin_lock(&dev->se_port_lock); 668adf653f9SChristoph Hellwig list_del(&lun->lun_dev_link); 669adf653f9SChristoph Hellwig dev->export_count--; 670adf653f9SChristoph Hellwig rcu_assign_pointer(lun->lun_se_dev, NULL); 671adf653f9SChristoph Hellwig spin_unlock(&dev->se_port_lock); 672adf653f9SChristoph Hellwig } 6736bb82612SNicholas Bellinger if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) 6746bb82612SNicholas Bellinger hlist_del_rcu(&lun->link); 67549cb77e2SNicholas Bellinger 67649cb77e2SNicholas Bellinger lun->lun_shutdown = false; 6776bb82612SNicholas Bellinger mutex_unlock(&tpg->tpg_lun_mutex); 678c66ac9dbSNicholas Bellinger 6799a1049daSTejun Heo percpu_ref_exit(&lun->lun_ref); 680c66ac9dbSNicholas Bellinger } 681