blob: c672a40cbfe2c19b8f642063872ae2950139dc3e [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 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
29#include <linux/net.h>
30#include <linux/string.h>
31#include <linux/timer.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080034#include <linux/in.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040035#include <linux/export.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <net/sock.h>
37#include <net/tcp.h>
38#include <scsi/scsi.h>
39#include <scsi/scsi_cmnd.h>
40
41#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050042#include <target/target_core_backend.h>
43#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044
Christoph Hellwige26d99a2011-11-14 12:30:30 -050045#include "target_core_internal.h"
Andy Grovere3d6f902011-07-19 08:55:10 +000046
47extern struct se_device *g_lun0_dev;
48
49static DEFINE_SPINLOCK(tpg_lock);
50static LIST_HEAD(tpg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051
52/* core_clear_initiator_node_from_tpg():
53 *
54 *
55 */
56static void core_clear_initiator_node_from_tpg(
57 struct se_node_acl *nacl,
58 struct se_portal_group *tpg)
59{
60 int i;
61 struct se_dev_entry *deve;
62 struct se_lun *lun;
63 struct se_lun_acl *acl, *acl_tmp;
64
65 spin_lock_irq(&nacl->device_list_lock);
66 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -040067 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068
69 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
70 continue;
71
72 if (!deve->se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -070073 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +000075 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080076 continue;
77 }
78
79 lun = deve->se_lun;
80 spin_unlock_irq(&nacl->device_list_lock);
81 core_update_device_list_for_node(lun, NULL, deve->mapped_lun,
82 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
83
84 spin_lock(&lun->lun_acl_lock);
85 list_for_each_entry_safe(acl, acl_tmp,
86 &lun->lun_acl_list, lacl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -070087 if (!strcmp(acl->initiatorname, nacl->initiatorname) &&
88 (acl->mapped_lun == deve->mapped_lun))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 break;
90 }
91
92 if (!acl) {
Andy Grover6708bb22011-06-08 10:36:43 -070093 pr_err("Unable to locate struct se_lun_acl for %s,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094 " mapped_lun: %u\n", nacl->initiatorname,
95 deve->mapped_lun);
96 spin_unlock(&lun->lun_acl_lock);
97 spin_lock_irq(&nacl->device_list_lock);
98 continue;
99 }
100
101 list_del(&acl->lacl_list);
102 spin_unlock(&lun->lun_acl_lock);
103
104 spin_lock_irq(&nacl->device_list_lock);
105 kfree(acl);
106 }
107 spin_unlock_irq(&nacl->device_list_lock);
108}
109
110/* __core_tpg_get_initiator_node_acl():
111 *
112 * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
113 */
114struct se_node_acl *__core_tpg_get_initiator_node_acl(
115 struct se_portal_group *tpg,
116 const char *initiatorname)
117{
118 struct se_node_acl *acl;
119
120 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700121 if (!strcmp(acl->initiatorname, initiatorname))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 return acl;
123 }
124
125 return NULL;
126}
127
128/* core_tpg_get_initiator_node_acl():
129 *
130 *
131 */
132struct se_node_acl *core_tpg_get_initiator_node_acl(
133 struct se_portal_group *tpg,
134 unsigned char *initiatorname)
135{
136 struct se_node_acl *acl;
137
Roland Dreier28638882011-08-16 09:40:01 -0700138 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700140 if (!strcmp(acl->initiatorname, initiatorname) &&
141 !acl->dynamic_node_acl) {
Roland Dreier28638882011-08-16 09:40:01 -0700142 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800143 return acl;
144 }
145 }
Roland Dreier28638882011-08-16 09:40:01 -0700146 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147
148 return NULL;
149}
150
151/* core_tpg_add_node_to_devs():
152 *
153 *
154 */
155void core_tpg_add_node_to_devs(
156 struct se_node_acl *acl,
157 struct se_portal_group *tpg)
158{
159 int i = 0;
160 u32 lun_access = 0;
161 struct se_lun *lun;
162 struct se_device *dev;
163
164 spin_lock(&tpg->tpg_lun_lock);
165 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400166 lun = tpg->tpg_lun_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800167 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
168 continue;
169
170 spin_unlock(&tpg->tpg_lun_lock);
171
172 dev = lun->lun_se_dev;
173 /*
174 * By default in LIO-Target $FABRIC_MOD,
175 * demo_mode_write_protect is ON, or READ_ONLY;
176 */
Andy Grover6708bb22011-06-08 10:36:43 -0700177 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
Nicholas Bellinger58d92612012-03-20 21:26:48 -0700178 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800179 } else {
180 /*
181 * Allow only optical drives to issue R/W in default RO
182 * demo mode.
183 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000184 if (dev->transport->get_device_type(dev) == TYPE_DISK)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
186 else
187 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
188 }
189
Andy Grover6708bb22011-06-08 10:36:43 -0700190 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800191 " access for LUN in Demo Mode\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000192 tpg->se_tpg_tfo->get_fabric_name(),
193 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800194 (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
195 "READ-WRITE" : "READ-ONLY");
196
197 core_update_device_list_for_node(lun, NULL, lun->unpacked_lun,
198 lun_access, acl, tpg, 1);
199 spin_lock(&tpg->tpg_lun_lock);
200 }
201 spin_unlock(&tpg->tpg_lun_lock);
202}
203
204/* core_set_queue_depth_for_node():
205 *
206 *
207 */
208static int core_set_queue_depth_for_node(
209 struct se_portal_group *tpg,
210 struct se_node_acl *acl)
211{
212 if (!acl->queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700213 pr_err("Queue depth for %s Initiator Node: %s is 0,"
Andy Grovere3d6f902011-07-19 08:55:10 +0000214 "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215 acl->initiatorname);
216 acl->queue_depth = 1;
217 }
218
219 return 0;
220}
221
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400222void array_free(void *array, int n)
223{
224 void **a = array;
225 int i;
226
227 for (i = 0; i < n; i++)
228 kfree(a[i]);
229 kfree(a);
230}
231
232static void *array_zalloc(int n, size_t size, gfp_t flags)
233{
234 void **a;
235 int i;
236
237 a = kzalloc(n * sizeof(void*), flags);
238 if (!a)
239 return NULL;
240 for (i = 0; i < n; i++) {
241 a[i] = kzalloc(size, flags);
242 if (!a[i]) {
243 array_free(a, n);
244 return NULL;
245 }
246 }
247 return a;
248}
249
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250/* core_create_device_list_for_node():
251 *
252 *
253 */
254static int core_create_device_list_for_node(struct se_node_acl *nacl)
255{
256 struct se_dev_entry *deve;
257 int i;
258
Jörn Engelf2083242012-03-15 15:05:40 -0400259 nacl->device_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
260 sizeof(struct se_dev_entry), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700261 if (!nacl->device_list) {
262 pr_err("Unable to allocate memory for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263 " struct se_node_acl->device_list\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000264 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800265 }
266 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400267 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800268
269 atomic_set(&deve->ua_count, 0);
270 atomic_set(&deve->pr_ref_count, 0);
271 spin_lock_init(&deve->ua_lock);
272 INIT_LIST_HEAD(&deve->alua_port_list);
273 INIT_LIST_HEAD(&deve->ua_list);
274 }
275
276 return 0;
277}
278
279/* core_tpg_check_initiator_node_acl()
280 *
281 *
282 */
283struct se_node_acl *core_tpg_check_initiator_node_acl(
284 struct se_portal_group *tpg,
285 unsigned char *initiatorname)
286{
287 struct se_node_acl *acl;
288
289 acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700290 if (acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800291 return acl;
292
Andy Grover6708bb22011-06-08 10:36:43 -0700293 if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 return NULL;
295
Andy Grovere3d6f902011-07-19 08:55:10 +0000296 acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
Andy Grover6708bb22011-06-08 10:36:43 -0700297 if (!acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 return NULL;
299
300 INIT_LIST_HEAD(&acl->acl_list);
301 INIT_LIST_HEAD(&acl->acl_sess_list);
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800302 kref_init(&acl->acl_kref);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800303 init_completion(&acl->acl_free_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 spin_lock_init(&acl->device_list_lock);
305 spin_lock_init(&acl->nacl_sess_lock);
306 atomic_set(&acl->acl_pr_ref_count, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +0000307 acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
309 acl->se_tpg = tpg;
310 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
311 spin_lock_init(&acl->stats_lock);
312 acl->dynamic_node_acl = 1;
313
Andy Grovere3d6f902011-07-19 08:55:10 +0000314 tpg->se_tpg_tfo->set_default_node_attributes(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315
316 if (core_create_device_list_for_node(acl) < 0) {
Andy Grovere3d6f902011-07-19 08:55:10 +0000317 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 return NULL;
319 }
320
321 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
322 core_free_device_list_for_node(acl, tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000323 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 return NULL;
325 }
Nicholas Bellinger052605c2011-07-26 17:48:43 -0700326 /*
327 * Here we only create demo-mode MappedLUNs from the active
328 * TPG LUNs if the fabric is not explictly asking for
329 * tpg_check_demo_mode_login_only() == 1.
330 */
331 if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only != NULL) &&
332 (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) == 1))
333 do { ; } while (0);
334 else
335 core_tpg_add_node_to_devs(acl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336
Roland Dreier28638882011-08-16 09:40:01 -0700337 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
339 tpg->num_node_acls++;
Roland Dreier28638882011-08-16 09:40:01 -0700340 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341
Andy Grover6708bb22011-06-08 10:36:43 -0700342 pr_debug("%s_TPG[%u] - Added DYNAMIC ACL with TCQ Depth: %d for %s"
Andy Grovere3d6f902011-07-19 08:55:10 +0000343 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
344 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
345 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346
347 return acl;
348}
349EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
350
351void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
352{
353 while (atomic_read(&nacl->acl_pr_ref_count) != 0)
354 cpu_relax();
355}
356
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357void core_tpg_clear_object_luns(struct se_portal_group *tpg)
358{
Jörn Engel28168902012-03-15 15:06:58 -0400359 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 struct se_lun *lun;
361
362 spin_lock(&tpg->tpg_lun_lock);
363 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400364 lun = tpg->tpg_lun_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800365
366 if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) ||
367 (lun->lun_se_dev == NULL))
368 continue;
369
370 spin_unlock(&tpg->tpg_lun_lock);
Jörn Engel28168902012-03-15 15:06:58 -0400371 core_dev_del_lun(tpg, lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 spin_lock(&tpg->tpg_lun_lock);
373 }
374 spin_unlock(&tpg->tpg_lun_lock);
375}
376EXPORT_SYMBOL(core_tpg_clear_object_luns);
377
378/* core_tpg_add_initiator_node_acl():
379 *
380 *
381 */
382struct se_node_acl *core_tpg_add_initiator_node_acl(
383 struct se_portal_group *tpg,
384 struct se_node_acl *se_nacl,
385 const char *initiatorname,
386 u32 queue_depth)
387{
388 struct se_node_acl *acl = NULL;
389
Roland Dreier28638882011-08-16 09:40:01 -0700390 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393 if (acl->dynamic_node_acl) {
394 acl->dynamic_node_acl = 0;
Andy Grover6708bb22011-06-08 10:36:43 -0700395 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
Andy Grovere3d6f902011-07-19 08:55:10 +0000396 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
397 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
Roland Dreier28638882011-08-16 09:40:01 -0700398 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 /*
400 * Release the locally allocated struct se_node_acl
401 * because * core_tpg_add_initiator_node_acl() returned
402 * a pointer to an existing demo mode node ACL.
403 */
404 if (se_nacl)
Andy Grovere3d6f902011-07-19 08:55:10 +0000405 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 se_nacl);
407 goto done;
408 }
409
Andy Grover6708bb22011-06-08 10:36:43 -0700410 pr_err("ACL entry for %s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 " Node %s already exists for TPG %u, ignoring"
Andy Grovere3d6f902011-07-19 08:55:10 +0000412 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
413 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
Roland Dreier28638882011-08-16 09:40:01 -0700414 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 return ERR_PTR(-EEXIST);
416 }
Roland Dreier28638882011-08-16 09:40:01 -0700417 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418
Andy Grover6708bb22011-06-08 10:36:43 -0700419 if (!se_nacl) {
420 pr_err("struct se_node_acl pointer is NULL\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 return ERR_PTR(-EINVAL);
422 }
423 /*
424 * For v4.x logic the se_node_acl_s is hanging off a fabric
425 * dependent structure allocated via
426 * struct target_core_fabric_ops->fabric_make_nodeacl()
427 */
428 acl = se_nacl;
429
430 INIT_LIST_HEAD(&acl->acl_list);
431 INIT_LIST_HEAD(&acl->acl_sess_list);
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800432 kref_init(&acl->acl_kref);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800433 init_completion(&acl->acl_free_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800434 spin_lock_init(&acl->device_list_lock);
435 spin_lock_init(&acl->nacl_sess_lock);
436 atomic_set(&acl->acl_pr_ref_count, 0);
437 acl->queue_depth = queue_depth;
438 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
439 acl->se_tpg = tpg;
440 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
441 spin_lock_init(&acl->stats_lock);
442
Andy Grovere3d6f902011-07-19 08:55:10 +0000443 tpg->se_tpg_tfo->set_default_node_attributes(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444
445 if (core_create_device_list_for_node(acl) < 0) {
Andy Grovere3d6f902011-07-19 08:55:10 +0000446 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447 return ERR_PTR(-ENOMEM);
448 }
449
450 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
451 core_free_device_list_for_node(acl, tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000452 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 return ERR_PTR(-EINVAL);
454 }
455
Roland Dreier28638882011-08-16 09:40:01 -0700456 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
458 tpg->num_node_acls++;
Roland Dreier28638882011-08-16 09:40:01 -0700459 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460
461done:
Andy Grover6708bb22011-06-08 10:36:43 -0700462 pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
Andy Grovere3d6f902011-07-19 08:55:10 +0000463 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
464 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
465 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466
467 return acl;
468}
469EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
470
471/* core_tpg_del_initiator_node_acl():
472 *
473 *
474 */
475int core_tpg_del_initiator_node_acl(
476 struct se_portal_group *tpg,
477 struct se_node_acl *acl,
478 int force)
479{
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800480 LIST_HEAD(sess_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 struct se_session *sess, *sess_tmp;
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700482 unsigned long flags;
Jörn Engel28168902012-03-15 15:06:58 -0400483 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484
Roland Dreier28638882011-08-16 09:40:01 -0700485 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486 if (acl->dynamic_node_acl) {
487 acl->dynamic_node_acl = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488 }
489 list_del(&acl->acl_list);
490 tpg->num_node_acls--;
Roland Dreier28638882011-08-16 09:40:01 -0700491 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800493 spin_lock_irqsave(&acl->nacl_sess_lock, flags);
494 acl->acl_stop = 1;
495
496 list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
497 sess_acl_list) {
498 if (sess->sess_tearing_down != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499 continue;
500
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800501 target_get_session(sess);
502 list_move(&sess->sess_acl_list, &sess_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 }
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800504 spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
505
506 list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
507 list_del(&sess->sess_acl_list);
508
509 rc = tpg->se_tpg_tfo->shutdown_session(sess);
510 target_put_session(sess);
511 if (!rc)
512 continue;
513 target_put_session(sess);
514 }
515 target_put_nacl(acl);
516 /*
517 * Wait for last target_put_nacl() to complete in target_complete_nacl()
518 * for active fabric session transport_deregister_session() callbacks.
519 */
520 wait_for_completion(&acl->acl_free_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521
522 core_tpg_wait_for_nacl_pr_ref(acl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 core_clear_initiator_node_from_tpg(acl, tpg);
524 core_free_device_list_for_node(acl, tpg);
525
Andy Grover6708bb22011-06-08 10:36:43 -0700526 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
Andy Grovere3d6f902011-07-19 08:55:10 +0000527 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
528 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
529 tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530
531 return 0;
532}
533EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
534
535/* core_tpg_set_initiator_node_queue_depth():
536 *
537 *
538 */
539int core_tpg_set_initiator_node_queue_depth(
540 struct se_portal_group *tpg,
541 unsigned char *initiatorname,
542 u32 queue_depth,
543 int force)
544{
545 struct se_session *sess, *init_sess = NULL;
546 struct se_node_acl *acl;
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700547 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548 int dynamic_acl = 0;
549
Roland Dreier28638882011-08-16 09:40:01 -0700550 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -0700552 if (!acl) {
553 pr_err("Access Control List entry for %s Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 " Node %s does not exists for TPG %hu, ignoring"
Andy Grovere3d6f902011-07-19 08:55:10 +0000555 " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
556 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
Roland Dreier28638882011-08-16 09:40:01 -0700557 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558 return -ENODEV;
559 }
560 if (acl->dynamic_node_acl) {
561 acl->dynamic_node_acl = 0;
562 dynamic_acl = 1;
563 }
Roland Dreier28638882011-08-16 09:40:01 -0700564 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800565
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700566 spin_lock_irqsave(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
568 if (sess->se_node_acl != acl)
569 continue;
570
571 if (!force) {
Andy Grover6708bb22011-06-08 10:36:43 -0700572 pr_err("Unable to change queue depth for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573 " Initiator Node: %s while session is"
574 " operational. To forcefully change the queue"
575 " depth and force session reinstatement"
576 " use the \"force=1\" parameter.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000577 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700578 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579
Roland Dreier28638882011-08-16 09:40:01 -0700580 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 if (dynamic_acl)
582 acl->dynamic_node_acl = 1;
Roland Dreier28638882011-08-16 09:40:01 -0700583 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 return -EEXIST;
585 }
586 /*
587 * Determine if the session needs to be closed by our context.
588 */
Andy Grover6708bb22011-06-08 10:36:43 -0700589 if (!tpg->se_tpg_tfo->shutdown_session(sess))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 continue;
591
592 init_sess = sess;
593 break;
594 }
595
596 /*
597 * User has requested to change the queue depth for a Initiator Node.
598 * Change the value in the Node's struct se_node_acl, and call
599 * core_set_queue_depth_for_node() to add the requested queue depth.
600 *
Andy Grovere3d6f902011-07-19 08:55:10 +0000601 * Finally call tpg->se_tpg_tfo->close_session() to force session
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 * reinstatement to occur if there is an active session for the
603 * $FABRIC_MOD Initiator Node in question.
604 */
605 acl->queue_depth = queue_depth;
606
607 if (core_set_queue_depth_for_node(tpg, acl) < 0) {
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700608 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 /*
610 * Force session reinstatement if
611 * core_set_queue_depth_for_node() failed, because we assume
612 * the $FABRIC_MOD has already the set session reinstatement
Andy Grovere3d6f902011-07-19 08:55:10 +0000613 * bit from tpg->se_tpg_tfo->shutdown_session() called above.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 */
615 if (init_sess)
Andy Grovere3d6f902011-07-19 08:55:10 +0000616 tpg->se_tpg_tfo->close_session(init_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617
Roland Dreier28638882011-08-16 09:40:01 -0700618 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619 if (dynamic_acl)
620 acl->dynamic_node_acl = 1;
Roland Dreier28638882011-08-16 09:40:01 -0700621 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 return -EINVAL;
623 }
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700624 spin_unlock_irqrestore(&tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 /*
626 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
627 * forcefully shutdown the $FABRIC_MOD session/nexus.
628 */
629 if (init_sess)
Andy Grovere3d6f902011-07-19 08:55:10 +0000630 tpg->se_tpg_tfo->close_session(init_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631
Joe Perchesbfb90352011-08-17 06:58:04 -0700632 pr_debug("Successfully changed queue depth to: %d for Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 " Node: %s on %s Target Portal Group: %u\n", queue_depth,
Andy Grovere3d6f902011-07-19 08:55:10 +0000634 initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
635 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636
Roland Dreier28638882011-08-16 09:40:01 -0700637 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638 if (dynamic_acl)
639 acl->dynamic_node_acl = 1;
Roland Dreier28638882011-08-16 09:40:01 -0700640 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800641
642 return 0;
643}
644EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
645
646static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
647{
648 /* Set in core_dev_setup_virtual_lun0() */
Andy Grovere3d6f902011-07-19 08:55:10 +0000649 struct se_device *dev = g_lun0_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
651 u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
652 int ret;
653
654 lun->unpacked_lun = 0;
655 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
656 atomic_set(&lun->lun_acl_count, 0);
657 init_completion(&lun->lun_shutdown_comp);
658 INIT_LIST_HEAD(&lun->lun_acl_list);
659 INIT_LIST_HEAD(&lun->lun_cmd_list);
660 spin_lock_init(&lun->lun_acl_lock);
661 spin_lock_init(&lun->lun_cmd_lock);
662 spin_lock_init(&lun->lun_sep_lock);
663
664 ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
665 if (ret < 0)
Andy Grovere3d6f902011-07-19 08:55:10 +0000666 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667
668 return 0;
669}
670
671static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
672{
673 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
674
675 core_tpg_post_dellun(se_tpg, lun);
676}
677
678int core_tpg_register(
679 struct target_core_fabric_ops *tfo,
680 struct se_wwn *se_wwn,
681 struct se_portal_group *se_tpg,
682 void *tpg_fabric_ptr,
683 int se_tpg_type)
684{
685 struct se_lun *lun;
686 u32 i;
687
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400688 se_tpg->tpg_lun_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
689 sizeof(struct se_lun), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700690 if (!se_tpg->tpg_lun_list) {
691 pr_err("Unable to allocate struct se_portal_group->"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692 "tpg_lun_list\n");
693 return -ENOMEM;
694 }
695
696 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400697 lun = se_tpg->tpg_lun_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 lun->unpacked_lun = i;
699 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
700 atomic_set(&lun->lun_acl_count, 0);
701 init_completion(&lun->lun_shutdown_comp);
702 INIT_LIST_HEAD(&lun->lun_acl_list);
703 INIT_LIST_HEAD(&lun->lun_cmd_list);
704 spin_lock_init(&lun->lun_acl_lock);
705 spin_lock_init(&lun->lun_cmd_lock);
706 spin_lock_init(&lun->lun_sep_lock);
707 }
708
709 se_tpg->se_tpg_type = se_tpg_type;
710 se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
711 se_tpg->se_tpg_tfo = tfo;
712 se_tpg->se_tpg_wwn = se_wwn;
713 atomic_set(&se_tpg->tpg_pr_ref_count, 0);
714 INIT_LIST_HEAD(&se_tpg->acl_node_list);
Andy Grovere3d6f902011-07-19 08:55:10 +0000715 INIT_LIST_HEAD(&se_tpg->se_tpg_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
717 spin_lock_init(&se_tpg->acl_node_lock);
718 spin_lock_init(&se_tpg->session_lock);
719 spin_lock_init(&se_tpg->tpg_lun_lock);
720
721 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
722 if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
723 kfree(se_tpg);
724 return -ENOMEM;
725 }
726 }
727
Andy Grovere3d6f902011-07-19 08:55:10 +0000728 spin_lock_bh(&tpg_lock);
729 list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
730 spin_unlock_bh(&tpg_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800731
Andy Grover6708bb22011-06-08 10:36:43 -0700732 pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
734 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
735 "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
736 "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
737
738 return 0;
739}
740EXPORT_SYMBOL(core_tpg_register);
741
742int core_tpg_deregister(struct se_portal_group *se_tpg)
743{
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800744 struct se_node_acl *nacl, *nacl_tmp;
745
Andy Grover6708bb22011-06-08 10:36:43 -0700746 pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800747 " for endpoint: %s Portal Tag %u\n",
748 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
Andy Grovere3d6f902011-07-19 08:55:10 +0000749 "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
750 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
751 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800752
Andy Grovere3d6f902011-07-19 08:55:10 +0000753 spin_lock_bh(&tpg_lock);
754 list_del(&se_tpg->se_tpg_node);
755 spin_unlock_bh(&tpg_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800756
757 while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
758 cpu_relax();
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800759 /*
760 * Release any remaining demo-mode generated se_node_acl that have
761 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
762 * in transport_deregister_session().
763 */
Roland Dreier28638882011-08-16 09:40:01 -0700764 spin_lock_irq(&se_tpg->acl_node_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800765 list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
766 acl_list) {
767 list_del(&nacl->acl_list);
768 se_tpg->num_node_acls--;
Roland Dreier28638882011-08-16 09:40:01 -0700769 spin_unlock_irq(&se_tpg->acl_node_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800770
771 core_tpg_wait_for_nacl_pr_ref(nacl);
772 core_free_device_list_for_node(nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000773 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800774
Roland Dreier28638882011-08-16 09:40:01 -0700775 spin_lock_irq(&se_tpg->acl_node_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800776 }
Roland Dreier28638882011-08-16 09:40:01 -0700777 spin_unlock_irq(&se_tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800778
779 if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
780 core_tpg_release_virtual_lun0(se_tpg);
781
782 se_tpg->se_tpg_fabric_ptr = NULL;
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400783 array_free(se_tpg->tpg_lun_list, TRANSPORT_MAX_LUNS_PER_TPG);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800784 return 0;
785}
786EXPORT_SYMBOL(core_tpg_deregister);
787
788struct se_lun *core_tpg_pre_addlun(
789 struct se_portal_group *tpg,
790 u32 unpacked_lun)
791{
792 struct se_lun *lun;
793
794 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700795 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796 "-1: %u for Target Portal Group: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000797 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +0000799 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800 return ERR_PTR(-EOVERFLOW);
801 }
802
803 spin_lock(&tpg->tpg_lun_lock);
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400804 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700806 pr_err("TPG Logical Unit Number: %u is already active"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 " on %s Target Portal Group: %u, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000808 unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
809 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810 spin_unlock(&tpg->tpg_lun_lock);
811 return ERR_PTR(-EINVAL);
812 }
813 spin_unlock(&tpg->tpg_lun_lock);
814
815 return lun;
816}
817
818int core_tpg_post_addlun(
819 struct se_portal_group *tpg,
820 struct se_lun *lun,
821 u32 lun_access,
822 void *lun_ptr)
823{
Andy Grovere3d6f902011-07-19 08:55:10 +0000824 int ret;
825
826 ret = core_dev_export(lun_ptr, tpg, lun);
827 if (ret < 0)
828 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829
830 spin_lock(&tpg->tpg_lun_lock);
831 lun->lun_access = lun_access;
832 lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
833 spin_unlock(&tpg->tpg_lun_lock);
834
835 return 0;
836}
837
838static void core_tpg_shutdown_lun(
839 struct se_portal_group *tpg,
840 struct se_lun *lun)
841{
842 core_clear_lun_from_tpg(lun, tpg);
843 transport_clear_lun_from_sessions(lun);
844}
845
846struct se_lun *core_tpg_pre_dellun(
847 struct se_portal_group *tpg,
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +0100848 u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849{
850 struct se_lun *lun;
851
852 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700853 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854 "-1: %u for Target Portal Group: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000855 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +0000857 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858 return ERR_PTR(-EOVERFLOW);
859 }
860
861 spin_lock(&tpg->tpg_lun_lock);
Jörn Engel4a5a75f2012-03-15 15:05:12 -0400862 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
Andy Grover6708bb22011-06-08 10:36:43 -0700864 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800865 " Target Portal Group: %u, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000866 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
867 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800868 spin_unlock(&tpg->tpg_lun_lock);
869 return ERR_PTR(-ENODEV);
870 }
871 spin_unlock(&tpg->tpg_lun_lock);
872
873 return lun;
874}
875
876int core_tpg_post_dellun(
877 struct se_portal_group *tpg,
878 struct se_lun *lun)
879{
880 core_tpg_shutdown_lun(tpg, lun);
881
882 core_dev_unexport(lun->lun_se_dev, tpg, lun);
883
884 spin_lock(&tpg->tpg_lun_lock);
885 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
886 spin_unlock(&tpg->tpg_lun_lock);
887
888 return 0;
889}