forked from rbowler/spinhawk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cckdutil.c
2716 lines (2371 loc) · 96.2 KB
/
cckdutil.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
/* CCKDUTIL.C (c) Copyright Greg Smith, 2000-2009 */
/* Compressed CKD Common routines */
/*-------------------------------------------------------------------*/
/* This module contains functions for compressed CKD devices */
/* used by more than 1 main program. */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _CCKDUTIL_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "opcode.h"
/*-------------------------------------------------------------------*/
typedef struct _SPCTAB { /* Space table */
BYTE typ; /* Type of space */
int val; /* Value for space */
U32 pos; /* Space offset */
U32 len; /* Space length */
U32 siz; /* Space size */
} SPCTAB;
#define SPCTAB_NONE 0 /* Ignore this space entry */
#define SPCTAB_DEVHDR 1 /* Space is device header */
#define SPCTAB_CDEVHDR 2 /* Space is compressed hdr */
#define SPCTAB_L1 3 /* Space is level 1 table */
#define SPCTAB_L2 4 /* Space is level 2 table */
#define SPCTAB_TRK 5 /* Space is track image */
#define SPCTAB_BLKGRP 6 /* Space is blkgrp image */
#define SPCTAB_FREE 7 /* Space is free block */
#define SPCTAB_EOF 8 /* Space is end-of-file */
/*-------------------------------------------------------------------*/
/* Internal functions */
/*-------------------------------------------------------------------*/
static int comp_spctab_sort(const void *a, const void *b);
static int cdsk_spctab_sort(const void *a, const void *b);
static int cdsk_build_free_space(SPCTAB *spctab, int s);
static int cdsk_valid_trk (int trk, BYTE *buf, int heads, int len);
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static char *spaces[] = { "none", "devhdr", "cdevhdr", "l1", "l2",
"trk", "blkgrp", "free", "eof" };
static char *comps[] = { "none", "zlib", "bzip2" };
/*-------------------------------------------------------------------*/
/* Change the endianess of a compressed file */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_swapend (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int fd; /* File descriptor */
int rc; /* Return code */
struct stat fst; /* File status buffer */
int i; /* Index */
int swapend; /* 1=swap space */
int len; /* Length */
off_t off, lopos, hipos; /* File offsets */
CCKD_DEVHDR cdevhdr; /* Compressed ckd header */
CCKD_L1ENT *l1 = NULL; /* Level 1 table */
CCKD_L2ENT l2[256]; /* Level 2 table */
CCKD_FREEBLK freeblk; /* Free block */
/* Get fd */
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/* Get file size */
if (fstat (fd, &fst) < 0)
goto cswp_fstat_error;
hipos = fst.st_size;
/* Device header */
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto cswp_read_error;
swapend = (cdevhdr.options & CCKD_BIGENDIAN) != cckd_endian();
cckd_swapend_chdr (&cdevhdr);
cdevhdr.options |= CCKD_ORDWR;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &cdevhdr, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_chdr (&cdevhdr);
/* l1 table */
len = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
if ((l1 = malloc (len)) == NULL)
goto cswp_malloc_error;
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, l1, len)) != len)
goto cswp_read_error;
cckd_swapend_l1 (l1, (int)cdevhdr.numl1tab);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, l1, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_l1 (l1, (int)cdevhdr.numl1tab);
lopos = CCKD_L1TAB_POS + len;
/* l2 tables */
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff
|| l1[i] < lopos || l1[i] > hipos - CCKD_L2TAB_SIZE)
continue;
off = (off_t)l1[i];
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = read (fd, l2, len)) != len)
goto cswp_read_error;
cckd_swapend_l2 (l2);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, l2, len)) != len)
goto cswp_write_error;
}
free (l1);
l1 = NULL;
/* free space */
if (cdevhdr.free && cdevhdr.free >= lopos
&& cdevhdr.free <= hipos - CCKD_FREEBLK_SIZE)
{
off = (off_t)cdevhdr.free;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_FREEBLK_SIZE;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
if (memcmp(&freeblk, "FREE_BLK", 8) == 0)
{
/* New format free space */
for (i = 0; i < cdevhdr.free_number; i++)
{
off += CCKD_FREEBLK_SIZE;
if (off > hipos - CCKD_FREEBLK_SIZE)
break;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
cckd_swapend_free (&freeblk);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &freeblk, len)) != len)
goto cswp_write_error;
} /* for each free space */
} /* if new format free space */
else
{
/* Old format free space */
for (i = 0; i < cdevhdr.free_number; i++)
{
if (off < lopos || off > hipos - CCKD_FREEBLK_SIZE)
break;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
cckd_swapend_free (&freeblk);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &freeblk, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_free (&freeblk);
off = (off_t)freeblk.pos;
} /* for each free space */
} /* else old format free space */
} /* if free space */
return 0;
/* error exits */
cswp_fstat_error:
cckdumsg (dev, 701, "fstat error: %s\n", strerror(errno));
goto cswp_error;
cswp_lseek_error:
cckdumsg (dev, 702, "lseek error, offset 0x%" I64_FMT "x: %s\n",
(long long)off, strerror(errno));
goto cswp_error;
cswp_read_error:
cckdumsg (dev, 703, "read error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cswp_error;
cswp_write_error:
cckdumsg (dev, 704, "write error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cswp_error;
cswp_malloc_error:
cckdumsg (dev, 705, "malloc error, size %d: %s\n",
len, strerror(errno));
goto cswp_error;
cswp_error:
if (l1) free(l1);
return -1;
}
/*-------------------------------------------------------------------*/
/* Swap endian - compressed device header */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_chdr (CCKD_DEVHDR *cdevhdr)
{
/* fix the compressed ckd header */
cdevhdr->options ^= CCKD_BIGENDIAN;
cckd_swapend4 ((char *) &cdevhdr->numl1tab);
cckd_swapend4 ((char *) &cdevhdr->numl2tab);
cckd_swapend4 ((char *) &cdevhdr->size);
cckd_swapend4 ((char *) &cdevhdr->used);
cckd_swapend4 ((char *) &cdevhdr->free);
cckd_swapend4 ((char *) &cdevhdr->free_total);
cckd_swapend4 ((char *) &cdevhdr->free_largest);
cckd_swapend4 ((char *) &cdevhdr->free_number);
cckd_swapend4 ((char *) &cdevhdr->free_imbed);
cckd_swapend2 ((char *) &cdevhdr->compress_parm);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 1 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l1 (CCKD_L1ENT *l1, int n)
{
int i; /* Index */
for (i = 0; i < n; i++)
cckd_swapend4 ((char *) &l1[i]);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 2 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l2 (CCKD_L2ENT *l2)
{
int i; /* Index */
for (i = 0; i < 256; i++)
{
cckd_swapend4 ((char *) &l2[i].pos);
cckd_swapend2 ((char *) &l2[i].len);
cckd_swapend2 ((char *) &l2[i].size);
}
}
/*-------------------------------------------------------------------*/
/* Swap endian - free space entry */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_free (CCKD_FREEBLK *fb)
{
cckd_swapend4 ((char *) &fb->pos);
cckd_swapend4 ((char *) &fb->len);
}
/*-------------------------------------------------------------------*/
/* Swap endian - 4 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend4 (char *c)
{
char temp[4];
memcpy (&temp, c, 4);
c[0] = temp[3];
c[1] = temp[2];
c[2] = temp[1];
c[3] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Swap endian - 2 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend2 (char *c)
{
char temp[2];
memcpy (&temp, c, 2);
c[0] = temp[1];
c[1] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Are we little or big endian? From Harbison&Steele. */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_endian()
{
union
{
long l;
char c[sizeof (long)];
} u;
u.l = 1;
return u.c[sizeof (long) - 1] == 1 ? CCKD_BIGENDIAN : 0;
}
/*-------------------------------------------------------------------
* Remove all free space from a compressed ckd file
*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_comp (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int fd; /* File descriptor */
struct stat fst; /* File status buffer */
long long maxsize; /* Max cckd file size */
int rc; /* Return code */
off_t off; /* File offset */
off_t l2area; /* Boundary for l2 tables */
int len; /* Length */
int i, j, l, n; /* Work variables */
int relocate = 0; /* 1=spaces will be relocated*/
int l1size; /* l1 table size */
U32 next; /* offset of next space */
int s; /* space table index */
CKDDASD_DEVHDR devhdr; /* CKD device header */
CCKD_DEVHDR cdevhdr; /* CCKD device header */
CCKD_L1ENT *l1=NULL; /* -> l1 table */
CCKD_L2ENT **l2=NULL; /* -> l2 table array */
SPCTAB *spctab=NULL; /* -> space table */
BYTE *rbuf=NULL; /* Relocation buffer */
BYTE *p; /* -> relocation buffer */
int rlen=0; /* Relocation buffer length */
CCKD_L2ENT zero_l2[256]; /* Empty l2 table (zeros) */
CCKD_L2ENT ff_l2[256]; /* Empty l2 table (0xff's) */
BYTE buf[65536*4]; /* Buffer */
/*---------------------------------------------------------------
* Get fd
*---------------------------------------------------------------*/
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/*---------------------------------------------------------------
* Get file statistics
*---------------------------------------------------------------*/
if (fstat (fd, &fst) < 0)
goto comp_fstat_error;
maxsize = sizeof(off_t) == 4 ? 0x7fffffffll : 0xffffffffll;
/*---------------------------------------------------------------
* Read device header
*---------------------------------------------------------------*/
off = 0;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CKDDASD_DEVHDR_SIZE;
if ((rc = read (fd, &devhdr, len)) != len)
goto comp_read_error;
if (memcmp (devhdr.devid, "CKD_C370", 8) != 0
&& memcmp (devhdr.devid, "CKD_S370", 8) != 0
&& memcmp (devhdr.devid, "FBA_C370", 8) != 0
&& memcmp (devhdr.devid, "FBA_S370", 8) != 0)
{
cckdumsg (dev, 999, "not a compressed dasd file\n");
goto comp_error;
}
comp_restart:
/*---------------------------------------------------------------
* Read compressed device header
*---------------------------------------------------------------*/
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto comp_read_error;
/*---------------------------------------------------------------
* Check the endianess of the file
*---------------------------------------------------------------*/
if ((cdevhdr.options & CCKD_BIGENDIAN) != cckd_endian())
{
cckdumsg (dev, 101, "converting to %s\n",
cckd_endian() ? "big-endian" : "little-endian");
if (cckd_swapend (dev) < 0)
goto comp_error;
else
goto comp_restart;
}
/*---------------------------------------------------------------
* Some header checks
*---------------------------------------------------------------*/
if ((off_t)cdevhdr.size != fst.st_size
|| cdevhdr.size != cdevhdr.used || cdevhdr.free != 0
|| cdevhdr.free_total != 0 || cdevhdr.free_largest != 0
|| cdevhdr.free_number != 0 || cdevhdr.free_imbed != 0)
relocate = 1;
/*---------------------------------------------------------------
* Build empty l2 tables
*---------------------------------------------------------------*/
memset (&zero_l2, 0, CCKD_L2TAB_SIZE);
if (cdevhdr.nullfmt != 0)
for (i = 0; i < 256; i++)
zero_l2[i].len = zero_l2[i].size = cdevhdr.nullfmt;
memset (&ff_l2, 0xff, CCKD_L2TAB_SIZE);
/*---------------------------------------------------------------
* Read the l1 table
*---------------------------------------------------------------*/
l1size = len = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
if ((l1 = malloc (len)) == NULL)
goto comp_malloc_error;
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, l1, len)) != len)
goto comp_read_error;
/*---------------------------------------------------------------
* Build the space table
*---------------------------------------------------------------*/
n = 1 + 1 + 1 + cdevhdr.numl1tab + 1;
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
n += 256;
len = sizeof(SPCTAB);
if ((spctab = calloc (n, len)) == NULL)
goto comp_calloc_error;
s = 0;
spctab[s].typ = SPCTAB_DEVHDR;
spctab[s].val = -1;
spctab[s].pos = 0;
spctab[s].len =
spctab[s].siz = CKDDASD_DEVHDR_SIZE;
s++;
spctab[s].typ = SPCTAB_CDEVHDR;
spctab[s].val = -1;
spctab[s].pos = CCKD_DEVHDR_POS;
spctab[s].len =
spctab[s].siz = CCKD_DEVHDR_SIZE;
s++;
spctab[s].typ = SPCTAB_L1;
spctab[s].val = -1;
spctab[s].pos = CCKD_L1TAB_POS;
spctab[s].len =
spctab[s].siz = l1size;
s++;
spctab[s].typ = SPCTAB_EOF;
spctab[s].val = -1;
spctab[s].pos = fst.st_size;
spctab[s].len =
spctab[s].siz = 0;
s++;
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
{
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = l1[i];
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
}
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
/*---------------------------------------------------------------
* Read level 2 tables
*---------------------------------------------------------------*/
n = cdevhdr.numl1tab;
len = sizeof (void *);
if ((l2 = calloc (n, len)) == NULL)
goto comp_calloc_error;
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
if (spctab[i].typ != SPCTAB_L2) continue;
l = spctab[i].val;
len = CCKD_L2TAB_SIZE;
if ((l2[l] = malloc (len)) == NULL)
goto comp_malloc_error;
off = (off_t)spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, l2[l], len)) != len)
goto comp_read_error;
for (j = 0; j < 256; j++)
{
if (l2[l][j].pos == 0 || l2[l][j].pos == 0xffffffff)
continue;
spctab[s].typ = SPCTAB_TRK;
spctab[s].val = spctab[i].val*256 + j;
spctab[s].pos = l2[l][j].pos;
spctab[s].len = l2[l][j].len;
spctab[s].siz = l2[l][j].size;
s++;
} /* for each l2 entry */
/* check if empty l2 table */
if (memcmp (l2[l], &zero_l2, CCKD_L2TAB_SIZE) == 0
|| memcmp (l2[l], &ff_l2, CCKD_L2TAB_SIZE) == 0)
{
l1[l] = l2[l][0].pos; /* 0x00000000 or 0xffffffff */
spctab[i].typ = SPCTAB_NONE;
free (l2[l]);
l2[l] = NULL;
relocate = 1;
}
} /* for each space */
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
/* set relocate flag if last space is free space */
if (spctab[s-2].pos + spctab[s-2].len != spctab[s-1].pos)
relocate = 1;
/*---------------------------------------------------------------
* relocate l2 tables in order
*---------------------------------------------------------------*/
/* determine l2 area */
l2area = CCKD_L1TAB_POS + l1size;
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff) continue;
if (l1[i] != l2area)
relocate = 1;
l2area += CCKD_L2TAB_SIZE;
}
/* quick return if all l2 tables are orderered and no free space */
if (!relocate)
{
for (i = 1; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i-1].pos + spctab[i-1].len != spctab[i].pos)
break;
if (spctab[i].typ == SPCTAB_EOF)
{
cckdumsg (dev, 103, "file already compressed\n");
goto comp_return_ok;
}
}
/* file will be updated */
cdevhdr.options |= CCKD_ORDWR;
/* calculate track size within the l2 area */
for (i = rlen = 0; spctab[i].pos < l2area; i++)
if (spctab[i].typ == SPCTAB_TRK)
rlen += sizeof(spctab[i].val) + sizeof(spctab[i].len)
+ spctab[i].len;
/* read any tracks in the l2area into rbuf */
if ((len = rlen) > 0)
{
if ((rbuf = malloc (len)) == NULL)
goto comp_malloc_error;
for (i = 0, p = rbuf; spctab[i].pos < l2area; i++)
{
if (spctab[i].typ != SPCTAB_TRK) continue;
memcpy (p, &spctab[i].val, sizeof(spctab[i].val));
p += sizeof(spctab[i].val);
memcpy (p, &spctab[i].len, sizeof(spctab[i].len));
p += sizeof(spctab[i].len);
off = (off_t)spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = spctab[i].len;
if ((rc = read (fd, p, len)) != len)
goto comp_read_error;
p += len;
spctab[i].typ = SPCTAB_NONE;
} /* for each space in the l2 area */
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
} /* if any tracks to relocate */
/* remove all l2 tables from the space table */
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_L2)
spctab[i].typ = SPCTAB_NONE;
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
/* add all l2 tables at their ordered offsets */
off = CCKD_L1TAB_POS + l1size;
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff) continue;
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = (U32)off;
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
off += CCKD_L2TAB_SIZE;
}
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
/* set end-of-file position */
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
/*---------------------------------------------------------------
* Perform compression
*---------------------------------------------------------------*/
/* move spaces left */
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
/* ignore contiguous spaces */
if (spctab[i].pos + spctab[i].len == spctab[i+1].pos)
continue;
/* found a gap */
off = (off_t)spctab[i+1].pos;
/* figure out how much we can read */
for (len = 0, j = i + 1; spctab[j].typ != SPCTAB_EOF; j++)
{
if (len + spctab[j].len > sizeof(buf))
break;
next = spctab[j].pos + spctab[j].len;
spctab[j].pos = spctab[i].pos + spctab[i].len + len;
spctab[j].siz = spctab[j].len;
len += spctab[j].len;
if (next != spctab[j+1].pos)
break;
} /* search for contiguous spaces */
/* this can happen if the next space is end-of-file */
if (len == 0)
continue;
/* read the image(s) to be relocated */
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, buf, len)) != len)
goto comp_write_error;
/* write the images */
off = (off_t)spctab[i].pos + spctab[i].len;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = write (fd, buf, len)) != len)
goto comp_write_error;
}
/* adjust the size of the file */
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
/*---------------------------------------------------------------
* Write spaces relocated from the l2area to the end of the file
*---------------------------------------------------------------*/
off = (off_t)spctab[s-1].pos;
p = rbuf;
while (rlen)
{
spctab[s].typ = SPCTAB_TRK;
spctab[s].pos = (U32)off;
memcpy (&spctab[s].val, p, sizeof(spctab[s].val));
p += sizeof(spctab[s].val);
memcpy (&spctab[s].len, p, sizeof(spctab[s].len));
spctab[s].siz = spctab[s].len;
p += sizeof(spctab[s].len);
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = spctab[s].len;
if ((rc = write (fd, p, len)) != len)
goto comp_write_error;
p += len;
off += len;
rlen -= len + sizeof(spctab[s].val) + sizeof(spctab[s].len);
s++;
} /* for each relocated space in l2area */
/* adjust the space table */
if (rbuf)
{
free (rbuf); rbuf = NULL;
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
}
/*---------------------------------------------------------------
* Update the device header
*---------------------------------------------------------------*/
cdevhdr.size =
cdevhdr.used = spctab[s-1].pos;
cdevhdr.free =
cdevhdr.free_total =
cdevhdr.free_largest =
cdevhdr.free_number =
cdevhdr.free_imbed = 0;
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
/*---------------------------------------------------------------
* Update the lookup tables
*---------------------------------------------------------------*/
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_L2)
l1[spctab[i].val] = spctab[i].pos;
else if (spctab[i].typ == SPCTAB_TRK)
{
l = spctab[i].val / 256;
j = spctab[i].val % 256;
l2[l][j].pos = spctab[i].pos;
l2[l][j].len =
l2[l][j].size = spctab[i].len;
}
/*---------------------------------------------------------------
* Write the cdevhdr, l1 table and l2 tables
*---------------------------------------------------------------*/
/* write cdevhdr */
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = write (fd, &cdevhdr, len)) != len)
goto comp_write_error;
/* write l1 table */
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = l1size;
if ((rc = write (fd, l1, len)) != len)
goto comp_write_error;
/* write l2 tables */
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
{
off = (off_t)l1[i];
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = write (fd, l2[i], len)) != len)
goto comp_lseek_error;
}
/* truncate the file */
off = (off_t)spctab[s-1].pos;
if (off < fst.st_size)
{
ftruncate (fd, off);
cckdumsg (dev, 102, "compress successful, %lld bytes released\n",
(long long)fst.st_size - off);
}
else
cckdumsg (dev, 102, "compress successful, L2 tables relocated\n");
/*---------------------------------------------------------------
* Return
*---------------------------------------------------------------*/
comp_return_ok:
rc = 0;
comp_return:
if (rbuf) free(rbuf);
if (l2)
{
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i])
free (l2[i]);
free (l2);
}
if (l1) free (l1);
if (spctab) free (spctab);
return rc;
/*---------------------------------------------------------------
* Error exits
*---------------------------------------------------------------*/
comp_fstat_error:
cckdumsg (dev, 701, "fstat error: %s\n",
strerror(errno));
goto comp_error;
comp_lseek_error:
cckdumsg (dev, 702, "lseek error, offset 0x%" I64_FMT "x: %s\n",
(long long)off, strerror(errno));
goto comp_error;
comp_read_error:
cckdumsg (dev, 703, "read error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto comp_error;
comp_write_error:
cckdumsg (dev, 704, "write error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto comp_error;
comp_malloc_error:
cckdumsg (dev, 705, "malloc error, size %d: %s\n",
len, strerror(errno));
goto comp_error;
comp_calloc_error:
cckdumsg (dev, 706, "calloc error, size %d: %s\n",
n*len, strerror(errno));
goto comp_error;
comp_error:
rc = -1;
goto comp_return;
} /* cckd_comp() */
/*-------------------------------------------------------------------
* cckd_comp() space table sort
*-------------------------------------------------------------------*/
static int comp_spctab_sort(const void *a, const void *b)
{
const SPCTAB *x = a, *y = b;
if (x->typ == SPCTAB_NONE) return 1;
else if (y->typ == SPCTAB_NONE) return -1;
else if (x->typ == SPCTAB_EOF) return 1;
else if (y->typ == SPCTAB_EOF) return -1;
else if (x->pos < y->pos) return -1;
else return 1;
}
/*-------------------------------------------------------------------
* Perform check function on a compressed ckd file
*
* check levels
* -1 devhdr, cdevhdr, l1 table
* 0 devhdr, cdevhdr, l1 table, l2 tables
* 1 devhdr, cdevhdr, l1 table, l2 tables, free spaces
* 2 devhdr, cdevhdr, l1 table, l2 tables, free spaces, trkhdrs
* 3 devhdr, cdevhdr, l1 table, l2 tables, free spaces, trkimgs
* 4 devhdr, cdevhdr. Build everything else from recovery
*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_chkdsk(DEVBLK *dev, int level)
{
CCKDDASD_EXT *cckd; /* -> ckd extension */
int fd; /* file descriptor */
struct stat fst; /* file status information */
int fdflags; /* file descriptor flags */
long long maxsize; /* max cckd file size */
int ro; /* 1=file opened read-only */
int f, i, j, l, n; /* work integers */
int l1x, l2x; /* l1, l2 table indexes */
BYTE compmask[256]; /* compression byte mask
00 - supported
0x - valid, not supported
ff - invalid */
off_t off; /* file offset */
int len; /* length to read */
int rc; /* function return code */
int comp; /* trkhdr compression byte[0]*/
int cyl; /* trkhdr cyl bytes[1-2]*/
int head; /* trkhdr head bytes[3-4]*/
int trk; /* trkhdr calculated trk */
int cyls; /* number cylinders */
int heads; /* number heads/cylinder */
int trks; /* number tracks */
unsigned int trksz; /* track size */
int blks; /* number fba blocks */
int blkgrp; /* current block group nbr */
int blkgrps; /* number fba block groups */
unsigned int blkgrpsz; /* fba block group size */
int trktyp; /* track type (TRK, BLKGRP) */
int ckddasd=0; /* 1=ckd */
int fbadasd=0; /* 1= fba */
int shadow=0; /* 0xff=shadow file */
int hdrerr=0; /* non-zero: header errors */
int fsperr=0; /* 1=rebuild free space */
int comperrs=0; /* 1=unsupported comp found */
int recovery=0; /* 1=perform track recovery */
int valid; /* 1=valid trk recovered */
int l1size; /* size of l1 table */
int swapend=0; /* 1=call cckd_swapend */
U32 lopos, hipos; /* low/high file positions */
int pass; /* recovery pass number (fba)*/
int s; /* space table index */
SPCTAB *spctab=NULL; /* -> space table */
BYTE *l2errs=NULL; /* l2 error table */
BYTE *rcvtab=NULL; /* recovered tracks */
CKDDASD_DEVHDR devhdr; /* device header */
CCKD_DEVHDR cdevhdr; /* compressed device header */
CCKD_DEVHDR cdevhdr2; /* compressed device header 2*/
CCKD_L1ENT *l1=NULL; /* -> level 1 table */
CCKD_L2ENT l2ent; /* level 2 entry */
CCKD_L2ENT l2tab[256]; /* level 2 table */
CCKD_L2ENT **l2=NULL; /* -> level 2 table array */
CCKD_L2ENT empty_l2[256]; /* Empty l2 table */
CCKD_FREEBLK freeblk; /* free block */
CCKD_FREEBLK *fsp=NULL; /* free blocks (new format) */
BYTE buf[4*65536]; /* buffer */
/* Get fd */
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/* Get some file information */
if ( fstat (fd, &fst) < 0 )
goto cdsk_fstat_error;
hipos = fst.st_size;
maxsize = sizeof(off_t) == 4 ? 0x7fffffffll : 0xffffffffll;
fdflags = get_file_accmode_flags(fd);
ro = (fdflags & O_RDWR) == 0;
/* Build table for compression byte test */
memset (compmask, 0xff, 256);
compmask[0] = 0;
#if defined(HAVE_LIBZ)
compmask[CCKD_COMPRESS_ZLIB] = 0;
#else
compmask[CCKD_COMPRESS_ZLIB] = 1;
#endif
#if defined(CCKD_BZIP2)
compmask[CCKD_COMPRESS_BZIP2] = 0;
#else
compmask[CCKD_COMPRESS_BZIP2] = 2;
#endif
/*---------------------------------------------------------------
* Header checks
*---------------------------------------------------------------*/
/* Read the device header */
off = 0;
if ( lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CKDDASD_DEVHDR_SIZE;
if ((rc = read (fd, &devhdr, len)) != len)
goto cdsk_read_error;
/* Device header checks */
if (memcmp(devhdr.devid, "CKD_C370", 8) == 0
|| memcmp(devhdr.devid, "CKD_S370", 8) == 0
)
ckddasd = 1;
else if (memcmp(devhdr.devid, "FBA_C370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0
)
fbadasd = 1;
else
{
cckdumsg (dev, 999, "not a compressed dasd file\n");
goto cdsk_error;
}
if (memcmp(devhdr.devid, "CKD_S370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0
)
shadow = 0xff;
trktyp = ckddasd ? SPCTAB_TRK : SPCTAB_BLKGRP;
/* Read the cckd device header */
off = CCKD_DEVHDR_POS;
if ( lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto cdsk_read_error;