forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_optical.c
1573 lines (1303 loc) · 38.7 KB
/
file_optical.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 2016, IBM Corp.
*
* 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.
*/
/*
* Block driver for the File Optical Format.
*
* Copyright (C) 2016, IBM Corp.
* Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
* Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <endian.h>
#include <scsi/scsi.h>
#include <linux/cdrom.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include "be_byteshift.h"
#include "tcmu-runner.h"
#include "tcmur_device.h"
#include "libtcmu.h"
struct fbo_state {
int fd;
uint64_t num_lbas;
uint32_t block_size;
uint32_t cur_lba;
#define FBO_READ_ONLY 0x01
#define FBO_PREV_EJECT 0x02
#define FBO_DEV_IO 0x04
#define FBO_BUSY_EVENT 0x08
#define FBO_FORMATTING 0x10
#define FBO_FORMAT_IMMED 0x20
uint32_t flags;
uint32_t format_progress;
uint8_t event_op_ch_code;
uint8_t async_cache_count;
pthread_mutex_t state_mtx;
int curr_handler;
};
static void fbo_report_op_change(struct tcmu_device *dev, uint8_t code)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
pthread_mutex_lock(&state->state_mtx);
if (code > state->event_op_ch_code)
state->event_op_ch_code = code;
pthread_mutex_unlock(&state->state_mtx);
}
/* Note: this is called per lun, not per mapping */
static int fbo_open(struct tcmu_device *dev, bool reopen)
{
struct fbo_state *state;
int64_t size;
char *options;
char *path;
int ret;
state = calloc(1, sizeof(*state));
if (!state)
return -ENOMEM;
tcmur_dev_set_private(dev, state);
#if 0
/* TBD: If we can figure out how the hw_block_size attribute
* gets set (and change it), we could use that in the future.
*/
state->block_size = tcmu_cfgfs_dev_get_attr(dev, "hw_block_size");
if (state->block_size == -1) {
tcmu_err("Could not get device block size\n");
goto err;
}
tcmu_dev_set_block_size(dev, state->block_size);
#else
/* MM logical units use a block size of 2048 */
state->block_size = 2048;
tcmu_dev_set_block_size(dev, state->block_size);
#endif
size = tcmu_cfgfs_dev_get_info_u64(dev, "Size", &ret);
if (ret < 0) {
tcmu_err("Could not get device size\n");
goto err;
}
tcmu_dev_set_num_lbas(dev, size / state->block_size);
state->num_lbas = size / state->block_size;
tcmu_dbg("open: cfgstring %s\n", tcmu_dev_get_cfgstring(dev));
options = strchr(tcmu_dev_get_cfgstring(dev), '/');
if (!options) {
tcmu_err("invalid cfgstring\n");
goto err;
}
options += 1; /* get past '/' */
while (options[0] != '/') {
if (!strncasecmp(options, "ro/", 3))
state->flags |= FBO_READ_ONLY;
else
tcmu_err("Ignoring unknown option %s\n", options);
options = strchr(options, '/');
if (!options) {
tcmu_err("no path found in cfgstring");
goto err;
}
options += 1;
}
path = options;
if (!path) {
tcmu_err("no path found in cfgstring\n");
goto err;
}
if (access(path, F_OK) == -1)
state->fd = open(path, O_CREAT | O_RDWR | O_EXCL,
S_IRUSR | S_IWUSR);
else if (state->flags & FBO_READ_ONLY)
state->fd = open(path, O_RDONLY, 0);
else
state->fd = open(path, O_RDWR, 0);
if (state->fd == -1) {
tcmu_err("could not open %s: %m\n", path);
goto err;
}
tcmu_dbg("FBO Open: fd %d\n", state->fd);
pthread_mutex_init(&state->state_mtx, NULL);
/* Record that we've changed our Operational state */
fbo_report_op_change(dev, 0x02);
return 0;
err:
free(state);
return -EINVAL;
}
static void fbo_close(struct tcmu_device *dev)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
pthread_mutex_destroy(&state->state_mtx);
close(state->fd);
free(state);
}
static int fbo_emulate_inquiry(uint8_t *cdb, struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
uint8_t buf[36];
if ((cdb[1] & 0x01) || cdb[2])
return TCMU_STS_INVALID_CDB;
memset(buf, 0, sizeof(buf));
buf[0] = 0x05; /* CD/DVD device */
buf[1] = 0x80; /* Removable Medium Bit */
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], "VOPTA", 5);
memcpy(&buf[32], "0001", 4);
buf[4] = 31; /* additional length */
/* TBD: Resid data? */
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
return TCMU_STS_OK;
}
static int fbo_emulate_request_sense(struct tcmu_device *dev, uint8_t *cdb,
struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t buf[18];
if (cdb[1] & 0x01)
return TCMU_STS_INVALID_CDB;
/* Note that upon successful completion, Request Sense returns the
* sense data in the data buffer, not as sense data.
*/
memset(buf, 0, sizeof(buf));
buf[0] = 0x70;
buf[7] = 0xa;
if (state->flags & FBO_FORMATTING) {
buf[2] = NOT_READY;
buf[12] = 0x04; // Not Ready
buf[13] = 0x04; // Format in progress
buf[15] = 0x80;
put_unaligned_be16(state->format_progress, &buf[16]);
}
else {
buf[2] = NO_SENSE;
}
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, sizeof(buf));
return TCMU_STS_OK;
}
static int fbo_handle_rwerp_page(uint8_t *buf, size_t buf_len,
uint8_t page_control)
{
if (buf_len < 12)
return -1;
buf[0] = GPMODE_R_W_ERROR_PAGE;
buf[1] = 10;
if (page_control != 1) {
buf[3] = 5; /* Read Retry Count */
buf[8] = 5; /* Write Retry Count */
}
return 12;
}
static int fbo_handle_cache_page(uint8_t *buf, size_t buf_len,
uint8_t page_control)
{
if (buf_len < 12)
return -1;
buf[0] = GPMODE_WCACHING_PAGE;
buf[1] = 10;
if (page_control != 1) {
buf[2] = 4; /* WCE=1 */
}
return 12;
}
static int fbo_handle_timeout_page(uint8_t *buf, size_t buf_len,
uint8_t page_control)
{
if (buf_len < 10)
return -1;
buf[0] = GPMODE_TO_PROTECT_PAGE;
buf[1] = 8;
if (page_control != 1) {
buf[6] = 0xff;
buf[7] = 0xff;
buf[8] = 0xff;
buf[9] = 0xff;
}
return 10;
}
static struct {
uint8_t page;
int (*get)(uint8_t *buf, size_t buf_len, uint8_t page_control);
} fbo_modesense_handlers[] = {
{ GPMODE_R_W_ERROR_PAGE, fbo_handle_rwerp_page },
{ GPMODE_WCACHING_PAGE, fbo_handle_cache_page },
{ GPMODE_TO_PROTECT_PAGE, fbo_handle_timeout_page },
};
static int fbo_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_control = (cdb[2] >> 6) & 0x3;
uint8_t page_code = cdb[2] & 0x3F;
bool return_pages = true;
size_t alloc_len = tcmu_cdb_get_xfer_length(cdb);
int i;
int ret;
int used_len;
uint8_t buf[512];
bool got_sense = false;
/* We don't support saved pages */
if (page_control == 3)
return TCMU_STS_NOTSUPP_SAVE_PARAMS;
memset(buf, 0, sizeof(buf));
if ((page_code == 0) && (page_control == 0))
return_pages = false;
/* Mode parameter header length */
used_len = sense_ten ? 8 : 4;
if (return_pages) {
if (page_code == 0x3f) {
got_sense = true;
for (i = 0; i < ARRAY_SIZE(fbo_modesense_handlers);
i++) {
ret = fbo_modesense_handlers[i].get(&buf[used_len],
sizeof(buf) - used_len,
page_control);
if (ret <= 0)
break;
if (used_len + ret >= alloc_len)
break;
used_len += ret;
}
}
else {
for (i = 0; i < ARRAY_SIZE(fbo_modesense_handlers);
i++) {
if (page_code == fbo_modesense_handlers[i].page) {
ret = fbo_modesense_handlers[i].get(&buf[used_len],
sizeof(buf) - used_len,
page_control);
if (ret <= 0)
break;
used_len += ret;
got_sense = true;
break;
}
}
}
if (!got_sense)
return TCMU_STS_INVALID_CDB;
}
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, used_len);
return TCMU_STS_OK;
}
static int fbo_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;
size_t alloc_len = tcmu_cdb_get_xfer_length(cdb);
int i;
int ret;
int used_len;
uint8_t buf[512];
uint8_t in_buf[512];
bool got_sense;
/* Abort if !PF or SP */
if (!(cdb[1] & 0x10) || (cdb[1] & 0x01))
return TCMU_STS_INVALID_CDB;
if (alloc_len > sizeof(in_buf))
return TCMU_STS_INVALID_PARAM_LIST_LEN;
memset(buf, 0, sizeof(buf));
if (tcmu_memcpy_from_iovec(in_buf, sizeof(in_buf), iovec, iov_cnt) !=
alloc_len)
return TCMU_STS_INVALID_PARAM_LIST_LEN;
/* Mode parameter header length */
used_len = select_ten ? 8 : 4;
while (alloc_len > used_len) {
got_sense = false;
page_code = in_buf[used_len];
for (i = 0; i < ARRAY_SIZE(fbo_modesense_handlers); i++) {
if (page_code == fbo_modesense_handlers[i].page) {
ret = fbo_modesense_handlers[i].get(&buf[used_len],
sizeof(buf) - used_len,
0);
if (ret <= 0)
return TCMU_STS_INVALID_CDB;
if (used_len + ret > alloc_len)
return TCMU_STS_INVALID_PARAM_LIST_LEN;
got_sense = true;
break;
}
}
if (!got_sense)
return TCMU_STS_INVALID_PARAM_LIST;
/* We don't support changing anything, so data must match */
if (memcmp(&buf[used_len], &in_buf[used_len], ret))
return TCMU_STS_INVALID_PARAM_LIST;
used_len += ret;
}
return TCMU_STS_OK;
}
static int fbo_emulate_allow_medium_removal(struct tcmu_device *dev,
uint8_t *cdb, uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
pthread_mutex_lock(&state->state_mtx);
/* We're ignoring the persistent prevent bit */
if (cdb[4] & 0x01)
state->flags |= FBO_PREV_EJECT;
else
state->flags &= ~FBO_PREV_EJECT;
pthread_mutex_unlock(&state->state_mtx);
return TCMU_STS_OK;
}
static int fbo_emulate_read_toc(struct tcmu_device *dev, uint8_t *cdb,
struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t time_bit = cdb[1] & 0x02;
uint8_t format = cdb[2] & 0x0f;
uint8_t buf[512];
memset(buf, 0, sizeof(buf));
// TBD: If we simulate start/stop, then fail if stopped
switch (format) {
case 0x00: // Formatted TOC
buf[1] = 0x12; // TOC Data Length
buf[2] = 1; // First Track
buf[3] = 1; // Last Track
buf[5] = 0x14; // ADR + CONTROL
buf[6] = 1; // Track #
if (time_bit)
buf[10] = CDROM_MSF; // 00:00:02:00
if (state->flags & FBO_READ_ONLY)
buf[13] = 0x14; // ADR + CONTROL
else
buf[13] = 0x17; // ADR + CONTROL
buf[14] = CDROM_LEADOUT; // Track #
if (time_bit) {
/* Max time address is 00:FF:3B:4A */
if (state->num_lbas >=
(0xff * CD_SECS + CD_SECS - 1) * CD_FRAMES +
CD_FRAMES - 1 - CD_MSF_OFFSET) {
buf[17] = 0xff;
buf[18] = CD_SECS - 1;
buf[19] = CD_FRAMES - 1;
}
else {
buf[17] = (state->num_lbas / CD_FRAMES + 2) /
CD_SECS;
buf[18] = (state->num_lbas / CD_FRAMES + 2) %
CD_SECS;
buf[19] = state->num_lbas % CD_FRAMES;
}
}
else {
put_unaligned_be32(state->num_lbas, &buf[16]);
}
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 0x14);
break;
case 0x01: // Multi-Session Information
buf[1] = 0xa; // TOC Data Length
buf[2] = 1; // First Complete Session
buf[3] = 1; // Last Complete Session
buf[5] = 0x14; // ADR + CONTROL
buf[6] = 1; // Track #
if (time_bit)
buf[10] = 2; // 00:00:02:00
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 0xc);
break;
default:
return TCMU_STS_INVALID_CDB;
}
return TCMU_STS_OK;
}
static int fbo_emulate_get_configuration(struct tcmu_device *dev, uint8_t *cdb,
struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t rt = cdb[1] & 0x03;
uint16_t start = be16toh(*(uint16_t *)&cdb[2]);
int used_len;
uint8_t *cur;
uint8_t buf[512];
/* Reserved value for RT */
if (rt == 3)
return TCMU_STS_INVALID_CDB;
memset(buf, 0, sizeof(buf));
/* Set current profile in the feature header */
if (state->flags & FBO_READ_ONLY)
put_unaligned_be16(0x10, &buf[6]); //DVD-ROM
else
put_unaligned_be16(0x12, &buf[6]); //DVD-ROM
/* Feature header */
used_len = 8;
/* Profile List Feature */
if ((rt == 2 && start == 0x0000) ||
(start <= 0x0000 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0000);
cur[2] = 3; // persistent=1, current=1
cur[3] = 16; // # of profile descriptors * 4
*(uint16_t *)&cur[4] = htobe16(0x12); // DVD-RAM Profile
if (!(state->flags & FBO_READ_ONLY))
cur[6] = 1; // active profile
*(uint16_t *)&cur[8] = htobe16(0x10); // DVD-ROM Profile
if (state->flags & FBO_READ_ONLY)
cur[10] = 1; // active profile
*(uint16_t *)&cur[12] = htobe16(0x0a); // CD-RW Profile
*(uint16_t *)&cur[16] = htobe16(0x08); // CD-ROM Profile
used_len += 20;
}
/* Core Feature */
if ((rt == 2 && start == 0x0001) ||
(start <= 0x0001 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0001);
cur[2] = 7; // version=1, persistent=1, current=1
cur[3] = 8; // additional length
/* SCSI Physical Interface Standard */
*(uint32_t *)&cur[4] = htobe32(1);
cur[8] = 1; // Device Busy Event
used_len += 12;
}
/* Morphing Feature */
if ((rt == 2 && start == 0x0002) ||
(start <= 0x0002 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0002);
cur[2] = 7; // version=1, persistent=1, current=1
cur[3] = 4; // additional length
cur[4] = 2; // OCEvent = 1, ASYNC = 0
used_len += 8;
}
/* Removable Medium Feature */
if ((rt == 2 && start == 0x0003) ||
(start <= 0x0003 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0003);
cur[2] = 3; // persistent=1, current=1
cur[3] = 4; // additional length
cur[4] = 0x20; // mechanism=1, eject=0, pvnt jmpr=0, lock=0
used_len += 8;
}
/* Random Readable Feature */
if ((rt == 2 && start == 0x0010) ||
(start <= 0x0010 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0010);
cur[2] = 1; // version=0, persistent=0, current=1
cur[3] = 8; // additional length
*(uint32_t *)&cur[4] = htobe32(state->block_size);
*(uint16_t *)&cur[8] = htobe16(0x10); // Blocking
cur[10] = 0x01; // PP=1
used_len += 12;
}
/* Multi Read Feature - never current */
if ((rt == 2 && start == 0x001d) ||
(start <= 0x001d && rt == 0)) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x001d);
cur[2] = 0; // version=0, persistent=0, current=0
cur[3] = 0; // additional length
used_len += 4;
}
/* CD Read Feature - never current */
if ((rt == 2 && start == 0x001e) ||
(start <= 0x001e && rt == 0)) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x001e);
cur[2] = 8; // version=2, persistent=0, current=0
cur[3] = 4; // additional length
used_len += 8;
}
/* DVD Read Feature */
if ((rt == 2 && start == 0x001f) ||
(start <= 0x001f && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x001f);
cur[2] = 1; // version=0, persistent=0, current=1
cur[3] = 0; // additional length
used_len += 4;
}
/* Random Writable Feature */
if ((rt == 2 && start == CDF_RWRT) ||
(start <= CDF_RWRT &&
(rt == 0 || (rt == 1 && !(state->flags & FBO_READ_ONLY))))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(CDF_RWRT);
if (state->flags & FBO_READ_ONLY)
cur[2] = 4; // version=1, persistent=0, current=0
else
cur[2] = 5; // version=1, persistent=0, current=1
cur[3] = 0xc; // additional length
*(uint32_t *)&cur[4] = htobe32(state->num_lbas - 1);
*(uint32_t *)&cur[8] = htobe32(state->block_size);
*(uint16_t *)&cur[12] = htobe16(0x10); // Blocking
cur[14] = 0x01; // PP=1
used_len += 16;
}
/* Incremental Streaming Writable Feature - never current */
if ((rt == 2 && start == 0x0021) ||
(start <= 0x0021 && rt == 0)) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0021);
cur[2] = 4; // version=1, persistent=0, current=0
cur[3] = 4; // additional length
used_len += 8;
}
/* Formattable Feature */
if ((rt == 2 && start == 0x0023) ||
(start <= 0x0023 &&
(rt == 0 || (rt == 1 && !(state->flags & FBO_READ_ONLY))))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0023);
if (state->flags & FBO_READ_ONLY)
cur[2] = 0; // version=0, persistent=0, current=0
else
cur[2] = 1; // version=0, persistent=0, current=1
cur[3] = 0; // additional length
used_len += 4;
}
/* Hardware Defect Management Feature */
if ((rt == 2 && start == CDF_HWDM) ||
(start <= CDF_HWDM &&
(rt == 0 || (rt == 1 && !(state->flags & FBO_READ_ONLY))))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(CDF_HWDM);
if (state->flags & FBO_READ_ONLY)
cur[2] = 4; // version=1, persistent=0, current=0
else
cur[2] = 5; // version=1, persistent=0, current=1
cur[3] = 4; // additional length
used_len += 8;
}
/* Restricted Overwrite Feature - never current */
if ((rt == 2 && start == 0x0026) ||
(start <= 0x0026 && rt == 0)) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0026);
cur[2] = 0; // version=0, persistent=0, current=0
cur[3] = 0; // additional length
used_len += 4;
}
/* CD Track at Once Feature - never current */
if ((rt == 2 && start == 0x002d) ||
(start <= 0x002d && rt == 0)) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x002d);
cur[2] = 8; // version=2, persistent=0, current=0
cur[3] = 4; // additional length
used_len += 8;
}
/* Timeout Feature */
if ((rt == 2 && start == 0x0105) ||
(start <= 0x0105 && (rt == 0 || rt == 1))) {
cur = &buf[used_len];
*(uint16_t *)&cur[0] = htobe16(0x0105);
cur[2] = 5; // version=1, persistent=0, current=1
cur[3] = 4; // additional length
used_len += 8;
}
put_unaligned_be32(used_len - 4, &buf[0]);
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, used_len);
return TCMU_STS_OK;
}
static int fbo_emulate_get_event_status_notification(struct tcmu_device *dev,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t class_req = cdb[4];
uint16_t alloc_len = tcmu_cdb_get_xfer_length(cdb);
int used_len;
uint8_t buf[8];
if (!(cdb[1] & 0x01))
/* We don't support asynchronous operation */
return TCMU_STS_INVALID_CDB;
memset(buf, 0, sizeof(buf));
buf[3] = 0x42; // Operational Change and Device Busy
if (!(class_req & 0x42)) {
/* We don't support any requested notification classes */
buf[2] = 0x80;
used_len = 4;
goto done;
}
pthread_mutex_lock(&state->state_mtx);
if (class_req & 0x02 &&
(state->event_op_ch_code || !(class_req & 0x40) ||
!(state->flags & FBO_BUSY_EVENT)))
{
/* We're reporting an Operational Change event */
buf[2] = 1;
if (alloc_len > 4)
{
buf[4] = state->event_op_ch_code;
if (state->event_op_ch_code)
put_unaligned_be16(0x0001, &buf[6]);
state->event_op_ch_code = 0;
used_len = 8;
}
else {
/* Only return the header */
used_len = 4;
}
}
else {
/* We're reporting a Device Busy event */
buf[2] = 6;
if (alloc_len > 4)
{
if (state->flags & FBO_BUSY_EVENT) {
/* A Busy Event has occurred */
buf[4] = 1;
state->flags &= ~FBO_BUSY_EVENT;
}
if ((state->flags & FBO_FORMAT_IMMED) ||
state->async_cache_count)
/* Our current state is "busy" */
buf[5] = 1;
used_len = 8;
}
else {
/* Only return the header */
used_len = 4;
}
}
pthread_mutex_unlock(&state->state_mtx);
done:
put_unaligned_be16(used_len - 4, &buf[0]);
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, used_len);
return TCMU_STS_OK;
}
static int fbo_emulate_read_disc_information(struct tcmu_device *dev,
uint8_t *cdb, struct iovec *iovec,
size_t iov_cnt, uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t buf[34];
memset(buf, 0, sizeof(buf));
buf[1] = 32; // Disc Information Length
if (state->flags & FBO_READ_ONLY)
buf[2] = 0x0E; // Era=0, Last=3, Status=2
else
buf[2] = 0x1F; // Era=1, Last=3, Status=3
buf[3] = 1; // First Track
buf[4] = 1; // One session
buf[5] = 1; // First Track (LSB)
buf[6] = 1; // Last Track (LSB)
buf[7] = 0x20; // URU=1
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 34);
return TCMU_STS_OK;
}
static int fbo_emulate_read_dvd_structure(struct tcmu_device *dev, uint8_t *cdb,
struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t format = cdb[7];
uint32_t start_phys;
uint32_t end_phys;
char manuf_info[] = "VIRTUAL FB OPT";
uint8_t buf[512];
// TBD: If we simulate start/stop, then fail if stopped and format != 0xff
/* Fail anything other than layer 0 */
if (cdb[6])
return TCMU_STS_INVALID_CDB;
memset(buf, 0, sizeof(buf));
switch (format) {
case DVD_STRUCT_PHYSICAL:
if (state->flags & FBO_READ_ONLY)
start_phys = 0x30000;
else
start_phys = 0x31000;
buf[1] = 19; //DVD Structure Data Length
if (state->flags & FBO_READ_ONLY)
buf[4] = 0x02; // Book Type=0, Part Version=2
else
buf[4] = 0x12; // Book Type=1, Part Version=2
buf[5] = 0x0f; // Maximum Rate=Not specified
if (!(state->flags & FBO_READ_ONLY))
buf[6] = 0x02; // Layer Type=2
buf[9] = start_phys >> 16; // Starting physical sector
put_unaligned_be16(start_phys & 0xffff, &buf[10]);
end_phys = start_phys + state->num_lbas - 1;
buf[13] = end_phys >> 16;
put_unaligned_be16(end_phys & 0xffff, &buf[14]);
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 21);
break;
case DVD_STRUCT_COPYRIGHT:
buf[1] = 6; // DVD Structure Data Length
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 8);
break;
case DVD_STRUCT_MANUFACT:
buf[2] = strlen(manuf_info) + 2; // DVD Struct Data Len
memcpy(&buf[4], manuf_info, strlen(manuf_info));
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf,
strlen(manuf_info) + 4);
break;
case 0x09: // DVD-RAM Medium Status (Cartridge Info)
if (state->flags & FBO_READ_ONLY)
return TCMU_STS_INVALID_CDB;
buf[1] = 6; // DVD Structure Data Length
buf[5] = 0x10; // Disc Type Identification
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 8);
break;
case 0xff: // Read DVD Structure Capabilities List
buf[1] = 18; // DVD Structure Data Length
buf[4] = 0x00; // Format Field
buf[5] = 0x40; // SDS=0, RDS=1
buf[7] = 21; // Structure Length
buf[8] = 0x01; // Format Field
buf[9] = 0x40; // SDS=0, RDS=1
buf[11] = 8; // Structure Length
buf[12] = 0x04; // Format Field
buf[13] = 0x40; // SDS=0, RDS=1
buf[15] = strlen(manuf_info) + 4; // Structure Length
buf[16] = 0x09; // Format Field
buf[17] = 0x40; // SDS=0, RDS=1
buf[19] = 8; // Structure Length
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 20);
break;
default:
return TCMU_STS_INVALID_CDB;
}
return TCMU_STS_OK;
}
static int fbo_emulate_mechanism_status(struct tcmu_device *dev, uint8_t *cdb,
struct iovec *iovec, size_t iov_cnt,
uint8_t *sense)
{
struct fbo_state *state = tcmur_dev_get_private(dev);
uint8_t buf[8];
memset(buf, 0, sizeof(buf));
if (state->flags & FBO_DEV_IO) {
buf[1] = 0x20; // mechanism state=1 (playing)
buf[2] = (state->cur_lba >> 16) & 0xff;
put_unaligned_be16(state->cur_lba & 0xffff, &buf[3]);
}
tcmu_memcpy_into_iovec(iovec, iov_cnt, buf, 8);
return TCMU_STS_OK;
}
static int fbo_do_sync(struct fbo_state *state, uint8_t *sense)
{
int rc;
rc = fsync(state->fd);
if (rc) {
tcmu_err("sync failed: %m\n");
return TCMU_STS_WR_ERR;
}
return TCMU_STS_OK;
}