xref: /linux/drivers/target/target_core_pr.c (revision fb8fd1da808feef6af8686986fedcfe45c693dc8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_pr.c
4  *
5  * This file contains SPC-3 compliant persistent reservations and
6  * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7  *
8  * (c) Copyright 2009-2013 Datera, Inc.
9  *
10  * Nicholas A. Bellinger <nab@kernel.org>
11  *
12  ******************************************************************************/
13 
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/list.h>
17 #include <linux/vmalloc.h>
18 #include <linux/file.h>
19 #include <linux/fcntl.h>
20 #include <linux/fs.h>
21 #include <linux/fs_struct.h>
22 #include <linux/hex.h>
23 #include <scsi/scsi_proto.h>
24 #include <linux/unaligned.h>
25 
26 #include <target/target_core_base.h>
27 #include <target/target_core_backend.h>
28 #include <target/target_core_fabric.h>
29 
30 #include "target_core_internal.h"
31 #include "target_core_pr.h"
32 #include "target_core_ua.h"
33 
34 /*
35  * lio_sess_get_initiator_sid() prints the six ISID bytes as twelve hex
36  * digits, and iSCSI TransportIDs carry the same digits.  Convert all six
37  * bytes: reading the first eight digits as a big-endian u64 keeps only the
38  * first four ISID bytes, so two sessions of one initiator that differ only
39  * in the qualifier, the usual case, would compare equal.  Text in any other
40  * format keeps the old conversion of its first eight characters, without
41  * reading past the end of a shorter string.
42  */
43 u64 target_isid_to_bin(const char *isid)
44 {
45 	u8 bin[6];
46 	char raw[8] = {};
47 
48 	if (strnlen(isid, 2 * sizeof(bin) + 1) == 2 * sizeof(bin) &&
49 	    !hex2bin(bin, isid, sizeof(bin)))
50 		return get_unaligned_be48(bin);
51 
52 	memcpy(raw, isid, strnlen(isid, sizeof(raw)));
53 	return get_unaligned_be64(raw);
54 }
55 
56 /*
57  * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
58  */
59 struct pr_transport_id_holder {
60 	struct t10_pr_registration *dest_pr_reg;
61 	struct se_portal_group *dest_tpg;
62 	struct se_node_acl *dest_node_acl;
63 	struct se_dev_entry *dest_se_deve;
64 	struct list_head dest_list;
65 };
66 
67 void core_pr_dump_initiator_port(
68 	struct t10_pr_registration *pr_reg,
69 	char *buf,
70 	u32 size)
71 {
72 	if (!pr_reg->isid_present_at_reg) {
73 		buf[0] = '\0';
74 		return;
75 	}
76 
77 	snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
78 }
79 
80 enum register_type {
81 	REGISTER,
82 	REGISTER_AND_IGNORE_EXISTING_KEY,
83 	REGISTER_AND_MOVE,
84 };
85 
86 enum preempt_type {
87 	PREEMPT,
88 	PREEMPT_AND_ABORT,
89 };
90 
91 static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
92 					      struct t10_pr_registration *, int, int);
93 
94 static int is_reservation_holder(
95 	struct t10_pr_registration *pr_res_holder,
96 	struct t10_pr_registration *pr_reg)
97 {
98 	int pr_res_type;
99 
100 	if (pr_res_holder) {
101 		pr_res_type = pr_res_holder->pr_res_type;
102 
103 		return pr_res_holder == pr_reg ||
104 		       pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
105 		       pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
106 	}
107 	return 0;
108 }
109 
110 static sense_reason_t
111 target_scsi2_reservation_check(struct se_cmd *cmd)
112 {
113 	struct se_device *dev = cmd->se_dev;
114 	struct se_session *sess = cmd->se_sess;
115 
116 	switch (cmd->t_task_cdb[0]) {
117 	case INQUIRY:
118 	case RELEASE_6:
119 	case RELEASE_10:
120 		return 0;
121 	default:
122 		break;
123 	}
124 
125 	if (!dev->reservation_holder || !sess)
126 		return 0;
127 
128 	if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
129 		return TCM_RESERVATION_CONFLICT;
130 
131 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
132 		if (dev->dev_res_bin_isid != sess->sess_bin_isid)
133 			return TCM_RESERVATION_CONFLICT;
134 	}
135 
136 	return 0;
137 }
138 
139 static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
140 					struct se_node_acl *, struct se_session *);
141 static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
142 
143 static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
144 {
145 	struct se_session *se_sess = cmd->se_sess;
146 	struct se_device *dev = cmd->se_dev;
147 	struct t10_pr_registration *pr_reg;
148 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
149 	int conflict = 0;
150 
151 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
152 			se_sess);
153 	if (pr_reg) {
154 		/*
155 		 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
156 		 * behavior
157 		 *
158 		 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
159 		 * status, but no reservation shall be established and the
160 		 * persistent reservation shall not be changed, if the command
161 		 * is received from a) and b) below.
162 		 *
163 		 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
164 		 * status, but the persistent reservation shall not be released,
165 		 * if the command is received from a) and b)
166 		 *
167 		 * a) An I_T nexus that is a persistent reservation holder; or
168 		 * b) An I_T nexus that is registered if a registrants only or
169 		 *    all registrants type persistent reservation is present.
170 		 *
171 		 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
172 		 * RELEASE(6) command, or RELEASE(10) command shall be processed
173 		 * as defined in SPC-2.
174 		 */
175 		if (pr_reg->pr_res_holder) {
176 			core_scsi3_put_pr_reg(pr_reg);
177 			return 1;
178 		}
179 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
180 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
181 		    (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
182 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
183 			core_scsi3_put_pr_reg(pr_reg);
184 			return 1;
185 		}
186 		core_scsi3_put_pr_reg(pr_reg);
187 		conflict = 1;
188 	} else {
189 		/*
190 		 * Following spc2r20 5.5.1 Reservations overview:
191 		 *
192 		 * If a logical unit has executed a PERSISTENT RESERVE OUT
193 		 * command with the REGISTER or the REGISTER AND IGNORE
194 		 * EXISTING KEY service action and is still registered by any
195 		 * initiator, all RESERVE commands and all RELEASE commands
196 		 * regardless of initiator shall conflict and shall terminate
197 		 * with a RESERVATION CONFLICT status.
198 		 */
199 		spin_lock(&pr_tmpl->registration_lock);
200 		conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
201 		spin_unlock(&pr_tmpl->registration_lock);
202 	}
203 
204 	if (conflict) {
205 		pr_err("Received legacy SPC-2 RESERVE/RELEASE"
206 			" while active SPC-3 registrations exist,"
207 			" returning RESERVATION_CONFLICT\n");
208 		return -EBUSY;
209 	}
210 
211 	return 0;
212 }
213 
214 void target_release_reservation(struct se_device *dev)
215 {
216 	dev->reservation_holder = NULL;
217 	dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
218 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
219 		dev->dev_res_bin_isid = 0;
220 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
221 	}
222 }
223 
224 sense_reason_t
225 target_scsi2_reservation_release(struct se_cmd *cmd)
226 {
227 	struct se_device *dev = cmd->se_dev;
228 	struct se_session *sess = cmd->se_sess;
229 	struct se_portal_group *tpg;
230 	int rc;
231 
232 	if (!sess || !sess->se_tpg)
233 		goto out;
234 	rc = target_check_scsi2_reservation_conflict(cmd);
235 	if (rc == 1)
236 		goto out;
237 	if (rc < 0)
238 		return TCM_RESERVATION_CONFLICT;
239 
240 	spin_lock(&dev->dev_reservation_lock);
241 	if (!dev->reservation_holder || !sess)
242 		goto out_unlock;
243 
244 	if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
245 		goto out_unlock;
246 
247 	if (dev->dev_res_bin_isid != sess->sess_bin_isid)
248 		goto out_unlock;
249 
250 	target_release_reservation(dev);
251 	tpg = sess->se_tpg;
252 	pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
253 		" MAPPED LUN: %llu for %s\n",
254 		tpg->se_tpg_tfo->fabric_name,
255 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
256 		sess->se_node_acl->initiatorname);
257 
258 out_unlock:
259 	spin_unlock(&dev->dev_reservation_lock);
260 out:
261 	target_complete_cmd(cmd, SAM_STAT_GOOD);
262 	return 0;
263 }
264 
265 sense_reason_t
266 target_scsi2_reservation_reserve(struct se_cmd *cmd)
267 {
268 	struct se_device *dev = cmd->se_dev;
269 	struct se_session *sess = cmd->se_sess;
270 	struct se_portal_group *tpg;
271 	sense_reason_t ret = 0;
272 	int rc;
273 
274 	if ((cmd->t_task_cdb[1] & 0x01) &&
275 	    (cmd->t_task_cdb[1] & 0x02)) {
276 		pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
277 		return TCM_UNSUPPORTED_SCSI_OPCODE;
278 	}
279 	/*
280 	 * This is currently the case for target_core_mod passthrough struct se_cmd
281 	 * ops
282 	 */
283 	if (!sess || !sess->se_tpg)
284 		goto out;
285 	rc = target_check_scsi2_reservation_conflict(cmd);
286 	if (rc == 1)
287 		goto out;
288 
289 	if (rc < 0)
290 		return TCM_RESERVATION_CONFLICT;
291 
292 	tpg = sess->se_tpg;
293 	spin_lock(&dev->dev_reservation_lock);
294 	if (dev->reservation_holder &&
295 	    dev->reservation_holder->se_node_acl != sess->se_node_acl) {
296 		pr_err("SCSI-2 RESERVATION CONFLICT for %s fabric\n",
297 			tpg->se_tpg_tfo->fabric_name);
298 		pr_err("Original reserver LUN: %llu %s\n",
299 			cmd->se_lun->unpacked_lun,
300 			dev->reservation_holder->se_node_acl->initiatorname);
301 		pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
302 			" from %s \n", cmd->se_lun->unpacked_lun,
303 			cmd->orig_fe_lun,
304 			sess->se_node_acl->initiatorname);
305 		ret = TCM_RESERVATION_CONFLICT;
306 		goto out_unlock;
307 	}
308 
309 	dev->reservation_holder = sess;
310 	dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
311 	/*
312 	 * An all-zero ISID converts to 0, so test whether the fabric has
313 	 * ISIDs rather than the value.
314 	 */
315 	if (tpg->se_tpg_tfo->sess_get_initiator_sid) {
316 		dev->dev_res_bin_isid = sess->sess_bin_isid;
317 		dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
318 	}
319 	pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
320 		" for %s\n", tpg->se_tpg_tfo->fabric_name,
321 		cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
322 		sess->se_node_acl->initiatorname);
323 
324 out_unlock:
325 	spin_unlock(&dev->dev_reservation_lock);
326 out:
327 	if (!ret)
328 		target_complete_cmd(cmd, SAM_STAT_GOOD);
329 	return ret;
330 }
331 
332 
333 /*
334  * Begin SPC-3/SPC-4 Persistent Reservations emulation support
335  *
336  * This function is called by those initiator ports who are *NOT*
337  * the active PR reservation holder when a reservation is present.
338  */
339 static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
340 					bool isid_mismatch)
341 {
342 	unsigned char *cdb = cmd->t_task_cdb;
343 	struct se_session *se_sess = cmd->se_sess;
344 	struct se_node_acl *nacl = se_sess->se_node_acl;
345 	int other_cdb = 0;
346 	int registered_nexus = 0, ret = 1; /* Conflict by default */
347 	int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
348 	int we = 0; /* Write Exclusive */
349 	int legacy = 0; /* Act like a legacy device and return
350 			 * RESERVATION CONFLICT on some CDBs */
351 
352 	if (isid_mismatch) {
353 		struct t10_pr_registration *pr_reg;
354 
355 		/*
356 		 * Another I_T nexus of the holder's initiator.  DEF_PR_REG_ACTIVE
357 		 * is shared by all of its sessions, so look for a registration
358 		 * made through this one.
359 		 */
360 		pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, nacl, se_sess);
361 		if (pr_reg) {
362 			registered_nexus = 1;
363 			core_scsi3_put_pr_reg(pr_reg);
364 		}
365 	} else {
366 		struct se_dev_entry *se_deve;
367 
368 		rcu_read_lock();
369 		se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
370 		if (se_deve)
371 			registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
372 						    &se_deve->deve_flags);
373 		rcu_read_unlock();
374 	}
375 
376 	switch (pr_reg_type) {
377 	case PR_TYPE_WRITE_EXCLUSIVE:
378 		we = 1;
379 		fallthrough;
380 	case PR_TYPE_EXCLUSIVE_ACCESS:
381 		/*
382 		 * Some commands are only allowed for the persistent reservation
383 		 * holder.
384 		 */
385 		break;
386 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
387 		we = 1;
388 		fallthrough;
389 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
390 		/*
391 		 * Some commands are only allowed for registered I_T Nexuses.
392 		 */
393 		reg_only = 1;
394 		break;
395 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
396 		we = 1;
397 		fallthrough;
398 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
399 		/*
400 		 * Each registered I_T Nexus is a reservation holder.
401 		 */
402 		all_reg = 1;
403 		break;
404 	default:
405 		return -EINVAL;
406 	}
407 	/*
408 	 * Referenced from spc4r17 table 45 for *NON* PR holder access
409 	 */
410 	switch (cdb[0]) {
411 	case SECURITY_PROTOCOL_IN:
412 		if (registered_nexus)
413 			return 0;
414 		ret = (we) ? 0 : 1;
415 		break;
416 	case MODE_SENSE:
417 	case MODE_SENSE_10:
418 	case READ_ATTRIBUTE:
419 	case READ_BUFFER:
420 	case RECEIVE_DIAGNOSTIC:
421 		if (legacy) {
422 			ret = 1;
423 			break;
424 		}
425 		if (registered_nexus) {
426 			ret = 0;
427 			break;
428 		}
429 		ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
430 		break;
431 	case PERSISTENT_RESERVE_OUT:
432 		/*
433 		 * This follows PERSISTENT_RESERVE_OUT service actions that
434 		 * are allowed in the presence of various reservations.
435 		 * See spc4r17, table 46
436 		 */
437 		switch (cdb[1] & 0x1f) {
438 		case PRO_CLEAR:
439 		case PRO_PREEMPT:
440 		case PRO_PREEMPT_AND_ABORT:
441 			ret = (registered_nexus) ? 0 : 1;
442 			break;
443 		case PRO_REGISTER:
444 		case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
445 			ret = 0;
446 			break;
447 		case PRO_REGISTER_AND_MOVE:
448 		case PRO_RESERVE:
449 			ret = 1;
450 			break;
451 		case PRO_RELEASE:
452 			ret = (registered_nexus) ? 0 : 1;
453 			break;
454 		default:
455 			pr_err("Unknown PERSISTENT_RESERVE_OUT service"
456 				" action: 0x%02x\n", cdb[1] & 0x1f);
457 			return -EINVAL;
458 		}
459 		break;
460 	case RELEASE_6:
461 	case RELEASE_10:
462 		/* Handled by CRH=1 in target_scsi2_reservation_release() */
463 		ret = 0;
464 		break;
465 	case RESERVE_6:
466 	case RESERVE_10:
467 		/* Handled by CRH=1 in target_scsi2_reservation_reserve() */
468 		ret = 0;
469 		break;
470 	case TEST_UNIT_READY:
471 		ret = (legacy) ? 1 : 0; /* Conflict for legacy */
472 		break;
473 	case MAINTENANCE_IN:
474 		switch (cdb[1] & 0x1f) {
475 		case MI_MANAGEMENT_PROTOCOL_IN:
476 			if (registered_nexus) {
477 				ret = 0;
478 				break;
479 			}
480 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
481 			break;
482 		case MI_REPORT_SUPPORTED_OPERATION_CODES:
483 		case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
484 			if (legacy) {
485 				ret = 1;
486 				break;
487 			}
488 			if (registered_nexus) {
489 				ret = 0;
490 				break;
491 			}
492 			ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
493 			break;
494 		case MI_REPORT_ALIASES:
495 		case MI_REPORT_IDENTIFYING_INFORMATION:
496 		case MI_REPORT_PRIORITY:
497 		case MI_REPORT_TARGET_PGS:
498 		case MI_REPORT_TIMESTAMP:
499 			ret = 0; /* Allowed */
500 			break;
501 		default:
502 			pr_err("Unknown MI Service Action: 0x%02x\n",
503 				(cdb[1] & 0x1f));
504 			return -EINVAL;
505 		}
506 		break;
507 	case ACCESS_CONTROL_IN:
508 	case ACCESS_CONTROL_OUT:
509 	case INQUIRY:
510 	case LOG_SENSE:
511 	case SERVICE_ACTION_IN_12:
512 	case READ_CAPACITY:
513 	case REPORT_LUNS:
514 	case REQUEST_SENSE:
515 	case PERSISTENT_RESERVE_IN:
516 		ret = 0; /*/ Allowed CDBs */
517 		break;
518 	default:
519 		other_cdb = 1;
520 		break;
521 	}
522 	/*
523 	 * Case where the CDB is explicitly allowed in the above switch
524 	 * statement.
525 	 */
526 	if (!ret && !other_cdb) {
527 		pr_debug("Allowing explicit CDB: 0x%02x for %s"
528 			" reservation holder\n", cdb[0],
529 			core_scsi3_pr_dump_type(pr_reg_type));
530 
531 		return ret;
532 	}
533 	/*
534 	 * Check if write exclusive initiator ports *NOT* holding the
535 	 * WRITE_EXCLUSIVE_* reservation.
536 	 */
537 	if (we && !registered_nexus) {
538 		if (cmd->data_direction == DMA_TO_DEVICE) {
539 			/*
540 			 * Conflict for write exclusive
541 			 */
542 			pr_debug("%s Conflict for unregistered nexus"
543 				" %s CDB: 0x%02x to %s reservation\n",
544 				transport_dump_cmd_direction(cmd),
545 				se_sess->se_node_acl->initiatorname, cdb[0],
546 				core_scsi3_pr_dump_type(pr_reg_type));
547 			return 1;
548 		} else {
549 			/*
550 			 * Allow non WRITE CDBs for all Write Exclusive
551 			 * PR TYPEs to pass for registered and
552 			 * non-registered_nexuxes NOT holding the reservation.
553 			 *
554 			 * We only make noise for the unregisterd nexuses,
555 			 * as we expect registered non-reservation holding
556 			 * nexuses to issue CDBs.
557 			 */
558 
559 			if (!registered_nexus) {
560 				pr_debug("Allowing implicit CDB: 0x%02x"
561 					" for %s reservation on unregistered"
562 					" nexus\n", cdb[0],
563 					core_scsi3_pr_dump_type(pr_reg_type));
564 			}
565 
566 			return 0;
567 		}
568 	} else if ((reg_only) || (all_reg)) {
569 		if (registered_nexus) {
570 			/*
571 			 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
572 			 * allow commands from registered nexuses.
573 			 */
574 
575 			pr_debug("Allowing implicit CDB: 0x%02x for %s"
576 				" reservation\n", cdb[0],
577 				core_scsi3_pr_dump_type(pr_reg_type));
578 
579 			return 0;
580 		}
581        } else if (we && registered_nexus) {
582                /*
583                 * Reads are allowed for Write Exclusive locks
584                 * from all registrants.
585                 */
586                if (cmd->data_direction == DMA_FROM_DEVICE) {
587                        pr_debug("Allowing READ CDB: 0x%02x for %s"
588                                " reservation\n", cdb[0],
589                                core_scsi3_pr_dump_type(pr_reg_type));
590 
591                        return 0;
592                }
593 	}
594 	pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
595 		" for %s reservation\n", transport_dump_cmd_direction(cmd),
596 		(registered_nexus) ? "" : "un",
597 		se_sess->se_node_acl->initiatorname, cdb[0],
598 		core_scsi3_pr_dump_type(pr_reg_type));
599 
600 	return 1; /* Conflict by default */
601 }
602 
603 static sense_reason_t
604 target_scsi3_pr_reservation_check(struct se_cmd *cmd)
605 {
606 	struct se_device *dev = cmd->se_dev;
607 	struct se_session *sess = cmd->se_sess;
608 	u32 pr_reg_type;
609 	bool isid_mismatch = false;
610 
611 	if (!dev->dev_pr_res_holder)
612 		return 0;
613 
614 	pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
615 	cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
616 	if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
617 		goto check_nonholder;
618 
619 	if (dev->dev_pr_res_holder->isid_present_at_reg) {
620 		if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
621 		    sess->sess_bin_isid) {
622 			isid_mismatch = true;
623 			goto check_nonholder;
624 		}
625 	}
626 
627 	return 0;
628 
629 check_nonholder:
630 	if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
631 		return TCM_RESERVATION_CONFLICT;
632 	return 0;
633 }
634 
635 static u32 core_scsi3_pr_generation(struct se_device *dev)
636 {
637 	u32 prg;
638 
639 	/*
640 	 * PRGeneration field shall contain the value of a 32-bit wrapping
641 	 * counter mainted by the device server.
642 	 *
643 	 * Note that this is done regardless of Active Persist across
644 	 * Target PowerLoss (APTPL)
645 	 *
646 	 * See spc4r17 section 6.3.12 READ_KEYS service action
647 	 */
648 	spin_lock(&dev->dev_reservation_lock);
649 	prg = dev->t10_pr.pr_generation++;
650 	spin_unlock(&dev->dev_reservation_lock);
651 
652 	return prg;
653 }
654 
655 static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
656 	struct se_device *dev,
657 	struct se_node_acl *nacl,
658 	struct se_lun *lun,
659 	struct se_dev_entry *dest_deve,
660 	u64 mapped_lun,
661 	unsigned char *isid,
662 	u64 sa_res_key,
663 	int all_tg_pt,
664 	int aptpl)
665 {
666 	struct t10_pr_registration *pr_reg;
667 
668 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
669 	if (!pr_reg) {
670 		pr_err("Unable to allocate struct t10_pr_registration\n");
671 		return NULL;
672 	}
673 
674 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
675 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
676 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
677 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
678 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
679 	atomic_set(&pr_reg->pr_res_holders, 0);
680 	pr_reg->pr_reg_nacl = nacl;
681 	/*
682 	 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
683 	 * the se_dev_entry->pr_ref will have been already obtained by
684 	 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
685 	 *
686 	 * Otherwise, locate se_dev_entry now and obtain a reference until
687 	 * registration completes in __core_scsi3_add_registration().
688 	 */
689 	if (dest_deve) {
690 		pr_reg->pr_reg_deve = dest_deve;
691 	} else {
692 		rcu_read_lock();
693 		pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
694 		if (!pr_reg->pr_reg_deve) {
695 			rcu_read_unlock();
696 			pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
697 				nacl->initiatorname, mapped_lun);
698 			kmem_cache_free(t10_pr_reg_cache, pr_reg);
699 			return NULL;
700 		}
701 		kref_get(&pr_reg->pr_reg_deve->pr_kref);
702 		rcu_read_unlock();
703 	}
704 	pr_reg->pr_res_mapped_lun = mapped_lun;
705 	pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
706 	pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi;
707 	pr_reg->pr_res_key = sa_res_key;
708 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
709 	pr_reg->pr_reg_aptpl = aptpl;
710 	/*
711 	 * If an ISID value for this SCSI Initiator Port exists,
712 	 * save it to the registration now.
713 	 */
714 	if (isid != NULL) {
715 		pr_reg->pr_reg_bin_isid = target_isid_to_bin(isid);
716 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
717 		pr_reg->isid_present_at_reg = 1;
718 	}
719 
720 	return pr_reg;
721 }
722 
723 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
724 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
725 
726 /*
727  * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
728  * modes.
729  */
730 static struct t10_pr_registration *__core_scsi3_alloc_registration(
731 	struct se_device *dev,
732 	struct se_node_acl *nacl,
733 	struct se_lun *lun,
734 	struct se_dev_entry *deve,
735 	u64 mapped_lun,
736 	unsigned char *isid,
737 	u64 sa_res_key,
738 	int all_tg_pt,
739 	int aptpl)
740 {
741 	struct se_dev_entry *deve_tmp;
742 	struct se_node_acl *nacl_tmp;
743 	struct se_lun_acl *lacl_tmp;
744 	struct se_lun *lun_tmp, *next, *dest_lun;
745 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
746 	struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
747 	int ret;
748 	/*
749 	 * Create a registration for the I_T Nexus upon which the
750 	 * PROUT REGISTER was received.
751 	 */
752 	pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
753 						    isid, sa_res_key, all_tg_pt,
754 						    aptpl);
755 	if (!pr_reg)
756 		return NULL;
757 	/*
758 	 * Return pointer to pr_reg for ALL_TG_PT=0
759 	 */
760 	if (!all_tg_pt)
761 		return pr_reg;
762 	/*
763 	 * Create list of matching SCSI Initiator Port registrations
764 	 * for ALL_TG_PT=1
765 	 */
766 	spin_lock(&dev->se_port_lock);
767 	list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
768 		if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
769 			continue;
770 		spin_unlock(&dev->se_port_lock);
771 
772 		spin_lock(&lun_tmp->lun_deve_lock);
773 		list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
774 			/*
775 			 * This pointer will be NULL for demo mode MappedLUNs
776 			 * that have not been make explicit via a ConfigFS
777 			 * MappedLUN group for the SCSI Initiator Node ACL.
778 			 */
779 			if (!deve_tmp->se_lun_acl)
780 				continue;
781 
782 			lacl_tmp = deve_tmp->se_lun_acl;
783 			nacl_tmp = lacl_tmp->se_lun_nacl;
784 			/*
785 			 * Skip the matching struct se_node_acl that is allocated
786 			 * above..
787 			 */
788 			if (nacl == nacl_tmp)
789 				continue;
790 			/*
791 			 * Only perform PR registrations for target ports on
792 			 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
793 			 * arrived.
794 			 */
795 			if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
796 				continue;
797 			/*
798 			 * Look for a matching Initiator Node ACL in ASCII format
799 			 */
800 			if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
801 				continue;
802 
803 			kref_get(&deve_tmp->pr_kref);
804 			spin_unlock(&lun_tmp->lun_deve_lock);
805 			/*
806 			 * Grab a configfs group dependency that is released
807 			 * for the exception path at label out: below, or upon
808 			 * completion of adding ALL_TG_PT=1 registrations in
809 			 * __core_scsi3_add_registration()
810 			 */
811 			ret = core_scsi3_lunacl_depend_item(deve_tmp);
812 			if (ret < 0) {
813 				pr_err("core_scsi3_lunacl_depend"
814 						"_item() failed\n");
815 				percpu_ref_put(&lun_tmp->lun_ref);
816 				kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
817 				goto out;
818 			}
819 			/*
820 			 * Located a matching SCSI Initiator Port on a different
821 			 * port, allocate the pr_reg_atp and attach it to the
822 			 * pr_reg->pr_reg_atp_list that will be processed once
823 			 * the original *pr_reg is processed in
824 			 * __core_scsi3_add_registration()
825 			 */
826 			dest_lun = deve_tmp->se_lun;
827 
828 			pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
829 						nacl_tmp, dest_lun, deve_tmp,
830 						deve_tmp->mapped_lun, NULL,
831 						sa_res_key, all_tg_pt, aptpl);
832 			if (!pr_reg_atp) {
833 				percpu_ref_put(&lun_tmp->lun_ref);
834 				core_scsi3_lunacl_undepend_item(deve_tmp);
835 				goto out;
836 			}
837 
838 			list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
839 				      &pr_reg->pr_reg_atp_list);
840 			spin_lock(&lun_tmp->lun_deve_lock);
841 		}
842 		spin_unlock(&lun_tmp->lun_deve_lock);
843 
844 		spin_lock(&dev->se_port_lock);
845 		percpu_ref_put(&lun_tmp->lun_ref);
846 	}
847 	spin_unlock(&dev->se_port_lock);
848 
849 	return pr_reg;
850 out:
851 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
852 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
853 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
854 		core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
855 		kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
856 	}
857 	kmem_cache_free(t10_pr_reg_cache, pr_reg);
858 	return NULL;
859 }
860 
861 int core_scsi3_alloc_aptpl_registration(
862 	struct t10_reservation *pr_tmpl,
863 	u64 sa_res_key,
864 	unsigned char *i_port,
865 	unsigned char *isid,
866 	u64 mapped_lun,
867 	unsigned char *t_port,
868 	u16 tpgt,
869 	u64 target_lun,
870 	int res_holder,
871 	int all_tg_pt,
872 	u8 type)
873 {
874 	struct t10_pr_registration *pr_reg;
875 
876 	if (!i_port || !t_port || !sa_res_key) {
877 		pr_err("Illegal parameters for APTPL registration\n");
878 		return -EINVAL;
879 	}
880 
881 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
882 	if (!pr_reg) {
883 		pr_err("Unable to allocate struct t10_pr_registration\n");
884 		return -ENOMEM;
885 	}
886 
887 	INIT_LIST_HEAD(&pr_reg->pr_reg_list);
888 	INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
889 	INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
890 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
891 	INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
892 	atomic_set(&pr_reg->pr_res_holders, 0);
893 	pr_reg->pr_reg_nacl = NULL;
894 	pr_reg->pr_reg_deve = NULL;
895 	pr_reg->pr_res_mapped_lun = mapped_lun;
896 	pr_reg->pr_aptpl_target_lun = target_lun;
897 	pr_reg->pr_res_key = sa_res_key;
898 	pr_reg->pr_reg_all_tg_pt = all_tg_pt;
899 	pr_reg->pr_reg_aptpl = 1;
900 	pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
901 	pr_reg->pr_res_type = type;
902 	/*
903 	 * If an ISID value had been saved in APTPL metadata for this
904 	 * SCSI Initiator Port, restore it now.
905 	 */
906 	if (isid != NULL) {
907 		pr_reg->pr_reg_bin_isid = target_isid_to_bin(isid);
908 		snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
909 		pr_reg->isid_present_at_reg = 1;
910 	}
911 	/*
912 	 * Copy the i_port and t_port information from caller.
913 	 */
914 	snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
915 	snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
916 	pr_reg->pr_reg_tpgt = tpgt;
917 	/*
918 	 * Set pr_res_holder from caller, the pr_reg who is the reservation
919 	 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
920 	 * the Initiator Node LUN ACL from the fabric module is created for
921 	 * this registration.
922 	 */
923 	pr_reg->pr_res_holder = res_holder;
924 
925 	list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
926 	pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
927 			" metadata\n", (res_holder) ? "+reservation" : "");
928 	return 0;
929 }
930 
931 static void core_scsi3_aptpl_reserve(
932 	struct se_device *dev,
933 	struct se_portal_group *tpg,
934 	struct se_node_acl *node_acl,
935 	struct t10_pr_registration *pr_reg)
936 {
937 	char i_buf[PR_REG_ISID_ID_LEN] = { };
938 
939 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
940 
941 	spin_lock(&dev->dev_reservation_lock);
942 	dev->dev_pr_res_holder = pr_reg;
943 	spin_unlock(&dev->dev_reservation_lock);
944 
945 	pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
946 		" new reservation holder TYPE: %s ALL_TG_PT: %d\n",
947 		tpg->se_tpg_tfo->fabric_name,
948 		core_scsi3_pr_dump_type(pr_reg->pr_res_type),
949 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
950 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
951 		tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
952 		i_buf);
953 }
954 
955 static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
956 				struct t10_pr_registration *, enum register_type, int);
957 
958 static int __core_scsi3_check_aptpl_registration(
959 	struct se_device *dev,
960 	struct se_portal_group *tpg,
961 	struct se_lun *lun,
962 	u64 target_lun,
963 	struct se_node_acl *nacl,
964 	u64 mapped_lun)
965 {
966 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
967 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
968 	unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { };
969 	unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { };
970 	u16 tpgt;
971 
972 	/*
973 	 * Copy Initiator Port information from struct se_node_acl
974 	 */
975 	snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
976 	snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
977 			tpg->se_tpg_tfo->tpg_get_wwn(tpg));
978 	tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
979 	/*
980 	 * Look for the matching registrations+reservation from those
981 	 * created from APTPL metadata.  Note that multiple registrations
982 	 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
983 	 * TransportIDs.
984 	 */
985 	spin_lock(&pr_tmpl->aptpl_reg_lock);
986 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
987 				pr_reg_aptpl_list) {
988 
989 		if (!strcmp(pr_reg->pr_iport, i_port) &&
990 		     (pr_reg->pr_res_mapped_lun == mapped_lun) &&
991 		    !(strcmp(pr_reg->pr_tport, t_port)) &&
992 		     (pr_reg->pr_reg_tpgt == tpgt) &&
993 		     (pr_reg->pr_aptpl_target_lun == target_lun)) {
994 			/*
995 			 * Obtain the ->pr_reg_deve pointer + reference, that
996 			 * is released by __core_scsi3_add_registration() below.
997 			 */
998 			rcu_read_lock();
999 			pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
1000 			if (!pr_reg->pr_reg_deve) {
1001 				pr_err("Unable to locate PR APTPL %s mapped_lun:"
1002 					" %llu\n", nacl->initiatorname, mapped_lun);
1003 				rcu_read_unlock();
1004 				continue;
1005 			}
1006 			kref_get(&pr_reg->pr_reg_deve->pr_kref);
1007 			rcu_read_unlock();
1008 
1009 			pr_reg->pr_reg_nacl = nacl;
1010 			pr_reg->tg_pt_sep_rtpi = lun->lun_tpg->tpg_rtpi;
1011 			list_del(&pr_reg->pr_reg_aptpl_list);
1012 			spin_unlock(&pr_tmpl->aptpl_reg_lock);
1013 			/*
1014 			 * At this point all of the pointers in *pr_reg will
1015 			 * be setup, so go ahead and add the registration.
1016 			 */
1017 			__core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
1018 			/*
1019 			 * If this registration is the reservation holder,
1020 			 * make that happen now..
1021 			 */
1022 			if (pr_reg->pr_res_holder)
1023 				core_scsi3_aptpl_reserve(dev, tpg,
1024 						nacl, pr_reg);
1025 			/*
1026 			 * Reenable pr_aptpl_active to accept new metadata
1027 			 * updates once the SCSI device is active again..
1028 			 */
1029 			spin_lock(&pr_tmpl->aptpl_reg_lock);
1030 			pr_tmpl->pr_aptpl_active = 1;
1031 		}
1032 	}
1033 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1034 
1035 	return 0;
1036 }
1037 
1038 int core_scsi3_check_aptpl_registration(
1039 	struct se_device *dev,
1040 	struct se_portal_group *tpg,
1041 	struct se_lun *lun,
1042 	struct se_node_acl *nacl,
1043 	u64 mapped_lun)
1044 {
1045 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1046 		return 0;
1047 
1048 	return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
1049 						     lun->unpacked_lun, nacl,
1050 						     mapped_lun);
1051 }
1052 
1053 static void __core_scsi3_dump_registration(
1054 	const struct target_core_fabric_ops *tfo,
1055 	struct se_device *dev,
1056 	struct se_node_acl *nacl,
1057 	struct t10_pr_registration *pr_reg,
1058 	enum register_type register_type)
1059 {
1060 	struct se_portal_group *se_tpg = nacl->se_tpg;
1061 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1062 
1063 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1064 
1065 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1066 		" Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
1067 		"_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
1068 		"_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
1069 		i_buf);
1070 	pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1071 		 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
1072 		tfo->tpg_get_tag(se_tpg));
1073 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1074 		" Port(s)\n",  tfo->fabric_name,
1075 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1076 		dev->transport->name);
1077 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1078 		" 0x%08x  APTPL: %d\n", tfo->fabric_name,
1079 		pr_reg->pr_res_key, pr_reg->pr_res_generation,
1080 		pr_reg->pr_reg_aptpl);
1081 }
1082 
1083 static void __core_scsi3_add_registration(
1084 	struct se_device *dev,
1085 	struct se_node_acl *nacl,
1086 	struct t10_pr_registration *pr_reg,
1087 	enum register_type register_type,
1088 	int register_move)
1089 {
1090 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1091 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1092 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1093 	struct se_dev_entry *deve;
1094 
1095 	/*
1096 	 * Increment PRgeneration counter for struct se_device upon a successful
1097 	 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1098 	 *
1099 	 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1100 	 * action, the struct se_device->dev_reservation_lock will already be held,
1101 	 * so we do not call core_scsi3_pr_generation() which grabs the lock
1102 	 * for the REGISTER.
1103 	 */
1104 	pr_reg->pr_res_generation = (register_move) ?
1105 			dev->t10_pr.pr_generation++ :
1106 			core_scsi3_pr_generation(dev);
1107 
1108 	spin_lock(&pr_tmpl->registration_lock);
1109 	list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1110 
1111 	__core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1112 	spin_unlock(&pr_tmpl->registration_lock);
1113 	/*
1114 	 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1115 	 */
1116 	if (!pr_reg->pr_reg_all_tg_pt || register_move)
1117 		goto out;
1118 	/*
1119 	 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1120 	 * allocated in __core_scsi3_alloc_registration()
1121 	 */
1122 	list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1123 			&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1124 		struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1125 
1126 		list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1127 
1128 		pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1129 
1130 		spin_lock(&pr_tmpl->registration_lock);
1131 		list_add_tail(&pr_reg_tmp->pr_reg_list,
1132 			      &pr_tmpl->registration_list);
1133 
1134 		__core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1135 					       register_type);
1136 		spin_unlock(&pr_tmpl->registration_lock);
1137 		/*
1138 		 * Drop configfs group dependency reference and deve->pr_kref
1139 		 * obtained from  __core_scsi3_alloc_registration() code.
1140 		 */
1141 		rcu_read_lock();
1142 		deve = pr_reg_tmp->pr_reg_deve;
1143 		if (deve) {
1144 			set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1145 			core_scsi3_lunacl_undepend_item(deve);
1146 			pr_reg_tmp->pr_reg_deve = NULL;
1147 		}
1148 		rcu_read_unlock();
1149 	}
1150 out:
1151 	/*
1152 	 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1153 	 */
1154 	rcu_read_lock();
1155 	deve = pr_reg->pr_reg_deve;
1156 	if (deve) {
1157 		set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1158 		kref_put(&deve->pr_kref, target_pr_kref_release);
1159 		pr_reg->pr_reg_deve = NULL;
1160 	}
1161 	rcu_read_unlock();
1162 }
1163 
1164 static int core_scsi3_alloc_registration(
1165 	struct se_device *dev,
1166 	struct se_node_acl *nacl,
1167 	struct se_lun *lun,
1168 	struct se_dev_entry *deve,
1169 	u64 mapped_lun,
1170 	unsigned char *isid,
1171 	u64 sa_res_key,
1172 	int all_tg_pt,
1173 	int aptpl,
1174 	enum register_type register_type,
1175 	int register_move)
1176 {
1177 	struct t10_pr_registration *pr_reg;
1178 
1179 	pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1180 						 isid, sa_res_key, all_tg_pt,
1181 						 aptpl);
1182 	if (!pr_reg)
1183 		return -EPERM;
1184 
1185 	__core_scsi3_add_registration(dev, nacl, pr_reg,
1186 			register_type, register_move);
1187 	return 0;
1188 }
1189 
1190 static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1191 	struct se_device *dev,
1192 	struct se_node_acl *nacl,
1193 	unsigned char *isid)
1194 {
1195 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1196 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1197 
1198 	spin_lock(&pr_tmpl->registration_lock);
1199 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1200 			&pr_tmpl->registration_list, pr_reg_list) {
1201 		/*
1202 		 * First look for a matching struct se_node_acl
1203 		 */
1204 		if (pr_reg->pr_reg_nacl != nacl)
1205 			continue;
1206 
1207 		/*
1208 		 * If this registration does NOT contain a fabric provided
1209 		 * ISID, then we have found a match.
1210 		 */
1211 		if (!pr_reg->isid_present_at_reg) {
1212 			atomic_inc_mb(&pr_reg->pr_res_holders);
1213 			spin_unlock(&pr_tmpl->registration_lock);
1214 			return pr_reg;
1215 		}
1216 		/*
1217 		 * If the *pr_reg contains a fabric defined ISID for multi-value
1218 		 * SCSI Initiator Port TransportIDs, then we expect a valid
1219 		 * matching ISID to be provided by the local SCSI Initiator Port.
1220 		 */
1221 		if (!isid)
1222 			continue;
1223 		if (strcmp(isid, pr_reg->pr_reg_isid))
1224 			continue;
1225 
1226 		atomic_inc_mb(&pr_reg->pr_res_holders);
1227 		spin_unlock(&pr_tmpl->registration_lock);
1228 		return pr_reg;
1229 	}
1230 	spin_unlock(&pr_tmpl->registration_lock);
1231 
1232 	return NULL;
1233 }
1234 
1235 static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1236 	struct se_device *dev,
1237 	struct se_node_acl *nacl,
1238 	struct se_session *sess)
1239 {
1240 	struct se_portal_group *tpg = nacl->se_tpg;
1241 	unsigned char buf[PR_REG_ISID_LEN] = { };
1242 	unsigned char *isid_ptr = NULL;
1243 
1244 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1245 		tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1246 					PR_REG_ISID_LEN);
1247 		isid_ptr = &buf[0];
1248 	}
1249 
1250 	return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1251 }
1252 
1253 static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1254 {
1255 	atomic_dec_mb(&pr_reg->pr_res_holders);
1256 }
1257 
1258 static int core_scsi3_check_implicit_release(
1259 	struct se_device *dev,
1260 	struct t10_pr_registration *pr_reg)
1261 {
1262 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1263 	struct t10_pr_registration *pr_res_holder;
1264 	int ret = 0;
1265 
1266 	spin_lock(&dev->dev_reservation_lock);
1267 	pr_res_holder = dev->dev_pr_res_holder;
1268 	if (!pr_res_holder) {
1269 		spin_unlock(&dev->dev_reservation_lock);
1270 		return ret;
1271 	}
1272 	if (pr_res_holder == pr_reg) {
1273 		/*
1274 		 * Perform an implicit RELEASE if the registration that
1275 		 * is being released is holding the reservation.
1276 		 *
1277 		 * From spc4r17, section 5.7.11.1:
1278 		 *
1279 		 * e) If the I_T nexus is the persistent reservation holder
1280 		 *    and the persistent reservation is not an all registrants
1281 		 *    type, then a PERSISTENT RESERVE OUT command with REGISTER
1282 		 *    service action or REGISTER AND  IGNORE EXISTING KEY
1283 		 *    service action with the SERVICE ACTION RESERVATION KEY
1284 		 *    field set to zero (see 5.7.11.3).
1285 		 */
1286 		__core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
1287 		ret = 1;
1288 		/*
1289 		 * For 'All Registrants' reservation types, all existing
1290 		 * registrations are still processed as reservation holders
1291 		 * in core_scsi3_pr_seq_non_holder() after the initial
1292 		 * reservation holder is implicitly released here.
1293 		 */
1294 	} else if (pr_reg->pr_reg_all_tg_pt &&
1295 		  (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1296 			  pr_reg->pr_reg_nacl->initiatorname)) &&
1297 		  (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1298 		pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1299 			" UNREGISTER while existing reservation with matching"
1300 			" key 0x%016Lx is present from another SCSI Initiator"
1301 			" Port\n", pr_reg->pr_res_key);
1302 		ret = -EPERM;
1303 	}
1304 	spin_unlock(&dev->dev_reservation_lock);
1305 
1306 	return ret;
1307 }
1308 
1309 static void __core_scsi3_free_registration(
1310 	struct se_device *dev,
1311 	struct t10_pr_registration *pr_reg,
1312 	struct list_head *preempt_and_abort_list,
1313 	int dec_holders)
1314 	__releases(&pr_tmpl->registration_lock)
1315 	__acquires(&pr_tmpl->registration_lock)
1316 {
1317 	const struct target_core_fabric_ops *tfo =
1318 			pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1319 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1320 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1321 	struct se_dev_entry *deve;
1322 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1323 
1324 	lockdep_assert_held(&pr_tmpl->registration_lock);
1325 
1326 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1327 
1328 	if (!list_empty(&pr_reg->pr_reg_list))
1329 		list_del(&pr_reg->pr_reg_list);
1330 	/*
1331 	 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1332 	 * so call core_scsi3_put_pr_reg() to decrement our reference.
1333 	 */
1334 	if (dec_holders)
1335 		core_scsi3_put_pr_reg(pr_reg);
1336 
1337 	spin_unlock(&pr_tmpl->registration_lock);
1338 	/*
1339 	 * Wait until all reference from any other I_T nexuses for this
1340 	 * *pr_reg have been released.  Because list_del() is called above,
1341 	 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1342 	 * count back to zero, and we release *pr_reg.
1343 	 */
1344 	while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1345 		pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1346 				tfo->fabric_name);
1347 		cpu_relax();
1348 	}
1349 
1350 	rcu_read_lock();
1351 	deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1352 	if (deve)
1353 		clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1354 	rcu_read_unlock();
1355 
1356 	spin_lock(&pr_tmpl->registration_lock);
1357 	pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1358 		" Node: %s%s\n", tfo->fabric_name,
1359 		pr_reg->pr_reg_nacl->initiatorname,
1360 		i_buf);
1361 	pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1362 		" Port(s)\n", tfo->fabric_name,
1363 		(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1364 		dev->transport->name);
1365 	pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1366 		" 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
1367 		pr_reg->pr_res_generation);
1368 
1369 	if (!preempt_and_abort_list) {
1370 		pr_reg->pr_reg_deve = NULL;
1371 		pr_reg->pr_reg_nacl = NULL;
1372 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1373 		return;
1374 	}
1375 	/*
1376 	 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1377 	 * are released once the ABORT_TASK_SET has completed..
1378 	 */
1379 	list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1380 }
1381 
1382 void core_scsi3_free_pr_reg_from_nacl(
1383 	struct se_device *dev,
1384 	struct se_node_acl *nacl)
1385 {
1386 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1387 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1388 	bool free_reg = false;
1389 	/*
1390 	 * If the passed se_node_acl matches the reservation holder,
1391 	 * release the reservation.
1392 	 */
1393 	spin_lock(&dev->dev_reservation_lock);
1394 	pr_res_holder = dev->dev_pr_res_holder;
1395 	if ((pr_res_holder != NULL) &&
1396 	    (pr_res_holder->pr_reg_nacl == nacl)) {
1397 		__core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1398 		free_reg = true;
1399 	}
1400 	spin_unlock(&dev->dev_reservation_lock);
1401 	/*
1402 	 * Release any registration associated with the struct se_node_acl.
1403 	 */
1404 	spin_lock(&pr_tmpl->registration_lock);
1405 	if (pr_res_holder && free_reg)
1406 		__core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1407 
1408 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1409 			&pr_tmpl->registration_list, pr_reg_list) {
1410 
1411 		if (pr_reg->pr_reg_nacl != nacl)
1412 			continue;
1413 
1414 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1415 	}
1416 	spin_unlock(&pr_tmpl->registration_lock);
1417 }
1418 
1419 void core_scsi3_free_all_registrations(
1420 	struct se_device *dev)
1421 {
1422 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
1423 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1424 
1425 	spin_lock(&dev->dev_reservation_lock);
1426 	pr_res_holder = dev->dev_pr_res_holder;
1427 	if (pr_res_holder != NULL) {
1428 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1429 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
1430 						  pr_res_holder, 0, 0);
1431 	}
1432 	spin_unlock(&dev->dev_reservation_lock);
1433 
1434 	spin_lock(&pr_tmpl->registration_lock);
1435 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1436 			&pr_tmpl->registration_list, pr_reg_list) {
1437 
1438 		__core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1439 	}
1440 	spin_unlock(&pr_tmpl->registration_lock);
1441 
1442 	spin_lock(&pr_tmpl->aptpl_reg_lock);
1443 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1444 				pr_reg_aptpl_list) {
1445 		list_del(&pr_reg->pr_reg_aptpl_list);
1446 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
1447 	}
1448 	spin_unlock(&pr_tmpl->aptpl_reg_lock);
1449 }
1450 
1451 static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1452 {
1453 	return target_depend_item(&tpg->tpg_group.cg_item);
1454 }
1455 
1456 static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1457 {
1458 	target_undepend_item(&tpg->tpg_group.cg_item);
1459 	atomic_dec_mb(&tpg->tpg_pr_ref_count);
1460 }
1461 
1462 static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1463 {
1464 	if (nacl->dynamic_node_acl)
1465 		return 0;
1466 	return target_depend_item(&nacl->acl_group.cg_item);
1467 }
1468 
1469 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1470 {
1471 	if (!nacl->dynamic_node_acl)
1472 		target_undepend_item(&nacl->acl_group.cg_item);
1473 	atomic_dec_mb(&nacl->acl_pr_ref_count);
1474 }
1475 
1476 static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1477 {
1478 	/*
1479 	 * For nacl->dynamic_node_acl=1
1480 	 */
1481 	if (!se_deve->se_lun_acl)
1482 		return 0;
1483 
1484 	return target_depend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
1485 }
1486 
1487 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1488 {
1489 	/*
1490 	 * For nacl->dynamic_node_acl=1
1491 	 */
1492 	if (!se_deve->se_lun_acl) {
1493 		kref_put(&se_deve->pr_kref, target_pr_kref_release);
1494 		return;
1495 	}
1496 
1497 	target_undepend_item(&se_deve->se_lun_acl->se_lun_group.cg_item);
1498 	kref_put(&se_deve->pr_kref, target_pr_kref_release);
1499 }
1500 
1501 static sense_reason_t
1502 core_scsi3_decode_spec_i_port(
1503 	struct se_cmd *cmd,
1504 	struct se_portal_group *tpg,
1505 	unsigned char *l_isid,
1506 	u64 sa_res_key,
1507 	int all_tg_pt,
1508 	int aptpl)
1509 {
1510 	struct se_device *dev = cmd->se_dev;
1511 	struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1512 	struct se_session *se_sess = cmd->se_sess;
1513 	struct se_node_acl *dest_node_acl = NULL;
1514 	struct se_dev_entry *dest_se_deve = NULL;
1515 	struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1516 	struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1517 	LIST_HEAD(tid_dest_list);
1518 	struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1519 	unsigned char *buf, *ptr, proto_ident;
1520 	unsigned char i_str[TRANSPORT_IQN_LEN];
1521 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
1522 	sense_reason_t ret;
1523 	u32 tpdl, tid_len = 0;
1524 	u32 dest_rtpi = 0;
1525 	bool tid_found;
1526 
1527 	/*
1528 	 * Allocate a struct pr_transport_id_holder and setup the
1529 	 * local_node_acl pointer and add to struct list_head tid_dest_list
1530 	 * for add registration processing in the loop of tid_dest_list below.
1531 	 */
1532 	tidh_new = kzalloc_obj(struct pr_transport_id_holder);
1533 	if (!tidh_new) {
1534 		pr_err("Unable to allocate tidh_new\n");
1535 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1536 	}
1537 	INIT_LIST_HEAD(&tidh_new->dest_list);
1538 	tidh_new->dest_tpg = tpg;
1539 	tidh_new->dest_node_acl = se_sess->se_node_acl;
1540 
1541 	local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1542 				se_sess->se_node_acl, cmd->se_lun,
1543 				NULL, cmd->orig_fe_lun, l_isid,
1544 				sa_res_key, all_tg_pt, aptpl);
1545 	if (!local_pr_reg) {
1546 		kfree(tidh_new);
1547 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1548 	}
1549 
1550 	if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) {
1551 		kfree(tidh_new);
1552 		kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1553 			 target_pr_kref_release);
1554 		kmem_cache_free(t10_pr_reg_cache, local_pr_reg);
1555 		return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1556 	}
1557 
1558 	tidh_new->dest_pr_reg = local_pr_reg;
1559 	list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1560 
1561 	if (cmd->data_length < 28) {
1562 		pr_warn("SPC-PR: Received PR OUT parameter list"
1563 			" length too small: %u\n", cmd->data_length);
1564 		ret = TCM_INVALID_PARAMETER_LIST;
1565 		goto out;
1566 	}
1567 
1568 	buf = transport_kmap_data_sg(cmd);
1569 	if (!buf) {
1570 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1571 		goto out;
1572 	}
1573 
1574 	/*
1575 	 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1576 	 * first extract TransportID Parameter Data Length, and make sure
1577 	 * the value matches up to the SCSI expected data transfer length.
1578 	 */
1579 	tpdl = get_unaligned_be32(&buf[24]);
1580 
1581 	if ((tpdl + 28) != cmd->data_length) {
1582 		pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1583 			" does not equal CDB data_length: %u\n", tpdl,
1584 			cmd->data_length);
1585 		ret = TCM_INVALID_PARAMETER_LIST;
1586 		goto out_unmap;
1587 	}
1588 	/*
1589 	 * Start processing the received transport IDs using the
1590 	 * receiving I_T Nexus portal's fabric dependent methods to
1591 	 * obtain the SCSI Initiator Port/Device Identifiers.
1592 	 */
1593 	ptr = &buf[28];
1594 
1595 	while (tpdl > 0) {
1596 		struct se_lun *dest_lun, *tmp_lun;
1597 
1598 		proto_ident = (ptr[0] & 0x0f);
1599 		dest_tpg = NULL;
1600 
1601 		spin_lock(&dev->se_port_lock);
1602 		list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1603 			tmp_tpg = tmp_lun->lun_tpg;
1604 
1605 			/*
1606 			 * Look for the matching proto_ident provided by
1607 			 * the received TransportID
1608 			 */
1609 			if (tmp_tpg->proto_id != proto_ident)
1610 				continue;
1611 			dest_rtpi = tmp_lun->lun_tpg->tpg_rtpi;
1612 
1613 			iport_ptr = NULL;
1614 			tid_found = target_parse_pr_out_transport_id(tmp_tpg,
1615 					ptr, tpdl, &tid_len, &iport_ptr, i_str);
1616 			if (!tid_found)
1617 				continue;
1618 			/*
1619 			 * Determine if this SCSI device server requires that
1620 			 * SCSI Intiatior TransportID w/ ISIDs is enforced
1621 			 * for fabric modules (iSCSI) requiring them.
1622 			 */
1623 			if (tpg->se_tpg_tfo->sess_get_initiator_sid &&
1624 			    dev->dev_attrib.enforce_pr_isids &&
1625 			    !iport_ptr) {
1626 				pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.",
1627 					i_str);
1628 				ret = TCM_INVALID_PARAMETER_LIST;
1629 				spin_unlock(&dev->se_port_lock);
1630 				goto out_unmap;
1631 			}
1632 
1633 			atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
1634 			spin_unlock(&dev->se_port_lock);
1635 
1636 			if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1637 				pr_err(" core_scsi3_tpg_depend_item()"
1638 					" for tmp_tpg\n");
1639 				atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
1640 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1641 				goto out_unmap;
1642 			}
1643 			/*
1644 			 * Locate the destination initiator ACL to be registered
1645 			 * from the decoded fabric module specific TransportID
1646 			 * at *i_str.
1647 			 */
1648 			mutex_lock(&tmp_tpg->acl_node_mutex);
1649 			dest_node_acl = __core_tpg_get_initiator_node_acl(
1650 						tmp_tpg, i_str);
1651 			if (dest_node_acl)
1652 				atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
1653 			mutex_unlock(&tmp_tpg->acl_node_mutex);
1654 
1655 			if (!dest_node_acl) {
1656 				core_scsi3_tpg_undepend_item(tmp_tpg);
1657 				spin_lock(&dev->se_port_lock);
1658 				continue;
1659 			}
1660 
1661 			if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1662 				pr_err("configfs_depend_item() failed"
1663 					" for dest_node_acl->acl_group\n");
1664 				atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
1665 				core_scsi3_tpg_undepend_item(tmp_tpg);
1666 				ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1667 				goto out_unmap;
1668 			}
1669 
1670 			dest_tpg = tmp_tpg;
1671 			pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s Port RTPI: %u\n",
1672 				dest_tpg->se_tpg_tfo->fabric_name,
1673 				dest_node_acl->initiatorname, dest_rtpi);
1674 
1675 			spin_lock(&dev->se_port_lock);
1676 			break;
1677 		}
1678 		spin_unlock(&dev->se_port_lock);
1679 
1680 		if (!dest_tpg) {
1681 			pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1682 					" dest_tpg\n");
1683 			ret = TCM_INVALID_PARAMETER_LIST;
1684 			goto out_unmap;
1685 		}
1686 
1687 		pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1688 			" tid_len: %d for %s + %s\n",
1689 			dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
1690 			tpdl, tid_len, i_str, iport_ptr);
1691 
1692 		if (tid_len > tpdl) {
1693 			pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1694 				" %u for Transport ID: %s\n", tid_len, ptr);
1695 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1696 			core_scsi3_tpg_undepend_item(dest_tpg);
1697 			ret = TCM_INVALID_PARAMETER_LIST;
1698 			goto out_unmap;
1699 		}
1700 		/*
1701 		 * Locate the desintation struct se_dev_entry pointer for matching
1702 		 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1703 		 * Target Port.
1704 		 */
1705 		dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1706 					dest_rtpi);
1707 		if (!dest_se_deve) {
1708 			pr_err("Unable to locate %s dest_se_deve from destination RTPI: %u\n",
1709 				dest_tpg->se_tpg_tfo->fabric_name,
1710 				dest_rtpi);
1711 
1712 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1713 			core_scsi3_tpg_undepend_item(dest_tpg);
1714 			ret = TCM_INVALID_PARAMETER_LIST;
1715 			goto out_unmap;
1716 		}
1717 
1718 		if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1719 			pr_err("core_scsi3_lunacl_depend_item()"
1720 					" failed\n");
1721 			kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
1722 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1723 			core_scsi3_tpg_undepend_item(dest_tpg);
1724 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1725 			goto out_unmap;
1726 		}
1727 
1728 		pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1729 			" dest_se_deve mapped_lun: %llu\n",
1730 			dest_tpg->se_tpg_tfo->fabric_name,
1731 			dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1732 
1733 		/*
1734 		 * Skip any TransportIDs that already have a registration for
1735 		 * this target port.
1736 		 */
1737 		pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1738 					iport_ptr);
1739 		if (pr_reg_e) {
1740 			core_scsi3_put_pr_reg(pr_reg_e);
1741 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1742 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1743 			core_scsi3_tpg_undepend_item(dest_tpg);
1744 			ptr += tid_len;
1745 			tpdl -= tid_len;
1746 			tid_len = 0;
1747 			continue;
1748 		}
1749 		/*
1750 		 * Allocate a struct pr_transport_id_holder and setup
1751 		 * the dest_node_acl and dest_se_deve pointers for the
1752 		 * loop below.
1753 		 */
1754 		tidh_new = kzalloc_obj(struct pr_transport_id_holder);
1755 		if (!tidh_new) {
1756 			pr_err("Unable to allocate tidh_new\n");
1757 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1758 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1759 			core_scsi3_tpg_undepend_item(dest_tpg);
1760 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1761 			goto out_unmap;
1762 		}
1763 		INIT_LIST_HEAD(&tidh_new->dest_list);
1764 		tidh_new->dest_tpg = dest_tpg;
1765 		tidh_new->dest_node_acl = dest_node_acl;
1766 		tidh_new->dest_se_deve = dest_se_deve;
1767 
1768 		/*
1769 		 * Allocate, but do NOT add the registration for the
1770 		 * TransportID referenced SCSI Initiator port.  This
1771 		 * done because of the following from spc4r17 in section
1772 		 * 6.14.3 wrt SPEC_I_PT:
1773 		 *
1774 		 * "If a registration fails for any initiator port (e.g., if th
1775 		 * logical unit does not have enough resources available to
1776 		 * hold the registration information), no registrations shall be
1777 		 * made, and the command shall be terminated with
1778 		 * CHECK CONDITION status."
1779 		 *
1780 		 * That means we call __core_scsi3_alloc_registration() here,
1781 		 * and then call __core_scsi3_add_registration() in the
1782 		 * 2nd loop which will never fail.
1783 		 */
1784 		dest_lun = dest_se_deve->se_lun;
1785 
1786 		dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1787 					dest_node_acl, dest_lun, dest_se_deve,
1788 					dest_se_deve->mapped_lun, iport_ptr,
1789 					sa_res_key, all_tg_pt, aptpl);
1790 		if (!dest_pr_reg) {
1791 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1792 			core_scsi3_nodeacl_undepend_item(dest_node_acl);
1793 			core_scsi3_tpg_undepend_item(dest_tpg);
1794 			kfree(tidh_new);
1795 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1796 			goto out_unmap;
1797 		}
1798 		tidh_new->dest_pr_reg = dest_pr_reg;
1799 		list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1800 
1801 		ptr += tid_len;
1802 		tpdl -= tid_len;
1803 		tid_len = 0;
1804 
1805 	}
1806 
1807 	transport_kunmap_data_sg(cmd);
1808 
1809 	/*
1810 	 * Go ahead and create a registrations from tid_dest_list for the
1811 	 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1812 	 * and dest_se_deve.
1813 	 *
1814 	 * The SA Reservation Key from the PROUT is set for the
1815 	 * registration, and ALL_TG_PT is also passed.  ALL_TG_PT=1
1816 	 * means that the TransportID Initiator port will be
1817 	 * registered on all of the target ports in the SCSI target device
1818 	 * ALL_TG_PT=0 means the registration will only be for the
1819 	 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1820 	 * was received.
1821 	 */
1822 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1823 		dest_tpg = tidh->dest_tpg;
1824 		dest_node_acl = tidh->dest_node_acl;
1825 		dest_se_deve = tidh->dest_se_deve;
1826 		dest_pr_reg = tidh->dest_pr_reg;
1827 
1828 		list_del(&tidh->dest_list);
1829 		kfree(tidh);
1830 
1831 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1832 		core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1833 
1834 		__core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1835 					dest_pr_reg, 0, 0);
1836 
1837 		pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1838 			" registered Transport ID for Node: %s%s Mapped LUN:"
1839 			" %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
1840 			dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1841 			dest_se_deve->mapped_lun : 0);
1842 
1843 		if (dest_pr_reg == local_pr_reg)
1844 			continue;
1845 
1846 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1847 		core_scsi3_tpg_undepend_item(dest_tpg);
1848 	}
1849 
1850 	return 0;
1851 out_unmap:
1852 	transport_kunmap_data_sg(cmd);
1853 out:
1854 	/*
1855 	 * For the failure case, release everything from tid_dest_list
1856 	 * including *dest_pr_reg and the configfs dependances..
1857 	 */
1858 	list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1859 		bool is_local = false;
1860 
1861 		dest_tpg = tidh->dest_tpg;
1862 		dest_node_acl = tidh->dest_node_acl;
1863 		dest_se_deve = tidh->dest_se_deve;
1864 		dest_pr_reg = tidh->dest_pr_reg;
1865 
1866 		if (dest_pr_reg == local_pr_reg)
1867 			is_local = true;
1868 
1869 		list_del(&tidh->dest_list);
1870 		kfree(tidh);
1871 		/*
1872 		 * Release any extra ALL_TG_PT=1 registrations for
1873 		 * the SPEC_I_PT=1 case.
1874 		 */
1875 		list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1876 				&dest_pr_reg->pr_reg_atp_list,
1877 				pr_reg_atp_mem_list) {
1878 			list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1879 			core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1880 			kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1881 		}
1882 
1883 		kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1884 
1885 		if (dest_se_deve)
1886 			core_scsi3_lunacl_undepend_item(dest_se_deve);
1887 
1888 		if (is_local)
1889 			continue;
1890 
1891 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
1892 		core_scsi3_tpg_undepend_item(dest_tpg);
1893 	}
1894 	return ret;
1895 }
1896 
1897 static int core_scsi3_update_aptpl_buf(
1898 	struct se_device *dev,
1899 	unsigned char *buf,
1900 	u32 pr_aptpl_buf_len)
1901 {
1902 	struct se_portal_group *tpg;
1903 	struct t10_pr_registration *pr_reg;
1904 	unsigned char tmp[512], isid_buf[32];
1905 	ssize_t len = 0;
1906 	int reg_count = 0;
1907 	int ret = 0;
1908 
1909 	spin_lock(&dev->dev_reservation_lock);
1910 	spin_lock(&dev->t10_pr.registration_lock);
1911 	/*
1912 	 * Walk the registration list..
1913 	 */
1914 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1915 			pr_reg_list) {
1916 
1917 		tmp[0] = '\0';
1918 		isid_buf[0] = '\0';
1919 		tpg = pr_reg->pr_reg_nacl->se_tpg;
1920 		/*
1921 		 * Write out any ISID value to APTPL metadata that was included
1922 		 * in the original registration.
1923 		 */
1924 		if (pr_reg->isid_present_at_reg)
1925 			snprintf(isid_buf, 32, "initiator_sid=%s\n",
1926 					pr_reg->pr_reg_isid);
1927 		/*
1928 		 * Include special metadata if the pr_reg matches the
1929 		 * reservation holder.
1930 		 */
1931 		if (dev->dev_pr_res_holder == pr_reg) {
1932 			snprintf(tmp, 512, "PR_REG_START: %d"
1933 				"\ninitiator_fabric=%s\n"
1934 				"initiator_node=%s\n%s"
1935 				"sa_res_key=%llu\n"
1936 				"res_holder=1\nres_type=%02x\n"
1937 				"res_scope=%02x\nres_all_tg_pt=%d\n"
1938 				"mapped_lun=%llu\n", reg_count,
1939 				tpg->se_tpg_tfo->fabric_name,
1940 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1941 				pr_reg->pr_res_key, pr_reg->pr_res_type,
1942 				pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1943 				pr_reg->pr_res_mapped_lun);
1944 		} else {
1945 			snprintf(tmp, 512, "PR_REG_START: %d\n"
1946 				"initiator_fabric=%s\ninitiator_node=%s\n%s"
1947 				"sa_res_key=%llu\nres_holder=0\n"
1948 				"res_all_tg_pt=%d\nmapped_lun=%llu\n",
1949 				reg_count, tpg->se_tpg_tfo->fabric_name,
1950 				pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1951 				pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1952 				pr_reg->pr_res_mapped_lun);
1953 		}
1954 
1955 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1956 			pr_err("Unable to update renaming APTPL metadata,"
1957 			       " reallocating larger buffer\n");
1958 			ret = -EMSGSIZE;
1959 			goto out;
1960 		}
1961 		len += sprintf(buf+len, "%s", tmp);
1962 
1963 		/*
1964 		 * Include information about the associated SCSI target port.
1965 		 */
1966 		snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1967 			"tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
1968 			" %d\n", tpg->se_tpg_tfo->fabric_name,
1969 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1970 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
1971 			pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1972 			reg_count);
1973 
1974 		if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1975 			pr_err("Unable to update renaming APTPL metadata,"
1976 			       " reallocating larger buffer\n");
1977 			ret = -EMSGSIZE;
1978 			goto out;
1979 		}
1980 		len += sprintf(buf+len, "%s", tmp);
1981 		reg_count++;
1982 	}
1983 
1984 	if (!reg_count)
1985 		len += sprintf(buf+len, "No Registrations or Reservations");
1986 
1987 out:
1988 	spin_unlock(&dev->t10_pr.registration_lock);
1989 	spin_unlock(&dev->dev_reservation_lock);
1990 
1991 	return ret;
1992 }
1993 
1994 static int __core_scsi3_write_aptpl_to_file(
1995 	struct se_device *dev,
1996 	unsigned char *buf)
1997 {
1998 	struct t10_wwn *wwn = &dev->t10_wwn;
1999 	struct file *file;
2000 	int flags = O_RDWR | O_CREAT | O_TRUNC;
2001 	char *path;
2002 	u32 pr_aptpl_buf_len;
2003 	int ret;
2004 	loff_t pos = 0;
2005 
2006 	path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
2007 			&wwn->unit_serial[0]);
2008 	if (!path)
2009 		return -ENOMEM;
2010 
2011 	scoped_with_init_fs()
2012 		file = filp_open(path, flags, 0600);
2013 	if (IS_ERR(file)) {
2014 		pr_err("filp_open(%s) for APTPL metadata"
2015 			" failed\n", path);
2016 		kfree(path);
2017 		return PTR_ERR(file);
2018 	}
2019 
2020 	pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
2021 
2022 	ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
2023 
2024 	if (ret < 0)
2025 		pr_debug("Error writing APTPL metadata file: %s\n", path);
2026 	fput(file);
2027 	kfree(path);
2028 
2029 	return (ret < 0) ? -EIO : 0;
2030 }
2031 
2032 /*
2033  * Clear the APTPL metadata if APTPL has been disabled, otherwise
2034  * write out the updated metadata to struct file for this SCSI device.
2035  */
2036 static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
2037 {
2038 	unsigned char *buf;
2039 	int rc, len = PR_APTPL_BUF_LEN;
2040 
2041 	if (!aptpl) {
2042 		char *null_buf = "No Registrations or Reservations\n";
2043 
2044 		rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
2045 		dev->t10_pr.pr_aptpl_active = 0;
2046 		pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
2047 
2048 		if (rc)
2049 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2050 
2051 		return 0;
2052 	}
2053 retry:
2054 	buf = vzalloc(len);
2055 	if (!buf)
2056 		return TCM_OUT_OF_RESOURCES;
2057 
2058 	rc = core_scsi3_update_aptpl_buf(dev, buf, len);
2059 	if (rc < 0) {
2060 		vfree(buf);
2061 		len *= 2;
2062 		goto retry;
2063 	}
2064 
2065 	rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2066 	if (rc != 0) {
2067 		pr_err("SPC-3 PR: Could not update APTPL\n");
2068 		vfree(buf);
2069 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2070 	}
2071 	dev->t10_pr.pr_aptpl_active = 1;
2072 	vfree(buf);
2073 	pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2074 	return 0;
2075 }
2076 
2077 static sense_reason_t
2078 core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2079 		bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
2080 {
2081 	struct se_session *se_sess = cmd->se_sess;
2082 	struct se_device *dev = cmd->se_dev;
2083 	struct se_lun *se_lun = cmd->se_lun;
2084 	struct se_portal_group *se_tpg;
2085 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
2086 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2087 	unsigned char isid_buf[PR_REG_ISID_LEN] = { };
2088 	unsigned char *isid_ptr = NULL;
2089 	sense_reason_t ret = TCM_NO_SENSE;
2090 	int pr_holder = 0, type;
2091 
2092 	if (!se_sess || !se_lun) {
2093 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2094 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2095 	}
2096 	se_tpg = se_sess->se_tpg;
2097 
2098 	if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2099 		se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2100 				PR_REG_ISID_LEN);
2101 		isid_ptr = &isid_buf[0];
2102 	}
2103 	/*
2104 	 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2105 	 */
2106 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2107 	if (!pr_reg) {
2108 		if (res_key) {
2109 			pr_warn("SPC-3 PR: Reservation Key non-zero"
2110 				" for SA REGISTER, returning CONFLICT\n");
2111 			return TCM_RESERVATION_CONFLICT;
2112 		}
2113 		/*
2114 		 * Do nothing but return GOOD status.
2115 		 */
2116 		if (!sa_res_key)
2117 			return 0;
2118 
2119 		if (!spec_i_pt) {
2120 			/*
2121 			 * Perform the Service Action REGISTER on the Initiator
2122 			 * Port Endpoint that the PRO was received from on the
2123 			 * Logical Unit of the SCSI device server.
2124 			 */
2125 			if (core_scsi3_alloc_registration(cmd->se_dev,
2126 					se_sess->se_node_acl, cmd->se_lun,
2127 					NULL, cmd->orig_fe_lun, isid_ptr,
2128 					sa_res_key, all_tg_pt, aptpl,
2129 					register_type, 0)) {
2130 				pr_err("Unable to allocate"
2131 					" struct t10_pr_registration\n");
2132 				return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
2133 			}
2134 		} else {
2135 			/*
2136 			 * Register both the Initiator port that received
2137 			 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2138 			 * TransportID from Parameter list and loop through
2139 			 * fabric dependent parameter list while calling
2140 			 * logic from of core_scsi3_alloc_registration() for
2141 			 * each TransportID provided SCSI Initiator Port/Device
2142 			 */
2143 			ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2144 					isid_ptr, sa_res_key, all_tg_pt, aptpl);
2145 			if (ret != 0)
2146 				return ret;
2147 		}
2148 		return core_scsi3_update_and_write_aptpl(dev, aptpl);
2149 	}
2150 
2151 	/* ok, existing registration */
2152 
2153 	if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2154 		pr_err("SPC-3 PR REGISTER: Received"
2155 		       " res_key: 0x%016Lx does not match"
2156 		       " existing SA REGISTER res_key:"
2157 		       " 0x%016Lx\n", res_key,
2158 		       pr_reg->pr_res_key);
2159 		ret = TCM_RESERVATION_CONFLICT;
2160 		goto out;
2161 	}
2162 
2163 	if (spec_i_pt) {
2164 		pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2165 			" set on a registered nexus\n");
2166 		ret = TCM_INVALID_PARAMETER_LIST;
2167 		goto out;
2168 	}
2169 
2170 	/*
2171 	 * An existing ALL_TG_PT=1 registration being released
2172 	 * must also set ALL_TG_PT=1 in the incoming PROUT.
2173 	 */
2174 	if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2175 		pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2176 			" registration exists, but ALL_TG_PT=1 bit not"
2177 			" present in received PROUT\n");
2178 		ret = TCM_INVALID_CDB_FIELD;
2179 		goto out;
2180 	}
2181 
2182 	/*
2183 	 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2184 	 */
2185 	if (sa_res_key) {
2186 		/*
2187 		 * Increment PRgeneration counter for struct se_device"
2188 		 * upon a successful REGISTER, see spc4r17 section 6.3.2
2189 		 * READ_KEYS service action.
2190 		 */
2191 		pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2192 		pr_reg->pr_res_key = sa_res_key;
2193 		pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2194 			 " Key for %s to: 0x%016Lx PRgeneration:"
2195 			 " 0x%08x\n", cmd->se_tfo->fabric_name,
2196 			 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2197 			 pr_reg->pr_reg_nacl->initiatorname,
2198 			 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2199 
2200 	} else {
2201 		/*
2202 		 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2203 		 */
2204 		type = pr_reg->pr_res_type;
2205 		pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2206 							      pr_reg);
2207 		if (pr_holder < 0) {
2208 			ret = TCM_RESERVATION_CONFLICT;
2209 			goto out;
2210 		}
2211 
2212 		spin_lock(&pr_tmpl->registration_lock);
2213 		/*
2214 		 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2215 		 * and matching pr_res_key.
2216 		 */
2217 		if (pr_reg->pr_reg_all_tg_pt) {
2218 			list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2219 					&pr_tmpl->registration_list,
2220 					pr_reg_list) {
2221 
2222 				if (!pr_reg_p->pr_reg_all_tg_pt)
2223 					continue;
2224 				if (pr_reg_p->pr_res_key != res_key)
2225 					continue;
2226 				if (pr_reg == pr_reg_p)
2227 					continue;
2228 				if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2229 					   pr_reg_p->pr_reg_nacl->initiatorname))
2230 					continue;
2231 
2232 				__core_scsi3_free_registration(dev,
2233 						pr_reg_p, NULL, 0);
2234 			}
2235 		}
2236 
2237 		/*
2238 		 * Release the calling I_T Nexus registration now..
2239 		 */
2240 		__core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2241 		pr_reg = NULL;
2242 
2243 		/*
2244 		 * From spc4r17, section 5.7.11.3 Unregistering
2245 		 *
2246 		 * If the persistent reservation is a registrants only
2247 		 * type, the device server shall establish a unit
2248 		 * attention condition for the initiator port associated
2249 		 * with every registered I_T nexus except for the I_T
2250 		 * nexus on which the PERSISTENT RESERVE OUT command was
2251 		 * received, with the additional sense code set to
2252 		 * RESERVATIONS RELEASED.
2253 		 */
2254 		if (pr_holder &&
2255 		    (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2256 		     type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2257 			list_for_each_entry(pr_reg_p,
2258 					&pr_tmpl->registration_list,
2259 					pr_reg_list) {
2260 
2261 				target_ua_allocate_lun(
2262 					pr_reg_p->pr_reg_nacl,
2263 					pr_reg_p->pr_res_mapped_lun,
2264 					0x2A,
2265 					ASCQ_2AH_RESERVATIONS_RELEASED);
2266 			}
2267 		}
2268 
2269 		spin_unlock(&pr_tmpl->registration_lock);
2270 	}
2271 
2272 	ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
2273 
2274 out:
2275 	if (pr_reg)
2276 		core_scsi3_put_pr_reg(pr_reg);
2277 	return ret;
2278 }
2279 
2280 unsigned char *core_scsi3_pr_dump_type(int type)
2281 {
2282 	switch (type) {
2283 	case PR_TYPE_WRITE_EXCLUSIVE:
2284 		return "Write Exclusive Access";
2285 	case PR_TYPE_EXCLUSIVE_ACCESS:
2286 		return "Exclusive Access";
2287 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2288 		return "Write Exclusive Access, Registrants Only";
2289 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2290 		return "Exclusive Access, Registrants Only";
2291 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2292 		return "Write Exclusive Access, All Registrants";
2293 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2294 		return "Exclusive Access, All Registrants";
2295 	default:
2296 		break;
2297 	}
2298 
2299 	return "Unknown SPC-3 PR Type";
2300 }
2301 
2302 static sense_reason_t
2303 core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2304 {
2305 	struct se_device *dev = cmd->se_dev;
2306 	struct se_session *se_sess = cmd->se_sess;
2307 	struct se_lun *se_lun = cmd->se_lun;
2308 	struct t10_pr_registration *pr_reg, *pr_res_holder;
2309 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2310 	char i_buf[PR_REG_ISID_ID_LEN] = { };
2311 	sense_reason_t ret;
2312 
2313 	if (!se_sess || !se_lun) {
2314 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2315 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2316 	}
2317 	/*
2318 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2319 	 */
2320 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2321 				se_sess);
2322 	if (!pr_reg) {
2323 		pr_err("SPC-3 PR: Unable to locate"
2324 			" PR_REGISTERED *pr_reg for RESERVE\n");
2325 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2326 	}
2327 	/*
2328 	 * From spc4r17 Section 5.7.9: Reserving:
2329 	 *
2330 	 * An application client creates a persistent reservation by issuing
2331 	 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2332 	 * a registered I_T nexus with the following parameters:
2333 	 *    a) RESERVATION KEY set to the value of the reservation key that is
2334 	 * 	 registered with the logical unit for the I_T nexus; and
2335 	 */
2336 	if (res_key != pr_reg->pr_res_key) {
2337 		pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2338 			" does not match existing SA REGISTER res_key:"
2339 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2340 		ret = TCM_RESERVATION_CONFLICT;
2341 		goto out_put_pr_reg;
2342 	}
2343 	/*
2344 	 * From spc4r17 Section 5.7.9: Reserving:
2345 	 *
2346 	 * From above:
2347 	 *  b) TYPE field and SCOPE field set to the persistent reservation
2348 	 *     being created.
2349 	 *
2350 	 * Only one persistent reservation is allowed at a time per logical unit
2351 	 * and that persistent reservation has a scope of LU_SCOPE.
2352 	 */
2353 	if (scope != PR_SCOPE_LU_SCOPE) {
2354 		pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2355 		ret = TCM_INVALID_PARAMETER_LIST;
2356 		goto out_put_pr_reg;
2357 	}
2358 	/*
2359 	 * See if we have an existing PR reservation holder pointer at
2360 	 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2361 	 * *pr_res_holder.
2362 	 */
2363 	spin_lock(&dev->dev_reservation_lock);
2364 	pr_res_holder = dev->dev_pr_res_holder;
2365 	if (pr_res_holder) {
2366 		/*
2367 		 * From spc4r17 Section 5.7.9: Reserving:
2368 		 *
2369 		 * If the device server receives a PERSISTENT RESERVE OUT
2370 		 * command from an I_T nexus other than a persistent reservation
2371 		 * holder (see 5.7.10) that attempts to create a persistent
2372 		 * reservation when a persistent reservation already exists for
2373 		 * the logical unit, then the command shall be completed with
2374 		 * RESERVATION CONFLICT status.
2375 		 */
2376 		if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2377 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2378 			pr_err("SPC-3 PR: Attempted RESERVE from"
2379 				" [%s]: %s while reservation already held by"
2380 				" [%s]: %s, returning RESERVATION_CONFLICT\n",
2381 				cmd->se_tfo->fabric_name,
2382 				se_sess->se_node_acl->initiatorname,
2383 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2384 				pr_res_holder->pr_reg_nacl->initiatorname);
2385 
2386 			spin_unlock(&dev->dev_reservation_lock);
2387 			ret = TCM_RESERVATION_CONFLICT;
2388 			goto out_put_pr_reg;
2389 		}
2390 		/*
2391 		 * From spc4r17 Section 5.7.9: Reserving:
2392 		 *
2393 		 * If a persistent reservation holder attempts to modify the
2394 		 * type or scope of an existing persistent reservation, the
2395 		 * command shall be completed with RESERVATION CONFLICT status.
2396 		 */
2397 		if ((pr_res_holder->pr_res_type != type) ||
2398 		    (pr_res_holder->pr_res_scope != scope)) {
2399 			struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2400 			pr_err("SPC-3 PR: Attempted RESERVE from"
2401 				" [%s]: %s trying to change TYPE and/or SCOPE,"
2402 				" while reservation already held by [%s]: %s,"
2403 				" returning RESERVATION_CONFLICT\n",
2404 				cmd->se_tfo->fabric_name,
2405 				se_sess->se_node_acl->initiatorname,
2406 				pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2407 				pr_res_holder->pr_reg_nacl->initiatorname);
2408 
2409 			spin_unlock(&dev->dev_reservation_lock);
2410 			ret = TCM_RESERVATION_CONFLICT;
2411 			goto out_put_pr_reg;
2412 		}
2413 		/*
2414 		 * From spc4r17 Section 5.7.9: Reserving:
2415 		 *
2416 		 * If the device server receives a PERSISTENT RESERVE OUT
2417 		 * command with RESERVE service action where the TYPE field and
2418 		 * the SCOPE field contain the same values as the existing type
2419 		 * and scope from a persistent reservation holder, it shall not
2420 		 * make any change to the existing persistent reservation and
2421 		 * shall completethe command with GOOD status.
2422 		 */
2423 		spin_unlock(&dev->dev_reservation_lock);
2424 		ret = 0;
2425 		goto out_put_pr_reg;
2426 	}
2427 	/*
2428 	 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2429 	 * TYPE/SCOPE.  Also set the received scope and type in *pr_reg.
2430 	 */
2431 	pr_reg->pr_res_scope = scope;
2432 	pr_reg->pr_res_type = type;
2433 	pr_reg->pr_res_holder = 1;
2434 	dev->dev_pr_res_holder = pr_reg;
2435 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2436 
2437 	pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2438 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2439 		cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
2440 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2441 	pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2442 			cmd->se_tfo->fabric_name,
2443 			se_sess->se_node_acl->initiatorname,
2444 			i_buf);
2445 	spin_unlock(&dev->dev_reservation_lock);
2446 
2447 	if (pr_tmpl->pr_aptpl_active)
2448 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2449 
2450 	ret = 0;
2451 out_put_pr_reg:
2452 	core_scsi3_put_pr_reg(pr_reg);
2453 	return ret;
2454 }
2455 
2456 static sense_reason_t
2457 core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2458 		u64 res_key)
2459 {
2460 	switch (type) {
2461 	case PR_TYPE_WRITE_EXCLUSIVE:
2462 	case PR_TYPE_EXCLUSIVE_ACCESS:
2463 	case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2464 	case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2465 	case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2466 	case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2467 		return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2468 	default:
2469 		pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2470 			" 0x%02x\n", type);
2471 		return TCM_INVALID_CDB_FIELD;
2472 	}
2473 }
2474 
2475 static void __core_scsi3_complete_pro_release(
2476 	struct se_device *dev,
2477 	struct se_node_acl *se_nacl,
2478 	struct t10_pr_registration *pr_reg,
2479 	int explicit,
2480 	int unreg)
2481 {
2482 	const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2483 	char i_buf[PR_REG_ISID_ID_LEN] = { };
2484 	int pr_res_type = 0, pr_res_scope = 0;
2485 
2486 	lockdep_assert_held(&dev->dev_reservation_lock);
2487 
2488 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2489 	/*
2490 	 * Go ahead and release the current PR reservation holder.
2491 	 * If an All Registrants reservation is currently active and
2492 	 * a unregister operation is requested, replace the current
2493 	 * dev_pr_res_holder with another active registration.
2494 	 */
2495 	if (dev->dev_pr_res_holder) {
2496 		pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2497 		pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2498 		dev->dev_pr_res_holder->pr_res_type = 0;
2499 		dev->dev_pr_res_holder->pr_res_scope = 0;
2500 		dev->dev_pr_res_holder->pr_res_holder = 0;
2501 		dev->dev_pr_res_holder = NULL;
2502 	}
2503 	if (!unreg)
2504 		goto out;
2505 
2506 	spin_lock(&dev->t10_pr.registration_lock);
2507 	list_del_init(&pr_reg->pr_reg_list);
2508 	/*
2509 	 * If the I_T nexus is a reservation holder, the persistent reservation
2510 	 * is of an all registrants type, and the I_T nexus is the last remaining
2511 	 * registered I_T nexus, then the device server shall also release the
2512 	 * persistent reservation.
2513 	 */
2514 	if (!list_empty(&dev->t10_pr.registration_list) &&
2515 	    ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2516 	     (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2517 		dev->dev_pr_res_holder =
2518 			list_entry(dev->t10_pr.registration_list.next,
2519 				   struct t10_pr_registration, pr_reg_list);
2520 		dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2521 		dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2522 		dev->dev_pr_res_holder->pr_res_holder = 1;
2523 	}
2524 	spin_unlock(&dev->t10_pr.registration_lock);
2525 out:
2526 	if (!dev->dev_pr_res_holder) {
2527 		pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2528 			" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2529 			tfo->fabric_name, (explicit) ? "explicit" :
2530 			"implicit", core_scsi3_pr_dump_type(pr_res_type),
2531 			(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2532 	}
2533 	pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2534 		tfo->fabric_name, se_nacl->initiatorname,
2535 		i_buf);
2536 	/*
2537 	 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2538 	 */
2539 	pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2540 }
2541 
2542 static sense_reason_t
2543 core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2544 		u64 res_key)
2545 {
2546 	struct se_device *dev = cmd->se_dev;
2547 	struct se_session *se_sess = cmd->se_sess;
2548 	struct se_lun *se_lun = cmd->se_lun;
2549 	struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2550 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2551 	sense_reason_t ret = 0;
2552 
2553 	if (!se_sess || !se_lun) {
2554 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2555 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2556 	}
2557 	/*
2558 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2559 	 */
2560 	pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2561 	if (!pr_reg) {
2562 		pr_err("SPC-3 PR: Unable to locate"
2563 			" PR_REGISTERED *pr_reg for RELEASE\n");
2564 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2565 	}
2566 	/*
2567 	 * From spc4r17 Section 5.7.11.2 Releasing:
2568 	 *
2569 	 * If there is no persistent reservation or in response to a persistent
2570 	 * reservation release request from a registered I_T nexus that is not a
2571 	 * persistent reservation holder (see 5.7.10), the device server shall
2572 	 * do the following:
2573 	 *
2574 	 *     a) Not release the persistent reservation, if any;
2575 	 *     b) Not remove any registrations; and
2576 	 *     c) Complete the command with GOOD status.
2577 	 */
2578 	spin_lock(&dev->dev_reservation_lock);
2579 	pr_res_holder = dev->dev_pr_res_holder;
2580 	if (!pr_res_holder) {
2581 		/*
2582 		 * No persistent reservation, return GOOD status.
2583 		 */
2584 		spin_unlock(&dev->dev_reservation_lock);
2585 		goto out_put_pr_reg;
2586 	}
2587 
2588 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
2589 		/*
2590 		 * Release request from a registered I_T nexus that is not a
2591 		 * persistent reservation holder. return GOOD status.
2592 		 */
2593 		spin_unlock(&dev->dev_reservation_lock);
2594 		goto out_put_pr_reg;
2595 	}
2596 
2597 	/*
2598 	 * From spc4r17 Section 5.7.11.2 Releasing:
2599 	 *
2600 	 * Only the persistent reservation holder (see 5.7.10) is allowed to
2601 	 * release a persistent reservation.
2602 	 *
2603 	 * An application client releases the persistent reservation by issuing
2604 	 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2605 	 * an I_T nexus that is a persistent reservation holder with the
2606 	 * following parameters:
2607 	 *
2608 	 *     a) RESERVATION KEY field set to the value of the reservation key
2609 	 *	  that is registered with the logical unit for the I_T nexus;
2610 	 */
2611 	if (res_key != pr_reg->pr_res_key) {
2612 		pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2613 			" does not match existing SA REGISTER res_key:"
2614 			" 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2615 		spin_unlock(&dev->dev_reservation_lock);
2616 		ret = TCM_RESERVATION_CONFLICT;
2617 		goto out_put_pr_reg;
2618 	}
2619 	/*
2620 	 * From spc4r17 Section 5.7.11.2 Releasing and above:
2621 	 *
2622 	 * b) TYPE field and SCOPE field set to match the persistent
2623 	 *    reservation being released.
2624 	 */
2625 	if ((pr_res_holder->pr_res_type != type) ||
2626 	    (pr_res_holder->pr_res_scope != scope)) {
2627 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2628 		pr_err("SPC-3 PR RELEASE: Attempted to release"
2629 			" reservation from [%s]: %s with different TYPE "
2630 			"and/or SCOPE  while reservation already held by"
2631 			" [%s]: %s, returning RESERVATION_CONFLICT\n",
2632 			cmd->se_tfo->fabric_name,
2633 			se_sess->se_node_acl->initiatorname,
2634 			pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
2635 			pr_res_holder->pr_reg_nacl->initiatorname);
2636 
2637 		spin_unlock(&dev->dev_reservation_lock);
2638 		ret = TCM_RESERVATION_CONFLICT;
2639 		goto out_put_pr_reg;
2640 	}
2641 	/*
2642 	 * In response to a persistent reservation release request from the
2643 	 * persistent reservation holder the device server shall perform a
2644 	 * release by doing the following as an uninterrupted series of actions:
2645 	 * a) Release the persistent reservation;
2646 	 * b) Not remove any registration(s);
2647 	 * c) If the released persistent reservation is a registrants only type
2648 	 * or all registrants type persistent reservation,
2649 	 *    the device server shall establish a unit attention condition for
2650 	 *    the initiator port associated with every regis-
2651 	 *    tered I_T nexus other than I_T nexus on which the PERSISTENT
2652 	 *    RESERVE OUT command with RELEASE service action was received,
2653 	 *    with the additional sense code set to RESERVATIONS RELEASED; and
2654 	 * d) If the persistent reservation is of any other type, the device
2655 	 *    server shall not establish a unit attention condition.
2656 	 */
2657 	__core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2658 					  pr_reg, 1, 0);
2659 
2660 	spin_unlock(&dev->dev_reservation_lock);
2661 
2662 	if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2663 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2664 	    (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2665 	    (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2666 		/*
2667 		 * If no UNIT ATTENTION conditions will be established for
2668 		 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2669 		 * go ahead and check for APTPL=1 update+write below
2670 		 */
2671 		goto write_aptpl;
2672 	}
2673 
2674 	spin_lock(&pr_tmpl->registration_lock);
2675 	list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2676 			pr_reg_list) {
2677 		/*
2678 		 * Do not establish a UNIT ATTENTION condition
2679 		 * for the calling I_T Nexus
2680 		 */
2681 		if (pr_reg_p == pr_reg)
2682 			continue;
2683 
2684 		target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
2685 				pr_reg_p->pr_res_mapped_lun,
2686 				0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2687 	}
2688 	spin_unlock(&pr_tmpl->registration_lock);
2689 
2690 write_aptpl:
2691 	if (pr_tmpl->pr_aptpl_active)
2692 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2693 
2694 out_put_pr_reg:
2695 	core_scsi3_put_pr_reg(pr_reg);
2696 	return ret;
2697 }
2698 
2699 static sense_reason_t
2700 core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2701 {
2702 	struct se_device *dev = cmd->se_dev;
2703 	struct se_node_acl *pr_reg_nacl;
2704 	struct se_session *se_sess = cmd->se_sess;
2705 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2706 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2707 	u64 pr_res_mapped_lun = 0;
2708 	int calling_it_nexus = 0;
2709 	/*
2710 	 * Locate the existing *pr_reg via struct se_node_acl pointers
2711 	 */
2712 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2713 			se_sess->se_node_acl, se_sess);
2714 	if (!pr_reg_n) {
2715 		pr_err("SPC-3 PR: Unable to locate"
2716 			" PR_REGISTERED *pr_reg for CLEAR\n");
2717 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2718 	}
2719 	/*
2720 	 * From spc4r17 section 5.7.11.6, Clearing:
2721 	 *
2722 	 * Any application client may release the persistent reservation and
2723 	 * remove all registrations from a device server by issuing a
2724 	 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2725 	 * registered I_T nexus with the following parameter:
2726 	 *
2727 	 *	a) RESERVATION KEY field set to the value of the reservation key
2728 	 * 	   that is registered with the logical unit for the I_T nexus.
2729 	 */
2730 	if (res_key != pr_reg_n->pr_res_key) {
2731 		pr_err("SPC-3 PR REGISTER: Received"
2732 			" res_key: 0x%016Lx does not match"
2733 			" existing SA REGISTER res_key:"
2734 			" 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2735 		core_scsi3_put_pr_reg(pr_reg_n);
2736 		return TCM_RESERVATION_CONFLICT;
2737 	}
2738 	/*
2739 	 * a) Release the persistent reservation, if any;
2740 	 */
2741 	spin_lock(&dev->dev_reservation_lock);
2742 	pr_res_holder = dev->dev_pr_res_holder;
2743 	if (pr_res_holder) {
2744 		struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2745 		__core_scsi3_complete_pro_release(dev, pr_res_nacl,
2746 						  pr_res_holder, 0, 0);
2747 	}
2748 	spin_unlock(&dev->dev_reservation_lock);
2749 	/*
2750 	 * b) Remove all registration(s) (see spc4r17 5.7.7);
2751 	 */
2752 	spin_lock(&pr_tmpl->registration_lock);
2753 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2754 			&pr_tmpl->registration_list, pr_reg_list) {
2755 
2756 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2757 		pr_reg_nacl = pr_reg->pr_reg_nacl;
2758 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2759 		__core_scsi3_free_registration(dev, pr_reg, NULL,
2760 					calling_it_nexus);
2761 		/*
2762 		 * e) Establish a unit attention condition for the initiator
2763 		 *    port associated with every registered I_T nexus other
2764 		 *    than the I_T nexus on which the PERSISTENT RESERVE OUT
2765 		 *    command with CLEAR service action was received, with the
2766 		 *    additional sense code set to RESERVATIONS PREEMPTED.
2767 		 */
2768 		if (!calling_it_nexus)
2769 			target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
2770 				0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2771 	}
2772 	spin_unlock(&pr_tmpl->registration_lock);
2773 
2774 	pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2775 		cmd->se_tfo->fabric_name);
2776 
2777 	core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
2778 
2779 	core_scsi3_pr_generation(dev);
2780 	return 0;
2781 }
2782 
2783 static void __core_scsi3_complete_pro_preempt(
2784 	struct se_device *dev,
2785 	struct t10_pr_registration *pr_reg,
2786 	struct list_head *preempt_and_abort_list,
2787 	int type,
2788 	int scope,
2789 	enum preempt_type preempt_type)
2790 {
2791 	struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2792 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2793 	char i_buf[PR_REG_ISID_ID_LEN] = { };
2794 
2795 	lockdep_assert_held(&dev->dev_reservation_lock);
2796 
2797 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
2798 	/*
2799 	 * Do an implicit RELEASE of the existing reservation.
2800 	 */
2801 	if (dev->dev_pr_res_holder)
2802 		__core_scsi3_complete_pro_release(dev, nacl,
2803 						  dev->dev_pr_res_holder, 0, 0);
2804 
2805 	dev->dev_pr_res_holder = pr_reg;
2806 	pr_reg->pr_res_holder = 1;
2807 	pr_reg->pr_res_type = type;
2808 	pr_reg->pr_res_scope = scope;
2809 
2810 	pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2811 		" reservation holder TYPE: %s ALL_TG_PT: %d\n",
2812 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2813 		core_scsi3_pr_dump_type(type),
2814 		(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2815 	pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2816 		tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
2817 		nacl->initiatorname, i_buf);
2818 	/*
2819 	 * For PREEMPT_AND_ABORT, add the preempting reservation's
2820 	 * struct t10_pr_registration to the list that will be compared
2821 	 * against received CDBs..
2822 	 */
2823 	if (preempt_and_abort_list)
2824 		list_add_tail(&pr_reg->pr_reg_abort_list,
2825 				preempt_and_abort_list);
2826 }
2827 
2828 static void core_scsi3_release_preempt_and_abort(
2829 	struct list_head *preempt_and_abort_list,
2830 	struct t10_pr_registration *pr_reg_holder)
2831 {
2832 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2833 
2834 	list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2835 				pr_reg_abort_list) {
2836 
2837 		list_del(&pr_reg->pr_reg_abort_list);
2838 		if (pr_reg_holder == pr_reg)
2839 			continue;
2840 		if (pr_reg->pr_res_holder) {
2841 			pr_warn("pr_reg->pr_res_holder still set\n");
2842 			continue;
2843 		}
2844 
2845 		pr_reg->pr_reg_deve = NULL;
2846 		pr_reg->pr_reg_nacl = NULL;
2847 		kmem_cache_free(t10_pr_reg_cache, pr_reg);
2848 	}
2849 }
2850 
2851 static sense_reason_t
2852 core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2853 		u64 sa_res_key, enum preempt_type preempt_type)
2854 {
2855 	struct se_device *dev = cmd->se_dev;
2856 	struct se_node_acl *pr_reg_nacl;
2857 	struct se_session *se_sess = cmd->se_sess;
2858 	LIST_HEAD(preempt_and_abort_list);
2859 	struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2860 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
2861 	u64 pr_res_mapped_lun = 0;
2862 	int all_reg = 0, calling_it_nexus = 0;
2863 	bool sa_res_key_unmatched = sa_res_key != 0;
2864 	int prh_type = 0, prh_scope = 0;
2865 
2866 	if (!se_sess)
2867 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2868 
2869 	pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2870 				se_sess);
2871 	if (!pr_reg_n) {
2872 		pr_err("SPC-3 PR: Unable to locate"
2873 			" PR_REGISTERED *pr_reg for PREEMPT%s\n",
2874 			(preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
2875 		return TCM_RESERVATION_CONFLICT;
2876 	}
2877 	if (pr_reg_n->pr_res_key != res_key) {
2878 		core_scsi3_put_pr_reg(pr_reg_n);
2879 		return TCM_RESERVATION_CONFLICT;
2880 	}
2881 
2882 	spin_lock(&dev->dev_reservation_lock);
2883 	pr_res_holder = dev->dev_pr_res_holder;
2884 	if (pr_res_holder &&
2885 	   ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2886 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2887 		all_reg = 1;
2888 
2889 	if (!all_reg && !sa_res_key) {
2890 		spin_unlock(&dev->dev_reservation_lock);
2891 		core_scsi3_put_pr_reg(pr_reg_n);
2892 		return TCM_INVALID_PARAMETER_LIST;
2893 	}
2894 
2895 	/* Validate TYPE and SCOPE fields if they will be used */
2896 	if (pr_res_holder &&
2897 	    (pr_res_holder->pr_res_key == sa_res_key ||
2898 	     (all_reg && !sa_res_key))) {
2899 		switch (type) {
2900 		case PR_TYPE_WRITE_EXCLUSIVE:
2901 		case PR_TYPE_EXCLUSIVE_ACCESS:
2902 		case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2903 		case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2904 		case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2905 		case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2906 			break;
2907 		default:
2908 			pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
2909 				" Type: 0x%02x\n",
2910 				(preempt_type == PREEMPT_AND_ABORT) ?
2911 				"_AND_ABORT" : "", type);
2912 			spin_unlock(&dev->dev_reservation_lock);
2913 			core_scsi3_put_pr_reg(pr_reg_n);
2914 			return TCM_INVALID_CDB_FIELD;
2915 		}
2916 
2917 		if (scope != PR_SCOPE_LU_SCOPE) {
2918 			pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2919 			spin_unlock(&dev->dev_reservation_lock);
2920 			core_scsi3_put_pr_reg(pr_reg_n);
2921 			return TCM_INVALID_PARAMETER_LIST;
2922 		}
2923 	}
2924 
2925 	/*
2926 	 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2927 	 *
2928 	 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2929 	 * persistent reservation holder or there is no persistent reservation
2930 	 * holder (i.e., there is no persistent reservation), then the device
2931 	 * server shall perform a preempt by doing the following in an
2932 	 * uninterrupted series of actions. (See below..)
2933 	 */
2934 	if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2935 		/*
2936 		 * No existing or SA Reservation Key matching reservations..
2937 		 *
2938 		 * PROUT SA PREEMPT with All Registrant type reservations are
2939 		 * allowed to be processed without a matching SA Reservation Key
2940 		 */
2941 		spin_lock(&pr_tmpl->registration_lock);
2942 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2943 				&pr_tmpl->registration_list, pr_reg_list) {
2944 			/*
2945 			 * Removing of registrations in non all registrants
2946 			 * type reservations without a matching SA reservation
2947 			 * key.
2948 			 *
2949 			 * a) Remove the registrations for all I_T nexuses
2950 			 *    specified by the SERVICE ACTION RESERVATION KEY
2951 			 *    field;
2952 			 * b) Ignore the contents of the SCOPE and TYPE fields;
2953 			 * c) Process tasks as defined in 5.7.1; and
2954 			 * d) Establish a unit attention condition for the
2955 			 *    initiator port associated with every I_T nexus
2956 			 *    that lost its registration other than the I_T
2957 			 *    nexus on which the PERSISTENT RESERVE OUT command
2958 			 *    was received, with the additional sense code set
2959 			 *    to REGISTRATIONS PREEMPTED.
2960 			 */
2961 			if (!all_reg) {
2962 				if (pr_reg->pr_res_key != sa_res_key)
2963 					continue;
2964 				sa_res_key_unmatched = false;
2965 
2966 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2967 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2968 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2969 				__core_scsi3_free_registration(dev, pr_reg,
2970 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2971 						NULL, calling_it_nexus);
2972 			} else {
2973 				/*
2974 				 * Case for any existing all registrants type
2975 				 * reservation, follow logic in spc4r17 section
2976 				 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2977 				 *
2978 				 * For a ZERO SA Reservation key, release
2979 				 * all other registrations and do an implicit
2980 				 * release of active persistent reservation.
2981 				 *
2982 				 * For a non-ZERO SA Reservation key, only
2983 				 * release the matching reservation key from
2984 				 * registrations.
2985 				 */
2986 				if ((sa_res_key) &&
2987 				     (pr_reg->pr_res_key != sa_res_key))
2988 					continue;
2989 				sa_res_key_unmatched = false;
2990 
2991 				calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2992 				if (calling_it_nexus)
2993 					continue;
2994 
2995 				pr_reg_nacl = pr_reg->pr_reg_nacl;
2996 				pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2997 				__core_scsi3_free_registration(dev, pr_reg,
2998 					(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
2999 						NULL, 0);
3000 			}
3001 			if (!calling_it_nexus)
3002 				target_ua_allocate_lun(pr_reg_nacl,
3003 					pr_res_mapped_lun, 0x2A,
3004 					ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3005 		}
3006 		spin_unlock(&pr_tmpl->registration_lock);
3007 		/*
3008 		 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3009 		 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3010 		 * RESERVATION KEY field to a value that does not match any
3011 		 * registered reservation key, then the device server shall
3012 		 * complete the command with RESERVATION CONFLICT status.
3013 		 */
3014 		if (sa_res_key_unmatched) {
3015 			spin_unlock(&dev->dev_reservation_lock);
3016 			core_scsi3_put_pr_reg(pr_reg_n);
3017 			return TCM_RESERVATION_CONFLICT;
3018 		}
3019 		/*
3020 		 * For an existing all registrants type reservation
3021 		 * with a zero SA rservation key, preempt the existing
3022 		 * reservation with the new PR type and scope.
3023 		 */
3024 		if (pr_res_holder && all_reg && !(sa_res_key)) {
3025 			__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3026 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3027 				type, scope, preempt_type);
3028 		}
3029 
3030 		spin_unlock(&dev->dev_reservation_lock);
3031 
3032 		/*
3033 		 * SPC-4 5.12.11.2.6 Preempting and aborting
3034 		 * The actions described in this subclause shall be performed
3035 		 * for all I_T nexuses that are registered with the non-zero
3036 		 * SERVICE ACTION RESERVATION KEY value, without regard for
3037 		 * whether the preempted I_T nexuses hold the persistent
3038 		 * reservation. If the SERVICE ACTION RESERVATION KEY field is
3039 		 * set to zero and an all registrants persistent reservation is
3040 		 * present, the device server shall abort all commands for all
3041 		 * registered I_T nexuses.
3042 		 */
3043 		if (preempt_type == PREEMPT_AND_ABORT) {
3044 			core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list,
3045 					   cmd);
3046 			core_scsi3_release_preempt_and_abort(
3047 				&preempt_and_abort_list, pr_reg_n);
3048 		}
3049 
3050 		if (pr_tmpl->pr_aptpl_active)
3051 			core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3052 
3053 		core_scsi3_put_pr_reg(pr_reg_n);
3054 		core_scsi3_pr_generation(cmd->se_dev);
3055 		return 0;
3056 	}
3057 	/*
3058 	 * The PREEMPTing SA reservation key matches that of the
3059 	 * existing persistent reservation, first, we check if
3060 	 * we are preempting our own reservation.
3061 	 * From spc4r17, section 5.7.11.4.3 Preempting
3062 	 * persistent reservations and registration handling
3063 	 *
3064 	 * If an all registrants persistent reservation is not
3065 	 * present, it is not an error for the persistent
3066 	 * reservation holder to preempt itself (i.e., a
3067 	 * PERSISTENT RESERVE OUT with a PREEMPT service action
3068 	 * or a PREEMPT AND ABORT service action with the
3069 	 * SERVICE ACTION RESERVATION KEY value equal to the
3070 	 * persistent reservation holder's reservation key that
3071 	 * is received from the persistent reservation holder).
3072 	 * In that case, the device server shall establish the
3073 	 * new persistent reservation and maintain the
3074 	 * registration.
3075 	 */
3076 	prh_type = pr_res_holder->pr_res_type;
3077 	prh_scope = pr_res_holder->pr_res_scope;
3078 	/*
3079 	 * If the SERVICE ACTION RESERVATION KEY field identifies a
3080 	 * persistent reservation holder (see 5.7.10), the device
3081 	 * server shall perform a preempt by doing the following as
3082 	 * an uninterrupted series of actions:
3083 	 *
3084 	 * a) Release the persistent reservation for the holder
3085 	 *    identified by the SERVICE ACTION RESERVATION KEY field;
3086 	 */
3087 	if (pr_reg_n != pr_res_holder)
3088 		__core_scsi3_complete_pro_release(dev,
3089 						  pr_res_holder->pr_reg_nacl,
3090 						  dev->dev_pr_res_holder, 0, 0);
3091 	/*
3092 	 * b) Remove the registrations for all I_T nexuses identified
3093 	 *    by the SERVICE ACTION RESERVATION KEY field, except the
3094 	 *    I_T nexus that is being used for the PERSISTENT RESERVE
3095 	 *    OUT command. If an all registrants persistent reservation
3096 	 *    is present and the SERVICE ACTION RESERVATION KEY field
3097 	 *    is set to zero, then all registrations shall be removed
3098 	 *    except for that of the I_T nexus that is being used for
3099 	 *    the PERSISTENT RESERVE OUT command;
3100 	 */
3101 	spin_lock(&pr_tmpl->registration_lock);
3102 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3103 			&pr_tmpl->registration_list, pr_reg_list) {
3104 
3105 		calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3106 		if (calling_it_nexus)
3107 			continue;
3108 
3109 		if (sa_res_key && pr_reg->pr_res_key != sa_res_key)
3110 			continue;
3111 
3112 		pr_reg_nacl = pr_reg->pr_reg_nacl;
3113 		pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3114 		__core_scsi3_free_registration(dev, pr_reg,
3115 				(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3116 				calling_it_nexus);
3117 		/*
3118 		 * e) Establish a unit attention condition for the initiator
3119 		 *    port associated with every I_T nexus that lost its
3120 		 *    persistent reservation and/or registration, with the
3121 		 *    additional sense code set to REGISTRATIONS PREEMPTED;
3122 		 */
3123 		target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3124 				ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3125 	}
3126 	spin_unlock(&pr_tmpl->registration_lock);
3127 	/*
3128 	 * c) Establish a persistent reservation for the preempting
3129 	 *    I_T nexus using the contents of the SCOPE and TYPE fields;
3130 	 */
3131 	__core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3132 			(preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3133 			type, scope, preempt_type);
3134 	/*
3135 	 * d) Process tasks as defined in 5.7.1;
3136 	 * e) See above..
3137 	 * f) If the type or scope has changed, then for every I_T nexus
3138 	 *    whose reservation key was not removed, except for the I_T
3139 	 *    nexus on which the PERSISTENT RESERVE OUT command was
3140 	 *    received, the device server shall establish a unit
3141 	 *    attention condition for the initiator port associated with
3142 	 *    that I_T nexus, with the additional sense code set to
3143 	 *    RESERVATIONS RELEASED. If the type or scope have not
3144 	 *    changed, then no unit attention condition(s) shall be
3145 	 *    established for this reason.
3146 	 */
3147 	if ((prh_type != type) || (prh_scope != scope)) {
3148 		spin_lock(&pr_tmpl->registration_lock);
3149 		list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3150 				&pr_tmpl->registration_list, pr_reg_list) {
3151 
3152 			calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3153 			if (calling_it_nexus)
3154 				continue;
3155 
3156 			target_ua_allocate_lun(pr_reg->pr_reg_nacl,
3157 					pr_reg->pr_res_mapped_lun, 0x2A,
3158 					ASCQ_2AH_RESERVATIONS_RELEASED);
3159 		}
3160 		spin_unlock(&pr_tmpl->registration_lock);
3161 	}
3162 	spin_unlock(&dev->dev_reservation_lock);
3163 	/*
3164 	 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3165 	 * All received CDBs for the matching existing reservation and
3166 	 * registrations undergo ABORT_TASK logic.
3167 	 *
3168 	 * From there, core_scsi3_release_preempt_and_abort() will
3169 	 * release every registration in the list (which have already
3170 	 * been removed from the primary pr_reg list), except the
3171 	 * new persistent reservation holder, the calling Initiator Port.
3172 	 */
3173 	if (preempt_type == PREEMPT_AND_ABORT) {
3174 		core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3175 		core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3176 						pr_reg_n);
3177 	}
3178 
3179 	if (pr_tmpl->pr_aptpl_active)
3180 		core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
3181 
3182 	core_scsi3_put_pr_reg(pr_reg_n);
3183 	core_scsi3_pr_generation(cmd->se_dev);
3184 	return 0;
3185 }
3186 
3187 static sense_reason_t
3188 core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3189 		u64 sa_res_key, int aptpl, int unreg)
3190 {
3191 	struct se_session *se_sess = cmd->se_sess;
3192 	struct se_device *dev = cmd->se_dev;
3193 	struct se_dev_entry *dest_se_deve = NULL;
3194 	struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
3195 	struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3196 	struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3197 	const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3198 	struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3199 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
3200 	unsigned char *buf;
3201 	unsigned char initiator_str[TRANSPORT_IQN_LEN];
3202 	char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN] = { };
3203 	u32 tid_len, tmp_tid_len;
3204 	int new_reg = 0, type, scope, matching_iname;
3205 	sense_reason_t ret;
3206 	unsigned short rtpi;
3207 	unsigned char proto_ident;
3208 	bool tid_found;
3209 
3210 	if (!se_sess || !se_lun) {
3211 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3212 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3213 	}
3214 
3215 	se_tpg = se_sess->se_tpg;
3216 	tf_ops = se_tpg->se_tpg_tfo;
3217 	/*
3218 	 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3219 	 *	Register behaviors for a REGISTER AND MOVE service action
3220 	 *
3221 	 * Locate the existing *pr_reg via struct se_node_acl pointers
3222 	 */
3223 	pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3224 				se_sess);
3225 	if (!pr_reg) {
3226 		pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3227 			" *pr_reg for REGISTER_AND_MOVE\n");
3228 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3229 	}
3230 	/*
3231 	 * The provided reservation key much match the existing reservation key
3232 	 * provided during this initiator's I_T nexus registration.
3233 	 */
3234 	if (res_key != pr_reg->pr_res_key) {
3235 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3236 			" res_key: 0x%016Lx does not match existing SA REGISTER"
3237 			" res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3238 		ret = TCM_RESERVATION_CONFLICT;
3239 		goto out_put_pr_reg;
3240 	}
3241 	/*
3242 	 * The service active reservation key needs to be non zero
3243 	 */
3244 	if (!sa_res_key) {
3245 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3246 			" sa_res_key\n");
3247 		ret = TCM_INVALID_PARAMETER_LIST;
3248 		goto out_put_pr_reg;
3249 	}
3250 
3251 	/*
3252 	 * Determine the Relative Target Port Identifier where the reservation
3253 	 * will be moved to for the TransportID containing SCSI initiator WWN
3254 	 * information.
3255 	 */
3256 	buf = transport_kmap_data_sg(cmd);
3257 	if (!buf) {
3258 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3259 		goto out_put_pr_reg;
3260 	}
3261 
3262 	rtpi = get_unaligned_be16(&buf[18]);
3263 	tid_len = get_unaligned_be32(&buf[20]);
3264 	transport_kunmap_data_sg(cmd);
3265 	buf = NULL;
3266 
3267 	/* TransportID is at least 24 bytes. buf[24] is its first byte. */
3268 	if (tid_len < 24 ||
3269 	    tid_len != cmd->data_length - 24) {
3270 		pr_err("SPC-3 PR: Illegal tid_len: %u for data_length: %u\n",
3271 		       tid_len, cmd->data_length);
3272 		ret = TCM_INVALID_PARAMETER_LIST;
3273 		goto out_put_pr_reg;
3274 	}
3275 
3276 	spin_lock(&dev->se_port_lock);
3277 	list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3278 		if (tmp_lun->lun_tpg->tpg_rtpi != rtpi)
3279 			continue;
3280 		dest_se_tpg = tmp_lun->lun_tpg;
3281 		dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3282 		if (!dest_tf_ops)
3283 			continue;
3284 
3285 		atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
3286 		spin_unlock(&dev->se_port_lock);
3287 
3288 		if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3289 			pr_err("core_scsi3_tpg_depend_item() failed"
3290 				" for dest_se_tpg\n");
3291 			atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
3292 			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3293 			goto out_put_pr_reg;
3294 		}
3295 
3296 		spin_lock(&dev->se_port_lock);
3297 		break;
3298 	}
3299 	spin_unlock(&dev->se_port_lock);
3300 
3301 	if (!dest_se_tpg || !dest_tf_ops) {
3302 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3303 			" fabric ops from Relative Target Port Identifier:"
3304 			" %hu\n", rtpi);
3305 		ret = TCM_INVALID_PARAMETER_LIST;
3306 		goto out_put_pr_reg;
3307 	}
3308 
3309 	buf = transport_kmap_data_sg(cmd);
3310 	if (!buf) {
3311 		ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3312 		goto out_put_pr_reg;
3313 	}
3314 	proto_ident = (buf[24] & 0x0f);
3315 
3316 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3317 			" 0x%02x\n", proto_ident);
3318 
3319 	if (proto_ident != dest_se_tpg->proto_id) {
3320 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3321 			" proto_ident: 0x%02x does not match ident: 0x%02x"
3322 			" from fabric: %s\n", proto_ident,
3323 			dest_se_tpg->proto_id,
3324 			dest_tf_ops->fabric_name);
3325 		ret = TCM_INVALID_PARAMETER_LIST;
3326 		goto out;
3327 	}
3328 	tid_found = target_parse_pr_out_transport_id(dest_se_tpg,
3329 			&buf[24], tid_len, &tmp_tid_len, &iport_ptr, initiator_str);
3330 	if (!tid_found) {
3331 		pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3332 			" initiator_str from Transport ID\n");
3333 		ret = TCM_INVALID_PARAMETER_LIST;
3334 		goto out;
3335 	}
3336 
3337 	pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3338 		" %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
3339 		"port" : "device", initiator_str, (iport_ptr != NULL) ?
3340 		iport_ptr : "");
3341 	/*
3342 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3343 	 * action specifies a TransportID that is the same as the initiator port
3344 	 * of the I_T nexus for the command received, then the command shall
3345 	 * be terminated with CHECK CONDITION status, with the sense key set to
3346 	 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3347 	 * IN PARAMETER LIST.
3348 	 */
3349 	pr_reg_nacl = pr_reg->pr_reg_nacl;
3350 	matching_iname = (!strcmp(initiator_str,
3351 				  pr_reg_nacl->initiatorname)) ? 1 : 0;
3352 	if (!matching_iname)
3353 		goto after_iport_check;
3354 
3355 	if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3356 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3357 			" matches: %s on received I_T Nexus\n", initiator_str,
3358 			pr_reg_nacl->initiatorname);
3359 		ret = TCM_INVALID_PARAMETER_LIST;
3360 		goto out;
3361 	}
3362 	if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3363 		pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3364 			" matches: %s %s on received I_T Nexus\n",
3365 			initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3366 			pr_reg->pr_reg_isid);
3367 		ret = TCM_INVALID_PARAMETER_LIST;
3368 		goto out;
3369 	}
3370 after_iport_check:
3371 	/*
3372 	 * Locate the destination struct se_node_acl from the received Transport ID
3373 	 */
3374 	mutex_lock(&dest_se_tpg->acl_node_mutex);
3375 	dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3376 				initiator_str);
3377 	if (dest_node_acl)
3378 		atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
3379 	mutex_unlock(&dest_se_tpg->acl_node_mutex);
3380 
3381 	if (!dest_node_acl) {
3382 		pr_err("Unable to locate %s dest_node_acl for"
3383 			" TransportID%s\n", dest_tf_ops->fabric_name,
3384 			initiator_str);
3385 		ret = TCM_INVALID_PARAMETER_LIST;
3386 		goto out;
3387 	}
3388 
3389 	if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3390 		pr_err("core_scsi3_nodeacl_depend_item() for"
3391 			" dest_node_acl\n");
3392 		atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
3393 		dest_node_acl = NULL;
3394 		ret = TCM_INVALID_PARAMETER_LIST;
3395 		goto out;
3396 	}
3397 
3398 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3399 		" %s from TransportID\n", dest_tf_ops->fabric_name,
3400 		dest_node_acl->initiatorname);
3401 
3402 	/*
3403 	 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3404 	 * PORT IDENTIFIER.
3405 	 */
3406 	dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3407 	if (!dest_se_deve) {
3408 		pr_err("Unable to locate %s dest_se_deve from RTPI:"
3409 			" %hu\n",  dest_tf_ops->fabric_name, rtpi);
3410 		ret = TCM_INVALID_PARAMETER_LIST;
3411 		goto out;
3412 	}
3413 
3414 	if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3415 		pr_err("core_scsi3_lunacl_depend_item() failed\n");
3416 		kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
3417 		dest_se_deve = NULL;
3418 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3419 		goto out;
3420 	}
3421 
3422 	pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3423 		" ACL for dest_se_deve->mapped_lun: %llu\n",
3424 		dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
3425 		dest_se_deve->mapped_lun);
3426 
3427 	/*
3428 	 * A persistent reservation needs to already existing in order to
3429 	 * successfully complete the REGISTER_AND_MOVE service action..
3430 	 */
3431 	spin_lock(&dev->dev_reservation_lock);
3432 	pr_res_holder = dev->dev_pr_res_holder;
3433 	if (!pr_res_holder) {
3434 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3435 			" currently held\n");
3436 		spin_unlock(&dev->dev_reservation_lock);
3437 		ret = TCM_INVALID_CDB_FIELD;
3438 		goto out;
3439 	}
3440 	/*
3441 	 * The received on I_T Nexus must be the reservation holder.
3442 	 *
3443 	 * From spc4r17 section 5.7.8  Table 50 --
3444 	 * 	Register behaviors for a REGISTER AND MOVE service action
3445 	 */
3446 	if (!is_reservation_holder(pr_res_holder, pr_reg)) {
3447 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3448 			" Nexus is not reservation holder\n");
3449 		spin_unlock(&dev->dev_reservation_lock);
3450 		ret = TCM_RESERVATION_CONFLICT;
3451 		goto out;
3452 	}
3453 	/*
3454 	 * From spc4r17 section 5.7.8: registering and moving reservation
3455 	 *
3456 	 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3457 	 * action is received and the established persistent reservation is a
3458 	 * Write Exclusive - All Registrants type or Exclusive Access -
3459 	 * All Registrants type reservation, then the command shall be completed
3460 	 * with RESERVATION CONFLICT status.
3461 	 */
3462 	if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3463 	    (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3464 		pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3465 			" reservation for type: %s\n",
3466 			core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3467 		spin_unlock(&dev->dev_reservation_lock);
3468 		ret = TCM_RESERVATION_CONFLICT;
3469 		goto out;
3470 	}
3471 	pr_res_nacl = pr_res_holder->pr_reg_nacl;
3472 	/*
3473 	 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3474 	 */
3475 	type = pr_res_holder->pr_res_type;
3476 	scope = pr_res_holder->pr_res_type;
3477 	/*
3478 	 * c) Associate the reservation key specified in the SERVICE ACTION
3479 	 *    RESERVATION KEY field with the I_T nexus specified as the
3480 	 *    destination of the register and move, where:
3481 	 *    A) The I_T nexus is specified by the TransportID and the
3482 	 *	 RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3483 	 *    B) Regardless of the TransportID format used, the association for
3484 	 *       the initiator port is based on either the initiator port name
3485 	 *       (see 3.1.71) on SCSI transport protocols where port names are
3486 	 *       required or the initiator port identifier (see 3.1.70) on SCSI
3487 	 *       transport protocols where port names are not required;
3488 	 * d) Register the reservation key specified in the SERVICE ACTION
3489 	 *    RESERVATION KEY field;
3490 	 *
3491 	 * Also, It is not an error for a REGISTER AND MOVE service action to
3492 	 * register an I_T nexus that is already registered with the same
3493 	 * reservation key or a different reservation key.
3494 	 */
3495 	dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3496 					iport_ptr);
3497 	if (!dest_pr_reg) {
3498 		struct se_lun *dest_lun = dest_se_deve->se_lun;
3499 
3500 		spin_unlock(&dev->dev_reservation_lock);
3501 		if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3502 					dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3503 					iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
3504 			ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
3505 			goto out;
3506 		}
3507 		spin_lock(&dev->dev_reservation_lock);
3508 		dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3509 						iport_ptr);
3510 		new_reg = 1;
3511 	} else {
3512 		/*
3513 		 * e) Retain the reservation key specified in the SERVICE ACTION
3514 		 *    RESERVATION KEY field and associated information;
3515 		 */
3516 		dest_pr_reg->pr_res_key = sa_res_key;
3517 	}
3518 	/*
3519 	 * f) Release the persistent reservation for the persistent reservation
3520 	 *    holder (i.e., the I_T nexus on which the
3521 	 */
3522 	__core_scsi3_complete_pro_release(dev, pr_res_nacl,
3523 					  dev->dev_pr_res_holder, 0, 0);
3524 	/*
3525 	 * g) Move the persistent reservation to the specified I_T nexus using
3526 	 *    the same scope and type as the persistent reservation released in
3527 	 *    item f); and
3528 	 */
3529 	dev->dev_pr_res_holder = dest_pr_reg;
3530 	dest_pr_reg->pr_res_holder = 1;
3531 	dest_pr_reg->pr_res_type = type;
3532 	pr_reg->pr_res_scope = scope;
3533 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
3534 	/*
3535 	 * Increment PRGeneration for existing registrations..
3536 	 */
3537 	if (!new_reg)
3538 		dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3539 	spin_unlock(&dev->dev_reservation_lock);
3540 
3541 	pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3542 		" created new reservation holder TYPE: %s on object RTPI:"
3543 		" %hu  PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
3544 		core_scsi3_pr_dump_type(type), rtpi,
3545 		dest_pr_reg->pr_res_generation);
3546 	pr_debug("SPC-3 PR Successfully moved reservation from"
3547 		" %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3548 		tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3549 		i_buf, dest_tf_ops->fabric_name,
3550 		dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3551 		iport_ptr : "");
3552 	/*
3553 	 * It is now safe to release configfs group dependencies for destination
3554 	 * of Transport ID Initiator Device/Port Identifier
3555 	 */
3556 	core_scsi3_lunacl_undepend_item(dest_se_deve);
3557 	core_scsi3_nodeacl_undepend_item(dest_node_acl);
3558 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3559 	/*
3560 	 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3561 	 * nexus on which PERSISTENT RESERVE OUT command was received.
3562 	 */
3563 	if (unreg) {
3564 		spin_lock(&pr_tmpl->registration_lock);
3565 		__core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3566 		spin_unlock(&pr_tmpl->registration_lock);
3567 	} else
3568 		core_scsi3_put_pr_reg(pr_reg);
3569 
3570 	core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
3571 
3572 	core_scsi3_put_pr_reg(dest_pr_reg);
3573 	/*
3574 	 * iport_ptr aliases the PR-OUT parameter list mapped above, so the
3575 	 * buffer is unmapped only here on success (and at out: on error).
3576 	 */
3577 	transport_kunmap_data_sg(cmd);
3578 	return 0;
3579 out:
3580 	if (buf)
3581 		transport_kunmap_data_sg(cmd);
3582 	if (dest_se_deve)
3583 		core_scsi3_lunacl_undepend_item(dest_se_deve);
3584 	if (dest_node_acl)
3585 		core_scsi3_nodeacl_undepend_item(dest_node_acl);
3586 	core_scsi3_tpg_undepend_item(dest_se_tpg);
3587 
3588 out_put_pr_reg:
3589 	core_scsi3_put_pr_reg(pr_reg);
3590 	return ret;
3591 }
3592 
3593 static sense_reason_t
3594 target_try_pr_out_pt(struct se_cmd *cmd, u8 sa, u64 res_key, u64 sa_res_key,
3595 		     u8 type, bool aptpl, bool all_tg_pt, bool spec_i_pt)
3596 {
3597 	struct exec_cmd_ops *ops = cmd->protocol_data;
3598 
3599 	if (!cmd->se_sess || !cmd->se_lun) {
3600 		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3601 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3602 	}
3603 
3604 	if (!ops->execute_pr_out) {
3605 		pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n");
3606 		return TCM_UNSUPPORTED_SCSI_OPCODE;
3607 	}
3608 
3609 	switch (sa) {
3610 	case PRO_REGISTER_AND_MOVE:
3611 	case PRO_REPLACE_LOST_RESERVATION:
3612 		pr_err("SPC-3 PR: PRO_REGISTER_AND_MOVE and PRO_REPLACE_LOST_RESERVATION are not supported by PR passthrough.\n");
3613 		return TCM_UNSUPPORTED_SCSI_OPCODE;
3614 	}
3615 
3616 	if (spec_i_pt || all_tg_pt) {
3617 		pr_err("SPC-3 PR: SPEC_I_PT and ALL_TG_PT are not supported by PR passthrough.\n");
3618 		return TCM_UNSUPPORTED_SCSI_OPCODE;
3619 	}
3620 
3621 	return ops->execute_pr_out(cmd, sa, res_key, sa_res_key, type, aptpl);
3622 }
3623 
3624 /*
3625  * See spc4r17 section 6.14 Table 170
3626  */
3627 sense_reason_t
3628 target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3629 {
3630 	struct se_device *dev = cmd->se_dev;
3631 	unsigned char *cdb = &cmd->t_task_cdb[0];
3632 	unsigned char *buf;
3633 	u64 res_key, sa_res_key;
3634 	int sa, scope, type, aptpl;
3635 	int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3636 	sense_reason_t ret;
3637 
3638 	/*
3639 	 * Following spc2r20 5.5.1 Reservations overview:
3640 	 *
3641 	 * If a logical unit has been reserved by any RESERVE command and is
3642 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3643 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3644 	 * initiator or service action and shall terminate with a RESERVATION
3645 	 * CONFLICT status.
3646 	 */
3647 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3648 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3649 			" SPC-2 reservation is held, returning"
3650 			" RESERVATION_CONFLICT\n");
3651 		return TCM_RESERVATION_CONFLICT;
3652 	}
3653 
3654 	/*
3655 	 * FIXME: A NULL struct se_session pointer means an this is not coming from
3656 	 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3657 	 */
3658 	if (!cmd->se_sess)
3659 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3660 
3661 	if (cmd->data_length < 24) {
3662 		pr_warn("SPC-PR: Received PR OUT parameter list"
3663 			" length too small: %u\n", cmd->data_length);
3664 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3665 	}
3666 
3667 	/*
3668 	 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3669 	 */
3670 	sa = (cdb[1] & 0x1f);
3671 	scope = (cdb[2] & 0xf0);
3672 	type = (cdb[2] & 0x0f);
3673 
3674 	buf = transport_kmap_data_sg(cmd);
3675 	if (!buf)
3676 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3677 
3678 	/*
3679 	 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3680 	 */
3681 	res_key = get_unaligned_be64(&buf[0]);
3682 	sa_res_key = get_unaligned_be64(&buf[8]);
3683 	/*
3684 	 * REGISTER_AND_MOVE uses a different SA parameter list containing
3685 	 * SCSI TransportIDs.
3686 	 */
3687 	if (sa != PRO_REGISTER_AND_MOVE) {
3688 		spec_i_pt = (buf[20] & 0x08);
3689 		all_tg_pt = (buf[20] & 0x04);
3690 		aptpl = (buf[20] & 0x01);
3691 	} else {
3692 		aptpl = (buf[17] & 0x01);
3693 		unreg = (buf[17] & 0x02);
3694 	}
3695 	/*
3696 	 * If the backend device has been configured to force APTPL metadata
3697 	 * write-out, go ahead and propigate aptpl=1 down now.
3698 	 */
3699 	if (dev->dev_attrib.force_pr_aptpl)
3700 		aptpl = 1;
3701 
3702 	transport_kunmap_data_sg(cmd);
3703 	buf = NULL;
3704 
3705 	/*
3706 	 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3707 	 */
3708 	if (spec_i_pt && (sa != PRO_REGISTER))
3709 		return TCM_INVALID_PARAMETER_LIST;
3710 
3711 	/*
3712 	 * From spc4r17 section 6.14:
3713 	 *
3714 	 * If the SPEC_I_PT bit is set to zero, the service action is not
3715 	 * REGISTER AND MOVE, and the parameter list length is not 24, then
3716 	 * the command shall be terminated with CHECK CONDITION status, with
3717 	 * the sense key set to ILLEGAL REQUEST, and the additional sense
3718 	 * code set to PARAMETER LIST LENGTH ERROR.
3719 	 */
3720 	if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
3721 	    (cmd->data_length != 24)) {
3722 		pr_warn("SPC-PR: Received PR OUT illegal parameter"
3723 			" list length: %u\n", cmd->data_length);
3724 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
3725 	}
3726 
3727 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) {
3728 		ret = target_try_pr_out_pt(cmd, sa, res_key, sa_res_key, type,
3729 					   aptpl, all_tg_pt, spec_i_pt);
3730 		goto done;
3731 	}
3732 
3733 	/*
3734 	 * (core_scsi3_emulate_pro_* function parameters
3735 	 * are defined by spc4r17 Table 174:
3736 	 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3737 	 */
3738 	switch (sa) {
3739 	case PRO_REGISTER:
3740 		ret = core_scsi3_emulate_pro_register(cmd,
3741 			res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
3742 		break;
3743 	case PRO_RESERVE:
3744 		ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3745 		break;
3746 	case PRO_RELEASE:
3747 		ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3748 		break;
3749 	case PRO_CLEAR:
3750 		ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3751 		break;
3752 	case PRO_PREEMPT:
3753 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3754 					res_key, sa_res_key, PREEMPT);
3755 		break;
3756 	case PRO_PREEMPT_AND_ABORT:
3757 		ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3758 					res_key, sa_res_key, PREEMPT_AND_ABORT);
3759 		break;
3760 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3761 		ret = core_scsi3_emulate_pro_register(cmd,
3762 			0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
3763 		break;
3764 	case PRO_REGISTER_AND_MOVE:
3765 		ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3766 				sa_res_key, aptpl, unreg);
3767 		break;
3768 	default:
3769 		pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3770 			" action: 0x%02x\n", sa);
3771 		return TCM_INVALID_CDB_FIELD;
3772 	}
3773 
3774 done:
3775 	if (!ret)
3776 		target_complete_cmd(cmd, SAM_STAT_GOOD);
3777 	return ret;
3778 }
3779 
3780 /*
3781  * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3782  *
3783  * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3784  */
3785 static sense_reason_t
3786 core_scsi3_pri_read_keys(struct se_cmd *cmd)
3787 {
3788 	struct se_device *dev = cmd->se_dev;
3789 	struct t10_pr_registration *pr_reg;
3790 	unsigned char *buf;
3791 	u32 add_len = 0, off = 8;
3792 
3793 	if (cmd->data_length < 8) {
3794 		pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3795 			" too small\n", cmd->data_length);
3796 		return TCM_INVALID_CDB_FIELD;
3797 	}
3798 
3799 	buf = transport_kmap_data_sg(cmd);
3800 	if (!buf)
3801 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3802 
3803 	put_unaligned_be32(dev->t10_pr.pr_generation, buf);
3804 
3805 	spin_lock(&dev->t10_pr.registration_lock);
3806 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3807 			pr_reg_list) {
3808 		/*
3809 		 * Check for overflow of 8byte PRI READ_KEYS payload and
3810 		 * next reservation key list descriptor.
3811 		 */
3812 		if (off + 8 <= cmd->data_length) {
3813 			put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3814 			off += 8;
3815 		}
3816 		/*
3817 		 * SPC5r17: 6.16.2 READ KEYS service action
3818 		 * The ADDITIONAL LENGTH field indicates the number of bytes in
3819 		 * the Reservation key list. The contents of the ADDITIONAL
3820 		 * LENGTH field are not altered based on the allocation length
3821 		 */
3822 		add_len += 8;
3823 	}
3824 	spin_unlock(&dev->t10_pr.registration_lock);
3825 
3826 	put_unaligned_be32(add_len, &buf[4]);
3827 	target_set_cmd_data_length(cmd, 8 + add_len);
3828 
3829 	transport_kunmap_data_sg(cmd);
3830 
3831 	return 0;
3832 }
3833 
3834 /*
3835  * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3836  *
3837  * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3838  */
3839 static sense_reason_t
3840 core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3841 {
3842 	struct se_device *dev = cmd->se_dev;
3843 	struct t10_pr_registration *pr_reg;
3844 	unsigned char *buf;
3845 	u64 pr_res_key;
3846 	u32 add_len = 0;
3847 
3848 	if (cmd->data_length < 8) {
3849 		pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3850 			" too small\n", cmd->data_length);
3851 		return TCM_INVALID_CDB_FIELD;
3852 	}
3853 
3854 	buf = transport_kmap_data_sg(cmd);
3855 	if (!buf)
3856 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3857 
3858 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
3859 
3860 	spin_lock(&dev->dev_reservation_lock);
3861 	pr_reg = dev->dev_pr_res_holder;
3862 	if (pr_reg) {
3863 		/*
3864 		 * Set the Additional Length to 16 when a reservation is held
3865 		 */
3866 		add_len = 16;
3867 		put_unaligned_be32(add_len, &buf[4]);
3868 
3869 		if (cmd->data_length < 22)
3870 			goto err;
3871 
3872 		/*
3873 		 * Set the Reservation key.
3874 		 *
3875 		 * From spc4r17, section 5.7.10:
3876 		 * A persistent reservation holder has its reservation key
3877 		 * returned in the parameter data from a PERSISTENT
3878 		 * RESERVE IN command with READ RESERVATION service action as
3879 		 * follows:
3880 		 * a) For a persistent reservation of the type Write Exclusive
3881 		 *    - All Registrants or Exclusive Access ­ All Regitrants,
3882 		 *      the reservation key shall be set to zero; or
3883 		 * b) For all other persistent reservation types, the
3884 		 *    reservation key shall be set to the registered
3885 		 *    reservation key for the I_T nexus that holds the
3886 		 *    persistent reservation.
3887 		 */
3888 		if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3889 		    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3890 			pr_res_key = 0;
3891 		else
3892 			pr_res_key = pr_reg->pr_res_key;
3893 
3894 		put_unaligned_be64(pr_res_key, &buf[8]);
3895 		/*
3896 		 * Set the SCOPE and TYPE
3897 		 */
3898 		buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3899 			  (pr_reg->pr_res_type & 0x0f);
3900 	}
3901 
3902 	target_set_cmd_data_length(cmd, 8 + add_len);
3903 
3904 err:
3905 	spin_unlock(&dev->dev_reservation_lock);
3906 	transport_kunmap_data_sg(cmd);
3907 
3908 	return 0;
3909 }
3910 
3911 /*
3912  * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3913  *
3914  * See spc4r17 section 6.13.4 Table 165
3915  */
3916 static sense_reason_t
3917 core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3918 {
3919 	struct se_device *dev = cmd->se_dev;
3920 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
3921 	unsigned char *buf;
3922 	u16 len = 8; /* Hardcoded to 8. */
3923 
3924 	if (cmd->data_length < 6) {
3925 		pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3926 			" %u too small\n", cmd->data_length);
3927 		return TCM_INVALID_CDB_FIELD;
3928 	}
3929 
3930 	buf = transport_kmap_data_sg(cmd);
3931 	if (!buf)
3932 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3933 
3934 	put_unaligned_be16(len, &buf[0]);
3935 	buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3936 	buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3937 	buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3938 	buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3939 	/*
3940 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3941 	 * set the TMV: Task Mask Valid bit.
3942 	 */
3943 	buf[3] |= 0x80;
3944 	/*
3945 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3946 	 */
3947 	buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3948 	/*
3949 	 * PTPL_A: Persistence across Target Power Loss Active bit
3950 	 */
3951 	if (pr_tmpl->pr_aptpl_active)
3952 		buf[3] |= 0x01;
3953 	/*
3954 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3955 	 */
3956 	buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3957 	buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3958 	buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3959 	buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3960 	buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3961 	buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3962 
3963 	target_set_cmd_data_length(cmd, len);
3964 
3965 	transport_kunmap_data_sg(cmd);
3966 
3967 	return 0;
3968 }
3969 
3970 /*
3971  * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3972  *
3973  * See spc4r17 section 6.13.5 Table 168 and 169
3974  */
3975 static sense_reason_t
3976 core_scsi3_pri_read_full_status(struct se_cmd *cmd)
3977 {
3978 	struct se_device *dev = cmd->se_dev;
3979 	struct se_node_acl *se_nacl;
3980 	struct se_portal_group *se_tpg;
3981 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
3982 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
3983 	unsigned char *buf;
3984 	u32 add_desc_len = 0, add_len = 0;
3985 	u32 off = 8; /* off into first Full Status descriptor */
3986 	int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
3987 	int exp_desc_len, desc_len;
3988 	bool all_reg = false;
3989 
3990 	if (cmd->data_length < 8) {
3991 		pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3992 			" too small\n", cmd->data_length);
3993 		return TCM_INVALID_CDB_FIELD;
3994 	}
3995 
3996 	buf = transport_kmap_data_sg(cmd);
3997 	if (!buf)
3998 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3999 
4000 	put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
4001 
4002 	spin_lock(&dev->dev_reservation_lock);
4003 	if (dev->dev_pr_res_holder) {
4004 		struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
4005 
4006 		if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
4007 		    pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
4008 			all_reg = true;
4009 			pr_res_type = pr_holder->pr_res_type;
4010 			pr_res_scope = pr_holder->pr_res_scope;
4011 		}
4012 	}
4013 	spin_unlock(&dev->dev_reservation_lock);
4014 
4015 	spin_lock(&pr_tmpl->registration_lock);
4016 	list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4017 			&pr_tmpl->registration_list, pr_reg_list) {
4018 
4019 		se_nacl = pr_reg->pr_reg_nacl;
4020 		se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4021 		add_desc_len = 0;
4022 
4023 		atomic_inc_mb(&pr_reg->pr_res_holders);
4024 		spin_unlock(&pr_tmpl->registration_lock);
4025 		/*
4026 		 * Determine expected length of $FABRIC_MOD specific
4027 		 * TransportID full status descriptor..
4028 		 */
4029 		exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
4030 					&format_code);
4031 		if (exp_desc_len < 0 ||
4032 		    exp_desc_len + add_len > cmd->data_length) {
4033 			pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4034 				" out of buffer: %d\n", cmd->data_length);
4035 			spin_lock(&pr_tmpl->registration_lock);
4036 			atomic_dec_mb(&pr_reg->pr_res_holders);
4037 			break;
4038 		}
4039 		/*
4040 		 * Set RESERVATION KEY
4041 		 */
4042 		put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
4043 		off += 8;
4044 		off += 4; /* Skip Over Reserved area */
4045 
4046 		/*
4047 		 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4048 		 */
4049 		if (pr_reg->pr_reg_all_tg_pt)
4050 			buf[off] = 0x02;
4051 		/*
4052 		 * The struct se_lun pointer will be present for the
4053 		 * reservation holder for PR_HOLDER bit.
4054 		 *
4055 		 * Also, if this registration is the reservation
4056 		 * holder or there is an All Registrants reservation
4057 		 * active, fill in SCOPE and TYPE in the next byte.
4058 		 */
4059 		if (pr_reg->pr_res_holder) {
4060 			buf[off++] |= 0x01;
4061 			buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4062 				     (pr_reg->pr_res_type & 0x0f);
4063 		} else if (all_reg) {
4064 			buf[off++] |= 0x01;
4065 			buf[off++] = (pr_res_scope & 0xf0) |
4066 				     (pr_res_type & 0x0f);
4067 		} else {
4068 			off += 2;
4069 		}
4070 
4071 		off += 4; /* Skip over reserved area */
4072 		/*
4073 		 * From spc4r17 6.3.15:
4074 		 *
4075 		 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4076 		 * IDENTIFIER field contains the relative port identifier (see
4077 		 * 3.1.120) of the target port that is part of the I_T nexus
4078 		 * described by this full status descriptor. If the ALL_TG_PT
4079 		 * bit is set to one, the contents of the RELATIVE TARGET PORT
4080 		 * IDENTIFIER field are not defined by this standard.
4081 		 */
4082 		if (!pr_reg->pr_reg_all_tg_pt) {
4083 			u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
4084 
4085 			put_unaligned_be16(sep_rtpi, &buf[off]);
4086 			off += 2;
4087 		} else
4088 			off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4089 
4090 		buf[off+4] = se_tpg->proto_id;
4091 
4092 		/*
4093 		 * Now, have the $FABRIC_MOD fill in the transport ID.
4094 		 */
4095 		desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4096 				&format_code, &buf[off+4]);
4097 
4098 		spin_lock(&pr_tmpl->registration_lock);
4099 		atomic_dec_mb(&pr_reg->pr_res_holders);
4100 
4101 		if (desc_len < 0)
4102 			break;
4103 		/*
4104 		 * Set the ADDITIONAL DESCRIPTOR LENGTH
4105 		 */
4106 		put_unaligned_be32(desc_len, &buf[off]);
4107 		off += 4;
4108 		/*
4109 		 * Size of full desctipor header minus TransportID
4110 		 * containing $FABRIC_MOD specific) initiator device/port
4111 		 * WWN information.
4112 		 *
4113 		 *  See spc4r17 Section 6.13.5 Table 169
4114 		 */
4115 		add_desc_len = (24 + desc_len);
4116 
4117 		off += desc_len;
4118 		add_len += add_desc_len;
4119 	}
4120 	spin_unlock(&pr_tmpl->registration_lock);
4121 	/*
4122 	 * Set ADDITIONAL_LENGTH
4123 	 */
4124 	put_unaligned_be32(add_len, &buf[4]);
4125 	target_set_cmd_data_length(cmd, 8 + add_len);
4126 
4127 	transport_kunmap_data_sg(cmd);
4128 
4129 	return 0;
4130 }
4131 
4132 static sense_reason_t target_try_pr_in_pt(struct se_cmd *cmd, u8 sa)
4133 {
4134 	struct exec_cmd_ops *ops = cmd->protocol_data;
4135 	unsigned char *buf;
4136 	sense_reason_t ret;
4137 
4138 	if (cmd->data_length < 8) {
4139 		pr_err("PRIN SA SCSI Data Length: %u too small\n",
4140 		       cmd->data_length);
4141 		return TCM_INVALID_CDB_FIELD;
4142 	}
4143 
4144 	if (!ops->execute_pr_in) {
4145 		pr_err("SPC-3 PR: Device has been configured for PR passthrough but it's not supported by the backend.\n");
4146 		return TCM_UNSUPPORTED_SCSI_OPCODE;
4147 	}
4148 
4149 	if (sa == PRI_READ_FULL_STATUS) {
4150 		pr_err("SPC-3 PR: PRI_READ_FULL_STATUS is not supported by PR passthrough.\n");
4151 		return TCM_UNSUPPORTED_SCSI_OPCODE;
4152 	}
4153 
4154 	buf = transport_kmap_data_sg(cmd);
4155 	if (!buf)
4156 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4157 
4158 	ret = ops->execute_pr_in(cmd, sa, buf);
4159 
4160 	transport_kunmap_data_sg(cmd);
4161 	return ret;
4162 }
4163 
4164 sense_reason_t
4165 target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4166 {
4167 	u8 sa = cmd->t_task_cdb[1] & 0x1f;
4168 	sense_reason_t ret;
4169 
4170 	/*
4171 	 * Following spc2r20 5.5.1 Reservations overview:
4172 	 *
4173 	 * If a logical unit has been reserved by any RESERVE command and is
4174 	 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4175 	 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4176 	 * initiator or service action and shall terminate with a RESERVATION
4177 	 * CONFLICT status.
4178 	 */
4179 	if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4180 		pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4181 			" SPC-2 reservation is held, returning"
4182 			" RESERVATION_CONFLICT\n");
4183 		return TCM_RESERVATION_CONFLICT;
4184 	}
4185 
4186 	if (cmd->se_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) {
4187 		ret = target_try_pr_in_pt(cmd, sa);
4188 		goto done;
4189 	}
4190 
4191 	switch (sa) {
4192 	case PRI_READ_KEYS:
4193 		ret = core_scsi3_pri_read_keys(cmd);
4194 		break;
4195 	case PRI_READ_RESERVATION:
4196 		ret = core_scsi3_pri_read_reservation(cmd);
4197 		break;
4198 	case PRI_REPORT_CAPABILITIES:
4199 		ret = core_scsi3_pri_report_capabilities(cmd);
4200 		break;
4201 	case PRI_READ_FULL_STATUS:
4202 		ret = core_scsi3_pri_read_full_status(cmd);
4203 		break;
4204 	default:
4205 		pr_err("Unknown PERSISTENT_RESERVE_IN service"
4206 			" action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4207 		return TCM_INVALID_CDB_FIELD;
4208 	}
4209 
4210 done:
4211 	if (!ret)
4212 		target_complete_cmd(cmd, SAM_STAT_GOOD);
4213 	return ret;
4214 }
4215 
4216 sense_reason_t
4217 target_check_reservation(struct se_cmd *cmd)
4218 {
4219 	struct se_device *dev = cmd->se_dev;
4220 	sense_reason_t ret;
4221 
4222 	if (!cmd->se_sess)
4223 		return 0;
4224 	if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4225 		return 0;
4226 	if (!dev->dev_attrib.emulate_pr)
4227 		return 0;
4228 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
4229 		return 0;
4230 
4231 	spin_lock(&dev->dev_reservation_lock);
4232 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4233 		ret = target_scsi2_reservation_check(cmd);
4234 	else
4235 		ret = target_scsi3_pr_reservation_check(cmd);
4236 	spin_unlock(&dev->dev_reservation_lock);
4237 
4238 	return ret;
4239 }
4240