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/export.h>
30 #include <linux/file.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_alua.h"
42 #include "target_core_ua.h"
44 static sense_reason_t
core_alua_check_transition(int state
, int valid
,
46 static int core_alua_set_tg_pt_secondary_state(
47 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
48 struct se_port
*port
, int explicit, int offline
);
50 static char *core_alua_dump_state(int state
);
52 static u16 alua_lu_gps_counter
;
53 static u32 alua_lu_gps_count
;
55 static DEFINE_SPINLOCK(lu_gps_lock
);
56 static LIST_HEAD(lu_gps_list
);
58 struct t10_alua_lu_gp
*default_lu_gp
;
63 * See sbc3r35 section 5.23
66 target_emulate_report_referrals(struct se_cmd
*cmd
)
68 struct se_device
*dev
= cmd
->se_dev
;
69 struct t10_alua_lba_map
*map
;
70 struct t10_alua_lba_map_member
*map_mem
;
74 if (cmd
->data_length
< 4) {
75 pr_warn("REPORT REFERRALS allocation length %u too"
76 " small\n", cmd
->data_length
);
77 return TCM_INVALID_CDB_FIELD
;
80 buf
= transport_kmap_data_sg(cmd
);
82 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
85 spin_lock(&dev
->t10_alua
.lba_map_lock
);
86 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
87 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
88 transport_kunmap_data_sg(cmd
);
90 return TCM_UNSUPPORTED_SCSI_OPCODE
;
93 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
95 int desc_num
= off
+ 3;
99 if (cmd
->data_length
> off
)
100 put_unaligned_be64(map
->lba_map_first_lba
, &buf
[off
]);
102 if (cmd
->data_length
> off
)
103 put_unaligned_be64(map
->lba_map_last_lba
, &buf
[off
]);
107 list_for_each_entry(map_mem
, &map
->lba_map_mem_list
,
109 int alua_state
= map_mem
->lba_map_mem_alua_state
;
110 int alua_pg_id
= map_mem
->lba_map_mem_alua_pg_id
;
112 if (cmd
->data_length
> off
)
113 buf
[off
] = alua_state
& 0x0f;
115 if (cmd
->data_length
> off
)
116 buf
[off
] = (alua_pg_id
>> 8) & 0xff;
118 if (cmd
->data_length
> off
)
119 buf
[off
] = (alua_pg_id
& 0xff);
124 if (cmd
->data_length
> desc_num
)
125 buf
[desc_num
] = pg_num
;
127 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
130 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
132 put_unaligned_be16(rd_len
, &buf
[2]);
134 transport_kunmap_data_sg(cmd
);
136 target_complete_cmd(cmd
, GOOD
);
141 * REPORT_TARGET_PORT_GROUPS
143 * See spc4r17 section 6.27
146 target_emulate_report_target_port_groups(struct se_cmd
*cmd
)
148 struct se_device
*dev
= cmd
->se_dev
;
149 struct se_port
*port
;
150 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
151 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
154 int ext_hdr
= (cmd
->t_task_cdb
[1] & 0x20);
157 * Skip over RESERVED area to first Target port group descriptor
158 * depending on the PARAMETER DATA FORMAT type..
165 if (cmd
->data_length
< off
) {
166 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
167 " small for %s header\n", cmd
->data_length
,
168 (ext_hdr
) ? "extended" : "normal");
169 return TCM_INVALID_CDB_FIELD
;
171 buf
= transport_kmap_data_sg(cmd
);
173 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
175 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
176 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
179 * Check if the Target port group and Target port descriptor list
180 * based on tg_pt_gp_members count will fit into the response payload.
181 * Otherwise, bump rd_len to let the initiator know we have exceeded
182 * the allocation length and the response is truncated.
184 if ((off
+ 8 + (tg_pt_gp
->tg_pt_gp_members
* 4)) >
186 rd_len
+= 8 + (tg_pt_gp
->tg_pt_gp_members
* 4);
190 * PREF: Preferred target port bit, determine if this
191 * bit should be set for port group.
193 if (tg_pt_gp
->tg_pt_gp_pref
)
196 * Set the ASYMMETRIC ACCESS State
198 buf
[off
++] |= (atomic_read(
199 &tg_pt_gp
->tg_pt_gp_alua_access_state
) & 0xff);
201 * Set supported ASYMMETRIC ACCESS State bits
203 buf
[off
++] |= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
207 buf
[off
++] = ((tg_pt_gp
->tg_pt_gp_id
>> 8) & 0xff);
208 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_id
& 0xff);
210 off
++; /* Skip over Reserved */
214 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_alua_access_status
& 0xff);
216 * Vendor Specific field
222 buf
[off
++] = (tg_pt_gp
->tg_pt_gp_members
& 0xff);
225 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
226 list_for_each_entry(tg_pt_gp_mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
228 port
= tg_pt_gp_mem
->tg_pt
;
230 * Start Target Port descriptor format
232 * See spc4r17 section 6.2.7 Table 247
234 off
+= 2; /* Skip over Obsolete */
236 * Set RELATIVE TARGET PORT IDENTIFIER
238 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
239 buf
[off
++] = (port
->sep_rtpi
& 0xff);
242 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
244 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
246 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
248 put_unaligned_be32(rd_len
, &buf
[0]);
251 * Fill in the Extended header parameter data format if requested
256 * Set the implicit transition time (in seconds) for the application
257 * client to use as a base for it's transition timeout value.
259 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
260 * this CDB was received upon to determine this value individually
261 * for ALUA target port group.
263 port
= cmd
->se_lun
->lun_sep
;
264 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
266 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
267 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
269 buf
[5] = tg_pt_gp
->tg_pt_gp_implicit_trans_secs
;
270 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
273 transport_kunmap_data_sg(cmd
);
275 target_complete_cmd(cmd
, GOOD
);
280 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
282 * See spc4r17 section 6.35
285 target_emulate_set_target_port_groups(struct se_cmd
*cmd
)
287 struct se_device
*dev
= cmd
->se_dev
;
288 struct se_port
*port
, *l_port
= cmd
->se_lun
->lun_sep
;
289 struct se_node_acl
*nacl
= cmd
->se_sess
->se_node_acl
;
290 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *l_tg_pt_gp
;
291 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *l_tg_pt_gp_mem
;
294 sense_reason_t rc
= TCM_NO_SENSE
;
295 u32 len
= 4; /* Skip over RESERVED area in header */
296 int alua_access_state
, primary
= 0, valid_states
;
300 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
302 if (cmd
->data_length
< 4) {
303 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
304 " small\n", cmd
->data_length
);
305 return TCM_INVALID_PARAMETER_LIST
;
308 buf
= transport_kmap_data_sg(cmd
);
310 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
313 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
314 * for the local tg_pt_gp.
316 l_tg_pt_gp_mem
= l_port
->sep_alua_tg_pt_gp_mem
;
317 if (!l_tg_pt_gp_mem
) {
318 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
319 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
322 spin_lock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
323 l_tg_pt_gp
= l_tg_pt_gp_mem
->tg_pt_gp
;
325 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
326 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
327 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
330 spin_unlock(&l_tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
332 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
333 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
334 " while TPGS_EXPLICIT_ALUA is disabled\n");
335 rc
= TCM_UNSUPPORTED_SCSI_OPCODE
;
338 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
340 ptr
= &buf
[4]; /* Skip over RESERVED area in header */
342 while (len
< cmd
->data_length
) {
344 alua_access_state
= (ptr
[0] & 0x0f);
346 * Check the received ALUA access state, and determine if
347 * the state is a primary or secondary target port asymmetric
350 rc
= core_alua_check_transition(alua_access_state
,
351 valid_states
, &primary
);
354 * If the SET TARGET PORT GROUPS attempts to establish
355 * an invalid combination of target port asymmetric
356 * access states or attempts to establish an
357 * unsupported target port asymmetric access state,
358 * then the command shall be terminated with CHECK
359 * CONDITION status, with the sense key set to ILLEGAL
360 * REQUEST, and the additional sense code set to INVALID
361 * FIELD IN PARAMETER LIST.
367 * If the ASYMMETRIC ACCESS STATE field (see table 267)
368 * specifies a primary target port asymmetric access state,
369 * then the TARGET PORT GROUP OR TARGET PORT field specifies
370 * a primary target port group for which the primary target
371 * port asymmetric access state shall be changed. If the
372 * ASYMMETRIC ACCESS STATE field specifies a secondary target
373 * port asymmetric access state, then the TARGET PORT GROUP OR
374 * TARGET PORT field specifies the relative target port
375 * identifier (see 3.1.120) of the target port for which the
376 * secondary target port asymmetric access state shall be
380 tg_pt_id
= get_unaligned_be16(ptr
+ 2);
382 * Locate the matching target port group ID from
383 * the global tg_pt_gp list
385 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
386 list_for_each_entry(tg_pt_gp
,
387 &dev
->t10_alua
.tg_pt_gps_list
,
389 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
392 if (tg_pt_id
!= tg_pt_gp
->tg_pt_gp_id
)
395 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
396 smp_mb__after_atomic_inc();
398 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
400 if (!core_alua_do_port_transition(tg_pt_gp
,
402 alua_access_state
, 1))
405 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
406 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
407 smp_mb__after_atomic_dec();
410 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
413 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
414 * the Target Port in question for the the incoming
415 * SET_TARGET_PORT_GROUPS op.
417 rtpi
= get_unaligned_be16(ptr
+ 2);
419 * Locate the matching relative target port identifier
420 * for the struct se_device storage object.
422 spin_lock(&dev
->se_port_lock
);
423 list_for_each_entry(port
, &dev
->dev_sep_list
,
425 if (port
->sep_rtpi
!= rtpi
)
428 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
430 spin_unlock(&dev
->se_port_lock
);
432 if (!core_alua_set_tg_pt_secondary_state(
433 tg_pt_gp_mem
, port
, 1, 1))
436 spin_lock(&dev
->se_port_lock
);
439 spin_unlock(&dev
->se_port_lock
);
443 rc
= TCM_INVALID_PARAMETER_LIST
;
452 transport_kunmap_data_sg(cmd
);
454 target_complete_cmd(cmd
, GOOD
);
458 static inline void set_ascq(struct se_cmd
*cmd
, u8 alua_ascq
)
461 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
462 * The ALUA additional sense code qualifier (ASCQ) is determined
463 * by the ALUA primary or secondary access state..
465 pr_debug("[%s]: ALUA TG Port not available, "
466 "SenseKey: NOT_READY, ASC/ASCQ: "
468 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
470 cmd
->scsi_asc
= 0x04;
471 cmd
->scsi_ascq
= alua_ascq
;
474 static inline void core_alua_state_nonoptimized(
477 int nonop_delay_msecs
)
480 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
481 * later to determine if processing of this cmd needs to be
482 * temporarily delayed for the Active/NonOptimized primary access state.
484 cmd
->se_cmd_flags
|= SCF_ALUA_NON_OPTIMIZED
;
485 cmd
->alua_nonop_delay
= nonop_delay_msecs
;
488 static inline int core_alua_state_lba_dependent(
490 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
492 struct se_device
*dev
= cmd
->se_dev
;
493 u64 segment_size
, segment_mult
, sectors
, lba
;
495 /* Only need to check for cdb actually containing LBAs */
496 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
))
499 spin_lock(&dev
->t10_alua
.lba_map_lock
);
500 segment_size
= dev
->t10_alua
.lba_map_segment_size
;
501 segment_mult
= dev
->t10_alua
.lba_map_segment_multiplier
;
502 sectors
= cmd
->data_length
/ dev
->dev_attrib
.block_size
;
504 lba
= cmd
->t_task_lba
;
505 while (lba
< cmd
->t_task_lba
+ sectors
) {
506 struct t10_alua_lba_map
*cur_map
= NULL
, *map
;
507 struct t10_alua_lba_map_member
*map_mem
;
509 list_for_each_entry(map
, &dev
->t10_alua
.lba_map_list
,
511 u64 start_lba
, last_lba
;
512 u64 first_lba
= map
->lba_map_first_lba
;
516 start_lba
= do_div(tmp
, segment_size
* segment_mult
);
518 last_lba
= first_lba
+ segment_size
- 1;
519 if (start_lba
>= first_lba
&&
520 start_lba
<= last_lba
) {
526 last_lba
= map
->lba_map_last_lba
;
527 if (lba
>= first_lba
&& lba
<= last_lba
) {
535 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
536 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
539 list_for_each_entry(map_mem
, &cur_map
->lba_map_mem_list
,
541 if (map_mem
->lba_map_mem_alua_pg_id
!=
542 tg_pt_gp
->tg_pt_gp_id
)
544 switch(map_mem
->lba_map_mem_alua_state
) {
545 case ALUA_ACCESS_STATE_STANDBY
:
546 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
547 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
549 case ALUA_ACCESS_STATE_UNAVAILABLE
:
550 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
551 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
558 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
562 static inline int core_alua_state_standby(
567 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
568 * spc4r17 section 5.9.2.4.4
577 case RECEIVE_DIAGNOSTIC
:
578 case SEND_DIAGNOSTIC
:
581 case SERVICE_ACTION_IN
:
582 switch (cdb
[1] & 0x1f) {
583 case SAI_READ_CAPACITY_16
:
586 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
590 switch (cdb
[1] & 0x1f) {
591 case MI_REPORT_TARGET_PGS
:
594 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
597 case MAINTENANCE_OUT
:
599 case MO_SET_TARGET_PGS
:
602 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
606 case PERSISTENT_RESERVE_IN
:
607 case PERSISTENT_RESERVE_OUT
:
612 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_STANDBY
);
619 static inline int core_alua_state_unavailable(
624 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
625 * spc4r17 section 5.9.2.4.5
632 switch (cdb
[1] & 0x1f) {
633 case MI_REPORT_TARGET_PGS
:
636 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
639 case MAINTENANCE_OUT
:
641 case MO_SET_TARGET_PGS
:
644 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
652 set_ascq(cmd
, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE
);
659 static inline int core_alua_state_transition(
664 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
665 * spc4r17 section 5.9.2.5
672 switch (cdb
[1] & 0x1f) {
673 case MI_REPORT_TARGET_PGS
:
676 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
684 set_ascq(cmd
, ASCQ_04H_ALUA_STATE_TRANSITION
);
692 * return 1: Is used to signal LUN not accessible, and check condition/not ready
693 * return 0: Used to signal success
694 * return -1: Used to signal failure, and invalid cdb field
697 target_alua_state_check(struct se_cmd
*cmd
)
699 struct se_device
*dev
= cmd
->se_dev
;
700 unsigned char *cdb
= cmd
->t_task_cdb
;
701 struct se_lun
*lun
= cmd
->se_lun
;
702 struct se_port
*port
= lun
->lun_sep
;
703 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
704 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
705 int out_alua_state
, nonop_delay_msecs
;
707 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
709 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
715 * First, check for a struct se_port specific secondary ALUA target port
716 * access state: OFFLINE
718 if (atomic_read(&port
->sep_tg_pt_secondary_offline
)) {
719 pr_debug("ALUA: Got secondary offline status for local"
721 set_ascq(cmd
, ASCQ_04H_ALUA_OFFLINE
);
722 return TCM_CHECK_CONDITION_NOT_READY
;
725 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
726 * ALUA target port group, to obtain current ALUA access state.
727 * Otherwise look for the underlying struct se_device association with
728 * a ALUA logical unit group.
730 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
734 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
735 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
736 out_alua_state
= atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
737 nonop_delay_msecs
= tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
;
738 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
740 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
741 * statement so the compiler knows explicitly to check this case first.
742 * For the Optimized ALUA access state case, we want to process the
743 * incoming fabric cmd ASAP..
745 if (out_alua_state
== ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
)
748 switch (out_alua_state
) {
749 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
750 core_alua_state_nonoptimized(cmd
, cdb
, nonop_delay_msecs
);
752 case ALUA_ACCESS_STATE_STANDBY
:
753 if (core_alua_state_standby(cmd
, cdb
))
754 return TCM_CHECK_CONDITION_NOT_READY
;
756 case ALUA_ACCESS_STATE_UNAVAILABLE
:
757 if (core_alua_state_unavailable(cmd
, cdb
))
758 return TCM_CHECK_CONDITION_NOT_READY
;
760 case ALUA_ACCESS_STATE_TRANSITION
:
761 if (core_alua_state_transition(cmd
, cdb
))
762 return TCM_CHECK_CONDITION_NOT_READY
;
764 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
765 if (core_alua_state_lba_dependent(cmd
, tg_pt_gp
))
766 return TCM_CHECK_CONDITION_NOT_READY
;
769 * OFFLINE is a secondary ALUA target port group access state, that is
770 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
772 case ALUA_ACCESS_STATE_OFFLINE
:
774 pr_err("Unknown ALUA access state: 0x%02x\n",
776 return TCM_INVALID_CDB_FIELD
;
783 * Check implicit and explicit ALUA state change request.
785 static sense_reason_t
786 core_alua_check_transition(int state
, int valid
, int *primary
)
789 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
790 * defined as primary target port asymmetric access states.
793 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
794 if (!(valid
& ALUA_AO_SUP
))
798 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
799 if (!(valid
& ALUA_AN_SUP
))
803 case ALUA_ACCESS_STATE_STANDBY
:
804 if (!(valid
& ALUA_S_SUP
))
808 case ALUA_ACCESS_STATE_UNAVAILABLE
:
809 if (!(valid
& ALUA_U_SUP
))
813 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
814 if (!(valid
& ALUA_LBD_SUP
))
818 case ALUA_ACCESS_STATE_OFFLINE
:
820 * OFFLINE state is defined as a secondary target port
821 * asymmetric access state.
823 if (!(valid
& ALUA_O_SUP
))
827 case ALUA_ACCESS_STATE_TRANSITION
:
829 * Transitioning is set internally, and
830 * cannot be selected manually.
834 pr_err("Unknown ALUA access state: 0x%02x\n", state
);
835 return TCM_INVALID_PARAMETER_LIST
;
841 pr_err("ALUA access state %s not supported",
842 core_alua_dump_state(state
));
843 return TCM_INVALID_PARAMETER_LIST
;
846 static char *core_alua_dump_state(int state
)
849 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
:
850 return "Active/Optimized";
851 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED
:
852 return "Active/NonOptimized";
853 case ALUA_ACCESS_STATE_LBA_DEPENDENT
:
854 return "LBA Dependent";
855 case ALUA_ACCESS_STATE_STANDBY
:
857 case ALUA_ACCESS_STATE_UNAVAILABLE
:
858 return "Unavailable";
859 case ALUA_ACCESS_STATE_OFFLINE
:
861 case ALUA_ACCESS_STATE_TRANSITION
:
862 return "Transitioning";
870 char *core_alua_dump_status(int status
)
873 case ALUA_STATUS_NONE
:
875 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
876 return "Altered by Explicit STPG";
877 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
:
878 return "Altered by Implicit ALUA";
887 * Used by fabric modules to determine when we need to delay processing
888 * for the Active/NonOptimized paths..
890 int core_alua_check_nonop_delay(
893 if (!(cmd
->se_cmd_flags
& SCF_ALUA_NON_OPTIMIZED
))
898 * The ALUA Active/NonOptimized access state delay can be disabled
899 * in via configfs with a value of zero
901 if (!cmd
->alua_nonop_delay
)
904 * struct se_cmd->alua_nonop_delay gets set by a target port group
905 * defined interval in core_alua_state_nonoptimized()
907 msleep_interruptible(cmd
->alua_nonop_delay
);
910 EXPORT_SYMBOL(core_alua_check_nonop_delay
);
913 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
916 static int core_alua_write_tpg_metadata(
918 unsigned char *md_buf
,
921 struct file
*file
= filp_open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
925 pr_err("filp_open(%s) for ALUA metadata failed\n", path
);
928 ret
= kernel_write(file
, md_buf
, md_buf_len
, 0);
930 pr_err("Error writing ALUA metadata file: %s\n", path
);
932 return (ret
< 0) ? -EIO
: 0;
936 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
938 static int core_alua_update_tpg_primary_metadata(
939 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
941 unsigned char *md_buf
;
942 struct t10_wwn
*wwn
= &tg_pt_gp
->tg_pt_gp_dev
->t10_wwn
;
943 char path
[ALUA_METADATA_PATH_LEN
];
946 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
948 pr_err("Unable to allocate buf for ALUA metadata\n");
952 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
954 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
,
956 "alua_access_state=0x%02x\n"
957 "alua_access_status=0x%02x\n",
958 tg_pt_gp
->tg_pt_gp_id
,
959 tg_pt_gp
->tg_pt_gp_alua_pending_state
,
960 tg_pt_gp
->tg_pt_gp_alua_access_status
);
962 snprintf(path
, ALUA_METADATA_PATH_LEN
,
963 "/var/target/alua/tpgs_%s/%s", &wwn
->unit_serial
[0],
964 config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
));
966 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
971 static void core_alua_do_transition_tg_pt_work(struct work_struct
*work
)
973 struct t10_alua_tg_pt_gp
*tg_pt_gp
= container_of(work
,
974 struct t10_alua_tg_pt_gp
, tg_pt_gp_transition_work
.work
);
975 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
976 struct se_dev_entry
*se_deve
;
977 struct se_lun_acl
*lacl
;
978 struct se_port
*port
;
979 struct t10_alua_tg_pt_gp_member
*mem
;
980 bool explicit = (tg_pt_gp
->tg_pt_gp_alua_access_status
==
981 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
);
983 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
984 list_for_each_entry(mem
, &tg_pt_gp
->tg_pt_gp_mem_list
,
988 * After an implicit target port asymmetric access state
989 * change, a device server shall establish a unit attention
990 * condition for the initiator port associated with every I_T
991 * nexus with the additional sense code set to ASYMMETRIC
992 * ACCESS STATE CHANGED.
994 * After an explicit target port asymmetric access state
995 * change, a device server shall establish a unit attention
996 * condition with the additional sense code set to ASYMMETRIC
997 * ACCESS STATE CHANGED for the initiator port associated with
998 * every I_T nexus other than the I_T nexus on which the SET
999 * TARGET PORT GROUPS command
1001 atomic_inc(&mem
->tg_pt_gp_mem_ref_cnt
);
1002 smp_mb__after_atomic_inc();
1003 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1005 spin_lock_bh(&port
->sep_alua_lock
);
1006 list_for_each_entry(se_deve
, &port
->sep_alua_list
,
1008 lacl
= se_deve
->se_lun_acl
;
1010 * se_deve->se_lun_acl pointer may be NULL for a
1011 * entry created without explicit Node+MappedLUN ACLs
1016 if ((tg_pt_gp
->tg_pt_gp_alua_access_status
==
1017 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
1018 (tg_pt_gp
->tg_pt_gp_alua_nacl
!= NULL
) &&
1019 (tg_pt_gp
->tg_pt_gp_alua_nacl
== lacl
->se_lun_nacl
) &&
1020 (tg_pt_gp
->tg_pt_gp_alua_port
!= NULL
) &&
1021 (tg_pt_gp
->tg_pt_gp_alua_port
== port
))
1024 core_scsi3_ua_allocate(lacl
->se_lun_nacl
,
1025 se_deve
->mapped_lun
, 0x2A,
1026 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED
);
1028 spin_unlock_bh(&port
->sep_alua_lock
);
1030 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1031 atomic_dec(&mem
->tg_pt_gp_mem_ref_cnt
);
1032 smp_mb__after_atomic_dec();
1034 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1036 * Update the ALUA metadata buf that has been allocated in
1037 * core_alua_do_port_transition(), this metadata will be written
1040 * Note that there is the case where we do not want to update the
1041 * metadata when the saved metadata is being parsed in userspace
1042 * when setting the existing port access state and access status.
1044 * Also note that the failure to write out the ALUA metadata to
1045 * struct file does NOT affect the actual ALUA transition.
1047 if (tg_pt_gp
->tg_pt_gp_write_metadata
) {
1048 mutex_lock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1049 core_alua_update_tpg_primary_metadata(tg_pt_gp
);
1050 mutex_unlock(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1053 * Set the current primary ALUA access state to the requested new state
1055 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1056 tg_pt_gp
->tg_pt_gp_alua_pending_state
);
1058 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1059 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1060 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1061 tg_pt_gp
->tg_pt_gp_id
,
1062 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_previous_state
),
1063 core_alua_dump_state(tg_pt_gp
->tg_pt_gp_alua_pending_state
));
1064 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1065 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1066 smp_mb__after_atomic_dec();
1067 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1069 if (tg_pt_gp
->tg_pt_gp_transition_complete
)
1070 complete(tg_pt_gp
->tg_pt_gp_transition_complete
);
1073 static int core_alua_do_transition_tg_pt(
1074 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1078 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1079 DECLARE_COMPLETION_ONSTACK(wait
);
1081 /* Nothing to be done here */
1082 if (atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) == new_state
)
1085 if (new_state
== ALUA_ACCESS_STATE_TRANSITION
)
1089 * Flush any pending transitions
1091 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
&&
1092 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
) ==
1093 ALUA_ACCESS_STATE_TRANSITION
) {
1095 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1096 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1097 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1098 wait_for_completion(&wait
);
1099 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1104 * Save the old primary ALUA access state, and set the current state
1105 * to ALUA_ACCESS_STATE_TRANSITION.
1107 tg_pt_gp
->tg_pt_gp_alua_previous_state
=
1108 atomic_read(&tg_pt_gp
->tg_pt_gp_alua_access_state
);
1109 tg_pt_gp
->tg_pt_gp_alua_pending_state
= new_state
;
1111 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1112 ALUA_ACCESS_STATE_TRANSITION
);
1113 tg_pt_gp
->tg_pt_gp_alua_access_status
= (explicit) ?
1114 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1115 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1118 * Check for the optional ALUA primary state transition delay
1120 if (tg_pt_gp
->tg_pt_gp_trans_delay_msecs
!= 0)
1121 msleep_interruptible(tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
1124 * Take a reference for workqueue item
1126 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1127 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1128 smp_mb__after_atomic_inc();
1129 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1131 if (!explicit && tg_pt_gp
->tg_pt_gp_implicit_trans_secs
) {
1132 unsigned long transition_tmo
;
1134 transition_tmo
= tg_pt_gp
->tg_pt_gp_implicit_trans_secs
* HZ
;
1135 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1136 &tg_pt_gp
->tg_pt_gp_transition_work
,
1139 tg_pt_gp
->tg_pt_gp_transition_complete
= &wait
;
1140 queue_delayed_work(tg_pt_gp
->tg_pt_gp_dev
->tmr_wq
,
1141 &tg_pt_gp
->tg_pt_gp_transition_work
, 0);
1142 wait_for_completion(&wait
);
1143 tg_pt_gp
->tg_pt_gp_transition_complete
= NULL
;
1149 int core_alua_do_port_transition(
1150 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
,
1151 struct se_device
*l_dev
,
1152 struct se_port
*l_port
,
1153 struct se_node_acl
*l_nacl
,
1157 struct se_device
*dev
;
1158 struct t10_alua_lu_gp
*lu_gp
;
1159 struct t10_alua_lu_gp_member
*lu_gp_mem
, *local_lu_gp_mem
;
1160 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1161 int primary
, valid_states
, rc
= 0;
1163 valid_states
= l_tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1164 if (core_alua_check_transition(new_state
, valid_states
, &primary
) != 0)
1167 local_lu_gp_mem
= l_dev
->dev_alua_lu_gp_mem
;
1168 spin_lock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1169 lu_gp
= local_lu_gp_mem
->lu_gp
;
1170 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1171 smp_mb__after_atomic_inc();
1172 spin_unlock(&local_lu_gp_mem
->lu_gp_mem_lock
);
1174 * For storage objects that are members of the 'default_lu_gp',
1175 * we only do transition on the passed *l_tp_pt_gp, and not
1176 * on all of the matching target port groups IDs in default_lu_gp.
1178 if (!lu_gp
->lu_gp_id
) {
1180 * core_alua_do_transition_tg_pt() will always return
1183 l_tg_pt_gp
->tg_pt_gp_alua_port
= l_port
;
1184 l_tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1185 rc
= core_alua_do_transition_tg_pt(l_tg_pt_gp
,
1186 new_state
, explicit);
1187 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1188 smp_mb__after_atomic_dec();
1192 * For all other LU groups aside from 'default_lu_gp', walk all of
1193 * the associated storage objects looking for a matching target port
1194 * group ID from the local target port group.
1196 spin_lock(&lu_gp
->lu_gp_lock
);
1197 list_for_each_entry(lu_gp_mem
, &lu_gp
->lu_gp_mem_list
,
1200 dev
= lu_gp_mem
->lu_gp_mem_dev
;
1201 atomic_inc(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1202 smp_mb__after_atomic_inc();
1203 spin_unlock(&lu_gp
->lu_gp_lock
);
1205 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1206 list_for_each_entry(tg_pt_gp
,
1207 &dev
->t10_alua
.tg_pt_gps_list
,
1210 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1213 * If the target behavior port asymmetric access state
1214 * is changed for any target port group accessible via
1215 * a logical unit within a LU group, the target port
1216 * behavior group asymmetric access states for the same
1217 * target port group accessible via other logical units
1218 * in that LU group will also change.
1220 if (l_tg_pt_gp
->tg_pt_gp_id
!= tg_pt_gp
->tg_pt_gp_id
)
1223 if (l_tg_pt_gp
== tg_pt_gp
) {
1224 tg_pt_gp
->tg_pt_gp_alua_port
= l_port
;
1225 tg_pt_gp
->tg_pt_gp_alua_nacl
= l_nacl
;
1227 tg_pt_gp
->tg_pt_gp_alua_port
= NULL
;
1228 tg_pt_gp
->tg_pt_gp_alua_nacl
= NULL
;
1230 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1231 smp_mb__after_atomic_inc();
1232 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1234 * core_alua_do_transition_tg_pt() will always return
1237 rc
= core_alua_do_transition_tg_pt(tg_pt_gp
,
1238 new_state
, explicit);
1240 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1241 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1242 smp_mb__after_atomic_dec();
1246 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1248 spin_lock(&lu_gp
->lu_gp_lock
);
1249 atomic_dec(&lu_gp_mem
->lu_gp_mem_ref_cnt
);
1250 smp_mb__after_atomic_dec();
1252 spin_unlock(&lu_gp
->lu_gp_lock
);
1255 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1256 " Group IDs: %hu %s transition to primary state: %s\n",
1257 config_item_name(&lu_gp
->lu_gp_group
.cg_item
),
1258 l_tg_pt_gp
->tg_pt_gp_id
,
1259 (explicit) ? "explicit" : "implicit",
1260 core_alua_dump_state(new_state
));
1263 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1264 smp_mb__after_atomic_dec();
1269 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1271 static int core_alua_update_tpg_secondary_metadata(
1272 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1273 struct se_port
*port
)
1275 unsigned char *md_buf
;
1276 struct se_portal_group
*se_tpg
= port
->sep_tpg
;
1277 char path
[ALUA_METADATA_PATH_LEN
], wwn
[ALUA_SECONDARY_METADATA_WWN_LEN
];
1280 md_buf
= kzalloc(ALUA_MD_BUF_LEN
, GFP_KERNEL
);
1282 pr_err("Unable to allocate buf for ALUA metadata\n");
1286 memset(path
, 0, ALUA_METADATA_PATH_LEN
);
1287 memset(wwn
, 0, ALUA_SECONDARY_METADATA_WWN_LEN
);
1289 len
= snprintf(wwn
, ALUA_SECONDARY_METADATA_WWN_LEN
, "%s",
1290 se_tpg
->se_tpg_tfo
->tpg_get_wwn(se_tpg
));
1292 if (se_tpg
->se_tpg_tfo
->tpg_get_tag
!= NULL
)
1293 snprintf(wwn
+len
, ALUA_SECONDARY_METADATA_WWN_LEN
-len
, "+%hu",
1294 se_tpg
->se_tpg_tfo
->tpg_get_tag(se_tpg
));
1296 len
= snprintf(md_buf
, ALUA_MD_BUF_LEN
, "alua_tg_pt_offline=%d\n"
1297 "alua_tg_pt_status=0x%02x\n",
1298 atomic_read(&port
->sep_tg_pt_secondary_offline
),
1299 port
->sep_tg_pt_secondary_stat
);
1301 snprintf(path
, ALUA_METADATA_PATH_LEN
, "/var/target/alua/%s/%s/lun_%u",
1302 se_tpg
->se_tpg_tfo
->get_fabric_name(), wwn
,
1303 port
->sep_lun
->unpacked_lun
);
1305 rc
= core_alua_write_tpg_metadata(path
, md_buf
, len
);
1311 static int core_alua_set_tg_pt_secondary_state(
1312 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1313 struct se_port
*port
,
1317 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1318 int trans_delay_msecs
;
1320 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1321 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1323 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1324 pr_err("Unable to complete secondary state"
1328 trans_delay_msecs
= tg_pt_gp
->tg_pt_gp_trans_delay_msecs
;
1330 * Set the secondary ALUA target port access state to OFFLINE
1331 * or release the previously secondary state for struct se_port
1334 atomic_set(&port
->sep_tg_pt_secondary_offline
, 1);
1336 atomic_set(&port
->sep_tg_pt_secondary_offline
, 0);
1338 port
->sep_tg_pt_secondary_stat
= (explicit) ?
1339 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
:
1340 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
;
1342 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1343 " to secondary access state: %s\n", (explicit) ? "explicit" :
1344 "implicit", config_item_name(&tg_pt_gp
->tg_pt_gp_group
.cg_item
),
1345 tg_pt_gp
->tg_pt_gp_id
, (offline
) ? "OFFLINE" : "ONLINE");
1347 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1349 * Do the optional transition delay after we set the secondary
1350 * ALUA access state.
1352 if (trans_delay_msecs
!= 0)
1353 msleep_interruptible(trans_delay_msecs
);
1355 * See if we need to update the ALUA fabric port metadata for
1356 * secondary state and status
1358 if (port
->sep_tg_pt_secondary_write_md
) {
1359 mutex_lock(&port
->sep_tg_pt_md_mutex
);
1360 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem
, port
);
1361 mutex_unlock(&port
->sep_tg_pt_md_mutex
);
1367 struct t10_alua_lba_map
*
1368 core_alua_allocate_lba_map(struct list_head
*list
,
1369 u64 first_lba
, u64 last_lba
)
1371 struct t10_alua_lba_map
*lba_map
;
1373 lba_map
= kmem_cache_zalloc(t10_alua_lba_map_cache
, GFP_KERNEL
);
1375 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1376 return ERR_PTR(-ENOMEM
);
1378 INIT_LIST_HEAD(&lba_map
->lba_map_mem_list
);
1379 lba_map
->lba_map_first_lba
= first_lba
;
1380 lba_map
->lba_map_last_lba
= last_lba
;
1382 list_add_tail(&lba_map
->lba_map_list
, list
);
1387 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map
*lba_map
,
1388 int pg_id
, int state
)
1390 struct t10_alua_lba_map_member
*lba_map_mem
;
1392 list_for_each_entry(lba_map_mem
, &lba_map
->lba_map_mem_list
,
1394 if (lba_map_mem
->lba_map_mem_alua_pg_id
== pg_id
) {
1395 pr_err("Duplicate pg_id %d in lba_map\n", pg_id
);
1400 lba_map_mem
= kmem_cache_zalloc(t10_alua_lba_map_mem_cache
, GFP_KERNEL
);
1402 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1405 lba_map_mem
->lba_map_mem_alua_state
= state
;
1406 lba_map_mem
->lba_map_mem_alua_pg_id
= pg_id
;
1408 list_add_tail(&lba_map_mem
->lba_map_mem_list
,
1409 &lba_map
->lba_map_mem_list
);
1414 core_alua_free_lba_map(struct list_head
*lba_list
)
1416 struct t10_alua_lba_map
*lba_map
, *lba_map_tmp
;
1417 struct t10_alua_lba_map_member
*lba_map_mem
, *lba_map_mem_tmp
;
1419 list_for_each_entry_safe(lba_map
, lba_map_tmp
, lba_list
,
1421 list_for_each_entry_safe(lba_map_mem
, lba_map_mem_tmp
,
1422 &lba_map
->lba_map_mem_list
,
1424 list_del(&lba_map_mem
->lba_map_mem_list
);
1425 kmem_cache_free(t10_alua_lba_map_mem_cache
,
1428 list_del(&lba_map
->lba_map_list
);
1429 kmem_cache_free(t10_alua_lba_map_cache
, lba_map
);
1434 core_alua_set_lba_map(struct se_device
*dev
, struct list_head
*lba_map_list
,
1435 int segment_size
, int segment_mult
)
1437 struct list_head old_lba_map_list
;
1438 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1439 int activate
= 0, supported
;
1441 INIT_LIST_HEAD(&old_lba_map_list
);
1442 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1443 dev
->t10_alua
.lba_map_segment_size
= segment_size
;
1444 dev
->t10_alua
.lba_map_segment_multiplier
= segment_mult
;
1445 list_splice_init(&dev
->t10_alua
.lba_map_list
, &old_lba_map_list
);
1447 list_splice_init(lba_map_list
, &dev
->t10_alua
.lba_map_list
);
1450 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1451 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1452 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1455 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1457 supported
= tg_pt_gp
->tg_pt_gp_alua_supported_states
;
1459 supported
|= ALUA_LBD_SUP
;
1461 supported
&= ~ALUA_LBD_SUP
;
1462 tg_pt_gp
->tg_pt_gp_alua_supported_states
= supported
;
1464 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1465 core_alua_free_lba_map(&old_lba_map_list
);
1468 struct t10_alua_lu_gp
*
1469 core_alua_allocate_lu_gp(const char *name
, int def_group
)
1471 struct t10_alua_lu_gp
*lu_gp
;
1473 lu_gp
= kmem_cache_zalloc(t10_alua_lu_gp_cache
, GFP_KERNEL
);
1475 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1476 return ERR_PTR(-ENOMEM
);
1478 INIT_LIST_HEAD(&lu_gp
->lu_gp_node
);
1479 INIT_LIST_HEAD(&lu_gp
->lu_gp_mem_list
);
1480 spin_lock_init(&lu_gp
->lu_gp_lock
);
1481 atomic_set(&lu_gp
->lu_gp_ref_cnt
, 0);
1484 lu_gp
->lu_gp_id
= alua_lu_gps_counter
++;
1485 lu_gp
->lu_gp_valid_id
= 1;
1486 alua_lu_gps_count
++;
1492 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp
*lu_gp
, u16 lu_gp_id
)
1494 struct t10_alua_lu_gp
*lu_gp_tmp
;
1497 * The lu_gp->lu_gp_id may only be set once..
1499 if (lu_gp
->lu_gp_valid_id
) {
1500 pr_warn("ALUA LU Group already has a valid ID,"
1501 " ignoring request\n");
1505 spin_lock(&lu_gps_lock
);
1506 if (alua_lu_gps_count
== 0x0000ffff) {
1507 pr_err("Maximum ALUA alua_lu_gps_count:"
1508 " 0x0000ffff reached\n");
1509 spin_unlock(&lu_gps_lock
);
1510 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1514 lu_gp_id_tmp
= (lu_gp_id
!= 0) ? lu_gp_id
:
1515 alua_lu_gps_counter
++;
1517 list_for_each_entry(lu_gp_tmp
, &lu_gps_list
, lu_gp_node
) {
1518 if (lu_gp_tmp
->lu_gp_id
== lu_gp_id_tmp
) {
1522 pr_warn("ALUA Logical Unit Group ID: %hu"
1523 " already exists, ignoring request\n",
1525 spin_unlock(&lu_gps_lock
);
1530 lu_gp
->lu_gp_id
= lu_gp_id_tmp
;
1531 lu_gp
->lu_gp_valid_id
= 1;
1532 list_add_tail(&lu_gp
->lu_gp_node
, &lu_gps_list
);
1533 alua_lu_gps_count
++;
1534 spin_unlock(&lu_gps_lock
);
1539 static struct t10_alua_lu_gp_member
*
1540 core_alua_allocate_lu_gp_mem(struct se_device
*dev
)
1542 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1544 lu_gp_mem
= kmem_cache_zalloc(t10_alua_lu_gp_mem_cache
, GFP_KERNEL
);
1546 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1547 return ERR_PTR(-ENOMEM
);
1549 INIT_LIST_HEAD(&lu_gp_mem
->lu_gp_mem_list
);
1550 spin_lock_init(&lu_gp_mem
->lu_gp_mem_lock
);
1551 atomic_set(&lu_gp_mem
->lu_gp_mem_ref_cnt
, 0);
1553 lu_gp_mem
->lu_gp_mem_dev
= dev
;
1554 dev
->dev_alua_lu_gp_mem
= lu_gp_mem
;
1559 void core_alua_free_lu_gp(struct t10_alua_lu_gp
*lu_gp
)
1561 struct t10_alua_lu_gp_member
*lu_gp_mem
, *lu_gp_mem_tmp
;
1563 * Once we have reached this point, config_item_put() has
1564 * already been called from target_core_alua_drop_lu_gp().
1566 * Here, we remove the *lu_gp from the global list so that
1567 * no associations can be made while we are releasing
1568 * struct t10_alua_lu_gp.
1570 spin_lock(&lu_gps_lock
);
1571 list_del(&lu_gp
->lu_gp_node
);
1572 alua_lu_gps_count
--;
1573 spin_unlock(&lu_gps_lock
);
1575 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1576 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1577 * released with core_alua_put_lu_gp_from_name()
1579 while (atomic_read(&lu_gp
->lu_gp_ref_cnt
))
1582 * Release reference to struct t10_alua_lu_gp * from all associated
1585 spin_lock(&lu_gp
->lu_gp_lock
);
1586 list_for_each_entry_safe(lu_gp_mem
, lu_gp_mem_tmp
,
1587 &lu_gp
->lu_gp_mem_list
, lu_gp_mem_list
) {
1588 if (lu_gp_mem
->lu_gp_assoc
) {
1589 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1590 lu_gp
->lu_gp_members
--;
1591 lu_gp_mem
->lu_gp_assoc
= 0;
1593 spin_unlock(&lu_gp
->lu_gp_lock
);
1596 * lu_gp_mem is associated with a single
1597 * struct se_device->dev_alua_lu_gp_mem, and is released when
1598 * struct se_device is released via core_alua_free_lu_gp_mem().
1600 * If the passed lu_gp does NOT match the default_lu_gp, assume
1601 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1603 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1604 if (lu_gp
!= default_lu_gp
)
1605 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
1608 lu_gp_mem
->lu_gp
= NULL
;
1609 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1611 spin_lock(&lu_gp
->lu_gp_lock
);
1613 spin_unlock(&lu_gp
->lu_gp_lock
);
1615 kmem_cache_free(t10_alua_lu_gp_cache
, lu_gp
);
1618 void core_alua_free_lu_gp_mem(struct se_device
*dev
)
1620 struct t10_alua_lu_gp
*lu_gp
;
1621 struct t10_alua_lu_gp_member
*lu_gp_mem
;
1623 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
1627 while (atomic_read(&lu_gp_mem
->lu_gp_mem_ref_cnt
))
1630 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
1631 lu_gp
= lu_gp_mem
->lu_gp
;
1633 spin_lock(&lu_gp
->lu_gp_lock
);
1634 if (lu_gp_mem
->lu_gp_assoc
) {
1635 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1636 lu_gp
->lu_gp_members
--;
1637 lu_gp_mem
->lu_gp_assoc
= 0;
1639 spin_unlock(&lu_gp
->lu_gp_lock
);
1640 lu_gp_mem
->lu_gp
= NULL
;
1642 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
1644 kmem_cache_free(t10_alua_lu_gp_mem_cache
, lu_gp_mem
);
1647 struct t10_alua_lu_gp
*core_alua_get_lu_gp_by_name(const char *name
)
1649 struct t10_alua_lu_gp
*lu_gp
;
1650 struct config_item
*ci
;
1652 spin_lock(&lu_gps_lock
);
1653 list_for_each_entry(lu_gp
, &lu_gps_list
, lu_gp_node
) {
1654 if (!lu_gp
->lu_gp_valid_id
)
1656 ci
= &lu_gp
->lu_gp_group
.cg_item
;
1657 if (!strcmp(config_item_name(ci
), name
)) {
1658 atomic_inc(&lu_gp
->lu_gp_ref_cnt
);
1659 spin_unlock(&lu_gps_lock
);
1663 spin_unlock(&lu_gps_lock
);
1668 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp
*lu_gp
)
1670 spin_lock(&lu_gps_lock
);
1671 atomic_dec(&lu_gp
->lu_gp_ref_cnt
);
1672 spin_unlock(&lu_gps_lock
);
1676 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1678 void __core_alua_attach_lu_gp_mem(
1679 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1680 struct t10_alua_lu_gp
*lu_gp
)
1682 spin_lock(&lu_gp
->lu_gp_lock
);
1683 lu_gp_mem
->lu_gp
= lu_gp
;
1684 lu_gp_mem
->lu_gp_assoc
= 1;
1685 list_add_tail(&lu_gp_mem
->lu_gp_mem_list
, &lu_gp
->lu_gp_mem_list
);
1686 lu_gp
->lu_gp_members
++;
1687 spin_unlock(&lu_gp
->lu_gp_lock
);
1691 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1693 void __core_alua_drop_lu_gp_mem(
1694 struct t10_alua_lu_gp_member
*lu_gp_mem
,
1695 struct t10_alua_lu_gp
*lu_gp
)
1697 spin_lock(&lu_gp
->lu_gp_lock
);
1698 list_del(&lu_gp_mem
->lu_gp_mem_list
);
1699 lu_gp_mem
->lu_gp
= NULL
;
1700 lu_gp_mem
->lu_gp_assoc
= 0;
1701 lu_gp
->lu_gp_members
--;
1702 spin_unlock(&lu_gp
->lu_gp_lock
);
1705 struct t10_alua_tg_pt_gp
*core_alua_allocate_tg_pt_gp(struct se_device
*dev
,
1706 const char *name
, int def_group
)
1708 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1710 tg_pt_gp
= kmem_cache_zalloc(t10_alua_tg_pt_gp_cache
, GFP_KERNEL
);
1712 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1715 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_list
);
1716 INIT_LIST_HEAD(&tg_pt_gp
->tg_pt_gp_mem_list
);
1717 mutex_init(&tg_pt_gp
->tg_pt_gp_md_mutex
);
1718 spin_lock_init(&tg_pt_gp
->tg_pt_gp_lock
);
1719 atomic_set(&tg_pt_gp
->tg_pt_gp_ref_cnt
, 0);
1720 INIT_DELAYED_WORK(&tg_pt_gp
->tg_pt_gp_transition_work
,
1721 core_alua_do_transition_tg_pt_work
);
1722 tg_pt_gp
->tg_pt_gp_dev
= dev
;
1723 atomic_set(&tg_pt_gp
->tg_pt_gp_alua_access_state
,
1724 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED
);
1726 * Enable both explicit and implicit ALUA support by default
1728 tg_pt_gp
->tg_pt_gp_alua_access_type
=
1729 TPGS_EXPLICIT_ALUA
| TPGS_IMPLICIT_ALUA
;
1731 * Set the default Active/NonOptimized Delay in milliseconds
1733 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= ALUA_DEFAULT_NONOP_DELAY_MSECS
;
1734 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= ALUA_DEFAULT_TRANS_DELAY_MSECS
;
1735 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= ALUA_DEFAULT_IMPLICIT_TRANS_SECS
;
1738 * Enable all supported states
1740 tg_pt_gp
->tg_pt_gp_alua_supported_states
=
1741 ALUA_T_SUP
| ALUA_O_SUP
|
1742 ALUA_U_SUP
| ALUA_S_SUP
| ALUA_AN_SUP
| ALUA_AO_SUP
;
1745 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1746 tg_pt_gp
->tg_pt_gp_id
=
1747 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1748 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1749 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1750 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1751 &dev
->t10_alua
.tg_pt_gps_list
);
1752 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1758 int core_alua_set_tg_pt_gp_id(
1759 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
1762 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1763 struct t10_alua_tg_pt_gp
*tg_pt_gp_tmp
;
1764 u16 tg_pt_gp_id_tmp
;
1767 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1769 if (tg_pt_gp
->tg_pt_gp_valid_id
) {
1770 pr_warn("ALUA TG PT Group already has a valid ID,"
1771 " ignoring request\n");
1775 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1776 if (dev
->t10_alua
.alua_tg_pt_gps_count
== 0x0000ffff) {
1777 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1778 " 0x0000ffff reached\n");
1779 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1780 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1784 tg_pt_gp_id_tmp
= (tg_pt_gp_id
!= 0) ? tg_pt_gp_id
:
1785 dev
->t10_alua
.alua_tg_pt_gps_counter
++;
1787 list_for_each_entry(tg_pt_gp_tmp
, &dev
->t10_alua
.tg_pt_gps_list
,
1789 if (tg_pt_gp_tmp
->tg_pt_gp_id
== tg_pt_gp_id_tmp
) {
1793 pr_err("ALUA Target Port Group ID: %hu already"
1794 " exists, ignoring request\n", tg_pt_gp_id
);
1795 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1800 tg_pt_gp
->tg_pt_gp_id
= tg_pt_gp_id_tmp
;
1801 tg_pt_gp
->tg_pt_gp_valid_id
= 1;
1802 list_add_tail(&tg_pt_gp
->tg_pt_gp_list
,
1803 &dev
->t10_alua
.tg_pt_gps_list
);
1804 dev
->t10_alua
.alua_tg_pt_gps_count
++;
1805 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1810 struct t10_alua_tg_pt_gp_member
*core_alua_allocate_tg_pt_gp_mem(
1811 struct se_port
*port
)
1813 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1815 tg_pt_gp_mem
= kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache
,
1817 if (!tg_pt_gp_mem
) {
1818 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1819 return ERR_PTR(-ENOMEM
);
1821 INIT_LIST_HEAD(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1822 spin_lock_init(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1823 atomic_set(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
, 0);
1825 tg_pt_gp_mem
->tg_pt
= port
;
1826 port
->sep_alua_tg_pt_gp_mem
= tg_pt_gp_mem
;
1828 return tg_pt_gp_mem
;
1831 void core_alua_free_tg_pt_gp(
1832 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1834 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1835 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
, *tg_pt_gp_mem_tmp
;
1838 * Once we have reached this point, config_item_put() has already
1839 * been called from target_core_alua_drop_tg_pt_gp().
1841 * Here we remove *tg_pt_gp from the global list so that
1842 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1843 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1845 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1846 list_del(&tg_pt_gp
->tg_pt_gp_list
);
1847 dev
->t10_alua
.alua_tg_pt_gps_counter
--;
1848 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1850 flush_delayed_work(&tg_pt_gp
->tg_pt_gp_transition_work
);
1853 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1854 * core_alua_get_tg_pt_gp_by_name() in
1855 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1856 * to be released with core_alua_put_tg_pt_gp_from_name().
1858 while (atomic_read(&tg_pt_gp
->tg_pt_gp_ref_cnt
))
1862 * Release reference to struct t10_alua_tg_pt_gp from all associated
1865 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1866 list_for_each_entry_safe(tg_pt_gp_mem
, tg_pt_gp_mem_tmp
,
1867 &tg_pt_gp
->tg_pt_gp_mem_list
, tg_pt_gp_mem_list
) {
1868 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1869 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1870 tg_pt_gp
->tg_pt_gp_members
--;
1871 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1873 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1875 * tg_pt_gp_mem is associated with a single
1876 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1877 * core_alua_free_tg_pt_gp_mem().
1879 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1880 * assume we want to re-associate a given tg_pt_gp_mem with
1883 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1884 if (tg_pt_gp
!= dev
->t10_alua
.default_tg_pt_gp
) {
1885 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
1886 dev
->t10_alua
.default_tg_pt_gp
);
1888 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1889 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1891 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1893 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1895 kmem_cache_free(t10_alua_tg_pt_gp_cache
, tg_pt_gp
);
1898 void core_alua_free_tg_pt_gp_mem(struct se_port
*port
)
1900 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1901 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1903 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
1907 while (atomic_read(&tg_pt_gp_mem
->tg_pt_gp_mem_ref_cnt
))
1910 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1911 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
1913 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1914 if (tg_pt_gp_mem
->tg_pt_gp_assoc
) {
1915 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1916 tg_pt_gp
->tg_pt_gp_members
--;
1917 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1919 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1920 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1922 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
1924 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache
, tg_pt_gp_mem
);
1927 static struct t10_alua_tg_pt_gp
*core_alua_get_tg_pt_gp_by_name(
1928 struct se_device
*dev
, const char *name
)
1930 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1931 struct config_item
*ci
;
1933 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1934 list_for_each_entry(tg_pt_gp
, &dev
->t10_alua
.tg_pt_gps_list
,
1936 if (!tg_pt_gp
->tg_pt_gp_valid_id
)
1938 ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
1939 if (!strcmp(config_item_name(ci
), name
)) {
1940 atomic_inc(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1941 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1945 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1950 static void core_alua_put_tg_pt_gp_from_name(
1951 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1953 struct se_device
*dev
= tg_pt_gp
->tg_pt_gp_dev
;
1955 spin_lock(&dev
->t10_alua
.tg_pt_gps_lock
);
1956 atomic_dec(&tg_pt_gp
->tg_pt_gp_ref_cnt
);
1957 spin_unlock(&dev
->t10_alua
.tg_pt_gps_lock
);
1961 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1963 void __core_alua_attach_tg_pt_gp_mem(
1964 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1965 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1967 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1968 tg_pt_gp_mem
->tg_pt_gp
= tg_pt_gp
;
1969 tg_pt_gp_mem
->tg_pt_gp_assoc
= 1;
1970 list_add_tail(&tg_pt_gp_mem
->tg_pt_gp_mem_list
,
1971 &tg_pt_gp
->tg_pt_gp_mem_list
);
1972 tg_pt_gp
->tg_pt_gp_members
++;
1973 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1977 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1979 static void __core_alua_drop_tg_pt_gp_mem(
1980 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
,
1981 struct t10_alua_tg_pt_gp
*tg_pt_gp
)
1983 spin_lock(&tg_pt_gp
->tg_pt_gp_lock
);
1984 list_del(&tg_pt_gp_mem
->tg_pt_gp_mem_list
);
1985 tg_pt_gp_mem
->tg_pt_gp
= NULL
;
1986 tg_pt_gp_mem
->tg_pt_gp_assoc
= 0;
1987 tg_pt_gp
->tg_pt_gp_members
--;
1988 spin_unlock(&tg_pt_gp
->tg_pt_gp_lock
);
1991 ssize_t
core_alua_show_tg_pt_gp_info(struct se_port
*port
, char *page
)
1993 struct config_item
*tg_pt_ci
;
1994 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
1995 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
1998 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
2002 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
2003 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
2005 tg_pt_ci
= &tg_pt_gp
->tg_pt_gp_group
.cg_item
;
2006 len
+= sprintf(page
, "TG Port Alias: %s\nTG Port Group ID:"
2007 " %hu\nTG Port Primary Access State: %s\nTG Port "
2008 "Primary Access Status: %s\nTG Port Secondary Access"
2009 " State: %s\nTG Port Secondary Access Status: %s\n",
2010 config_item_name(tg_pt_ci
), tg_pt_gp
->tg_pt_gp_id
,
2011 core_alua_dump_state(atomic_read(
2012 &tg_pt_gp
->tg_pt_gp_alua_access_state
)),
2013 core_alua_dump_status(
2014 tg_pt_gp
->tg_pt_gp_alua_access_status
),
2015 (atomic_read(&port
->sep_tg_pt_secondary_offline
)) ?
2017 core_alua_dump_status(port
->sep_tg_pt_secondary_stat
));
2019 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
2024 ssize_t
core_alua_store_tg_pt_gp_info(
2025 struct se_port
*port
,
2029 struct se_portal_group
*tpg
;
2031 struct se_device
*dev
= port
->sep_lun
->lun_se_dev
;
2032 struct t10_alua_tg_pt_gp
*tg_pt_gp
= NULL
, *tg_pt_gp_new
= NULL
;
2033 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
2034 unsigned char buf
[TG_PT_GROUP_NAME_BUF
];
2037 tpg
= port
->sep_tpg
;
2038 lun
= port
->sep_lun
;
2040 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
2044 if (count
> TG_PT_GROUP_NAME_BUF
) {
2045 pr_err("ALUA Target Port Group alias too large!\n");
2048 memset(buf
, 0, TG_PT_GROUP_NAME_BUF
);
2049 memcpy(buf
, page
, count
);
2051 * Any ALUA target port group alias besides "NULL" means we will be
2052 * making a new group association.
2054 if (strcmp(strstrip(buf
), "NULL")) {
2056 * core_alua_get_tg_pt_gp_by_name() will increment reference to
2057 * struct t10_alua_tg_pt_gp. This reference is released with
2058 * core_alua_put_tg_pt_gp_from_name() below.
2060 tg_pt_gp_new
= core_alua_get_tg_pt_gp_by_name(dev
,
2066 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
2067 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
2070 * Clearing an existing tg_pt_gp association, and replacing
2071 * with the default_tg_pt_gp.
2073 if (!tg_pt_gp_new
) {
2074 pr_debug("Target_Core_ConfigFS: Moving"
2075 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
2076 " alua/%s, ID: %hu back to"
2077 " default_tg_pt_gp\n",
2078 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2079 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2080 config_item_name(&lun
->lun_group
.cg_item
),
2082 &tg_pt_gp
->tg_pt_gp_group
.cg_item
),
2083 tg_pt_gp
->tg_pt_gp_id
);
2085 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
2086 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
,
2087 dev
->t10_alua
.default_tg_pt_gp
);
2088 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
2093 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
2095 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp
);
2099 * Associate tg_pt_gp_mem with tg_pt_gp_new.
2101 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem
, tg_pt_gp_new
);
2102 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
2103 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2104 " Target Port Group: alua/%s, ID: %hu\n", (move
) ?
2105 "Moving" : "Adding", tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
2106 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
2107 config_item_name(&lun
->lun_group
.cg_item
),
2108 config_item_name(&tg_pt_gp_new
->tg_pt_gp_group
.cg_item
),
2109 tg_pt_gp_new
->tg_pt_gp_id
);
2111 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new
);
2115 ssize_t
core_alua_show_access_type(
2116 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2119 if ((tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
) &&
2120 (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
))
2121 return sprintf(page
, "Implicit and Explicit\n");
2122 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_IMPLICIT_ALUA
)
2123 return sprintf(page
, "Implicit\n");
2124 else if (tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)
2125 return sprintf(page
, "Explicit\n");
2127 return sprintf(page
, "None\n");
2130 ssize_t
core_alua_store_access_type(
2131 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2138 ret
= kstrtoul(page
, 0, &tmp
);
2140 pr_err("Unable to extract alua_access_type\n");
2143 if ((tmp
!= 0) && (tmp
!= 1) && (tmp
!= 2) && (tmp
!= 3)) {
2144 pr_err("Illegal value for alua_access_type:"
2149 tg_pt_gp
->tg_pt_gp_alua_access_type
=
2150 TPGS_IMPLICIT_ALUA
| TPGS_EXPLICIT_ALUA
;
2152 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_EXPLICIT_ALUA
;
2154 tg_pt_gp
->tg_pt_gp_alua_access_type
= TPGS_IMPLICIT_ALUA
;
2156 tg_pt_gp
->tg_pt_gp_alua_access_type
= 0;
2161 ssize_t
core_alua_show_nonop_delay_msecs(
2162 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2165 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
);
2168 ssize_t
core_alua_store_nonop_delay_msecs(
2169 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2176 ret
= kstrtoul(page
, 0, &tmp
);
2178 pr_err("Unable to extract nonop_delay_msecs\n");
2181 if (tmp
> ALUA_MAX_NONOP_DELAY_MSECS
) {
2182 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2183 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp
,
2184 ALUA_MAX_NONOP_DELAY_MSECS
);
2187 tg_pt_gp
->tg_pt_gp_nonop_delay_msecs
= (int)tmp
;
2192 ssize_t
core_alua_show_trans_delay_msecs(
2193 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2196 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_trans_delay_msecs
);
2199 ssize_t
core_alua_store_trans_delay_msecs(
2200 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2207 ret
= kstrtoul(page
, 0, &tmp
);
2209 pr_err("Unable to extract trans_delay_msecs\n");
2212 if (tmp
> ALUA_MAX_TRANS_DELAY_MSECS
) {
2213 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2214 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp
,
2215 ALUA_MAX_TRANS_DELAY_MSECS
);
2218 tg_pt_gp
->tg_pt_gp_trans_delay_msecs
= (int)tmp
;
2223 ssize_t
core_alua_show_implicit_trans_secs(
2224 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2227 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_implicit_trans_secs
);
2230 ssize_t
core_alua_store_implicit_trans_secs(
2231 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2238 ret
= kstrtoul(page
, 0, &tmp
);
2240 pr_err("Unable to extract implicit_trans_secs\n");
2243 if (tmp
> ALUA_MAX_IMPLICIT_TRANS_SECS
) {
2244 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2245 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp
,
2246 ALUA_MAX_IMPLICIT_TRANS_SECS
);
2249 tg_pt_gp
->tg_pt_gp_implicit_trans_secs
= (int)tmp
;
2254 ssize_t
core_alua_show_preferred_bit(
2255 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2258 return sprintf(page
, "%d\n", tg_pt_gp
->tg_pt_gp_pref
);
2261 ssize_t
core_alua_store_preferred_bit(
2262 struct t10_alua_tg_pt_gp
*tg_pt_gp
,
2269 ret
= kstrtoul(page
, 0, &tmp
);
2271 pr_err("Unable to extract preferred ALUA value\n");
2274 if ((tmp
!= 0) && (tmp
!= 1)) {
2275 pr_err("Illegal value for preferred ALUA: %lu\n", tmp
);
2278 tg_pt_gp
->tg_pt_gp_pref
= (int)tmp
;
2283 ssize_t
core_alua_show_offline_bit(struct se_lun
*lun
, char *page
)
2288 return sprintf(page
, "%d\n",
2289 atomic_read(&lun
->lun_sep
->sep_tg_pt_secondary_offline
));
2292 ssize_t
core_alua_store_offline_bit(
2297 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
2304 ret
= kstrtoul(page
, 0, &tmp
);
2306 pr_err("Unable to extract alua_tg_pt_offline value\n");
2309 if ((tmp
!= 0) && (tmp
!= 1)) {
2310 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2314 tg_pt_gp_mem
= lun
->lun_sep
->sep_alua_tg_pt_gp_mem
;
2315 if (!tg_pt_gp_mem
) {
2316 pr_err("Unable to locate *tg_pt_gp_mem\n");
2320 ret
= core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem
,
2321 lun
->lun_sep
, 0, (int)tmp
);
2328 ssize_t
core_alua_show_secondary_status(
2332 return sprintf(page
, "%d\n", lun
->lun_sep
->sep_tg_pt_secondary_stat
);
2335 ssize_t
core_alua_store_secondary_status(
2343 ret
= kstrtoul(page
, 0, &tmp
);
2345 pr_err("Unable to extract alua_tg_pt_status\n");
2348 if ((tmp
!= ALUA_STATUS_NONE
) &&
2349 (tmp
!= ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG
) &&
2350 (tmp
!= ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA
)) {
2351 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2355 lun
->lun_sep
->sep_tg_pt_secondary_stat
= (int)tmp
;
2360 ssize_t
core_alua_show_secondary_write_metadata(
2364 return sprintf(page
, "%d\n",
2365 lun
->lun_sep
->sep_tg_pt_secondary_write_md
);
2368 ssize_t
core_alua_store_secondary_write_metadata(
2376 ret
= kstrtoul(page
, 0, &tmp
);
2378 pr_err("Unable to extract alua_tg_pt_write_md\n");
2381 if ((tmp
!= 0) && (tmp
!= 1)) {
2382 pr_err("Illegal value for alua_tg_pt_write_md:"
2386 lun
->lun_sep
->sep_tg_pt_secondary_write_md
= (int)tmp
;
2391 int core_setup_alua(struct se_device
*dev
)
2393 if (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
&&
2394 !(dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)) {
2395 struct t10_alua_lu_gp_member
*lu_gp_mem
;
2398 * Associate this struct se_device with the default ALUA
2401 lu_gp_mem
= core_alua_allocate_lu_gp_mem(dev
);
2402 if (IS_ERR(lu_gp_mem
))
2403 return PTR_ERR(lu_gp_mem
);
2405 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
2406 __core_alua_attach_lu_gp_mem(lu_gp_mem
,
2408 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
2410 pr_debug("%s: Adding to default ALUA LU Group:"
2411 " core/alua/lu_gps/default_lu_gp\n",
2412 dev
->transport
->name
);