target_core_alua.c 62.2 KB
Newer Older
1 2 3 4 5
/*******************************************************************************
 * Filename:  target_core_alua.c
 *
 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
 *
6
 * (c) Copyright 2009-2013 Datera, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Nicholas A. Bellinger <nab@kernel.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ******************************************************************************/

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/configfs.h>
29
#include <linux/export.h>
30
#include <linux/file.h>
31
#include <scsi/scsi_proto.h>
32
#include <asm/unaligned.h>
33 34

#include <target/target_core_base.h>
35 36
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
37

38
#include "target_core_internal.h"
39 40 41
#include "target_core_alua.h"
#include "target_core_ua.h"

42 43
static sense_reason_t core_alua_check_transition(int state, int valid,
						 int *primary);
44
static int core_alua_set_tg_pt_secondary_state(
45
		struct se_lun *lun, int explicit, int offline);
46

47 48
static char *core_alua_dump_state(int state);

49 50 51
static void __target_attach_tg_pt_gp(struct se_lun *lun,
		struct t10_alua_tg_pt_gp *tg_pt_gp);

52 53 54 55 56 57 58 59
static u16 alua_lu_gps_counter;
static u32 alua_lu_gps_count;

static DEFINE_SPINLOCK(lu_gps_lock);
static LIST_HEAD(lu_gps_list);

struct t10_alua_lu_gp *default_lu_gp;

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*
 * REPORT REFERRALS
 *
 * See sbc3r35 section 5.23
 */
sense_reason_t
target_emulate_report_referrals(struct se_cmd *cmd)
{
	struct se_device *dev = cmd->se_dev;
	struct t10_alua_lba_map *map;
	struct t10_alua_lba_map_member *map_mem;
	unsigned char *buf;
	u32 rd_len = 0, off;

	if (cmd->data_length < 4) {
		pr_warn("REPORT REFERRALS allocation length %u too"
			" small\n", cmd->data_length);
		return TCM_INVALID_CDB_FIELD;
	}

	buf = transport_kmap_data_sg(cmd);
	if (!buf)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;

	off = 4;
	spin_lock(&dev->t10_alua.lba_map_lock);
	if (list_empty(&dev->t10_alua.lba_map_list)) {
		spin_unlock(&dev->t10_alua.lba_map_lock);
		transport_kunmap_data_sg(cmd);

		return TCM_UNSUPPORTED_SCSI_OPCODE;
	}

	list_for_each_entry(map, &dev->t10_alua.lba_map_list,
			    lba_map_list) {
		int desc_num = off + 3;
		int pg_num;

		off += 4;
99 100
		if (cmd->data_length > off)
			put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
101
		off += 8;
102 103
		if (cmd->data_length > off)
			put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
104 105 106 107 108
		off += 8;
		rd_len += 20;
		pg_num = 0;
		list_for_each_entry(map_mem, &map->lba_map_mem_list,
				    lba_map_mem_list) {
109 110 111 112 113 114 115 116 117 118 119
			int alua_state = map_mem->lba_map_mem_alua_state;
			int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;

			if (cmd->data_length > off)
				buf[off] = alua_state & 0x0f;
			off += 2;
			if (cmd->data_length > off)
				buf[off] = (alua_pg_id >> 8) & 0xff;
			off++;
			if (cmd->data_length > off)
				buf[off] = (alua_pg_id & 0xff);
120 121 122 123
			off++;
			rd_len += 4;
			pg_num++;
		}
124 125
		if (cmd->data_length > desc_num)
			buf[desc_num] = pg_num;
126 127 128 129 130 131 132 133 134 135 136 137 138 139
	}
	spin_unlock(&dev->t10_alua.lba_map_lock);

	/*
	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
	 */
	put_unaligned_be16(rd_len, &buf[2]);

	transport_kunmap_data_sg(cmd);

	target_complete_cmd(cmd, GOOD);
	return 0;
}

140 141 142 143 144
/*
 * REPORT_TARGET_PORT_GROUPS
 *
 * See spc4r17 section 6.27
 */
145 146
sense_reason_t
target_emulate_report_target_port_groups(struct se_cmd *cmd)
147
{
148
	struct se_device *dev = cmd->se_dev;
149
	struct t10_alua_tg_pt_gp *tg_pt_gp;
150
	struct se_lun *lun;
151
	unsigned char *buf;
152 153
	u32 rd_len = 0, off;
	int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
154

155
	/*
156 157
	 * Skip over RESERVED area to first Target port group descriptor
	 * depending on the PARAMETER DATA FORMAT type..
158
	 */
159 160 161 162 163 164 165 166 167
	if (ext_hdr != 0)
		off = 8;
	else
		off = 4;

	if (cmd->data_length < off) {
		pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
			" small for %s header\n", cmd->data_length,
			(ext_hdr) ? "extended" : "normal");
168
		return TCM_INVALID_CDB_FIELD;
169
	}
170
	buf = transport_kmap_data_sg(cmd);
171 172
	if (!buf)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
173

174 175
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
176
			tg_pt_gp_list) {
177 178 179 180 181 182 183 184 185 186 187
		/*
		 * Check if the Target port group and Target port descriptor list
		 * based on tg_pt_gp_members count will fit into the response payload.
		 * Otherwise, bump rd_len to let the initiator know we have exceeded
		 * the allocation length and the response is truncated.
		 */
		if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
		     cmd->data_length) {
			rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
			continue;
		}
