forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.c
926 lines (771 loc) · 19.2 KB
/
api.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
/*
* Copyright 2014, Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <scsi/scsi.h>
#include <endian.h>
#include <errno.h>
#include "libtcmu_log.h"
#include "libtcmu_common.h"
#include "libtcmu_priv.h"
#define SECTOR_SIZE 512
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
int tcmu_get_attribute(struct tcmu_device *dev, const char *name)
{
int fd;
char path[256];
char buf[16];
ssize_t ret;
unsigned int val;
snprintf(path, sizeof(path), "/sys/kernel/config/target/core/%s/%s/attrib/%s",
dev->tcm_hba_name, dev->tcm_dev_name, name);
fd = open(path, O_RDONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to read attribute %s\n", name);
return -EINVAL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read attribute %s\n", name);
return -EINVAL;
}
val = strtoul(buf, NULL, 0);
if (val == ULONG_MAX) {
tcmu_err("could not convert string to value\n");
return -EINVAL;
}
return val;
}
/*
* Return a string that contains the device's WWN, or NULL.
*
* Callers must free the result with free().
*/
static char *tcmu_get_wwn(struct tcmu_device *dev)
{
int fd;
char path[256];
char buf[256];
char *ret_buf;
int ret;
snprintf(path, sizeof(path),
"/sys/kernel/config/target/core/%s/%s/wwn/vpd_unit_serial",
dev->tcm_hba_name, dev->tcm_dev_name);
fd = open(path, O_RDONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to read unit serial\n");
return NULL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read unit serial\n");
return NULL;
}
/* Kill the trailing '\n' */
buf[ret-1] = '\0';
/* Skip to the good stuff */
ret = asprintf(&ret_buf, "%s", &buf[28]);
if (ret == -1) {
tcmu_err("could not convert string to value\n");
return NULL;
}
return ret_buf;
}
long long tcmu_get_device_size(struct tcmu_device *dev)
{
int fd;
char path[256];
char buf[4096];
ssize_t ret;
char *rover;
unsigned long long size;
snprintf(path, sizeof(path), "/sys/kernel/config/target/core/%s/%s/info",
dev->tcm_hba_name, dev->tcm_dev_name);
fd = open(path, O_RDONLY);
if (fd == -1) {
tcmu_err("Could not open configfs to read dev info\n");
return -EINVAL;
}
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret == -1) {
tcmu_err("Could not read configfs to read dev info\n");
return -EINVAL;
}
buf[sizeof(buf)-1] = '\0'; /* paranoid? Ensure null terminated */
rover = strstr(buf, " Size: ");
if (!rover) {
tcmu_err("Could not find \" Size: \" in %s\n", path);
return -EINVAL;
}
rover += 7; /* get to the value */
size = strtoull(rover, NULL, 0);
if (size == ULLONG_MAX) {
tcmu_err("Could not get size\n");
return -EINVAL;
}
return size;
}
int tcmu_get_cdb_length(uint8_t *cdb)
{
uint8_t group_code = cdb[0] >> 5;
/* See spc-4 4.2.5.1 operation code */
switch (group_code) {
case 0: /*000b for 6 bytes commands */
return 6;
case 1: /*001b for 10 bytes commands */
case 2: /*010b for 10 bytes commands */
return 10;
case 3: /*011b Reserved ? */
if (cdb[0] == 0x7f)
return 8 + cdb[7];
return -EINVAL;
case 4: /*100b for 16 bytes commands */
return 16;
case 5: /*101b for 12 bytes commands */
return 12;
case 6: /*110b Vendor Specific */
case 7: /*111b Vendor Specific */
default:
/* TODO: */
return -EINVAL;
}
}
uint64_t tcmu_get_lba(uint8_t *cdb)
{
uint16_t val;
switch (tcmu_get_cdb_length(cdb)) {
case 6:
val = be16toh(*((uint16_t *)&cdb[2]));
return ((cdb[1] & 0x1f) << 16) | val;
case 10:
return be32toh(*((u_int32_t *)&cdb[2]));
case 12:
return be32toh(*((u_int32_t *)&cdb[2]));
case 16:
return be64toh(*((u_int64_t *)&cdb[2]));
default:
return -EINVAL;
}
}
uint32_t tcmu_get_xfer_length(uint8_t *cdb)
{
switch (tcmu_get_cdb_length(cdb)) {
case 6:
return cdb[4];
case 10:
return be16toh(*((uint16_t *)&cdb[7]));
case 12:
return be32toh(*((u_int32_t *)&cdb[6]));
case 16:
return be32toh(*((u_int32_t *)&cdb[10]));
default:
return -EINVAL;
}
}
/*
* Returns location of first mismatch between bytes in mem and the iovec.
* If they are the same, return -1.
*/
off_t tcmu_compare_with_iovec(void *mem, struct iovec *iovec, size_t size)
{
off_t mem_off;
int ret;
mem_off = 0;
while (size) {
size_t part = min(size, iovec->iov_len);
ret = memcmp(mem + mem_off, iovec->iov_base, part);
if (ret) {
size_t pos;
char *spos = mem + mem_off;
char *dpos = iovec->iov_base;
/*
* Data differed, this is assumed to be 'rare'
* so use a much more expensive byte-by-byte
* comparison to find out at which offset the
* data differs.
*/
for (pos = 0; pos < part && *spos++ == *dpos++;
pos++)
;
return pos + mem_off;
}
size -= part;
mem_off += part;
iovec++;
}
return -1;
}
/*
* Consume an iovec. Count must not exceed the total iovec[] size.
*/
void tcmu_seek_in_iovec(struct iovec *iovec, size_t count)
{
while (count) {
if (count >= iovec->iov_len) {
count -= iovec->iov_len;
iovec->iov_len = 0;
iovec++;
} else {
iovec->iov_base += count;
iovec->iov_len -= count;
count = 0;
}
}
}
size_t tcmu_iovec_length(struct iovec *iovec, size_t iov_cnt)
{
size_t length = 0;
while (iov_cnt) {
length += iovec->iov_len;
iovec++;
iov_cnt--;
}
return length;
}
int tcmu_set_sense_data(uint8_t *sense_buf, uint8_t key, uint16_t asc_ascq,
uint32_t *info)
{
memset(sense_buf, 0, 18);
sense_buf[0] = 0x70; /* fixed, current */
sense_buf[2] = key;
sense_buf[7] = 0xa;
sense_buf[12] = (asc_ascq >> 8) & 0xff;
sense_buf[13] = asc_ascq & 0xff;
if (info) {
if (key == MISCOMPARE) {
uint32_t val32 = htobe32(*info);
memcpy(&sense_buf[3], &val32, 4);
sense_buf[0] |= 0x80;
} else if (key == NOT_READY) {
uint16_t val16 = htobe16((uint16_t)*info);
memcpy(&sense_buf[16], &val16, 2);
sense_buf[15] |= 0x80;
}
}
/*
* It's very common to set sense and return check condition.
* Returning this lets us do both in one go. Or, just ignore
* this and return scsi_status yourself.
*/
return SAM_STAT_CHECK_CONDITION;
}
/*
* Zero iovec.
*/
void tcmu_zero_iovec(struct iovec *iovec, size_t iov_cnt)
{
while (iov_cnt) {
bzero(iovec->iov_base, iovec->iov_len);
iovec++;
iov_cnt--;
}
}
/*
* Copy data into an iovec, and consume the space in the iovec.
*
* Will truncate instead of overrunning the iovec.
*/
size_t tcmu_memcpy_into_iovec(
struct iovec *iovec,
size_t iov_cnt,
void *src,
size_t len)
{
size_t copied = 0;
while (len && iov_cnt) {
size_t to_copy = min(iovec->iov_len, len);
if (to_copy) {
memcpy(iovec->iov_base, src + copied, to_copy);
len -= to_copy;
copied += to_copy;
iovec->iov_base += to_copy;
iovec->iov_len -= to_copy;
}
iovec++;
iov_cnt--;
}
return copied;
}
/*
* Copy data from an iovec, and consume the space in the iovec.
*/
size_t tcmu_memcpy_from_iovec(
void *dest,
size_t len,
struct iovec *iovec,
size_t iov_cnt)
{
size_t copied = 0;
while (len && iov_cnt) {
size_t to_copy = min(iovec->iov_len, len);
if (to_copy) {
memcpy(dest + copied, iovec->iov_base, to_copy);
len -= to_copy;
copied += to_copy;
iovec->iov_base += to_copy;
iovec->iov_len -= to_copy;
}
iovec++;
iov_cnt--;
}
return copied;
}
int tcmu_emulate_std_inquiry(
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
uint8_t buf[36];
size_t copied;
memset(buf, 0, sizeof(buf));
buf[2] = 0x05; /* SPC-3 */
buf[3] = 0x02; /* response data format */
buf[7] = 0x02; /* CmdQue */
memcpy(&buf[8], "LIO-ORG ", 8);
memset(&buf[16], 0x20, 16);
memcpy(&buf[16], "TCMU device", 11);
memcpy(&buf[32], "0002", 4);
buf[4] = 31; /* Set additional length to 31 */
copied = tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
if (copied != sizeof(buf)) {
return tcmu_set_sense_data(sense, HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE, NULL);
}
return SAM_STAT_GOOD;
}
/* This func from CCAN str/hex/hex.c. Public Domain */
static bool char_to_hex(unsigned char *val, char c)
{
if (c >= '0' && c <= '9') {
*val = c - '0';
return true;
}
if (c >= 'a' && c <= 'f') {
*val = c - 'a' + 10;
return true;
}
if (c >= 'A' && c <= 'F') {
*val = c - 'A' + 10;
return true;
}
return false;
}
int tcmu_emulate_evpd_inquiry(
struct tcmu_device *dev,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
size_t copied;
switch (cdb[2]) {
case 0x0: /* Supported VPD pages */
{
char data[7];
memset(data, 0, sizeof(data));
/* data[1] (page code) already 0 */
data[5] = 0x83;
data[6] = 0xb0;
data[3] = 3;
copied = tcmu_memcpy_into_iovec(iovec, iov_cnt, data,
sizeof(data));
if (copied != sizeof(data)) {
return tcmu_set_sense_data(sense, HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE,
NULL);
}
return SAM_STAT_GOOD;
}
break;
case 0x83: /* Device identification */
{
char data[512];
char *ptr;
size_t used = 0;
char *wwn;
size_t len;
uint16_t *tot_len = (uint16_t*) &data[2];
memset(data, 0, sizeof(data));
data[1] = 0x83;
wwn = tcmu_get_wwn(dev);
if (!wwn) {
return tcmu_set_sense_data(sense, HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE, NULL);
}
ptr = &data[4];
/* 1/3: T10 Vendor id */
ptr[0] = 2; /* code set: ASCII */
ptr[1] = 1; /* identifier: T10 vendor id */
memcpy(&ptr[4], "LIO-ORG ", 8);
len = snprintf(&ptr[12], sizeof(data) - 16, "%s", wwn);
ptr[3] = 8 + len + 1;
used += (uint8_t)ptr[3] + 4;
ptr += used;
/* 2/3: NAA binary */
ptr[0] = 1; /* code set: binary */
ptr[1] = 3; /* identifier: NAA */
ptr[3] = 16; /* body length for naa registered extended format */
/*
* Set type 6 and use OpenFabrics IEEE Company ID: 00 14 05
*/
ptr[4] = 0x60;
ptr[5] = 0x01;
ptr[6] = 0x40;
ptr[7] = 0x50;
/*
* Fill in the rest with a binary representation of WWN
*
* This implementation only uses a nibble out of every byte of
* WWN, but this is what the kernel does, and it's nice for our
* values to match.
*/
char *p = wwn;
bool next = true;
int i = 7;
for ( ; *p && i < 20; p++) {
uint8_t val;
if (!char_to_hex(&val, *p))
continue;
if (next) {
next = false;
ptr[i++] |= val;
} else {
next = true;
ptr[i] = val << 4;
}
}
used += 20;
ptr += 20;
/* 3/3: Vendor specific */
ptr[0] = 2; /* code set: ASCII */
ptr[1] = 0; /* identifier: vendor-specific */
len = snprintf(&ptr[4], sizeof(data) - used - 4, "%s", dev->cfgstring);
ptr[3] = len + 1;
used += (uint8_t)ptr[3] + 4;
/* Done with descriptor list */
*tot_len = htobe16(used);
copied = tcmu_memcpy_into_iovec(iovec, iov_cnt, data,
used + 4);
if (copied != used + 4) {
return tcmu_set_sense_data(sense, HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE,
NULL);
}
free(wwn);
wwn = NULL;
return SAM_STAT_GOOD;
}
break;
case 0xb0: /* Block Limits */
{
char data[64];
int block_size;
int max_sectors;
int max_xfer_length;
uint16_t val16;
uint32_t val32;
memset(data, 0, sizeof(data));
data[1] = 0xb0;
val16 = htobe16(0x3c);
memcpy(&data[2], &val16, 2);
block_size = tcmu_get_attribute(dev, "hw_block_size");
if (block_size < 0) {
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
}
max_sectors = tcmu_get_attribute(dev, "hw_max_sectors");
if (max_sectors < 0) {
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
}
/* Convert from sectors to blocks */
max_xfer_length = max_sectors / (block_size / SECTOR_SIZE);
val32 = htobe32(max_xfer_length);
/* Max xfer length */
memcpy(&data[8], &val32, 4);
/* Optimal xfer length */
memcpy(&data[12], &val32, 4);
copied = tcmu_memcpy_into_iovec(iovec, iov_cnt, data,
sizeof(data));
if (copied != sizeof(data)) {
return tcmu_set_sense_data(sense, HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE,
NULL);
}
return SAM_STAT_GOOD;
}
break;
default:
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
}
}
/*
* Emulate INQUIRY(0x12)
*/
int tcmu_emulate_inquiry(
struct tcmu_device *dev,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
if (!(cdb[1] & 0x01)) {
if (!cdb[2])
return tcmu_emulate_std_inquiry(cdb, iovec, iov_cnt, sense);
else
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
}
else {
return tcmu_emulate_evpd_inquiry(dev, cdb, iovec, iov_cnt, sense);
}
}
int tcmu_emulate_test_unit_ready(
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
return SAM_STAT_GOOD;
}
int tcmu_emulate_read_capacity_10(
uint64_t num_lbas,
uint32_t block_size,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
uint8_t buf[8];
uint32_t val32;
memset(buf, 0, sizeof(buf));
if (num_lbas < 0x100000000ULL) {
// Return the LBA of the last logical block, so subtract 1.
val32 = htobe32(num_lbas-1);
} else {
// This lets the initiator know that he needs to use
// Read Capacity(16).
val32 = 0xffffffff;
}
memcpy(&buf[0], &val32, 4);
val32 = htobe32(block_size);
memcpy(&buf[4], &val32, 4);
/* all else is zero */
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
return SAM_STAT_GOOD;
}
int tcmu_emulate_read_capacity_16(
uint64_t num_lbas,
uint32_t block_size,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
uint8_t buf[32];
uint64_t val64;
uint32_t val32;
memset(buf, 0, sizeof(buf));
// Return the LBA of the last logical block, so subtract 1.
val64 = htobe64(num_lbas-1);
memcpy(&buf[0], &val64, 8);
val32 = htobe32(block_size);
memcpy(&buf[8], &val32, 4);
/* all else is zero */
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
return SAM_STAT_GOOD;
}
int handle_cache_page(uint8_t *buf, size_t buf_len)
{
if (buf_len < 20)
return -1;
buf[0] = 0x8;
buf[1] = 0x12;
buf[2] = 0x4; // WCE=1
return 20;
}
static struct {
uint8_t page;
uint8_t subpage;
int (*get)(uint8_t *buf, size_t buf_len);
} modesense_handlers[] = {
{8, 0, handle_cache_page},
// TODO: control page
};
/*
* Handle MODE_SENSE(6) and MODE_SENSE(10).
*
* For TYPE_DISK only.
*/
int tcmu_emulate_mode_sense(
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
bool sense_ten = (cdb[0] == MODE_SENSE_10);
uint8_t page_code = cdb[2] & 0x3f;
uint8_t subpage_code = cdb[3];
size_t alloc_len = tcmu_get_xfer_length(cdb);
int i;
int ret;
size_t used_len;
uint8_t buf[512];
bool got_sense = false;
memset(buf, 0, sizeof(buf));
/* Mode parameter header. Mode data length filled in at the end. */
used_len = sense_ten ? 8 : 4;
/* Don't fill in device-specific parameter */
/* This helper fn doesn't support sw write protect (SWP) */
/* Don't report block descriptors */
if (page_code == 0x3f) {
got_sense = true;
for (i = 0; i < ARRAY_SIZE(modesense_handlers); i++) {
ret = modesense_handlers[i].get(&buf[used_len], sizeof(buf) - used_len);
if (ret <= 0)
break;
if (sense_ten && (used_len + ret >= 255))
break;
if (used_len + ret > alloc_len)
break;
used_len += ret;
}
}
else {
for (i = 0; i < ARRAY_SIZE(modesense_handlers); i++) {
if (page_code == modesense_handlers[i].page
&& subpage_code == modesense_handlers[i].subpage) {
ret = modesense_handlers[i].get(&buf[used_len],
sizeof(buf) - used_len);
if (ret <= 0)
break;
if (!sense_ten && (used_len + ret >= 255))
break;
if (used_len + ret > alloc_len)
break;
used_len += ret;
got_sense = true;
break;
}
}
}
if (!got_sense)
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
if (sense_ten) {
uint16_t *ptr = (uint16_t*) buf;
*ptr = htobe16(used_len - 2);
}
else {
buf[0] = used_len - 1;
}
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
return SAM_STAT_GOOD;
}
/*
* Handle MODE_SELECT(6) and MODE_SELECT(10).
*
* For TYPE_DISK only.
*/
int tcmu_emulate_mode_select(
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
bool select_ten = (cdb[0] == MODE_SELECT_10);
uint8_t page_code = cdb[2] & 0x3f;
uint8_t subpage_code = cdb[3];
size_t alloc_len = tcmu_get_xfer_length(cdb);
int i;
int ret = 0;
size_t hdr_len = select_ten ? 8 : 4;
uint8_t buf[512];
uint8_t in_buf[512];
bool got_sense = false;
if (!alloc_len)
return SAM_STAT_GOOD;
if (tcmu_memcpy_from_iovec(in_buf, sizeof(in_buf), iovec, iov_cnt) >= sizeof(in_buf))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_PARAMETER_LIST_LENGTH_ERROR, NULL);
/* Abort if !pf or sp */
if (!(cdb[1] & 0x10) || (cdb[1] & 0x01))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
memset(buf, 0, sizeof(buf));
for (i = 0; i < ARRAY_SIZE(modesense_handlers); i++) {
if (page_code == modesense_handlers[i].page
&& subpage_code == modesense_handlers[i].subpage) {
ret = modesense_handlers[i].get(&buf[hdr_len],
sizeof(buf) - hdr_len);
if (ret <= 0)
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
if (!select_ten && (hdr_len + ret >= 255))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
got_sense = true;
break;
}
}
if (!got_sense)
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
if (alloc_len < (hdr_len + ret))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_PARAMETER_LIST_LENGTH_ERROR, NULL);
/* Verify what was selected is identical to what sense returns, since we
don't support actually setting anything. */
if (memcmp(&buf[hdr_len], &in_buf[hdr_len], ret))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_PARAMETER_LIST, NULL);
return SAM_STAT_GOOD;
}
int tcmu_emulate_start_stop(struct tcmu_device *dev, uint8_t *cdb,
uint8_t *sense)
{
if ((cdb[4] >> 4) & 0xf)
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
/* Currently, we don't allow ejecting the medium, so we're
* ignoring the FBO_PREV_EJECT flag, but it may turn out that
* initiators do not handle this well, so we may have to change
* this behavior.
*/
if (!(cdb[4] & 0x01))
return tcmu_set_sense_data(sense, ILLEGAL_REQUEST,
ASC_INVALID_FIELD_IN_CDB, NULL);
return SAM_STAT_GOOD;
}