forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapedev.c
3946 lines (3345 loc) · 136 KB
/
tapedev.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
/* TAPEDEV.C (c) Copyright Roger Bowler, 1999-2001 */
/* ESA/390 Tape Device Handler */
/*-------------------------------------------------------------------*/
/* This module contains device handling functions for emulated */
/* 3420 magnetic tape devices for the Hercules ESA/390 emulator. */
/* */
/* Three emulated tape formats are supported: */
/* 1. AWSTAPE This is the format used by the P/390. */
/* The entire tape is contained in a single flat file. */
/* Each tape block is preceded by a 6-byte header. */
/* Files are separated by tapemarks, which consist */
/* of headers with zero block length. */
/* AWSTAPE files are readable and writable. */
/* 2. OMATAPE This is the Optical Media Attach device format. */
/* Each physical file on the tape is represented by */
/* a separate flat file. The collection of files that */
/* make up the physical tape is obtained from an ASCII */
/* text file called the "tape description file", whose */
/* file name is always tapes/xxxxxx.tdf (where xxxxxx */
/* is the volume serial number of the tape). */
/* Three formats of tape files are supported: */
/* * FIXED files contain fixed length EBCDIC blocks */
/* with no headers or delimiters. The block length */
/* is specified in the TDF file. */
/* * TEXT files contain variable length ASCII blocks */
/* delimited by carriage return line feed sequences. */
/* The data is translated to EBCDIC by this module. */
/* * HEADER files contain variable length blocks of */
/* EBCDIC data prefixed by a 16-byte header. */
/* The TDF file and all of the tape files must reside */
/* reside under the same directory which is normally */
/* on CDROM but can be on disk. */
/* OMATAPE files are supported as read-only media. */
/* 3. SCSITAPE This format allows reading and writing of 4mm or */
/* 8mm DAT tape, 9-track open-reel tape, or 3480-type */
/* cartridge on an appropriate SCSI-attached drive. */
/* All SCSI tapes are processed using the generalized */
/* SCSI tape driver (st.c) which is controlled using */
/* the MTIOCxxx set of IOCTL commands. */
/* 4. HET This format is based on the AWSTAPE format but has */
/* been extended to support compression. Since the */
/* basic file format has remained the same, AWSTAPEs */
/* can be read/written using the HET routines. */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Additional credits: */
/* 3480 commands contributed by Jan Jaeger */
/* Sense byte improvements by Jan Jaeger */
/* 3480 Read Block ID and Locate CCWs by Brandon Hill */
/* Unloaded tape support by Brandon Hill v209*/
/* HET format support by Leland Lucius v209*/
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Reference information for OMA file formats: */
/* SC53-1200 S/370 and S/390 Optical Media Attach/2 User's Guide */
/* SC53-1201 S/370 and S/390 Optical Media Attach/2 Technical Ref */
/*-------------------------------------------------------------------*/
#include "hercules.h"
#include "parser.h"
/*-------------------------------------------------------------------*/
/* Internal macro definitions */
/*-------------------------------------------------------------------*/
#define MAX_BLKLEN 65530 /* Maximum I/O buffer size */
#define TAPE_UNLOADED "*" /* Name for unloaded drive */
/*-------------------------------------------------------------------*/
/* Definitions for 3420/3480 sense bytes */
/*-------------------------------------------------------------------*/
#define SENSE1_TAPE_NOISE 0x80 /* Noise */
#define SENSE1_TAPE_TUA 0x40 /* TU Status A (ready) */
#define SENSE1_TAPE_TUB 0x20 /* TU Status B (not ready) */
#define SENSE1_TAPE_7TRK 0x10 /* 7-track feature */
#define SENSE1_TAPE_LOADPT 0x08 /* Tape is at load point */
#define SENSE1_TAPE_WRT 0x04 /* Tape is in write status */
#define SENSE1_TAPE_FP 0x02 /* File protect status */
#define SENSE1_TAPE_NCA 0x01 /* Not capable */
#define SENSE4_TAPE_EOT 0x20 /* Tape indicate (EOT) */
#define SENSE5_TAPE_SRDCHK 0x08 /* Start read check */
#define SENSE5_TAPE_PARTREC 0x04 /* Partial record */
#define SENSE7_TAPE_LOADFAIL 0x01 /* Load failure */
/*-------------------------------------------------------------------*/
/* Definitions for 3480 commands */
/*-------------------------------------------------------------------*/
/* Format control byte for Load Display command */
#define FCB_FS 0xE0 /* Format control bits... */
#define FCB_FS_NODISP 0x60 /* Do not display messages */
#define FCB_AM 0x10 /* Alternate messages */
#define FCB_BM 0x80 /* Blinking message */
#define FCB_DM 0x40 /* Display low/high message */
/* Path state byte for Sense Path Group ID command */
#define SPG_PATHSTAT 0xC0 /* Pathing status bits... */
#define SPG_PATHSTAT_RESET 0x00 /* ...reset */
#define SPG_PATHSTAT_RESV 0x40 /* ...reserved bit setting */
#define SPG_PATHSTAT_UNGROUPED 0x80 /* ...ungrouped */
#define SPG_PATHSTAT_GROUPED 0xC0 /* ...grouped */
#define SPG_PARTSTAT 0x30 /* Partitioning status bits..*/
#define SPG_PARTSTAT_IENABLED 0x00 /* ...implicitly enabled */
#define SPG_PARTSTAT_RESV 0x10 /* ...reserved bit setting */
#define SPG_PARTSTAT_DISABLED 0x20 /* ...disabled */
#define SPG_PARTSTAT_XENABLED 0x30 /* ...explicitly enabled */
#define SPG_PATHMODE 0x08 /* Path mode bit... */
#define SPG_PATHMODE_SINGLE 0x00 /* ...single path mode */
#define SPG_PATHMODE_RESV 0x08 /* ...reserved bit setting */
#define SPG_RESERVED 0x07 /* Reserved bits, must be 0 */
/* Function control byte for Set Path Group ID command */
#define SPG_SET_MULTIPATH 0x80 /* Set multipath mode */
#define SPG_SET_COMMAND 0x60 /* Set path command bits... */
#define SPG_SET_ESTABLISH 0x00 /* ...establish group */
#define SPG_SET_DISBAND 0x20 /* ...disband group */
#define SPG_SET_RESIGN 0x40 /* ...resign from group */
#define SPG_SET_COMMAND_RESV 0x60 /* ...reserved bit setting */
#define SPG_SET_RESV 0x1F /* Reserved bits, must be 0 */
/*-------------------------------------------------------------------*/
/* Definitions for tape device type field in device block */
/*-------------------------------------------------------------------*/
#define TAPEDEVT_AWSTAPE 1 /* AWSTAPE format disk file */
#define TAPEDEVT_OMATAPE 2 /* OMATAPE format disk files */
#define TAPEDEVT_SCSITAPE 3 /* Physical SCSI tape */
#define TAPEDEVT_HET 4 /* HET format disk file */
/*-------------------------------------------------------------------*/
/* Structure definition for tape block headers */
/*-------------------------------------------------------------------*/
/*
* The integer fields in the HET, AWSTAPE and OMATAPE headers are
* encoded in the Intel format (i.e. the bytes of the integer are held
* in reverse order). For this reason the integers are defined as byte
* arrays, and the bytes are fetched individually in order to make
* the code portable across architectures which use either the Intel
* format or the S/370 format.
*
* Block length fields contain the length of the emulated tape block
* and do not include the length of the header.
*
* For the AWSTAPE and HET formats:
* - the first block has a previous block length of zero
* - a tapemark is indicated by a header with a block length of zero
* and a flag byte of X'40'
*
* For the OMATAPE format:
* - the first block has a previous header offset of X'FFFFFFFF'
* - a tapemark is indicated by a header with a block length of
* X'FFFFFFFF'
* - each block is followed by padding bytes if necessary to ensure
* that the next header starts on a 16-byte boundary
*
*/
typedef struct _AWSTAPE_BLKHDR {
HWORD curblkl; /* Length of this block */
HWORD prvblkl; /* Length of previous block */
BYTE flags1; /* Flags byte 1 */
BYTE flags2; /* Flags byte 2 */
} AWSTAPE_BLKHDR;
/* Definitions for AWSTAPE_BLKHDR flags byte 1 */
#define AWSTAPE_FLAG1_NEWREC 0x80 /* Start of new record */
#define AWSTAPE_FLAG1_TAPEMARK 0x40 /* Tape mark */
#define AWSTAPE_FLAG1_ENDREC 0x20 /* End of record */
typedef struct _OMATAPE_BLKHDR {
FWORD curblkl; /* Length of this block */
FWORD prvhdro; /* Offset of previous block
header from start of file */
FWORD omaid; /* OMA identifier (contains
ASCII characters "@HDF") */
FWORD resv; /* Reserved */
} OMATAPE_BLKHDR;
/*-------------------------------------------------------------------*/
/* Structure definition for OMA tape descriptor array */
/*-------------------------------------------------------------------*/
typedef struct _OMATAPE_DESC {
BYTE filename[256]; /* Filename of data file */
BYTE format; /* H=HEADERS,T=TEXT,F=FIXED */
BYTE resv; /* Reserved for alignment */
U16 blklen; /* Fixed block length */
} OMATAPE_DESC;
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static struct mt_tape_info tapeinfo[] = MT_TAPE_INFO;
static struct mt_tape_info densinfo[] = {
{0x01, "NRZI (800 bpi)"},
{0x02, "PE (1600 bpi)"},
{0x03, "GCR (6250 bpi)"},
{0x05, "QIC-45/60 (GCR, 8000 bpi)"},
{0x06, "PE (3200 bpi)"},
{0x07, "IMFM (6400 bpi)"},
{0x08, "GCR (8000 bpi)"},
{0x09, "GCR /37871 bpi)"},
{0x0A, "MFM (6667 bpi)"},
{0x0B, "PE (1600 bpi)"},
{0x0C, "GCR (12960 bpi)"},
{0x0D, "GCR (25380 bpi)"},
{0x0F, "QIC-120 (GCR 10000 bpi)"},
{0x10, "QIC-150/250 (GCR 10000 bpi)"},
{0x11, "QIC-320/525 (GCR 16000 bpi)"},
{0x12, "QIC-1350 (RLL 51667 bpi)"},
{0x13, "DDS (61000 bpi)"},
{0x14, "EXB-8200 (RLL 43245 bpi)"},
{0x15, "EXB-8500 (RLL 45434 bpi)"},
{0x16, "MFM 10000 bpi"},
{0x17, "MFM 42500 bpi"},
{0x24, "DDS-2"},
{0x8C, "EXB-8505 compressed"},
{0x90, "EXB-8205 compressed"},
{0, NULL}};
static PARSER ptab[] =
{
{ "awstape", NULL },
{ "idrc", "%d" },
{ "compress", "%d" },
{ "method", "%d" },
{ "level", "%d" },
{ "chunksize", "%d" },
{ NULL, NULL },
};
enum
{
TDPARM_NONE,
TDPARM_AWSTAPE,
TDPARM_IDRC,
TDPARM_COMPRESS,
TDPARM_METHOD,
TDPARM_LEVEL,
TDPARM_CHKSIZE,
};
/*-------------------------------------------------------------------*/
/* Open an AWSTAPE format file */
/* */
/* If successful, the file descriptor is stored in the device block */
/* and the return value is zero. Otherwise the return value is -1. */
/*-------------------------------------------------------------------*/
static int open_awstape (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
/* Check for no tape in drive */
if (!strcmp (dev->filename, TAPE_UNLOADED))
{
dev->sense[0] = SENSE_IR;
dev->sense[1] = SENSE1_TAPE_TUB;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Open the AWSTAPE file */
rc = open (dev->filename, O_RDWR | O_BINARY);
/* If file is read-only, attempt to open again */
if (rc < 0 && (errno == EROFS || errno == EACCES))
{
dev->readonly = 1;
rc = open (dev->filename, O_RDONLY | O_BINARY);
}
/* Check for successful open */
if (rc < 0)
{
logmsg ("HHC201I Error opening %s: %s\n",
dev->filename, strerror(errno));
if (dev->tapedevt == TAPEDEVT_AWSTAPE)
strcpy(dev->filename, TAPE_UNLOADED);
dev->sense[0] = SENSE_IR;
dev->sense[1] = SENSE1_TAPE_TUB;
dev->sense[7] = SENSE7_TAPE_LOADFAIL;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Store the file descriptor in the device block */
dev->fd = rc;
return 0;
} /* end function open_awstape */
/*-------------------------------------------------------------------*/
/* Read an AWSTAPE block header */
/* */
/* If successful, return value is zero, and buffer contains header. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int readhdr_awstape (DEVBLK *dev, long blkpos,
AWSTAPE_BLKHDR *buf, BYTE *unitstat)
{
int rc; /* Return code */
/* Reposition file to the requested block header */
rc = lseek (dev->fd, blkpos, SEEK_SET);
if (rc < 0)
{
/* Handle seek error condition */
logmsg ("HHC202I Error seeking to offset %8.8lX "
"in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Read the 6-byte block header */
rc = read (dev->fd, buf, sizeof(AWSTAPE_BLKHDR));
/* Handle read error condition */
if (rc < 0)
{
logmsg ("HHC203I Error reading block header "
"at offset %8.8lX in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Handle end of file (uninitialized tape) condition */
if (rc == 0)
{
logmsg ("HHC203I End of file (uninitialized tape) "
"at offset %8.8lX in file %s\n",
blkpos, dev->filename);
/* Set unit exception with tape indicate (end of tape) */
dev->sense[4] = SENSE4_TAPE_EOT;
*unitstat = CSW_CE | CSW_DE | CSW_UX;
return -1;
}
/* Handle end of file within block header */
if (rc < sizeof(AWSTAPE_BLKHDR))
{
logmsg ("HHC204I Unexpected end of file in block header "
"at offset %8.8lX in file %s\n",
blkpos, dev->filename);
/* Set unit check with data check and partial record */
dev->sense[0] = SENSE_DC;
dev->sense[1] = SENSE1_TAPE_NOISE;
dev->sense[5] = SENSE5_TAPE_PARTREC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Successful return */
return 0;
} /* end function readhdr_awstape */
/*-------------------------------------------------------------------*/
/* Read a block from an AWSTAPE format file */
/* */
/* If successful, return value is block length read. */
/* If a tapemark was read, the return value is zero, and the */
/* current file number in the device block is incremented. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int read_awstape (DEVBLK *dev, BYTE *buf, BYTE *unitstat)
{
int rc; /* Return code */
AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */
long blkpos; /* Offset of block header */
U16 blklen; /* Data length of block */
/* Initialize current block position */
blkpos = dev->nxtblkpos;
/* Read the 6-byte block header */
rc = readhdr_awstape (dev, blkpos, &awshdr, unitstat);
if (rc < 0) return -1;
/* Extract the block length from the block header */
blklen = ((U16)(awshdr.curblkl[1]) << 8)
| awshdr.curblkl[0];
/* Calculate the offsets of the next and previous blocks */
dev->nxtblkpos = blkpos + sizeof(awshdr) + blklen;
dev->prvblkpos = blkpos;
/* Increment file number and return zero if tapemark was read */
if (blklen == 0)
{
dev->curfilen++;
return 0;
}
/* Read data block from tape file */
rc = read (dev->fd, buf, blklen);
/* Handle read error condition */
if (rc < 0)
{
logmsg ("HHC205I Error reading data block "
"at offset %8.8lX in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Handle end of file within data block */
if (rc < blklen)
{
logmsg ("HHC206I Unexpected end of file in data block "
"at offset %8.8lX in file %s\n",
blkpos, dev->filename);
/* Set unit check with data check and partial record */
dev->sense[0] = SENSE_DC;
dev->sense[1] = SENSE1_TAPE_NOISE;
dev->sense[5] = SENSE5_TAPE_PARTREC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Return block length */
return blklen;
} /* end function read_awstape */
/*-------------------------------------------------------------------*/
/* Write a block to an AWSTAPE format file */
/* */
/* If successful, return value is zero. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int write_awstape (DEVBLK *dev, BYTE *buf, U16 blklen,
BYTE *unitstat)
{
int rc; /* Return code */
AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */
long blkpos; /* Offset of block header */
U16 prvblkl; /* Length of previous block */
/* Initialize current block position and previous block length */
blkpos = dev->nxtblkpos;
prvblkl = 0;
/* Determine previous block length if not at start of tape */
if (dev->nxtblkpos > 0)
{
/* Reread the previous block header */
rc = readhdr_awstape (dev, dev->prvblkpos, &awshdr, unitstat);
if (rc < 0) return -1;
/* Extract the block length from the block header */
prvblkl = ((U16)(awshdr.curblkl[1]) << 8)
| awshdr.curblkl[0];
/* Recalculate the offset of the next block */
blkpos = dev->prvblkpos + sizeof(awshdr) + prvblkl;
}
/* Reposition file to the new block header */
rc = lseek (dev->fd, blkpos, SEEK_SET);
if (rc < 0)
{
/* Handle seek error condition */
logmsg ("HHC207I Error seeking to offset %8.8lX "
"in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Build the 6-byte block header */
awshdr.curblkl[0] = blklen & 0xFF;
awshdr.curblkl[1] = (blklen >> 8) & 0xFF;
awshdr.prvblkl[0] = prvblkl & 0xFF;
awshdr.prvblkl[1] = (prvblkl >>8) & 0xFF;
awshdr.flags1 = AWSTAPE_FLAG1_NEWREC | AWSTAPE_FLAG1_ENDREC;
awshdr.flags2 = 0;
/* Write the block header */
rc = write (dev->fd, &awshdr, sizeof(awshdr));
if (rc < sizeof(awshdr))
{
/* Handle write error condition */
logmsg ("HHC208I Error writing block header "
"at offset %8.8lX in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Calculate the offsets of the next and previous blocks */
dev->nxtblkpos = blkpos + sizeof(awshdr) + blklen;
dev->prvblkpos = blkpos;
/* Write the data block */
rc = write (dev->fd, buf, blklen);
if (rc < blklen)
{
/* Handle write error condition */
logmsg ("HHC209I Error writing data block "
"at offset %8.8lX in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Return normal status */
return 0;
} /* end function write_awstape */
/*-------------------------------------------------------------------*/
/* Write a tapemark to an AWSTAPE format file */
/* */
/* If successful, return value is zero. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int write_awsmark (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */
long blkpos; /* Offset of block header */
U16 prvblkl; /* Length of previous block */
/* Initialize current block position and previous block length */
blkpos = dev->nxtblkpos;
prvblkl = 0;
/* Determine previous block length if not at start of tape */
if (dev->nxtblkpos > 0)
{
/* Reread the previous block header */
rc = readhdr_awstape (dev, dev->prvblkpos, &awshdr, unitstat);
if (rc < 0) return -1;
/* Extract the block length from the block header */
prvblkl = ((U16)(awshdr.curblkl[1]) << 8)
| awshdr.curblkl[0];
/* Recalculate the offset of the next block */
blkpos = dev->prvblkpos + sizeof(awshdr) + prvblkl;
}
/* Reposition file to the new block header */
rc = lseek (dev->fd, blkpos, SEEK_SET);
if (rc < 0)
{
/* Handle seek error condition */
logmsg ("HHC210I Error seeking to offset %8.8lX "
"in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Build the 6-byte block header */
awshdr.curblkl[0] = 0;
awshdr.curblkl[1] = 0;
awshdr.prvblkl[0] = prvblkl & 0xFF;
awshdr.prvblkl[1] = (prvblkl >>8) & 0xFF;
awshdr.flags1 = AWSTAPE_FLAG1_TAPEMARK;
awshdr.flags2 = 0;
/* Write the block header */
rc = write (dev->fd, &awshdr, sizeof(awshdr));
if (rc < sizeof(awshdr))
{
/* Handle write error condition */
logmsg ("HHC211I Error writing block header "
"at offset %8.8lX in file %s: %s\n",
blkpos, dev->filename, strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Calculate the offsets of the next and previous blocks */
dev->nxtblkpos = blkpos + sizeof(awshdr);
dev->prvblkpos = blkpos;
/* Return normal status */
return 0;
} /* end function write_awsmark */
/*-------------------------------------------------------------------*/
/* Forward space over next block of AWSTAPE format file */
/* */
/* If successful, return value is the length of the block skipped. */
/* If the block skipped was a tapemark, the return value is zero, */
/* and the current file number in the device block is incremented. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int fsb_awstape (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */
long blkpos; /* Offset of block header */
U16 blklen; /* Data length of block */
/* Initialize current block position */
blkpos = dev->nxtblkpos;
/* Read the 6-byte block header */
rc = readhdr_awstape (dev, blkpos, &awshdr, unitstat);
if (rc < 0) return -1;
/* Extract the block length from the block header */
blklen = ((U16)(awshdr.curblkl[1]) << 8)
| awshdr.curblkl[0];
/* Calculate the offsets of the next and previous blocks */
dev->nxtblkpos = blkpos + sizeof(awshdr) + blklen;
dev->prvblkpos = blkpos;
/* Increment current file number if tapemark was skipped */
if (blklen == 0)
dev->curfilen++;
dev->blockid++;
/* Return block length or zero if tapemark */
return blklen;
} /* end function fsb_awstape */
/*-------------------------------------------------------------------*/
/* Backspace to previous block of AWSTAPE format file */
/* */
/* If successful, return value is the length of the block. */
/* If the block is a tapemark, the return value is zero, */
/* and the current file number in the device block is decremented. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int bsb_awstape (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
AWSTAPE_BLKHDR awshdr; /* AWSTAPE block header */
U16 curblkl; /* Length of current block */
U16 prvblkl; /* Length of previous block */
long blkpos; /* Offset of block header */
/* Unit check if already at start of tape */
if (dev->nxtblkpos == 0)
{
dev->sense[0] = 0;
dev->sense[1] = SENSE1_TAPE_LOADPT;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Backspace to previous block position */
blkpos = dev->prvblkpos;
/* Read the 6-byte block header */
rc = readhdr_awstape (dev, blkpos, &awshdr, unitstat);
if (rc < 0) return -1;
/* Extract the block lengths from the block header */
curblkl = ((U16)(awshdr.curblkl[1]) << 8)
| awshdr.curblkl[0];
prvblkl = ((U16)(awshdr.prvblkl[1]) << 8)
| awshdr.prvblkl[0];
/* Calculate the offset of the previous block */
dev->prvblkpos = blkpos - sizeof(awshdr) - prvblkl;
dev->nxtblkpos = blkpos;
/* Decrement current file number if backspaced over tapemark */
if (curblkl == 0)
dev->curfilen--;
dev->blockid--;
/* Return block length or zero if tapemark */
return curblkl;
} /* end function bsb_awstape */
/*-------------------------------------------------------------------*/
/* Forward space to next logical file of AWSTAPE format file */
/* */
/* For AWSTAPE files, the forward space file operation is achieved */
/* by forward spacing blocks until positioned just after a tapemark. */
/* */
/* If successful, return value is zero, and the current file number */
/* in the device block is incremented by fsb_awstape. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int fsf_awstape (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
while (1)
{
/* Forward space over next block */
rc = fsb_awstape (dev, unitstat);
if (rc < 0) return -1;
/* Exit loop if spaced over a tapemark */
if (rc == 0) break;
} /* end while */
/* Return normal status */
return 0;
} /* end function fsf_awstape */
/*-------------------------------------------------------------------*/
/* Backspace to previous logical file of AWSTAPE format file */
/* */
/* For AWSTAPE files, the backspace file operation is achieved */
/* by backspacing blocks until positioned just before a tapemark */
/* or until positioned at start of tape. */
/* */
/* If successful, return value is zero, and the current file number */
/* in the device block is decremented by bsb_awstape. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int bsf_awstape (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
while (1)
{
/* Exit if now at start of tape */
if (dev->nxtblkpos == 0)
break;
/* Backspace to previous block position */
rc = bsb_awstape (dev, unitstat);
if (rc < 0) return -1;
/* Exit loop if backspaced over a tapemark */
if (rc == 0) break;
} /* end while */
/* Return normal status */
return 0;
} /* end function bsf_awstape */
/*-------------------------------------------------------------------*/
/* Open an HET format file */
/* */
/* If successful, the het control blk is stored in the device block */
/* and the return value is zero. Otherwise the return value is -1. */
/*-------------------------------------------------------------------*/
static int open_het (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
/* Open the HET file */
rc = het_open (&dev->hetb, dev->filename, HETOPEN_CREATE );
if (rc >= 0)
{
rc = het_cntl (dev->hetb,
HETCNTL_SET | HETCNTL_COMPRESS,
dev->tdparms.compress);
if (rc >= 0)
{
rc = het_cntl (dev->hetb,
HETCNTL_SET | HETCNTL_METHOD,
dev->tdparms.method);
if (rc >= 0)
{
rc = het_cntl (dev->hetb,
HETCNTL_SET | HETCNTL_LEVEL,
dev->tdparms.level);
if (rc < 0)
{
rc = het_cntl (dev->hetb,
HETCNTL_SET | HETCNTL_CHUNKSIZE,
dev->tdparms.chksize);
}
}
}
}
/* Check for successful open */
if (rc < 0)
{
het_close (&dev->hetb);
logmsg ("HHC290I Error opening %s: %s(%s)\n",
dev->filename, het_error(rc), strerror(errno));
dev->sense[0] = SENSE_IR;
dev->sense[1] = SENSE1_TAPE_TUB;
dev->sense[7] = SENSE7_TAPE_LOADFAIL;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Indicate file opened */
dev->fd = 1;
return 0;
} /* end function open_het */
/*-------------------------------------------------------------------*/
/* Close an HET format file */
/* */
/* The HET file is close and all device block fields reinitialized. */
/*-------------------------------------------------------------------*/
static void close_het (DEVBLK *dev)
{
/* Close the HET file */
het_close (&dev->hetb);
/* Reinitialize the DEV fields */
dev->fd = -1;
return;
} /* end function close_het */
/*-------------------------------------------------------------------*/
/* Read a block from an HET format file */
/* */
/* If successful, return value is block length read. */
/* If a tapemark was read, the return value is zero, and the */
/* current file number in the device block is incremented. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int read_het (DEVBLK *dev, BYTE *buf, BYTE *unitstat)
{
int rc; /* Return code */
rc = het_read (dev->hetb, buf);
if (rc < 0)
{
/* Increment file number and return zero if tapemark was read */
if (rc == HETE_TAPEMARK)
{
dev->curfilen++;
return 0;
}
/* Handle end of file (uninitialized tape) condition */
if (rc == HETE_EOT)
{
logmsg ("HHC203I End of file (uninitialized tape) "
"at block %8.8X in file %s\n",
dev->hetb->cblk, dev->filename);
/* Set unit exception with tape indicate (end of tape) */
dev->sense[4] = SENSE4_TAPE_EOT;
*unitstat = CSW_CE | CSW_DE | CSW_UX;
return -1;
}
logmsg ("HHC205I Error reading data block "
"at block %8.8X in file %s: %s(%s)\n",
dev->hetb->cblk, dev->filename,
het_error(rc), strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Return block length */
return rc;
} /* end function read_het */
/*-------------------------------------------------------------------*/
/* Write a block to an HET format file */
/* */
/* If successful, return value is zero. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int write_het (DEVBLK *dev, BYTE *buf, U16 blklen,
BYTE *unitstat)
{
int rc; /* Return code */
/* Write the data block */
rc = het_write (dev->hetb, buf, blklen);
if (rc < 0)
{
/* Handle write error condition */
logmsg ("HHC209I Error writing data block "
"at block %8.8X in file %s: %s(%s)\n",
dev->hetb->cblk, dev->filename,
het_error(rc), strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Return normal status */
return 0;
} /* end function write_het */
/*-------------------------------------------------------------------*/
/* Write a tapemark to an HET format file */
/* */
/* If successful, return value is zero. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int write_hetmark (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
/* Write the tape mark */
rc = het_tapemark (dev->hetb);
if (rc < 0)
{
/* Handle error condition */
logmsg ("HHC211I Error writing tape mark "
"at block %8.8X in file %s: %s(%s)\n",
dev->hetb->cblk, dev->filename,
het_error(rc), strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
return -1;
}
/* Return normal status */
return 0;
} /* end function write_hetmark */
/*-------------------------------------------------------------------*/
/* Forward space over next block of an HET format file */
/* */
/* If successful, return value +1. */
/* If the block skipped was a tapemark, the return value is zero, */
/* and the current file number in the device block is incremented. */
/* If error, return value is -1 and unitstat is set to CE+DE+UC */
/*-------------------------------------------------------------------*/
static int fsb_het (DEVBLK *dev, BYTE *unitstat)
{
int rc; /* Return code */
/* Forward space one block */
rc = het_fsb (dev->hetb);
if (rc < 0)
{
/* Increment file number and return zero if tapemark was read */
if (rc == HETE_TAPEMARK)
{
dev->blockid++;
dev->curfilen++;
return 0;
}
logmsg ("HHC205I Error forward spacing "
"at block %8.8X in file %s: %s(%s)\n",
dev->hetb->cblk, dev->filename,
het_error(rc), strerror(errno));
/* Set unit check with equipment check */
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;