blob: 2d0381dd105c4998f225603e80bc22e2a6245c08 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_tpg.c
3 *
4 * This file contains generic Target Portal Group related functions.
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2002-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.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 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 ******************************************************************************/
25
26#include <linux/net.h>
27#include <linux/string.h>
28#include <linux/timer.h>
29#include <linux/slab.h>
30#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#include <linux/in.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040032#include <linux/export.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080033#include <net/sock.h>
34#include <net/tcp.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020035#include <scsi/scsi_proto.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036
37#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050038#include <target/target_core_backend.h>
39#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040
Christoph Hellwige26d99a2011-11-14 12:30:30 -050041#include "target_core_internal.h"
Christoph Hellwigadf653f2015-05-25 21:33:08 -070042#include "target_core_alua.h"
Nicholas Bellingere2480562014-10-04 04:23:15 +000043#include "target_core_pr.h"
Hannes Reineckee986a352015-06-18 11:43:38 +020044#include "target_core_ua.h"
Andy Grovere3d6f902011-07-19 08:55:10 +000045
46extern struct se_device *g_lun0_dev;
47
48static DEFINE_SPINLOCK(tpg_lock);
49static LIST_HEAD(tpg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051/* __core_tpg_get_initiator_node_acl():
52 *
Nicholas Bellinger403edd72015-03-08 22:33:47 +000053 * mutex_lock(&tpg->acl_node_mutex); must be held when calling
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054 */
55struct se_node_acl *__core_tpg_get_initiator_node_acl(
56 struct se_portal_group *tpg,
57 const char *initiatorname)
58{
59 struct se_node_acl *acl;
60
61 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -070062 if (!strcmp(acl->initiatorname, initiatorname))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063 return acl;
64 }
65
66 return NULL;
67}
68
69/* core_tpg_get_initiator_node_acl():
70 *
71 *
72 */
73struct se_node_acl *core_tpg_get_initiator_node_acl(
74 struct se_portal_group *tpg,
75 unsigned char *initiatorname)
76{
77 struct se_node_acl *acl;
78
Nicholas Bellinger403edd72015-03-08 22:33:47 +000079 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerfcf29482013-02-18 18:00:33 -080080 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
Nicholas Bellinger403edd72015-03-08 22:33:47 +000081 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082
Nicholas Bellingerfcf29482013-02-18 18:00:33 -080083 return acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084}
Thomas Glanzmannb3fde032013-10-07 23:13:02 +020085EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080086
Hannes Reineckee986a352015-06-18 11:43:38 +020087void core_allocate_nexus_loss_ua(
88 struct se_node_acl *nacl)
89{
90 struct se_dev_entry *deve;
91
92 if (!nacl)
93 return;
94
95 rcu_read_lock();
96 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
97 core_scsi3_ua_allocate(deve, 0x29,
98 ASCQ_29H_NEXUS_LOSS_OCCURRED);
99 rcu_read_unlock();
100}
101EXPORT_SYMBOL(core_allocate_nexus_loss_ua);
102
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103/* core_tpg_add_node_to_devs():
104 *
105 *
106 */
107void core_tpg_add_node_to_devs(
108 struct se_node_acl *acl,
Nicholas Bellingerdf9766c2015-05-22 02:05:19 +0000109 struct se_portal_group *tpg,
110 struct se_lun *lun_orig)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 u32 lun_access = 0;
113 struct se_lun *lun;
114 struct se_device *dev;
115
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700116 mutex_lock(&tpg->tpg_lun_mutex);
117 hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
Nicholas Bellingerdf9766c2015-05-22 02:05:19 +0000118 if (lun_orig && lun != lun_orig)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 continue;
120
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700121 dev = rcu_dereference_check(lun->lun_se_dev,
122 lockdep_is_held(&tpg->tpg_lun_mutex));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800123 /*
124 * By default in LIO-Target $FABRIC_MOD,
125 * demo_mode_write_protect is ON, or READ_ONLY;
126 */
Andy Grover6708bb22011-06-08 10:36:43 -0700127 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
Nicholas Bellinger58d92612012-03-20 21:26:48 -0700128 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800129 } else {
130 /*
131 * Allow only optical drives to issue R/W in default RO
132 * demo mode.
133 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000134 if (dev->transport->get_device_type(dev) == TYPE_DISK)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
136 else
137 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
138 }
139
Hannes Reineckef2d30682015-06-10 08:41:22 +0200140 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 " access for LUN in Demo Mode\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000142 tpg->se_tpg_tfo->get_fabric_name(),
143 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144 (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
145 "READ-WRITE" : "READ-ONLY");
146
Andy Grovere80ac6c2012-07-12 17:34:58 -0700147 core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700148 lun_access, acl, tpg);
Nicholas Bellingere2480562014-10-04 04:23:15 +0000149 /*
150 * Check to see if there are any existing persistent reservation
151 * APTPL pre-registrations that need to be enabled for this dynamic
152 * LUN ACL now..
153 */
154 core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
155 lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800156 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700157 mutex_unlock(&tpg->tpg_lun_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800158}
159
160/* core_set_queue_depth_for_node():
161 *
162 *
163 */
164static int core_set_queue_depth_for_node(
165 struct se_portal_group *tpg,
166 struct se_node_acl *acl)
167{
168 if (!acl->queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700169 pr_err("Queue depth for %s Initiator Node: %s is 0,"
Andy Grovere3d6f902011-07-19 08:55:10 +0000170 "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171 acl->initiatorname);
172 acl->queue_depth = 1;
173 }
174
175 return 0;
176}
177
Christoph Hellwige413f472015-04-13 19:51:15 +0200178static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg,
179 const unsigned char *initiatorname)
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400180{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800181 struct se_node_acl *acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800182
Christoph Hellwig144bc4c2015-04-13 19:51:16 +0200183 acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size),
184 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700185 if (!acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800186 return NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187
188 INIT_LIST_HEAD(&acl->acl_list);
189 INIT_LIST_HEAD(&acl->acl_sess_list);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700190 INIT_HLIST_HEAD(&acl->lun_entry_hlist);
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800191 kref_init(&acl->acl_kref);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800192 init_completion(&acl->acl_free_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193 spin_lock_init(&acl->nacl_sess_lock);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700194 mutex_init(&acl->lun_entry_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800195 atomic_set(&acl->acl_pr_ref_count, 0);
Christoph Hellwige1750d22015-04-13 19:51:13 +0200196 if (tpg->se_tpg_tfo->tpg_get_default_depth)
197 acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
198 else
199 acl->queue_depth = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800200 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
201 acl->se_tpg = tpg;
202 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203
Andy Grovere3d6f902011-07-19 08:55:10 +0000204 tpg->se_tpg_tfo->set_default_node_attributes(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205
Christoph Hellwige413f472015-04-13 19:51:15 +0200206 if (core_set_queue_depth_for_node(tpg, acl) < 0)
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700207 goto out_free_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208
Christoph Hellwige413f472015-04-13 19:51:15 +0200209 return acl;
210
Christoph Hellwige413f472015-04-13 19:51:15 +0200211out_free_acl:
Christoph Hellwig144bc4c2015-04-13 19:51:16 +0200212 kfree(acl);
Christoph Hellwige413f472015-04-13 19:51:15 +0200213 return NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214}
215
Christoph Hellwige413f472015-04-13 19:51:15 +0200216static void target_add_node_acl(struct se_node_acl *acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217{
Christoph Hellwige413f472015-04-13 19:51:15 +0200218 struct se_portal_group *tpg = acl->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800219
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000220 mutex_lock(&tpg->acl_node_mutex);
Christoph Hellwige413f472015-04-13 19:51:15 +0200221 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
222 tpg->num_node_acls++;
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000223 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224
Christoph Hellwige413f472015-04-13 19:51:15 +0200225 pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
226 " Initiator Node: %s\n",
227 tpg->se_tpg_tfo->get_fabric_name(),
228 tpg->se_tpg_tfo->tpg_get_tag(tpg),
229 acl->dynamic_node_acl ? "DYNAMIC" : "",
230 acl->queue_depth,
231 tpg->se_tpg_tfo->get_fabric_name(),
232 acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800233}
234
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800235struct se_node_acl *core_tpg_check_initiator_node_acl(
236 struct se_portal_group *tpg,
237 unsigned char *initiatorname)
238{
239 struct se_node_acl *acl;
240
241 acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
242 if (acl)
243 return acl;
244
245 if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
246 return NULL;
247
Christoph Hellwige413f472015-04-13 19:51:15 +0200248 acl = target_alloc_node_acl(tpg, initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249 if (!acl)
250 return NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800251 acl->dynamic_node_acl = 1;
252
Nicholas Bellinger052605c2011-07-26 17:48:43 -0700253 /*
254 * Here we only create demo-mode MappedLUNs from the active
Masanari Iida35d1efe2012-08-16 22:43:13 +0900255 * TPG LUNs if the fabric is not explicitly asking for
Nicholas Bellinger052605c2011-07-26 17:48:43 -0700256 * tpg_check_demo_mode_login_only() == 1.
257 */
Andy Grovercdf88a22012-07-12 17:34:57 -0700258 if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
259 (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
Nicholas Bellingerdf9766c2015-05-22 02:05:19 +0000260 core_tpg_add_node_to_devs(acl, tpg, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800261
Christoph Hellwige413f472015-04-13 19:51:15 +0200262 target_add_node_acl(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263 return acl;
264}
265EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
266
267void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
268{
269 while (atomic_read(&nacl->acl_pr_ref_count) != 0)
270 cpu_relax();
271}
272
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273struct se_node_acl *core_tpg_add_initiator_node_acl(
274 struct se_portal_group *tpg,
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200275 const char *initiatorname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200277 struct se_node_acl *acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000279 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700281 if (acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800282 if (acl->dynamic_node_acl) {
283 acl->dynamic_node_acl = 0;
Andy Grover6708bb22011-06-08 10:36:43 -0700284 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
Andy Grovere3d6f902011-07-19 08:55:10 +0000285 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
286 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000287 mutex_unlock(&tpg->acl_node_mutex);
Christoph Hellwige413f472015-04-13 19:51:15 +0200288 return acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289 }
290
Andy Grover6708bb22011-06-08 10:36:43 -0700291 pr_err("ACL entry for %s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292 " Node %s already exists for TPG %u, ignoring"
Andy Grovere3d6f902011-07-19 08:55:10 +0000293 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
294 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000295 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800296 return ERR_PTR(-EEXIST);
297 }
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000298 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299
Christoph Hellwige413f472015-04-13 19:51:15 +0200300 acl = target_alloc_node_acl(tpg, initiatorname);
301 if (!acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303
Christoph Hellwige413f472015-04-13 19:51:15 +0200304 target_add_node_acl(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800305 return acl;
306}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200308void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309{
Christoph Hellwigc7d6a802015-04-13 19:51:14 +0200310 struct se_portal_group *tpg = acl->se_tpg;
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800311 LIST_HEAD(sess_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800312 struct se_session *sess, *sess_tmp;
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700313 unsigned long flags;
Jörn Engel28168902012-03-15 15:06:58 -0400314 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000316 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317 if (acl->dynamic_node_acl) {
318 acl->dynamic_node_acl = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800319 }
320 list_del(&acl->acl_list);
321 tpg->num_node_acls--;
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000322 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800324 spin_lock_irqsave(&acl->nacl_sess_lock, flags);
325 acl->acl_stop = 1;
326
327 list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
328 sess_acl_list) {
329 if (sess->sess_tearing_down != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 continue;
331
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800332 target_get_session(sess);
333 list_move(&sess->sess_acl_list, &sess_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334 }
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800335 spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
336
337 list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
338 list_del(&sess->sess_acl_list);
339
340 rc = tpg->se_tpg_tfo->shutdown_session(sess);
341 target_put_session(sess);
342 if (!rc)
343 continue;
344 target_put_session(sess);
345 }
346 target_put_nacl(acl);
347 /*
348 * Wait for last target_put_nacl() to complete in target_complete_nacl()
349 * for active fabric session transport_deregister_session() callbacks.
350 */
351 wait_for_completion(&acl->acl_free_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352
353 core_tpg_wait_for_nacl_pr_ref(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 core_free_device_list_for_node(acl, tpg);
355
Andy Grover6708bb22011-06-08 10:36:43 -0700356 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
Andy Grovere3d6f902011-07-19 08:55:10 +0000357 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
358 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
359 tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360
Christoph Hellwig144bc4c2015-04-13 19:51:16 +0200361 kfree(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363
364/* core_tpg_set_initiator_node_queue_depth():
365 *
366 *
367 */
368int core_tpg_set_initiator_node_queue_depth(
369 struct se_portal_group *tpg,
370 unsigned char *initiatorname,
371 u32 queue_depth,
372 int force)
373{
374 struct se_session *sess, *init_sess = NULL;
375 struct se_node_acl *acl;
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700376 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 int dynamic_acl = 0;
378
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000379 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700381 if (!acl) {
382 pr_err("Access Control List entry for %s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383 " Node %s does not exists for TPG %hu, ignoring"
Andy Grovere3d6f902011-07-19 08:55:10 +0000384 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
385 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000386 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 return -ENODEV;
388 }
389 if (acl->dynamic_node_acl) {
390 acl->dynamic_node_acl = 0;
391 dynamic_acl = 1;
392 }
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000393 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700395 spin_lock_irqsave(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396 list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
397 if (sess->se_node_acl != acl)
398 continue;
399
400 if (!force) {
Andy Grover6708bb22011-06-08 10:36:43 -0700401 pr_err("Unable to change queue depth for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 " Initiator Node: %s while session is"
403 " operational. To forcefully change the queue"
404 " depth and force session reinstatement"
405 " use the \"force=1\" parameter.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000406 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700407 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000409 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 if (dynamic_acl)
411 acl->dynamic_node_acl = 1;
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000412 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800413 return -EEXIST;
414 }
415 /*
416 * Determine if the session needs to be closed by our context.
417 */
Andy Grover6708bb22011-06-08 10:36:43 -0700418 if (!tpg->se_tpg_tfo->shutdown_session(sess))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 continue;
420
421 init_sess = sess;
422 break;
423 }
424
425 /*
426 * User has requested to change the queue depth for a Initiator Node.
427 * Change the value in the Node's struct se_node_acl, and call
428 * core_set_queue_depth_for_node() to add the requested queue depth.
429 *
Andy Grovere3d6f902011-07-19 08:55:10 +0000430 * Finally call tpg->se_tpg_tfo->close_session() to force session
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 * reinstatement to occur if there is an active session for the
432 * $FABRIC_MOD Initiator Node in question.
433 */
434 acl->queue_depth = queue_depth;
435
436 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700437 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 /*
439 * Force session reinstatement if
440 * core_set_queue_depth_for_node() failed, because we assume
441 * the $FABRIC_MOD has already the set session reinstatement
Andy Grovere3d6f902011-07-19 08:55:10 +0000442 * bit from tpg->se_tpg_tfo->shutdown_session() called above.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800443 */
444 if (init_sess)
Andy Grovere3d6f902011-07-19 08:55:10 +0000445 tpg->se_tpg_tfo->close_session(init_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000447 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800448 if (dynamic_acl)
449 acl->dynamic_node_acl = 1;
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000450 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 return -EINVAL;
452 }
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700453 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 /*
455 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
456 * forcefully shutdown the $FABRIC_MOD session/nexus.
457 */
458 if (init_sess)
Andy Grovere3d6f902011-07-19 08:55:10 +0000459 tpg->se_tpg_tfo->close_session(init_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460
Joe Perchesbfb90352011-08-17 06:58:04 -0700461 pr_debug("Successfully changed queue depth to: %d for Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800462 " Node: %s on %s Target Portal Group: %u\n", queue_depth,
Andy Grovere3d6f902011-07-19 08:55:10 +0000463 initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
464 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000466 mutex_lock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 if (dynamic_acl)
468 acl->dynamic_node_acl = 1;
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000469 mutex_unlock(&tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470
471 return 0;
472}
473EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
474
Andy Grover79e62fc2012-12-11 16:30:53 -0800475/* core_tpg_set_initiator_node_tag():
476 *
477 * Initiator nodeacl tags are not used internally, but may be used by
478 * userspace to emulate aliases or groups.
479 * Returns length of newly-set tag or -EINVAL.
480 */
481int core_tpg_set_initiator_node_tag(
482 struct se_portal_group *tpg,
483 struct se_node_acl *acl,
484 const char *new_tag)
485{
486 if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
487 return -EINVAL;
488
489 if (!strncmp("NULL", new_tag, 4)) {
490 acl->acl_tag[0] = '\0';
491 return 0;
492 }
493
494 return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
495}
496EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
497
Nicholas Bellinger52777972013-11-06 21:03:43 -0800498static void core_tpg_lun_ref_release(struct percpu_ref *ref)
499{
500 struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
501
502 complete(&lun->lun_ref_comp);
503}
504
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505int core_tpg_register(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 struct se_wwn *se_wwn,
507 struct se_portal_group *se_tpg,
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200508 int proto_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509{
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700510 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -0700512 if (!se_tpg)
513 return -EINVAL;
514 /*
515 * For the typical case where core_tpg_register() is called by a
516 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
517 * configfs context, use the original tf_ops pointer already saved
518 * by target-core in target_fabric_make_wwn().
519 *
520 * Otherwise, for special cases like iscsi-target discovery TPGs
521 * the caller is responsible for setting ->se_tpg_tfo ahead of
522 * calling core_tpg_register().
523 */
524 if (se_wwn)
525 se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
526
527 if (!se_tpg->se_tpg_tfo) {
528 pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
529 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530 }
531
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700532 INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200533 se_tpg->proto_id = proto_id;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 se_tpg->se_tpg_wwn = se_wwn;
535 atomic_set(&se_tpg->tpg_pr_ref_count, 0);
536 INIT_LIST_HEAD(&se_tpg->acl_node_list);
Andy Grovere3d6f902011-07-19 08:55:10 +0000537 INIT_LIST_HEAD(&se_tpg->se_tpg_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 spin_lock_init(&se_tpg->session_lock);
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700540 mutex_init(&se_tpg->tpg_lun_mutex);
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000541 mutex_init(&se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200543 if (se_tpg->proto_id >= 0) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700544 se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
545 if (IS_ERR(se_tpg->tpg_virt_lun0))
546 return PTR_ERR(se_tpg->tpg_virt_lun0);
547
548 ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
549 TRANSPORT_LUNFLAGS_READ_ONLY, g_lun0_dev);
550 if (ret < 0) {
551 kfree(se_tpg->tpg_virt_lun0);
552 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 }
554 }
555
Andy Grovere3d6f902011-07-19 08:55:10 +0000556 spin_lock_bh(&tpg_lock);
557 list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
558 spin_unlock_bh(&tpg_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200560 pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -0700561 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->get_fabric_name(),
562 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
563 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
564 se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800565
566 return 0;
567}
568EXPORT_SYMBOL(core_tpg_register);
569
570int core_tpg_deregister(struct se_portal_group *se_tpg)
571{
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200572 const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800573 struct se_node_acl *nacl, *nacl_tmp;
Nicholas Bellinger22793de2015-03-06 10:23:25 +0000574 LIST_HEAD(node_list);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800575
Christoph Hellwige4aae5a2015-05-01 17:47:56 +0200576 pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
577 "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(),
578 tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
579 se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580
Andy Grovere3d6f902011-07-19 08:55:10 +0000581 spin_lock_bh(&tpg_lock);
582 list_del(&se_tpg->se_tpg_node);
583 spin_unlock_bh(&tpg_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584
585 while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
586 cpu_relax();
Nicholas Bellinger403edd72015-03-08 22:33:47 +0000587
Nicholas Bellinger22793de2015-03-06 10:23:25 +0000588 mutex_lock(&se_tpg->acl_node_mutex);
589 list_splice_init(&se_tpg->acl_node_list, &node_list);
590 mutex_unlock(&se_tpg->acl_node_mutex);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800591 /*
592 * Release any remaining demo-mode generated se_node_acl that have
593 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
594 * in transport_deregister_session().
595 */
Nicholas Bellinger22793de2015-03-06 10:23:25 +0000596 list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800597 list_del(&nacl->acl_list);
598 se_tpg->num_node_acls--;
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800599
600 core_tpg_wait_for_nacl_pr_ref(nacl);
601 core_free_device_list_for_node(nacl, se_tpg);
Christoph Hellwig144bc4c2015-04-13 19:51:16 +0200602 kfree(nacl);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800603 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700605 if (se_tpg->proto_id >= 0) {
606 core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
607 kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
608 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 return 0;
611}
612EXPORT_SYMBOL(core_tpg_deregister);
613
Andy Groverd344f8a2013-11-26 12:07:54 -0800614struct se_lun *core_tpg_alloc_lun(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615 struct se_portal_group *tpg,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200616 u64 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617{
618 struct se_lun *lun;
619
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700620 lun = kzalloc(sizeof(*lun), GFP_KERNEL);
621 if (!lun) {
622 pr_err("Unable to allocate se_lun memory\n");
623 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800624 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700625 lun->unpacked_lun = unpacked_lun;
626 lun->lun_link_magic = SE_LUN_LINK_MAGIC;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700627 atomic_set(&lun->lun_acl_count, 0);
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700628 init_completion(&lun->lun_ref_comp);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700629 INIT_LIST_HEAD(&lun->lun_deve_list);
630 INIT_LIST_HEAD(&lun->lun_dev_link);
631 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
632 spin_lock_init(&lun->lun_deve_lock);
633 mutex_init(&lun->lun_tg_pt_md_mutex);
634 INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
635 spin_lock_init(&lun->lun_tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700636 lun->lun_tpg = tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800637
638 return lun;
639}
640
Andy Groverd344f8a2013-11-26 12:07:54 -0800641int core_tpg_add_lun(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642 struct se_portal_group *tpg,
643 struct se_lun *lun,
644 u32 lun_access,
Andy Grover340dbf72013-12-12 16:12:33 -0800645 struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646{
Andy Grovere3d6f902011-07-19 08:55:10 +0000647 int ret;
648
Tejun Heo2aad2a82014-09-24 13:31:50 -0400649 ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
Tejun Heoa34375e2014-09-08 09:51:30 +0900650 GFP_KERNEL);
Andy Grovere3d6f902011-07-19 08:55:10 +0000651 if (ret < 0)
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700652 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700654 ret = core_alloc_rtpi(lun, dev);
655 if (ret)
656 goto out_kill_ref;
Nicholas Bellinger52777972013-11-06 21:03:43 -0800657
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700658 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
659 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
660 target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800661
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700662 mutex_lock(&tpg->tpg_lun_mutex);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700663
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700664 spin_lock(&dev->se_port_lock);
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700665 lun->lun_index = dev->dev_index;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700666 rcu_assign_pointer(lun->lun_se_dev, dev);
667 dev->export_count++;
668 list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
669 spin_unlock(&dev->se_port_lock);
670
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 lun->lun_access = lun_access;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700672 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
673 hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
674 mutex_unlock(&tpg->tpg_lun_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800675
676 return 0;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700677
678out_kill_ref:
679 percpu_ref_exit(&lun->lun_ref);
680out:
681 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682}
683
Andy Grovercd9d7cb2014-06-30 16:39:44 -0700684void core_tpg_remove_lun(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800685 struct se_portal_group *tpg,
686 struct se_lun *lun)
687{
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700688 /*
689 * rcu_dereference_raw protected by se_lun->lun_group symlink
690 * reference to se_device->dev_group.
691 */
692 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700693
Nicholas Bellinger4a9a6c82013-11-06 21:05:19 -0800694 core_clear_lun_from_tpg(lun, tpg);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700695 /*
696 * Wait for any active I/O references to percpu se_lun->lun_ref to
697 * be released. Also, se_lun->lun_ref is now used by PR and ALUA
698 * logic when referencing a remote target port during ALL_TGT_PT=1
699 * and generating UNIT_ATTENTIONs for ALUA access state transition.
700 */
Nicholas Bellinger4a9a6c82013-11-06 21:05:19 -0800701 transport_clear_lun_ref(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700703 mutex_lock(&tpg->tpg_lun_mutex);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700704 if (lun->lun_se_dev) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700705 target_detach_tg_pt_gp(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800706
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700707 spin_lock(&dev->se_port_lock);
708 list_del(&lun->lun_dev_link);
709 dev->export_count--;
710 rcu_assign_pointer(lun->lun_se_dev, NULL);
711 spin_unlock(&dev->se_port_lock);
712 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700713 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
714 hlist_del_rcu(&lun->link);
715 mutex_unlock(&tpg->tpg_lun_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716
Tejun Heo9a1049d2014-06-28 08:10:14 -0400717 percpu_ref_exit(&lun->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718}