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