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