/
vtllib.c
1116 lines (956 loc) · 24.9 KB
/
vtllib.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
/*
* Shared routines between vtltape & vtllibrary
*
* Copyright (C) 2005 - 2009 Mark Harvey markh794 at gmail dot com
* mark_harvey at symantec dot com
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdint.h>
#ifndef Solaris
#include <byteswap.h>
#endif
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/ipc.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include "be_byteshift.h"
#include "list.h"
#include "scsi.h"
#include "vtl_common.h"
#include "vtllib.h"
#include "ssc.h"
#include <zlib.h>
#include "log.h"
#include "q.h"
#ifndef Solaris
int ioctl(int, int, void *);
#endif
static int reset = 0;
static struct state_description {
char *state_desc;
} state_desc[] = {
{ "Initialising v1", },
{ "Idle", },
{ "Unloading", },
{ "Loading", },
{ "Loading Cleaning Tape", },
{ "Loading WORM media", },
{ "Loaded", },
{ "Loaded - Idle", },
{ "Load failed", },
{ "Rewinding", },
{ "Positioning", },
{ "Locate", },
{ "Reading", },
{ "Writing", },
{ "Unloading", },
{ "Erasing", },
{ "Moving media from drive to slot", },
{ "Moving media from slot to drive", },
{ "Moving media from drive to MAP", },
{ "Moving media from MAP to drive", },
{ "Moving media from slot to MAP", },
{ "Moving media from MAP to slot", },
{ "Moving media from drive to drive", },
{ "Moving media from slot to slot", },
{ "Opening MAP", },
{ "Closing MAP", },
{ "Robot Inventory", },
{ "Initialise Elements", },
{ "Online", },
{ "Offline", },
{ "System Uninitialised", },
};
uint8_t sense[SENSE_BUF_SIZE];
uint8_t blockDescriptorBlock[8] = {0, 0, 0, 0, 0, 0, 0, 0 };
void mhvtl_prt_cdb(int lvl, uint64_t sn, uint8_t *cdb)
{
int groupCode;
groupCode = (cdb[0] & 0xe0) >> 5;
switch (groupCode) {
case 0: /* 6 byte commands */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") "
"%02x %02x %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5]);
break;
case 1: /* 10 byte commands */
case 2: /* 10 byte commands */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") "
"%02x %02x %02x %02x %02x %02x"
" %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3],
cdb[4], cdb[5], cdb[6], cdb[7],
cdb[8], cdb[9]);
break;
case 3: /* Reserved - There is always one exception ;) */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") "
"%02x %02x %02x %02x %02x %02x"
" %02x %02x %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3],
cdb[4], cdb[5], cdb[6], cdb[7],
cdb[8], cdb[9], cdb[10], cdb[11]);
break;
case 4: /* 16 byte commands */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") "
"%02x %02x %02x %02x %02x %02x"
" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3],
cdb[4], cdb[5], cdb[6], cdb[7],
cdb[8], cdb[9], cdb[10], cdb[11],
cdb[12], cdb[13], cdb[14], cdb[15]);
break;
case 5: /* 12 byte commands */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") "
"%02x %02x %02x %02x %02x %02x %02x"
" %02x %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3],
cdb[4], cdb[5], cdb[6], cdb[7],
cdb[8], cdb[9], cdb[10], cdb[11]);
break;
case 6: /* Vendor Specific */
case 7: /* Vendor Specific */
MHVTL_DBG_NO_FUNC(lvl, "CDB (%" PRId64 ") VENDOR SPECIFIC !! "
" %02x %02x %02x %02x %02x %02x",
sn, cdb[0], cdb[1], cdb[2], cdb[3],
cdb[4], cdb[5]);
break;
}
}
/*
* Fills in a global array with current sense data
* Sets 'sam status' to SAM_STAT_CHECK_CONDITION.
*/
void mkSenseBuf(uint8_t sense_d, uint32_t sense_q, uint8_t *sam_stat)
{
/* Clear Sense key status */
memset(sense, 0, SENSE_BUF_SIZE);
*sam_stat = SAM_STAT_CHECK_CONDITION;
sense[0] = SD_CURRENT_INFORMATION_FIXED;
/* SPC4 (Revision 30) Ch: 4.5.1 states:
* The RESPONSE CODE field shall be set to 70h in all unit attention
* condition sense data in which:
* - The ADDITIONAL SENSE CODE field is set to 29h
* - The ADDITIONAL SENSE CODE is set to MODE PARAMETERS CHANGED
*/
switch (sense_d) {
case UNIT_ATTENTION:
if ((sense_q >> 8) == 0x29)
break;
if (sense_q == E_MODE_PARAMETERS_CHANGED)
break;
/* Fall thru to default handling */
default:
sense[0] |= SD_VALID;
break;
}
sense[2] = sense_d;
sense[7] = SENSE_BUF_SIZE - 8;
put_unaligned_be16(sense_q, &sense[12]);
MHVTL_DBG(3, "Sense buf: %p", &sense);
MHVTL_DBG(1, "SENSE [Key/ASC/ASCQ] [%02x %02x %02x]",
sense[2], sense[12], sense[13]);
}
int check_reset(uint8_t *sam_stat)
{
int retval = reset;
if (reset) {
mkSenseBuf(UNIT_ATTENTION, E_POWERON_RESET, sam_stat);
reset = 0;
}
return retval;
}
void reset_device(void)
{
reset = 1;
}
#define READ_POSITION_LONG_LEN 32
/* Return tape position - long format
*
* Need to implement.
* [ 4 - 7] Partition No.
* - The partition number for the current logical position
* [ 8 - 15] Logical Object No.
* - The number of logical blocks between the beginning of the
* - partition and the current logical position.
* [16 - 23] Logical Field Identifier
* - Number of Filemarks between the beginning of the partiion and
* - the logical position.
* [24 - 31] Logical Set Identifier
* - Number of Setmarks between the beginning of the partiion and
* - the logical position.
*/
int resp_read_position_long(loff_t pos, uint8_t *buf, uint8_t *sam_stat)
{
uint64_t partition = 1L;
MHVTL_DBG(1, "Position %ld", (long)pos);
memset(buf, 0, READ_POSITION_LONG_LEN); /* Clear 'array' */
if ((pos == 0) || (pos == 1))
buf[0] = 0x80; /* Begining of Partition */
/* partition should be zero, as we only support one */
buf[4] = (partition >> 24);
buf[5] = (partition >> 16);
buf[6] = (partition >> 8);
buf[7] = partition;
/* we don't know logical field identifier */
/* FIXME: this code is wrong -- pos should be returned in [8 - 15] */
buf[8] = buf[9] = (pos >> 16);
buf[6] = buf[10] = (pos >> 8);
buf[7] = buf[11] = pos;
return READ_POSITION_LONG_LEN;
}
#define READ_POSITION_LEN 20
/* Return tape position - short format */
int resp_read_position(loff_t pos, uint8_t *buf, uint8_t *sam_stat)
{
memset(buf, 0, READ_POSITION_LEN); /* Clear 'array' */
if ((pos == 0) || (pos == 1))
buf[0] = 0x80; /* Begining of Partition */
buf[4] = buf[8] = (pos >> 24);
buf[5] = buf[9] = (pos >> 16);
buf[6] = buf[10] = (pos >> 8);
buf[7] = buf[11] = pos;
MHVTL_DBG(1, "Positioned at block %ld", (long)pos);
return READ_POSITION_LEN;
}
#define READBLOCKLIMITS_ARR_SZ 6
int resp_read_block_limits(struct vtl_ds *dbuf_p, int sz)
{
uint8_t *arr = dbuf_p->data;
memset(arr, 0, READBLOCKLIMITS_ARR_SZ);
arr[1] = (sz >> 16);
arr[2] = (sz >> 8);
arr[3] = sz;
arr[5] = 0x4; /* Minimum block size */
return READBLOCKLIMITS_ARR_SZ;
}
/*
* Copy data in struct 'report_luns' into bufer and return length
*/
int resp_report_lun(struct report_luns *rpLUNs, uint8_t *buf, uint8_t *sam_stat)
{
uint64_t size = get_unaligned_be64(&rpLUNs->size) + 8;
memcpy( buf, (uint8_t *)&rpLUNs, size);
return size;
}
/*
* Respond with S/No. of media currently mounted
*/
int resp_read_media_serial(uint8_t *sno, uint8_t *buf, uint8_t *sam_stat)
{
uint64_t size = 0L;
MHVTL_DBG(1, "Read media S/No not implemented yet!");
return size;
}
void setTapeAlert(struct TapeAlert_page *ta, uint64_t flg)
{
int a;
MHVTL_DBG(2, "Setting TapeAlert flags 0x%.8x %.8x",
(uint32_t)(flg >> 32) & 0xffffffff,
(uint32_t)flg & 0xffffffff);
for (a = 0; a < 64; a++)
ta->TapeAlert[a].value = (flg & (1ull << a)) ? 1 : 0;
}
/*
* Simple function to read 'count' bytes from the chardev into 'buf'.
*/
int retrieve_CDB_data(int cdev, struct vtl_ds *ds)
{
MHVTL_DBG(3, "retrieving %d bytes from kernel", ds->sz);
ioctl(cdev, VTL_GET_DATA, ds);
return ds->sz;
}
/*
* Passes struct vtl_ds to kernel module.
* struct contains amount of data, status and pointer to data struct.
*
* Returns nothing.
*/
void completeSCSICommand(int cdev, struct vtl_ds *ds)
{
uint8_t *s;
MHVTL_DBG(2, "OP s/n: (%ld), sz: %d, sam_status: %d",
(unsigned long)ds->serialNo,
ds->sz, ds->sam_stat);
ioctl(cdev, VTL_PUT_DATA, ds);
s = ds->sense_buf;
if (ds->sam_stat == SAM_STAT_CHECK_CONDITION)
MHVTL_DBG(3, "[Key/ASC/ASCQ] [%02x %02x %02x]",
s[2], s[12], s[13]);
ds->sam_stat = 0;
}
/* Hex dump 'count' bytes at p */
void hex_dump(uint8_t *p, int count)
{
int j;
for (j = 0; j < count; j++) {
if ((j != 0) && (j % 16 == 0))
printf("\n");
printf("%02x ", p[j]);
}
printf("\n");
}
/* Writing to the kernel module will block until the device is created.
* Unfortunately, we need to be polling the device and process the
* SCSI op code before the lu can be created.
* Chicken & Egg.
* So spawn child process and don't wait for return.
* Let the child process write to the kernel module
*/
pid_t add_lu(int minor, struct vtl_ctl *ctl)
{
char str[1024];
pid_t pid;
ssize_t retval;
int pseudo;
char *pseudo_filename = "/sys/bus/pseudo/drivers/mhvtl/add_lu";
char errmsg[512];
sprintf(str, "add %d %d %d %d\n",
minor, ctl->channel, ctl->id, ctl->lun);
switch(pid = fork()) {
case 0: /* Child */
pseudo = open(pseudo_filename, O_WRONLY);
if (pseudo < 0) {
sprintf(errmsg, "Could not open %s", pseudo_filename);
MHVTL_DBG(1, "%s : %s", errmsg, strerror(errno));
perror("Cound not open 'add_lu'");
exit(-1);
}
retval = write(pseudo, str, strlen(str));
MHVTL_DBG(2, "Wrote %s (%d bytes)", str, (int)retval);
close(pseudo);
MHVTL_DBG(1, "Child anounces 'lu created'.");
exit(0);
break;
case -1:
perror("Failed to fork()");
MHVTL_DBG(1, "Fail to fork() %s", strerror(errno));
return 0;
break;
default:
MHVTL_DBG(1, "From a proud parent - birth of PID %ld",
(long)pid);
return pid;
break;
}
return 0;
}
static int chrdev_get_major(void)
{
FILE *f;
char *filename = "/sys/bus/pseudo/drivers/mhvtl/major";
int rc = 0;
int x;
int majno;
f = fopen(filename, "r");
if (!f) {
MHVTL_DBG(1, "Can't open %s: %s", filename, strerror(errno));
return -ENOENT;
}
x = fscanf(f, "%d", &majno);
if (!x) {
MHVTL_DBG(1, "Cant identify major number for mhvtl");
rc = -1;
} else
rc = majno;
fclose(f);
return rc;
}
int chrdev_chown(uint8_t minor, uid_t uid, uid_t gid)
{
char pathname[64];
int x;
snprintf(pathname, sizeof(pathname), "/dev/mhvtl%d", minor);
MHVTL_DBG(3, "chown(%s, %d, %d)", pathname, (int)uid, (int)gid);
x = chown(pathname, uid, uid);
if (x == -1) {
MHVTL_DBG(1, "Can't change ownership for device node mhvtl: %s",
strerror(errno));
return -1;
}
return 0;
}
int chrdev_create(uint8_t minor)
{
int majno;
int x;
int ret = 0;
dev_t dev;
char pathname[64];
snprintf(pathname, sizeof(pathname), "/dev/mhvtl%d", minor);
majno = chrdev_get_major();
if (majno == -ENOENT) {
MHVTL_DBG(1, "** Incorrect version of kernel module loaded **");
ret = -1;
goto err;
}
dev = makedev(majno, minor);
MHVTL_DBG(2, "Major number: %d, minor number: %d",
major(dev), minor(dev));
MHVTL_DBG(3, "mknod(%s, %02o, major: %d minor: %d",
pathname, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,
major(dev), minor(dev));
x = mknod(pathname, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, dev);
if (x < 0) {
if (errno == EEXIST) /* Success if node already exists */
return 0;
MHVTL_DBG(1, "Error creating device node for mhvtl: %s",
strerror(errno));
ret = -1;
}
err:
return ret;
}
int chrdev_open(char *name, uint8_t minor)
{
FILE *f;
char devname[256];
char buf[256];
int devn;
int ctlfd;
f = fopen("/proc/devices", "r");
if (!f) {
printf("Cannot open control path to the driver: %s\n",
strerror(errno));
return -1;
}
devn = 0;
while (!feof(f)) {
if (!fgets(buf, sizeof(buf), f))
break;
if (sscanf(buf, "%d %s", &devn, devname) != 2)
continue;
if (!strcmp(devname, name))
break;
devn = 0;
}
fclose(f);
if (!devn) {
printf("Cannot find %s in /proc/devices - "
"make sure the module is loaded\n", name);
return -1;
}
snprintf(devname, sizeof(devname), "/dev/%s%d", name, minor);
ctlfd = open(devname, O_RDWR|O_NONBLOCK);
if (ctlfd < 0) {
printf("Cannot open %s %s\n", devname, strerror(errno));
fflush(NULL);
printf("\n\n");
return -1;
}
return ctlfd;
}
/* Create the fifo and open it for writing (appending)
* Return 0 on success,
* Return errno on failure
*/
int open_fifo(FILE **fifo_fd, char *fifoname)
{
int ret;
umask(0);
ret = 0;
ret = mknod(fifoname, S_IFIFO|0644, 0);
if ((ret < 0) && (errno != EEXIST)) {
MHVTL_LOG("Sorry, cant create %s: %s, Disabling fifo feature",
fifoname, strerror(errno));
ret = errno;
} else {
*fifo_fd = fopen(fifoname, "w+");
if (*fifo_fd) {
MHVTL_DBG(2, "Successfully opened named pipe: %s",
fifoname);
} else {
MHVTL_LOG("Sorry, cant open %s: %s, "
"Disabling fifo feature",
fifoname, strerror(errno));
ret = errno;
}
}
return ret;
}
void status_change(FILE *fifo_fd, int current_status, int m_id, char **msg)
{
if (!fifo_fd)
return;
if (*msg) {
fprintf(fifo_fd, "%d: %s\n", m_id, *msg);
free(*msg);
*msg = NULL;
} else
fprintf(fifo_fd, "%d: %s\n", m_id,
state_desc[current_status].state_desc);
fflush(fifo_fd);
return;
}
/*
* Pinched straight from SCSI tgt code
* Thanks guys..
*
* Modified to always return success. Earlier kernels don't have oom_adjust
* 'feature' so don't fail if we can't find it..
*/
int oom_adjust(void)
{
int fd, ret;
char path[64];
/* Avoid oom-killer */
sprintf(path, "/proc/%d/oom_adj", getpid());
fd = open(path, O_WRONLY);
if (fd < 0) {
MHVTL_DBG(3, "Can't open oom-killer's pardon %s, %s",
path, strerror(errno));
return 0;
}
ret = write(fd, "-17\n", 4);
if (ret < 0) {
MHVTL_DBG(3, "Can't adjust oom-killer's pardon %s, %s",
path, strerror(errno));
}
close(fd);
return 0;
}
/* fgets but replace '\n' with null */
char *readline(char *buf, int len, FILE *s)
{
int i;
char *ret;
ret = fgets(buf, len, s);
if (!ret)
return ret;
/* Skip blank line */
for (i = 1; i < len; i++)
if (buf[i] == '\n')
buf[i] = 0;
MHVTL_DBG(3, "%s", buf);
return ret;
}
/* Copy bytes from 'src' to 'dest, blank-filling to length 'len'. There will
* not be a NULL byte at the end.
*/
void blank_fill(uint8_t *dest, char *src, int len)
{
int i;
for (i = 0; i < len; i++) {
if (*src != '\0')
*dest++ = *src++;
else
*dest++ = ' ';
}
}
void truncate_spaces(char *s, int maxlen)
{
int x;
for (x = 0; x < maxlen; x++)
if ((s[x] == ' ') || (s[x] == '\0')) {
s[x] = '\0';
return;
}
}
/* MHVTL_VERSION looks like : 0.18.xx or 1.xx.xx
*
* Convert into a string after converting the 18.xx into "18xx"
*
* NOTE: Caller has to free string after use.
*/
char *get_version(void)
{
char b[64];
int x, y, z;
char *c;
c = malloc(32); /* Way more than enough for a 4 byte string */
if (!c)
return NULL;
sprintf(b, "%s", MHVTL_VERSION);
sscanf(b, "%d.%d.%d", &x, &y, &z);
if (x)
sprintf(c, "%02d%02d", x, y);
else
sprintf(c, "%02d%02d", y, z);
return c;
}
void log_opcode(char *opcode, uint8_t *cdb, struct vtl_ds *dbuf_p)
{
MHVTL_DBG(1, "*** Unsupported op code: %s ***", opcode);
mkSenseBuf(ILLEGAL_REQUEST, E_INVALID_OP_CODE, &dbuf_p->sam_stat);
MHVTL_DBG_PRT_CDB(1, dbuf_p->serialNo, cdb);
}
/*
* Send a ping message to the queue & wait for a response...
* If we get a response after 2 seconds, there must be another
* daemon listening on this message queue.
*/
int check_for_running_daemons(int minor)
{
return 0;
}
/* Abort if string length > len */
void checkstrlen(char *s, int len)
{
if (strlen(s) > len) {
MHVTL_DBG(1, "String %s is > %d... Aborting", s, len);
printf("String %s longer than %d chars\n", s, len);
printf("Please fix config file\n");
abort();
}
}
int device_type_register(struct lu_phy_attr *lu, struct device_type_template *t)
{
lu->scsi_ops = t;
return 0;
}
uint8_t set_compression_mode_pg(struct list_head *l, int lvl)
{
struct mode *m;
uint8_t *p;
MHVTL_DBG(3, "*** Trace ***");
/* Find pointer to Data Compression mode Page */
m = lookup_pcode(l, MODE_DATA_COMPRESSION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
p = m->pcodePointer;
p[2] |= 0x80; /* Set data compression enable */
}
/* Find pointer to Device Configuration mode Page */
m = lookup_pcode(l, MODE_DEVICE_CONFIGURATION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
p = m->pcodePointer;
p[14] = lvl;
}
return SAM_STAT_GOOD;
}
uint8_t clear_compression_mode_pg(struct list_head *l)
{
struct mode *m;
uint8_t *p;
MHVTL_DBG(3, "*** Trace ***");
/* Find pointer to Data Compression mode Page */
m = lookup_pcode(l, MODE_DATA_COMPRESSION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
p = m->pcodePointer;
p[2] &= 0x7f; /* clear data compression enable */
}
/* Find pointer to Device Configuration mode Page */
m = lookup_pcode(l, MODE_DEVICE_CONFIGURATION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
p = m->pcodePointer;
p[14] = Z_NO_COMPRESSION;
}
return SAM_STAT_GOOD;
}
uint8_t clear_WORM(struct list_head *l)
{
uint8_t *smp_dp;
struct mode *m;
m = lookup_pcode(l, MODE_MEDIUM_CONFIGURATION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
smp_dp = m->pcodePointer;
if (!smp_dp)
return SAM_STAT_GOOD;
smp_dp[2] = 0x0;
}
return SAM_STAT_GOOD;
}
uint8_t set_WORM(struct list_head *l)
{
uint8_t *smp_dp;
struct mode *m;
MHVTL_DBG(3, "*** Trace ***");
m = lookup_pcode(l, MODE_MEDIUM_CONFIGURATION, 0);
MHVTL_DBG(3, "l: %p, m: %p, m->pcodePointer: %p",
l, m, m->pcodePointer);
if (m) {
smp_dp = m->pcodePointer;
if (!smp_dp)
return SAM_STAT_GOOD;
smp_dp[2] = 0x10;
smp_dp[4] = 0x01; /* Indicate label overwrite */
}
return SAM_STAT_GOOD;
}
/* Remove newline from string and fill rest of 'len' with char 'c' */
void rmnl(char *s, unsigned char c, int len)
{
int i;
int found = 0;
for (i = 0; i < len; i++) {
if (s[i] == '\n')
found = 1;
if (found)
s[i] = c;
}
}
void update_vpd_86(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xb0)];
uint8_t *worm;
worm = p;
*vpd_pg->data = (*worm) ? 1 : 0; /* Set WORM bit */
}
void update_vpd_b0(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xb0)];
uint8_t *worm;
worm = p;
*vpd_pg->data = (*worm) ? 1 : 0; /* Set WORM bit */
}
void update_vpd_b1(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xb1)];
memcpy(vpd_pg->data, p, vpd_pg->sz);
}
void update_vpd_b2(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xb2)];
memcpy(vpd_pg->data, p, vpd_pg->sz);
}
void update_vpd_c0(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xc0)];
memcpy(&vpd_pg->data[20], p, strlen(p));
}
void update_vpd_c1(struct lu_phy_attr *lu, void *p)
{
struct vpd *vpd_pg = lu->lu_vpd[PCODE_OFFSET(0xc1)];
memcpy(vpd_pg->data, p, vpd_pg->sz);
}
int add_density_support(struct list_head *l, struct density_info *di, int rw)
{
struct supported_density_list *supported;
supported = malloc(sizeof(struct supported_density_list));
if (!supported)
return -ENOMEM;
supported->density_info = di;
supported->rw = rw;
list_add_tail(&supported->siblings, l);
return 0;
}
void process_fifoname(struct lu_phy_attr *lu, char *s, int flag)
{
MHVTL_DBG(3, "entry: %s, flag: %d, existing name: %s",
s, flag, lu->fifoname);
if (lu->fifo_flag) /* fifo set via '-f <fifo>' switch */
return;
checkstrlen(s, MALLOC_SZ - 1);
if (lu->fifoname)
free(lu->fifoname);
lu->fifoname = malloc(strlen(s) + 2);
if (!lu->fifoname) {
printf("Unable to malloc fifo buffer");
exit(-ENOMEM);
}
lu->fifo_flag = flag;
/* Already checked for sane length */
strcpy(lu->fifoname, s);
}
/* Remove message queue */
void cleanup_msg(void)
{
int msqid;
int retval;
struct msqid_ds ds;
msqid = init_queue();
if (msqid < 0) {
MHVTL_LOG("Failed to open msg queue: %s", strerror(errno));
return;
}
retval = msgctl(msqid, IPC_RMID, &ds);
if (retval < 0) {
MHVTL_LOG("Failed to remove msg queue: %s", strerror(errno));
} else {
MHVTL_DBG(2, "Removed ipc resources");
}
}
#define QUERYSHM 0
#define INCSHM 1
#define DECSHM 2
/* max number of unique 'fifo path names */
#define MAX_SLOTS 128
/* Assume enough space for each 'fifo path name' to be 120 chars or less */
#define SHM_SIZE MAX_SLOTS * 128
static int mhvtl_shared_mem(char *path, int flag)
{
struct fifo_count {
int consumers;
int path_offset;
};
int mhvtl_shm;
int retval = -1;
int i;
int base_offset;
key_t key;
struct fifo_count *base;
struct shmid_ds buf;
char *fifo_path;
char *n_path;
char magic_string[] = "mhVTL";
/* Default to hard coded value */
if (path == NULL)
n_path = &magic_string[0];
else
n_path = path;
key = 0x4d61726b;
i = 0;
mhvtl_shm = shmget(key, SHM_SIZE, IPC_CREAT|0666);
if (mhvtl_shm < 0) {
printf("Attempt to get Shared memory failed\n");
MHVTL_LOG("Attempt to get shared memory failed");
return -ENOMEM;
}
base = shmat(mhvtl_shm, NULL, 0);
if (base == (void *) -1) {
MHVTL_LOG("Failed to attach to shm: %s", strerror(errno));
return -1;
}
base_offset = sizeof(struct fifo_count) * MAX_SLOTS;
fifo_path = (void *)base + base_offset;
i = 0;
if (!base[i].path_offset) { /* index '0' special global counter */
base[i].path_offset = base_offset;
strcpy(fifo_path, magic_string);
}
/* Walk array looking for a string match or an empty slot */
for (i = 0; i < MAX_SLOTS; i++) {
if (base[i].path_offset) {
fifo_path = (void *)base + base[i].path_offset;
MHVTL_DBG(3, "Index %d in use, offset: %d, "
"path: %p (%s), count: %d",
i, base_offset, fifo_path, fifo_path,
base[i].consumers);
base_offset += strlen(fifo_path) + 1;
} else { /* Empty 'slot' */
base[i].path_offset = base_offset;
fifo_path = (void *)base + base_offset;
strcpy(fifo_path, n_path);
MHVTL_DBG(3, "Index %d unused, offset %d, "
"Addr %p, Added path: %s",