188 189 190 191 192 193 194 195 196 197 198 199 200 201
		/*
		 * PREF: Preferred target port bit, determine if this
		 * bit should be set for port group.
		 */
		if (tg_pt_gp->tg_pt_gp_pref)
			buf[off] = 0x80;
		/*
		 * Set the ASYMMETRIC ACCESS State
		 */
		buf[off++] |= (atomic_read(
			&tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
		/*
		 * Set supported ASYMMETRIC ACCESS State bits
		 */
202
		buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		/*
		 * TARGET PORT GROUP
		 */
		buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
		buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);

		off++; /* Skip over Reserved */
		/*
		 * STATUS CODE
		 */
		buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
		/*
		 * Vendor Specific field
		 */
		buf[off++] = 0x00;
		/*
		 * TARGET PORT COUNT
		 */
		buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
		rd_len += 8;

		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
225 226
		list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
				lun_tg_pt_gp_link) {
227 228 229 230 231 232 233 234 235
			/*
			 * Start Target Port descriptor format
			 *
			 * See spc4r17 section 6.2.7 Table 247
			 */
			off += 2; /* Skip over Obsolete */
			/*
			 * Set RELATIVE TARGET PORT IDENTIFIER
			 */
236 237
			buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
			buf[off++] = (lun->lun_rtpi & 0xff);
238 239 240 241
			rd_len += 4;
		}
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
	}
242
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
243 244 245
	/*
	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
	 */
246
	put_unaligned_be32(rd_len, &buf[0]);
247

248 249 250 251 252 253
	/*
	 * Fill in the Extended header parameter data format if requested
	 */
	if (ext_hdr != 0) {
		buf[4] = 0x10;
		/*
254
		 * Set the implicit transition time (in seconds) for the application
255 256 257 258 259 260
		 * client to use as a base for it's transition timeout value.
		 *
		 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
		 * this CDB was received upon to determine this value individually
		 * for ALUA target port group.
		 */
261 262 263 264 265
		spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
		tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
		if (tg_pt_gp)
			buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
		spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
266
	}
267
	transport_kunmap_data_sg(cmd);
268

269
	target_complete_cmd(cmd, GOOD);
270 271 272 273
	return 0;
}

/*
274
 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
275 276 277
 *
 * See spc4r17 section 6.35
 */
278 279
sense_reason_t
target_emulate_set_target_port_groups(struct se_cmd *cmd)
280
{
281
	struct se_device *dev = cmd->se_dev;
282
	struct se_lun *l_lun = cmd->se_lun;
283
	struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
284
	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
285 286
	unsigned char *buf;
	unsigned char *ptr;
287
	sense_reason_t rc = TCM_NO_SENSE;
288
	u32 len = 4; /* Skip over RESERVED area in header */
289
	int alua_access_state, primary = 0, valid_states;
290 291
	u16 tg_pt_id, rtpi;

292 293 294
	if (cmd->data_length < 4) {
		pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
			" small\n", cmd->data_length);
295
		return TCM_INVALID_PARAMETER_LIST;
296 297
	}

298
	buf = transport_kmap_data_sg(cmd);
299 300
	if (!buf)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
301

302
	/*
303
	 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
304 305
	 * for the local tg_pt_gp.
	 */
306 307
	spin_lock(&l_lun->lun_tg_pt_gp_lock);
	l_tg_pt_gp = l_lun->lun_tg_pt_gp;
308
	if (!l_tg_pt_gp) {
309 310
		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
		pr_err("Unable to access l_lun->tg_pt_gp\n");
311
		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
312
		goto out;
313 314
	}

315
	if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
316
		spin_unlock(&l_lun->lun_tg_pt_gp_lock);
317
		pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
318
				" while TPGS_EXPLICIT_ALUA is disabled\n");
319
		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
320
		goto out;
321
	}
322
	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
323
	spin_unlock(&l_lun->lun_tg_pt_gp_lock);
324

325 326
	ptr = &buf[4]; /* Skip over RESERVED area in header */

