11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_tpg.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains generic Target Portal Group related functions.
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2002-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  ******************************************************************************/
12c66ac9dbSNicholas Bellinger 
13c66ac9dbSNicholas Bellinger #include <linux/net.h>
14c66ac9dbSNicholas Bellinger #include <linux/string.h>
15c66ac9dbSNicholas Bellinger #include <linux/timer.h>
16c66ac9dbSNicholas Bellinger #include <linux/slab.h>
17c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
18c66ac9dbSNicholas Bellinger #include <linux/in.h>
19c53181afSPaul Gortmaker #include <linux/export.h>
20c66ac9dbSNicholas Bellinger #include <net/sock.h>
21c66ac9dbSNicholas Bellinger #include <net/tcp.h>
22ba929992SBart Van Assche #include <scsi/scsi_proto.h>
23c66ac9dbSNicholas Bellinger 
24c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
26c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
27c66ac9dbSNicholas Bellinger 
28e26d99aeSChristoph Hellwig #include "target_core_internal.h"
29adf653f9SChristoph Hellwig #include "target_core_alua.h"
30e2480563SNicholas Bellinger #include "target_core_pr.h"
31e986a35aSHannes Reinecke #include "target_core_ua.h"
32e3d6f909SAndy Grover 
33e3d6f909SAndy Grover extern struct se_device *g_lun0_dev;
34e3d6f909SAndy Grover static DEFINE_XARRAY_ALLOC(tpg_xa);
35c66ac9dbSNicholas Bellinger 
36c66ac9dbSNicholas Bellinger /*	__core_tpg_get_initiator_node_acl():
37403edd78SNicholas Bellinger  *
38c66ac9dbSNicholas Bellinger  *	mutex_lock(&tpg->acl_node_mutex); must be held when calling
39c66ac9dbSNicholas Bellinger  */
__core_tpg_get_initiator_node_acl(struct se_portal_group * tpg,const char * initiatorname)40c66ac9dbSNicholas Bellinger struct se_node_acl *__core_tpg_get_initiator_node_acl(
41c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
42c66ac9dbSNicholas Bellinger 	const char *initiatorname)
43c66ac9dbSNicholas Bellinger {
44c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
45c66ac9dbSNicholas Bellinger 
466708bb27SAndy Grover 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
47c66ac9dbSNicholas Bellinger 		if (!strcmp(acl->initiatorname, initiatorname))
48c66ac9dbSNicholas Bellinger 			return acl;
49c66ac9dbSNicholas Bellinger 	}
50c66ac9dbSNicholas Bellinger 
51c66ac9dbSNicholas Bellinger 	return NULL;
52c66ac9dbSNicholas Bellinger }
53c66ac9dbSNicholas Bellinger 
54c66ac9dbSNicholas Bellinger /*	core_tpg_get_initiator_node_acl():
55c66ac9dbSNicholas Bellinger  *
56c66ac9dbSNicholas Bellinger  *
57c66ac9dbSNicholas Bellinger  */
core_tpg_get_initiator_node_acl(struct se_portal_group * tpg,unsigned char * initiatorname)58c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_get_initiator_node_acl(
59c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
60c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
61c66ac9dbSNicholas Bellinger {
6221aaa23bSNicholas Bellinger 	struct se_node_acl *acl;
6321aaa23bSNicholas Bellinger 	/*
6421aaa23bSNicholas Bellinger 	 * Obtain se_node_acl->acl_kref using fabric driver provided
6521aaa23bSNicholas Bellinger 	 * initiatorname[] during node acl endpoint lookup driven by
6621aaa23bSNicholas Bellinger 	 * new se_session login.
6721aaa23bSNicholas Bellinger 	 *
6821aaa23bSNicholas Bellinger 	 * The reference is held until se_session shutdown -> release
6921aaa23bSNicholas Bellinger 	 * occurs via fabric driver invoked transport_deregister_session()
7021aaa23bSNicholas Bellinger 	 * or transport_free_session() code.
71403edd78SNicholas Bellinger 	 */
72fcf29481SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
7321aaa23bSNicholas Bellinger 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
7421aaa23bSNicholas Bellinger 	if (acl) {
7521aaa23bSNicholas Bellinger 		if (!kref_get_unless_zero(&acl->acl_kref))
7621aaa23bSNicholas Bellinger 			acl = NULL;
77403edd78SNicholas Bellinger 	}
78c66ac9dbSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
79fcf29481SNicholas Bellinger 
80c66ac9dbSNicholas Bellinger 	return acl;
81b3fde035SThomas Glanzmann }
82c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
83e986a35aSHannes Reinecke 
core_allocate_nexus_loss_ua(struct se_node_acl * nacl)84e986a35aSHannes Reinecke void core_allocate_nexus_loss_ua(
85e986a35aSHannes Reinecke 	struct se_node_acl *nacl)
86e986a35aSHannes Reinecke {
87e986a35aSHannes Reinecke 	struct se_dev_entry *deve;
88e986a35aSHannes Reinecke 
89e986a35aSHannes Reinecke 	if (!nacl)
90e986a35aSHannes Reinecke 		return;
91e986a35aSHannes Reinecke 
92e986a35aSHannes Reinecke 	rcu_read_lock();
93e986a35aSHannes Reinecke 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
94e986a35aSHannes Reinecke 		core_scsi3_ua_allocate(deve, 0x29,
95e986a35aSHannes Reinecke 			ASCQ_29H_NEXUS_LOSS_OCCURRED);
96e986a35aSHannes Reinecke 	rcu_read_unlock();
97e986a35aSHannes Reinecke }
98e986a35aSHannes Reinecke EXPORT_SYMBOL(core_allocate_nexus_loss_ua);
99c66ac9dbSNicholas Bellinger 
100c66ac9dbSNicholas Bellinger /*	core_tpg_add_node_to_devs():
101c66ac9dbSNicholas Bellinger  *
102c66ac9dbSNicholas Bellinger  *
103c66ac9dbSNicholas Bellinger  */
core_tpg_add_node_to_devs(struct se_node_acl * acl,struct se_portal_group * tpg,struct se_lun * lun_orig)104c66ac9dbSNicholas Bellinger void core_tpg_add_node_to_devs(
105df9766caSNicholas Bellinger 	struct se_node_acl *acl,
106df9766caSNicholas Bellinger 	struct se_portal_group *tpg,
107c66ac9dbSNicholas Bellinger 	struct se_lun *lun_orig)
10803a68b44SAndy Grover {
109c66ac9dbSNicholas Bellinger 	bool lun_access_ro = true;
110c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
111c66ac9dbSNicholas Bellinger 	struct se_device *dev;
1126bb82612SNicholas Bellinger 
1136bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
114df9766caSNicholas Bellinger 	hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
115c66ac9dbSNicholas Bellinger 		if (lun_orig && lun != lun_orig)
116c66ac9dbSNicholas Bellinger 			continue;
1174cc987eaSNicholas Bellinger 
1184cc987eaSNicholas Bellinger 		dev = rcu_dereference_check(lun->lun_se_dev,
119c66ac9dbSNicholas Bellinger 					    lockdep_is_held(&tpg->tpg_lun_mutex));
120c66ac9dbSNicholas Bellinger 		/*
121c66ac9dbSNicholas Bellinger 		 * By default in LIO-Target $FABRIC_MOD,
122c66ac9dbSNicholas Bellinger 		 * demo_mode_write_protect is ON, or READ_ONLY;
1236708bb27SAndy Grover 		 */
12403a68b44SAndy Grover 		if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
125c66ac9dbSNicholas Bellinger 			lun_access_ro = false;
126c66ac9dbSNicholas Bellinger 		} else {
127c66ac9dbSNicholas Bellinger 			/*
128c66ac9dbSNicholas Bellinger 			 * Allow only optical drives to issue R/W in default RO
129c66ac9dbSNicholas Bellinger 			 * demo mode.
130e3d6f909SAndy Grover 			 */
13103a68b44SAndy Grover 			if (dev->transport->get_device_type(dev) == TYPE_DISK)
132c66ac9dbSNicholas Bellinger 				lun_access_ro = true;
13303a68b44SAndy Grover 			else
134c66ac9dbSNicholas Bellinger 				lun_access_ro = false;
135c66ac9dbSNicholas Bellinger 		}
136f2d30680SHannes Reinecke 
137c66ac9dbSNicholas Bellinger 		pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s"
13830c7ca93SDavid Disseldorp 			" access for LUN in Demo Mode\n",
139e3d6f909SAndy Grover 			tpg->se_tpg_tfo->fabric_name,
14003a68b44SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
141c66ac9dbSNicholas Bellinger 			lun_access_ro ? "READ-ONLY" : "READ-WRITE");
142e80ac6c4SAndy Grover 
14303a68b44SAndy Grover 		core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
144e2480563SNicholas Bellinger 						 lun_access_ro, acl, tpg);
145e2480563SNicholas Bellinger 		/*
146e2480563SNicholas Bellinger 		 * Check to see if there are any existing persistent reservation
147e2480563SNicholas Bellinger 		 * APTPL pre-registrations that need to be enabled for this dynamic
148e2480563SNicholas Bellinger 		 * LUN ACL now..
149e2480563SNicholas Bellinger 		 */
150e2480563SNicholas Bellinger 		core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
151c66ac9dbSNicholas Bellinger 						    lun->unpacked_lun);
1526bb82612SNicholas Bellinger 	}
153c66ac9dbSNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
154c66ac9dbSNicholas Bellinger }
155d36ad77fSNicholas Bellinger 
156d36ad77fSNicholas Bellinger static void
target_set_nacl_queue_depth(struct se_portal_group * tpg,struct se_node_acl * acl,u32 queue_depth)157d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(struct se_portal_group *tpg,
158c66ac9dbSNicholas Bellinger 			    struct se_node_acl *acl, u32 queue_depth)
159d36ad77fSNicholas Bellinger {
160d36ad77fSNicholas Bellinger 	acl->queue_depth = queue_depth;
161c66ac9dbSNicholas Bellinger 
162d36ad77fSNicholas Bellinger 	if (!acl->queue_depth) {
16330c7ca93SDavid Disseldorp 		pr_warn("Queue depth for %s Initiator Node: %s is 0,"
164c66ac9dbSNicholas Bellinger 			"defaulting to 1.\n", tpg->se_tpg_tfo->fabric_name,
165c66ac9dbSNicholas Bellinger 			acl->initiatorname);
166c66ac9dbSNicholas Bellinger 		acl->queue_depth = 1;
167c66ac9dbSNicholas Bellinger 	}
168c66ac9dbSNicholas Bellinger }
169e413f472SChristoph Hellwig 
target_alloc_node_acl(struct se_portal_group * tpg,const unsigned char * initiatorname)170e413f472SChristoph Hellwig static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg,
1714a5a75f3SJörn Engel 		const unsigned char *initiatorname)
172c66ac9dbSNicholas Bellinger {
173d36ad77fSNicholas Bellinger 	struct se_node_acl *acl;
174c66ac9dbSNicholas Bellinger 	u32 queue_depth;
175144bc4c2SChristoph Hellwig 
176144bc4c2SChristoph Hellwig 	acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size),
1776708bb27SAndy Grover 			GFP_KERNEL);
178c66ac9dbSNicholas Bellinger 	if (!acl)
179c66ac9dbSNicholas Bellinger 		return NULL;
180c66ac9dbSNicholas Bellinger 
181c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_list);
18229a05deeSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_sess_list);
183afb999ffSNicholas Bellinger 	INIT_HLIST_HEAD(&acl->lun_entry_hlist);
18401468346SNicholas Bellinger 	kref_init(&acl->acl_kref);
185c66ac9dbSNicholas Bellinger 	init_completion(&acl->acl_free_comp);
18629a05deeSNicholas Bellinger 	spin_lock_init(&acl->nacl_sess_lock);
187c66ac9dbSNicholas Bellinger 	mutex_init(&acl->lun_entry_mutex);
188d36ad77fSNicholas Bellinger 	atomic_set(&acl->acl_pr_ref_count, 0);
189e1750d20SChristoph Hellwig 
190d36ad77fSNicholas Bellinger 	if (tpg->se_tpg_tfo->tpg_get_default_depth)
191e1750d20SChristoph Hellwig 		queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
192d36ad77fSNicholas Bellinger 	else
193d36ad77fSNicholas Bellinger 		queue_depth = 1;
194d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
195c66ac9dbSNicholas Bellinger 
196c66ac9dbSNicholas Bellinger 	snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
197c66ac9dbSNicholas Bellinger 	acl->se_tpg = tpg;
198c66ac9dbSNicholas Bellinger 	acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
199e3d6f909SAndy Grover 
200c66ac9dbSNicholas Bellinger 	tpg->se_tpg_tfo->set_default_node_attributes(acl);
201e413f472SChristoph Hellwig 
202c66ac9dbSNicholas Bellinger 	return acl;
203c66ac9dbSNicholas Bellinger }
204e413f472SChristoph Hellwig 
target_add_node_acl(struct se_node_acl * acl)205c66ac9dbSNicholas Bellinger static void target_add_node_acl(struct se_node_acl *acl)
206e413f472SChristoph Hellwig {
207c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
208403edd78SNicholas Bellinger 
209e413f472SChristoph Hellwig 	mutex_lock(&tpg->acl_node_mutex);
210403edd78SNicholas Bellinger 	list_add_tail(&acl->acl_list, &tpg->acl_node_list);
211c66ac9dbSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
212e413f472SChristoph Hellwig 
213e413f472SChristoph Hellwig 	pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
21430c7ca93SDavid Disseldorp 		" Initiator Node: %s\n",
215e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->fabric_name,
216e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
217e413f472SChristoph Hellwig 		acl->dynamic_node_acl ? "DYNAMIC" : "",
21830c7ca93SDavid Disseldorp 		acl->queue_depth,
219e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->fabric_name,
220c66ac9dbSNicholas Bellinger 		acl->initiatorname);
221c66ac9dbSNicholas Bellinger }
22221aaa23bSNicholas Bellinger 
target_tpg_has_node_acl(struct se_portal_group * tpg,const char * initiatorname)22321aaa23bSNicholas Bellinger bool target_tpg_has_node_acl(struct se_portal_group *tpg,
22421aaa23bSNicholas Bellinger 			     const char *initiatorname)
22521aaa23bSNicholas Bellinger {
22621aaa23bSNicholas Bellinger 	struct se_node_acl *acl;
22721aaa23bSNicholas Bellinger 	bool found = false;
22821aaa23bSNicholas Bellinger 
22921aaa23bSNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
23021aaa23bSNicholas Bellinger 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
23121aaa23bSNicholas Bellinger 		if (!strcmp(acl->initiatorname, initiatorname)) {
23221aaa23bSNicholas Bellinger 			found = true;
23321aaa23bSNicholas Bellinger 			break;
23421aaa23bSNicholas Bellinger 		}
23521aaa23bSNicholas Bellinger 	}
23621aaa23bSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
23721aaa23bSNicholas Bellinger 
23821aaa23bSNicholas Bellinger 	return found;
23921aaa23bSNicholas Bellinger }
24021aaa23bSNicholas Bellinger EXPORT_SYMBOL(target_tpg_has_node_acl);
241c66ac9dbSNicholas Bellinger 
core_tpg_check_initiator_node_acl(struct se_portal_group * tpg,unsigned char * initiatorname)242c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_check_initiator_node_acl(
243c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
244c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
245c66ac9dbSNicholas Bellinger {
246c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
247c66ac9dbSNicholas Bellinger 
248c66ac9dbSNicholas Bellinger 	acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
249c66ac9dbSNicholas Bellinger 	if (acl)
250c66ac9dbSNicholas Bellinger 		return acl;
251c66ac9dbSNicholas Bellinger 
252c66ac9dbSNicholas Bellinger 	if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
253c66ac9dbSNicholas Bellinger 		return NULL;
254e413f472SChristoph Hellwig 
255c66ac9dbSNicholas Bellinger 	acl = target_alloc_node_acl(tpg, initiatorname);
256c66ac9dbSNicholas Bellinger 	if (!acl)
25721aaa23bSNicholas Bellinger 		return NULL;
25821aaa23bSNicholas Bellinger 	/*
25921aaa23bSNicholas Bellinger 	 * When allocating a dynamically generated node_acl, go ahead
26021aaa23bSNicholas Bellinger 	 * and take the extra kref now before returning to the fabric
26121aaa23bSNicholas Bellinger 	 * driver caller.
26221aaa23bSNicholas Bellinger 	 *
26321aaa23bSNicholas Bellinger 	 * Note this reference will be released at session shutdown
26421aaa23bSNicholas Bellinger 	 * time within transport_free_session() code.
26521aaa23bSNicholas Bellinger 	 */
266c66ac9dbSNicholas Bellinger 	kref_get(&acl->acl_kref);
267c66ac9dbSNicholas Bellinger 	acl->dynamic_node_acl = 1;
268052605c6SNicholas Bellinger 
269052605c6SNicholas Bellinger 	/*
27035d1efe8SMasanari Iida 	 * Here we only create demo-mode MappedLUNs from the active
271052605c6SNicholas Bellinger 	 * TPG LUNs if the fabric is not explicitly asking for
272052605c6SNicholas Bellinger 	 * tpg_check_demo_mode_login_only() == 1.
273cdf88a2fSAndy Grover 	 */
274cdf88a2fSAndy Grover 	if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
275df9766caSNicholas Bellinger 	    (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
276c66ac9dbSNicholas Bellinger 		core_tpg_add_node_to_devs(acl, tpg, NULL);
277e413f472SChristoph Hellwig 
278c66ac9dbSNicholas Bellinger 	target_add_node_acl(acl);
279c66ac9dbSNicholas Bellinger 	return acl;
280c66ac9dbSNicholas Bellinger }
281c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
282c66ac9dbSNicholas Bellinger 
core_tpg_wait_for_nacl_pr_ref(struct se_node_acl * nacl)283c66ac9dbSNicholas Bellinger void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
284c66ac9dbSNicholas Bellinger {
285c66ac9dbSNicholas Bellinger 	while (atomic_read(&nacl->acl_pr_ref_count) != 0)
286c66ac9dbSNicholas Bellinger 		cpu_relax();
287c66ac9dbSNicholas Bellinger }
288c66ac9dbSNicholas Bellinger 
core_tpg_add_initiator_node_acl(struct se_portal_group * tpg,const char * initiatorname)289c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_add_initiator_node_acl(
290c7d6a803SChristoph Hellwig 	struct se_portal_group *tpg,
291c66ac9dbSNicholas Bellinger 	const char *initiatorname)
292c7d6a803SChristoph Hellwig {
293c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
294403edd78SNicholas Bellinger 
295c66ac9dbSNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
2966708bb27SAndy Grover 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
297c66ac9dbSNicholas Bellinger 	if (acl) {
298c66ac9dbSNicholas Bellinger 		if (acl->dynamic_node_acl) {
2996708bb27SAndy Grover 			acl->dynamic_node_acl = 0;
30030c7ca93SDavid Disseldorp 			pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
301e3d6f909SAndy Grover 				" for %s\n", tpg->se_tpg_tfo->fabric_name,
302403edd78SNicholas Bellinger 				tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
303e413f472SChristoph Hellwig 			mutex_unlock(&tpg->acl_node_mutex);
304c66ac9dbSNicholas Bellinger 			return acl;
305c66ac9dbSNicholas Bellinger 		}
3066708bb27SAndy Grover 
307c66ac9dbSNicholas Bellinger 		pr_err("ACL entry for %s Initiator"
30830c7ca93SDavid Disseldorp 			" Node %s already exists for TPG %u, ignoring"
309e3d6f909SAndy Grover 			" request.\n",  tpg->se_tpg_tfo->fabric_name,
310403edd78SNicholas Bellinger 			initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
311c66ac9dbSNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
312c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EEXIST);
313403edd78SNicholas Bellinger 	}
314c66ac9dbSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
315e413f472SChristoph Hellwig 
316e413f472SChristoph Hellwig 	acl = target_alloc_node_acl(tpg, initiatorname);
317c66ac9dbSNicholas Bellinger 	if (!acl)
318c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
319e413f472SChristoph Hellwig 
320c66ac9dbSNicholas Bellinger 	target_add_node_acl(acl);
321c66ac9dbSNicholas Bellinger 	return acl;
322c66ac9dbSNicholas Bellinger }
323fba81f88SChristoph Hellwig 
target_shutdown_sessions(struct se_node_acl * acl)324bc6e6bb4SChristoph Hellwig static void target_shutdown_sessions(struct se_node_acl *acl)
325bc6e6bb4SChristoph Hellwig {
326bc6e6bb4SChristoph Hellwig 	struct se_session *sess;
327bc6e6bb4SChristoph Hellwig 	unsigned long flags;
328bc6e6bb4SChristoph Hellwig 
329fba81f88SChristoph Hellwig restart:
330bc6e6bb4SChristoph Hellwig 	spin_lock_irqsave(&acl->nacl_sess_lock, flags);
331*becd9be6SMike Christie 	list_for_each_entry(sess, &acl->acl_sess_list, sess_acl_list) {
332bc6e6bb4SChristoph Hellwig 		if (sess->cmd_cnt && atomic_read(&sess->cmd_cnt->stopped))
333bc6e6bb4SChristoph Hellwig 			continue;
334fba81f88SChristoph Hellwig 
335bc6e6bb4SChristoph Hellwig 		list_del_init(&sess->sess_acl_list);
336d94331faSChristoph Hellwig 		spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
337d94331faSChristoph Hellwig 
338d94331faSChristoph Hellwig 		if (acl->se_tpg->se_tpg_tfo->close_session)
339bc6e6bb4SChristoph Hellwig 			acl->se_tpg->se_tpg_tfo->close_session(sess);
340bc6e6bb4SChristoph Hellwig 		goto restart;
341bc6e6bb4SChristoph Hellwig 	}
342bc6e6bb4SChristoph Hellwig 	spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
343bc6e6bb4SChristoph Hellwig }
344c7d6a803SChristoph Hellwig 
core_tpg_del_initiator_node_acl(struct se_node_acl * acl)345c66ac9dbSNicholas Bellinger void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
346c7d6a803SChristoph Hellwig {
347c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
348403edd78SNicholas Bellinger 
349bc6e6bb4SChristoph Hellwig 	mutex_lock(&tpg->acl_node_mutex);
350c66ac9dbSNicholas Bellinger 	if (acl->dynamic_node_acl)
3516f48655fSNicholas Bellinger 		acl->dynamic_node_acl = 0;
352403edd78SNicholas Bellinger 	list_del_init(&acl->acl_list);
353c66ac9dbSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
354fba81f88SChristoph Hellwig 
355337c0607SNicholas Bellinger 	target_shutdown_sessions(acl);
356337c0607SNicholas Bellinger 
357337c0607SNicholas Bellinger 	target_put_nacl(acl);
358337c0607SNicholas Bellinger 	/*
359337c0607SNicholas Bellinger 	 * Wait for last target_put_nacl() to complete in target_complete_nacl()
360337c0607SNicholas Bellinger 	 * for active fabric session transport_deregister_session() callbacks.
361337c0607SNicholas Bellinger 	 */
362c66ac9dbSNicholas Bellinger 	wait_for_completion(&acl->acl_free_comp);
363c66ac9dbSNicholas Bellinger 
364c66ac9dbSNicholas Bellinger 	core_tpg_wait_for_nacl_pr_ref(acl);
365c66ac9dbSNicholas Bellinger 	core_free_device_list_for_node(acl, tpg);
3666708bb27SAndy Grover 
36730c7ca93SDavid Disseldorp 	pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
368e3d6f909SAndy Grover 		" Initiator Node: %s\n", tpg->se_tpg_tfo->fabric_name,
36930c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
370c66ac9dbSNicholas Bellinger 		tpg->se_tpg_tfo->fabric_name, acl->initiatorname);
371144bc4c2SChristoph Hellwig 
372c66ac9dbSNicholas Bellinger 	kfree(acl);
373c66ac9dbSNicholas Bellinger }
374c66ac9dbSNicholas Bellinger 
375c66ac9dbSNicholas Bellinger /*	core_tpg_set_initiator_node_queue_depth():
376c66ac9dbSNicholas Bellinger  *
377c66ac9dbSNicholas Bellinger  *
378c66ac9dbSNicholas Bellinger  */
core_tpg_set_initiator_node_queue_depth(struct se_node_acl * acl,u32 queue_depth)379d36ad77fSNicholas Bellinger int core_tpg_set_initiator_node_queue_depth(
380d36ad77fSNicholas Bellinger 	struct se_node_acl *acl,
381c66ac9dbSNicholas Bellinger 	u32 queue_depth)
382d36ad77fSNicholas Bellinger {
383c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
384c66ac9dbSNicholas Bellinger 
38546861cddSNicholas Bellinger 	/*
38646861cddSNicholas Bellinger 	 * Allow the setting of se_node_acl queue_depth to be idempotent,
38746861cddSNicholas Bellinger 	 * and not force a session shutdown event if the value is not
38846861cddSNicholas Bellinger 	 * changing.
38946861cddSNicholas Bellinger 	 */
39046861cddSNicholas Bellinger 	if (acl->queue_depth == queue_depth)
39146861cddSNicholas Bellinger 		return 0;
392c66ac9dbSNicholas Bellinger 	/*
393c66ac9dbSNicholas Bellinger 	 * User has requested to change the queue depth for a Initiator Node.
394d36ad77fSNicholas Bellinger 	 * Change the value in the Node's struct se_node_acl, and call
395d36ad77fSNicholas Bellinger 	 * target_set_nacl_queue_depth() to set the new queue depth.
396d36ad77fSNicholas Bellinger 	 */
397d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
398d36ad77fSNicholas Bellinger 
399bc6e6bb4SChristoph Hellwig 	/*
400c66ac9dbSNicholas Bellinger 	 * Shutdown all pending sessions to force session reinstatement.
401fba81f88SChristoph Hellwig 	 */
402c66ac9dbSNicholas Bellinger 	target_shutdown_sessions(acl);
403bfb9035cSJoe Perches 
404d36ad77fSNicholas Bellinger 	pr_debug("Successfully changed queue depth to: %d for Initiator"
40530c7ca93SDavid Disseldorp 		" Node: %s on %s Target Portal Group: %u\n", acl->queue_depth,
406e3d6f909SAndy Grover 		acl->initiatorname, tpg->se_tpg_tfo->fabric_name,
407c66ac9dbSNicholas Bellinger 		tpg->se_tpg_tfo->tpg_get_tag(tpg));
408c66ac9dbSNicholas Bellinger 
409c66ac9dbSNicholas Bellinger 	return 0;
410c66ac9dbSNicholas Bellinger }
411c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
41279e62fc3SAndy Grover 
41379e62fc3SAndy Grover /*	core_tpg_set_initiator_node_tag():
41479e62fc3SAndy Grover  *
41579e62fc3SAndy Grover  *	Initiator nodeacl tags are not used internally, but may be used by
41679e62fc3SAndy Grover  *	userspace to emulate aliases or groups.
41779e62fc3SAndy Grover  *	Returns length of newly-set tag or -EINVAL.
41879e62fc3SAndy Grover  */
core_tpg_set_initiator_node_tag(struct se_portal_group * tpg,struct se_node_acl * acl,const char * new_tag)41979e62fc3SAndy Grover int core_tpg_set_initiator_node_tag(
42079e62fc3SAndy Grover 	struct se_portal_group *tpg,
42179e62fc3SAndy Grover 	struct se_node_acl *acl,
42279e62fc3SAndy Grover 	const char *new_tag)
42379e62fc3SAndy Grover {
42479e62fc3SAndy Grover 	if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
42579e62fc3SAndy Grover 		return -EINVAL;
42679e62fc3SAndy Grover 
42779e62fc3SAndy Grover 	if (!strncmp("NULL", new_tag, 4)) {
42879e62fc3SAndy Grover 		acl->acl_tag[0] = '\0';
42979e62fc3SAndy Grover 		return 0;
43079e62fc3SAndy Grover 	}
43179e62fc3SAndy Grover 
43279e62fc3SAndy Grover 	return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
43379e62fc3SAndy Grover }
43479e62fc3SAndy Grover EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
4355277797dSNicholas Bellinger 
core_tpg_lun_ref_release(struct percpu_ref * ref)4365277797dSNicholas Bellinger static void core_tpg_lun_ref_release(struct percpu_ref *ref)
4375277797dSNicholas Bellinger {
4385277797dSNicholas Bellinger 	struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
439bd4e2d29SNicholas Bellinger 
4405277797dSNicholas Bellinger 	complete(&lun->lun_shutdown_comp);
4415277797dSNicholas Bellinger }
4422bce1a6dSBart Van Assche 
target_tpg_register_rtpi(struct se_portal_group * se_tpg)443c66ac9dbSNicholas Bellinger static int target_tpg_register_rtpi(struct se_portal_group *se_tpg)
444c66ac9dbSNicholas Bellinger {
445c66ac9dbSNicholas Bellinger 	u32 val;
446e4aae5afSChristoph Hellwig 	int ret;
447c66ac9dbSNicholas Bellinger 
448adf653f9SChristoph Hellwig 	if (se_tpg->rtpi_manual) {
449c66ac9dbSNicholas Bellinger 		ret = xa_insert(&tpg_xa, se_tpg->tpg_rtpi, se_tpg, GFP_KERNEL);
450bc0c94b1SNicholas Bellinger 		if (ret) {
451bc0c94b1SNicholas Bellinger 			pr_info("%s_TPG[%hu] - Can not set RTPI %#x, it is already busy",
452bc0c94b1SNicholas Bellinger 				se_tpg->se_tpg_tfo->fabric_name,
453bc0c94b1SNicholas Bellinger 				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg),
454bc0c94b1SNicholas Bellinger 				se_tpg->tpg_rtpi);
455bc0c94b1SNicholas Bellinger 			return -EINVAL;
456bc0c94b1SNicholas Bellinger 		}
457bc0c94b1SNicholas Bellinger 	} else {
458bc0c94b1SNicholas Bellinger 		ret = xa_alloc(&tpg_xa, &val, se_tpg,
459bc0c94b1SNicholas Bellinger 			       XA_LIMIT(1, USHRT_MAX), GFP_KERNEL);
460bc0c94b1SNicholas Bellinger 		if (!ret)
461bc0c94b1SNicholas Bellinger 			se_tpg->tpg_rtpi = val;
462bc0c94b1SNicholas Bellinger 	}
463bc0c94b1SNicholas Bellinger 
464bc0c94b1SNicholas Bellinger 	return ret;
465bc0c94b1SNicholas Bellinger }
466bc0c94b1SNicholas Bellinger 
target_tpg_deregister_rtpi(struct se_portal_group * se_tpg)467bc0c94b1SNicholas Bellinger static void target_tpg_deregister_rtpi(struct se_portal_group *se_tpg)
468c66ac9dbSNicholas Bellinger {
469c66ac9dbSNicholas Bellinger 	if (se_tpg->tpg_rtpi && se_tpg->enabled)
4706bb82612SNicholas Bellinger 		xa_erase(&tpg_xa, se_tpg->tpg_rtpi);
471e4aae5afSChristoph Hellwig }
472c66ac9dbSNicholas Bellinger 
target_tpg_enable(struct se_portal_group * se_tpg)473c66ac9dbSNicholas Bellinger int target_tpg_enable(struct se_portal_group *se_tpg)
474c66ac9dbSNicholas Bellinger {
475c66ac9dbSNicholas Bellinger 	int ret;
476c66ac9dbSNicholas Bellinger 
4776bb82612SNicholas Bellinger 	ret = target_tpg_register_rtpi(se_tpg);
478403edd78SNicholas Bellinger 	if (ret)
479c66ac9dbSNicholas Bellinger 		return ret;
480e4aae5afSChristoph Hellwig 
481adf653f9SChristoph Hellwig 	ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, true);
482adf653f9SChristoph Hellwig 	if (ret) {
483adf653f9SChristoph Hellwig 		target_tpg_deregister_rtpi(se_tpg);
484adf653f9SChristoph Hellwig 		return ret;
485adf653f9SChristoph Hellwig 	}
48603a68b44SAndy Grover 
487adf653f9SChristoph Hellwig 	se_tpg->enabled = true;
488adf653f9SChristoph Hellwig 
489adf653f9SChristoph Hellwig 	return 0;
490c66ac9dbSNicholas Bellinger }
491c66ac9dbSNicholas Bellinger 
target_tpg_disable(struct se_portal_group * se_tpg)492c66ac9dbSNicholas Bellinger int target_tpg_disable(struct se_portal_group *se_tpg)
493e4aae5afSChristoph Hellwig {
49430c7ca93SDavid Disseldorp 	int ret;
495bc0c94b1SNicholas Bellinger 
496bc0c94b1SNicholas Bellinger 	target_tpg_deregister_rtpi(se_tpg);
497bc0c94b1SNicholas Bellinger 
498c66ac9dbSNicholas Bellinger 	ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, false);
499c66ac9dbSNicholas Bellinger 	if (!ret)
500c66ac9dbSNicholas Bellinger 		se_tpg->enabled = false;
501c66ac9dbSNicholas Bellinger 
502c66ac9dbSNicholas Bellinger 	return ret;
503c66ac9dbSNicholas Bellinger }
504c66ac9dbSNicholas Bellinger 
505e4aae5afSChristoph Hellwig /* Does not change se_wwn->priv. */
core_tpg_register(struct se_wwn * se_wwn,struct se_portal_group * se_tpg,int proto_id)506e89d15eeSNicholas Bellinger int core_tpg_register(
50722793de5SNicholas Bellinger 	struct se_wwn *se_wwn,
508e89d15eeSNicholas Bellinger 	struct se_portal_group *se_tpg,
509e4aae5afSChristoph Hellwig 	int proto_id)
51030c7ca93SDavid Disseldorp {
511e4aae5afSChristoph Hellwig 	int ret;
512e4aae5afSChristoph Hellwig 
513c66ac9dbSNicholas Bellinger 	if (!se_tpg)
514c66ac9dbSNicholas Bellinger 		return -EINVAL;
515c66ac9dbSNicholas Bellinger 	/*
516403edd78SNicholas Bellinger 	 * For the typical case where core_tpg_register() is called by a
51722793de5SNicholas Bellinger 	 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
51822793de5SNicholas Bellinger 	 * configfs context, use the original tf_ops pointer already saved
51922793de5SNicholas Bellinger 	 * by target-core in target_fabric_make_wwn().
520e89d15eeSNicholas Bellinger 	 *
521e89d15eeSNicholas Bellinger 	 * Otherwise, for special cases like iscsi-target discovery TPGs
522e89d15eeSNicholas Bellinger 	 * the caller is responsible for setting ->se_tpg_tfo ahead of
523e89d15eeSNicholas Bellinger 	 * calling core_tpg_register().
524e89d15eeSNicholas Bellinger 	 */
52522793de5SNicholas Bellinger 	if (se_wwn)
5266f48655fSNicholas Bellinger 		se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
527e89d15eeSNicholas Bellinger 
528e89d15eeSNicholas Bellinger 	if (!se_tpg->se_tpg_tfo) {
529e89d15eeSNicholas Bellinger 		pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
530144bc4c2SChristoph Hellwig 		return -EINVAL;
531e89d15eeSNicholas Bellinger 	}
532c66ac9dbSNicholas Bellinger 
533adf653f9SChristoph Hellwig 	INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
534adf653f9SChristoph Hellwig 	se_tpg->proto_id = proto_id;
535adf653f9SChristoph Hellwig 	se_tpg->se_tpg_wwn = se_wwn;
536adf653f9SChristoph Hellwig 	atomic_set(&se_tpg->tpg_pr_ref_count, 0);
537c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->acl_node_list);
538c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
539c66ac9dbSNicholas Bellinger 	spin_lock_init(&se_tpg->session_lock);
540c66ac9dbSNicholas Bellinger 	mutex_init(&se_tpg->tpg_lun_mutex);
541c66ac9dbSNicholas Bellinger 	mutex_init(&se_tpg->acl_node_mutex);
542d344f8a1SAndy Grover 
543c66ac9dbSNicholas Bellinger 	if (se_tpg->proto_id >= 0) {
544f2d30680SHannes Reinecke 		se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
545c66ac9dbSNicholas Bellinger 		if (IS_ERR(se_tpg->tpg_virt_lun0))
546c66ac9dbSNicholas Bellinger 			return PTR_ERR(se_tpg->tpg_virt_lun0);
547c66ac9dbSNicholas Bellinger 
5486bb82612SNicholas Bellinger 		ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
5496bb82612SNicholas Bellinger 				true, g_lun0_dev);
5506bb82612SNicholas Bellinger 		if (ret < 0) {
5516bb82612SNicholas Bellinger 			kfree(se_tpg->tpg_virt_lun0);
552c66ac9dbSNicholas Bellinger 			return ret;
5536bb82612SNicholas Bellinger 		}
5546bb82612SNicholas Bellinger 	}
555bd4e2d29SNicholas Bellinger 
556adf653f9SChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
557adf653f9SChristoph Hellwig 		 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->fabric_name,
558adf653f9SChristoph Hellwig 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
559adf653f9SChristoph Hellwig 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
560adf653f9SChristoph Hellwig 		se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
561adf653f9SChristoph Hellwig 
562adf653f9SChristoph Hellwig 	return 0;
563adf653f9SChristoph Hellwig }
564c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_register);
565c66ac9dbSNicholas Bellinger 
core_tpg_deregister(struct se_portal_group * se_tpg)566c66ac9dbSNicholas Bellinger int core_tpg_deregister(struct se_portal_group *se_tpg)
567c66ac9dbSNicholas Bellinger {
568d344f8a1SAndy Grover 	const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
569c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl, *nacl_tmp;
570c66ac9dbSNicholas Bellinger 	LIST_HEAD(node_list);
57103a68b44SAndy Grover 
572340dbf72SAndy Grover 	pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
573c66ac9dbSNicholas Bellinger 		 "Proto: %d, Portal Tag: %u\n", tfo->fabric_name,
574e3d6f909SAndy Grover 		tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
575e3d6f909SAndy Grover 		se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
5762aad2a86STejun Heo 
577a34375efSTejun Heo 	while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
578e3d6f909SAndy Grover 		cpu_relax();
579adf653f9SChristoph Hellwig 
580c66ac9dbSNicholas Bellinger 	mutex_lock(&se_tpg->acl_node_mutex);
581adf653f9SChristoph Hellwig 	list_splice_init(&se_tpg->acl_node_list, &node_list);
582adf653f9SChristoph Hellwig 	mutex_unlock(&se_tpg->acl_node_mutex);
583adf653f9SChristoph Hellwig 	/*
5845277797dSNicholas Bellinger 	 * Release any remaining demo-mode generated se_node_acl that have
58569088a04SBodo Stroesser 	 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
586adf653f9SChristoph Hellwig 	 * in transport_deregister_session().
587adf653f9SChristoph Hellwig 	 */
588c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
5896bb82612SNicholas Bellinger 		list_del_init(&nacl->acl_list);
590adf653f9SChristoph Hellwig 
591adf653f9SChristoph Hellwig 		core_tpg_wait_for_nacl_pr_ref(nacl);
5924cc987eaSNicholas Bellinger 		core_free_device_list_for_node(nacl, se_tpg);
593adf653f9SChristoph Hellwig 		kfree(nacl);
594adf653f9SChristoph Hellwig 	}
595adf653f9SChristoph Hellwig 
596adf653f9SChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
597adf653f9SChristoph Hellwig 		core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
598eeeb9522SNicholas Bellinger 		kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
59903a68b44SAndy Grover 	}
600eeeb9522SNicholas Bellinger 
60103a68b44SAndy Grover 	target_tpg_deregister_rtpi(se_tpg);
6026bb82612SNicholas Bellinger 
6036bb82612SNicholas Bellinger 	return 0;
6046bb82612SNicholas Bellinger }
605c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_deregister);
606c66ac9dbSNicholas Bellinger 
core_tpg_alloc_lun(struct se_portal_group * tpg,u64 unpacked_lun)607adf653f9SChristoph Hellwig struct se_lun *core_tpg_alloc_lun(
608adf653f9SChristoph Hellwig 	struct se_portal_group *tpg,
609adf653f9SChristoph Hellwig 	u64 unpacked_lun)
610adf653f9SChristoph Hellwig {
611adf653f9SChristoph Hellwig 	struct se_lun *lun;
612c66ac9dbSNicholas Bellinger 
613c66ac9dbSNicholas Bellinger 	lun = kzalloc(sizeof(*lun), GFP_KERNEL);
614cd9d7cbaSAndy Grover 	if (!lun) {
615c66ac9dbSNicholas Bellinger 		pr_err("Unable to allocate se_lun memory\n");
616c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
617c66ac9dbSNicholas Bellinger 	}
6184cc987eaSNicholas Bellinger 	lun->unpacked_lun = unpacked_lun;
6194cc987eaSNicholas Bellinger 	atomic_set(&lun->lun_acl_count, 0);
6204cc987eaSNicholas Bellinger 	init_completion(&lun->lun_shutdown_comp);
6214cc987eaSNicholas Bellinger 	INIT_LIST_HEAD(&lun->lun_deve_list);
6224cc987eaSNicholas Bellinger 	INIT_LIST_HEAD(&lun->lun_dev_link);
6236bb82612SNicholas Bellinger 	atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
62449cb77e2SNicholas Bellinger 	spin_lock_init(&lun->lun_deve_lock);
62549cb77e2SNicholas Bellinger 	mutex_init(&lun->lun_tg_pt_md_mutex);
6264a9a6c8dSNicholas Bellinger 	INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
6279e37d042SNicholas Bellinger 	spin_lock_init(&lun->lun_tg_pt_gp_lock);
6289e37d042SNicholas Bellinger 	lun->lun_tpg = tpg;
6299e37d042SNicholas Bellinger 
6309e37d042SNicholas Bellinger 	return lun;
6319e37d042SNicholas Bellinger }
6329e37d042SNicholas Bellinger 
core_tpg_add_lun(struct se_portal_group * tpg,struct se_lun * lun,bool lun_access_ro,struct se_device * dev)6334a9a6c8dSNicholas Bellinger int core_tpg_add_lun(
634c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
6356bb82612SNicholas Bellinger 	struct se_lun *lun,
636adf653f9SChristoph Hellwig 	bool lun_access_ro,
637adf653f9SChristoph Hellwig 	struct se_device *dev)
638c66ac9dbSNicholas Bellinger {
639adf653f9SChristoph Hellwig 	int ret;
640adf653f9SChristoph Hellwig 
641adf653f9SChristoph Hellwig 	ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
642adf653f9SChristoph Hellwig 			      GFP_KERNEL);
643adf653f9SChristoph Hellwig 	if (ret < 0)
644adf653f9SChristoph Hellwig 		goto out;
6456bb82612SNicholas Bellinger 
6466bb82612SNicholas Bellinger 	if (!(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
64749cb77e2SNicholas Bellinger 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
64849cb77e2SNicholas Bellinger 		target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
6496bb82612SNicholas Bellinger 
650c66ac9dbSNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
6519a1049daSTejun Heo 
652c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
653 	lun->lun_index = dev->dev_index;
654 	rcu_assign_pointer(lun->lun_se_dev, dev);
655 	dev->export_count++;
656 	list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
657 	spin_unlock(&dev->se_port_lock);
658 
659 	if (dev->dev_flags & DF_READ_ONLY)
660 		lun->lun_access_ro = true;
661 	else
662 		lun->lun_access_ro = lun_access_ro;
663 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
664 		hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
665 	mutex_unlock(&tpg->tpg_lun_mutex);
666 
667 	return 0;
668 
669 out:
670 	return ret;
671 }
672 
core_tpg_remove_lun(struct se_portal_group * tpg,struct se_lun * lun)673 void core_tpg_remove_lun(
674 	struct se_portal_group *tpg,
675 	struct se_lun *lun)
676 {
677 	/*
678 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
679 	 * reference to se_device->dev_group.
680 	 */
681 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
682 
683 	lun->lun_shutdown = true;
684 
685 	core_clear_lun_from_tpg(lun, tpg);
686 	/*
687 	 * Wait for any active I/O references to percpu se_lun->lun_ref to
688 	 * be released.  Also, se_lun->lun_ref is now used by PR and ALUA
689 	 * logic when referencing a remote target port during ALL_TGT_PT=1
690 	 * and generating UNIT_ATTENTIONs for ALUA access state transition.
691 	 */
692 	transport_clear_lun_ref(lun);
693 
694 	mutex_lock(&tpg->tpg_lun_mutex);
695 	if (lun->lun_se_dev) {
696 		target_detach_tg_pt_gp(lun);
697 
698 		spin_lock(&dev->se_port_lock);
699 		list_del(&lun->lun_dev_link);
700 		dev->export_count--;
701 		rcu_assign_pointer(lun->lun_se_dev, NULL);
702 		spin_unlock(&dev->se_port_lock);
703 	}
704 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
705 		hlist_del_rcu(&lun->link);
706 
707 	lun->lun_shutdown = false;
708 	mutex_unlock(&tpg->tpg_lun_mutex);
709 
710 	percpu_ref_exit(&lun->lun_ref);
711 }
712