target_core_alua.c 64.7 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 32
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
33
#include <asm/unaligned.h>
34 35

#include <target/target_core_base.h>
36 37
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
38 39
#include <target/target_core_configfs.h>

C
Christoph Hellwig 已提交
40
#include "target_core_internal.h"
41 42 43
#include "target_core_alua.h"
#include "target_core_ua.h"

44 45
static sense_reason_t core_alua_check_transition(int state, int valid,
						 int *primary);
46 47
static int core_alua_set_tg_pt_secondary_state(
		struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
48
		struct se_port *port, int explicit, int offline);
49

50 51
static char *core_alua_dump_state(int state);

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 150 151
	struct se_port *port;
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
152
	unsigned char *buf;
153 154
	u32 rd_len = 0, off;
	int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
155

156
	/*
157 158
	 * Skip over RESERVED area to first Target port group descriptor
	 * depending on the PARAMETER DATA FORMAT type..
159
	 */
160 161 162 163 164 165 166 167 168
	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");
169
		return TCM_INVALID_CDB_FIELD;
170
	}
171
	buf = transport_kmap_data_sg(cmd);
172 173
	if (!buf)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
174

175 176
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
177
			tg_pt_gp_list) {
178 179 180 181 182 183 184 185 186 187 188
		/*
		 * 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;
		}
189 190 191 192 193 194 195 196 197 198 199 200 201 202
		/*
		 * 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
		 */
203
		buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		/*
		 * 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);
		list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
				tg_pt_gp_mem_list) {
			port = tg_pt_gp_mem->tg_pt;
			/*
			 * Start Target Port descriptor format
			 *
			 * See spc4r17 section 6.2.7 Table 247
			 */
			off += 2; /* Skip over Obsolete */
			/*
			 * Set RELATIVE TARGET PORT IDENTIFIER
			 */
			buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
			buf[off++] = (port->sep_rtpi & 0xff);
			rd_len += 4;
		}
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
	}
244
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
245 246 247
	/*
	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
	 */
248
	put_unaligned_be32(rd_len, &buf[0]);
249

250 251 252 253 254 255
	/*
	 * Fill in the Extended header parameter data format if requested
	 */
	if (ext_hdr != 0) {
		buf[4] = 0x10;
		/*
256
		 * Set the implicit transition time (in seconds) for the application
257 258 259 260 261 262 263 264 265 266 267 268
		 * 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.
		 */
		port = cmd->se_lun->lun_sep;
		tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
		if (tg_pt_gp_mem) {
			spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
			tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
			if (tg_pt_gp)
269
				buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
270 271 272
			spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
		}
	}
273
	transport_kunmap_data_sg(cmd);
274

275
	target_complete_cmd(cmd, GOOD);
276 277 278 279
	return 0;
}

/*
280
 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
281 282 283
 *
 * See spc4r17 section 6.35
 */
284 285
sense_reason_t
target_emulate_set_target_port_groups(struct se_cmd *cmd)
286
{
287
	struct se_device *dev = cmd->se_dev;
288 289
	struct se_port *port, *l_port = cmd->se_lun->lun_sep;
	struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
290 291
	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
292 293
	unsigned char *buf;
	unsigned char *ptr;
294
	sense_reason_t rc = TCM_NO_SENSE;
295
	u32 len = 4; /* Skip over RESERVED area in header */
296
	int alua_access_state, primary = 0, valid_states;
297 298
	u16 tg_pt_id, rtpi;

299 300 301
	if (!l_port)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;

302 303 304
	if (cmd->data_length < 4) {
		pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
			" small\n", cmd->data_length);
305
		return TCM_INVALID_PARAMETER_LIST;
306 307
	}

308
	buf = transport_kmap_data_sg(cmd);
309 310
	if (!buf)
		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
311

312
	/*
313
	 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
314 315 316
	 * for the local tg_pt_gp.
	 */
	l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
317 318
	if (!l_tg_pt_gp_mem) {
		pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
319
		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
320
		goto out;
321 322 323
	}
	spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
	l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
324
	if (!l_tg_pt_gp) {
325
		spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
326
		pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
327
		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
328
		goto out;
329 330 331
	}
	spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);

332
	if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
333
		pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
334
				" while TPGS_EXPLICIT_ALUA is disabled\n");
335
		rc = TCM_UNSUPPORTED_SCSI_OPCODE;
336
		goto out;
337
	}
338
	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
339

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

342
	while (len < cmd->data_length) {
343
		bool found = false;
344 345 346 347 348 349
		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.
		 */
350 351
		rc = core_alua_check_transition(alua_access_state,
						valid_states, &primary);
352
		if (rc) {
353 354 355 356 357 358 359 360 361 362
			/*
			 * 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.
			 */
363
			goto out;
364
		}
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379
		/*
		 * 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) {
380
			tg_pt_id = get_unaligned_be16(ptr + 2);
381 382 383 384
			/*
			 * Locate the matching target port group ID from
			 * the global tg_pt_gp list
			 */
385
			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
386
			list_for_each_entry(tg_pt_gp,
387
					&dev->t10_alua.tg_pt_gps_list,
388
					tg_pt_gp_list) {
389
				if (!tg_pt_gp->tg_pt_gp_valid_id)
390 391 392 393 394 395 396
					continue;

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

				atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
				smp_mb__after_atomic_inc();
397

398
				spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
399

400
				if (!core_alua_do_port_transition(tg_pt_gp,
401
						dev, l_port, nacl,
402 403
						alua_access_state, 1))
					found = true;
404

405
				spin_lock(&dev->t10_alua.tg_pt_gps_lock);
406 407 408 409
				atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
				smp_mb__after_atomic_dec();
				break;
			}
410
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
411 412
		} else {
			/*
H
Hannes Reinecke 已提交
413
			 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
414 415 416
			 * the Target Port in question for the the incoming
			 * SET_TARGET_PORT_GROUPS op.
			 */
417
			rtpi = get_unaligned_be16(ptr + 2);
418
			/*
419
			 * Locate the matching relative target port identifier
420 421 422 423 424 425 426 427 428
			 * for the struct se_device storage object.
			 */
			spin_lock(&dev->se_port_lock);
			list_for_each_entry(port, &dev->dev_sep_list,
							sep_list) {
				if (port->sep_rtpi != rtpi)
					continue;

				tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
429

430 431
				spin_unlock(&dev->se_port_lock);

432 433 434
				if (!core_alua_set_tg_pt_secondary_state(
						tg_pt_gp_mem, port, 1, 1))
					found = true;
435 436 437 438 439

				spin_lock(&dev->se_port_lock);
				break;
			}
			spin_unlock(&dev->se_port_lock);
440 441 442 443 444
		}

		if (!found) {
			rc = TCM_INVALID_PARAMETER_LIST;
			goto out;
445 446 447 448 449 450
		}

		ptr += 4;
		len += 4;
	}

