target: Drop incorrect se_lun_acl release for dynamic -> explict ACL conversion
[pandora-kernel.git] / drivers / target / target_core_tpg.c
1 /*******************************************************************************
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>
34 #include <linux/in.h>
35 #include <linux/export.h>
36 #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>
42 #include <target/target_core_device.h>
43 #include <target/target_core_tpg.h>
44 #include <target/target_core_transport.h>
45 #include <target/target_core_fabric_ops.h>
46
47 #include "target_core_hba.h"
48 #include "target_core_stat.h"
49
50 extern struct se_device *g_lun0_dev;
51
52 static DEFINE_SPINLOCK(tpg_lock);
53 static LIST_HEAD(tpg_list);
54
55 /*      core_clear_initiator_node_from_tpg():
56  *
57  *
58  */
59 static void core_clear_initiator_node_from_tpg(
60         struct se_node_acl *nacl,
61         struct se_portal_group *tpg)
62 {
63         int i;
64         struct se_dev_entry *deve;
65         struct se_lun *lun;
66
67         spin_lock_irq(&nacl->device_list_lock);
68         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
69                 deve = &nacl->device_list[i];
70
71                 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
72                         continue;
73
74                 if (!deve->se_lun) {
75                         pr_err("%s device entries device pointer is"
76                                 " NULL, but Initiator has access.\n",
77                                 tpg->se_tpg_tfo->get_fabric_name());
78                         continue;
79                 }
80
81                 lun = deve->se_lun;
82                 spin_unlock_irq(&nacl->device_list_lock);
83                 core_update_device_list_for_node(lun, NULL, deve->mapped_lun,
84                         TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
85
86                 spin_lock_irq(&nacl->device_list_lock);
87         }
88         spin_unlock_irq(&nacl->device_list_lock);
89 }
90
91 /*      __core_tpg_get_initiator_node_acl():
92  *
93  *      spin_lock_bh(&tpg->acl_node_lock); must be held when calling
94  */
95 struct se_node_acl *__core_tpg_get_initiator_node_acl(
96         struct se_portal_group *tpg,
97         const char *initiatorname)
98 {
99         struct se_node_acl *acl;
100
101         list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
102                 if (!strcmp(acl->initiatorname, initiatorname))
103                         return acl;
104         }
105
106         return NULL;
107 }
108
109 /*      core_tpg_get_initiator_node_acl():
110  *
111  *
112  */
113 struct se_node_acl *core_tpg_get_initiator_node_acl(
114         struct se_portal_group *tpg,
115         unsigned char *initiatorname)
116 {
117         struct se_node_acl *acl;
118
119         spin_lock_irq(&tpg->acl_node_lock);
120         list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
121                 if (!strcmp(acl->initiatorname, initiatorname) &&
122                     !acl->dynamic_node_acl) {
123                         spin_unlock_irq(&tpg->acl_node_lock);
124                         return acl;
125                 }
126         }
127         spin_unlock_irq(&tpg->acl_node_lock);
128
129         return NULL;
130 }
131
132 /*      core_tpg_add_node_to_devs():
133  *
134  *
135  */
136 void core_tpg_add_node_to_devs(
137         struct se_node_acl *acl,
138         struct se_portal_group *tpg)
139 {
140         int i = 0;
141         u32 lun_access = 0;
142         struct se_lun *lun;
143         struct se_device *dev;
144
145         spin_lock(&tpg->tpg_lun_lock);
146         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
147                 lun = &tpg->tpg_lun_list[i];
148                 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
149                         continue;
150
151                 spin_unlock(&tpg->tpg_lun_lock);
152
153                 dev = lun->lun_se_dev;
154                 /*
155                  * By default in LIO-Target $FABRIC_MOD,
156                  * demo_mode_write_protect is ON, or READ_ONLY;
157                  */
158                 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
159                         if (dev->dev_flags & DF_READ_ONLY)
160                                 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
161                         else
162                                 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
163                 } else {
164                         /*
165                          * Allow only optical drives to issue R/W in default RO
166                          * demo mode.
167                          */
168                         if (dev->transport->get_device_type(dev) == TYPE_DISK)
169                                 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
170                         else
171                                 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
172                 }
173
174                 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
175                         " access for LUN in Demo Mode\n",
176                         tpg->se_tpg_tfo->get_fabric_name(),
177                         tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
178                         (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
179                         "READ-WRITE" : "READ-ONLY");
180
181                 core_update_device_list_for_node(lun, NULL, lun->unpacked_lun,
182                                 lun_access, acl, tpg, 1);
183                 spin_lock(&tpg->tpg_lun_lock);
184         }
185         spin_unlock(&tpg->tpg_lun_lock);
186 }
187
188 /*      core_set_queue_depth_for_node():
189  *
190  *
191  */
192 static int core_set_queue_depth_for_node(
193         struct se_portal_group *tpg,
194         struct se_node_acl *acl)
195 {
196         if (!acl->queue_depth) {
197                 pr_err("Queue depth for %s Initiator Node: %s is 0,"
198                         "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
199                         acl->initiatorname);
200                 acl->queue_depth = 1;
201         }
202
203         return 0;
204 }
205
206 /*      core_create_device_list_for_node():
207  *
208  *
209  */
210 static int core_create_device_list_for_node(struct se_node_acl *nacl)
211 {
212         struct se_dev_entry *deve;
213         int i;
214
215         nacl->device_list = kzalloc(sizeof(struct se_dev_entry) *
216                                 TRANSPORT_MAX_LUNS_PER_TPG, GFP_KERNEL);
217         if (!nacl->device_list) {
218                 pr_err("Unable to allocate memory for"
219                         " struct se_node_acl->device_list\n");
220                 return -ENOMEM;
221         }
222         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
223                 deve = &nacl->device_list[i];
224
225                 atomic_set(&deve->ua_count, 0);
226                 atomic_set(&deve->pr_ref_count, 0);
227                 spin_lock_init(&deve->ua_lock);
228                 INIT_LIST_HEAD(&deve->alua_port_list);
229                 INIT_LIST_HEAD(&deve->ua_list);
230         }
231
232         return 0;
233 }
234
235 /*      core_tpg_check_initiator_node_acl()
236  *
237  *
238  */
239 struct se_node_acl *core_tpg_check_initiator_node_acl(
240         struct se_portal_group *tpg,
241         unsigned char *initiatorname)
242 {
243         struct se_node_acl *acl;
244
245         acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
246         if (acl)
247                 return acl;
248
249         if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
250                 return NULL;
251
252         acl =  tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
253         if (!acl)
254                 return NULL;
255
256         INIT_LIST_HEAD(&acl->acl_list);
257         INIT_LIST_HEAD(&acl->acl_sess_list);
258         spin_lock_init(&acl->device_list_lock);
259         spin_lock_init(&acl->nacl_sess_lock);
260         atomic_set(&acl->acl_pr_ref_count, 0);
261         acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
262         snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
263         acl->se_tpg = tpg;
264         acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
265         spin_lock_init(&acl->stats_lock);
266         acl->dynamic_node_acl = 1;
267
268         tpg->se_tpg_tfo->set_default_node_attributes(acl);
269
270         if (core_create_device_list_for_node(acl) < 0) {
271                 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
272                 return NULL;
273         }
274
275         if (core_set_queue_depth_for_node(tpg, acl) < 0) {
276                 core_free_device_list_for_node(acl, tpg);
277                 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
278                 return NULL;
279         }
280         /*
281          * Here we only create demo-mode MappedLUNs from the active
282          * TPG LUNs if the fabric is not explictly asking for
283          * tpg_check_demo_mode_login_only() == 1.
284          */
285         if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only != NULL) &&
286             (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) == 1))
287                 do { ; } while (0);
288         else
289                 core_tpg_add_node_to_devs(acl, tpg);
290
291         spin_lock_irq(&tpg->acl_node_lock);
292         list_add_tail(&acl->acl_list, &tpg->acl_node_list);
293         tpg->num_node_acls++;
294         spin_unlock_irq(&tpg->acl_node_lock);
295
296         pr_debug("%s_TPG[%u] - Added DYNAMIC ACL with TCQ Depth: %d for %s"
297                 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
298                 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
299                 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
300
301         return acl;
302 }
303 EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
304
305 void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
306 {
307         while (atomic_read(&nacl->acl_pr_ref_count) != 0)
308                 cpu_relax();
309 }
310
311 void core_tpg_clear_object_luns(struct se_portal_group *tpg)
312 {
313         int i, ret;
314         struct se_lun *lun;
315
316         spin_lock(&tpg->tpg_lun_lock);
317         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
318                 lun = &tpg->tpg_lun_list[i];
319
320                 if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) ||
321                     (lun->lun_se_dev == NULL))
322                         continue;
323
324                 spin_unlock(&tpg->tpg_lun_lock);
325                 ret = core_dev_del_lun(tpg, lun->unpacked_lun);
326                 spin_lock(&tpg->tpg_lun_lock);
327         }
328         spin_unlock(&tpg->tpg_lun_lock);
329 }
330 EXPORT_SYMBOL(core_tpg_clear_object_luns);
331
332 /*      core_tpg_add_initiator_node_acl():
333  *
334  *
335  */
336 struct se_node_acl *core_tpg_add_initiator_node_acl(
337         struct se_portal_group *tpg,
338         struct se_node_acl *se_nacl,
339         const char *initiatorname,
340         u32 queue_depth)
341 {
342         struct se_node_acl *acl = NULL;
343
344         spin_lock_irq(&tpg->acl_node_lock);
345         acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
346         if (acl) {
347                 if (acl->dynamic_node_acl) {
348                         acl->dynamic_node_acl = 0;
349                         pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
350                                 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
351                                 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
352                         spin_unlock_irq(&tpg->acl_node_lock);
353                         /*
354                          * Release the locally allocated struct se_node_acl
355                          * because * core_tpg_add_initiator_node_acl() returned
356                          * a pointer to an existing demo mode node ACL.
357                          */
358                         if (se_nacl)
359                                 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg,
360                                                         se_nacl);
361                         goto done;
362                 }
363
364                 pr_err("ACL entry for %s Initiator"
365                         " Node %s already exists for TPG %u, ignoring"
366                         " request.\n",  tpg->se_tpg_tfo->get_fabric_name(),
367                         initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
368                 spin_unlock_irq(&tpg->acl_node_lock);
369                 return ERR_PTR(-EEXIST);
370         }
371         spin_unlock_irq(&tpg->acl_node_lock);
372
373         if (!se_nacl) {
374                 pr_err("struct se_node_acl pointer is NULL\n");
375                 return ERR_PTR(-EINVAL);
376         }
377         /*
378          * For v4.x logic the se_node_acl_s is hanging off a fabric
379          * dependent structure allocated via
380          * struct target_core_fabric_ops->fabric_make_nodeacl()
381          */
382         acl = se_nacl;
383
384         INIT_LIST_HEAD(&acl->acl_list);
385         INIT_LIST_HEAD(&acl->acl_sess_list);
386         spin_lock_init(&acl->device_list_lock);
387         spin_lock_init(&acl->nacl_sess_lock);
388         atomic_set(&acl->acl_pr_ref_count, 0);
389         acl->queue_depth = queue_depth;
390         snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
391         acl->se_tpg = tpg;
392         acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
393         spin_lock_init(&acl->stats_lock);
394
395         tpg->se_tpg_tfo->set_default_node_attributes(acl);
396
397         if (core_create_device_list_for_node(acl) < 0) {
398                 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
399                 return ERR_PTR(-ENOMEM);
400         }
401
402         if (core_set_queue_depth_for_node(tpg, acl) < 0) {
403                 core_free_device_list_for_node(acl, tpg);
404                 tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
405                 return ERR_PTR(-EINVAL);
406         }
407
408         spin_lock_irq(&tpg->acl_node_lock);
409         list_add_tail(&acl->acl_list, &tpg->acl_node_list);
410         tpg->num_node_acls++;
411         spin_unlock_irq(&tpg->acl_node_lock);
412
413 done:
414         pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
415                 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
416                 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
417                 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
418
419         return acl;
420 }
421 EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
422
423 /*      core_tpg_del_initiator_node_acl():
424  *
425  *
426  */
427 int core_tpg_del_initiator_node_acl(
428         struct se_portal_group *tpg,
429         struct se_node_acl *acl,
430         int force)
431 {
432         struct se_session *sess, *sess_tmp;
433         int dynamic_acl = 0;
434
435         spin_lock_irq(&tpg->acl_node_lock);
436         if (acl->dynamic_node_acl) {
437                 acl->dynamic_node_acl = 0;
438                 dynamic_acl = 1;
439         }
440         list_del(&acl->acl_list);
441         tpg->num_node_acls--;
442         spin_unlock_irq(&tpg->acl_node_lock);
443
444         spin_lock_bh(&tpg->session_lock);
445         list_for_each_entry_safe(sess, sess_tmp,
446                                 &tpg->tpg_sess_list, sess_list) {
447                 if (sess->se_node_acl != acl)
448                         continue;
449                 /*
450                  * Determine if the session needs to be closed by our context.
451                  */
452                 if (!tpg->se_tpg_tfo->shutdown_session(sess))
453                         continue;
454
455                 spin_unlock_bh(&tpg->session_lock);
456                 /*
457                  * If the $FABRIC_MOD session for the Initiator Node ACL exists,
458                  * forcefully shutdown the $FABRIC_MOD session/nexus.
459                  */
460                 tpg->se_tpg_tfo->close_session(sess);
461
462                 spin_lock_bh(&tpg->session_lock);
463         }
464         spin_unlock_bh(&tpg->session_lock);
465
466         core_tpg_wait_for_nacl_pr_ref(acl);
467         core_clear_initiator_node_from_tpg(acl, tpg);
468         core_free_device_list_for_node(acl, tpg);
469
470         pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
471                 " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
472                 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
473                 tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
474
475         return 0;
476 }
477 EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
478
479 /*      core_tpg_set_initiator_node_queue_depth():
480  *
481  *
482  */
483 int core_tpg_set_initiator_node_queue_depth(
484         struct se_portal_group *tpg,
485         unsigned char *initiatorname,
486         u32 queue_depth,
487         int force)
488 {
489         struct se_session *sess, *init_sess = NULL;
490         struct se_node_acl *acl;
491         int dynamic_acl = 0;
492
493         spin_lock_irq(&tpg->acl_node_lock);
494         acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
495         if (!acl) {
496                 pr_err("Access Control List entry for %s Initiator"
497                         " Node %s does not exists for TPG %hu, ignoring"
498                         " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
499                         initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
500                 spin_unlock_irq(&tpg->acl_node_lock);
501                 return -ENODEV;
502         }
503         if (acl->dynamic_node_acl) {
504                 acl->dynamic_node_acl = 0;
505                 dynamic_acl = 1;
506         }
507         spin_unlock_irq(&tpg->acl_node_lock);
508
509         spin_lock_bh(&tpg->session_lock);
510         list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
511                 if (sess->se_node_acl != acl)
512                         continue;
513
514                 if (!force) {
515                         pr_err("Unable to change queue depth for %s"
516                                 " Initiator Node: %s while session is"
517                                 " operational.  To forcefully change the queue"
518                                 " depth and force session reinstatement"
519                                 " use the \"force=1\" parameter.\n",
520                                 tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
521                         spin_unlock_bh(&tpg->session_lock);
522
523                         spin_lock_irq(&tpg->acl_node_lock);
524                         if (dynamic_acl)
525                                 acl->dynamic_node_acl = 1;
526                         spin_unlock_irq(&tpg->acl_node_lock);
527                         return -EEXIST;
528                 }
529                 /*
530                  * Determine if the session needs to be closed by our context.
531                  */
532                 if (!tpg->se_tpg_tfo->shutdown_session(sess))
533                         continue;
534
535                 init_sess = sess;
536                 break;
537         }
538
539         /*
540          * User has requested to change the queue depth for a Initiator Node.
541          * Change the value in the Node's struct se_node_acl, and call
542          * core_set_queue_depth_for_node() to add the requested queue depth.
543          *
544          * Finally call  tpg->se_tpg_tfo->close_session() to force session
545          * reinstatement to occur if there is an active session for the
546          * $FABRIC_MOD Initiator Node in question.
547          */
548         acl->queue_depth = queue_depth;
549
550         if (core_set_queue_depth_for_node(tpg, acl) < 0) {
551                 spin_unlock_bh(&tpg->session_lock);
552                 /*
553                  * Force session reinstatement if
554                  * core_set_queue_depth_for_node() failed, because we assume
555                  * the $FABRIC_MOD has already the set session reinstatement
556                  * bit from tpg->se_tpg_tfo->shutdown_session() called above.
557                  */
558                 if (init_sess)
559                         tpg->se_tpg_tfo->close_session(init_sess);
560
561                 spin_lock_irq(&tpg->acl_node_lock);
562                 if (dynamic_acl)
563                         acl->dynamic_node_acl = 1;
564                 spin_unlock_irq(&tpg->acl_node_lock);
565                 return -EINVAL;
566         }
567         spin_unlock_bh(&tpg->session_lock);
568         /*
569          * If the $FABRIC_MOD session for the Initiator Node ACL exists,
570          * forcefully shutdown the $FABRIC_MOD session/nexus.
571          */
572         if (init_sess)
573                 tpg->se_tpg_tfo->close_session(init_sess);
574
575         pr_debug("Successfully changed queue depth to: %d for Initiator"
576                 " Node: %s on %s Target Portal Group: %u\n", queue_depth,
577                 initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
578                 tpg->se_tpg_tfo->tpg_get_tag(tpg));
579
580         spin_lock_irq(&tpg->acl_node_lock);
581         if (dynamic_acl)
582                 acl->dynamic_node_acl = 1;
583         spin_unlock_irq(&tpg->acl_node_lock);
584
585         return 0;
586 }
587 EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
588
589 static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
590 {
591         /* Set in core_dev_setup_virtual_lun0() */
592         struct se_device *dev = g_lun0_dev;
593         struct se_lun *lun = &se_tpg->tpg_virt_lun0;
594         u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
595         int ret;
596
597         lun->unpacked_lun = 0;
598         lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
599         atomic_set(&lun->lun_acl_count, 0);
600         init_completion(&lun->lun_shutdown_comp);
601         INIT_LIST_HEAD(&lun->lun_acl_list);
602         INIT_LIST_HEAD(&lun->lun_cmd_list);
603         spin_lock_init(&lun->lun_acl_lock);
604         spin_lock_init(&lun->lun_cmd_lock);
605         spin_lock_init(&lun->lun_sep_lock);
606
607         ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
608         if (ret < 0)
609                 return ret;
610
611         return 0;
612 }
613
614 static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
615 {
616         struct se_lun *lun = &se_tpg->tpg_virt_lun0;
617
618         core_tpg_post_dellun(se_tpg, lun);
619 }
620
621 int core_tpg_register(
622         struct target_core_fabric_ops *tfo,
623         struct se_wwn *se_wwn,
624         struct se_portal_group *se_tpg,
625         void *tpg_fabric_ptr,
626         int se_tpg_type)
627 {
628         struct se_lun *lun;
629         u32 i;
630
631         se_tpg->tpg_lun_list = kzalloc((sizeof(struct se_lun) *
632                                 TRANSPORT_MAX_LUNS_PER_TPG), GFP_KERNEL);
633         if (!se_tpg->tpg_lun_list) {
634                 pr_err("Unable to allocate struct se_portal_group->"
635                                 "tpg_lun_list\n");
636                 return -ENOMEM;
637         }
638
639         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
640                 lun = &se_tpg->tpg_lun_list[i];
641                 lun->unpacked_lun = i;
642                 lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
643                 atomic_set(&lun->lun_acl_count, 0);
644                 init_completion(&lun->lun_shutdown_comp);
645                 INIT_LIST_HEAD(&lun->lun_acl_list);
646                 INIT_LIST_HEAD(&lun->lun_cmd_list);
647                 spin_lock_init(&lun->lun_acl_lock);
648                 spin_lock_init(&lun->lun_cmd_lock);
649                 spin_lock_init(&lun->lun_sep_lock);
650         }
651
652         se_tpg->se_tpg_type = se_tpg_type;
653         se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
654         se_tpg->se_tpg_tfo = tfo;
655         se_tpg->se_tpg_wwn = se_wwn;
656         atomic_set(&se_tpg->tpg_pr_ref_count, 0);
657         INIT_LIST_HEAD(&se_tpg->acl_node_list);
658         INIT_LIST_HEAD(&se_tpg->se_tpg_node);
659         INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
660         spin_lock_init(&se_tpg->acl_node_lock);
661         spin_lock_init(&se_tpg->session_lock);
662         spin_lock_init(&se_tpg->tpg_lun_lock);
663
664         if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
665                 if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
666                         kfree(se_tpg);
667                         return -ENOMEM;
668                 }
669         }
670
671         spin_lock_bh(&tpg_lock);
672         list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
673         spin_unlock_bh(&tpg_lock);
674
675         pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
676                 " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
677                 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
678                 "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
679                 "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
680
681         return 0;
682 }
683 EXPORT_SYMBOL(core_tpg_register);
684
685 int core_tpg_deregister(struct se_portal_group *se_tpg)
686 {
687         struct se_node_acl *nacl, *nacl_tmp;
688
689         pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
690                 " for endpoint: %s Portal Tag %u\n",
691                 (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
692                 "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
693                 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
694                 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
695
696         spin_lock_bh(&tpg_lock);
697         list_del(&se_tpg->se_tpg_node);
698         spin_unlock_bh(&tpg_lock);
699
700         while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
701                 cpu_relax();
702         /*
703          * Release any remaining demo-mode generated se_node_acl that have
704          * not been released because of TFO->tpg_check_demo_mode_cache() == 1
705          * in transport_deregister_session().
706          */
707         spin_lock_irq(&se_tpg->acl_node_lock);
708         list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
709                         acl_list) {
710                 list_del(&nacl->acl_list);
711                 se_tpg->num_node_acls--;
712                 spin_unlock_irq(&se_tpg->acl_node_lock);
713
714                 core_tpg_wait_for_nacl_pr_ref(nacl);
715                 core_free_device_list_for_node(nacl, se_tpg);
716                 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
717
718                 spin_lock_irq(&se_tpg->acl_node_lock);
719         }
720         spin_unlock_irq(&se_tpg->acl_node_lock);
721
722         if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
723                 core_tpg_release_virtual_lun0(se_tpg);
724
725         se_tpg->se_tpg_fabric_ptr = NULL;
726         kfree(se_tpg->tpg_lun_list);
727         return 0;
728 }
729 EXPORT_SYMBOL(core_tpg_deregister);
730
731 struct se_lun *core_tpg_pre_addlun(
732         struct se_portal_group *tpg,
733         u32 unpacked_lun)
734 {
735         struct se_lun *lun;
736
737         if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
738                 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
739                         "-1: %u for Target Portal Group: %u\n",
740                         tpg->se_tpg_tfo->get_fabric_name(),
741                         unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
742                         tpg->se_tpg_tfo->tpg_get_tag(tpg));
743                 return ERR_PTR(-EOVERFLOW);
744         }
745
746         spin_lock(&tpg->tpg_lun_lock);
747         lun = &tpg->tpg_lun_list[unpacked_lun];
748         if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
749                 pr_err("TPG Logical Unit Number: %u is already active"
750                         " on %s Target Portal Group: %u, ignoring request.\n",
751                         unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
752                         tpg->se_tpg_tfo->tpg_get_tag(tpg));
753                 spin_unlock(&tpg->tpg_lun_lock);
754                 return ERR_PTR(-EINVAL);
755         }
756         spin_unlock(&tpg->tpg_lun_lock);
757
758         return lun;
759 }
760
761 int core_tpg_post_addlun(
762         struct se_portal_group *tpg,
763         struct se_lun *lun,
764         u32 lun_access,
765         void *lun_ptr)
766 {
767         int ret;
768
769         ret = core_dev_export(lun_ptr, tpg, lun);
770         if (ret < 0)
771                 return ret;
772
773         spin_lock(&tpg->tpg_lun_lock);
774         lun->lun_access = lun_access;
775         lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
776         spin_unlock(&tpg->tpg_lun_lock);
777
778         return 0;
779 }
780
781 static void core_tpg_shutdown_lun(
782         struct se_portal_group *tpg,
783         struct se_lun *lun)
784 {
785         core_clear_lun_from_tpg(lun, tpg);
786         transport_clear_lun_from_sessions(lun);
787 }
788
789 struct se_lun *core_tpg_pre_dellun(
790         struct se_portal_group *tpg,
791         u32 unpacked_lun,
792         int *ret)
793 {
794         struct se_lun *lun;
795
796         if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
797                 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
798                         "-1: %u for Target Portal Group: %u\n",
799                         tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
800                         TRANSPORT_MAX_LUNS_PER_TPG-1,
801                         tpg->se_tpg_tfo->tpg_get_tag(tpg));
802                 return ERR_PTR(-EOVERFLOW);
803         }
804
805         spin_lock(&tpg->tpg_lun_lock);
806         lun = &tpg->tpg_lun_list[unpacked_lun];
807         if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
808                 pr_err("%s Logical Unit Number: %u is not active on"
809                         " Target Portal Group: %u, ignoring request.\n",
810                         tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
811                         tpg->se_tpg_tfo->tpg_get_tag(tpg));
812                 spin_unlock(&tpg->tpg_lun_lock);
813                 return ERR_PTR(-ENODEV);
814         }
815         spin_unlock(&tpg->tpg_lun_lock);
816
817         return lun;
818 }
819
820 int core_tpg_post_dellun(
821         struct se_portal_group *tpg,
822         struct se_lun *lun)
823 {
824         core_tpg_shutdown_lun(tpg, lun);
825
826         core_dev_unexport(lun->lun_se_dev, tpg, lun);
827
828         spin_lock(&tpg->tpg_lun_lock);
829         lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
830         spin_unlock(&tpg->tpg_lun_lock);
831
832         return 0;
833 }