327
	while (len < cmd->data_length) {
328
		bool found = false;
329 330 331 332 333 334
		alua_access_state = (ptr[0] & 0x0f);
		/*
		 * Check the received ALUA access state, and determine if
		 * the state is a primary or secondary target port asymmetric
		 * access state.
		 */
335 336
		rc = core_alua_check_transition(alua_access_state,
						valid_states, &primary);
337
		if (rc) {
338 339 340 341 342 343 344 345 346 347
			/*
			 * If the SET TARGET PORT GROUPS attempts to establish
			 * an invalid combination of target port asymmetric
			 * access states or attempts to establish an
			 * unsupported target port asymmetric access state,
			 * then the command shall be terminated with CHECK
			 * CONDITION status, with the sense key set to ILLEGAL
			 * REQUEST, and the additional sense code set to INVALID
			 * FIELD IN PARAMETER LIST.
			 */
348
			goto out;
349
		}
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364
		/*
		 * If the ASYMMETRIC ACCESS STATE field (see table 267)
		 * specifies a primary target port asymmetric access state,
		 * then the TARGET PORT GROUP OR TARGET PORT field specifies
		 * a primary target port group for which the primary target
		 * port asymmetric access state shall be changed. If the
		 * ASYMMETRIC ACCESS STATE field specifies a secondary target
		 * port asymmetric access state, then the TARGET PORT GROUP OR
		 * TARGET PORT field specifies the relative target port
		 * identifier (see 3.1.120) of the target port for which the
		 * secondary target port asymmetric access state shall be
		 * changed.
		 */
		if (primary) {
365
			tg_pt_id = get_unaligned_be16(ptr + 2);
366 367 368 369
			/*
			 * Locate the matching target port group ID from
			 * the global tg_pt_gp list
			 */
370
			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
371
			list_for_each_entry(tg_pt_gp,
372
					&dev->t10_alua.tg_pt_gps_list,
373
					tg_pt_gp_list) {
374
				if (!tg_pt_gp->tg_pt_gp_valid_id)
375 376 377 378 379
					continue;

				if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
					continue;

380
				atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
381

382
				spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
383

384
				if (!core_alua_do_port_transition(tg_pt_gp,
385
						dev, l_lun, nacl,
386 387
						alua_access_state, 1))
					found = true;
388

389
				spin_lock(&dev->t10_alua.tg_pt_gps_lock);
390
				atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
391 392
				break;
			}
393
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
394
		} else {
395 396
			struct se_lun *lun;

397
			/*
398
			 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
399 400 401
			 * the Target Port in question for the the incoming
			 * SET_TARGET_PORT_GROUPS op.
			 */
402
			rtpi = get_unaligned_be16(ptr + 2);
403
			/*
404
			 * Locate the matching relative target port identifier
405 406 407
			 * for the struct se_device storage object.
			 */
			spin_lock(&dev->se_port_lock);
408 409 410
			list_for_each_entry(lun, &dev->dev_sep_list,
							lun_dev_link) {
				if (lun->lun_rtpi != rtpi)
411 412
					continue;

413
				// XXX: racy unlock
414 415
				spin_unlock(&dev->se_port_lock);

416
				if (!core_alua_set_tg_pt_secondary_state(
417
						lun, 1, 1))
418
					found = true;
419 420 421 422 423

				spin_lock(&dev->se_port_lock);
				break;
			}
			spin_unlock(&dev->se_port_lock);
424 425 426 427 428
		}

		if (!found) {
			rc = TCM_INVALID_PARAMETER_LIST;
			goto out;
429 430 431 432 433 434
		}

		ptr += 4;
		len += 4;
	}

435
out:
436
	transport_kunmap_data_sg(cmd);
437 438 439
	if (!rc)
		target_complete_cmd(cmd, GOOD);
	return rc;
440 441
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
{
	/*
	 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
	 * The ALUA additional sense code qualifier (ASCQ) is determined
	 * by the ALUA primary or secondary access state..
	 */
	pr_debug("[%s]: ALUA TG Port not available, "
		"SenseKey: NOT_READY, ASC/ASCQ: "
		"0x04/0x%02x\n",
		cmd->se_tfo->get_fabric_name(), alua_ascq);

	cmd->scsi_asc = 0x04;
	cmd->scsi_ascq = alua_ascq;
}

static inline void core_alua_state_nonoptimized(
459 460
	struct se_cmd *cmd,
	unsigned char *cdb,
461
	int nonop_delay_msecs)
462 463 464 465 466 467 468 469 470 471
{
	/*
	 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
	 * later to determine if processing of this cmd needs to be
	 * temporarily delayed for the Active/NonOptimized primary access state.
	 */
	cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
	cmd->alua_nonop_delay = nonop_delay_msecs;
}

472 473
static inline int core_alua_state_lba_dependent(
	struct se_cmd *cmd,
474
	struct t10_alua_tg_pt_gp *tg_pt_gp)
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
{
	struct se_device *dev = cmd->se_dev;
	u64 segment_size, segment_mult, sectors, lba;

	/* Only need to check for cdb actually containing LBAs */
	if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
		return 0;

	spin_lock(&dev->t10_alua.lba_map_lock);
	segment_size = dev->t10_alua.lba_map_segment_size;
	segment_mult = dev->t10_alua.lba_map_segment_multiplier;
	sectors = cmd->data_length / dev->dev_attrib.block_size;

	lba = cmd->t_task_lba;
	while (lba < cmd->t_task_lba + sectors) {
		struct t10_alua_lba_map *cur_map = NULL, *map;
		struct t10_alua_lba_map_member *map_mem;

		list_for_each_entry(map, &dev->t10_alua.lba_map_list,
				    lba_map_list) {
			u64 start_lba, last_lba;
			u64 first_lba = map->lba_map_first_lba;

			if (segment_mult) {
				u64 tmp = lba;
500
				start_lba = do_div(tmp, segment_size * segment_mult);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

				last_lba = first_lba + segment_size - 1;
				if (start_lba >= first_lba &&
				    start_lba <= last_lba) {
					lba += segment_size;
					cur_map = map;
					break;
				}
			} else {
				last_lba = map->lba_map_last_lba;
				if (lba >= first_lba && lba <= last_lba) {
					lba = last_lba + 1;
					cur_map = map;
					break;
				}
			}
		}
		if (!cur_map) {
			spin_unlock(&dev->t10_alua.lba_map_lock);
520
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
521 522 523 524 525 526 527 528 529 530
			return 1;
		}
		list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
				    lba_map_mem_list) {
			if (map_mem->lba_map_mem_alua_pg_id !=
			    tg_pt_gp->tg_pt_gp_id)
				continue;
			switch(map_mem->lba_map_mem_alua_state) {
			case ALUA_ACCESS_STATE_STANDBY:
				spin_unlock(&dev->t10_alua.lba_map_lock);
531
				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
532 533 534
				return 1;
			case ALUA_ACCESS_STATE_UNAVAILABLE:
				spin_unlock(&dev->t10_alua.lba_map_lock);
535
				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
536 537 538 539 540 541 542 543 544 545
				return 1;
			default:
				break;
			}
		}
	}
	spin_unlock(&dev->t10_alua.lba_map_lock);
	return 0;
}

