forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cckddasd64.c
4795 lines (3997 loc) · 161 KB
/
cckddasd64.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
/* CCKDDASD64.C (C) Copyright Roger Bowler, 1999-2012 */
/* (C) Copyright Greg Smith, 2002-2012 */
/* (C) and others 2013-2021 */
/* */
/* CCKD (Compressed CKD) 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 _CCKDDASD64_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "devtype.h"
#include "opcode.h"
#include "dasdblks.h"
#include "cckddasd.h"
#include "ccwarn.h"
DISABLE_GCC_UNUSED_SET_WARNING;
/*-------------------------------------------------------------------*/
/* Compressed CKD dasd initialization */
/*-------------------------------------------------------------------*/
int cckd64_dasd_init_handler ( DEVBLK *dev, int argc, char *argv[] )
{
CCKD64_EXT *cckd; /* -> cckd extension */
DEVBLK *dev2; /* -> device in cckd queue */
int i; /* Counter */
int fdflags; /* File flags */
char buf[32]; /* Work buffer */
UNREFERENCED( argc );
UNREFERENCED( argv );
/* Initialize the global cckd block if necessary */
if (memcmp( &cckdblk.id, CCKDBLK_ID, sizeof( cckdblk.id )))
cckd_dasd_init( 0, NULL );
/* Obtain area for cckd extension */
dev->cckd_ext = cckd = cckd_calloc( dev, "ext", 1, sizeof( CCKD64_EXT ));
if (cckd == NULL)
return -1;
/* Initialize locks and conditions */
initialize_lock( &cckd->cckdiolock );
MSGBUF( buf, "&cckd->cckdiolock %1d:%04X", LCSS_DEVNUM );
set_lock_name( &cckd->cckdiolock, buf );
initialize_lock( &cckd->filelock );
MSGBUF( buf, "&cckd->filelock %1d:%04X", LCSS_DEVNUM );
set_lock_name( &cckd->filelock, buf );
initialize_condition( &cckd->cckdiocond );
/* Initialize some variables */
obtain_lock (&cckd->filelock);
cckd->L1idx = cckd->sfx = cckd->L2_active = -1;
dev->cache = cckd->free_idx1st = -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->cckd_maxsize = CCKD64_MAXSIZE;
/* call the chkdsk function */
if (cckd64_chkdsk (dev, 0) < 0)
return -1;
/* Perform initial read */
if (cckd64_read_init (dev) < 0)
return -1;
if (cckd->fbadasd) dev->ckdtrksz = CFBA_BLKGRP_SIZE;
/* open the shadow files */
if (cckd64_sf_init (dev) < 0)
{
WRMSG (HHC00300, "E", LCSS_DEVNUM);
return -1;
}
/* Update the device handler routines */
if (cckd->ckddasd)
dev->hnd = &cckd_dasd_device_hndinfo;
else
dev->hnd = &cfba_dasd_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 cckd64_dasd_init_handler */
/*-------------------------------------------------------------------*/
/* Close a Compressed CKD Device */
/*-------------------------------------------------------------------*/
int cckd64_dasd_close_device (DEVBLK *dev)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc, i; /* Return code, Loop index */
if (!dev->cckd64)
return cckd_dasd_close_device( dev );
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->cckdiolock );
{
cckd->stopping = 1;
cckd64_flush_cache( dev );
while (cckd->wrpending || cckd->cckdioact)
{
cckd->cckdwaiters++;
rc = timed_wait_condition_relative_usecs(
&cckd->cckdiocond, &cckd->cckdiolock, 1000000, NULL );
cckd->cckdwaiters--;
cckd64_flush_cache( dev );
if (EINTR == rc)
continue;
/* Prevent rare but possible hang at shutdown */
if (1
&& ETIMEDOUT == rc
&& (cckd->wrpending || cckd->cckdioact)
&& sysblk.shutdown
)
{
CCKD_TRACE( "closing device while wrpending=%d cckdioact=%d",
cckd->wrpending, cckd->cckdioact );
// "%1d:%04X CCKD file %s: closing device while wrpending=%d cckdioact=%d"
WRMSG( HHC00381, "W", LCSS_DEVNUM, dev->filename,
cckd->wrpending, cckd->cckdioact );
break;
}
}
broadcast_condition( &cckd->cckdiocond );
cckd64_purge_cache( dev );
cckd64_purge_l2( dev );
dev->bufcur = -1;
dev->cache = -1;
if (cckd->newbuf)
cckd_free( dev, "newbuf", cckd->newbuf );
}
release_lock( &cckd->cckdiolock );
/* Remove the device from the cckd queue */
cckd_lock_devchain(1);
if (dev == cckdblk.dev1st) cckdblk.dev1st = cckd->devnext;
else
{
DEVBLK *dev2 = cckdblk.dev1st;
CCKD64_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);
cckd64_harden (dev);
/* close the shadow files */
for (i = 1; i <= cckd->sfn; i++)
{
cckd64_close (dev, i);
cckd->open[i] = 0;
}
/* free the level 1 tables */
for (i = 0; i <= cckd->sfn; i++)
cckd->L1tab[i] = cckd_free (dev, "l1", cckd->L1tab[i]);
/* reset the device handler */
if (cckd->ckddasd)
dev->hnd = &ckd_dasd_device_hndinfo;
else
dev->hnd = &fba_dasd_device_hndinfo;
/* write some statistics */
if (!dev->batch && !cckdblk.nosfd)
cckd64_sf_stats (dev);
release_lock (&cckd->filelock);
/* Destroy the cckd extension's locks and conditions */
destroy_lock( &cckd->cckdiolock );
destroy_lock( &cckd->filelock );
destroy_condition( &cckd->cckdiocond );
/* free the cckd extension itself */
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 */
cckd_dasd_term_if_appropriate();
dev->buf = NULL;
dev->bufsize = 0;
return 0;
} /* end function cckd64_dasd_close_device */
/*-------------------------------------------------------------------*/
/* Compressed ckd start/resume channel program */
/*-------------------------------------------------------------------*/
void cckd64_dasd_start (DEVBLK *dev)
{
CCKD64_EXT *cckd; /* -> cckd extension */
U16 devnum = 0; /* Last active device number */
int trk = 0; /* Last active track */
if (!dev->cckd64)
{
cckd_dasd_start( dev );
return;
}
cckd = dev->cckd_ext;
CCKD_TRACE( "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_BLKGRP_SIZE;
/* Check for merge */
obtain_lock(&cckd->cckdiolock);
if (cckd->merging)
{
CCKD_TRACE( "start i/o waiting for merge%s","");
while (cckd->merging)
{
cckd->cckdwaiters++;
wait_condition (&cckd->cckdiocond, &cckd->cckdiolock);
cckd->cckdwaiters--;
}
dev->bufcur = dev->cache = -1;
}
cckd->cckdioact = 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->cckdwaiters && !cckd->wrpending)
broadcast_condition (&cckd->cckdiocond);
}
}
else
dev->bufcur = dev->cache = -1;
cache_unlock (CACHE_DEVBUF);
release_lock (&cckd->cckdiolock);
return;
} /* end function cckd64_dasd_start */
/*-------------------------------------------------------------------*/
/* Compressed ckd end/suspend channel program */
/*-------------------------------------------------------------------*/
void cckd64_dasd_end (DEVBLK *dev)
{
CCKD64_EXT *cckd; /* -> cckd extension */
if (!dev->cckd64)
{
cckd_dasd_end( dev );
return;
}
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( "end i/o bufcur %d cache[%d] waiters %d",
dev->bufcur, dev->cache, cckd->cckdwaiters);
obtain_lock (&cckd->cckdiolock);
cckd->cckdioact = 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->cckdwaiters != 0))
cckd64_flush_cache (dev);
else if (cckd->cckdwaiters)
broadcast_condition (&cckd->cckdiocond);
release_lock (&cckd->cckdiolock);
} /* end function cckd64_dasd_end */
/*-------------------------------------------------------------------*/
/* Open a cckd file */
/* */
/* If O_CREAT is not set and mode is non-zero then the error message */
/* will be supressed. */
/*-------------------------------------------------------------------*/
int cckd64_open (DEVBLK *dev, int sfx, int flags, mode_t mode)
{
CCKD64_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)
cckd64_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", LCSS_DEVNUM, sfx, cckd_sf_name (dev, sfx),
"open()", strerror(errno));
CCKD_TRACE( "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( "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 cckd64_close (DEVBLK *dev, int sfx)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc = 0; /* Return code */
cckd = dev->cckd_ext;
CCKD_TRACE( "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", LCSS_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 cckd64_read( DEVBLK* dev, int sfx, U64 off, void* buf, unsigned int len )
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
cckd = dev->cckd_ext;
CCKD_TRACE( "file[%d] fd[%d] read, off 0x%16.16"PRIx64" len %d",
sfx, cckd->fd[ sfx ], off, len );
/* Seek to specified offset */
if (lseek( cckd->fd[ sfx ], off, SEEK_SET ) < 0)
{
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_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)
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_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 );
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_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 cckd64_write( DEVBLK* dev, int sfx, U64 off, void* buf, unsigned int len )
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc = 0; /* Return code */
cckd = dev->cckd_ext;
CCKD_TRACE( "file[%d] fd[%d] write, off 0x%16.16"PRIx64" len %d",
sfx, cckd->fd[ sfx ], off, len );
/* Seek to specified offset */
if (lseek( cckd->fd[ sfx ], off, SEEK_SET ) < 0)
{
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_DEVNUM, sfx, cckd_sf_name( dev, sfx ),
"lseek()", off, strerror( errno ));
cckd_print_itrace();
return -1;
}
/* Write the data */
rc = write( cckd->fd[ sfx ], buf, len );
if (rc < (int)len)
{
if (rc < 0)
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_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 );
// "%1d:%04X CCKD file[%d] %s: error in function %s at offset 0x%16.16"PRIX64": %s"
WRMSG( HHC00302, "E", LCSS_DEVNUM, sfx, cckd_sf_name( dev, sfx ),
"write()", off, buf );
}
cckd_print_itrace();
return -1;
}
return rc;
} /* end function cckd64_write */
/*-------------------------------------------------------------------*/
/* Truncate a cckd file */
/*-------------------------------------------------------------------*/
int cckd64_ftruncate(DEVBLK *dev, int sfx, U64 off)
{
CCKD64_EXT *cckd; /* -> cckd extension */
cckd = dev->cckd_ext;
CCKD_TRACE( "file[%d] fd[%d] ftruncate, off 0x%16.16"PRIx64,
sfx, cckd->fd[sfx], off);
/* Truncate the file */
if (ftruncate (cckd->fd[sfx], off) < 0)
{
WRMSG (HHC00302, "E", LCSS_DEVNUM, sfx, cckd_sf_name (dev, sfx),
"ftruncate()", off, strerror(errno));
cckd_print_itrace ();
return -1;
}
return 0;
} /* end function cckd_ftruncate */
/*-------------------------------------------------------------------*/
/* Compressed ckd read track image */
/*-------------------------------------------------------------------*/
int cckd64_read_track (DEVBLK *dev, int trk, BYTE *unitstat)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int len; /* Compressed length */
BYTE *newbuf; /* Uncompressed buffer */
int cache; /* New active cache entry */
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);
}
/* 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)
{
len = cache_getval(CACHE_DEVBUF, dev->cache);
newbuf = cckd64_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;
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( "read trk %d uncompressed len %d",
trk, dev->buflen);
}
dev->comp = dev->buf[0] & CCKD_COMPRESS_MASK;
if (dev->comp != 0) dev->compoff = CKD_TRKHDR_SIZE;
return 0;
}
CCKD_TRACE( "read trk %d (%s)", trk, "asynchronous");
/* read the new track */
dev->bufupd = 0;
*unitstat = 0;
cache = cckd64_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 = CKD_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 = cckd64_read_track (dev, trk, unitstat);
else
rc = 0;
return rc;
} /* end function cckd64_read_track */
/*-------------------------------------------------------------------*/
/* Compressed ckd update track image */
/*-------------------------------------------------------------------*/
int cckd64_update_track (DEVBLK *dev, int trk, int off,
BYTE *buf, int len, BYTE *unitstat)
{
CCKD64_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( "updt trk %d offset %"PRId64" 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 cckd64_update_track */
/*-------------------------------------------------------------------*/
/* Return used cylinders */
/*-------------------------------------------------------------------*/
int cckd64_used (DEVBLK *dev)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int L1idx, l2x; /* Lookup table indexes */
int sfx; /* Shadow file suffix */
CCKD64_L2ENT l2; /* Copied level 2 entry */
if (!dev->cckd64)
return cckd_used( dev );
cckd = dev->cckd_ext;
obtain_lock (&cckd->filelock);
/* Find the last used level 1 table entry */
for (L1idx = cckd->cdevhdr[0].num_L1tab - 1; L1idx > 0; L1idx--)
{
sfx = cckd->sfn;
while (cckd->L1tab[sfx][L1idx] == CCKD64_MAXSIZE && sfx > 0) sfx--;
if (cckd->L1tab[sfx][L1idx]) break;
}
/* Find the last used level 2 table entry */
for (l2x = 255; l2x >= 0; l2x--)
{
rc = cckd64_read_l2ent (dev, &l2, L1idx * 256 + l2x);
if (rc < 0 || l2.L2_trkoff != 0) break;
}
release_lock (&cckd->filelock);
return (L1idx * 256 + l2x + dev->ckdheads) / dev->ckdheads;
}
/*-------------------------------------------------------------------*/
/* Compressed fba read block(s) */
/*-------------------------------------------------------------------*/
int cfba64_read_block (DEVBLK *dev, int blkgrp, BYTE *unitstat)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int cache; /* New active cache entry */
BYTE *cbuf; /* -> cache buffer */
BYTE *newbuf; /* Uncompressed buffer */
int len; /* Compressed length */
int maxlen; /* Size for cache entry */
cckd = dev->cckd_ext;
if (dev->cache >= 0)
cbuf = cache_getbuf (CACHE_DEVBUF, dev->cache, 0);
else
cbuf = NULL;
maxlen = CFBA_BLKGRP_SIZE + CKD_TRKHDR_SIZE;
/* Return if reading the same track image */
if (blkgrp == dev->bufcur && dev->cache >= 0)
{
/* Block group image may be compressed */
if ((cbuf[0] & CCKD_COMPRESS_MASK) != 0
&& (cbuf[0] & dev->comps) == 0)
{
len = cache_getval(CACHE_DEVBUF, dev->cache) + CKD_TRKHDR_SIZE;
newbuf = cckd64_uncompress (dev, cbuf, len, maxlen, blkgrp);
if (newbuf == NULL) {
dev->sense[0] = SENSE_EC;
*unitstat = CSW_CE | CSW_DE | CSW_UC;
dev->bufcur = dev->cache = -1;
return -1;
}
cache_setbuf (CACHE_DEVBUF, dev->cache, newbuf, maxlen);
cbuf = newbuf;
dev->buf = newbuf + CKD_TRKHDR_SIZE;
dev->buflen = CFBA_BLKGRP_SIZE;
cache_setval (CACHE_DEVBUF, dev->cache, dev->buflen);
dev->bufsize = cache_getlen (CACHE_DEVBUF, dev->cache);
dev->bufupd = 0;
CCKD_TRACE( "read bkgrp %d uncompressed len %d",
blkgrp, dev->buflen);
}
dev->comp = cbuf[0] & CCKD_COMPRESS_MASK;
return 0;
}
CCKD_TRACE( "read blkgrp %d (%s)", blkgrp, "asynchronous");
/* Read the new blkgrp */
dev->bufupd = 0;
*unitstat = 0;
cache = cckd64_read_trk (dev, blkgrp, 0, unitstat);
if (cache < 0)
{
dev->bufcur = dev->cache = -1;
return -1;
}
dev->cache = cache;
cbuf = cache_getbuf (CACHE_DEVBUF, dev->cache, 0);
dev->buf = cbuf + CKD_TRKHDR_SIZE;
dev->bufcur = blkgrp;
dev->bufoff = 0;
dev->bufoffhi = CFBA_BLKGRP_SIZE;
dev->buflen = CFBA_BLKGRP_SIZE;
cache_setval (CACHE_DEVBUF, dev->cache, dev->buflen);
dev->bufsize = cache_getlen (CACHE_DEVBUF, dev->cache);
dev->comp = cbuf[0] & CCKD_COMPRESS_MASK;
/* If the image is compressed then call ourself recursively
to cause the image to get uncompressed. This is because
`bufcur' will match blkgrp and `comps' won't match `comp' */
if (dev->comp != 0 && (dev->comp & dev->comps) == 0)
rc = cfba64_read_block (dev, blkgrp, unitstat);
else
rc = 0;
return rc;
} /* end function cfba64_read_block */
/*-------------------------------------------------------------------*/
/* Compressed fba write block(s) */
/*-------------------------------------------------------------------*/
int cfba64_write_block (DEVBLK *dev, int blkgrp, int off,
BYTE *buf, int len, BYTE *unitstat)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
BYTE *cbuf; /* -> cache buffer */
cckd = dev->cckd_ext;
if (dev->cache >= 0)
cbuf = cache_getbuf (CACHE_DEVBUF, dev->cache, 0);
else
cbuf = NULL;
/* Read the block group if it's not current or compressed.
`dev->comps' is set to zero forcing the read routine to
uncompress the image. */
if (blkgrp != dev->bufcur || (cbuf[0] & CCKD_COMPRESS_MASK) != 0)
{
dev->comps = 0;
rc = (dev->hnd->read) (dev, blkgrp, unitstat);
if (rc < 0)
{
dev->bufcur = dev->cache = -1;
return -1;
}
}
/* Copy the data into the buffer */
if (buf) memcpy (dev->buf + off, buf, 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, blkgrp);
}
return len;
} /* end function cfba64_write_block */
/*-------------------------------------------------------------------*/
/* Return used blocks */
/*-------------------------------------------------------------------*/
int cfba64_used (DEVBLK *dev)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int L1idx, l2x; /* Lookup table indexes */
int sfx; /* Shadow file suffix */
CCKD64_L2ENT l2; /* Copied level 2 entry */
if (!dev->cckd64)
return cfba_used( dev );
cckd = dev->cckd_ext;
obtain_lock (&cckd->filelock);
/* Find the last used level 1 table entry */
for (L1idx = cckd->cdevhdr[0].num_L1tab - 1; L1idx > 0; L1idx--)
{
sfx = cckd->sfn;
while (cckd->L1tab[sfx][L1idx] == CCKD64_MAXSIZE && sfx > 0) sfx--;
if (cckd->L1tab[sfx][L1idx]) break;
}
/* Find the last used level 2 table entry */
for (l2x = 255; l2x >= 0; l2x--)
{
rc = cckd64_read_l2ent (dev, &l2, L1idx * 256 + l2x);
if (rc < 0 || l2.L2_trkoff != 0) break;
}
release_lock (&cckd->filelock);
return (L1idx * 256 + l2x + CFBA_BLKS_PER_GRP) / CFBA_BLKS_PER_GRP;
}
/*-------------------------------------------------------------------*/
/* Read a track image */
/* */
/* This routine can be called by the i/o thread (`ra' == 0) or */
/* by readahead threads (0 < `ra' <= cckdblk.ramax). */
/* */
/*-------------------------------------------------------------------*/
int cckd64_read_trk(DEVBLK *dev, int trk, int ra, BYTE *unitstat)
{
CCKD64_EXT *cckd; /* -> cckd extension */
int fnd; /* Cache index for hit */
int lru; /* Oldest unused cache index */
int len; /* Length of track image */
int maxlen; /* Length for buffer */
int curtrk = -1; /* Current track (at entry) */
U16 devnum; /* Device number */
U32 oldtrk; /* Stolen track number */
U32 flag; /* Cache flag */
BYTE *buf; /* Read buffer */
if (!dev->cckd64)
return cckd_read_trk( dev, trk, ra, unitstat );
cckd = dev->cckd_ext;
CCKD_TRACE( "%d rdtrk %d", ra, trk);
maxlen = cckd->ckddasd ? dev->ckdtrksz
: CKD_TRKHDR_SIZE + CFBA_BLKGRP_SIZE;
if (!ra) obtain_lock (&cckd->cckdiolock);
cache_lock (CACHE_DEVBUF);
/* Inactivate the old entry */
if (!ra)
{
curtrk = dev->bufcur;
if (dev->cache >= 0)
cache_setflag(CACHE_DEVBUF, dev->cache, ~CCKD_CACHE_ACTIVE, 0);
dev->bufcur = dev->cache = -1;
}
cckd_read_trk_retry:
/* scan the cache array for the track */
fnd = cache_lookup (CACHE_DEVBUF, CCKD_CACHE_SETKEY(dev->devnum, trk), &lru);
/* check for cache hit */
if (fnd >= 0)
{
if (ra) /* readahead doesn't care about a cache hit */
{ cache_unlock (CACHE_DEVBUF);
return fnd;
}
/* Mark the new entry active */
cache_setflag(CACHE_DEVBUF, fnd, ~0, CCKD_CACHE_ACTIVE | CCKD_CACHE_USED);
cache_setage(CACHE_DEVBUF, fnd);
/* If the entry is pending write then change it to `updated' */
if (cache_getflag(CACHE_DEVBUF, fnd) & CCKD_CACHE_WRITE)
{
cache_setflag(CACHE_DEVBUF, fnd, ~CCKD_CACHE_WRITE, CCKD_CACHE_UPDATED);
cckd->wrpending--;
if (cckd->cckdwaiters && !cckd->wrpending)
broadcast_condition (&cckd->cckdiocond);
}
buf = cache_getbuf(CACHE_DEVBUF, fnd, 0);
cache_unlock (CACHE_DEVBUF);
CCKD_TRACE( "%d rdtrk[%d] %d cache hit buf %p:%2.2x%2.2x%2.2x%2.2x%2.2x",
ra, fnd, trk, buf, buf[0], buf[1], buf[2], buf[3], buf[4]);
cckdblk.stats_switches++; cckd->switches++;
cckdblk.stats_cachehits++; cckd->cachehits++;