451
out:
452
	transport_kunmap_data_sg(cmd);
453 454 455
	if (!rc)
		target_complete_cmd(cmd, GOOD);
	return rc;
456 457
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
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(
475 476
	struct se_cmd *cmd,
	unsigned char *cdb,
477
	int nonop_delay_msecs)
478 479 480 481 482 483 484 485 486 487
{
	/*
	 * 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;
}

488 489
static inline int core_alua_state_lba_dependent(
	struct se_cmd *cmd,
490
	struct t10_alua_tg_pt_gp *tg_pt_gp)
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
{
	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;
516
				start_lba = do_div(tmp, segment_size * segment_mult);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

				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);
536
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
537 538 539 540 541 542 543 544 545 546
			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);
547
				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
548 549 550
				return 1;
			case ALUA_ACCESS_STATE_UNAVAILABLE:
				spin_unlock(&dev->t10_alua.lba_map_lock);
551
				set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
552 553 554 555 556 557 558 559 560 561
				return 1;
			default:
				break;
			}
		}
	}
	spin_unlock(&dev->t10_alua.lba_map_lock);
	return 0;
}

562 563
static inline int core_alua_state_standby(
	struct se_cmd *cmd,
564
	unsigned char *cdb)
565 566 567 568 569 570 571 572 573 574 575 576 577 578
{
	/*
	 * 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:
579
	case READ_CAPACITY:
580
		return 0;
581 582 583 584 585 586 587 588
	case SERVICE_ACTION_IN:
		switch (cdb[1] & 0x1f) {
		case SAI_READ_CAPACITY_16:
			return 0;
		default:
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
			return 1;
		}
589
	case MAINTENANCE_IN:
590
		switch (cdb[1] & 0x1f) {
591 592 593
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
594
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
595 596 597 598 599 600 601
			return 1;
		}
	case MAINTENANCE_OUT:
		switch (cdb[1]) {
		case MO_SET_TARGET_PGS:
			return 0;
		default:
602
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
603 604 605 606 607 608 609 610 611
			return 1;
		}
	case REQUEST_SENSE:
	case PERSISTENT_RESERVE_IN:
	case PERSISTENT_RESERVE_OUT:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
612
		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
613 614 615 616 617 618 619 620
		return 1;
	}

	return 0;
}

static inline int core_alua_state_unavailable(
	struct se_cmd *cmd,
621
	unsigned char *cdb)
622 623 624 625 626 627 628 629
{
	/*
	 * 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:
630
		return 0;
631
	case MAINTENANCE_IN:
632
		switch (cdb[1] & 0x1f) {
633 634 635
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
636
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
637 638 639 640 641 642 643
			return 1;
		}
	case MAINTENANCE_OUT:
		switch (cdb[1]) {
		case MO_SET_TARGET_PGS:
			return 0;
		default:
644
			set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
645 646 647 648 649 650 651
			return 1;
		}
	case REQUEST_SENSE:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
652
		set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
653 654 655 656 657 658 659 660
		return 1;
	}

	return 0;
}

static inline int core_alua_state_transition(
	struct se_cmd *cmd,
661
	unsigned char *cdb)
662 663
{
	/*
H
Hannes Reinecke 已提交
664
	 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
665 666 667 668 669
	 * spc4r17 section 5.9.2.5
	 */
	switch (cdb[0]) {
	case INQUIRY:
	case REPORT_LUNS:
670
		return 0;
671
	case MAINTENANCE_IN:
672
		switch (cdb[1] & 0x1f) {
673 674 675
		case MI_REPORT_TARGET_PGS:
			return 0;
		default:
676
			set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
677 678 679 680 681 682 683
			return 1;
		}
	case REQUEST_SENSE:
	case READ_BUFFER:
	case WRITE_BUFFER:
		return 0;
	default:
684
		set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
685 686 687 688 689 690 691
		return 1;
	}

	return 0;
}

/*
H
Hannes Reinecke 已提交
692
 * return 1: Is used to signal LUN not accessible, and check condition/not ready
693
 * return 0: Used to signal success
H
Hannes Reinecke 已提交
694
 * return -1: Used to signal failure, and invalid cdb field
695
 */
696 697
sense_reason_t
target_alua_state_check(struct se_cmd *cmd)
698
{
C
Christoph Hellwig 已提交
699 700
	struct se_device *dev = cmd->se_dev;
	unsigned char *cdb = cmd->t_task_cdb;
701
	struct se_lun *lun = cmd->se_lun;
702 703 704 705
	struct se_port *port = lun->lun_sep;
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
	int out_alua_state, nonop_delay_msecs;
C
Christoph Hellwig 已提交
706 707 708 709 710

	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
		return 0;
	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
		return 0;
711

712
	if (!port)
713 714 715 716 717 718
		return 0;
	/*
	 * First, check for a struct se_port specific secondary ALUA target port
	 * access state: OFFLINE
	 */
	if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
719
		pr_debug("ALUA: Got secondary offline status for local"
720
				" target port\n");
721 722
		set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
		return TCM_CHECK_CONDITION_NOT_READY;
723 724 725 726 727 728 729 730
	}
	 /*
	 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
	 * ALUA target port group, to obtain current ALUA access state.
	 * Otherwise look for the underlying struct se_device association with
	 * a ALUA logical unit group.
	 */
	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
731 732 733
	if (!tg_pt_gp_mem)
		return 0;

734 735 736 737 738 739
	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
	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;
	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	/*
740
	 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
L
Lucas De Marchi 已提交
741
	 * statement so the compiler knows explicitly to check this case first.
742 743 744
	 * For the Optimized ALUA access state case, we want to process the
	 * incoming fabric cmd ASAP..
	 */
745
	if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