546 547
static inline int core_alua_state_standby(
	struct se_cmd *cmd,
548
	unsigned char *cdb)
549 550 551 552 553 554 555 556 557 558 559 560 561 562
{
	/*
	 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
	 * spc4r17 section 5.9.2.4.4
	 */
	switch (cdb[0]) {
	case INQUIRY:
	case LOG_SELECT:
	case LOG_SENSE:
	case MODE_SELECT:
	case MODE_SENSE:
	case REPORT_LUNS:
	case RECEIVE_DIAGNOSTIC:
	case SEND_DIAGNOSTIC:
563
	case READ_CAPACITY:
564
		return 0;
565
	case SERVICE_ACTION_IN_16:
566 567 568 569 570 571 572
		switch (cdb[1] & 0x1f) {
		case SAI_READ_CAPACITY_16:
			return 0;
		default:
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
			return 1;
		}
573
	case MAINTENANCE_IN:
574
		switch (cdb[1] & 0x1f) {
575 576 577
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
578
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
579 580 581 582 583 584 585
			return 1;
		}
	case MAINTENANCE_OUT:
		switch (cdb[1]) {
		case MO_SET_TARGET_PGS:
			return 0;
		default:
586
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
587 588 589 590 591 592 593 594 595
			return 1;
		}
	case REQUEST_SENSE:
	case PERSISTENT_RESERVE_IN:
	case PERSISTENT_RESERVE_OUT:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
596
		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
597 598 599 600 601 602 603 604
		return 1;
	}

	return 0;
}

static inline int core_alua_state_unavailable(
	struct se_cmd *cmd,
605
	unsigned char *cdb)
606 607 608 609 610 611 612 613
{
	/*
	 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
	 * spc4r17 section 5.9.2.4.5
	 */
	switch (cdb[0]) {
	case INQUIRY:
	case REPORT_LUNS:
614
		return 0;
615
	case MAINTENANCE_IN:
616
		switch (cdb[1] & 0x1f) {
617 618 619
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
620
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
621 622 623 624 625 626 627
			return 1;
		}
	case MAINTENANCE_OUT:
		switch (cdb[1]) {
		case MO_SET_TARGET_PGS:
			return 0;
		default:
628
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
629 630 631 632 633 634 635
			return 1;
		}
	case REQUEST_SENSE:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
636
		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
637 638 639 640 641 642 643 644
		return 1;
	}

	return 0;
}

static inline int core_alua_state_transition(
	struct se_cmd *cmd,
645
	unsigned char *cdb)
646 647
{
	/*
648
	 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
649 650 651 652 653
	 * spc4r17 section 5.9.2.5
	 */
	switch (cdb[0]) {
	case INQUIRY:
	case REPORT_LUNS:
654
		return 0;
655
	case MAINTENANCE_IN:
656
		switch (cdb[1] & 0x1f) {
657 658 659
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
660
			set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
661 662 663 664 665 666 667
			return 1;
		}
	case REQUEST_SENSE:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
668
		set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
669 670 671 672 673 674 675
		return 1;
	}

	return 0;
}

/*
676
 * return 1: Is used to signal LUN not accessible, and check condition/not ready
677
 * return 0: Used to signal success
678
 * return -1: Used to signal failure, and invalid cdb field
679
 */
680 681
sense_reason_t
target_alua_state_check(struct se_cmd *cmd)
682
{
683 684
	struct se_device *dev = cmd->se_dev;
	unsigned char *cdb = cmd->t_task_cdb;
685
	struct se_lun *lun = cmd->se_lun;
686 687
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	int out_alua_state, nonop_delay_msecs;
688 689 690

	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
		return 0;
691
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
692
		return 0;
693 694 695 696 697

	/*
	 * First, check for a struct se_port specific secondary ALUA target port
	 * access state: OFFLINE
	 */
698
	if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
699
		pr_debug("ALUA: Got secondary offline status for local"
700
				" target port\n");
701 702
		set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
		return TCM_CHECK_CONDITION_NOT_READY;
703
	}
704 705

	if (!lun->lun_tg_pt_gp)
706 707
		return 0;

708 709
	spin_lock(&lun->lun_tg_pt_gp_lock);
	tg_pt_gp = lun->lun_tg_pt_gp;
710 711
	out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
	nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
712 713 714

	// XXX: keeps using tg_pt_gp witout reference after unlock
	spin_unlock(&lun->lun_tg_pt_gp_lock);
715
	/*
716
	 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
717
	 * statement so the compiler knows explicitly to check this case first.
718 719 720
	 * For the Optimized ALUA access state case, we want to process the
	 * incoming fabric cmd ASAP..
	 */
721
	if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
