Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_tpg.c | |
3 | * | |
4 | * This file contains generic Target Portal Group related functions. | |
5 | * | |
4c76251e | 6 | * (c) Copyright 2002-2013 Datera, Inc. |
c66ac9db NB |
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> | |
c66ac9db | 31 | #include <linux/in.h> |
c53181af | 32 | #include <linux/export.h> |
c66ac9db NB |
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> | |
c4795fb2 CH |
39 | #include <target/target_core_backend.h> |
40 | #include <target/target_core_fabric.h> | |
c66ac9db | 41 | |
e26d99ae | 42 | #include "target_core_internal.h" |
e2480563 | 43 | #include "target_core_pr.h" |
e3d6f909 AG |
44 | |
45 | extern struct se_device *g_lun0_dev; | |
46 | ||
47 | static DEFINE_SPINLOCK(tpg_lock); | |
48 | static LIST_HEAD(tpg_list); | |
c66ac9db | 49 | |
c66ac9db NB |
50 | /* __core_tpg_get_initiator_node_acl(): |
51 | * | |
52 | * spin_lock_bh(&tpg->acl_node_lock); must be held when calling | |
53 | */ | |
54 | struct se_node_acl *__core_tpg_get_initiator_node_acl( | |
55 | struct se_portal_group *tpg, | |
56 | const char *initiatorname) | |
57 | { | |
58 | struct se_node_acl *acl; | |
59 | ||
60 | list_for_each_entry(acl, &tpg->acl_node_list, acl_list) { | |
6708bb27 | 61 | if (!strcmp(acl->initiatorname, initiatorname)) |
c66ac9db NB |
62 | return acl; |
63 | } | |
64 | ||
65 | return NULL; | |
66 | } | |
67 | ||
68 | /* core_tpg_get_initiator_node_acl(): | |
69 | * | |
70 | * | |
71 | */ | |
72 | struct se_node_acl *core_tpg_get_initiator_node_acl( | |
73 | struct se_portal_group *tpg, | |
74 | unsigned char *initiatorname) | |
75 | { | |
76 | struct se_node_acl *acl; | |
77 | ||
28638887 | 78 | spin_lock_irq(&tpg->acl_node_lock); |
fcf29481 | 79 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
28638887 | 80 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db | 81 | |
fcf29481 | 82 | return acl; |
c66ac9db | 83 | } |
b3fde035 | 84 | EXPORT_SYMBOL(core_tpg_get_initiator_node_acl); |
c66ac9db NB |
85 | |
86 | /* core_tpg_add_node_to_devs(): | |
87 | * | |
88 | * | |
89 | */ | |
90 | void core_tpg_add_node_to_devs( | |
91 | struct se_node_acl *acl, | |
92 | struct se_portal_group *tpg) | |
93 | { | |
94 | int i = 0; | |
95 | u32 lun_access = 0; | |
96 | struct se_lun *lun; | |
97 | struct se_device *dev; | |
98 | ||
99 | spin_lock(&tpg->tpg_lun_lock); | |
100 | for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { | |
4a5a75f3 | 101 | lun = tpg->tpg_lun_list[i]; |
c66ac9db NB |
102 | if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) |
103 | continue; | |
104 | ||
105 | spin_unlock(&tpg->tpg_lun_lock); | |
106 | ||
107 | dev = lun->lun_se_dev; | |
108 | /* | |
109 | * By default in LIO-Target $FABRIC_MOD, | |
110 | * demo_mode_write_protect is ON, or READ_ONLY; | |
111 | */ | |
6708bb27 | 112 | if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) { |
58d92618 | 113 | lun_access = TRANSPORT_LUNFLAGS_READ_WRITE; |
c66ac9db NB |
114 | } else { |
115 | /* | |
116 | * Allow only optical drives to issue R/W in default RO | |
117 | * demo mode. | |
118 | */ | |
e3d6f909 | 119 | if (dev->transport->get_device_type(dev) == TYPE_DISK) |
c66ac9db NB |
120 | lun_access = TRANSPORT_LUNFLAGS_READ_ONLY; |
121 | else | |
122 | lun_access = TRANSPORT_LUNFLAGS_READ_WRITE; | |
123 | } | |
124 | ||
6708bb27 | 125 | pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s" |
c66ac9db | 126 | " access for LUN in Demo Mode\n", |
e3d6f909 AG |
127 | tpg->se_tpg_tfo->get_fabric_name(), |
128 | tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, | |
c66ac9db NB |
129 | (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ? |
130 | "READ-WRITE" : "READ-ONLY"); | |
131 | ||
e80ac6c4 AG |
132 | core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun, |
133 | lun_access, acl, tpg); | |
e2480563 NB |
134 | /* |
135 | * Check to see if there are any existing persistent reservation | |
136 | * APTPL pre-registrations that need to be enabled for this dynamic | |
137 | * LUN ACL now.. | |
138 | */ | |
139 | core_scsi3_check_aptpl_registration(dev, tpg, lun, acl, | |
140 | lun->unpacked_lun); | |
c66ac9db NB |
141 | spin_lock(&tpg->tpg_lun_lock); |
142 | } | |
143 | spin_unlock(&tpg->tpg_lun_lock); | |
144 | } | |
145 | ||
146 | /* core_set_queue_depth_for_node(): | |
147 | * | |
148 | * | |
149 | */ | |
150 | static int core_set_queue_depth_for_node( | |
151 | struct se_portal_group *tpg, | |
152 | struct se_node_acl *acl) | |
153 | { | |
154 | if (!acl->queue_depth) { | |
6708bb27 | 155 | pr_err("Queue depth for %s Initiator Node: %s is 0," |
e3d6f909 | 156 | "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
157 | acl->initiatorname); |
158 | acl->queue_depth = 1; | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
4a5a75f3 JE |
164 | void array_free(void *array, int n) |
165 | { | |
166 | void **a = array; | |
167 | int i; | |
168 | ||
169 | for (i = 0; i < n; i++) | |
170 | kfree(a[i]); | |
171 | kfree(a); | |
172 | } | |
173 | ||
174 | static void *array_zalloc(int n, size_t size, gfp_t flags) | |
175 | { | |
176 | void **a; | |
177 | int i; | |
178 | ||
179 | a = kzalloc(n * sizeof(void*), flags); | |
180 | if (!a) | |
181 | return NULL; | |
182 | for (i = 0; i < n; i++) { | |
183 | a[i] = kzalloc(size, flags); | |
184 | if (!a[i]) { | |
185 | array_free(a, n); | |
186 | return NULL; | |
187 | } | |
188 | } | |
189 | return a; | |
190 | } | |
191 | ||
e413f472 CH |
192 | static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg, |
193 | const unsigned char *initiatorname) | |
c66ac9db NB |
194 | { |
195 | struct se_node_acl *acl; | |
196 | ||
144bc4c2 CH |
197 | acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size), |
198 | GFP_KERNEL); | |
6708bb27 | 199 | if (!acl) |
c66ac9db NB |
200 | return NULL; |
201 | ||
202 | INIT_LIST_HEAD(&acl->acl_list); | |
203 | INIT_LIST_HEAD(&acl->acl_sess_list); | |
29a05dee | 204 | INIT_HLIST_HEAD(&acl->lun_entry_hlist); |
afb999ff | 205 | kref_init(&acl->acl_kref); |
01468346 | 206 | init_completion(&acl->acl_free_comp); |
c66ac9db | 207 | spin_lock_init(&acl->nacl_sess_lock); |
29a05dee | 208 | mutex_init(&acl->lun_entry_mutex); |
c66ac9db | 209 | atomic_set(&acl->acl_pr_ref_count, 0); |
e1750d20 CH |
210 | if (tpg->se_tpg_tfo->tpg_get_default_depth) |
211 | acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg); | |
212 | else | |
213 | acl->queue_depth = 1; | |
c66ac9db NB |
214 | snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname); |
215 | acl->se_tpg = tpg; | |
216 | acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX); | |
c66ac9db | 217 | |
e3d6f909 | 218 | tpg->se_tpg_tfo->set_default_node_attributes(acl); |
c66ac9db | 219 | |
e413f472 | 220 | if (core_set_queue_depth_for_node(tpg, acl) < 0) |
29a05dee | 221 | goto out_free_acl; |
e413f472 CH |
222 | |
223 | return acl; | |
224 | ||
e413f472 | 225 | out_free_acl: |
144bc4c2 | 226 | kfree(acl); |
e413f472 CH |
227 | return NULL; |
228 | } | |
229 | ||
230 | static void target_add_node_acl(struct se_node_acl *acl) | |
231 | { | |
232 | struct se_portal_group *tpg = acl->se_tpg; | |
233 | ||
234 | spin_lock_irq(&tpg->acl_node_lock); | |
235 | list_add_tail(&acl->acl_list, &tpg->acl_node_list); | |
236 | tpg->num_node_acls++; | |
237 | spin_unlock_irq(&tpg->acl_node_lock); | |
238 | ||
239 | pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s" | |
240 | " Initiator Node: %s\n", | |
241 | tpg->se_tpg_tfo->get_fabric_name(), | |
242 | tpg->se_tpg_tfo->tpg_get_tag(tpg), | |
243 | acl->dynamic_node_acl ? "DYNAMIC" : "", | |
244 | acl->queue_depth, | |
245 | tpg->se_tpg_tfo->get_fabric_name(), | |
246 | acl->initiatorname); | |
247 | } | |
248 | ||
249 | struct se_node_acl *core_tpg_check_initiator_node_acl( | |
250 | struct se_portal_group *tpg, | |
251 | unsigned char *initiatorname) | |
252 | { | |
253 | struct se_node_acl *acl; | |
254 | ||
255 | acl = core_tpg_get_initiator_node_acl(tpg, initiatorname); | |
256 | if (acl) | |
257 | return acl; | |
258 | ||
259 | if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) | |
c66ac9db | 260 | return NULL; |
c66ac9db | 261 | |
e413f472 CH |
262 | acl = target_alloc_node_acl(tpg, initiatorname); |
263 | if (!acl) | |
c66ac9db | 264 | return NULL; |
e413f472 CH |
265 | acl->dynamic_node_acl = 1; |
266 | ||
052605c6 NB |
267 | /* |
268 | * Here we only create demo-mode MappedLUNs from the active | |
35d1efe8 | 269 | * TPG LUNs if the fabric is not explicitly asking for |
052605c6 NB |
270 | * tpg_check_demo_mode_login_only() == 1. |
271 | */ | |
cdf88a2f AG |
272 | if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) || |
273 | (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1)) | |
052605c6 | 274 | core_tpg_add_node_to_devs(acl, tpg); |
c66ac9db | 275 | |
e413f472 | 276 | target_add_node_acl(acl); |
c66ac9db NB |
277 | return acl; |
278 | } | |
279 | EXPORT_SYMBOL(core_tpg_check_initiator_node_acl); | |
280 | ||
281 | void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl) | |
282 | { | |
283 | while (atomic_read(&nacl->acl_pr_ref_count) != 0) | |
284 | cpu_relax(); | |
285 | } | |
286 | ||
c66ac9db NB |
287 | void core_tpg_clear_object_luns(struct se_portal_group *tpg) |
288 | { | |
28168905 | 289 | int i; |
c66ac9db NB |
290 | struct se_lun *lun; |
291 | ||
292 | spin_lock(&tpg->tpg_lun_lock); | |
293 | for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { | |
4a5a75f3 | 294 | lun = tpg->tpg_lun_list[i]; |
c66ac9db NB |
295 | |
296 | if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) || | |
297 | (lun->lun_se_dev == NULL)) | |
298 | continue; | |
299 | ||
300 | spin_unlock(&tpg->tpg_lun_lock); | |
cd9d7cba | 301 | core_dev_del_lun(tpg, lun); |
c66ac9db NB |
302 | spin_lock(&tpg->tpg_lun_lock); |
303 | } | |
304 | spin_unlock(&tpg->tpg_lun_lock); | |
305 | } | |
306 | EXPORT_SYMBOL(core_tpg_clear_object_luns); | |
307 | ||
c66ac9db NB |
308 | struct se_node_acl *core_tpg_add_initiator_node_acl( |
309 | struct se_portal_group *tpg, | |
c7d6a803 | 310 | const char *initiatorname) |
c66ac9db | 311 | { |
c7d6a803 | 312 | struct se_node_acl *acl; |
c66ac9db | 313 | |
28638887 | 314 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db | 315 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
6708bb27 | 316 | if (acl) { |
c66ac9db NB |
317 | if (acl->dynamic_node_acl) { |
318 | acl->dynamic_node_acl = 0; | |
6708bb27 | 319 | pr_debug("%s_TPG[%u] - Replacing dynamic ACL" |
e3d6f909 AG |
320 | " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), |
321 | tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname); | |
28638887 | 322 | spin_unlock_irq(&tpg->acl_node_lock); |
e413f472 | 323 | return acl; |
c66ac9db NB |
324 | } |
325 | ||
6708bb27 | 326 | pr_err("ACL entry for %s Initiator" |
c66ac9db | 327 | " Node %s already exists for TPG %u, ignoring" |
e3d6f909 AG |
328 | " request.\n", tpg->se_tpg_tfo->get_fabric_name(), |
329 | initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
28638887 | 330 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
331 | return ERR_PTR(-EEXIST); |
332 | } | |
28638887 | 333 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db | 334 | |
e413f472 CH |
335 | acl = target_alloc_node_acl(tpg, initiatorname); |
336 | if (!acl) | |
c66ac9db | 337 | return ERR_PTR(-ENOMEM); |
c66ac9db | 338 | |
e413f472 | 339 | target_add_node_acl(acl); |
c66ac9db NB |
340 | return acl; |
341 | } | |
c66ac9db | 342 | |
c7d6a803 | 343 | void core_tpg_del_initiator_node_acl(struct se_node_acl *acl) |
c66ac9db | 344 | { |
c7d6a803 | 345 | struct se_portal_group *tpg = acl->se_tpg; |
337c0607 | 346 | LIST_HEAD(sess_list); |
c66ac9db | 347 | struct se_session *sess, *sess_tmp; |
140854cb | 348 | unsigned long flags; |
28168905 | 349 | int rc; |
c66ac9db | 350 | |
28638887 | 351 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
352 | if (acl->dynamic_node_acl) { |
353 | acl->dynamic_node_acl = 0; | |
c66ac9db NB |
354 | } |
355 | list_del(&acl->acl_list); | |
356 | tpg->num_node_acls--; | |
28638887 | 357 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db | 358 | |
337c0607 NB |
359 | spin_lock_irqsave(&acl->nacl_sess_lock, flags); |
360 | acl->acl_stop = 1; | |
361 | ||
362 | list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list, | |
363 | sess_acl_list) { | |
364 | if (sess->sess_tearing_down != 0) | |
c66ac9db NB |
365 | continue; |
366 | ||
337c0607 NB |
367 | target_get_session(sess); |
368 | list_move(&sess->sess_acl_list, &sess_list); | |
369 | } | |
370 | spin_unlock_irqrestore(&acl->nacl_sess_lock, flags); | |
371 | ||
372 | list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) { | |
373 | list_del(&sess->sess_acl_list); | |
c66ac9db | 374 | |
337c0607 NB |
375 | rc = tpg->se_tpg_tfo->shutdown_session(sess); |
376 | target_put_session(sess); | |
377 | if (!rc) | |
378 | continue; | |
379 | target_put_session(sess); | |
c66ac9db | 380 | } |
337c0607 NB |
381 | target_put_nacl(acl); |
382 | /* | |
383 | * Wait for last target_put_nacl() to complete in target_complete_nacl() | |
384 | * for active fabric session transport_deregister_session() callbacks. | |
385 | */ | |
386 | wait_for_completion(&acl->acl_free_comp); | |
c66ac9db NB |
387 | |
388 | core_tpg_wait_for_nacl_pr_ref(acl); | |
c66ac9db NB |
389 | core_free_device_list_for_node(acl, tpg); |
390 | ||
6708bb27 | 391 | pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s" |
e3d6f909 AG |
392 | " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(), |
393 | tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth, | |
394 | tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname); | |
c66ac9db | 395 | |
144bc4c2 | 396 | kfree(acl); |
c66ac9db | 397 | } |
c66ac9db NB |
398 | |
399 | /* core_tpg_set_initiator_node_queue_depth(): | |
400 | * | |
401 | * | |
402 | */ | |
403 | int core_tpg_set_initiator_node_queue_depth( | |
404 | struct se_portal_group *tpg, | |
405 | unsigned char *initiatorname, | |
406 | u32 queue_depth, | |
407 | int force) | |
408 | { | |
409 | struct se_session *sess, *init_sess = NULL; | |
410 | struct se_node_acl *acl; | |
140854cb | 411 | unsigned long flags; |
c66ac9db NB |
412 | int dynamic_acl = 0; |
413 | ||
28638887 | 414 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db | 415 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
6708bb27 AG |
416 | if (!acl) { |
417 | pr_err("Access Control List entry for %s Initiator" | |
c66ac9db | 418 | " Node %s does not exists for TPG %hu, ignoring" |
e3d6f909 AG |
419 | " request.\n", tpg->se_tpg_tfo->get_fabric_name(), |
420 | initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
28638887 | 421 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
422 | return -ENODEV; |
423 | } | |
424 | if (acl->dynamic_node_acl) { | |
425 | acl->dynamic_node_acl = 0; | |
426 | dynamic_acl = 1; | |
427 | } | |
28638887 | 428 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db | 429 | |
140854cb | 430 | spin_lock_irqsave(&tpg->session_lock, flags); |
c66ac9db NB |
431 | list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) { |
432 | if (sess->se_node_acl != acl) | |
433 | continue; | |
434 | ||
435 | if (!force) { | |
6708bb27 | 436 | pr_err("Unable to change queue depth for %s" |
c66ac9db NB |
437 | " Initiator Node: %s while session is" |
438 | " operational. To forcefully change the queue" | |
439 | " depth and force session reinstatement" | |
440 | " use the \"force=1\" parameter.\n", | |
e3d6f909 | 441 | tpg->se_tpg_tfo->get_fabric_name(), initiatorname); |
140854cb | 442 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db | 443 | |
28638887 | 444 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
445 | if (dynamic_acl) |
446 | acl->dynamic_node_acl = 1; | |
28638887 | 447 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
448 | return -EEXIST; |
449 | } | |
450 | /* | |
451 | * Determine if the session needs to be closed by our context. | |
452 | */ | |
6708bb27 | 453 | if (!tpg->se_tpg_tfo->shutdown_session(sess)) |
c66ac9db NB |
454 | continue; |
455 | ||
456 | init_sess = sess; | |
457 | break; | |
458 | } | |
459 | ||
460 | /* | |
461 | * User has requested to change the queue depth for a Initiator Node. | |
462 | * Change the value in the Node's struct se_node_acl, and call | |
463 | * core_set_queue_depth_for_node() to add the requested queue depth. | |
464 | * | |
e3d6f909 | 465 | * Finally call tpg->se_tpg_tfo->close_session() to force session |
c66ac9db NB |
466 | * reinstatement to occur if there is an active session for the |
467 | * $FABRIC_MOD Initiator Node in question. | |
468 | */ | |
469 | acl->queue_depth = queue_depth; | |
470 | ||
471 | if (core_set_queue_depth_for_node(tpg, acl) < 0) { | |
140854cb | 472 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db NB |
473 | /* |
474 | * Force session reinstatement if | |
475 | * core_set_queue_depth_for_node() failed, because we assume | |
476 | * the $FABRIC_MOD has already the set session reinstatement | |
e3d6f909 | 477 | * bit from tpg->se_tpg_tfo->shutdown_session() called above. |
c66ac9db NB |
478 | */ |
479 | if (init_sess) | |
e3d6f909 | 480 | tpg->se_tpg_tfo->close_session(init_sess); |
c66ac9db | 481 | |
28638887 | 482 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
483 | if (dynamic_acl) |
484 | acl->dynamic_node_acl = 1; | |
28638887 | 485 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
486 | return -EINVAL; |
487 | } | |
140854cb | 488 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db NB |
489 | /* |
490 | * If the $FABRIC_MOD session for the Initiator Node ACL exists, | |
491 | * forcefully shutdown the $FABRIC_MOD session/nexus. | |
492 | */ | |
493 | if (init_sess) | |
e3d6f909 | 494 | tpg->se_tpg_tfo->close_session(init_sess); |
c66ac9db | 495 | |
bfb9035c | 496 | pr_debug("Successfully changed queue depth to: %d for Initiator" |
c66ac9db | 497 | " Node: %s on %s Target Portal Group: %u\n", queue_depth, |
e3d6f909 AG |
498 | initiatorname, tpg->se_tpg_tfo->get_fabric_name(), |
499 | tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
c66ac9db | 500 | |
28638887 | 501 | spin_lock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
502 | if (dynamic_acl) |
503 | acl->dynamic_node_acl = 1; | |
28638887 | 504 | spin_unlock_irq(&tpg->acl_node_lock); |
c66ac9db NB |
505 | |
506 | return 0; | |
507 | } | |
508 | EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth); | |
509 | ||
79e62fc3 AG |
510 | /* core_tpg_set_initiator_node_tag(): |
511 | * | |
512 | * Initiator nodeacl tags are not used internally, but may be used by | |
513 | * userspace to emulate aliases or groups. | |
514 | * Returns length of newly-set tag or -EINVAL. | |
515 | */ | |
516 | int core_tpg_set_initiator_node_tag( | |
517 | struct se_portal_group *tpg, | |
518 | struct se_node_acl *acl, | |
519 | const char *new_tag) | |
520 | { | |
521 | if (strlen(new_tag) >= MAX_ACL_TAG_SIZE) | |
522 | return -EINVAL; | |
523 | ||
524 | if (!strncmp("NULL", new_tag, 4)) { | |
525 | acl->acl_tag[0] = '\0'; | |
526 | return 0; | |
527 | } | |
528 | ||
529 | return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag); | |
530 | } | |
531 | EXPORT_SYMBOL(core_tpg_set_initiator_node_tag); | |
532 | ||
5277797d NB |
533 | static void core_tpg_lun_ref_release(struct percpu_ref *ref) |
534 | { | |
535 | struct se_lun *lun = container_of(ref, struct se_lun, lun_ref); | |
536 | ||
537 | complete(&lun->lun_ref_comp); | |
538 | } | |
539 | ||
c66ac9db NB |
540 | static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg) |
541 | { | |
542 | /* Set in core_dev_setup_virtual_lun0() */ | |
e3d6f909 | 543 | struct se_device *dev = g_lun0_dev; |
c66ac9db NB |
544 | struct se_lun *lun = &se_tpg->tpg_virt_lun0; |
545 | u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY; | |
546 | int ret; | |
547 | ||
548 | lun->unpacked_lun = 0; | |
549 | lun->lun_status = TRANSPORT_LUN_STATUS_FREE; | |
550 | atomic_set(&lun->lun_acl_count, 0); | |
551 | init_completion(&lun->lun_shutdown_comp); | |
552 | INIT_LIST_HEAD(&lun->lun_acl_list); | |
c66ac9db | 553 | spin_lock_init(&lun->lun_acl_lock); |
c66ac9db | 554 | spin_lock_init(&lun->lun_sep_lock); |
5277797d | 555 | init_completion(&lun->lun_ref_comp); |
c66ac9db | 556 | |
d344f8a1 | 557 | ret = core_tpg_add_lun(se_tpg, lun, lun_access, dev); |
de06875f | 558 | if (ret < 0) |
5277797d | 559 | return ret; |
5277797d | 560 | |
c66ac9db NB |
561 | return 0; |
562 | } | |
563 | ||
c66ac9db | 564 | int core_tpg_register( |
9ac8928e | 565 | const struct target_core_fabric_ops *tfo, |
c66ac9db NB |
566 | struct se_wwn *se_wwn, |
567 | struct se_portal_group *se_tpg, | |
e4aae5af | 568 | int proto_id) |
c66ac9db NB |
569 | { |
570 | struct se_lun *lun; | |
571 | u32 i; | |
572 | ||
4a5a75f3 JE |
573 | se_tpg->tpg_lun_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG, |
574 | sizeof(struct se_lun), GFP_KERNEL); | |
6708bb27 AG |
575 | if (!se_tpg->tpg_lun_list) { |
576 | pr_err("Unable to allocate struct se_portal_group->" | |
c66ac9db NB |
577 | "tpg_lun_list\n"); |
578 | return -ENOMEM; | |
579 | } | |
580 | ||
581 | for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { | |
4a5a75f3 | 582 | lun = se_tpg->tpg_lun_list[i]; |
c66ac9db | 583 | lun->unpacked_lun = i; |
0ff87549 | 584 | lun->lun_link_magic = SE_LUN_LINK_MAGIC; |
c66ac9db NB |
585 | lun->lun_status = TRANSPORT_LUN_STATUS_FREE; |
586 | atomic_set(&lun->lun_acl_count, 0); | |
587 | init_completion(&lun->lun_shutdown_comp); | |
588 | INIT_LIST_HEAD(&lun->lun_acl_list); | |
c66ac9db | 589 | spin_lock_init(&lun->lun_acl_lock); |
c66ac9db | 590 | spin_lock_init(&lun->lun_sep_lock); |
5277797d | 591 | init_completion(&lun->lun_ref_comp); |
c66ac9db NB |
592 | } |
593 | ||
e4aae5af | 594 | se_tpg->proto_id = proto_id; |
c66ac9db NB |
595 | se_tpg->se_tpg_tfo = tfo; |
596 | se_tpg->se_tpg_wwn = se_wwn; | |
597 | atomic_set(&se_tpg->tpg_pr_ref_count, 0); | |
598 | INIT_LIST_HEAD(&se_tpg->acl_node_list); | |
e3d6f909 | 599 | INIT_LIST_HEAD(&se_tpg->se_tpg_node); |
c66ac9db NB |
600 | INIT_LIST_HEAD(&se_tpg->tpg_sess_list); |
601 | spin_lock_init(&se_tpg->acl_node_lock); | |
602 | spin_lock_init(&se_tpg->session_lock); | |
603 | spin_lock_init(&se_tpg->tpg_lun_lock); | |
604 | ||
e4aae5af | 605 | if (se_tpg->proto_id >= 0) { |
c66ac9db | 606 | if (core_tpg_setup_virtual_lun0(se_tpg) < 0) { |
0fb889b8 WY |
607 | array_free(se_tpg->tpg_lun_list, |
608 | TRANSPORT_MAX_LUNS_PER_TPG); | |
c66ac9db NB |
609 | return -ENOMEM; |
610 | } | |
611 | } | |
612 | ||
e3d6f909 AG |
613 | spin_lock_bh(&tpg_lock); |
614 | list_add_tail(&se_tpg->se_tpg_node, &tpg_list); | |
615 | spin_unlock_bh(&tpg_lock); | |
c66ac9db | 616 | |
e4aae5af CH |
617 | pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, " |
618 | "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(), | |
619 | tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL, | |
620 | se_tpg->proto_id, tfo->tpg_get_tag(se_tpg)); | |
c66ac9db NB |
621 | |
622 | return 0; | |
623 | } | |
624 | EXPORT_SYMBOL(core_tpg_register); | |
625 | ||
626 | int core_tpg_deregister(struct se_portal_group *se_tpg) | |
627 | { | |
e4aae5af | 628 | const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo; |
e89d15ee NB |
629 | struct se_node_acl *nacl, *nacl_tmp; |
630 | ||
e4aae5af CH |
631 | pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, " |
632 | "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(), | |
633 | tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL, | |
634 | se_tpg->proto_id, tfo->tpg_get_tag(se_tpg)); | |
c66ac9db | 635 | |
e3d6f909 AG |
636 | spin_lock_bh(&tpg_lock); |
637 | list_del(&se_tpg->se_tpg_node); | |
638 | spin_unlock_bh(&tpg_lock); | |
c66ac9db NB |
639 | |
640 | while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0) | |
641 | cpu_relax(); | |
e89d15ee NB |
642 | /* |
643 | * Release any remaining demo-mode generated se_node_acl that have | |
644 | * not been released because of TFO->tpg_check_demo_mode_cache() == 1 | |
645 | * in transport_deregister_session(). | |
646 | */ | |
28638887 | 647 | spin_lock_irq(&se_tpg->acl_node_lock); |
e89d15ee NB |
648 | list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list, |
649 | acl_list) { | |
650 | list_del(&nacl->acl_list); | |
651 | se_tpg->num_node_acls--; | |
28638887 | 652 | spin_unlock_irq(&se_tpg->acl_node_lock); |
e89d15ee NB |
653 | |
654 | core_tpg_wait_for_nacl_pr_ref(nacl); | |
655 | core_free_device_list_for_node(nacl, se_tpg); | |
144bc4c2 | 656 | kfree(nacl); |
e89d15ee | 657 | |
28638887 | 658 | spin_lock_irq(&se_tpg->acl_node_lock); |
e89d15ee | 659 | } |
28638887 | 660 | spin_unlock_irq(&se_tpg->acl_node_lock); |
c66ac9db | 661 | |
e4aae5af | 662 | if (se_tpg->proto_id >= 0) |
9c7d6154 | 663 | core_tpg_remove_lun(se_tpg, &se_tpg->tpg_virt_lun0); |
c66ac9db | 664 | |
4a5a75f3 | 665 | array_free(se_tpg->tpg_lun_list, TRANSPORT_MAX_LUNS_PER_TPG); |
c66ac9db NB |
666 | return 0; |
667 | } | |
668 | EXPORT_SYMBOL(core_tpg_deregister); | |
669 | ||
d344f8a1 | 670 | struct se_lun *core_tpg_alloc_lun( |
c66ac9db NB |
671 | struct se_portal_group *tpg, |
672 | u32 unpacked_lun) | |
673 | { | |
674 | struct se_lun *lun; | |
675 | ||
676 | if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) { | |
6708bb27 | 677 | pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG" |
c66ac9db | 678 | "-1: %u for Target Portal Group: %u\n", |
e3d6f909 | 679 | tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db | 680 | unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1, |
e3d6f909 | 681 | tpg->se_tpg_tfo->tpg_get_tag(tpg)); |
c66ac9db NB |
682 | return ERR_PTR(-EOVERFLOW); |
683 | } | |
684 | ||
685 | spin_lock(&tpg->tpg_lun_lock); | |
4a5a75f3 | 686 | lun = tpg->tpg_lun_list[unpacked_lun]; |
c66ac9db | 687 | if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) { |
6708bb27 | 688 | pr_err("TPG Logical Unit Number: %u is already active" |
c66ac9db | 689 | " on %s Target Portal Group: %u, ignoring request.\n", |
e3d6f909 AG |
690 | unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(), |
691 | tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
c66ac9db NB |
692 | spin_unlock(&tpg->tpg_lun_lock); |
693 | return ERR_PTR(-EINVAL); | |
694 | } | |
695 | spin_unlock(&tpg->tpg_lun_lock); | |
696 | ||
697 | return lun; | |
698 | } | |
699 | ||
d344f8a1 | 700 | int core_tpg_add_lun( |
c66ac9db NB |
701 | struct se_portal_group *tpg, |
702 | struct se_lun *lun, | |
703 | u32 lun_access, | |
340dbf72 | 704 | struct se_device *dev) |
c66ac9db | 705 | { |
e3d6f909 AG |
706 | int ret; |
707 | ||
2aad2a86 | 708 | ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0, |
a34375ef | 709 | GFP_KERNEL); |
e3d6f909 AG |
710 | if (ret < 0) |
711 | return ret; | |
c66ac9db | 712 | |
340dbf72 | 713 | ret = core_dev_export(dev, tpg, lun); |
5277797d | 714 | if (ret < 0) { |
9a1049da | 715 | percpu_ref_exit(&lun->lun_ref); |
5277797d NB |
716 | return ret; |
717 | } | |
718 | ||
c66ac9db NB |
719 | spin_lock(&tpg->tpg_lun_lock); |
720 | lun->lun_access = lun_access; | |
721 | lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE; | |
722 | spin_unlock(&tpg->tpg_lun_lock); | |
723 | ||
724 | return 0; | |
725 | } | |
726 | ||
cd9d7cba | 727 | void core_tpg_remove_lun( |
c66ac9db NB |
728 | struct se_portal_group *tpg, |
729 | struct se_lun *lun) | |
730 | { | |
4a9a6c8d NB |
731 | core_clear_lun_from_tpg(lun, tpg); |
732 | transport_clear_lun_ref(lun); | |
c66ac9db NB |
733 | |
734 | core_dev_unexport(lun->lun_se_dev, tpg, lun); | |
735 | ||
736 | spin_lock(&tpg->tpg_lun_lock); | |
737 | lun->lun_status = TRANSPORT_LUN_STATUS_FREE; | |
738 | spin_unlock(&tpg->tpg_lun_lock); | |
739 | ||
9a1049da | 740 | percpu_ref_exit(&lun->lun_ref); |
c66ac9db | 741 | } |