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