746 747 748 749
		return 0;

	switch (out_alua_state) {
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
750
		core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
C
Christoph Hellwig 已提交
751
		break;
752
	case ALUA_ACCESS_STATE_STANDBY:
753 754
		if (core_alua_state_standby(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
C
Christoph Hellwig 已提交
755
		break;
756
	case ALUA_ACCESS_STATE_UNAVAILABLE:
757 758
		if (core_alua_state_unavailable(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
C
Christoph Hellwig 已提交
759
		break;
760
	case ALUA_ACCESS_STATE_TRANSITION:
761 762
		if (core_alua_state_transition(cmd, cdb))
			return TCM_CHECK_CONDITION_NOT_READY;
C
Christoph Hellwig 已提交
763
		break;
764
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
765 766
		if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
			return TCM_CHECK_CONDITION_NOT_READY;
767
		break;
768 769 770 771 772 773
	/*
	 * OFFLINE is a secondary ALUA target port group access state, that is
	 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
	 */
	case ALUA_ACCESS_STATE_OFFLINE:
	default:
774
		pr_err("Unknown ALUA access state: 0x%02x\n",
775
				out_alua_state);
776
		return TCM_INVALID_CDB_FIELD;
777 778
	}

779
	return 0;
780 781 782
}

/*
783
 * Check implicit and explicit ALUA state change request.
784
 */
785
static sense_reason_t
786
core_alua_check_transition(int state, int valid, int *primary)
787
{
788 789 790 791
	/*
	 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
	 * defined as primary target port asymmetric access states.
	 */
792
	switch (state) {
793
	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
794 795 796 797
		if (!(valid & ALUA_AO_SUP))
			goto not_supported;
		*primary = 1;
		break;
798
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
799 800 801 802
		if (!(valid & ALUA_AN_SUP))
			goto not_supported;
		*primary = 1;
		break;
803
	case ALUA_ACCESS_STATE_STANDBY:
804 805 806 807
		if (!(valid & ALUA_S_SUP))
			goto not_supported;
		*primary = 1;
		break;
808
	case ALUA_ACCESS_STATE_UNAVAILABLE:
809 810
		if (!(valid & ALUA_U_SUP))
			goto not_supported;
811 812
		*primary = 1;
		break;
813 814 815 816 817
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
		if (!(valid & ALUA_LBD_SUP))
			goto not_supported;
		*primary = 1;
		break;
818 819 820 821 822
	case ALUA_ACCESS_STATE_OFFLINE:
		/*
		 * OFFLINE state is defined as a secondary target port
		 * asymmetric access state.
		 */
823 824
		if (!(valid & ALUA_O_SUP))
			goto not_supported;
825 826
		*primary = 0;
		break;
827 828 829 830 831 832
	case ALUA_ACCESS_STATE_TRANSITION:
		/*
		 * Transitioning is set internally, and
		 * cannot be selected manually.
		 */
		goto not_supported;
833
	default:
834
		pr_err("Unknown ALUA access state: 0x%02x\n", state);
835
		return TCM_INVALID_PARAMETER_LIST;
836 837 838
	}

	return 0;
839 840 841 842 843

not_supported:
	pr_err("ALUA access state %s not supported",
	       core_alua_dump_state(state));
	return TCM_INVALID_PARAMETER_LIST;
844 845 846 847 848
}

static char *core_alua_dump_state(int state)
{
	switch (state) {
849
	case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
850 851 852
		return "Active/Optimized";
	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
		return "Active/NonOptimized";
853 854
	case ALUA_ACCESS_STATE_LBA_DEPENDENT:
		return "LBA Dependent";
855 856 857 858 859 860
	case ALUA_ACCESS_STATE_STANDBY:
		return "Standby";
	case ALUA_ACCESS_STATE_UNAVAILABLE:
		return "Unavailable";
	case ALUA_ACCESS_STATE_OFFLINE:
		return "Offline";
861 862
	case ALUA_ACCESS_STATE_TRANSITION:
		return "Transitioning";
863 864 865 866 867 868 869 870 871 872 873 874
	default:
		return "Unknown";
	}

	return NULL;
}

char *core_alua_dump_status(int status)
{
	switch (status) {
	case ALUA_STATUS_NONE:
		return "None";
875 876 877 878
	case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
		return "Altered by Explicit STPG";
	case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
		return "Altered by Implicit ALUA";
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	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
	 */
901
	if (!cmd->alua_nonop_delay)
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
		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);

/*
 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
 *
 */
static int core_alua_write_tpg_metadata(
	const char *path,
	unsigned char *md_buf,
	u32 md_buf_len)
{
921 922
	struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
	int ret;
923

924 925
	if (IS_ERR(file)) {
		pr_err("filp_open(%s) for ALUA metadata failed\n", path);
926 927
		return -ENODEV;
	}
928 929
	ret = kernel_write(file, md_buf, md_buf_len, 0);
	if (ret < 0)
930
		pr_err("Error writing ALUA metadata file: %s\n", path);
931
	fput(file);
932
	return (ret < 0) ? -EIO : 0;
933 934 935 936 937 938
}

/*
 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
 */
static int core_alua_update_tpg_primary_metadata(
939
	struct t10_alua_tg_pt_gp *tg_pt_gp)
940
{
941
	unsigned char *md_buf;
942
	struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
943
	char path[ALUA_METADATA_PATH_LEN];
944 945 946 947 948 949 950
	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;
	}
951 952 953

	memset(path, 0, ALUA_METADATA_PATH_LEN);

954
	len = snprintf(md_buf, ALUA_MD_BUF_LEN,
955 956 957
			"tg_pt_gp_id=%hu\n"
			"alua_access_state=0x%02x\n"
			"alua_access_status=0x%02x\n",
958 959
			tg_pt_gp->tg_pt_gp_id,
			tg_pt_gp->tg_pt_gp_alua_pending_state,
960 961 962 963 964 965
			tg_pt_gp->tg_pt_gp_alua_access_status);

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

966 967 968
	rc = core_alua_write_tpg_metadata(path, md_buf, len);
	kfree(md_buf);
	return rc;
969 970
}

971
static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
972
{
973 974 975
	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;
976 977 978 979
	struct se_dev_entry *se_deve;
	struct se_lun_acl *lacl;
	struct se_port *port;
	struct t10_alua_tg_pt_gp_member *mem;
980 981
	bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
			 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
982 983 984 985 986 987 988 989 990 991

	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
	list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
				tg_pt_gp_mem_list) {
		port = mem->tg_pt;
		/*
		 * 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
H
Hannes Reinecke 已提交
992
		 * ACCESS STATE CHANGED.
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		 *
		 * 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
		 */
		atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
		smp_mb__after_atomic_inc();
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);

		spin_lock_bh(&port->sep_alua_lock);
		list_for_each_entry(se_deve, &port->sep_alua_list,
					alua_port_list) {
			lacl = se_deve->se_lun_acl;
			/*
			 * se_deve->se_lun_acl pointer may be NULL for a
1011
			 * entry created without explicit Node+MappedLUN ACLs
1012
			 */
1013
			if (!lacl)
1014 1015
				continue;

1016 1017 1018 1019 1020 1021
			if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
			     ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
			   (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
			    (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl) &&
			   (tg_pt_gp->tg_pt_gp_alua_port != NULL) &&
			    (tg_pt_gp->tg_pt_gp_alua_port == port))
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
				continue;

			core_scsi3_ua_allocate(lacl->se_lun_nacl,
				se_deve->mapped_lun, 0x2A,
				ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
		}
		spin_unlock_bh(&port->sep_alua_lock);

		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
		atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
		smp_mb__after_atomic_dec();
	}
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
	/*
	 * 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);
1049
		core_alua_update_tpg_primary_metadata(tg_pt_gp);
1050 1051 1052 1053 1054
		mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
	}
	/*
	 * Set the current primary ALUA access state to the requested new state
	 */
1055 1056
	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
		   tg_pt_gp->tg_pt_gp_alua_pending_state);
1057

1058
	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1059 1060
		" from primary access state %s to %s\n", (explicit) ? "explicit" :
		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1061 1062 1063
		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));
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 1102 1103 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 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
	smp_mb__after_atomic_dec();
	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;

	/*
	 * 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);
	smp_mb__after_atomic_inc();
	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;
	}
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

	return 0;
}

int core_alua_do_port_transition(
	struct t10_alua_tg_pt_gp *l_tg_pt_gp,
	struct se_device *l_dev,
	struct se_port *l_port,
	struct se_node_acl *l_nacl,
	int new_state,
1155
	int explicit)
1156 1157 1158 1159 1160
{
	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;
1161
	int primary, valid_states, rc = 0;
1162

1163 1164
	valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
	if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
		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);
	smp_mb__after_atomic_inc();
	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.
	 */
