aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2015-05-26 00:33:08 -0400
committerNicholas Bellinger <nab@linux-iscsi.org>2015-06-01 03:26:38 -0400
commitadf653f92f38e80a78bb77e912d49bcc8055330f (patch)
tree374904527485a43dbc810905fa6f280cd1c2f5cf
parentb3eeea6619a0ed4f37138661c49339b21361d397 (diff)
target: Subsume se_port + t10_alua_tg_pt_gp_member into se_lun
This patch eliminates all se_port + t10_alua_tg_pt_gp_member usage, and converts current users to direct se_lun pointer dereference. This includes the removal of core_export_port(), core_release_port() core_dev_export() and core_dev_unexport(). Along with conversion of special case se_lun pointer dereference within PR ALL_TG_PT=1 and ALUA access state transition UNIT_ATTENTION handling. Also, update core_enable_device_list_for_node() to reference the new per se_lun->lun_deve_list when creating a new entry, or replacing an existing one via RCU. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
-rw-r--r--drivers/target/target_core_alua.c394
-rw-r--r--drivers/target/target_core_alua.h14
-rw-r--r--drivers/target/target_core_configfs.c11
-rw-r--r--drivers/target/target_core_device.c179
-rw-r--r--drivers/target/target_core_fabric_configfs.c32
-rw-r--r--drivers/target/target_core_internal.h12
-rw-r--r--drivers/target/target_core_pr.c56
-rw-r--r--drivers/target/target_core_spc.c48
-rw-r--r--drivers/target/target_core_stat.c299
-rw-r--r--drivers/target/target_core_tpg.c95
-rw-r--r--drivers/target/target_core_transport.c34
-rw-r--r--drivers/target/target_core_xcopy.c17
-rw-r--r--include/target/target_core_base.h74
13 files changed, 458 insertions, 807 deletions
diff --git a/drivers/target/target_core_alua.c b/drivers/target/target_core_alua.c
index 53cdfabac861..1109c2833fe6 100644
--- a/drivers/target/target_core_alua.c
+++ b/drivers/target/target_core_alua.c
@@ -43,11 +43,13 @@
43static sense_reason_t core_alua_check_transition(int state, int valid, 43static sense_reason_t core_alua_check_transition(int state, int valid,
44 int *primary); 44 int *primary);
45static int core_alua_set_tg_pt_secondary_state( 45static int core_alua_set_tg_pt_secondary_state(
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 46 struct se_lun *lun, int explicit, int offline);
47 struct se_port *port, int explicit, int offline);
48 47
49static char *core_alua_dump_state(int state); 48static char *core_alua_dump_state(int state);
50 49
50static void __target_attach_tg_pt_gp(struct se_lun *lun,
51 struct t10_alua_tg_pt_gp *tg_pt_gp);
52
51static u16 alua_lu_gps_counter; 53static u16 alua_lu_gps_counter;
52static u32 alua_lu_gps_count; 54static u32 alua_lu_gps_count;
53 55
@@ -145,9 +147,8 @@ sense_reason_t
145target_emulate_report_target_port_groups(struct se_cmd *cmd) 147target_emulate_report_target_port_groups(struct se_cmd *cmd)
146{ 148{
147 struct se_device *dev = cmd->se_dev; 149 struct se_device *dev = cmd->se_dev;
148 struct se_port *port;
149 struct t10_alua_tg_pt_gp *tg_pt_gp; 150 struct t10_alua_tg_pt_gp *tg_pt_gp;
150 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 151 struct se_lun *lun;
151 unsigned char *buf; 152 unsigned char *buf;
152 u32 rd_len = 0, off; 153 u32 rd_len = 0, off;
153 int ext_hdr = (cmd->t_task_cdb[1] & 0x20); 154 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
@@ -222,9 +223,8 @@ target_emulate_report_target_port_groups(struct se_cmd *cmd)
222 rd_len += 8; 223 rd_len += 8;
223 224
224 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 225 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
225 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list, 226 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
226 tg_pt_gp_mem_list) { 227 lun_tg_pt_gp_link) {
227 port = tg_pt_gp_mem->tg_pt;
228 /* 228 /*
229 * Start Target Port descriptor format 229 * Start Target Port descriptor format
230 * 230 *
@@ -234,8 +234,8 @@ target_emulate_report_target_port_groups(struct se_cmd *cmd)
234 /* 234 /*
235 * Set RELATIVE TARGET PORT IDENTIFIER 235 * Set RELATIVE TARGET PORT IDENTIFIER
236 */ 236 */
237 buf[off++] = ((port->sep_rtpi >> 8) & 0xff); 237 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
238 buf[off++] = (port->sep_rtpi & 0xff); 238 buf[off++] = (lun->lun_rtpi & 0xff);
239 rd_len += 4; 239 rd_len += 4;
240 } 240 }
241 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 241 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
@@ -259,15 +259,11 @@ target_emulate_report_target_port_groups(struct se_cmd *cmd)
259 * this CDB was received upon to determine this value individually 259 * this CDB was received upon to determine this value individually
260 * for ALUA target port group. 260 * for ALUA target port group.
261 */ 261 */
262 port = cmd->se_lun->lun_sep; 262 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
263 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 263 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
264 if (tg_pt_gp_mem) { 264 if (tg_pt_gp)
265 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 265 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
266 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 266 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
267 if (tg_pt_gp)
268 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
269 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
270 }
271 } 267 }
272 transport_kunmap_data_sg(cmd); 268 transport_kunmap_data_sg(cmd);
273 269
@@ -284,10 +280,9 @@ sense_reason_t
284target_emulate_set_target_port_groups(struct se_cmd *cmd) 280target_emulate_set_target_port_groups(struct se_cmd *cmd)
285{ 281{
286 struct se_device *dev = cmd->se_dev; 282 struct se_device *dev = cmd->se_dev;
287 struct se_port *port, *l_port = cmd->se_lun->lun_sep; 283 struct se_lun *l_lun = cmd->se_lun;
288 struct se_node_acl *nacl = cmd->se_sess->se_node_acl; 284 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
289 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp; 285 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
290 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
291 unsigned char *buf; 286 unsigned char *buf;
292 unsigned char *ptr; 287 unsigned char *ptr;
293 sense_reason_t rc = TCM_NO_SENSE; 288 sense_reason_t rc = TCM_NO_SENSE;
@@ -295,9 +290,6 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
295 int alua_access_state, primary = 0, valid_states; 290 int alua_access_state, primary = 0, valid_states;
296 u16 tg_pt_id, rtpi; 291 u16 tg_pt_id, rtpi;
297 292
298 if (!l_port)
299 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
300
301 if (cmd->data_length < 4) { 293 if (cmd->data_length < 4) {
302 pr_warn("SET TARGET PORT GROUPS parameter list length %u too" 294 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
303 " small\n", cmd->data_length); 295 " small\n", cmd->data_length);
@@ -312,29 +304,24 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
312 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed 304 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
313 * for the local tg_pt_gp. 305 * for the local tg_pt_gp.
314 */ 306 */
315 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem; 307 spin_lock(&l_lun->lun_tg_pt_gp_lock);
316 if (!l_tg_pt_gp_mem) { 308 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
317 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
318 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
319 goto out;
320 }
321 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
322 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
323 if (!l_tg_pt_gp) { 309 if (!l_tg_pt_gp) {
324 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); 310 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
325 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n"); 311 pr_err("Unable to access l_lun->tg_pt_gp\n");
326 rc = TCM_UNSUPPORTED_SCSI_OPCODE; 312 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
327 goto out; 313 goto out;
328 } 314 }
329 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
330 315
331 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) { 316 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
317 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
332 pr_debug("Unable to process SET_TARGET_PORT_GROUPS" 318 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
333 " while TPGS_EXPLICIT_ALUA is disabled\n"); 319 " while TPGS_EXPLICIT_ALUA is disabled\n");
334 rc = TCM_UNSUPPORTED_SCSI_OPCODE; 320 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
335 goto out; 321 goto out;
336 } 322 }
337 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states; 323 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
324 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
338 325
339 ptr = &buf[4]; /* Skip over RESERVED area in header */ 326 ptr = &buf[4]; /* Skip over RESERVED area in header */
340 327
@@ -396,7 +383,7 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
396 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 383 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
397 384
398 if (!core_alua_do_port_transition(tg_pt_gp, 385 if (!core_alua_do_port_transition(tg_pt_gp,
399 dev, l_port, nacl, 386 dev, l_lun, nacl,
400 alua_access_state, 1)) 387 alua_access_state, 1))
401 found = true; 388 found = true;
402 389
@@ -406,6 +393,8 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
406 } 393 }
407 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 394 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
408 } else { 395 } else {
396 struct se_lun *lun;
397
409 /* 398 /*
410 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify 399 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
411 * the Target Port in question for the the incoming 400 * the Target Port in question for the the incoming
@@ -417,17 +406,16 @@ target_emulate_set_target_port_groups(struct se_cmd *cmd)
417 * for the struct se_device storage object. 406 * for the struct se_device storage object.
418 */ 407 */
419 spin_lock(&dev->se_port_lock); 408 spin_lock(&dev->se_port_lock);
420 list_for_each_entry(port, &dev->dev_sep_list, 409 list_for_each_entry(lun, &dev->dev_sep_list,
421 sep_list) { 410 lun_dev_link) {
422 if (port->sep_rtpi != rtpi) 411 if (lun->lun_rtpi != rtpi)
423 continue; 412 continue;
424 413
425 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 414 // XXX: racy unlock
426
427 spin_unlock(&dev->se_port_lock); 415 spin_unlock(&dev->se_port_lock);
428 416
429 if (!core_alua_set_tg_pt_secondary_state( 417 if (!core_alua_set_tg_pt_secondary_state(
430 tg_pt_gp_mem, port, 1, 1)) 418 lun, 1, 1))
431 found = true; 419 found = true;
432 420
433 spin_lock(&dev->se_port_lock); 421 spin_lock(&dev->se_port_lock);
@@ -696,9 +684,7 @@ target_alua_state_check(struct se_cmd *cmd)
696 struct se_device *dev = cmd->se_dev; 684 struct se_device *dev = cmd->se_dev;
697 unsigned char *cdb = cmd->t_task_cdb; 685 unsigned char *cdb = cmd->t_task_cdb;
698 struct se_lun *lun = cmd->se_lun; 686 struct se_lun *lun = cmd->se_lun;
699 struct se_port *port = lun->lun_sep;
700 struct t10_alua_tg_pt_gp *tg_pt_gp; 687 struct t10_alua_tg_pt_gp *tg_pt_gp;
701 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
702 int out_alua_state, nonop_delay_msecs; 688 int out_alua_state, nonop_delay_msecs;
703 689
704 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE) 690 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
@@ -706,33 +692,27 @@ target_alua_state_check(struct se_cmd *cmd)
706 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 692 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
707 return 0; 693 return 0;
708 694
709 if (!port)
710 return 0;
711 /* 695 /*
712 * First, check for a struct se_port specific secondary ALUA target port 696 * First, check for a struct se_port specific secondary ALUA target port
713 * access state: OFFLINE 697 * access state: OFFLINE
714 */ 698 */
715 if (atomic_read(&port->sep_tg_pt_secondary_offline)) { 699 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
716 pr_debug("ALUA: Got secondary offline status for local" 700 pr_debug("ALUA: Got secondary offline status for local"
717 " target port\n"); 701 " target port\n");
718 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE); 702 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
719 return TCM_CHECK_CONDITION_NOT_READY; 703 return TCM_CHECK_CONDITION_NOT_READY;
720 } 704 }
721 /* 705
722 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the 706 if (!lun->lun_tg_pt_gp)
723 * ALUA target port group, to obtain current ALUA access state.
724 * Otherwise look for the underlying struct se_device association with
725 * a ALUA logical unit group.
726 */
727 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
728 if (!tg_pt_gp_mem)
729 return 0; 707 return 0;
730 708
731 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 709 spin_lock(&lun->lun_tg_pt_gp_lock);
732 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 710 tg_pt_gp = lun->lun_tg_pt_gp;
733 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); 711 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
734 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs; 712 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
735 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 713
714 // XXX: keeps using tg_pt_gp witout reference after unlock
715 spin_unlock(&lun->lun_tg_pt_gp_lock);
736 /* 716 /*
737 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional 717 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
738 * statement so the compiler knows explicitly to check this case first. 718 * statement so the compiler knows explicitly to check this case first.
@@ -764,7 +744,7 @@ target_alua_state_check(struct se_cmd *cmd)
764 break; 744 break;
765 /* 745 /*
766 * OFFLINE is a secondary ALUA target port group access state, that is 746 * OFFLINE is a secondary ALUA target port group access state, that is
767 * handled above with struct se_port->sep_tg_pt_secondary_offline=1 747 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
768 */ 748 */
769 case ALUA_ACCESS_STATE_OFFLINE: 749 case ALUA_ACCESS_STATE_OFFLINE:
770 default: 750 default:
@@ -906,10 +886,6 @@ int core_alua_check_nonop_delay(
906} 886}
907EXPORT_SYMBOL(core_alua_check_nonop_delay); 887EXPORT_SYMBOL(core_alua_check_nonop_delay);
908 888
909/*
910 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
911 *
912 */
913static int core_alua_write_tpg_metadata( 889static int core_alua_write_tpg_metadata(
914 const char *path, 890 const char *path,
915 unsigned char *md_buf, 891 unsigned char *md_buf,
@@ -971,16 +947,14 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
971 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work); 947 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
972 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; 948 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
973 struct se_dev_entry *se_deve; 949 struct se_dev_entry *se_deve;
950 struct se_lun *lun;
974 struct se_lun_acl *lacl; 951 struct se_lun_acl *lacl;
975 struct se_port *port;
976 struct t10_alua_tg_pt_gp_member *mem;
977 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status == 952 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
978 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG); 953 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
979 954
980 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 955 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
981 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list, 956 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
982 tg_pt_gp_mem_list) { 957 lun_tg_pt_gp_link) {
983 port = mem->tg_pt;
984 /* 958 /*
985 * After an implicit target port asymmetric access state 959 * After an implicit target port asymmetric access state
986 * change, a device server shall establish a unit attention 960 * change, a device server shall establish a unit attention
@@ -995,14 +969,13 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
995 * every I_T nexus other than the I_T nexus on which the SET 969 * every I_T nexus other than the I_T nexus on which the SET
996 * TARGET PORT GROUPS command 970 * TARGET PORT GROUPS command
997 */ 971 */
998 atomic_inc_mb(&mem->tg_pt_gp_mem_ref_cnt); 972 atomic_inc_mb(&lun->lun_active);
999 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 973 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1000 974
1001 spin_lock_bh(&port->sep_alua_lock); 975 spin_lock_bh(&lun->lun_deve_lock);
1002 list_for_each_entry(se_deve, &port->sep_alua_list, 976 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
1003 alua_port_list) {
1004 lacl = rcu_dereference_check(se_deve->se_lun_acl, 977 lacl = rcu_dereference_check(se_deve->se_lun_acl,
1005 lockdep_is_held(&port->sep_alua_lock)); 978 lockdep_is_held(&lun->lun_deve_lock));
1006 /* 979 /*
1007 * se_deve->se_lun_acl pointer may be NULL for a 980 * se_deve->se_lun_acl pointer may be NULL for a
1008 * entry created without explicit Node+MappedLUN ACLs 981 * entry created without explicit Node+MappedLUN ACLs
@@ -1014,18 +987,18 @@ static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
1014 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) && 987 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
1015 (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) && 988 (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
1016 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl) && 989 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl) &&
1017 (tg_pt_gp->tg_pt_gp_alua_port != NULL) && 990 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
1018 (tg_pt_gp->tg_pt_gp_alua_port == port)) 991 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
1019 continue; 992 continue;
1020 993
1021 core_scsi3_ua_allocate(lacl->se_lun_nacl, 994 core_scsi3_ua_allocate(lacl->se_lun_nacl,
1022 se_deve->mapped_lun, 0x2A, 995 se_deve->mapped_lun, 0x2A,
1023 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED); 996 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
1024 } 997 }
1025 spin_unlock_bh(&port->sep_alua_lock); 998 spin_unlock_bh(&lun->lun_deve_lock);
1026 999
1027 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1000 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1028 atomic_dec_mb(&mem->tg_pt_gp_mem_ref_cnt); 1001 atomic_dec_mb(&lun->lun_active);
1029 } 1002 }
1030 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1003 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1031 /* 1004 /*
@@ -1143,7 +1116,7 @@ static int core_alua_do_transition_tg_pt(
1143int core_alua_do_port_transition( 1116int core_alua_do_port_transition(
1144 struct t10_alua_tg_pt_gp *l_tg_pt_gp, 1117 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1145 struct se_device *l_dev, 1118 struct se_device *l_dev,
1146 struct se_port *l_port, 1119 struct se_lun *l_lun,
1147 struct se_node_acl *l_nacl, 1120 struct se_node_acl *l_nacl,
1148 int new_state, 1121 int new_state,
1149 int explicit) 1122 int explicit)
@@ -1173,7 +1146,7 @@ int core_alua_do_port_transition(
1173 * core_alua_do_transition_tg_pt() will always return 1146 * core_alua_do_transition_tg_pt() will always return
1174 * success. 1147 * success.
1175 */ 1148 */
1176 l_tg_pt_gp->tg_pt_gp_alua_port = l_port; 1149 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1177 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl; 1150 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1178 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp, 1151 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1179 new_state, explicit); 1152 new_state, explicit);
@@ -1212,10 +1185,10 @@ int core_alua_do_port_transition(
1212 continue; 1185 continue;
1213 1186
1214 if (l_tg_pt_gp == tg_pt_gp) { 1187 if (l_tg_pt_gp == tg_pt_gp) {
1215 tg_pt_gp->tg_pt_gp_alua_port = l_port; 1188 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1216 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl; 1189 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1217 } else { 1190 } else {
1218 tg_pt_gp->tg_pt_gp_alua_port = NULL; 1191 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1219 tg_pt_gp->tg_pt_gp_alua_nacl = NULL; 1192 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1220 } 1193 }
1221 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt); 1194 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
@@ -1252,22 +1225,20 @@ int core_alua_do_port_transition(
1252 return rc; 1225 return rc;
1253} 1226}
1254 1227
1255/* 1228static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1256 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1257 */
1258static int core_alua_update_tpg_secondary_metadata(
1259 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1260 struct se_port *port)
1261{ 1229{
1230 struct se_portal_group *se_tpg = lun->lun_tpg;
1262 unsigned char *md_buf; 1231 unsigned char *md_buf;
1263 struct se_portal_group *se_tpg = port->sep_tpg;
1264 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN]; 1232 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1265 int len, rc; 1233 int len, rc;
1266 1234
1235 mutex_lock(&lun->lun_tg_pt_md_mutex);
1236
1267 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL); 1237 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1268 if (!md_buf) { 1238 if (!md_buf) {
1269 pr_err("Unable to allocate buf for ALUA metadata\n"); 1239 pr_err("Unable to allocate buf for ALUA metadata\n");
1270 return -ENOMEM; 1240 rc = -ENOMEM;
1241 goto out_unlock;
1271 } 1242 }
1272 1243
1273 memset(path, 0, ALUA_METADATA_PATH_LEN); 1244 memset(path, 0, ALUA_METADATA_PATH_LEN);
@@ -1282,32 +1253,33 @@ static int core_alua_update_tpg_secondary_metadata(
1282 1253
1283 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n" 1254 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1284 "alua_tg_pt_status=0x%02x\n", 1255 "alua_tg_pt_status=0x%02x\n",
1285 atomic_read(&port->sep_tg_pt_secondary_offline), 1256 atomic_read(&lun->lun_tg_pt_secondary_offline),
1286 port->sep_tg_pt_secondary_stat); 1257 lun->lun_tg_pt_secondary_stat);
1287 1258
1288 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u", 1259 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1289 se_tpg->se_tpg_tfo->get_fabric_name(), wwn, 1260 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1290 port->sep_lun->unpacked_lun); 1261 lun->unpacked_lun);
1291 1262
1292 rc = core_alua_write_tpg_metadata(path, md_buf, len); 1263 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1293 kfree(md_buf); 1264 kfree(md_buf);
1294 1265
1266out_unlock:
1267 mutex_unlock(&lun->lun_tg_pt_md_mutex);
1295 return rc; 1268 return rc;
1296} 1269}
1297 1270
1298static int core_alua_set_tg_pt_secondary_state( 1271static int core_alua_set_tg_pt_secondary_state(
1299 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1272 struct se_lun *lun,
1300 struct se_port *port,
1301 int explicit, 1273 int explicit,
1302 int offline) 1274 int offline)
1303{ 1275{
1304 struct t10_alua_tg_pt_gp *tg_pt_gp; 1276 struct t10_alua_tg_pt_gp *tg_pt_gp;
1305 int trans_delay_msecs; 1277 int trans_delay_msecs;
1306 1278
1307 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1279 spin_lock(&lun->lun_tg_pt_gp_lock);
1308 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1280 tg_pt_gp = lun->lun_tg_pt_gp;
1309 if (!tg_pt_gp) { 1281 if (!tg_pt_gp) {
1310 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1282 spin_unlock(&lun->lun_tg_pt_gp_lock);
1311 pr_err("Unable to complete secondary state" 1283 pr_err("Unable to complete secondary state"
1312 " transition\n"); 1284 " transition\n");
1313 return -EINVAL; 1285 return -EINVAL;
@@ -1315,14 +1287,14 @@ static int core_alua_set_tg_pt_secondary_state(
1315 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs; 1287 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1316 /* 1288 /*
1317 * Set the secondary ALUA target port access state to OFFLINE 1289 * Set the secondary ALUA target port access state to OFFLINE
1318 * or release the previously secondary state for struct se_port 1290 * or release the previously secondary state for struct se_lun
1319 */ 1291 */
1320 if (offline) 1292 if (offline)
1321 atomic_set(&port->sep_tg_pt_secondary_offline, 1); 1293 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1322 else 1294 else
1323 atomic_set(&port->sep_tg_pt_secondary_offline, 0); 1295 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1324 1296
1325 port->sep_tg_pt_secondary_stat = (explicit) ? 1297 lun->lun_tg_pt_secondary_stat = (explicit) ?
1326 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG : 1298 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1327 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA; 1299 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1328 1300
@@ -1331,7 +1303,7 @@ static int core_alua_set_tg_pt_secondary_state(
1331 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item), 1303 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1332 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE"); 1304 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1333 1305
1334 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1306 spin_unlock(&lun->lun_tg_pt_gp_lock);
1335 /* 1307 /*
1336 * Do the optional transition delay after we set the secondary 1308 * Do the optional transition delay after we set the secondary
1337 * ALUA access state. 1309 * ALUA access state.
@@ -1342,11 +1314,8 @@ static int core_alua_set_tg_pt_secondary_state(
1342 * See if we need to update the ALUA fabric port metadata for 1314 * See if we need to update the ALUA fabric port metadata for
1343 * secondary state and status 1315 * secondary state and status
1344 */ 1316 */
1345 if (port->sep_tg_pt_secondary_write_md) { 1317 if (lun->lun_tg_pt_secondary_write_md)
1346 mutex_lock(&port->sep_tg_pt_md_mutex); 1318 core_alua_update_tpg_secondary_metadata(lun);
1347 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port);
1348 mutex_unlock(&port->sep_tg_pt_md_mutex);
1349 }
1350 1319
1351 return 0; 1320 return 0;
1352} 1321}
@@ -1700,7 +1669,7 @@ struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1700 return NULL; 1669 return NULL;
1701 } 1670 }
1702 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list); 1671 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1703 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list); 1672 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
1704 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex); 1673 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1705 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock); 1674 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1706 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0); 1675 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
@@ -1794,32 +1763,11 @@ again:
1794 return 0; 1763 return 0;
1795} 1764}
1796 1765
1797struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1798 struct se_port *port)
1799{
1800 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1801
1802 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1803 GFP_KERNEL);
1804 if (!tg_pt_gp_mem) {
1805 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1806 return ERR_PTR(-ENOMEM);
1807 }
1808 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1809 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1810 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1811
1812 tg_pt_gp_mem->tg_pt = port;
1813 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1814
1815 return tg_pt_gp_mem;
1816}
1817
1818void core_alua_free_tg_pt_gp( 1766void core_alua_free_tg_pt_gp(
1819 struct t10_alua_tg_pt_gp *tg_pt_gp) 1767 struct t10_alua_tg_pt_gp *tg_pt_gp)
1820{ 1768{
1821 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev; 1769 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1822 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp; 1770 struct se_lun *lun, *next;
1823 1771
1824 /* 1772 /*
1825 * Once we have reached this point, config_item_put() has already 1773 * Once we have reached this point, config_item_put() has already
@@ -1850,30 +1798,24 @@ void core_alua_free_tg_pt_gp(
1850 * struct se_port. 1798 * struct se_port.
1851 */ 1799 */
1852 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1800 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1853 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp, 1801 list_for_each_entry_safe(lun, next,
1854 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) { 1802 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1855 if (tg_pt_gp_mem->tg_pt_gp_assoc) { 1803 list_del_init(&lun->lun_tg_pt_gp_link);
1856 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1804 tg_pt_gp->tg_pt_gp_members--;
1857 tg_pt_gp->tg_pt_gp_members--; 1805
1858 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1859 }
1860 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1806 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1861 /* 1807 /*
1862 * tg_pt_gp_mem is associated with a single
1863 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1864 * core_alua_free_tg_pt_gp_mem().
1865 *
1866 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp, 1808 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1867 * assume we want to re-associate a given tg_pt_gp_mem with 1809 * assume we want to re-associate a given tg_pt_gp_mem with
1868 * default_tg_pt_gp. 1810 * default_tg_pt_gp.
1869 */ 1811 */
1870 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1812 spin_lock(&lun->lun_tg_pt_gp_lock);
1871 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) { 1813 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1872 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, 1814 __target_attach_tg_pt_gp(lun,
1873 dev->t10_alua.default_tg_pt_gp); 1815 dev->t10_alua.default_tg_pt_gp);
1874 } else 1816 } else
1875 tg_pt_gp_mem->tg_pt_gp = NULL; 1817 lun->lun_tg_pt_gp = NULL;
1876 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1818 spin_unlock(&lun->lun_tg_pt_gp_lock);
1877 1819
1878 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1820 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1879 } 1821 }
@@ -1882,35 +1824,6 @@ void core_alua_free_tg_pt_gp(
1882 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp); 1824 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1883} 1825}
1884 1826
1885void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1886{
1887 struct t10_alua_tg_pt_gp *tg_pt_gp;
1888 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1889
1890 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1891 if (!tg_pt_gp_mem)
1892 return;
1893
1894 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1895 cpu_relax();
1896
1897 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1898 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1899 if (tg_pt_gp) {
1900 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1901 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1902 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1903 tg_pt_gp->tg_pt_gp_members--;
1904 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1905 }
1906 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1907 tg_pt_gp_mem->tg_pt_gp = NULL;
1908 }
1909 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1910
1911 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1912}
1913
1914static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name( 1827static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1915 struct se_device *dev, const char *name) 1828 struct se_device *dev, const char *name)
1916{ 1829{
@@ -1944,50 +1857,58 @@ static void core_alua_put_tg_pt_gp_from_name(
1944 spin_unlock(&dev->t10_alua.tg_pt_gps_lock); 1857 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1945} 1858}
1946 1859
1947/* 1860static void __target_attach_tg_pt_gp(struct se_lun *lun,
1948 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held 1861 struct t10_alua_tg_pt_gp *tg_pt_gp)
1949 */
1950void __core_alua_attach_tg_pt_gp_mem(
1951 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1952 struct t10_alua_tg_pt_gp *tg_pt_gp)
1953{ 1862{
1863 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1864
1954 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1865 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1955 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp; 1866 lun->lun_tg_pt_gp = tg_pt_gp;
1956 tg_pt_gp_mem->tg_pt_gp_assoc = 1; 1867 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
1957 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1958 &tg_pt_gp->tg_pt_gp_mem_list);
1959 tg_pt_gp->tg_pt_gp_members++; 1868 tg_pt_gp->tg_pt_gp_members++;
1960 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1869 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1961} 1870}
1962 1871
1963/* 1872void target_attach_tg_pt_gp(struct se_lun *lun,
1964 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held 1873 struct t10_alua_tg_pt_gp *tg_pt_gp)
1965 */ 1874{
1966static void __core_alua_drop_tg_pt_gp_mem( 1875 spin_lock(&lun->lun_tg_pt_gp_lock);
1967 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, 1876 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1968 struct t10_alua_tg_pt_gp *tg_pt_gp) 1877 spin_unlock(&lun->lun_tg_pt_gp_lock);
1878}
1879
1880static void __target_detach_tg_pt_gp(struct se_lun *lun,
1881 struct t10_alua_tg_pt_gp *tg_pt_gp)
1969{ 1882{
1883 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1884
1970 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 1885 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1971 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list); 1886 list_del_init(&lun->lun_tg_pt_gp_link);
1972 tg_pt_gp_mem->tg_pt_gp = NULL;
1973 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1974 tg_pt_gp->tg_pt_gp_members--; 1887 tg_pt_gp->tg_pt_gp_members--;
1975 spin_unlock(&tg_pt_gp->tg_pt_gp_lock); 1888 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1889
1890 lun->lun_tg_pt_gp = NULL;
1976} 1891}
1977 1892
1978ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page) 1893void target_detach_tg_pt_gp(struct se_lun *lun)
1894{
1895 struct t10_alua_tg_pt_gp *tg_pt_gp;
1896
1897 spin_lock(&lun->lun_tg_pt_gp_lock);
1898 tg_pt_gp = lun->lun_tg_pt_gp;
1899 if (tg_pt_gp)
1900 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1901 spin_unlock(&lun->lun_tg_pt_gp_lock);
1902}
1903
1904ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
1979{ 1905{
1980 struct config_item *tg_pt_ci; 1906 struct config_item *tg_pt_ci;
1981 struct t10_alua_tg_pt_gp *tg_pt_gp; 1907 struct t10_alua_tg_pt_gp *tg_pt_gp;
1982 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1983 ssize_t len = 0; 1908 ssize_t len = 0;
1984 1909
1985 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 1910 spin_lock(&lun->lun_tg_pt_gp_lock);
1986 if (!tg_pt_gp_mem) 1911 tg_pt_gp = lun->lun_tg_pt_gp;
1987 return len;
1988
1989 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1990 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1991 if (tg_pt_gp) { 1912 if (tg_pt_gp) {
1992 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item; 1913 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1993 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:" 1914 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
@@ -1999,34 +1920,29 @@ ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1999 &tg_pt_gp->tg_pt_gp_alua_access_state)), 1920 &tg_pt_gp->tg_pt_gp_alua_access_state)),
2000 core_alua_dump_status( 1921 core_alua_dump_status(
2001 tg_pt_gp->tg_pt_gp_alua_access_status), 1922 tg_pt_gp->tg_pt_gp_alua_access_status),
2002 (atomic_read(&port->sep_tg_pt_secondary_offline)) ? 1923 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
2003 "Offline" : "None", 1924 "Offline" : "None",
2004 core_alua_dump_status(port->sep_tg_pt_secondary_stat)); 1925 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
2005 } 1926 }
2006 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1927 spin_unlock(&lun->lun_tg_pt_gp_lock);
2007 1928
2008 return len; 1929 return len;
2009} 1930}
2010 1931
2011ssize_t core_alua_store_tg_pt_gp_info( 1932ssize_t core_alua_store_tg_pt_gp_info(
2012 struct se_port *port, 1933 struct se_lun *lun,
2013 const char *page, 1934 const char *page,
2014 size_t count) 1935 size_t count)
2015{ 1936{
2016 struct se_portal_group *tpg; 1937 struct se_portal_group *tpg = lun->lun_tpg;
2017 struct se_lun *lun; 1938 struct se_device *dev = lun->lun_se_dev;
2018 struct se_device *dev = port->sep_lun->lun_se_dev;
2019 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL; 1939 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
2020 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2021 unsigned char buf[TG_PT_GROUP_NAME_BUF]; 1940 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2022 int move = 0; 1941 int move = 0;
2023 1942
2024 tpg = port->sep_tpg; 1943 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2025 lun = port->sep_lun; 1944 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2026 1945 return -ENODEV;
2027 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
2028 if (!tg_pt_gp_mem)
2029 return 0;
2030 1946
2031 if (count > TG_PT_GROUP_NAME_BUF) { 1947 if (count > TG_PT_GROUP_NAME_BUF) {
2032 pr_err("ALUA Target Port Group alias too large!\n"); 1948 pr_err("ALUA Target Port Group alias too large!\n");
@@ -2050,8 +1966,8 @@ ssize_t core_alua_store_tg_pt_gp_info(
2050 return -ENODEV; 1966 return -ENODEV;
2051 } 1967 }
2052 1968
2053 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1969 spin_lock(&lun->lun_tg_pt_gp_lock);
2054 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; 1970 tg_pt_gp = lun->lun_tg_pt_gp;
2055 if (tg_pt_gp) { 1971 if (tg_pt_gp) {
2056 /* 1972 /*
2057 * Clearing an existing tg_pt_gp association, and replacing 1973 * Clearing an existing tg_pt_gp association, and replacing
@@ -2069,24 +1985,19 @@ ssize_t core_alua_store_tg_pt_gp_info(
2069 &tg_pt_gp->tg_pt_gp_group.cg_item), 1985 &tg_pt_gp->tg_pt_gp_group.cg_item),
2070 tg_pt_gp->tg_pt_gp_id); 1986 tg_pt_gp->tg_pt_gp_id);
2071 1987
2072 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp); 1988 __target_detach_tg_pt_gp(lun, tg_pt_gp);
2073 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, 1989 __target_attach_tg_pt_gp(lun,
2074 dev->t10_alua.default_tg_pt_gp); 1990 dev->t10_alua.default_tg_pt_gp);
2075 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 1991 spin_unlock(&lun->lun_tg_pt_gp_lock);
2076 1992
2077 return count; 1993 return count;
2078 } 1994 }
2079 /* 1995 __target_detach_tg_pt_gp(lun, tg_pt_gp);
2080 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
2081 */
2082 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
2083 move = 1; 1996 move = 1;
2084 } 1997 }
2085 /* 1998
2086 * Associate tg_pt_gp_mem with tg_pt_gp_new. 1999 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2087 */ 2000 spin_unlock(&lun->lun_tg_pt_gp_lock);
2088 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
2089 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
2090 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA" 2001 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2091 " Target Port Group: alua/%s, ID: %hu\n", (move) ? 2002 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
2092 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg), 2003 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
@@ -2269,11 +2180,8 @@ ssize_t core_alua_store_preferred_bit(
2269 2180
2270ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page) 2181ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2271{ 2182{
2272 if (!lun->lun_sep)
2273 return -ENODEV;
2274
2275 return sprintf(page, "%d\n", 2183 return sprintf(page, "%d\n",
2276 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline)); 2184 atomic_read(&lun->lun_tg_pt_secondary_offline));
2277} 2185}
2278 2186
2279ssize_t core_alua_store_offline_bit( 2187ssize_t core_alua_store_offline_bit(
@@ -2281,11 +2189,12 @@ ssize_t core_alua_store_offline_bit(
2281 const char *page, 2189 const char *page,
2282 size_t count) 2190 size_t count)
2283{ 2191{
2284 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; 2192 struct se_device *dev = lun->lun_se_dev;
2285 unsigned long tmp; 2193 unsigned long tmp;
2286 int ret; 2194 int ret;
2287 2195
2288 if (!lun->lun_sep) 2196 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2197 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2289 return -ENODEV; 2198 return -ENODEV;
2290 2199
2291 ret = kstrtoul(page, 0, &tmp); 2200 ret = kstrtoul(page, 0, &tmp);
@@ -2298,14 +2207,8 @@ ssize_t core_alua_store_offline_bit(
2298 tmp); 2207 tmp);
2299 return -EINVAL; 2208 return -EINVAL;
2300 } 2209 }
2301 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
2302 if (!tg_pt_gp_mem) {
2303 pr_err("Unable to locate *tg_pt_gp_mem\n");
2304 return -EINVAL;
2305 }
2306 2210
2307 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem, 2211 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
2308 lun->lun_sep, 0, (int)tmp);
2309 if (ret < 0) 2212 if (ret < 0)
2310 return -EINVAL; 2213 return -EINVAL;
2311 2214
@@ -2316,7 +2219,7 @@ ssize_t core_alua_show_secondary_status(
2316 struct se_lun *lun, 2219 struct se_lun *lun,
2317 char *page) 2220 char *page)
2318{ 2221{
2319 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat); 2222 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
2320} 2223}
2321 2224
2322ssize_t core_alua_store_secondary_status( 2225ssize_t core_alua_store_secondary_status(
@@ -2339,7 +2242,7 @@ ssize_t core_alua_store_secondary_status(
2339 tmp); 2242 tmp);
2340 return -EINVAL; 2243 return -EINVAL;
2341 } 2244 }
2342 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp; 2245 lun->lun_tg_pt_secondary_stat = (int)tmp;
2343 2246
2344 return count; 2247 return count;
2345} 2248}
@@ -2348,8 +2251,7 @@ ssize_t core_alua_show_secondary_write_metadata(
2348 struct se_lun *lun, 2251 struct se_lun *lun,
2349 char *page) 2252 char *page)
2350{ 2253{
2351 return sprintf(page, "%d\n", 2254 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
2352 lun->lun_sep->sep_tg_pt_secondary_write_md);
2353} 2255}
2354 2256
2355ssize_t core_alua_store_secondary_write_metadata( 2257ssize_t core_alua_store_secondary_write_metadata(
@@ -2370,7 +2272,7 @@ ssize_t core_alua_store_secondary_write_metadata(
2370 " %lu\n", tmp); 2272 " %lu\n", tmp);
2371 return -EINVAL; 2273 return -EINVAL;
2372 } 2274 }
2373 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp; 2275 lun->lun_tg_pt_secondary_write_md = (int)tmp;
2374 2276
2375 return count; 2277 return count;
2376} 2278}
diff --git a/drivers/target/target_core_alua.h b/drivers/target/target_core_alua.h
index 0a7d65e80404..9b250f9b33bf 100644
--- a/drivers/target/target_core_alua.h
+++ b/drivers/target/target_core_alua.h
@@ -85,7 +85,6 @@
85extern struct kmem_cache *t10_alua_lu_gp_cache; 85extern struct kmem_cache *t10_alua_lu_gp_cache;
86extern struct kmem_cache *t10_alua_lu_gp_mem_cache; 86extern struct kmem_cache *t10_alua_lu_gp_mem_cache;
87extern struct kmem_cache *t10_alua_tg_pt_gp_cache; 87extern struct kmem_cache *t10_alua_tg_pt_gp_cache;
88extern struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
89extern struct kmem_cache *t10_alua_lba_map_cache; 88extern struct kmem_cache *t10_alua_lba_map_cache;
90extern struct kmem_cache *t10_alua_lba_map_mem_cache; 89extern struct kmem_cache *t10_alua_lba_map_mem_cache;
91 90
@@ -94,7 +93,7 @@ extern sense_reason_t target_emulate_set_target_port_groups(struct se_cmd *);
94extern sense_reason_t target_emulate_report_referrals(struct se_cmd *); 93extern sense_reason_t target_emulate_report_referrals(struct se_cmd *);
95extern int core_alua_check_nonop_delay(struct se_cmd *); 94extern int core_alua_check_nonop_delay(struct se_cmd *);
96extern int core_alua_do_port_transition(struct t10_alua_tg_pt_gp *, 95extern int core_alua_do_port_transition(struct t10_alua_tg_pt_gp *,
97 struct se_device *, struct se_port *, 96 struct se_device *, struct se_lun *,
98 struct se_node_acl *, int, int); 97 struct se_node_acl *, int, int);
99extern char *core_alua_dump_status(int); 98extern char *core_alua_dump_status(int);
100extern struct t10_alua_lba_map *core_alua_allocate_lba_map( 99extern struct t10_alua_lba_map *core_alua_allocate_lba_map(
@@ -117,14 +116,11 @@ extern void core_alua_drop_lu_gp_dev(struct se_device *);
117extern struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp( 116extern struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
118 struct se_device *, const char *, int); 117 struct se_device *, const char *, int);
119extern int core_alua_set_tg_pt_gp_id(struct t10_alua_tg_pt_gp *, u16); 118extern int core_alua_set_tg_pt_gp_id(struct t10_alua_tg_pt_gp *, u16);
120extern struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
121 struct se_port *);
122extern void core_alua_free_tg_pt_gp(struct t10_alua_tg_pt_gp *); 119extern void core_alua_free_tg_pt_gp(struct t10_alua_tg_pt_gp *);
123extern void core_alua_free_tg_pt_gp_mem(struct se_port *); 120extern void target_detach_tg_pt_gp(struct se_lun *);
124extern void __core_alua_attach_tg_pt_gp_mem(struct t10_alua_tg_pt_gp_member *, 121extern void target_attach_tg_pt_gp(struct se_lun *, struct t10_alua_tg_pt_gp *);
125 struct t10_alua_tg_pt_gp *); 122extern ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *, char *);
126extern ssize_t core_alua_show_tg_pt_gp_info(struct se_port *, char *); 123extern ssize_t core_alua_store_tg_pt_gp_info(struct se_lun *, const char *,
127extern ssize_t core_alua_store_tg_pt_gp_info(struct se_port *, const char *,
128 size_t); 124 size_t);
129extern ssize_t core_alua_show_access_type(struct t10_alua_tg_pt_gp *, char *); 125extern ssize_t core_alua_show_access_type(struct t10_alua_tg_pt_gp *, char *);
130extern ssize_t core_alua_store_access_type(struct t10_alua_tg_pt_gp *, 126extern ssize_t core_alua_store_access_type(struct t10_alua_tg_pt_gp *,
diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 4313eea060ed..f97b969e6714 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -2889,21 +2889,16 @@ static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2889 struct t10_alua_tg_pt_gp *tg_pt_gp, 2889 struct t10_alua_tg_pt_gp *tg_pt_gp,
2890 char *page) 2890 char *page)
2891{ 2891{
2892 struct se_port *port;
2893 struct se_portal_group *tpg;
2894 struct se_lun *lun; 2892 struct se_lun *lun;
2895 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2896 ssize_t len = 0, cur_len; 2893 ssize_t len = 0, cur_len;
2897 unsigned char buf[TG_PT_GROUP_NAME_BUF]; 2894 unsigned char buf[TG_PT_GROUP_NAME_BUF];
2898 2895
2899 memset(buf, 0, TG_PT_GROUP_NAME_BUF); 2896 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2900 2897
2901 spin_lock(&tg_pt_gp->tg_pt_gp_lock); 2898 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2902 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list, 2899 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
2903 tg_pt_gp_mem_list) { 2900 lun_tg_pt_gp_link) {
2904 port = tg_pt_gp_mem->tg_pt; 2901 struct se_portal_group *tpg = lun->lun_tpg;
2905 tpg = port->sep_tpg;
2906 lun = port->sep_lun;
2907 2902
2908 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu" 2903 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2909 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(), 2904 "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c
index 1e4485b1849d..8485e9a789fc 100644
--- a/drivers/target/target_core_device.c
+++ b/drivers/target/target_core_device.c
@@ -120,8 +120,8 @@ transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
120 (se_cmd->data_direction != DMA_NONE)) 120 (se_cmd->data_direction != DMA_NONE))
121 return TCM_WRITE_PROTECTED; 121 return TCM_WRITE_PROTECTED;
122 122
123 se_lun = &se_sess->se_tpg->tpg_virt_lun0; 123 se_lun = se_sess->se_tpg->tpg_virt_lun0;
124 se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0; 124 se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
125 se_cmd->orig_fe_lun = 0; 125 se_cmd->orig_fe_lun = 0;
126 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; 126 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
127 127
@@ -309,7 +309,6 @@ int core_enable_device_list_for_node(
309 struct se_node_acl *nacl, 309 struct se_node_acl *nacl,
310 struct se_portal_group *tpg) 310 struct se_portal_group *tpg)
311{ 311{
312 struct se_port *port = lun->lun_sep;
313 struct se_dev_entry *orig, *new; 312 struct se_dev_entry *orig, *new;
314 313
315 new = kzalloc(sizeof(*new), GFP_KERNEL); 314 new = kzalloc(sizeof(*new), GFP_KERNEL);
@@ -320,8 +319,8 @@ int core_enable_device_list_for_node(
320 319
321 atomic_set(&new->ua_count, 0); 320 atomic_set(&new->ua_count, 0);
322 spin_lock_init(&new->ua_lock); 321 spin_lock_init(&new->ua_lock);
323 INIT_LIST_HEAD(&new->alua_port_list);
324 INIT_LIST_HEAD(&new->ua_list); 322 INIT_LIST_HEAD(&new->ua_list);
323 INIT_LIST_HEAD(&new->lun_link);
325 324
326 new->mapped_lun = mapped_lun; 325 new->mapped_lun = mapped_lun;
327 kref_init(&new->pr_kref); 326 kref_init(&new->pr_kref);
@@ -357,10 +356,10 @@ int core_enable_device_list_for_node(
357 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist); 356 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
358 mutex_unlock(&nacl->lun_entry_mutex); 357 mutex_unlock(&nacl->lun_entry_mutex);
359 358
360 spin_lock_bh(&port->sep_alua_lock); 359 spin_lock_bh(&lun->lun_deve_lock);
361 list_del(&orig->alua_port_list); 360 list_del(&orig->lun_link);
362 list_add_tail(&new->alua_port_list, &port->sep_alua_list); 361 list_add_tail(&new->lun_link, &lun->lun_deve_list);
363 spin_unlock_bh(&port->sep_alua_lock); 362 spin_unlock_bh(&lun->lun_deve_lock);
364 363
365 kref_put(&orig->pr_kref, target_pr_kref_release); 364 kref_put(&orig->pr_kref, target_pr_kref_release);
366 wait_for_completion(&orig->pr_comp); 365 wait_for_completion(&orig->pr_comp);
@@ -374,9 +373,9 @@ int core_enable_device_list_for_node(
374 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist); 373 hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
375 mutex_unlock(&nacl->lun_entry_mutex); 374 mutex_unlock(&nacl->lun_entry_mutex);
376 375
377 spin_lock_bh(&port->sep_alua_lock); 376 spin_lock_bh(&lun->lun_deve_lock);
378 list_add_tail(&new->alua_port_list, &port->sep_alua_list); 377 list_add_tail(&new->lun_link, &lun->lun_deve_list);
379 spin_unlock_bh(&port->sep_alua_lock); 378 spin_unlock_bh(&lun->lun_deve_lock);
380 379
381 return 0; 380 return 0;
382} 381}
@@ -390,23 +389,22 @@ void core_disable_device_list_for_node(
390 struct se_node_acl *nacl, 389 struct se_node_acl *nacl,
391 struct se_portal_group *tpg) 390 struct se_portal_group *tpg)
392{ 391{
393 struct se_port *port = lun->lun_sep;
394 /* 392 /*
395 * If the MappedLUN entry is being disabled, the entry in 393 * If the MappedLUN entry is being disabled, the entry in
396 * port->sep_alua_list must be removed now before clearing the 394 * lun->lun_deve_list must be removed now before clearing the
397 * struct se_dev_entry pointers below as logic in 395 * struct se_dev_entry pointers below as logic in
398 * core_alua_do_transition_tg_pt() depends on these being present. 396 * core_alua_do_transition_tg_pt() depends on these being present.
399 * 397 *
400 * deve->se_lun_acl will be NULL for demo-mode created LUNs 398 * deve->se_lun_acl will be NULL for demo-mode created LUNs
401 * that have not been explicitly converted to MappedLUNs -> 399 * that have not been explicitly converted to MappedLUNs ->
402 * struct se_lun_acl, but we remove deve->alua_port_list from 400 * struct se_lun_acl, but we remove deve->lun_link from
403 * port->sep_alua_list. This also means that active UAs and 401 * lun->lun_deve_list. This also means that active UAs and
404 * NodeACL context specific PR metadata for demo-mode 402 * NodeACL context specific PR metadata for demo-mode
405 * MappedLUN *deve will be released below.. 403 * MappedLUN *deve will be released below..
406 */ 404 */
407 spin_lock_bh(&port->sep_alua_lock); 405 spin_lock_bh(&lun->lun_deve_lock);
408 list_del(&orig->alua_port_list); 406 list_del(&orig->lun_link);
409 spin_unlock_bh(&port->sep_alua_lock); 407 spin_unlock_bh(&lun->lun_deve_lock);
410 /* 408 /*
411 * Disable struct se_dev_entry LUN ACL mapping 409 * Disable struct se_dev_entry LUN ACL mapping
412 */ 410 */
@@ -458,27 +456,16 @@ void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
458 mutex_unlock(&tpg->acl_node_mutex); 456 mutex_unlock(&tpg->acl_node_mutex);
459} 457}
460 458
461static struct se_port *core_alloc_port(struct se_device *dev) 459int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
462{ 460{
463 struct se_port *port, *port_tmp; 461 struct se_lun *tmp;
464
465 port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
466 if (!port) {
467 pr_err("Unable to allocate struct se_port\n");
468 return ERR_PTR(-ENOMEM);
469 }
470 INIT_LIST_HEAD(&port->sep_alua_list);
471 INIT_LIST_HEAD(&port->sep_list);
472 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
473 spin_lock_init(&port->sep_alua_lock);
474 mutex_init(&port->sep_tg_pt_md_mutex);
475 462
476 spin_lock(&dev->se_port_lock); 463 spin_lock(&dev->se_port_lock);
477 if (dev->dev_port_count == 0x0000ffff) { 464 if (dev->export_count == 0x0000ffff) {
478 pr_warn("Reached dev->dev_port_count ==" 465 pr_warn("Reached dev->dev_port_count =="
479 " 0x0000ffff\n"); 466 " 0x0000ffff\n");
480 spin_unlock(&dev->se_port_lock); 467 spin_unlock(&dev->se_port_lock);
481 return ERR_PTR(-ENOSPC); 468 return -ENOSPC;
482 } 469 }
483again: 470again:
484 /* 471 /*
@@ -493,135 +480,23 @@ again:
493 * 2h Relative port 2, historically known as port B 480 * 2h Relative port 2, historically known as port B
494 * 3h to FFFFh Relative port 3 through 65 535 481 * 3h to FFFFh Relative port 3 through 65 535
495 */ 482 */
496 port->sep_rtpi = dev->dev_rpti_counter++; 483 lun->lun_rtpi = dev->dev_rpti_counter++;
497 if (!port->sep_rtpi) 484 if (!lun->lun_rtpi)
498 goto again; 485 goto again;
499 486
500 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) { 487 list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
501 /* 488 /*
502 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique 489 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
503 * for 16-bit wrap.. 490 * for 16-bit wrap..
504 */ 491 */
505 if (port->sep_rtpi == port_tmp->sep_rtpi) 492 if (lun->lun_rtpi == tmp->lun_rtpi)
506 goto again; 493 goto again;
507 } 494 }
508 spin_unlock(&dev->se_port_lock); 495 spin_unlock(&dev->se_port_lock);
509 496
510 return port;
511}
512
513static void core_export_port(
514 struct se_device *dev,
515 struct se_portal_group *tpg,
516 struct se_port *port,
517 struct se_lun *lun)
518{
519 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
520
521 spin_lock(&dev->se_port_lock);
522 spin_lock(&lun->lun_sep_lock);
523 port->sep_tpg = tpg;
524 port->sep_lun = lun;
525 lun->lun_sep = port;
526 spin_unlock(&lun->lun_sep_lock);
527
528 list_add_tail(&port->sep_list, &dev->dev_sep_list);
529 spin_unlock(&dev->se_port_lock);
530
531 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
532 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
533 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
534 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
535 pr_err("Unable to allocate t10_alua_tg_pt"
536 "_gp_member_t\n");
537 return;
538 }
539 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
540 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
541 dev->t10_alua.default_tg_pt_gp);
542 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
543 pr_debug("%s/%s: Adding to default ALUA Target Port"
544 " Group: alua/default_tg_pt_gp\n",
545 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
546 }
547
548 dev->dev_port_count++;
549 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
550}
551
552/*
553 * Called with struct se_device->se_port_lock spinlock held.
554 */
555static void core_release_port(struct se_device *dev, struct se_port *port)
556 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
557{
558 /*
559 * Wait for any port reference for PR ALL_TG_PT=1 operation
560 * to complete in __core_scsi3_alloc_registration()
561 */
562 spin_unlock(&dev->se_port_lock);
563 if (atomic_read(&port->sep_tg_pt_ref_cnt))
564 cpu_relax();
565 spin_lock(&dev->se_port_lock);
566
567 core_alua_free_tg_pt_gp_mem(port);
568
569 list_del(&port->sep_list);
570 dev->dev_port_count--;
571 kfree(port);
572}
573
574int core_dev_export(
575 struct se_device *dev,
576 struct se_portal_group *tpg,
577 struct se_lun *lun)
578{
579 struct se_hba *hba = dev->se_hba;
580 struct se_port *port;
581
582 port = core_alloc_port(dev);
583 if (IS_ERR(port))
584 return PTR_ERR(port);
585
586 lun->lun_index = dev->dev_index;
587 lun->lun_se_dev = dev;
588 lun->lun_rtpi = port->sep_rtpi;
589
590 spin_lock(&hba->device_lock);
591 dev->export_count++;
592 spin_unlock(&hba->device_lock);
593
594 core_export_port(dev, tpg, port, lun);
595 return 0; 497 return 0;
596} 498}
597 499
598void core_dev_unexport(
599 struct se_device *dev,
600 struct se_portal_group *tpg,
601 struct se_lun *lun)
602{
603 struct se_hba *hba = dev->se_hba;
604 struct se_port *port = lun->lun_sep;
605
606 spin_lock(&lun->lun_sep_lock);
607 if (lun->lun_se_dev == NULL) {
608 spin_unlock(&lun->lun_sep_lock);
609 return;
610 }
611 spin_unlock(&lun->lun_sep_lock);
612
613 spin_lock(&dev->se_port_lock);
614 core_release_port(dev, port);
615 spin_unlock(&dev->se_port_lock);
616
617 spin_lock(&hba->device_lock);
618 dev->export_count--;
619 spin_unlock(&hba->device_lock);
620
621 lun->lun_sep = NULL;
622 lun->lun_se_dev = NULL;
623}
624
625static void se_release_vpd_for_dev(struct se_device *dev) 500static void se_release_vpd_for_dev(struct se_device *dev)
626{ 501{
627 struct t10_vpd *vpd, *vpd_tmp; 502 struct t10_vpd *vpd, *vpd_tmp;
@@ -783,10 +658,10 @@ int core_dev_add_initiator_node_lun_acl(
783} 658}
784 659
785int core_dev_del_initiator_node_lun_acl( 660int core_dev_del_initiator_node_lun_acl(
786 struct se_portal_group *tpg,
787 struct se_lun *lun, 661 struct se_lun *lun,
788 struct se_lun_acl *lacl) 662 struct se_lun_acl *lacl)
789{ 663{
664 struct se_portal_group *tpg = lun->lun_tpg;
790 struct se_node_acl *nacl; 665 struct se_node_acl *nacl;
791 struct se_dev_entry *deve; 666 struct se_dev_entry *deve;
792 667
@@ -930,6 +805,10 @@ struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
930 xcopy_lun->lun_se_dev = dev; 805 xcopy_lun->lun_se_dev = dev;
931 spin_lock_init(&xcopy_lun->lun_sep_lock); 806 spin_lock_init(&xcopy_lun->lun_sep_lock);
932 init_completion(&xcopy_lun->lun_ref_comp); 807 init_completion(&xcopy_lun->lun_ref_comp);
808 INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
809 INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
810 mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
811 xcopy_lun->lun_tpg = &xcopy_pt_tpg;
933 812
934 return dev; 813 return dev;
935} 814}
diff --git a/drivers/target/target_core_fabric_configfs.c b/drivers/target/target_core_fabric_configfs.c
index 9be8030e016f..0ee182fce1a6 100644
--- a/drivers/target/target_core_fabric_configfs.c
+++ b/drivers/target/target_core_fabric_configfs.c
@@ -91,12 +91,11 @@ static int target_fabric_mappedlun_link(
91 /* 91 /*
92 * Ensure that the source port exists 92 * Ensure that the source port exists
93 */ 93 */
94 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) { 94 if (!lun->lun_se_dev) {
95 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep" 95 pr_err("Source se_lun->lun_se_dev does not exist\n");
96 "_tpg does not exist\n");
97 return -EINVAL; 96 return -EINVAL;
98 } 97 }
99 se_tpg = lun->lun_sep->sep_tpg; 98 se_tpg = lun->lun_tpg;
100 99
101 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item; 100 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
102 tpg_ci = &nacl_ci->ci_group->cg_item; 101 tpg_ci = &nacl_ci->ci_group->cg_item;
@@ -150,9 +149,8 @@ static int target_fabric_mappedlun_unlink(
150 struct se_lun_acl, se_lun_group); 149 struct se_lun_acl, se_lun_group);
151 struct se_lun *lun = container_of(to_config_group(lun_ci), 150 struct se_lun *lun = container_of(to_config_group(lun_ci),
152 struct se_lun, lun_group); 151 struct se_lun, lun_group);
153 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
154 152
155 return core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl); 153 return core_dev_del_initiator_node_lun_acl(lun, lacl);
156} 154}
157 155
158CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl); 156CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
@@ -643,10 +641,10 @@ static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
643 struct se_lun *lun, 641 struct se_lun *lun,
644 char *page) 642 char *page)
645{ 643{
646 if (!lun || !lun->lun_sep) 644 if (!lun || !lun->lun_se_dev)
647 return -ENODEV; 645 return -ENODEV;
648 646
649 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page); 647 return core_alua_show_tg_pt_gp_info(lun, page);
650} 648}
651 649
652static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp( 650static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
@@ -654,10 +652,10 @@ static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
654 const char *page, 652 const char *page,
655 size_t count) 653 size_t count)
656{ 654{
657 if (!lun || !lun->lun_sep) 655 if (!lun || !lun->lun_se_dev)
658 return -ENODEV; 656 return -ENODEV;
659 657
660 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count); 658 return core_alua_store_tg_pt_gp_info(lun, page, count);
661} 659}
662 660
663TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR); 661TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
@@ -669,7 +667,7 @@ static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
669 struct se_lun *lun, 667 struct se_lun *lun,
670 char *page) 668 char *page)
671{ 669{
672 if (!lun || !lun->lun_sep) 670 if (!lun || !lun->lun_se_dev)
673 return -ENODEV; 671 return -ENODEV;
674 672
675 return core_alua_show_offline_bit(lun, page); 673 return core_alua_show_offline_bit(lun, page);
@@ -680,7 +678,7 @@ static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
680 const char *page, 678 const char *page,
681 size_t count) 679 size_t count)
682{ 680{
683 if (!lun || !lun->lun_sep) 681 if (!lun || !lun->lun_se_dev)
684 return -ENODEV; 682 return -ENODEV;
685 683
686 return core_alua_store_offline_bit(lun, page, count); 684 return core_alua_store_offline_bit(lun, page, count);
@@ -695,7 +693,7 @@ static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
695 struct se_lun *lun, 693 struct se_lun *lun,
696 char *page) 694 char *page)
697{ 695{
698 if (!lun || !lun->lun_sep) 696 if (!lun || !lun->lun_se_dev)
699 return -ENODEV; 697 return -ENODEV;
700 698
701 return core_alua_show_secondary_status(lun, page); 699 return core_alua_show_secondary_status(lun, page);
@@ -706,7 +704,7 @@ static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
706 const char *page, 704 const char *page,
707 size_t count) 705 size_t count)
708{ 706{
709 if (!lun || !lun->lun_sep) 707 if (!lun || !lun->lun_se_dev)
710 return -ENODEV; 708 return -ENODEV;
711 709
712 return core_alua_store_secondary_status(lun, page, count); 710 return core_alua_store_secondary_status(lun, page, count);
@@ -721,7 +719,7 @@ static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
721 struct se_lun *lun, 719 struct se_lun *lun,
722 char *page) 720 char *page)
723{ 721{
724 if (!lun || !lun->lun_sep) 722 if (!lun || !lun->lun_se_dev)
725 return -ENODEV; 723 return -ENODEV;
726 724
727 return core_alua_show_secondary_write_metadata(lun, page); 725 return core_alua_show_secondary_write_metadata(lun, page);
@@ -732,7 +730,7 @@ static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
732 const char *page, 730 const char *page,
733 size_t count) 731 size_t count)
734{ 732{
735 if (!lun || !lun->lun_sep) 733 if (!lun || !lun->lun_se_dev)
736 return -ENODEV; 734 return -ENODEV;
737 735
738 return core_alua_store_secondary_write_metadata(lun, page, count); 736 return core_alua_store_secondary_write_metadata(lun, page, count);
@@ -811,7 +809,7 @@ static int target_fabric_port_unlink(
811{ 809{
812 struct se_lun *lun = container_of(to_config_group(lun_ci), 810 struct se_lun *lun = container_of(to_config_group(lun_ci),
813 struct se_lun, lun_group); 811 struct se_lun, lun_group);
814 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg; 812 struct se_portal_group *se_tpg = lun->lun_tpg;
815 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf; 813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
816 814
817 if (tf->tf_ops->fabric_pre_unlink) { 815 if (tf->tf_ops->fabric_pre_unlink) {
diff --git a/drivers/target/target_core_internal.h b/drivers/target/target_core_internal.h
index 23f992ed7cae..8d8737a13e6f 100644
--- a/drivers/target/target_core_internal.h
+++ b/drivers/target/target_core_internal.h
@@ -21,6 +21,7 @@ extern struct t10_alua_lu_gp *default_lu_gp;
21extern struct mutex g_device_mutex; 21extern struct mutex g_device_mutex;
22extern struct list_head g_device_list; 22extern struct list_head g_device_list;
23 23
24int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev);
24struct se_dev_entry *core_get_se_deve_from_rtpi(struct se_node_acl *, u16); 25struct se_dev_entry *core_get_se_deve_from_rtpi(struct se_node_acl *, u16);
25void target_pr_kref_release(struct kref *); 26void target_pr_kref_release(struct kref *);
26void core_free_device_list_for_node(struct se_node_acl *, 27void core_free_device_list_for_node(struct se_node_acl *,
@@ -32,10 +33,6 @@ int core_enable_device_list_for_node(struct se_lun *, struct se_lun_acl *,
32void core_disable_device_list_for_node(struct se_lun *, struct se_dev_entry *, 33void core_disable_device_list_for_node(struct se_lun *, struct se_dev_entry *,
33 struct se_node_acl *, struct se_portal_group *); 34 struct se_node_acl *, struct se_portal_group *);
34void core_clear_lun_from_tpg(struct se_lun *, struct se_portal_group *); 35void core_clear_lun_from_tpg(struct se_lun *, struct se_portal_group *);
35int core_dev_export(struct se_device *, struct se_portal_group *,
36 struct se_lun *);
37void core_dev_unexport(struct se_device *, struct se_portal_group *,
38 struct se_lun *);
39int core_dev_add_lun(struct se_portal_group *, struct se_device *, 36int core_dev_add_lun(struct se_portal_group *, struct se_device *,
40 struct se_lun *lun); 37 struct se_lun *lun);
41void core_dev_del_lun(struct se_portal_group *, struct se_lun *); 38void core_dev_del_lun(struct se_portal_group *, struct se_lun *);
@@ -43,8 +40,8 @@ struct se_lun_acl *core_dev_init_initiator_node_lun_acl(struct se_portal_group *
43 struct se_node_acl *, u32, int *); 40 struct se_node_acl *, u32, int *);
44int core_dev_add_initiator_node_lun_acl(struct se_portal_group *, 41int core_dev_add_initiator_node_lun_acl(struct se_portal_group *,
45 struct se_lun_acl *, struct se_lun *lun, u32); 42 struct se_lun_acl *, struct se_lun *lun, u32);
46int core_dev_del_initiator_node_lun_acl(struct se_portal_group *, 43int core_dev_del_initiator_node_lun_acl(struct se_lun *,
47 struct se_lun *, struct se_lun_acl *); 44 struct se_lun_acl *);
48void core_dev_free_initiator_node_lun_acl(struct se_portal_group *, 45void core_dev_free_initiator_node_lun_acl(struct se_portal_group *,
49 struct se_lun_acl *lacl); 46 struct se_lun_acl *lacl);
50int core_dev_setup_virtual_lun0(void); 47int core_dev_setup_virtual_lun0(void);
@@ -120,4 +117,7 @@ void target_stat_setup_dev_default_groups(struct se_device *);
120void target_stat_setup_port_default_groups(struct se_lun *); 117void target_stat_setup_port_default_groups(struct se_lun *);
121void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *); 118void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *);
122 119
120/* target_core_xcopy.c */
121extern struct se_portal_group xcopy_pt_tpg;
122
123#endif /* TARGET_CORE_INTERNAL_H */ 123#endif /* TARGET_CORE_INTERNAL_H */
diff --git a/drivers/target/target_core_pr.c b/drivers/target/target_core_pr.c
index b983f8a54766..60624bb6c598 100644
--- a/drivers/target/target_core_pr.c
+++ b/drivers/target/target_core_pr.c
@@ -642,7 +642,7 @@ static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
642 pr_reg->pr_reg_deve = deve; 642 pr_reg->pr_reg_deve = deve;
643 pr_reg->pr_res_mapped_lun = mapped_lun; 643 pr_reg->pr_res_mapped_lun = mapped_lun;
644 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun; 644 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
645 pr_reg->tg_pt_sep_rtpi = lun->lun_sep->sep_rtpi; 645 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
646 pr_reg->pr_res_key = sa_res_key; 646 pr_reg->pr_res_key = sa_res_key;
647 pr_reg->pr_reg_all_tg_pt = all_tg_pt; 647 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
648 pr_reg->pr_reg_aptpl = aptpl; 648 pr_reg->pr_reg_aptpl = aptpl;
@@ -680,8 +680,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
680 struct se_dev_entry *deve_tmp; 680 struct se_dev_entry *deve_tmp;
681 struct se_node_acl *nacl_tmp; 681 struct se_node_acl *nacl_tmp;
682 struct se_lun_acl *lacl_tmp; 682 struct se_lun_acl *lacl_tmp;
683 struct se_lun *lun_tmp; 683 struct se_lun *lun_tmp, *next, *dest_lun;
684 struct se_port *port, *port_tmp;
685 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo; 684 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
686 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe; 685 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
687 int ret; 686 int ret;
@@ -704,13 +703,12 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
704 * for ALL_TG_PT=1 703 * for ALL_TG_PT=1
705 */ 704 */
706 spin_lock(&dev->se_port_lock); 705 spin_lock(&dev->se_port_lock);
707 list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) { 706 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
708 atomic_inc_mb(&port->sep_tg_pt_ref_cnt); 707 atomic_inc_mb(&lun_tmp->lun_active);
709 spin_unlock(&dev->se_port_lock); 708 spin_unlock(&dev->se_port_lock);
710 709
711 spin_lock_bh(&port->sep_alua_lock); 710 spin_lock_bh(&lun_tmp->lun_deve_lock);
712 list_for_each_entry(deve_tmp, &port->sep_alua_list, 711 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
713 alua_port_list) {
714 /* 712 /*
715 * This pointer will be NULL for demo mode MappedLUNs 713 * This pointer will be NULL for demo mode MappedLUNs
716 * that have not been make explicit via a ConfigFS 714 * that have not been make explicit via a ConfigFS
@@ -720,7 +718,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
720 continue; 718 continue;
721 719
722 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl, 720 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
723 lockdep_is_held(&port->sep_alua_lock)); 721 lockdep_is_held(&lun_tmp->lun_deve_lock));
724 nacl_tmp = lacl_tmp->se_lun_nacl; 722 nacl_tmp = lacl_tmp->se_lun_nacl;
725 /* 723 /*
726 * Skip the matching struct se_node_acl that is allocated 724 * Skip the matching struct se_node_acl that is allocated
@@ -742,7 +740,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
742 continue; 740 continue;
743 741
744 kref_get(&deve_tmp->pr_kref); 742 kref_get(&deve_tmp->pr_kref);
745 spin_unlock_bh(&port->sep_alua_lock); 743 spin_unlock_bh(&lun_tmp->lun_deve_lock);
746 /* 744 /*
747 * Grab a configfs group dependency that is released 745 * Grab a configfs group dependency that is released
748 * for the exception path at label out: below, or upon 746 * for the exception path at label out: below, or upon
@@ -753,7 +751,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
753 if (ret < 0) { 751 if (ret < 0) {
754 pr_err("core_scsi3_lunacl_depend" 752 pr_err("core_scsi3_lunacl_depend"
755 "_item() failed\n"); 753 "_item() failed\n");
756 atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 754 atomic_dec_mb(&lun->lun_active);
757 kref_put(&deve_tmp->pr_kref, target_pr_kref_release); 755 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
758 goto out; 756 goto out;
759 } 757 }
@@ -764,27 +762,27 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
764 * the original *pr_reg is processed in 762 * the original *pr_reg is processed in
765 * __core_scsi3_add_registration() 763 * __core_scsi3_add_registration()
766 */ 764 */
767 lun_tmp = rcu_dereference_check(deve_tmp->se_lun, 765 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
768 atomic_read(&deve_tmp->pr_kref.refcount) != 0); 766 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
769 767
770 pr_reg_atp = __core_scsi3_do_alloc_registration(dev, 768 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
771 nacl_tmp, lun_tmp, deve_tmp, 769 nacl_tmp, dest_lun, deve_tmp,
772 deve_tmp->mapped_lun, NULL, 770 deve_tmp->mapped_lun, NULL,
773 sa_res_key, all_tg_pt, aptpl); 771 sa_res_key, all_tg_pt, aptpl);
774 if (!pr_reg_atp) { 772 if (!pr_reg_atp) {
775 atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 773 atomic_dec_mb(&lun_tmp->lun_active);
776 core_scsi3_lunacl_undepend_item(deve_tmp); 774 core_scsi3_lunacl_undepend_item(deve_tmp);
777 goto out; 775 goto out;
778 } 776 }
779 777
780 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list, 778 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
781 &pr_reg->pr_reg_atp_list); 779 &pr_reg->pr_reg_atp_list);
782 spin_lock_bh(&port->sep_alua_lock); 780 spin_lock_bh(&lun_tmp->lun_deve_lock);
783 } 781 }
784 spin_unlock_bh(&port->sep_alua_lock); 782 spin_unlock_bh(&lun_tmp->lun_deve_lock);
785 783
786 spin_lock(&dev->se_port_lock); 784 spin_lock(&dev->se_port_lock);
787 atomic_dec_mb(&port->sep_tg_pt_ref_cnt); 785 atomic_dec_mb(&lun_tmp->lun_active);
788 } 786 }
789 spin_unlock(&dev->se_port_lock); 787 spin_unlock(&dev->se_port_lock);
790 788
@@ -938,7 +936,7 @@ static int __core_scsi3_check_aptpl_registration(
938 (pr_reg->pr_aptpl_target_lun == target_lun)) { 936 (pr_reg->pr_aptpl_target_lun == target_lun)) {
939 937
940 pr_reg->pr_reg_nacl = nacl; 938 pr_reg->pr_reg_nacl = nacl;
941 pr_reg->tg_pt_sep_rtpi = lun->lun_sep->sep_rtpi; 939 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
942 940
943 list_del(&pr_reg->pr_reg_aptpl_list); 941 list_del(&pr_reg->pr_reg_aptpl_list);
944 spin_unlock(&pr_tmpl->aptpl_reg_lock); 942 spin_unlock(&pr_tmpl->aptpl_reg_lock);
@@ -1465,7 +1463,6 @@ core_scsi3_decode_spec_i_port(
1465 int aptpl) 1463 int aptpl)
1466{ 1464{
1467 struct se_device *dev = cmd->se_dev; 1465 struct se_device *dev = cmd->se_dev;
1468 struct se_port *tmp_port;
1469 struct se_portal_group *dest_tpg = NULL, *tmp_tpg; 1466 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1470 struct se_session *se_sess = cmd->se_sess; 1467 struct se_session *se_sess = cmd->se_sess;
1471 struct se_node_acl *dest_node_acl = NULL; 1468 struct se_node_acl *dest_node_acl = NULL;
@@ -1550,16 +1547,14 @@ core_scsi3_decode_spec_i_port(
1550 ptr = &buf[28]; 1547 ptr = &buf[28];
1551 1548
1552 while (tpdl > 0) { 1549 while (tpdl > 0) {
1553 struct se_lun *dest_lun; 1550 struct se_lun *dest_lun, *tmp_lun;
1554 1551
1555 proto_ident = (ptr[0] & 0x0f); 1552 proto_ident = (ptr[0] & 0x0f);
1556 dest_tpg = NULL; 1553 dest_tpg = NULL;
1557 1554
1558 spin_lock(&dev->se_port_lock); 1555 spin_lock(&dev->se_port_lock);
1559 list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) { 1556 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1560 tmp_tpg = tmp_port->sep_tpg; 1557 tmp_tpg = tmp_lun->lun_tpg;
1561 if (!tmp_tpg)
1562 continue;
1563 1558
1564 /* 1559 /*
1565 * Look for the matching proto_ident provided by 1560 * Look for the matching proto_ident provided by
@@ -1567,7 +1562,7 @@ core_scsi3_decode_spec_i_port(
1567 */ 1562 */
1568 if (tmp_tpg->proto_id != proto_ident) 1563 if (tmp_tpg->proto_id != proto_ident)
1569 continue; 1564 continue;
1570 dest_rtpi = tmp_port->sep_rtpi; 1565 dest_rtpi = tmp_lun->lun_rtpi;
1571 1566
1572 i_str = target_parse_pr_out_transport_id(tmp_tpg, 1567 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1573 (const char *)ptr, &tid_len, &iport_ptr); 1568 (const char *)ptr, &tid_len, &iport_ptr);
@@ -3119,9 +3114,8 @@ core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3119 struct se_session *se_sess = cmd->se_sess; 3114 struct se_session *se_sess = cmd->se_sess;
3120 struct se_device *dev = cmd->se_dev; 3115 struct se_device *dev = cmd->se_dev;
3121 struct se_dev_entry *dest_se_deve = NULL; 3116 struct se_dev_entry *dest_se_deve = NULL;
3122 struct se_lun *se_lun = cmd->se_lun; 3117 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3123 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL; 3118 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3124 struct se_port *se_port;
3125 struct se_portal_group *se_tpg, *dest_se_tpg = NULL; 3119 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3126 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops; 3120 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3127 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg; 3121 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
@@ -3206,12 +3200,10 @@ core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3206 } 3200 }
3207 3201
3208 spin_lock(&dev->se_port_lock); 3202 spin_lock(&dev->se_port_lock);
3209 list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) { 3203 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3210 if (se_port->sep_rtpi != rtpi) 3204 if (tmp_lun->lun_rtpi != rtpi)
3211 continue;
3212 dest_se_tpg = se_port->sep_tpg;
3213 if (!dest_se_tpg)
3214 continue; 3205 continue;
3206 dest_se_tpg = tmp_lun->lun_tpg;
3215 dest_tf_ops = dest_se_tpg->se_tpg_tfo; 3207 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3216 if (!dest_tf_ops) 3208 if (!dest_tf_ops)
3217 continue; 3209 continue;
diff --git a/drivers/target/target_core_spc.c b/drivers/target/target_core_spc.c
index 9f995b87b8d1..34d8292a4432 100644
--- a/drivers/target/target_core_spc.c
+++ b/drivers/target/target_core_spc.c
@@ -37,10 +37,9 @@
37#include "target_core_ua.h" 37#include "target_core_ua.h"
38#include "target_core_xcopy.h" 38#include "target_core_xcopy.h"
39 39
40static void spc_fill_alua_data(struct se_port *port, unsigned char *buf) 40static void spc_fill_alua_data(struct se_lun *lun, unsigned char *buf)
41{ 41{
42 struct t10_alua_tg_pt_gp *tg_pt_gp; 42 struct t10_alua_tg_pt_gp *tg_pt_gp;
43 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
44 43
45 /* 44 /*
46 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS. 45 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
@@ -53,17 +52,11 @@ static void spc_fill_alua_data(struct se_port *port, unsigned char *buf)
53 * 52 *
54 * See spc4r17 section 6.4.2 Table 135 53 * See spc4r17 section 6.4.2 Table 135
55 */ 54 */
56 if (!port) 55 spin_lock(&lun->lun_tg_pt_gp_lock);
57 return; 56 tg_pt_gp = lun->lun_tg_pt_gp;
58 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
59 if (!tg_pt_gp_mem)
60 return;
61
62 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
63 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
64 if (tg_pt_gp) 57 if (tg_pt_gp)
65 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type; 58 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
66 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 59 spin_unlock(&lun->lun_tg_pt_gp_lock);
67} 60}
68 61
69sense_reason_t 62sense_reason_t
@@ -94,7 +87,7 @@ spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf)
94 /* 87 /*
95 * Enable SCCS and TPGS fields for Emulated ALUA 88 * Enable SCCS and TPGS fields for Emulated ALUA
96 */ 89 */
97 spc_fill_alua_data(lun->lun_sep, buf); 90 spc_fill_alua_data(lun, buf);
98 91
99 /* 92 /*
100 * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY 93 * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY
@@ -181,11 +174,9 @@ spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
181{ 174{
182 struct se_device *dev = cmd->se_dev; 175 struct se_device *dev = cmd->se_dev;
183 struct se_lun *lun = cmd->se_lun; 176 struct se_lun *lun = cmd->se_lun;
184 struct se_port *port = NULL;
185 struct se_portal_group *tpg = NULL; 177 struct se_portal_group *tpg = NULL;
186 struct t10_alua_lu_gp_member *lu_gp_mem; 178 struct t10_alua_lu_gp_member *lu_gp_mem;
187 struct t10_alua_tg_pt_gp *tg_pt_gp; 179 struct t10_alua_tg_pt_gp *tg_pt_gp;
188 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
189 unsigned char *prod = &dev->t10_wwn.model[0]; 180 unsigned char *prod = &dev->t10_wwn.model[0];
190 u32 prod_len; 181 u32 prod_len;
191 u32 unit_serial_len, off = 0; 182 u32 unit_serial_len, off = 0;
@@ -267,18 +258,15 @@ check_t10_vend_desc:
267 /* Header size for Designation descriptor */ 258 /* Header size for Designation descriptor */
268 len += (id_len + 4); 259 len += (id_len + 4);
269 off += (id_len + 4); 260 off += (id_len + 4);
270 /* 261
271 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD 262 if (1) {
272 */
273 port = lun->lun_sep;
274 if (port) {
275 struct t10_alua_lu_gp *lu_gp; 263 struct t10_alua_lu_gp *lu_gp;
276 u32 padding, scsi_name_len, scsi_target_len; 264 u32 padding, scsi_name_len, scsi_target_len;
277 u16 lu_gp_id = 0; 265 u16 lu_gp_id = 0;
278 u16 tg_pt_gp_id = 0; 266 u16 tg_pt_gp_id = 0;
279 u16 tpgt; 267 u16 tpgt;
280 268
281 tpg = port->sep_tpg; 269 tpg = lun->lun_tpg;
282 /* 270 /*
283 * Relative target port identifer, see spc4r17 271 * Relative target port identifer, see spc4r17
284 * section 7.7.3.7 272 * section 7.7.3.7
@@ -298,8 +286,8 @@ check_t10_vend_desc:
298 /* Skip over Obsolete field in RTPI payload 286 /* Skip over Obsolete field in RTPI payload
299 * in Table 472 */ 287 * in Table 472 */
300 off += 2; 288 off += 2;
301 buf[off++] = ((port->sep_rtpi >> 8) & 0xff); 289 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
302 buf[off++] = (port->sep_rtpi & 0xff); 290 buf[off++] = (lun->lun_rtpi & 0xff);
303 len += 8; /* Header size + Designation descriptor */ 291 len += 8; /* Header size + Designation descriptor */
304 /* 292 /*
305 * Target port group identifier, see spc4r17 293 * Target port group identifier, see spc4r17
@@ -308,18 +296,14 @@ check_t10_vend_desc:
308 * Get the PROTOCOL IDENTIFIER as defined by spc4r17 296 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
309 * section 7.5.1 Table 362 297 * section 7.5.1 Table 362
310 */ 298 */
311 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; 299 spin_lock(&lun->lun_tg_pt_gp_lock);
312 if (!tg_pt_gp_mem) 300 tg_pt_gp = lun->lun_tg_pt_gp;
313 goto check_lu_gp;
314
315 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
316 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
317 if (!tg_pt_gp) { 301 if (!tg_pt_gp) {
318 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 302 spin_unlock(&lun->lun_tg_pt_gp_lock);
319 goto check_lu_gp; 303 goto check_lu_gp;
320 } 304 }
321 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id; 305 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
322 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 306 spin_unlock(&lun->lun_tg_pt_gp_lock);
323 307
324 buf[off] = tpg->proto_id << 4; 308 buf[off] = tpg->proto_id << 4;
325 buf[off++] |= 0x1; /* CODE SET == Binary */ 309 buf[off++] |= 0x1; /* CODE SET == Binary */
@@ -694,7 +678,7 @@ static sense_reason_t
694spc_emulate_inquiry(struct se_cmd *cmd) 678spc_emulate_inquiry(struct se_cmd *cmd)
695{ 679{
696 struct se_device *dev = cmd->se_dev; 680 struct se_device *dev = cmd->se_dev;
697 struct se_portal_group *tpg = cmd->se_lun->lun_sep->sep_tpg; 681 struct se_portal_group *tpg = cmd->se_lun->lun_tpg;
698 unsigned char *rbuf; 682 unsigned char *rbuf;
699 unsigned char *cdb = cmd->t_task_cdb; 683 unsigned char *cdb = cmd->t_task_cdb;
700 unsigned char *buf; 684 unsigned char *buf;
@@ -708,7 +692,7 @@ spc_emulate_inquiry(struct se_cmd *cmd)
708 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 692 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
709 } 693 }
710 694
711 if (dev == tpg->tpg_virt_lun0.lun_se_dev) 695 if (dev == tpg->tpg_virt_lun0->lun_se_dev)
712 buf[0] = 0x3f; /* Not connected */ 696 buf[0] = 0x3f; /* Not connected */
713 else 697 else
714 buf[0] = dev->transport->get_device_type(dev); 698 buf[0] = dev->transport->get_device_type(dev);
diff --git a/drivers/target/target_core_stat.c b/drivers/target/target_core_stat.c
index ea1287940a7c..8e080efb0188 100644
--- a/drivers/target/target_core_stat.c
+++ b/drivers/target/target_core_stat.c
@@ -106,7 +106,7 @@ static ssize_t target_stat_scsi_dev_show_attr_ports(
106 struct se_device *dev = 106 struct se_device *dev =
107 container_of(sgrps, struct se_device, dev_stat_grps); 107 container_of(sgrps, struct se_device, dev_stat_grps);
108 108
109 return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_port_count); 109 return snprintf(page, PAGE_SIZE, "%u\n", dev->export_count);
110} 110}
111DEV_STAT_SCSI_DEV_ATTR_RO(ports); 111DEV_STAT_SCSI_DEV_ATTR_RO(ports);
112 112
@@ -542,19 +542,13 @@ static ssize_t target_stat_scsi_port_show_attr_inst(
542 struct se_port_stat_grps *pgrps, char *page) 542 struct se_port_stat_grps *pgrps, char *page)
543{ 543{
544 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 544 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
545 struct se_port *sep; 545 struct se_device *dev;
546 struct se_device *dev = lun->lun_se_dev; 546 ssize_t ret = -ENODEV;
547 struct se_hba *hba;
548 ssize_t ret;
549 547
550 spin_lock(&lun->lun_sep_lock); 548 spin_lock(&lun->lun_sep_lock);
551 sep = lun->lun_sep; 549 dev = lun->lun_se_dev;
552 if (!sep) { 550 if (dev)
553 spin_unlock(&lun->lun_sep_lock); 551 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
554 return -ENODEV;
555 }
556 hba = dev->se_hba;
557 ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
558 spin_unlock(&lun->lun_sep_lock); 552 spin_unlock(&lun->lun_sep_lock);
559 return ret; 553 return ret;
560} 554}
@@ -564,17 +558,13 @@ static ssize_t target_stat_scsi_port_show_attr_dev(
564 struct se_port_stat_grps *pgrps, char *page) 558 struct se_port_stat_grps *pgrps, char *page)
565{ 559{
566 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 560 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
567 struct se_port *sep; 561 struct se_device *dev;
568 struct se_device *dev = lun->lun_se_dev; 562 ssize_t ret = -ENODEV;
569 ssize_t ret;
570 563
571 spin_lock(&lun->lun_sep_lock); 564 spin_lock(&lun->lun_sep_lock);
572 sep = lun->lun_sep; 565 dev = lun->lun_se_dev;
573 if (!sep) { 566 if (dev)
574 spin_unlock(&lun->lun_sep_lock); 567 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
575 return -ENODEV;
576 }
577 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
578 spin_unlock(&lun->lun_sep_lock); 568 spin_unlock(&lun->lun_sep_lock);
579 return ret; 569 return ret;
580} 570}
@@ -584,16 +574,13 @@ static ssize_t target_stat_scsi_port_show_attr_indx(
584 struct se_port_stat_grps *pgrps, char *page) 574 struct se_port_stat_grps *pgrps, char *page)
585{ 575{
586 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 576 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
587 struct se_port *sep; 577 struct se_device *dev;
588 ssize_t ret; 578 ssize_t ret = -ENODEV;
589 579
590 spin_lock(&lun->lun_sep_lock); 580 spin_lock(&lun->lun_sep_lock);
591 sep = lun->lun_sep; 581 dev = lun->lun_se_dev;
592 if (!sep) { 582 if (dev)
593 spin_unlock(&lun->lun_sep_lock); 583 ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
594 return -ENODEV;
595 }
596 ret = snprintf(page, PAGE_SIZE, "%u\n", sep->sep_index);
597 spin_unlock(&lun->lun_sep_lock); 584 spin_unlock(&lun->lun_sep_lock);
598 return ret; 585 return ret;
599} 586}
@@ -603,20 +590,13 @@ static ssize_t target_stat_scsi_port_show_attr_role(
603 struct se_port_stat_grps *pgrps, char *page) 590 struct se_port_stat_grps *pgrps, char *page)
604{ 591{
605 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 592 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
606 struct se_device *dev = lun->lun_se_dev; 593 struct se_device *dev;
607 struct se_port *sep; 594 ssize_t ret = -ENODEV;
608 ssize_t ret;
609
610 if (!dev)
611 return -ENODEV;
612 595
613 spin_lock(&lun->lun_sep_lock); 596 spin_lock(&lun->lun_sep_lock);
614 sep = lun->lun_sep; 597 dev = lun->lun_se_dev;
615 if (!sep) { 598 if (dev)
616 spin_unlock(&lun->lun_sep_lock); 599 ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
617 return -ENODEV;
618 }
619 ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
620 spin_unlock(&lun->lun_sep_lock); 600 spin_unlock(&lun->lun_sep_lock);
621 return ret; 601 return ret;
622} 602}
@@ -626,17 +606,15 @@ static ssize_t target_stat_scsi_port_show_attr_busy_count(
626 struct se_port_stat_grps *pgrps, char *page) 606 struct se_port_stat_grps *pgrps, char *page)
627{ 607{
628 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 608 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
629 struct se_port *sep; 609 struct se_device *dev;
630 ssize_t ret; 610 ssize_t ret = -ENODEV;
631 611
632 spin_lock(&lun->lun_sep_lock); 612 spin_lock(&lun->lun_sep_lock);
633 sep = lun->lun_sep; 613 dev = lun->lun_se_dev;
634 if (!sep) { 614 if (dev) {
635 spin_unlock(&lun->lun_sep_lock); 615 /* FIXME: scsiPortBusyStatuses */
636 return -ENODEV; 616 ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
637 } 617 }
638 /* FIXME: scsiPortBusyStatuses */
639 ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
640 spin_unlock(&lun->lun_sep_lock); 618 spin_unlock(&lun->lun_sep_lock);
641 return ret; 619 return ret;
642} 620}
@@ -685,19 +663,13 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_inst(
685 struct se_port_stat_grps *pgrps, char *page) 663 struct se_port_stat_grps *pgrps, char *page)
686{ 664{
687 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 665 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
688 struct se_device *dev = lun->lun_se_dev; 666 struct se_device *dev;
689 struct se_port *sep; 667 ssize_t ret = -ENODEV;
690 struct se_hba *hba;
691 ssize_t ret;
692 668
693 spin_lock(&lun->lun_sep_lock); 669 spin_lock(&lun->lun_sep_lock);
694 sep = lun->lun_sep; 670 dev = lun->lun_se_dev;
695 if (!sep) { 671 if (dev)
696 spin_unlock(&lun->lun_sep_lock); 672 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
697 return -ENODEV;
698 }
699 hba = dev->se_hba;
700 ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
701 spin_unlock(&lun->lun_sep_lock); 673 spin_unlock(&lun->lun_sep_lock);
702 return ret; 674 return ret;
703} 675}
@@ -707,17 +679,13 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_dev(
707 struct se_port_stat_grps *pgrps, char *page) 679 struct se_port_stat_grps *pgrps, char *page)
708{ 680{
709 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 681 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
710 struct se_device *dev = lun->lun_se_dev; 682 struct se_device *dev;
711 struct se_port *sep; 683 ssize_t ret = -ENODEV;
712 ssize_t ret;
713 684
714 spin_lock(&lun->lun_sep_lock); 685 spin_lock(&lun->lun_sep_lock);
715 sep = lun->lun_sep; 686 dev = lun->lun_se_dev;
716 if (!sep) { 687 if (dev)
717 spin_unlock(&lun->lun_sep_lock); 688 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
718 return -ENODEV;
719 }
720 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
721 spin_unlock(&lun->lun_sep_lock); 689 spin_unlock(&lun->lun_sep_lock);
722 return ret; 690 return ret;
723} 691}
@@ -727,16 +695,13 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_indx(
727 struct se_port_stat_grps *pgrps, char *page) 695 struct se_port_stat_grps *pgrps, char *page)
728{ 696{
729 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 697 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
730 struct se_port *sep; 698 struct se_device *dev;
731 ssize_t ret; 699 ssize_t ret = -ENODEV;
732 700
733 spin_lock(&lun->lun_sep_lock); 701 spin_lock(&lun->lun_sep_lock);
734 sep = lun->lun_sep; 702 dev = lun->lun_se_dev;
735 if (!sep) { 703 if (dev)
736 spin_unlock(&lun->lun_sep_lock); 704 ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
737 return -ENODEV;
738 }
739 ret = snprintf(page, PAGE_SIZE, "%u\n", sep->sep_index);
740 spin_unlock(&lun->lun_sep_lock); 705 spin_unlock(&lun->lun_sep_lock);
741 return ret; 706 return ret;
742} 707}
@@ -746,20 +711,16 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_name(
746 struct se_port_stat_grps *pgrps, char *page) 711 struct se_port_stat_grps *pgrps, char *page)
747{ 712{
748 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 713 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
749 struct se_port *sep; 714 struct se_portal_group *tpg = lun->lun_tpg;
750 struct se_portal_group *tpg; 715 struct se_device *dev;
751 ssize_t ret; 716 ssize_t ret = -ENODEV;
752 717
753 spin_lock(&lun->lun_sep_lock); 718 spin_lock(&lun->lun_sep_lock);
754 sep = lun->lun_sep; 719 dev = lun->lun_se_dev;
755 if (!sep) { 720 if (dev)
756 spin_unlock(&lun->lun_sep_lock); 721 ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
757 return -ENODEV; 722 tpg->se_tpg_tfo->get_fabric_name(),
758 } 723 lun->lun_rtpi);
759 tpg = sep->sep_tpg;
760
761 ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
762 tpg->se_tpg_tfo->get_fabric_name(), sep->sep_index);
763 spin_unlock(&lun->lun_sep_lock); 724 spin_unlock(&lun->lun_sep_lock);
764 return ret; 725 return ret;
765} 726}
@@ -769,21 +730,16 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_port_index(
769 struct se_port_stat_grps *pgrps, char *page) 730 struct se_port_stat_grps *pgrps, char *page)
770{ 731{
771 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 732 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
772 struct se_port *sep; 733 struct se_portal_group *tpg = lun->lun_tpg;
773 struct se_portal_group *tpg; 734 struct se_device *dev;
774 ssize_t ret; 735 ssize_t ret = -ENODEV;
775 736
776 spin_lock(&lun->lun_sep_lock); 737 spin_lock(&lun->lun_sep_lock);
777 sep = lun->lun_sep; 738 dev = lun->lun_se_dev;
778 if (!sep) { 739 if (dev)
779 spin_unlock(&lun->lun_sep_lock); 740 ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
780 return -ENODEV; 741 tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
781 } 742 tpg->se_tpg_tfo->tpg_get_tag(tpg));
782 tpg = sep->sep_tpg;
783
784 ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
785 tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
786 tpg->se_tpg_tfo->tpg_get_tag(tpg));
787 spin_unlock(&lun->lun_sep_lock); 743 spin_unlock(&lun->lun_sep_lock);
788 return ret; 744 return ret;
789} 745}
@@ -793,17 +749,13 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_in_cmds(
793 struct se_port_stat_grps *pgrps, char *page) 749 struct se_port_stat_grps *pgrps, char *page)
794{ 750{
795 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 751 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
796 struct se_port *sep; 752 struct se_device *dev;
797 ssize_t ret; 753 ssize_t ret = -ENODEV;
798 754
799 spin_lock(&lun->lun_sep_lock); 755 spin_lock(&lun->lun_sep_lock);
800 sep = lun->lun_sep; 756 dev = lun->lun_se_dev;
801 if (!sep) { 757 if (dev)
802 spin_unlock(&lun->lun_sep_lock); 758 ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_stats.cmd_pdus);
803 return -ENODEV;
804 }
805
806 ret = snprintf(page, PAGE_SIZE, "%llu\n", sep->sep_stats.cmd_pdus);
807 spin_unlock(&lun->lun_sep_lock); 759 spin_unlock(&lun->lun_sep_lock);
808 return ret; 760 return ret;
809} 761}
@@ -813,18 +765,14 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_write_mbytes(
813 struct se_port_stat_grps *pgrps, char *page) 765 struct se_port_stat_grps *pgrps, char *page)
814{ 766{
815 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 767 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
816 struct se_port *sep; 768 struct se_device *dev;
817 ssize_t ret; 769 ssize_t ret = -ENODEV;
818 770
819 spin_lock(&lun->lun_sep_lock); 771 spin_lock(&lun->lun_sep_lock);
820 sep = lun->lun_sep; 772 dev = lun->lun_se_dev;
821 if (!sep) { 773 if (dev)
822 spin_unlock(&lun->lun_sep_lock); 774 ret = snprintf(page, PAGE_SIZE, "%u\n",
823 return -ENODEV; 775 (u32)(lun->lun_stats.rx_data_octets >> 20));
824 }
825
826 ret = snprintf(page, PAGE_SIZE, "%u\n",
827 (u32)(sep->sep_stats.rx_data_octets >> 20));
828 spin_unlock(&lun->lun_sep_lock); 776 spin_unlock(&lun->lun_sep_lock);
829 return ret; 777 return ret;
830} 778}
@@ -834,18 +782,14 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_read_mbytes(
834 struct se_port_stat_grps *pgrps, char *page) 782 struct se_port_stat_grps *pgrps, char *page)
835{ 783{
836 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 784 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
837 struct se_port *sep; 785 struct se_device *dev;
838 ssize_t ret; 786 ssize_t ret = -ENODEV;
839 787
840 spin_lock(&lun->lun_sep_lock); 788 spin_lock(&lun->lun_sep_lock);
841 sep = lun->lun_sep; 789 dev = lun->lun_se_dev;
842 if (!sep) { 790 if (dev)
843 spin_unlock(&lun->lun_sep_lock); 791 ret = snprintf(page, PAGE_SIZE, "%u\n",
844 return -ENODEV; 792 (u32)(lun->lun_stats.tx_data_octets >> 20));
845 }
846
847 ret = snprintf(page, PAGE_SIZE, "%u\n",
848 (u32)(sep->sep_stats.tx_data_octets >> 20));
849 spin_unlock(&lun->lun_sep_lock); 793 spin_unlock(&lun->lun_sep_lock);
850 return ret; 794 return ret;
851} 795}
@@ -855,18 +799,15 @@ static ssize_t target_stat_scsi_tgt_port_show_attr_hs_in_cmds(
855 struct se_port_stat_grps *pgrps, char *page) 799 struct se_port_stat_grps *pgrps, char *page)
856{ 800{
857 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 801 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
858 struct se_port *sep; 802 struct se_device *dev;
859 ssize_t ret; 803 ssize_t ret = -ENODEV;
860 804
861 spin_lock(&lun->lun_sep_lock); 805 spin_lock(&lun->lun_sep_lock);
862 sep = lun->lun_sep; 806 dev = lun->lun_se_dev;
863 if (!sep) { 807 if (dev) {
864 spin_unlock(&lun->lun_sep_lock); 808 /* FIXME: scsiTgtPortHsInCommands */
865 return -ENODEV; 809 ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
866 } 810 }
867
868 /* FIXME: scsiTgtPortHsInCommands */
869 ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
870 spin_unlock(&lun->lun_sep_lock); 811 spin_unlock(&lun->lun_sep_lock);
871 return ret; 812 return ret;
872} 813}
@@ -921,20 +862,13 @@ static ssize_t target_stat_scsi_transport_show_attr_inst(
921 struct se_port_stat_grps *pgrps, char *page) 862 struct se_port_stat_grps *pgrps, char *page)
922{ 863{
923 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 864 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
924 struct se_device *dev = lun->lun_se_dev; 865 struct se_device *dev;
925 struct se_port *sep; 866 ssize_t ret = -ENODEV;
926 struct se_hba *hba;
927 ssize_t ret;
928 867
929 spin_lock(&lun->lun_sep_lock); 868 spin_lock(&lun->lun_sep_lock);
930 sep = lun->lun_sep; 869 dev = lun->lun_se_dev;
931 if (!sep) { 870 if (dev)
932 spin_unlock(&lun->lun_sep_lock); 871 ret = snprintf(page, PAGE_SIZE, "%u\n", dev->se_hba->hba_index);
933 return -ENODEV;
934 }
935
936 hba = dev->se_hba;
937 ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
938 spin_unlock(&lun->lun_sep_lock); 872 spin_unlock(&lun->lun_sep_lock);
939 return ret; 873 return ret;
940} 874}
@@ -944,20 +878,17 @@ static ssize_t target_stat_scsi_transport_show_attr_device(
944 struct se_port_stat_grps *pgrps, char *page) 878 struct se_port_stat_grps *pgrps, char *page)
945{ 879{
946 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 880 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
947 struct se_port *sep; 881 struct se_device *dev;
948 struct se_portal_group *tpg; 882 struct se_portal_group *tpg = lun->lun_tpg;
949 ssize_t ret; 883 ssize_t ret = -ENODEV;
950 884
951 spin_lock(&lun->lun_sep_lock); 885 spin_lock(&lun->lun_sep_lock);
952 sep = lun->lun_sep; 886 dev = lun->lun_se_dev;
953 if (!sep) { 887 if (dev) {
954 spin_unlock(&lun->lun_sep_lock); 888 /* scsiTransportType */
955 return -ENODEV; 889 ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
890 tpg->se_tpg_tfo->get_fabric_name());
956 } 891 }
957 tpg = sep->sep_tpg;
958 /* scsiTransportType */
959 ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
960 tpg->se_tpg_tfo->get_fabric_name());
961 spin_unlock(&lun->lun_sep_lock); 892 spin_unlock(&lun->lun_sep_lock);
962 return ret; 893 return ret;
963} 894}
@@ -967,19 +898,15 @@ static ssize_t target_stat_scsi_transport_show_attr_indx(
967 struct se_port_stat_grps *pgrps, char *page) 898 struct se_port_stat_grps *pgrps, char *page)
968{ 899{
969 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 900 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
970 struct se_port *sep; 901 struct se_device *dev;
971 struct se_portal_group *tpg; 902 struct se_portal_group *tpg = lun->lun_tpg;
972 ssize_t ret; 903 ssize_t ret = -ENODEV;
973 904
974 spin_lock(&lun->lun_sep_lock); 905 spin_lock(&lun->lun_sep_lock);
975 sep = lun->lun_sep; 906 dev = lun->lun_se_dev;
976 if (!sep) { 907 if (dev)
977 spin_unlock(&lun->lun_sep_lock); 908 ret = snprintf(page, PAGE_SIZE, "%u\n",
978 return -ENODEV; 909 tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
979 }
980 tpg = sep->sep_tpg;
981 ret = snprintf(page, PAGE_SIZE, "%u\n",
982 tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
983 spin_unlock(&lun->lun_sep_lock); 910 spin_unlock(&lun->lun_sep_lock);
984 return ret; 911 return ret;
985} 912}
@@ -990,24 +917,20 @@ static ssize_t target_stat_scsi_transport_show_attr_dev_name(
990{ 917{
991 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps); 918 struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
992 struct se_device *dev = lun->lun_se_dev; 919 struct se_device *dev = lun->lun_se_dev;
993 struct se_port *sep; 920 struct se_portal_group *tpg = lun->lun_tpg;
994 struct se_portal_group *tpg;
995 struct t10_wwn *wwn; 921 struct t10_wwn *wwn;
996 ssize_t ret; 922 ssize_t ret = -ENODEV;
997 923
998 spin_lock(&lun->lun_sep_lock); 924 spin_lock(&lun->lun_sep_lock);
999 sep = lun->lun_sep; 925 dev = lun->lun_se_dev;
1000 if (!sep) { 926 if (dev) {
1001 spin_unlock(&lun->lun_sep_lock); 927 wwn = &dev->t10_wwn;
1002 return -ENODEV; 928 /* scsiTransportDevName */
929 ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
930 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
931 (strlen(wwn->unit_serial)) ? wwn->unit_serial :
932 wwn->vendor);
1003 } 933 }
1004 tpg = sep->sep_tpg;
1005 wwn = &dev->t10_wwn;
1006 /* scsiTransportDevName */
1007 ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
1008 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1009 (strlen(wwn->unit_serial)) ? wwn->unit_serial :
1010 wwn->vendor);
1011 spin_unlock(&lun->lun_sep_lock); 934 spin_unlock(&lun->lun_sep_lock);
1012 return ret; 935 return ret;
1013} 936}
diff --git a/drivers/target/target_core_tpg.c b/drivers/target/target_core_tpg.c
index fd531fcf3191..499b1399035a 100644
--- a/drivers/target/target_core_tpg.c
+++ b/drivers/target/target_core_tpg.c
@@ -40,6 +40,7 @@
40#include <target/target_core_fabric.h> 40#include <target/target_core_fabric.h>
41 41
42#include "target_core_internal.h" 42#include "target_core_internal.h"
43#include "target_core_alua.h"
43#include "target_core_pr.h" 44#include "target_core_pr.h"
44 45
45extern struct se_device *g_lun0_dev; 46extern struct se_device *g_lun0_dev;
@@ -484,32 +485,14 @@ static void core_tpg_lun_ref_release(struct percpu_ref *ref)
484 complete(&lun->lun_ref_comp); 485 complete(&lun->lun_ref_comp);
485} 486}
486 487
487static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
488{
489 /* Set in core_dev_setup_virtual_lun0() */
490 struct se_device *dev = g_lun0_dev;
491 struct se_lun *lun = &se_tpg->tpg_virt_lun0;
492 u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
493 int ret;
494
495 lun->unpacked_lun = 0;
496 atomic_set(&lun->lun_acl_count, 0);
497 spin_lock_init(&lun->lun_sep_lock);
498 init_completion(&lun->lun_ref_comp);
499
500 ret = core_tpg_add_lun(se_tpg, lun, lun_access, dev);
501 if (ret < 0)
502 return ret;
503
504 return 0;
505}
506
507int core_tpg_register( 488int core_tpg_register(
508 const struct target_core_fabric_ops *tfo, 489 const struct target_core_fabric_ops *tfo,
509 struct se_wwn *se_wwn, 490 struct se_wwn *se_wwn,
510 struct se_portal_group *se_tpg, 491 struct se_portal_group *se_tpg,
511 int proto_id) 492 int proto_id)
512{ 493{
494 int ret;
495
513 INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist); 496 INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
514 se_tpg->proto_id = proto_id; 497 se_tpg->proto_id = proto_id;
515 se_tpg->se_tpg_tfo = tfo; 498 se_tpg->se_tpg_tfo = tfo;
@@ -523,8 +506,16 @@ int core_tpg_register(
523 mutex_init(&se_tpg->acl_node_mutex); 506 mutex_init(&se_tpg->acl_node_mutex);
524 507
525 if (se_tpg->proto_id >= 0) { 508 if (se_tpg->proto_id >= 0) {
526 if (core_tpg_setup_virtual_lun0(se_tpg) < 0) 509 se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
527 return -ENOMEM; 510 if (IS_ERR(se_tpg->tpg_virt_lun0))
511 return PTR_ERR(se_tpg->tpg_virt_lun0);
512
513 ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
514 TRANSPORT_LUNFLAGS_READ_ONLY, g_lun0_dev);
515 if (ret < 0) {
516 kfree(se_tpg->tpg_virt_lun0);
517 return ret;
518 }
528 } 519 }
529 520
530 spin_lock_bh(&tpg_lock); 521 spin_lock_bh(&tpg_lock);
@@ -575,8 +566,10 @@ int core_tpg_deregister(struct se_portal_group *se_tpg)
575 kfree(nacl); 566 kfree(nacl);
576 } 567 }
577 568
578 if (se_tpg->proto_id >= 0) 569 if (se_tpg->proto_id >= 0) {
579 core_tpg_remove_lun(se_tpg, &se_tpg->tpg_virt_lun0); 570 core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
571 kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
572 }
580 573
581 return 0; 574 return 0;
582} 575}
@@ -607,6 +600,15 @@ struct se_lun *core_tpg_alloc_lun(
607 atomic_set(&lun->lun_acl_count, 0); 600 atomic_set(&lun->lun_acl_count, 0);
608 spin_lock_init(&lun->lun_sep_lock); 601 spin_lock_init(&lun->lun_sep_lock);
609 init_completion(&lun->lun_ref_comp); 602 init_completion(&lun->lun_ref_comp);
603 INIT_LIST_HEAD(&lun->lun_deve_list);
604 INIT_LIST_HEAD(&lun->lun_dev_link);
605 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
606 spin_lock_init(&lun->lun_deve_lock);
607 mutex_init(&lun->lun_tg_pt_md_mutex);
608 INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
609 spin_lock_init(&lun->lun_tg_pt_gp_lock);
610 atomic_set(&lun->lun_active, 0);
611 lun->lun_tpg = tpg;
610 612
611 return lun; 613 return lun;
612} 614}
@@ -622,21 +624,40 @@ int core_tpg_add_lun(
622 ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0, 624 ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
623 GFP_KERNEL); 625 GFP_KERNEL);
624 if (ret < 0) 626 if (ret < 0)
625 return ret; 627 goto out;
626 628
627 ret = core_dev_export(dev, tpg, lun); 629 ret = core_alloc_rtpi(lun, dev);
628 if (ret < 0) { 630 if (ret)
629 percpu_ref_exit(&lun->lun_ref); 631 goto out_kill_ref;
630 return ret; 632
631 } 633 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
634 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
635 target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
632 636
633 mutex_lock(&tpg->tpg_lun_mutex); 637 mutex_lock(&tpg->tpg_lun_mutex);
638
639 spin_lock(&lun->lun_sep_lock);
640 lun->lun_index = dev->dev_index;
641 lun->lun_se_dev = dev;
642 spin_unlock(&lun->lun_sep_lock);
643
644 spin_lock(&dev->se_port_lock);
645 rcu_assign_pointer(lun->lun_se_dev, dev);
646 dev->export_count++;
647 list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
648 spin_unlock(&dev->se_port_lock);
649
634 lun->lun_access = lun_access; 650 lun->lun_access = lun_access;
635 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) 651 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
636 hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist); 652 hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
637 mutex_unlock(&tpg->tpg_lun_mutex); 653 mutex_unlock(&tpg->tpg_lun_mutex);
638 654
639 return 0; 655 return 0;
656
657out_kill_ref:
658 percpu_ref_exit(&lun->lun_ref);
659out:
660 return ret;
640} 661}
641 662
642void core_tpg_remove_lun( 663void core_tpg_remove_lun(
@@ -648,9 +669,19 @@ void core_tpg_remove_lun(
648 core_clear_lun_from_tpg(lun, tpg); 669 core_clear_lun_from_tpg(lun, tpg);
649 transport_clear_lun_ref(lun); 670 transport_clear_lun_ref(lun);
650 671
651 core_dev_unexport(lun->lun_se_dev, tpg, lun);
652
653 mutex_lock(&tpg->tpg_lun_mutex); 672 mutex_lock(&tpg->tpg_lun_mutex);
673 if (lun->lun_se_dev) {
674 while (atomic_read(&lun->lun_active))
675 cpu_relax();
676
677 target_detach_tg_pt_gp(lun);
678
679 spin_lock(&dev->se_port_lock);
680 list_del(&lun->lun_dev_link);
681 dev->export_count--;
682 rcu_assign_pointer(lun->lun_se_dev, NULL);
683 spin_unlock(&dev->se_port_lock);
684 }
654 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) 685 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
655 hlist_del_rcu(&lun->link); 686 hlist_del_rcu(&lun->link);
656 mutex_unlock(&tpg->tpg_lun_mutex); 687 mutex_unlock(&tpg->tpg_lun_mutex);
diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c
index 7c0518a28186..ef6fdd8a47c4 100644
--- a/drivers/target/target_core_transport.c
+++ b/drivers/target/target_core_transport.c
@@ -60,7 +60,6 @@ struct kmem_cache *t10_pr_reg_cache;
60struct kmem_cache *t10_alua_lu_gp_cache; 60struct kmem_cache *t10_alua_lu_gp_cache;
61struct kmem_cache *t10_alua_lu_gp_mem_cache; 61struct kmem_cache *t10_alua_lu_gp_mem_cache;
62struct kmem_cache *t10_alua_tg_pt_gp_cache; 62struct kmem_cache *t10_alua_tg_pt_gp_cache;
63struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
64struct kmem_cache *t10_alua_lba_map_cache; 63struct kmem_cache *t10_alua_lba_map_cache;
65struct kmem_cache *t10_alua_lba_map_mem_cache; 64struct kmem_cache *t10_alua_lba_map_mem_cache;
66 65
@@ -119,16 +118,6 @@ int init_se_kmem_caches(void)
119 "cache failed\n"); 118 "cache failed\n");
120 goto out_free_lu_gp_mem_cache; 119 goto out_free_lu_gp_mem_cache;
121 } 120 }
122 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
123 "t10_alua_tg_pt_gp_mem_cache",
124 sizeof(struct t10_alua_tg_pt_gp_member),
125 __alignof__(struct t10_alua_tg_pt_gp_member),
126 0, NULL);
127 if (!t10_alua_tg_pt_gp_mem_cache) {
128 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
129 "mem_t failed\n");
130 goto out_free_tg_pt_gp_cache;
131 }
132 t10_alua_lba_map_cache = kmem_cache_create( 121 t10_alua_lba_map_cache = kmem_cache_create(
133 "t10_alua_lba_map_cache", 122 "t10_alua_lba_map_cache",
134 sizeof(struct t10_alua_lba_map), 123 sizeof(struct t10_alua_lba_map),
@@ -136,7 +125,7 @@ int init_se_kmem_caches(void)
136 if (!t10_alua_lba_map_cache) { 125 if (!t10_alua_lba_map_cache) {
137 pr_err("kmem_cache_create() for t10_alua_lba_map_" 126 pr_err("kmem_cache_create() for t10_alua_lba_map_"
138 "cache failed\n"); 127 "cache failed\n");
139 goto out_free_tg_pt_gp_mem_cache; 128 goto out_free_tg_pt_gp_cache;
140 } 129 }
141 t10_alua_lba_map_mem_cache = kmem_cache_create( 130 t10_alua_lba_map_mem_cache = kmem_cache_create(
142 "t10_alua_lba_map_mem_cache", 131 "t10_alua_lba_map_mem_cache",
@@ -159,8 +148,6 @@ out_free_lba_map_mem_cache:
159 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 148 kmem_cache_destroy(t10_alua_lba_map_mem_cache);
160out_free_lba_map_cache: 149out_free_lba_map_cache:
161 kmem_cache_destroy(t10_alua_lba_map_cache); 150 kmem_cache_destroy(t10_alua_lba_map_cache);
162out_free_tg_pt_gp_mem_cache:
163 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
164out_free_tg_pt_gp_cache: 151out_free_tg_pt_gp_cache:
165 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 152 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
166out_free_lu_gp_mem_cache: 153out_free_lu_gp_mem_cache:
@@ -186,7 +173,6 @@ void release_se_kmem_caches(void)
186 kmem_cache_destroy(t10_alua_lu_gp_cache); 173 kmem_cache_destroy(t10_alua_lu_gp_cache);
187 kmem_cache_destroy(t10_alua_lu_gp_mem_cache); 174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
188 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 175 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
189 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
190 kmem_cache_destroy(t10_alua_lba_map_cache); 176 kmem_cache_destroy(t10_alua_lba_map_cache);
191 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 177 kmem_cache_destroy(t10_alua_lba_map_mem_cache);
192} 178}
@@ -1277,8 +1263,7 @@ target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb)
1277 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; 1263 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1278 1264
1279 spin_lock(&cmd->se_lun->lun_sep_lock); 1265 spin_lock(&cmd->se_lun->lun_sep_lock);
1280 if (cmd->se_lun->lun_sep) 1266 cmd->se_lun->lun_stats.cmd_pdus++;
1281 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1282 spin_unlock(&cmd->se_lun->lun_sep_lock); 1267 spin_unlock(&cmd->se_lun->lun_sep_lock);
1283 return 0; 1268 return 0;
1284} 1269}
@@ -2076,10 +2061,7 @@ queue_rsp:
2076 switch (cmd->data_direction) { 2061 switch (cmd->data_direction) {
2077 case DMA_FROM_DEVICE: 2062 case DMA_FROM_DEVICE:
2078 spin_lock(&cmd->se_lun->lun_sep_lock); 2063 spin_lock(&cmd->se_lun->lun_sep_lock);
2079 if (cmd->se_lun->lun_sep) { 2064 cmd->se_lun->lun_stats.tx_data_octets += cmd->data_length;
2080 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
2081 cmd->data_length;
2082 }
2083 spin_unlock(&cmd->se_lun->lun_sep_lock); 2065 spin_unlock(&cmd->se_lun->lun_sep_lock);
2084 /* 2066 /*
2085 * Perform READ_STRIP of PI using software emulation when 2067 * Perform READ_STRIP of PI using software emulation when
@@ -2104,20 +2086,14 @@ queue_rsp:
2104 break; 2086 break;
2105 case DMA_TO_DEVICE: 2087 case DMA_TO_DEVICE:
2106 spin_lock(&cmd->se_lun->lun_sep_lock); 2088 spin_lock(&cmd->se_lun->lun_sep_lock);
2107 if (cmd->se_lun->lun_sep) { 2089 cmd->se_lun->lun_stats.rx_data_octets += cmd->data_length;
2108 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
2109 cmd->data_length;
2110 }
2111 spin_unlock(&cmd->se_lun->lun_sep_lock); 2090 spin_unlock(&cmd->se_lun->lun_sep_lock);
2112 /* 2091 /*
2113 * Check if we need to send READ payload for BIDI-COMMAND 2092 * Check if we need to send READ payload for BIDI-COMMAND
2114 */ 2093 */
2115 if (cmd->se_cmd_flags & SCF_BIDI) { 2094 if (cmd->se_cmd_flags & SCF_BIDI) {
2116 spin_lock(&cmd->se_lun->lun_sep_lock); 2095 spin_lock(&cmd->se_lun->lun_sep_lock);
2117 if (cmd->se_lun->lun_sep) { 2096 cmd->se_lun->lun_stats.tx_data_octets += cmd->data_length;
2118 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
2119 cmd->data_length;
2120 }
2121 spin_unlock(&cmd->se_lun->lun_sep_lock); 2097 spin_unlock(&cmd->se_lun->lun_sep_lock);
2122 ret = cmd->se_tfo->queue_data_in(cmd); 2098 ret = cmd->se_tfo->queue_data_in(cmd);
2123 if (ret == -EAGAIN || ret == -ENOMEM) 2099 if (ret == -EAGAIN || ret == -ENOMEM)
diff --git a/drivers/target/target_core_xcopy.c b/drivers/target/target_core_xcopy.c
index 3556a9dcee46..49111d39e795 100644
--- a/drivers/target/target_core_xcopy.c
+++ b/drivers/target/target_core_xcopy.c
@@ -348,8 +348,7 @@ struct xcopy_pt_cmd {
348 unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER]; 348 unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
349}; 349};
350 350
351static struct se_port xcopy_pt_port; 351struct se_portal_group xcopy_pt_tpg;
352static struct se_portal_group xcopy_pt_tpg;
353static struct se_session xcopy_pt_sess; 352static struct se_session xcopy_pt_sess;
354static struct se_node_acl xcopy_pt_nacl; 353static struct se_node_acl xcopy_pt_nacl;
355 354
@@ -439,17 +438,11 @@ int target_xcopy_setup_pt(void)
439 return -ENOMEM; 438 return -ENOMEM;
440 } 439 }
441 440
442 memset(&xcopy_pt_port, 0, sizeof(struct se_port));
443 INIT_LIST_HEAD(&xcopy_pt_port.sep_alua_list);
444 INIT_LIST_HEAD(&xcopy_pt_port.sep_list);
445 mutex_init(&xcopy_pt_port.sep_tg_pt_md_mutex);
446
447 memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group)); 441 memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
448 INIT_LIST_HEAD(&xcopy_pt_tpg.se_tpg_node); 442 INIT_LIST_HEAD(&xcopy_pt_tpg.se_tpg_node);
449 INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list); 443 INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
450 INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list); 444 INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
451 445
452 xcopy_pt_port.sep_tpg = &xcopy_pt_tpg;
453 xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo; 446 xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo;
454 447
455 memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl)); 448 memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl));
@@ -490,10 +483,6 @@ static void target_xcopy_setup_pt_port(
490 */ 483 */
491 if (remote_port) { 484 if (remote_port) {
492 xpt_cmd->remote_port = remote_port; 485 xpt_cmd->remote_port = remote_port;
493 pt_cmd->se_lun->lun_sep = &xcopy_pt_port;
494 pr_debug("Setup emulated remote DEST xcopy_pt_port: %p to"
495 " cmd->se_lun->lun_sep for X-COPY data PUSH\n",
496 pt_cmd->se_lun->lun_sep);
497 } else { 486 } else {
498 pt_cmd->se_lun = ec_cmd->se_lun; 487 pt_cmd->se_lun = ec_cmd->se_lun;
499 pt_cmd->se_dev = ec_cmd->se_dev; 488 pt_cmd->se_dev = ec_cmd->se_dev;
@@ -513,10 +502,6 @@ static void target_xcopy_setup_pt_port(
513 */ 502 */
514 if (remote_port) { 503 if (remote_port) {
515 xpt_cmd->remote_port = remote_port; 504 xpt_cmd->remote_port = remote_port;
516 pt_cmd->se_lun->lun_sep = &xcopy_pt_port;
517 pr_debug("Setup emulated remote SRC xcopy_pt_port: %p to"
518 " cmd->se_lun->lun_sep for X-COPY data PULL\n",
519 pt_cmd->se_lun->lun_sep);
520 } else { 505 } else {
521 pt_cmd->se_lun = ec_cmd->se_lun; 506 pt_cmd->se_lun = ec_cmd->se_lun;
522 pt_cmd->se_dev = ec_cmd->se_dev; 507 pt_cmd->se_dev = ec_cmd->se_dev;
diff --git a/include/target/target_core_base.h b/include/target/target_core_base.h
index ee7abdd6b7a2..1927dd5947a7 100644
--- a/include/target/target_core_base.h
+++ b/include/target/target_core_base.h
@@ -304,22 +304,13 @@ struct t10_alua_tg_pt_gp {
304 struct se_device *tg_pt_gp_dev; 304 struct se_device *tg_pt_gp_dev;
305 struct config_group tg_pt_gp_group; 305 struct config_group tg_pt_gp_group;
306 struct list_head tg_pt_gp_list; 306 struct list_head tg_pt_gp_list;
307 struct list_head tg_pt_gp_mem_list; 307 struct list_head tg_pt_gp_lun_list;
308 struct se_port *tg_pt_gp_alua_port; 308 struct se_lun *tg_pt_gp_alua_lun;
309 struct se_node_acl *tg_pt_gp_alua_nacl; 309 struct se_node_acl *tg_pt_gp_alua_nacl;
310 struct delayed_work tg_pt_gp_transition_work; 310 struct delayed_work tg_pt_gp_transition_work;
311 struct completion *tg_pt_gp_transition_complete; 311 struct completion *tg_pt_gp_transition_complete;
312}; 312};
313 313
314struct t10_alua_tg_pt_gp_member {
315 bool tg_pt_gp_assoc;
316 atomic_t tg_pt_gp_mem_ref_cnt;
317 spinlock_t tg_pt_gp_mem_lock;
318 struct t10_alua_tg_pt_gp *tg_pt_gp;
319 struct se_port *tg_pt;
320 struct list_head tg_pt_gp_mem_list;
321};
322
323struct t10_vpd { 314struct t10_vpd {
324 unsigned char device_identifier[INQUIRY_VPD_DEVICE_IDENTIFIER_LEN]; 315 unsigned char device_identifier[INQUIRY_VPD_DEVICE_IDENTIFIER_LEN];
325 int protocol_identifier_set; 316 int protocol_identifier_set;
@@ -650,6 +641,7 @@ struct se_dev_entry {
650#define DEF_PR_REG_ACTIVE 1 641#define DEF_PR_REG_ACTIVE 1
651 unsigned long deve_flags; 642 unsigned long deve_flags;
652 struct list_head alua_port_list; 643 struct list_head alua_port_list;
644 struct list_head lun_link;
653 struct list_head ua_list; 645 struct list_head ua_list;
654 struct hlist_node link; 646 struct hlist_node link;
655 struct rcu_head rcu_head; 647 struct rcu_head rcu_head;
@@ -697,7 +689,14 @@ struct se_port_stat_grps {
697 struct config_group scsi_transport_group; 689 struct config_group scsi_transport_group;
698}; 690};
699 691
692struct scsi_port_stats {
693 u32 cmd_pdus;
694 u64 tx_data_octets;
695 u64 rx_data_octets;
696};
697
700struct se_lun { 698struct se_lun {
699 /* RELATIVE TARGET PORT IDENTIFER */
701 u16 lun_rtpi; 700 u16 lun_rtpi;
702#define SE_LUN_LINK_MAGIC 0xffff7771 701#define SE_LUN_LINK_MAGIC 0xffff7771
703 u32 lun_link_magic; 702 u32 lun_link_magic;
@@ -707,12 +706,30 @@ struct se_lun {
707 u32 lun_index; 706 u32 lun_index;
708 atomic_t lun_acl_count; 707 atomic_t lun_acl_count;
709 spinlock_t lun_sep_lock; 708 spinlock_t lun_sep_lock;
710 struct se_device *lun_se_dev; 709 struct se_device __rcu *lun_se_dev;
711 struct se_port *lun_sep; 710
711 struct list_head lun_deve_list;
712 spinlock_t lun_deve_lock;
713
714 /* ALUA state */
715 int lun_tg_pt_secondary_stat;
716 int lun_tg_pt_secondary_write_md;
717 atomic_t lun_tg_pt_secondary_offline;
718 struct mutex lun_tg_pt_md_mutex;
719
720 /* ALUA target port group linkage */
721 struct list_head lun_tg_pt_gp_link;
722 struct t10_alua_tg_pt_gp *lun_tg_pt_gp;
723 spinlock_t lun_tg_pt_gp_lock;
724
725 atomic_t lun_active;
726 struct se_portal_group *lun_tpg;
727 struct scsi_port_stats lun_stats;
712 struct config_group lun_group; 728 struct config_group lun_group;
713 struct se_port_stat_grps port_stat_grps; 729 struct se_port_stat_grps port_stat_grps;
714 struct completion lun_ref_comp; 730 struct completion lun_ref_comp;
715 struct percpu_ref lun_ref; 731 struct percpu_ref lun_ref;
732 struct list_head lun_dev_link;
716 struct hlist_node link; 733 struct hlist_node link;
717 struct rcu_head rcu_head; 734 struct rcu_head rcu_head;
718}; 735};
@@ -737,7 +754,6 @@ struct se_device {
737#define DF_EMULATED_VPD_UNIT_SERIAL 0x00000004 754#define DF_EMULATED_VPD_UNIT_SERIAL 0x00000004
738#define DF_USING_UDEV_PATH 0x00000008 755#define DF_USING_UDEV_PATH 0x00000008
739#define DF_USING_ALIAS 0x00000010 756#define DF_USING_ALIAS 0x00000010
740 u32 dev_port_count;
741 /* Physical device queue depth */ 757 /* Physical device queue depth */
742 u32 queue_depth; 758 u32 queue_depth;
743 /* Used for SPC-2 reservations enforce of ISIDs */ 759 /* Used for SPC-2 reservations enforce of ISIDs */
@@ -754,7 +770,7 @@ struct se_device {
754 atomic_t dev_ordered_id; 770 atomic_t dev_ordered_id;
755 atomic_t dev_ordered_sync; 771 atomic_t dev_ordered_sync;
756 atomic_t dev_qf_count; 772 atomic_t dev_qf_count;
757 int export_count; 773 u32 export_count;
758 spinlock_t delayed_cmd_lock; 774 spinlock_t delayed_cmd_lock;
759 spinlock_t execute_task_lock; 775 spinlock_t execute_task_lock;
760 spinlock_t dev_reservation_lock; 776 spinlock_t dev_reservation_lock;
@@ -821,32 +837,6 @@ struct se_hba {
821 struct target_backend *backend; 837 struct target_backend *backend;
822}; 838};
823 839
824struct scsi_port_stats {
825 u64 cmd_pdus;
826 u64 tx_data_octets;
827 u64 rx_data_octets;
828};
829
830struct se_port {
831 /* RELATIVE TARGET PORT IDENTIFER */
832 u16 sep_rtpi;
833 int sep_tg_pt_secondary_stat;
834 int sep_tg_pt_secondary_write_md;
835 u32 sep_index;
836 struct scsi_port_stats sep_stats;
837 /* Used for ALUA Target Port Groups membership */
838 atomic_t sep_tg_pt_secondary_offline;
839 /* Used for PR ALL_TG_PT=1 */
840 atomic_t sep_tg_pt_ref_cnt;
841 spinlock_t sep_alua_lock;
842 struct mutex sep_tg_pt_md_mutex;
843 struct t10_alua_tg_pt_gp_member *sep_alua_tg_pt_gp_mem;
844 struct se_lun *sep_lun;
845 struct se_portal_group *sep_tpg;
846 struct list_head sep_alua_list;
847 struct list_head sep_list;
848};
849
850struct se_tpg_np { 840struct se_tpg_np {
851 struct se_portal_group *tpg_np_parent; 841 struct se_portal_group *tpg_np_parent;
852 struct config_group tpg_np_group; 842 struct config_group tpg_np_group;
@@ -872,7 +862,7 @@ struct se_portal_group {
872 /* linked list for initiator ACL list */ 862 /* linked list for initiator ACL list */
873 struct list_head acl_node_list; 863 struct list_head acl_node_list;
874 struct hlist_head tpg_lun_hlist; 864 struct hlist_head tpg_lun_hlist;
875 struct se_lun tpg_virt_lun0; 865 struct se_lun *tpg_virt_lun0;
876 /* List of TCM sessions associated wth this TPG */ 866 /* List of TCM sessions associated wth this TPG */
877 struct list_head tpg_sess_list; 867 struct list_head tpg_sess_list;
878 /* Pointer to $FABRIC_MOD dependent code */ 868 /* Pointer to $FABRIC_MOD dependent code */