hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/target/target_core_device.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*******************************************************************************
23 * Filename: target_core_device.c (based on iscsi_target_device.c)
34 *
....@@ -7,20 +8,6 @@
78 * (c) Copyright 2003-2013 Datera, Inc.
89 *
910 * Nicholas A. Bellinger <nab@kernel.org>
10
- *
11
- * This program is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU General Public License
22
- * along with this program; if not, write to the Free Software
23
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2411 *
2512 ******************************************************************************/
2613
....@@ -58,7 +45,7 @@
5845 struct se_device *g_lun0_dev;
5946
6047 sense_reason_t
61
-transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
48
+transport_lookup_cmd_lun(struct se_cmd *se_cmd)
6249 {
6350 struct se_lun *se_lun = NULL;
6451 struct se_session *se_sess = se_cmd->se_sess;
....@@ -67,7 +54,7 @@
6754 sense_reason_t ret = TCM_NO_SENSE;
6855
6956 rcu_read_lock();
70
- deve = target_nacl_find_deve(nacl, unpacked_lun);
57
+ deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
7158 if (deve) {
7259 atomic_long_inc(&deve->total_cmds);
7360
....@@ -87,7 +74,6 @@
8774
8875 se_cmd->se_lun = se_lun;
8976 se_cmd->pr_res_key = deve->pr_res_key;
90
- se_cmd->orig_fe_lun = unpacked_lun;
9177 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
9278 se_cmd->lun_ref_active = true;
9379
....@@ -95,8 +81,8 @@
9581 deve->lun_access_ro) {
9682 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
9783 " Access for 0x%08llx\n",
98
- se_cmd->se_tfo->get_fabric_name(),
99
- unpacked_lun);
84
+ se_cmd->se_tfo->fabric_name,
85
+ se_cmd->orig_fe_lun);
10086 rcu_read_unlock();
10187 ret = TCM_WRITE_PROTECTED;
10288 goto ref_dev;
....@@ -111,17 +97,17 @@
11197 * REPORT_LUNS, et al to be returned when no active
11298 * MappedLUN=0 exists for this Initiator Port.
11399 */
114
- if (unpacked_lun != 0) {
100
+ if (se_cmd->orig_fe_lun != 0) {
115101 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
116
- " Access for 0x%08llx\n",
117
- se_cmd->se_tfo->get_fabric_name(),
118
- unpacked_lun);
102
+ " Access for 0x%08llx from %s\n",
103
+ se_cmd->se_tfo->fabric_name,
104
+ se_cmd->orig_fe_lun,
105
+ nacl->initiatorname);
119106 return TCM_NON_EXISTENT_LUN;
120107 }
121108
122109 se_lun = se_sess->se_tpg->tpg_virt_lun0;
123110 se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
124
- se_cmd->orig_fe_lun = 0;
125111 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
126112
127113 percpu_ref_get(&se_lun->lun_ref);
....@@ -157,7 +143,7 @@
157143 }
158144 EXPORT_SYMBOL(transport_lookup_cmd_lun);
159145
160
-int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
146
+int transport_lookup_tmr_lun(struct se_cmd *se_cmd)
161147 {
162148 struct se_dev_entry *deve;
163149 struct se_lun *se_lun = NULL;
....@@ -167,7 +153,7 @@
167153 unsigned long flags;
168154
169155 rcu_read_lock();
170
- deve = target_nacl_find_deve(nacl, unpacked_lun);
156
+ deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
171157 if (deve) {
172158 se_lun = rcu_dereference(deve->se_lun);
173159
....@@ -178,7 +164,6 @@
178164
179165 se_cmd->se_lun = se_lun;
180166 se_cmd->pr_res_key = deve->pr_res_key;
181
- se_cmd->orig_fe_lun = unpacked_lun;
182167 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
183168 se_cmd->lun_ref_active = true;
184169 }
....@@ -187,9 +172,10 @@
187172
188173 if (!se_lun) {
189174 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
190
- " Access for 0x%08llx\n",
191
- se_cmd->se_tfo->get_fabric_name(),
192
- unpacked_lun);
175
+ " Access for 0x%08llx for %s\n",
176
+ se_cmd->se_tfo->fabric_name,
177
+ se_cmd->orig_fe_lun,
178
+ nacl->initiatorname);
193179 return -ENODEV;
194180 }
195181 se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
....@@ -237,7 +223,7 @@
237223 if (!lun) {
238224 pr_err("%s device entries device pointer is"
239225 " NULL, but Initiator has access.\n",
240
- tpg->se_tpg_tfo->get_fabric_name());
226
+ tpg->se_tpg_tfo->fabric_name);
241227 continue;
242228 }
243229 if (lun->lun_rtpi != rtpi)
....@@ -404,9 +390,6 @@
404390 return 0;
405391 }
406392
407
-/*
408
- * Called with se_node_acl->lun_entry_mutex held.
409
- */
410393 void core_disable_device_list_for_node(
411394 struct se_lun *lun,
412395 struct se_dev_entry *orig,
....@@ -418,6 +401,9 @@
418401 * reference to se_device->dev_group.
419402 */
420403 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
404
+
405
+ lockdep_assert_held(&nacl->lun_entry_mutex);
406
+
421407 /*
422408 * If the MappedLUN entry is being disabled, the entry in
423409 * lun->lun_deve_list must be removed now before clearing the
....@@ -571,9 +557,9 @@
571557 return rc;
572558
573559 pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
574
- " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
560
+ " CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
575561 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
576
- tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
562
+ tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
577563 /*
578564 * Update LUN maps for dynamically added initiators when
579565 * generate_node_acl is enabled.
....@@ -604,9 +590,9 @@
604590 struct se_lun *lun)
605591 {
606592 pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
607
- " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
593
+ " device object\n", tpg->se_tpg_tfo->fabric_name,
608594 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
609
- tpg->se_tpg_tfo->get_fabric_name());
595
+ tpg->se_tpg_tfo->fabric_name);
610596
611597 core_tpg_remove_lun(tpg, lun);
612598 }
....@@ -621,7 +607,7 @@
621607
622608 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
623609 pr_err("%s InitiatorName exceeds maximum size.\n",
624
- tpg->se_tpg_tfo->get_fabric_name());
610
+ tpg->se_tpg_tfo->fabric_name);
625611 *ret = -EOVERFLOW;
626612 return NULL;
627613 }
....@@ -664,7 +650,7 @@
664650 return -EINVAL;
665651
666652 pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
667
- " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
653
+ " InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
668654 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
669655 lun_access_ro ? "RO" : "RW",
670656 nacl->initiatorname);
....@@ -697,7 +683,7 @@
697683
698684 pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
699685 " InitiatorNode: %s Mapped LUN: %llu\n",
700
- tpg->se_tpg_tfo->get_fabric_name(),
686
+ tpg->se_tpg_tfo->fabric_name,
701687 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
702688 nacl->initiatorname, lacl->mapped_lun);
703689
....@@ -709,9 +695,9 @@
709695 struct se_lun_acl *lacl)
710696 {
711697 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
712
- " Mapped LUN: %llu\n", tpg->se_tpg_tfo->get_fabric_name(),
698
+ " Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
713699 tpg->se_tpg_tfo->tpg_get_tag(tpg),
714
- tpg->se_tpg_tfo->get_fabric_name(),
700
+ tpg->se_tpg_tfo->fabric_name,
715701 lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
716702
717703 kfree(lacl);
....@@ -720,36 +706,17 @@
720706 static void scsi_dump_inquiry(struct se_device *dev)
721707 {
722708 struct t10_wwn *wwn = &dev->t10_wwn;
723
- char buf[17];
724
- int i, device_type;
709
+ int device_type = dev->transport->get_device_type(dev);
710
+
725711 /*
726712 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
727713 */
728
- for (i = 0; i < 8; i++)
729
- if (wwn->vendor[i] >= 0x20)
730
- buf[i] = wwn->vendor[i];
731
- else
732
- buf[i] = ' ';
733
- buf[i] = '\0';
734
- pr_debug(" Vendor: %s\n", buf);
735
-
736
- for (i = 0; i < 16; i++)
737
- if (wwn->model[i] >= 0x20)
738
- buf[i] = wwn->model[i];
739
- else
740
- buf[i] = ' ';
741
- buf[i] = '\0';
742
- pr_debug(" Model: %s\n", buf);
743
-
744
- for (i = 0; i < 4; i++)
745
- if (wwn->revision[i] >= 0x20)
746
- buf[i] = wwn->revision[i];
747
- else
748
- buf[i] = ' ';
749
- buf[i] = '\0';
750
- pr_debug(" Revision: %s\n", buf);
751
-
752
- device_type = dev->transport->get_device_type(dev);
714
+ pr_debug(" Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
715
+ wwn->vendor);
716
+ pr_debug(" Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
717
+ wwn->model);
718
+ pr_debug(" Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
719
+ wwn->revision);
753720 pr_debug(" Type: %s ", scsi_device_type(device_type));
754721 }
755722
....@@ -757,22 +724,34 @@
757724 {
758725 struct se_device *dev;
759726 struct se_lun *xcopy_lun;
727
+ int i;
760728
761729 dev = hba->backend->ops->alloc_device(hba, name);
762730 if (!dev)
763731 return NULL;
764732
733
+ dev->queues = kcalloc(nr_cpu_ids, sizeof(*dev->queues), GFP_KERNEL);
734
+ if (!dev->queues) {
735
+ dev->transport->free_device(dev);
736
+ return NULL;
737
+ }
738
+
739
+ dev->queue_cnt = nr_cpu_ids;
740
+ for (i = 0; i < dev->queue_cnt; i++) {
741
+ INIT_LIST_HEAD(&dev->queues[i].state_list);
742
+ spin_lock_init(&dev->queues[i].lock);
743
+ }
744
+
765745 dev->se_hba = hba;
766746 dev->transport = hba->backend->ops;
747
+ dev->transport_flags = dev->transport->transport_flags_default;
767748 dev->prot_length = sizeof(struct t10_pi_tuple);
768749 dev->hba_index = hba->hba_index;
769750
770751 INIT_LIST_HEAD(&dev->dev_sep_list);
771752 INIT_LIST_HEAD(&dev->dev_tmr_list);
772753 INIT_LIST_HEAD(&dev->delayed_cmd_list);
773
- INIT_LIST_HEAD(&dev->state_list);
774754 INIT_LIST_HEAD(&dev->qf_cmd_list);
775
- spin_lock_init(&dev->execute_task_lock);
776755 spin_lock_init(&dev->delayed_cmd_lock);
777756 spin_lock_init(&dev->dev_reservation_lock);
778757 spin_lock_init(&dev->se_port_lock);
....@@ -791,6 +770,7 @@
791770 spin_lock_init(&dev->t10_alua.lba_map_lock);
792771
793772 INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
773
+ mutex_init(&dev->lun_reset_mutex);
794774
795775 dev->t10_wwn.t10_dev = dev;
796776 dev->t10_alua.t10_dev = dev;
....@@ -801,12 +781,13 @@
801781 dev->dev_attrib.emulate_fua_write = 1;
802782 dev->dev_attrib.emulate_fua_read = 1;
803783 dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
804
- dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
784
+ dev->dev_attrib.emulate_ua_intlck_ctrl = TARGET_UA_INTLCK_CTRL_CLEAR;
805785 dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
806786 dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
807787 dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
808788 dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
809789 dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
790
+ dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
810791 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
811792 dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
812793 dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
....@@ -824,12 +805,18 @@
824805
825806 xcopy_lun = &dev->xcopy_lun;
826807 rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
827
- init_completion(&xcopy_lun->lun_ref_comp);
828808 init_completion(&xcopy_lun->lun_shutdown_comp);
829809 INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
830810 INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
831811 mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
832812 xcopy_lun->lun_tpg = &xcopy_pt_tpg;
813
+
814
+ /* Preload the default INQUIRY const values */
815
+ strlcpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
816
+ strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
817
+ sizeof(dev->t10_wwn.model));
818
+ strlcpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
819
+ sizeof(dev->t10_wwn.revision));
833820
834821 return dev;
835822 }
....@@ -856,7 +843,6 @@
856843 attrib->unmap_granularity = q->limits.discard_granularity / block_size;
857844 attrib->unmap_granularity_alignment = q->limits.discard_alignment /
858845 block_size;
859
- attrib->unmap_zeroes_data = (q->limits.max_write_zeroes_sectors);
860846 return true;
861847 }
862848 EXPORT_SYMBOL(target_configure_unmap_from_queue);
....@@ -881,7 +867,6 @@
881867 EXPORT_SYMBOL(target_to_linux_sector);
882868
883869 struct devices_idr_iter {
884
- struct config_item *prev_item;
885870 int (*fn)(struct se_device *dev, void *data);
886871 void *data;
887872 };
....@@ -891,10 +876,8 @@
891876 {
892877 struct devices_idr_iter *iter = data;
893878 struct se_device *dev = p;
879
+ struct config_item *item;
894880 int ret;
895
-
896
- config_item_put(iter->prev_item);
897
- iter->prev_item = NULL;
898881
899882 /*
900883 * We add the device early to the idr, so it can be used
....@@ -905,12 +888,13 @@
905888 if (!target_dev_configured(dev))
906889 return 0;
907890
908
- iter->prev_item = config_item_get_unless_zero(&dev->dev_group.cg_item);
909
- if (!iter->prev_item)
891
+ item = config_item_get_unless_zero(&dev->dev_group.cg_item);
892
+ if (!item)
910893 return 0;
911894 mutex_unlock(&device_mutex);
912895
913896 ret = iter->fn(dev, iter->data);
897
+ config_item_put(item);
914898
915899 mutex_lock(&device_mutex);
916900 return ret;
....@@ -933,7 +917,6 @@
933917 mutex_lock(&device_mutex);
934918 ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
935919 mutex_unlock(&device_mutex);
936
- config_item_put(iter.prev_item);
937920 return ret;
938921 }
939922
....@@ -989,34 +972,9 @@
989972 goto out_destroy_device;
990973
991974 /*
992
- * Startup the struct se_device processing thread
993
- */
994
- dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
995
- dev->transport->name);
996
- if (!dev->tmr_wq) {
997
- pr_err("Unable to create tmr workqueue for %s\n",
998
- dev->transport->name);
999
- ret = -ENOMEM;
1000
- goto out_free_alua;
1001
- }
1002
-
1003
- /*
1004975 * Setup work_queue for QUEUE_FULL
1005976 */
1006977 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1007
-
1008
- /*
1009
- * Preload the initial INQUIRY const values if we are doing
1010
- * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1011
- * passthrough because this is being provided by the backend LLD.
1012
- */
1013
- if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
1014
- strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1015
- strncpy(&dev->t10_wwn.model[0],
1016
- dev->transport->inquiry_prod, 16);
1017
- strncpy(&dev->t10_wwn.revision[0],
1018
- dev->transport->inquiry_rev, 4);
1019
- }
1020978
1021979 scsi_dump_inquiry(dev);
1022980
....@@ -1028,8 +986,6 @@
1028986
1029987 return 0;
1030988
1031
-out_free_alua:
1032
- core_alua_free_lu_gp_mem(dev);
1033989 out_destroy_device:
1034990 dev->transport->destroy_device(dev);
1035991 out_free_index:
....@@ -1048,8 +1004,6 @@
10481004 WARN_ON(!list_empty(&dev->dev_sep_list));
10491005
10501006 if (target_dev_configured(dev)) {
1051
- destroy_workqueue(dev->tmr_wq);
1052
-
10531007 dev->transport->destroy_device(dev);
10541008
10551009 mutex_lock(&device_mutex);
....@@ -1069,6 +1023,7 @@
10691023 if (dev->transport->free_prot)
10701024 dev->transport->free_prot(dev);
10711025
1026
+ kfree(dev->queues);
10721027 dev->transport->free_device(dev);
10731028 }
10741029
....@@ -1140,11 +1095,23 @@
11401095 }
11411096
11421097 /*
1098
+ * With emulate_pr disabled, all reservation requests should fail,
1099
+ * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
1100
+ */
1101
+ if (!dev->dev_attrib.emulate_pr &&
1102
+ ((cdb[0] == PERSISTENT_RESERVE_IN) ||
1103
+ (cdb[0] == PERSISTENT_RESERVE_OUT) ||
1104
+ (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
1105
+ (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
1106
+ return TCM_UNSUPPORTED_SCSI_OPCODE;
1107
+ }
1108
+
1109
+ /*
11431110 * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
11441111 * emulate the response, since tcmu does not have the information
11451112 * required to process these commands.
11461113 */
1147
- if (!(dev->transport->transport_flags &
1114
+ if (!(dev->transport_flags &
11481115 TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
11491116 if (cdb[0] == PERSISTENT_RESERVE_IN) {
11501117 cmd->execute_cmd = target_scsi3_emulate_pr_in;