1178
	if (!lu_gp->lu_gp_id) {
1179 1180 1181 1182
		/*
		 * core_alua_do_transition_tg_pt() will always return
		 * success.
		 */
1183 1184 1185 1186
		l_tg_pt_gp->tg_pt_gp_alua_port = l_port;
		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);
1187 1188
		atomic_dec(&lu_gp->lu_gp_ref_cnt);
		smp_mb__after_atomic_dec();
1189
		return rc;
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	}
	/*
	 * 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;
		atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
		smp_mb__after_atomic_inc();
		spin_unlock(&lu_gp->lu_gp_lock);

1205
		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1206
		list_for_each_entry(tg_pt_gp,
1207
				&dev->t10_alua.tg_pt_gps_list,
1208 1209
				tg_pt_gp_list) {

1210
			if (!tg_pt_gp->tg_pt_gp_valid_id)
1211 1212 1213
				continue;
			/*
			 * If the target behavior port asymmetric access state
H
Hannes Reinecke 已提交
1214
			 * is changed for any target port group accessible via
1215 1216 1217 1218 1219 1220 1221 1222 1223
			 * 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) {
1224 1225
				tg_pt_gp->tg_pt_gp_alua_port = l_port;
				tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1226
			} else {
1227 1228
				tg_pt_gp->tg_pt_gp_alua_port = NULL;
				tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1229 1230 1231
			}
			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
			smp_mb__after_atomic_inc();
1232
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1233 1234 1235 1236
			/*
			 * core_alua_do_transition_tg_pt() will always return
			 * success.
			 */
1237 1238
			rc = core_alua_do_transition_tg_pt(tg_pt_gp,
					new_state, explicit);
1239

1240
			spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1241 1242
			atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
			smp_mb__after_atomic_dec();
1243 1244
			if (rc)
				break;
1245
		}
1246
		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1247 1248 1249 1250 1251 1252 1253

		spin_lock(&lu_gp->lu_gp_lock);
		atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
		smp_mb__after_atomic_dec();
	}
	spin_unlock(&lu_gp->lu_gp_lock);

1254 1255 1256 1257 1258 1259 1260 1261
	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));
	}
1262 1263 1264

	atomic_dec(&lu_gp->lu_gp_ref_cnt);
	smp_mb__after_atomic_dec();
1265
	return rc;
1266 1267 1268 1269 1270 1271 1272
}

/*
 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
 */
static int core_alua_update_tpg_secondary_metadata(
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1273
	struct se_port *port)
1274
{
1275
	unsigned char *md_buf;
1276 1277
	struct se_portal_group *se_tpg = port->sep_tpg;
	char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1278 1279 1280 1281 1282 1283 1284
	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;
	}
1285 1286 1287 1288 1289

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

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

1292
	if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1293
		snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1294
				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1295

1296
	len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1297 1298 1299 1300 1301
			"alua_tg_pt_status=0x%02x\n",
			atomic_read(&port->sep_tg_pt_secondary_offline),
			port->sep_tg_pt_secondary_stat);

	snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1302
			se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1303 1304
			port->sep_lun->unpacked_lun);

1305 1306 1307 1308
	rc = core_alua_write_tpg_metadata(path, md_buf, len);
	kfree(md_buf);

	return rc;
1309 1310 1311 1312 1313
}

static int core_alua_set_tg_pt_secondary_state(
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
	struct se_port *port,
1314
	int explicit,
1315 1316 1317 1318 1319 1320 1321
	int offline)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	int trans_delay_msecs;

	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1322
	if (!tg_pt_gp) {
1323
		spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1324
		pr_err("Unable to complete secondary state"
1325
				" transition\n");
1326
		return -EINVAL;
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	}
	trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
	/*
	 * Set the secondary ALUA target port access state to OFFLINE
	 * or release the previously secondary state for struct se_port
	 */
	if (offline)
		atomic_set(&port->sep_tg_pt_secondary_offline, 1);
	else
		atomic_set(&port->sep_tg_pt_secondary_offline, 0);

1338 1339 1340
	port->sep_tg_pt_secondary_stat = (explicit) ?
			ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
			ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1341

1342
	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1343 1344
		" to secondary access state: %s\n", (explicit) ? "explicit" :
		"implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
		tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");

	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	/*
	 * 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
	 */
	if (port->sep_tg_pt_secondary_write_md) {
		mutex_lock(&port->sep_tg_pt_md_mutex);
1360
		core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port);
1361 1362 1363 1364 1365 1366
		mutex_unlock(&port->sep_tg_pt_md_mutex);
	}

	return 0;
}

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 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
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);
}

