1 /*******************************************************************************
2 * Filename: target_core_tpg.c
4 * This file contains generic Target Portal Group related functions.
6 * (c) Copyright 2002-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/net.h>
27 #include <linux/string.h>
28 #include <linux/timer.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
32 #include <linux/export.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_cmnd.h>
38 #include <target/target_core_base.h>
39 #include <target/target_core_backend.h>
40 #include <target/target_core_fabric.h>
42 #include "target_core_internal.h"
43 #include "target_core_pr.h"
45 extern struct se_device
*g_lun0_dev
;
47 static DEFINE_SPINLOCK(tpg_lock
);
48 static LIST_HEAD(tpg_list
);
50 /* __core_tpg_get_initiator_node_acl():
52 * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
54 struct se_node_acl
*__core_tpg_get_initiator_node_acl(
55 struct se_portal_group
*tpg
,
56 const char *initiatorname
)
58 struct se_node_acl
*acl
;
60 list_for_each_entry(acl
, &tpg
->acl_node_list
, acl_list
) {
61 if (!strcmp(acl
->initiatorname
, initiatorname
))
68 /* core_tpg_get_initiator_node_acl():
72 struct se_node_acl
*core_tpg_get_initiator_node_acl(
73 struct se_portal_group
*tpg
,
74 unsigned char *initiatorname
)
76 struct se_node_acl
*acl
;
78 spin_lock_irq(&tpg
->acl_node_lock
);
79 acl
= __core_tpg_get_initiator_node_acl(tpg
, initiatorname
);
80 spin_unlock_irq(&tpg
->acl_node_lock
);
84 EXPORT_SYMBOL(core_tpg_get_initiator_node_acl
);
86 /* core_tpg_add_node_to_devs():
90 void core_tpg_add_node_to_devs(
91 struct se_node_acl
*acl
,
92 struct se_portal_group
*tpg
)
96 struct se_device
*dev
;
98 mutex_lock(&tpg
->tpg_lun_mutex
);
99 hlist_for_each_entry_rcu(lun
, &tpg
->tpg_lun_hlist
, link
) {
100 if (lun
->lun_status
!= TRANSPORT_LUN_STATUS_ACTIVE
)
103 dev
= lun
->lun_se_dev
;
105 * By default in LIO-Target $FABRIC_MOD,
106 * demo_mode_write_protect is ON, or READ_ONLY;
108 if (!tpg
->se_tpg_tfo
->tpg_check_demo_mode_write_protect(tpg
)) {
109 lun_access
= TRANSPORT_LUNFLAGS_READ_WRITE
;
112 * Allow only optical drives to issue R/W in default RO
115 if (dev
->transport
->get_device_type(dev
) == TYPE_DISK
)
116 lun_access
= TRANSPORT_LUNFLAGS_READ_ONLY
;
118 lun_access
= TRANSPORT_LUNFLAGS_READ_WRITE
;
121 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
122 " access for LUN in Demo Mode\n",
123 tpg
->se_tpg_tfo
->get_fabric_name(),
124 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
), lun
->unpacked_lun
,
125 (lun_access
== TRANSPORT_LUNFLAGS_READ_WRITE
) ?
126 "READ-WRITE" : "READ-ONLY");
128 core_enable_device_list_for_node(lun
, NULL
, lun
->unpacked_lun
,
129 lun_access
, acl
, tpg
);
131 * Check to see if there are any existing persistent reservation
132 * APTPL pre-registrations that need to be enabled for this dynamic
135 core_scsi3_check_aptpl_registration(dev
, tpg
, lun
, acl
,
138 mutex_unlock(&tpg
->tpg_lun_mutex
);
141 /* core_set_queue_depth_for_node():
145 static int core_set_queue_depth_for_node(
146 struct se_portal_group
*tpg
,
147 struct se_node_acl
*acl
)
149 if (!acl
->queue_depth
) {
150 pr_err("Queue depth for %s Initiator Node: %s is 0,"
151 "defaulting to 1.\n", tpg
->se_tpg_tfo
->get_fabric_name(),
153 acl
->queue_depth
= 1;
159 static struct se_node_acl
*target_alloc_node_acl(struct se_portal_group
*tpg
,
160 const unsigned char *initiatorname
)
162 struct se_node_acl
*acl
;
164 acl
= kzalloc(max(sizeof(*acl
), tpg
->se_tpg_tfo
->node_acl_size
),
169 INIT_LIST_HEAD(&acl
->acl_list
);
170 INIT_LIST_HEAD(&acl
->acl_sess_list
);
171 INIT_HLIST_HEAD(&acl
->lun_entry_hlist
);
172 kref_init(&acl
->acl_kref
);
173 init_completion(&acl
->acl_free_comp
);
174 spin_lock_init(&acl
->nacl_sess_lock
);
175 mutex_init(&acl
->lun_entry_mutex
);
176 atomic_set(&acl
->acl_pr_ref_count
, 0);
177 if (tpg
->se_tpg_tfo
->tpg_get_default_depth
)
178 acl
->queue_depth
= tpg
->se_tpg_tfo
->tpg_get_default_depth(tpg
);
180 acl
->queue_depth
= 1;
181 snprintf(acl
->initiatorname
, TRANSPORT_IQN_LEN
, "%s", initiatorname
);
183 acl
->acl_index
= scsi_get_new_index(SCSI_AUTH_INTR_INDEX
);
185 tpg
->se_tpg_tfo
->set_default_node_attributes(acl
);
187 if (core_set_queue_depth_for_node(tpg
, acl
) < 0)
197 static void target_add_node_acl(struct se_node_acl
*acl
)
199 struct se_portal_group
*tpg
= acl
->se_tpg
;
201 spin_lock_irq(&tpg
->acl_node_lock
);
202 list_add_tail(&acl
->acl_list
, &tpg
->acl_node_list
);
203 tpg
->num_node_acls
++;
204 spin_unlock_irq(&tpg
->acl_node_lock
);
206 pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
207 " Initiator Node: %s\n",
208 tpg
->se_tpg_tfo
->get_fabric_name(),
209 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
210 acl
->dynamic_node_acl
? "DYNAMIC" : "",
212 tpg
->se_tpg_tfo
->get_fabric_name(),
216 struct se_node_acl
*core_tpg_check_initiator_node_acl(
217 struct se_portal_group
*tpg
,
218 unsigned char *initiatorname
)
220 struct se_node_acl
*acl
;
222 acl
= core_tpg_get_initiator_node_acl(tpg
, initiatorname
);
226 if (!tpg
->se_tpg_tfo
->tpg_check_demo_mode(tpg
))
229 acl
= target_alloc_node_acl(tpg
, initiatorname
);
232 acl
->dynamic_node_acl
= 1;
235 * Here we only create demo-mode MappedLUNs from the active
236 * TPG LUNs if the fabric is not explicitly asking for
237 * tpg_check_demo_mode_login_only() == 1.
239 if ((tpg
->se_tpg_tfo
->tpg_check_demo_mode_login_only
== NULL
) ||
240 (tpg
->se_tpg_tfo
->tpg_check_demo_mode_login_only(tpg
) != 1))
241 core_tpg_add_node_to_devs(acl
, tpg
);
243 target_add_node_acl(acl
);
246 EXPORT_SYMBOL(core_tpg_check_initiator_node_acl
);
248 void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl
*nacl
)
250 while (atomic_read(&nacl
->acl_pr_ref_count
) != 0)
254 struct se_node_acl
*core_tpg_add_initiator_node_acl(
255 struct se_portal_group
*tpg
,
256 const char *initiatorname
)
258 struct se_node_acl
*acl
;
260 spin_lock_irq(&tpg
->acl_node_lock
);
261 acl
= __core_tpg_get_initiator_node_acl(tpg
, initiatorname
);
263 if (acl
->dynamic_node_acl
) {
264 acl
->dynamic_node_acl
= 0;
265 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
266 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
267 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
), initiatorname
);
268 spin_unlock_irq(&tpg
->acl_node_lock
);
272 pr_err("ACL entry for %s Initiator"
273 " Node %s already exists for TPG %u, ignoring"
274 " request.\n", tpg
->se_tpg_tfo
->get_fabric_name(),
275 initiatorname
, tpg
->se_tpg_tfo
->tpg_get_tag(tpg
));
276 spin_unlock_irq(&tpg
->acl_node_lock
);
277 return ERR_PTR(-EEXIST
);
279 spin_unlock_irq(&tpg
->acl_node_lock
);
281 acl
= target_alloc_node_acl(tpg
, initiatorname
);
283 return ERR_PTR(-ENOMEM
);
285 target_add_node_acl(acl
);
289 void core_tpg_del_initiator_node_acl(struct se_node_acl
*acl
)
291 struct se_portal_group
*tpg
= acl
->se_tpg
;
292 LIST_HEAD(sess_list
);
293 struct se_session
*sess
, *sess_tmp
;
297 spin_lock_irq(&tpg
->acl_node_lock
);
298 if (acl
->dynamic_node_acl
) {
299 acl
->dynamic_node_acl
= 0;
301 list_del(&acl
->acl_list
);
302 tpg
->num_node_acls
--;
303 spin_unlock_irq(&tpg
->acl_node_lock
);
305 spin_lock_irqsave(&acl
->nacl_sess_lock
, flags
);
308 list_for_each_entry_safe(sess
, sess_tmp
, &acl
->acl_sess_list
,
310 if (sess
->sess_tearing_down
!= 0)
313 target_get_session(sess
);
314 list_move(&sess
->sess_acl_list
, &sess_list
);
316 spin_unlock_irqrestore(&acl
->nacl_sess_lock
, flags
);
318 list_for_each_entry_safe(sess
, sess_tmp
, &sess_list
, sess_acl_list
) {
319 list_del(&sess
->sess_acl_list
);
321 rc
= tpg
->se_tpg_tfo
->shutdown_session(sess
);
322 target_put_session(sess
);
325 target_put_session(sess
);
327 target_put_nacl(acl
);
329 * Wait for last target_put_nacl() to complete in target_complete_nacl()
330 * for active fabric session transport_deregister_session() callbacks.
332 wait_for_completion(&acl
->acl_free_comp
);
334 core_tpg_wait_for_nacl_pr_ref(acl
);
335 core_free_device_list_for_node(acl
, tpg
);
337 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
338 " Initiator Node: %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
339 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
), acl
->queue_depth
,
340 tpg
->se_tpg_tfo
->get_fabric_name(), acl
->initiatorname
);
345 /* core_tpg_set_initiator_node_queue_depth():
349 int core_tpg_set_initiator_node_queue_depth(
350 struct se_portal_group
*tpg
,
351 unsigned char *initiatorname
,
355 struct se_session
*sess
, *init_sess
= NULL
;
356 struct se_node_acl
*acl
;
360 spin_lock_irq(&tpg
->acl_node_lock
);
361 acl
= __core_tpg_get_initiator_node_acl(tpg
, initiatorname
);
363 pr_err("Access Control List entry for %s Initiator"
364 " Node %s does not exists for TPG %hu, ignoring"
365 " request.\n", tpg
->se_tpg_tfo
->get_fabric_name(),
366 initiatorname
, tpg
->se_tpg_tfo
->tpg_get_tag(tpg
));
367 spin_unlock_irq(&tpg
->acl_node_lock
);
370 if (acl
->dynamic_node_acl
) {
371 acl
->dynamic_node_acl
= 0;
374 spin_unlock_irq(&tpg
->acl_node_lock
);
376 spin_lock_irqsave(&tpg
->session_lock
, flags
);
377 list_for_each_entry(sess
, &tpg
->tpg_sess_list
, sess_list
) {
378 if (sess
->se_node_acl
!= acl
)
382 pr_err("Unable to change queue depth for %s"
383 " Initiator Node: %s while session is"
384 " operational. To forcefully change the queue"
385 " depth and force session reinstatement"
386 " use the \"force=1\" parameter.\n",
387 tpg
->se_tpg_tfo
->get_fabric_name(), initiatorname
);
388 spin_unlock_irqrestore(&tpg
->session_lock
, flags
);
390 spin_lock_irq(&tpg
->acl_node_lock
);
392 acl
->dynamic_node_acl
= 1;
393 spin_unlock_irq(&tpg
->acl_node_lock
);
397 * Determine if the session needs to be closed by our context.
399 if (!tpg
->se_tpg_tfo
->shutdown_session(sess
))
407 * User has requested to change the queue depth for a Initiator Node.
408 * Change the value in the Node's struct se_node_acl, and call
409 * core_set_queue_depth_for_node() to add the requested queue depth.
411 * Finally call tpg->se_tpg_tfo->close_session() to force session
412 * reinstatement to occur if there is an active session for the
413 * $FABRIC_MOD Initiator Node in question.
415 acl
->queue_depth
= queue_depth
;
417 if (core_set_queue_depth_for_node(tpg
, acl
) < 0) {
418 spin_unlock_irqrestore(&tpg
->session_lock
, flags
);
420 * Force session reinstatement if
421 * core_set_queue_depth_for_node() failed, because we assume
422 * the $FABRIC_MOD has already the set session reinstatement
423 * bit from tpg->se_tpg_tfo->shutdown_session() called above.
426 tpg
->se_tpg_tfo
->close_session(init_sess
);
428 spin_lock_irq(&tpg
->acl_node_lock
);
430 acl
->dynamic_node_acl
= 1;
431 spin_unlock_irq(&tpg
->acl_node_lock
);
434 spin_unlock_irqrestore(&tpg
->session_lock
, flags
);
436 * If the $FABRIC_MOD session for the Initiator Node ACL exists,
437 * forcefully shutdown the $FABRIC_MOD session/nexus.
440 tpg
->se_tpg_tfo
->close_session(init_sess
);
442 pr_debug("Successfully changed queue depth to: %d for Initiator"
443 " Node: %s on %s Target Portal Group: %u\n", queue_depth
,
444 initiatorname
, tpg
->se_tpg_tfo
->get_fabric_name(),
445 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
));
447 spin_lock_irq(&tpg
->acl_node_lock
);
449 acl
->dynamic_node_acl
= 1;
450 spin_unlock_irq(&tpg
->acl_node_lock
);
454 EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth
);
456 /* core_tpg_set_initiator_node_tag():
458 * Initiator nodeacl tags are not used internally, but may be used by
459 * userspace to emulate aliases or groups.
460 * Returns length of newly-set tag or -EINVAL.
462 int core_tpg_set_initiator_node_tag(
463 struct se_portal_group
*tpg
,
464 struct se_node_acl
*acl
,
467 if (strlen(new_tag
) >= MAX_ACL_TAG_SIZE
)
470 if (!strncmp("NULL", new_tag
, 4)) {
471 acl
->acl_tag
[0] = '\0';
475 return snprintf(acl
->acl_tag
, MAX_ACL_TAG_SIZE
, "%s", new_tag
);
477 EXPORT_SYMBOL(core_tpg_set_initiator_node_tag
);
479 static void core_tpg_lun_ref_release(struct percpu_ref
*ref
)
481 struct se_lun
*lun
= container_of(ref
, struct se_lun
, lun_ref
);
483 complete(&lun
->lun_ref_comp
);
486 static int core_tpg_setup_virtual_lun0(struct se_portal_group
*se_tpg
)
488 /* Set in core_dev_setup_virtual_lun0() */
489 struct se_device
*dev
= g_lun0_dev
;
490 struct se_lun
*lun
= &se_tpg
->tpg_virt_lun0
;
491 u32 lun_access
= TRANSPORT_LUNFLAGS_READ_ONLY
;
494 lun
->unpacked_lun
= 0;
495 lun
->lun_status
= TRANSPORT_LUN_STATUS_FREE
;
496 atomic_set(&lun
->lun_acl_count
, 0);
497 init_completion(&lun
->lun_shutdown_comp
);
498 INIT_LIST_HEAD(&lun
->lun_acl_list
);
499 spin_lock_init(&lun
->lun_acl_lock
);
500 spin_lock_init(&lun
->lun_sep_lock
);
501 init_completion(&lun
->lun_ref_comp
);
503 ret
= core_tpg_add_lun(se_tpg
, lun
, lun_access
, dev
);
510 int core_tpg_register(
511 const struct target_core_fabric_ops
*tfo
,
512 struct se_wwn
*se_wwn
,
513 struct se_portal_group
*se_tpg
,
516 INIT_HLIST_HEAD(&se_tpg
->tpg_lun_hlist
);
517 se_tpg
->proto_id
= proto_id
;
518 se_tpg
->se_tpg_tfo
= tfo
;
519 se_tpg
->se_tpg_wwn
= se_wwn
;
520 atomic_set(&se_tpg
->tpg_pr_ref_count
, 0);
521 INIT_LIST_HEAD(&se_tpg
->acl_node_list
);
522 INIT_LIST_HEAD(&se_tpg
->se_tpg_node
);
523 INIT_LIST_HEAD(&se_tpg
->tpg_sess_list
);
524 spin_lock_init(&se_tpg
->acl_node_lock
);
525 spin_lock_init(&se_tpg
->session_lock
);
526 mutex_init(&se_tpg
->tpg_lun_mutex
);
528 if (se_tpg
->proto_id
>= 0) {
529 if (core_tpg_setup_virtual_lun0(se_tpg
) < 0)
533 spin_lock_bh(&tpg_lock
);
534 list_add_tail(&se_tpg
->se_tpg_node
, &tpg_list
);
535 spin_unlock_bh(&tpg_lock
);
537 pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
538 "Proto: %d, Portal Tag: %u\n", tfo
->get_fabric_name(),
539 tfo
->tpg_get_wwn(se_tpg
) ? tfo
->tpg_get_wwn(se_tpg
) : NULL
,
540 se_tpg
->proto_id
, tfo
->tpg_get_tag(se_tpg
));
544 EXPORT_SYMBOL(core_tpg_register
);
546 int core_tpg_deregister(struct se_portal_group
*se_tpg
)
548 const struct target_core_fabric_ops
*tfo
= se_tpg
->se_tpg_tfo
;
549 struct se_node_acl
*nacl
, *nacl_tmp
;
551 pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
552 "Proto: %d, Portal Tag: %u\n", tfo
->get_fabric_name(),
553 tfo
->tpg_get_wwn(se_tpg
) ? tfo
->tpg_get_wwn(se_tpg
) : NULL
,
554 se_tpg
->proto_id
, tfo
->tpg_get_tag(se_tpg
));
556 spin_lock_bh(&tpg_lock
);
557 list_del(&se_tpg
->se_tpg_node
);
558 spin_unlock_bh(&tpg_lock
);
560 while (atomic_read(&se_tpg
->tpg_pr_ref_count
) != 0)
563 * Release any remaining demo-mode generated se_node_acl that have
564 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
565 * in transport_deregister_session().
567 spin_lock_irq(&se_tpg
->acl_node_lock
);
568 list_for_each_entry_safe(nacl
, nacl_tmp
, &se_tpg
->acl_node_list
,
570 list_del(&nacl
->acl_list
);
571 se_tpg
->num_node_acls
--;
572 spin_unlock_irq(&se_tpg
->acl_node_lock
);
574 core_tpg_wait_for_nacl_pr_ref(nacl
);
575 core_free_device_list_for_node(nacl
, se_tpg
);
578 spin_lock_irq(&se_tpg
->acl_node_lock
);
580 spin_unlock_irq(&se_tpg
->acl_node_lock
);
582 if (se_tpg
->proto_id
>= 0)
583 core_tpg_remove_lun(se_tpg
, &se_tpg
->tpg_virt_lun0
);
587 EXPORT_SYMBOL(core_tpg_deregister
);
589 struct se_lun
*core_tpg_alloc_lun(
590 struct se_portal_group
*tpg
,
595 if (unpacked_lun
> (TRANSPORT_MAX_LUNS_PER_TPG
-1)) {
596 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
597 "-1: %u for Target Portal Group: %u\n",
598 tpg
->se_tpg_tfo
->get_fabric_name(),
599 unpacked_lun
, TRANSPORT_MAX_LUNS_PER_TPG
-1,
600 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
));
601 return ERR_PTR(-EOVERFLOW
);
604 lun
= kzalloc(sizeof(*lun
), GFP_KERNEL
);
606 pr_err("Unable to allocate se_lun memory\n");
607 return ERR_PTR(-ENOMEM
);
609 lun
->unpacked_lun
= unpacked_lun
;
610 lun
->lun_link_magic
= SE_LUN_LINK_MAGIC
;
611 lun
->lun_status
= TRANSPORT_LUN_STATUS_FREE
;
612 atomic_set(&lun
->lun_acl_count
, 0);
613 init_completion(&lun
->lun_shutdown_comp
);
614 INIT_LIST_HEAD(&lun
->lun_acl_list
);
615 spin_lock_init(&lun
->lun_acl_lock
);
616 spin_lock_init(&lun
->lun_sep_lock
);
617 init_completion(&lun
->lun_ref_comp
);
622 int core_tpg_add_lun(
623 struct se_portal_group
*tpg
,
626 struct se_device
*dev
)
630 ret
= percpu_ref_init(&lun
->lun_ref
, core_tpg_lun_ref_release
, 0,
635 ret
= core_dev_export(dev
, tpg
, lun
);
637 percpu_ref_exit(&lun
->lun_ref
);
641 mutex_lock(&tpg
->tpg_lun_mutex
);
642 lun
->lun_access
= lun_access
;
643 lun
->lun_status
= TRANSPORT_LUN_STATUS_ACTIVE
;
644 if (!(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
645 hlist_add_head_rcu(&lun
->link
, &tpg
->tpg_lun_hlist
);
646 mutex_unlock(&tpg
->tpg_lun_mutex
);
651 void core_tpg_remove_lun(
652 struct se_portal_group
*tpg
,
655 struct se_device
*dev
= lun
->lun_se_dev
;
657 core_clear_lun_from_tpg(lun
, tpg
);
658 transport_clear_lun_ref(lun
);
660 core_dev_unexport(lun
->lun_se_dev
, tpg
, lun
);
662 mutex_lock(&tpg
->tpg_lun_mutex
);
663 lun
->lun_status
= TRANSPORT_LUN_STATUS_FREE
;
664 if (!(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
665 hlist_del_rcu(&lun
->link
);
666 mutex_unlock(&tpg
->tpg_lun_mutex
);
668 percpu_ref_exit(&lun
->lun_ref
);