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