forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_core_pr.c
4059 lines (3686 loc) · 122 KB
/
target_core_pr.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Filename: target_core_pr.c
*
* This file contains SPC-3 compliant persistent reservations and
* legacy SPC-2 reservations with compatible reservation handling (CRH=1)
*
* (c) Copyright 2009-2013 Datera, Inc.
*
* Nicholas A. Bellinger <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
******************************************************************************/
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/file.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <asm/unaligned.h>
#include <target/target_core_base.h>
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
#include <target/target_core_configfs.h>
#include "target_core_internal.h"
#include "target_core_pr.h"
#include "target_core_ua.h"
/*
* Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
*/
struct pr_transport_id_holder {
int dest_local_nexus;
struct t10_pr_registration *dest_pr_reg;
struct se_portal_group *dest_tpg;
struct se_node_acl *dest_node_acl;
struct se_dev_entry *dest_se_deve;
struct list_head dest_list;
};
void core_pr_dump_initiator_port(
struct t10_pr_registration *pr_reg,
char *buf,
u32 size)
{
if (!pr_reg->isid_present_at_reg)
buf[0] = '\0';
snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
}
enum register_type {
REGISTER,
REGISTER_AND_IGNORE_EXISTING_KEY,
REGISTER_AND_MOVE,
};
enum preempt_type {
PREEMPT,
PREEMPT_AND_ABORT,
};
static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
struct t10_pr_registration *, int);
static sense_reason_t
target_scsi2_reservation_check(struct se_cmd *cmd)
{
struct se_device *dev = cmd->se_dev;
struct se_session *sess = cmd->se_sess;
switch (cmd->t_task_cdb[0]) {
case INQUIRY:
case RELEASE:
case RELEASE_10:
return 0;
default:
break;
}
if (!dev->dev_reserved_node_acl || !sess)
return 0;
if (dev->dev_reserved_node_acl != sess->se_node_acl)
return TCM_RESERVATION_CONFLICT;
if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
if (dev->dev_res_bin_isid != sess->sess_bin_isid)
return TCM_RESERVATION_CONFLICT;
}
return 0;
}
static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
struct se_node_acl *, struct se_session *);
static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
{
struct se_session *se_sess = cmd->se_sess;
struct se_device *dev = cmd->se_dev;
struct t10_pr_registration *pr_reg;
struct t10_reservation *pr_tmpl = &dev->t10_pr;
int conflict = 0;
pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
se_sess);
if (pr_reg) {
/*
* From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
* behavior
*
* A RESERVE(6) or RESERVE(10) command shall complete with GOOD
* status, but no reservation shall be established and the
* persistent reservation shall not be changed, if the command
* is received from a) and b) below.
*
* A RELEASE(6) or RELEASE(10) command shall complete with GOOD
* status, but the persistent reservation shall not be released,
* if the command is received from a) and b)
*
* a) An I_T nexus that is a persistent reservation holder; or
* b) An I_T nexus that is registered if a registrants only or
* all registrants type persistent reservation is present.
*
* In all other cases, a RESERVE(6) command, RESERVE(10) command,
* RELEASE(6) command, or RELEASE(10) command shall be processed
* as defined in SPC-2.
*/
if (pr_reg->pr_res_holder) {
core_scsi3_put_pr_reg(pr_reg);
return 1;
}
if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
(pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
(pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
(pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
core_scsi3_put_pr_reg(pr_reg);
return 1;
}
core_scsi3_put_pr_reg(pr_reg);
conflict = 1;
} else {
/*
* Following spc2r20 5.5.1 Reservations overview:
*
* If a logical unit has executed a PERSISTENT RESERVE OUT
* command with the REGISTER or the REGISTER AND IGNORE
* EXISTING KEY service action and is still registered by any
* initiator, all RESERVE commands and all RELEASE commands
* regardless of initiator shall conflict and shall terminate
* with a RESERVATION CONFLICT status.
*/
spin_lock(&pr_tmpl->registration_lock);
conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
spin_unlock(&pr_tmpl->registration_lock);
}
if (conflict) {
pr_err("Received legacy SPC-2 RESERVE/RELEASE"
" while active SPC-3 registrations exist,"
" returning RESERVATION_CONFLICT\n");
return -EBUSY;
}
return 0;
}
sense_reason_t
target_scsi2_reservation_release(struct se_cmd *cmd)
{
struct se_device *dev = cmd->se_dev;
struct se_session *sess = cmd->se_sess;
struct se_portal_group *tpg;
int rc;
if (!sess || !sess->se_tpg)
goto out;
rc = target_check_scsi2_reservation_conflict(cmd);
if (rc == 1)
goto out;
if (rc < 0)
return TCM_RESERVATION_CONFLICT;
spin_lock(&dev->dev_reservation_lock);
if (!dev->dev_reserved_node_acl || !sess)
goto out_unlock;
if (dev->dev_reserved_node_acl != sess->se_node_acl)
goto out_unlock;
if (dev->dev_res_bin_isid != sess->sess_bin_isid)
goto out_unlock;
dev->dev_reserved_node_acl = NULL;
dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
dev->dev_res_bin_isid = 0;
dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
}
tpg = sess->se_tpg;
pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
" MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
sess->se_node_acl->initiatorname);
out_unlock:
spin_unlock(&dev->dev_reservation_lock);
out:
target_complete_cmd(cmd, GOOD);
return 0;
}
sense_reason_t
target_scsi2_reservation_reserve(struct se_cmd *cmd)
{
struct se_device *dev = cmd->se_dev;
struct se_session *sess = cmd->se_sess;
struct se_portal_group *tpg;
sense_reason_t ret = 0;
int rc;
if ((cmd->t_task_cdb[1] & 0x01) &&
(cmd->t_task_cdb[1] & 0x02)) {
pr_err("LongIO and Obselete Bits set, returning"
" ILLEGAL_REQUEST\n");
return TCM_UNSUPPORTED_SCSI_OPCODE;
}
/*
* This is currently the case for target_core_mod passthrough struct se_cmd
* ops
*/
if (!sess || !sess->se_tpg)
goto out;
rc = target_check_scsi2_reservation_conflict(cmd);
if (rc == 1)
goto out;
if (rc < 0)
return TCM_RESERVATION_CONFLICT;
tpg = sess->se_tpg;
spin_lock(&dev->dev_reservation_lock);
if (dev->dev_reserved_node_acl &&
(dev->dev_reserved_node_acl != sess->se_node_acl)) {
pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
tpg->se_tpg_tfo->get_fabric_name());
pr_err("Original reserver LUN: %u %s\n",
cmd->se_lun->unpacked_lun,
dev->dev_reserved_node_acl->initiatorname);
pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
" from %s \n", cmd->se_lun->unpacked_lun,
cmd->se_deve->mapped_lun,
sess->se_node_acl->initiatorname);
ret = TCM_RESERVATION_CONFLICT;
goto out_unlock;
}
dev->dev_reserved_node_acl = sess->se_node_acl;
dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
if (sess->sess_bin_isid != 0) {
dev->dev_res_bin_isid = sess->sess_bin_isid;
dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
}
pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
" for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
sess->se_node_acl->initiatorname);
out_unlock:
spin_unlock(&dev->dev_reservation_lock);
out:
if (!ret)
target_complete_cmd(cmd, GOOD);
return ret;
}
/*
* Begin SPC-3/SPC-4 Persistent Reservations emulation support
*
* This function is called by those initiator ports who are *NOT*
* the active PR reservation holder when a reservation is present.
*/
static int core_scsi3_pr_seq_non_holder(
struct se_cmd *cmd,
u32 pr_reg_type)
{
unsigned char *cdb = cmd->t_task_cdb;
struct se_dev_entry *se_deve;
struct se_session *se_sess = cmd->se_sess;
int other_cdb = 0, ignore_reg;
int registered_nexus = 0, ret = 1; /* Conflict by default */
int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
int we = 0; /* Write Exclusive */
int legacy = 0; /* Act like a legacy device and return
* RESERVATION CONFLICT on some CDBs */
se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
/*
* Determine if the registration should be ignored due to
* non-matching ISIDs in target_scsi3_pr_reservation_check().
*/
ignore_reg = (pr_reg_type & 0x80000000);
if (ignore_reg)
pr_reg_type &= ~0x80000000;
switch (pr_reg_type) {
case PR_TYPE_WRITE_EXCLUSIVE:
we = 1;
case PR_TYPE_EXCLUSIVE_ACCESS:
/*
* Some commands are only allowed for the persistent reservation
* holder.
*/
if ((se_deve->def_pr_registered) && !(ignore_reg))
registered_nexus = 1;
break;
case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
we = 1;
case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
/*
* Some commands are only allowed for registered I_T Nexuses.
*/
reg_only = 1;
if ((se_deve->def_pr_registered) && !(ignore_reg))
registered_nexus = 1;
break;
case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
we = 1;
case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
/*
* Each registered I_T Nexus is a reservation holder.
*/
all_reg = 1;
if ((se_deve->def_pr_registered) && !(ignore_reg))
registered_nexus = 1;
break;
default:
return -EINVAL;
}
/*
* Referenced from spc4r17 table 45 for *NON* PR holder access
*/
switch (cdb[0]) {
case SECURITY_PROTOCOL_IN:
if (registered_nexus)
return 0;
ret = (we) ? 0 : 1;
break;
case MODE_SENSE:
case MODE_SENSE_10:
case READ_ATTRIBUTE:
case READ_BUFFER:
case RECEIVE_DIAGNOSTIC:
if (legacy) {
ret = 1;
break;
}
if (registered_nexus) {
ret = 0;
break;
}
ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
break;
case PERSISTENT_RESERVE_OUT:
/*
* This follows PERSISTENT_RESERVE_OUT service actions that
* are allowed in the presence of various reservations.
* See spc4r17, table 46
*/
switch (cdb[1] & 0x1f) {
case PRO_CLEAR:
case PRO_PREEMPT:
case PRO_PREEMPT_AND_ABORT:
ret = (registered_nexus) ? 0 : 1;
break;
case PRO_REGISTER:
case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
ret = 0;
break;
case PRO_REGISTER_AND_MOVE:
case PRO_RESERVE:
ret = 1;
break;
case PRO_RELEASE:
ret = (registered_nexus) ? 0 : 1;
break;
default:
pr_err("Unknown PERSISTENT_RESERVE_OUT service"
" action: 0x%02x\n", cdb[1] & 0x1f);
return -EINVAL;
}
break;
case RELEASE:
case RELEASE_10:
/* Handled by CRH=1 in target_scsi2_reservation_release() */
ret = 0;
break;
case RESERVE:
case RESERVE_10:
/* Handled by CRH=1 in target_scsi2_reservation_reserve() */
ret = 0;
break;
case TEST_UNIT_READY:
ret = (legacy) ? 1 : 0; /* Conflict for legacy */
break;
case MAINTENANCE_IN:
switch (cdb[1] & 0x1f) {
case MI_MANAGEMENT_PROTOCOL_IN:
if (registered_nexus) {
ret = 0;
break;
}
ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
break;
case MI_REPORT_SUPPORTED_OPERATION_CODES:
case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
if (legacy) {
ret = 1;
break;
}
if (registered_nexus) {
ret = 0;
break;
}
ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
break;
case MI_REPORT_ALIASES:
case MI_REPORT_IDENTIFYING_INFORMATION:
case MI_REPORT_PRIORITY:
case MI_REPORT_TARGET_PGS:
case MI_REPORT_TIMESTAMP:
ret = 0; /* Allowed */
break;
default:
pr_err("Unknown MI Service Action: 0x%02x\n",
(cdb[1] & 0x1f));
return -EINVAL;
}
break;
case ACCESS_CONTROL_IN:
case ACCESS_CONTROL_OUT:
case INQUIRY:
case LOG_SENSE:
case READ_MEDIA_SERIAL_NUMBER:
case REPORT_LUNS:
case REQUEST_SENSE:
case PERSISTENT_RESERVE_IN:
ret = 0; /*/ Allowed CDBs */
break;
default:
other_cdb = 1;
break;
}
/*
* Case where the CDB is explicitly allowed in the above switch
* statement.
*/
if (!ret && !other_cdb) {
pr_debug("Allowing explicit CDB: 0x%02x for %s"
" reservation holder\n", cdb[0],
core_scsi3_pr_dump_type(pr_reg_type));
return ret;
}
/*
* Check if write exclusive initiator ports *NOT* holding the
* WRITE_EXCLUSIVE_* reservation.
*/
if (we && !registered_nexus) {
if (cmd->data_direction == DMA_TO_DEVICE) {
/*
* Conflict for write exclusive
*/
pr_debug("%s Conflict for unregistered nexus"
" %s CDB: 0x%02x to %s reservation\n",
transport_dump_cmd_direction(cmd),
se_sess->se_node_acl->initiatorname, cdb[0],
core_scsi3_pr_dump_type(pr_reg_type));
return 1;
} else {
/*
* Allow non WRITE CDBs for all Write Exclusive
* PR TYPEs to pass for registered and
* non-registered_nexuxes NOT holding the reservation.
*
* We only make noise for the unregisterd nexuses,
* as we expect registered non-reservation holding
* nexuses to issue CDBs.
*/
if (!registered_nexus) {
pr_debug("Allowing implicit CDB: 0x%02x"
" for %s reservation on unregistered"
" nexus\n", cdb[0],
core_scsi3_pr_dump_type(pr_reg_type));
}
return 0;
}
} else if ((reg_only) || (all_reg)) {
if (registered_nexus) {
/*
* For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
* allow commands from registered nexuses.
*/
pr_debug("Allowing implicit CDB: 0x%02x for %s"
" reservation\n", cdb[0],
core_scsi3_pr_dump_type(pr_reg_type));
return 0;
}
}
pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
" for %s reservation\n", transport_dump_cmd_direction(cmd),
(registered_nexus) ? "" : "un",
se_sess->se_node_acl->initiatorname, cdb[0],
core_scsi3_pr_dump_type(pr_reg_type));
return 1; /* Conflict by default */
}
static sense_reason_t
target_scsi3_pr_reservation_check(struct se_cmd *cmd)
{
struct se_device *dev = cmd->se_dev;
struct se_session *sess = cmd->se_sess;
u32 pr_reg_type;
if (!dev->dev_pr_res_holder)
return 0;
pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
goto check_nonholder;
if (dev->dev_pr_res_holder->isid_present_at_reg) {
if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
sess->sess_bin_isid) {
pr_reg_type |= 0x80000000;
goto check_nonholder;
}
}
return 0;
check_nonholder:
if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type))
return TCM_RESERVATION_CONFLICT;
return 0;
}
static u32 core_scsi3_pr_generation(struct se_device *dev)
{
u32 prg;
/*
* PRGeneration field shall contain the value of a 32-bit wrapping
* counter mainted by the device server.
*
* Note that this is done regardless of Active Persist across
* Target PowerLoss (APTPL)
*
* See spc4r17 section 6.3.12 READ_KEYS service action
*/
spin_lock(&dev->dev_reservation_lock);
prg = dev->t10_pr.pr_generation++;
spin_unlock(&dev->dev_reservation_lock);
return prg;
}
static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
struct se_device *dev,
struct se_node_acl *nacl,
struct se_dev_entry *deve,
unsigned char *isid,
u64 sa_res_key,
int all_tg_pt,
int aptpl)
{
struct t10_pr_registration *pr_reg;
pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
if (!pr_reg) {
pr_err("Unable to allocate struct t10_pr_registration\n");
return NULL;
}
INIT_LIST_HEAD(&pr_reg->pr_reg_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
atomic_set(&pr_reg->pr_res_holders, 0);
pr_reg->pr_reg_nacl = nacl;
pr_reg->pr_reg_deve = deve;
pr_reg->pr_res_mapped_lun = deve->mapped_lun;
pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
pr_reg->pr_res_key = sa_res_key;
pr_reg->pr_reg_all_tg_pt = all_tg_pt;
pr_reg->pr_reg_aptpl = aptpl;
pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
/*
* If an ISID value for this SCSI Initiator Port exists,
* save it to the registration now.
*/
if (isid != NULL) {
pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
pr_reg->isid_present_at_reg = 1;
}
return pr_reg;
}
static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
/*
* Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
* modes.
*/
static struct t10_pr_registration *__core_scsi3_alloc_registration(
struct se_device *dev,
struct se_node_acl *nacl,
struct se_dev_entry *deve,
unsigned char *isid,
u64 sa_res_key,
int all_tg_pt,
int aptpl)
{
struct se_dev_entry *deve_tmp;
struct se_node_acl *nacl_tmp;
struct se_port *port, *port_tmp;
struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
int ret;
/*
* Create a registration for the I_T Nexus upon which the
* PROUT REGISTER was received.
*/
pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
sa_res_key, all_tg_pt, aptpl);
if (!pr_reg)
return NULL;
/*
* Return pointer to pr_reg for ALL_TG_PT=0
*/
if (!all_tg_pt)
return pr_reg;
/*
* Create list of matching SCSI Initiator Port registrations
* for ALL_TG_PT=1
*/
spin_lock(&dev->se_port_lock);
list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
atomic_inc(&port->sep_tg_pt_ref_cnt);
smp_mb__after_atomic_inc();
spin_unlock(&dev->se_port_lock);
spin_lock_bh(&port->sep_alua_lock);
list_for_each_entry(deve_tmp, &port->sep_alua_list,
alua_port_list) {
/*
* This pointer will be NULL for demo mode MappedLUNs
* that have not been make explicit via a ConfigFS
* MappedLUN group for the SCSI Initiator Node ACL.
*/
if (!deve_tmp->se_lun_acl)
continue;
nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
/*
* Skip the matching struct se_node_acl that is allocated
* above..
*/
if (nacl == nacl_tmp)
continue;
/*
* Only perform PR registrations for target ports on
* the same fabric module as the REGISTER w/ ALL_TG_PT=1
* arrived.
*/
if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
continue;
/*
* Look for a matching Initiator Node ACL in ASCII format
*/
if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
continue;
atomic_inc(&deve_tmp->pr_ref_count);
smp_mb__after_atomic_inc();
spin_unlock_bh(&port->sep_alua_lock);
/*
* Grab a configfs group dependency that is released
* for the exception path at label out: below, or upon
* completion of adding ALL_TG_PT=1 registrations in
* __core_scsi3_add_registration()
*/
ret = core_scsi3_lunacl_depend_item(deve_tmp);
if (ret < 0) {
pr_err("core_scsi3_lunacl_depend"
"_item() failed\n");
atomic_dec(&port->sep_tg_pt_ref_cnt);
smp_mb__after_atomic_dec();
atomic_dec(&deve_tmp->pr_ref_count);
smp_mb__after_atomic_dec();
goto out;
}
/*
* Located a matching SCSI Initiator Port on a different
* port, allocate the pr_reg_atp and attach it to the
* pr_reg->pr_reg_atp_list that will be processed once
* the original *pr_reg is processed in
* __core_scsi3_add_registration()
*/
pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
nacl_tmp, deve_tmp, NULL,
sa_res_key, all_tg_pt, aptpl);
if (!pr_reg_atp) {
atomic_dec(&port->sep_tg_pt_ref_cnt);
smp_mb__after_atomic_dec();
atomic_dec(&deve_tmp->pr_ref_count);
smp_mb__after_atomic_dec();
core_scsi3_lunacl_undepend_item(deve_tmp);
goto out;
}
list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
&pr_reg->pr_reg_atp_list);
spin_lock_bh(&port->sep_alua_lock);
}
spin_unlock_bh(&port->sep_alua_lock);
spin_lock(&dev->se_port_lock);
atomic_dec(&port->sep_tg_pt_ref_cnt);
smp_mb__after_atomic_dec();
}
spin_unlock(&dev->se_port_lock);
return pr_reg;
out:
list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
&pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
}
kmem_cache_free(t10_pr_reg_cache, pr_reg);
return NULL;
}
int core_scsi3_alloc_aptpl_registration(
struct t10_reservation *pr_tmpl,
u64 sa_res_key,
unsigned char *i_port,
unsigned char *isid,
u32 mapped_lun,
unsigned char *t_port,
u16 tpgt,
u32 target_lun,
int res_holder,
int all_tg_pt,
u8 type)
{
struct t10_pr_registration *pr_reg;
if (!i_port || !t_port || !sa_res_key) {
pr_err("Illegal parameters for APTPL registration\n");
return -EINVAL;
}
pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
if (!pr_reg) {
pr_err("Unable to allocate struct t10_pr_registration\n");
return -ENOMEM;
}
INIT_LIST_HEAD(&pr_reg->pr_reg_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
atomic_set(&pr_reg->pr_res_holders, 0);
pr_reg->pr_reg_nacl = NULL;
pr_reg->pr_reg_deve = NULL;
pr_reg->pr_res_mapped_lun = mapped_lun;
pr_reg->pr_aptpl_target_lun = target_lun;
pr_reg->pr_res_key = sa_res_key;
pr_reg->pr_reg_all_tg_pt = all_tg_pt;
pr_reg->pr_reg_aptpl = 1;
pr_reg->pr_reg_tg_pt_lun = NULL;
pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
pr_reg->pr_res_type = type;
/*
* If an ISID value had been saved in APTPL metadata for this
* SCSI Initiator Port, restore it now.
*/
if (isid != NULL) {
pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
pr_reg->isid_present_at_reg = 1;
}
/*
* Copy the i_port and t_port information from caller.
*/
snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
pr_reg->pr_reg_tpgt = tpgt;
/*
* Set pr_res_holder from caller, the pr_reg who is the reservation
* holder will get it's pointer set in core_scsi3_aptpl_reserve() once
* the Initiator Node LUN ACL from the fabric module is created for
* this registration.
*/
pr_reg->pr_res_holder = res_holder;
list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
" metadata\n", (res_holder) ? "+reservation" : "");
return 0;
}
static void core_scsi3_aptpl_reserve(
struct se_device *dev,
struct se_portal_group *tpg,
struct se_node_acl *node_acl,
struct t10_pr_registration *pr_reg)
{
char i_buf[PR_REG_ISID_ID_LEN];
memset(i_buf, 0, PR_REG_ISID_ID_LEN);
core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
spin_lock(&dev->dev_reservation_lock);
dev->dev_pr_res_holder = pr_reg;
spin_unlock(&dev->dev_reservation_lock);
pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
" new reservation holder TYPE: %s ALL_TG_PT: %d\n",
tpg->se_tpg_tfo->get_fabric_name(),
core_scsi3_pr_dump_type(pr_reg->pr_res_type),
(pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
i_buf);
}
static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
struct t10_pr_registration *, enum register_type, int);
static int __core_scsi3_check_aptpl_registration(
struct se_device *dev,
struct se_portal_group *tpg,
struct se_lun *lun,
u32 target_lun,
struct se_node_acl *nacl,
struct se_dev_entry *deve)
{
struct t10_pr_registration *pr_reg, *pr_reg_tmp;
struct t10_reservation *pr_tmpl = &dev->t10_pr;
unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
u16 tpgt;
memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
/*
* Copy Initiator Port information from struct se_node_acl
*/
snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
tpg->se_tpg_tfo->tpg_get_wwn(tpg));
tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
/*
* Look for the matching registrations+reservation from those
* created from APTPL metadata. Note that multiple registrations
* may exist for fabrics that use ISIDs in their SCSI Initiator Port
* TransportIDs.
*/
spin_lock(&pr_tmpl->aptpl_reg_lock);
list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
pr_reg_aptpl_list) {
if (!strcmp(pr_reg->pr_iport, i_port) &&
(pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
!(strcmp(pr_reg->pr_tport, t_port)) &&
(pr_reg->pr_reg_tpgt == tpgt) &&
(pr_reg->pr_aptpl_target_lun == target_lun)) {
pr_reg->pr_reg_nacl = nacl;
pr_reg->pr_reg_deve = deve;
pr_reg->pr_reg_tg_pt_lun = lun;
list_del(&pr_reg->pr_reg_aptpl_list);
spin_unlock(&pr_tmpl->aptpl_reg_lock);
/*
* At this point all of the pointers in *pr_reg will
* be setup, so go ahead and add the registration.
*/
__core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
/*
* If this registration is the reservation holder,
* make that happen now..
*/
if (pr_reg->pr_res_holder)
core_scsi3_aptpl_reserve(dev, tpg,
nacl, pr_reg);
/*
* Reenable pr_aptpl_active to accept new metadata
* updates once the SCSI device is active again..
*/
spin_lock(&pr_tmpl->aptpl_reg_lock);
pr_tmpl->pr_aptpl_active = 1;
}
}
spin_unlock(&pr_tmpl->aptpl_reg_lock);
return 0;
}
int core_scsi3_check_aptpl_registration(
struct se_device *dev,
struct se_portal_group *tpg,
struct se_lun *lun,
struct se_lun_acl *lun_acl)
{
struct se_node_acl *nacl = lun_acl->se_lun_nacl;
struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
return 0;
return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
lun->unpacked_lun, nacl, deve);
}
static void __core_scsi3_dump_registration(
struct target_core_fabric_ops *tfo,
struct se_device *dev,
struct se_node_acl *nacl,
struct t10_pr_registration *pr_reg,
enum register_type register_type)
{
struct se_portal_group *se_tpg = nacl->se_tpg;
char i_buf[PR_REG_ISID_ID_LEN];
memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
" Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
"_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
"_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
i_buf);
pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
tfo->tpg_get_tag(se_tpg));
pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
" Port(s)\n", tfo->get_fabric_name(),
(pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
dev->transport->name);
pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
" 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
pr_reg->pr_res_key, pr_reg->pr_res_generation,
pr_reg->pr_reg_aptpl);
}
/*
* this function can be called with struct se_device->dev_reservation_lock
* when register_move = 1
*/
static void __core_scsi3_add_registration(
struct se_device *dev,
struct se_node_acl *nacl,
struct t10_pr_registration *pr_reg,
enum register_type register_type,
int register_move)
{