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