1468 1469 1470 1471 1472 1473
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);
1474 1475
	if (!lu_gp) {
		pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1476
		return ERR_PTR(-ENOMEM);
1477
	}
1478
	INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1479 1480 1481 1482 1483
	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) {
1484
		lu_gp->lu_gp_id = alua_lu_gps_counter++;
1485
		lu_gp->lu_gp_valid_id = 1;
1486
		alua_lu_gps_count++;
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	}

	return lu_gp;
}

int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
{
	struct t10_alua_lu_gp *lu_gp_tmp;
	u16 lu_gp_id_tmp;
	/*
	 * The lu_gp->lu_gp_id may only be set once..
	 */
	if (lu_gp->lu_gp_valid_id) {
1500
		pr_warn("ALUA LU Group already has a valid ID,"
1501
			" ignoring request\n");
1502
		return -EINVAL;
1503 1504
	}

1505 1506
	spin_lock(&lu_gps_lock);
	if (alua_lu_gps_count == 0x0000ffff) {
1507
		pr_err("Maximum ALUA alua_lu_gps_count:"
1508
				" 0x0000ffff reached\n");
1509
		spin_unlock(&lu_gps_lock);
1510
		kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1511
		return -ENOSPC;
1512 1513 1514
	}
again:
	lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1515
				alua_lu_gps_counter++;
1516

1517
	list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1518
		if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1519
			if (!lu_gp_id)
1520 1521
				goto again;

1522
			pr_warn("ALUA Logical Unit Group ID: %hu"
1523 1524
				" already exists, ignoring request\n",
				lu_gp_id);
1525 1526
			spin_unlock(&lu_gps_lock);
			return -EINVAL;
1527 1528 1529 1530 1531
		}
	}

	lu_gp->lu_gp_id = lu_gp_id_tmp;
	lu_gp->lu_gp_valid_id = 1;
1532 1533 1534
	list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
	alua_lu_gps_count++;
	spin_unlock(&lu_gps_lock);
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544

	return 0;
}

static struct t10_alua_lu_gp_member *
core_alua_allocate_lu_gp_mem(struct se_device *dev)
{
	struct t10_alua_lu_gp_member *lu_gp_mem;

	lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1545 1546
	if (!lu_gp_mem) {
		pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
		return ERR_PTR(-ENOMEM);
	}
	INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
	spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
	atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);

	lu_gp_mem->lu_gp_mem_dev = dev;
	dev->dev_alua_lu_gp_mem = lu_gp_mem;

	return lu_gp_mem;
}

void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
{
	struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
	/*
	 * Once we have reached this point, config_item_put() has
	 * already been called from target_core_alua_drop_lu_gp().
	 *
	 * Here, we remove the *lu_gp from the global list so that
	 * no associations can be made while we are releasing
	 * struct t10_alua_lu_gp.
	 */
1570 1571 1572 1573
	spin_lock(&lu_gps_lock);
	list_del(&lu_gp->lu_gp_node);
	alua_lu_gps_count--;
	spin_unlock(&lu_gps_lock);
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
	/*
	 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
	 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
	 * released with core_alua_put_lu_gp_from_name()
	 */
	while (atomic_read(&lu_gp->lu_gp_ref_cnt))
		cpu_relax();
	/*
	 * Release reference to struct t10_alua_lu_gp * from all associated
	 * struct se_device.
	 */
	spin_lock(&lu_gp->lu_gp_lock);
	list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
				&lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
		if (lu_gp_mem->lu_gp_assoc) {
			list_del(&lu_gp_mem->lu_gp_mem_list);
			lu_gp->lu_gp_members--;
			lu_gp_mem->lu_gp_assoc = 0;
		}
		spin_unlock(&lu_gp->lu_gp_lock);
		/*
		 *
L
Lucas De Marchi 已提交
1596
		 * lu_gp_mem is associated with a single
1597 1598 1599 1600
		 * struct se_device->dev_alua_lu_gp_mem, and is released when
		 * struct se_device is released via core_alua_free_lu_gp_mem().
		 *
		 * If the passed lu_gp does NOT match the default_lu_gp, assume
H
Hannes Reinecke 已提交
1601
		 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1602 1603
		 */
		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1604
		if (lu_gp != default_lu_gp)
1605
			__core_alua_attach_lu_gp_mem(lu_gp_mem,
1606
					default_lu_gp);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
		else
			lu_gp_mem->lu_gp = NULL;
		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

		spin_lock(&lu_gp->lu_gp_lock);
	}
	spin_unlock(&lu_gp->lu_gp_lock);

	kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
}

void core_alua_free_lu_gp_mem(struct se_device *dev)
{
	struct t10_alua_lu_gp *lu_gp;
	struct t10_alua_lu_gp_member *lu_gp_mem;

	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1624
	if (!lu_gp_mem)
1625 1626 1627 1628 1629 1630 1631
		return;

	while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
		cpu_relax();

	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
	lu_gp = lu_gp_mem->lu_gp;
1632
	if (lu_gp) {
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
		spin_lock(&lu_gp->lu_gp_lock);
		if (lu_gp_mem->lu_gp_assoc) {
			list_del(&lu_gp_mem->lu_gp_mem_list);
			lu_gp->lu_gp_members--;
			lu_gp_mem->lu_gp_assoc = 0;
		}
		spin_unlock(&lu_gp->lu_gp_lock);
		lu_gp_mem->lu_gp = NULL;
	}
	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

	kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
}

struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
{
	struct t10_alua_lu_gp *lu_gp;
	struct config_item *ci;

1652 1653
	spin_lock(&lu_gps_lock);
	list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1654
		if (!lu_gp->lu_gp_valid_id)
1655 1656
			continue;
		ci = &lu_gp->lu_gp_group.cg_item;
1657
		if (!strcmp(config_item_name(ci), name)) {
1658
			atomic_inc(&lu_gp->lu_gp_ref_cnt);
1659
			spin_unlock(&lu_gps_lock);
1660 1661 1662
			return lu_gp;
		}
	}
1663
	spin_unlock(&lu_gps_lock);
1664 1665 1666 1667 1668 1669

	return NULL;
}

void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
{
1670
	spin_lock(&lu_gps_lock);
1671
	atomic_dec(&lu_gp->lu_gp_ref_cnt);
1672
	spin_unlock(&lu_gps_lock);
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
}

/*
 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
 */
