diff options
Diffstat (limited to 'drivers/target/target_core_device.c')
-rw-r--r-- | drivers/target/target_core_device.c | 145 |
1 files changed, 118 insertions, 27 deletions
diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c index 8add07f387f9..e8dd6da164b2 100644 --- a/drivers/target/target_core_device.c +++ b/drivers/target/target_core_device.c | |||
@@ -49,8 +49,9 @@ | |||
49 | #include "target_core_pr.h" | 49 | #include "target_core_pr.h" |
50 | #include "target_core_ua.h" | 50 | #include "target_core_ua.h" |
51 | 51 | ||
52 | DEFINE_MUTEX(g_device_mutex); | 52 | static DEFINE_MUTEX(device_mutex); |
53 | LIST_HEAD(g_device_list); | 53 | static LIST_HEAD(device_list); |
54 | static DEFINE_IDR(devices_idr); | ||
54 | 55 | ||
55 | static struct se_hba *lun0_hba; | 56 | static struct se_hba *lun0_hba; |
56 | /* not static, needed by tpg.c */ | 57 | /* not static, needed by tpg.c */ |
@@ -168,11 +169,20 @@ int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun) | |||
168 | rcu_read_lock(); | 169 | rcu_read_lock(); |
169 | deve = target_nacl_find_deve(nacl, unpacked_lun); | 170 | deve = target_nacl_find_deve(nacl, unpacked_lun); |
170 | if (deve) { | 171 | if (deve) { |
171 | se_cmd->se_lun = rcu_dereference(deve->se_lun); | ||
172 | se_lun = rcu_dereference(deve->se_lun); | 172 | se_lun = rcu_dereference(deve->se_lun); |
173 | |||
174 | if (!percpu_ref_tryget_live(&se_lun->lun_ref)) { | ||
175 | se_lun = NULL; | ||
176 | goto out_unlock; | ||
177 | } | ||
178 | |||
179 | se_cmd->se_lun = rcu_dereference(deve->se_lun); | ||
173 | se_cmd->pr_res_key = deve->pr_res_key; | 180 | se_cmd->pr_res_key = deve->pr_res_key; |
174 | se_cmd->orig_fe_lun = unpacked_lun; | 181 | se_cmd->orig_fe_lun = unpacked_lun; |
182 | se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; | ||
183 | se_cmd->lun_ref_active = true; | ||
175 | } | 184 | } |
185 | out_unlock: | ||
176 | rcu_read_unlock(); | 186 | rcu_read_unlock(); |
177 | 187 | ||
178 | if (!se_lun) { | 188 | if (!se_lun) { |
@@ -182,9 +192,6 @@ int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun) | |||
182 | unpacked_lun); | 192 | unpacked_lun); |
183 | return -ENODEV; | 193 | return -ENODEV; |
184 | } | 194 | } |
185 | /* | ||
186 | * XXX: Add percpu se_lun->lun_ref reference count for TMR | ||
187 | */ | ||
188 | se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev); | 195 | se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev); |
189 | se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev); | 196 | se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev); |
190 | 197 | ||
@@ -756,19 +763,16 @@ struct se_device *target_alloc_device(struct se_hba *hba, const char *name) | |||
756 | if (!dev) | 763 | if (!dev) |
757 | return NULL; | 764 | return NULL; |
758 | 765 | ||
759 | dev->dev_link_magic = SE_DEV_LINK_MAGIC; | ||
760 | dev->se_hba = hba; | 766 | dev->se_hba = hba; |
761 | dev->transport = hba->backend->ops; | 767 | dev->transport = hba->backend->ops; |
762 | dev->prot_length = sizeof(struct t10_pi_tuple); | 768 | dev->prot_length = sizeof(struct t10_pi_tuple); |
763 | dev->hba_index = hba->hba_index; | 769 | dev->hba_index = hba->hba_index; |
764 | 770 | ||
765 | INIT_LIST_HEAD(&dev->dev_list); | ||
766 | INIT_LIST_HEAD(&dev->dev_sep_list); | 771 | INIT_LIST_HEAD(&dev->dev_sep_list); |
767 | INIT_LIST_HEAD(&dev->dev_tmr_list); | 772 | INIT_LIST_HEAD(&dev->dev_tmr_list); |
768 | INIT_LIST_HEAD(&dev->delayed_cmd_list); | 773 | INIT_LIST_HEAD(&dev->delayed_cmd_list); |
769 | INIT_LIST_HEAD(&dev->state_list); | 774 | INIT_LIST_HEAD(&dev->state_list); |
770 | INIT_LIST_HEAD(&dev->qf_cmd_list); | 775 | INIT_LIST_HEAD(&dev->qf_cmd_list); |
771 | INIT_LIST_HEAD(&dev->g_dev_node); | ||
772 | spin_lock_init(&dev->execute_task_lock); | 776 | spin_lock_init(&dev->execute_task_lock); |
773 | spin_lock_init(&dev->delayed_cmd_lock); | 777 | spin_lock_init(&dev->delayed_cmd_lock); |
774 | spin_lock_init(&dev->dev_reservation_lock); | 778 | spin_lock_init(&dev->dev_reservation_lock); |
@@ -851,7 +855,7 @@ bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib, | |||
851 | attrib->unmap_granularity = q->limits.discard_granularity / block_size; | 855 | attrib->unmap_granularity = q->limits.discard_granularity / block_size; |
852 | attrib->unmap_granularity_alignment = q->limits.discard_alignment / | 856 | attrib->unmap_granularity_alignment = q->limits.discard_alignment / |
853 | block_size; | 857 | block_size; |
854 | attrib->unmap_zeroes_data = 0; | 858 | attrib->unmap_zeroes_data = (q->limits.max_write_zeroes_sectors); |
855 | return true; | 859 | return true; |
856 | } | 860 | } |
857 | EXPORT_SYMBOL(target_configure_unmap_from_queue); | 861 | EXPORT_SYMBOL(target_configure_unmap_from_queue); |
@@ -875,10 +879,79 @@ sector_t target_to_linux_sector(struct se_device *dev, sector_t lb) | |||
875 | } | 879 | } |
876 | EXPORT_SYMBOL(target_to_linux_sector); | 880 | EXPORT_SYMBOL(target_to_linux_sector); |
877 | 881 | ||
882 | /** | ||
883 | * target_find_device - find a se_device by its dev_index | ||
884 | * @id: dev_index | ||
885 | * @do_depend: true if caller needs target_depend_item to be done | ||
886 | * | ||
887 | * If do_depend is true, the caller must do a target_undepend_item | ||
888 | * when finished using the device. | ||
889 | * | ||
890 | * If do_depend is false, the caller must be called in a configfs | ||
891 | * callback or during removal. | ||
892 | */ | ||
893 | struct se_device *target_find_device(int id, bool do_depend) | ||
894 | { | ||
895 | struct se_device *dev; | ||
896 | |||
897 | mutex_lock(&device_mutex); | ||
898 | dev = idr_find(&devices_idr, id); | ||
899 | if (dev && do_depend && target_depend_item(&dev->dev_group.cg_item)) | ||
900 | dev = NULL; | ||
901 | mutex_unlock(&device_mutex); | ||
902 | return dev; | ||
903 | } | ||
904 | EXPORT_SYMBOL(target_find_device); | ||
905 | |||
906 | struct devices_idr_iter { | ||
907 | int (*fn)(struct se_device *dev, void *data); | ||
908 | void *data; | ||
909 | }; | ||
910 | |||
911 | static int target_devices_idr_iter(int id, void *p, void *data) | ||
912 | { | ||
913 | struct devices_idr_iter *iter = data; | ||
914 | struct se_device *dev = p; | ||
915 | |||
916 | /* | ||
917 | * We add the device early to the idr, so it can be used | ||
918 | * by backend modules during configuration. We do not want | ||
919 | * to allow other callers to access partially setup devices, | ||
920 | * so we skip them here. | ||
921 | */ | ||
922 | if (!(dev->dev_flags & DF_CONFIGURED)) | ||
923 | return 0; | ||
924 | |||
925 | return iter->fn(dev, iter->data); | ||
926 | } | ||
927 | |||
928 | /** | ||
929 | * target_for_each_device - iterate over configured devices | ||
930 | * @fn: iterator function | ||
931 | * @data: pointer to data that will be passed to fn | ||
932 | * | ||
933 | * fn must return 0 to continue looping over devices. non-zero will break | ||
934 | * from the loop and return that value to the caller. | ||
935 | */ | ||
936 | int target_for_each_device(int (*fn)(struct se_device *dev, void *data), | ||
937 | void *data) | ||
938 | { | ||
939 | struct devices_idr_iter iter; | ||
940 | int ret; | ||
941 | |||
942 | iter.fn = fn; | ||
943 | iter.data = data; | ||
944 | |||
945 | mutex_lock(&device_mutex); | ||
946 | ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter); | ||
947 | mutex_unlock(&device_mutex); | ||
948 | return ret; | ||
949 | } | ||
950 | |||
878 | int target_configure_device(struct se_device *dev) | 951 | int target_configure_device(struct se_device *dev) |
879 | { | 952 | { |
880 | struct se_hba *hba = dev->se_hba; | 953 | struct se_hba *hba = dev->se_hba; |
881 | int ret; | 954 | int ret, id; |
882 | 955 | ||
883 | if (dev->dev_flags & DF_CONFIGURED) { | 956 | if (dev->dev_flags & DF_CONFIGURED) { |
884 | pr_err("se_dev->se_dev_ptr already set for storage" | 957 | pr_err("se_dev->se_dev_ptr already set for storage" |
@@ -886,9 +959,26 @@ int target_configure_device(struct se_device *dev) | |||
886 | return -EEXIST; | 959 | return -EEXIST; |
887 | } | 960 | } |
888 | 961 | ||
962 | /* | ||
963 | * Add early so modules like tcmu can use during its | ||
964 | * configuration. | ||
965 | */ | ||
966 | mutex_lock(&device_mutex); | ||
967 | /* | ||
968 | * Use cyclic to try and avoid collisions with devices | ||
969 | * that were recently removed. | ||
970 | */ | ||
971 | id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL); | ||
972 | mutex_unlock(&device_mutex); | ||
973 | if (id < 0) { | ||
974 | ret = -ENOMEM; | ||
975 | goto out; | ||
976 | } | ||
977 | dev->dev_index = id; | ||
978 | |||
889 | ret = dev->transport->configure_device(dev); | 979 | ret = dev->transport->configure_device(dev); |
890 | if (ret) | 980 | if (ret) |
891 | goto out; | 981 | goto out_free_index; |
892 | /* | 982 | /* |
893 | * XXX: there is not much point to have two different values here.. | 983 | * XXX: there is not much point to have two different values here.. |
894 | */ | 984 | */ |
@@ -903,12 +993,11 @@ int target_configure_device(struct se_device *dev) | |||
903 | dev->dev_attrib.hw_block_size); | 993 | dev->dev_attrib.hw_block_size); |
904 | dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors; | 994 | dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors; |
905 | 995 | ||
906 | dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX); | ||
907 | dev->creation_time = get_jiffies_64(); | 996 | dev->creation_time = get_jiffies_64(); |
908 | 997 | ||
909 | ret = core_setup_alua(dev); | 998 | ret = core_setup_alua(dev); |
910 | if (ret) | 999 | if (ret) |
911 | goto out; | 1000 | goto out_free_index; |
912 | 1001 | ||
913 | /* | 1002 | /* |
914 | * Startup the struct se_device processing thread | 1003 | * Startup the struct se_device processing thread |
@@ -946,16 +1035,16 @@ int target_configure_device(struct se_device *dev) | |||
946 | hba->dev_count++; | 1035 | hba->dev_count++; |
947 | spin_unlock(&hba->device_lock); | 1036 | spin_unlock(&hba->device_lock); |
948 | 1037 | ||
949 | mutex_lock(&g_device_mutex); | ||
950 | list_add_tail(&dev->g_dev_node, &g_device_list); | ||
951 | mutex_unlock(&g_device_mutex); | ||
952 | |||
953 | dev->dev_flags |= DF_CONFIGURED; | 1038 | dev->dev_flags |= DF_CONFIGURED; |
954 | 1039 | ||
955 | return 0; | 1040 | return 0; |
956 | 1041 | ||
957 | out_free_alua: | 1042 | out_free_alua: |
958 | core_alua_free_lu_gp_mem(dev); | 1043 | core_alua_free_lu_gp_mem(dev); |
1044 | out_free_index: | ||
1045 | mutex_lock(&device_mutex); | ||
1046 | idr_remove(&devices_idr, dev->dev_index); | ||
1047 | mutex_unlock(&device_mutex); | ||
959 | out: | 1048 | out: |
960 | se_release_vpd_for_dev(dev); | 1049 | se_release_vpd_for_dev(dev); |
961 | return ret; | 1050 | return ret; |
@@ -970,9 +1059,11 @@ void target_free_device(struct se_device *dev) | |||
970 | if (dev->dev_flags & DF_CONFIGURED) { | 1059 | if (dev->dev_flags & DF_CONFIGURED) { |
971 | destroy_workqueue(dev->tmr_wq); | 1060 | destroy_workqueue(dev->tmr_wq); |
972 | 1061 | ||
973 | mutex_lock(&g_device_mutex); | 1062 | dev->transport->destroy_device(dev); |
974 | list_del(&dev->g_dev_node); | 1063 | |
975 | mutex_unlock(&g_device_mutex); | 1064 | mutex_lock(&device_mutex); |
1065 | idr_remove(&devices_idr, dev->dev_index); | ||
1066 | mutex_unlock(&device_mutex); | ||
976 | 1067 | ||
977 | spin_lock(&hba->device_lock); | 1068 | spin_lock(&hba->device_lock); |
978 | hba->dev_count--; | 1069 | hba->dev_count--; |
@@ -1087,19 +1178,19 @@ passthrough_parse_cdb(struct se_cmd *cmd, | |||
1087 | TRANSPORT_FLAG_PASSTHROUGH_PGR)) { | 1178 | TRANSPORT_FLAG_PASSTHROUGH_PGR)) { |
1088 | if (cdb[0] == PERSISTENT_RESERVE_IN) { | 1179 | if (cdb[0] == PERSISTENT_RESERVE_IN) { |
1089 | cmd->execute_cmd = target_scsi3_emulate_pr_in; | 1180 | cmd->execute_cmd = target_scsi3_emulate_pr_in; |
1090 | size = (cdb[7] << 8) + cdb[8]; | 1181 | size = get_unaligned_be16(&cdb[7]); |
1091 | return target_cmd_size_check(cmd, size); | 1182 | return target_cmd_size_check(cmd, size); |
1092 | } | 1183 | } |
1093 | if (cdb[0] == PERSISTENT_RESERVE_OUT) { | 1184 | if (cdb[0] == PERSISTENT_RESERVE_OUT) { |
1094 | cmd->execute_cmd = target_scsi3_emulate_pr_out; | 1185 | cmd->execute_cmd = target_scsi3_emulate_pr_out; |
1095 | size = (cdb[7] << 8) + cdb[8]; | 1186 | size = get_unaligned_be32(&cdb[5]); |
1096 | return target_cmd_size_check(cmd, size); | 1187 | return target_cmd_size_check(cmd, size); |
1097 | } | 1188 | } |
1098 | 1189 | ||
1099 | if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) { | 1190 | if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) { |
1100 | cmd->execute_cmd = target_scsi2_reservation_release; | 1191 | cmd->execute_cmd = target_scsi2_reservation_release; |
1101 | if (cdb[0] == RELEASE_10) | 1192 | if (cdb[0] == RELEASE_10) |
1102 | size = (cdb[7] << 8) | cdb[8]; | 1193 | size = get_unaligned_be16(&cdb[7]); |
1103 | else | 1194 | else |
1104 | size = cmd->data_length; | 1195 | size = cmd->data_length; |
1105 | return target_cmd_size_check(cmd, size); | 1196 | return target_cmd_size_check(cmd, size); |
@@ -1107,7 +1198,7 @@ passthrough_parse_cdb(struct se_cmd *cmd, | |||
1107 | if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) { | 1198 | if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) { |
1108 | cmd->execute_cmd = target_scsi2_reservation_reserve; | 1199 | cmd->execute_cmd = target_scsi2_reservation_reserve; |
1109 | if (cdb[0] == RESERVE_10) | 1200 | if (cdb[0] == RESERVE_10) |
1110 | size = (cdb[7] << 8) | cdb[8]; | 1201 | size = get_unaligned_be16(&cdb[7]); |
1111 | else | 1202 | else |
1112 | size = cmd->data_length; | 1203 | size = cmd->data_length; |
1113 | return target_cmd_size_check(cmd, size); | 1204 | return target_cmd_size_check(cmd, size); |
@@ -1126,7 +1217,7 @@ passthrough_parse_cdb(struct se_cmd *cmd, | |||
1126 | case WRITE_16: | 1217 | case WRITE_16: |
1127 | case WRITE_VERIFY: | 1218 | case WRITE_VERIFY: |
1128 | case WRITE_VERIFY_12: | 1219 | case WRITE_VERIFY_12: |
1129 | case 0x8e: /* WRITE_VERIFY_16 */ | 1220 | case WRITE_VERIFY_16: |
1130 | case COMPARE_AND_WRITE: | 1221 | case COMPARE_AND_WRITE: |
1131 | case XDWRITEREAD_10: | 1222 | case XDWRITEREAD_10: |
1132 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | 1223 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
@@ -1135,7 +1226,7 @@ passthrough_parse_cdb(struct se_cmd *cmd, | |||
1135 | switch (get_unaligned_be16(&cdb[8])) { | 1226 | switch (get_unaligned_be16(&cdb[8])) { |
1136 | case READ_32: | 1227 | case READ_32: |
1137 | case WRITE_32: | 1228 | case WRITE_32: |
1138 | case 0x0c: /* WRITE_VERIFY_32 */ | 1229 | case WRITE_VERIFY_32: |
1139 | case XDWRITEREAD_32: | 1230 | case XDWRITEREAD_32: |
1140 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; | 1231 | cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB; |
1141 | break; | 1232 | break; |