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