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