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