722 723 724 725
		return 0;

	switch (out_alua_state) {
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
726
		core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
727
		break;
728
	case ALUA_ACCESS_STATE_STANDBY:
729 730
		if (core_alua_state_standby(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
731
		break;
732
	case ALUA_ACCESS_STATE_UNAVAILABLE:
733 734
		if (core_alua_state_unavailable(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
735
		break;
736
	case ALUA_ACCESS_STATE_TRANSITION:
737 738
		if (core_alua_state_transition(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
739
		break;
740
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
741 742
		if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
			return TCM_CHECK_CONDITION_NOT_READY;
743
		break;
744 745
	/*
	 * OFFLINE is a secondary ALUA target port group access state, that is
746
	 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
747 748 749
	 */
	case ALUA_ACCESS_STATE_OFFLINE:
	default:
750
		pr_err("Unknown ALUA access state: 0x%02x\n",
751
				out_alua_state);
752
		return TCM_INVALID_CDB_FIELD;
753 754
	}

755
	return 0;
756 757 758
}

/*
759
 * Check implicit and explicit ALUA state change request.
760
 */
761
static sense_reason_t
762
core_alua_check_transition(int state, int valid, int *primary)
763
{
764 765 766 767
	/*
	 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
	 * defined as primary target port asymmetric access states.
	 */
768
	switch (state) {
769
	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
770 771 772 773
		if (!(valid & ALUA_AO_SUP))
			goto not_supported;
		*primary = 1;
		break;
774
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
775 776 777 778
		if (!(valid & ALUA_AN_SUP))
			goto not_supported;
		*primary = 1;
		break;
779
	case ALUA_ACCESS_STATE_STANDBY:
780 781 782 783
		if (!(valid & ALUA_S_SUP))
			goto not_supported;
		*primary = 1;
		break;
784
	case ALUA_ACCESS_STATE_UNAVAILABLE:
785 786
		if (!(valid & ALUA_U_SUP))
			goto not_supported;
787 788
		*primary = 1;
		break;
789 790 791 792 793
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
		if (!(valid & ALUA_LBD_SUP))
			goto not_supported;
		*primary = 1;
		break;
794 795 796 797 798
	case ALUA_ACCESS_STATE_OFFLINE:
		/*
		 * OFFLINE state is defined as a secondary target port
		 * asymmetric access state.
		 */
799 800
		if (!(valid & ALUA_O_SUP))
			goto not_supported;
801 802
		*primary = 0;
		break;
803 804 805 806 807 808
	case ALUA_ACCESS_STATE_TRANSITION:
		/*
		 * Transitioning is set internally, and
		 * cannot be selected manually.
		 */
		goto not_supported;
809
	default:
810
		pr_err("Unknown ALUA access state: 0x%02x\n", state);
811
		return TCM_INVALID_PARAMETER_LIST;
812 813 814
	}

	return 0;
815 816 817 818 819

not_supported:
	pr_err("ALUA access state %s not supported",
	       core_alua_dump_state(state));
	return TCM_INVALID_PARAMETER_LIST;
820 821 822 823 824
}

static char *core_alua_dump_state(int state)
{
	switch (state) {
825
	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
826 827 828
		return "Active/Optimized";
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
		return "Active/NonOptimized";
829 830
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
		return "LBA Dependent";
831 832 833 834 835 836
	case ALUA_ACCESS_STATE_STANDBY:
		return "Standby";
	case ALUA_ACCESS_STATE_UNAVAILABLE:
		return "Unavailable";
	case ALUA_ACCESS_STATE_OFFLINE:
		return "Offline";
837 838
	case ALUA_ACCESS_STATE_TRANSITION:
		return "Transitioning";
839 840 841 842 843 844 845 846 847 848 849 850
	default:
		return "Unknown";
	}

	return NULL;
}

char *core_alua_dump_status(int status)
{
	switch (status) {
	case ALUA_STATUS_NONE:
		return "None";
851 852 853 854
	case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
		return "Altered by Explicit STPG";
	case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
		return "Altered by Implicit ALUA";
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
	default:
		return "Unknown";
	}

	return NULL;
}

/*
 * Used by fabric modules to determine when we need to delay processing
 * for the Active/NonOptimized paths..
 */
int core_alua_check_nonop_delay(
	struct se_cmd *cmd)
{
	if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
		return 0;
	if (in_interrupt())
		return 0;
	/*
	 * The ALUA Active/NonOptimized access state delay can be disabled
	 * in via configfs with a value of zero
	 */
877
	if (!cmd->alua_nonop_delay)
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
		return 0;
	/*
	 * struct se_cmd->alua_nonop_delay gets set by a target port group
	 * defined interval in core_alua_state_nonoptimized()
	 */
	msleep_interruptible(cmd->alua_nonop_delay);
	return 0;
}
EXPORT_SYMBOL(core_alua_check_nonop_delay);

static int core_alua_write_tpg_metadata(
	const char *path,
	unsigned char *md_buf,
	u32 md_buf_len)
{
893 894
	struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
	int ret;
895

896 897
	if (IS_ERR(file)) {
		pr_err("filp_open(%s) for ALUA metadata failed\n", path);
898 899
		return -ENODEV;
	}
900 901
	ret = kernel_write(file, md_buf, md_buf_len, 0);
	if (ret < 0)
902
		pr_err("Error writing ALUA metadata file: %s\n", path);
903
	fput(file);
904
	return (ret < 0) ? -EIO : 0;
905 906 907 908 909 910
}

/*
 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
 */
static int core_alua_update_tpg_primary_metadata(
911
	struct t10_alua_tg_pt_gp *tg_pt_gp)
912
{
913
	unsigned char *md_buf;
914
	struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
915
	char path[ALUA_METADATA_PATH_LEN];
916 917 918 919 920 921 922
	int len, rc;

	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
	if (!md_buf) {
		pr_err("Unable to allocate buf for ALUA metadata\n");
		return -ENOMEM;
	}
923 924 925

	memset(path, 0, ALUA_METADATA_PATH_LEN);

926
	len = snprintf(md_buf, ALUA_MD_BUF_LEN,
927 928 929
			"tg_pt_gp_id=%hu\n"
			"alua_access_state=0x%02x\n"
			"alua_access_status=0x%02x\n",
930 931
			tg_pt_gp->tg_pt_gp_id,
			tg_pt_gp->tg_pt_gp_alua_pending_state,
932 933 934
			tg_pt_gp->tg_pt_gp_alua_access_status);

	snprintf(path, ALUA_METADATA_PATH_LEN,
935
		"%s/alua/tpgs_%s/%s", db_root, &wwn->unit_serial[0],
936 937
		config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));

938 939 940
	rc = core_alua_write_tpg_metadata(path, md_buf, len);
	kfree(md_buf);
	return rc;
941 942
}

943
static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
944 945
{
	struct se_dev_entry *se_deve;
946
	struct se_lun *lun;
947 948 949
	struct se_lun_acl *lacl;

	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
950 951
	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
				lun_tg_pt_gp_link) {
952 953 954 955 956
		/*
		 * After an implicit target port asymmetric access state
		 * change, a device server shall establish a unit attention
		 * condition for the initiator port associated with every I_T
		 * nexus with the additional sense code set to ASYMMETRIC
957
		 * ACCESS STATE CHANGED.
958 959 960 961 962 963 964 965
		 *
		 * After an explicit target port asymmetric access state
		 * change, a device server shall establish a unit attention
		 * condition with the additional sense code set to ASYMMETRIC
		 * ACCESS STATE CHANGED for the initiator port associated with
		 * every I_T nexus other than the I_T nexus on which the SET
		 * TARGET PORT GROUPS command
		 */
966 967
		if (!percpu_ref_tryget_live(&lun->lun_ref))
			continue;
968 969
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);

970
		spin_lock(&lun->lun_deve_lock);
971
		list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
972
			lacl = rcu_dereference_check(se_deve->se_lun_acl,
973
					lockdep_is_held(&lun->lun_deve_lock));
974

975
			/*
976 977 978 979 980 981 982 983
			 * spc4r37 p.242:
			 * After an explicit target port asymmetric access
			 * state change, a device server shall establish a
			 * unit attention condition with the additional sense
			 * code set to ASYMMETRIC ACCESS STATE CHANGED for
			 * the initiator port associated with every I_T nexus
			 * other than the I_T nexus on which the SET TARGET
			 * PORT GROUPS command was received.
984
			 */
985 986
			if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
			     ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
987 988
			   (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
			    (tg_pt_gp->tg_pt_gp_alua_lun == lun))
989 990
				continue;

991 992 993 994 995 996 997 998 999
			/*
			 * se_deve->se_lun_acl pointer may be NULL for a
			 * entry created without explicit Node+MappedLUN ACLs
			 */
			if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
			    (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
				continue;

			core_scsi3_ua_allocate(se_deve, 0x2A,
1000 1001
				ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
		}
1002
		spin_unlock(&lun->lun_deve_lock);
1003 1004

		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1005
		percpu_ref_put(&lun->lun_ref);
1006 1007
	}
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
}

static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
		struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
	bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
			 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	/*
	 * Update the ALUA metadata buf that has been allocated in
	 * core_alua_do_port_transition(), this metadata will be written
	 * to struct file.
	 *
	 * Note that there is the case where we do not want to update the
	 * metadata when the saved metadata is being parsed in userspace
	 * when setting the existing port access state and access status.
	 *
	 * Also note that the failure to write out the ALUA metadata to
	 * struct file does NOT affect the actual ALUA transition.
	 */
	if (tg_pt_gp->tg_pt_gp_write_metadata) {
		mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
1032
		core_alua_update_tpg_primary_metadata(tg_pt_gp);
1033 1034 1035 1036 1037
		mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
	}
	/*
	 * Set the current primary ALUA access state to the requested new state
	 */
1038 1039
	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
		   tg_pt_gp->tg_pt_gp_alua_pending_state);
1040

1041
	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1042 1043
		" from primary access state %s to %s\n", (explicit) ? "explicit" :
		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1044 1045 1046
		tg_pt_gp->tg_pt_gp_id,
		core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
		core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
1047 1048 1049

	core_alua_queue_state_change_ua(tg_pt_gp);

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);

	if (tg_pt_gp->tg_pt_gp_transition_complete)
		complete(tg_pt_gp->tg_pt_gp_transition_complete);
}

