xref: /linux/drivers/target/target_core_alua.c (revision 0ad53fe3ae82443c74ff8cfd7bd13377cc1134a3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_alua.c
4  *
5  * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6  *
7  * (c) Copyright 2009-2013 Datera, Inc.
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  ******************************************************************************/
12 
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/configfs.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/fcntl.h>
19 #include <linux/file.h>
20 #include <linux/fs.h>
21 #include <scsi/scsi_proto.h>
22 #include <asm/unaligned.h>
23 
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
27 
28 #include "target_core_internal.h"
29 #include "target_core_alua.h"
30 #include "target_core_ua.h"
31 
32 static sense_reason_t core_alua_check_transition(int state, int valid,
33 						 int *primary, int explicit);
34 static int core_alua_set_tg_pt_secondary_state(
35 		struct se_lun *lun, int explicit, int offline);
36 
37 static char *core_alua_dump_state(int state);
38 
39 static void __target_attach_tg_pt_gp(struct se_lun *lun,
40 		struct t10_alua_tg_pt_gp *tg_pt_gp);
41 
42 static u16 alua_lu_gps_counter;
43 static u32 alua_lu_gps_count;
44 
45 static DEFINE_SPINLOCK(lu_gps_lock);
46 static LIST_HEAD(lu_gps_list);
47 
48 struct t10_alua_lu_gp *default_lu_gp;
49 
50 /*
51  * REPORT REFERRALS
52  *
53  * See sbc3r35 section 5.23
54  */
55 sense_reason_t
56 target_emulate_report_referrals(struct se_cmd *cmd)
57 {
58 	struct se_device *dev = cmd->se_dev;
59 	struct t10_alua_lba_map *map;
60 	struct t10_alua_lba_map_member *map_mem;
61 	unsigned char *buf;
62 	u32 rd_len = 0, off;
63 
64 	if (cmd->data_length < 4) {
65 		pr_warn("REPORT REFERRALS allocation length %u too"
66 			" small\n", cmd->data_length);
67 		return TCM_INVALID_CDB_FIELD;
68 	}
69 
70 	buf = transport_kmap_data_sg(cmd);
71 	if (!buf)
72 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
73 
74 	off = 4;
75 	spin_lock(&dev->t10_alua.lba_map_lock);
76 	if (list_empty(&dev->t10_alua.lba_map_list)) {
77 		spin_unlock(&dev->t10_alua.lba_map_lock);
78 		transport_kunmap_data_sg(cmd);
79 
80 		return TCM_UNSUPPORTED_SCSI_OPCODE;
81 	}
82 
83 	list_for_each_entry(map, &dev->t10_alua.lba_map_list,
84 			    lba_map_list) {
85 		int desc_num = off + 3;
86 		int pg_num;
87 
88 		off += 4;
89 		if (cmd->data_length > off)
90 			put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
91 		off += 8;
92 		if (cmd->data_length > off)
93 			put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
94 		off += 8;
95 		rd_len += 20;
96 		pg_num = 0;
97 		list_for_each_entry(map_mem, &map->lba_map_mem_list,
98 				    lba_map_mem_list) {
99 			int alua_state = map_mem->lba_map_mem_alua_state;
100 			int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
101 
102 			if (cmd->data_length > off)
103 				buf[off] = alua_state & 0x0f;
104 			off += 2;
105 			if (cmd->data_length > off)
106 				buf[off] = (alua_pg_id >> 8) & 0xff;
107 			off++;
108 			if (cmd->data_length > off)
109 				buf[off] = (alua_pg_id & 0xff);
110 			off++;
111 			rd_len += 4;
112 			pg_num++;
113 		}
114 		if (cmd->data_length > desc_num)
115 			buf[desc_num] = pg_num;
116 	}
117 	spin_unlock(&dev->t10_alua.lba_map_lock);
118 
119 	/*
120 	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
121 	 */
122 	put_unaligned_be16(rd_len, &buf[2]);
123 
124 	transport_kunmap_data_sg(cmd);
125 
126 	target_complete_cmd(cmd, SAM_STAT_GOOD);
127 	return 0;
128 }
129 
130 /*
131  * REPORT_TARGET_PORT_GROUPS
132  *
133  * See spc4r17 section 6.27
134  */
135 sense_reason_t
136 target_emulate_report_target_port_groups(struct se_cmd *cmd)
137 {
138 	struct se_device *dev = cmd->se_dev;
139 	struct t10_alua_tg_pt_gp *tg_pt_gp;
140 	struct se_lun *lun;
141 	unsigned char *buf;
142 	u32 rd_len = 0, off;
143 	int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
144 
145 	/*
146 	 * Skip over RESERVED area to first Target port group descriptor
147 	 * depending on the PARAMETER DATA FORMAT type..
148 	 */
149 	if (ext_hdr != 0)
150 		off = 8;
151 	else
152 		off = 4;
153 
154 	if (cmd->data_length < off) {
155 		pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
156 			" small for %s header\n", cmd->data_length,
157 			(ext_hdr) ? "extended" : "normal");
158 		return TCM_INVALID_CDB_FIELD;
159 	}
160 	buf = transport_kmap_data_sg(cmd);
161 	if (!buf)
162 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
163 
164 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
165 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
166 			tg_pt_gp_list) {
167 		/*
168 		 * Check if the Target port group and Target port descriptor list
169 		 * based on tg_pt_gp_members count will fit into the response payload.
170 		 * Otherwise, bump rd_len to let the initiator know we have exceeded
171 		 * the allocation length and the response is truncated.
172 		 */
173 		if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
174 		     cmd->data_length) {
175 			rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
176 			continue;
177 		}
178 		/*
179 		 * PREF: Preferred target port bit, determine if this
180 		 * bit should be set for port group.
181 		 */
182 		if (tg_pt_gp->tg_pt_gp_pref)
183 			buf[off] = 0x80;
184 		/*
185 		 * Set the ASYMMETRIC ACCESS State
186 		 */
187 		buf[off++] |= tg_pt_gp->tg_pt_gp_alua_access_state & 0xff;
188 		/*
189 		 * Set supported ASYMMETRIC ACCESS State bits
190 		 */
191 		buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
192 		/*
193 		 * TARGET PORT GROUP
194 		 */
195 		put_unaligned_be16(tg_pt_gp->tg_pt_gp_id, &buf[off]);
196 		off += 2;
197 
198 		off++; /* Skip over Reserved */
199 		/*
200 		 * STATUS CODE
201 		 */
202 		buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
203 		/*
204 		 * Vendor Specific field
205 		 */
206 		buf[off++] = 0x00;
207 		/*
208 		 * TARGET PORT COUNT
209 		 */
210 		buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
211 		rd_len += 8;
212 
213 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
214 		list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
215 				lun_tg_pt_gp_link) {
216 			/*
217 			 * Start Target Port descriptor format
218 			 *
219 			 * See spc4r17 section 6.2.7 Table 247
220 			 */
221 			off += 2; /* Skip over Obsolete */
222 			/*
223 			 * Set RELATIVE TARGET PORT IDENTIFIER
224 			 */
225 			put_unaligned_be16(lun->lun_rtpi, &buf[off]);
226 			off += 2;
227 			rd_len += 4;
228 		}
229 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
230 	}
231 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
232 	/*
233 	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
234 	 */
235 	put_unaligned_be32(rd_len, &buf[0]);
236 
237 	/*
238 	 * Fill in the Extended header parameter data format if requested
239 	 */
240 	if (ext_hdr != 0) {
241 		buf[4] = 0x10;
242 		/*
243 		 * Set the implicit transition time (in seconds) for the application
244 		 * client to use as a base for it's transition timeout value.
245 		 *
246 		 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
247 		 * this CDB was received upon to determine this value individually
248 		 * for ALUA target port group.
249 		 */
250 		spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
251 		tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
252 		if (tg_pt_gp)
253 			buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
254 		spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
255 	}
256 	transport_kunmap_data_sg(cmd);
257 
258 	target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, rd_len + 4);
259 	return 0;
260 }
261 
262 /*
263  * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
264  *
265  * See spc4r17 section 6.35
266  */
267 sense_reason_t
268 target_emulate_set_target_port_groups(struct se_cmd *cmd)
269 {
270 	struct se_device *dev = cmd->se_dev;
271 	struct se_lun *l_lun = cmd->se_lun;
272 	struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
273 	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
274 	unsigned char *buf;
275 	unsigned char *ptr;
276 	sense_reason_t rc = TCM_NO_SENSE;
277 	u32 len = 4; /* Skip over RESERVED area in header */
278 	int alua_access_state, primary = 0, valid_states;
279 	u16 tg_pt_id, rtpi;
280 
281 	if (cmd->data_length < 4) {
282 		pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
283 			" small\n", cmd->data_length);
284 		return TCM_INVALID_PARAMETER_LIST;
285 	}
286 
287 	buf = transport_kmap_data_sg(cmd);
288 	if (!buf)
289 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
290 
291 	/*
292 	 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
293 	 * for the local tg_pt_gp.
294 	 */
295 	spin_lock(&l_lun->lun_tg_pt_gp_lock);
296 	l_tg_pt_gp = l_lun->lun_tg_pt_gp;
297 	if (!l_tg_pt_gp) {
298 		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
299 		pr_err("Unable to access l_lun->tg_pt_gp\n");
300 		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
301 		goto out;
302 	}
303 
304 	if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
305 		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
306 		pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
307 				" while TPGS_EXPLICIT_ALUA is disabled\n");
308 		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
309 		goto out;
310 	}
311 	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
312 	spin_unlock(&l_lun->lun_tg_pt_gp_lock);
313 
314 	ptr = &buf[4]; /* Skip over RESERVED area in header */
315 
316 	while (len < cmd->data_length) {
317 		bool found = false;
318 		alua_access_state = (ptr[0] & 0x0f);
319 		/*
320 		 * Check the received ALUA access state, and determine if
321 		 * the state is a primary or secondary target port asymmetric
322 		 * access state.
323 		 */
324 		rc = core_alua_check_transition(alua_access_state, valid_states,
325 						&primary, 1);
326 		if (rc) {
327 			/*
328 			 * If the SET TARGET PORT GROUPS attempts to establish
329 			 * an invalid combination of target port asymmetric
330 			 * access states or attempts to establish an
331 			 * unsupported target port asymmetric access state,
332 			 * then the command shall be terminated with CHECK
333 			 * CONDITION status, with the sense key set to ILLEGAL
334 			 * REQUEST, and the additional sense code set to INVALID
335 			 * FIELD IN PARAMETER LIST.
336 			 */
337 			goto out;
338 		}
339 
340 		/*
341 		 * If the ASYMMETRIC ACCESS STATE field (see table 267)
342 		 * specifies a primary target port asymmetric access state,
343 		 * then the TARGET PORT GROUP OR TARGET PORT field specifies
344 		 * a primary target port group for which the primary target
345 		 * port asymmetric access state shall be changed. If the
346 		 * ASYMMETRIC ACCESS STATE field specifies a secondary target
347 		 * port asymmetric access state, then the TARGET PORT GROUP OR
348 		 * TARGET PORT field specifies the relative target port
349 		 * identifier (see 3.1.120) of the target port for which the
350 		 * secondary target port asymmetric access state shall be
351 		 * changed.
352 		 */
353 		if (primary) {
354 			tg_pt_id = get_unaligned_be16(ptr + 2);
355 			/*
356 			 * Locate the matching target port group ID from
357 			 * the global tg_pt_gp list
358 			 */
359 			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
360 			list_for_each_entry(tg_pt_gp,
361 					&dev->t10_alua.tg_pt_gps_list,
362 					tg_pt_gp_list) {
363 				if (!tg_pt_gp->tg_pt_gp_valid_id)
364 					continue;
365 
366 				if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
367 					continue;
368 
369 				atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
370 
371 				spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
372 
373 				if (!core_alua_do_port_transition(tg_pt_gp,
374 						dev, l_lun, nacl,
375 						alua_access_state, 1))
376 					found = true;
377 
378 				spin_lock(&dev->t10_alua.tg_pt_gps_lock);
379 				atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
380 				break;
381 			}
382 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
383 		} else {
384 			struct se_lun *lun;
385 
386 			/*
387 			 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
388 			 * the Target Port in question for the the incoming
389 			 * SET_TARGET_PORT_GROUPS op.
390 			 */
391 			rtpi = get_unaligned_be16(ptr + 2);
392 			/*
393 			 * Locate the matching relative target port identifier
394 			 * for the struct se_device storage object.
395 			 */
396 			spin_lock(&dev->se_port_lock);
397 			list_for_each_entry(lun, &dev->dev_sep_list,
398 							lun_dev_link) {
399 				if (lun->lun_rtpi != rtpi)
400 					continue;
401 
402 				// XXX: racy unlock
403 				spin_unlock(&dev->se_port_lock);
404 
405 				if (!core_alua_set_tg_pt_secondary_state(
406 						lun, 1, 1))
407 					found = true;
408 
409 				spin_lock(&dev->se_port_lock);
410 				break;
411 			}
412 			spin_unlock(&dev->se_port_lock);
413 		}
414 
415 		if (!found) {
416 			rc = TCM_INVALID_PARAMETER_LIST;
417 			goto out;
418 		}
419 
420 		ptr += 4;
421 		len += 4;
422 	}
423 
424 out:
425 	transport_kunmap_data_sg(cmd);
426 	if (!rc)
427 		target_complete_cmd(cmd, SAM_STAT_GOOD);
428 	return rc;
429 }
430 
431 static inline void core_alua_state_nonoptimized(
432 	struct se_cmd *cmd,
433 	unsigned char *cdb,
434 	int nonop_delay_msecs)
435 {
436 	/*
437 	 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
438 	 * later to determine if processing of this cmd needs to be
439 	 * temporarily delayed for the Active/NonOptimized primary access state.
440 	 */
441 	cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
442 	cmd->alua_nonop_delay = nonop_delay_msecs;
443 }
444 
445 static inline sense_reason_t core_alua_state_lba_dependent(
446 	struct se_cmd *cmd,
447 	u16 tg_pt_gp_id)
448 {
449 	struct se_device *dev = cmd->se_dev;
450 	u64 segment_size, segment_mult, sectors, lba;
451 
452 	/* Only need to check for cdb actually containing LBAs */
453 	if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
454 		return 0;
455 
456 	spin_lock(&dev->t10_alua.lba_map_lock);
457 	segment_size = dev->t10_alua.lba_map_segment_size;
458 	segment_mult = dev->t10_alua.lba_map_segment_multiplier;
459 	sectors = cmd->data_length / dev->dev_attrib.block_size;
460 
461 	lba = cmd->t_task_lba;
462 	while (lba < cmd->t_task_lba + sectors) {
463 		struct t10_alua_lba_map *cur_map = NULL, *map;
464 		struct t10_alua_lba_map_member *map_mem;
465 
466 		list_for_each_entry(map, &dev->t10_alua.lba_map_list,
467 				    lba_map_list) {
468 			u64 start_lba, last_lba;
469 			u64 first_lba = map->lba_map_first_lba;
470 
471 			if (segment_mult) {
472 				u64 tmp = lba;
473 				start_lba = do_div(tmp, segment_size * segment_mult);
474 
475 				last_lba = first_lba + segment_size - 1;
476 				if (start_lba >= first_lba &&
477 				    start_lba <= last_lba) {
478 					lba += segment_size;
479 					cur_map = map;
480 					break;
481 				}
482 			} else {
483 				last_lba = map->lba_map_last_lba;
484 				if (lba >= first_lba && lba <= last_lba) {
485 					lba = last_lba + 1;
486 					cur_map = map;
487 					break;
488 				}
489 			}
490 		}
491 		if (!cur_map) {
492 			spin_unlock(&dev->t10_alua.lba_map_lock);
493 			return TCM_ALUA_TG_PT_UNAVAILABLE;
494 		}
495 		list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
496 				    lba_map_mem_list) {
497 			if (map_mem->lba_map_mem_alua_pg_id != tg_pt_gp_id)
498 				continue;
499 			switch(map_mem->lba_map_mem_alua_state) {
500 			case ALUA_ACCESS_STATE_STANDBY:
501 				spin_unlock(&dev->t10_alua.lba_map_lock);
502 				return TCM_ALUA_TG_PT_STANDBY;
503 			case ALUA_ACCESS_STATE_UNAVAILABLE:
504 				spin_unlock(&dev->t10_alua.lba_map_lock);
505 				return TCM_ALUA_TG_PT_UNAVAILABLE;
506 			default:
507 				break;
508 			}
509 		}
510 	}
511 	spin_unlock(&dev->t10_alua.lba_map_lock);
512 	return 0;
513 }
514 
515 static inline sense_reason_t core_alua_state_standby(
516 	struct se_cmd *cmd,
517 	unsigned char *cdb)
518 {
519 	/*
520 	 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
521 	 * spc4r17 section 5.9.2.4.4
522 	 */
523 	switch (cdb[0]) {
524 	case INQUIRY:
525 	case LOG_SELECT:
526 	case LOG_SENSE:
527 	case MODE_SELECT:
528 	case MODE_SENSE:
529 	case REPORT_LUNS:
530 	case RECEIVE_DIAGNOSTIC:
531 	case SEND_DIAGNOSTIC:
532 	case READ_CAPACITY:
533 		return 0;
534 	case SERVICE_ACTION_IN_16:
535 		switch (cdb[1] & 0x1f) {
536 		case SAI_READ_CAPACITY_16:
537 			return 0;
538 		default:
539 			return TCM_ALUA_TG_PT_STANDBY;
540 		}
541 	case MAINTENANCE_IN:
542 		switch (cdb[1] & 0x1f) {
543 		case MI_REPORT_TARGET_PGS:
544 			return 0;
545 		default:
546 			return TCM_ALUA_TG_PT_STANDBY;
547 		}
548 	case MAINTENANCE_OUT:
549 		switch (cdb[1]) {
550 		case MO_SET_TARGET_PGS:
551 			return 0;
552 		default:
553 			return TCM_ALUA_TG_PT_STANDBY;
554 		}
555 	case REQUEST_SENSE:
556 	case PERSISTENT_RESERVE_IN:
557 	case PERSISTENT_RESERVE_OUT:
558 	case READ_BUFFER:
559 	case WRITE_BUFFER:
560 		return 0;
561 	default:
562 		return TCM_ALUA_TG_PT_STANDBY;
563 	}
564 
565 	return 0;
566 }
567 
568 static inline sense_reason_t core_alua_state_unavailable(
569 	struct se_cmd *cmd,
570 	unsigned char *cdb)
571 {
572 	/*
573 	 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
574 	 * spc4r17 section 5.9.2.4.5
575 	 */
576 	switch (cdb[0]) {
577 	case INQUIRY:
578 	case REPORT_LUNS:
579 		return 0;
580 	case MAINTENANCE_IN:
581 		switch (cdb[1] & 0x1f) {
582 		case MI_REPORT_TARGET_PGS:
583 			return 0;
584 		default:
585 			return TCM_ALUA_TG_PT_UNAVAILABLE;
586 		}
587 	case MAINTENANCE_OUT:
588 		switch (cdb[1]) {
589 		case MO_SET_TARGET_PGS:
590 			return 0;
591 		default:
592 			return TCM_ALUA_TG_PT_UNAVAILABLE;
593 		}
594 	case REQUEST_SENSE:
595 	case READ_BUFFER:
596 	case WRITE_BUFFER:
597 		return 0;
598 	default:
599 		return TCM_ALUA_TG_PT_UNAVAILABLE;
600 	}
601 
602 	return 0;
603 }
604 
605 static inline sense_reason_t core_alua_state_transition(
606 	struct se_cmd *cmd,
607 	unsigned char *cdb)
608 {
609 	/*
610 	 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
611 	 * spc4r17 section 5.9.2.5
612 	 */
613 	switch (cdb[0]) {
614 	case INQUIRY:
615 	case REPORT_LUNS:
616 		return 0;
617 	case MAINTENANCE_IN:
618 		switch (cdb[1] & 0x1f) {
619 		case MI_REPORT_TARGET_PGS:
620 			return 0;
621 		default:
622 			return TCM_ALUA_STATE_TRANSITION;
623 		}
624 	case REQUEST_SENSE:
625 	case READ_BUFFER:
626 	case WRITE_BUFFER:
627 		return 0;
628 	default:
629 		return TCM_ALUA_STATE_TRANSITION;
630 	}
631 
632 	return 0;
633 }
634 
635 /*
636  * return 1: Is used to signal LUN not accessible, and check condition/not ready
637  * return 0: Used to signal success
638  * return -1: Used to signal failure, and invalid cdb field
639  */
640 sense_reason_t
641 target_alua_state_check(struct se_cmd *cmd)
642 {
643 	struct se_device *dev = cmd->se_dev;
644 	unsigned char *cdb = cmd->t_task_cdb;
645 	struct se_lun *lun = cmd->se_lun;
646 	struct t10_alua_tg_pt_gp *tg_pt_gp;
647 	int out_alua_state, nonop_delay_msecs;
648 	u16 tg_pt_gp_id;
649 	sense_reason_t rc = TCM_NO_SENSE;
650 
651 	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
652 		return 0;
653 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
654 		return 0;
655 
656 	/*
657 	 * First, check for a struct se_port specific secondary ALUA target port
658 	 * access state: OFFLINE
659 	 */
660 	if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
661 		pr_debug("ALUA: Got secondary offline status for local"
662 				" target port\n");
663 		return TCM_ALUA_OFFLINE;
664 	}
665 
666 	if (!lun->lun_tg_pt_gp)
667 		return 0;
668 
669 	spin_lock(&lun->lun_tg_pt_gp_lock);
670 	tg_pt_gp = lun->lun_tg_pt_gp;
671 	out_alua_state = tg_pt_gp->tg_pt_gp_alua_access_state;
672 	nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
673 	tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
674 
675 	spin_unlock(&lun->lun_tg_pt_gp_lock);
676 	/*
677 	 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
678 	 * statement so the compiler knows explicitly to check this case first.
679 	 * For the Optimized ALUA access state case, we want to process the
680 	 * incoming fabric cmd ASAP..
681 	 */
682 	if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
683 		return 0;
684 
685 	switch (out_alua_state) {
686 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
687 		core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
688 		break;
689 	case ALUA_ACCESS_STATE_STANDBY:
690 		rc = core_alua_state_standby(cmd, cdb);
691 		break;
692 	case ALUA_ACCESS_STATE_UNAVAILABLE:
693 		rc = core_alua_state_unavailable(cmd, cdb);
694 		break;
695 	case ALUA_ACCESS_STATE_TRANSITION:
696 		rc = core_alua_state_transition(cmd, cdb);
697 		break;
698 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
699 		rc = core_alua_state_lba_dependent(cmd, tg_pt_gp_id);
700 		break;
701 	/*
702 	 * OFFLINE is a secondary ALUA target port group access state, that is
703 	 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
704 	 */
705 	case ALUA_ACCESS_STATE_OFFLINE:
706 	default:
707 		pr_err("Unknown ALUA access state: 0x%02x\n",
708 				out_alua_state);
709 		rc = TCM_INVALID_CDB_FIELD;
710 	}
711 
712 	if (rc && rc != TCM_INVALID_CDB_FIELD) {
713 		pr_debug("[%s]: ALUA TG Port not available, "
714 			"SenseKey: NOT_READY, ASC/rc: 0x04/%d\n",
715 			cmd->se_tfo->fabric_name, rc);
716 	}
717 
718 	return rc;
719 }
720 
721 /*
722  * Check implicit and explicit ALUA state change request.
723  */
724 static sense_reason_t
725 core_alua_check_transition(int state, int valid, int *primary, int explicit)
726 {
727 	/*
728 	 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
729 	 * defined as primary target port asymmetric access states.
730 	 */
731 	switch (state) {
732 	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
733 		if (!(valid & ALUA_AO_SUP))
734 			goto not_supported;
735 		*primary = 1;
736 		break;
737 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
738 		if (!(valid & ALUA_AN_SUP))
739 			goto not_supported;
740 		*primary = 1;
741 		break;
742 	case ALUA_ACCESS_STATE_STANDBY:
743 		if (!(valid & ALUA_S_SUP))
744 			goto not_supported;
745 		*primary = 1;
746 		break;
747 	case ALUA_ACCESS_STATE_UNAVAILABLE:
748 		if (!(valid & ALUA_U_SUP))
749 			goto not_supported;
750 		*primary = 1;
751 		break;
752 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
753 		if (!(valid & ALUA_LBD_SUP))
754 			goto not_supported;
755 		*primary = 1;
756 		break;
757 	case ALUA_ACCESS_STATE_OFFLINE:
758 		/*
759 		 * OFFLINE state is defined as a secondary target port
760 		 * asymmetric access state.
761 		 */
762 		if (!(valid & ALUA_O_SUP))
763 			goto not_supported;
764 		*primary = 0;
765 		break;
766 	case ALUA_ACCESS_STATE_TRANSITION:
767 		if (!(valid & ALUA_T_SUP) || explicit)
768 			/*
769 			 * Transitioning is set internally and by tcmu daemon,
770 			 * and cannot be selected through a STPG.
771 			 */
772 			goto not_supported;
773 		*primary = 0;
774 		break;
775 	default:
776 		pr_err("Unknown ALUA access state: 0x%02x\n", state);
777 		return TCM_INVALID_PARAMETER_LIST;
778 	}
779 
780 	return 0;
781 
782 not_supported:
783 	pr_err("ALUA access state %s not supported",
784 	       core_alua_dump_state(state));
785 	return TCM_INVALID_PARAMETER_LIST;
786 }
787 
788 static char *core_alua_dump_state(int state)
789 {
790 	switch (state) {
791 	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
792 		return "Active/Optimized";
793 	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
794 		return "Active/NonOptimized";
795 	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
796 		return "LBA Dependent";
797 	case ALUA_ACCESS_STATE_STANDBY:
798 		return "Standby";
799 	case ALUA_ACCESS_STATE_UNAVAILABLE:
800 		return "Unavailable";
801 	case ALUA_ACCESS_STATE_OFFLINE:
802 		return "Offline";
803 	case ALUA_ACCESS_STATE_TRANSITION:
804 		return "Transitioning";
805 	default:
806 		return "Unknown";
807 	}
808 
809 	return NULL;
810 }
811 
812 char *core_alua_dump_status(int status)
813 {
814 	switch (status) {
815 	case ALUA_STATUS_NONE:
816 		return "None";
817 	case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
818 		return "Altered by Explicit STPG";
819 	case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
820 		return "Altered by Implicit ALUA";
821 	default:
822 		return "Unknown";
823 	}
824 
825 	return NULL;
826 }
827 
828 /*
829  * Used by fabric modules to determine when we need to delay processing
830  * for the Active/NonOptimized paths..
831  */
832 int core_alua_check_nonop_delay(
833 	struct se_cmd *cmd)
834 {
835 	if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
836 		return 0;
837 	/*
838 	 * The ALUA Active/NonOptimized access state delay can be disabled
839 	 * in via configfs with a value of zero
840 	 */
841 	if (!cmd->alua_nonop_delay)
842 		return 0;
843 	/*
844 	 * struct se_cmd->alua_nonop_delay gets set by a target port group
845 	 * defined interval in core_alua_state_nonoptimized()
846 	 */
847 	msleep_interruptible(cmd->alua_nonop_delay);
848 	return 0;
849 }
850 EXPORT_SYMBOL(core_alua_check_nonop_delay);
851 
852 static int core_alua_write_tpg_metadata(
853 	const char *path,
854 	unsigned char *md_buf,
855 	u32 md_buf_len)
856 {
857 	struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
858 	loff_t pos = 0;
859 	int ret;
860 
861 	if (IS_ERR(file)) {
862 		pr_err("filp_open(%s) for ALUA metadata failed\n", path);
863 		return -ENODEV;
864 	}
865 	ret = kernel_write(file, md_buf, md_buf_len, &pos);
866 	if (ret < 0)
867 		pr_err("Error writing ALUA metadata file: %s\n", path);
868 	fput(file);
869 	return (ret < 0) ? -EIO : 0;
870 }
871 
872 static int core_alua_update_tpg_primary_metadata(
873 	struct t10_alua_tg_pt_gp *tg_pt_gp)
874 {
875 	unsigned char *md_buf;
876 	struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
877 	char *path;
878 	int len, rc;
879 
880 	lockdep_assert_held(&tg_pt_gp->tg_pt_gp_transition_mutex);
881 
882 	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
883 	if (!md_buf) {
884 		pr_err("Unable to allocate buf for ALUA metadata\n");
885 		return -ENOMEM;
886 	}
887 
888 	len = snprintf(md_buf, ALUA_MD_BUF_LEN,
889 			"tg_pt_gp_id=%hu\n"
890 			"alua_access_state=0x%02x\n"
891 			"alua_access_status=0x%02x\n",
892 			tg_pt_gp->tg_pt_gp_id,
893 			tg_pt_gp->tg_pt_gp_alua_access_state,
894 			tg_pt_gp->tg_pt_gp_alua_access_status);
895 
896 	rc = -ENOMEM;
897 	path = kasprintf(GFP_KERNEL, "%s/alua/tpgs_%s/%s", db_root,
898 			&wwn->unit_serial[0],
899 			config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
900 	if (path) {
901 		rc = core_alua_write_tpg_metadata(path, md_buf, len);
902 		kfree(path);
903 	}
904 	kfree(md_buf);
905 	return rc;
906 }
907 
908 static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
909 {
910 	struct se_dev_entry *se_deve;
911 	struct se_lun *lun;
912 	struct se_lun_acl *lacl;
913 
914 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
915 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
916 				lun_tg_pt_gp_link) {
917 		/*
918 		 * After an implicit target port asymmetric access state
919 		 * change, a device server shall establish a unit attention
920 		 * condition for the initiator port associated with every I_T
921 		 * nexus with the additional sense code set to ASYMMETRIC
922 		 * ACCESS STATE CHANGED.
923 		 *
924 		 * After an explicit target port asymmetric access state
925 		 * change, a device server shall establish a unit attention
926 		 * condition with the additional sense code set to ASYMMETRIC
927 		 * ACCESS STATE CHANGED for the initiator port associated with
928 		 * every I_T nexus other than the I_T nexus on which the SET
929 		 * TARGET PORT GROUPS command
930 		 */
931 		if (!percpu_ref_tryget_live(&lun->lun_ref))
932 			continue;
933 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
934 
935 		spin_lock(&lun->lun_deve_lock);
936 		list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
937 			lacl = rcu_dereference_check(se_deve->se_lun_acl,
938 					lockdep_is_held(&lun->lun_deve_lock));
939 
940 			/*
941 			 * spc4r37 p.242:
942 			 * After an explicit target port asymmetric access
943 			 * state change, a device server shall establish a
944 			 * unit attention condition with the additional sense
945 			 * code set to ASYMMETRIC ACCESS STATE CHANGED for
946 			 * the initiator port associated with every I_T nexus
947 			 * other than the I_T nexus on which the SET TARGET
948 			 * PORT GROUPS command was received.
949 			 */
950 			if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
951 			     ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
952 			   (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
953 			    (tg_pt_gp->tg_pt_gp_alua_lun == lun))
954 				continue;
955 
956 			/*
957 			 * se_deve->se_lun_acl pointer may be NULL for a
958 			 * entry created without explicit Node+MappedLUN ACLs
959 			 */
960 			if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
961 			    (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
962 				continue;
963 
964 			core_scsi3_ua_allocate(se_deve, 0x2A,
965 				ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
966 		}
967 		spin_unlock(&lun->lun_deve_lock);
968 
969 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
970 		percpu_ref_put(&lun->lun_ref);
971 	}
972 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
973 }
974 
975 static int core_alua_do_transition_tg_pt(
976 	struct t10_alua_tg_pt_gp *tg_pt_gp,
977 	int new_state,
978 	int explicit)
979 {
980 	int prev_state;
981 
982 	mutex_lock(&tg_pt_gp->tg_pt_gp_transition_mutex);
983 	/* Nothing to be done here */
984 	if (tg_pt_gp->tg_pt_gp_alua_access_state == new_state) {
985 		mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
986 		return 0;
987 	}
988 
989 	if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION) {
990 		mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
991 		return -EAGAIN;
992 	}
993 
994 	/*
995 	 * Save the old primary ALUA access state, and set the current state
996 	 * to ALUA_ACCESS_STATE_TRANSITION.
997 	 */
998 	prev_state = tg_pt_gp->tg_pt_gp_alua_access_state;
999 	tg_pt_gp->tg_pt_gp_alua_access_state = ALUA_ACCESS_STATE_TRANSITION;
1000 	tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1001 				ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1002 				ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1003 
1004 	core_alua_queue_state_change_ua(tg_pt_gp);
1005 
1006 	if (new_state == ALUA_ACCESS_STATE_TRANSITION) {
1007 		mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1008 		return 0;
1009 	}
1010 
1011 	/*
1012 	 * Check for the optional ALUA primary state transition delay
1013 	 */
1014 	if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1015 		msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1016 
1017 	/*
1018 	 * Set the current primary ALUA access state to the requested new state
1019 	 */
1020 	tg_pt_gp->tg_pt_gp_alua_access_state = new_state;
1021 
1022 	/*
1023 	 * Update the ALUA metadata buf that has been allocated in
1024 	 * core_alua_do_port_transition(), this metadata will be written
1025 	 * to struct file.
1026 	 *
1027 	 * Note that there is the case where we do not want to update the
1028 	 * metadata when the saved metadata is being parsed in userspace
1029 	 * when setting the existing port access state and access status.
1030 	 *
1031 	 * Also note that the failure to write out the ALUA metadata to
1032 	 * struct file does NOT affect the actual ALUA transition.
1033 	 */
1034 	if (tg_pt_gp->tg_pt_gp_write_metadata) {
1035 		core_alua_update_tpg_primary_metadata(tg_pt_gp);
1036 	}
1037 
1038 	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1039 		" from primary access state %s to %s\n", (explicit) ? "explicit" :
1040 		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1041 		tg_pt_gp->tg_pt_gp_id,
1042 		core_alua_dump_state(prev_state),
1043 		core_alua_dump_state(new_state));
1044 
1045 	core_alua_queue_state_change_ua(tg_pt_gp);
1046 
1047 	mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1048 	return 0;
1049 }
1050 
1051 int core_alua_do_port_transition(
1052 	struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1053 	struct se_device *l_dev,
1054 	struct se_lun *l_lun,
1055 	struct se_node_acl *l_nacl,
1056 	int new_state,
1057 	int explicit)
1058 {
1059 	struct se_device *dev;
1060 	struct t10_alua_lu_gp *lu_gp;
1061 	struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1062 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1063 	int primary, valid_states, rc = 0;
1064 
1065 	if (l_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
1066 		return -ENODEV;
1067 
1068 	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1069 	if (core_alua_check_transition(new_state, valid_states, &primary,
1070 				       explicit) != 0)
1071 		return -EINVAL;
1072 
1073 	local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1074 	spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1075 	lu_gp = local_lu_gp_mem->lu_gp;
1076 	atomic_inc(&lu_gp->lu_gp_ref_cnt);
1077 	spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1078 	/*
1079 	 * For storage objects that are members of the 'default_lu_gp',
1080 	 * we only do transition on the passed *l_tp_pt_gp, and not
1081 	 * on all of the matching target port groups IDs in default_lu_gp.
1082 	 */
1083 	if (!lu_gp->lu_gp_id) {
1084 		/*
1085 		 * core_alua_do_transition_tg_pt() will always return
1086 		 * success.
1087 		 */
1088 		l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1089 		l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1090 		rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1091 						   new_state, explicit);
1092 		atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1093 		return rc;
1094 	}
1095 	/*
1096 	 * For all other LU groups aside from 'default_lu_gp', walk all of
1097 	 * the associated storage objects looking for a matching target port
1098 	 * group ID from the local target port group.
1099 	 */
1100 	spin_lock(&lu_gp->lu_gp_lock);
1101 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1102 				lu_gp_mem_list) {
1103 
1104 		dev = lu_gp_mem->lu_gp_mem_dev;
1105 		atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1106 		spin_unlock(&lu_gp->lu_gp_lock);
1107 
1108 		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1109 		list_for_each_entry(tg_pt_gp,
1110 				&dev->t10_alua.tg_pt_gps_list,
1111 				tg_pt_gp_list) {
1112 
1113 			if (!tg_pt_gp->tg_pt_gp_valid_id)
1114 				continue;
1115 			/*
1116 			 * If the target behavior port asymmetric access state
1117 			 * is changed for any target port group accessible via
1118 			 * a logical unit within a LU group, the target port
1119 			 * behavior group asymmetric access states for the same
1120 			 * target port group accessible via other logical units
1121 			 * in that LU group will also change.
1122 			 */
1123 			if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1124 				continue;
1125 
1126 			if (l_tg_pt_gp == tg_pt_gp) {
1127 				tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1128 				tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1129 			} else {
1130 				tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1131 				tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1132 			}
1133 			atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1134 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1135 			/*
1136 			 * core_alua_do_transition_tg_pt() will always return
1137 			 * success.
1138 			 */
1139 			rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1140 					new_state, explicit);
1141 
1142 			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1143 			atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1144 			if (rc)
1145 				break;
1146 		}
1147 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1148 
1149 		spin_lock(&lu_gp->lu_gp_lock);
1150 		atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1151 	}
1152 	spin_unlock(&lu_gp->lu_gp_lock);
1153 
1154 	if (!rc) {
1155 		pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1156 			 " Group IDs: %hu %s transition to primary state: %s\n",
1157 			 config_item_name(&lu_gp->lu_gp_group.cg_item),
1158 			 l_tg_pt_gp->tg_pt_gp_id,
1159 			 (explicit) ? "explicit" : "implicit",
1160 			 core_alua_dump_state(new_state));
1161 	}
1162 
1163 	atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1164 	return rc;
1165 }
1166 
1167 static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1168 {
1169 	struct se_portal_group *se_tpg = lun->lun_tpg;
1170 	unsigned char *md_buf;
1171 	char *path;
1172 	int len, rc;
1173 
1174 	mutex_lock(&lun->lun_tg_pt_md_mutex);
1175 
1176 	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1177 	if (!md_buf) {
1178 		pr_err("Unable to allocate buf for ALUA metadata\n");
1179 		rc = -ENOMEM;
1180 		goto out_unlock;
1181 	}
1182 
1183 	len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1184 			"alua_tg_pt_status=0x%02x\n",
1185 			atomic_read(&lun->lun_tg_pt_secondary_offline),
1186 			lun->lun_tg_pt_secondary_stat);
1187 
1188 	if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL) {
1189 		path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s+%hu/lun_%llu",
1190 				db_root, se_tpg->se_tpg_tfo->fabric_name,
1191 				se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1192 				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg),
1193 				lun->unpacked_lun);
1194 	} else {
1195 		path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s/lun_%llu",
1196 				db_root, se_tpg->se_tpg_tfo->fabric_name,
1197 				se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1198 				lun->unpacked_lun);
1199 	}
1200 	if (!path) {
1201 		rc = -ENOMEM;
1202 		goto out_free;
1203 	}
1204 
1205 	rc = core_alua_write_tpg_metadata(path, md_buf, len);
1206 	kfree(path);
1207 out_free:
1208 	kfree(md_buf);
1209 out_unlock:
1210 	mutex_unlock(&lun->lun_tg_pt_md_mutex);
1211 	return rc;
1212 }
1213 
1214 static int core_alua_set_tg_pt_secondary_state(
1215 	struct se_lun *lun,
1216 	int explicit,
1217 	int offline)
1218 {
1219 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1220 	int trans_delay_msecs;
1221 
1222 	spin_lock(&lun->lun_tg_pt_gp_lock);
1223 	tg_pt_gp = lun->lun_tg_pt_gp;
1224 	if (!tg_pt_gp) {
1225 		spin_unlock(&lun->lun_tg_pt_gp_lock);
1226 		pr_err("Unable to complete secondary state"
1227 				" transition\n");
1228 		return -EINVAL;
1229 	}
1230 	trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1231 	/*
1232 	 * Set the secondary ALUA target port access state to OFFLINE
1233 	 * or release the previously secondary state for struct se_lun
1234 	 */
1235 	if (offline)
1236 		atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1237 	else
1238 		atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1239 
1240 	lun->lun_tg_pt_secondary_stat = (explicit) ?
1241 			ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1242 			ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1243 
1244 	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1245 		" to secondary access state: %s\n", (explicit) ? "explicit" :
1246 		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1247 		tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1248 
1249 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1250 	/*
1251 	 * Do the optional transition delay after we set the secondary
1252 	 * ALUA access state.
1253 	 */
1254 	if (trans_delay_msecs != 0)
1255 		msleep_interruptible(trans_delay_msecs);
1256 	/*
1257 	 * See if we need to update the ALUA fabric port metadata for
1258 	 * secondary state and status
1259 	 */
1260 	if (lun->lun_tg_pt_secondary_write_md)
1261 		core_alua_update_tpg_secondary_metadata(lun);
1262 
1263 	return 0;
1264 }
1265 
1266 struct t10_alua_lba_map *
1267 core_alua_allocate_lba_map(struct list_head *list,
1268 			   u64 first_lba, u64 last_lba)
1269 {
1270 	struct t10_alua_lba_map *lba_map;
1271 
1272 	lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1273 	if (!lba_map) {
1274 		pr_err("Unable to allocate struct t10_alua_lba_map\n");
1275 		return ERR_PTR(-ENOMEM);
1276 	}
1277 	INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1278 	lba_map->lba_map_first_lba = first_lba;
1279 	lba_map->lba_map_last_lba = last_lba;
1280 
1281 	list_add_tail(&lba_map->lba_map_list, list);
1282 	return lba_map;
1283 }
1284 
1285 int
1286 core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1287 			       int pg_id, int state)
1288 {
1289 	struct t10_alua_lba_map_member *lba_map_mem;
1290 
1291 	list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1292 			    lba_map_mem_list) {
1293 		if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1294 			pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1295 			return -EINVAL;
1296 		}
1297 	}
1298 
1299 	lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1300 	if (!lba_map_mem) {
1301 		pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1302 		return -ENOMEM;
1303 	}
1304 	lba_map_mem->lba_map_mem_alua_state = state;
1305 	lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1306 
1307 	list_add_tail(&lba_map_mem->lba_map_mem_list,
1308 		      &lba_map->lba_map_mem_list);
1309 	return 0;
1310 }
1311 
1312 void
1313 core_alua_free_lba_map(struct list_head *lba_list)
1314 {
1315 	struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1316 	struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1317 
1318 	list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1319 				 lba_map_list) {
1320 		list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1321 					 &lba_map->lba_map_mem_list,
1322 					 lba_map_mem_list) {
1323 			list_del(&lba_map_mem->lba_map_mem_list);
1324 			kmem_cache_free(t10_alua_lba_map_mem_cache,
1325 					lba_map_mem);
1326 		}
1327 		list_del(&lba_map->lba_map_list);
1328 		kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1329 	}
1330 }
1331 
1332 void
1333 core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1334 		      int segment_size, int segment_mult)
1335 {
1336 	struct list_head old_lba_map_list;
1337 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1338 	int activate = 0, supported;
1339 
1340 	INIT_LIST_HEAD(&old_lba_map_list);
1341 	spin_lock(&dev->t10_alua.lba_map_lock);
1342 	dev->t10_alua.lba_map_segment_size = segment_size;
1343 	dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1344 	list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1345 	if (lba_map_list) {
1346 		list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1347 		activate = 1;
1348 	}
1349 	spin_unlock(&dev->t10_alua.lba_map_lock);
1350 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1351 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1352 			    tg_pt_gp_list) {
1353 
1354 		if (!tg_pt_gp->tg_pt_gp_valid_id)
1355 			continue;
1356 		supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1357 		if (activate)
1358 			supported |= ALUA_LBD_SUP;
1359 		else
1360 			supported &= ~ALUA_LBD_SUP;
1361 		tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1362 	}
1363 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1364 	core_alua_free_lba_map(&old_lba_map_list);
1365 }
1366 
1367 struct t10_alua_lu_gp *
1368 core_alua_allocate_lu_gp(const char *name, int def_group)
1369 {
1370 	struct t10_alua_lu_gp *lu_gp;
1371 
1372 	lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1373 	if (!lu_gp) {
1374 		pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1375 		return ERR_PTR(-ENOMEM);
1376 	}
1377 	INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1378 	INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1379 	spin_lock_init(&lu_gp->lu_gp_lock);
1380 	atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1381 
1382 	if (def_group) {
1383 		lu_gp->lu_gp_id = alua_lu_gps_counter++;
1384 		lu_gp->lu_gp_valid_id = 1;
1385 		alua_lu_gps_count++;
1386 	}
1387 
1388 	return lu_gp;
1389 }
1390 
1391 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1392 {
1393 	struct t10_alua_lu_gp *lu_gp_tmp;
1394 	u16 lu_gp_id_tmp;
1395 	/*
1396 	 * The lu_gp->lu_gp_id may only be set once..
1397 	 */
1398 	if (lu_gp->lu_gp_valid_id) {
1399 		pr_warn("ALUA LU Group already has a valid ID,"
1400 			" ignoring request\n");
1401 		return -EINVAL;
1402 	}
1403 
1404 	spin_lock(&lu_gps_lock);
1405 	if (alua_lu_gps_count == 0x0000ffff) {
1406 		pr_err("Maximum ALUA alua_lu_gps_count:"
1407 				" 0x0000ffff reached\n");
1408 		spin_unlock(&lu_gps_lock);
1409 		kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1410 		return -ENOSPC;
1411 	}
1412 again:
1413 	lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1414 				alua_lu_gps_counter++;
1415 
1416 	list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1417 		if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1418 			if (!lu_gp_id)
1419 				goto again;
1420 
1421 			pr_warn("ALUA Logical Unit Group ID: %hu"
1422 				" already exists, ignoring request\n",
1423 				lu_gp_id);
1424 			spin_unlock(&lu_gps_lock);
1425 			return -EINVAL;
1426 		}
1427 	}
1428 
1429 	lu_gp->lu_gp_id = lu_gp_id_tmp;
1430 	lu_gp->lu_gp_valid_id = 1;
1431 	list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1432 	alua_lu_gps_count++;
1433 	spin_unlock(&lu_gps_lock);
1434 
1435 	return 0;
1436 }
1437 
1438 static struct t10_alua_lu_gp_member *
1439 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1440 {
1441 	struct t10_alua_lu_gp_member *lu_gp_mem;
1442 
1443 	lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1444 	if (!lu_gp_mem) {
1445 		pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1446 		return ERR_PTR(-ENOMEM);
1447 	}
1448 	INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1449 	spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1450 	atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1451 
1452 	lu_gp_mem->lu_gp_mem_dev = dev;
1453 	dev->dev_alua_lu_gp_mem = lu_gp_mem;
1454 
1455 	return lu_gp_mem;
1456 }
1457 
1458 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1459 {
1460 	struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1461 	/*
1462 	 * Once we have reached this point, config_item_put() has
1463 	 * already been called from target_core_alua_drop_lu_gp().
1464 	 *
1465 	 * Here, we remove the *lu_gp from the global list so that
1466 	 * no associations can be made while we are releasing
1467 	 * struct t10_alua_lu_gp.
1468 	 */
1469 	spin_lock(&lu_gps_lock);
1470 	list_del(&lu_gp->lu_gp_node);
1471 	alua_lu_gps_count--;
1472 	spin_unlock(&lu_gps_lock);
1473 	/*
1474 	 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1475 	 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1476 	 * released with core_alua_put_lu_gp_from_name()
1477 	 */
1478 	while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1479 		cpu_relax();
1480 	/*
1481 	 * Release reference to struct t10_alua_lu_gp * from all associated
1482 	 * struct se_device.
1483 	 */
1484 	spin_lock(&lu_gp->lu_gp_lock);
1485 	list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1486 				&lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1487 		if (lu_gp_mem->lu_gp_assoc) {
1488 			list_del(&lu_gp_mem->lu_gp_mem_list);
1489 			lu_gp->lu_gp_members--;
1490 			lu_gp_mem->lu_gp_assoc = 0;
1491 		}
1492 		spin_unlock(&lu_gp->lu_gp_lock);
1493 		/*
1494 		 *
1495 		 * lu_gp_mem is associated with a single
1496 		 * struct se_device->dev_alua_lu_gp_mem, and is released when
1497 		 * struct se_device is released via core_alua_free_lu_gp_mem().
1498 		 *
1499 		 * If the passed lu_gp does NOT match the default_lu_gp, assume
1500 		 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1501 		 */
1502 		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1503 		if (lu_gp != default_lu_gp)
1504 			__core_alua_attach_lu_gp_mem(lu_gp_mem,
1505 					default_lu_gp);
1506 		else
1507 			lu_gp_mem->lu_gp = NULL;
1508 		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1509 
1510 		spin_lock(&lu_gp->lu_gp_lock);
1511 	}
1512 	spin_unlock(&lu_gp->lu_gp_lock);
1513 
1514 	kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1515 }
1516 
1517 void core_alua_free_lu_gp_mem(struct se_device *dev)
1518 {
1519 	struct t10_alua_lu_gp *lu_gp;
1520 	struct t10_alua_lu_gp_member *lu_gp_mem;
1521 
1522 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1523 	if (!lu_gp_mem)
1524 		return;
1525 
1526 	while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1527 		cpu_relax();
1528 
1529 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1530 	lu_gp = lu_gp_mem->lu_gp;
1531 	if (lu_gp) {
1532 		spin_lock(&lu_gp->lu_gp_lock);
1533 		if (lu_gp_mem->lu_gp_assoc) {
1534 			list_del(&lu_gp_mem->lu_gp_mem_list);
1535 			lu_gp->lu_gp_members--;
1536 			lu_gp_mem->lu_gp_assoc = 0;
1537 		}
1538 		spin_unlock(&lu_gp->lu_gp_lock);
1539 		lu_gp_mem->lu_gp = NULL;
1540 	}
1541 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1542 
1543 	kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1544 }
1545 
1546 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1547 {
1548 	struct t10_alua_lu_gp *lu_gp;
1549 	struct config_item *ci;
1550 
1551 	spin_lock(&lu_gps_lock);
1552 	list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1553 		if (!lu_gp->lu_gp_valid_id)
1554 			continue;
1555 		ci = &lu_gp->lu_gp_group.cg_item;
1556 		if (!strcmp(config_item_name(ci), name)) {
1557 			atomic_inc(&lu_gp->lu_gp_ref_cnt);
1558 			spin_unlock(&lu_gps_lock);
1559 			return lu_gp;
1560 		}
1561 	}
1562 	spin_unlock(&lu_gps_lock);
1563 
1564 	return NULL;
1565 }
1566 
1567 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1568 {
1569 	spin_lock(&lu_gps_lock);
1570 	atomic_dec(&lu_gp->lu_gp_ref_cnt);
1571 	spin_unlock(&lu_gps_lock);
1572 }
1573 
1574 /*
1575  * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1576  */
1577 void __core_alua_attach_lu_gp_mem(
1578 	struct t10_alua_lu_gp_member *lu_gp_mem,
1579 	struct t10_alua_lu_gp *lu_gp)
1580 {
1581 	spin_lock(&lu_gp->lu_gp_lock);
1582 	lu_gp_mem->lu_gp = lu_gp;
1583 	lu_gp_mem->lu_gp_assoc = 1;
1584 	list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1585 	lu_gp->lu_gp_members++;
1586 	spin_unlock(&lu_gp->lu_gp_lock);
1587 }
1588 
1589 /*
1590  * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1591  */
1592 void __core_alua_drop_lu_gp_mem(
1593 	struct t10_alua_lu_gp_member *lu_gp_mem,
1594 	struct t10_alua_lu_gp *lu_gp)
1595 {
1596 	spin_lock(&lu_gp->lu_gp_lock);
1597 	list_del(&lu_gp_mem->lu_gp_mem_list);
1598 	lu_gp_mem->lu_gp = NULL;
1599 	lu_gp_mem->lu_gp_assoc = 0;
1600 	lu_gp->lu_gp_members--;
1601 	spin_unlock(&lu_gp->lu_gp_lock);
1602 }
1603 
1604 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1605 		const char *name, int def_group)
1606 {
1607 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1608 
1609 	tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1610 	if (!tg_pt_gp) {
1611 		pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1612 		return NULL;
1613 	}
1614 	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1615 	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
1616 	mutex_init(&tg_pt_gp->tg_pt_gp_transition_mutex);
1617 	spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1618 	atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1619 	tg_pt_gp->tg_pt_gp_dev = dev;
1620 	tg_pt_gp->tg_pt_gp_alua_access_state =
1621 			ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1622 	/*
1623 	 * Enable both explicit and implicit ALUA support by default
1624 	 */
1625 	tg_pt_gp->tg_pt_gp_alua_access_type =
1626 			TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
1627 	/*
1628 	 * Set the default Active/NonOptimized Delay in milliseconds
1629 	 */
1630 	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1631 	tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1632 	tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
1633 
1634 	/*
1635 	 * Enable all supported states
1636 	 */
1637 	tg_pt_gp->tg_pt_gp_alua_supported_states =
1638 	    ALUA_T_SUP | ALUA_O_SUP |
1639 	    ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1640 
1641 	if (def_group) {
1642 		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1643 		tg_pt_gp->tg_pt_gp_id =
1644 				dev->t10_alua.alua_tg_pt_gps_counter++;
1645 		tg_pt_gp->tg_pt_gp_valid_id = 1;
1646 		dev->t10_alua.alua_tg_pt_gps_count++;
1647 		list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1648 			      &dev->t10_alua.tg_pt_gps_list);
1649 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1650 	}
1651 
1652 	return tg_pt_gp;
1653 }
1654 
1655 int core_alua_set_tg_pt_gp_id(
1656 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1657 	u16 tg_pt_gp_id)
1658 {
1659 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1660 	struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1661 	u16 tg_pt_gp_id_tmp;
1662 
1663 	/*
1664 	 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1665 	 */
1666 	if (tg_pt_gp->tg_pt_gp_valid_id) {
1667 		pr_warn("ALUA TG PT Group already has a valid ID,"
1668 			" ignoring request\n");
1669 		return -EINVAL;
1670 	}
1671 
1672 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1673 	if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1674 		pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1675 			" 0x0000ffff reached\n");
1676 		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1677 		kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1678 		return -ENOSPC;
1679 	}
1680 again:
1681 	tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1682 			dev->t10_alua.alua_tg_pt_gps_counter++;
1683 
1684 	list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1685 			tg_pt_gp_list) {
1686 		if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1687 			if (!tg_pt_gp_id)
1688 				goto again;
1689 
1690 			pr_err("ALUA Target Port Group ID: %hu already"
1691 				" exists, ignoring request\n", tg_pt_gp_id);
1692 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1693 			return -EINVAL;
1694 		}
1695 	}
1696 
1697 	tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1698 	tg_pt_gp->tg_pt_gp_valid_id = 1;
1699 	list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1700 			&dev->t10_alua.tg_pt_gps_list);
1701 	dev->t10_alua.alua_tg_pt_gps_count++;
1702 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1703 
1704 	return 0;
1705 }
1706 
1707 void core_alua_free_tg_pt_gp(
1708 	struct t10_alua_tg_pt_gp *tg_pt_gp)
1709 {
1710 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1711 	struct se_lun *lun, *next;
1712 
1713 	/*
1714 	 * Once we have reached this point, config_item_put() has already
1715 	 * been called from target_core_alua_drop_tg_pt_gp().
1716 	 *
1717 	 * Here we remove *tg_pt_gp from the global list so that
1718 	 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1719 	 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1720 	 */
1721 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1722 	if (tg_pt_gp->tg_pt_gp_valid_id) {
1723 		list_del(&tg_pt_gp->tg_pt_gp_list);
1724 		dev->t10_alua.alua_tg_pt_gps_count--;
1725 	}
1726 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1727 
1728 	/*
1729 	 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1730 	 * core_alua_get_tg_pt_gp_by_name() in
1731 	 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1732 	 * to be released with core_alua_put_tg_pt_gp_from_name().
1733 	 */
1734 	while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1735 		cpu_relax();
1736 
1737 	/*
1738 	 * Release reference to struct t10_alua_tg_pt_gp from all associated
1739 	 * struct se_port.
1740 	 */
1741 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1742 	list_for_each_entry_safe(lun, next,
1743 			&tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1744 		list_del_init(&lun->lun_tg_pt_gp_link);
1745 		tg_pt_gp->tg_pt_gp_members--;
1746 
1747 		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1748 		/*
1749 		 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1750 		 * assume we want to re-associate a given tg_pt_gp_mem with
1751 		 * default_tg_pt_gp.
1752 		 */
1753 		spin_lock(&lun->lun_tg_pt_gp_lock);
1754 		if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1755 			__target_attach_tg_pt_gp(lun,
1756 					dev->t10_alua.default_tg_pt_gp);
1757 		} else
1758 			lun->lun_tg_pt_gp = NULL;
1759 		spin_unlock(&lun->lun_tg_pt_gp_lock);
1760 
1761 		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1762 	}
1763 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1764 
1765 	kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1766 }
1767 
1768 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1769 		struct se_device *dev, const char *name)
1770 {
1771 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1772 	struct config_item *ci;
1773 
1774 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1775 	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1776 			tg_pt_gp_list) {
1777 		if (!tg_pt_gp->tg_pt_gp_valid_id)
1778 			continue;
1779 		ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1780 		if (!strcmp(config_item_name(ci), name)) {
1781 			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1782 			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1783 			return tg_pt_gp;
1784 		}
1785 	}
1786 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1787 
1788 	return NULL;
1789 }
1790 
1791 static void core_alua_put_tg_pt_gp_from_name(
1792 	struct t10_alua_tg_pt_gp *tg_pt_gp)
1793 {
1794 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1795 
1796 	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1797 	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1798 	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1799 }
1800 
1801 static void __target_attach_tg_pt_gp(struct se_lun *lun,
1802 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1803 {
1804 	struct se_dev_entry *se_deve;
1805 
1806 	assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1807 
1808 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1809 	lun->lun_tg_pt_gp = tg_pt_gp;
1810 	list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
1811 	tg_pt_gp->tg_pt_gp_members++;
1812 	spin_lock(&lun->lun_deve_lock);
1813 	list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1814 		core_scsi3_ua_allocate(se_deve, 0x3f,
1815 				       ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1816 	spin_unlock(&lun->lun_deve_lock);
1817 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1818 }
1819 
1820 void target_attach_tg_pt_gp(struct se_lun *lun,
1821 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1822 {
1823 	spin_lock(&lun->lun_tg_pt_gp_lock);
1824 	__target_attach_tg_pt_gp(lun, tg_pt_gp);
1825 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1826 }
1827 
1828 static void __target_detach_tg_pt_gp(struct se_lun *lun,
1829 		struct t10_alua_tg_pt_gp *tg_pt_gp)
1830 {
1831 	assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1832 
1833 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1834 	list_del_init(&lun->lun_tg_pt_gp_link);
1835 	tg_pt_gp->tg_pt_gp_members--;
1836 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1837 
1838 	lun->lun_tg_pt_gp = NULL;
1839 }
1840 
1841 void target_detach_tg_pt_gp(struct se_lun *lun)
1842 {
1843 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1844 
1845 	spin_lock(&lun->lun_tg_pt_gp_lock);
1846 	tg_pt_gp = lun->lun_tg_pt_gp;
1847 	if (tg_pt_gp)
1848 		__target_detach_tg_pt_gp(lun, tg_pt_gp);
1849 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1850 }
1851 
1852 ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
1853 {
1854 	struct config_item *tg_pt_ci;
1855 	struct t10_alua_tg_pt_gp *tg_pt_gp;
1856 	ssize_t len = 0;
1857 
1858 	spin_lock(&lun->lun_tg_pt_gp_lock);
1859 	tg_pt_gp = lun->lun_tg_pt_gp;
1860 	if (tg_pt_gp) {
1861 		tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1862 		len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1863 			" %hu\nTG Port Primary Access State: %s\nTG Port "
1864 			"Primary Access Status: %s\nTG Port Secondary Access"
1865 			" State: %s\nTG Port Secondary Access Status: %s\n",
1866 			config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1867 			core_alua_dump_state(
1868 				tg_pt_gp->tg_pt_gp_alua_access_state),
1869 			core_alua_dump_status(
1870 				tg_pt_gp->tg_pt_gp_alua_access_status),
1871 			atomic_read(&lun->lun_tg_pt_secondary_offline) ?
1872 			"Offline" : "None",
1873 			core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
1874 	}
1875 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1876 
1877 	return len;
1878 }
1879 
1880 ssize_t core_alua_store_tg_pt_gp_info(
1881 	struct se_lun *lun,
1882 	const char *page,
1883 	size_t count)
1884 {
1885 	struct se_portal_group *tpg = lun->lun_tpg;
1886 	/*
1887 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
1888 	 * reference to se_device->dev_group.
1889 	 */
1890 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
1891 	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1892 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
1893 	int move = 0;
1894 
1895 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
1896 	    (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1897 		return -ENODEV;
1898 
1899 	if (count > TG_PT_GROUP_NAME_BUF) {
1900 		pr_err("ALUA Target Port Group alias too large!\n");
1901 		return -EINVAL;
1902 	}
1903 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1904 	memcpy(buf, page, count);
1905 	/*
1906 	 * Any ALUA target port group alias besides "NULL" means we will be
1907 	 * making a new group association.
1908 	 */
1909 	if (strcmp(strstrip(buf), "NULL")) {
1910 		/*
1911 		 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1912 		 * struct t10_alua_tg_pt_gp.  This reference is released with
1913 		 * core_alua_put_tg_pt_gp_from_name() below.
1914 		 */
1915 		tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1916 					strstrip(buf));
1917 		if (!tg_pt_gp_new)
1918 			return -ENODEV;
1919 	}
1920 
1921 	spin_lock(&lun->lun_tg_pt_gp_lock);
1922 	tg_pt_gp = lun->lun_tg_pt_gp;
1923 	if (tg_pt_gp) {
1924 		/*
1925 		 * Clearing an existing tg_pt_gp association, and replacing
1926 		 * with the default_tg_pt_gp.
1927 		 */
1928 		if (!tg_pt_gp_new) {
1929 			pr_debug("Target_Core_ConfigFS: Moving"
1930 				" %s/tpgt_%hu/%s from ALUA Target Port Group:"
1931 				" alua/%s, ID: %hu back to"
1932 				" default_tg_pt_gp\n",
1933 				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1934 				tpg->se_tpg_tfo->tpg_get_tag(tpg),
1935 				config_item_name(&lun->lun_group.cg_item),
1936 				config_item_name(
1937 					&tg_pt_gp->tg_pt_gp_group.cg_item),
1938 				tg_pt_gp->tg_pt_gp_id);
1939 
1940 			__target_detach_tg_pt_gp(lun, tg_pt_gp);
1941 			__target_attach_tg_pt_gp(lun,
1942 					dev->t10_alua.default_tg_pt_gp);
1943 			spin_unlock(&lun->lun_tg_pt_gp_lock);
1944 
1945 			return count;
1946 		}
1947 		__target_detach_tg_pt_gp(lun, tg_pt_gp);
1948 		move = 1;
1949 	}
1950 
1951 	__target_attach_tg_pt_gp(lun, tg_pt_gp_new);
1952 	spin_unlock(&lun->lun_tg_pt_gp_lock);
1953 	pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1954 		" Target Port Group: alua/%s, ID: %hu\n", (move) ?
1955 		"Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1956 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
1957 		config_item_name(&lun->lun_group.cg_item),
1958 		config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1959 		tg_pt_gp_new->tg_pt_gp_id);
1960 
1961 	core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1962 	return count;
1963 }
1964 
1965 ssize_t core_alua_show_access_type(
1966 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1967 	char *page)
1968 {
1969 	if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
1970 	    (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
1971 		return sprintf(page, "Implicit and Explicit\n");
1972 	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
1973 		return sprintf(page, "Implicit\n");
1974 	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
1975 		return sprintf(page, "Explicit\n");
1976 	else
1977 		return sprintf(page, "None\n");
1978 }
1979 
1980 ssize_t core_alua_store_access_type(
1981 	struct t10_alua_tg_pt_gp *tg_pt_gp,
1982 	const char *page,
1983 	size_t count)
1984 {
1985 	unsigned long tmp;
1986 	int ret;
1987 
1988 	ret = kstrtoul(page, 0, &tmp);
1989 	if (ret < 0) {
1990 		pr_err("Unable to extract alua_access_type\n");
1991 		return ret;
1992 	}
1993 	if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1994 		pr_err("Illegal value for alua_access_type:"
1995 				" %lu\n", tmp);
1996 		return -EINVAL;
1997 	}
1998 	if (tmp == 3)
1999 		tg_pt_gp->tg_pt_gp_alua_access_type =
2000 			TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
2001 	else if (tmp == 2)
2002 		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
2003 	else if (tmp == 1)
2004 		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
2005 	else
2006 		tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2007 
2008 	return count;
2009 }
2010 
2011 ssize_t core_alua_show_nonop_delay_msecs(
2012 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2013 	char *page)
2014 {
2015 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2016 }
2017 
2018 ssize_t core_alua_store_nonop_delay_msecs(
2019 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2020 	const char *page,
2021 	size_t count)
2022 {
2023 	unsigned long tmp;
2024 	int ret;
2025 
2026 	ret = kstrtoul(page, 0, &tmp);
2027 	if (ret < 0) {
2028 		pr_err("Unable to extract nonop_delay_msecs\n");
2029 		return ret;
2030 	}
2031 	if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
2032 		pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2033 			" ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2034 			ALUA_MAX_NONOP_DELAY_MSECS);
2035 		return -EINVAL;
2036 	}
2037 	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2038 
2039 	return count;
2040 }
2041 
2042 ssize_t core_alua_show_trans_delay_msecs(
2043 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2044 	char *page)
2045 {
2046 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2047 }
2048 
2049 ssize_t core_alua_store_trans_delay_msecs(
2050 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2051 	const char *page,
2052 	size_t count)
2053 {
2054 	unsigned long tmp;
2055 	int ret;
2056 
2057 	ret = kstrtoul(page, 0, &tmp);
2058 	if (ret < 0) {
2059 		pr_err("Unable to extract trans_delay_msecs\n");
2060 		return ret;
2061 	}
2062 	if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
2063 		pr_err("Passed trans_delay_msecs: %lu, exceeds"
2064 			" ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2065 			ALUA_MAX_TRANS_DELAY_MSECS);
2066 		return -EINVAL;
2067 	}
2068 	tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2069 
2070 	return count;
2071 }
2072 
2073 ssize_t core_alua_show_implicit_trans_secs(
2074 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2075 	char *page)
2076 {
2077 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
2078 }
2079 
2080 ssize_t core_alua_store_implicit_trans_secs(
2081 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2082 	const char *page,
2083 	size_t count)
2084 {
2085 	unsigned long tmp;
2086 	int ret;
2087 
2088 	ret = kstrtoul(page, 0, &tmp);
2089 	if (ret < 0) {
2090 		pr_err("Unable to extract implicit_trans_secs\n");
2091 		return ret;
2092 	}
2093 	if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2094 		pr_err("Passed implicit_trans_secs: %lu, exceeds"
2095 			" ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2096 			ALUA_MAX_IMPLICIT_TRANS_SECS);
2097 		return  -EINVAL;
2098 	}
2099 	tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
2100 
2101 	return count;
2102 }
2103 
2104 ssize_t core_alua_show_preferred_bit(
2105 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2106 	char *page)
2107 {
2108 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2109 }
2110 
2111 ssize_t core_alua_store_preferred_bit(
2112 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2113 	const char *page,
2114 	size_t count)
2115 {
2116 	unsigned long tmp;
2117 	int ret;
2118 
2119 	ret = kstrtoul(page, 0, &tmp);
2120 	if (ret < 0) {
2121 		pr_err("Unable to extract preferred ALUA value\n");
2122 		return ret;
2123 	}
2124 	if ((tmp != 0) && (tmp != 1)) {
2125 		pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
2126 		return -EINVAL;
2127 	}
2128 	tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2129 
2130 	return count;
2131 }
2132 
2133 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2134 {
2135 	return sprintf(page, "%d\n",
2136 		atomic_read(&lun->lun_tg_pt_secondary_offline));
2137 }
2138 
2139 ssize_t core_alua_store_offline_bit(
2140 	struct se_lun *lun,
2141 	const char *page,
2142 	size_t count)
2143 {
2144 	/*
2145 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
2146 	 * reference to se_device->dev_group.
2147 	 */
2148 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
2149 	unsigned long tmp;
2150 	int ret;
2151 
2152 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
2153 	    (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2154 		return -ENODEV;
2155 
2156 	ret = kstrtoul(page, 0, &tmp);
2157 	if (ret < 0) {
2158 		pr_err("Unable to extract alua_tg_pt_offline value\n");
2159 		return ret;
2160 	}
2161 	if ((tmp != 0) && (tmp != 1)) {
2162 		pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2163 				tmp);
2164 		return -EINVAL;
2165 	}
2166 
2167 	ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
2168 	if (ret < 0)
2169 		return -EINVAL;
2170 
2171 	return count;
2172 }
2173 
2174 ssize_t core_alua_show_secondary_status(
2175 	struct se_lun *lun,
2176 	char *page)
2177 {
2178 	return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
2179 }
2180 
2181 ssize_t core_alua_store_secondary_status(
2182 	struct se_lun *lun,
2183 	const char *page,
2184 	size_t count)
2185 {
2186 	unsigned long tmp;
2187 	int ret;
2188 
2189 	ret = kstrtoul(page, 0, &tmp);
2190 	if (ret < 0) {
2191 		pr_err("Unable to extract alua_tg_pt_status\n");
2192 		return ret;
2193 	}
2194 	if ((tmp != ALUA_STATUS_NONE) &&
2195 	    (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2196 	    (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2197 		pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2198 				tmp);
2199 		return -EINVAL;
2200 	}
2201 	lun->lun_tg_pt_secondary_stat = (int)tmp;
2202 
2203 	return count;
2204 }
2205 
2206 ssize_t core_alua_show_secondary_write_metadata(
2207 	struct se_lun *lun,
2208 	char *page)
2209 {
2210 	return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
2211 }
2212 
2213 ssize_t core_alua_store_secondary_write_metadata(
2214 	struct se_lun *lun,
2215 	const char *page,
2216 	size_t count)
2217 {
2218 	unsigned long tmp;
2219 	int ret;
2220 
2221 	ret = kstrtoul(page, 0, &tmp);
2222 	if (ret < 0) {
2223 		pr_err("Unable to extract alua_tg_pt_write_md\n");
2224 		return ret;
2225 	}
2226 	if ((tmp != 0) && (tmp != 1)) {
2227 		pr_err("Illegal value for alua_tg_pt_write_md:"
2228 				" %lu\n", tmp);
2229 		return -EINVAL;
2230 	}
2231 	lun->lun_tg_pt_secondary_write_md = (int)tmp;
2232 
2233 	return count;
2234 }
2235 
2236 int core_setup_alua(struct se_device *dev)
2237 {
2238 	if (!(dev->transport_flags &
2239 	     TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
2240 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2241 		struct t10_alua_lu_gp_member *lu_gp_mem;
2242 
2243 		/*
2244 		 * Associate this struct se_device with the default ALUA
2245 		 * LUN Group.
2246 		 */
2247 		lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2248 		if (IS_ERR(lu_gp_mem))
2249 			return PTR_ERR(lu_gp_mem);
2250 
2251 		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2252 		__core_alua_attach_lu_gp_mem(lu_gp_mem,
2253 				default_lu_gp);
2254 		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2255 
2256 		pr_debug("%s: Adding to default ALUA LU Group:"
2257 			" core/alua/lu_gps/default_lu_gp\n",
2258 			dev->transport->name);
2259 	}
2260 
2261 	return 0;
2262 }
2263