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