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