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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
|
/* CCKDUTIL.C (c) Copyright Roger Bowler, 1999-2009 */
/* ESA/390 Compressed CKD Common routines */
// $Id: cckdutil.c 5125 2009-01-23 12:01:44Z bernard $
/*-------------------------------------------------------------------*/
/* This module contains functions for compressed CKD devices */
/* used by more than 1 main program. */
/*-------------------------------------------------------------------*/
// $Log$
// Revision 1.57 2008/09/04 22:03:15 gsmith
// Fix 64-bit length problem in cdsk_valid_trk - Tony Harminc
//
// Revision 1.56 2007/12/01 23:31:57 fish
// Fix cckdcdsk/cckdcomp/cckdutil no message o/p issue
//
// Revision 1.55 2007/08/28 20:22:41 gsmith
// cckdutil fix for 64-bit - zhackules
//
// Revision 1.54 2007/06/23 00:04:03 ivan
// Update copyright notices to include current year (2007)
//
// Revision 1.53 2006/12/08 09:43:17 jj
// Add CVS message log
//
#include "hstdinc.h"
#define _CCKDUTIL_C_
#define _HDASD_DLL_
#include "hercules.h"
#include "opcode.h"
/*-------------------------------------------------------------------*/
typedef struct _SPCTAB { /* Space table */
BYTE typ; /* Type of space */
int val; /* Value for space */
U32 pos; /* Space offset */
U32 len; /* Space length */
U32 siz; /* Space size */
} SPCTAB;
#define SPCTAB_NONE 0 /* Ignore this space entry */
#define SPCTAB_DEVHDR 1 /* Space is device header */
#define SPCTAB_CDEVHDR 2 /* Space is compressed hdr */
#define SPCTAB_L1 3 /* Space is level 1 table */
#define SPCTAB_L2 4 /* Space is level 2 table */
#define SPCTAB_TRK 5 /* Space is track image */
#define SPCTAB_BLKGRP 6 /* Space is blkgrp image */
#define SPCTAB_FREE 7 /* Space is free block */
#define SPCTAB_EOF 8 /* Space is end-of-file */
/*-------------------------------------------------------------------*/
/* Internal functions */
/*-------------------------------------------------------------------*/
static int comp_spctab_sort(const void *a, const void *b);
static int cdsk_spctab_sort(const void *a, const void *b);
static int cdsk_build_free_space(SPCTAB *spctab, int s);
static int cdsk_valid_trk (int trk, BYTE *buf, int heads, int len);
/*-------------------------------------------------------------------*/
/* Static data areas */
/*-------------------------------------------------------------------*/
static BYTE eighthexFF[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
static char *spaces[] = { "none", "devhdr", "cdevhdr", "l1", "l2",
"trk", "blkgrp", "free", "eof" };
static char *comps[] = { "none", "zlib", "bzip2" };
/*-------------------------------------------------------------------*/
/* Change the endianess of a compressed file */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_swapend (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int fd; /* File descriptor */
int rc; /* Return code */
struct stat fst; /* File status buffer */
int i; /* Index */
int swapend; /* 1=swap space */
int len; /* Length */
off_t off, lopos, hipos; /* File offsets */
CCKD_DEVHDR cdevhdr; /* Compressed ckd header */
CCKD_L1ENT *l1 = NULL; /* Level 1 table */
CCKD_L2ENT l2[256]; /* Level 2 table */
CCKD_FREEBLK freeblk; /* Free block */
/* Get fd */
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/* Get file size */
if (fstat (fd, &fst) < 0)
goto cswp_fstat_error;
hipos = fst.st_size;
/* Device header */
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto cswp_read_error;
swapend = (cdevhdr.options & CCKD_BIGENDIAN) != cckd_endian();
cckd_swapend_chdr (&cdevhdr);
cdevhdr.options |= CCKD_ORDWR;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &cdevhdr, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_chdr (&cdevhdr);
/* l1 table */
len = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
if ((l1 = malloc (len)) == NULL)
goto cswp_malloc_error;
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, l1, len)) != len)
goto cswp_read_error;
cckd_swapend_l1 (l1, (int)cdevhdr.numl1tab);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, l1, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_l1 (l1, (int)cdevhdr.numl1tab);
lopos = CCKD_L1TAB_POS + len;
/* l2 tables */
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff
|| l1[i] < lopos || l1[i] > hipos - CCKD_L2TAB_SIZE)
continue;
off = (off_t)l1[i];
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = read (fd, l2, len)) != len)
goto cswp_read_error;
cckd_swapend_l2 (l2);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, l2, len)) != len)
goto cswp_write_error;
}
free (l1);
l1 = NULL;
/* free space */
if (cdevhdr.free && cdevhdr.free >= lopos
&& cdevhdr.free <= hipos - CCKD_FREEBLK_SIZE)
{
off = (off_t)cdevhdr.free;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
len = CCKD_FREEBLK_SIZE;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
if (memcmp(&freeblk, "FREE_BLK", 8) == 0)
{
/* New format free space */
for (i = 0; i < cdevhdr.free_number; i++)
{
off += CCKD_FREEBLK_SIZE;
if (off > hipos - CCKD_FREEBLK_SIZE)
break;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
cckd_swapend_free (&freeblk);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &freeblk, len)) != len)
goto cswp_write_error;
} /* for each free space */
} /* if new format free space */
else
{
/* Old format free space */
for (i = 0; i < cdevhdr.free_number; i++)
{
if (off < lopos || off > hipos - CCKD_FREEBLK_SIZE)
break;
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = read (fd, &freeblk, len)) != len)
goto cswp_read_error;
cckd_swapend_free (&freeblk);
if (lseek (fd, off, SEEK_SET) < 0)
goto cswp_lseek_error;
if ((rc = write (fd, &freeblk, len)) != len)
goto cswp_write_error;
if (!swapend) cckd_swapend_free (&freeblk);
off = (off_t)freeblk.pos;
} /* for each free space */
} /* else old format free space */
} /* if free space */
return 0;
/* error exits */
cswp_fstat_error:
cckdumsg (dev, 701, "fstat error: %s\n", strerror(errno));
goto cswp_error;
cswp_lseek_error:
cckdumsg (dev, 702, "lseek error, offset 0x%" I64_FMT "x: %s\n",
(long long)off, strerror(errno));
goto cswp_error;
cswp_read_error:
cckdumsg (dev, 703, "read error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cswp_error;
cswp_write_error:
cckdumsg (dev, 704, "write error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cswp_error;
cswp_malloc_error:
cckdumsg (dev, 705, "malloc error, size %d: %s\n",
len, strerror(errno));
goto cswp_error;
cswp_error:
if (l1) free(l1);
return -1;
}
/*-------------------------------------------------------------------*/
/* Swap endian - compressed device header */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_chdr (CCKD_DEVHDR *cdevhdr)
{
/* fix the compressed ckd header */
cdevhdr->options ^= CCKD_BIGENDIAN;
cckd_swapend4 ((char *) &cdevhdr->numl1tab);
cckd_swapend4 ((char *) &cdevhdr->numl2tab);
cckd_swapend4 ((char *) &cdevhdr->size);
cckd_swapend4 ((char *) &cdevhdr->used);
cckd_swapend4 ((char *) &cdevhdr->free);
cckd_swapend4 ((char *) &cdevhdr->free_total);
cckd_swapend4 ((char *) &cdevhdr->free_largest);
cckd_swapend4 ((char *) &cdevhdr->free_number);
cckd_swapend4 ((char *) &cdevhdr->free_imbed);
cckd_swapend2 ((char *) &cdevhdr->compress_parm);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 1 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l1 (CCKD_L1ENT *l1, int n)
{
int i; /* Index */
for (i = 0; i < n; i++)
cckd_swapend4 ((char *) &l1[i]);
}
/*-------------------------------------------------------------------*/
/* Swap endian - level 2 table */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_l2 (CCKD_L2ENT *l2)
{
int i; /* Index */
for (i = 0; i < 256; i++)
{
cckd_swapend4 ((char *) &l2[i].pos);
cckd_swapend2 ((char *) &l2[i].len);
cckd_swapend2 ((char *) &l2[i].size);
}
}
/*-------------------------------------------------------------------*/
/* Swap endian - free space entry */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend_free (CCKD_FREEBLK *fb)
{
cckd_swapend4 ((char *) &fb->pos);
cckd_swapend4 ((char *) &fb->len);
}
/*-------------------------------------------------------------------*/
/* Swap endian - 4 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend4 (char *c)
{
char temp[4];
memcpy (&temp, c, 4);
c[0] = temp[3];
c[1] = temp[2];
c[2] = temp[1];
c[3] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Swap endian - 2 bytes */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckd_swapend2 (char *c)
{
char temp[2];
memcpy (&temp, c, 2);
c[0] = temp[1];
c[1] = temp[0];
}
/*-------------------------------------------------------------------*/
/* Are we little or big endian? From Harbison&Steele. */
/*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_endian()
{
union
{
long l;
char c[sizeof (long)];
} u;
u.l = 1;
return u.c[sizeof (long) - 1] == 1 ? CCKD_BIGENDIAN : 0;
}
/*-------------------------------------------------------------------
* Remove all free space from a compressed ckd file
*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_comp (DEVBLK *dev)
{
CCKDDASD_EXT *cckd; /* -> cckd extension */
int fd; /* File descriptor */
struct stat fst; /* File status buffer */
long long maxsize; /* Max cckd file size */
int rc; /* Return code */
off_t off; /* File offset */
off_t l2area; /* Boundary for l2 tables */
int len; /* Length */
int i, j, l, n; /* Work variables */
int relocate = 0; /* 1=spaces will be relocated*/
int l1size; /* l1 table size */
U32 next; /* offset of next space */
int s; /* space table index */
CKDDASD_DEVHDR devhdr; /* CKD device header */
CCKD_DEVHDR cdevhdr; /* CCKD device header */
CCKD_L1ENT *l1=NULL; /* -> l1 table */
CCKD_L2ENT **l2=NULL; /* -> l2 table array */
SPCTAB *spctab=NULL; /* -> space table */
BYTE *rbuf=NULL; /* Relocation buffer */
BYTE *p; /* -> relocation buffer */
int rlen=0; /* Relocation buffer length */
CCKD_L2ENT zero_l2[256]; /* Empty l2 table (zeros) */
CCKD_L2ENT ff_l2[256]; /* Empty l2 table (0xff's) */
BYTE buf[65536*4]; /* Buffer */
/*---------------------------------------------------------------
* Get fd
*---------------------------------------------------------------*/
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/*---------------------------------------------------------------
* Get file statistics
*---------------------------------------------------------------*/
if (fstat (fd, &fst) < 0)
goto comp_fstat_error;
maxsize = sizeof(off_t) == 4 ? 0x7fffffffll : 0xffffffffll;
/*---------------------------------------------------------------
* Read device header
*---------------------------------------------------------------*/
off = 0;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CKDDASD_DEVHDR_SIZE;
if ((rc = read (fd, &devhdr, len)) != len)
goto comp_read_error;
if (memcmp (devhdr.devid, "CKD_C370", 8) != 0
&& memcmp (devhdr.devid, "CKD_S370", 8) != 0
&& memcmp (devhdr.devid, "FBA_C370", 8) != 0
&& memcmp (devhdr.devid, "FBA_S370", 8) != 0)
{
cckdumsg (dev, 999, "not a compressed dasd file\n");
goto comp_error;
}
comp_restart:
/*---------------------------------------------------------------
* Read compressed device header
*---------------------------------------------------------------*/
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto comp_read_error;
/*---------------------------------------------------------------
* Check the endianess of the file
*---------------------------------------------------------------*/
if ((cdevhdr.options & CCKD_BIGENDIAN) != cckd_endian())
{
cckdumsg (dev, 101, "converting to %s\n",
cckd_endian() ? "big-endian" : "little-endian");
if (cckd_swapend (dev) < 0)
goto comp_error;
else
goto comp_restart;
}
/*---------------------------------------------------------------
* Some header checks
*---------------------------------------------------------------*/
if ((off_t)cdevhdr.size != fst.st_size
|| cdevhdr.size != cdevhdr.used || cdevhdr.free != 0
|| cdevhdr.free_total != 0 || cdevhdr.free_largest != 0
|| cdevhdr.free_number != 0 || cdevhdr.free_imbed != 0)
relocate = 1;
/*---------------------------------------------------------------
* Build empty l2 tables
*---------------------------------------------------------------*/
memset (&zero_l2, 0, CCKD_L2TAB_SIZE);
if (cdevhdr.nullfmt != 0)
for (i = 0; i < 256; i++)
zero_l2[i].len = zero_l2[i].size = cdevhdr.nullfmt;
memset (&ff_l2, 0xff, CCKD_L2TAB_SIZE);
/*---------------------------------------------------------------
* Read the l1 table
*---------------------------------------------------------------*/
l1size = len = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
if ((l1 = malloc (len)) == NULL)
goto comp_malloc_error;
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, l1, len)) != len)
goto comp_read_error;
/*---------------------------------------------------------------
* Build the space table
*---------------------------------------------------------------*/
n = 1 + 1 + 1 + cdevhdr.numl1tab + 1;
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
n += 256;
len = sizeof(SPCTAB);
if ((spctab = calloc (n, len)) == NULL)
goto comp_calloc_error;
s = 0;
spctab[s].typ = SPCTAB_DEVHDR;
spctab[s].val = -1;
spctab[s].pos = 0;
spctab[s].len =
spctab[s].siz = CKDDASD_DEVHDR_SIZE;
s++;
spctab[s].typ = SPCTAB_CDEVHDR;
spctab[s].val = -1;
spctab[s].pos = CCKD_DEVHDR_POS;
spctab[s].len =
spctab[s].siz = CCKD_DEVHDR_SIZE;
s++;
spctab[s].typ = SPCTAB_L1;
spctab[s].val = -1;
spctab[s].pos = CCKD_L1TAB_POS;
spctab[s].len =
spctab[s].siz = l1size;
s++;
spctab[s].typ = SPCTAB_EOF;
spctab[s].val = -1;
spctab[s].pos = fst.st_size;
spctab[s].len =
spctab[s].siz = 0;
s++;
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
{
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = l1[i];
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
}
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
/*---------------------------------------------------------------
* Read level 2 tables
*---------------------------------------------------------------*/
n = cdevhdr.numl1tab;
len = sizeof (void *);
if ((l2 = calloc (n, len)) == NULL)
goto comp_calloc_error;
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
if (spctab[i].typ != SPCTAB_L2) continue;
l = spctab[i].val;
len = CCKD_L2TAB_SIZE;
if ((l2[l] = malloc (len)) == NULL)
goto comp_malloc_error;
off = (off_t)spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, l2[l], len)) != len)
goto comp_read_error;
for (j = 0; j < 256; j++)
{
if (l2[l][j].pos == 0 || l2[l][j].pos == 0xffffffff)
continue;
spctab[s].typ = SPCTAB_TRK;
spctab[s].val = spctab[i].val*256 + j;
spctab[s].pos = l2[l][j].pos;
spctab[s].len = l2[l][j].len;
spctab[s].siz = l2[l][j].size;
s++;
} /* for each l2 entry */
/* check if empty l2 table */
if (memcmp (l2[l], &zero_l2, CCKD_L2TAB_SIZE) == 0
|| memcmp (l2[l], &ff_l2, CCKD_L2TAB_SIZE) == 0)
{
l1[l] = l2[l][0].pos; /* 0x00000000 or 0xffffffff */
spctab[i].typ = SPCTAB_NONE;
free (l2[l]);
l2[l] = NULL;
relocate = 1;
}
} /* for each space */
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
/* set relocate flag if last space is free space */
if (spctab[s-2].pos + spctab[s-2].len != spctab[s-1].pos)
relocate = 1;
/*---------------------------------------------------------------
* relocate l2 tables in order
*---------------------------------------------------------------*/
/* determine l2 area */
l2area = CCKD_L1TAB_POS + l1size;
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff) continue;
if (l1[i] != l2area)
relocate = 1;
l2area += CCKD_L2TAB_SIZE;
}
/* quick return if all l2 tables are orderered and no free space */
if (!relocate)
{
for (i = 1; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i-1].pos + spctab[i-1].len != spctab[i].pos)
break;
if (spctab[i].typ == SPCTAB_EOF)
{
cckdumsg (dev, 103, "file already compressed\n");
goto comp_return_ok;
}
}
/* file will be updated */
cdevhdr.options |= CCKD_ORDWR;
/* calculate track size within the l2 area */
for (i = rlen = 0; spctab[i].pos < l2area; i++)
if (spctab[i].typ == SPCTAB_TRK)
rlen += sizeof(spctab[i].val) + sizeof(spctab[i].len)
+ spctab[i].len;
/* read any tracks in the l2area into rbuf */
if ((len = rlen) > 0)
{
if ((rbuf = malloc (len)) == NULL)
goto comp_malloc_error;
for (i = 0, p = rbuf; spctab[i].pos < l2area; i++)
{
if (spctab[i].typ != SPCTAB_TRK) continue;
memcpy (p, &spctab[i].val, sizeof(spctab[i].val));
p += sizeof(spctab[i].val);
memcpy (p, &spctab[i].len, sizeof(spctab[i].len));
p += sizeof(spctab[i].len);
off = (off_t)spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = spctab[i].len;
if ((rc = read (fd, p, len)) != len)
goto comp_read_error;
p += len;
spctab[i].typ = SPCTAB_NONE;
} /* for each space in the l2 area */
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
} /* if any tracks to relocate */
/* remove all l2 tables from the space table */
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_L2)
spctab[i].typ = SPCTAB_NONE;
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
/* add all l2 tables at their ordered offsets */
off = CCKD_L1TAB_POS + l1size;
for (i = 0; i < cdevhdr.numl1tab; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff) continue;
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = (U32)off;
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
off += CCKD_L2TAB_SIZE;
}
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
/* set end-of-file position */
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
/*---------------------------------------------------------------
* Perform compression
*---------------------------------------------------------------*/
/* move spaces left */
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
/* ignore contiguous spaces */
if (spctab[i].pos + spctab[i].len == spctab[i+1].pos)
continue;
/* found a gap */
off = (off_t)spctab[i+1].pos;
/* figure out how much we can read */
for (len = 0, j = i + 1; spctab[j].typ != SPCTAB_EOF; j++)
{
if (len + spctab[j].len > sizeof(buf))
break;
next = spctab[j].pos + spctab[j].len;
spctab[j].pos = spctab[i].pos + spctab[i].len + len;
spctab[j].siz = spctab[j].len;
len += spctab[j].len;
if (next != spctab[j+1].pos)
break;
} /* search for contiguous spaces */
/* this can happen if the next space is end-of-file */
if (len == 0)
continue;
/* read the image(s) to be relocated */
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = read (fd, buf, len)) != len)
goto comp_write_error;
/* write the images */
off = (off_t)spctab[i].pos + spctab[i].len;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
if ((rc = write (fd, buf, len)) != len)
goto comp_write_error;
}
/* adjust the size of the file */
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
/*---------------------------------------------------------------
* Write spaces relocated from the l2area to the end of the file
*---------------------------------------------------------------*/
off = (off_t)spctab[s-1].pos;
p = rbuf;
while (rlen)
{
spctab[s].typ = SPCTAB_TRK;
spctab[s].pos = (U32)off;
memcpy (&spctab[s].val, p, sizeof(spctab[s].val));
p += sizeof(spctab[s].val);
memcpy (&spctab[s].len, p, sizeof(spctab[s].len));
spctab[s].siz = spctab[s].len;
p += sizeof(spctab[s].len);
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = spctab[s].len;
if ((rc = write (fd, p, len)) != len)
goto comp_write_error;
p += len;
off += len;
rlen -= len + sizeof(spctab[s].val) + sizeof(spctab[s].len);
s++;
} /* for each relocated space in l2area */
/* adjust the space table */
if (rbuf)
{
free (rbuf); rbuf = NULL;
qsort (spctab, s, sizeof(SPCTAB), comp_spctab_sort);
spctab[s-1].pos = spctab[s-2].pos + spctab[s-2].len;
}
/*---------------------------------------------------------------
* Update the device header
*---------------------------------------------------------------*/
cdevhdr.size =
cdevhdr.used = spctab[s-1].pos;
cdevhdr.free =
cdevhdr.free_total =
cdevhdr.free_largest =
cdevhdr.free_number =
cdevhdr.free_imbed = 0;
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
/*---------------------------------------------------------------
* Update the lookup tables
*---------------------------------------------------------------*/
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_L2)
l1[spctab[i].val] = spctab[i].pos;
else if (spctab[i].typ == SPCTAB_TRK)
{
l = spctab[i].val / 256;
j = spctab[i].val % 256;
l2[l][j].pos = spctab[i].pos;
l2[l][j].len =
l2[l][j].size = spctab[i].len;
}
/*---------------------------------------------------------------
* Write the cdevhdr, l1 table and l2 tables
*---------------------------------------------------------------*/
/* write cdevhdr */
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = write (fd, &cdevhdr, len)) != len)
goto comp_write_error;
/* write l1 table */
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = l1size;
if ((rc = write (fd, l1, len)) != len)
goto comp_write_error;
/* write l2 tables */
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
{
off = (off_t)l1[i];
if (lseek (fd, off, SEEK_SET) < 0)
goto comp_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = write (fd, l2[i], len)) != len)
goto comp_lseek_error;
}
/* truncate the file */
off = (off_t)spctab[s-1].pos;
if (off < fst.st_size)
{
ftruncate (fd, off);
cckdumsg (dev, 102, "compress successful, %lld bytes released\n",
(long long)fst.st_size - off);
}
else
cckdumsg (dev, 102, "compress successful, L2 tables relocated\n");
/*---------------------------------------------------------------
* Return
*---------------------------------------------------------------*/
comp_return_ok:
rc = 0;
comp_return:
if (rbuf) free(rbuf);
if (l2)
{
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i])
free (l2[i]);
free (l2);
}
if (l1) free (l1);
if (spctab) free (spctab);
return rc;
/*---------------------------------------------------------------
* Error exits
*---------------------------------------------------------------*/
comp_fstat_error:
cckdumsg (dev, 701, "fstat error: %s\n",
strerror(errno));
goto comp_error;
comp_lseek_error:
cckdumsg (dev, 702, "lseek error, offset 0x%" I64_FMT "x: %s\n",
(long long)off, strerror(errno));
goto comp_error;
comp_read_error:
cckdumsg (dev, 703, "read error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto comp_error;
comp_write_error:
cckdumsg (dev, 704, "write error rc=%d, offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto comp_error;
comp_malloc_error:
cckdumsg (dev, 705, "malloc error, size %d: %s\n",
len, strerror(errno));
goto comp_error;
comp_calloc_error:
cckdumsg (dev, 706, "calloc error, size %d: %s\n",
n*len, strerror(errno));
goto comp_error;
comp_error:
rc = -1;
goto comp_return;
} /* cckd_comp() */
/*-------------------------------------------------------------------
* cckd_comp() space table sort
*-------------------------------------------------------------------*/
static int comp_spctab_sort(const void *a, const void *b)
{
const SPCTAB *x = a, *y = b;
if (x->typ == SPCTAB_NONE) return 1;
else if (y->typ == SPCTAB_NONE) return -1;
else if (x->typ == SPCTAB_EOF) return 1;
else if (y->typ == SPCTAB_EOF) return -1;
else if (x->pos < y->pos) return -1;
else return 1;
}
/*-------------------------------------------------------------------
* Perform check function on a compressed ckd file
*
* check levels
* -1 devhdr, cdevhdr, l1 table
* 0 devhdr, cdevhdr, l1 table, l2 tables
* 1 devhdr, cdevhdr, l1 table, l2 tables, free spaces
* 2 devhdr, cdevhdr, l1 table, l2 tables, free spaces, trkhdrs
* 3 devhdr, cdevhdr, l1 table, l2 tables, free spaces, trkimgs
* 4 devhdr, cdevhdr. Build everything else from recovery
*-------------------------------------------------------------------*/
DLL_EXPORT int cckd_chkdsk(DEVBLK *dev, int level)
{
CCKDDASD_EXT *cckd; /* -> ckd extension */
int fd; /* file descriptor */
struct stat fst; /* file status information */
int fdflags; /* file descriptor flags */
long long maxsize; /* max cckd file size */
int ro; /* 1=file opened read-only */
int f, i, j, l, n; /* work integers */
int l1x, l2x; /* l1, l2 table indexes */
BYTE compmask[256]; /* compression byte mask
00 - supported
0x - valid, not supported
ff - invalid */
off_t off; /* file offset */
int len; /* length to read */
int rc; /* function return code */
int comp; /* trkhdr compression byte[0]*/
int cyl; /* trkhdr cyl bytes[1-2]*/
int head; /* trkhdr head bytes[3-4]*/
int trk; /* trkhdr calculated trk */
int cyls; /* number cylinders */
int heads; /* number heads/cylinder */
int trks; /* number tracks */
unsigned int trksz; /* track size */
int blks; /* number fba blocks */
int blkgrp; /* current block group nbr */
int blkgrps; /* number fba block groups */
unsigned int blkgrpsz; /* fba block group size */
int trktyp; /* track type (TRK, BLKGRP) */
int ckddasd=0; /* 1=ckd */
int fbadasd=0; /* 1= fba */
int shadow=0; /* 0xff=shadow file */
int hdrerr=0; /* non-zero: header errors */
int fsperr=0; /* 1=rebuild free space */
int comperrs=0; /* 1=unsupported comp found */
int recovery=0; /* 1=perform track recovery */
int valid; /* 1=valid trk recovered */
int l1size; /* size of l1 table */
int swapend=0; /* 1=call cckd_swapend */
U32 lopos, hipos; /* low/high file positions */
int pass; /* recovery pass number (fba)*/
int s; /* space table index */
SPCTAB *spctab=NULL; /* -> space table */
BYTE *l2errs=NULL; /* l2 error table */
BYTE *rcvtab=NULL; /* recovered tracks */
CKDDASD_DEVHDR devhdr; /* device header */
CCKD_DEVHDR cdevhdr; /* compressed device header */
CCKD_DEVHDR cdevhdr2; /* compressed device header 2*/
CCKD_L1ENT *l1=NULL; /* -> level 1 table */
CCKD_L2ENT l2ent; /* level 2 entry */
CCKD_L2ENT l2tab[256]; /* level 2 table */
CCKD_L2ENT **l2=NULL; /* -> level 2 table array */
CCKD_L2ENT empty_l2[256]; /* Empty l2 table */
CCKD_FREEBLK freeblk; /* free block */
CCKD_FREEBLK *fsp=NULL; /* free blocks (new format) */
BYTE buf[4*65536]; /* buffer */
/* Get fd */
cckd = dev->cckd_ext;
if (cckd == NULL)
fd = dev->fd;
else
fd = cckd->fd[cckd->sfn];
/* Get some file information */
if ( fstat (fd, &fst) < 0 )
goto cdsk_fstat_error;
hipos = fst.st_size;
maxsize = sizeof(off_t) == 4 ? 0x7fffffffll : 0xffffffffll;
fdflags = get_file_accmode_flags(fd);
ro = (fdflags & O_RDWR) == 0;
/* Build table for compression byte test */
memset (compmask, 0xff, 256);
compmask[0] = 0;
#if defined(HAVE_LIBZ)
compmask[CCKD_COMPRESS_ZLIB] = 0;
#else
compmask[CCKD_COMPRESS_ZLIB] = 1;
#endif
#if defined(CCKD_BZIP2)
compmask[CCKD_COMPRESS_BZIP2] = 0;
#else
compmask[CCKD_COMPRESS_BZIP2] = 2;
#endif
/*---------------------------------------------------------------
* Header checks
*---------------------------------------------------------------*/
/* Read the device header */
off = 0;
if ( lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CKDDASD_DEVHDR_SIZE;
if ((rc = read (fd, &devhdr, len)) != len)
goto cdsk_read_error;
/* Device header checks */
if (memcmp(devhdr.devid, "CKD_C370", 8) == 0
|| memcmp(devhdr.devid, "CKD_S370", 8) == 0
)
ckddasd = 1;
else if (memcmp(devhdr.devid, "FBA_C370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0
)
fbadasd = 1;
else
{
cckdumsg (dev, 999, "not a compressed dasd file\n");
goto cdsk_error;
}
if (memcmp(devhdr.devid, "CKD_S370", 8) == 0
|| memcmp(devhdr.devid, "FBA_S370", 8) == 0
)
shadow = 0xff;
trktyp = ckddasd ? SPCTAB_TRK : SPCTAB_BLKGRP;
/* Read the cckd device header */
off = CCKD_DEVHDR_POS;
if ( lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CCKD_DEVHDR_SIZE;
if ((rc = read (fd, &cdevhdr, len)) != len)
goto cdsk_read_error;
/* Endianess check */
if ((cdevhdr.options & CCKD_BIGENDIAN) != cckd_endian())
{
if (!ro)
{
cckdumsg (dev, 101, "converting to %s\n",
cckd_endian() ? "big-endian" : "little-endian");
if (cckd_swapend (dev) < 0)
goto cdsk_error;
if (level < 0) level = 0;
swapend = 0;
}
else
swapend = 1;
cckd_swapend_chdr (&cdevhdr);
}
/* ckd checks */
if (ckddasd)
{
CKDDEV *ckd;
heads = (devhdr.heads[3] << 24)
+ (devhdr.heads[2] << 16)
+ (devhdr.heads[1] << 8)
+ (devhdr.heads[0]);
cyls = (cdevhdr.cyls[3] << 24)
+ (cdevhdr.cyls[2] << 16)
+ (cdevhdr.cyls[1] << 8)
+ (cdevhdr.cyls[0]);
trks = heads * cyls;
trksz = (devhdr.trksize[3] << 24)
+ (devhdr.trksize[2] << 16)
+ (devhdr.trksize[1] << 8)
+ (devhdr.trksize[0]);
/* ckd dasd lookup */
ckd = dasd_lookup (DASD_CKDDEV, NULL, devhdr.devtype, cyls);
if (ckd == NULL)
{
cckdumsg (dev, 900, "dasd lookup error type=%2.2X cyls=%d\n",
devhdr.devtype, cyls);
goto cdsk_error;
}
/* track size check */
n = sizeof(CKDDASD_TRKHDR)
+ sizeof(CKDDASD_RECHDR) + 8 /* r0 length */
+ sizeof(CKDDASD_RECHDR) + ckd->r1 /* max data length */
+ sizeof(eighthexFF);
n = ((n+511)/512)*512;
if ((int)trksz != n)
{
cckdumsg (dev, 901, "bad trksize: %d, expecting %d\n",
trksz, n);
goto cdsk_error;
}
/* number of heads check */
if (heads != ckd->heads)
{
cckdumsg (dev, 902, "bad number of heads: %d, expecting %d\n",
heads, ckd->heads);
goto cdsk_error;
}
} /* if (ckddasd) */
/* fba checks */
else
{
/* Note: cyls & heads are setup for ckd type hdr checks */
blks = (cdevhdr.cyls[3] << 24)
+ (cdevhdr.cyls[2] << 16)
+ (cdevhdr.cyls[1] << 8)
+ (cdevhdr.cyls[0]);
trks = blks / CFBA_BLOCK_NUM;
if (blks % CFBA_BLOCK_NUM) trks++;
trksz = CFBA_BLOCK_SIZE + CKDDASD_TRKHDR_SIZE;
heads = 65536;
cyls = trks / heads;
if (trks % heads) cyls++;
}
/* fba variables */
blkgrps = trks;
blkgrpsz = trksz;
/* `numl1tab' check */
n = trks / 256;
if (trks % 256) n++;
if (cdevhdr.numl1tab != n && cdevhdr.numl1tab != n + 1)
{
cckdumsg (dev, 903, "bad `numl1tab': %d, expecting %d\n",
cdevhdr.numl1tab, n);
goto cdsk_error;
}
l1size = cdevhdr.numl1tab * CCKD_L1ENT_SIZE;
if (CCKD_L1TAB_POS + l1size > fst.st_size)
{
cckdumsg (dev, 904, "file too small to contain L1 table: %d, need %d",
fst.st_size, CCKD_L1TAB_POS + l1size);
goto cdsk_error;
}
/* check level 2 if SPERRS bit on */
if (!ro && level < 2 && (cdevhdr.options & CCKD_SPERRS))
{
level = 2;
cckdumsg (dev, 600, "forcing check level %d; space error bit on\n", level);
}
/* cdevhdr inconsistencies check */
hdrerr = 0;
hdrerr |= fst.st_size != (off_t)cdevhdr.size && cdevhdr.size != cdevhdr.free ? 0x0001 : 0;
hdrerr |= cdevhdr.size != cdevhdr.used + cdevhdr.free_total ? 0x0002 : 0;
hdrerr |= cdevhdr.free_largest > cdevhdr.free_total - cdevhdr.free_imbed ? 0x0004 : 0;
hdrerr |= cdevhdr.free == 0 && cdevhdr.free_number != 0 ? 0x0008 : 0;
hdrerr |= cdevhdr.free == 0 && cdevhdr.free_total != cdevhdr.free_imbed ? 0x0010 : 0;
hdrerr |= cdevhdr.free != 0 && cdevhdr.free_total == 0 ? 0x0020 : 0;
hdrerr |= cdevhdr.free != 0 && cdevhdr.free_number == 0 ? 0x0040 : 0;
hdrerr |= cdevhdr.free_number == 0 && cdevhdr.free_total != cdevhdr.free_imbed ? 0x0080 : 0;
hdrerr |= cdevhdr.free_number != 0 && cdevhdr.free_total <= cdevhdr.free_imbed ? 0x0100 : 0;
hdrerr |= cdevhdr.free_imbed > cdevhdr.free_total ? 0x0200 : 0;
/* Additional checking if header errors */
if (hdrerr != 0)
{
cckdumsg (dev, 601, "cdevhdr inconsistencies found, code=%4.4x\n", hdrerr);
if (level < 1)
{
level = 1;
cckdumsg (dev, 600, "forcing check level %d\n", level);
}
}
/* Additional checking if not properly closed */
if (level < 1 && (cdevhdr.options & CCKD_OPENED))
{
level = 1;
cckdumsg (dev, 600, "forcing check level %d; file not closed\n", level);
}
/* Additional checking if last opened for read/write */
if (level < 0 && (cdevhdr.options & CCKD_ORDWR))
level = 0;
/* Set check level -1 */
if (level == 0 && !dev->batch && !hdrerr
&& (cdevhdr.options & (CCKD_OPENED|CCKD_SPERRS)) == 0
&& ((cdevhdr.options & (CCKD_ORDWR)) == 0 || ro))
level = -1;
/* Build empty l2 table */
memset (&empty_l2, shadow, CCKD_L2TAB_SIZE);
if (shadow == 0 && cdevhdr.nullfmt != 0)
for (i = 0; i < 256; i++)
empty_l2[i].len = empty_l2[i].size = cdevhdr.nullfmt;
/*---------------------------------------------------------------
* read the level 1 table
*---------------------------------------------------------------*/
len = l1size;
if ((l1 = malloc (len)) == NULL)
goto cdsk_error;
off = CCKD_L1TAB_POS;
if ( lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = read (fd, l1, len)) != len)
goto cdsk_read_error;
if (swapend) cckd_swapend_l1 (l1, (int)cdevhdr.numl1tab);
lopos = CCKD_L1TAB_POS + l1size;
/*---------------------------------------------------------------
* initialize the space table
*---------------------------------------------------------------*/
/* find number of non-null l1 entries */
for (i = n = 0; i < cdevhdr.numl1tab; i++)
if (l1[i] != 0 && l1[i] != 0xffffffff)
n++;
if (level >= 4) n = cdevhdr.numl1tab;
/* calculate max possible space table entries */
n = 1 + 1 + 1 // devhdr, cdevhdr, l1tab
+ n // l2tabs
+ (n * 256) // trk/blk images
+ (1 + n + (n * 256) + 1) // max possible free spaces
+ 1; // end-of-file
/* obtain the space table */
len = sizeof(SPCTAB);
if ((spctab = calloc (n, len)) == NULL)
goto cdsk_calloc_error;
/* populate the table with what we have */
s = 0;
/* devhdr */
spctab[s].typ = SPCTAB_DEVHDR;
spctab[s].val = -1;
spctab[s].pos = 0;
spctab[s].len =
spctab[s].siz = CKDDASD_DEVHDR_SIZE;
s++;
/* cdevhdr */
spctab[s].typ = SPCTAB_CDEVHDR;
spctab[s].val = -1;
spctab[s].pos = CCKD_DEVHDR_POS;
spctab[s].len =
spctab[s].siz = CCKD_DEVHDR_SIZE;
s++;
/* l1 table */
spctab[s].typ = SPCTAB_L1;
spctab[s].val = -1;
spctab[s].pos = CCKD_L1TAB_POS;
spctab[s].len =
spctab[s].siz = l1size;
s++;
/* l2 tables */
for (i = 0; i < cdevhdr.numl1tab && level < 4; i++)
{
if (l1[i] == 0 || l1[i] == 0xffffffff) continue;
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = l1[i];
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
}
/* end-of-file */
spctab[s].typ = SPCTAB_EOF;
spctab[s].val = -1;
spctab[s].pos = (U32)fst.st_size;
spctab[s].len =
spctab[s].siz = 0;
s++;
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
/*---------------------------------------------------------------
* Quick return if level -1
*---------------------------------------------------------------*/
if (level < 0)
{
int err = 0;
/* check for overlaps */
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].pos + spctab[i].siz > spctab[i+1].pos)
err = 1;
/* exit if no errors */
if (!err) goto cdsk_return_ok;
}
/*---------------------------------------------------------------
* obtain the l2errs table and recovery table
*---------------------------------------------------------------*/
len = sizeof(BYTE);
n = cdevhdr.numl1tab;
if ((l2errs = calloc (n, len)) == NULL)
goto cdsk_calloc_error;
n = trks;
if ((rcvtab = calloc (n, len)) == NULL)
goto cdsk_calloc_error;
/*---------------------------------------------------------------
* Special processing for level 4 (recover everything)
*---------------------------------------------------------------*/
if (level == 4)
{
memset (l2errs, 1, cdevhdr.numl1tab);
memset (rcvtab, 1, trks);
goto cdsk_recovery;
}
/*---------------------------------------------------------------
* Read the level 2 tables
*---------------------------------------------------------------*/
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
if (spctab[i].typ != SPCTAB_L2
|| spctab[i].pos < lopos || spctab[i].pos > hipos)
continue;
off = spctab[i].pos;
if ( lseek (fd, off, SEEK_SET) < 0 )
goto cdsk_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = read (fd, l2tab, len)) != len)
goto cdsk_read_error;
if (swapend) cckd_swapend_l2 (l2tab);
/* add trks/blkgrps to the space table */
for (j = 0; j < 256; j++)
{
if (l2tab[j].pos != 0 && l2tab[j].pos != 0xffffffff)
{
spctab[s].typ = trktyp;
spctab[s].val = spctab[i].val * 256 + j;
spctab[s].pos = l2tab[j].pos;
spctab[s].len = l2tab[j].len;
spctab[s].siz = l2tab[j].size;
s++;
}
}
}
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
/*---------------------------------------------------------------
* Consistency checks.
*
* The space table is now populated with everything but free
* space. Therefore we can infer what the free space should
* be (ie gaps between allocated spaces).
*---------------------------------------------------------------*/
lopos = CCKD_L1TAB_POS + l1size;
hipos = fst.st_size;
/* Make adjustment if new format free space is at the end */
len = spctab[s-1].pos - (spctab[s-2].pos + spctab[s-2].siz);
if (len > 0
&& cdevhdr.size == cdevhdr.free
&& cdevhdr.size + len == spctab[s-1].pos)
{
spctab[s-1].pos -= len;
hipos -= len;
}
memset (&cdevhdr2, 0, CCKD_DEVHDR_SIZE);
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
/* Calculate gap size */
len = spctab[i+1].pos - (spctab[i].pos + spctab[i].siz);
/* Update space statistics */
cdevhdr2.size += spctab[i].siz + len;
cdevhdr2.used += spctab[i].len;
if (len > 0)
{
cdevhdr2.free_number++;
cdevhdr2.free_total += len;
if (cdevhdr2.free_largest < (U32)len)
cdevhdr2.free_largest = (U32)len;
}
if (spctab[i].typ == trktyp)
{
cdevhdr2.free_total += spctab[i].siz - spctab[i].len;
cdevhdr2.free_imbed += spctab[i].siz - spctab[i].len;
}
/* ignore devhdr, cdevhdr and l1 (these are `out of bounds') */
if (spctab[i].typ == SPCTAB_DEVHDR
|| spctab[i].typ == SPCTAB_CDEVHDR
|| spctab[i].typ == SPCTAB_L1
)
continue;
/* check if the space is out of bounds */
valid = (off_t)spctab[i].pos >= lopos
&& (off_t)spctab[i].pos + spctab[i].siz <= hipos;
/* Overlap check */
if (len < 0 || !valid)
{
char space1[32], space2[32];
recovery = 1;
/* issue error message */
j = sprintf(space1, "%s", spaces[spctab[i].typ]);
if (spctab[i].val >= 0)
sprintf(space1+j, "[%d]", spctab[i].val);
j = sprintf(space2, "%s", spaces[spctab[i+1].typ]);
if (spctab[i+1].val >= 0)
sprintf(space2+j, "[%d]", spctab[i+1].val);
if (!valid)
cckdumsg(dev, 602, "%s offset 0x%" I32_FMT "x len %d is out of bounds\n",
space1, spctab[i].pos, spctab[i].siz);
else
cckdumsg(dev, 603, "%s offset 0x%" I32_FMT "x len %d overlaps %s offset 0x%" I32_FMT "x\n",
space1, spctab[i].pos, spctab[i].siz, space2, spctab[i+1].pos);
/* setup recovery */
if (spctab[i].typ == SPCTAB_L2)
{
l2errs[spctab[i].val] = 1;
/* Mark all tracks for the l2 for recovery */
memset (rcvtab + (spctab[i].val*256), 1, 256);
}
else if (spctab[i].typ == trktyp)
rcvtab[spctab[i].val] = 1;
if (spctab[i+1].typ == SPCTAB_L2 && valid)
{
l2errs[spctab[i+1].val] = 1;
memset (rcvtab + (spctab[i+1].val*256), 1, 256);
}
else if (spctab[i+1].typ == trktyp && valid)
rcvtab[spctab[i+1].val] = 1;
} /* if overlap or out of bounds */
/* Check image l2 entry consistency */
else if (spctab[i].typ == trktyp
&& (spctab[i].len < CKDDASD_TRKHDR_SIZE
|| spctab[i].len > spctab[i].siz
|| spctab[i].len > trksz))
{
recovery = 1;
/* issue error message */
cckdumsg(dev, 604, "%s[%d] l2 inconsistency: len %d, size %d\n",
spaces[trktyp], spctab[i].val,
spctab[i].len, spctab[i].siz);
/* setup recovery */
rcvtab[spctab[i].val] = 1;
} /* if inconsistent l2 */
} /* for each space */
/* remove any l2 tables or tracks in error from the space table */
for (i = 0; recovery && spctab[i].typ != SPCTAB_EOF; i++)
if ((spctab[i].typ == SPCTAB_L2 && l2errs[spctab[i].val])
|| (spctab[i].typ == trktyp && rcvtab[spctab[i].val]))
spctab[i].typ = SPCTAB_NONE;
/* overlaps are serious */
if (recovery && level < 3)
{
level = 3;
cckdumsg (dev, 600, "forcing check level %d\n", level);
}
/* Rebuild free space if any errors */
if (recovery || hdrerr
|| cdevhdr.size != cdevhdr2.size
|| cdevhdr.used != cdevhdr2.used
|| cdevhdr.free_number != cdevhdr2.free_number
|| cdevhdr.free_largest != cdevhdr2.free_largest
|| cdevhdr.free_total != cdevhdr2.free_total
|| cdevhdr.free_imbed != cdevhdr2.free_imbed
)
fsperr = 1;
/*---------------------------------------------------------------
* read the free space
*---------------------------------------------------------------*/
lopos = CCKD_L1TAB_POS + l1size;
hipos = fst.st_size;
if (level >= 1 && !fsperr)
{
while (cdevhdr.free) // `while' so code can break
{
fsperr = 1; // be pessimistic
fsp = NULL;
/* Read the free space */
off = (off_t)cdevhdr.free;
len = CCKD_FREEBLK_SIZE;
if (off < lopos || off + CCKD_FREEBLK_SIZE > hipos
|| lseek (fd, off, SEEK_SET) < 0
|| (rc = read (fd, &freeblk, len)) != len)
break;
if (memcmp (&freeblk, "FREE_BLK", 8) == 0)
{
/* new format free space */
len = cdevhdr.free_number * CCKD_FREEBLK_SIZE;
if ((fsp = malloc(len)) == NULL
|| (rc = read (fd, fsp, len)) != len)
break;
for (i = 0; i < cdevhdr.free_number; i++)
{
if (swapend) cckd_swapend_free (&fsp[i]);
spctab[s].typ = SPCTAB_FREE;
spctab[s].val = -1;
spctab[s].pos = fsp[i].pos;
spctab[s].len =
spctab[s].siz = fsp[i].len;
/* Free space should be ascending */
if (spctab[s].pos < lopos
|| spctab[s].pos + spctab[s].siz > hipos)
break;
lopos = spctab[s].pos + spctab[s].siz;
s++;
} /* for each free space */
if (i >= cdevhdr.free_number)
fsperr = 0;
} /* new format free space */
else
{
/* old format free space */
off = (off_t)cdevhdr.free;
len = CCKD_FREEBLK_SIZE;
for (i = 0; i < cdevhdr.free_number; i++)
{
if (off < lopos || off > hipos) break;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = read (fd, &freeblk, len)) != len)
goto cdsk_read_error;
if (swapend) cckd_swapend_free (&freeblk);
spctab[s].typ = SPCTAB_FREE;
spctab[s].val = -1;
spctab[s].pos = (U32)off;
spctab[s].len =
spctab[s].siz = freeblk.len;
s++;
lopos = off + freeblk.len;
off = (off_t)freeblk.pos;
}
if (i >= cdevhdr.free_number && freeblk.pos == 0)
fsperr = 0;
} /* if old format free space */
if (fsp) free(fsp);
fsp = NULL;
/* Check for gaps/overlaps */
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
for (i = 0; !fsperr && spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].pos + spctab[i].siz != spctab[i+1].pos)
fsperr = 1;
break;
} /* while (cdevhdr.free) */
} /* if (level >= 1 && !fsperr) */
if (fsperr)
cckdumsg (dev, 610, "free space errors detected\n");
/*---------------------------------------------------------------
* Read track headers/images
*---------------------------------------------------------------*/
cdsk_space_check:
if (level >= 2)
{
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
if (spctab[i].typ != trktyp) continue;
/* read the header or image depending on the check level */
off = spctab[i].pos;
if ( lseek (fd, off, SEEK_SET) < 0 )
goto cdsk_lseek_error;
len = level < 3 ? CKDDASD_TRKHDR_SIZE : spctab[i].len;
if ((rc = read (fd, buf, len)) != len)
goto cdsk_read_error;
/* Extract header info */
comp = buf[0];
cyl = fetch_hw (buf + 1);
head = fetch_hw (buf + 3);
trk = cyl * heads + head;
/* Validate header info */
if (compmask[comp] == 0xff
|| cyl >= cyls || head >= heads
|| trk != spctab[i].val)
{
cckdumsg (dev, 620, "%s[%d] hdr error offset 0x%" I64_FMT
"x: %2.2x%2.2x%2.2x%2.2x%2.2x\n",
spaces[trktyp], spctab[i].val, (long long)off,
buf[0],buf[1],buf[2],buf[3],buf[4]);
/* recover this track */
rcvtab[spctab[i].val] = recovery = 1;
spctab[i].typ = SPCTAB_NONE;
/* Force level 3 checking */
if (level < 3)
{
level = 3;
cckdumsg (dev, 600, "forcing check level %d\n", level);
goto cdsk_space_check;
}
continue;
} /* if invalid header info */
/* Check if compression supported */
if (compmask[comp])
{
comperrs = 1;
cckdumsg ( dev, 621, "%s[%d] compressed using %s, not supported\n",
spaces[trktyp], trk, comps[compmask[comp]]);
continue;
}
/* Validate the space if check level 3 */
if (level > 2)
{
if (!cdsk_valid_trk (trk, buf, heads, len))
{
cckdumsg (dev, 622, "%s[%d] offset 0x%" I64_FMT
"x len %d validation error\n",
spaces[trktyp], trk, (long long)off, len);
/* recover this track */
rcvtab[trk] = recovery = 1;
spctab[i].typ = SPCTAB_NONE;
} /* if invalid space */
else
rcvtab[trk] = 0;
} /* if level > 2 */
} /* for each space */
} /* if (level >= 2) */
/*---------------------------------------------------------------
* Recovery
*---------------------------------------------------------------*/
cdsk_recovery:
if (recovery || level == 4)
{
U32 flen, fpos;
/*-----------------------------------------------------------
* Phase 1 -- recover trk/blkgrp images
*-----------------------------------------------------------*/
/*
* Reset the end-of-file pos to the file size
* It might have been changed if new format free space
* occurred at the end of the file.
*/
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
spctab[s-1].pos = fst.st_size;
/* count number tracks to be recovered */
for (i = n = 0; i < trks; i++)
if (rcvtab[i] == 1)
n++;
/*-----------------------------------------------------------
* ckd recovery
*-----------------------------------------------------------*/
if (ckddasd)
{
/* recovery loop */
s = cdsk_build_free_space (spctab, s);
for (f = 0; spctab[f].typ != SPCTAB_EOF && n; )
{
/* next free space if too small */
if (spctab[f].typ != SPCTAB_FREE
|| spctab[f].siz <= CKDDASD_TRKHDR_SIZE+8)
{
for (f = f + 1; spctab[f].typ != SPCTAB_EOF; f++)
if (spctab[f].typ == SPCTAB_FREE)
break;
continue;
}
fpos = spctab[f].pos;
flen = spctab[f].siz;
/* length to read */
len = flen < sizeof(buf) ? flen : sizeof(buf);
/* read the free space */
off = (off_t)fpos;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = read (fd, buf, len)) != len)
goto cdsk_read_error;
/* Scan the space for a trkhdr */
for (i = 0; i < len - (CKDDASD_TRKHDR_SIZE+8); i++)
{
/* Check compression byte */
if (compmask[buf[i]])
continue;
/* Fetch possible trkhdr */
comp = buf[i];
cyl = fetch_hw (buf + i + 1);
head = fetch_hw (buf + i + 3);
trk = cyl * heads + head;
/* Validate possible trkhdr */
if (cyl >= cyls || head >= heads || rcvtab[trk] != 1)
continue;
/* Quick validation for compress none */
if (comp == CCKD_COMPRESS_NONE
&& (fetch_hw (buf + i + 5) != cyl // r0 cyl
|| fetch_hw (buf + i + 7) != head // r0 head
|| buf[i + 9] != 0 // r0 record
|| buf[i + 10] != 0 // r0 key length
|| fetch_hw (buf + i + 11) != 8 // r0 data length
)
)
continue;
/* Quick validation for zlib */
else if (comp == CCKD_COMPRESS_ZLIB
&& fetch_hw(buf + i + 5) % 31 != 0)
continue;
/* Quick validation for bzip2 */
else if (comp == CCKD_COMPRESS_BZIP2
&& (buf[i+5] != 'B' || buf[i+6] != 'Z'))
continue;
/*
* If we are in `borrowed space' then start over
* with the current position at the beginning
*/
if (flen > (U32)len && i > len - (int)trksz)
break;
/* Checks for comp none */
if (comp == CCKD_COMPRESS_NONE)
{
l = len - i;
if ((l = cdsk_valid_trk (trk, buf+i, heads, -l)))
goto cdsk_ckd_recover;
else
continue;
}
/* Check short `length' */
if (flen == (U32)len && (l = len - i) <= 1024)
{
if (cdsk_valid_trk (trk, buf+i, heads, l))
{
while (cdsk_valid_trk (trk, buf+i, heads, --l));
l++;
goto cdsk_ckd_recover;
}
}
/* Scan for next trkhdr */
for (j = i + CKDDASD_TRKHDR_SIZE+8;
j <= len - (CKDDASD_TRKHDR_SIZE+8);
j++)
{
if (j - i > (int)trksz) break;
if (compmask[buf[j]] != 0
|| fetch_hw(buf+j+1) >= cyls
|| fetch_hw(buf+j+3) >= heads)
continue;
/* check uncompressed hdr */
if (buf[j] == CCKD_COMPRESS_NONE
&& (fetch_hw (buf+j+5) != fetch_hw(buf+j+1)
|| fetch_hw (buf+j+7) != fetch_hw(buf+j+3)
|| buf[j+9] != 0 || buf[j+10] != 0
|| fetch_hw(buf+j+11) != 8))
continue;
/* check zlib compressed header */
else if (buf[j] == CCKD_COMPRESS_ZLIB
&& fetch_hw(buf + j + 5) % 31 != 0)
continue;
/* check bzip2 compressed header */
else if (buf[j] == CCKD_COMPRESS_BZIP2
&& (buf[j+5] != 'B' || buf[j+6] != 'Z'))
continue;
/* check to possible trkhdr */
l = j - i;
if (cdsk_valid_trk (trk, buf+i, heads, l))
{
#if 0
while (cdsk_valid_trk (trk, buf+i, heads, --l));
l++;
#endif
goto cdsk_ckd_recover;
}
} /* scan for next trkhdr */
/* Check `length' */
if (flen == (U32)len && (l = len - i) <= (int)trksz)
{
if (cdsk_valid_trk (trk, buf+i, heads, l))
{
while (cdsk_valid_trk (trk, buf+i, heads, --l));
l++;
goto cdsk_ckd_recover;
}
}
/* Scan all lengths */
for (l = CKDDASD_TRKHDR_SIZE+8; i + l <= len; l++)
{
if (l > (int)trksz)
break;
if (cdsk_valid_trk (trk, buf+i, heads, l))
goto cdsk_ckd_recover;
} /* for all lengths */
continue;
cdsk_ckd_recover:
cckdumsg (dev, 301, "%s[%d] recovered offset 0x%" I64_FMT "x len %d\n",
spaces[trktyp], trk, (long long)off + i, l);
n--;
rcvtab[trk] = 2;
/* add recovered track to the space table */
spctab[s].typ = trktyp;
spctab[s].val = trk;
spctab[s].pos = fpos + i;
spctab[s].len =
spctab[s].siz = l;
s++;
/*
* adjust `i' knowing it will be incremented
* in the `for' loop above.
*/
i += l - 1;
} /* for each byte in the free space */
/* Adjust the free space for what we processed */
spctab[f].pos += i;
spctab[f].len -= i;
spctab[f].siz -= i;
} /* for each free space */
} /* if ckddasd */
/*-----------------------------------------------------------
* fba recovery
*-----------------------------------------------------------*/
/*
* FBA blkgrps are harder to recover than CKD tracks because
* there is not any information within the blkgrp itself to
* validate (unlike a track, which has count fields that
* terminate in an end-of-track marker).
*
* On the first pass we recover all compressed blkgrps since
* these are readily validated (they must uncompress to a
* certain size, CFBA_BLOCK_SIZE+CKDDASD_TRKHDR_SIZE). We
* also recover uncompressed blkgrps if they are followed by
* a valid trkhdr (and don't occur to close to the beginning
* of the file).
*
* On the second pass we recover all uncompressed blkgrps
* that weren't recovered in the first pass. The only
* criteria is that the compression byte is zero and the
* 4 byte blkgrp number is in range and there are at least
* CFBA_BLOCK_SIZE bytes following.
*/
for (pass = 0; fbadasd && pass < 2; pass++)
{
lopos = CCKD_L1TAB_POS + (cdevhdr.numl1tab * 4);
if (pass == 0)
lopos += (cdevhdr.numl1tab * CCKD_L2TAB_SIZE);
/* recovery loop */
s = cdsk_build_free_space (spctab, s);
for (f = 0; spctab[f].typ != SPCTAB_EOF && n > 0; )
{
U32 flen, fpos;
/* next free space if too small */
if (spctab[f].typ != SPCTAB_FREE
|| spctab[f].siz <= CKDDASD_TRKHDR_SIZE+8
|| (pass == 1 && spctab[f].siz < blkgrpsz))
{
for (f = f + 1; spctab[f].typ != SPCTAB_EOF; f++)
if (spctab[f].typ == SPCTAB_FREE)
break;
continue;
}
fpos = spctab[f].pos;
flen = spctab[f].siz;
/*
* calculate length to read
* if flen > len then we only read part of the space
*/
len = flen < sizeof(buf) ? flen : sizeof(buf);
/* read the free space */
off = (off_t)fpos;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = read (fd, buf, len)) != len)
goto cdsk_read_error;
/* Scan the space */
for (i = 0; i < len - (CKDDASD_TRKHDR_SIZE+8); i++)
{
/* For pass 1 the size left must be at least blkgrpsz */
if (pass == 1 && len - i < (int)blkgrpsz)
break;
/* Check compression byte */
if ((pass == 0 && compmask[buf[i]])
|| (pass == 1 && buf[i] != CCKD_COMPRESS_NONE))
continue;
/* Fetch possible trkhdr */
comp = buf[i];
blkgrp = fetch_fw (buf + i + 1);
/* Validate possible trkhdr */
if (blkgrp < 0 || blkgrp >= blkgrps
|| rcvtab[blkgrp] != 1)
continue;
/* Validation for compress none */
if (comp == CCKD_COMPRESS_NONE
&& flen == (U32)len && len - i < (int)blkgrpsz)
continue;
/* Quick validation for zlib */
else if (comp == CCKD_COMPRESS_ZLIB
&& fetch_hw(buf + i + 5) % 31 != 0)
continue;
/* Quick validation for bzip2 */
else if (comp == CCKD_COMPRESS_BZIP2
&& (buf[i+5] != 'B' || buf[i+6] != 'Z'))
continue;
/*
* If we are in `borrowed space' then start over
* with the current position at the beginning
*/
if (flen > (U32)len && i > len - (int)blkgrpsz)
break;
/* Checks for comp none */
if (comp == CCKD_COMPRESS_NONE)
{
l = blkgrpsz;
if (len - i < (int)blkgrpsz || fpos + i < lopos)
continue;
if (len - i == (int)blkgrpsz && flen == (U32)len)
goto cdsk_fba_recover;
/* Pass 0 checks */
if (pass == 0
&& (len - i - l < CKDDASD_TRKHDR_SIZE+8
|| compmask[buf[i+l]]
|| fetch_fw (buf+i+l+1) >= (unsigned int)blkgrps)
)
continue;
goto cdsk_fba_recover;
}
/* The tests below are for pass 0 only */
if (pass == 1)
continue;
/* Check short `length' */
if (flen == (U32)len && (l = len - i) <= 1024)
{
if (cdsk_valid_trk (blkgrp, buf+i, heads, l))
{
while (cdsk_valid_trk (blkgrp, buf+i, heads, --l));
l++;
goto cdsk_fba_recover;
}
}
/* Scan for next trkhdr */
for (j = i + CKDDASD_TRKHDR_SIZE+8;
j <= len - (CKDDASD_TRKHDR_SIZE+8);
j++)
{
if (j - i > (int)blkgrpsz) break;
if (compmask[buf[j]] != 0
|| fetch_fw(buf+j+1) >= (unsigned int)blkgrps)
continue;
/* check zlib compressed header */
if (buf[j] == CCKD_COMPRESS_ZLIB
&& fetch_hw(buf + j + 5) % 31 != 0)
continue;
/* check bzip2 compressed header */
else if (buf[j] == CCKD_COMPRESS_BZIP2
&& (buf[j+5] != 'B' || buf[j+6] != 'Z'))
continue;
/* check to possible trkhdr */
l = j - i;
if (cdsk_valid_trk (blkgrp, buf+i, heads, l))
{
#if 0
while (cdsk_valid_trk (blkgrp, buf+i, heads, --l));
l++;
#endif
goto cdsk_fba_recover;
}
} /* scan for next trkhdr */
/* Check `length' */
l = len - i;
if (flen == (U32)len && l <= (int)blkgrpsz)
{
if (cdsk_valid_trk (blkgrp, buf+i, heads, l))
{
while (cdsk_valid_trk (blkgrp, buf+i, heads, --l));
l++;
goto cdsk_fba_recover;
}
}
/* Scan all lengths */
for (l = CKDDASD_TRKHDR_SIZE+8; i + l <= len; l++)
{
if (l > (int)blkgrpsz)
break;
if (cdsk_valid_trk (blkgrp, buf+i, heads, l))
goto cdsk_fba_recover;
} /* for all lengths */
continue;
cdsk_fba_recover:
cckdumsg (dev, 301, "%s[%d] recovered offset 0x%" I64_FMT "x len %d\n",
spaces[trktyp], blkgrp, (long long)off + i, l);
n--;
rcvtab[blkgrp] = 2;
/* Enable recovery of comp 0 blkgrps for pass 0 */
if (fpos + i < lopos)
lopos = fpos + i;
/* add recovered block group to the space table */
spctab[s].typ = trktyp;
spctab[s].val = blkgrp;
spctab[s].pos = fpos + i;
spctab[s].len =
spctab[s].siz = l;
s++;
/*
* adjust `i' knowing it will be incremented
* in the `for' loop above.
*/
i += l - 1;
} /* for each byte in the free space */
/* Adjust the free space for what we processed */
spctab[f].pos += i;
spctab[f].len -= i;
spctab[f].siz -= i;
} /* for each free space */
} /* if fbadasd */
for (i = n = 0; i < trks; i++)
if (rcvtab[i] == 2)
n++;
cckdumsg (dev, 300, "%d %s images recovered\n",
n, spaces[trktyp]);
/*-----------------------------------------------------------
* Phase 2 -- rebuild affected l2 tables
*-----------------------------------------------------------*/
/*
* Make sure there's at least one non-zero `rcvtab' entry
* for l2 tables in `l2errs'. Space validation may have
* turned off all `rcvtab' entries for an l2.
*/
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2errs[i])
rcvtab[i*256] = 1;
/* Get storage for the l2 table array */
n = cdevhdr.numl1tab;
len = sizeof(void *);
if ((l2 = calloc (n, len)) == NULL)
goto cdsk_calloc_error;
/* Get storage for the rebuilt l2 tables */
len = CCKD_L2TAB_SIZE;
for (i = 0; i < trks; i++)
{
l1x = i / 256;
if (rcvtab[i] != 0 && l2[l1x] == NULL)
{
if ((l2[l1x] = malloc (len)) == NULL)
goto cdsk_malloc_error;
l1[l1x] = shadow ? 0xffffffff : 0;
memcpy (l2[l1x], &empty_l2, len);
}
}
/* Rebuild the l2 tables */
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
if (spctab[i].typ == SPCTAB_L2 && l2[spctab[i].val])
spctab[i].typ = SPCTAB_NONE;
else if (spctab[i].typ == trktyp && l2[spctab[i].val/256])
{
l1x = spctab[i].val / 256;
l2x = spctab[i].val % 256;
l2[l1x][l2x].pos = spctab[i].pos;
l2[l1x][l2x].len = spctab[i].len;
l2[l1x][l2x].size = spctab[i].siz;
}
} /* for each space */
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
/* Look for empty l2 tables */
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i] != NULL
&& memcmp (l2[i], &empty_l2, CCKD_L2TAB_SIZE) == 0)
{
free (l2[i]);
l2[i] = NULL;
}
/*
* `s-1' indexes the SPCTAB_EOF space table entry.
* Set its `pos' to the maximum allowed value to ensure
* there will be free space for the rebuilt l2 tables.
*/
spctab[s-1].pos = (U32)maxsize;
/* Build the free space */
s = cdsk_build_free_space (spctab, s);
/* Find space for the rebuilt l2 tables */
for (i = j = 0; i < cdevhdr.numl1tab; i++)
{
if (l2[i] == NULL) continue;
/* find a free space */
for ( ; spctab[j].typ != SPCTAB_EOF; j++)
if (spctab[j].typ == SPCTAB_FREE
&& spctab[j].siz >= CCKD_L2TAB_SIZE)
break;
/* weird error if no space */
if (spctab[j].typ == SPCTAB_EOF)
{
cckdumsg (dev, 905, "not enough file space for recovery\n");
goto cdsk_error;
}
/* add l2 space */
l1[i] = spctab[j].pos;
spctab[s].typ = SPCTAB_L2;
spctab[s].val = i;
spctab[s].pos = spctab[j].pos;
spctab[s].len =
spctab[s].siz = CCKD_L2TAB_SIZE;
s++;
/* adjust the free space */
spctab[j].pos += CCKD_L2TAB_SIZE;
spctab[j].len -= CCKD_L2TAB_SIZE;
spctab[j].siz -= CCKD_L2TAB_SIZE;
} /* for each l2 table */
/*-----------------------------------------------------------
* Phase 3 -- write l1 and l2 tables
*-----------------------------------------------------------*/
if (ro)
{
cckdumsg (dev, 500, "recovery not completed, file opened read-only\n");
goto cdsk_error;
}
if (comperrs)
{
cckdumsg (dev, 501, "recovery not completed, missing compression\n");
goto cdsk_error;
}
/* Write the l1 table */
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = l1size;
if ((rc = write (fd, l1, len)) != len)
goto cdsk_write_error;
/* Write l2 tables */
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
{
l1x = spctab[i].val;
if (spctab[i].typ != SPCTAB_L2 || l2[l1x] == NULL)
continue;
off = (off_t)l1[l1x];
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CCKD_L2TAB_SIZE;
if ((rc = write (fd, l2[l1x], len)) != len)
goto cdsk_write_error;
free (l2[l1x]);
l2[l1x] = NULL;
} /* for each space */
/* Free recovery related storage */
if (l2)
{
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i])
free (l2[i]);
free (l2); l2 = NULL;
}
free (l2errs); l2errs = NULL;
free (rcvtab); rcvtab = NULL;
/* Ensure we do free space recovery */
fsperr = 1;
} /* if (recovery || level >= 4) */
/*---------------------------------------------------------------
* Rebuild free space
*---------------------------------------------------------------*/
if (fsperr && ro)
cckdumsg (dev, 502, "free space not rebuilt, file opened read-only\n");
else if (fsperr)
{
/*-----------------------------------------------------------
* Phase 1 -- build the free space
* make sure the last space isn't free space and
* that each free space is long enough (8 bytes).
*-----------------------------------------------------------*/
cdsk_fsperr_retry:
s = cdsk_build_free_space (spctab, s);
/*
* spctab[s-1] is the SPCTAB_EOF entry.
* if spctab[s-2] is SPCTAB_FREE then discard it
*/
if (spctab[s-2].typ == SPCTAB_FREE)
{
spctab[s-1].typ = SPCTAB_NONE;
spctab[s-2].typ = SPCTAB_EOF;
spctab[s-2].val = -1;
spctab[s-2].len =
spctab[s-2].siz = 0;
s--;
}
/*
* Check for short free spaces.
* If found, shift left until the next free space or eof.
*/
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_FREE
&& spctab[i].siz < CCKD_FREEBLK_SIZE)
break;
if (spctab[i].typ != SPCTAB_EOF)
{
/* Shift following space left */
l = spctab[i++].siz;
while (spctab[i].typ != SPCTAB_FREE && spctab[i].typ != SPCTAB_EOF)
{
/* Read the space and write shifted to the left */
off = (off_t)spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = spctab[i].siz;
if ((rc = read (fd, buf, len)) != len)
goto cdsk_read_error;
off -= l;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = write (fd, buf, len)) != len)
goto cdsk_write_error;
spctab[i].pos -= l;
/* Update the l2 or l1 table entry */
if (spctab[i].typ == trktyp)
{
l1x = spctab[i].val/256;
l2x = spctab[i].val%256;
off = (off_t)l1[l1x] + l2x * CCKD_L2ENT_SIZE;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CCKD_L2ENT_SIZE;
if ((rc = read (fd, &l2ent, len)) != len)
goto cdsk_read_error;
l2ent.pos -= l;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = write (fd, &l2ent, len)) != len)
goto cdsk_write_error;
} /* trk/blkgrp relocated */
else if (spctab[i].typ == SPCTAB_L2)
l1[spctab[i].val] -= l;
i++;
} /* while not FREE space or EOF */
goto cdsk_fsperr_retry;
} /* if short free space found */
/*-----------------------------------------------------------
* Phase 2 -- rebuild free space statistics
*-----------------------------------------------------------*/
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
cdevhdr.size = cdevhdr.used = cdevhdr.free =
cdevhdr.free_total = cdevhdr.free_largest =
cdevhdr.free_number = cdevhdr.free_imbed = 0;
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_FREE)
{
cdevhdr.size += spctab[i].siz;
if (spctab[i].siz > cdevhdr.free_largest)
cdevhdr.free_largest = spctab[i].siz;
cdevhdr.free_total += spctab[i].siz;
cdevhdr.free_number++;
}
else
{
cdevhdr.size += spctab[i].siz;
cdevhdr.used += spctab[i].len;
cdevhdr.free_total += spctab[i].siz - spctab[i].len;
cdevhdr.free_imbed += spctab[i].siz - spctab[i].len;
}
/*-----------------------------------------------------------
* Phase 3 -- write the free space
*-----------------------------------------------------------*/
if (cdevhdr.free_number)
{
/* size needed for new format free space */
len = (cdevhdr.free_number+1) * CCKD_FREEBLK_SIZE;
/* look for existing free space to fit new format free space */
for (i = off = 0; !off && spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_FREE && len <= (int)spctab[i].siz)
off = (off_t)spctab[i].pos;
/* if no applicable space see if we can append to the file */
if (!off && maxsize - cdevhdr.size >= len)
off = (off_t)cdevhdr.size;
/* get free space buffer */
if (off && (fsp = malloc (len)) == NULL)
off = 0;
if (off)
{
/* new format free space */
memcpy (fsp, "FREE_BLK", 8);
for (i = 0, j = 1; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_FREE)
{
fsp[j].pos = spctab[i].pos;
fsp[j++].len = spctab[i].siz;
}
/* Write the free space */
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if ((rc = write (fd, fsp, len)) != len)
goto cdsk_write_error;
cdevhdr.free = (U32)off;
free (fsp);
fsp = NULL;
} /* new format free space */
else
{
/* old format free space */
len = CCKD_FREEBLK_SIZE;
for (i = 0; spctab[i].typ != SPCTAB_FREE; i++);
cdevhdr.free = spctab[i].pos;
off = (off_t)spctab[i].pos;
freeblk.pos = 0;
freeblk.len = spctab[i].siz;
for (i = i + 1; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].typ == SPCTAB_FREE)
{
freeblk.pos = spctab[i].pos;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if (write (fd, &freeblk, len) != len)
goto cdsk_write_error;
off = (off_t)spctab[i].pos;
freeblk.pos = 0;
freeblk.len = spctab[i].len;
}
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
if (write (fd, &freeblk, len) != len)
goto cdsk_write_error;
} /* old format free space */
} /* if (cdevhdr.free_number) */
/* Write cdevhdr and l1 table */
off = CCKD_DEVHDR_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = CCKD_DEVHDR_SIZE;
if (write (fd, &cdevhdr, len) != len)
goto cdsk_write_error;
off = CCKD_L1TAB_POS;
if (lseek (fd, off, SEEK_SET) < 0)
goto cdsk_lseek_error;
len = l1size;
if (write (fd, l1, len) != len)
goto cdsk_write_error;
/* Truncate the file */
off = (off_t)cdevhdr.size;
if (cdevhdr.free == cdevhdr.size)
off += (cdevhdr.free_number+1) * CCKD_FREEBLK_SIZE;
rc = ftruncate (fd, off);
cckdumsg (dev, 104, "free space rebuilt\n");
} /* if (fsperr) */
/*---------------------------------------------------------------
* Return
*---------------------------------------------------------------*/
cdsk_return_ok:
rc = recovery ? 2 : fsperr ? 1 : 0;
if (!ro && (cdevhdr.options & (CCKD_ORDWR|CCKD_OPENED|CCKD_SPERRS)))
{
/*
* Leave the ORDWR bit on for now. This will prevent
* old-format free space releases from doing a -1 check
* on a file that has new-format free space
*/
#if 0
cdevhdr.options &= ~(CCKD_ORDWR|CCKD_OPENED|CCKD_SPERRS);
#else
cdevhdr.options &= ~(CCKD_OPENED|CCKD_SPERRS);
#endif
/* Set version.release.modlvl */
cdevhdr.vrm[0] = CCKD_VERSION;
cdevhdr.vrm[1] = CCKD_RELEASE;
cdevhdr.vrm[2] = CCKD_MODLVL;
off = CCKD_DEVHDR_POS;
if (lseek (fd, CCKD_DEVHDR_POS, SEEK_SET) >= 0)
write (fd, &cdevhdr, CCKD_DEVHDR_SIZE);
}
cdsk_return:
/* free all space */
if (l1) free (l1);
if (spctab) free (spctab);
if (l2errs) free (l2errs);
if (rcvtab) free (rcvtab);
if (fsp) free (fsp);
if (l2)
{
for (i = 0; i < cdevhdr.numl1tab; i++)
if (l2[i]) free (l2[i]);
free (l2);
}
return rc;
/*---------------------------------------------------------------
* Error exits
*---------------------------------------------------------------*/
cdsk_fstat_error:
cckdumsg (dev, 701, "fstat error: %s\n", strerror(errno));
goto cdsk_error;
cdsk_lseek_error:
cckdumsg (dev, 702, "lseek error offset 0x%" I64_FMT "x: %s\n",
(long long)off, strerror(errno));
goto cdsk_error;
cdsk_read_error:
cckdumsg (dev, 703, "read error rc=%d offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cdsk_error;
cdsk_write_error:
cckdumsg (dev, 704, "write error rc=%d offset 0x%" I64_FMT "x len %d: %s\n",
rc, (long long)off, len, rc < 0 ? strerror(errno) : "incomplete");
goto cdsk_error;
cdsk_malloc_error:
cckdumsg (dev, 705, "malloc error, size=%d: %s\n",
len, strerror(errno));
goto cdsk_error;
cdsk_calloc_error:
cckdumsg (dev, 706, "calloc error, size=%d: %s\n",
n*len, strerror(errno));
goto cdsk_error;
cdsk_error:
rc = -1;
goto cdsk_return;
} /* end function cckd_chkdsk */
/*-------------------------------------------------------------------
* cckd_chkdsk() space table sort
*-------------------------------------------------------------------*/
static int cdsk_spctab_sort(const void *a, const void *b)
{
const SPCTAB *x = a, *y = b;
if (x->typ == SPCTAB_NONE) return 1;
else if (y->typ == SPCTAB_NONE) return -1;
else if (x->typ == SPCTAB_EOF) return 1;
else if (y->typ == SPCTAB_EOF) return -1;
else if (x->pos < y->pos) return -1;
else return 1;
} /* end function cdsk_spctab_sort */
/*-------------------------------------------------------------------*/
/* Build free space in the space table */
/*-------------------------------------------------------------------*/
static int cdsk_build_free_space(SPCTAB *spctab, int s)
{
int i;
for (i = 0; i < s; i++)
if (spctab[i].typ == SPCTAB_FREE)
spctab[i].typ = SPCTAB_NONE;
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
while (spctab[s-1].typ == SPCTAB_NONE) s--;
for (i = 0; spctab[i].typ != SPCTAB_EOF; i++)
if (spctab[i].pos + spctab[i].siz < spctab[i+1].pos)
{
spctab[s].typ = SPCTAB_FREE;
spctab[s].val = -1;
spctab[s].pos = spctab[i].pos + spctab[i].siz;
spctab[s].len =
spctab[s].siz = spctab[i+1].pos - spctab[s].pos;
s++;
}
qsort (spctab, s, sizeof(SPCTAB), cdsk_spctab_sort);
return s;
}
/*-------------------------------------------------------------------*/
/* Validate a track image */
/* */
/* If `len' is negative and compression is CCKD_COMPRESS_NONE then */
/* `len' indicates a buffer size containing the track image and the */
/* value returned is the actual track image length */
/*-------------------------------------------------------------------*/
static int cdsk_valid_trk (int trk, BYTE *buf, int heads, int len)
{
int i; /* Index */
int len2; /* Positive `len' */
int kl, dl; /* Key/Data lengths */
BYTE *bufp; /* Buffer pointer */
int bufl; /* Buffer length */
#ifdef HAVE_LIBZ
uLongf zlen;
#endif
#ifdef CCKD_BZIP2
unsigned int bz2len;
#endif
#if defined(HAVE_LIBZ) || defined(CCKD_BZIP2)
int rc; /* Return code */
BYTE buf2[65536]; /* Uncompressed buffer */
#endif
/* Negative len only allowed for comp none */
len2 = len > 0 ? len : -len;
if (len2 < CKDDASD_TRKHDR_SIZE + 8)
return 0;
/* Uncompress the track/block image */
switch (buf[0]) {
case CCKD_COMPRESS_NONE:
bufp = buf;
bufl = len2;
break;
#ifdef HAVE_LIBZ
case CCKD_COMPRESS_ZLIB:
if (len < 0) return 0;
bufp = (BYTE *)buf2;
memcpy (buf2, buf, CKDDASD_TRKHDR_SIZE);
zlen = sizeof(buf2) - CKDDASD_TRKHDR_SIZE;
rc = uncompress (buf2 + CKDDASD_TRKHDR_SIZE, &zlen,
buf + CKDDASD_TRKHDR_SIZE, len - CKDDASD_TRKHDR_SIZE);
if (rc != Z_OK)
return 0;
bufl = (int)zlen + CKDDASD_TRKHDR_SIZE;
break;
#endif
#ifdef CCKD_BZIP2
case CCKD_COMPRESS_BZIP2:
if (len < 0) return 0;
bufp = (BYTE *)buf2;
memcpy (buf2, buf, CKDDASD_TRKHDR_SIZE);
bz2len = sizeof(buf2) - CKDDASD_TRKHDR_SIZE;
rc = BZ2_bzBuffToBuffDecompress ( (char *)&buf2[CKDDASD_TRKHDR_SIZE], &bz2len,
(char *)&buf[CKDDASD_TRKHDR_SIZE], len - CKDDASD_TRKHDR_SIZE, 0, 0);
if (rc != BZ_OK)
return 0;
bufl = (int)bz2len + CKDDASD_TRKHDR_SIZE;
break;
#endif
default:
return 0;
} /* switch (buf[0]) */
/* fba check */
if (heads == 65536)
{
if (bufl != CFBA_BLOCK_SIZE + CKDDASD_TRKHDR_SIZE)
return 0;
else
return len > 0 ? len : bufl;
}
/* Check length */
if (bufl <= 5 + 8 + 8 + 8 + 8) return 0;
/* Check ha */
if (fetch_hw(bufp + 1) != trk / heads
|| fetch_hw(bufp + 3) != trk % heads)
return 0;
/* Check r0 */
if (fetch_hw(bufp + 1) != fetch_hw(bufp + 5)
|| fetch_hw(bufp + 3) != fetch_hw(bufp + 7)
|| bufp[9] != 0 || bufp[10] != 0
|| fetch_hw(bufp +11) != 8)
return 0;
/* Check user records */
for (i = 21; i < bufl - 8; i += 8 + kl + dl)
{
if (fetch_hw(bufp + i + 2) >= heads || bufp[i + 4] == 0)
break;
kl = bufp[i + 5];
dl = fetch_hw(bufp + i + 6);
}
if (len < 0) bufl = i + 8;
if (i != bufl - 8 || memcmp(bufp + i, eighthexFF, 8))
return 0;
return len > 0 ? len : bufl;
} /* end function cdsk_valid_trk */
/*-------------------------------------------------------------------*/
/* Message function */
/*-------------------------------------------------------------------*/
DLL_EXPORT void cckdumsg (DEVBLK *dev, int n, char *format, ...)
{
CCKDDASD_EXT *cckd;
int sfx;
int i;
char *p;
va_list vl;
char msg[4096];
cckd = dev->cckd_ext;
sfx = cckd ? cckd->sfn : -1;
i = sprintf (msg, "HHCCU%3.3d%c ",
n, n < 400 ? 'I' : n < 700 ? 'W' : 'E');
if (sfx >= 0)
i += sprintf (msg+i, "%4.4X file[%d]: ", dev->devnum, sfx);
else
{
if ((p = strrchr(dev->filename, '/')) == NULL
&& (p = strrchr(dev->filename, '\\')) == NULL)
p = dev->filename;
else
p++;
i += sprintf (msg+i, "%s: ", p);
}
va_start (vl, format);
vsprintf (msg+i, format, vl);
va_end (vl);
if (dev->batch)
fprintf(stdout,"%s",msg);
else
logmsg("%s",msg);
}
|