1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_device.c (based on iscsi_target_device.c)
4 *
5 * This file contains the TCM Virtual Device and Disk Transport
6 * agnostic related functions.
7 *
8 * (c) Copyright 2003-2013 Datera, Inc.
9 *
10 * Nicholas A. Bellinger <nab@kernel.org>
11 *
12 ******************************************************************************/
13
14 #include <linux/net.h>
15 #include <linux/string.h>
16 #include <linux/delay.h>
17 #include <linux/timer.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/kthread.h>
21 #include <linux/in.h>
22 #include <linux/export.h>
23 #include <linux/t10-pi.h>
24 #include <asm/unaligned.h>
25 #include <net/sock.h>
26 #include <net/tcp.h>
27 #include <scsi/scsi_common.h>
28 #include <scsi/scsi_proto.h>
29
30 #include <target/target_core_base.h>
31 #include <target/target_core_backend.h>
32 #include <target/target_core_fabric.h>
33
34 #include "target_core_internal.h"
35 #include "target_core_alua.h"
36 #include "target_core_pr.h"
37 #include "target_core_ua.h"
38
39 static DEFINE_MUTEX(device_mutex);
40 static LIST_HEAD(device_list);
41 static DEFINE_IDR(devices_idr);
42
43 static struct se_hba *lun0_hba;
44 /* not static, needed by tpg.c */
45 struct se_device *g_lun0_dev;
46
47 sense_reason_t
transport_lookup_cmd_lun(struct se_cmd * se_cmd,u64 unpacked_lun)48 transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
49 {
50 struct se_lun *se_lun = NULL;
51 struct se_session *se_sess = se_cmd->se_sess;
52 struct se_node_acl *nacl = se_sess->se_node_acl;
53 struct se_dev_entry *deve;
54 sense_reason_t ret = TCM_NO_SENSE;
55
56 rcu_read_lock();
57 deve = target_nacl_find_deve(nacl, unpacked_lun);
58 if (deve) {
59 atomic_long_inc(&deve->total_cmds);
60
61 if (se_cmd->data_direction == DMA_TO_DEVICE)
62 atomic_long_add(se_cmd->data_length,
63 &deve->write_bytes);
64 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
65 atomic_long_add(se_cmd->data_length,
66 &deve->read_bytes);
67
68 se_lun = rcu_dereference(deve->se_lun);
69
70 if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
71 se_lun = NULL;
72 goto out_unlock;
73 }
74
75 se_cmd->se_lun = se_lun;
76 se_cmd->pr_res_key = deve->pr_res_key;
77 se_cmd->orig_fe_lun = unpacked_lun;
78 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
79 se_cmd->lun_ref_active = true;
80
81 if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
82 deve->lun_access_ro) {
83 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
84 " Access for 0x%08llx\n",
85 se_cmd->se_tfo->fabric_name,
86 unpacked_lun);
87 rcu_read_unlock();
88 ret = TCM_WRITE_PROTECTED;
89 goto ref_dev;
90 }
91 }
92 out_unlock:
93 rcu_read_unlock();
94
95 if (!se_lun) {
96 /*
97 * Use the se_portal_group->tpg_virt_lun0 to allow for
98 * REPORT_LUNS, et al to be returned when no active
99 * MappedLUN=0 exists for this Initiator Port.
100 */
101 if (unpacked_lun != 0) {
102 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
103 " Access for 0x%08llx\n",
104 se_cmd->se_tfo->fabric_name,
105 unpacked_lun);
106 return TCM_NON_EXISTENT_LUN;
107 }
108
109 se_lun = se_sess->se_tpg->tpg_virt_lun0;
110 se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
111 se_cmd->orig_fe_lun = 0;
112 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
113
114 percpu_ref_get(&se_lun->lun_ref);
115 se_cmd->lun_ref_active = true;
116
117 /*
118 * Force WRITE PROTECT for virtual LUN 0
119 */
120 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
121 (se_cmd->data_direction != DMA_NONE)) {
122 ret = TCM_WRITE_PROTECTED;
123 goto ref_dev;
124 }
125 }
126 /*
127 * RCU reference protected by percpu se_lun->lun_ref taken above that
128 * must drop to zero (including initial reference) before this se_lun
129 * pointer can be kfree_rcu() by the final se_lun->lun_group put via
130 * target_core_fabric_configfs.c:target_fabric_port_release
131 */
132 ref_dev:
133 se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
134 atomic_long_inc(&se_cmd->se_dev->num_cmds);
135
136 if (se_cmd->data_direction == DMA_TO_DEVICE)
137 atomic_long_add(se_cmd->data_length,
138 &se_cmd->se_dev->write_bytes);
139 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
140 atomic_long_add(se_cmd->data_length,
141 &se_cmd->se_dev->read_bytes);
142
143 return ret;
144 }
145 EXPORT_SYMBOL(transport_lookup_cmd_lun);
146
transport_lookup_tmr_lun(struct se_cmd * se_cmd,u64 unpacked_lun)147 int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
148 {
149 struct se_dev_entry *deve;
150 struct se_lun *se_lun = NULL;
151 struct se_session *se_sess = se_cmd->se_sess;
152 struct se_node_acl *nacl = se_sess->se_node_acl;
153 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
154
155 rcu_read_lock();
156 deve = target_nacl_find_deve(nacl, unpacked_lun);
157 if (deve) {
158 se_lun = rcu_dereference(deve->se_lun);
159
160 if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
161 se_lun = NULL;
162 goto out_unlock;
163 }
164
165 se_cmd->se_lun = se_lun;
166 se_cmd->pr_res_key = deve->pr_res_key;
167 se_cmd->orig_fe_lun = unpacked_lun;
168 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
169 se_cmd->lun_ref_active = true;
170 }
171 out_unlock:
172 rcu_read_unlock();
173
174 if (!se_lun) {
175 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
176 " Access for 0x%08llx\n",
177 se_cmd->se_tfo->fabric_name,
178 unpacked_lun);
179 return -ENODEV;
180 }
181 se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
182 se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
183
184 return 0;
185 }
186 EXPORT_SYMBOL(transport_lookup_tmr_lun);
187
target_lun_is_rdonly(struct se_cmd * cmd)188 bool target_lun_is_rdonly(struct se_cmd *cmd)
189 {
190 struct se_session *se_sess = cmd->se_sess;
191 struct se_dev_entry *deve;
192 bool ret;
193
194 rcu_read_lock();
195 deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
196 ret = deve && deve->lun_access_ro;
197 rcu_read_unlock();
198
199 return ret;
200 }
201 EXPORT_SYMBOL(target_lun_is_rdonly);
202
203 /*
204 * This function is called from core_scsi3_emulate_pro_register_and_move()
205 * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
206 * when a matching rtpi is found.
207 */
core_get_se_deve_from_rtpi(struct se_node_acl * nacl,u16 rtpi)208 struct se_dev_entry *core_get_se_deve_from_rtpi(
209 struct se_node_acl *nacl,
210 u16 rtpi)
211 {
212 struct se_dev_entry *deve;
213 struct se_lun *lun;
214 struct se_portal_group *tpg = nacl->se_tpg;
215
216 rcu_read_lock();
217 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
218 lun = rcu_dereference(deve->se_lun);
219 if (!lun) {
220 pr_err("%s device entries device pointer is"
221 " NULL, but Initiator has access.\n",
222 tpg->se_tpg_tfo->fabric_name);
223 continue;
224 }
225 if (lun->lun_rtpi != rtpi)
226 continue;
227
228 kref_get(&deve->pr_kref);
229 rcu_read_unlock();
230
231 return deve;
232 }
233 rcu_read_unlock();
234
235 return NULL;
236 }
237
core_free_device_list_for_node(struct se_node_acl * nacl,struct se_portal_group * tpg)238 void core_free_device_list_for_node(
239 struct se_node_acl *nacl,
240 struct se_portal_group *tpg)
241 {
242 struct se_dev_entry *deve;
243
244 mutex_lock(&nacl->lun_entry_mutex);
245 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
246 struct se_lun *lun = rcu_dereference_check(deve->se_lun,
247 lockdep_is_held(&nacl->lun_entry_mutex));
248 core_disable_device_list_for_node(lun, deve, nacl, tpg);
249 }
250 mutex_unlock(&nacl->lun_entry_mutex);
251 }
252
core_update_device_list_access(u64 mapped_lun,bool lun_access_ro,struct se_node_acl * nacl)253 void core_update_device_list_access(
254 u64 mapped_lun,
255 bool lun_access_ro,
256 struct se_node_acl *nacl)
257 {
258 struct se_dev_entry *deve;
259
260 mutex_lock(&nacl->lun_entry_mutex);
261 deve = target_nacl_find_deve(nacl, mapped_lun);
262 if (deve)
263 deve->lun_access_ro = lun_access_ro;
264 mutex_unlock(&nacl->lun_entry_mutex);
265 }
266
267 /*
268 * Called with rcu_read_lock or nacl->device_list_lock held.
269 */
target_nacl_find_deve(struct se_node_acl * nacl,u64 mapped_lun)270 struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
271 {
272 struct se_dev_entry *deve;
273
274 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
275 if (deve->mapped_lun == mapped_lun)
276 return deve;
277
278 return NULL;
279 }
280 EXPORT_SYMBOL(target_nacl_find_deve);
281
target_pr_kref_release(struct kref * kref)282 void target_pr_kref_release(struct kref *kref)
283 {
284 struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
285 pr_kref);
286 complete(&deve->pr_comp);
287 }
288
289 static void
target_luns_data_has_changed(struct se_node_acl * nacl,struct se_dev_entry * new,bool skip_new)290 target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
291 bool skip_new)
292 {
293 struct se_dev_entry *tmp;
294
295 rcu_read_lock();
296 hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
297 if (skip_new && tmp == new)
298 continue;
299 core_scsi3_ua_allocate(tmp, 0x3F,
300 ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
301 }
302 rcu_read_unlock();
303 }
304
core_enable_device_list_for_node(struct se_lun * lun,struct se_lun_acl * lun_acl,u64 mapped_lun,bool lun_access_ro,struct se_node_acl * nacl,struct se_portal_group * tpg)305 int core_enable_device_list_for_node(
306 struct se_lun *lun,
307 struct se_lun_acl *lun_acl,
308 u64 mapped_lun,
309 bool lun_access_ro,
310 struct se_node_acl *nacl,
311 struct se_portal_group *tpg)
312 {
313 struct se_dev_entry *orig, *new;
314
315 new = kzalloc(sizeof(*new), GFP_KERNEL);
316 if (!new) {
317 pr_err("Unable to allocate se_dev_entry memory\n");
318 return -ENOMEM;
319 }
320
321 spin_lock_init(&new->ua_lock);
322 INIT_LIST_HEAD(&new->ua_list);
323 INIT_LIST_HEAD(&new->lun_link);
324
325 new->mapped_lun = mapped_lun;
326 kref_init(&new->pr_kref);
327 init_completion(&new->pr_comp);
328
329 new->lun_access_ro = lun_access_ro;
330 new->creation_time = get_jiffies_64();
331 new->attach_count++;
332
333 mutex_lock(&nacl->lun_entry_mutex);
334 orig = target_nacl_find_deve(nacl, mapped_lun);
335 if (orig && orig->se_lun) {
336 struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
337 lockdep_is_held(&nacl->lun_entry_mutex));
338
339 if (orig_lun != lun) {
340 pr_err("Existing orig->se_lun doesn't match new lun"
341 " for dynamic -> explicit NodeACL conversion:"
342 " %s\n", nacl->initiatorname);
343 mutex_unlock(&nacl->lun_entry_mutex);
344 kfree(new);
345 return -EINVAL;
346 }
347 if (orig->se_lun_acl != NULL) {
348 pr_warn_ratelimited("Detected existing explicit"
349 " se_lun_acl->se_lun_group reference for %s"
350 " mapped_lun: %llu, failing\n",
351 nacl->initiatorname, mapped_lun);
352 mutex_unlock(&nacl->lun_entry_mutex);
353 kfree(new);
354 return -EINVAL;
355 }
356
357 rcu_assign_pointer(new->se_lun, lun);
358 rcu_assign_pointer(new->se_lun_acl, lun_acl);
359 hlist_del_rcu(&orig->link);
360 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
361 mutex_unlock(&nacl->lun_entry_mutex);
362
363 spin_lock(&lun->lun_deve_lock);
364 list_del(&orig->lun_link);
365 list_add_tail(&new->lun_link, &lun->lun_deve_list);
366 spin_unlock(&lun->lun_deve_lock);
367
368 kref_put(&orig->pr_kref, target_pr_kref_release);
369 wait_for_completion(&orig->pr_comp);
370
371 target_luns_data_has_changed(nacl, new, true);
372 kfree_rcu(orig, rcu_head);
373 return 0;
374 }
375
376 rcu_assign_pointer(new->se_lun, lun);
377 rcu_assign_pointer(new->se_lun_acl, lun_acl);
378 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
379 mutex_unlock(&nacl->lun_entry_mutex);
380
381 spin_lock(&lun->lun_deve_lock);
382 list_add_tail(&new->lun_link, &lun->lun_deve_list);
383 spin_unlock(&lun->lun_deve_lock);
384
385 target_luns_data_has_changed(nacl, new, true);
386 return 0;
387 }
388
core_disable_device_list_for_node(struct se_lun * lun,struct se_dev_entry * orig,struct se_node_acl * nacl,struct se_portal_group * tpg)389 void core_disable_device_list_for_node(
390 struct se_lun *lun,
391 struct se_dev_entry *orig,
392 struct se_node_acl *nacl,
393 struct se_portal_group *tpg)
394 {
395 /*
396 * rcu_dereference_raw protected by se_lun->lun_group symlink
397 * reference to se_device->dev_group.
398 */
399 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
400
401 lockdep_assert_held(&nacl->lun_entry_mutex);
402
403 /*
404 * If the MappedLUN entry is being disabled, the entry in
405 * lun->lun_deve_list must be removed now before clearing the
406 * struct se_dev_entry pointers below as logic in
407 * core_alua_do_transition_tg_pt() depends on these being present.
408 *
409 * deve->se_lun_acl will be NULL for demo-mode created LUNs
410 * that have not been explicitly converted to MappedLUNs ->
411 * struct se_lun_acl, but we remove deve->lun_link from
412 * lun->lun_deve_list. This also means that active UAs and
413 * NodeACL context specific PR metadata for demo-mode
414 * MappedLUN *deve will be released below..
415 */
416 spin_lock(&lun->lun_deve_lock);
417 list_del(&orig->lun_link);
418 spin_unlock(&lun->lun_deve_lock);
419 /*
420 * Disable struct se_dev_entry LUN ACL mapping
421 */
422 core_scsi3_ua_release_all(orig);
423
424 hlist_del_rcu(&orig->link);
425 clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
426 orig->lun_access_ro = false;
427 orig->creation_time = 0;
428 orig->attach_count--;
429 /*
430 * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
431 * or REGISTER_AND_MOVE PR operation to complete.
432 */
433 kref_put(&orig->pr_kref, target_pr_kref_release);
434 wait_for_completion(&orig->pr_comp);
435
436 rcu_assign_pointer(orig->se_lun, NULL);
437 rcu_assign_pointer(orig->se_lun_acl, NULL);
438
439 kfree_rcu(orig, rcu_head);
440
441 core_scsi3_free_pr_reg_from_nacl(dev, nacl);
442 target_luns_data_has_changed(nacl, NULL, false);
443 }
444
445 /* core_clear_lun_from_tpg():
446 *
447 *
448 */
core_clear_lun_from_tpg(struct se_lun * lun,struct se_portal_group * tpg)449 void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
450 {
451 struct se_node_acl *nacl;
452 struct se_dev_entry *deve;
453
454 mutex_lock(&tpg->acl_node_mutex);
455 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
456
457 mutex_lock(&nacl->lun_entry_mutex);
458 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
459 struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
460 lockdep_is_held(&nacl->lun_entry_mutex));
461
462 if (lun != tmp_lun)
463 continue;
464
465 core_disable_device_list_for_node(lun, deve, nacl, tpg);
466 }
467 mutex_unlock(&nacl->lun_entry_mutex);
468 }
469 mutex_unlock(&tpg->acl_node_mutex);
470 }
471
core_alloc_rtpi(struct se_lun * lun,struct se_device * dev)472 int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
473 {
474 struct se_lun *tmp;
475
476 spin_lock(&dev->se_port_lock);
477 if (dev->export_count == 0x0000ffff) {
478 pr_warn("Reached dev->dev_port_count =="
479 " 0x0000ffff\n");
480 spin_unlock(&dev->se_port_lock);
481 return -ENOSPC;
482 }
483 again:
484 /*
485 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
486 * Here is the table from spc4r17 section 7.7.3.8.
487 *
488 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
489 *
490 * Code Description
491 * 0h Reserved
492 * 1h Relative port 1, historically known as port A
493 * 2h Relative port 2, historically known as port B
494 * 3h to FFFFh Relative port 3 through 65 535
495 */
496 lun->lun_rtpi = dev->dev_rpti_counter++;
497 if (!lun->lun_rtpi)
498 goto again;
499
500 list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
501 /*
502 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
503 * for 16-bit wrap..
504 */
505 if (lun->lun_rtpi == tmp->lun_rtpi)
506 goto again;
507 }
508 spin_unlock(&dev->se_port_lock);
509
510 return 0;
511 }
512
se_release_vpd_for_dev(struct se_device * dev)513 static void se_release_vpd_for_dev(struct se_device *dev)
514 {
515 struct t10_vpd *vpd, *vpd_tmp;
516
517 spin_lock(&dev->t10_wwn.t10_vpd_lock);
518 list_for_each_entry_safe(vpd, vpd_tmp,
519 &dev->t10_wwn.t10_vpd_list, vpd_list) {
520 list_del(&vpd->vpd_list);
521 kfree(vpd);
522 }
523 spin_unlock(&dev->t10_wwn.t10_vpd_lock);
524 }
525
se_dev_align_max_sectors(u32 max_sectors,u32 block_size)526 static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
527 {
528 u32 aligned_max_sectors;
529 u32 alignment;
530 /*
531 * Limit max_sectors to a PAGE_SIZE aligned value for modern
532 * transport_allocate_data_tasks() operation.
533 */
534 alignment = max(1ul, PAGE_SIZE / block_size);
535 aligned_max_sectors = rounddown(max_sectors, alignment);
536
537 if (max_sectors != aligned_max_sectors)
538 pr_info("Rounding down aligned max_sectors from %u to %u\n",
539 max_sectors, aligned_max_sectors);
540
541 return aligned_max_sectors;
542 }
543
core_dev_add_lun(struct se_portal_group * tpg,struct se_device * dev,struct se_lun * lun)544 int core_dev_add_lun(
545 struct se_portal_group *tpg,
546 struct se_device *dev,
547 struct se_lun *lun)
548 {
549 int rc;
550
551 rc = core_tpg_add_lun(tpg, lun, false, dev);
552 if (rc < 0)
553 return rc;
554
555 pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
556 " CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
557 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
558 tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
559 /*
560 * Update LUN maps for dynamically added initiators when
561 * generate_node_acl is enabled.
562 */
563 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
564 struct se_node_acl *acl;
565
566 mutex_lock(&tpg->acl_node_mutex);
567 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
568 if (acl->dynamic_node_acl &&
569 (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
570 !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
571 core_tpg_add_node_to_devs(acl, tpg, lun);
572 }
573 }
574 mutex_unlock(&tpg->acl_node_mutex);
575 }
576
577 return 0;
578 }
579
580 /* core_dev_del_lun():
581 *
582 *
583 */
core_dev_del_lun(struct se_portal_group * tpg,struct se_lun * lun)584 void core_dev_del_lun(
585 struct se_portal_group *tpg,
586 struct se_lun *lun)
587 {
588 pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
589 " device object\n", tpg->se_tpg_tfo->fabric_name,
590 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
591 tpg->se_tpg_tfo->fabric_name);
592
593 core_tpg_remove_lun(tpg, lun);
594 }
595
core_dev_init_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_node_acl * nacl,u64 mapped_lun,int * ret)596 struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
597 struct se_portal_group *tpg,
598 struct se_node_acl *nacl,
599 u64 mapped_lun,
600 int *ret)
601 {
602 struct se_lun_acl *lacl;
603
604 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
605 pr_err("%s InitiatorName exceeds maximum size.\n",
606 tpg->se_tpg_tfo->fabric_name);
607 *ret = -EOVERFLOW;
608 return NULL;
609 }
610 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
611 if (!lacl) {
612 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
613 *ret = -ENOMEM;
614 return NULL;
615 }
616
617 lacl->mapped_lun = mapped_lun;
618 lacl->se_lun_nacl = nacl;
619
620 return lacl;
621 }
622
core_dev_add_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_lun_acl * lacl,struct se_lun * lun,bool lun_access_ro)623 int core_dev_add_initiator_node_lun_acl(
624 struct se_portal_group *tpg,
625 struct se_lun_acl *lacl,
626 struct se_lun *lun,
627 bool lun_access_ro)
628 {
629 struct se_node_acl *nacl = lacl->se_lun_nacl;
630 /*
631 * rcu_dereference_raw protected by se_lun->lun_group symlink
632 * reference to se_device->dev_group.
633 */
634 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
635
636 if (!nacl)
637 return -EINVAL;
638
639 if (lun->lun_access_ro)
640 lun_access_ro = true;
641
642 lacl->se_lun = lun;
643
644 if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
645 lun_access_ro, nacl, tpg) < 0)
646 return -EINVAL;
647
648 pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
649 " InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
650 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
651 lun_access_ro ? "RO" : "RW",
652 nacl->initiatorname);
653 /*
654 * Check to see if there are any existing persistent reservation APTPL
655 * pre-registrations that need to be enabled for this LUN ACL..
656 */
657 core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
658 lacl->mapped_lun);
659 return 0;
660 }
661
core_dev_del_initiator_node_lun_acl(struct se_lun * lun,struct se_lun_acl * lacl)662 int core_dev_del_initiator_node_lun_acl(
663 struct se_lun *lun,
664 struct se_lun_acl *lacl)
665 {
666 struct se_portal_group *tpg = lun->lun_tpg;
667 struct se_node_acl *nacl;
668 struct se_dev_entry *deve;
669
670 nacl = lacl->se_lun_nacl;
671 if (!nacl)
672 return -EINVAL;
673
674 mutex_lock(&nacl->lun_entry_mutex);
675 deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
676 if (deve)
677 core_disable_device_list_for_node(lun, deve, nacl, tpg);
678 mutex_unlock(&nacl->lun_entry_mutex);
679
680 pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
681 " InitiatorNode: %s Mapped LUN: %llu\n",
682 tpg->se_tpg_tfo->fabric_name,
683 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
684 nacl->initiatorname, lacl->mapped_lun);
685
686 return 0;
687 }
688
core_dev_free_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_lun_acl * lacl)689 void core_dev_free_initiator_node_lun_acl(
690 struct se_portal_group *tpg,
691 struct se_lun_acl *lacl)
692 {
693 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
694 " Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
695 tpg->se_tpg_tfo->tpg_get_tag(tpg),
696 tpg->se_tpg_tfo->fabric_name,
697 lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
698
699 kfree(lacl);
700 }
701
scsi_dump_inquiry(struct se_device * dev)702 static void scsi_dump_inquiry(struct se_device *dev)
703 {
704 struct t10_wwn *wwn = &dev->t10_wwn;
705 int device_type = dev->transport->get_device_type(dev);
706
707 /*
708 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
709 */
710 pr_debug(" Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
711 wwn->vendor);
712 pr_debug(" Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
713 wwn->model);
714 pr_debug(" Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
715 wwn->revision);
716 pr_debug(" Type: %s ", scsi_device_type(device_type));
717 }
718
target_alloc_device(struct se_hba * hba,const char * name)719 struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
720 {
721 struct se_device *dev;
722 struct se_lun *xcopy_lun;
723
724 dev = hba->backend->ops->alloc_device(hba, name);
725 if (!dev)
726 return NULL;
727
728 dev->se_hba = hba;
729 dev->transport = hba->backend->ops;
730 dev->prot_length = sizeof(struct t10_pi_tuple);
731 dev->hba_index = hba->hba_index;
732
733 INIT_LIST_HEAD(&dev->dev_sep_list);
734 INIT_LIST_HEAD(&dev->dev_tmr_list);
735 INIT_LIST_HEAD(&dev->delayed_cmd_list);
736 INIT_LIST_HEAD(&dev->state_list);
737 INIT_LIST_HEAD(&dev->qf_cmd_list);
738 spin_lock_init(&dev->execute_task_lock);
739 spin_lock_init(&dev->delayed_cmd_lock);
740 spin_lock_init(&dev->dev_reservation_lock);
741 spin_lock_init(&dev->se_port_lock);
742 spin_lock_init(&dev->se_tmr_lock);
743 spin_lock_init(&dev->qf_cmd_lock);
744 sema_init(&dev->caw_sem, 1);
745 INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
746 spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
747 INIT_LIST_HEAD(&dev->t10_pr.registration_list);
748 INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
749 spin_lock_init(&dev->t10_pr.registration_lock);
750 spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
751 INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
752 spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
753 INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
754 spin_lock_init(&dev->t10_alua.lba_map_lock);
755
756 INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
757
758 dev->t10_wwn.t10_dev = dev;
759 dev->t10_alua.t10_dev = dev;
760
761 dev->dev_attrib.da_dev = dev;
762 dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
763 dev->dev_attrib.emulate_dpo = 1;
764 dev->dev_attrib.emulate_fua_write = 1;
765 dev->dev_attrib.emulate_fua_read = 1;
766 dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
767 dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
768 dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
769 dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
770 dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
771 dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
772 dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
773 dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
774 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
775 dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
776 dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
777 dev->dev_attrib.is_nonrot = DA_IS_NONROT;
778 dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
779 dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
780 dev->dev_attrib.max_unmap_block_desc_count =
781 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
782 dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
783 dev->dev_attrib.unmap_granularity_alignment =
784 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
785 dev->dev_attrib.unmap_zeroes_data =
786 DA_UNMAP_ZEROES_DATA_DEFAULT;
787 dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
788
789 xcopy_lun = &dev->xcopy_lun;
790 rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
791 init_completion(&xcopy_lun->lun_shutdown_comp);
792 INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
793 INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
794 mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
795 xcopy_lun->lun_tpg = &xcopy_pt_tpg;
796
797 /* Preload the default INQUIRY const values */
798 strlcpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
799 strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
800 sizeof(dev->t10_wwn.model));
801 strlcpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
802 sizeof(dev->t10_wwn.revision));
803
804 return dev;
805 }
806
807 /*
808 * Check if the underlying struct block_device request_queue supports
809 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
810 * in ATA and we need to set TPE=1
811 */
target_configure_unmap_from_queue(struct se_dev_attrib * attrib,struct request_queue * q)812 bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
813 struct request_queue *q)
814 {
815 int block_size = queue_logical_block_size(q);
816
817 if (!blk_queue_discard(q))
818 return false;
819
820 attrib->max_unmap_lba_count =
821 q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
822 /*
823 * Currently hardcoded to 1 in Linux/SCSI code..
824 */
825 attrib->max_unmap_block_desc_count = 1;
826 attrib->unmap_granularity = q->limits.discard_granularity / block_size;
827 attrib->unmap_granularity_alignment = q->limits.discard_alignment /
828 block_size;
829 attrib->unmap_zeroes_data = (q->limits.max_write_zeroes_sectors);
830 return true;
831 }
832 EXPORT_SYMBOL(target_configure_unmap_from_queue);
833
834 /*
835 * Convert from blocksize advertised to the initiator to the 512 byte
836 * units unconditionally used by the Linux block layer.
837 */
target_to_linux_sector(struct se_device * dev,sector_t lb)838 sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
839 {
840 switch (dev->dev_attrib.block_size) {
841 case 4096:
842 return lb << 3;
843 case 2048:
844 return lb << 2;
845 case 1024:
846 return lb << 1;
847 default:
848 return lb;
849 }
850 }
851 EXPORT_SYMBOL(target_to_linux_sector);
852
853 struct devices_idr_iter {
854 int (*fn)(struct se_device *dev, void *data);
855 void *data;
856 };
857
target_devices_idr_iter(int id,void * p,void * data)858 static int target_devices_idr_iter(int id, void *p, void *data)
859 __must_hold(&device_mutex)
860 {
861 struct devices_idr_iter *iter = data;
862 struct se_device *dev = p;
863 struct config_item *item;
864 int ret;
865
866 /*
867 * We add the device early to the idr, so it can be used
868 * by backend modules during configuration. We do not want
869 * to allow other callers to access partially setup devices,
870 * so we skip them here.
871 */
872 if (!target_dev_configured(dev))
873 return 0;
874
875 item = config_item_get_unless_zero(&dev->dev_group.cg_item);
876 if (!item)
877 return 0;
878 mutex_unlock(&device_mutex);
879
880 ret = iter->fn(dev, iter->data);
881 config_item_put(item);
882
883 mutex_lock(&device_mutex);
884 return ret;
885 }
886
887 /**
888 * target_for_each_device - iterate over configured devices
889 * @fn: iterator function
890 * @data: pointer to data that will be passed to fn
891 *
892 * fn must return 0 to continue looping over devices. non-zero will break
893 * from the loop and return that value to the caller.
894 */
target_for_each_device(int (* fn)(struct se_device * dev,void * data),void * data)895 int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
896 void *data)
897 {
898 struct devices_idr_iter iter = { .fn = fn, .data = data };
899 int ret;
900
901 mutex_lock(&device_mutex);
902 ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
903 mutex_unlock(&device_mutex);
904 return ret;
905 }
906
target_configure_device(struct se_device * dev)907 int target_configure_device(struct se_device *dev)
908 {
909 struct se_hba *hba = dev->se_hba;
910 int ret, id;
911
912 if (target_dev_configured(dev)) {
913 pr_err("se_dev->se_dev_ptr already set for storage"
914 " object\n");
915 return -EEXIST;
916 }
917
918 /*
919 * Add early so modules like tcmu can use during its
920 * configuration.
921 */
922 mutex_lock(&device_mutex);
923 /*
924 * Use cyclic to try and avoid collisions with devices
925 * that were recently removed.
926 */
927 id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
928 mutex_unlock(&device_mutex);
929 if (id < 0) {
930 ret = -ENOMEM;
931 goto out;
932 }
933 dev->dev_index = id;
934
935 ret = dev->transport->configure_device(dev);
936 if (ret)
937 goto out_free_index;
938 /*
939 * XXX: there is not much point to have two different values here..
940 */
941 dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
942 dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
943
944 /*
945 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
946 */
947 dev->dev_attrib.hw_max_sectors =
948 se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
949 dev->dev_attrib.hw_block_size);
950 dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
951
952 dev->creation_time = get_jiffies_64();
953
954 ret = core_setup_alua(dev);
955 if (ret)
956 goto out_destroy_device;
957
958 /*
959 * Setup work_queue for QUEUE_FULL
960 */
961 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
962
963 scsi_dump_inquiry(dev);
964
965 spin_lock(&hba->device_lock);
966 hba->dev_count++;
967 spin_unlock(&hba->device_lock);
968
969 dev->dev_flags |= DF_CONFIGURED;
970
971 return 0;
972
973 out_destroy_device:
974 dev->transport->destroy_device(dev);
975 out_free_index:
976 mutex_lock(&device_mutex);
977 idr_remove(&devices_idr, dev->dev_index);
978 mutex_unlock(&device_mutex);
979 out:
980 se_release_vpd_for_dev(dev);
981 return ret;
982 }
983
target_free_device(struct se_device * dev)984 void target_free_device(struct se_device *dev)
985 {
986 struct se_hba *hba = dev->se_hba;
987
988 WARN_ON(!list_empty(&dev->dev_sep_list));
989
990 if (target_dev_configured(dev)) {
991 dev->transport->destroy_device(dev);
992
993 mutex_lock(&device_mutex);
994 idr_remove(&devices_idr, dev->dev_index);
995 mutex_unlock(&device_mutex);
996
997 spin_lock(&hba->device_lock);
998 hba->dev_count--;
999 spin_unlock(&hba->device_lock);
1000 }
1001
1002 core_alua_free_lu_gp_mem(dev);
1003 core_alua_set_lba_map(dev, NULL, 0, 0);
1004 core_scsi3_free_all_registrations(dev);
1005 se_release_vpd_for_dev(dev);
1006
1007 if (dev->transport->free_prot)
1008 dev->transport->free_prot(dev);
1009
1010 dev->transport->free_device(dev);
1011 }
1012
core_dev_setup_virtual_lun0(void)1013 int core_dev_setup_virtual_lun0(void)
1014 {
1015 struct se_hba *hba;
1016 struct se_device *dev;
1017 char buf[] = "rd_pages=8,rd_nullio=1";
1018 int ret;
1019
1020 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1021 if (IS_ERR(hba))
1022 return PTR_ERR(hba);
1023
1024 dev = target_alloc_device(hba, "virt_lun0");
1025 if (!dev) {
1026 ret = -ENOMEM;
1027 goto out_free_hba;
1028 }
1029
1030 hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
1031
1032 ret = target_configure_device(dev);
1033 if (ret)
1034 goto out_free_se_dev;
1035
1036 lun0_hba = hba;
1037 g_lun0_dev = dev;
1038 return 0;
1039
1040 out_free_se_dev:
1041 target_free_device(dev);
1042 out_free_hba:
1043 core_delete_hba(hba);
1044 return ret;
1045 }
1046
1047
core_dev_release_virtual_lun0(void)1048 void core_dev_release_virtual_lun0(void)
1049 {
1050 struct se_hba *hba = lun0_hba;
1051
1052 if (!hba)
1053 return;
1054
1055 if (g_lun0_dev)
1056 target_free_device(g_lun0_dev);
1057 core_delete_hba(hba);
1058 }
1059
1060 /*
1061 * Common CDB parsing for kernel and user passthrough.
1062 */
1063 sense_reason_t
passthrough_parse_cdb(struct se_cmd * cmd,sense_reason_t (* exec_cmd)(struct se_cmd * cmd))1064 passthrough_parse_cdb(struct se_cmd *cmd,
1065 sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
1066 {
1067 unsigned char *cdb = cmd->t_task_cdb;
1068 struct se_device *dev = cmd->se_dev;
1069 unsigned int size;
1070
1071 /*
1072 * For REPORT LUNS we always need to emulate the response, for everything
1073 * else, pass it up.
1074 */
1075 if (cdb[0] == REPORT_LUNS) {
1076 cmd->execute_cmd = spc_emulate_report_luns;
1077 return TCM_NO_SENSE;
1078 }
1079
1080 /*
1081 * With emulate_pr disabled, all reservation requests should fail,
1082 * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
1083 */
1084 if (!dev->dev_attrib.emulate_pr &&
1085 ((cdb[0] == PERSISTENT_RESERVE_IN) ||
1086 (cdb[0] == PERSISTENT_RESERVE_OUT) ||
1087 (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
1088 (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
1089 return TCM_UNSUPPORTED_SCSI_OPCODE;
1090 }
1091
1092 /*
1093 * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
1094 * emulate the response, since tcmu does not have the information
1095 * required to process these commands.
1096 */
1097 if (!(dev->transport->transport_flags &
1098 TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1099 if (cdb[0] == PERSISTENT_RESERVE_IN) {
1100 cmd->execute_cmd = target_scsi3_emulate_pr_in;
1101 size = get_unaligned_be16(&cdb[7]);
1102 return target_cmd_size_check(cmd, size);
1103 }
1104 if (cdb[0] == PERSISTENT_RESERVE_OUT) {
1105 cmd->execute_cmd = target_scsi3_emulate_pr_out;
1106 size = get_unaligned_be32(&cdb[5]);
1107 return target_cmd_size_check(cmd, size);
1108 }
1109
1110 if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
1111 cmd->execute_cmd = target_scsi2_reservation_release;
1112 if (cdb[0] == RELEASE_10)
1113 size = get_unaligned_be16(&cdb[7]);
1114 else
1115 size = cmd->data_length;
1116 return target_cmd_size_check(cmd, size);
1117 }
1118 if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
1119 cmd->execute_cmd = target_scsi2_reservation_reserve;
1120 if (cdb[0] == RESERVE_10)
1121 size = get_unaligned_be16(&cdb[7]);
1122 else
1123 size = cmd->data_length;
1124 return target_cmd_size_check(cmd, size);
1125 }
1126 }
1127
1128 /* Set DATA_CDB flag for ops that should have it */
1129 switch (cdb[0]) {
1130 case READ_6:
1131 case READ_10:
1132 case READ_12:
1133 case READ_16:
1134 case WRITE_6:
1135 case WRITE_10:
1136 case WRITE_12:
1137 case WRITE_16:
1138 case WRITE_VERIFY:
1139 case WRITE_VERIFY_12:
1140 case WRITE_VERIFY_16:
1141 case COMPARE_AND_WRITE:
1142 case XDWRITEREAD_10:
1143 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1144 break;
1145 case VARIABLE_LENGTH_CMD:
1146 switch (get_unaligned_be16(&cdb[8])) {
1147 case READ_32:
1148 case WRITE_32:
1149 case WRITE_VERIFY_32:
1150 case XDWRITEREAD_32:
1151 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1152 break;
1153 }
1154 }
1155
1156 cmd->execute_cmd = exec_cmd;
1157
1158 return TCM_NO_SENSE;
1159 }
1160 EXPORT_SYMBOL(passthrough_parse_cdb);
1161