1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_alua.c
5 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
7 * (c) Copyright 2009-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 ******************************************************************************/
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/configfs.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/fcntl.h>
19 #include <linux/file.h>
21 #include <scsi/scsi_proto.h>
22 #include <linux/unaligned.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
28 #include "target_core_internal.h"
29 #include "target_core_alua.h"
30 #include "target_core_ua.h"
32 static sense_reason_t
core_alua_check_transition(int state
, int valid
,
33 int *primary
, int explicit);
34 static int core_alua_set_tg_pt_secondary_state(
35 struct se_lun
*lun
, int explicit, int offline
);
37 static char *core_alua_dump_state(int state
);
39 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
40 struct t10_alua_tg_pt_gp
*tg_pt_gp
);
42 static u16 alua_lu_gps_counter
;
43 static u32 alua_lu_gps_count
;
45 static DEFINE_SPINLOCK(lu_gps_lock
);
46 static LIST_HEAD(lu_gps_list
);
48 struct t10_alua_lu_gp
*default_lu_gp
;
53 * See sbc3r35 section 5.23
56 target_emulate_report_referrals(struct se_cmd
*cmd
)
58 struct se_device
*dev
= cmd
->se_dev
;
59 struct t10_alua_lba_map
*map
;
60 struct t10_alua_lba_map_member
*map_mem
;
64 if (cmd
->data_length
< 4) {
65 pr_warn("REPORT REFERRALS allocation length %u too"
66 " small\n", cmd
->data_length
);
67 return TCM_INVALID_CDB_FIELD
;
70 buf
= transport_kmap_data_sg(cmd
);
72 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
75 spin_lock(&dev
->t10_alua
.lba_map_lock
);
76 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
77 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
78 transport_kunmap_data_sg(cmd
);
80 return TCM_UNSUPPORTED_SCSI_OPCODE
;
83 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
85 int desc_num
= off
+ 3;
89 if (cmd
->data_length
> off
)
90 put_unaligned_be64(map
->lba_map_first_lba
, &buf
[off
]);
92 if (cmd
->data_length
> off
)
93 put_unaligned_be64(map
->lba_map_last_lba
, &buf
[off
]);
97 list_for_each_entry(map_mem
, &map
->lba_map_mem_list
,
99 int alua_state
= map_mem
->lba_map_mem_alua_state
;
100 int alua_pg_id
= map_mem
->lba_map_mem_alua_pg_id
;
102 if (cmd
->data_length
> off
)
103 buf
[off
] = alua_state
& 0x0f;
105 if (cmd
->data_length
> off
)
106 buf
[off
] = (alua_pg_id
>> 8) & 0xff;
108 if (cmd
->data_length
> off
)
109 buf
[off
] = (alua_pg_id
& 0xff);
114 if (cmd
->data_length
> desc_num
)
115 buf
[desc_num
] = pg_num
;
117 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
120 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
122 put_unaligned_be16(rd_len
, &buf
[2]);
124 transport_kunmap_data_sg(cmd
);
126 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
131 * REPORT_TARGET_PORT_GROUPS
133 * See spc4r17 section 6.27
136 target_emulate_report_target_port_groups(struct se_cmd
*cmd
)
138 struct se_device
*dev
= cmd
->se_dev
;
139 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
143 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
146 * Skip over RESERVED area to first Target port group descriptor
147 * depending on the PARAMETER DATA FORMAT type..
154 if (cmd
->data_length
< off
) {
155 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
156 " small for %s header\n", cmd
->data_length
,
157 (ext_hdr
) ? "extended" : "normal");
158 return TCM_INVALID_CDB_FIELD
;
160 buf
= transport_kmap_data_sg(cmd
);
162 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
164 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
165 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
167 /* Skip empty port groups */
168 if (!tg_pt_gp
->tg_pt_gp_members
)
171 * Check if the Target port group and Target port descriptor list
172 * based on tg_pt_gp_members count will fit into the response payload.
173 * Otherwise, bump rd_len to let the initiator know we have exceeded
174 * the allocation length and the response is truncated.
176 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
178 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
182 * PREF: Preferred target port bit, determine if this
183 * bit should be set for port group.
185 if (tg_pt_gp
->tg_pt_gp_pref
)
188 * Set the ASYMMETRIC ACCESS State
190 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_access_state
& 0xff;
192 * Set supported ASYMMETRIC ACCESS State bits
194 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
198 put_unaligned_be16(tg_pt_gp
->tg_pt_gp_id
, &buf
[off
]);
201 off
++; /* Skip over Reserved */
205 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
207 * Vendor Specific field
213 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
216 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
217 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
220 * Start Target Port descriptor format
222 * See spc4r17 section 6.2.7 Table 247
224 off
+= 2; /* Skip over Obsolete */
226 * Set RELATIVE TARGET PORT IDENTIFIER
228 put_unaligned_be16(lun
->lun_tpg
->tpg_rtpi
, &buf
[off
]);
232 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
234 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
236 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
238 put_unaligned_be32(rd_len
, &buf
[0]);
241 * Fill in the Extended header parameter data format if requested
246 * Set the implicit transition time (in seconds) for the application
247 * client to use as a base for it's transition timeout value.
249 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
250 * this CDB was received upon to determine this value individually
251 * for ALUA target port group.
254 tg_pt_gp
= rcu_dereference(cmd
->se_lun
->lun_tg_pt_gp
);
256 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
259 transport_kunmap_data_sg(cmd
);
261 target_complete_cmd_with_length(cmd
, SAM_STAT_GOOD
, rd_len
+ 4);
266 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
268 * See spc4r17 section 6.35
271 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
273 struct se_device
*dev
= cmd
->se_dev
;
274 struct se_lun
*l_lun
= cmd
->se_lun
;
275 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
276 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
279 sense_reason_t rc
= TCM_NO_SENSE
;
280 u32 len
= 4; /* Skip over RESERVED area in header */
281 int alua_access_state
, primary
= 0, valid_states
;
284 if (cmd
->data_length
< 4) {
285 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
286 " small\n", cmd
->data_length
);
287 return TCM_INVALID_PARAMETER_LIST
;
290 buf
= transport_kmap_data_sg(cmd
);
292 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
295 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
296 * for the local tg_pt_gp.
299 l_tg_pt_gp
= rcu_dereference(l_lun
->lun_tg_pt_gp
);
302 pr_err("Unable to access l_lun->tg_pt_gp\n");
303 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
307 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
309 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
310 " while TPGS_EXPLICIT_ALUA is disabled\n");
311 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
314 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
317 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
319 while (len
< cmd
->data_length
) {
321 alua_access_state
= (ptr
[0] & 0x0f);
323 * Check the received ALUA access state, and determine if
324 * the state is a primary or secondary target port asymmetric
327 rc
= core_alua_check_transition(alua_access_state
, valid_states
,
331 * If the SET TARGET PORT GROUPS attempts to establish
332 * an invalid combination of target port asymmetric
333 * access states or attempts to establish an
334 * unsupported target port asymmetric access state,
335 * then the command shall be terminated with CHECK
336 * CONDITION status, with the sense key set to ILLEGAL
337 * REQUEST, and the additional sense code set to INVALID
338 * FIELD IN PARAMETER LIST.
344 * If the ASYMMETRIC ACCESS STATE field (see table 267)
345 * specifies a primary target port asymmetric access state,
346 * then the TARGET PORT GROUP OR TARGET PORT field specifies
347 * a primary target port group for which the primary target
348 * port asymmetric access state shall be changed. If the
349 * ASYMMETRIC ACCESS STATE field specifies a secondary target
350 * port asymmetric access state, then the TARGET PORT GROUP OR
351 * TARGET PORT field specifies the relative target port
352 * identifier (see 3.1.120) of the target port for which the
353 * secondary target port asymmetric access state shall be
357 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
359 * Locate the matching target port group ID from
360 * the global tg_pt_gp list
362 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
363 list_for_each_entry(tg_pt_gp
,
364 &dev
->t10_alua
.tg_pt_gps_list
,
366 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
369 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
372 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
374 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
376 if (!core_alua_do_port_transition(tg_pt_gp
,
378 alua_access_state
, 1))
381 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
382 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
385 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
390 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
391 * the Target Port in question for the incoming
392 * SET_TARGET_PORT_GROUPS op.
394 rtpi
= get_unaligned_be16(ptr
+ 2);
396 * Locate the matching relative target port identifier
397 * for the struct se_device storage object.
399 spin_lock(&dev
->se_port_lock
);
400 list_for_each_entry(lun
, &dev
->dev_sep_list
,
402 if (lun
->lun_tpg
->tpg_rtpi
!= rtpi
)
406 spin_unlock(&dev
->se_port_lock
);
408 if (!core_alua_set_tg_pt_secondary_state(
412 spin_lock(&dev
->se_port_lock
);
415 spin_unlock(&dev
->se_port_lock
);
419 rc
= TCM_INVALID_PARAMETER_LIST
;
428 transport_kunmap_data_sg(cmd
);
430 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
434 static inline void core_alua_state_nonoptimized(
437 int nonop_delay_msecs
)
440 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
441 * later to determine if processing of this cmd needs to be
442 * temporarily delayed for the Active/NonOptimized primary access state.
444 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
445 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
448 static inline sense_reason_t
core_alua_state_lba_dependent(
452 struct se_device
*dev
= cmd
->se_dev
;
453 u64 segment_size
, segment_mult
, sectors
, lba
;
455 /* Only need to check for cdb actually containing LBAs */
456 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
459 spin_lock(&dev
->t10_alua
.lba_map_lock
);
460 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
461 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
462 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
464 lba
= cmd
->t_task_lba
;
465 while (lba
< cmd
->t_task_lba
+ sectors
) {
466 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
467 struct t10_alua_lba_map_member
*map_mem
;
469 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
471 u64 start_lba
, last_lba
;
472 u64 first_lba
= map
->lba_map_first_lba
;
476 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
478 last_lba
= first_lba
+ segment_size
- 1;
479 if (start_lba
>= first_lba
&&
480 start_lba
<= last_lba
) {
486 last_lba
= map
->lba_map_last_lba
;
487 if (lba
>= first_lba
&& lba
<= last_lba
) {
495 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
496 return TCM_ALUA_TG_PT_UNAVAILABLE
;
498 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
500 if (map_mem
->lba_map_mem_alua_pg_id
!= tg_pt_gp_id
)
502 switch(map_mem
->lba_map_mem_alua_state
) {
503 case ALUA_ACCESS_STATE_STANDBY
:
504 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
505 return TCM_ALUA_TG_PT_STANDBY
;
506 case ALUA_ACCESS_STATE_UNAVAILABLE
:
507 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
508 return TCM_ALUA_TG_PT_UNAVAILABLE
;
514 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
518 static inline sense_reason_t
core_alua_state_standby(
523 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
524 * spc4r17 section 5.9.2.4.4
533 case RECEIVE_DIAGNOSTIC
:
534 case SEND_DIAGNOSTIC
:
537 case SERVICE_ACTION_IN_16
:
538 switch (cdb
[1] & 0x1f) {
539 case SAI_READ_CAPACITY_16
:
542 return TCM_ALUA_TG_PT_STANDBY
;
545 switch (cdb
[1] & 0x1f) {
546 case MI_REPORT_TARGET_PGS
:
549 return TCM_ALUA_TG_PT_STANDBY
;
551 case MAINTENANCE_OUT
:
553 case MO_SET_TARGET_PGS
:
556 return TCM_ALUA_TG_PT_STANDBY
;
559 case PERSISTENT_RESERVE_IN
:
560 case PERSISTENT_RESERVE_OUT
:
565 return TCM_ALUA_TG_PT_STANDBY
;
571 static inline sense_reason_t
core_alua_state_unavailable(
576 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
577 * spc4r17 section 5.9.2.4.5
584 switch (cdb
[1] & 0x1f) {
585 case MI_REPORT_TARGET_PGS
:
588 return TCM_ALUA_TG_PT_UNAVAILABLE
;
590 case MAINTENANCE_OUT
:
592 case MO_SET_TARGET_PGS
:
595 return TCM_ALUA_TG_PT_UNAVAILABLE
;
602 return TCM_ALUA_TG_PT_UNAVAILABLE
;
608 static inline sense_reason_t
core_alua_state_transition(
613 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
614 * spc4r17 section 5.9.2.5
621 switch (cdb
[1] & 0x1f) {
622 case MI_REPORT_TARGET_PGS
:
625 return TCM_ALUA_STATE_TRANSITION
;
632 return TCM_ALUA_STATE_TRANSITION
;
639 * return 1: Is used to signal LUN not accessible, and check condition/not ready
640 * return 0: Used to signal success
641 * return -1: Used to signal failure, and invalid cdb field
644 target_alua_state_check(struct se_cmd
*cmd
)
646 struct se_device
*dev
= cmd
->se_dev
;
647 unsigned char *cdb
= cmd
->t_task_cdb
;
648 struct se_lun
*lun
= cmd
->se_lun
;
649 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
650 int out_alua_state
, nonop_delay_msecs
;
652 sense_reason_t rc
= TCM_NO_SENSE
;
654 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
656 if (dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
660 * First, check for a struct se_port specific secondary ALUA target port
661 * access state: OFFLINE
663 if (atomic_read(&lun
->lun_tg_pt_secondary_offline
)) {
664 pr_debug("ALUA: Got secondary offline status for local"
666 return TCM_ALUA_OFFLINE
;
669 tg_pt_gp
= rcu_dereference(lun
->lun_tg_pt_gp
);
675 out_alua_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
676 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
677 tg_pt_gp_id
= tg_pt_gp
->tg_pt_gp_id
;
680 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
681 * statement so the compiler knows explicitly to check this case first.
682 * For the Optimized ALUA access state case, we want to process the
683 * incoming fabric cmd ASAP..
685 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
688 switch (out_alua_state
) {
689 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
690 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
692 case ALUA_ACCESS_STATE_STANDBY
:
693 rc
= core_alua_state_standby(cmd
, cdb
);
695 case ALUA_ACCESS_STATE_UNAVAILABLE
:
696 rc
= core_alua_state_unavailable(cmd
, cdb
);
698 case ALUA_ACCESS_STATE_TRANSITION
:
699 rc
= core_alua_state_transition(cmd
, cdb
);
701 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
702 rc
= core_alua_state_lba_dependent(cmd
, tg_pt_gp_id
);
705 * OFFLINE is a secondary ALUA target port group access state, that is
706 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
708 case ALUA_ACCESS_STATE_OFFLINE
:
710 pr_err("Unknown ALUA access state: 0x%02x\n",
712 rc
= TCM_INVALID_CDB_FIELD
;
715 if (rc
&& rc
!= TCM_INVALID_CDB_FIELD
) {
716 pr_debug("[%s]: ALUA TG Port not available, "
717 "SenseKey: NOT_READY, ASC/rc: 0x04/%d\n",
718 cmd
->se_tfo
->fabric_name
, rc
);
725 * Check implicit and explicit ALUA state change request.
727 static sense_reason_t
728 core_alua_check_transition(int state
, int valid
, int *primary
, int explicit)
731 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
732 * defined as primary target port asymmetric access states.
735 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
736 if (!(valid
& ALUA_AO_SUP
))
740 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
741 if (!(valid
& ALUA_AN_SUP
))
745 case ALUA_ACCESS_STATE_STANDBY
:
746 if (!(valid
& ALUA_S_SUP
))
750 case ALUA_ACCESS_STATE_UNAVAILABLE
:
751 if (!(valid
& ALUA_U_SUP
))
755 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
756 if (!(valid
& ALUA_LBD_SUP
))
760 case ALUA_ACCESS_STATE_OFFLINE
:
762 * OFFLINE state is defined as a secondary target port
763 * asymmetric access state.
765 if (!(valid
& ALUA_O_SUP
))
769 case ALUA_ACCESS_STATE_TRANSITION
:
770 if (!(valid
& ALUA_T_SUP
) || explicit)
772 * Transitioning is set internally and by tcmu daemon,
773 * and cannot be selected through a STPG.
779 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
780 return TCM_INVALID_PARAMETER_LIST
;
786 pr_err("ALUA access state %s not supported",
787 core_alua_dump_state(state
));
788 return TCM_INVALID_PARAMETER_LIST
;
791 static char *core_alua_dump_state(int state
)
794 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
795 return "Active/Optimized";
796 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
797 return "Active/NonOptimized";
798 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
799 return "LBA Dependent";
800 case ALUA_ACCESS_STATE_STANDBY
:
802 case ALUA_ACCESS_STATE_UNAVAILABLE
:
803 return "Unavailable";
804 case ALUA_ACCESS_STATE_OFFLINE
:
806 case ALUA_ACCESS_STATE_TRANSITION
:
807 return "Transitioning";
815 char *core_alua_dump_status(int status
)
818 case ALUA_STATUS_NONE
:
820 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
821 return "Altered by Explicit STPG";
822 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
823 return "Altered by Implicit ALUA";
832 * Used by fabric modules to determine when we need to delay processing
833 * for the Active/NonOptimized paths..
835 int core_alua_check_nonop_delay(
838 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
841 * The ALUA Active/NonOptimized access state delay can be disabled
842 * in via configfs with a value of zero
844 if (!cmd
->alua_nonop_delay
)
847 * struct se_cmd->alua_nonop_delay gets set by a target port group
848 * defined interval in core_alua_state_nonoptimized()
850 msleep_interruptible(cmd
->alua_nonop_delay
);
854 static int core_alua_write_tpg_metadata(
856 unsigned char *md_buf
,
859 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
864 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
867 ret
= kernel_write(file
, md_buf
, md_buf_len
, &pos
);
869 pr_err("Error writing ALUA metadata file: %s\n", path
);
871 return (ret
< 0) ? -EIO
: 0;
874 static int core_alua_update_tpg_primary_metadata(
875 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
877 unsigned char *md_buf
;
878 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
882 lockdep_assert_held(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
884 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
886 pr_err("Unable to allocate buf for ALUA metadata\n");
890 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
892 "alua_access_state=0x%02x\n"
893 "alua_access_status=0x%02x\n",
894 tg_pt_gp
->tg_pt_gp_id
,
895 tg_pt_gp
->tg_pt_gp_alua_access_state
,
896 tg_pt_gp
->tg_pt_gp_alua_access_status
);
899 path
= kasprintf(GFP_KERNEL
, "%s/alua/tpgs_%s/%s", db_root
,
900 &wwn
->unit_serial
[0],
901 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
903 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
910 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp
*tg_pt_gp
)
912 struct se_dev_entry
*se_deve
;
914 struct se_lun_acl
*lacl
;
916 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
917 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
920 * After an implicit target port asymmetric access state
921 * change, a device server shall establish a unit attention
922 * condition for the initiator port associated with every I_T
923 * nexus with the additional sense code set to ASYMMETRIC
924 * ACCESS STATE CHANGED.
926 * After an explicit target port asymmetric access state
927 * change, a device server shall establish a unit attention
928 * condition with the additional sense code set to ASYMMETRIC
929 * ACCESS STATE CHANGED for the initiator port associated with
930 * every I_T nexus other than the I_T nexus on which the SET
931 * TARGET PORT GROUPS command
933 if (!percpu_ref_tryget_live(&lun
->lun_ref
))
935 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
937 spin_lock(&lun
->lun_deve_lock
);
938 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
) {
939 lacl
= se_deve
->se_lun_acl
;
943 * After an explicit target port asymmetric access
944 * state change, a device server shall establish a
945 * unit attention condition with the additional sense
946 * code set to ASYMMETRIC ACCESS STATE CHANGED for
947 * the initiator port associated with every I_T nexus
948 * other than the I_T nexus on which the SET TARGET
949 * PORT GROUPS command was received.
951 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
952 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
953 (tg_pt_gp
->tg_pt_gp_alua_lun
!= NULL
) &&
954 (tg_pt_gp
->tg_pt_gp_alua_lun
== lun
))
958 * se_deve->se_lun_acl pointer may be NULL for a
959 * entry created without explicit Node+MappedLUN ACLs
961 if (lacl
&& (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
962 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
))
965 core_scsi3_ua_allocate(se_deve
, 0x2A,
966 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
968 spin_unlock(&lun
->lun_deve_lock
);
970 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
971 percpu_ref_put(&lun
->lun_ref
);
973 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
976 static int core_alua_do_transition_tg_pt(
977 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
983 mutex_lock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
984 /* Nothing to be done here */
985 if (tg_pt_gp
->tg_pt_gp_alua_access_state
== new_state
) {
986 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
990 if (explicit && new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
991 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
996 * Save the old primary ALUA access state, and set the current state
997 * to ALUA_ACCESS_STATE_TRANSITION.
999 prev_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
1000 tg_pt_gp
->tg_pt_gp_alua_access_state
= ALUA_ACCESS_STATE_TRANSITION
;
1001 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1002 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1003 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1005 core_alua_queue_state_change_ua(tg_pt_gp
);
1007 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
1008 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1013 * Check for the optional ALUA primary state transition delay
1015 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1016 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1019 * Set the current primary ALUA access state to the requested new state
1021 tg_pt_gp
->tg_pt_gp_alua_access_state
= new_state
;
1024 * Update the ALUA metadata buf that has been allocated in
1025 * core_alua_do_port_transition(), this metadata will be written
1028 * Note that there is the case where we do not want to update the
1029 * metadata when the saved metadata is being parsed in userspace
1030 * when setting the existing port access state and access status.
1032 * Also note that the failure to write out the ALUA metadata to
1033 * struct file does NOT affect the actual ALUA transition.
1035 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1036 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1039 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1040 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1041 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1042 tg_pt_gp
->tg_pt_gp_id
,
1043 core_alua_dump_state(prev_state
),
1044 core_alua_dump_state(new_state
));
1046 core_alua_queue_state_change_ua(tg_pt_gp
);
1048 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1052 int core_alua_do_port_transition(
1053 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1054 struct se_device
*l_dev
,
1055 struct se_lun
*l_lun
,
1056 struct se_node_acl
*l_nacl
,
1060 struct se_device
*dev
;
1061 struct t10_alua_lu_gp
*lu_gp
;
1062 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1063 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1064 int primary
, valid_states
, rc
= 0;
1066 if (l_dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
1069 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1070 if (core_alua_check_transition(new_state
, valid_states
, &primary
,
1074 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1075 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1076 lu_gp
= local_lu_gp_mem
->lu_gp
;
1077 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1078 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1080 * For storage objects that are members of the 'default_lu_gp',
1081 * we only do transition on the passed *l_tp_pt_gp, and not
1082 * on all of the matching target port groups IDs in default_lu_gp.
1084 if (!lu_gp
->lu_gp_id
) {
1086 * core_alua_do_transition_tg_pt() will always return
1089 l_tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1090 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1091 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1092 new_state
, explicit);
1093 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1097 * For all other LU groups aside from 'default_lu_gp', walk all of
1098 * the associated storage objects looking for a matching target port
1099 * group ID from the local target port group.
1101 spin_lock(&lu_gp
->lu_gp_lock
);
1102 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1105 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1106 atomic_inc_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1107 spin_unlock(&lu_gp
->lu_gp_lock
);
1109 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1110 list_for_each_entry(tg_pt_gp
,
1111 &dev
->t10_alua
.tg_pt_gps_list
,
1114 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1117 * If the target behavior port asymmetric access state
1118 * is changed for any target port group accessible via
1119 * a logical unit within a LU group, the target port
1120 * behavior group asymmetric access states for the same
1121 * target port group accessible via other logical units
1122 * in that LU group will also change.
1124 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1127 if (l_tg_pt_gp
== tg_pt_gp
) {
1128 tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1129 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1131 tg_pt_gp
->tg_pt_gp_alua_lun
= NULL
;
1132 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1134 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1135 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1137 * core_alua_do_transition_tg_pt() will always return
1140 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1141 new_state
, explicit);
1143 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1144 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1148 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1150 spin_lock(&lu_gp
->lu_gp_lock
);
1151 atomic_dec_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1153 spin_unlock(&lu_gp
->lu_gp_lock
);
1156 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1157 " Group IDs: %hu %s transition to primary state: %s\n",
1158 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1159 l_tg_pt_gp
->tg_pt_gp_id
,
1160 (explicit) ? "explicit" : "implicit",
1161 core_alua_dump_state(new_state
));
1164 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1168 static int core_alua_update_tpg_secondary_metadata(struct se_lun
*lun
)
1170 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
1171 unsigned char *md_buf
;
1175 mutex_lock(&lun
->lun_tg_pt_md_mutex
);
1177 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1179 pr_err("Unable to allocate buf for ALUA metadata\n");
1184 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1185 "alua_tg_pt_status=0x%02x\n",
1186 atomic_read(&lun
->lun_tg_pt_secondary_offline
),
1187 lun
->lun_tg_pt_secondary_stat
);
1189 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
) {
1190 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s+%hu/lun_%llu",
1191 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1192 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1193 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
),
1196 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s/lun_%llu",
1197 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1198 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1206 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1211 mutex_unlock(&lun
->lun_tg_pt_md_mutex
);
1215 static int core_alua_set_tg_pt_secondary_state(
1220 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1221 int trans_delay_msecs
;
1224 tg_pt_gp
= rcu_dereference(lun
->lun_tg_pt_gp
);
1227 pr_err("Unable to complete secondary state"
1231 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1233 * Set the secondary ALUA target port access state to OFFLINE
1234 * or release the previously secondary state for struct se_lun
1237 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 1);
1239 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 0);
1241 lun
->lun_tg_pt_secondary_stat
= (explicit) ?
1242 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1243 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1245 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1246 " to secondary access state: %s\n", (explicit) ? "explicit" :
1247 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1248 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1252 * Do the optional transition delay after we set the secondary
1253 * ALUA access state.
1255 if (trans_delay_msecs
!= 0)
1256 msleep_interruptible(trans_delay_msecs
);
1258 * See if we need to update the ALUA fabric port metadata for
1259 * secondary state and status
1261 if (lun
->lun_tg_pt_secondary_write_md
)
1262 core_alua_update_tpg_secondary_metadata(lun
);
1267 struct t10_alua_lba_map
*
1268 core_alua_allocate_lba_map(struct list_head
*list
,
1269 u64 first_lba
, u64 last_lba
)
1271 struct t10_alua_lba_map
*lba_map
;
1273 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1275 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1276 return ERR_PTR(-ENOMEM
);
1278 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1279 lba_map
->lba_map_first_lba
= first_lba
;
1280 lba_map
->lba_map_last_lba
= last_lba
;
1282 list_add_tail(&lba_map
->lba_map_list
, list
);
1287 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1288 int pg_id
, int state
)
1290 struct t10_alua_lba_map_member
*lba_map_mem
;
1292 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1294 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1295 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1300 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1302 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1305 lba_map_mem
->lba_map_mem_alua_state
= state
;
1306 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1308 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1309 &lba_map
->lba_map_mem_list
);
1314 core_alua_free_lba_map(struct list_head
*lba_list
)
1316 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1317 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1319 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1321 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1322 &lba_map
->lba_map_mem_list
,
1324 list_del(&lba_map_mem
->lba_map_mem_list
);
1325 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1328 list_del(&lba_map
->lba_map_list
);
1329 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1334 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1335 int segment_size
, int segment_mult
)
1337 struct list_head old_lba_map_list
;
1338 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1339 int activate
= 0, supported
;
1341 INIT_LIST_HEAD(&old_lba_map_list
);
1342 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1343 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1344 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1345 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1347 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1350 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1351 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1352 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1355 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1357 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1359 supported
|= ALUA_LBD_SUP
;
1361 supported
&= ~ALUA_LBD_SUP
;
1362 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1364 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1365 core_alua_free_lba_map(&old_lba_map_list
);
1368 struct t10_alua_lu_gp
*
1369 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1371 struct t10_alua_lu_gp
*lu_gp
;
1373 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1375 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1376 return ERR_PTR(-ENOMEM
);
1378 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1379 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1380 spin_lock_init(&lu_gp
->lu_gp_lock
);
1381 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1384 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1385 lu_gp
->lu_gp_valid_id
= 1;
1386 alua_lu_gps_count
++;
1392 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1394 struct t10_alua_lu_gp
*lu_gp_tmp
;
1397 * The lu_gp->lu_gp_id may only be set once..
1399 if (lu_gp
->lu_gp_valid_id
) {
1400 pr_warn("ALUA LU Group already has a valid ID,"
1401 " ignoring request\n");
1405 spin_lock(&lu_gps_lock
);
1406 if (alua_lu_gps_count
== 0x0000ffff) {
1407 pr_err("Maximum ALUA alua_lu_gps_count:"
1408 " 0x0000ffff reached\n");
1409 spin_unlock(&lu_gps_lock
);
1410 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1414 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1415 alua_lu_gps_counter
++;
1417 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1418 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1422 pr_warn("ALUA Logical Unit Group ID: %hu"
1423 " already exists, ignoring request\n",
1425 spin_unlock(&lu_gps_lock
);
1430 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1431 lu_gp
->lu_gp_valid_id
= 1;
1432 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1433 alua_lu_gps_count
++;
1434 spin_unlock(&lu_gps_lock
);
1439 static struct t10_alua_lu_gp_member
*
1440 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1442 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1444 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1446 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1447 return ERR_PTR(-ENOMEM
);
1449 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1450 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1451 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1453 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1454 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1459 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1461 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1463 * Once we have reached this point, config_item_put() has
1464 * already been called from target_core_alua_drop_lu_gp().
1466 * Here, we remove the *lu_gp from the global list so that
1467 * no associations can be made while we are releasing
1468 * struct t10_alua_lu_gp.
1470 spin_lock(&lu_gps_lock
);
1471 list_del(&lu_gp
->lu_gp_node
);
1472 alua_lu_gps_count
--;
1473 spin_unlock(&lu_gps_lock
);
1475 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1476 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1477 * released with core_alua_put_lu_gp_from_name()
1479 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1482 * Release reference to struct t10_alua_lu_gp * from all associated
1485 spin_lock(&lu_gp
->lu_gp_lock
);
1486 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1487 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1488 if (lu_gp_mem
->lu_gp_assoc
) {
1489 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1490 lu_gp
->lu_gp_members
--;
1491 lu_gp_mem
->lu_gp_assoc
= 0;
1493 spin_unlock(&lu_gp
->lu_gp_lock
);
1496 * lu_gp_mem is associated with a single
1497 * struct se_device->dev_alua_lu_gp_mem, and is released when
1498 * struct se_device is released via core_alua_free_lu_gp_mem().
1500 * If the passed lu_gp does NOT match the default_lu_gp, assume
1501 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1503 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1504 if (lu_gp
!= default_lu_gp
)
1505 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1508 lu_gp_mem
->lu_gp
= NULL
;
1509 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1511 spin_lock(&lu_gp
->lu_gp_lock
);
1513 spin_unlock(&lu_gp
->lu_gp_lock
);
1515 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1518 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1520 struct t10_alua_lu_gp
*lu_gp
;
1521 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1523 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1527 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1530 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1531 lu_gp
= lu_gp_mem
->lu_gp
;
1533 spin_lock(&lu_gp
->lu_gp_lock
);
1534 if (lu_gp_mem
->lu_gp_assoc
) {
1535 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1536 lu_gp
->lu_gp_members
--;
1537 lu_gp_mem
->lu_gp_assoc
= 0;
1539 spin_unlock(&lu_gp
->lu_gp_lock
);
1540 lu_gp_mem
->lu_gp
= NULL
;
1542 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1544 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1547 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1549 struct t10_alua_lu_gp
*lu_gp
;
1550 struct config_item
*ci
;
1552 spin_lock(&lu_gps_lock
);
1553 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1554 if (!lu_gp
->lu_gp_valid_id
)
1556 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1557 if (!strcmp(config_item_name(ci
), name
)) {
1558 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1559 spin_unlock(&lu_gps_lock
);
1563 spin_unlock(&lu_gps_lock
);
1568 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1570 spin_lock(&lu_gps_lock
);
1571 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1572 spin_unlock(&lu_gps_lock
);
1576 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1578 void __core_alua_attach_lu_gp_mem(
1579 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1580 struct t10_alua_lu_gp
*lu_gp
)
1582 spin_lock(&lu_gp
->lu_gp_lock
);
1583 lu_gp_mem
->lu_gp
= lu_gp
;
1584 lu_gp_mem
->lu_gp_assoc
= 1;
1585 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1586 lu_gp
->lu_gp_members
++;
1587 spin_unlock(&lu_gp
->lu_gp_lock
);
1591 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1593 void __core_alua_drop_lu_gp_mem(
1594 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1595 struct t10_alua_lu_gp
*lu_gp
)
1597 spin_lock(&lu_gp
->lu_gp_lock
);
1598 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1599 lu_gp_mem
->lu_gp
= NULL
;
1600 lu_gp_mem
->lu_gp_assoc
= 0;
1601 lu_gp
->lu_gp_members
--;
1602 spin_unlock(&lu_gp
->lu_gp_lock
);
1605 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1606 const char *name
, int def_group
)
1608 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1610 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1612 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1615 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1616 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_lun_list
);
1617 mutex_init(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1618 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1619 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1620 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1621 tg_pt_gp
->tg_pt_gp_alua_access_state
=
1622 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
;
1624 * Enable both explicit and implicit ALUA support by default
1626 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1627 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1629 * Set the default Active/NonOptimized Delay in milliseconds
1631 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1632 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1633 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1636 * Enable all supported states
1638 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1639 ALUA_T_SUP
| ALUA_O_SUP
|
1640 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1643 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1644 tg_pt_gp
->tg_pt_gp_id
=
1645 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1646 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1647 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1648 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1649 &dev
->t10_alua
.tg_pt_gps_list
);
1650 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1656 int core_alua_set_tg_pt_gp_id(
1657 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1660 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1661 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1662 u16 tg_pt_gp_id_tmp
;
1665 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1667 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1668 pr_warn("ALUA TG PT Group already has a valid ID,"
1669 " ignoring request\n");
1673 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1674 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1675 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1676 " 0x0000ffff reached\n");
1677 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1681 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1682 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1684 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1686 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1690 pr_err("ALUA Target Port Group ID: %hu already"
1691 " exists, ignoring request\n", tg_pt_gp_id
);
1692 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1697 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1698 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1699 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1700 &dev
->t10_alua
.tg_pt_gps_list
);
1701 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1702 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1707 void core_alua_free_tg_pt_gp(
1708 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1710 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1711 struct se_lun
*lun
, *next
;
1714 * Once we have reached this point, config_item_put() has already
1715 * been called from target_core_alua_drop_tg_pt_gp().
1717 * Here we remove *tg_pt_gp from the global list so that
1718 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1719 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1721 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1722 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1723 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1724 dev
->t10_alua
.alua_tg_pt_gps_count
--;
1726 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1729 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1730 * core_alua_get_tg_pt_gp_by_name() in
1731 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1732 * to be released with core_alua_put_tg_pt_gp_from_name().
1734 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1738 * Release reference to struct t10_alua_tg_pt_gp from all associated
1741 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1742 list_for_each_entry_safe(lun
, next
,
1743 &tg_pt_gp
->tg_pt_gp_lun_list
, lun_tg_pt_gp_link
) {
1744 list_del_init(&lun
->lun_tg_pt_gp_link
);
1745 tg_pt_gp
->tg_pt_gp_members
--;
1747 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1749 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1750 * assume we want to re-associate a given tg_pt_gp_mem with
1753 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1754 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1755 __target_attach_tg_pt_gp(lun
,
1756 dev
->t10_alua
.default_tg_pt_gp
);
1758 rcu_assign_pointer(lun
->lun_tg_pt_gp
, NULL
);
1759 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1761 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1763 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1766 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1769 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1770 struct se_device
*dev
, const char *name
)
1772 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1773 struct config_item
*ci
;
1775 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1776 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1778 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1780 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1781 if (!strcmp(config_item_name(ci
), name
)) {
1782 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1783 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1787 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1792 static void core_alua_put_tg_pt_gp_from_name(
1793 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1795 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1797 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1798 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1799 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1802 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
1803 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1805 struct se_dev_entry
*se_deve
;
1807 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1809 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1810 rcu_assign_pointer(lun
->lun_tg_pt_gp
, tg_pt_gp
);
1811 list_add_tail(&lun
->lun_tg_pt_gp_link
, &tg_pt_gp
->tg_pt_gp_lun_list
);
1812 tg_pt_gp
->tg_pt_gp_members
++;
1813 spin_lock(&lun
->lun_deve_lock
);
1814 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
)
1815 core_scsi3_ua_allocate(se_deve
, 0x3f,
1816 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED
);
1817 spin_unlock(&lun
->lun_deve_lock
);
1818 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1821 void target_attach_tg_pt_gp(struct se_lun
*lun
,
1822 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1824 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1825 __target_attach_tg_pt_gp(lun
, tg_pt_gp
);
1826 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1830 static void __target_detach_tg_pt_gp(struct se_lun
*lun
,
1831 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1833 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1835 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1836 list_del_init(&lun
->lun_tg_pt_gp_link
);
1837 tg_pt_gp
->tg_pt_gp_members
--;
1838 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1841 void target_detach_tg_pt_gp(struct se_lun
*lun
)
1843 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1845 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1846 tg_pt_gp
= rcu_dereference_check(lun
->lun_tg_pt_gp
,
1847 lockdep_is_held(&lun
->lun_tg_pt_gp_lock
));
1849 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1850 rcu_assign_pointer(lun
->lun_tg_pt_gp
, NULL
);
1852 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1856 static void target_swap_tg_pt_gp(struct se_lun
*lun
,
1857 struct t10_alua_tg_pt_gp
*old_tg_pt_gp
,
1858 struct t10_alua_tg_pt_gp
*new_tg_pt_gp
)
1860 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1863 __target_detach_tg_pt_gp(lun
, old_tg_pt_gp
);
1864 __target_attach_tg_pt_gp(lun
, new_tg_pt_gp
);
1867 ssize_t
core_alua_show_tg_pt_gp_info(struct se_lun
*lun
, char *page
)
1869 struct config_item
*tg_pt_ci
;
1870 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1874 tg_pt_gp
= rcu_dereference(lun
->lun_tg_pt_gp
);
1876 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1877 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1878 " %hu\nTG Port Primary Access State: %s\nTG Port "
1879 "Primary Access Status: %s\nTG Port Secondary Access"
1880 " State: %s\nTG Port Secondary Access Status: %s\n",
1881 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1882 core_alua_dump_state(
1883 tg_pt_gp
->tg_pt_gp_alua_access_state
),
1884 core_alua_dump_status(
1885 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1886 atomic_read(&lun
->lun_tg_pt_secondary_offline
) ?
1888 core_alua_dump_status(lun
->lun_tg_pt_secondary_stat
));
1895 ssize_t
core_alua_store_tg_pt_gp_info(
1900 struct se_portal_group
*tpg
= lun
->lun_tpg
;
1902 * rcu_dereference_raw protected by se_lun->lun_group symlink
1903 * reference to se_device->dev_group.
1905 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
1906 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1907 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1910 if (dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
1911 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
1914 if (count
> TG_PT_GROUP_NAME_BUF
) {
1915 pr_err("ALUA Target Port Group alias too large!\n");
1918 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1919 memcpy(buf
, page
, count
);
1921 * Any ALUA target port group alias besides "NULL" means we will be
1922 * making a new group association.
1924 if (strcmp(strstrip(buf
), "NULL")) {
1926 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1927 * struct t10_alua_tg_pt_gp. This reference is released with
1928 * core_alua_put_tg_pt_gp_from_name() below.
1930 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
1936 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1937 tg_pt_gp
= rcu_dereference_check(lun
->lun_tg_pt_gp
,
1938 lockdep_is_held(&lun
->lun_tg_pt_gp_lock
));
1941 * Clearing an existing tg_pt_gp association, and replacing
1942 * with the default_tg_pt_gp.
1944 if (!tg_pt_gp_new
) {
1945 pr_debug("Target_Core_ConfigFS: Moving"
1946 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1947 " alua/%s, ID: %hu back to"
1948 " default_tg_pt_gp\n",
1949 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1950 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1951 config_item_name(&lun
->lun_group
.cg_item
),
1953 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1954 tg_pt_gp
->tg_pt_gp_id
);
1956 target_swap_tg_pt_gp(lun
, tg_pt_gp
,
1957 dev
->t10_alua
.default_tg_pt_gp
);
1958 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1965 target_swap_tg_pt_gp(lun
, tg_pt_gp
, tg_pt_gp_new
);
1966 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1967 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1968 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1969 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1970 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1971 config_item_name(&lun
->lun_group
.cg_item
),
1972 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1973 tg_pt_gp_new
->tg_pt_gp_id
);
1975 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1981 ssize_t
core_alua_show_access_type(
1982 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1985 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
1986 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
1987 return sprintf(page
, "Implicit and Explicit\n");
1988 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
1989 return sprintf(page
, "Implicit\n");
1990 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
1991 return sprintf(page
, "Explicit\n");
1993 return sprintf(page
, "None\n");
1996 ssize_t
core_alua_store_access_type(
1997 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2004 ret
= kstrtoul(page
, 0, &tmp
);
2006 pr_err("Unable to extract alua_access_type\n");
2009 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2010 pr_err("Illegal value for alua_access_type:"
2015 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2016 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2018 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2020 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2022 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2027 ssize_t
core_alua_show_nonop_delay_msecs(
2028 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2031 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2034 ssize_t
core_alua_store_nonop_delay_msecs(
2035 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2042 ret
= kstrtoul(page
, 0, &tmp
);
2044 pr_err("Unable to extract nonop_delay_msecs\n");
2047 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2048 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2049 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2050 ALUA_MAX_NONOP_DELAY_MSECS
);
2053 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2058 ssize_t
core_alua_show_trans_delay_msecs(
2059 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2062 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2065 ssize_t
core_alua_store_trans_delay_msecs(
2066 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2073 ret
= kstrtoul(page
, 0, &tmp
);
2075 pr_err("Unable to extract trans_delay_msecs\n");
2078 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2079 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2080 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2081 ALUA_MAX_TRANS_DELAY_MSECS
);
2084 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2089 ssize_t
core_alua_show_implicit_trans_secs(
2090 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2093 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2096 ssize_t
core_alua_store_implicit_trans_secs(
2097 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2104 ret
= kstrtoul(page
, 0, &tmp
);
2106 pr_err("Unable to extract implicit_trans_secs\n");
2109 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2110 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2111 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2112 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2115 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2120 ssize_t
core_alua_show_preferred_bit(
2121 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2124 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2127 ssize_t
core_alua_store_preferred_bit(
2128 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2135 ret
= kstrtoul(page
, 0, &tmp
);
2137 pr_err("Unable to extract preferred ALUA value\n");
2140 if ((tmp
!= 0) && (tmp
!= 1)) {
2141 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2144 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2149 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2151 return sprintf(page
, "%d\n",
2152 atomic_read(&lun
->lun_tg_pt_secondary_offline
));
2155 ssize_t
core_alua_store_offline_bit(
2161 * rcu_dereference_raw protected by se_lun->lun_group symlink
2162 * reference to se_device->dev_group.
2164 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
2168 if (dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
2169 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
2172 ret
= kstrtoul(page
, 0, &tmp
);
2174 pr_err("Unable to extract alua_tg_pt_offline value\n");
2177 if ((tmp
!= 0) && (tmp
!= 1)) {
2178 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2183 ret
= core_alua_set_tg_pt_secondary_state(lun
, 0, (int)tmp
);
2190 ssize_t
core_alua_show_secondary_status(
2194 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_stat
);
2197 ssize_t
core_alua_store_secondary_status(
2205 ret
= kstrtoul(page
, 0, &tmp
);
2207 pr_err("Unable to extract alua_tg_pt_status\n");
2210 if ((tmp
!= ALUA_STATUS_NONE
) &&
2211 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2212 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2213 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2217 lun
->lun_tg_pt_secondary_stat
= (int)tmp
;
2222 ssize_t
core_alua_show_secondary_write_metadata(
2226 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_write_md
);
2229 ssize_t
core_alua_store_secondary_write_metadata(
2237 ret
= kstrtoul(page
, 0, &tmp
);
2239 pr_err("Unable to extract alua_tg_pt_write_md\n");
2242 if ((tmp
!= 0) && (tmp
!= 1)) {
2243 pr_err("Illegal value for alua_tg_pt_write_md:"
2247 lun
->lun_tg_pt_secondary_write_md
= (int)tmp
;
2252 int core_setup_alua(struct se_device
*dev
)
2254 if (!(dev
->transport_flags
&
2255 TRANSPORT_FLAG_PASSTHROUGH_ALUA
) &&
2256 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2257 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2260 * Associate this struct se_device with the default ALUA
2263 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2264 if (IS_ERR(lu_gp_mem
))
2265 return PTR_ERR(lu_gp_mem
);
2267 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2268 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2270 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2272 pr_debug("%s: Adding to default ALUA LU Group:"
2273 " core/alua/lu_gps/default_lu_gp\n",
2274 dev
->transport
->name
);