void __core_alua_attach_lu_gp_mem(
	struct t10_alua_lu_gp_member *lu_gp_mem,
	struct t10_alua_lu_gp *lu_gp)
{
	spin_lock(&lu_gp->lu_gp_lock);
	lu_gp_mem->lu_gp = lu_gp;
	lu_gp_mem->lu_gp_assoc = 1;
	list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
	lu_gp->lu_gp_members++;
	spin_unlock(&lu_gp->lu_gp_lock);
}

/*
 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
 */
void __core_alua_drop_lu_gp_mem(
	struct t10_alua_lu_gp_member *lu_gp_mem,
	struct t10_alua_lu_gp *lu_gp)
{
	spin_lock(&lu_gp->lu_gp_lock);
	list_del(&lu_gp_mem->lu_gp_mem_list);
	lu_gp_mem->lu_gp = NULL;
	lu_gp_mem->lu_gp_assoc = 0;
	lu_gp->lu_gp_members--;
	spin_unlock(&lu_gp->lu_gp_lock);
}

1705 1706
struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
		const char *name, int def_group)
1707 1708 1709 1710
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;

	tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1711 1712
	if (!tg_pt_gp) {
		pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1713 1714 1715 1716 1717 1718 1719
		return NULL;
	}
	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
	mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
	spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
	atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1720 1721
	INIT_DELAYED_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
			  core_alua_do_transition_tg_pt_work);
1722
	tg_pt_gp->tg_pt_gp_dev = dev;
1723
	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1724
		ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
1725
	/*
1726
	 * Enable both explicit and implicit ALUA support by default
1727 1728
	 */
	tg_pt_gp->tg_pt_gp_alua_access_type =
1729
			TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
1730 1731 1732 1733 1734
	/*
	 * Set the default Active/NonOptimized Delay in milliseconds
	 */
	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
	tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1735
	tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
1736

1737 1738 1739 1740 1741 1742 1743
	/*
	 * Enable all supported states
	 */
	tg_pt_gp->tg_pt_gp_alua_supported_states =
	    ALUA_T_SUP | ALUA_O_SUP |
	    ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;

1744
	if (def_group) {
1745
		spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1746
		tg_pt_gp->tg_pt_gp_id =
1747
				dev->t10_alua.alua_tg_pt_gps_counter++;
1748
		tg_pt_gp->tg_pt_gp_valid_id = 1;
1749
		dev->t10_alua.alua_tg_pt_gps_count++;
1750
		list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1751 1752
			      &dev->t10_alua.tg_pt_gps_list);
		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1753 1754 1755 1756 1757 1758 1759 1760 1761
	}

	return tg_pt_gp;
}

int core_alua_set_tg_pt_gp_id(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	u16 tg_pt_gp_id)
{
1762
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1763 1764
	struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
	u16 tg_pt_gp_id_tmp;
1765

1766 1767 1768 1769
	/*
	 * The tg_pt_gp->tg_pt_gp_id may only be set once..
	 */
	if (tg_pt_gp->tg_pt_gp_valid_id) {
1770
		pr_warn("ALUA TG PT Group already has a valid ID,"
1771
			" ignoring request\n");
1772
		return -EINVAL;
1773 1774
	}

1775 1776
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1777
		pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1778
			" 0x0000ffff reached\n");
1779
		spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1780
		kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1781
		return -ENOSPC;
1782 1783 1784
	}
again:
	tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1785
			dev->t10_alua.alua_tg_pt_gps_counter++;
1786

1787
	list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1788 1789
			tg_pt_gp_list) {
		if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1790
			if (!tg_pt_gp_id)
1791 1792
				goto again;

1793
			pr_err("ALUA Target Port Group ID: %hu already"
1794
				" exists, ignoring request\n", tg_pt_gp_id);
1795
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1796
			return -EINVAL;
1797 1798 1799 1800 1801 1802
		}
	}

	tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
	tg_pt_gp->tg_pt_gp_valid_id = 1;
	list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1803 1804 1805
			&dev->t10_alua.tg_pt_gps_list);
	dev->t10_alua.alua_tg_pt_gps_count++;
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

	return 0;
}

struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
	struct se_port *port)
{
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;

	tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
				GFP_KERNEL);
1817 1818
	if (!tg_pt_gp_mem) {
		pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
		return ERR_PTR(-ENOMEM);
	}
	INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
	spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);

	tg_pt_gp_mem->tg_pt = port;
	port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;

	return tg_pt_gp_mem;
}

void core_alua_free_tg_pt_gp(
	struct t10_alua_tg_pt_gp *tg_pt_gp)
{
1834
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1835
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1836

1837 1838 1839 1840 1841
	/*
	 * Once we have reached this point, config_item_put() has already
	 * been called from target_core_alua_drop_tg_pt_gp().
	 *
	 * Here we remove *tg_pt_gp from the global list so that
H
Hannes Reinecke 已提交
1842
	 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1843 1844
	 * can be made while we are releasing struct t10_alua_tg_pt_gp.
	 */
1845
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1846
	list_del(&tg_pt_gp->tg_pt_gp_list);
1847 1848 1849
	dev->t10_alua.alua_tg_pt_gps_counter--;
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);

1850 1851
	flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);

1852 1853 1854 1855 1856 1857 1858 1859
	/*
	 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
	 * core_alua_get_tg_pt_gp_by_name() in
	 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
	 * to be released with core_alua_put_tg_pt_gp_from_name().
	 */
	while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
		cpu_relax();
1860

1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	/*
	 * Release reference to struct t10_alua_tg_pt_gp from all associated
	 * struct se_port.
	 */
	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
	list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
			&tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
		if (tg_pt_gp_mem->tg_pt_gp_assoc) {
			list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
			tg_pt_gp->tg_pt_gp_members--;
			tg_pt_gp_mem->tg_pt_gp_assoc = 0;
		}
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
		/*
L
Lucas De Marchi 已提交
1875
		 * tg_pt_gp_mem is associated with a single
1876 1877 1878 1879
		 * se_port->sep_alua_tg_pt_gp_mem, and is released via
		 * core_alua_free_tg_pt_gp_mem().
		 *
		 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
H
Hannes Reinecke 已提交
1880
		 * assume we want to re-associate a given tg_pt_gp_mem with
1881 1882 1883
		 * default_tg_pt_gp.
		 */
		spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1884
		if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1885
			__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1886
					dev->t10_alua.default_tg_pt_gp);
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
		} else
			tg_pt_gp_mem->tg_pt_gp = NULL;
		spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);

		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
	}
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);

	kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
}

void core_alua_free_tg_pt_gp_mem(struct se_port *port)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;

	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1904
	if (!tg_pt_gp_mem)