static int core_alua_do_transition_tg_pt(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	int new_state,
	int explicit)
{
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
	DECLARE_COMPLETION_ONSTACK(wait);

	/* Nothing to be done here */
	if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
		return 0;

	if (new_state == ALUA_ACCESS_STATE_TRANSITION)
		return -EAGAIN;

	/*
	 * Flush any pending transitions
	 */
	if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs &&
	    atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
	    ALUA_ACCESS_STATE_TRANSITION) {
		/* Just in case */
		tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
		tg_pt_gp->tg_pt_gp_transition_complete = &wait;
		flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
		wait_for_completion(&wait);
		tg_pt_gp->tg_pt_gp_transition_complete = NULL;
		return 0;
	}

	/*
	 * Save the old primary ALUA access state, and set the current state
	 * to ALUA_ACCESS_STATE_TRANSITION.
	 */
	tg_pt_gp->tg_pt_gp_alua_previous_state =
		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
	tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;

	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
			ALUA_ACCESS_STATE_TRANSITION);
	tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
				ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
				ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;

1102 1103
	core_alua_queue_state_change_ua(tg_pt_gp);

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	/*
	 * Check for the optional ALUA primary state transition delay
	 */
	if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
		msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);

	/*
	 * Take a reference for workqueue item
	 */
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);

	if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
		unsigned long transition_tmo;

		transition_tmo = tg_pt_gp->tg_pt_gp_implicit_trans_secs * HZ;
		queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
				   &tg_pt_gp->tg_pt_gp_transition_work,
				   transition_tmo);
	} else {
		tg_pt_gp->tg_pt_gp_transition_complete = &wait;
		queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
				   &tg_pt_gp->tg_pt_gp_transition_work, 0);
		wait_for_completion(&wait);
		tg_pt_gp->tg_pt_gp_transition_complete = NULL;
	}
