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