1905 1906 1907 1908 1909 1910 1911
		return;

	while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
		cpu_relax();

	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1912
	if (tg_pt_gp) {
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
		if (tg_pt_gp_mem->tg_pt_gp_assoc) {
			list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
			tg_pt_gp->tg_pt_gp_members--;
			tg_pt_gp_mem->tg_pt_gp_assoc = 0;
		}
		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
		tg_pt_gp_mem->tg_pt_gp = NULL;
	}
	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);

	kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
}

static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1928
		struct se_device *dev, const char *name)
1929 1930 1931 1932
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct config_item *ci;

1933 1934
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
	list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1935
			tg_pt_gp_list) {
1936
		if (!tg_pt_gp->tg_pt_gp_valid_id)
1937 1938
			continue;
		ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1939
		if (!strcmp(config_item_name(ci), name)) {
1940
			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1941
			spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1942 1943 1944
			return tg_pt_gp;
		}
	}
1945
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1946 1947 1948 1949 1950 1951 1952

	return NULL;
}

static void core_alua_put_tg_pt_gp_from_name(
	struct t10_alua_tg_pt_gp *tg_pt_gp)
{
1953
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1954

1955
	spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1956
	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1957
	spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
}

/*
 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
 */
void __core_alua_attach_tg_pt_gp_mem(
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
	struct t10_alua_tg_pt_gp *tg_pt_gp)
{
	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
	tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
	tg_pt_gp_mem->tg_pt_gp_assoc = 1;
	list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
			&tg_pt_gp->tg_pt_gp_mem_list);
	tg_pt_gp->tg_pt_gp_members++;
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
}

/*
 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
 */
static void __core_alua_drop_tg_pt_gp_mem(
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
	struct t10_alua_tg_pt_gp *tg_pt_gp)
{
	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
	list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
	tg_pt_gp_mem->tg_pt_gp = NULL;
	tg_pt_gp_mem->tg_pt_gp_assoc = 0;
	tg_pt_gp->tg_pt_gp_members--;
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
}

ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
{
	struct config_item *tg_pt_ci;
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
	ssize_t len = 0;

	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1999
	if (!tg_pt_gp_mem)
2000 2001 2002 2003
		return len;

	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
2004
	if (tg_pt_gp) {
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
		tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
		len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
			" %hu\nTG Port Primary Access State: %s\nTG Port "
			"Primary Access Status: %s\nTG Port Secondary Access"
			" State: %s\nTG Port Secondary Access Status: %s\n",
			config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
			core_alua_dump_state(atomic_read(
					&tg_pt_gp->tg_pt_gp_alua_access_state)),
			core_alua_dump_status(
				tg_pt_gp->tg_pt_gp_alua_access_status),
			(atomic_read(&port->sep_tg_pt_secondary_offline)) ?
			"Offline" : "None",
			core_alua_dump_status(port->sep_tg_pt_secondary_stat));
	}
	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);

	return len;
}

ssize_t core_alua_store_tg_pt_gp_info(
	struct se_port *port,
	const char *page,
	size_t count)
{
	struct se_portal_group *tpg;
	struct se_lun *lun;
2031
	struct se_device *dev = port->sep_lun->lun_se_dev;
2032 2033 2034 2035 2036 2037 2038 2039
	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
	unsigned char buf[TG_PT_GROUP_NAME_BUF];
	int move = 0;

	tpg = port->sep_tpg;
	lun = port->sep_lun;

C
Christoph Hellwig 已提交
2040 2041 2042
	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
	if (!tg_pt_gp_mem)
		return 0;
2043 2044

	if (count > TG_PT_GROUP_NAME_BUF) {
2045
		pr_err("ALUA Target Port Group alias too large!\n");
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
		return -EINVAL;
	}
	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
	memcpy(buf, page, count);
	/*
	 * Any ALUA target port group alias besides "NULL" means we will be
	 * making a new group association.
	 */
	if (strcmp(strstrip(buf), "NULL")) {
		/*
		 * core_alua_get_tg_pt_gp_by_name() will increment reference to
		 * struct t10_alua_tg_pt_gp.  This reference is released with
		 * core_alua_put_tg_pt_gp_from_name() below.
		 */
2060
		tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
2061
					strstrip(buf));
2062
		if (!tg_pt_gp_new)
2063 2064 2065 2066 2067
			return -ENODEV;
	}

	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
2068
	if (tg_pt_gp) {
2069 2070 2071 2072
		/*
		 * Clearing an existing tg_pt_gp association, and replacing
		 * with the default_tg_pt_gp.
		 */
2073 2074
		if (!tg_pt_gp_new) {
			pr_debug("Target_Core_ConfigFS: Moving"
2075 2076 2077
				" %s/tpgt_%hu/%s from ALUA Target Port Group:"
				" alua/%s, ID: %hu back to"
				" default_tg_pt_gp\n",
2078 2079
				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
				tpg->se_tpg_tfo->tpg_get_tag(tpg),
2080 2081 2082 2083 2084 2085 2086
				config_item_name(&lun->lun_group.cg_item),
				config_item_name(
					&tg_pt_gp->tg_pt_gp_group.cg_item),
				tg_pt_gp->tg_pt_gp_id);

			__core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
			__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
2087
					dev->t10_alua.default_tg_pt_gp);
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
			spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);

			return count;
		}
		/*
		 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
		 */
		__core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
		move = 1;
	}
	/*
	 * Associate tg_pt_gp_mem with tg_pt_gp_new.
	 */
	__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
2103
	pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
2104
		" Target Port Group: alua/%s, ID: %hu\n", (move) ?
2105 2106
		"Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
		tpg->se_tpg_tfo->tpg_get_tag(tpg),
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
		config_item_name(&lun->lun_group.cg_item),
		config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
		tg_pt_gp_new->tg_pt_gp_id);

	core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
	return count;
}

ssize_t core_alua_show_access_type(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	char *page)
{
2119 2120 2121 2122 2123 2124 2125
	if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
	    (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
		return sprintf(page, "Implicit and Explicit\n");
	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
		return sprintf(page, "Implicit\n");
	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
		return sprintf(page, "Explicit\n");
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
	else
		return sprintf(page, "None\n");
}

ssize_t core_alua_store_access_type(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2138
	ret = kstrtoul(page, 0, &tmp);
2139
	if (ret < 0) {
2140
		pr_err("Unable to extract alua_access_type\n");
2141
		return ret;
2142 2143
	}
	if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
2144
		pr_err("Illegal value for alua_access_type:"
2145 2146 2147 2148 2149
				" %lu\n", tmp);
		return -EINVAL;
	}
	if (tmp == 3)
		tg_pt_gp->tg_pt_gp_alua_access_type =
2150
			TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
2151
	else if (tmp == 2)
2152
		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
2153
	else if (tmp == 1)
2154
		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	else
		tg_pt_gp->tg_pt_gp_alua_access_type = 0;

	return count;
}