1131 1132 1133 1134 1135 1136 1137

	return 0;
}

int core_alua_do_port_transition(
	struct t10_alua_tg_pt_gp *l_tg_pt_gp,
	struct se_device *l_dev,
1138
	struct se_lun *l_lun,
1139 1140
	struct se_node_acl *l_nacl,
	int new_state,
1141
	int explicit)
1142 1143 1144 1145 1146
{
	struct se_device *dev;
	struct t10_alua_lu_gp *lu_gp;
	struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
	struct t10_alua_tg_pt_gp *tg_pt_gp;
1147
	int primary, valid_states, rc = 0;
1148

1149 1150
	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
	if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
		return -EINVAL;

	local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
	spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
	lu_gp = local_lu_gp_mem->lu_gp;
	atomic_inc(&lu_gp->lu_gp_ref_cnt);
	spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
	/*
	 * For storage objects that are members of the 'default_lu_gp',
	 * we only do transition on the passed *l_tp_pt_gp, and not
	 * on all of the matching target port groups IDs in default_lu_gp.
	 */
1163
	if (!lu_gp->lu_gp_id) {
1164 1165 1166 1167
		/*
		 * core_alua_do_transition_tg_pt() will always return
		 * success.
		 */
1168
		l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1169 1170 1171
		l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
		rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
						   new_state, explicit);
1172
		atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1173
		return rc;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	}
	/*
	 * For all other LU groups aside from 'default_lu_gp', walk all of
	 * the associated storage objects looking for a matching target port
	 * group ID from the local target port group.
	 */
	spin_lock(&lu_gp->lu_gp_lock);
	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
				lu_gp_mem_list) {

		dev = lu_gp_mem->lu_gp_mem_dev;
1185
		atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1186 1187
		spin_unlock(&lu_gp->lu_gp_lock);

1188
		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1189
		list_for_each_entry(tg_pt_gp,
1190
				&dev->t10_alua.tg_pt_gps_list,
1191 1192
				tg_pt_gp_list) {

1193
			if (!tg_pt_gp->tg_pt_gp_valid_id)
1194 1195 1196
				continue;
			/*
			 * If the target behavior port asymmetric access state
1197
			 * is changed for any target port group accessible via
1198 1199 1200 1201 1202 1203 1204 1205 1206
			 * a logical unit within a LU group, the target port
			 * behavior group asymmetric access states for the same
			 * target port group accessible via other logical units
			 * in that LU group will also change.
			 */
			if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
				continue;

			if (l_tg_pt_gp == tg_pt_gp) {
1207
				tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1208
				tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1209
			} else {
1210
				tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1211
				tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1212
			}
1213
			atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1214
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1215 1216 1217 1218
			/*
			 * core_alua_do_transition_tg_pt() will always return
			 * success.
			 */
1219 1220
			rc = core_alua_do_transition_tg_pt(tg_pt_gp,
					new_state, explicit);
1221

1222
			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1223
			atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1224 1225
			if (rc)
				break;
1226
		}
1227
		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1228 1229

		spin_lock(&lu_gp->lu_gp_lock);
1230
		atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1231 1232 1233
	}
	spin_unlock(&lu_gp->lu_gp_lock);

1234 1235 1236 1237 1238 1239 1240 1241
	if (!rc) {
		pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
			 " Group IDs: %hu %s transition to primary state: %s\n",
			 config_item_name(&lu_gp->lu_gp_group.cg_item),
			 l_tg_pt_gp->tg_pt_gp_id,
			 (explicit) ? "explicit" : "implicit",
			 core_alua_dump_state(new_state));
	}
1242

1243
	atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1244
	return rc;
1245 1246
}

1247
static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1248
{
1249
	struct se_portal_group *se_tpg = lun->lun_tpg;
1250
	unsigned char *md_buf;
1251
	char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1252 1253
	int len, rc;

1254 1255
	mutex_lock(&lun->lun_tg_pt_md_mutex);

1256 1257 1258
	md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
	if (!md_buf) {
		pr_err("Unable to allocate buf for ALUA metadata\n");
1259 1260
		rc = -ENOMEM;
		goto out_unlock;
1261
	}
1262 1263 1264 1265 1266

	memset(path, 0, ALUA_METADATA_PATH_LEN);
	memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);

	len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1267
			se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1268

1269
	if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1270
		snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1271
				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1272

1273
	len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1274
			"alua_tg_pt_status=0x%02x\n",
1275 1276
			atomic_read(&lun->lun_tg_pt_secondary_offline),
			lun->lun_tg_pt_secondary_stat);
1277

