-
Notifications
You must be signed in to change notification settings - Fork 41
/
dasdutil.c
2239 lines (1978 loc) · 84.7 KB
/
dasdutil.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
/* DASDUTIL.C (c) Copyright Roger Bowler, 1999-2010 */
/* Hercules DASD Utilities: Common subroutines */
/*-------------------------------------------------------------------*/
/* This module contains common subroutines used by DASD utilities */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _DASDUTIL_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "dasdblks.h"
#include "devtype.h"
#include "opcode.h"
/*-------------------------------------------------------------------*/
/* External references (defined in ckddasd.c) */
/*-------------------------------------------------------------------*/
extern DEVHND ckddasd_device_hndinfo;
/*-------------------------------------------------------------------*/
/* Internal macro definitions */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static int verbose = 0; /* Be chatty about reads etc. */
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static BYTE iplpsw[8] = {0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x0F};
static BYTE iplccw1[8] = {0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
static BYTE iplccw2[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static int nextnum = 0;
#if 0
SYSBLK sysblk; /* Currently required for shared.c */
#endif
/*-------------------------------------------------------------------*/
/* Subroutine to convert a null-terminated string to upper case */
/*-------------------------------------------------------------------*/
DLL_EXPORT void string_to_upper (char *source)
{
int i; /* Array subscript */
for (i = 0; source[i] != '\0'; i++)
source[i] = toupper(source[i]);
} /* end function string_to_upper */
/*-------------------------------------------------------------------*/
/* Subroutine to convert a null-terminated string to lower case */
/*-------------------------------------------------------------------*/
DLL_EXPORT void string_to_lower (char *source)
{
int i; /* Array subscript */
for (i = 0; source[i] != '\0'; i++)
source[i] = tolower(source[i]);
} /* end function string_to_lower */
/*-------------------------------------------------------------------*/
/* Subroutine to convert a string to EBCDIC and pad with blanks */
/*-------------------------------------------------------------------*/
DLL_EXPORT void convert_to_ebcdic (BYTE *dest, int len, char *source)
{
int i; /* Array subscript */
set_codepage(NULL);
for (i = 0; i < len && source[i] != '\0'; i++)
dest[i] = host_to_guest(source[i]);
while (i < len)
dest[i++] = 0x40;
} /* end function convert_to_ebcdic */
/*-------------------------------------------------------------------*/
/* Subroutine to convert an EBCDIC string to an ASCIIZ string. */
/* Removes trailing blanks and adds a terminating null. */
/* Returns the length of the ASCII string excluding terminating null */
/*-------------------------------------------------------------------*/
DLL_EXPORT int make_asciiz (char *dest, int destlen, BYTE *src, int srclen)
{
int len; /* Result length */
set_codepage(NULL);
for (len=0; len < srclen && len < destlen-1; len++)
dest[len] = guest_to_host(src[len]);
while (len > 0 && dest[len-1] == SPACE) len--;
dest[len] = '\0';
return len;
} /* end function make_asciiz */
/*-------------------------------------------------------------------*/
/* Subroutine to print a data block in hex and character format. */
/*-------------------------------------------------------------------*/
DLL_EXPORT void data_dump ( void *addr, int len )
{
unsigned int maxlen = 2048;
unsigned int i, xi, offset, startoff = 0;
BYTE c;
BYTE *pchar;
char print_chars[17];
char hex_chars[64];
char prev_hex[64] = "";
int firstsame = 0;
int lastsame = 0;
set_codepage(NULL);
pchar = (BYTE *)addr;
for (offset=0; ; )
{
if (offset >= maxlen && offset <= len - maxlen)
{
offset += 16;
pchar += 16;
prev_hex[0] = '\0';
continue;
}
if ( offset > 0 )
{
if ( strcmp ( hex_chars, prev_hex ) == 0 )
{
if ( firstsame == 0 ) firstsame = startoff;
lastsame = startoff;
}
else
{
if ( firstsame != 0 )
{
if ( lastsame == firstsame )
printf ("Line %4.4X same as above\n",
firstsame );
else
printf ("Lines %4.4X to %4.4X same as above\n",
firstsame, lastsame );
firstsame = lastsame = 0;
}
printf ("+%4.4X %s %s\n",
startoff, hex_chars, print_chars);
strcpy ( prev_hex, hex_chars );
}
}
if ( offset >= (U32)len ) break;
memset ( print_chars, 0, sizeof(print_chars) );
memset ( hex_chars, SPACE, sizeof(hex_chars) );
startoff = offset;
for (xi=0, i=0; i < 16; i++)
{
c = *pchar++;
if (offset < (U32)len) {
sprintf(hex_chars+xi, "%2.2X", c);
print_chars[i] = '.';
if (isprint(c)) print_chars[i] = c;
c = guest_to_host(c);
if (isprint(c)) print_chars[i] = c;
}
offset++;
xi += 2;
hex_chars[xi] = SPACE;
if ((offset & 3) == 0) xi++;
} /* end for(i) */
hex_chars[xi] = '\0';
} /* end for(offset) */
} /* end function data_dump */
/*-------------------------------------------------------------------*/
/* Subroutine to read a track from the CKD DASD image */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* cyl Cylinder number */
/* head Head number */
/* Output: */
/* The track is read into trkbuf, and curcyl and curhead */
/* are set to the cylinder and head number. */
/* */
/* Return value is 0 if successful, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int read_track (CIFBLK *cif, int cyl, int head)
{
int rc; /* Return code */
int trk; /* Track number */
DEVBLK *dev; /* -> CKD device block */
BYTE unitstat; /* Unit status */
/* Exit if required track is already in buffer */
if (cif->curcyl == cyl && cif->curhead == head)
return 0;
dev = &cif->devblk;
if (cif->trkmodif)
{
cif->trkmodif = 0;
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU001I Updating cyl %d head %d\n"),
cif->curcyl, cif->curhead);
trk = (cif->curcyl * cif->heads) + cif->curhead;
rc = (dev->hnd->write)(dev, trk, 0, NULL, cif->trksz, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU002E %s write track error: stat=%2.2X\n"),
cif->fname, unitstat);
return -1;
}
}
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU003I Reading cyl %d head %d\n"), cyl, head);
trk = (cyl * cif->heads) + head;
rc = (dev->hnd->read)(dev, trk, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU004E %s read track error: stat=%2.2X\n"),
cif->fname, unitstat);
return -1;
}
/* Set current buf, cylinder and head */
cif->trkbuf = dev->buf;
cif->curcyl = cyl;
cif->curhead = head;
return 0;
} /* end function read_track */
/*-------------------------------------------------------------------*/
/* Subroutine to read a block from the CKD DASD image */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* cyl Cylinder number of requested block */
/* head Head number of requested block */
/* rec Record number of requested block */
/* Output: */
/* keyptr Pointer to record key */
/* keylen Actual key length */
/* dataptr Pointer to record data */
/* datalen Actual data length */
/* */
/* Return value is 0 if successful, +1 if end of track, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int read_block (CIFBLK *cif, int cyl, int head, int rec, BYTE **keyptr,
int *keylen, BYTE **dataptr, int *datalen)
{
int rc; /* Return code */
BYTE *ptr; /* -> byte in track buffer */
CKDDASD_RECHDR *rechdr; /* -> Record header */
int kl; /* Key length */
int dl; /* Data length */
/* Read the required track into the track buffer if necessary */
rc = read_track (cif, cyl, head);
if (rc < 0) return -1;
/* Search for the requested record in the track buffer */
ptr = cif->trkbuf;
ptr += CKDDASD_TRKHDR_SIZE;
while (1)
{
/* Exit with record not found if end of track */
if (memcmp(ptr, eighthexFF, 8) == 0)
return +1;
/* Extract key length and data length from count field */
rechdr = (CKDDASD_RECHDR*)ptr;
kl = rechdr->klen;
dl = (rechdr->dlen[0] << 8) | rechdr->dlen[1];
/* Exit if requested record number found */
if (rechdr->rec == rec)
break;
/* Issue progress message */
// fprintf (stdout,
// "Skipping CCHHR=%2.2X%2.2X%2.2X%2.2X"
// "%2.2X KL=%2.2X DL=%2.2X%2.2X\n",
// rechdr->cyl[0], rechdr->cyl[1],
// rechdr->head[0], rechdr->head[1],
// rechdr->rec, rechdr->klen,
// rechdr->dlen[0], rechdr->dlen[1]);
/* Point past count key and data to next block */
ptr += CKDDASD_RECHDR_SIZE + kl + dl;
}
/* Return key and data pointers and lengths */
if (keyptr != NULL) *keyptr = ptr + CKDDASD_RECHDR_SIZE;
if (keylen != NULL) *keylen = kl;
if (dataptr != NULL) *dataptr = ptr + CKDDASD_RECHDR_SIZE + kl;
if (datalen != NULL) *datalen = dl;
return 0;
} /* end function read_block */
/*-------------------------------------------------------------------*/
/* Subroutine to search a dataset for a specified key */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* key Key value */
/* keylen Key length */
/* noext Number of extents */
/* extent Dataset extent array */
/* Output: */
/* cyl Cylinder number of requested block */
/* head Head number of requested block */
/* rec Record number of requested block */
/* */
/* Return value is 0 if successful, +1 if key not found, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int search_key_equal (CIFBLK *cif, BYTE *key, int keylen, int noext,
DSXTENT extent[], int *cyl, int *head, int *rec)
{
int rc; /* Return code */
int ccyl; /* Cylinder number */
int chead; /* Head number */
int cext; /* Extent sequence number */
int ecyl; /* Extent end cylinder */
int ehead; /* Extent end head */
BYTE *ptr; /* -> byte in track buffer */
CKDDASD_RECHDR *rechdr; /* -> Record header */
int kl; /* Key length */
int dl; /* Data length */
/* Start at first track of first extent */
cext = 0;
ccyl = (extent[cext].xtbcyl[0] << 8) | extent[cext].xtbcyl[1];
chead = (extent[cext].xtbtrk[0] << 8) | extent[cext].xtbtrk[1];
ecyl = (extent[cext].xtecyl[0] << 8) | extent[cext].xtecyl[1];
ehead = (extent[cext].xtetrk[0] << 8) | extent[cext].xtetrk[1];
if (verbose)
{
fprintf (stdout,
_("HHCDU005I Searching extent %d begin (%d,%d) end (%d,%d)\n"),
cext, ccyl, chead, ecyl, ehead);
}
while (1)
{
/* Read the required track into the track buffer */
rc = read_track (cif, ccyl, chead);
if (rc < 0) return -1;
/* Search for the requested record in the track buffer */
ptr = cif->trkbuf;
ptr += CKDDASD_TRKHDR_SIZE;
while (1)
{
/* Exit loop at end of track */
if (memcmp(ptr, eighthexFF, 8) == 0)
break;
/* Extract key length and data length from count field */
rechdr = (CKDDASD_RECHDR*)ptr;
kl = rechdr->klen;
dl = (rechdr->dlen[0] << 8) | rechdr->dlen[1];
/* Return if requested record key found */
if (kl == keylen
&& memcmp(ptr + CKDDASD_RECHDR_SIZE, key, 44) == 0)
{
*cyl = ccyl;
*head = chead;
*rec = rechdr->rec;
return 0;
}
/* Issue progress message */
// fprintf (stdout,
// "Skipping CCHHR=%2.2X%2.2X%2.2X%2.2X"
// "%2.2X KL=%2.2X DL=%2.2X%2.2X\n",
// rechdr->cyl[0], rechdr->cyl[1],
// rechdr->head[0], rechdr->head[1],
// rechdr->rec, rechdr->klen,
// rechdr->dlen[0], rechdr->dlen[1]);
/* Point past count key and data to next block */
ptr += CKDDASD_RECHDR_SIZE + kl + dl;
} /* end while */
/* Point to the next track */
chead++;
if (chead >= cif->heads)
{
ccyl++;
chead = 0;
}
/* Loop if next track is within current extent */
if (ccyl < ecyl || (ccyl == ecyl && chead <= ehead))
continue;
/* Move to next extent */
cext++;
if (cext >= noext) break;
ccyl = (extent[cext].xtbcyl[0] << 8) | extent[cext].xtbcyl[1];
chead = (extent[cext].xtbtrk[0] << 8) | extent[cext].xtbtrk[1];
ecyl = (extent[cext].xtecyl[0] << 8) | extent[cext].xtecyl[1];
ehead = (extent[cext].xtetrk[0] << 8) | extent[cext].xtetrk[1];
if (verbose)
{
fprintf (stdout,
_("HHCDU006I Searching extent %d begin (%d,%d) end (%d,%d)\n"),
cext, ccyl, chead, ecyl, ehead);
}
} /* end while */
/* Return record not found at end of extents */
return +1;
} /* end function search_key_equal */
/*-------------------------------------------------------------------*/
/* Subroutine to convert relative track to cylinder and head */
/* Input: */
/* tt Relative track number */
/* noext Number of extents in dataset */
/* extent Dataset extent array */
/* heads Number of tracks per cylinder */
/* Output: */
/* cyl Cylinder number */
/* head Head number */
/* */
/* Return value is 0 if successful, or -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int convert_tt (int tt, int noext, DSXTENT extent[], int heads,
int *cyl, int *head)
{
int i; /* Extent sequence number */
int trk; /* Relative track number */
int bcyl; /* Extent begin cylinder */
int btrk; /* Extent begin head */
int ecyl; /* Extent end cylinder */
int etrk; /* Extent end head */
int start; /* Extent begin track */
int end; /* Extent end track */
int extsize; /* Extent size in tracks */
for (i = 0, trk = tt; i < noext; i++)
{
bcyl = (extent[i].xtbcyl[0] << 8) | extent[i].xtbcyl[1];
btrk = (extent[i].xtbtrk[0] << 8) | extent[i].xtbtrk[1];
ecyl = (extent[i].xtecyl[0] << 8) | extent[i].xtecyl[1];
etrk = (extent[i].xtetrk[0] << 8) | extent[i].xtetrk[1];
start = (bcyl * heads) + btrk;
end = (ecyl * heads) + etrk;
extsize = end - start + 1;
if (trk < extsize)
{
trk += start;
*cyl = trk / heads;
*head = trk % heads;
return 0;
}
trk -= extsize;
} /* end for(i) */
fprintf (stderr,
_("HHCDU007E Track %d not found in extent table\n"),
tt);
return -1;
} /* end function convert_tt */
/*-------------------------------------------------------------------*/
/* Subroutine to open a CKD image file */
/* Input: */
/* fname CKD image file name */
/* sfname xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* omode Open mode: O_RDONLY or O_RDWR */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* */
/* The CKD image file is opened, a track buffer is obtained, */
/* and a CKD image file descriptor structure is built. */
/* Return value is a pointer to the CKD image file descriptor */
/* structure if successful, or NULL if unsuccessful. */
/*-------------------------------------------------------------------*/
DLL_EXPORT CIFBLK* open_ckd_image (char *fname, char *sfname, int omode,
int dasdcopy)
{
int fd; /* File descriptor */
int rc; /* Return code */
int len; /* Record length */
CKDDASD_DEVHDR devhdr; /* CKD device header */
CIFBLK *cif; /* CKD image file descriptor */
DEVBLK *dev; /* CKD device block */
CKDDEV *ckd; /* CKD DASD table entry */
char *rmtdev; /* Possible remote device */
char *argv[2]; /* Arguments to */
int argc=0; /* */
char sfxname[FILENAME_MAX*2];/* Suffixed file name */
char typname[64];
char pathname[MAX_PATH]; /* file path in host format */
/* Obtain storage for the file descriptor structure */
cif = (CIFBLK*) calloc (sizeof(CIFBLK), 1);
if (cif == NULL)
{
fprintf (stderr,
_("HHCDU008E Cannot obtain storage for device descriptor "
"buffer: %s\n"),
strerror(errno));
return NULL;
}
/* Initialize the devblk */
dev = &cif->devblk;
if ((omode & O_RDWR) == 0) dev->ckdrdonly = 1;
dev->fd = -1;
dev->batch = 1;
dev->dasdcopy = dasdcopy;
/* If the filename has a `:' then it may be a remote device */
rmtdev = strchr(fname, ':');
/* Read the device header so we can determine the device type */
strcpy (sfxname, fname);
hostpath(pathname, sfxname, sizeof(pathname));
fd = hopen(pathname, omode);
if (fd < 0)
{
/* If no shadow file name was specified, then try opening the
file with the file sequence number in the name */
if (sfname == NULL)
{
int i;
char *s,*suffix;
/* Look for last slash marking end of directory name */
s = strrchr (fname, '/');
if (s == NULL) s = fname;
/* Insert suffix before first dot in file name, or
append suffix to file name if there is no dot.
If the filename already has a place for the suffix
then use that. */
s = strchr (s, '.');
if (s != NULL)
{
i = s - fname;
if (i > 2 && fname[i-2] == '_')
suffix = sfxname + i - 1;
else
{
strcpy (sfxname + i, "_1");
strcat (sfxname, fname + i);
suffix = sfxname + i + 1;
}
}
else
{
if (strlen(sfxname) < 2 || sfxname[strlen(sfxname)-2] != '_')
strcat (sfxname, "_1");
suffix = sfxname + strlen(sfxname) - 1;
}
*suffix = '1';
hostpath(pathname, sfxname, sizeof(pathname));
fd = hopen(pathname, omode);
}
if (fd < 0 && rmtdev == NULL)
{
fprintf (stderr, _("HHCDU009E Cannot open %s: %s\n"),
fname, strerror(errno));
free (cif);
return NULL;
}
else if (fd < 0) strcpy (sfxname, fname);
}
/* If not a possible remote devic, check the dasd header
and set the device type */
if (fd >= 0)
{
len = read (fd, &devhdr, CKDDASD_DEVHDR_SIZE);
if (len < 0)
{
fprintf (stderr, _("HHCDU010E %s read error: %s\n"),
fname, strerror(errno));
close (fd);
free (cif);
return NULL;
}
close (fd);
if (len < (int)CKDDASD_DEVHDR_SIZE
|| (memcmp(devhdr.devid, "CKD_P370", 8)
&& memcmp(devhdr.devid, "CKD_C370", 8)))
{
fprintf (stderr, _("HHCDU011E %s CKD header invalid\n"), fname);
free (cif);
return NULL;
}
/* Set the device type */
ckd = dasd_lookup (DASD_CKDDEV, NULL, devhdr.devtype, 0);
if (ckd == NULL)
{
fprintf(stderr, _("HHCDU012E DASD table entry not found for "
"devtype 0x%2.2X\n"),
devhdr.devtype);
free (cif);
return NULL;
}
dev->devtype = ckd->devt;
snprintf(typname,64,"%4.4X",dev->devtype);
dev->typname=typname; /* Makes HDL Happy */
}
/* Set the device handlers */
dev->hnd = &ckddasd_device_hndinfo;
/* Set the device number */
dev->devnum = ++nextnum;
/* Build arguments for ckddasd_init_handler */
argv[0] = sfxname;
argc++;
if (sfname != NULL)
{
argv[1] = sfname;
argc++;
}
/* Call the device handler initialization function */
rc = (dev->hnd->init)(dev, argc, argv);
if (rc < 0)
{
fprintf (stderr, _("HHCDU013E CKD initialization failed for %s\n"),
fname);
free (cif);
return NULL;
}
/* Call the device start exit */
if (dev->hnd->start) (dev->hnd->start) (dev);
/* Set CIF fields */
cif->fname = fname;
cif->fd = dev->fd;
/* Extract the number of heads and the track size */
cif->heads = dev->ckdheads;
cif->trksz = ((U32)(devhdr.trksize[3]) << 24)
| ((U32)(devhdr.trksize[2]) << 16)
| ((U32)(devhdr.trksize[1]) << 8)
| (U32)(devhdr.trksize[0]);
if (verbose)
{
fprintf (stderr,
_("HHCDU014I %s heads=%d trklen=%d\n"),
cif->fname, cif->heads, cif->trksz);
}
/* Indicate that the track buffer is empty */
cif->curcyl = -1;
cif->curhead = -1;
cif->trkmodif = 0;
return cif;
} /* end function open_ckd_image */
/*-------------------------------------------------------------------*/
/* Subroutine to close a CKD image file */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* */
/* The track buffer is flushed and released, the CKD image file */
/* is closed, and the file descriptor structure is released. */
/* Return value is 0 if successful, -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int close_ckd_image (CIFBLK *cif)
{
int rc; /* Return code */
int trk; /* Track number */
DEVBLK *dev; /* -> CKD device block */
BYTE unitstat; /* Unit status */
dev = &cif->devblk;
/* Write the last track if modified */
if (cif->trkmodif)
{
if (verbose) /* Issue progress message */
fprintf (stdout, _("HHCDU015I Updating cyl %d head %d\n"),
cif->curcyl, cif->curhead);
trk = (cif->curcyl * cif->heads) + cif->curhead;
rc = (dev->hnd->write)(dev, trk, 0, NULL, cif->trksz, &unitstat);
if (rc < 0)
{
fprintf (stderr, _("HHCDU016E %s write track error: stat=%2.2X\n"),
cif->fname, unitstat);
}
}
/* Call the END exit */
if (dev->hnd->end) (dev->hnd->end) (dev);
/* Close the CKD image file */
(dev->hnd->close)(dev);
/* Release the file descriptor structure */
free (cif);
return 0;
} /* end function close_ckd_image */
/*-------------------------------------------------------------------*/
/* Subroutine to open a FBA image file */
/* Input: */
/* fname FBA image file name */
/* sfname xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* omode Open mode: O_RDONLY or O_RDWR */
/* dasdcopy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* */
/* The FBA image file is opened, a track buffer is obtained, */
/* and a FBA image file descriptor structure is built. */
/* Return value is a pointer to the FBA image file descriptor */
/* structure if successful, or NULL if unsuccessful. */
/*-------------------------------------------------------------------*/
DLL_EXPORT CIFBLK* open_fba_image (char *fname, char *sfname, int omode,
int dasdcopy)
{
int rc; /* Return code */
CIFBLK *cif; /* FBA image file descriptor */
DEVBLK *dev; /* FBA device block */
FBADEV *fba; /* FBA DASD table entry */
char *argv[2]; /* Arguments to */
int argc=0; /* device open */
/* Obtain storage for the file descriptor structure */
cif = (CIFBLK*) calloc (sizeof(CIFBLK), 1);
if (cif == NULL)
{
fprintf (stderr,
_("HHCDU017E Cannot obtain storage for device descriptor "
"buffer: %s\n"),
strerror(errno));
return NULL;
}
/* Initialize the devblk */
dev = &cif->devblk;
if ((omode & O_RDWR) == 0) dev->ckdrdonly = 1;
dev->batch = 1;
dev->dasdcopy = dasdcopy;
/* Set the device type */
fba = dasd_lookup (DASD_FBADEV, NULL, DEFAULT_FBA_TYPE, 0);
if (fba == NULL)
{
fprintf(stderr, _("HHCDU018E DASD table entry not found for "
"devtype 0x%2.2X\n"),
DEFAULT_FBA_TYPE);
free (cif);
return NULL;
}
dev->devtype = fba->devt;
/* Set the device handlers */
dev->hnd = &fbadasd_device_hndinfo;
/* Set the device number */
dev->devnum = ++nextnum;
/* Build arguments for fbadasd_init_handler */
argv[0] = fname;
argc++;
if (sfname != NULL)
{
argv[1] = sfname;
argc++;
}
/* Call the device handler initialization function */
rc = (dev->hnd->init)(dev, argc, argv);
if (rc < 0)
{
fprintf (stderr, _("HHCDU019E FBA initialization failed for %s\n"),
fname);
free (cif);
return NULL;
}
/* Set CIF fields */
cif->fname = fname;
cif->fd = dev->fd;
/* Extract the number of sectors and the sector size */
cif->heads = dev->fbanumblk;
cif->trksz = dev->fbablksiz;
if (verbose)
{
fprintf (stderr,
_("HHCDU020I %s sectors=%d size=%d\n"),
cif->fname, cif->heads, cif->trksz);
}
/* Indicate that the track buffer is empty */
cif->curcyl = -1;
cif->curhead = -1;
cif->trkmodif = 0;
return cif;
} /* end function open_fba_image */
/*-------------------------------------------------------------------*/
/* Subroutine to build extent array for specified dataset */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* dsnama -> Dataset name (ASCIIZ) */
/* Output: */
/* extent Extent array (up to 16 entries) */
/* noext Number of extents */
/* */
/* Return value is 0 if successful, or -1 if error */
/*-------------------------------------------------------------------*/
DLL_EXPORT int build_extent_array (CIFBLK *cif, char *dsnama, DSXTENT extent[],
int *noext)
{
int rc; /* Return code */
int len; /* Record length */
int cyl; /* Cylinder number */
int head; /* Head number */
int rec; /* Record number */
BYTE *vol1data; /* -> Volume label */
FORMAT1_DSCB *f1dscb; /* -> Format 1 DSCB */
FORMAT3_DSCB *f3dscb; /* -> Format 3 DSCB */
FORMAT4_DSCB *f4dscb; /* -> Format 4 DSCB */
BYTE dsname[44]; /* Dataset name (EBCDIC) */
char volser[7]; /* Volume serial (ASCIIZ) */
/* Convert the dataset name to EBCDIC */
convert_to_ebcdic (dsname, sizeof(dsname), dsnama);
/* Read the volume label */
rc = read_block (cif, 0, 0, 3, NULL, NULL, &vol1data, &len);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU021E VOL1 record not found\n"));
return -1;
}
/* Extract the volume serial and the CCHHR of the format 4 DSCB */
make_asciiz (volser, sizeof(volser), vol1data+4, 6);
cyl = (vol1data[11] << 8) | vol1data[12];
head = (vol1data[13] << 8) | vol1data[14];
rec = vol1data[15];
if (verbose)
{
fprintf (stdout,
_("HHCDU022I VOLSER=%s VTOC=%4.4X%4.4X%2.2X\n"),
volser, cyl, head, rec);
}
/* Read the format 4 DSCB */
rc = read_block (cif, cyl, head, rec,
(void *)&f4dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU023E F4DSCB record not found\n"));
return -1;
}
if (verbose)
{
fprintf (stdout,
_("HHCDU023I VTOC start %2.2X%2.2X%2.2X%2.2X "
"end %2.2X%2.2X%2.2X%2.2X\n"),
f4dscb->ds4vtoce.xtbcyl[0], f4dscb->ds4vtoce.xtbcyl[1],
f4dscb->ds4vtoce.xtbtrk[0], f4dscb->ds4vtoce.xtbtrk[1],
f4dscb->ds4vtoce.xtecyl[0], f4dscb->ds4vtoce.xtecyl[1],
f4dscb->ds4vtoce.xtetrk[0], f4dscb->ds4vtoce.xtetrk[1]);
}
/* Search for the requested dataset in the VTOC */
rc = search_key_equal (cif, dsname, sizeof(dsname),
1, &(f4dscb->ds4vtoce),
&cyl, &head, &rec);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr,
_("HHCDU024E Dataset %s not found in VTOC\n"),
dsnama);
return -1;
}
if (verbose)
{
fprintf (stdout,
_("HHCDU025I DSNAME=%s F1DSCB CCHHR=%4.4X%4.4X%2.2X\n"),
dsnama, cyl, head, rec);
}
/* Read the format 1 DSCB */
rc = read_block (cif, cyl, head, rec,
(void *)&f1dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU026E F1DSCB record not found\n"));
return -1;
}
/* Extract number of extents and first 3 extent descriptors */
*noext = f1dscb->ds1noepv;
extent[0] = f1dscb->ds1ext1;
extent[1] = f1dscb->ds1ext2;
extent[2] = f1dscb->ds1ext3;
/* Obtain additional extent descriptors */
if (f1dscb->ds1noepv > 3)
{
/* Read the format 3 DSCB */
cyl = (f1dscb->ds1ptrds[0] << 8) | f1dscb->ds1ptrds[1];
head = (f1dscb->ds1ptrds[2] << 8) | f1dscb->ds1ptrds[3];
rec = f1dscb->ds1ptrds[4];
rc = read_block (cif, cyl, head, rec,
(void *)&f3dscb, &len, NULL, NULL);
if (rc < 0) return -1;
if (rc > 0)
{
fprintf (stderr, _("HHCDU027E F3DSCB record not found\n"));
return -1;
}
/* Extract the next 13 extent descriptors */
extent[3] = f3dscb->ds3extnt[0];
extent[4] = f3dscb->ds3extnt[1];
extent[5] = f3dscb->ds3extnt[2];
extent[6] = f3dscb->ds3extnt[3];
extent[7] = f3dscb->ds3adext[0];
extent[8] = f3dscb->ds3adext[1];
extent[9] = f3dscb->ds3adext[2];
extent[10] = f3dscb->ds3adext[3];
extent[11] = f3dscb->ds3adext[4];
extent[12] = f3dscb->ds3adext[5];
extent[13] = f3dscb->ds3adext[6];
extent[14] = f3dscb->ds3adext[7];
extent[15] = f3dscb->ds3adext[8];
}
return 0;
} /* end function build_extent_array */
/*-------------------------------------------------------------------*/
/* Subroutine to calculate physical device track capacities */
/* Input: */
/* cif -> CKD image file descriptor structure */
/* used Number of bytes used so far on track, */
/* excluding home address and record 0 */
/* keylen Key length of proposed new record */
/* datalen Data length of proposed new record */
/* Output: */
/* newused Number of bytes used including proposed new record */
/* trkbaln Number of bytes remaining on track */
/* physlen Number of bytes on physical track (=ds4devtk) */
/* kbconst Overhead for non-last keyed block (=ds4devi) */
/* lbconst Overhead for last keyed block (=ds4devl) */
/* nkconst Overhead difference for non-keyed block (=ds4devk) */
/* devflag Device flag byte for VTOC (=ds4devfg) */
/* tolfact Device tolerance factor (=ds4devtl) */
/* maxdlen Maximum data length for non-keyed record 1 */
/* numrecs Number of records of specified length per track */
/* numhead Number of tracks per cylinder */
/* numcyls Number of cylinders per volume */
/* Note: */
/* A NULL address may be specified for any of the output */
/* fields if the output value is not required. */
/* The return value is 0 if the record will fit on the track, */
/* +1 if record will not fit on track, or -1 if unknown devtype */
/* Note: */
/* Although the virtual DASD image file contains no interrecord */