]>
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 | 39 | #include <target/target_core_backend.h> |
bc0c94b1 | 40 | #include <target/target_core_configfs.h> |
c4795fb2 | 41 | #include <target/target_core_fabric.h> |
c66ac9db | 42 | |
e26d99ae | 43 | #include "target_core_internal.h" |
adf653f9 | 44 | #include "target_core_alua.h" |
e2480563 | 45 | #include "target_core_pr.h" |
e3d6f909 AG |
46 | |
47 | extern struct se_device *g_lun0_dev; | |
48 | ||
49 | static DEFINE_SPINLOCK(tpg_lock); | |
50 | static LIST_HEAD(tpg_list); | |
c66ac9db | 51 | |
c66ac9db NB |
52 | /* __core_tpg_get_initiator_node_acl(): |
53 | * | |
403edd78 | 54 | * mutex_lock(&tpg->acl_node_mutex); must be held when calling |
c66ac9db NB |
55 | */ |
56 | struct se_node_acl *__core_tpg_get_initiator_node_acl( | |
57 | struct se_portal_group *tpg, | |
58 | const char *initiatorname) | |
59 | { | |
60 | struct se_node_acl *acl; | |
61 | ||
62 | list_for_each_entry(acl, &tpg->acl_node_list, acl_list) { | |
6708bb27 | 63 | if (!strcmp(acl->initiatorname, initiatorname)) |
c66ac9db NB |
64 | return acl; |
65 | } | |
66 | ||
67 | return NULL; | |
68 | } | |
69 | ||
70 | /* core_tpg_get_initiator_node_acl(): | |
71 | * | |
72 | * | |
73 | */ | |
74 | struct se_node_acl *core_tpg_get_initiator_node_acl( | |
75 | struct se_portal_group *tpg, | |
76 | unsigned char *initiatorname) | |
77 | { | |
78 | struct se_node_acl *acl; | |
79 | ||
403edd78 | 80 | mutex_lock(&tpg->acl_node_mutex); |
fcf29481 | 81 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
403edd78 | 82 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db | 83 | |
fcf29481 | 84 | return acl; |
c66ac9db | 85 | } |
b3fde035 | 86 | EXPORT_SYMBOL(core_tpg_get_initiator_node_acl); |
c66ac9db NB |
87 | |
88 | /* core_tpg_add_node_to_devs(): | |
89 | * | |
90 | * | |
91 | */ | |
92 | void core_tpg_add_node_to_devs( | |
93 | struct se_node_acl *acl, | |
df9766ca NB |
94 | struct se_portal_group *tpg, |
95 | struct se_lun *lun_orig) | |
c66ac9db | 96 | { |
c66ac9db NB |
97 | u32 lun_access = 0; |
98 | struct se_lun *lun; | |
99 | struct se_device *dev; | |
100 | ||
6bb82612 NB |
101 | mutex_lock(&tpg->tpg_lun_mutex); |
102 | hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) { | |
df9766ca NB |
103 | if (lun_orig && lun != lun_orig) |
104 | continue; | |
c66ac9db | 105 | |
4cc987ea NB |
106 | dev = rcu_dereference_check(lun->lun_se_dev, |
107 | lockdep_is_held(&tpg->tpg_lun_mutex)); | |
c66ac9db NB |
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 | 132 | core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun, |
6bb82612 | 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 | 141 | } |
6bb82612 | 142 | mutex_unlock(&tpg->tpg_lun_mutex); |
c66ac9db NB |
143 | } |
144 | ||
145 | /* core_set_queue_depth_for_node(): | |
146 | * | |
147 | * | |
148 | */ | |
149 | static int core_set_queue_depth_for_node( | |
150 | struct se_portal_group *tpg, | |
151 | struct se_node_acl *acl) | |
152 | { | |
153 | if (!acl->queue_depth) { | |
6708bb27 | 154 | pr_err("Queue depth for %s Initiator Node: %s is 0," |
e3d6f909 | 155 | "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
156 | acl->initiatorname); |
157 | acl->queue_depth = 1; | |
158 | } | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
e413f472 CH |
163 | static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg, |
164 | const unsigned char *initiatorname) | |
c66ac9db NB |
165 | { |
166 | struct se_node_acl *acl; | |
167 | ||
144bc4c2 CH |
168 | acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size), |
169 | GFP_KERNEL); | |
6708bb27 | 170 | if (!acl) |
c66ac9db NB |
171 | return NULL; |
172 | ||
173 | INIT_LIST_HEAD(&acl->acl_list); | |
174 | INIT_LIST_HEAD(&acl->acl_sess_list); | |
29a05dee | 175 | INIT_HLIST_HEAD(&acl->lun_entry_hlist); |
afb999ff | 176 | kref_init(&acl->acl_kref); |
01468346 | 177 | init_completion(&acl->acl_free_comp); |
c66ac9db | 178 | spin_lock_init(&acl->nacl_sess_lock); |
29a05dee | 179 | mutex_init(&acl->lun_entry_mutex); |
c66ac9db | 180 | atomic_set(&acl->acl_pr_ref_count, 0); |
e1750d20 CH |
181 | if (tpg->se_tpg_tfo->tpg_get_default_depth) |
182 | acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg); | |
183 | else | |
184 | acl->queue_depth = 1; | |
c66ac9db NB |
185 | snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname); |
186 | acl->se_tpg = tpg; | |
187 | acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX); | |
c66ac9db | 188 | |
e3d6f909 | 189 | tpg->se_tpg_tfo->set_default_node_attributes(acl); |
c66ac9db | 190 | |
e413f472 | 191 | if (core_set_queue_depth_for_node(tpg, acl) < 0) |
29a05dee | 192 | goto out_free_acl; |
e413f472 CH |
193 | |
194 | return acl; | |
195 | ||
e413f472 | 196 | out_free_acl: |
144bc4c2 | 197 | kfree(acl); |
e413f472 CH |
198 | return NULL; |
199 | } | |
200 | ||
201 | static void target_add_node_acl(struct se_node_acl *acl) | |
202 | { | |
203 | struct se_portal_group *tpg = acl->se_tpg; | |
204 | ||
403edd78 | 205 | mutex_lock(&tpg->acl_node_mutex); |
e413f472 CH |
206 | list_add_tail(&acl->acl_list, &tpg->acl_node_list); |
207 | tpg->num_node_acls++; | |
403edd78 | 208 | mutex_unlock(&tpg->acl_node_mutex); |
e413f472 CH |
209 | |
210 | pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s" | |
211 | " Initiator Node: %s\n", | |
212 | tpg->se_tpg_tfo->get_fabric_name(), | |
213 | tpg->se_tpg_tfo->tpg_get_tag(tpg), | |
214 | acl->dynamic_node_acl ? "DYNAMIC" : "", | |
215 | acl->queue_depth, | |
216 | tpg->se_tpg_tfo->get_fabric_name(), | |
217 | acl->initiatorname); | |
218 | } | |
219 | ||
220 | struct se_node_acl *core_tpg_check_initiator_node_acl( | |
221 | struct se_portal_group *tpg, | |
222 | unsigned char *initiatorname) | |
223 | { | |
224 | struct se_node_acl *acl; | |
225 | ||
226 | acl = core_tpg_get_initiator_node_acl(tpg, initiatorname); | |
227 | if (acl) | |
228 | return acl; | |
229 | ||
230 | if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) | |
c66ac9db | 231 | return NULL; |
c66ac9db | 232 | |
e413f472 CH |
233 | acl = target_alloc_node_acl(tpg, initiatorname); |
234 | if (!acl) | |
c66ac9db | 235 | return NULL; |
e413f472 CH |
236 | acl->dynamic_node_acl = 1; |
237 | ||
052605c6 NB |
238 | /* |
239 | * Here we only create demo-mode MappedLUNs from the active | |
35d1efe8 | 240 | * TPG LUNs if the fabric is not explicitly asking for |
052605c6 NB |
241 | * tpg_check_demo_mode_login_only() == 1. |
242 | */ | |
cdf88a2f AG |
243 | if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) || |
244 | (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1)) | |
df9766ca | 245 | core_tpg_add_node_to_devs(acl, tpg, NULL); |
c66ac9db | 246 | |
e413f472 | 247 | target_add_node_acl(acl); |
c66ac9db NB |
248 | return acl; |
249 | } | |
250 | EXPORT_SYMBOL(core_tpg_check_initiator_node_acl); | |
251 | ||
252 | void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl) | |
253 | { | |
254 | while (atomic_read(&nacl->acl_pr_ref_count) != 0) | |
255 | cpu_relax(); | |
256 | } | |
257 | ||
c66ac9db NB |
258 | struct se_node_acl *core_tpg_add_initiator_node_acl( |
259 | struct se_portal_group *tpg, | |
c7d6a803 | 260 | const char *initiatorname) |
c66ac9db | 261 | { |
c7d6a803 | 262 | struct se_node_acl *acl; |
c66ac9db | 263 | |
403edd78 | 264 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db | 265 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
6708bb27 | 266 | if (acl) { |
c66ac9db NB |
267 | if (acl->dynamic_node_acl) { |
268 | acl->dynamic_node_acl = 0; | |
6708bb27 | 269 | pr_debug("%s_TPG[%u] - Replacing dynamic ACL" |
e3d6f909 AG |
270 | " for %s\n", tpg->se_tpg_tfo->get_fabric_name(), |
271 | tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname); | |
403edd78 | 272 | mutex_unlock(&tpg->acl_node_mutex); |
e413f472 | 273 | return acl; |
c66ac9db NB |
274 | } |
275 | ||
6708bb27 | 276 | pr_err("ACL entry for %s Initiator" |
c66ac9db | 277 | " Node %s already exists for TPG %u, ignoring" |
e3d6f909 AG |
278 | " request.\n", tpg->se_tpg_tfo->get_fabric_name(), |
279 | initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
403edd78 | 280 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db NB |
281 | return ERR_PTR(-EEXIST); |
282 | } | |
403edd78 | 283 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db | 284 | |
e413f472 CH |
285 | acl = target_alloc_node_acl(tpg, initiatorname); |
286 | if (!acl) | |
c66ac9db | 287 | return ERR_PTR(-ENOMEM); |
c66ac9db | 288 | |
e413f472 | 289 | target_add_node_acl(acl); |
c66ac9db NB |
290 | return acl; |
291 | } | |
c66ac9db | 292 | |
c7d6a803 | 293 | void core_tpg_del_initiator_node_acl(struct se_node_acl *acl) |
c66ac9db | 294 | { |
c7d6a803 | 295 | struct se_portal_group *tpg = acl->se_tpg; |
337c0607 | 296 | LIST_HEAD(sess_list); |
c66ac9db | 297 | struct se_session *sess, *sess_tmp; |
140854cb | 298 | unsigned long flags; |
28168905 | 299 | int rc; |
c66ac9db | 300 | |
403edd78 | 301 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db NB |
302 | if (acl->dynamic_node_acl) { |
303 | acl->dynamic_node_acl = 0; | |
c66ac9db NB |
304 | } |
305 | list_del(&acl->acl_list); | |
306 | tpg->num_node_acls--; | |
403edd78 | 307 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db | 308 | |
337c0607 NB |
309 | spin_lock_irqsave(&acl->nacl_sess_lock, flags); |
310 | acl->acl_stop = 1; | |
311 | ||
312 | list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list, | |
313 | sess_acl_list) { | |
314 | if (sess->sess_tearing_down != 0) | |
c66ac9db NB |
315 | continue; |
316 | ||
337c0607 NB |
317 | target_get_session(sess); |
318 | list_move(&sess->sess_acl_list, &sess_list); | |
319 | } | |
320 | spin_unlock_irqrestore(&acl->nacl_sess_lock, flags); | |
321 | ||
322 | list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) { | |
323 | list_del(&sess->sess_acl_list); | |
c66ac9db | 324 | |
337c0607 NB |
325 | rc = tpg->se_tpg_tfo->shutdown_session(sess); |
326 | target_put_session(sess); | |
327 | if (!rc) | |
328 | continue; | |
329 | target_put_session(sess); | |
c66ac9db | 330 | } |
337c0607 NB |
331 | target_put_nacl(acl); |
332 | /* | |
333 | * Wait for last target_put_nacl() to complete in target_complete_nacl() | |
334 | * for active fabric session transport_deregister_session() callbacks. | |
335 | */ | |
336 | wait_for_completion(&acl->acl_free_comp); | |
c66ac9db NB |
337 | |
338 | core_tpg_wait_for_nacl_pr_ref(acl); | |
c66ac9db NB |
339 | core_free_device_list_for_node(acl, tpg); |
340 | ||
6708bb27 | 341 | pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s" |
e3d6f909 AG |
342 | " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(), |
343 | tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth, | |
344 | tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname); | |
c66ac9db | 345 | |
144bc4c2 | 346 | kfree(acl); |
c66ac9db | 347 | } |
c66ac9db NB |
348 | |
349 | /* core_tpg_set_initiator_node_queue_depth(): | |
350 | * | |
351 | * | |
352 | */ | |
353 | int core_tpg_set_initiator_node_queue_depth( | |
354 | struct se_portal_group *tpg, | |
355 | unsigned char *initiatorname, | |
356 | u32 queue_depth, | |
357 | int force) | |
358 | { | |
359 | struct se_session *sess, *init_sess = NULL; | |
360 | struct se_node_acl *acl; | |
140854cb | 361 | unsigned long flags; |
c66ac9db NB |
362 | int dynamic_acl = 0; |
363 | ||
403edd78 | 364 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db | 365 | acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname); |
6708bb27 AG |
366 | if (!acl) { |
367 | pr_err("Access Control List entry for %s Initiator" | |
c66ac9db | 368 | " Node %s does not exists for TPG %hu, ignoring" |
e3d6f909 AG |
369 | " request.\n", tpg->se_tpg_tfo->get_fabric_name(), |
370 | initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
403edd78 | 371 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db NB |
372 | return -ENODEV; |
373 | } | |
374 | if (acl->dynamic_node_acl) { | |
375 | acl->dynamic_node_acl = 0; | |
376 | dynamic_acl = 1; | |
377 | } | |
403edd78 | 378 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db | 379 | |
140854cb | 380 | spin_lock_irqsave(&tpg->session_lock, flags); |
c66ac9db NB |
381 | list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) { |
382 | if (sess->se_node_acl != acl) | |
383 | continue; | |
384 | ||
385 | if (!force) { | |
6708bb27 | 386 | pr_err("Unable to change queue depth for %s" |
c66ac9db NB |
387 | " Initiator Node: %s while session is" |
388 | " operational. To forcefully change the queue" | |
389 | " depth and force session reinstatement" | |
390 | " use the \"force=1\" parameter.\n", | |
e3d6f909 | 391 | tpg->se_tpg_tfo->get_fabric_name(), initiatorname); |
140854cb | 392 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db | 393 | |
403edd78 | 394 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db NB |
395 | if (dynamic_acl) |
396 | acl->dynamic_node_acl = 1; | |
403edd78 | 397 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db NB |
398 | return -EEXIST; |
399 | } | |
400 | /* | |
401 | * Determine if the session needs to be closed by our context. | |
402 | */ | |
6708bb27 | 403 | if (!tpg->se_tpg_tfo->shutdown_session(sess)) |
c66ac9db NB |
404 | continue; |
405 | ||
406 | init_sess = sess; | |
407 | break; | |
408 | } | |
409 | ||
410 | /* | |
411 | * User has requested to change the queue depth for a Initiator Node. | |
412 | * Change the value in the Node's struct se_node_acl, and call | |
413 | * core_set_queue_depth_for_node() to add the requested queue depth. | |
414 | * | |
e3d6f909 | 415 | * Finally call tpg->se_tpg_tfo->close_session() to force session |
c66ac9db NB |
416 | * reinstatement to occur if there is an active session for the |
417 | * $FABRIC_MOD Initiator Node in question. | |
418 | */ | |
419 | acl->queue_depth = queue_depth; | |
420 | ||
421 | if (core_set_queue_depth_for_node(tpg, acl) < 0) { | |
140854cb | 422 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db NB |
423 | /* |
424 | * Force session reinstatement if | |
425 | * core_set_queue_depth_for_node() failed, because we assume | |
426 | * the $FABRIC_MOD has already the set session reinstatement | |
e3d6f909 | 427 | * bit from tpg->se_tpg_tfo->shutdown_session() called above. |
c66ac9db NB |
428 | */ |
429 | if (init_sess) | |
e3d6f909 | 430 | tpg->se_tpg_tfo->close_session(init_sess); |
c66ac9db | 431 | |
403edd78 | 432 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db NB |
433 | if (dynamic_acl) |
434 | acl->dynamic_node_acl = 1; | |
403edd78 | 435 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db NB |
436 | return -EINVAL; |
437 | } | |
140854cb | 438 | spin_unlock_irqrestore(&tpg->session_lock, flags); |
c66ac9db NB |
439 | /* |
440 | * If the $FABRIC_MOD session for the Initiator Node ACL exists, | |
441 | * forcefully shutdown the $FABRIC_MOD session/nexus. | |
442 | */ | |
443 | if (init_sess) | |
e3d6f909 | 444 | tpg->se_tpg_tfo->close_session(init_sess); |
c66ac9db | 445 | |
bfb9035c | 446 | pr_debug("Successfully changed queue depth to: %d for Initiator" |
c66ac9db | 447 | " Node: %s on %s Target Portal Group: %u\n", queue_depth, |
e3d6f909 AG |
448 | initiatorname, tpg->se_tpg_tfo->get_fabric_name(), |
449 | tpg->se_tpg_tfo->tpg_get_tag(tpg)); | |
c66ac9db | 450 | |
403edd78 | 451 | mutex_lock(&tpg->acl_node_mutex); |
c66ac9db NB |
452 | if (dynamic_acl) |
453 | acl->dynamic_node_acl = 1; | |
403edd78 | 454 | mutex_unlock(&tpg->acl_node_mutex); |
c66ac9db NB |
455 | |
456 | return 0; | |
457 | } | |
458 | EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth); | |
459 | ||
79e62fc3 AG |
460 | /* core_tpg_set_initiator_node_tag(): |
461 | * | |
462 | * Initiator nodeacl tags are not used internally, but may be used by | |
463 | * userspace to emulate aliases or groups. | |
464 | * Returns length of newly-set tag or -EINVAL. | |
465 | */ | |
466 | int core_tpg_set_initiator_node_tag( | |
467 | struct se_portal_group *tpg, | |
468 | struct se_node_acl *acl, | |
469 | const char *new_tag) | |
470 | { | |
471 | if (strlen(new_tag) >= MAX_ACL_TAG_SIZE) | |
472 | return -EINVAL; | |
473 | ||
474 | if (!strncmp("NULL", new_tag, 4)) { | |
475 | acl->acl_tag[0] = '\0'; | |
476 | return 0; | |
477 | } | |
478 | ||
479 | return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag); | |
480 | } | |
481 | EXPORT_SYMBOL(core_tpg_set_initiator_node_tag); | |
482 | ||
5277797d NB |
483 | static void core_tpg_lun_ref_release(struct percpu_ref *ref) |
484 | { | |
485 | struct se_lun *lun = container_of(ref, struct se_lun, lun_ref); | |
486 | ||
487 | complete(&lun->lun_ref_comp); | |
488 | } | |
489 | ||
c66ac9db | 490 | int core_tpg_register( |
c66ac9db NB |
491 | struct se_wwn *se_wwn, |
492 | struct se_portal_group *se_tpg, | |
e4aae5af | 493 | int proto_id) |
c66ac9db | 494 | { |
adf653f9 CH |
495 | int ret; |
496 | ||
bc0c94b1 NB |
497 | if (!se_tpg) |
498 | return -EINVAL; | |
499 | /* | |
500 | * For the typical case where core_tpg_register() is called by a | |
501 | * fabric driver from target_core_fabric_ops->fabric_make_tpg() | |
502 | * configfs context, use the original tf_ops pointer already saved | |
503 | * by target-core in target_fabric_make_wwn(). | |
504 | * | |
505 | * Otherwise, for special cases like iscsi-target discovery TPGs | |
506 | * the caller is responsible for setting ->se_tpg_tfo ahead of | |
507 | * calling core_tpg_register(). | |
508 | */ | |
509 | if (se_wwn) | |
510 | se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops; | |
511 | ||
512 | if (!se_tpg->se_tpg_tfo) { | |
513 | pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n"); | |
514 | return -EINVAL; | |
515 | } | |
516 | ||
6bb82612 | 517 | INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist); |
e4aae5af | 518 | se_tpg->proto_id = proto_id; |
c66ac9db NB |
519 | se_tpg->se_tpg_wwn = se_wwn; |
520 | atomic_set(&se_tpg->tpg_pr_ref_count, 0); | |
521 | INIT_LIST_HEAD(&se_tpg->acl_node_list); | |
e3d6f909 | 522 | INIT_LIST_HEAD(&se_tpg->se_tpg_node); |
c66ac9db | 523 | INIT_LIST_HEAD(&se_tpg->tpg_sess_list); |
c66ac9db | 524 | spin_lock_init(&se_tpg->session_lock); |
6bb82612 | 525 | mutex_init(&se_tpg->tpg_lun_mutex); |
403edd78 | 526 | mutex_init(&se_tpg->acl_node_mutex); |
c66ac9db | 527 | |
e4aae5af | 528 | if (se_tpg->proto_id >= 0) { |
adf653f9 CH |
529 | se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0); |
530 | if (IS_ERR(se_tpg->tpg_virt_lun0)) | |
531 | return PTR_ERR(se_tpg->tpg_virt_lun0); | |
532 | ||
533 | ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0, | |
534 | TRANSPORT_LUNFLAGS_READ_ONLY, g_lun0_dev); | |
535 | if (ret < 0) { | |
536 | kfree(se_tpg->tpg_virt_lun0); | |
537 | return ret; | |
538 | } | |
c66ac9db NB |
539 | } |
540 | ||
e3d6f909 AG |
541 | spin_lock_bh(&tpg_lock); |
542 | list_add_tail(&se_tpg->se_tpg_node, &tpg_list); | |
543 | spin_unlock_bh(&tpg_lock); | |
c66ac9db | 544 | |
e4aae5af | 545 | pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, " |
bc0c94b1 NB |
546 | "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->get_fabric_name(), |
547 | se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ? | |
548 | se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL, | |
549 | se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg)); | |
c66ac9db NB |
550 | |
551 | return 0; | |
552 | } | |
553 | EXPORT_SYMBOL(core_tpg_register); | |
554 | ||
555 | int core_tpg_deregister(struct se_portal_group *se_tpg) | |
556 | { | |
e4aae5af | 557 | const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo; |
e89d15ee | 558 | struct se_node_acl *nacl, *nacl_tmp; |
22793de5 | 559 | LIST_HEAD(node_list); |
e89d15ee | 560 | |
e4aae5af CH |
561 | pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, " |
562 | "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(), | |
563 | tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL, | |
564 | se_tpg->proto_id, tfo->tpg_get_tag(se_tpg)); | |
c66ac9db | 565 | |
e3d6f909 AG |
566 | spin_lock_bh(&tpg_lock); |
567 | list_del(&se_tpg->se_tpg_node); | |
568 | spin_unlock_bh(&tpg_lock); | |
c66ac9db NB |
569 | |
570 | while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0) | |
571 | cpu_relax(); | |
403edd78 | 572 | |
22793de5 NB |
573 | mutex_lock(&se_tpg->acl_node_mutex); |
574 | list_splice_init(&se_tpg->acl_node_list, &node_list); | |
575 | mutex_unlock(&se_tpg->acl_node_mutex); | |
e89d15ee NB |
576 | /* |
577 | * Release any remaining demo-mode generated se_node_acl that have | |
578 | * not been released because of TFO->tpg_check_demo_mode_cache() == 1 | |
579 | * in transport_deregister_session(). | |
580 | */ | |
22793de5 | 581 | list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) { |
e89d15ee NB |
582 | list_del(&nacl->acl_list); |
583 | se_tpg->num_node_acls--; | |
e89d15ee NB |
584 | |
585 | core_tpg_wait_for_nacl_pr_ref(nacl); | |
586 | core_free_device_list_for_node(nacl, se_tpg); | |
144bc4c2 | 587 | kfree(nacl); |
e89d15ee | 588 | } |
c66ac9db | 589 | |
adf653f9 CH |
590 | if (se_tpg->proto_id >= 0) { |
591 | core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0); | |
592 | kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head); | |
593 | } | |
c66ac9db | 594 | |
c66ac9db NB |
595 | return 0; |
596 | } | |
597 | EXPORT_SYMBOL(core_tpg_deregister); | |
598 | ||
d344f8a1 | 599 | struct se_lun *core_tpg_alloc_lun( |
c66ac9db NB |
600 | struct se_portal_group *tpg, |
601 | u32 unpacked_lun) | |
602 | { | |
603 | struct se_lun *lun; | |
604 | ||
605 | if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) { | |
6708bb27 | 606 | pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG" |
c66ac9db | 607 | "-1: %u for Target Portal Group: %u\n", |
e3d6f909 | 608 | tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db | 609 | unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1, |
e3d6f909 | 610 | tpg->se_tpg_tfo->tpg_get_tag(tpg)); |
c66ac9db NB |
611 | return ERR_PTR(-EOVERFLOW); |
612 | } | |
613 | ||
6bb82612 NB |
614 | lun = kzalloc(sizeof(*lun), GFP_KERNEL); |
615 | if (!lun) { | |
616 | pr_err("Unable to allocate se_lun memory\n"); | |
617 | return ERR_PTR(-ENOMEM); | |
c66ac9db | 618 | } |
6bb82612 NB |
619 | lun->unpacked_lun = unpacked_lun; |
620 | lun->lun_link_magic = SE_LUN_LINK_MAGIC; | |
6bb82612 | 621 | atomic_set(&lun->lun_acl_count, 0); |
6bb82612 | 622 | init_completion(&lun->lun_ref_comp); |
adf653f9 CH |
623 | INIT_LIST_HEAD(&lun->lun_deve_list); |
624 | INIT_LIST_HEAD(&lun->lun_dev_link); | |
625 | atomic_set(&lun->lun_tg_pt_secondary_offline, 0); | |
626 | spin_lock_init(&lun->lun_deve_lock); | |
627 | mutex_init(&lun->lun_tg_pt_md_mutex); | |
628 | INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link); | |
629 | spin_lock_init(&lun->lun_tg_pt_gp_lock); | |
adf653f9 | 630 | lun->lun_tpg = tpg; |
c66ac9db NB |
631 | |
632 | return lun; | |
633 | } | |
634 | ||
d344f8a1 | 635 | int core_tpg_add_lun( |
c66ac9db NB |
636 | struct se_portal_group *tpg, |
637 | struct se_lun *lun, | |
638 | u32 lun_access, | |
340dbf72 | 639 | struct se_device *dev) |
c66ac9db | 640 | { |
e3d6f909 AG |
641 | int ret; |
642 | ||
2aad2a86 | 643 | ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0, |
a34375ef | 644 | GFP_KERNEL); |
e3d6f909 | 645 | if (ret < 0) |
adf653f9 | 646 | goto out; |
c66ac9db | 647 | |
adf653f9 CH |
648 | ret = core_alloc_rtpi(lun, dev); |
649 | if (ret) | |
650 | goto out_kill_ref; | |
651 | ||
652 | if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) && | |
653 | !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) | |
654 | target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp); | |
5277797d | 655 | |
6bb82612 | 656 | mutex_lock(&tpg->tpg_lun_mutex); |
adf653f9 | 657 | |
adf653f9 | 658 | spin_lock(&dev->se_port_lock); |
4cc987ea | 659 | lun->lun_index = dev->dev_index; |
adf653f9 CH |
660 | rcu_assign_pointer(lun->lun_se_dev, dev); |
661 | dev->export_count++; | |
662 | list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list); | |
663 | spin_unlock(&dev->se_port_lock); | |
664 | ||
c66ac9db | 665 | lun->lun_access = lun_access; |
6bb82612 NB |
666 | if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) |
667 | hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist); | |
668 | mutex_unlock(&tpg->tpg_lun_mutex); | |
c66ac9db NB |
669 | |
670 | return 0; | |
adf653f9 CH |
671 | |
672 | out_kill_ref: | |
673 | percpu_ref_exit(&lun->lun_ref); | |
674 | out: | |
675 | return ret; | |
c66ac9db NB |
676 | } |
677 | ||
cd9d7cba | 678 | void core_tpg_remove_lun( |
c66ac9db NB |
679 | struct se_portal_group *tpg, |
680 | struct se_lun *lun) | |
681 | { | |
4cc987ea NB |
682 | /* |
683 | * rcu_dereference_raw protected by se_lun->lun_group symlink | |
684 | * reference to se_device->dev_group. | |
685 | */ | |
686 | struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev); | |
6bb82612 | 687 | |
4a9a6c8d | 688 | core_clear_lun_from_tpg(lun, tpg); |
9e37d042 NB |
689 | /* |
690 | * Wait for any active I/O references to percpu se_lun->lun_ref to | |
691 | * be released. Also, se_lun->lun_ref is now used by PR and ALUA | |
692 | * logic when referencing a remote target port during ALL_TGT_PT=1 | |
693 | * and generating UNIT_ATTENTIONs for ALUA access state transition. | |
694 | */ | |
4a9a6c8d | 695 | transport_clear_lun_ref(lun); |
c66ac9db | 696 | |
6bb82612 | 697 | mutex_lock(&tpg->tpg_lun_mutex); |
adf653f9 | 698 | if (lun->lun_se_dev) { |
adf653f9 CH |
699 | target_detach_tg_pt_gp(lun); |
700 | ||
701 | spin_lock(&dev->se_port_lock); | |
702 | list_del(&lun->lun_dev_link); | |
703 | dev->export_count--; | |
704 | rcu_assign_pointer(lun->lun_se_dev, NULL); | |
705 | spin_unlock(&dev->se_port_lock); | |
706 | } | |
6bb82612 NB |
707 | if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) |
708 | hlist_del_rcu(&lun->link); | |
709 | mutex_unlock(&tpg->tpg_lun_mutex); | |
c66ac9db | 710 | |
9a1049da | 711 | percpu_ref_exit(&lun->lun_ref); |
c66ac9db | 712 | } |