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 <asm/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
, 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
,
168 * Check if the Target port group and Target port descriptor list
169 * based on tg_pt_gp_members count will fit into the response payload.
170 * Otherwise, bump rd_len to let the initiator know we have exceeded
171 * the allocation length and the response is truncated.
173 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
175 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
179 * PREF: Preferred target port bit, determine if this
180 * bit should be set for port group.
182 if (tg_pt_gp
->tg_pt_gp_pref
)
185 * Set the ASYMMETRIC ACCESS State
187 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_access_state
& 0xff;
189 * Set supported ASYMMETRIC ACCESS State bits
191 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
195 put_unaligned_be16(tg_pt_gp
->tg_pt_gp_id
, &buf
[off
]);
198 off
++; /* Skip over Reserved */
202 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
204 * Vendor Specific field
210 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
213 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
214 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
217 * Start Target Port descriptor format
219 * See spc4r17 section 6.2.7 Table 247
221 off
+= 2; /* Skip over Obsolete */
223 * Set RELATIVE TARGET PORT IDENTIFIER
225 put_unaligned_be16(lun
->lun_rtpi
, &buf
[off
]);
229 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
231 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
233 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
235 put_unaligned_be32(rd_len
, &buf
[0]);
238 * Fill in the Extended header parameter data format if requested
243 * Set the implicit transition time (in seconds) for the application
244 * client to use as a base for it's transition timeout value.
246 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
247 * this CDB was received upon to determine this value individually
248 * for ALUA target port group.
250 spin_lock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
251 tg_pt_gp
= cmd
->se_lun
->lun_tg_pt_gp
;
253 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
254 spin_unlock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
256 transport_kunmap_data_sg(cmd
);
258 target_complete_cmd_with_length(cmd
, GOOD
, rd_len
+ 4);
263 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
265 * See spc4r17 section 6.35
268 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
270 struct se_device
*dev
= cmd
->se_dev
;
271 struct se_lun
*l_lun
= cmd
->se_lun
;
272 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
273 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
276 sense_reason_t rc
= TCM_NO_SENSE
;
277 u32 len
= 4; /* Skip over RESERVED area in header */
278 int alua_access_state
, primary
= 0, valid_states
;
281 if (cmd
->data_length
< 4) {
282 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
283 " small\n", cmd
->data_length
);
284 return TCM_INVALID_PARAMETER_LIST
;
287 buf
= transport_kmap_data_sg(cmd
);
289 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
292 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
293 * for the local tg_pt_gp.
295 spin_lock(&l_lun
->lun_tg_pt_gp_lock
);
296 l_tg_pt_gp
= l_lun
->lun_tg_pt_gp
;
298 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
299 pr_err("Unable to access l_lun->tg_pt_gp\n");
300 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
304 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
305 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
306 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
307 " while TPGS_EXPLICIT_ALUA is disabled\n");
308 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
311 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
312 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
314 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
316 while (len
< cmd
->data_length
) {
318 alua_access_state
= (ptr
[0] & 0x0f);
320 * Check the received ALUA access state, and determine if
321 * the state is a primary or secondary target port asymmetric
324 rc
= core_alua_check_transition(alua_access_state
, valid_states
,
328 * If the SET TARGET PORT GROUPS attempts to establish
329 * an invalid combination of target port asymmetric
330 * access states or attempts to establish an
331 * unsupported target port asymmetric access state,
332 * then the command shall be terminated with CHECK
333 * CONDITION status, with the sense key set to ILLEGAL
334 * REQUEST, and the additional sense code set to INVALID
335 * FIELD IN PARAMETER LIST.
341 * If the ASYMMETRIC ACCESS STATE field (see table 267)
342 * specifies a primary target port asymmetric access state,
343 * then the TARGET PORT GROUP OR TARGET PORT field specifies
344 * a primary target port group for which the primary target
345 * port asymmetric access state shall be changed. If the
346 * ASYMMETRIC ACCESS STATE field specifies a secondary target
347 * port asymmetric access state, then the TARGET PORT GROUP OR
348 * TARGET PORT field specifies the relative target port
349 * identifier (see 3.1.120) of the target port for which the
350 * secondary target port asymmetric access state shall be
354 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
356 * Locate the matching target port group ID from
357 * the global tg_pt_gp list
359 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
360 list_for_each_entry(tg_pt_gp
,
361 &dev
->t10_alua
.tg_pt_gps_list
,
363 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
366 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
369 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
371 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
373 if (!core_alua_do_port_transition(tg_pt_gp
,
375 alua_access_state
, 1))
378 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
379 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
382 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
387 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
388 * the Target Port in question for the the incoming
389 * SET_TARGET_PORT_GROUPS op.
391 rtpi
= get_unaligned_be16(ptr
+ 2);
393 * Locate the matching relative target port identifier
394 * for the struct se_device storage object.
396 spin_lock(&dev
->se_port_lock
);
397 list_for_each_entry(lun
, &dev
->dev_sep_list
,
399 if (lun
->lun_rtpi
!= rtpi
)
403 spin_unlock(&dev
->se_port_lock
);
405 if (!core_alua_set_tg_pt_secondary_state(
409 spin_lock(&dev
->se_port_lock
);
412 spin_unlock(&dev
->se_port_lock
);
416 rc
= TCM_INVALID_PARAMETER_LIST
;
425 transport_kunmap_data_sg(cmd
);
427 target_complete_cmd(cmd
, GOOD
);
431 static inline void set_ascq(struct se_cmd
*cmd
, u8 alua_ascq
)
434 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
435 * The ALUA additional sense code qualifier (ASCQ) is determined
436 * by the ALUA primary or secondary access state..
438 pr_debug("[%s]: ALUA TG Port not available, "
439 "SenseKey: NOT_READY, ASC/ASCQ: "
441 cmd
->se_tfo
->fabric_name
, alua_ascq
);
443 cmd
->scsi_asc
= 0x04;
444 cmd
->scsi_ascq
= alua_ascq
;
447 static inline void core_alua_state_nonoptimized(
450 int nonop_delay_msecs
)
453 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
454 * later to determine if processing of this cmd needs to be
455 * temporarily delayed for the Active/NonOptimized primary access state.
457 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
458 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
461 static inline int core_alua_state_lba_dependent(
463 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
465 struct se_device
*dev
= cmd
->se_dev
;
466 u64 segment_size
, segment_mult
, sectors
, lba
;
468 /* Only need to check for cdb actually containing LBAs */
469 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
472 spin_lock(&dev
->t10_alua
.lba_map_lock
);
473 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
474 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
475 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
477 lba
= cmd
->t_task_lba
;
478 while (lba
< cmd
->t_task_lba
+ sectors
) {
479 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
480 struct t10_alua_lba_map_member
*map_mem
;
482 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
484 u64 start_lba
, last_lba
;
485 u64 first_lba
= map
->lba_map_first_lba
;
489 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
491 last_lba
= first_lba
+ segment_size
- 1;
492 if (start_lba
>= first_lba
&&
493 start_lba
<= last_lba
) {
499 last_lba
= map
->lba_map_last_lba
;
500 if (lba
>= first_lba
&& lba
<= last_lba
) {
508 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
509 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
512 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
514 if (map_mem
->lba_map_mem_alua_pg_id
!=
515 tg_pt_gp
->tg_pt_gp_id
)
517 switch(map_mem
->lba_map_mem_alua_state
) {
518 case ALUA_ACCESS_STATE_STANDBY
:
519 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
520 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
522 case ALUA_ACCESS_STATE_UNAVAILABLE
:
523 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
524 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
531 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
535 static inline int core_alua_state_standby(
540 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
541 * spc4r17 section 5.9.2.4.4
550 case RECEIVE_DIAGNOSTIC
:
551 case SEND_DIAGNOSTIC
:
554 case SERVICE_ACTION_IN_16
:
555 switch (cdb
[1] & 0x1f) {
556 case SAI_READ_CAPACITY_16
:
559 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
563 switch (cdb
[1] & 0x1f) {
564 case MI_REPORT_TARGET_PGS
:
567 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
570 case MAINTENANCE_OUT
:
572 case MO_SET_TARGET_PGS
:
575 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
579 case PERSISTENT_RESERVE_IN
:
580 case PERSISTENT_RESERVE_OUT
:
585 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
592 static inline int core_alua_state_unavailable(
597 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
598 * spc4r17 section 5.9.2.4.5
605 switch (cdb
[1] & 0x1f) {
606 case MI_REPORT_TARGET_PGS
:
609 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
612 case MAINTENANCE_OUT
:
614 case MO_SET_TARGET_PGS
:
617 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
625 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
632 static inline int core_alua_state_transition(
637 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
638 * spc4r17 section 5.9.2.5
645 switch (cdb
[1] & 0x1f) {
646 case MI_REPORT_TARGET_PGS
:
649 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
657 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
665 * return 1: Is used to signal LUN not accessible, and check condition/not ready
666 * return 0: Used to signal success
667 * return -1: Used to signal failure, and invalid cdb field
670 target_alua_state_check(struct se_cmd
*cmd
)
672 struct se_device
*dev
= cmd
->se_dev
;
673 unsigned char *cdb
= cmd
->t_task_cdb
;
674 struct se_lun
*lun
= cmd
->se_lun
;
675 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
676 int out_alua_state
, nonop_delay_msecs
;
678 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
680 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
684 * First, check for a struct se_port specific secondary ALUA target port
685 * access state: OFFLINE
687 if (atomic_read(&lun
->lun_tg_pt_secondary_offline
)) {
688 pr_debug("ALUA: Got secondary offline status for local"
690 set_ascq(cmd
, ASCQ_04H_ALUA_OFFLINE
);
691 return TCM_CHECK_CONDITION_NOT_READY
;
694 if (!lun
->lun_tg_pt_gp
)
697 spin_lock(&lun
->lun_tg_pt_gp_lock
);
698 tg_pt_gp
= lun
->lun_tg_pt_gp
;
699 out_alua_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
700 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
702 // XXX: keeps using tg_pt_gp witout reference after unlock
703 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
705 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
706 * statement so the compiler knows explicitly to check this case first.
707 * For the Optimized ALUA access state case, we want to process the
708 * incoming fabric cmd ASAP..
710 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
713 switch (out_alua_state
) {
714 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
715 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
717 case ALUA_ACCESS_STATE_STANDBY
:
718 if (core_alua_state_standby(cmd
, cdb
))
719 return TCM_CHECK_CONDITION_NOT_READY
;
721 case ALUA_ACCESS_STATE_UNAVAILABLE
:
722 if (core_alua_state_unavailable(cmd
, cdb
))
723 return TCM_CHECK_CONDITION_NOT_READY
;
725 case ALUA_ACCESS_STATE_TRANSITION
:
726 if (core_alua_state_transition(cmd
, cdb
))
727 return TCM_CHECK_CONDITION_NOT_READY
;
729 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
730 if (core_alua_state_lba_dependent(cmd
, tg_pt_gp
))
731 return TCM_CHECK_CONDITION_NOT_READY
;
734 * OFFLINE is a secondary ALUA target port group access state, that is
735 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
737 case ALUA_ACCESS_STATE_OFFLINE
:
739 pr_err("Unknown ALUA access state: 0x%02x\n",
741 return TCM_INVALID_CDB_FIELD
;
748 * Check implicit and explicit ALUA state change request.
750 static sense_reason_t
751 core_alua_check_transition(int state
, int valid
, int *primary
, int explicit)
754 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
755 * defined as primary target port asymmetric access states.
758 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
759 if (!(valid
& ALUA_AO_SUP
))
763 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
764 if (!(valid
& ALUA_AN_SUP
))
768 case ALUA_ACCESS_STATE_STANDBY
:
769 if (!(valid
& ALUA_S_SUP
))
773 case ALUA_ACCESS_STATE_UNAVAILABLE
:
774 if (!(valid
& ALUA_U_SUP
))
778 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
779 if (!(valid
& ALUA_LBD_SUP
))
783 case ALUA_ACCESS_STATE_OFFLINE
:
785 * OFFLINE state is defined as a secondary target port
786 * asymmetric access state.
788 if (!(valid
& ALUA_O_SUP
))
792 case ALUA_ACCESS_STATE_TRANSITION
:
793 if (!(valid
& ALUA_T_SUP
) || explicit)
795 * Transitioning is set internally and by tcmu daemon,
796 * and cannot be selected through a STPG.
802 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
803 return TCM_INVALID_PARAMETER_LIST
;
809 pr_err("ALUA access state %s not supported",
810 core_alua_dump_state(state
));
811 return TCM_INVALID_PARAMETER_LIST
;
814 static char *core_alua_dump_state(int state
)
817 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
818 return "Active/Optimized";
819 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
820 return "Active/NonOptimized";
821 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
822 return "LBA Dependent";
823 case ALUA_ACCESS_STATE_STANDBY
:
825 case ALUA_ACCESS_STATE_UNAVAILABLE
:
826 return "Unavailable";
827 case ALUA_ACCESS_STATE_OFFLINE
:
829 case ALUA_ACCESS_STATE_TRANSITION
:
830 return "Transitioning";
838 char *core_alua_dump_status(int status
)
841 case ALUA_STATUS_NONE
:
843 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
844 return "Altered by Explicit STPG";
845 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
846 return "Altered by Implicit ALUA";
855 * Used by fabric modules to determine when we need to delay processing
856 * for the Active/NonOptimized paths..
858 int core_alua_check_nonop_delay(
861 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
866 * The ALUA Active/NonOptimized access state delay can be disabled
867 * in via configfs with a value of zero
869 if (!cmd
->alua_nonop_delay
)
872 * struct se_cmd->alua_nonop_delay gets set by a target port group
873 * defined interval in core_alua_state_nonoptimized()
875 msleep_interruptible(cmd
->alua_nonop_delay
);
878 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
880 static int core_alua_write_tpg_metadata(
882 unsigned char *md_buf
,
885 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
890 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
893 ret
= kernel_write(file
, md_buf
, md_buf_len
, &pos
);
895 pr_err("Error writing ALUA metadata file: %s\n", path
);
897 return (ret
< 0) ? -EIO
: 0;
900 static int core_alua_update_tpg_primary_metadata(
901 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
903 unsigned char *md_buf
;
904 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
908 lockdep_assert_held(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
910 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
912 pr_err("Unable to allocate buf for ALUA metadata\n");
916 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
918 "alua_access_state=0x%02x\n"
919 "alua_access_status=0x%02x\n",
920 tg_pt_gp
->tg_pt_gp_id
,
921 tg_pt_gp
->tg_pt_gp_alua_access_state
,
922 tg_pt_gp
->tg_pt_gp_alua_access_status
);
925 path
= kasprintf(GFP_KERNEL
, "%s/alua/tpgs_%s/%s", db_root
,
926 &wwn
->unit_serial
[0],
927 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
929 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
936 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp
*tg_pt_gp
)
938 struct se_dev_entry
*se_deve
;
940 struct se_lun_acl
*lacl
;
942 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
943 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
946 * After an implicit target port asymmetric access state
947 * change, a device server shall establish a unit attention
948 * condition for the initiator port associated with every I_T
949 * nexus with the additional sense code set to ASYMMETRIC
950 * ACCESS STATE CHANGED.
952 * After an explicit target port asymmetric access state
953 * change, a device server shall establish a unit attention
954 * condition with the additional sense code set to ASYMMETRIC
955 * ACCESS STATE CHANGED for the initiator port associated with
956 * every I_T nexus other than the I_T nexus on which the SET
957 * TARGET PORT GROUPS command
959 if (!percpu_ref_tryget_live(&lun
->lun_ref
))
961 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
963 spin_lock(&lun
->lun_deve_lock
);
964 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
) {
965 lacl
= rcu_dereference_check(se_deve
->se_lun_acl
,
966 lockdep_is_held(&lun
->lun_deve_lock
));
970 * After an explicit target port asymmetric access
971 * state change, a device server shall establish a
972 * unit attention condition with the additional sense
973 * code set to ASYMMETRIC ACCESS STATE CHANGED for
974 * the initiator port associated with every I_T nexus
975 * other than the I_T nexus on which the SET TARGET
976 * PORT GROUPS command was received.
978 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
979 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
980 (tg_pt_gp
->tg_pt_gp_alua_lun
!= NULL
) &&
981 (tg_pt_gp
->tg_pt_gp_alua_lun
== lun
))
985 * se_deve->se_lun_acl pointer may be NULL for a
986 * entry created without explicit Node+MappedLUN ACLs
988 if (lacl
&& (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
989 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
))
992 core_scsi3_ua_allocate(se_deve
, 0x2A,
993 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
995 spin_unlock(&lun
->lun_deve_lock
);
997 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
998 percpu_ref_put(&lun
->lun_ref
);
1000 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1003 static int core_alua_do_transition_tg_pt(
1004 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1010 mutex_lock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1011 /* Nothing to be done here */
1012 if (tg_pt_gp
->tg_pt_gp_alua_access_state
== new_state
) {
1013 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1017 if (explicit && new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
1018 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1023 * Save the old primary ALUA access state, and set the current state
1024 * to ALUA_ACCESS_STATE_TRANSITION.
1026 prev_state
= tg_pt_gp
->tg_pt_gp_alua_access_state
;
1027 tg_pt_gp
->tg_pt_gp_alua_access_state
= ALUA_ACCESS_STATE_TRANSITION
;
1028 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1029 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1030 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1032 core_alua_queue_state_change_ua(tg_pt_gp
);
1034 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
) {
1035 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1040 * Check for the optional ALUA primary state transition delay
1042 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1043 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1046 * Set the current primary ALUA access state to the requested new state
1048 tg_pt_gp
->tg_pt_gp_alua_access_state
= new_state
;
1051 * Update the ALUA metadata buf that has been allocated in
1052 * core_alua_do_port_transition(), this metadata will be written
1055 * Note that there is the case where we do not want to update the
1056 * metadata when the saved metadata is being parsed in userspace
1057 * when setting the existing port access state and access status.
1059 * Also note that the failure to write out the ALUA metadata to
1060 * struct file does NOT affect the actual ALUA transition.
1062 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1063 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1066 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1067 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1068 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1069 tg_pt_gp
->tg_pt_gp_id
,
1070 core_alua_dump_state(prev_state
),
1071 core_alua_dump_state(new_state
));
1073 core_alua_queue_state_change_ua(tg_pt_gp
);
1075 mutex_unlock(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1079 int core_alua_do_port_transition(
1080 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1081 struct se_device
*l_dev
,
1082 struct se_lun
*l_lun
,
1083 struct se_node_acl
*l_nacl
,
1087 struct se_device
*dev
;
1088 struct t10_alua_lu_gp
*lu_gp
;
1089 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1090 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1091 int primary
, valid_states
, rc
= 0;
1093 if (l_dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
)
1096 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1097 if (core_alua_check_transition(new_state
, valid_states
, &primary
,
1101 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1102 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1103 lu_gp
= local_lu_gp_mem
->lu_gp
;
1104 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1105 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1107 * For storage objects that are members of the 'default_lu_gp',
1108 * we only do transition on the passed *l_tp_pt_gp, and not
1109 * on all of the matching target port groups IDs in default_lu_gp.
1111 if (!lu_gp
->lu_gp_id
) {
1113 * core_alua_do_transition_tg_pt() will always return
1116 l_tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1117 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1118 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1119 new_state
, explicit);
1120 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1124 * For all other LU groups aside from 'default_lu_gp', walk all of
1125 * the associated storage objects looking for a matching target port
1126 * group ID from the local target port group.
1128 spin_lock(&lu_gp
->lu_gp_lock
);
1129 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1132 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1133 atomic_inc_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1134 spin_unlock(&lu_gp
->lu_gp_lock
);
1136 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1137 list_for_each_entry(tg_pt_gp
,
1138 &dev
->t10_alua
.tg_pt_gps_list
,
1141 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1144 * If the target behavior port asymmetric access state
1145 * is changed for any target port group accessible via
1146 * a logical unit within a LU group, the target port
1147 * behavior group asymmetric access states for the same
1148 * target port group accessible via other logical units
1149 * in that LU group will also change.
1151 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1154 if (l_tg_pt_gp
== tg_pt_gp
) {
1155 tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1156 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1158 tg_pt_gp
->tg_pt_gp_alua_lun
= NULL
;
1159 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1161 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1162 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1164 * core_alua_do_transition_tg_pt() will always return
1167 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1168 new_state
, explicit);
1170 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1171 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1175 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1177 spin_lock(&lu_gp
->lu_gp_lock
);
1178 atomic_dec_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1180 spin_unlock(&lu_gp
->lu_gp_lock
);
1183 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1184 " Group IDs: %hu %s transition to primary state: %s\n",
1185 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1186 l_tg_pt_gp
->tg_pt_gp_id
,
1187 (explicit) ? "explicit" : "implicit",
1188 core_alua_dump_state(new_state
));
1191 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1195 static int core_alua_update_tpg_secondary_metadata(struct se_lun
*lun
)
1197 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
1198 unsigned char *md_buf
;
1202 mutex_lock(&lun
->lun_tg_pt_md_mutex
);
1204 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1206 pr_err("Unable to allocate buf for ALUA metadata\n");
1211 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1212 "alua_tg_pt_status=0x%02x\n",
1213 atomic_read(&lun
->lun_tg_pt_secondary_offline
),
1214 lun
->lun_tg_pt_secondary_stat
);
1216 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
) {
1217 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s+%hu/lun_%llu",
1218 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1219 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1220 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
),
1223 path
= kasprintf(GFP_KERNEL
, "%s/alua/%s/%s/lun_%llu",
1224 db_root
, se_tpg
->se_tpg_tfo
->fabric_name
,
1225 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
),
1233 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1238 mutex_unlock(&lun
->lun_tg_pt_md_mutex
);
1242 static int core_alua_set_tg_pt_secondary_state(
1247 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1248 int trans_delay_msecs
;
1250 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1251 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1253 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1254 pr_err("Unable to complete secondary state"
1258 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1260 * Set the secondary ALUA target port access state to OFFLINE
1261 * or release the previously secondary state for struct se_lun
1264 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 1);
1266 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 0);
1268 lun
->lun_tg_pt_secondary_stat
= (explicit) ?
1269 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1270 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1272 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1273 " to secondary access state: %s\n", (explicit) ? "explicit" :
1274 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1275 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1277 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1279 * Do the optional transition delay after we set the secondary
1280 * ALUA access state.
1282 if (trans_delay_msecs
!= 0)
1283 msleep_interruptible(trans_delay_msecs
);
1285 * See if we need to update the ALUA fabric port metadata for
1286 * secondary state and status
1288 if (lun
->lun_tg_pt_secondary_write_md
)
1289 core_alua_update_tpg_secondary_metadata(lun
);
1294 struct t10_alua_lba_map
*
1295 core_alua_allocate_lba_map(struct list_head
*list
,
1296 u64 first_lba
, u64 last_lba
)
1298 struct t10_alua_lba_map
*lba_map
;
1300 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1302 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1303 return ERR_PTR(-ENOMEM
);
1305 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1306 lba_map
->lba_map_first_lba
= first_lba
;
1307 lba_map
->lba_map_last_lba
= last_lba
;
1309 list_add_tail(&lba_map
->lba_map_list
, list
);
1314 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1315 int pg_id
, int state
)
1317 struct t10_alua_lba_map_member
*lba_map_mem
;
1319 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1321 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1322 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1327 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1329 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1332 lba_map_mem
->lba_map_mem_alua_state
= state
;
1333 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1335 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1336 &lba_map
->lba_map_mem_list
);
1341 core_alua_free_lba_map(struct list_head
*lba_list
)
1343 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1344 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1346 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1348 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1349 &lba_map
->lba_map_mem_list
,
1351 list_del(&lba_map_mem
->lba_map_mem_list
);
1352 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1355 list_del(&lba_map
->lba_map_list
);
1356 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1361 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1362 int segment_size
, int segment_mult
)
1364 struct list_head old_lba_map_list
;
1365 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1366 int activate
= 0, supported
;
1368 INIT_LIST_HEAD(&old_lba_map_list
);
1369 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1370 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1371 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1372 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1374 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1377 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1378 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1379 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1382 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1384 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1386 supported
|= ALUA_LBD_SUP
;
1388 supported
&= ~ALUA_LBD_SUP
;
1389 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1391 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1392 core_alua_free_lba_map(&old_lba_map_list
);
1395 struct t10_alua_lu_gp
*
1396 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1398 struct t10_alua_lu_gp
*lu_gp
;
1400 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1402 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1403 return ERR_PTR(-ENOMEM
);
1405 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1406 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1407 spin_lock_init(&lu_gp
->lu_gp_lock
);
1408 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1411 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1412 lu_gp
->lu_gp_valid_id
= 1;
1413 alua_lu_gps_count
++;
1419 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1421 struct t10_alua_lu_gp
*lu_gp_tmp
;
1424 * The lu_gp->lu_gp_id may only be set once..
1426 if (lu_gp
->lu_gp_valid_id
) {
1427 pr_warn("ALUA LU Group already has a valid ID,"
1428 " ignoring request\n");
1432 spin_lock(&lu_gps_lock
);
1433 if (alua_lu_gps_count
== 0x0000ffff) {
1434 pr_err("Maximum ALUA alua_lu_gps_count:"
1435 " 0x0000ffff reached\n");
1436 spin_unlock(&lu_gps_lock
);
1437 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1441 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1442 alua_lu_gps_counter
++;
1444 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1445 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1449 pr_warn("ALUA Logical Unit Group ID: %hu"
1450 " already exists, ignoring request\n",
1452 spin_unlock(&lu_gps_lock
);
1457 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1458 lu_gp
->lu_gp_valid_id
= 1;
1459 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1460 alua_lu_gps_count
++;
1461 spin_unlock(&lu_gps_lock
);
1466 static struct t10_alua_lu_gp_member
*
1467 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1469 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1471 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1473 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1474 return ERR_PTR(-ENOMEM
);
1476 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1477 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1478 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1480 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1481 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1486 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1488 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1490 * Once we have reached this point, config_item_put() has
1491 * already been called from target_core_alua_drop_lu_gp().
1493 * Here, we remove the *lu_gp from the global list so that
1494 * no associations can be made while we are releasing
1495 * struct t10_alua_lu_gp.
1497 spin_lock(&lu_gps_lock
);
1498 list_del(&lu_gp
->lu_gp_node
);
1499 alua_lu_gps_count
--;
1500 spin_unlock(&lu_gps_lock
);
1502 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1503 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1504 * released with core_alua_put_lu_gp_from_name()
1506 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1509 * Release reference to struct t10_alua_lu_gp * from all associated
1512 spin_lock(&lu_gp
->lu_gp_lock
);
1513 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1514 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1515 if (lu_gp_mem
->lu_gp_assoc
) {
1516 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1517 lu_gp
->lu_gp_members
--;
1518 lu_gp_mem
->lu_gp_assoc
= 0;
1520 spin_unlock(&lu_gp
->lu_gp_lock
);
1523 * lu_gp_mem is associated with a single
1524 * struct se_device->dev_alua_lu_gp_mem, and is released when
1525 * struct se_device is released via core_alua_free_lu_gp_mem().
1527 * If the passed lu_gp does NOT match the default_lu_gp, assume
1528 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1530 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1531 if (lu_gp
!= default_lu_gp
)
1532 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1535 lu_gp_mem
->lu_gp
= NULL
;
1536 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1538 spin_lock(&lu_gp
->lu_gp_lock
);
1540 spin_unlock(&lu_gp
->lu_gp_lock
);
1542 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1545 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1547 struct t10_alua_lu_gp
*lu_gp
;
1548 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1550 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1554 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1557 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1558 lu_gp
= lu_gp_mem
->lu_gp
;
1560 spin_lock(&lu_gp
->lu_gp_lock
);
1561 if (lu_gp_mem
->lu_gp_assoc
) {
1562 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1563 lu_gp
->lu_gp_members
--;
1564 lu_gp_mem
->lu_gp_assoc
= 0;
1566 spin_unlock(&lu_gp
->lu_gp_lock
);
1567 lu_gp_mem
->lu_gp
= NULL
;
1569 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1571 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1574 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1576 struct t10_alua_lu_gp
*lu_gp
;
1577 struct config_item
*ci
;
1579 spin_lock(&lu_gps_lock
);
1580 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1581 if (!lu_gp
->lu_gp_valid_id
)
1583 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1584 if (!strcmp(config_item_name(ci
), name
)) {
1585 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1586 spin_unlock(&lu_gps_lock
);
1590 spin_unlock(&lu_gps_lock
);
1595 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1597 spin_lock(&lu_gps_lock
);
1598 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1599 spin_unlock(&lu_gps_lock
);
1603 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1605 void __core_alua_attach_lu_gp_mem(
1606 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1607 struct t10_alua_lu_gp
*lu_gp
)
1609 spin_lock(&lu_gp
->lu_gp_lock
);
1610 lu_gp_mem
->lu_gp
= lu_gp
;
1611 lu_gp_mem
->lu_gp_assoc
= 1;
1612 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1613 lu_gp
->lu_gp_members
++;
1614 spin_unlock(&lu_gp
->lu_gp_lock
);
1618 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1620 void __core_alua_drop_lu_gp_mem(
1621 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1622 struct t10_alua_lu_gp
*lu_gp
)
1624 spin_lock(&lu_gp
->lu_gp_lock
);
1625 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1626 lu_gp_mem
->lu_gp
= NULL
;
1627 lu_gp_mem
->lu_gp_assoc
= 0;
1628 lu_gp
->lu_gp_members
--;
1629 spin_unlock(&lu_gp
->lu_gp_lock
);
1632 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1633 const char *name
, int def_group
)
1635 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1637 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1639 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1642 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1643 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_lun_list
);
1644 mutex_init(&tg_pt_gp
->tg_pt_gp_transition_mutex
);
1645 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1646 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1647 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1648 tg_pt_gp
->tg_pt_gp_alua_access_state
=
1649 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
;
1651 * Enable both explicit and implicit ALUA support by default
1653 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1654 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1656 * Set the default Active/NonOptimized Delay in milliseconds
1658 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1659 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1660 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1663 * Enable all supported states
1665 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1666 ALUA_T_SUP
| ALUA_O_SUP
|
1667 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1670 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1671 tg_pt_gp
->tg_pt_gp_id
=
1672 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1673 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1674 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1675 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1676 &dev
->t10_alua
.tg_pt_gps_list
);
1677 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1683 int core_alua_set_tg_pt_gp_id(
1684 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1687 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1688 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1689 u16 tg_pt_gp_id_tmp
;
1692 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1694 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1695 pr_warn("ALUA TG PT Group already has a valid ID,"
1696 " ignoring request\n");
1700 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1701 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1702 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1703 " 0x0000ffff reached\n");
1704 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1705 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1709 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1710 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1712 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1714 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1718 pr_err("ALUA Target Port Group ID: %hu already"
1719 " exists, ignoring request\n", tg_pt_gp_id
);
1720 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1725 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1726 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1727 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1728 &dev
->t10_alua
.tg_pt_gps_list
);
1729 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1730 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1735 void core_alua_free_tg_pt_gp(
1736 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1738 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1739 struct se_lun
*lun
, *next
;
1742 * Once we have reached this point, config_item_put() has already
1743 * been called from target_core_alua_drop_tg_pt_gp().
1745 * Here we remove *tg_pt_gp from the global list so that
1746 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1747 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1749 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1750 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1751 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1752 dev
->t10_alua
.alua_tg_pt_gps_count
--;
1754 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1757 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1758 * core_alua_get_tg_pt_gp_by_name() in
1759 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1760 * to be released with core_alua_put_tg_pt_gp_from_name().
1762 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1766 * Release reference to struct t10_alua_tg_pt_gp from all associated
1769 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1770 list_for_each_entry_safe(lun
, next
,
1771 &tg_pt_gp
->tg_pt_gp_lun_list
, lun_tg_pt_gp_link
) {
1772 list_del_init(&lun
->lun_tg_pt_gp_link
);
1773 tg_pt_gp
->tg_pt_gp_members
--;
1775 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1777 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1778 * assume we want to re-associate a given tg_pt_gp_mem with
1781 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1782 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1783 __target_attach_tg_pt_gp(lun
,
1784 dev
->t10_alua
.default_tg_pt_gp
);
1786 lun
->lun_tg_pt_gp
= NULL
;
1787 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1789 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1791 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1793 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1796 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1797 struct se_device
*dev
, const char *name
)
1799 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1800 struct config_item
*ci
;
1802 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1803 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1805 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1807 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1808 if (!strcmp(config_item_name(ci
), name
)) {
1809 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1810 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1814 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1819 static void core_alua_put_tg_pt_gp_from_name(
1820 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1822 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1824 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1825 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1826 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1829 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
1830 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1832 struct se_dev_entry
*se_deve
;
1834 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1836 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1837 lun
->lun_tg_pt_gp
= tg_pt_gp
;
1838 list_add_tail(&lun
->lun_tg_pt_gp_link
, &tg_pt_gp
->tg_pt_gp_lun_list
);
1839 tg_pt_gp
->tg_pt_gp_members
++;
1840 spin_lock(&lun
->lun_deve_lock
);
1841 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
)
1842 core_scsi3_ua_allocate(se_deve
, 0x3f,
1843 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED
);
1844 spin_unlock(&lun
->lun_deve_lock
);
1845 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1848 void target_attach_tg_pt_gp(struct se_lun
*lun
,
1849 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1851 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1852 __target_attach_tg_pt_gp(lun
, tg_pt_gp
);
1853 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1856 static void __target_detach_tg_pt_gp(struct se_lun
*lun
,
1857 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1859 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1861 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1862 list_del_init(&lun
->lun_tg_pt_gp_link
);
1863 tg_pt_gp
->tg_pt_gp_members
--;
1864 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1866 lun
->lun_tg_pt_gp
= NULL
;
1869 void target_detach_tg_pt_gp(struct se_lun
*lun
)
1871 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1873 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1874 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1876 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1877 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1880 ssize_t
core_alua_show_tg_pt_gp_info(struct se_lun
*lun
, char *page
)
1882 struct config_item
*tg_pt_ci
;
1883 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1886 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1887 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1889 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1890 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1891 " %hu\nTG Port Primary Access State: %s\nTG Port "
1892 "Primary Access Status: %s\nTG Port Secondary Access"
1893 " State: %s\nTG Port Secondary Access Status: %s\n",
1894 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1895 core_alua_dump_state(
1896 tg_pt_gp
->tg_pt_gp_alua_access_state
),
1897 core_alua_dump_status(
1898 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1899 atomic_read(&lun
->lun_tg_pt_secondary_offline
) ?
1901 core_alua_dump_status(lun
->lun_tg_pt_secondary_stat
));
1903 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1908 ssize_t
core_alua_store_tg_pt_gp_info(
1913 struct se_portal_group
*tpg
= lun
->lun_tpg
;
1915 * rcu_dereference_raw protected by se_lun->lun_group symlink
1916 * reference to se_device->dev_group.
1918 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
1919 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1920 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1923 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
1924 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
1927 if (count
> TG_PT_GROUP_NAME_BUF
) {
1928 pr_err("ALUA Target Port Group alias too large!\n");
1931 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1932 memcpy(buf
, page
, count
);
1934 * Any ALUA target port group alias besides "NULL" means we will be
1935 * making a new group association.
1937 if (strcmp(strstrip(buf
), "NULL")) {
1939 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1940 * struct t10_alua_tg_pt_gp. This reference is released with
1941 * core_alua_put_tg_pt_gp_from_name() below.
1943 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
1949 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1950 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1953 * Clearing an existing tg_pt_gp association, and replacing
1954 * with the default_tg_pt_gp.
1956 if (!tg_pt_gp_new
) {
1957 pr_debug("Target_Core_ConfigFS: Moving"
1958 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1959 " alua/%s, ID: %hu back to"
1960 " default_tg_pt_gp\n",
1961 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1962 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1963 config_item_name(&lun
->lun_group
.cg_item
),
1965 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1966 tg_pt_gp
->tg_pt_gp_id
);
1968 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1969 __target_attach_tg_pt_gp(lun
,
1970 dev
->t10_alua
.default_tg_pt_gp
);
1971 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1975 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1979 __target_attach_tg_pt_gp(lun
, tg_pt_gp_new
);
1980 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1981 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1982 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
1983 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1984 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1985 config_item_name(&lun
->lun_group
.cg_item
),
1986 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
1987 tg_pt_gp_new
->tg_pt_gp_id
);
1989 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
1993 ssize_t
core_alua_show_access_type(
1994 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1997 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
1998 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
1999 return sprintf(page
, "Implicit and Explicit\n");
2000 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
2001 return sprintf(page
, "Implicit\n");
2002 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
2003 return sprintf(page
, "Explicit\n");
2005 return sprintf(page
, "None\n");
2008 ssize_t
core_alua_store_access_type(
2009 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2016 ret
= kstrtoul(page
, 0, &tmp
);
2018 pr_err("Unable to extract alua_access_type\n");
2021 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2022 pr_err("Illegal value for alua_access_type:"
2027 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2028 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2030 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2032 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2034 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2039 ssize_t
core_alua_show_nonop_delay_msecs(
2040 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2043 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2046 ssize_t
core_alua_store_nonop_delay_msecs(
2047 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2054 ret
= kstrtoul(page
, 0, &tmp
);
2056 pr_err("Unable to extract nonop_delay_msecs\n");
2059 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2060 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2061 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2062 ALUA_MAX_NONOP_DELAY_MSECS
);
2065 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2070 ssize_t
core_alua_show_trans_delay_msecs(
2071 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2074 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2077 ssize_t
core_alua_store_trans_delay_msecs(
2078 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2085 ret
= kstrtoul(page
, 0, &tmp
);
2087 pr_err("Unable to extract trans_delay_msecs\n");
2090 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2091 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2092 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2093 ALUA_MAX_TRANS_DELAY_MSECS
);
2096 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2101 ssize_t
core_alua_show_implicit_trans_secs(
2102 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2105 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2108 ssize_t
core_alua_store_implicit_trans_secs(
2109 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2116 ret
= kstrtoul(page
, 0, &tmp
);
2118 pr_err("Unable to extract implicit_trans_secs\n");
2121 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2122 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2123 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2124 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2127 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2132 ssize_t
core_alua_show_preferred_bit(
2133 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2136 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2139 ssize_t
core_alua_store_preferred_bit(
2140 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2147 ret
= kstrtoul(page
, 0, &tmp
);
2149 pr_err("Unable to extract preferred ALUA value\n");
2152 if ((tmp
!= 0) && (tmp
!= 1)) {
2153 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2156 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2161 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2163 return sprintf(page
, "%d\n",
2164 atomic_read(&lun
->lun_tg_pt_secondary_offline
));
2167 ssize_t
core_alua_store_offline_bit(
2173 * rcu_dereference_raw protected by se_lun->lun_group symlink
2174 * reference to se_device->dev_group.
2176 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
2180 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_ALUA
||
2181 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
2184 ret
= kstrtoul(page
, 0, &tmp
);
2186 pr_err("Unable to extract alua_tg_pt_offline value\n");
2189 if ((tmp
!= 0) && (tmp
!= 1)) {
2190 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2195 ret
= core_alua_set_tg_pt_secondary_state(lun
, 0, (int)tmp
);
2202 ssize_t
core_alua_show_secondary_status(
2206 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_stat
);
2209 ssize_t
core_alua_store_secondary_status(
2217 ret
= kstrtoul(page
, 0, &tmp
);
2219 pr_err("Unable to extract alua_tg_pt_status\n");
2222 if ((tmp
!= ALUA_STATUS_NONE
) &&
2223 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2224 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2225 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2229 lun
->lun_tg_pt_secondary_stat
= (int)tmp
;
2234 ssize_t
core_alua_show_secondary_write_metadata(
2238 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_write_md
);
2241 ssize_t
core_alua_store_secondary_write_metadata(
2249 ret
= kstrtoul(page
, 0, &tmp
);
2251 pr_err("Unable to extract alua_tg_pt_write_md\n");
2254 if ((tmp
!= 0) && (tmp
!= 1)) {
2255 pr_err("Illegal value for alua_tg_pt_write_md:"
2259 lun
->lun_tg_pt_secondary_write_md
= (int)tmp
;
2264 int core_setup_alua(struct se_device
*dev
)
2266 if (!(dev
->transport
->transport_flags
&
2267 TRANSPORT_FLAG_PASSTHROUGH_ALUA
) &&
2268 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2269 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2272 * Associate this struct se_device with the default ALUA
2275 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2276 if (IS_ERR(lu_gp_mem
))
2277 return PTR_ERR(lu_gp_mem
);
2279 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2280 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2282 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2284 pr_debug("%s: Adding to default ALUA LU Group:"
2285 " core/alua/lu_gps/default_lu_gp\n",
2286 dev
->transport
->name
);