-
Notifications
You must be signed in to change notification settings - Fork 6
/
cckddasd.c
6803 lines (5732 loc) · 227 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 */
/* (C) and others 2013-2022 */
/* */
/* 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 _CCKDDASD_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;
/*-------------------------------------------------------------------*/
/* Global Variables */
/*-------------------------------------------------------------------*/
DLL_EXPORT CCKDBLK cckdblk; /* cckd global area */
char* compname [] = { "none", "zlib", "bzip2" };
CCKD_L2ENT empty_l2 [ CKD_NULLTRK_FMTMAX + 1 ][256] = {0};
CCKD64_L2ENT empty64_l2 [ CKD_NULLTRK_FMTMAX + 1 ][256] = {0};
DLL_EXPORT int gctab[5] = /* Garbage Collection parameters */
{
4096, /* critical 50% - 100% */
2048, /* severe 25% - 50% */
1024, /* moderate 12.5% - 25% */
512, /* light 6.3% - 12.5% */
256 /* none 0% - 6.3% */
};
/*-------------------------------------------------------------------*/
/* CCKD global initialization */
/*-------------------------------------------------------------------*/
int cckd_dasd_init( int argc, BYTE* argv[] )
{
int i, j; /* Loop indexes */
UNREFERENCED(argc);
UNREFERENCED(argv);
if (memcmp( &cckdblk.id, CCKDBLK_ID, sizeof( cckdblk.id )) == 0)
return 0;
/* Clear the cckdblk */
memset( &cckdblk, 0, sizeof( cckdblk ));
/* Initialize locks and conditions */
memcpy( &cckdblk.id, CCKDBLK_ID, sizeof( cckdblk.id ));
initialize_lock( &cckdblk.gclock );
initialize_lock( &cckdblk.ralock );
initialize_lock( &cckdblk.wrlock );
initialize_lock( &cckdblk.devlock );
initialize_lock( &cckdblk.trclock );
initialize_condition( &cckdblk.gccond );
initialize_condition( &cckdblk.racond );
initialize_condition( &cckdblk.wrcond );
initialize_condition( &cckdblk.devcond );
initialize_condition( &cckdblk.termcond );
/* Initialize trace table */
cckdblk.itrace = calloc( CCKD_DEF_NUM_TRACE, sizeof( CCKD_ITRACE ));
cckdblk.itracep = cckdblk.itrace;
cckdblk.itracex = cckdblk.itrace + CCKD_DEF_NUM_TRACE;
cckdblk.itracen = CCKD_DEF_NUM_TRACE;
cckdblk.itracec = 0;
/* Initialize some other variables */
cckdblk.ranbr = CCKD_DEF_RA_SIZE;
cckdblk.ramax = CCKD_DEF_RA;
cckdblk.wrmax = CCKD_DEF_WRITER;
cckdblk.gcmax = CCKD_DEF_GCOL;
cckdblk.gcint = CCKD_DEF_GCINT;
cckdblk.gcparm = CCKD_DEF_GCPARM;
cckdblk.readaheads = CCKD_DEF_READAHEADS;
cckdblk.freepend = CCKD_DEF_FREEPEND;
#if defined( HAVE_ZLIB )
cckdblk.comps |= CCKD_COMPRESS_ZLIB;
#endif
#if defined( CCKD_BZIP2 )
cckdblk.comps |= CCKD_COMPRESS_BZIP2;
#endif
cckdblk.comp = 0xff;
cckdblk.compparm = -1;
/* Set the writer thread's priority just BELOW the CPU threads'
in order to minimize any potential impact from compression.
*/
cckdblk.wrprio = sysblk.cpuprio - 1;
/* Initialize the readahead queue */
cckdblk.ra1st = -1;
cckdblk.ralast = -1;
cckdblk.rafree = 0;
for (i=0; i < cckdblk.ranbr; i++)
cckdblk.ra[i].ra_idxnxt = i + 1;
cckdblk.ra[cckdblk.ranbr - 1].ra_idxnxt = -1;
/* Clear the empty L2 tables */
for (i=0; i <= CKD_NULLTRK_FMTMAX; i++)
for (j=0; j < 256; j++)
{
empty_l2 [i][j] . L2_trkoff = 0;
empty_l2 [i][j] . L2_len = i;
empty_l2 [i][j] . L2_size = i;
empty64_l2 [i][j] . L2_trkoff = 0;
empty64_l2 [i][j] . L2_len = i;
empty64_l2 [i][j] . L2_size = i;
}
return 0;
} /* end function cckd_dasd_init */
/*-------------------------------------------------------------------*/
/* Perform global CCKD dasd termination (if appropriate) */
/*-------------------------------------------------------------------*/
void cckd_dasd_term_if_appropriate()
{
/* Check if it's time to terminate yet */
obtain_lock( &cckdblk.devlock );
{
if (cckdblk.dev1st)
{
/* Not time yet; return without doing anything */
release_lock( &cckdblk.devlock );
return;
}
/* cckdblk.dev1st == NULL: time to globally terminate */
}
release_lock( &cckdblk.devlock );
/* Terminate all readahead threads... */
obtain_lock( &cckdblk.ralock );
{
cckdblk.ramax = 0; /* signal all threads to terminate */
while (cckdblk.ras) /* wait for all threads to terminate */
{
broadcast_condition( &cckdblk.racond );
wait_condition( &cckdblk.termcond, &cckdblk.ralock );
}
}
release_lock( &cckdblk.ralock );
/* Terminate all garbage collection threads... */
obtain_lock( &cckdblk.gclock );
{
cckdblk.gcmax = 0; /* signal all threads to terminate */
while (cckdblk.gcs) /* wait for all threads to terminate */
{
broadcast_condition( &cckdblk.gccond );
wait_condition( &cckdblk.termcond, &cckdblk.gclock );
}
}
release_lock( &cckdblk.gclock );
/* Terminate all writer threads... */
obtain_lock( &cckdblk.wrlock );
{
cckdblk.wrmax = 0; /* signal all threads to terminate */
while (cckdblk.wrs) /* wait for all threads to terminate */
{
broadcast_condition( &cckdblk.wrcond );
wait_condition( &cckdblk.termcond, &cckdblk.wrlock );
}
}
release_lock( &cckdblk.wrlock );
} /* end function cckd_dasd_term */
/*-------------------------------------------------------------------*/
/* Compressed CKD dasd initialization */
/*-------------------------------------------------------------------*/
int cckd_dasd_init_handler ( DEVBLK *dev, int argc, char *argv[] )
{
CCKD_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( CCKD_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 = CCKD_MAXSIZE;
/* 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_BLKGRP_SIZE;
/* open the shadow files */
if (cckd_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 cckd_dasd_init_handler */
/*-------------------------------------------------------------------*/
/* Close a Compressed CKD Device */
/*-------------------------------------------------------------------*/
int cckd_dasd_close_device (DEVBLK *dev)
{
CCKD_EXT *cckd; /* -> cckd extension */
int rc, i; /* Return code, Loop index */
if (dev->cckd64)
return cckd64_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;
cckd_flush_cache( dev );
while (cckd->wrpending || cckd->cckdioact)
{
cckd->cckdwaiters++;
rc = timed_wait_condition_relative_usecs(
&cckd->cckdiocond, &cckd->cckdiolock, 1000000, NULL );
cckd->cckdwaiters--;
cckd_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 );
cckd_purge_cache( dev );
cckd_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;
CCKD_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->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)
cckd_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 cckd_dasd_close_device */
/*-------------------------------------------------------------------*/
/* Compressed ckd start/resume channel program */
/*-------------------------------------------------------------------*/
void cckd_dasd_start (DEVBLK *dev)
{
CCKD_EXT *cckd; /* -> cckd extension */
U16 devnum = 0; /* Last active device number */
int trk = 0; /* Last active track */
if (dev->cckd64)
{
cckd64_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 cckd_dasd_start */
/*-------------------------------------------------------------------*/
/* Compressed ckd end/suspend channel program */
/*-------------------------------------------------------------------*/
void cckd_dasd_end (DEVBLK *dev)
{
CCKD_EXT *cckd; /* -> cckd extension */
if (dev->cckd64)
{
cckd64_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))
cckd_flush_cache (dev);
else if (cckd->cckdwaiters)
broadcast_condition (&cckd->cckdiocond);
release_lock (&cckd->cckdiolock);
} /* end function cckd_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 cckd_open (DEVBLK *dev, int sfx, int flags, mode_t mode)
{
CCKD_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", 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 cckd_close (DEVBLK *dev, int sfx)
{
CCKD_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 cckd_read( DEVBLK* dev, int sfx, off_t off, void* buf, unsigned int len )
{
CCKD_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 cckd_write( DEVBLK* dev, int sfx, off_t off, void* buf, unsigned int len )
{
CCKD_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 cckd_write */
/*-------------------------------------------------------------------*/
/* Truncate a cckd file */
/*-------------------------------------------------------------------*/
int cckd_ftruncate(DEVBLK *dev, int sfx, off_t off)
{
CCKD_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 */
/*-------------------------------------------------------------------*/
/* malloc */
/*-------------------------------------------------------------------*/
void* cckd_malloc( DEVBLK* dev, char* id, size_t size )
{
void* p = NULL;
if (size)
p = malloc( size );
CCKD_TRACE( "%s malloc %p len %ld", id, p, (long) size );
if (!p)
{
char buf[64];
MSGBUF( buf, "malloc( %d )", (int) size );
// "%1d:%04X CCKD file: error in function %s: %s"
WRMSG( HHC00303, "E", LCSS_DEVNUM, buf, strerror( errno ));
cckd_print_itrace();
}
return p;
}
/*-------------------------------------------------------------------*/
/* calloc */
/*-------------------------------------------------------------------*/
void* cckd_calloc( DEVBLK* dev, char* id, size_t n, size_t size )
{
void* p = NULL;
if (n && size)
p = calloc( n, size );
CCKD_TRACE( "%s calloc %p len %ld", id, p, n * (long) size );
if (!p)
{
char buf[64];
MSGBUF( buf, "calloc( %d, %d )", (int) n, (int) size );
// "%1d:%04X CCKD file: error in function %s: %s"
WRMSG( HHC00303, "E", LCSS_DEVNUM, buf, strerror( errno ));
cckd_print_itrace();
}
return p;
}
/*-------------------------------------------------------------------*/
/* realloc */
/*-------------------------------------------------------------------*/
void* cckd_realloc( DEVBLK *dev, char *id, void* p, size_t size )
{
void* p2 = NULL;
if (size)
p2 = realloc( p, size );
CCKD_TRACE( "%s realloc %p len %ld", id, p, (long) size );
if (!p2)
{
char buf[64];
MSGBUF( buf, "realloc( %p, %d )", p, (int) size );
// "%1d:%04X CCKD file: error in function %s: %s"
WRMSG( HHC00303, "E", LCSS_DEVNUM, buf, strerror( errno ));
cckd_print_itrace();
}
return p2;
}
/*-------------------------------------------------------------------*/
/* free */
/*-------------------------------------------------------------------*/
void* cckd_free( DEVBLK* dev, char* id, void* p )
{
CCKD_TRACE( "%s free %p", id, p );
if (p) free( p );
return NULL;
}
/*-------------------------------------------------------------------*/
/* Compressed ckd read track image */
/*-------------------------------------------------------------------*/
int cckd_read_track (DEVBLK *dev, int trk, BYTE *unitstat)
{
CCKD_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 = 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;
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;
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 = 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 = cckd_read_track (dev, trk, unitstat);
else
rc = 0;
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)
{
CCKD_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 %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)
{
CCKD_EXT *cckd; /* -> cckd extension */
int rc; /* Return code */
int L1idx, l2x; /* Lookup table indexes */
int sfx; /* Shadow file suffix */
CCKD_L2ENT l2; /* Copied level 2 entry */
if (dev->cckd64)
return cckd64_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] == CCKD_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 = cckd_read_l2ent (dev, &l2, L1idx * 256 + l2x);
if (rc < 0 || l2.L2_trkoff != 0) break;
}