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