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