1278 1279
	snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu",
			db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1280
			lun->unpacked_lun);
1281

1282 1283 1284
	rc = core_alua_write_tpg_metadata(path, md_buf, len);
	kfree(md_buf);

1285 1286
out_unlock:
	mutex_unlock(&lun->lun_tg_pt_md_mutex);
1287
	return rc;
1288 1289 1290
}

static int core_alua_set_tg_pt_secondary_state(
1291
	struct se_lun *lun,
1292
	int explicit,
1293 1294 1295 1296 1297
	int offline)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	int trans_delay_msecs;

1298 1299
	spin_lock(&lun->lun_tg_pt_gp_lock);
	tg_pt_gp = lun->lun_tg_pt_gp;
1300
	if (!tg_pt_gp) {
1301
		spin_unlock(&lun->lun_tg_pt_gp_lock);
1302
		pr_err("Unable to complete secondary state"
1303
				" transition\n");
1304
		return -EINVAL;
1305 1306 1307 1308
	}
	trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
	/*
	 * Set the secondary ALUA target port access state to OFFLINE
1309
	 * or release the previously secondary state for struct se_lun
1310 1311
	 */
	if (offline)
1312
		atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1313
	else
1314
		atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1315

1316
	lun->lun_tg_pt_secondary_stat = (explicit) ?
1317 1318
			ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
			ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1319

1320
	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1321 1322
		" to secondary access state: %s\n", (explicit) ? "explicit" :
		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1323 1324
		tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");

1325
	spin_unlock(&lun->lun_tg_pt_gp_lock);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
	/*
	 * Do the optional transition delay after we set the secondary
	 * ALUA access state.
	 */
	if (trans_delay_msecs != 0)
		msleep_interruptible(trans_delay_msecs);
	/*
	 * See if we need to update the ALUA fabric port metadata for
	 * secondary state and status
	 */
1336 1337
	if (lun->lun_tg_pt_secondary_write_md)
		core_alua_update_tpg_secondary_metadata(lun);
1338 1339 1340 1341

	return 0;
}

1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
struct t10_alua_lba_map *
core_alua_allocate_lba_map(struct list_head *list,
			   u64 first_lba, u64 last_lba)
{
	struct t10_alua_lba_map *lba_map;

	lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
	if (!lba_map) {
		pr_err("Unable to allocate struct t10_alua_lba_map\n");
		return ERR_PTR(-ENOMEM);
	}
	INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
	lba_map->lba_map_first_lba = first_lba;
	lba_map->lba_map_last_lba = last_lba;

	list_add_tail(&lba_map->lba_map_list, list);
	return lba_map;
}

int
core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
			       int pg_id, int state)
{
	struct t10_alua_lba_map_member *lba_map_mem;

	list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
			    lba_map_mem_list) {
		if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
			pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
			return -EINVAL;
		}
	}

	lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
	if (!lba_map_mem) {
		pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
		return -ENOMEM;
	}
	lba_map_mem->lba_map_mem_alua_state = state;
	lba_map_mem->lba_map_mem_alua_pg_id = pg_id;

	list_add_tail(&lba_map_mem->lba_map_mem_list,
		      &lba_map->lba_map_mem_list);
	return 0;
}

void
core_alua_free_lba_map(struct list_head *lba_list)
{
	struct t10_alua_lba_map *lba_map, *lba_map_tmp;
	struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;

	list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
				 lba_map_list) {
		list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
					 &lba_map->lba_map_mem_list,
					 lba_map_mem_list) {
			list_del(&lba_map_mem->lba_map_mem_list);
			kmem_cache_free(t10_alua_lba_map_mem_cache,
					lba_map_mem);
		}
		list_del(&lba_map->lba_map_list);
		kmem_cache_free(t10_alua_lba_map_cache, lba_map);
	}
}

void
core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
		      int segment_size, int segment_mult)
{
	struct list_head old_lba_map_list;
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	int activate = 0, supported;

	INIT_LIST_HEAD(&old_lba_map_list);
	spin_lock(&dev->t10_alua.lba_map_lock);
	dev->t10_alua.lba_map_segment_size = segment_size;
	dev->t10_alua.lba_map_segment_multiplier = segment_mult;
	list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
	if (lba_map_list) {
		list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
		activate = 1;
	}
	spin_unlock(&dev->t10_alua.lba_map_lock);
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
			    tg_pt_gp_list) {

		if (!tg_pt_gp->tg_pt_gp_valid_id)
			continue;
		supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
		if (activate)
			supported |= ALUA_LBD_SUP;
		else
			supported &= ~ALUA_LBD_SUP;
		tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
	}
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
	core_alua_free_lba_map(&old_lba_map_list);
}

1443 1444 1445 1446 1447 1448
struct t10_alua_lu_gp *
core_alua_allocate_lu_gp(const char *name, int def_group)
{
	struct t10_alua_lu_gp *lu_gp;

	lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1449 1450
	if (!lu_gp) {
		pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1451
		return ERR_PTR(-ENOMEM);
1452
	}
1453
	INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1454 1455 1456 1457 1458
	INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
	spin_lock_init(&lu_gp->lu_gp_lock);
	atomic_set(&lu_gp->lu_gp_ref_cnt, 0);

	if (def_group) {
1459
		lu_gp->lu_gp_id = alua_lu_gps_counter++;
1460
		lu_gp->lu_gp_valid_id = 1;
1461
		alua_lu_gps_count++;
1462 1463 1464