1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/configfs.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/fcntl.h>
32 #include <linux/file.h>
34 #include <scsi/scsi_proto.h>
35 #include <asm/unaligned.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_backend.h>
39 #include <target/target_core_fabric.h>
41 #include "target_core_internal.h"
42 #include "target_core_alua.h"
43 #include "target_core_ua.h"
45 static sense_reason_t
core_alua_check_transition(int state
, int valid
,
47 static int core_alua_set_tg_pt_secondary_state(
48 struct se_lun
*lun
, int explicit, int offline
);
50 static char *core_alua_dump_state(int state
);
52 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
53 struct t10_alua_tg_pt_gp
*tg_pt_gp
);
55 static u16 alua_lu_gps_counter
;
56 static u32 alua_lu_gps_count
;
58 static DEFINE_SPINLOCK(lu_gps_lock
);
59 static LIST_HEAD(lu_gps_list
);
61 struct t10_alua_lu_gp
*default_lu_gp
;
66 * See sbc3r35 section 5.23
69 target_emulate_report_referrals(struct se_cmd
*cmd
)
71 struct se_device
*dev
= cmd
->se_dev
;
72 struct t10_alua_lba_map
*map
;
73 struct t10_alua_lba_map_member
*map_mem
;
77 if (cmd
->data_length
< 4) {
78 pr_warn("REPORT REFERRALS allocation length %u too"
79 " small\n", cmd
->data_length
);
80 return TCM_INVALID_CDB_FIELD
;
83 buf
= transport_kmap_data_sg(cmd
);
85 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
88 spin_lock(&dev
->t10_alua
.lba_map_lock
);
89 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
90 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
91 transport_kunmap_data_sg(cmd
);
93 return TCM_UNSUPPORTED_SCSI_OPCODE
;
96 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
98 int desc_num
= off
+ 3;
102 if (cmd
->data_length
> off
)
103 put_unaligned_be64(map
->lba_map_first_lba
, &buf
[off
]);
105 if (cmd
->data_length
> off
)
106 put_unaligned_be64(map
->lba_map_last_lba
, &buf
[off
]);
110 list_for_each_entry(map_mem
, &map
->lba_map_mem_list
,
112 int alua_state
= map_mem
->lba_map_mem_alua_state
;
113 int alua_pg_id
= map_mem
->lba_map_mem_alua_pg_id
;
115 if (cmd
->data_length
> off
)
116 buf
[off
] = alua_state
& 0x0f;
118 if (cmd
->data_length
> off
)
119 buf
[off
] = (alua_pg_id
>> 8) & 0xff;
121 if (cmd
->data_length
> off
)
122 buf
[off
] = (alua_pg_id
& 0xff);
127 if (cmd
->data_length
> desc_num
)
128 buf
[desc_num
] = pg_num
;
130 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
133 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
135 put_unaligned_be16(rd_len
, &buf
[2]);
137 transport_kunmap_data_sg(cmd
);
139 target_complete_cmd(cmd
, GOOD
);
144 * REPORT_TARGET_PORT_GROUPS
146 * See spc4r17 section 6.27
149 target_emulate_report_target_port_groups(struct se_cmd
*cmd
)
151 struct se_device
*dev
= cmd
->se_dev
;
152 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
156 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
159 * Skip over RESERVED area to first Target port group descriptor
160 * depending on the PARAMETER DATA FORMAT type..
167 if (cmd
->data_length
< off
) {
168 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
169 " small for %s header\n", cmd
->data_length
,
170 (ext_hdr
) ? "extended" : "normal");
171 return TCM_INVALID_CDB_FIELD
;
173 buf
= transport_kmap_data_sg(cmd
);
175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
177 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
178 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
181 * Check if the Target port group and Target port descriptor list
182 * based on tg_pt_gp_members count will fit into the response payload.
183 * Otherwise, bump rd_len to let the initiator know we have exceeded
184 * the allocation length and the response is truncated.
186 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
188 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
192 * PREF: Preferred target port bit, determine if this
193 * bit should be set for port group.
195 if (tg_pt_gp
->tg_pt_gp_pref
)
198 * Set the ASYMMETRIC ACCESS State
200 buf
[off
++] |= (atomic_read(
201 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
203 * Set supported ASYMMETRIC ACCESS State bits
205 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
209 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
210 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
212 off
++; /* Skip over Reserved */
216 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
218 * Vendor Specific field
224 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
227 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
228 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
231 * Start Target Port descriptor format
233 * See spc4r17 section 6.2.7 Table 247
235 off
+= 2; /* Skip over Obsolete */
237 * Set RELATIVE TARGET PORT IDENTIFIER
239 buf
[off
++] = ((lun
->lun_rtpi
>> 8) & 0xff);
240 buf
[off
++] = (lun
->lun_rtpi
& 0xff);
243 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
245 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
247 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
249 put_unaligned_be32(rd_len
, &buf
[0]);
252 * Fill in the Extended header parameter data format if requested
257 * Set the implicit transition time (in seconds) for the application
258 * client to use as a base for it's transition timeout value.
260 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
261 * this CDB was received upon to determine this value individually
262 * for ALUA target port group.
264 spin_lock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
265 tg_pt_gp
= cmd
->se_lun
->lun_tg_pt_gp
;
267 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
268 spin_unlock(&cmd
->se_lun
->lun_tg_pt_gp_lock
);
270 transport_kunmap_data_sg(cmd
);
272 target_complete_cmd(cmd
, GOOD
);
277 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
279 * See spc4r17 section 6.35
282 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
284 struct se_device
*dev
= cmd
->se_dev
;
285 struct se_lun
*l_lun
= cmd
->se_lun
;
286 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
287 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
290 sense_reason_t rc
= TCM_NO_SENSE
;
291 u32 len
= 4; /* Skip over RESERVED area in header */
292 int alua_access_state
, primary
= 0, valid_states
;
295 if (cmd
->data_length
< 4) {
296 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
297 " small\n", cmd
->data_length
);
298 return TCM_INVALID_PARAMETER_LIST
;
301 buf
= transport_kmap_data_sg(cmd
);
303 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
306 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
307 * for the local tg_pt_gp.
309 spin_lock(&l_lun
->lun_tg_pt_gp_lock
);
310 l_tg_pt_gp
= l_lun
->lun_tg_pt_gp
;
312 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
313 pr_err("Unable to access l_lun->tg_pt_gp\n");
314 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
318 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
319 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
320 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
321 " while TPGS_EXPLICIT_ALUA is disabled\n");
322 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
325 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
326 spin_unlock(&l_lun
->lun_tg_pt_gp_lock
);
328 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
330 while (len
< cmd
->data_length
) {
332 alua_access_state
= (ptr
[0] & 0x0f);
334 * Check the received ALUA access state, and determine if
335 * the state is a primary or secondary target port asymmetric
338 rc
= core_alua_check_transition(alua_access_state
,
339 valid_states
, &primary
);
342 * If the SET TARGET PORT GROUPS attempts to establish
343 * an invalid combination of target port asymmetric
344 * access states or attempts to establish an
345 * unsupported target port asymmetric access state,
346 * then the command shall be terminated with CHECK
347 * CONDITION status, with the sense key set to ILLEGAL
348 * REQUEST, and the additional sense code set to INVALID
349 * FIELD IN PARAMETER LIST.
355 * If the ASYMMETRIC ACCESS STATE field (see table 267)
356 * specifies a primary target port asymmetric access state,
357 * then the TARGET PORT GROUP OR TARGET PORT field specifies
358 * a primary target port group for which the primary target
359 * port asymmetric access state shall be changed. If the
360 * ASYMMETRIC ACCESS STATE field specifies a secondary target
361 * port asymmetric access state, then the TARGET PORT GROUP OR
362 * TARGET PORT field specifies the relative target port
363 * identifier (see 3.1.120) of the target port for which the
364 * secondary target port asymmetric access state shall be
368 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
370 * Locate the matching target port group ID from
371 * the global tg_pt_gp list
373 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
374 list_for_each_entry(tg_pt_gp
,
375 &dev
->t10_alua
.tg_pt_gps_list
,
377 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
380 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
383 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
385 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
387 if (!core_alua_do_port_transition(tg_pt_gp
,
389 alua_access_state
, 1))
392 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
393 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
396 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
401 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
402 * the Target Port in question for the the incoming
403 * SET_TARGET_PORT_GROUPS op.
405 rtpi
= get_unaligned_be16(ptr
+ 2);
407 * Locate the matching relative target port identifier
408 * for the struct se_device storage object.
410 spin_lock(&dev
->se_port_lock
);
411 list_for_each_entry(lun
, &dev
->dev_sep_list
,
413 if (lun
->lun_rtpi
!= rtpi
)
417 spin_unlock(&dev
->se_port_lock
);
419 if (!core_alua_set_tg_pt_secondary_state(
423 spin_lock(&dev
->se_port_lock
);
426 spin_unlock(&dev
->se_port_lock
);
430 rc
= TCM_INVALID_PARAMETER_LIST
;
439 transport_kunmap_data_sg(cmd
);
441 target_complete_cmd(cmd
, GOOD
);
445 static inline void set_ascq(struct se_cmd
*cmd
, u8 alua_ascq
)
448 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
449 * The ALUA additional sense code qualifier (ASCQ) is determined
450 * by the ALUA primary or secondary access state..
452 pr_debug("[%s]: ALUA TG Port not available, "
453 "SenseKey: NOT_READY, ASC/ASCQ: "
455 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
457 cmd
->scsi_asc
= 0x04;
458 cmd
->scsi_ascq
= alua_ascq
;
461 static inline void core_alua_state_nonoptimized(
464 int nonop_delay_msecs
)
467 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
468 * later to determine if processing of this cmd needs to be
469 * temporarily delayed for the Active/NonOptimized primary access state.
471 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
472 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
475 static inline int core_alua_state_lba_dependent(
477 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
479 struct se_device
*dev
= cmd
->se_dev
;
480 u64 segment_size
, segment_mult
, sectors
, lba
;
482 /* Only need to check for cdb actually containing LBAs */
483 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
486 spin_lock(&dev
->t10_alua
.lba_map_lock
);
487 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
488 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
489 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
491 lba
= cmd
->t_task_lba
;
492 while (lba
< cmd
->t_task_lba
+ sectors
) {
493 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
494 struct t10_alua_lba_map_member
*map_mem
;
496 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
498 u64 start_lba
, last_lba
;
499 u64 first_lba
= map
->lba_map_first_lba
;
503 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
505 last_lba
= first_lba
+ segment_size
- 1;
506 if (start_lba
>= first_lba
&&
507 start_lba
<= last_lba
) {
513 last_lba
= map
->lba_map_last_lba
;
514 if (lba
>= first_lba
&& lba
<= last_lba
) {
522 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
523 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
526 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
528 if (map_mem
->lba_map_mem_alua_pg_id
!=
529 tg_pt_gp
->tg_pt_gp_id
)
531 switch(map_mem
->lba_map_mem_alua_state
) {
532 case ALUA_ACCESS_STATE_STANDBY
:
533 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
534 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
536 case ALUA_ACCESS_STATE_UNAVAILABLE
:
537 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
538 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
545 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
549 static inline int core_alua_state_standby(
554 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
555 * spc4r17 section 5.9.2.4.4
564 case RECEIVE_DIAGNOSTIC
:
565 case SEND_DIAGNOSTIC
:
568 case SERVICE_ACTION_IN_16
:
569 switch (cdb
[1] & 0x1f) {
570 case SAI_READ_CAPACITY_16
:
573 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
577 switch (cdb
[1] & 0x1f) {
578 case MI_REPORT_TARGET_PGS
:
581 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
584 case MAINTENANCE_OUT
:
586 case MO_SET_TARGET_PGS
:
589 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
593 case PERSISTENT_RESERVE_IN
:
594 case PERSISTENT_RESERVE_OUT
:
599 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
606 static inline int core_alua_state_unavailable(
611 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
612 * spc4r17 section 5.9.2.4.5
619 switch (cdb
[1] & 0x1f) {
620 case MI_REPORT_TARGET_PGS
:
623 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
626 case MAINTENANCE_OUT
:
628 case MO_SET_TARGET_PGS
:
631 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
639 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
646 static inline int core_alua_state_transition(
651 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
652 * spc4r17 section 5.9.2.5
659 switch (cdb
[1] & 0x1f) {
660 case MI_REPORT_TARGET_PGS
:
663 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
671 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
679 * return 1: Is used to signal LUN not accessible, and check condition/not ready
680 * return 0: Used to signal success
681 * return -1: Used to signal failure, and invalid cdb field
684 target_alua_state_check(struct se_cmd
*cmd
)
686 struct se_device
*dev
= cmd
->se_dev
;
687 unsigned char *cdb
= cmd
->t_task_cdb
;
688 struct se_lun
*lun
= cmd
->se_lun
;
689 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
690 int out_alua_state
, nonop_delay_msecs
;
692 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
694 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
698 * First, check for a struct se_port specific secondary ALUA target port
699 * access state: OFFLINE
701 if (atomic_read(&lun
->lun_tg_pt_secondary_offline
)) {
702 pr_debug("ALUA: Got secondary offline status for local"
704 set_ascq(cmd
, ASCQ_04H_ALUA_OFFLINE
);
705 return TCM_CHECK_CONDITION_NOT_READY
;
708 if (!lun
->lun_tg_pt_gp
)
711 spin_lock(&lun
->lun_tg_pt_gp_lock
);
712 tg_pt_gp
= lun
->lun_tg_pt_gp
;
713 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
714 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
716 // XXX: keeps using tg_pt_gp witout reference after unlock
717 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
719 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
720 * statement so the compiler knows explicitly to check this case first.
721 * For the Optimized ALUA access state case, we want to process the
722 * incoming fabric cmd ASAP..
724 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
727 switch (out_alua_state
) {
728 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
729 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
731 case ALUA_ACCESS_STATE_STANDBY
:
732 if (core_alua_state_standby(cmd
, cdb
))
733 return TCM_CHECK_CONDITION_NOT_READY
;
735 case ALUA_ACCESS_STATE_UNAVAILABLE
:
736 if (core_alua_state_unavailable(cmd
, cdb
))
737 return TCM_CHECK_CONDITION_NOT_READY
;
739 case ALUA_ACCESS_STATE_TRANSITION
:
740 if (core_alua_state_transition(cmd
, cdb
))
741 return TCM_CHECK_CONDITION_NOT_READY
;
743 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
744 if (core_alua_state_lba_dependent(cmd
, tg_pt_gp
))
745 return TCM_CHECK_CONDITION_NOT_READY
;
748 * OFFLINE is a secondary ALUA target port group access state, that is
749 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
751 case ALUA_ACCESS_STATE_OFFLINE
:
753 pr_err("Unknown ALUA access state: 0x%02x\n",
755 return TCM_INVALID_CDB_FIELD
;
762 * Check implicit and explicit ALUA state change request.
764 static sense_reason_t
765 core_alua_check_transition(int state
, int valid
, int *primary
)
768 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
769 * defined as primary target port asymmetric access states.
772 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
773 if (!(valid
& ALUA_AO_SUP
))
777 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
778 if (!(valid
& ALUA_AN_SUP
))
782 case ALUA_ACCESS_STATE_STANDBY
:
783 if (!(valid
& ALUA_S_SUP
))
787 case ALUA_ACCESS_STATE_UNAVAILABLE
:
788 if (!(valid
& ALUA_U_SUP
))
792 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
793 if (!(valid
& ALUA_LBD_SUP
))
797 case ALUA_ACCESS_STATE_OFFLINE
:
799 * OFFLINE state is defined as a secondary target port
800 * asymmetric access state.
802 if (!(valid
& ALUA_O_SUP
))
806 case ALUA_ACCESS_STATE_TRANSITION
:
808 * Transitioning is set internally, and
809 * cannot be selected manually.
813 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
814 return TCM_INVALID_PARAMETER_LIST
;
820 pr_err("ALUA access state %s not supported",
821 core_alua_dump_state(state
));
822 return TCM_INVALID_PARAMETER_LIST
;
825 static char *core_alua_dump_state(int state
)
828 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
829 return "Active/Optimized";
830 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
831 return "Active/NonOptimized";
832 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
833 return "LBA Dependent";
834 case ALUA_ACCESS_STATE_STANDBY
:
836 case ALUA_ACCESS_STATE_UNAVAILABLE
:
837 return "Unavailable";
838 case ALUA_ACCESS_STATE_OFFLINE
:
840 case ALUA_ACCESS_STATE_TRANSITION
:
841 return "Transitioning";
849 char *core_alua_dump_status(int status
)
852 case ALUA_STATUS_NONE
:
854 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
855 return "Altered by Explicit STPG";
856 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
857 return "Altered by Implicit ALUA";
866 * Used by fabric modules to determine when we need to delay processing
867 * for the Active/NonOptimized paths..
869 int core_alua_check_nonop_delay(
872 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
877 * The ALUA Active/NonOptimized access state delay can be disabled
878 * in via configfs with a value of zero
880 if (!cmd
->alua_nonop_delay
)
883 * struct se_cmd->alua_nonop_delay gets set by a target port group
884 * defined interval in core_alua_state_nonoptimized()
886 msleep_interruptible(cmd
->alua_nonop_delay
);
889 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
891 static int core_alua_write_tpg_metadata(
893 unsigned char *md_buf
,
896 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
900 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
903 ret
= kernel_write(file
, md_buf
, md_buf_len
, 0);
905 pr_err("Error writing ALUA metadata file: %s\n", path
);
907 return (ret
< 0) ? -EIO
: 0;
911 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
913 static int core_alua_update_tpg_primary_metadata(
914 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
916 unsigned char *md_buf
;
917 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
918 char path
[ALUA_METADATA_PATH_LEN
];
921 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
923 pr_err("Unable to allocate buf for ALUA metadata\n");
927 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
929 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
931 "alua_access_state=0x%02x\n"
932 "alua_access_status=0x%02x\n",
933 tg_pt_gp
->tg_pt_gp_id
,
934 tg_pt_gp
->tg_pt_gp_alua_pending_state
,
935 tg_pt_gp
->tg_pt_gp_alua_access_status
);
937 snprintf(path
, ALUA_METADATA_PATH_LEN
,
938 "%s/alua/tpgs_%s/%s", db_root
, &wwn
->unit_serial
[0],
939 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
941 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
946 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp
*tg_pt_gp
)
948 struct se_dev_entry
*se_deve
;
950 struct se_lun_acl
*lacl
;
952 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
953 list_for_each_entry(lun
, &tg_pt_gp
->tg_pt_gp_lun_list
,
956 * After an implicit target port asymmetric access state
957 * change, a device server shall establish a unit attention
958 * condition for the initiator port associated with every I_T
959 * nexus with the additional sense code set to ASYMMETRIC
960 * ACCESS STATE CHANGED.
962 * After an explicit target port asymmetric access state
963 * change, a device server shall establish a unit attention
964 * condition with the additional sense code set to ASYMMETRIC
965 * ACCESS STATE CHANGED for the initiator port associated with
966 * every I_T nexus other than the I_T nexus on which the SET
967 * TARGET PORT GROUPS command
969 if (!percpu_ref_tryget_live(&lun
->lun_ref
))
971 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
973 spin_lock(&lun
->lun_deve_lock
);
974 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
) {
975 lacl
= rcu_dereference_check(se_deve
->se_lun_acl
,
976 lockdep_is_held(&lun
->lun_deve_lock
));
980 * After an explicit target port asymmetric access
981 * state change, a device server shall establish a
982 * unit attention condition with the additional sense
983 * code set to ASYMMETRIC ACCESS STATE CHANGED for
984 * the initiator port associated with every I_T nexus
985 * other than the I_T nexus on which the SET TARGET
986 * PORT GROUPS command was received.
988 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
989 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
990 (tg_pt_gp
->tg_pt_gp_alua_lun
!= NULL
) &&
991 (tg_pt_gp
->tg_pt_gp_alua_lun
== lun
))
995 * se_deve->se_lun_acl pointer may be NULL for a
996 * entry created without explicit Node+MappedLUN ACLs
998 if (lacl
&& (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
999 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
))
1002 core_scsi3_ua_allocate(se_deve
, 0x2A,
1003 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
1005 spin_unlock(&lun
->lun_deve_lock
);
1007 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1008 percpu_ref_put(&lun
->lun_ref
);
1010 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1013 static void core_alua_do_transition_tg_pt_work(struct work_struct
*work
)
1015 struct t10_alua_tg_pt_gp
*tg_pt_gp
= container_of(work
,
1016 struct t10_alua_tg_pt_gp
, tg_pt_gp_transition_work
.work
);
1017 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1018 bool explicit = (tg_pt_gp
->tg_pt_gp_alua_access_status
==
1019 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
);
1022 * Update the ALUA metadata buf that has been allocated in
1023 * core_alua_do_port_transition(), this metadata will be written
1026 * Note that there is the case where we do not want to update the
1027 * metadata when the saved metadata is being parsed in userspace
1028 * when setting the existing port access state and access status.
1030 * Also note that the failure to write out the ALUA metadata to
1031 * struct file does NOT affect the actual ALUA transition.
1033 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1034 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1035 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1036 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1039 * Set the current primary ALUA access state to the requested new state
1041 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1042 tg_pt_gp
->tg_pt_gp_alua_pending_state
);
1044 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1045 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1046 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1047 tg_pt_gp
->tg_pt_gp_id
,
1048 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_previous_state
),
1049 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_pending_state
));
1051 core_alua_queue_state_change_ua(tg_pt_gp
);
1053 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1054 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1055 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1057 if (tg_pt_gp
->tg_pt_gp_transition_complete
)
1058 complete(tg_pt_gp
->tg_pt_gp_transition_complete
);
1061 static int core_alua_do_transition_tg_pt(
1062 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1066 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1067 DECLARE_COMPLETION_ONSTACK(wait
);
1069 /* Nothing to be done here */
1070 if (atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) == new_state
)
1073 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
)
1077 * Flush any pending transitions
1079 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
&&
1080 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) ==
1081 ALUA_ACCESS_STATE_TRANSITION
) {
1083 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1084 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1085 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1086 wait_for_completion(&wait
);
1087 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1092 * Save the old primary ALUA access state, and set the current state
1093 * to ALUA_ACCESS_STATE_TRANSITION.
1095 tg_pt_gp
->tg_pt_gp_alua_previous_state
=
1096 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
1097 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1099 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1100 ALUA_ACCESS_STATE_TRANSITION
);
1101 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1102 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1103 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1105 core_alua_queue_state_change_ua(tg_pt_gp
);
1108 * Check for the optional ALUA primary state transition delay
1110 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1111 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1114 * Take a reference for workqueue item
1116 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1117 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1118 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1120 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
) {
1121 unsigned long transition_tmo
;
1123 transition_tmo
= tg_pt_gp
->tg_pt_gp_implicit_trans_secs
* HZ
;
1124 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1125 &tg_pt_gp
->tg_pt_gp_transition_work
,
1128 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1129 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1130 &tg_pt_gp
->tg_pt_gp_transition_work
, 0);
1131 wait_for_completion(&wait
);
1132 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1138 int core_alua_do_port_transition(
1139 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1140 struct se_device
*l_dev
,
1141 struct se_lun
*l_lun
,
1142 struct se_node_acl
*l_nacl
,
1146 struct se_device
*dev
;
1147 struct t10_alua_lu_gp
*lu_gp
;
1148 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1149 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1150 int primary
, valid_states
, rc
= 0;
1152 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1153 if (core_alua_check_transition(new_state
, valid_states
, &primary
) != 0)
1156 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1157 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1158 lu_gp
= local_lu_gp_mem
->lu_gp
;
1159 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1160 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1162 * For storage objects that are members of the 'default_lu_gp',
1163 * we only do transition on the passed *l_tp_pt_gp, and not
1164 * on all of the matching target port groups IDs in default_lu_gp.
1166 if (!lu_gp
->lu_gp_id
) {
1168 * core_alua_do_transition_tg_pt() will always return
1171 l_tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1172 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1173 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1174 new_state
, explicit);
1175 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1179 * For all other LU groups aside from 'default_lu_gp', walk all of
1180 * the associated storage objects looking for a matching target port
1181 * group ID from the local target port group.
1183 spin_lock(&lu_gp
->lu_gp_lock
);
1184 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1187 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1188 atomic_inc_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1189 spin_unlock(&lu_gp
->lu_gp_lock
);
1191 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1192 list_for_each_entry(tg_pt_gp
,
1193 &dev
->t10_alua
.tg_pt_gps_list
,
1196 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1199 * If the target behavior port asymmetric access state
1200 * is changed for any target port group accessible via
1201 * a logical unit within a LU group, the target port
1202 * behavior group asymmetric access states for the same
1203 * target port group accessible via other logical units
1204 * in that LU group will also change.
1206 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1209 if (l_tg_pt_gp
== tg_pt_gp
) {
1210 tg_pt_gp
->tg_pt_gp_alua_lun
= l_lun
;
1211 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1213 tg_pt_gp
->tg_pt_gp_alua_lun
= NULL
;
1214 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1216 atomic_inc_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1217 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1219 * core_alua_do_transition_tg_pt() will always return
1222 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1223 new_state
, explicit);
1225 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1226 atomic_dec_mb(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1230 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1232 spin_lock(&lu_gp
->lu_gp_lock
);
1233 atomic_dec_mb(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1235 spin_unlock(&lu_gp
->lu_gp_lock
);
1238 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1239 " Group IDs: %hu %s transition to primary state: %s\n",
1240 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1241 l_tg_pt_gp
->tg_pt_gp_id
,
1242 (explicit) ? "explicit" : "implicit",
1243 core_alua_dump_state(new_state
));
1246 atomic_dec_mb(&lu_gp
->lu_gp_ref_cnt
);
1250 static int core_alua_update_tpg_secondary_metadata(struct se_lun
*lun
)
1252 struct se_portal_group
*se_tpg
= lun
->lun_tpg
;
1253 unsigned char *md_buf
;
1254 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1257 mutex_lock(&lun
->lun_tg_pt_md_mutex
);
1259 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1261 pr_err("Unable to allocate buf for ALUA metadata\n");
1266 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1267 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1269 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1270 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1272 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1273 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1274 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1276 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1277 "alua_tg_pt_status=0x%02x\n",
1278 atomic_read(&lun
->lun_tg_pt_secondary_offline
),
1279 lun
->lun_tg_pt_secondary_stat
);
1281 snprintf(path
, ALUA_METADATA_PATH_LEN
, "%s/alua/%s/%s/lun_%llu",
1282 db_root
, se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1285 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1289 mutex_unlock(&lun
->lun_tg_pt_md_mutex
);
1293 static int core_alua_set_tg_pt_secondary_state(
1298 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1299 int trans_delay_msecs
;
1301 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1302 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1304 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1305 pr_err("Unable to complete secondary state"
1309 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1311 * Set the secondary ALUA target port access state to OFFLINE
1312 * or release the previously secondary state for struct se_lun
1315 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 1);
1317 atomic_set(&lun
->lun_tg_pt_secondary_offline
, 0);
1319 lun
->lun_tg_pt_secondary_stat
= (explicit) ?
1320 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1321 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1323 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1324 " to secondary access state: %s\n", (explicit) ? "explicit" :
1325 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1326 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1328 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1330 * Do the optional transition delay after we set the secondary
1331 * ALUA access state.
1333 if (trans_delay_msecs
!= 0)
1334 msleep_interruptible(trans_delay_msecs
);
1336 * See if we need to update the ALUA fabric port metadata for
1337 * secondary state and status
1339 if (lun
->lun_tg_pt_secondary_write_md
)
1340 core_alua_update_tpg_secondary_metadata(lun
);
1345 struct t10_alua_lba_map
*
1346 core_alua_allocate_lba_map(struct list_head
*list
,
1347 u64 first_lba
, u64 last_lba
)
1349 struct t10_alua_lba_map
*lba_map
;
1351 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1353 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1354 return ERR_PTR(-ENOMEM
);
1356 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1357 lba_map
->lba_map_first_lba
= first_lba
;
1358 lba_map
->lba_map_last_lba
= last_lba
;
1360 list_add_tail(&lba_map
->lba_map_list
, list
);
1365 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1366 int pg_id
, int state
)
1368 struct t10_alua_lba_map_member
*lba_map_mem
;
1370 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1372 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1373 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1378 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1380 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1383 lba_map_mem
->lba_map_mem_alua_state
= state
;
1384 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1386 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1387 &lba_map
->lba_map_mem_list
);
1392 core_alua_free_lba_map(struct list_head
*lba_list
)
1394 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1395 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1397 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1399 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1400 &lba_map
->lba_map_mem_list
,
1402 list_del(&lba_map_mem
->lba_map_mem_list
);
1403 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1406 list_del(&lba_map
->lba_map_list
);
1407 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1412 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1413 int segment_size
, int segment_mult
)
1415 struct list_head old_lba_map_list
;
1416 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1417 int activate
= 0, supported
;
1419 INIT_LIST_HEAD(&old_lba_map_list
);
1420 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1421 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1422 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1423 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1425 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1428 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1429 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1430 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1433 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1435 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1437 supported
|= ALUA_LBD_SUP
;
1439 supported
&= ~ALUA_LBD_SUP
;
1440 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1442 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1443 core_alua_free_lba_map(&old_lba_map_list
);
1446 struct t10_alua_lu_gp
*
1447 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1449 struct t10_alua_lu_gp
*lu_gp
;
1451 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1453 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1454 return ERR_PTR(-ENOMEM
);
1456 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1457 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1458 spin_lock_init(&lu_gp
->lu_gp_lock
);
1459 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1462 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1463 lu_gp
->lu_gp_valid_id
= 1;
1464 alua_lu_gps_count
++;
1470 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1472 struct t10_alua_lu_gp
*lu_gp_tmp
;
1475 * The lu_gp->lu_gp_id may only be set once..
1477 if (lu_gp
->lu_gp_valid_id
) {
1478 pr_warn("ALUA LU Group already has a valid ID,"
1479 " ignoring request\n");
1483 spin_lock(&lu_gps_lock
);
1484 if (alua_lu_gps_count
== 0x0000ffff) {
1485 pr_err("Maximum ALUA alua_lu_gps_count:"
1486 " 0x0000ffff reached\n");
1487 spin_unlock(&lu_gps_lock
);
1488 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1492 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1493 alua_lu_gps_counter
++;
1495 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1496 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1500 pr_warn("ALUA Logical Unit Group ID: %hu"
1501 " already exists, ignoring request\n",
1503 spin_unlock(&lu_gps_lock
);
1508 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1509 lu_gp
->lu_gp_valid_id
= 1;
1510 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1511 alua_lu_gps_count
++;
1512 spin_unlock(&lu_gps_lock
);
1517 static struct t10_alua_lu_gp_member
*
1518 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1520 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1522 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1524 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1525 return ERR_PTR(-ENOMEM
);
1527 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1528 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1529 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1531 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1532 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1537 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1539 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1541 * Once we have reached this point, config_item_put() has
1542 * already been called from target_core_alua_drop_lu_gp().
1544 * Here, we remove the *lu_gp from the global list so that
1545 * no associations can be made while we are releasing
1546 * struct t10_alua_lu_gp.
1548 spin_lock(&lu_gps_lock
);
1549 list_del(&lu_gp
->lu_gp_node
);
1550 alua_lu_gps_count
--;
1551 spin_unlock(&lu_gps_lock
);
1553 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1554 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1555 * released with core_alua_put_lu_gp_from_name()
1557 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1560 * Release reference to struct t10_alua_lu_gp * from all associated
1563 spin_lock(&lu_gp
->lu_gp_lock
);
1564 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1565 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1566 if (lu_gp_mem
->lu_gp_assoc
) {
1567 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1568 lu_gp
->lu_gp_members
--;
1569 lu_gp_mem
->lu_gp_assoc
= 0;
1571 spin_unlock(&lu_gp
->lu_gp_lock
);
1574 * lu_gp_mem is associated with a single
1575 * struct se_device->dev_alua_lu_gp_mem, and is released when
1576 * struct se_device is released via core_alua_free_lu_gp_mem().
1578 * If the passed lu_gp does NOT match the default_lu_gp, assume
1579 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1581 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1582 if (lu_gp
!= default_lu_gp
)
1583 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1586 lu_gp_mem
->lu_gp
= NULL
;
1587 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1589 spin_lock(&lu_gp
->lu_gp_lock
);
1591 spin_unlock(&lu_gp
->lu_gp_lock
);
1593 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1596 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1598 struct t10_alua_lu_gp
*lu_gp
;
1599 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1601 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1605 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1608 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1609 lu_gp
= lu_gp_mem
->lu_gp
;
1611 spin_lock(&lu_gp
->lu_gp_lock
);
1612 if (lu_gp_mem
->lu_gp_assoc
) {
1613 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1614 lu_gp
->lu_gp_members
--;
1615 lu_gp_mem
->lu_gp_assoc
= 0;
1617 spin_unlock(&lu_gp
->lu_gp_lock
);
1618 lu_gp_mem
->lu_gp
= NULL
;
1620 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1622 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1625 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1627 struct t10_alua_lu_gp
*lu_gp
;
1628 struct config_item
*ci
;
1630 spin_lock(&lu_gps_lock
);
1631 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1632 if (!lu_gp
->lu_gp_valid_id
)
1634 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1635 if (!strcmp(config_item_name(ci
), name
)) {
1636 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1637 spin_unlock(&lu_gps_lock
);
1641 spin_unlock(&lu_gps_lock
);
1646 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1648 spin_lock(&lu_gps_lock
);
1649 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1650 spin_unlock(&lu_gps_lock
);
1654 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1656 void __core_alua_attach_lu_gp_mem(
1657 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1658 struct t10_alua_lu_gp
*lu_gp
)
1660 spin_lock(&lu_gp
->lu_gp_lock
);
1661 lu_gp_mem
->lu_gp
= lu_gp
;
1662 lu_gp_mem
->lu_gp_assoc
= 1;
1663 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1664 lu_gp
->lu_gp_members
++;
1665 spin_unlock(&lu_gp
->lu_gp_lock
);
1669 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1671 void __core_alua_drop_lu_gp_mem(
1672 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1673 struct t10_alua_lu_gp
*lu_gp
)
1675 spin_lock(&lu_gp
->lu_gp_lock
);
1676 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1677 lu_gp_mem
->lu_gp
= NULL
;
1678 lu_gp_mem
->lu_gp_assoc
= 0;
1679 lu_gp
->lu_gp_members
--;
1680 spin_unlock(&lu_gp
->lu_gp_lock
);
1683 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1684 const char *name
, int def_group
)
1686 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1688 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1690 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1693 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1694 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_lun_list
);
1695 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1696 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1697 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1698 INIT_DELAYED_WORK(&tg_pt_gp
->tg_pt_gp_transition_work
,
1699 core_alua_do_transition_tg_pt_work
);
1700 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1701 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1702 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
);
1704 * Enable both explicit and implicit ALUA support by default
1706 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1707 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1709 * Set the default Active/NonOptimized Delay in milliseconds
1711 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1712 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1713 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1716 * Enable all supported states
1718 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1719 ALUA_T_SUP
| ALUA_O_SUP
|
1720 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1723 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1724 tg_pt_gp
->tg_pt_gp_id
=
1725 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1726 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1727 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1728 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1729 &dev
->t10_alua
.tg_pt_gps_list
);
1730 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1736 int core_alua_set_tg_pt_gp_id(
1737 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1740 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1741 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1742 u16 tg_pt_gp_id_tmp
;
1745 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1747 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1748 pr_warn("ALUA TG PT Group already has a valid ID,"
1749 " ignoring request\n");
1753 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1754 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1755 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1756 " 0x0000ffff reached\n");
1757 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1758 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1762 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1763 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1765 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1767 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1771 pr_err("ALUA Target Port Group ID: %hu already"
1772 " exists, ignoring request\n", tg_pt_gp_id
);
1773 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1778 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1779 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1780 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1781 &dev
->t10_alua
.tg_pt_gps_list
);
1782 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1783 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1788 void core_alua_free_tg_pt_gp(
1789 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1791 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1792 struct se_lun
*lun
, *next
;
1795 * Once we have reached this point, config_item_put() has already
1796 * been called from target_core_alua_drop_tg_pt_gp().
1798 * Here we remove *tg_pt_gp from the global list so that
1799 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1800 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1802 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1803 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1804 dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1805 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1807 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1810 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1811 * core_alua_get_tg_pt_gp_by_name() in
1812 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1813 * to be released with core_alua_put_tg_pt_gp_from_name().
1815 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1819 * Release reference to struct t10_alua_tg_pt_gp from all associated
1822 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1823 list_for_each_entry_safe(lun
, next
,
1824 &tg_pt_gp
->tg_pt_gp_lun_list
, lun_tg_pt_gp_link
) {
1825 list_del_init(&lun
->lun_tg_pt_gp_link
);
1826 tg_pt_gp
->tg_pt_gp_members
--;
1828 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1830 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1831 * assume we want to re-associate a given tg_pt_gp_mem with
1834 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1835 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1836 __target_attach_tg_pt_gp(lun
,
1837 dev
->t10_alua
.default_tg_pt_gp
);
1839 lun
->lun_tg_pt_gp
= NULL
;
1840 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1842 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1844 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1846 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1849 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1850 struct se_device
*dev
, const char *name
)
1852 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1853 struct config_item
*ci
;
1855 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1856 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1858 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1860 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1861 if (!strcmp(config_item_name(ci
), name
)) {
1862 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1863 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1867 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1872 static void core_alua_put_tg_pt_gp_from_name(
1873 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1875 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1877 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1878 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1879 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1882 static void __target_attach_tg_pt_gp(struct se_lun
*lun
,
1883 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1885 struct se_dev_entry
*se_deve
;
1887 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1889 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1890 lun
->lun_tg_pt_gp
= tg_pt_gp
;
1891 list_add_tail(&lun
->lun_tg_pt_gp_link
, &tg_pt_gp
->tg_pt_gp_lun_list
);
1892 tg_pt_gp
->tg_pt_gp_members
++;
1893 spin_lock(&lun
->lun_deve_lock
);
1894 list_for_each_entry(se_deve
, &lun
->lun_deve_list
, lun_link
)
1895 core_scsi3_ua_allocate(se_deve
, 0x3f,
1896 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED
);
1897 spin_unlock(&lun
->lun_deve_lock
);
1898 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1901 void target_attach_tg_pt_gp(struct se_lun
*lun
,
1902 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1904 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1905 __target_attach_tg_pt_gp(lun
, tg_pt_gp
);
1906 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1909 static void __target_detach_tg_pt_gp(struct se_lun
*lun
,
1910 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1912 assert_spin_locked(&lun
->lun_tg_pt_gp_lock
);
1914 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1915 list_del_init(&lun
->lun_tg_pt_gp_link
);
1916 tg_pt_gp
->tg_pt_gp_members
--;
1917 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1919 lun
->lun_tg_pt_gp
= NULL
;
1922 void target_detach_tg_pt_gp(struct se_lun
*lun
)
1924 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1926 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1927 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1929 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
1930 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1933 ssize_t
core_alua_show_tg_pt_gp_info(struct se_lun
*lun
, char *page
)
1935 struct config_item
*tg_pt_ci
;
1936 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1939 spin_lock(&lun
->lun_tg_pt_gp_lock
);
1940 tg_pt_gp
= lun
->lun_tg_pt_gp
;
1942 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1943 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
1944 " %hu\nTG Port Primary Access State: %s\nTG Port "
1945 "Primary Access Status: %s\nTG Port Secondary Access"
1946 " State: %s\nTG Port Secondary Access Status: %s\n",
1947 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
1948 core_alua_dump_state(atomic_read(
1949 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
1950 core_alua_dump_status(
1951 tg_pt_gp
->tg_pt_gp_alua_access_status
),
1952 atomic_read(&lun
->lun_tg_pt_secondary_offline
) ?
1954 core_alua_dump_status(lun
->lun_tg_pt_secondary_stat
));
1956 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
1961 ssize_t
core_alua_store_tg_pt_gp_info(
1966 struct se_portal_group
*tpg
= lun
->lun_tpg
;
1968 * rcu_dereference_raw protected by se_lun->lun_group symlink
1969 * reference to se_device->dev_group.
1971 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
1972 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
1973 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
1976 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
||
1977 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
1980 if (count
> TG_PT_GROUP_NAME_BUF
) {
1981 pr_err("ALUA Target Port Group alias too large!\n");
1984 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
1985 memcpy(buf
, page
, count
);
1987 * Any ALUA target port group alias besides "NULL" means we will be
1988 * making a new group association.
1990 if (strcmp(strstrip(buf
), "NULL")) {
1992 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1993 * struct t10_alua_tg_pt_gp. This reference is released with
1994 * core_alua_put_tg_pt_gp_from_name() below.
1996 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
2002 spin_lock(&lun
->lun_tg_pt_gp_lock
);
2003 tg_pt_gp
= lun
->lun_tg_pt_gp
;
2006 * Clearing an existing tg_pt_gp association, and replacing
2007 * with the default_tg_pt_gp.
2009 if (!tg_pt_gp_new
) {
2010 pr_debug("Target_Core_ConfigFS: Moving"
2011 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
2012 " alua/%s, ID: %hu back to"
2013 " default_tg_pt_gp\n",
2014 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2015 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2016 config_item_name(&lun
->lun_group
.cg_item
),
2018 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
2019 tg_pt_gp
->tg_pt_gp_id
);
2021 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
2022 __target_attach_tg_pt_gp(lun
,
2023 dev
->t10_alua
.default_tg_pt_gp
);
2024 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
2028 __target_detach_tg_pt_gp(lun
, tg_pt_gp
);
2032 __target_attach_tg_pt_gp(lun
, tg_pt_gp_new
);
2033 spin_unlock(&lun
->lun_tg_pt_gp_lock
);
2034 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2035 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
2036 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2037 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2038 config_item_name(&lun
->lun_group
.cg_item
),
2039 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
2040 tg_pt_gp_new
->tg_pt_gp_id
);
2042 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
2046 ssize_t
core_alua_show_access_type(
2047 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2050 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
2051 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
2052 return sprintf(page
, "Implicit and Explicit\n");
2053 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
2054 return sprintf(page
, "Implicit\n");
2055 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
2056 return sprintf(page
, "Explicit\n");
2058 return sprintf(page
, "None\n");
2061 ssize_t
core_alua_store_access_type(
2062 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2069 ret
= kstrtoul(page
, 0, &tmp
);
2071 pr_err("Unable to extract alua_access_type\n");
2074 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2075 pr_err("Illegal value for alua_access_type:"
2080 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2081 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2083 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2085 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2087 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2092 ssize_t
core_alua_show_nonop_delay_msecs(
2093 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2096 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2099 ssize_t
core_alua_store_nonop_delay_msecs(
2100 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2107 ret
= kstrtoul(page
, 0, &tmp
);
2109 pr_err("Unable to extract nonop_delay_msecs\n");
2112 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2113 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2114 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2115 ALUA_MAX_NONOP_DELAY_MSECS
);
2118 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2123 ssize_t
core_alua_show_trans_delay_msecs(
2124 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2127 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2130 ssize_t
core_alua_store_trans_delay_msecs(
2131 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2138 ret
= kstrtoul(page
, 0, &tmp
);
2140 pr_err("Unable to extract trans_delay_msecs\n");
2143 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2144 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2145 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2146 ALUA_MAX_TRANS_DELAY_MSECS
);
2149 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2154 ssize_t
core_alua_show_implicit_trans_secs(
2155 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2158 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2161 ssize_t
core_alua_store_implicit_trans_secs(
2162 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2169 ret
= kstrtoul(page
, 0, &tmp
);
2171 pr_err("Unable to extract implicit_trans_secs\n");
2174 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2175 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2176 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2177 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2180 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2185 ssize_t
core_alua_show_preferred_bit(
2186 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2189 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2192 ssize_t
core_alua_store_preferred_bit(
2193 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2200 ret
= kstrtoul(page
, 0, &tmp
);
2202 pr_err("Unable to extract preferred ALUA value\n");
2205 if ((tmp
!= 0) && (tmp
!= 1)) {
2206 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2209 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2214 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2216 return sprintf(page
, "%d\n",
2217 atomic_read(&lun
->lun_tg_pt_secondary_offline
));
2220 ssize_t
core_alua_store_offline_bit(
2226 * rcu_dereference_raw protected by se_lun->lun_group symlink
2227 * reference to se_device->dev_group.
2229 struct se_device
*dev
= rcu_dereference_raw(lun
->lun_se_dev
);
2233 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
||
2234 (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
))
2237 ret
= kstrtoul(page
, 0, &tmp
);
2239 pr_err("Unable to extract alua_tg_pt_offline value\n");
2242 if ((tmp
!= 0) && (tmp
!= 1)) {
2243 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2248 ret
= core_alua_set_tg_pt_secondary_state(lun
, 0, (int)tmp
);
2255 ssize_t
core_alua_show_secondary_status(
2259 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_stat
);
2262 ssize_t
core_alua_store_secondary_status(
2270 ret
= kstrtoul(page
, 0, &tmp
);
2272 pr_err("Unable to extract alua_tg_pt_status\n");
2275 if ((tmp
!= ALUA_STATUS_NONE
) &&
2276 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2277 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2278 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2282 lun
->lun_tg_pt_secondary_stat
= (int)tmp
;
2287 ssize_t
core_alua_show_secondary_write_metadata(
2291 return sprintf(page
, "%d\n", lun
->lun_tg_pt_secondary_write_md
);
2294 ssize_t
core_alua_store_secondary_write_metadata(
2302 ret
= kstrtoul(page
, 0, &tmp
);
2304 pr_err("Unable to extract alua_tg_pt_write_md\n");
2307 if ((tmp
!= 0) && (tmp
!= 1)) {
2308 pr_err("Illegal value for alua_tg_pt_write_md:"
2312 lun
->lun_tg_pt_secondary_write_md
= (int)tmp
;
2317 int core_setup_alua(struct se_device
*dev
)
2319 if (!(dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
) &&
2320 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2321 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2324 * Associate this struct se_device with the default ALUA
2327 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2328 if (IS_ERR(lu_gp_mem
))
2329 return PTR_ERR(lu_gp_mem
);
2331 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2332 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2334 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2336 pr_debug("%s: Adding to default ALUA LU Group:"
2337 " core/alua/lu_gps/default_lu_gp\n",
2338 dev
->transport
->name
);