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