forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcmur_cmd_handler.c
2394 lines (2007 loc) · 64.5 KB
/
tcmur_cmd_handler.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
/*
* Copyright (c) 2017 Red Hat, Inc.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 2.1 or any later version (LGPLv2.1 or
* later), or the Apache License 2.0.
*/
#define _GNU_SOURCE
#include <scsi/scsi.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ccan/list/list.h"
#include "darray.h"
#include "libtcmu.h"
#include "libtcmu_log.h"
#include "libtcmu_priv.h"
#include "libtcmu_common.h"
#include "tcmur_aio.h"
#include "tcmur_device.h"
#include "tcmu-runner.h"
#include "tcmu_runner_priv.h"
#include "tcmur_cmd_handler.h"
#include "alua.h"
static void _cleanup_spin_lock(void *arg)
{
pthread_spin_unlock(arg);
}
void tcmur_tcmulib_cmd_complete(struct tcmu_device *dev,
struct tcmulib_cmd *cmd, int rc)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
struct timespec curr_time;
pthread_cleanup_push(_cleanup_spin_lock, (void *)&rdev->lock);
pthread_spin_lock(&rdev->lock);
if (tcmur_cmd->timed_out) {
if (tcmur_get_time(dev, &curr_time)) {
tcmu_dev_info(dev, "Timed out command id %hu completed with status %d.\n",
cmd->cmd_id, rc);
} else {
tcmu_dev_info(dev, "Timed out command id %hu completed after %f seconds with status %d.\n",
cmd->cmd_id,
difftime(curr_time.tv_sec,
tcmur_cmd->start_time.tv_sec),
rc);
}
}
list_del(&tcmur_cmd->cmds_list_entry);
tcmulib_command_complete(dev, cmd, rc);
pthread_spin_unlock(&rdev->lock);
pthread_cleanup_pop(0);
}
static void aio_command_finish(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
int rc)
{
struct tcmur_device *rdev = tcmu_dev_get_private(dev);
int wake_up;
tcmur_tcmulib_cmd_complete(dev, cmd, rc);
track_aio_request_finish(rdev, &wake_up);
while (wake_up) {
tcmulib_processing_complete(dev);
track_aio_wakeup_finish(rdev, &wake_up);
}
}
void tcmur_cmd_complete(struct tcmu_device *dev, void *data, int rc)
{
struct tcmur_cmd *tcmur_cmd = data;
tcmur_cmd->done(dev, tcmur_cmd, rc);
}
static void tcmur_cmd_iovec_reset(struct tcmur_cmd *tcmur_cmd,
size_t data_length)
{
tcmur_cmd->iovec->iov_base = tcmur_cmd->iov_base_copy;
tcmur_cmd->iovec->iov_len = data_length;
}
static void tcmur_cmd_state_free(struct tcmur_cmd *tcmur_cmd)
{
free(tcmur_cmd->cmd_state);
}
static int tcmur_cmd_state_init(struct tcmur_cmd *tcmur_cmd, int state_length,
size_t data_length)
{
void *state;
int iov_length = 0;
if (data_length)
iov_length = data_length + sizeof(struct iovec);
state = calloc(1, state_length + iov_length);
if (!state)
return -ENOMEM;
tcmur_cmd->cmd_state = state;
tcmur_cmd->requested = data_length;
if (data_length) {
struct iovec *iov = state + state_length;
iov->iov_base = iov + 1;
iov->iov_len = data_length;
tcmur_cmd->iov_base_copy = iov->iov_base;
tcmur_cmd->iov_cnt = 1;
tcmur_cmd->iovec = iov;
}
return 0;
}
static inline int check_iovec_length(struct tcmu_device *dev,
struct tcmulib_cmd *cmd, uint32_t sectors)
{
size_t iov_length = tcmu_iovec_length(cmd->iovec, cmd->iov_cnt);
if (iov_length != tcmu_lba_to_byte(dev, sectors)) {
tcmu_dev_err(dev, "iov len mismatch: iov len %zu, xfer len %u, block size %u\n",
iov_length, sectors, tcmu_dev_get_block_size(dev));
return TCMU_STS_HW_ERR;
}
return TCMU_STS_OK;
}
static inline int check_lbas(struct tcmu_device *dev,
uint64_t start_lba, uint64_t lba_cnt)
{
uint64_t dev_last_lba = tcmu_dev_get_num_lbas(dev);
if (start_lba + lba_cnt > dev_last_lba || start_lba + lba_cnt < start_lba) {
tcmu_dev_err(dev, "cmd exceeds last lba %"PRIu64" (lba %"PRIu64", xfer len %"PRIu64")\n",
dev_last_lba, start_lba, lba_cnt);
return TCMU_STS_RANGE;
}
return TCMU_STS_OK;
}
static int check_lba_and_length(struct tcmu_device *dev,
struct tcmulib_cmd *cmd, uint32_t sectors)
{
uint8_t *cdb = cmd->cdb;
uint64_t start_lba = tcmu_cdb_get_lba(cdb);
int ret;
ret = check_iovec_length(dev, cmd, sectors);
if (ret)
return ret;
ret = check_lbas(dev, start_lba, sectors);
if (ret)
return ret;
return TCMU_STS_OK;
}
static void handle_generic_cbk(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd, int ret)
{
aio_command_finish(dev, tcmur_cmd->lib_cmd, ret);
}
static int read_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_cmd = data;
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
return rhandler->read(dev, tcmur_cmd, cmd->iovec, cmd->iov_cnt,
tcmu_iovec_length(cmd->iovec, cmd->iov_cnt),
tcmu_cdb_to_byte(dev, cmd->cdb));
}
static int write_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_cmd = data;
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
return rhandler->write(dev, tcmur_cmd, cmd->iovec, cmd->iov_cnt,
tcmu_iovec_length(cmd->iovec, cmd->iov_cnt),
tcmu_cdb_to_byte(dev, cmd->cdb));
}
struct unmap_state {
pthread_mutex_t lock;
unsigned int refcount;
int status;
};
struct unmap_descriptor {
uint64_t offset;
uint64_t length;
};
static int unmap_init(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
struct unmap_state *state;
int ret;
if (tcmur_cmd_state_init(tcmur_cmd, sizeof(*state), 0))
return TCMU_STS_NO_RESOURCE;
state = tcmur_cmd->cmd_state;
ret = pthread_mutex_init(&state->lock, NULL);
if (ret == -1) {
tcmu_dev_err(dev, "Failed to init spin lock in state!\n");
ret = TCMU_STS_HW_ERR;
goto out_free_state;
}
/* released by allocator when done submitting unmaps */
state->refcount = 1;
state->status = TCMU_STS_OK;
return TCMU_STS_OK;
out_free_state:
tcmur_cmd_state_free(tcmur_cmd);
return ret;
}
static void unmap_put(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
int ret)
{
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
struct unmap_state *state = tcmur_cmd->cmd_state;
int status;
pthread_mutex_lock(&state->lock);
if (state->status != TCMU_STS_OK && ret)
state->status = ret;
if (--state->refcount > 0) {
pthread_mutex_unlock(&state->lock);
return;
}
status = state->status;
pthread_mutex_unlock(&state->lock);
pthread_mutex_destroy(&state->lock);
tcmur_cmd_state_free(tcmur_cmd);
aio_command_finish(dev, cmd, status);
}
static void handle_unmap_cbk(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_ucmd, int ret)
{
struct unmap_descriptor *desc = tcmur_ucmd->cmd_state;
struct tcmulib_cmd *cmd = tcmur_ucmd->lib_cmd;
free(desc);
free(tcmur_ucmd);
unmap_put(dev, cmd, ret);
}
static int unmap_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_ucmd = data;
struct unmap_descriptor *desc = tcmur_ucmd->cmd_state;
uint64_t offset = desc->offset, length = desc->length;
return rhandler->unmap(dev, tcmur_ucmd, offset, length);
}
static int align_and_split_unmap(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
uint64_t lba, uint64_t nlbas)
{
struct unmap_state *state = tcmur_cmd->cmd_state;
uint64_t opt_unmap_gran;
uint64_t unmap_gran_align, mask;
int ret = TCMU_STS_NOT_HANDLED;
int j = 0;
struct unmap_descriptor *desc;
struct tcmur_cmd *tcmur_ucmd;
uint64_t lbas;
if (!dev->split_unmaps) {
/*
* Handler does not support vectored unmaps, but prefers to
* break up unmaps itself, so pass the entire segment to it.
*/
opt_unmap_gran = tcmu_dev_get_max_unmap_len(dev);
mask = 0;
} else {
/*
* Align the start lba of a unmap request and split the
* large num blocks into OPTIMAL UNMAP GRANULARITY size.
*
* NOTE: here we always asumme the OPTIMAL UNMAP GRANULARITY
* equals to UNMAP GRANULARITY ALIGNMENT to simplify the
* algorithm. In the future, for new devices that have different
* values the following align and split algorithm should be
* changed.
*/
/* OPTIMAL UNMAP GRANULARITY */
opt_unmap_gran = tcmu_dev_get_opt_unmap_gran(dev);
/* UNMAP GRANULARITY ALIGNMENT */
unmap_gran_align = tcmu_dev_get_unmap_gran_align(dev);
mask = unmap_gran_align - 1;
}
lbas = opt_unmap_gran - (lba & mask);
lbas = min(lbas, nlbas);
tcmu_dev_dbg(dev, "OPTIMAL UNMAP GRANULARITY: %"PRIu64", UNMAP GRANULARITY ALIGNMENT mask: %"PRIu64", lbas: %"PRIu64"\n",
opt_unmap_gran, mask, lbas);
while (nlbas) {
desc = calloc(1, sizeof(*desc));
if (!desc) {
tcmu_dev_err(dev, "Failed to calloc desc!\n");
return TCMU_STS_NO_RESOURCE;
}
desc->offset = tcmu_lba_to_byte(dev, lba);
desc->length = tcmu_lba_to_byte(dev, lbas);
tcmur_ucmd = calloc(1, sizeof(*tcmur_ucmd));
if (!tcmur_ucmd) {
tcmu_dev_err(dev, "Failed to calloc unmap cmd!\n");
ret = TCMU_STS_NO_RESOURCE;
goto free_desc;
}
tcmur_ucmd->cmd_state = desc;
tcmur_ucmd->lib_cmd = tcmur_cmd->lib_cmd;
tcmur_ucmd->done = handle_unmap_cbk;
/* The first one */
if (j++ == 0)
tcmu_dev_dbg(dev, "The first split: start lba: %"PRIu64", end lba: %"PRIu64", lbas: %"PRIu64"\n",
lba, lba + lbas - 1, lbas);
/* The last one */
if (nlbas == lbas) {
tcmu_dev_dbg(dev, "The last split: start lba: %"PRIu64", end lba: %"PRIu64", lbas: %"PRIu64"\n",
lba, lba + lbas - 1, lbas);
tcmu_dev_dbg(dev, "There are totally %d splits\n", j);
}
pthread_mutex_lock(&state->lock);
state->refcount++;
pthread_mutex_unlock(&state->lock);
ret = aio_request_schedule(dev, tcmur_ucmd, unmap_work_fn,
tcmur_cmd_complete);
if (ret != TCMU_STS_ASYNC_HANDLED)
goto free_ucmd;
nlbas -= lbas;
lba += lbas;
lbas = min(opt_unmap_gran, nlbas);
}
return ret;
free_ucmd:
pthread_mutex_lock(&state->lock);
state->refcount--;
pthread_mutex_unlock(&state->lock);
free(tcmur_ucmd);
free_desc:
free(desc);
return ret;
}
static int handle_unmap_internal(struct tcmu_device *dev, struct tcmulib_cmd *cmd,
uint16_t bddl, uint8_t *par)
{
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
struct unmap_state *state;
uint16_t offset = 0;
int ret = TCMU_STS_OK, i = 0;
ret = unmap_init(dev, cmd);
if (ret)
return ret;
state = tcmur_cmd->cmd_state;
/* The first descriptor list offset is 8 in Data-Out buffer */
par += 8;
while (bddl) {
uint64_t lba;
uint64_t nlbas;
lba = be64toh(*((uint64_t *)&par[offset]));
nlbas = be32toh(*((uint32_t *)&par[offset + 8]));
tcmu_dev_dbg(dev, "Parameter list %d, start lba: %"PRIu64", end lba: %"PRIu64", nlbas: %"PRIu64"\n",
i++, lba, lba + nlbas - 1, nlbas);
if (nlbas > tcmu_dev_get_max_unmap_len(dev)) {
tcmu_dev_err(dev, "Illegal parameter list LBA count %"PRIu64" exceeds:%u\n",
nlbas, tcmu_dev_get_max_unmap_len(dev));
ret = TCMU_STS_INVALID_PARAM_LIST;
goto done;
}
ret = check_lbas(dev, lba, nlbas);
if (ret)
goto done;
if (nlbas) {
ret = align_and_split_unmap(dev, tcmur_cmd, lba, nlbas);
if (ret != TCMU_STS_ASYNC_HANDLED)
goto done;
}
/* The unmap block descriptor data length is 16 */
offset += 16;
bddl -= 16;
}
done:
/*
* unmap_put will do the right thing, so always return
* TCMU_STS_ASYNC_HANDLED
*/
pthread_mutex_lock(&state->lock);
if (ret == TCMU_STS_ASYNC_HANDLED) {
ret = TCMU_STS_OK;
} else {
state->status = ret;
}
pthread_mutex_unlock(&state->lock);
unmap_put(dev, cmd, ret);
return TCMU_STS_ASYNC_HANDLED;
}
static int handle_unmap(struct tcmu_device *dev, struct tcmulib_cmd *origcmd)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
uint8_t *cdb = origcmd->cdb;
size_t copied, data_length = tcmu_cdb_get_xfer_length(cdb);
uint8_t *par;
uint16_t dl, bddl;
int ret;
if (!rhandler->unmap)
return TCMU_STS_INVALID_CMD;
/*
* ANCHOR bit check
*
* The ANCHOR in the Logical Block Provisioning VPD page is not
* supported, so the ANCHOR bit shouldn't be set here.
*/
if (cdb[1] & 0x01) {
tcmu_dev_err(dev, "Illegal request: anchor is not supported for now!\n");
return TCMU_STS_INVALID_CDB;
}
/*
* PARAMETER LIST LENGTH field.
*
* The PARAMETER LIST LENGTH field specifies the length in bytes of
* the UNMAP parameter data that shall be sent from the application
* client to the device server.
*
* A PARAMETER LIST LENGTH set to zero specifies that no data shall
* be sent.
*/
if (!data_length) {
tcmu_dev_dbg(dev, "Data-Out Buffer length is zero, just return okay\n");
return TCMU_STS_OK;
}
/*
* From sbc4r13, section 5.32.1 UNMAP command overview.
*
* The PARAMETER LIST LENGTH should be greater than eight,
*/
if (data_length < 8) {
tcmu_dev_err(dev, "Illegal parameter list length %zu and it should be >= 8\n",
data_length);
return TCMU_STS_INVALID_PARAM_LIST_LEN;
}
par = calloc(1, data_length);
if (!par) {
tcmu_dev_err(dev, "The state parameter is NULL!\n");
return TCMU_STS_NO_RESOURCE;
}
copied = tcmu_memcpy_from_iovec(par, data_length, origcmd->iovec,
origcmd->iov_cnt);
if (copied != data_length) {
tcmu_dev_err(dev, "Failed to copy the Data-Out Buffer !\n");
ret = TCMU_STS_INVALID_PARAM_LIST_LEN;
goto out_free_par;
}
/*
* If any UNMAP block descriptors in the UNMAP block descriptor
* list are truncated due to the parameter list length in the CDB,
* then that UNMAP block descriptor shall be ignored.
*
* So it will allow dl + 2 != data_length and bddl + 8 != data_length.
*/
dl = be16toh(*((uint16_t *)&par[0]));
bddl = be16toh(*((uint16_t *)&par[2]));
tcmu_dev_dbg(dev, "Data-Out Buffer Length: %zu, dl: %hu, bddl: %hu\n",
data_length, dl, bddl);
/*
* If the unmap block descriptor data length is not a multiple
* of 16, then the last unmap block descriptor is incomplete
* and shall be ignored.
*/
bddl &= ~0xF;
/*
* If the UNMAP BLOCK DESCRIPTOR DATA LENGTH is set to zero, then
* no unmap block descriptors are included in the UNMAP parameter
* list.
*/
if (!bddl) {
ret = TCMU_STS_OK;
goto out_free_par;
}
if (bddl / 16 > VPD_MAX_UNMAP_BLOCK_DESC_COUNT) {
tcmu_dev_err(dev, "Illegal parameter list count %hu exceeds :%u\n",
bddl / 16, VPD_MAX_UNMAP_BLOCK_DESC_COUNT);
ret = TCMU_STS_INVALID_PARAM_LIST;
goto out_free_par;
}
ret = handle_unmap_internal(dev, origcmd, bddl, par);
out_free_par:
free(par);
return ret;
}
struct write_same {
uint64_t cur_lba;
uint64_t lba_cnt;
};
static int writesame_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_cmd = data;
struct write_same *write_same = tcmur_cmd->cmd_state;
tcmur_cmd_iovec_reset(tcmur_cmd, tcmur_cmd->requested);
/*
* Write contents of the logical block data(from the Data-Out Buffer)
* to each LBA in the specified LBA range.
*/
return rhandler->write(dev, tcmur_cmd, tcmur_cmd->iovec,
tcmur_cmd->iov_cnt, tcmur_cmd->requested,
tcmu_lba_to_byte(dev, write_same->cur_lba));
}
static void handle_writesame_cbk(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd, int ret)
{
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
struct write_same *write_same = tcmur_cmd->cmd_state;
uint64_t write_lbas = tcmu_byte_to_lba(dev, tcmur_cmd->requested);
uint64_t left_lbas;
int rc;
/* write failed - bail out */
if (ret != TCMU_STS_OK)
goto finish_err;
write_same->cur_lba += write_lbas;
write_same->lba_cnt -= write_lbas;
left_lbas = write_same->lba_cnt;
if (!left_lbas)
goto finish_err;
if (left_lbas <= write_lbas) {
tcmu_dev_dbg(dev, "Last lba: %"PRIu64", write lbas: %"PRIu64"\n",
write_same->cur_lba, left_lbas);
tcmur_cmd->requested = tcmu_lba_to_byte(dev, left_lbas);
} else {
tcmu_dev_dbg(dev, "Next lba: %"PRIu64", write lbas: %"PRIu64"\n",
write_same->cur_lba, write_lbas);
}
rc = aio_request_schedule(dev, tcmur_cmd, writesame_work_fn,
tcmur_cmd_complete);
if (rc != TCMU_STS_ASYNC_HANDLED) {
tcmu_dev_err(dev, "Write same async handle cmd failure\n");
ret = TCMU_STS_WR_ERR;
goto finish_err;
}
return;
finish_err:
tcmur_cmd_state_free(tcmur_cmd);
aio_command_finish(dev, cmd, ret);
}
static int handle_writesame_check(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
uint8_t *cdb = cmd->cdb;
uint32_t lba_cnt = tcmu_cdb_get_xfer_length(cdb);
uint32_t block_size = tcmu_dev_get_block_size(dev);
uint64_t start_lba = tcmu_cdb_get_lba(cdb);
int ret;
if (cmd->iov_cnt != 1 || cmd->iovec->iov_len != block_size) {
tcmu_dev_err(dev, "Illegal Data-Out: iov_cnt %zu length: %zu\n",
cmd->iov_cnt, cmd->iovec->iov_len);
return TCMU_STS_INVALID_CDB;
}
/*
* From sbc4r13, section 5.50 WRITE SAME (16) command
*
* A write same (WSNZ) bit has beed set to one, so the device server
* won't support a value of zero here.
*/
if (!lba_cnt) {
tcmu_dev_err(dev, "The WSNZ = 1 & WRITE_SAME blocks = 0 is not supported!\n");
return TCMU_STS_INVALID_CDB;
}
/*
* The MAXIMUM WRITE SAME LENGTH field in Block Limits VPD page (B0h)
* limit the maximum block number for the WRITE SAME.
*/
if (lba_cnt > VPD_MAX_WRITE_SAME_LENGTH) {
tcmu_dev_err(dev, "blocks: %u exceeds MAXIMUM WRITE SAME LENGTH: %u\n",
lba_cnt, VPD_MAX_WRITE_SAME_LENGTH);
return TCMU_STS_INVALID_CDB;
}
/*
* The logical block address plus the number of blocks shouldn't
* exceeds the capacity of the medium
*/
ret = check_lbas(dev, start_lba, lba_cnt);
if (ret)
return ret;
tcmu_dev_dbg(dev, "Start lba: %"PRIu64", number of lba: %u, last lba: %"PRIu64"\n",
start_lba, lba_cnt, start_lba + lba_cnt - 1);
return TCMU_STS_OK;
}
static int handle_unmap_in_writesame(struct tcmu_device *dev,
struct tcmulib_cmd *cmd)
{
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
uint8_t *cdb = cmd->cdb;
uint64_t lba = tcmu_cdb_get_lba(cdb);
uint64_t nlbas = tcmu_cdb_get_xfer_length(cdb);
struct unmap_state *state;
int ret;
tcmu_dev_dbg(dev, "Do UNMAP in WRITE_SAME cmd!\n");
ret = unmap_init(dev, cmd);
if (ret)
return ret;
state = tcmur_cmd->cmd_state;
ret = align_and_split_unmap(dev, tcmur_cmd, lba, nlbas);
if (ret == TCMU_STS_ASYNC_HANDLED) {
ret = TCMU_STS_OK;
} else {
state->status = ret;
}
unmap_put(dev, cmd, ret);
return TCMU_STS_ASYNC_HANDLED;
}
static int handle_writesame(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
uint8_t *cdb = cmd->cdb;
uint32_t lba_cnt = tcmu_cdb_get_xfer_length(cdb);
uint32_t block_size = tcmu_dev_get_block_size(dev);
uint64_t start_lba = tcmu_cdb_get_lba(cdb);
uint64_t write_lbas;
size_t max_xfer_length, length = 1024 * 1024;
struct write_same *write_same;
int i, ret;
ret = handle_writesame_check(dev, cmd);
if (ret)
return ret;
if (rhandler->unmap && (cmd->cdb[1] & 0x08))
return handle_unmap_in_writesame(dev, cmd);
max_xfer_length = tcmu_dev_get_max_xfer_len(dev) * block_size;
length = round_up(length, max_xfer_length);
length = min(length, tcmu_lba_to_byte(dev, lba_cnt));
if (tcmur_cmd_state_init(tcmur_cmd, sizeof(*write_same), length)) {
tcmu_dev_err(dev, "Failed to calloc write_same data!\n");
return TCMU_STS_NO_RESOURCE;
}
tcmur_cmd->done = handle_writesame_cbk;
write_lbas = tcmu_byte_to_lba(dev, length);
for (i = 0; i < write_lbas; i++)
memcpy(tcmur_cmd->iovec->iov_base + i * block_size,
cmd->iovec->iov_base, block_size);
write_same = tcmur_cmd->cmd_state;
write_same->cur_lba = start_lba;
write_same->lba_cnt = lba_cnt;
tcmu_dev_dbg(dev, "First lba: %"PRIu64", write lbas: %"PRIu64"\n",
start_lba, write_lbas);
return aio_request_schedule(dev, tcmur_cmd, writesame_work_fn,
tcmur_cmd_complete);
}
static int tcmur_writesame_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_cmd *tcmur_cmd = data;
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
tcmur_writesame_fn_t write_same_fn = tcmur_cmd->cmd_state;
uint8_t *cdb = cmd->cdb;
uint64_t off = tcmu_cdb_to_byte(dev, cdb);
uint64_t len = tcmu_lba_to_byte(dev, tcmu_cdb_get_xfer_length(cdb));
/*
* Write contents of the logical block data(from the Data-Out Buffer)
* to each LBA in the specified LBA range.
*/
return write_same_fn(dev, tcmur_cmd, off, len, cmd->iovec,
cmd->iov_cnt);
}
int tcmur_handle_writesame(struct tcmu_device *dev, struct tcmur_cmd *tcmur_cmd,
tcmur_writesame_fn_t write_same_fn)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
int ret;
if (tcmu_dev_in_recovery(dev))
return TCMU_STS_BUSY;
ret = alua_check_state(dev, cmd);
if (ret)
return ret;
ret = handle_writesame_check(dev, cmd);
if (ret)
return ret;
if (rhandler->unmap && (cmd->cdb[1] & 0x08))
return handle_unmap_in_writesame(dev, cmd);
tcmur_cmd->cmd_state = write_same_fn;
tcmur_cmd->done = handle_generic_cbk;
return aio_request_schedule(dev, tcmur_cmd, tcmur_writesame_work_fn,
tcmur_cmd_complete);
}
/* async write verify */
struct write_verify_state {
struct iovec *w_iovec;
size_t w_iov_cnt;
};
static void handle_write_verify_read_cbk(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd, int ret)
{
struct write_verify_state *state = tcmur_cmd->cmd_state;
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
uint32_t cmp_offset;
/* failed read - bail out */
if (ret != TCMU_STS_OK)
goto done;
ret = TCMU_STS_OK;
cmp_offset = tcmu_iovec_compare(tcmur_cmd->iov_base_copy,
state->w_iovec, tcmur_cmd->requested);
if (cmp_offset != -1) {
tcmu_dev_err(dev, "Verify failed at offset %u\n", cmp_offset);
ret = TCMU_STS_MISCOMPARE;
tcmu_sense_set_info(cmd->sense_buf, cmp_offset);
}
done:
tcmur_cmd_state_free(tcmur_cmd);
aio_command_finish(dev, cmd, ret);
}
static int write_verify_read_work_fn(struct tcmu_device *dev, void *data)
{
struct tcmur_handler *rhandler = tcmu_get_runner_handler(dev);
struct tcmur_cmd *tcmur_cmd = data;
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
return rhandler->read(dev, tcmur_cmd, tcmur_cmd->iovec,
tcmur_cmd->iov_cnt, tcmur_cmd->requested,
tcmu_cdb_to_byte(dev, cmd->cdb));
}
static void handle_write_verify_write_cbk(struct tcmu_device *dev,
struct tcmur_cmd *tcmur_cmd,
int ret)
{
struct tcmulib_cmd *cmd = tcmur_cmd->lib_cmd;
/* write error - bail out */
if (ret != TCMU_STS_OK)
goto finish_err;
tcmur_cmd->done = handle_write_verify_read_cbk;
ret = aio_request_schedule(dev, tcmur_cmd, write_verify_read_work_fn,
tcmur_cmd_complete);
if (ret != TCMU_STS_ASYNC_HANDLED)
goto finish_err;
return;
finish_err:
tcmur_cmd_state_free(tcmur_cmd);
aio_command_finish(dev, cmd, ret);
}
static int handle_write_verify(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct tcmur_cmd *tcmur_cmd = cmd->hm_private;
uint8_t *cdb = cmd->cdb;
size_t length = tcmu_lba_to_byte(dev, tcmu_cdb_get_xfer_length(cdb));
struct write_verify_state *state;
int i, ret, state_len;
ret = check_lba_and_length(dev, cmd, tcmu_cdb_get_xfer_length(cdb));
if (ret)
return ret;
state_len = sizeof(*state) + (cmd->iov_cnt * sizeof(struct iovec));
if (tcmur_cmd_state_init(tcmur_cmd, state_len, length))
return TCMU_STS_NO_RESOURCE;
tcmur_cmd->done = handle_write_verify_write_cbk;
state = tcmur_cmd->cmd_state;
/*
* Copy cmd iovec for later comparision in case handler modifies
* pointers/lens.
*/
state->w_iovec = (void *)state + sizeof(*state);
state->w_iov_cnt = cmd->iov_cnt;
for (i = 0; i < cmd->iov_cnt; i++) {
state->w_iovec[i].iov_base = cmd->iovec[i].iov_base;
state->w_iovec[i].iov_len = cmd->iovec[i].iov_len;
}
ret = aio_request_schedule(dev, tcmur_cmd, write_work_fn,
tcmur_cmd_complete);
if (ret != TCMU_STS_ASYNC_HANDLED)
goto free_state;
return TCMU_STS_ASYNC_HANDLED;
free_state:
tcmur_cmd_state_free(tcmur_cmd);
return ret;
}
#define XCOPY_HDR_LEN 16
#define XCOPY_TARGET_DESC_LEN 32
#define XCOPY_SEGMENT_DESC_B2B_LEN 28
#define XCOPY_NAA_IEEE_REGEX_LEN 16
struct xcopy {
struct tcmu_device *origdev;
struct tcmu_device *src_dev;
uint8_t src_tid_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
struct tcmu_device *dst_dev;
uint8_t dst_tid_wwn[XCOPY_NAA_IEEE_REGEX_LEN];
uint64_t src_lba;
uint64_t dst_lba;
uint32_t stdi;
uint32_t dtdi;
uint32_t lba_cnt;
uint32_t copy_lbas;
};
/* For now only supports block -> block type */
static int xcopy_parse_segment_descs(uint8_t *seg_descs, struct xcopy *xcopy,
uint8_t sdll)
{
uint8_t *seg_desc = seg_descs;
uint8_t desc_len;
/*
* From spc4r31, section 6.3.7.5 Block device to block device
* operations
*
* The segment descriptor size should be 28 bytes
*/
if (sdll % XCOPY_SEGMENT_DESC_B2B_LEN != 0) {
tcmu_err("Illegal block --> block type segment descriptor length %u\n",
sdll);
return TCMU_STS_INVALID_PARAM_LIST;
}
/* From spc4r36q, section 6.4.3.5 SEGMENT DESCRIPTOR LIST LENGTH field
* If the number of segment descriptors exceeds the allowed number, the copy
* manager shall terminate the command with CHECK CONDITION status, with the
* sense key set to ILLEGAL REQUEST, and the additional sense code set to
* TOO MANY SEGMENT DESCRIPTORS.
*/
if (sdll > RCR_OP_MAX_SEGMENT_DESC_COUNT * XCOPY_SEGMENT_DESC_B2B_LEN) {
tcmu_err("Only %u segment descriptor(s) supported, but there are %u\n",
RCR_OP_MAX_SEGMENT_DESC_COUNT,
sdll / XCOPY_SEGMENT_DESC_B2B_LEN);
return TCMU_STS_TOO_MANY_SEG_DESC;
}
/* EXTENDED COPY segment descriptor type codes block --> block */
if (seg_desc[0] != XCOPY_SEG_DESC_TYPE_CODE_B2B) {
tcmu_err("Unsupport segment descriptor type code 0x%x\n",
seg_desc[0]);
return TCMU_STS_NOTSUPP_SEG_DESC_TYPE;
}
/*
* For block -> block type the length is 4-byte header + 0x18-byte
* data.
*/
desc_len = be16toh(*(uint16_t *)&seg_desc[2]);
if (desc_len != 0x18) {
tcmu_err("Invalid length for block->block type 0x%x\n",
desc_len);
return TCMU_STS_INVALID_PARAM_LIST;
}
/*
* From spc4r31, section 6.3.7.1 Segment descriptors introduction
*
* The SOURCE TARGET DESCRIPTOR INDEX field contains an index into
* the target descriptor list (see 6.3.1) identifying the source
* copy target device. The DESTINATION TARGET DESCRIPTOR INDEX field
* contains an index into the target descriptor list (see 6.3.1)
* identifying the destination copy target device.
*/
xcopy->stdi = be16toh(*(uint16_t *)&seg_desc[4]);
xcopy->dtdi = be16toh(*(uint16_t *)&seg_desc[6]);
tcmu_dbg("Segment descriptor: stdi: %hu dtdi: %hu\n", xcopy->stdi,
xcopy->dtdi);
xcopy->lba_cnt = be16toh(*(uint16_t *)&seg_desc[10]);
xcopy->src_lba = be64toh(*(uint64_t *)&seg_desc[12]);
xcopy->dst_lba = be64toh(*(uint64_t *)&seg_desc[20]);
tcmu_dbg("Segment descriptor: lba_cnt: %u src_lba: %"PRIu64" dst_lba: %"PRIu64"\n",
xcopy->lba_cnt, xcopy->src_lba, xcopy->dst_lba);
return TCMU_STS_OK;
}
static int xcopy_gen_naa_ieee(struct tcmu_device *udev, uint8_t *wwn)
{
char *buf, *p;
bool next = true;