forked from mtalexander/hercules-390
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cckddasd.c
5890 lines (5037 loc) · 200 KB
/
cckddasd.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
/* CCKDDASD.C (c) Copyright Roger Bowler, 1999-2012 */
/* (c) Copyright Greg Smith, 2002-2012 */
/* */
/* ESA/390 Compressed CKD Direct Access Storage Device Handler */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/*-------------------------------------------------------------------*/
/* This module contains device functions for compressed emulated */
/* count-key-data direct access storage devices. */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#define _CCKDDASD_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "devtype.h"
#include "opcode.h"
/*-------------------------------------------------------------------*/
/* Internal functions */
/*-------------------------------------------------------------------*/
int cckddasd_init(int argc, BYTE *argv[]);
int cckddasd_term();
int cckddasd_init_handler( DEVBLK *dev, int argc, char *argv[] );
int cckddasd_close_device(DEVBLK *dev);
void cckddasd_start(DEVBLK *dev);
void cckddasd_end(DEVBLK *dev);
int cckd_open (DEVBLK *dev, int sfx, int flags, mode_t mode);
int cckd_close (DEVBLK *dev, int sfx);
int cckd_read (DEVBLK *dev, int sfx, off_t off, void *buf, unsigned int len);
int cckd_write (DEVBLK *dev, int sfx, off_t off, void *buf, unsigned int len);
int cckd_ftruncate(DEVBLK *dev, int sfx, off_t off);
void *cckd_malloc(DEVBLK *dev, char *id, size_t size);
void *cckd_calloc(DEVBLK *dev, char *id, size_t n, size_t size);
void *cckd_free(DEVBLK *dev, char *id,void *p);
int cckd_read_track(DEVBLK *dev, int trk, BYTE *unitstat);
int cckd_update_track(DEVBLK *dev, int trk, int off,
BYTE *buf, int len, BYTE *unitstat);
int cckd_used(DEVBLK *dev);
int cfba_read_block(DEVBLK *dev, int blkgrp, BYTE *unitstat);
int cfba_write_block(DEVBLK *dev, int blkgrp, int off,
BYTE *buf, int wrlen, BYTE *unitstat);
int cfba_used(DEVBLK *dev);
int cckd_read_trk(DEVBLK *dev, int trk, int ra, BYTE *unitstat);
void cckd_readahead(DEVBLK *dev, int trk);
int cckd_readahead_scan(int *answer, int ix, int i, void *data);
void cckd_ra();
void cckd_flush_cache(DEVBLK *dev);
int cckd_flush_cache_scan(int *answer, int ix, int i, void *data);
void cckd_flush_cache_all();
void cckd_purge_cache(DEVBLK *dev);
int cckd_purge_cache_scan(int *answer, int ix, int i, void *data);
void cckd_writer(void *arg);
int cckd_writer_scan(int *o, int ix, int i, void *data);
off_t cckd_get_space(DEVBLK *dev, int *size, int flags);
void cckd_rel_space(DEVBLK *dev, off_t pos, int len, int size);
void cckd_flush_space(DEVBLK *dev);
int cckd_read_chdr(DEVBLK *dev);
int cckd_write_chdr(DEVBLK *dev);
int cckd_read_l1(DEVBLK *dev);
int cckd_write_l1(DEVBLK *dev);
int cckd_write_l1ent(DEVBLK *dev, int l1x);
int cckd_read_init(DEVBLK *dev);
int cckd_read_fsp(DEVBLK *dev);
int cckd_write_fsp(DEVBLK *dev);
int cckd_read_l2(DEVBLK *dev, int sfx, int l1x);
void cckd_purge_l2(DEVBLK *dev);
int cckd_purge_l2_scan(int *answer, int ix, int i, void *data);
int cckd_steal_l2();
int cckd_steal_l2_scan(int *answer, int ix, int i, void *data);
int cckd_write_l2(DEVBLK *dev);
int cckd_read_l2ent(DEVBLK *dev, CCKD_L2ENT *l2, int trk);
int cckd_write_l2ent(DEVBLK *dev, CCKD_L2ENT *l2, int trk);
int cckd_read_trkimg(DEVBLK *dev, BYTE *buf, int trk, BYTE *unitstat);
int cckd_write_trkimg(DEVBLK *dev, BYTE *buf, int len, int trk, int flags);
int cckd_harden(DEVBLK *dev);
int cckd_trklen(DEVBLK *dev, BYTE *buf);
int cckd_null_trk(DEVBLK *dev, BYTE *buf, int trk, int nullfmt);
int cckd_check_null_trk (DEVBLK *dev, BYTE *buf, int trk, int len);
int cckd_cchh(DEVBLK *dev, BYTE *buf, int trk);
int cckd_validate(DEVBLK *dev, BYTE *buf, int trk, int len);
char *cckd_sf_name(DEVBLK *dev, int sfx);
int cckd_sf_init(DEVBLK *dev);
int cckd_sf_new(DEVBLK *dev);
DLL_EXPORT void *cckd_sf_add(void *data);
DLL_EXPORT void *cckd_sf_remove(void *data);
DLL_EXPORT void *cckd_sf_comp(void *data);
DLL_EXPORT void *cckd_sf_chk(void *data);
DLL_EXPORT void *cckd_sf_stats(void *data);
int cckd_disable_syncio(DEVBLK *dev);
void cckd_lock_devchain(int flag);
void cckd_unlock_devchain();
void cckd_gcol();
int cckd_gc_percolate(DEVBLK *dev, unsigned int size);
int cckd_gc_l2(DEVBLK *dev, BYTE *buf);
DEVBLK *cckd_find_device_by_devnum (U16 devnum);
BYTE *cckd_uncompress(DEVBLK *dev, BYTE *from, int len, int maxlen, int trk);
int cckd_uncompress_zlib(DEVBLK *dev, BYTE *to, BYTE *from, int len, int maxlen);
int cckd_uncompress_bzip2(DEVBLK *dev, BYTE *to, BYTE *from, int len, int maxlen);
int cckd_compress(DEVBLK *dev, BYTE **to, BYTE *from, int len, int comp, int parm);
int cckd_compress_none(DEVBLK *dev, BYTE **to, BYTE *from, int len, int parm);
int cckd_compress_zlib(DEVBLK *dev, BYTE **to, BYTE *from, int len, int parm);
int cckd_compress_bzip2(DEVBLK *dev, BYTE **to, BYTE *from, int len, int parm);
void cckd_command_help();
void cckd_command_opts();
void cckd_command_stats();
void cckd_command_debug();
int cckd_command(char *op, int cmd);
DLL_EXPORT void cckd_print_itrace();
void cckd_trace(DEVBLK *dev, char *msg, ...);
/*-------------------------------------------------------------------*/
/* Definitions for sense data format codes and message codes */
/*-------------------------------------------------------------------*/
#define FORMAT_0 0 /* Program or System Checks */
#define FORMAT_1 1 /* Device Equipment Checks */
#define FORMAT_2 2 /* 3990 Equipment Checks */
#define FORMAT_3 3 /* 3990 Control Checks */
#define FORMAT_4 4 /* Data Checks */
#define FORMAT_5 5 /* Data Check + Displacement */
#define FORMAT_6 6 /* Usage Stats/Overrun Errors*/
#define FORMAT_7 7 /* Device Control Checks */
#define FORMAT_8 8 /* Device Equipment Checks */
#define FORMAT_9 9 /* Device Rd/Wrt/Seek Checks */
#define FORMAT_F 15 /* Cache Storage Checks */
#define MESSAGE_0 0 /* Message 0 */
#define MESSAGE_1 1 /* Message 1 */
#define MESSAGE_2 2 /* Message 2 */
#define MESSAGE_3 3 /* Message 3 */
#define MESSAGE_4 4 /* Message 4 */
#define MESSAGE_5 5 /* Message 5 */
#define MESSAGE_6 6 /* Message 6 */
#define MESSAGE_7 7 /* Message 7 */
#define MESSAGE_8 8 /* Message 8 */
#define MESSAGE_9 9 /* Message 9 */
#define MESSAGE_A 10 /* Message A */
#define MESSAGE_B 11 /* Message B */
#define MESSAGE_C 12 /* Message C */
#define MESSAGE_D 13 /* Message D */
#define MESSAGE_E 14 /* Message E */
#define MESSAGE_F 15 /* Message F */
/*-------------------------------------------------------------------*/
/* Data areas */
/*-------------------------------------------------------------------*/
static CCKD_L2ENT empty_l2[CKDDASD_NULLTRK_FMTMAX+1][256];
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
DEVHND cckddasd_device_hndinfo;
DEVHND cfbadasd_device_hndinfo;
DLL_EXPORT CCKDBLK cckdblk; /* cckd global area */
/*-------------------------------------------------------------------*/
/* CCKD global initialization */
/*-------------------------------------------------------------------*/
int cckddasd_init (int argc, BYTE *argv[])
{
int i, j; /* Loop indexes */
UNREFERENCED(argc);
UNREFERENCED(argv);
if (memcmp (&cckdblk.id, "CCKDBLK ", sizeof(cckdblk.id)) == 0)
return 0;
/* Clear the cckdblk */
memset(&cckdblk, 0, sizeof(CCKDBLK));
/* Initialize locks and conditions */
memcpy (&cckdblk.id, "CCKDBLK ", sizeof(cckdblk.id));
initialize_lock (&cckdblk.gclock);
initialize_lock (&cckdblk.ralock);
initialize_lock (&cckdblk.wrlock);
initialize_lock (&cckdblk.devlock);
initialize_condition (&cckdblk.gccond);
initialize_condition (&cckdblk.racond);
initialize_condition (&cckdblk.wrcond);
initialize_condition (&cckdblk.devcond);
initialize_condition (&cckdblk.termcond);
/* Initialize some variables */
cckdblk.wrprio = 16;
cckdblk.ranbr = CCKD_DEFAULT_RA_SIZE;
cckdblk.ramax = CCKD_DEFAULT_RA;
cckdblk.wrmax = CCKD_DEFAULT_WRITER;
cckdblk.gcmax = CCKD_DEFAULT_GCOL;
cckdblk.gcwait = CCKD_DEFAULT_GCOLWAIT;
cckdblk.gcparm = CCKD_DEFAULT_GCOLPARM;
cckdblk.readaheads = CCKD_DEFAULT_READAHEADS;
cckdblk.freepend = CCKD_DEFAULT_FREEPEND;
#ifdef HAVE_LIBZ
cckdblk.comps |= CCKD_COMPRESS_ZLIB;
#endif
#ifdef CCKD_BZIP2
cckdblk.comps |= CCKD_COMPRESS_BZIP2;
#endif
cckdblk.comp = 0xff;
cckdblk.compparm = -1;
/* Initialize the readahead queue */
cckdblk.ra1st = cckdblk.ralast = -1;
cckdblk.rafree = 0;
for (i = 0; i < cckdblk.ranbr; i++) cckdblk.ra[i].next = i + 1;
cckdblk.ra[cckdblk.ranbr - 1].next = -1;
/* Clear the empty L2 tables */
for (i = 0; i <= CKDDASD_NULLTRK_FMTMAX; i++)
for (j = 0; j < 256; j++)
{
empty_l2[i][j].pos = 0;
empty_l2[i][j].len = empty_l2[i][j].size = i;
}
return 0;
} /* end function cckddasd_init */
/*-------------------------------------------------------------------*/
/* CCKD dasd global termination */
/*-------------------------------------------------------------------*/
int cckddasd_term ()
{
/* Terminate the readahead threads */
obtain_lock (&cckdblk.ralock);
cckdblk.ramax = 0;
if (cckdblk.ras)
{
broadcast_condition (&cckdblk.racond);
wait_condition (&cckdblk.termcond, &cckdblk.ralock);
}
release_lock (&cckdblk.ralock);
destroy_lock (&cckdblk.ralock);
destroy_condition (&cckdblk.racond);
/* Terminate the garbage collection threads */
obtain_lock (&cckdblk.gclock);
cckdblk.gcmax = 0;
if (cckdblk.gcs)
{
broadcast_condition (&cckdblk.gccond);
wait_condition (&cckdblk.termcond, &cckdblk.gclock);
}
release_lock (&cckdblk.gclock);
destroy_lock (&cckdblk.gclock);
destroy_condition (&cckdblk.gccond);
/* Terminate the writer threads */
obtain_lock (&cckdblk.wrlock);
cckdblk.wrmax = 0;
if (cckdblk.wrs)
{
broadcast_condition (&cckdblk.wrcond);
wait_condition (&cckdblk.termcond, &cckdblk.wrlock);
}
release_lock (&cckdblk.wrlock);
destroy_lock (&cckdblk.wrlock);
destroy_condition (&cckdblk.wrcond);
destroy_lock (&cckdblk.devlock);
destroy_condition (&cckdblk.devcond);
destroy_condition (&cckdblk.termcond);
memset(&cckdblk, 0, sizeof(CCKDBLK));
return 0;
} /* end function cckddasd_term */
/*-------------------------------------------------------------------*/
/* CKD dasd initialization */
/*-------------------------------------------------------------------*/
int cckddasd_init_handler ( DEVBLK *dev, int argc, char *argv[] )
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
DEVBLK *dev2; /* -> device in cckd queue */
int i; /* Counter */
int fdflags; /* File flags */
UNREFERENCED(argc);
UNREFERENCED(argv);
/* Initialize the global cckd block if necessary */
if (memcmp (&cckdblk.id, "CCKDBLK ", sizeof(cckdblk.id)))
cckddasd_init (0, NULL);
/* Obtain area for cckd extension */
dev->cckd_ext = cckd = cckd_calloc (dev, "ext", 1, sizeof(CCKDDASD_EXT));
if (cckd == NULL)
return -1;
/* Initialize locks and conditions */
initialize_lock (&cckd->iolock);
initialize_lock (&cckd->filelock);
initialize_condition (&cckd->iocond);
/* Initialize some variables */
obtain_lock (&cckd->filelock);
cckd->l1x = cckd->sfx = cckd->l2active = -1;
dev->cache = cckd->free1st = -1;
cckd->fd[0] = dev->fd;
fdflags = get_file_accmode_flags( dev->fd );
cckd->open[0] = (fdflags & O_RDWR) ? CCKD_OPEN_RW : CCKD_OPEN_RO;
for (i = 1; i <= CCKD_MAX_SF; i++)
{
cckd->fd[i] = -1;
cckd->open[i] = CCKD_OPEN_NONE;
}
cckd->maxsize = sizeof(off_t) > 4 ? 0xffffffffll : 0x7fffffffll;
/* call the chkdsk function */
if (cckd_chkdsk (dev, 0) < 0)
return -1;
/* Perform initial read */
if (cckd_read_init (dev) < 0)
return -1;
if (cckd->fbadasd) dev->ckdtrksz = CFBA_BLOCK_SIZE;
/* open the shadow files */
if (cckd_sf_init (dev) < 0)
{
WRMSG (HHC00300, "E", SSID_TO_LCSS(dev->ssid), dev->devnum);
return -1;
}
/* Update the device handler routines */
if (cckd->ckddasd)
dev->hnd = &cckddasd_device_hndinfo;
else
dev->hnd = &cfbadasd_device_hndinfo;
release_lock (&cckd->filelock);
/* Insert the device into the cckd device queue */
cckd_lock_devchain(1);
for (cckd = NULL, dev2 = cckdblk.dev1st; dev2; dev2 = cckd->devnext)
cckd = dev2->cckd_ext;
if (cckd) cckd->devnext = dev;
else cckdblk.dev1st = dev;
cckd_unlock_devchain();
cckdblk.batch = dev->batch;
if (cckdblk.batch)
{
cckdblk.nostress = 1;
cckdblk.freepend = 0;
cckdblk.linuxnull = 1;
}
return 0;
} /* end function cckddasd_init_handler */
/*-------------------------------------------------------------------*/
/* Close a Compressed CKD Device */
/*-------------------------------------------------------------------*/
int cckddasd_close_device (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int i; /* Index */
cckd = dev->cckd_ext;
/* Wait for readaheads to finish */
obtain_lock(&cckdblk.ralock);
cckd->stopping = 1;
while (cckd->ras)
{
release_lock(&cckdblk.ralock);
usleep(1);
obtain_lock(&cckdblk.ralock);
}
release_lock(&cckdblk.ralock);
/* Flush the cache and wait for the writes to complete */
obtain_lock (&cckd->iolock);
cckd->stopping = 1;
cckd_flush_cache (dev);
while (cckd->wrpending || cckd->ioactive)
{
cckd->iowaiters++;
wait_condition (&cckd->iocond, &cckd->iolock);
cckd->iowaiters--;
cckd_flush_cache (dev);
}
broadcast_condition (&cckd->iocond);
cckd_purge_cache (dev); cckd_purge_l2 (dev);
dev->bufcur = dev->cache = -1;
if (cckd->newbuf) cckd_free (dev, "newbuf", cckd->newbuf);
release_lock (&cckd->iolock);
/* Remove the device from the cckd queue */
cckd_lock_devchain(1);
if (dev == cckdblk.dev1st) cckdblk.dev1st = cckd->devnext;
else
{
DEVBLK *dev2 = cckdblk.dev1st;
CCKDDASD_EXT *cckd2 = dev2->cckd_ext;
while (cckd2->devnext != dev)
{
dev2 = cckd2->devnext; cckd2 = dev2->cckd_ext;
}
cckd2->devnext = cckd->devnext;
}
cckd_unlock_devchain();
/* harden the file */
obtain_lock (&cckd->filelock);
cckd_harden (dev);
/* close the shadow files */
for (i = 1; i <= cckd->sfn; i++)
{
cckd_close (dev, i);
cckd->open[i] = 0;
}
/* free the level 1 tables */
for (i = 0; i <= cckd->sfn; i++)
cckd->l1[i] = cckd_free (dev, "l1", cckd->l1[i]);
/* reset the device handler */
if (cckd->ckddasd)
dev->hnd = &ckddasd_device_hndinfo;
else
dev->hnd = &fbadasd_device_hndinfo;
/* write some statistics */
if (!dev->batch)
cckd_sf_stats (dev);
release_lock (&cckd->filelock);
/* free the cckd extension */
dev->cckd_ext= cckd_free (dev, "ext", cckd);
if (dev->dasdsfn) free (dev->dasdsfn);
dev->dasdsfn = NULL;
close (dev->fd);
dev->fd = -1;
/* If no more devices then perform global termination */
if (cckdblk.dev1st == NULL) cckddasd_term ();
return 0;
} /* end function cckddasd_close_device */
/*-------------------------------------------------------------------*/
/* Compressed ckd start/resume channel program */
/*-------------------------------------------------------------------*/
void cckddasd_start (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
U16 devnum = 0; /* Last active device number */
int trk = 0; /* Last active track */
cckd = dev->cckd_ext;
cckd_trace (dev, "start i/o file[%d] bufcur %d cache[%d]",
cckd->sfn, dev->bufcur, dev->cache);
/* Reset buffer offsets */
dev->bufoff = 0;
dev->bufoffhi = cckd->ckddasd ? dev->ckdtrksz : CFBA_BLOCK_SIZE;
/* Check for merge - synchronous i/o should be disabled */
obtain_lock(&cckd->iolock);
if (cckd->merging)
{
cckd_trace (dev, "start i/o waiting for merge%s","");
while (cckd->merging)
{
cckd->iowaiters++;
wait_condition (&cckd->iocond, &cckd->iolock);
cckd->iowaiters--;
}
dev->bufcur = dev->cache = -1;
}
cckd->ioactive = 1;
cache_lock(CACHE_DEVBUF);
if (dev->cache >= 0)
CCKD_CACHE_GETKEY(dev->cache, devnum, trk);
/* Check if previous active entry is still valid and not busy */
if (dev->cache >= 0 && dev->devnum == devnum && dev->bufcur == trk
&& !(cache_getflag(CACHE_DEVBUF, dev->cache) & CCKD_CACHE_IOBUSY))
{
/* Make the entry active again */
cache_setflag (CACHE_DEVBUF, dev->cache, ~0, CCKD_CACHE_ACTIVE);
/* If the entry is pending write then change it to `updated' */
if (cache_getflag(CACHE_DEVBUF, dev->cache) & CCKD_CACHE_WRITE)
{
cache_setflag (CACHE_DEVBUF, dev->cache, ~CCKD_CACHE_WRITE, CCKD_CACHE_UPDATED);
cckd->wrpending--;
if (cckd->iowaiters && !cckd->wrpending)
broadcast_condition (&cckd->iocond);
}
}
else
dev->bufcur = dev->cache = -1;
cache_unlock (CACHE_DEVBUF);
release_lock (&cckd->iolock);
return;
} /* end function cckddasd_start */
/*-------------------------------------------------------------------*/
/* Compressed ckd end/suspend channel program */
/*-------------------------------------------------------------------*/
void cckddasd_end (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
cckd = dev->cckd_ext;
/* Update length if previous image was updated */
if (dev->bufupd && dev->bufcur >= 0 && dev->cache >= 0)
{
dev->buflen = cckd_trklen (dev, dev->buf);
cache_setval (CACHE_DEVBUF, dev->cache, dev->buflen);
}
dev->bufupd = 0;
cckd_trace (dev, "end i/o bufcur %d cache[%d] waiters %d",
dev->bufcur, dev->cache, cckd->iowaiters);
obtain_lock (&cckd->iolock);
cckd->ioactive = 0;
/* Make the current entry inactive */
if (dev->cache >= 0)
{
cache_lock (CACHE_DEVBUF);
cache_setflag (CACHE_DEVBUF, dev->cache, ~CCKD_CACHE_ACTIVE, 0);
cache_unlock (CACHE_DEVBUF);
}
/* Cause writers to start after first update */
if (cckd->updated && (cckdblk.wrs == 0 || cckd->iowaiters != 0))
cckd_flush_cache (dev);
else if (cckd->iowaiters)
broadcast_condition (&cckd->iocond);
release_lock (&cckd->iolock);
} /* end function cckddasd_end */
/*-------------------------------------------------------------------*/
/* Open a cckd file */
/* */
/* If O_CREAT is not set and mode is non-zero then the error message */
/* will be supressed. */
/*-------------------------------------------------------------------*/
int cckd_open (DEVBLK *dev, int sfx, int flags, mode_t mode)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int err; /* 1 = issue error message */
char pathname[MAX_PATH]; /* file path in host format */
cckd = dev->cckd_ext;
err = !((flags & O_CREAT) == 0 && mode != 0);
if (cckd->fd[sfx] >= 0)
cckd_close (dev, sfx);
hostpath(pathname, cckd_sf_name (dev, sfx), sizeof(pathname));
cckd->fd[sfx] = HOPEN (pathname, flags, mode);
if (sfx == 0) dev->fd = cckd->fd[sfx];
if (cckd->fd[sfx] >= 0)
cckd->open[sfx] = flags & O_RDWR ? CCKD_OPEN_RW :
cckd->open[sfx] == CCKD_OPEN_RW ?
CCKD_OPEN_RD : CCKD_OPEN_RO;
else
{
if (err)
{
WRMSG (HHC00301, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"open()", strerror(errno));
cckd_trace (dev, "file[%d] fd[%d] open %s error flags %8.8x mode %8.8x",
sfx, cckd->fd[sfx], cckd_sf_name (dev, sfx), flags, mode);
cckd_print_itrace ();
}
cckd->open[sfx] = CCKD_OPEN_NONE;
}
cckd_trace (dev, "file[%d] fd[%d] open %s, flags %8.8x mode %8.8x",
sfx, cckd->fd[sfx], cckd_sf_name (dev, sfx), flags, mode);
return cckd->fd[sfx];
} /* end function cckd_open */
/*-------------------------------------------------------------------*/
/* Close a cckd file */
/*-------------------------------------------------------------------*/
int cckd_close (DEVBLK *dev, int sfx)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc = 0; /* Return code */
cckd = dev->cckd_ext;
cckd_trace (dev, "file[%d] fd[%d] close %s",
sfx, cckd->fd[sfx], cckd_sf_name(dev, sfx));
if (cckd->fd[sfx] >= 0)
rc = close (cckd->fd[sfx]);
if (rc < 0)
{
WRMSG (HHC00301, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"close()", strerror(errno));
cckd_print_itrace ();
}
cckd->fd[sfx] = -1;
if (sfx == 0) dev->fd = -1;
return rc;
} /* end function cckd_close */
/*-------------------------------------------------------------------*/
/* Read from a cckd file */
/*-------------------------------------------------------------------*/
int cckd_read (DEVBLK *dev, int sfx, off_t off, void *buf, unsigned int len)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
cckd = dev->cckd_ext;
cckd_trace (dev, "file[%d] fd[%d] read, off 0x"I64_FMTx" len %d",
sfx, cckd->fd[sfx], off, len);
/* Seek to specified offset */
if (lseek (cckd->fd[sfx], off, SEEK_SET) < 0)
{
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"lseek()", off, strerror(errno));
cckd_print_itrace ();
return -1;
}
/* Read the data */
rc = read (cckd->fd[sfx], buf, len);
if (rc < (int)len)
{
if (rc < 0)
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"read()", off, strerror(errno));
else
{
char buf[128];
MSGBUF( buf, "read incomplete: read %d, expected %d", rc, len);
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"read()", off, buf);
}
cckd_print_itrace ();
return -1;
}
return rc;
} /* end function cckd_read */
/*-------------------------------------------------------------------*/
/* Write to a cckd file */
/*-------------------------------------------------------------------*/
int cckd_write (DEVBLK *dev, int sfx, off_t off, void *buf, unsigned int len)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc = 0; /* Return code */
cckd = dev->cckd_ext;
cckd_trace (dev, "file[%d] fd[%d] write, off 0x"I64_FMTx" len %d",
sfx, cckd->fd[sfx], off, len);
/* Seek to specified offset */
if (lseek (cckd->fd[sfx], off, SEEK_SET) < 0)
{
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"lseek()", off, strerror(errno));
return -1;
}
/* Write the data */
rc = write (cckd->fd[sfx], buf, len);
if (rc < (int)len)
{
if (rc < 0)
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"write()", off, strerror(errno));
else
{
char buf[128];
MSGBUF( buf, "write incomplete: write %d, expected %d", rc, len);
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"write()", off, buf);
}
cckd_print_itrace ();
return -1;
}
return rc;
} /* end function cckd_write */
/*-------------------------------------------------------------------*/
/* Truncate a cckd file */
/*-------------------------------------------------------------------*/
int cckd_ftruncate(DEVBLK *dev, int sfx, off_t off)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
cckd = dev->cckd_ext;
cckd_trace (dev, "file[%d] fd[%d] ftruncate, off 0x"I64_FMTx,
sfx, cckd->fd[sfx], off);
/* Truncate the file */
if (ftruncate (cckd->fd[sfx], off) < 0)
{
WRMSG (HHC00302, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, sfx, cckd_sf_name (dev, sfx),
"ftruncate()", off, strerror(errno));
cckd_print_itrace ();
return -1;
}
return 0;
} /* end function cckd_ftruncate */
/*-------------------------------------------------------------------*/
/* malloc */
/*-------------------------------------------------------------------*/
void *cckd_malloc (DEVBLK *dev, char *id, size_t size)
{
void *p; /* Pointer */
p = malloc (size);
cckd_trace (dev, "%s malloc %p len %ld", id, p, (long)size);
if (p == NULL)
{
char buf[64];
MSGBUF( buf, "malloc(%d)", (int)size);
WRMSG (HHC00303, "E", dev ? SSID_TO_LCSS(dev->ssid) : 0, dev ? dev->devnum : 0, buf, strerror(errno));
cckd_print_itrace ();
}
return p;
} /* end function cckd_malloc */
/*-------------------------------------------------------------------*/
/* calloc */
/*-------------------------------------------------------------------*/
void *cckd_calloc (DEVBLK *dev, char *id, size_t n, size_t size)
{
void *p; /* Pointer */
p = calloc (n, size);
cckd_trace (dev, "%s calloc %p len %ld", id, p, n*(long)size);
if (p == NULL)
{
char buf[64];
MSGBUF( buf, "calloc(%d, %d)", (int)n, (int)size);
WRMSG (HHC00303, "E", dev ? SSID_TO_LCSS(dev->ssid) : 0, dev ? dev->devnum : 0, buf, strerror(errno));
cckd_print_itrace ();
}
return p;
} /* end function cckd_calloc */
/*-------------------------------------------------------------------*/
/* free */
/*-------------------------------------------------------------------*/
void *cckd_free (DEVBLK *dev, char *id, void *p)
{
cckd_trace (dev, "%s free %p", id, p);
if (p) free (p);
return (void*)NULL;
} /* end function cckd_free */
/*-------------------------------------------------------------------*/
/* Compressed ckd read track image */
/*-------------------------------------------------------------------*/
int cckd_read_track (DEVBLK *dev, int trk, BYTE *unitstat)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int len; /* Compressed length */
BYTE *newbuf; /* Uncompressed buffer */
int cache; /* New active cache entry */
int syncio; /* Syncio indicator */
cckd = dev->cckd_ext;
/* Update length if previous image was updated */
if (dev->bufupd && dev->bufcur >= 0 && dev->cache >= 0)
{
dev->buflen = cckd_trklen (dev, dev->buf);
cache_setval (CACHE_DEVBUF, dev->cache, dev->buflen);
}
/* Turn off the synchronous I/O bit if trk overflow or trk 0 */
syncio = dev->syncio_active;
if (dev->ckdtrkof || trk == 0)
dev->syncio_active = 0;
/* Reset buffer offsets */
dev->bufoff = 0;
dev->bufoffhi = dev->ckdtrksz;
/* Check if reading the same track image */
if (trk == dev->bufcur && dev->cache >= 0)
{
/* Track image may be compressed */
if ((dev->buf[0] & CCKD_COMPRESS_MASK) != 0
&& (dev->buf[0] & dev->comps) == 0)
{
#if 0
/* Return if synchronous i/o */
if (dev->syncio_active)
{
cckd_trace (dev, "read trk %d syncio compressed", trk);
cckdblk.stats_synciomisses++;
dev->syncio_retry = 1;
return -1;
}
#endif
len = cache_getval(CACHE_DEVBUF, dev->cache);
newbuf = cckd_uncompress (dev, dev->buf, len, dev->ckdtrksz, trk);
if (newbuf == NULL) {
ckd_build_sense (dev, SENSE_EC, 0, 0, FORMAT_1, MESSAGE_0);
*unitstat = CSW_CE | CSW_DE | CSW_UC;
dev->bufcur = dev->cache = -1;
dev->syncio_active = syncio;
return -1;
}
cache_setbuf (CACHE_DEVBUF, dev->cache, newbuf, dev->ckdtrksz);
dev->buf = newbuf;
dev->buflen = cckd_trklen (dev, newbuf);
cache_setval (CACHE_DEVBUF, dev->cache, dev->buflen);
dev->bufsize = cache_getlen (CACHE_DEVBUF, dev->cache);
dev->bufupd = 0;
cckd_trace (dev, "read trk %d uncompressed len %d",
trk, dev->buflen);
}
dev->comp = dev->buf[0] & CCKD_COMPRESS_MASK;
if (dev->comp != 0) dev->compoff = CKDDASD_TRKHDR_SIZE;
return 0;
}
cckd_trace (dev, "read trk %d (%s)", trk,
dev->syncio_active ? "synchronous" : "asynchronous");
/* read the new track */
dev->bufupd = 0;
*unitstat = 0;
cache = cckd_read_trk (dev, trk, 0, unitstat);
if (cache < 0)
{
dev->bufcur = dev->cache = -1;
return -1;
}
dev->cache = cache;
dev->buf = cache_getbuf (CACHE_DEVBUF, dev->cache, 0);
dev->bufcur = trk;
dev->bufoff = 0;
dev->bufoffhi = dev->ckdtrksz;
dev->buflen = cache_getval (CACHE_DEVBUF, dev->cache);
dev->bufsize = cache_getlen (CACHE_DEVBUF, dev->cache);
dev->comp = dev->buf[0] & CCKD_COMPRESS_MASK;
if (dev->comp != 0) dev->compoff = CKDDASD_TRKHDR_SIZE;
/* If the image is compressed then call ourself recursively
to cause the image to get uncompressed */
if (dev->comp != 0 && (dev->comp & dev->comps) == 0)
rc = cckd_read_track (dev, trk, unitstat);
else
rc = 0;
dev->syncio_active = syncio;
return rc;
} /* end function cckd_read_track */
/*-------------------------------------------------------------------*/
/* Compressed ckd update track image */
/*-------------------------------------------------------------------*/
int cckd_update_track (DEVBLK *dev, int trk, int off,
BYTE *buf, int len, BYTE *unitstat)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
cckd = dev->cckd_ext;
/* Error if opened read-only */
if (dev->ckdrdonly && cckd->sfn == 0)
{
ckd_build_sense (dev, SENSE_EC, SENSE1_WRI, 0,FORMAT_1, MESSAGE_0);
*unitstat = CSW_CE | CSW_DE | CSW_UC;
dev->bufcur = dev->cache = -1;
return -1;
}
/* If the track is not current or compressed then read it.
`dev->comps' is set to zero forcing the read routine to
uncompress the image. */
if (trk != dev->bufcur || (dev->buf[0] & CCKD_COMPRESS_MASK) != 0)
{
dev->comps = 0;
rc = (dev->hnd->read) (dev, trk, unitstat);
if (rc < 0)
{
dev->bufcur = dev->cache = -1;
return -1;
}
}
/* Invalid track format if going past buffer end */
if (off + len > dev->ckdtrksz)
{
ckd_build_sense (dev, 0, SENSE1_ITF, 0, 0, 0);
*unitstat = CSW_CE | CSW_DE | CSW_UC;
dev->bufcur = dev->cache = -1;
return -1;
}
/* Copy the data into the buffer */
if (buf && len > 0) memcpy (dev->buf + off, buf, len);
cckd_trace (dev, "updt trk %d offset %d length %d",
trk, off, len);
/* Update the cache entry */
cache_setflag (CACHE_DEVBUF, dev->cache, ~0, CCKD_CACHE_UPDATED | CCKD_CACHE_USED);
cckd->updated = 1;
/* Notify the shared server of the update */
if (!dev->bufupd)
{
dev->bufupd = 1;
shared_update_notify (dev, trk);
}
return len;
} /* end function cckd_update_track */
/*-------------------------------------------------------------------*/
/* Return used cylinders */
/*-------------------------------------------------------------------*/
int cckd_used (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int l1x, l2x; /* Lookup table indexes */
int sfx; /* Shadow file suffix */
CCKD_L2ENT l2; /* Copied level 2 entry */
cckd = dev->cckd_ext;
obtain_lock (&cckd->filelock);
/* Find the last used level 1 table entry */
for (l1x = cckd->cdevhdr[0].numl1tab - 1; l1x > 0; l1x--)
{
sfx = cckd->sfn;
while (cckd->l1[sfx][l1x] == 0xffffffff && sfx > 0) sfx--;
if (cckd->l1[sfx][l1x]) break;
}
/* Find the last used level 2 table entry */
for (l2x = 255; l2x >= 0; l2x--)
{
rc = cckd_read_l2ent (dev, &l2, l1x * 256 + l2x);
if (rc < 0 || l2.pos != 0) break;