ssize_t core_alua_show_nonop_delay_msecs(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	char *page)
{
	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
}

ssize_t core_alua_store_nonop_delay_msecs(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2176
	ret = kstrtoul(page, 0, &tmp);
2177
	if (ret < 0) {
2178
		pr_err("Unable to extract nonop_delay_msecs\n");
2179
		return ret;
2180 2181
	}
	if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
2182
		pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
			" ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
			ALUA_MAX_NONOP_DELAY_MSECS);
		return -EINVAL;
	}
	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;

	return count;
}

ssize_t core_alua_show_trans_delay_msecs(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	char *page)
{
	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
}

ssize_t core_alua_store_trans_delay_msecs(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2207
	ret = kstrtoul(page, 0, &tmp);
2208
	if (ret < 0) {
2209
		pr_err("Unable to extract trans_delay_msecs\n");
2210
		return ret;
2211 2212
	}
	if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
2213
		pr_err("Passed trans_delay_msecs: %lu, exceeds"
2214 2215 2216 2217 2218 2219 2220 2221 2222
			" ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
			ALUA_MAX_TRANS_DELAY_MSECS);
		return -EINVAL;
	}
	tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;

	return count;
}

2223
ssize_t core_alua_show_implicit_trans_secs(
2224 2225 2226
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	char *page)
{
2227
	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
2228 2229
}

2230
ssize_t core_alua_store_implicit_trans_secs(
2231 2232 2233 2234 2235 2236 2237
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2238
	ret = kstrtoul(page, 0, &tmp);
2239
	if (ret < 0) {
2240
		pr_err("Unable to extract implicit_trans_secs\n");
2241
		return ret;
2242
	}
2243 2244 2245 2246
	if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
		pr_err("Passed implicit_trans_secs: %lu, exceeds"
			" ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
			ALUA_MAX_IMPLICIT_TRANS_SECS);
2247 2248
		return  -EINVAL;
	}
2249
	tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
2250 2251 2252 2253

	return count;
}

2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
ssize_t core_alua_show_preferred_bit(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	char *page)
{
	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
}

ssize_t core_alua_store_preferred_bit(
	struct t10_alua_tg_pt_gp *tg_pt_gp,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2269
	ret = kstrtoul(page, 0, &tmp);
2270
	if (ret < 0) {
2271
		pr_err("Unable to extract preferred ALUA value\n");
2272
		return ret;
2273 2274
	}
	if ((tmp != 0) && (tmp != 1)) {
2275
		pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
2276 2277 2278 2279 2280 2281 2282 2283 2284
		return -EINVAL;
	}
	tg_pt_gp->tg_pt_gp_pref = (int)tmp;

	return count;
}

ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
{
2285
	if (!lun->lun_sep)
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
		return -ENODEV;

	return sprintf(page, "%d\n",
		atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
}

ssize_t core_alua_store_offline_bit(
	struct se_lun *lun,
	const char *page,
	size_t count)
{
	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
	unsigned long tmp;
	int ret;

2301
	if (!lun->lun_sep)
2302 2303
		return -ENODEV;

2304
	ret = kstrtoul(page, 0, &tmp);
2305
	if (ret < 0) {
2306
		pr_err("Unable to extract alua_tg_pt_offline value\n");
2307
		return ret;
2308 2309
	}
	if ((tmp != 0) && (tmp != 1)) {
2310
		pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2311 2312 2313 2314
				tmp);
		return -EINVAL;
	}
	tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
2315 2316
	if (!tg_pt_gp_mem) {
		pr_err("Unable to locate *tg_pt_gp_mem\n");
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
		return -EINVAL;
	}

	ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
			lun->lun_sep, 0, (int)tmp);
	if (ret < 0)
		return -EINVAL;

	return count;
}

ssize_t core_alua_show_secondary_status(
	struct se_lun *lun,
	char *page)
{
	return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
}

ssize_t core_alua_store_secondary_status(
	struct se_lun *lun,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2343
	ret = kstrtoul(page, 0, &tmp);
2344
	if (ret < 0) {
2345
		pr_err("Unable to extract alua_tg_pt_status\n");
2346
		return ret;
2347 2348
	}
	if ((tmp != ALUA_STATUS_NONE) &&
2349 2350
	    (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
	    (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2351
		pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
				tmp);
		return -EINVAL;
	}
	lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;

	return count;
}

ssize_t core_alua_show_secondary_write_metadata(
	struct se_lun *lun,
	char *page)
{
	return sprintf(page, "%d\n",
			lun->lun_sep->sep_tg_pt_secondary_write_md);
}

ssize_t core_alua_store_secondary_write_metadata(
	struct se_lun *lun,
	const char *page,
	size_t count)
{
	unsigned long tmp;
	int ret;

2376
	ret = kstrtoul(page, 0, &tmp);
2377
	if (ret < 0) {
2378
		pr_err("Unable to extract alua_tg_pt_write_md\n");
2379
		return ret;
2380 2381
	}
	if ((tmp != 0) && (tmp != 1)) {
2382
		pr_err("Illegal value for alua_tg_pt_write_md:"
2383 2384 2385 2386 2387 2388 2389 2390
				" %lu\n", tmp);
		return -EINVAL;
	}
	lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;

	return count;
}

2391
int core_setup_alua(struct se_device *dev)
2392
{
C
Christoph Hellwig 已提交
2393 2394 2395
	if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
		struct t10_alua_lu_gp_member *lu_gp_mem;
2396

2397
		/*
L
Lucas De Marchi 已提交
2398
		 * Associate this struct se_device with the default ALUA
2399 2400 2401
		 * LUN Group.
		 */
		lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2402 2403
		if (IS_ERR(lu_gp_mem))
			return PTR_ERR(lu_gp_mem);
2404 2405 2406

		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
		__core_alua_attach_lu_gp_mem(lu_gp_mem,
2407
				default_lu_gp);
2408 2409
		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

2410
		pr_debug("%s: Adding to default ALUA LU Group:"
2411
			" core/alua/lu_gps/default_lu_gp\n",
2412
			dev->transport->name);
2413 2414 2415 2416
	}

	return 0;
}