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 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671
|
/*
* A utility program for copying files. Specialised for "files" that
* represent devices that understand the SCSI command set.
*
* Copyright (C) 2018-2023 D. Gilbert
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is a specialisation of the Unix "dd" command in which
* one or both of the given files is a scsi generic device.
* A logical block size ('bs') is assumed to be 512 if not given. This
* program complains if 'ibs' or 'obs' are given with some other value
* than 'bs'. If 'if' is not given or 'if=-' then stdin is assumed. If
* 'of' is not given or 'of=-' then stdout assumed.
*
* A non-standard argument "bpt" (blocks per transfer) is added to control
* the maximum number of blocks in each transfer. The default value is 128.
* For example if "bs=512" and "bpt=32" then a maximum of 32 blocks (16 KiB
* in this case) are transferred to or from the sg device in a single SCSI
* command.
*
* This version is designed for the linux kernel 4 and 5 series.
*
* sg_mrq_dd uses C++ threads and MRQ (multiple requests (in one invocation))
* facilities in the sg version 4 driver to do "dd" type copies and verifies.
*
*/
static const char * version_str = "1.45 20230415";
#define _XOPEN_SOURCE 600
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h> /* for nanosleep() */
#include <poll.h>
#include <limits.h>
// #include <pthread.h>
#include <signal.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#ifndef major
#include <sys/types.h>
#endif
#include <sys/time.h>
#include <linux/major.h> /* for MEM_MAJOR, SCSI_GENERIC_MAJOR, etc */
#include <linux/fs.h> /* for BLKSSZGET and friends */
#include <sys/mman.h> /* for mmap() system call */
#include <vector>
#include <array>
#include <atomic> // C++ header replacing <stdatomic.h>
#include <random>
#include <thread> // needed for std::this_thread::yield()
#include <mutex>
#include <condition_variable> // for infant_cv: copy/verify first segment
// single threaded
#include <chrono>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_GETRANDOM
#include <sys/random.h> /* for getrandom() system call */
#endif
#ifndef HAVE_LINUX_SG_V4_HDR
/* Kernel uapi header contain __user decorations on user space pointers
* to indicate they are unsafe in the kernel space. However glibc takes
* all those __user decorations out from headers in /usr/include/linux .
* So to stop compile errors when directly importing include/uapi/scsi/sg.h
* undef __user before doing that include. */
#define __user
/* Want to block the original sg.h header from also being included. That
* causes lots of multiple definition errors. This will only work if this
* header is included _before_ the original sg.h header. */
#define _SCSI_GENERIC_H /* original kernel header guard */
#define _SCSI_SG_H /* glibc header guard */
#include "uapi_sg.h" /* local copy of include/uapi/scsi/sg.h */
#else
#define __user
#endif /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
// C++ local header
#include "sg_scat_gath.h"
// C headers associated with sg3_utils library
#include "sg_lib.h"
#include "sg_cmds_basic.h"
#include "sg_io_linux.h"
#include "sg_unaligned.h"
#include "sg_pr2serr.h"
using namespace std;
// #ifdef __GNUC__
// #ifndef __clang__
// #pragma GCC diagnostic ignored "-Wclobbered"
// #endif
// #endif
#ifndef SGV4_FLAG_POLLED
#define SGV4_FLAG_POLLED 0x800
#endif
#define MAX_SGL_NUM_VAL (INT32_MAX - 1) /* should reduce for testing */
// #define MAX_SGL_NUM_VAL 7 /* should reduce for testing */
#if MAX_SGL_NUM_VAL > INT32_MAX
#error "MAX_SGL_NUM_VAL cannot exceed 2^31 - 1"
#endif
#define DEF_BLOCK_SIZE 512
#define DEF_BLOCKS_PER_TRANSFER 128
#define DEF_BLOCKS_PER_2048TRANSFER 32
#define DEF_SDT_ICT_MS 300
#define DEF_SDT_CRT_SEC 3
#define DEF_SCSI_CDB_SZ 10
#define MAX_SCSI_CDB_SZ 16 /* could be 32 */
#define PACK_ID_TID_MULTIPLIER (0x1000000) /* 16,777,216 */
#define MAX_SLICES 16 /* number of IFILE,OFILE pairs */
#define MAX_BPT_VALUE (1 << 24) /* used for maximum bs as well */
#define MAX_COUNT_SKIP_SEEK (1LL << 48) /* coverity wants upper bound */
#define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
#define READ_CAP_REPLY_LEN 8
#define RCAP16_REPLY_LEN 32
#define DEF_TIMEOUT 60000 /* 60,000 millisecs == 60 seconds */
#define SGP_READ10 0x28
#define SGP_PRE_FETCH10 0x34
#define SGP_PRE_FETCH16 0x90
#define SGP_VERIFY10 0x2f
#define SGP_WRITE10 0x2a
#define DEF_NUM_THREADS 4
#define MAX_NUM_THREADS 1024 /* was SG_MAX_QUEUE with v3 driver */
#define DEF_MRQ_NUM 16
#define FT_UNKNOWN 0 /* yet to be checked */
#define FT_OTHER 1 /* filetype other than one of the following */
#define FT_SG 2 /* filetype is sg char device */
#define FT_DEV_NULL 4 /* either /dev/null, /dev/zero, or "." */
#define FT_ST 8 /* filetype is st char device (tape) */
#define FT_BLOCK 16 /* filetype is a block device */
#define FT_FIFO 32 /* fifo (named or unnamed pipe (stdout)) */
#define FT_CHAR 64 /* fifo (named or unnamed pipe (stdout)) */
#define FT_RANDOM_0_FF 128 /* iflag=00, iflag=ff and iflag=random
override if=IFILE */
#define FT_ERROR 256 /* couldn't "stat" file */
#define DEV_NULL_MINOR_NUM 3
#define DEV_ZERO_MINOR_NUM 5
#define EBUFF_SZ 768
#define PROC_SCSI_SG_VERSION "/proc/scsi/sg/version"
#define SYS_SCSI_SG_VERSION "/sys/module/sg/version"
struct flags_t {
bool append;
bool coe;
bool dio;
bool direct;
bool dpo;
bool dsync;
bool excl;
bool ff;
bool fua;
bool masync; /* more async sg v4 driver fd flag */
bool mout_if; /* META_OUT_IF flag at mrq level */
bool nocreat;
bool no_dur;
bool no_thresh;
bool no_waitq; /* dummy, no longer supported, just warn */
bool order_wr;
bool polled; /* was previously 'hipri' */
bool qhead;
bool qtail;
bool random;
bool serial;
bool same_fds;
bool wq_excl;
bool zero;
int cdl; /* command duration limits, 0 --> no cdl */
int mmap;
};
typedef pair<int64_t, int> get_next_res_t; /* LBA, num */
typedef array<uint8_t, MAX_SCSI_CDB_SZ> cdb_arr_t;
struct cp_ver_pair_t {
cp_ver_pair_t() {}
get_next_res_t get_next(int desired_num_blks);
enum class my_state {empty,
init,
underway,
ignore,
finished} state = {my_state::empty};
int my_index = 0;
int in_fd = -1;
int in_type = FT_UNKNOWN;
int out_fd = -1;
int out_type = FT_UNKNOWN;
int64_t dd_count = 0;
atomic<int64_t> next_count_pos {};
atomic<int64_t> in_rem_count {};
atomic<int64_t> out_rem_count {};
atomic<int> in_partial {};
atomic<int> out_partial {};
atomic<int> sum_of_resids {};
};
typedef array<cp_ver_pair_t, MAX_SLICES> cp_ver_arr_t;
/* There is one instance of this structure and it is at file scope so it is
* initialized to zero. The design of this copy multi-threaded copy algorithm
* attempts to have no locks on the fast path. Contention in gcoll.get_next()
* is resolved by the loser repeating its operation. Statistics and error
* information is held in each thread until it shuts down and contention
* can occur at that point. */
struct global_collection /* one instance visible to all threads */
{
cp_ver_arr_t cp_ver_arr;
/* get_next() is the pivotal function for multi-threaded safety. It can
* be safely called from all threads with the desired number of blocks
* (typically mrq*bpt) and this function returns a pair. The first pair
* value is the starting count value/index [0..dd_count) and the second
* pair value is the number of blocks to copy. If desired_num_blks is
* negative this flags an error has occurred. If the second value in the
* returned pair is 0 then the calling thread should shutdown; a
* negative value indicates an error has occurred (e.g. in another
* thread) and the calling thread should shutdown. */
int in0fd;
int64_t dd_count;
int in_type; /* expect all IFILEs to have same type */
int cdbsz_in;
int help;
struct flags_t in_flags;
atomic<int> in_partial; /* | */
off_t in_st_size; /* Only for FT_OTHER (regular) file */
int mrq_num; /* if user gives 0, set this to 1 */
int out0fd;
int out_type;
int cdbsz_out;
struct flags_t out_flags;
atomic<int> out_partial; /* | */
off_t out_st_size; /* Only for FT_OTHER (regular) file */
condition_variable infant_cv; /* after thread:0 does first segment */
mutex infant_mut;
int bs;
int bpt;
int cmd_timeout; /* in milliseconds */
int elem_sz;
int outregfd;
int outreg_type;
off_t outreg_st_size;
atomic<int> dio_incomplete_count;
atomic<int> sum_of_resids;
atomic<int> reason_res;
atomic<int> most_recent_pack_id;
uint32_t sdt_ict; /* stall detection; initial check time (milliseconds) */
uint32_t sdt_crt; /* check repetition time (seconds), after first stall */
int dry_run;
int verbose;
bool mrq_eq_0; /* true when user gives mrq=0 */
bool processed;
bool cdbsz_given;
bool cdl_given;
bool count_given;
bool ese;
bool flexible;
bool mrq_polled;
bool ofile_given;
bool unit_nanosec; /* default duration unit is millisecond */
bool verify; /* don't copy, verify like Unix: cmp */
bool prefetch; /* for verify: do PF(b),RD(a),V(b)_a_data */
vector<string> inf_v;
vector<string> outf_v;
const char * infp;
const char * outfp;
class scat_gath_list i_sgl;
class scat_gath_list o_sgl;
};
typedef struct request_element
{ /* one instance per worker thread */
struct global_collection *clp;
bool has_share;
bool both_sg;
bool same_sg;
bool only_in_sg;
bool only_out_sg;
bool stop_after_write;
bool stop_now;
int id;
int bs;
int infd;
int outfd;
int outregfd;
uint8_t * buffp;
uint8_t * alloc_bp;
struct sg_io_v4 io_hdr4[2];
uint8_t cmd[MAX_SCSI_CDB_SZ];
uint8_t sb[SENSE_BUFF_LEN];
int dio_incomplete_count;
int mmap_active;
int rd_p_id;
int rep_count;
int rq_id;
int mmap_len;
int mrq_id;
int mrq_index;
int mrq_pack_id_off;
uint32_t a_mrq_din_blks;
uint32_t a_mrq_dout_blks;
int64_t in_follow_on;
int64_t out_follow_on;
int64_t in_local_count;
int64_t out_local_count;
int64_t in_rem_count;
int64_t out_rem_count;
int in_local_partial;
int out_local_partial;
int in_resid_bytes;
long seed;
#ifdef HAVE_SRAND48_R /* gcc extension. N.B. non-reentrant version slower */
struct drand48_data drand;/* opaque, used by srand48_r and mrand48_r */
#endif
} Rq_elem;
/* Additional parameters for sg_start_io() and sg_finish_io() */
struct sg_io_extra {
bool prefetch;
bool dout_is_split;
int hpv4_ind;
int blk_offset;
int blks;
};
#define MONO_MRQ_ID_INIT 0x10000
/* Use this class to wrap C++11 <random> features to produce uniform random
* unsigned ints in the range [lo, hi] (inclusive) given a_seed */
class Rand_uint {
public:
Rand_uint(unsigned int lo, unsigned int hi, unsigned int a_seed)
: uid(lo, hi), dre(a_seed) { }
/* uid ctor takes inclusive range when integral type */
unsigned int get() { return uid(dre); }
private:
uniform_int_distribution<unsigned int> uid;
default_random_engine dre;
};
static atomic<int> num_ebusy(0);
static atomic<int> num_start_eagain(0);
static atomic<int> num_fin_eagain(0);
static atomic<int> num_miscompare(0);
static atomic<int> num_fallthru_sigusr2(0);
static atomic<bool> vb_first_time(true);
static sigset_t signal_set;
static sigset_t orig_signal_set;
static const char * sg_allow_dio = "/sys/module/sg/parameters/allow_dio";
static int do_both_sg_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks,
vector<cdb_arr_t> & a_cdb,
vector<struct sg_io_v4> & a_v4);
static int do_both_sg_segment_mrq0(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks);
static int do_normal_sg_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks,
vector<cdb_arr_t> & a_cdb,
vector<struct sg_io_v4> & a_v4);
static int do_normal_normal_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks);
#define STRERR_BUFF_LEN 128
static mutex strerr_mut;
static bool have_sg_version = false;
static int sg_version = 0;
static bool sg_version_ge_40045 = false;
static atomic<bool> shutting_down{false};
static bool do_sync = false;
static int do_time = 1;
static struct global_collection gcoll;
static struct timeval start_tm;
static int num_threads = DEF_NUM_THREADS;
static bool after1 = false;
static int listen_t_tid;
static const char * my_name = "sg_mrq_dd: ";
// static const char * mrq_blk_s = "mrq: ordinary blocking";
static const char * mrq_svb_s = "mrq: shared variable blocking (svb)";
static const char * mrq_ob_s = "mrq: ordered blocking";
static const char * mrq_vb_s = "mrq: variable blocking";
#ifdef __GNUC__
static int pr2serr_lk(const char * fmt, ...)
__attribute__ ((format (printf, 1, 2)));
#else
static int pr2serr_lk(const char * fmt, ...);
#endif
static int
pr2serr_lk(const char * fmt, ...)
{
int n;
va_list args;
lock_guard<mutex> lk(strerr_mut);
va_start(args, fmt);
n = vfprintf(stderr, fmt, args);
va_end(args);
return n;
}
static void
usage(int pg_num)
{
if (pg_num > 4)
goto page5;
if (pg_num > 3)
goto page4;
else if (pg_num > 2)
goto page3;
else if (pg_num > 1)
goto page2;
pr2serr("Usage: sg_mrq_dd [bs=BS] [conv=CONV] [count=COUNT] [ibs=BS] "
"[if=IFILE*]\n"
" [iflag=FLAGS] [obs=BS] [of=OFILE*] "
"[oflag=FLAGS]\n"
" [seek=SEEK] [skip=SKIP] [--help] [--verify] "
"[--version]\n\n");
pr2serr(" [bpt=BPT] [cdbsz=6|10|12|16] [cdl=CDL] "
"[dio=0|1]\n"
" [elemsz_kb=EKB] [ese=0|1] [fua=0|1|2|3] "
"[polled=NRQS]\n"
" [mrq=NRQS] [ofreg=OFREG] [sdt=SDT] "
"[sync=0|1]\n"
" [thr=THR] [time=0|1|2[,TO]] [verbose=VERB] "
"[--dry-run]\n"
" [--pre-fetch] [--verbose] [--version]\n\n"
" where: operands have the form name=value and are pecular to "
"'dd'\n"
" style commands, and options start with one or "
"two hyphens;\n"
" the main operands and options (shown in first group "
"above) are:\n"
" bs must be device logical block size (default "
"512)\n"
" conv comma separated list from: [nocreat,noerror,"
"notrunc,\n"
" null,sync]\n"
" count number of blocks to copy (def: device size)\n"
" if file(s) or device(s) to read from (def: "
"stdin)\n"
" iflag comma separated list from: [00,coe,dio,"
"direct,dpo,\n"
" dsync,excl,ff,fua,masync,mmap,mout_if,nodur,"
"null,\n"
" order,qhead,qtail,random,same_fds,serial,"
"wq_excl]\n"
" of file(s) or device(s) to write to (def: "
"/dev/null)\n"
" 'of=.' also outputs to /dev/null\n"
" oflag comma separated list from: [append,nocreat,\n"
" <<list from iflag>>]\n"
" seek block position to start writing to OFILE\n"
" skip block position to start reading from IFILE\n"
" --compare|-c same action as --verify\n"
" --help|-h output this usage message then exit\n"
" --verify|-x do a verify (compare) operation [def: do a "
"copy]\n"
" --version|-V output version string then exit\n\n"
"Copy IFILE to OFILE, similar to dd command. A comma separated "
"list of files\n may be given for IFILE*, ditto for OFILE*. "
"This utility is specialized for\nSCSI devices and uses the "
"'multiple requests' (mrq) in a single invocation\nfacility in "
"version 4 of the sg driver unless mrq=0. Usually one or both\n"
"IFILE and OFILE will be sg devices. With the --verify option "
"it does a\nverify/compare operation instead of a copy. This "
"utility is Linux specific.\nUse '-hh', '-hhh', '-hhhh' or "
"'-hhhhh' for more information.\n"
);
return;
page2:
pr2serr("Syntax: sg_mrq_dd [operands] [options]\n\n"
" the lesser used operands and option are:\n\n"
" bpt is blocks_per_transfer (default is 128)\n"
" cdbsz size of SCSI READ, WRITE or VERIFY cdb_s "
"(default is 10)\n"
" cdl command duration limits value 0 to 7 (def: "
"0 (no cdl))\n"
" dio is direct IO, 1->attempt, 0->indirect IO (def)\n"
" elemsz_kb=EKB scatter gather list element size in "
"kibibytes;\n"
" must be power of two, >= page_size "
"(typically 4)\n"
" ese=0|1 exit on secondary error when 1, else continue\n"
" fua force unit access: 0->don't(def), 1->OFILE, "
"2->IFILE,\n"
" 3->OFILE+IFILE\n"
" ibs IFILE logical block size, cannot differ from "
"obs or bs\n"
" hipri same as polled=NRQS; name 'hipri' is deprecated\n"
" mrq NRQS is number of cmds placed in each sg "
"ioctl\n"
" (def: 16). Does not set mrq hipri flag.\n"
" if mrq=0 does one-by-one, blocking "
"ioctl(SG_IO)s\n"
" obs OFILE logical block size, cannot differ from "
"ibs or bs\n"
" ofreg OFREG is regular file or pipe to send what is "
"read from\n"
" polled similar to mrq=NRQS operand but also sets "
"polled flag\n"
" IFILE in the first half of each shared element\n"
" sdt stall detection times: CRT[,ICT]. CRT: check "
"repetition\n"
" time (after first) in seconds; ICT: initial "
"check time\n"
" in milliseconds. Default: 3,300 . Use CRT=0 "
"to disable\n"
" sync 0->no sync(def), 1->SYNCHRONIZE CACHE on OFILE "
"after copy\n"
" thr is number of threads, must be > 0, default 4, "
"max 1024\n"
" time 0->no timing; 1/2->millisec/nanosec precision "
"(def: 1);\n"
" TO is command timeout in seconds (def: 60)\n"
" verbose increase verbosity (def: VERB=0)\n"
" --dry-run|-d prepare but bypass copy/read\n"
" --prefetch|-p with verify: do pre-fetch first\n"
" --verbose|-v increase verbosity of utility\n\n"
"Use '-hhh', '-hhhh' or '-hhhhh' for more information about "
"flags.\n"
);
return;
page3:
pr2serr("Syntax: sg_mrq_dd [operands] [options]\n\n"
" where: 'iflag=<arg>' and 'oflag=<arg>' arguments are listed "
"below:\n\n"
" 00 use all zeros instead of if=IFILE (only in "
"iflag)\n"
" 00,ff generates blocks that contain own (32 bit be) "
"blk addr\n"
" append append output to OFILE (assumes OFILE is "
"regular file)\n"
" coe continue of error (reading, fills with zeros)\n"
" dio sets the SG_FLAG_DIRECT_IO in sg requests\n"
" direct sets the O_DIRECT flag on open()\n"
" dpo sets the DPO (disable page out) in SCSI READs "
"and WRITEs\n"
" dsync sets the O_SYNC flag on open()\n"
" excl sets the O_EXCL flag on open()\n"
" ff use all 0xff bytes instead of if=IFILE (only in "
"iflag)\n"
" fua sets the FUA (force unit access) in SCSI READs "
"and WRITEs\n"
" hipri same as 'polled'; name 'hipri' is deprecated\n"
" masync set 'more async' flag on this sg device\n"
" mmap setup mmap IO on IFILE or OFILE\n"
" mmap,mmap when used twice, doesn't call munmap()\n"
" mout_if set META_OUT_IF flag on control object\n"
" nocreat will fail rather than create OFILE\n"
" nodur turns off command duration calculations\n"
" no_thresh skip checking per fd max data xfer size\n"
" order require write ordering on sg->sg copy; only "
"for oflag\n"
" polled set POLLED flag and use blk_poll() for "
"completions\n"
" qhead queue new request at head of block queue\n"
" qtail queue new request at tail of block queue (def: "
"q at head)\n"
" random use random data instead of if=IFILE (only in "
"iflag)\n"
" same_fds each thread of a IOFILE pair uses same fds\n"
" serial serialize sg command execution (def: overlap)\n"
" wq_excl set SG_CTL_FLAGM_EXCL_WAITQ on this sg fd\n"
"\n"
"Copies IFILE to OFILE (and to OFILE2 if given). If IFILE and "
"OFILE are sg\ndevices 'shared' mode is selected. "
"When sharing, the data stays in a\nsingle "
"in-kernel buffer which is copied (or mmap-ed) to the user "
"space\nif the 'ofreg=OFREG' is given. Use '-hhhh' or '-hhhhh' "
"for more information.\n"
);
return;
page4:
pr2serr("pack_id:\n"
"These are ascending integers, starting at 1, associated with "
"each issued\nSCSI command. When both IFILE and OFILE are sg "
"devices, then the READ in\neach read-write pair is issued an "
"even pack_id and its WRITE pair is\ngiven the pack_id one "
"higher (i.e. an odd number). This enables a\n'dmesg -w' "
"user to see that progress is being "
"made.\n\n");
pr2serr("Debugging:\n"
"Apart from using one or more '--verbose' options which gets a "
"bit noisy\n'dmesg -w' can give a good overview "
"of what is happening.\nThat does a sg driver object tree "
"traversal that does minimal locking\nto make sure that each "
"traversal is 'safe'. So it is important to note\nthe whole "
"tree is not locked. This means for fast devices the overall\n"
"tree state may change while the traversal is occurring. For "
"example,\nit has been observed that both the read- and write- "
"sides of a request\nshare show they are in 'active' state "
"which should not be possible.\nIt occurs because the read-side "
"probably jumped out of active state and\nthe write-side "
"request entered it while some other nodes were being "
"printed.\n\n");
pr2serr("Busy state:\n"
"Busy state (abbreviated to 'bsy' in the dmesg "
"output)\nis entered during request setup and completion. It "
"is intended to be\na temporary state. It should not block "
"but does sometimes (e.g. in\nblock_get_request()). Even so "
"that blockage should be short and if not\nthere is a "
"problem.\n\n");
pr2serr("--verify :\n"
"For comparing IFILE with OFILE. Does repeated sequences of: "
"READ(ifile)\nand uses data returned to send to VERIFY(ofile, "
"BYTCHK=1). So the OFILE\ndevice/disk is doing the actual "
"comparison. Stops on first miscompare\nunless oflag=coe is "
"given\n\n");
pr2serr("--prefetch :\n"
"Used with --verify option. Prepends a PRE-FETCH(ofile, IMMED) "
"to verify\nsequence. This should speed the trailing VERIFY by "
"making sure that\nthe data it needs for the comparison is "
"already in its cache.\n");
return;
page5:
pr2serr(" IFILE and/or OFILE lists\n\n"
"For dd, its if= operand takes a single file (or device), ditto "
"for the of=\noperand. This utility extends that to "
"allowing a comma separated list\nof files. Ideally if multiple "
"IFILEs are given, the same number of OFILEs\nshould be given. "
"Simple expansions occur to make the list lengths equal\n"
"(e.g. if 5 IFILEs are given but no OFILEs, then OFILEs is "
"expanded to 5\n'/dev/null' files). IFILE,OFILE pairs with "
"the same list position are\ncalled a 'slice'. Each slice is "
"processed (i.e. copy or verify) in one or\nmore threads. The "
"number of threads must be >= the number of slices. Best\nif "
"the number of threads is an integer multiple of the number of "
"slices.\nThe file type of multiple IFILEs must be the same, "
"ditto for OFILEs.\nSupport for slices is for testing rather "
"than a general mechanism.\n");
}
static void
lk_print_command_len(const char *prefix, uint8_t * cmdp, int len, bool lock)
{
if (lock) {
lock_guard<mutex> lk(strerr_mut);
if (prefix && *prefix)
fputs(prefix, stderr);
sg_print_command_len(cmdp, len);
} else {
if (prefix && *prefix)
fputs(prefix, stderr);
sg_print_command_len(cmdp, len);
}
}
static void
lk_chk_n_print4(const char * leadin, const struct sg_io_v4 * h4p,
bool raw_sinfo)
{
lock_guard<mutex> lk(strerr_mut);
if (h4p->usr_ptr) {
const cdb_arr_t * cdbp = (const cdb_arr_t *)h4p->usr_ptr;
pr2serr("Failed cdb: ");
sg_print_command(cdbp->data());
} else
pr2serr("cdb: <null>\n");
sg_linux_sense_print(leadin, h4p->device_status, h4p->transport_status,
h4p->driver_status, (const uint8_t *)h4p->response,
h4p->response_len, raw_sinfo);
}
static void
hex2stderr_lk(const uint8_t * b_str, int len, int no_ascii)
{
lock_guard<mutex> lk(strerr_mut);
hex2stderr(b_str, len, no_ascii);
}
static int
system_wrapper(const char * cmd)
{
int res;
res = system(cmd);
if (WIFSIGNALED(res) &&
(WTERMSIG(res) == SIGINT || WTERMSIG(res) == SIGQUIT))
raise(WTERMSIG(res));
return WEXITSTATUS(res);
}
/* Flags decoded into abbreviations for those that are set, separated by
* '|' . */
static char *
sg_flags_str(int flags, int b_len, char * b)
{
int n = 0;
if ((b_len < 1) || (! b))
return b;
b[0] = '\0';
if (SG_FLAG_DIRECT_IO & flags) { /* 0x1 */
n += sg_scnpr(b + n, b_len - n, "DIO|");
if (n >= b_len)
goto fini;
}
if (SG_FLAG_MMAP_IO & flags) { /* 0x4 */
n += sg_scnpr(b + n, b_len - n, "MMAP|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_YIELD_TAG & flags) { /* 0x8 */
n += sg_scnpr(b + n, b_len - n, "YTAG|");
if (n >= b_len)
goto fini;
}
if (SG_FLAG_Q_AT_TAIL & flags) { /* 0x10 */
n += sg_scnpr(b + n, b_len - n, "QTAI|");
if (n >= b_len)
goto fini;
}
if (SG_FLAG_Q_AT_HEAD & flags) { /* 0x20 */
n += sg_scnpr(b + n, b_len - n, "QHEA|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_DOUT_OFFSET & flags) { /* 0x40 */
n += sg_scnpr(b + n, b_len - n, "DOFF|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_EVENTFD & flags) { /* 0x80 */
n += sg_scnpr(b + n, b_len - n, "EVFD|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_COMPLETE_B4 & flags) { /* 0x100 */
n += sg_scnpr(b + n, b_len - n, "CPL_B4|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_SIGNAL & flags) { /* 0x200 */
n += sg_scnpr(b + n, b_len - n, "SIGNAL|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_IMMED & flags) { /* 0x400 */
n += sg_scnpr(b + n, b_len - n, "IMM|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_POLLED & flags) { /* 0x800 */
n += sg_scnpr(b + n, b_len - n, "POLLED|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_STOP_IF & flags) { /* 0x1000 */
n += sg_scnpr(b + n, b_len - n, "STOPIF|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_DEV_SCOPE & flags) { /* 0x2000 */
n += sg_scnpr(b + n, b_len - n, "DEV_SC|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_SHARE & flags) { /* 0x4000 */
n += sg_scnpr(b + n, b_len - n, "SHARE|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_DO_ON_OTHER & flags) { /* 0x8000 */
n += sg_scnpr(b + n, b_len - n, "DO_OTH|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_NO_DXFER & flags) { /* 0x10000 */
n += sg_scnpr(b + n, b_len - n, "NOXFER|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_KEEP_SHARE & flags) { /* 0x20000 */
n += sg_scnpr(b + n, b_len - n, "KEEP_SH|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_MULTIPLE_REQS & flags) { /* 0x40000 */
n += sg_scnpr(b + n, b_len - n, "MRQS|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_ORDERED_WR & flags) { /* 0x80000 */
n += sg_scnpr(b + n, b_len - n, "OWR|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_REC_ORDER & flags) { /* 0x100000 */
n += sg_scnpr(b + n, b_len - n, "REC_O|");
if (n >= b_len)
goto fini;
}
if (SGV4_FLAG_META_OUT_IF & flags) { /* 0x200000 */
n += sg_scnpr(b + n, b_len - n, "MOUT_IF|");
if (n >= b_len)
goto fini;
}
if (0 == n)
n += sg_scnpr(b + n, b_len - n, "<none>");
fini:
if (n < b_len) { /* trim trailing '\' */
if ('|' == b[n - 1])
b[n - 1] = '\0';
} else if ('|' == b[b_len - 1])
b[b_len - 1] = '\0';
return b;
}
/* Info field decoded into abbreviations for those bits that are set,
* separated by '|' . */
static char *
sg_info_str(int info, int b_len, char * b)
{
int n = 0;
if ((b_len < 1) || (! b))
return b;
b[0] = '\0';
if (SG_INFO_CHECK & info) { /* 0x1 */
n += sg_scnpr(b + n, b_len - n, "CHK|");
if (n >= b_len)
goto fini;
}
if (SG_INFO_DIRECT_IO & info) { /* 0x2 */
n += sg_scnpr(b + n, b_len - n, "DIO|");
if (n >= b_len)
goto fini;
}
if (SG_INFO_MIXED_IO & info) { /* 0x4 */
n += sg_scnpr(b + n, b_len - n, "MIO|");
if (n >= b_len)
goto fini;
}
if (SG_INFO_DEVICE_DETACHING & info) { /* 0x8 */
n += sg_scnpr(b + n, b_len - n, "DETA|");
if (n >= b_len)
goto fini;
}
if (SG_INFO_ABORTED & info) { /* 0x10 */
n += sg_scnpr(b + n, b_len - n, "ABRT|");
if (n >= b_len)
goto fini;
}
if (SG_INFO_MRQ_FINI & info) { /* 0x20 */
n += sg_scnpr(b + n, b_len - n, "MRQF|");
if (n >= b_len)
goto fini;
}
fini:
if (n < b_len) { /* trim trailing '\' */
if ('|' == b[n - 1])
b[n - 1] = '\0';
} else if ('|' == b[b_len - 1])
b[b_len - 1] = '\0';
return b;
}
static void
v4hdr_out_lk(const char * leadin, const sg_io_v4 * h4p, int id, bool chk_info)
{
lock_guard<mutex> lk(strerr_mut);
char b[80];
if (leadin)
pr2serr("%s [id=%d]:\n", leadin, id);
if (('Q' != h4p->guard) || (0 != h4p->protocol) ||
(0 != h4p->subprotocol))
pr2serr(" <<<sg_io_v4 _NOT_ properly set>>>\n");
pr2serr(" pointers: cdb=%s sense=%s din=%p dout=%p\n",
(h4p->request ? "y" : "NULL"), (h4p->response ? "y" : "NULL"),
(void *)h4p->din_xferp, (void *)h4p->dout_xferp);
pr2serr(" lengths: cdb=%u sense=%u din=%u dout=%u\n",
h4p->request_len, h4p->max_response_len, h4p->din_xfer_len,
h4p->dout_xfer_len);
pr2serr(" flags=0x%x request_extra{pack_id}=%d\n",
h4p->flags, h4p->request_extra);
pr2serr(" flags set: %s\n", sg_flags_str(h4p->flags, sizeof(b), b));
pr2serr(" %s OUT:\n", leadin);
pr2serr(" response_len=%d driver/transport/device_status="
"0x%x/0x%x/0x%x\n", h4p->response_len, h4p->driver_status,
h4p->transport_status, h4p->device_status);
pr2serr(" info=0x%x din_resid=%u dout_resid=%u spare_out=%u "
"dur=%u\n",
h4p->info, h4p->din_resid, h4p->dout_resid, h4p->spare_out,
h4p->duration);
if (chk_info && (SG_INFO_CHECK & h4p->info))
pr2serr(" >>>> info: %s\n", sg_info_str(h4p->info, sizeof(b), b));
}
static void
fetch_sg_version(void)
{
FILE * fp;
char b[96];
have_sg_version = false;
sg_version = 0;
fp = fopen(PROC_SCSI_SG_VERSION, "r");
if (fp && fgets(b, sizeof(b) - 1, fp)) {
if (1 == sscanf(b, "%d", &sg_version))
have_sg_version = !!sg_version;
} else {
int j, k, l;
if (fp)
fclose(fp);
fp = fopen(SYS_SCSI_SG_VERSION, "r");
if (fp && fgets(b, sizeof(b) - 1, fp)) {
if (3 == sscanf(b, "%d.%d.%d", &j, &k, &l)) {
sg_version = (j * 10000) + (k * 100) + l;
have_sg_version = !!sg_version;
}
}
if (NULL == fp)
pr2serr("The sg driver may not be loaded\n");
}
if (fp)
fclose(fp);
}
static void
calc_duration_throughput(int contin)
{
struct timeval end_tm, res_tm;
double a, b;
gettimeofday(&end_tm, NULL);
res_tm.tv_sec = end_tm.tv_sec - start_tm.tv_sec;
res_tm.tv_usec = end_tm.tv_usec - start_tm.tv_usec;
if (res_tm.tv_usec < 0) {
--res_tm.tv_sec;
res_tm.tv_usec += 1000000;
}
a = res_tm.tv_sec;
a += (0.000001 * res_tm.tv_usec);
b = 0.0;
for (auto && cvp : gcoll.cp_ver_arr) {
if (cvp.state == cp_ver_pair_t::my_state::empty)
break;
b += (double)(cvp.dd_count - cvp.out_rem_count.load());
}
b *= (double)gcoll.bs;
pr2serr("time to %s data %s %d.%06d secs",
(gcoll.verify ? "verify" : "copy"), (contin ? "so far" : "was"),
(int)res_tm.tv_sec, (int)res_tm.tv_usec);
if ((a > 0.00001) && (b > 511))
pr2serr(", %.2f MB/sec\n", b / (a * 1000000.0));
else
pr2serr("\n");
}
static void
print_stats(const char * str)
{
bool show_slice = ((gcoll.cp_ver_arr.size() > 1) &&
(gcoll.cp_ver_arr[1].state !=
cp_ver_pair_t::my_state::empty));
int k = 0;
int64_t infull, outfull;
for (auto && cvp : gcoll.cp_ver_arr) {
++k;
if (cvp.state == cp_ver_pair_t::my_state::empty)
break;
if (cvp.state == cp_ver_pair_t::my_state::ignore) {
pr2serr(">>> IGNORING slice: %d\n", k);
continue;
}
if (show_slice)
pr2serr(">>> slice: %d\n", k);
if (0 != cvp.out_rem_count.load())
pr2serr(" remaining block count=%" PRId64 "\n",
cvp.out_rem_count.load());
infull = cvp.dd_count - cvp.in_rem_count.load();
pr2serr("%s%" PRId64 "+%d records in\n", str,
infull, cvp.in_partial.load());
if (cvp.out_type == FT_DEV_NULL)
pr2serr("%s0+0 records out\n", str);
else {
outfull = cvp.dd_count - cvp.out_rem_count.load();
pr2serr("%s%" PRId64 "+%d records %s\n", str,
outfull, cvp.out_partial.load(),
(gcoll.verify ? "verified" : "out"));
}
}
}
static void
interrupt_handler(int sig)
{
struct sigaction sigact;
sigact.sa_handler = SIG_DFL;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(sig, &sigact, NULL);
pr2serr("Interrupted by signal,");
if (do_time > 0)
calc_duration_throughput(0);
print_stats("");
kill(getpid(), sig);
}
static void
siginfo_handler(int sig)
{
if (sig) { ; } /* unused, dummy to suppress warning */
pr2serr("Progress report, continuing ...\n");
if (do_time > 0)
calc_duration_throughput(1);
print_stats(" ");
}
/* Usually this signal (SIGUSR2) will be caught by the timed wait in the
* sig_listen_thread thread but some might slip through while the timed
* wait is being re-armed or after that thread is finished. This handler
* acts as a backstop. */
static void
siginfo2_handler(int sig)
{
if (sig) { ; } /* unused, dummy to suppress warning */
++num_fallthru_sigusr2;
}
static void
install_handler(int sig_num, void (*sig_handler) (int sig))
{
struct sigaction sigact;
sigaction (sig_num, NULL, &sigact);
if (sigact.sa_handler != SIG_IGN)
{
sigact.sa_handler = sig_handler;
sigemptyset (&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction (sig_num, &sigact, NULL);
}
}
/* Make safe_strerror() thread safe */
static char *
tsafe_strerror(int code, char * ebp)
{
lock_guard<mutex> lk(strerr_mut);
char * cp;
cp = safe_strerror(code);
strncpy(ebp, cp, STRERR_BUFF_LEN);
ebp[STRERR_BUFF_LEN - 1] = '\0';
return ebp;
}
static int
dd_filetype(const char * filename, off_t & st_size)
{
struct stat st;
size_t len = strlen(filename);
if ((1 == len) && ('.' == filename[0]))
return FT_DEV_NULL;
if (stat(filename, &st) < 0)
return FT_ERROR;
if (S_ISCHR(st.st_mode)) {
if ((MEM_MAJOR == major(st.st_rdev)) &&
((DEV_NULL_MINOR_NUM == minor(st.st_rdev)) ||
(DEV_ZERO_MINOR_NUM == minor(st.st_rdev))))
return FT_DEV_NULL; /* treat /dev/null + /dev/zero the same */
if (SCSI_GENERIC_MAJOR == major(st.st_rdev))
return FT_SG;
if (SCSI_TAPE_MAJOR == major(st.st_rdev))
return FT_ST;
return FT_CHAR;
} else if (S_ISBLK(st.st_mode))
return FT_BLOCK;
else if (S_ISFIFO(st.st_mode))
return FT_FIFO;
st_size = st.st_size;
return FT_OTHER;
}
/* Returns reserved_buffer_size/mmap_size if success, else 0 for failure */
static int
sg_prepare_resbuf(int fd, struct global_collection *clp, bool is_in,
uint8_t **mmpp)
{
static bool done = false;
bool no_dur = is_in ? clp->in_flags.no_dur : clp->out_flags.no_dur;
bool masync = is_in ? clp->in_flags.masync : clp->out_flags.masync;
bool wq_excl = is_in ? clp->in_flags.wq_excl : clp->out_flags.wq_excl;
bool skip_thresh = is_in ? clp->in_flags.no_thresh :
clp->out_flags.no_thresh;
int elem_sz = clp->elem_sz;
int res, t, num, err;
uint8_t *mmp;
struct sg_extended_info sei {};
struct sg_extended_info * seip = &sei;
res = ioctl(fd, SG_GET_VERSION_NUM, &t);
if ((res < 0) || (t < 40000)) {
if (ioctl(fd, SG_GET_RESERVED_SIZE, &num) < 0) {
perror("SG_GET_RESERVED_SIZE ioctl failed");
return 0;
}
if (! done) {
done = true;
pr2serr_lk("%ssg driver prior to 4.0.00, reduced functionality\n",
my_name);
}
goto bypass;
}
if (elem_sz >= 4096) {
seip->sei_rd_mask |= SG_SEIM_SGAT_ELEM_SZ;
res = ioctl(fd, SG_SET_GET_EXTENDED, seip);
if (res < 0)
pr2serr_lk("sg_mrq_dd: %s: SG_SET_GET_EXTENDED(SGAT_ELEM_SZ) rd "
"error: %s\n", __func__, strerror(errno));
if (elem_sz != (int)seip->sgat_elem_sz) {
seip->sei_wr_mask |= SG_SEIM_SGAT_ELEM_SZ;
seip->sgat_elem_sz = elem_sz;
res = ioctl(fd, SG_SET_GET_EXTENDED, seip);
if (res < 0)
pr2serr_lk("sg_mrq_dd: %s: SG_SET_GET_EXTENDED(SGAT_ELEM_SZ) "
"wr error: %s\n", __func__, strerror(errno));
}
}
if (no_dur || masync || skip_thresh) {
seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
if (no_dur) {
seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_NO_DURATION;
seip->ctl_flags |= SG_CTL_FLAGM_NO_DURATION;
}
if (masync) {
seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_MORE_ASYNC;
seip->ctl_flags |= SG_CTL_FLAGM_MORE_ASYNC;
}
if (wq_excl) {
seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_EXCL_WAITQ;
seip->ctl_flags |= SG_CTL_FLAGM_EXCL_WAITQ;
}
if (skip_thresh) {
seip->tot_fd_thresh = 0;
sei.sei_wr_mask |= SG_SEIM_TOT_FD_THRESH;
}
res = ioctl(fd, SG_SET_GET_EXTENDED, seip);
if (res < 0)
pr2serr_lk("sg_mrq_dd: %s: SG_SET_GET_EXTENDED(NO_DURATION) "
"error: %s\n", __func__, strerror(errno));
}
bypass:
num = clp->bs * clp->bpt;
res = ioctl(fd, SG_SET_RESERVED_SIZE, &num);
if (res < 0) {
perror("sg_mrq_dd: SG_SET_RESERVED_SIZE error");
return 0;
} else {
int nn;
res = ioctl(fd, SG_GET_RESERVED_SIZE, &nn);
if (res < 0) {
perror("sg_mrq_dd: SG_GET_RESERVED_SIZE error");
return 0;
}
if (nn < num) {
pr2serr_lk("%s: SG_GET_RESERVED_SIZE shows size truncated, "
"wanted %d got %d\n", __func__, num, nn);
return 0;
}
if (mmpp) {
mmp = (uint8_t *)mmap(NULL, num, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (MAP_FAILED == mmp) {
err = errno;
pr2serr_lk("sg_mrq_dd: %s: sz=%d, fd=%d, mmap() failed: %s\n",
__func__, num, fd, strerror(err));
return 0;
}
*mmpp = mmp;
}
}
t = 1;
res = ioctl(fd, SG_SET_FORCE_PACK_ID, &t);
if (res < 0)
perror("sg_mrq_dd: SG_SET_FORCE_PACK_ID error");
if (clp->unit_nanosec) {
seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_TIME_IN_NS;
seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS;
if (ioctl(fd, SG_SET_GET_EXTENDED, seip) < 0) {
res = -1;
pr2serr_lk("ioctl(EXTENDED(TIME_IN_NS)) failed, errno=%d %s\n",
errno, strerror(errno));
}
}
if (clp->verbose) {
t = 1;
/* more info in the kernel log */
res = ioctl(fd, SG_SET_DEBUG, &t);
if (res < 0)
perror("sg_mrq_dd: SG_SET_DEBUG error");
}
return (res < 0) ? 0 : num;
}
static int
sg_in_open(struct global_collection *clp, const string & inf, uint8_t **mmpp,
int * mmap_lenp)
{
int fd, err, n;
int flags = O_RDWR;
char ebuff[EBUFF_SZ];
const char * fnp = inf.c_str();
if (clp->in_flags.direct)
flags |= O_DIRECT;
if (clp->in_flags.excl)
flags |= O_EXCL;
if (clp->in_flags.dsync)
flags |= O_SYNC;
if ((fd = open(fnp, flags)) < 0) {
err = errno;
snprintf(ebuff, EBUFF_SZ, "%s: could not open %s for sg reading",
__func__, fnp);
perror(ebuff);
return -sg_convert_errno(err);
}
n = sg_prepare_resbuf(fd, clp, true, mmpp);
if (n <= 0) {
close(fd);
return -SG_LIB_FILE_ERROR;
}
if (mmap_lenp)
*mmap_lenp = n;
return fd;
}
static int
sg_out_open(struct global_collection *clp, const string & outf,
uint8_t **mmpp, int * mmap_lenp)
{
int fd, err, n;
int flags = O_RDWR;
char ebuff[EBUFF_SZ];
const char * fnp = outf.c_str();
if (clp->out_flags.direct)
flags |= O_DIRECT;
if (clp->out_flags.excl)
flags |= O_EXCL;
if (clp->out_flags.dsync)
flags |= O_SYNC;
if ((fd = open(fnp, flags)) < 0) {
err = errno;
snprintf(ebuff, EBUFF_SZ, "%s: could not open %s for sg %s",
__func__, fnp, (clp->verify ? "verifying" : "writing"));
perror(ebuff);
return -sg_convert_errno(err);
}
n = sg_prepare_resbuf(fd, clp, false, mmpp);
if (n <= 0) {
close(fd);
return -SG_LIB_FILE_ERROR;
}
if (mmap_lenp)
*mmap_lenp = n;
return fd;
}
static int
reg_file_open(struct global_collection *clp, const string & fn_s,
bool for_wr)
{
int fd, flags;
char ebuff[EBUFF_SZ];
if (for_wr) {
flags = O_WRONLY;
if (! clp->out_flags.nocreat)
flags |= O_CREAT;
if (clp->out_flags.append)
flags |= O_APPEND;
} else
flags = O_RDONLY;
if (clp->in_flags.direct)
flags |= O_DIRECT;
if (clp->in_flags.excl)
flags |= O_EXCL;
if (clp->in_flags.dsync)
flags |= O_SYNC;
if (for_wr)
fd = open(fn_s.c_str(), flags, 0666);
else
fd = open(fn_s.c_str(), flags);
if (fd < 0) {
int err = errno;
snprintf(ebuff, EBUFF_SZ, "%scould not open %s for %sing ",
my_name, fn_s.c_str(), (for_wr ? "writ" : "read"));
perror(ebuff);
return -err;
}
return fd;
}
get_next_res_t
cp_ver_pair_t::get_next(int desired_num_blks)
{
int64_t expected, desired;
if (desired_num_blks <= 0) {
if (desired_num_blks < 0) {
if (next_count_pos.load() >= 0) /* flag error detection */
next_count_pos.store(desired_num_blks);
}
return make_pair(next_count_pos.load(), 0);
}
expected = next_count_pos.load();
do { /* allowed to race with other threads */
if (expected < 0)
return make_pair(0, (int)expected);
else if (expected >= dd_count)
return make_pair(expected, 0); /* clean finish */
desired = expected + desired_num_blks;
if (desired > dd_count)
desired = dd_count;
} while (! next_count_pos.compare_exchange_strong(expected, desired));
return make_pair(expected, desired - expected);
}
/* Return of 0 -> success, see sg_ll_read_capacity*() otherwise */
static int
scsi_read_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
{
int res;
uint8_t rcBuff[RCAP16_REPLY_LEN] = {};
res = sg_ll_readcap_10(sg_fd, 0, 0, rcBuff, READ_CAP_REPLY_LEN, false, 0);
if (0 != res)
goto bad;
if ((0xff == rcBuff[0]) && (0xff == rcBuff[1]) && (0xff == rcBuff[2]) &&
(0xff == rcBuff[3])) {
res = sg_ll_readcap_16(sg_fd, 0, 0, rcBuff, RCAP16_REPLY_LEN, false,
0);
if (0 != res)
goto bad;
*num_sect = sg_get_unaligned_be64(rcBuff + 0) + 1;
*sect_sz = sg_get_unaligned_be32(rcBuff + 8);
} else {
/* take care not to sign extend values > 0x7fffffff */
*num_sect = (int64_t)sg_get_unaligned_be32(rcBuff + 0) + 1;
*sect_sz = sg_get_unaligned_be32(rcBuff + 4);
}
return 0;
bad:
*num_sect = 0;
*sect_sz = 0;
return res;
}
/* Return of 0 -> success, -1 -> failure. BLKGETSIZE64, BLKGETSIZE and */
/* BLKSSZGET macros problematic (from <linux/fs.h> or <sys/mount.h>). */
static int
read_blkdev_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
{
#ifdef BLKSSZGET
if ((ioctl(sg_fd, BLKSSZGET, sect_sz) < 0) && (*sect_sz > 0)) {
perror("BLKSSZGET ioctl error");
return -1;
} else {
#ifdef BLKGETSIZE64
uint64_t ull;
if (ioctl(sg_fd, BLKGETSIZE64, &ull) < 0) {
perror("BLKGETSIZE64 ioctl error");
return -1;
}
*num_sect = ((int64_t)ull / (int64_t)*sect_sz);
#else
unsigned long ul;
if (ioctl(sg_fd, BLKGETSIZE, &ul) < 0) {
perror("BLKGETSIZE ioctl error");
return -1;
}
*num_sect = (int64_t)ul;
#endif
}
return 0;
#else
*num_sect = 0;
*sect_sz = 0;
return -1;
#endif
}
static void
flag_all_stop(struct global_collection * clp)
{
for (auto && elem : clp->cp_ver_arr) {
if (elem.state == cp_ver_pair_t::my_state::empty)
break;
elem.next_count_pos.store(-1);
}
}
/* Has an infinite loop doing a timed wait for any signals in signal_set.
* After each timeout (300 ms) checks if the most_recent_pack_id atomic
* integer has changed. If not after another two timeouts announces a stall
* has been detected. If shutting down atomic is true breaks out of loop and
* shuts down this thread. Other than that, this thread is normally cancelled
* by the main thread, after other threads have exited. */
static void
sig_listen_thread(struct global_collection * clp)
{
bool stall_reported = false;
int prev_pack_id = 0;
int sig_number, pack_id;
uint32_t ict_ms = (clp->sdt_ict ? clp->sdt_ict : DEF_SDT_ICT_MS);
struct timespec ts;
struct timespec * tsp = &ts;
tsp->tv_sec = ict_ms / 1000;
tsp->tv_nsec = (ict_ms % 1000) * 1000 * 1000; /* DEF_SDT_ICT_MS */
listen_t_tid = gettid(); // to facilitate sending SIGUSR2 to exit
while (1) {
sig_number = sigtimedwait(&signal_set, NULL, tsp);
if (sig_number < 0) {
int err = errno;
/* EAGAIN implies a timeout */
if ((EAGAIN == err) && (clp->sdt_crt > 0)) {
pack_id = clp->most_recent_pack_id.load();
if ((pack_id > 0) && (pack_id == prev_pack_id)) {
if (! stall_reported) {
stall_reported = true;
tsp->tv_sec = clp->sdt_crt;
tsp->tv_nsec = 0;
pr2serr_lk("%s: first stall at pack_id=%d detected\n",
__func__, pack_id);
} else
pr2serr_lk("%s: subsequent stall at pack_id=%d\n",
__func__, pack_id);
// following command assumes linux bash or similar shell
system_wrapper("cat /proc/scsi/sg/debug >> /dev/stderr\n");
// system_wrapper("/usr/bin/dmesg\n");
} else
prev_pack_id = pack_id;
} else if (EAGAIN != err)
pr2serr_lk("%s: sigtimedwait() errno=%d\n", __func__, err);
}
if (SIGINT == sig_number) {
pr2serr_lk("%sinterrupted by SIGINT\n", my_name);
flag_all_stop(clp);
shutting_down.store(true);
sigprocmask(SIG_SETMASK, &orig_signal_set, NULL);
raise(SIGINT);
break;
}
if (SIGUSR2 == sig_number) {
if (clp->verbose > 2)
pr2serr_lk("%s: SIGUSR2 received\n", __func__);
break;
} if (shutting_down)
break;
} /* end of while loop */
if (clp->verbose > 3)
pr2serr_lk("%s: exiting\n", __func__);
}
static bool
sg_share_prepare(int write_side_fd, int read_side_fd, int id, bool vb_b)
{
struct sg_extended_info sei {};
struct sg_extended_info * seip = &sei;
seip->sei_wr_mask |= SG_SEIM_SHARE_FD;
seip->sei_rd_mask |= SG_SEIM_SHARE_FD;
seip->share_fd = read_side_fd;
if (ioctl(write_side_fd, SG_SET_GET_EXTENDED, seip) < 0) {
pr2serr_lk("tid=%d: ioctl(EXTENDED(shared_fd=%d), failed "
"errno=%d %s\n", id, read_side_fd, errno,
strerror(errno));
return false;
}
if (vb_b)
pr2serr_lk("%s: tid=%d: ioctl(EXTENDED(shared_fd)) ok, "
"read_side_fd=%d, write_side_fd=%d\n", __func__, id,
read_side_fd, write_side_fd);
return true;
}
static void
sg_take_snap(int sg_fd, int id, bool vb_b)
{
struct sg_extended_info sei {};
struct sg_extended_info * seip = &sei;
seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS;
seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_SNAP_DEV;
seip->ctl_flags &= ~SG_CTL_FLAGM_SNAP_DEV; /* 0 --> append */
if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
pr2serr_lk("tid=%d: ioctl(EXTENDED(SNAP_DEV), failed errno=%d %s\n",
id, errno, strerror(errno));
return;
}
if (vb_b)
pr2serr_lk("tid=%d: ioctl(SNAP_DEV) ok\n", id);
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/* Each thread's "main" function */
static void
read_write_thread(struct global_collection * clp, int thr_idx, int slice_idx,
bool singleton)
{
Rq_elem rel {};
Rq_elem * rep = &rel;
int n, sz, fd, vb, err, seg_blks;
int res = 0;
int num_sg = 0;
bool own_infd = false;
bool in_is_sg, in_mmap, out_is_sg, out_mmap;
bool own_outfd = false;
bool only_one_sg = false;
struct cp_ver_pair_t & cvp = clp->cp_ver_arr[slice_idx];
class scat_gath_iter i_sg_it(clp->i_sgl);
class scat_gath_iter o_sg_it(clp->o_sgl);
const string & inf = clp->inf_v[slice_idx];
const string & outf = clp->outf_v[slice_idx];
vector<cdb_arr_t> a_cdb;
vector<struct sg_io_v4> a_v4;
vb = clp->verbose;
sz = clp->mrq_num * clp->bpt * clp->bs;
in_is_sg = (FT_SG == clp->in_type);
in_mmap = (in_is_sg && (clp->in_flags.mmap > 0));
out_is_sg = (FT_SG == clp->out_type);
out_mmap = (out_is_sg && (clp->out_flags.mmap > 0));
rep->clp = clp;
rep->id = thr_idx;
rep->bs = clp->bs;
if (in_is_sg && out_is_sg)
rep->both_sg = true;
else if (in_is_sg || out_is_sg) {
only_one_sg = true;
if (in_is_sg)
rep->only_in_sg = true;
else
rep->only_out_sg = true;
}
if (vb > 2) {
pr2serr_lk("%d <-- Starting worker thread, slice=%d\n", thr_idx,
slice_idx);
if (vb > 3)
pr2serr_lk(" %s ---> %s\n", inf.c_str(), outf.c_str());
}
if (! (rep->both_sg || in_mmap)) {
rep->buffp = sg_memalign(sz, 0 /* page align */, &rep->alloc_bp,
false);
if (NULL == rep->buffp) {
pr2serr_lk("Failed to allocate %d bytes, exiting\n", sz);
return;
}
}
rep->infd = clp->in0fd;
rep->outfd = clp->out0fd;
rep->outregfd = clp->outregfd;
rep->rep_count = 0;
rep->in_follow_on = -1;
rep->out_follow_on = -1;
if (cvp.state == cp_ver_pair_t::my_state::init)
cvp.state = cp_ver_pair_t::my_state::underway;
if (FT_OTHER == cvp.in_type) {
fd = reg_file_open(clp, inf, false);
if (fd < 0) {
pr2serr_lk("[%d]: unable to open IFILE of slice=%d\n", thr_idx,
slice_idx);
return;
}
rep->infd = fd;
}
if (FT_OTHER == cvp.out_type) {
fd = reg_file_open(clp, outf, true);
if (fd < 0) {
pr2serr_lk("[%d]: unable to open OFILE of slice=%d\n", thr_idx,
slice_idx);
return;
}
rep->outfd = fd;
}
if (rep->infd == rep->outfd) {
if (in_is_sg)
rep->same_sg = true;
}
if (clp->in_flags.random) {
#ifdef HAVE_GETRANDOM
ssize_t ssz = getrandom(&rep->seed, sizeof(rep->seed), GRND_NONBLOCK);
if (ssz < (ssize_t)sizeof(rep->seed)) {
pr2serr_lk("[%d] %s: getrandom() failed, ret=%d\n", thr_idx,
__func__, (int)ssz);
rep->seed = (long)time(NULL);
}
#else
rep->seed = (long)time(NULL); /* use seconds since epoch as proxy */
#endif
if (vb > 1)
pr2serr_lk("[%d] %s: seed=%ld\n", thr_idx, __func__, rep->seed);
#ifdef HAVE_SRAND48_R
srand48_r(rep->seed, &rep->drand);
#else
srand48(rep->seed);
#endif
}
if (in_is_sg && inf.size()) {
if ((clp->in_flags.same_fds || (0 == thr_idx)) &&
(cvp.in_fd >= 0))
fd = cvp.in_fd;
else {
fd = sg_in_open(clp, inf, (in_mmap ? &rep->buffp : NULL),
(in_mmap ? &rep->mmap_len : NULL));
if (fd < 0)
goto fini;
own_infd = true;
if (cvp.in_fd < 0)
cvp.in_fd = fd;
}
rep->infd = fd;
rep->mmap_active = in_mmap ? clp->in_flags.mmap : 0;
if (in_mmap && (vb > 4))
pr2serr_lk("[%d] %s: mmap buffp=%p\n", thr_idx, __func__,
rep->buffp);
++num_sg;
if (vb > 2)
pr2serr_lk("[%d]: opened local sg IFILE\n", thr_idx);
}
if (out_is_sg && outf.size()) {
if ((clp->out_flags.same_fds || (0 == thr_idx)) &&
(cvp.out_fd >= 0))
fd = cvp.out_fd;
else {
fd = sg_out_open(clp, outf, (out_mmap ? &rep->buffp : NULL),
(out_mmap ? &rep->mmap_len : NULL));
if (fd < 0)
goto fini;
own_outfd = true;
if (cvp.out_fd < 0)
cvp.out_fd = fd;
}
rep->outfd = fd;
if (! rep->mmap_active)
rep->mmap_active = out_mmap ? clp->out_flags.mmap : 0;
if (out_mmap && (vb > 4))
pr2serr_lk("[%d]: mmap buffp=%p\n", thr_idx, rep->buffp);
++num_sg;
if (vb > 2)
pr2serr_lk("[%d]: opened local sg OFILE\n", thr_idx);
}
if (vb > 2) {
if (in_is_sg && (! own_infd))
pr2serr_lk("[%d]: using global sg IFILE, fd=%d\n", thr_idx,
rep->infd);
if (out_is_sg && (! own_outfd))
pr2serr_lk("[%d]: using global sg OFILE, fd=%d\n", thr_idx,
rep->outfd);
}
if (rep->both_sg)
rep->has_share = sg_share_prepare(rep->outfd, rep->infd, thr_idx,
vb > 9);
if (vb > 9)
pr2serr_lk("[%d]: has_share=%s\n", thr_idx,
(rep->has_share ? "true" : "false"));
// share_and_ofreg = (rep->has_share && (rep->outregfd >= 0));
/* vvvvvvvvvvvvvv Main segment copy loop vvvvvvvvvvvvvvvvvvvvvvv */
while (! shutting_down) {
get_next_res_t gnr = cvp.get_next(clp->mrq_num * clp->bpt);
seg_blks = gnr.second;
if (seg_blks <= 0) {
if (seg_blks < 0)
res = -seg_blks;
else
cvp.state = cp_ver_pair_t::my_state::finished;
break;
}
if (! i_sg_it.set_by_blk_idx(gnr.first)) {
lock_guard<mutex> lk(strerr_mut);
pr2serr_lk("[%d]: input set_by_blk_idx() failed\n", thr_idx);
i_sg_it.dbg_print("input after set_by_blk_idx", false, vb > 5);
res = 2;
break;
}
if (! o_sg_it.set_by_blk_idx(gnr.first)) {
pr2serr_lk("[%d]: output set_by_blk_idx() failed\n", thr_idx);
res = 3;
break;
}
if (rep->both_sg) {
uint32_t nn = (2 * clp->mrq_num) + 4;
if (a_cdb.capacity() < nn)
a_cdb.reserve(nn);
if (a_v4.capacity() < nn)
a_v4.reserve(nn);
if (clp->mrq_eq_0)
res = do_both_sg_segment_mrq0(rep, i_sg_it, o_sg_it,
seg_blks);
else
res = do_both_sg_segment(rep, i_sg_it, o_sg_it, seg_blks,
a_cdb, a_v4);
if (res < 0)
break;
} else if (only_one_sg) {
uint32_t nn = clp->mrq_num + 4;
if (a_cdb.capacity() < nn)
a_cdb.reserve(nn);
if (a_v4.capacity() < nn)
a_v4.reserve(nn);
res = do_normal_sg_segment(rep, i_sg_it, o_sg_it, seg_blks, a_cdb,
a_v4);
if (res < 0)
break;
} else {
res = do_normal_normal_segment(rep, i_sg_it, o_sg_it, seg_blks);
if (res < 0)
break;
}
if (singleton) {
{
lock_guard<mutex> lk(clp->infant_mut);
clp->processed = true;
} /* this unlocks lk */
clp->infant_cv.notify_one();
singleton = false;
}
if (rep->stop_after_write || rep->stop_now) {
shutting_down = true;
break;
}
} /* ^^^^^^^^^^ end of main while loop which copies segments ^^^^^^ */
if (shutting_down) {
if (vb > 3)
pr2serr_lk("%s: t=%d: shutting down\n", __func__, rep->id);
goto fini;
}
if (singleton) {
{
lock_guard<mutex> lk(clp->infant_mut);
clp->processed = true;
} /* this unlocks lk */
clp->infant_cv.notify_one();
}
if (res < 0) {
if (seg_blks >= 0)
cvp.get_next(-1); /* flag error to main */
pr2serr_lk("%s: t=%d: aborting, res=%d\n", __func__, rep->id, res);
}
fini:
if ((1 == rep->mmap_active) && (rep->mmap_len > 0)) {
if (munmap(rep->buffp, rep->mmap_len) < 0) {
err = errno;
char bb[64];
pr2serr_lk("thread=%d: munmap() failed: %s\n", rep->id,
tsafe_strerror(err, bb));
}
if (vb > 4)
pr2serr_lk("thread=%d: munmap(%p, %d)\n", rep->id, rep->buffp,
rep->mmap_len);
rep->mmap_active = 0;
}
if (own_infd && (rep->infd >= 0)) {
if (vb && in_is_sg) {
if (ioctl(rep->infd, SG_GET_NUM_WAITING, &n) >= 0) {
if (n > 0)
pr2serr_lk("%s: tid=%d: num_waiting=%d prior close(in)\n",
__func__, rep->id, n);
} else {
err = errno;
pr2serr_lk("%s: [%d] ioctl(SG_GET_NUM_WAITING) errno=%d: "
"%s\n", __func__, rep->id, err, strerror(err));
}
}
close(rep->infd);
}
if (own_outfd && (rep->outfd >= 0)) {
if (vb && out_is_sg) {
if (ioctl(rep->outfd, SG_GET_NUM_WAITING, &n) >= 0) {
if (n > 0)
pr2serr_lk("%s: tid=%d: num_waiting=%d prior "
"close(out)\n", __func__, rep->id, n);
} else {
err = errno;
pr2serr_lk("%s: [%d] ioctl(SG_GET_NUM_WAITING) errno=%d: "
"%s\n", __func__, rep->id, err, strerror(err));
}
}
close(rep->outfd);
}
/* pass stats back to read-side */
if (vb > 3)
pr2serr_lk("%s: [%d] leaving: in/out local count=%" PRId64 "/%"
PRId64 "\n", __func__, rep->id, rep->in_local_count,
rep->out_local_count);
cvp.in_rem_count -= rep->in_local_count;
cvp.out_rem_count -= rep->out_local_count;
cvp.in_partial += rep->in_local_partial;
cvp.out_partial += rep->out_local_partial;
cvp.sum_of_resids += rep->in_resid_bytes;
if (rep->alloc_bp)
free(rep->alloc_bp);
}
/* N.B. Returns 'blocks' is successful, lesser positive number if there was
* a short read, or an error code which is negative. */
static int
normal_in_rd(Rq_elem * rep, int64_t lba, int blocks, int d_boff)
{
struct global_collection * clp = rep->clp;
int res, err;
int id = rep->id;
uint8_t * bp;
char strerr_buff[STRERR_BUFF_LEN];
if (clp->verbose > 4)
pr2serr_lk("[%d] %s: lba=%" PRIu64 ", blocks=%d, d_boff=%d\n", id,
__func__, lba, blocks, d_boff);
if (FT_RANDOM_0_FF == clp->in_type) {
int k, j;
const int jbump = sizeof(uint32_t);
long rn;
uint8_t * bp;
if (clp->in_flags.zero && clp->in_flags.ff && (rep->bs >= 4)) {
uint32_t pos = (uint32_t)lba;
uint32_t off;
for (k = 0, off = 0; k < blocks; ++k, off += rep->bs, ++pos) {
for (j = 0; j < (rep->bs - 3); j += 4)
sg_put_unaligned_be32(pos, rep->buffp + off + j);
}
} else if (clp->in_flags.zero)
memset(rep->buffp + d_boff, 0, blocks * rep->bs);
else if (clp->in_flags.ff)
memset(rep->buffp + d_boff, 0xff, blocks * rep->bs);
else {
bp = rep->buffp + d_boff;
for (k = 0; k < blocks; ++k, bp += rep->bs) {
for (j = 0; j < rep->bs; j += jbump) {
/* mrand48 takes uniformly from [-2^31, 2^31) */
#ifdef HAVE_SRAND48_R
mrand48_r(&rep->drand, &rn);
#else
rn = mrand48();
#endif
*((uint32_t *)(bp + j)) = (uint32_t)rn;
}
}
}
return blocks;
}
if (clp->in_type != FT_FIFO) {
int64_t pos = lba * rep->bs;
if (rep->in_follow_on != pos) {
if (lseek64(rep->infd, pos, SEEK_SET) < 0) {
err = errno;
pr2serr_lk("[%d] %s: >> lseek64(%" PRId64 "): %s\n", id,
__func__, pos, safe_strerror(err));
return -err;
}
rep->in_follow_on = pos;
}
}
bp = rep->buffp + d_boff;
while (((res = read(rep->infd, bp, blocks * rep->bs)) < 0) &&
((EINTR == errno) || (EAGAIN == errno)))
std::this_thread::yield();/* another thread may be able to progress */
if (res < 0) {
err = errno;
if (clp->in_flags.coe) {
memset(bp, 0, blocks * rep->bs);
pr2serr_lk("[%d] %s : >> substituted zeros for in blk=%" PRId64
" for %d bytes, %s\n", id, __func__, lba,
blocks * rep->bs,
tsafe_strerror(err, strerr_buff));
res = blocks * rep->bs;
} else {
pr2serr_lk("[%d] %s: error in normal read, %s\n", id, __func__,
tsafe_strerror(err, strerr_buff));
return -err;
}
}
rep->in_follow_on += res;
if (res < blocks * rep->bs) {
blocks = res / rep->bs;
if ((res % rep->bs) > 0) {
rep->in_local_partial++;
rep->in_resid_bytes = res % rep->bs;
}
}
return blocks;
}
/* N.B. Returns 'blocks' is successful, lesser positive number if there was
* a short write, or an error code which is negative. */
static int
normal_out_wr(Rq_elem * rep, int64_t lba, int blocks, int d_boff)
{
int res, err;
int id = rep->id;
struct global_collection * clp = rep->clp;
uint8_t * bp = rep->buffp + d_boff;
char strerr_buff[STRERR_BUFF_LEN];
if (clp->verbose > 4)
pr2serr_lk("[%d] %s: lba=%" PRIu64 ", blocks=%d, d_boff=%d\n", id,
__func__, lba, blocks, d_boff);
if (clp->in_type != FT_FIFO) {
int64_t pos = lba * rep->bs;
if (rep->out_follow_on != pos) {
if (lseek64(rep->outfd, pos, SEEK_SET) < 0) {
err = errno;
pr2serr_lk("[%d] %s: >> lseek64(%" PRId64 "): %s\n", id,
__func__, pos, safe_strerror(err));
return -err;
}
rep->out_follow_on = pos;
}
}
while (((res = write(rep->outfd, bp, blocks * rep->bs))
< 0) && ((EINTR == errno) || (EAGAIN == errno)))
std::this_thread::yield();/* another thread may be able to progress */
if (res < 0) {
err = errno;
if (clp->out_flags.coe) {
pr2serr_lk("[%d] %s: >> ignored error for out lba=%" PRId64
" for %d bytes, %s\n", id, __func__, lba,
blocks * rep->bs, tsafe_strerror(err, strerr_buff));
res = blocks * rep->bs;
}
else {
pr2serr_lk("[%d] %s: error normal write, %s\n", id, __func__,
tsafe_strerror(err, strerr_buff));
return -err;
}
}
rep->out_follow_on += res;
if (res < blocks * rep->bs) {
blocks = res / rep->bs;
if ((res % rep->bs) > 0) {
blocks++;
rep->out_local_partial++;
}
}
return blocks;
}
static int
extra_out_wr(Rq_elem * rep, int num_bytes, int d_boff)
{
int res, err;
int id = rep->id;
struct global_collection * clp = rep->clp;
uint8_t * bp = rep->buffp + d_boff;
char strerr_buff[STRERR_BUFF_LEN];
if (clp->verbose > 4)
pr2serr_lk("[%d] %s: num_bytes=%d, d_boff=%d\n", id, __func__,
num_bytes, d_boff);
while (((res = write(clp->out0fd, bp, num_bytes))
< 0) && ((EINTR == errno) || (EAGAIN == errno)))
std::this_thread::yield();/* another thread may be able to progress */
if (res < 0) {
err = errno;
pr2serr_lk("[%d] %s: error normal write, %s\n", id, __func__,
tsafe_strerror(err, strerr_buff));
return -err;
}
if (res > 0)
rep->out_local_partial++;
return res;
}
static int
sg_build_scsi_cdb(uint8_t * cdbp, int cdb_sz, unsigned int blocks,
int64_t start_block, bool ver_true, bool write_true,
bool fua, bool dpo, int cdl)
{
bool normal_rw = true;
int rd_opcode[] = {0x8, 0x28, 0xa8, 0x88};
int ve_opcode[] = {0xff /* no VER(6) */, 0x2f, 0xaf, 0x8f};
int wr_opcode[] = {0xa, 0x2a, 0xaa, 0x8a};
int sz_ind;
memset(cdbp, 0, cdb_sz);
if (ver_true) { /* only support VERIFY(10) */
if (cdb_sz < 10) {
pr2serr_lk("%s only support VERIFY(10)\n", my_name);
return 1;
}
cdb_sz = 10;
fua = false;
cdbp[1] |= 0x2; /* BYTCHK=1 --> sending dout for comparison */
cdbp[0] = ve_opcode[1];
normal_rw = false;
}
if (dpo)
cdbp[1] |= 0x10;
if (fua)
cdbp[1] |= 0x8;
switch (cdb_sz) {
case 6:
sz_ind = 0;
cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
sg_put_unaligned_be24(0x1fffff & start_block, cdbp + 1);
cdbp[4] = (256 == blocks) ? 0 : (uint8_t)blocks;
if (blocks > 256) {
pr2serr_lk("%sfor 6 byte commands, maximum number of blocks is "
"256\n", my_name);
return 1;
}
if ((start_block + blocks - 1) & (~0x1fffff)) {
pr2serr_lk("%sfor 6 byte commands, can't address blocks beyond "
"%d\n", my_name, 0x1fffff);
return 1;
}
if (dpo || fua) {
pr2serr_lk("%sfor 6 byte commands, neither dpo nor fua bits "
"supported\n", my_name);
return 1;
}
break;
case 10:
if (! ver_true) {
sz_ind = 1;
cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
}
sg_put_unaligned_be32((uint32_t)start_block, cdbp + 2);
sg_put_unaligned_be16((uint16_t)blocks, cdbp + 7);
if (blocks & (~0xffff)) {
pr2serr_lk("%sfor 10 byte commands, maximum number of blocks is "
"%d\n", my_name, 0xffff);
return 1;
}
break;
case 12:
sz_ind = 2;
cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
sg_put_unaligned_be32((uint32_t)start_block, cdbp + 2);
sg_put_unaligned_be32((uint32_t)blocks, cdbp + 6);
break;
case 16:
sz_ind = 3;
cdbp[0] = (uint8_t)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
sg_put_unaligned_be64((uint64_t)start_block, cdbp + 2);
sg_put_unaligned_be32((uint32_t)blocks, cdbp + 10);
if (normal_rw && (cdl > 0)) {
if (cdl & 0x4)
cdbp[1] |= 0x1;
if (cdl & 0x3)
cdbp[14] |= ((cdl & 0x3) << 6);
}
break;
default:
pr2serr_lk("%sexpected cdb size of 6, 10, 12, or 16 but got %d\n",
my_name, cdb_sz);
return 1;
}
return 0;
}
static int
process_mrq_response(Rq_elem * rep, const struct sg_io_v4 * ctl_v4p,
const struct sg_io_v4 * a_v4p, int num_mrq,
uint32_t & good_inblks, uint32_t & good_outblks,
bool & last_err_on_in)
{
struct global_collection * clp = rep->clp;
bool ok, all_good;
bool sb_in_co = !!(ctl_v4p->response);
int id = rep->id;
int resid = ctl_v4p->din_resid;
int sres = ctl_v4p->spare_out;
int n_subm = num_mrq - ctl_v4p->dout_resid;
int n_cmpl = ctl_v4p->info;
int n_good = 0;
int hole_count = 0;
int cat = 0;
int vb = clp->verbose;
int k, j, f1, slen;
char b[160];
good_inblks = 0;
good_outblks = 0;
if (vb > 2)
pr2serr_lk("[thread_id=%d] %s: num_mrq=%d, n_subm=%d, n_cmpl=%d\n",
id, __func__, num_mrq, n_subm, n_cmpl);
if (n_subm < 0) {
pr2serr_lk("[%d] co.dout_resid(%d) > num_mrq(%d)\n", id,
ctl_v4p->dout_resid, num_mrq);
return -1;
}
if (n_cmpl != (num_mrq - resid))
pr2serr_lk("[%d] co.info(%d) != (num_mrq(%d) - co.din_resid(%d))\n"
"will use co.info\n", id, n_cmpl, num_mrq, resid);
if (n_cmpl > n_subm) {
pr2serr_lk("[%d] n_cmpl(%d) > n_subm(%d), use n_subm for both\n",
id, n_cmpl, n_subm);
n_cmpl = n_subm;
}
if (sres) {
pr2serr_lk("[%d] secondary error: %s [%d], info=0x%x\n", id,
strerror(sres), sres, ctl_v4p->info);
if (E2BIG == sres) {
sg_take_snap(rep->infd, id, true);
sg_take_snap(rep->outfd, id, true);
}
}
/* Check if those submitted have finished or not. N.B. If there has been
* an error then there may be "holes" (i.e. info=0x0) in the array due
* to completions being out-of-order. */
for (k = 0, j = 0; ((k < num_mrq) && (j < n_subm));
++k, j += f1, ++a_v4p) {
slen = a_v4p->response_len;
if (! (SG_INFO_MRQ_FINI & a_v4p->info))
++hole_count;
ok = true;
f1 = !!(a_v4p->info); /* want to skip n_subm count if info is 0x0 */
if (SG_INFO_CHECK & a_v4p->info) {
if ((0 == k) && (SGV4_FLAG_META_OUT_IF & ctl_v4p->flags) &&
(UINT32_MAX == a_v4p->info)) {
hole_count = 0;
n_good = num_mrq;
good_inblks = rep->a_mrq_din_blks;
good_outblks = rep->a_mrq_dout_blks;
break;
}
ok = false;
pr2serr_lk("[%d] a_v4[%d]: SG_INFO_CHECK set [%s]\n", id, k,
sg_info_str(a_v4p->info, sizeof(b), b));
}
if (sg_scsi_status_is_bad(a_v4p->device_status) ||
a_v4p->transport_status || a_v4p->driver_status) {
ok = false;
last_err_on_in = ! (a_v4p->flags & SGV4_FLAG_DO_ON_OTHER);
if (SAM_STAT_CHECK_CONDITION != a_v4p->device_status) {
pr2serr_lk("[%d] a_v4[%d]:\n", id, k);
if (vb)
lk_chk_n_print4(" >>", a_v4p, vb > 4);
}
}
if (slen > 0) {
struct sg_scsi_sense_hdr ssh;
const uint8_t *sbp = (const uint8_t *)
(sb_in_co ? ctl_v4p->response : a_v4p->response);
if (sg_scsi_normalize_sense(sbp, slen, &ssh) &&
(ssh.response_code >= 0x70)) {
if (ssh.response_code & 0x1) {
ok = true;
last_err_on_in = false;
} else
cat = sg_err_category_sense(sbp, slen);
if (SPC_SK_MISCOMPARE == ssh.sense_key)
++num_miscompare;
pr2serr_lk("[%d] a_v4[%d]:\n", id, k);
if (vb)
lk_chk_n_print4(" >>", a_v4p, vb > 4);
}
} else if (! ok)
cat = SG_LIB_CAT_OTHER;
if (ok && f1) {
++n_good;
if (a_v4p->dout_xfer_len >= (uint32_t)rep->bs)
good_outblks += (a_v4p->dout_xfer_len - a_v4p->dout_resid) /
rep->bs;
if (a_v4p->din_xfer_len >= (uint32_t)rep->bs)
good_inblks += (a_v4p->din_xfer_len - a_v4p->din_resid) /
rep->bs;
}
if (! ok) {
if ((a_v4p->dout_xfer_len > 0) || (! clp->in_flags.coe))
rep->stop_after_write = true;
}
} /* end of request array scan loop */
if ((n_subm == num_mrq) || (vb < 3))
goto fini;
pr2serr_lk("[%d] checking response array _beyond_ number of "
"submissions [%d] to num_mrq:\n", id, k);
for (all_good = true; k < num_mrq; ++k, ++a_v4p) {
if (SG_INFO_MRQ_FINI & a_v4p->info) {
pr2serr_lk("[%d] a_v4[%d]: unexpected SG_INFO_MRQ_FINI set [%s]\n",
id, k, sg_info_str(a_v4p->info, sizeof(b), b));
all_good = false;
}
if (a_v4p->device_status || a_v4p->transport_status ||
a_v4p->driver_status) {
pr2serr_lk("[%d] a_v4[%d]:\n", id, k);
lk_chk_n_print4(" ", a_v4p, vb > 4);
all_good = false;
}
}
if (all_good)
pr2serr_lk(" ... all good\n");
fini:
if (cat > 0)
clp->reason_res.store(cat);
return n_good;
}
/* Returns number of blocks successfully processed or a negative error
* number. */
static int
sg_half_segment_mrq0(Rq_elem * rep, scat_gath_iter & sg_it, bool is_wr,
int seg_blks, uint8_t *dp)
{
int k, res, fd, pack_id_base, id, rflags;
int num, kk, lin_blks, cdbsz, err;
uint32_t q_blks = 0;
struct global_collection * clp = rep->clp;
cdb_arr_t t_cdb {};
struct sg_io_v4 t_v4 {};
struct sg_io_v4 * t_v4p = &t_v4;
struct flags_t * flagsp = is_wr ? &clp->out_flags : &clp->in_flags;
int vb = clp->verbose;
id = rep->id;
pack_id_base = id * PACK_ID_TID_MULTIPLIER;
rflags = 0;
fd = is_wr ? rep->outfd : rep->infd;
if (flagsp->mmap && (rep->outregfd >= 0))
rflags |= SGV4_FLAG_MMAP_IO;
if (flagsp->dio)
rflags |= SGV4_FLAG_DIRECT_IO;
if (flagsp->qhead)
rflags |= SGV4_FLAG_Q_AT_HEAD;
if (flagsp->qtail)
rflags |= SGV4_FLAG_Q_AT_TAIL;
if (flagsp->polled)
rflags |= SGV4_FLAG_POLLED;
for (k = 0, num = 0; seg_blks > 0; ++k, seg_blks -= num) {
kk = min<int>(seg_blks, clp->bpt);
lin_blks = sg_it.linear_for_n_blks(kk);
num = lin_blks;
if (num <= 0) {
res = 0;
pr2serr_lk("[%d] %s: unexpected num=%d\n", id, __func__, num);
break;
}
/* First build the command/request for the read-side */
cdbsz = is_wr ? clp->cdbsz_out : clp->cdbsz_in;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num, sg_it.current_lba(),
false, is_wr, flagsp->fua, flagsp->dpo,
flagsp->cdl);
if (res) {
pr2serr_lk("[%d] %s: sg_build_scsi_cdb() failed\n", id, __func__);
break;
} else if (vb > 3)
lk_print_command_len("cdb: ", t_cdb.data(), cdbsz, true);
t_v4p->guard = 'Q';
t_v4p->request = (uint64_t)t_cdb.data();
t_v4p->usr_ptr = t_v4p->request;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->flags = rflags;
t_v4p->request_len = cdbsz;
if (is_wr) {
t_v4p->dout_xfer_len = num * rep->bs;
t_v4p->dout_xferp = (uint64_t)(dp + (q_blks * rep->bs));
t_v4p->din_xfer_len = 0;
} else {
t_v4p->din_xfer_len = num * rep->bs;
t_v4p->din_xferp = (uint64_t)(dp + (q_blks * rep->bs));
t_v4p->dout_xfer_len = 0;
}
t_v4p->timeout = clp->cmd_timeout;
t_v4p->request_extra = pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
mrq0_again:
res = ioctl(fd, SG_IO, t_v4p);
err = errno;
if (vb > 5)
v4hdr_out_lk("sg_half_segment_mrq0: >> after ioctl(SG_IO)",
t_v4p, id, false);
if (res < 0) {
if (E2BIG == err)
sg_take_snap(fd, id, true);
else if (EBUSY == err) {
++num_ebusy;
std::this_thread::yield();/* so other threads can progress */
goto mrq0_again;
}
pr2serr_lk("[%d] %s: ioctl(SG_IO)-->%d, errno=%d: %s\n", id,
__func__, res, err, strerror(err));
return -err;
}
if (t_v4p->device_status || t_v4p->transport_status ||
t_v4p->driver_status) {
rep->stop_now = true;
pr2serr_lk("[%d] t_v4[%d]:\n", id, k);
lk_chk_n_print4(" ", t_v4p, vb > 4);
return q_blks;
}
q_blks += num;
sg_it.add_blks(num);
}
return q_blks;
}
/* Returns number of blocks successfully processed or a negative error
* number. */
static int
sg_half_segment(Rq_elem * rep, scat_gath_iter & sg_it, bool is_wr,
int seg_blks, uint8_t *dp, vector<cdb_arr_t> & a_cdb,
vector<struct sg_io_v4> & a_v4)
{
int num_mrq, k, res, fd, mrq_pack_id_base, id, b_len, rflags;
int num, kk, lin_blks, cdbsz, num_good, err;
int o_seg_blks = seg_blks;
uint32_t in_fin_blks, out_fin_blks;
uint32_t mrq_q_blks = 0;
uint32_t in_mrq_q_blks = 0;
uint32_t out_mrq_q_blks = 0;
const int max_cdb_sz = MAX_SCSI_CDB_SZ;
struct sg_io_v4 * a_v4p;
struct sg_io_v4 ctl_v4 {}; /* MRQ control object */
struct global_collection * clp = rep->clp;
const char * iosub_str = "SG_IOSUBMIT(variable blocking)";
char b[80];
cdb_arr_t t_cdb {};
struct sg_io_v4 t_v4 {};
struct sg_io_v4 * t_v4p = &t_v4;
struct flags_t * flagsp = is_wr ? &clp->out_flags : &clp->in_flags;
bool serial = flagsp->serial;
bool err_on_in = false;
int vb = clp->verbose;
id = rep->id;
b_len = sizeof(b);
if (serial)
iosub_str = "SG_IO(ordered blocking)";
a_cdb.clear();
a_v4.clear();
rep->a_mrq_din_blks = 0;
rep->a_mrq_dout_blks = 0;
mrq_pack_id_base = id * PACK_ID_TID_MULTIPLIER;
rflags = 0;
if (flagsp->mmap && (rep->outregfd >= 0))
rflags |= SGV4_FLAG_MMAP_IO;
if (flagsp->dio)
rflags |= SGV4_FLAG_DIRECT_IO;
if (flagsp->qhead)
rflags |= SGV4_FLAG_Q_AT_HEAD;
if (flagsp->qtail)
rflags |= SGV4_FLAG_Q_AT_TAIL;
if (flagsp->polled)
rflags |= SGV4_FLAG_POLLED;
for (k = 0, num = 0; seg_blks > 0; ++k, seg_blks -= num) {
kk = min<int>(seg_blks, clp->bpt);
lin_blks = sg_it.linear_for_n_blks(kk);
num = lin_blks;
if (num <= 0) {
res = 0;
pr2serr_lk("[%d] %s: unexpected num=%d\n", id, __func__, num);
break;
}
/* First build the command/request for the read-side */
cdbsz = is_wr ? clp->cdbsz_out : clp->cdbsz_in;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num, sg_it.current_lba(),
false, is_wr, flagsp->fua, flagsp->dpo,
flagsp->cdl);
if (res) {
pr2serr_lk("[%d] %s: sg_build_scsi_cdb() failed\n", id, __func__);
break;
} else if (vb > 3)
lk_print_command_len("cdb: ", t_cdb.data(), cdbsz, true);
a_cdb.push_back(t_cdb);
t_v4p->guard = 'Q';
t_v4p->flags = rflags;
t_v4p->request_len = cdbsz;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->flags = rflags;
t_v4p->usr_ptr = (uint64_t)&a_cdb[a_cdb.size() - 1];
if (is_wr) {
rep->a_mrq_dout_blks += num;
t_v4p->dout_xfer_len = num * rep->bs;
t_v4p->dout_xferp = (uint64_t)(dp + (mrq_q_blks * rep->bs));
t_v4p->din_xfer_len = 0;
} else {
rep->a_mrq_din_blks += num;
t_v4p->din_xfer_len = num * rep->bs;
t_v4p->din_xferp = (uint64_t)(dp + (mrq_q_blks * rep->bs));
t_v4p->dout_xfer_len = 0;
}
t_v4p->timeout = clp->cmd_timeout;
mrq_q_blks += num;
t_v4p->request_extra = mrq_pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
a_v4.push_back(t_v4);
sg_it.add_blks(num);
}
if (rep->only_in_sg)
fd = rep->infd;
else if (rep->only_out_sg)
fd = rep->outfd;
else {
pr2serr_lk("[%d] %s: why am I here? No sg devices\n", id, __func__);
return -EINVAL;
}
num_mrq = a_v4.size();
a_v4p = a_v4.data();
res = 0;
ctl_v4.guard = 'Q';
ctl_v4.request_len = a_cdb.size() * max_cdb_sz;
ctl_v4.request = (uint64_t)a_cdb.data();
ctl_v4.max_response_len = sizeof(rep->sb);
ctl_v4.response = (uint64_t)rep->sb;
ctl_v4.flags = SGV4_FLAG_MULTIPLE_REQS;
if (! flagsp->coe)
ctl_v4.flags |= SGV4_FLAG_STOP_IF;
if (clp->mrq_polled)
ctl_v4.flags |= SGV4_FLAG_POLLED;
if (clp->in_flags.mout_if || clp->out_flags.mout_if) {
ctl_v4.flags |= SGV4_FLAG_META_OUT_IF;
if (num_mrq > 0)
a_v4[0].info = UINT32_MAX;
}
ctl_v4.dout_xferp = (uint64_t)a_v4.data(); /* request array */
ctl_v4.dout_xfer_len = a_v4.size() * sizeof(struct sg_io_v4);
ctl_v4.din_xferp = (uint64_t)a_v4.data(); /* response array */
ctl_v4.din_xfer_len = a_v4.size() * sizeof(struct sg_io_v4);
if (false /* allow_mrq_abort */) {
ctl_v4.request_extra = mrq_pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(ctl_v4.request_extra);
}
if (vb && vb_first_time.load()) {
pr2serr_lk("First controlling object output by ioctl(%s), flags: "
"%s\n", iosub_str, sg_flags_str(ctl_v4.flags, b_len, b));
vb_first_time.store(false);
} else if (vb > 4) {
pr2serr_lk("[%d] %s: >> Control object _before_ ioctl(%s):\n", id,
__func__, iosub_str);
}
if (vb > 4) {
if (vb > 5)
hex2stderr_lk((const uint8_t *)&ctl_v4, sizeof(ctl_v4), 1);
v4hdr_out_lk(">> Control object before", &ctl_v4, id, false);
}
try_again:
if (!after1 && (vb > 1)) {
after1 = true;
pr2serr_lk("%s: %s\n", __func__, serial ? mrq_ob_s : mrq_vb_s);
}
if (serial)
res = ioctl(fd, SG_IO, &ctl_v4);
else
res = ioctl(fd, SG_IOSUBMIT, &ctl_v4); /* overlapping commands */
if (res < 0) {
err = errno;
if (E2BIG == err)
sg_take_snap(fd, id, true);
else if (EBUSY == err) {
++num_ebusy;
std::this_thread::yield();/* allow another thread to progress */
goto try_again;
}
pr2serr_lk("[%d] %s: ioctl(%s, %s)-->%d, errno=%d: %s\n", id,
__func__, iosub_str, sg_flags_str(ctl_v4.flags, b_len, b),
res, err, strerror(err));
return -err;
}
if (vb > 4) {
pr2serr_lk("%s: >> Control object after ioctl(%s) seg_blks=%d:\n",
__func__, iosub_str, o_seg_blks);
if (vb > 5)
hex2stderr_lk((const uint8_t *)&ctl_v4, sizeof(ctl_v4), 1);
v4hdr_out_lk(">> Control object after", &ctl_v4, id, false);
if (vb > 5) {
for (k = 0; k < num_mrq; ++k) {
if ((vb > 6) || a_v4p[k].info) {
snprintf(b, b_len, "a_v4[%d/%d]", k, num_mrq);
v4hdr_out_lk(b, (a_v4p + k), id, true);
}
}
}
}
num_good = process_mrq_response(rep, &ctl_v4, a_v4p, num_mrq, in_fin_blks,
out_fin_blks, err_on_in);
if (is_wr)
out_mrq_q_blks = mrq_q_blks;
else
in_mrq_q_blks = mrq_q_blks;
if (vb > 2)
pr2serr_lk("%s: >>> seg_blks=%d, num_good=%d, in_q/fin blks=%u/%u; "
"out_q/fin blks=%u/%u\n", __func__, o_seg_blks, num_good,
in_mrq_q_blks, in_fin_blks, out_mrq_q_blks, out_fin_blks);
if (clp->ese) {
int sres = ctl_v4.spare_out;
if (sres != 0) {
clp->reason_res.store(sg_convert_errno(sres));
pr2serr_lk("Exit due to secondary error [%d]\n", sres);
return -sres;
}
}
if (num_good < 0)
return -ENODATA;
else {
if (num_good < num_mrq) {
int resid_blks = in_mrq_q_blks - in_fin_blks;
if (resid_blks > 0) {
rep->in_rem_count += resid_blks;
rep->stop_after_write = ! (err_on_in && clp->in_flags.coe);
}
resid_blks = out_mrq_q_blks - out_fin_blks;
if (resid_blks > 0) {
rep->out_rem_count += resid_blks;
rep->stop_after_write = ! (! err_on_in && clp->out_flags.coe);
}
}
}
return is_wr ? out_fin_blks : in_fin_blks;
}
/* Returns number of blocks successfully processed or a negative error
* number. */
static int
do_normal_normal_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks)
{
int k, kk, res, id, num, d_off;
int o_seg_blks = seg_blks;
uint32_t in_fin_blks = 0;
uint32_t out_fin_blks = 0;
struct global_collection * clp = rep->clp;
id = rep->id;
d_off = 0;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num, d_off += num) {
kk = min<int>(seg_blks, clp->bpt);
num = i_sg_it.linear_for_n_blks(kk);
res = normal_in_rd(rep, i_sg_it.current_lba(), num,
d_off * rep->bs);
if (res < 0) {
pr2serr_lk("[%d] %s: normal in failed d_off=%d, err=%d\n",
id, __func__, d_off, -res);
break;
}
i_sg_it.add_blks(res);
if (res < num) {
d_off += res;
rep->stop_after_write = true;
break;
}
}
seg_blks = d_off;
in_fin_blks = seg_blks;
if (FT_DEV_NULL == clp->out_type)
goto fini;
d_off = 0;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num, d_off += num) {
kk = min<int>(seg_blks, clp->bpt);
num = o_sg_it.linear_for_n_blks(kk);
res = normal_out_wr(rep, o_sg_it.current_lba(), num,
d_off * rep->bs);
if (res < num) {
if (res < 0) {
pr2serr_lk("[%d] %s: normal out failed d_off=%d, err=%d\n",
id, __func__, d_off, -res);
break;
}
}
o_sg_it.add_blks(res);
if (res < num) {
d_off += res;
rep->stop_after_write = true;
break;
}
}
if (rep->in_resid_bytes > 0) {
res = extra_out_wr(rep, rep->in_resid_bytes, d_off * rep->bs);
if (res < 0)
pr2serr_lk("[%d] %s: extr out failed d_off=%d, err=%d\n", id,
__func__, d_off, -res);
rep->in_resid_bytes = 0;
}
seg_blks = d_off;
out_fin_blks = seg_blks;
fini:
rep->in_local_count += in_fin_blks;
rep->out_local_count += out_fin_blks;
if ((in_fin_blks + out_fin_blks) < (uint32_t)o_seg_blks) {
int resid_blks = o_seg_blks - in_fin_blks;
if (resid_blks > 0)
rep->in_rem_count += resid_blks;
resid_blks = o_seg_blks - out_fin_blks;
if (resid_blks > 0)
rep->out_rem_count += resid_blks;
}
return res < 0 ? res : (min<int>(in_fin_blks, out_fin_blks));
}
/* Returns number of blocks successfully processed or a negative error
* number. */
static int
do_normal_sg_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks,
vector<cdb_arr_t> & a_cdb,
vector<struct sg_io_v4> & a_v4)
{
bool in_is_normal = ! rep->only_in_sg;
int k, kk, res, id, num, d_off;
int o_seg_blks = seg_blks;
uint32_t in_fin_blks = 0;
uint32_t out_fin_blks = 0;
struct global_collection * clp = rep->clp;
id = rep->id;
a_cdb.clear();
a_v4.clear();
if (in_is_normal) { /* in: normal --> out : sg */
d_off = 0;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num, d_off += num) {
kk = min<int>(seg_blks, clp->bpt);
num = i_sg_it.linear_for_n_blks(kk);
res = normal_in_rd(rep, i_sg_it.current_lba(), num,
d_off * rep->bs);
if (res < 0) {
pr2serr_lk("[%d] %s: normal in failed d_off=%d, err=%d\n",
id, __func__, d_off, -res);
break;
}
i_sg_it.add_blks(res);
if (res < num) {
d_off += res;
rep->stop_after_write = true;
break;
}
}
seg_blks = d_off;
in_fin_blks = seg_blks;
if (rep->in_resid_bytes > 0) {
++seg_blks;
rep->in_resid_bytes = 0;
}
if (clp->mrq_eq_0)
res = sg_half_segment_mrq0(rep, o_sg_it, true /* is_wr */,
seg_blks, rep->buffp);
else
res = sg_half_segment(rep, o_sg_it, true /* is_wr */, seg_blks,
rep->buffp, a_cdb, a_v4);
if (res < seg_blks) {
if (res < 0) {
pr2serr_lk("[%d] %s: sg out failed d_off=%d, err=%d\n",
id, __func__, d_off, -res);
goto fini;
}
rep->stop_after_write = true;
}
seg_blks = res;
out_fin_blks = seg_blks;
} else { /* in: sg --> out: normal */
if (clp->mrq_eq_0)
res = sg_half_segment_mrq0(rep, i_sg_it, false, seg_blks,
rep->buffp);
else
res = sg_half_segment(rep, i_sg_it, false, seg_blks, rep->buffp,
a_cdb, a_v4);
if (res < seg_blks) {
if (res < 0) {
pr2serr_lk("[%d] %s: sg in failed, err=%d\n", id, __func__,
-res);
goto fini;
}
rep->stop_after_write = true;
}
seg_blks = res;
in_fin_blks = seg_blks;
if (FT_DEV_NULL == clp->out_type) {
out_fin_blks = seg_blks;/* so finish logic doesn't suspect ... */
goto bypass;
}
d_off = 0;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num, d_off += num) {
kk = min<int>(seg_blks, clp->bpt);
num = o_sg_it.linear_for_n_blks(kk);
res = normal_out_wr(rep, o_sg_it.current_lba(), num,
d_off * rep->bs);
if (res < num) {
if (res < 0) {
pr2serr_lk("[%d] %s: normal out failed d_off=%d, err=%d\n",
id, __func__, d_off, -res);
break;
}
}
o_sg_it.add_blks(res);
if (res < num) {
d_off += res;
rep->stop_after_write = true;
break;
}
}
seg_blks = d_off;
out_fin_blks = seg_blks;
}
bypass:
rep->in_local_count += in_fin_blks;
rep->out_local_count += out_fin_blks;
if ((in_fin_blks + out_fin_blks) < (uint32_t)o_seg_blks) {
int resid_blks = o_seg_blks - in_fin_blks;
if (resid_blks > 0)
rep->in_rem_count += resid_blks;
resid_blks = o_seg_blks - out_fin_blks;
if (resid_blks > 0)
rep->out_rem_count += resid_blks;
}
fini:
return res < 0 ? res : (min<int>(in_fin_blks, out_fin_blks));
}
/* This function sets up a multiple request (mrq) transaction and sends it
* to the pass-through. Returns number of blocks processed (==seg_blks for
* all good) or a negative error number. */
static int
do_both_sg_segment_mrq0(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks)
{
int k, kk, res, pack_id_base, id, iflags, oflags;
int num, i_lin_blks, o_lin_blks, cdbsz, err;
uint32_t in_fin_blks = 0;
uint32_t out_fin_blks = 0;
struct global_collection * clp = rep->clp;
int vb = clp->verbose;
cdb_arr_t t_cdb {};
struct sg_io_v4 t_v4 {};
struct sg_io_v4 * t_v4p = &t_v4;
struct flags_t * iflagsp = &clp->in_flags;
struct flags_t * oflagsp = &clp->out_flags;
const char * const a_ioctl_s = "do_both_sg_segment_mrq0: after "
"ioctl(SG_IO)";
id = rep->id;
pack_id_base = id * PACK_ID_TID_MULTIPLIER;
iflags = SGV4_FLAG_SHARE;
if (iflagsp->mmap && (rep->outregfd >= 0))
iflags |= SGV4_FLAG_MMAP_IO;
else
iflags |= SGV4_FLAG_NO_DXFER;
if (iflagsp->dio)
iflags |= SGV4_FLAG_DIRECT_IO;
if (iflagsp->qhead)
iflags |= SGV4_FLAG_Q_AT_HEAD;
if (iflagsp->qtail)
iflags |= SGV4_FLAG_Q_AT_TAIL;
if (iflagsp->polled)
iflags |= SGV4_FLAG_POLLED;
oflags = SGV4_FLAG_SHARE | SGV4_FLAG_NO_DXFER;
if (oflagsp->dio)
oflags |= SGV4_FLAG_DIRECT_IO;
if (oflagsp->qhead)
oflags |= SGV4_FLAG_Q_AT_HEAD;
if (oflagsp->qtail)
oflags |= SGV4_FLAG_Q_AT_TAIL;
if (oflagsp->polled)
oflags |= SGV4_FLAG_POLLED;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num) {
kk = min<int>(seg_blks, clp->bpt);
i_lin_blks = i_sg_it.linear_for_n_blks(kk);
o_lin_blks = o_sg_it.linear_for_n_blks(kk);
num = min<int>(i_lin_blks, o_lin_blks);
if (num <= 0) {
res = 0;
pr2serr_lk("[%d] %s: min(i_lin_blks=%d o_lin_blks=%d) < 1\n", id,
__func__, i_lin_blks, o_lin_blks);
break;
}
/* First build the command/request for the read-side*/
cdbsz = clp->cdbsz_in;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num,
i_sg_it.current_lba(), false, false,
iflagsp->fua, iflagsp->dpo, iflagsp->cdl);
if (res) {
pr2serr_lk("%s: t=%d: input sg_build_scsi_cdb() failed\n",
__func__, id);
break;
} else if (vb > 3)
lk_print_command_len("input cdb: ", t_cdb.data(), cdbsz, true);
t_v4p->guard = 'Q';
t_v4p->request = (uint64_t)t_cdb.data();
t_v4p->usr_ptr = t_v4p->request;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->flags = iflags;
t_v4p->request_len = cdbsz;
t_v4p->din_xfer_len = num * rep->bs;
t_v4p->dout_xfer_len = 0;
t_v4p->timeout = clp->cmd_timeout;
t_v4p->request_extra = pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
mrq0_again:
res = ioctl(rep->infd, SG_IO, t_v4p);
err = errno;
if (vb > 5)
v4hdr_out_lk(a_ioctl_s, t_v4p, id, false);
if (res < 0) {
if (E2BIG == err)
sg_take_snap(rep->infd, id, true);
else if (EBUSY == err) {
++num_ebusy;
std::this_thread::yield();/* so other threads can progress */
goto mrq0_again;
}
pr2serr_lk("[%d] %s: ioctl(SG_IO, read-side)-->%d, errno=%d: "
"%s\n", id, __func__, res, err, strerror(err));
return -err;
}
if (t_v4p->device_status || t_v4p->transport_status ||
t_v4p->driver_status) {
rep->stop_now = true;
pr2serr_lk("[%d] t_v4[%d]:\n", id, k);
lk_chk_n_print4(" ", t_v4p, vb > 4);
return min<int>(in_fin_blks, out_fin_blks);
}
rep->in_local_count += num;
in_fin_blks += num;
/* Now build the command/request for write-side (WRITE or VERIFY) */
cdbsz = clp->cdbsz_out;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num,
o_sg_it.current_lba(), clp->verify, true,
oflagsp->fua, oflagsp->dpo, oflagsp->cdl);
if (res) {
pr2serr_lk("%s: t=%d: output sg_build_scsi_cdb() failed\n",
__func__, id);
break;
} else if (vb > 3)
lk_print_command_len("output cdb: ", t_cdb.data(), cdbsz, true);
t_v4p->guard = 'Q';
t_v4p->request = (uint64_t)t_cdb.data();
t_v4p->usr_ptr = t_v4p->request;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->flags = oflags;
t_v4p->request_len = cdbsz;
t_v4p->din_xfer_len = 0;
t_v4p->dout_xfer_len = num * rep->bs;
t_v4p->timeout = clp->cmd_timeout;
t_v4p->request_extra = pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
mrq0_again2:
res = ioctl(rep->outfd, SG_IO, t_v4p);
err = errno;
if (vb > 5)
v4hdr_out_lk(a_ioctl_s, t_v4p, id, false);
if (res < 0) {
if (E2BIG == err)
sg_take_snap(rep->outfd, id, true);
else if (EBUSY == err) {
++num_ebusy;
std::this_thread::yield();/* so other threads can progress */
goto mrq0_again2;
}
pr2serr_lk("[%d] %s: ioctl(SG_IO, write-side)-->%d, errno=%d: "
"%s\n", id, __func__, res, err, strerror(err));
return -err;
}
if (t_v4p->device_status || t_v4p->transport_status ||
t_v4p->driver_status) {
rep->stop_now = true;
pr2serr_lk("[%d] t_v4[%d]:\n", id, k);
lk_chk_n_print4(" ", t_v4p, vb > 4);
return min<int>(in_fin_blks, out_fin_blks);
}
rep->out_local_count += num;
out_fin_blks += num;
i_sg_it.add_blks(num);
o_sg_it.add_blks(num);
}
return min<int>(in_fin_blks, out_fin_blks);
}
/* This function sets up a multiple request (mrq) transaction and sends it
* to the pass-through. Returns number of blocks processed (==seg_blks for
* all good) or a negative error number. */
static int
do_both_sg_segment(Rq_elem * rep, scat_gath_iter & i_sg_it,
scat_gath_iter & o_sg_it, int seg_blks,
vector<cdb_arr_t> & a_cdb,
vector<struct sg_io_v4> & a_v4)
{
bool err_on_in = false;
int num_mrq, k, res, fd, mrq_pack_id_base, id, b_len, iflags, oflags;
int num, kk, i_lin_blks, o_lin_blks, cdbsz, num_good, err;
int o_seg_blks = seg_blks;
uint32_t in_fin_blks = 0;
uint32_t out_fin_blks = 0;;
uint32_t in_mrq_q_blks = 0;
uint32_t out_mrq_q_blks = 0;
const int max_cdb_sz = MAX_SCSI_CDB_SZ;
struct sg_io_v4 * a_v4p;
struct sg_io_v4 ctl_v4 {}; /* MRQ control object */
struct global_collection * clp = rep->clp;
const char * iosub_str = "SG_IOSUBMIT(svb)";
char b[80];
cdb_arr_t t_cdb {};
struct sg_io_v4 t_v4 {};
struct sg_io_v4 * t_v4p = &t_v4;
struct flags_t * iflagsp = &clp->in_flags;
struct flags_t * oflagsp = &clp->out_flags;
int vb = clp->verbose;
id = rep->id;
b_len = sizeof(b);
a_cdb.clear();
a_v4.clear();
rep->a_mrq_din_blks = 0;
rep->a_mrq_dout_blks = 0;
mrq_pack_id_base = id * PACK_ID_TID_MULTIPLIER;
iflags = SGV4_FLAG_SHARE;
if (iflagsp->mmap && (rep->outregfd >= 0))
iflags |= SGV4_FLAG_MMAP_IO;
else
iflags |= SGV4_FLAG_NO_DXFER;
if (iflagsp->dio)
iflags |= SGV4_FLAG_DIRECT_IO;
if (iflagsp->qhead)
iflags |= SGV4_FLAG_Q_AT_HEAD;
if (iflagsp->qtail)
iflags |= SGV4_FLAG_Q_AT_TAIL;
if (iflagsp->polled)
iflags |= SGV4_FLAG_POLLED;
oflags = SGV4_FLAG_SHARE | SGV4_FLAG_NO_DXFER;
if (oflagsp->dio)
oflags |= SGV4_FLAG_DIRECT_IO;
if (oflagsp->qhead)
oflags |= SGV4_FLAG_Q_AT_HEAD;
if (oflagsp->qtail)
oflags |= SGV4_FLAG_Q_AT_TAIL;
if (oflagsp->polled)
oflags |= SGV4_FLAG_POLLED;
oflags |= SGV4_FLAG_DO_ON_OTHER;
for (k = 0; seg_blks > 0; ++k, seg_blks -= num) {
kk = min<int>(seg_blks, clp->bpt);
i_lin_blks = i_sg_it.linear_for_n_blks(kk);
o_lin_blks = o_sg_it.linear_for_n_blks(kk);
num = min<int>(i_lin_blks, o_lin_blks);
if (num <= 0) {
res = 0;
pr2serr_lk("[%d] %s: min(i_lin_blks=%d o_lin_blks=%d) < 1\n", id,
__func__, i_lin_blks, o_lin_blks);
break;
}
/* First build the command/request for the read-side*/
cdbsz = clp->cdbsz_in;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num,
i_sg_it.current_lba(), false, false,
iflagsp->fua, iflagsp->dpo, iflagsp->cdl);
if (res) {
pr2serr_lk("%s: t=%d: input sg_build_scsi_cdb() failed\n",
__func__, id);
break;
} else if (vb > 3)
lk_print_command_len("input cdb: ", t_cdb.data(), cdbsz, true);
a_cdb.push_back(t_cdb);
t_v4p->guard = 'Q';
t_v4p->flags = iflags;
t_v4p->request_len = cdbsz;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->usr_ptr = (uint64_t)&a_cdb[a_cdb.size() - 1];
t_v4p->din_xfer_len = num * rep->bs;
rep->a_mrq_din_blks += num;
t_v4p->dout_xfer_len = 0;
t_v4p->timeout = clp->cmd_timeout;
in_mrq_q_blks += num;
t_v4p->request_extra = mrq_pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
a_v4.push_back(t_v4);
/* Now build the command/request for write-side (WRITE or VERIFY) */
cdbsz = clp->cdbsz_out;
res = sg_build_scsi_cdb(t_cdb.data(), cdbsz, num,
o_sg_it.current_lba(), clp->verify, true,
oflagsp->fua, oflagsp->dpo, oflagsp->cdl);
if (res) {
pr2serr_lk("%s: t=%d: output sg_build_scsi_cdb() failed\n",
__func__, id);
break;
} else if (vb > 3)
lk_print_command_len("output cdb: ", t_cdb.data(), cdbsz, true);
a_cdb.push_back(t_cdb);
t_v4p->guard = 'Q';
t_v4p->flags = oflags;
t_v4p->request_len = cdbsz;
t_v4p->response = (uint64_t)rep->sb;
t_v4p->max_response_len = sizeof(rep->sb);
t_v4p->usr_ptr = (uint64_t)&a_cdb[a_cdb.size() - 1];
t_v4p->din_xfer_len = 0;
t_v4p->dout_xfer_len = num * rep->bs;
rep->a_mrq_dout_blks += num;
t_v4p->timeout = clp->cmd_timeout;
out_mrq_q_blks += num;
t_v4p->request_extra = mrq_pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(t_v4p->request_extra);
a_v4.push_back(t_v4);
i_sg_it.add_blks(num);
o_sg_it.add_blks(num);
}
if (vb > 6) {
pr2serr_lk("%s: t=%d: a_v4 array contents:\n", __func__, id);
hex2stderr_lk((const uint8_t *)a_v4.data(),
a_v4.size() * sizeof(struct sg_io_v4), 1);
}
if (rep->both_sg || rep->same_sg)
fd = rep->infd; /* assume share to rep->outfd */
else {
pr2serr_lk("[%d] %s: why am I here? Want 2 sg devices\n", id,
__func__);
res = -1;
goto fini;
}
num_mrq = a_v4.size();
a_v4p = a_v4.data();
res = 0;
ctl_v4.guard = 'Q';
ctl_v4.request_len = a_cdb.size() * max_cdb_sz;
ctl_v4.request = (uint64_t)a_cdb.data();
ctl_v4.max_response_len = sizeof(rep->sb);
ctl_v4.response = (uint64_t)rep->sb;
ctl_v4.flags = SGV4_FLAG_MULTIPLE_REQS | SGV4_FLAG_SHARE;
if (! (iflagsp->coe || oflagsp->coe))
ctl_v4.flags |= SGV4_FLAG_STOP_IF;
if ((! clp->verify) && clp->out_flags.order_wr)
ctl_v4.flags |= SGV4_FLAG_ORDERED_WR;
if (clp->mrq_polled)
ctl_v4.flags |= SGV4_FLAG_POLLED;
if (clp->in_flags.mout_if || clp->out_flags.mout_if) {
ctl_v4.flags |= SGV4_FLAG_META_OUT_IF;
if (num_mrq > 0)
a_v4[0].info = UINT32_MAX;
}
ctl_v4.dout_xferp = (uint64_t)a_v4.data(); /* request array */
ctl_v4.dout_xfer_len = a_v4.size() * sizeof(struct sg_io_v4);
ctl_v4.din_xferp = (uint64_t)a_v4.data(); /* response array */
ctl_v4.din_xfer_len = a_v4.size() * sizeof(struct sg_io_v4);
if (false /* allow_mrq_abort */) {
ctl_v4.request_extra = mrq_pack_id_base + ++rep->mrq_pack_id_off;
clp->most_recent_pack_id.store(ctl_v4.request_extra);
}
if (vb && vb_first_time.load()) {
pr2serr_lk("First controlling object output by ioctl(%s), flags: "
"%s\n", iosub_str, sg_flags_str(ctl_v4.flags, b_len, b));
vb_first_time.store(false);
} else if (vb > 4)
pr2serr_lk("%s: >> Control object _before_ ioctl(%s):\n", __func__,
iosub_str);
if (vb > 4) {
if (vb > 5)
hex2stderr_lk((const uint8_t *)&ctl_v4, sizeof(ctl_v4), 1);
v4hdr_out_lk(">> Control object before", &ctl_v4, id, false);
}
try_again:
if (!after1 && (vb > 1)) {
after1 = true;
pr2serr_lk("%s: %s\n", __func__, mrq_svb_s);
}
res = ioctl(fd, SG_IOSUBMIT, &ctl_v4);
if (res < 0) {
err = errno;
if (E2BIG == err)
sg_take_snap(fd, id, true);
else if (EBUSY == err) {
++num_ebusy;
std::this_thread::yield();/* allow another thread to progress */
goto try_again;
}
pr2serr_lk("%s: ioctl(%s, %s)-->%d, errno=%d: %s\n", __func__,
iosub_str, sg_flags_str(ctl_v4.flags, b_len, b), res, err,
strerror(err));
res = -err;
goto fini;
}
if (vb > 4) {
pr2serr_lk("%s: >> Control object after ioctl(%s) seg_blks=%d:\n",
__func__, iosub_str, o_seg_blks);
if (vb > 5)
hex2stderr_lk((const uint8_t *)&ctl_v4, sizeof(ctl_v4), 1);
v4hdr_out_lk(">> Control object after", &ctl_v4, id, false);
if (vb > 5) {
for (k = 0; k < num_mrq; ++k) {
if ((vb > 6) || a_v4p[k].info) {
snprintf(b, b_len, "a_v4[%d/%d]", k, num_mrq);
v4hdr_out_lk(b, (a_v4p + k), id, true);
}
}
}
}
num_good = process_mrq_response(rep, &ctl_v4, a_v4p, num_mrq, in_fin_blks,
out_fin_blks, err_on_in);
if (vb > 2)
pr2serr_lk("%s: >>> seg_blks=%d, num_good=%d, in_q/fin blks=%u/%u; "
"out_q/fin blks=%u/%u\n", __func__, o_seg_blks, num_good,
in_mrq_q_blks, in_fin_blks, out_mrq_q_blks, out_fin_blks);
if (clp->ese) {
int sres = ctl_v4.spare_out;
if (sres != 0) {
clp->reason_res.store(sg_convert_errno(sres));
pr2serr_lk("Exit due to secondary error [%d]\n", sres);
return -sres;
}
}
if (num_good < 0)
res = -ENODATA;
else {
rep->in_local_count += in_fin_blks;
rep->out_local_count += out_fin_blks;
if (num_good < num_mrq) { /* reduced number completed */
int resid_blks = in_mrq_q_blks - in_fin_blks;
if (resid_blks > 0) {
rep->in_rem_count += resid_blks;
rep->stop_after_write = ! (err_on_in && clp->in_flags.coe);
}
resid_blks = out_mrq_q_blks - out_fin_blks;
if (resid_blks > 0) {
rep->out_rem_count += resid_blks;
rep->stop_after_write = ! ((! err_on_in) &&
clp->out_flags.coe);
}
}
}
fini:
return res < 0 ? res : (min<int>(in_fin_blks, out_fin_blks));
}
#if 0
/* Returns number found and (partially) processed. 'num' is the number of
* completions to wait for when > 0. When 'num' is zero check all inflight
* request on 'fd' and return quickly if none completed (i.e. don't wait)
* If error return negative errno and if no request inflight or waiting
* then return -9999 . */
static int
sg_blk_poll(int fd, int num)
{
int res;
struct sg_extended_info sei {};
struct sg_extended_info * seip = &sei;
seip->sei_rd_mask |= SG_SEIM_BLK_POLL;
seip->sei_wr_mask |= SG_SEIM_BLK_POLL;
seip->num = (num < 0) ? 0 : num;
res = ioctl(fd, SG_SET_GET_EXTENDED, seip);
if (res < 0) {
pr2serr_lk("%s: SG_SET_GET_EXTENDED(BLK_POLL) error: %s\n",
__func__, strerror(errno));
return res;
}
return (seip->num == -1) ? -9999 : seip->num;
}
#endif
/* Returns the number of times 'ch' is found in string 's' given the
* string's length. */
static int
num_chs_in_str(const char * s, int slen, int ch)
{
int res = 0;
while (--slen >= 0) {
if (ch == s[slen])
++res;
}
return res;
}
/* Returns the number of times either 'ch1' or 'ch2' is found in
* string 's' given the string's length. */
int
num_either_ch_in_str(const char * s, int slen, int ch1, int ch2)
{
int k;
int res = 0;
while (--slen >= 0) {
k = s[slen];
if ((ch1 == k) || (ch2 == k))
++res;
}
return res;
}
/* Allocates and then populates a scatter gether list (array) and returns
* it via *sgl_pp. Return of 0 is okay, else error number (in which case
* NULL is written to *sgl_pp) . */
static int
skip_seek(struct global_collection *clp, const char * key, const char * buf,
bool is_skip, bool ignore_verbose)
{
bool def_hex = false;
int len;
int vb = clp->verbose; /* needs to appear before skip/seek= on cl */
int64_t ll;
const char * cp;
class scat_gath_list & either_list = is_skip ? clp->i_sgl : clp->o_sgl;
if (ignore_verbose)
vb = 0;
len = (int)strlen(buf);
if ((('-' == buf[0]) && (1 == len)) || ((len > 1) && ('@' == buf[0])) ||
((len > 2) && ('H' == toupper(buf[0])) && ('@' == buf[1]))) {
if ('H' == toupper(buf[0])) {
cp = buf + 2;
def_hex = true;
} else if ('-' == buf[0])
cp = buf;
else
cp = buf + 1;
if (! either_list.load_from_file(cp, def_hex, clp->flexible, true)) {
pr2serr("bad argument to '%s=' [err=%d]\n", key,
either_list.m_errno);
return SG_LIB_SYNTAX_ERROR;
}
} else if (num_either_ch_in_str(buf, len, ',', ' ') > 0) {
if (! either_list.load_from_cli(buf, vb > 0)) {
pr2serr("bad command line argument to '%s='\n", key);
return SG_LIB_SYNTAX_ERROR;
}
} else { /* single number on command line (e.g. skip=1234) */
ll = sg_get_llnum(buf);
if ((ll < 0) || (ll > MAX_COUNT_SKIP_SEEK)) {
pr2serr("bad argument to '%s='\n", key);
return SG_LIB_SYNTAX_ERROR;
}
either_list.append_1or(0, ll);
if (vb > 1)
pr2serr("%s: singleton, half a degenerate sgl element\n", key);
}
either_list.sum_scan(key, vb > 3 /* bool show_sgl */, vb > 1);
return 0;
}
static bool
process_flags(const char * arg, struct flags_t * fp)
{
char buff[256];
char * cp;
char * np;
strncpy(buff, arg, sizeof(buff));
buff[sizeof(buff) - 1] = '\0';
if ('\0' == buff[0]) {
pr2serr("no flag found\n");
return false;
}
cp = buff;
do {
np = strchr(cp, ',');
if (np)
*np++ = '\0';
if (0 == strcmp(cp, "00"))
fp->zero = true;
else if (0 == strcmp(cp, "append"))
fp->append = true;
else if (0 == strcmp(cp, "coe"))
fp->coe = true;
else if (0 == strcmp(cp, "dio"))
fp->dio = true;
else if (0 == strcmp(cp, "direct"))
fp->direct = true;
else if (0 == strcmp(cp, "dpo"))
fp->dpo = true;
else if (0 == strcmp(cp, "dsync"))
fp->dsync = true;
else if (0 == strcmp(cp, "excl"))
fp->excl = true;
else if (0 == strcmp(cp, "ff"))
fp->ff = true;
else if (0 == strcmp(cp, "fua"))
fp->fua = true;
else if (0 == strcmp(cp, "hipri"))
fp->polled = true;
else if (0 == strcmp(cp, "masync"))
fp->masync = true;
else if (0 == strcmp(cp, "mmap"))
++fp->mmap; /* mmap > 1 stops munmap() being called */
else if (0 == strcmp(cp, "nocreat"))
fp->nocreat = true;
else if (0 == strcmp(cp, "nodur"))
fp->no_dur = true;
else if (0 == strcmp(cp, "no_dur"))
fp->no_dur = true;
else if (0 == strcmp(cp, "no-dur"))
fp->no_dur = true;
else if (0 == strcmp(cp, "nothresh"))
fp->no_thresh = true;
else if (0 == strcmp(cp, "no_thresh"))
fp->no_thresh = true;
else if (0 == strcmp(cp, "no-thresh"))
fp->no_thresh = true;
else if (0 == strcmp(cp, "noxfer"))
; /* accept but ignore */
else if (0 == strcmp(cp, "null"))
;
else if (0 == strcmp(cp, "ordered"))
fp->order_wr = true;
else if (0 == strcmp(cp, "order"))
fp->order_wr = true;
else if (0 == strcmp(cp, "polled"))
fp->polled = true;
else if (0 == strcmp(cp, "qhead"))
fp->qhead = true;
else if (0 == strcmp(cp, "qtail"))
fp->qtail = true;
else if (0 == strcmp(cp, "random"))
fp->random = true;
else if ((0 == strcmp(cp, "mout_if")) || (0 == strcmp(cp, "mout-if")))
fp->mout_if = true;
else if ((0 == strcmp(cp, "same_fds")) ||
(0 == strcmp(cp, "same-fds")))
fp->same_fds = true;
else if (0 == strcmp(cp, "serial"))
fp->serial = true;
else if (0 == strcmp(cp, "swait"))
; /* accept but ignore */
else if (0 == strcmp(cp, "wq_excl"))
fp->wq_excl = true;
else {
pr2serr("unrecognised flag: %s\n", cp);
return false;
}
cp = np;
} while (cp);
return true;
}
/* Process arguments given to 'conv=" option. Returns 0 on success,
* 1 on error. */
static int
process_conv(const char * arg, struct flags_t * ifp, struct flags_t * ofp)
{
char buff[256];
char * cp;
char * np;
strncpy(buff, arg, sizeof(buff));
buff[sizeof(buff) - 1] = '\0';
if ('\0' == buff[0]) {
pr2serr("no conversions found\n");
return 1;
}
cp = buff;
do {
np = strchr(cp, ',');
if (np)
*np++ = '\0';
if (0 == strcmp(cp, "nocreat"))
ofp->nocreat = true;
else if (0 == strcmp(cp, "noerror"))
ifp->coe = true; /* will still fail on write error */
else if (0 == strcmp(cp, "notrunc"))
; /* this is the default action of sg_dd so ignore */
else if (0 == strcmp(cp, "null"))
;
else if (0 == strcmp(cp, "sync"))
; /* dd(susv4): pad errored block(s) with zeros but sg_dd does
* that by default. Typical dd use: 'conv=noerror,sync' */
else {
pr2serr("unrecognised flag: %s\n", cp);
return 1;
}
cp = np;
} while (cp);
return 0;
}
#define STR_SZ 1024
#define INOUTF_SZ 512
static int
parse_cmdline_sanity(int argc, char * argv[], struct global_collection * clp,
char * outregf)
{
bool contra = false;
bool verbose_given = false;
bool version_given = false;
bool verify_given = false;
bool bpt_given = false;
int ibs = 0;
int obs = 0;
int ret = 0;
int k, keylen, n, res;
char str[STR_SZ];
char * key;
char * buf;
char * skip_buf = NULL;
char * seek_buf = NULL;
const char * cp;
const char * ccp;
for (k = 1; k < argc; k++) {
if (argv[k]) {
strncpy(str, argv[k], STR_SZ);
str[STR_SZ - 1] = '\0';
} else
continue;
for (key = str, buf = key; *buf && *buf != '=';)
buf++;
if (*buf)
*buf++ = '\0';
keylen = strlen(key);
if (0 == strcmp(key, "bpt")) {
clp->bpt = sg_get_num(buf);
if ((clp->bpt < 0) || (clp->bpt > MAX_BPT_VALUE)) {
pr2serr("%sbad argument to 'bpt='\n", my_name);
goto syn_err;
}
bpt_given = true;
} else if (0 == strcmp(key, "bs")) {
clp->bs = sg_get_num(buf);
if ((clp->bs < 0) || (clp->bs > MAX_BPT_VALUE)) {
pr2serr("%sbad argument to 'bs='\n", my_name);
goto syn_err;
}
} else if (0 == strcmp(key, "cdbsz")) {
ccp = strchr(buf, ',');
n = sg_get_num(buf);
if ((n < 0) || (n > 32)) {
pr2serr("%s: bad argument to 'cdbsz=', expect 6, 10, 12 or "
"16\n", my_name);
goto syn_err;
}
clp->cdbsz_in = n;
if (ccp) {
n = sg_get_num(ccp + 1);
if ((n < 0) || (n > 32)) {
pr2serr("%s: bad second argument to 'cdbsz=', expect 6, "
"10, 12 or 16\n", my_name);
goto syn_err;
}
}
clp->cdbsz_out = n;
clp->cdbsz_given = true;
} else if (0 == strcmp(key, "cdl")) {
ccp = strchr(buf, ',');
n = sg_get_num(buf);
if ((n < 0) || (n > 7)) {
pr2serr("%s: bad argument to 'cdl=', expect 0 to 7\n",
my_name);
goto syn_err;
}
clp->in_flags.cdl = n;
if (ccp) {
n = sg_get_num(ccp + 1);
if ((n < 0) || (n > 7)) {
pr2serr("%s: bad second argument to 'cdl=', expect 0 "
"to 7\n", my_name);
goto syn_err;
}
}
clp->out_flags.cdl = n;
clp->cdl_given = true;
} else if (0 == strcmp(key, "coe")) {
/* not documented, for compat with sgh_dd */
clp->in_flags.coe = !! sg_get_num(buf);
clp->out_flags.coe = clp->in_flags.coe;
} else if (0 == strcmp(key, "conv")) {
if (process_conv(buf, &clp->in_flags, &clp->out_flags)) {
pr2serr("%s: bad argument to 'conv='\n", my_name);
goto syn_err;
}
} else if (0 == strcmp(key, "count")) {
if (clp->count_given) {
pr2serr("second 'count=' argument detected, only one "
"please\n");
contra = true;
goto syn_err;
}
if (0 != strcmp("-1", buf)) {
clp->dd_count = sg_get_llnum(buf);
if ((clp->dd_count < 0) ||
(clp->dd_count > MAX_COUNT_SKIP_SEEK)) {
pr2serr("%sbad argument to 'count='\n", my_name);
goto syn_err;
}
} /* treat 'count=-1' as calculate count (same as not given) */
clp->count_given = true;
} else if (0 == strcmp(key, "dio")) {
clp->in_flags.dio = !! sg_get_num(buf);
clp->out_flags.dio = clp->in_flags.dio;
} else if (0 == strcmp(key, "elemsz_kb")) {
n = sg_get_num(buf);
if ((n < 1) || (n > (MAX_BPT_VALUE / 1024))) {
pr2serr("elemsz_kb=EKB wants an integer > 0\n");
goto syn_err;
}
if (n & (n - 1)) {
pr2serr("elemsz_kb=EKB wants EKB to be power of 2\n");
goto syn_err;
}
clp->elem_sz = n * 1024;
} else if (0 == strcmp(key, "ese")) {
n = sg_get_num(buf);
if (n < 0) {
pr2serr("ese= wants 0 (default) or 1\n");
goto syn_err;
}
clp->ese = !!n;
} else if (0 == strcmp(key, "fua")) {
n = sg_get_num(buf);
if (n & 1)
clp->out_flags.fua = true;
if (n & 2)
clp->in_flags.fua = true;
} else if (0 == strcmp(key, "ibs")) {
ibs = sg_get_num(buf);
if ((ibs < 0) || (ibs > MAX_BPT_VALUE)) {
pr2serr("%sbad argument to 'ibs='\n", my_name);
goto syn_err;
}
} else if (0 == strcmp(key, "if")) {
if (clp->inf_v.size() > 0) {
pr2serr("Second 'if=' argument??\n");
goto syn_err;
} else {
cp = buf;
while ((ccp = strchr(cp, ','))) {
clp->inf_v.push_back(string(cp , ccp - cp));
cp = ccp + 1;
}
clp->inf_v.push_back(string(cp , strlen(cp)));
}
} else if (0 == strcmp(key, "iflag")) {
if (! process_flags(buf, &clp->in_flags)) {
pr2serr("%sbad argument to 'iflag='\n", my_name);
goto syn_err;
}
} else if ((0 == strcmp(key, "hipri")) ||
(0 == strcmp(key, "mrq")) ||
(0 == strcmp(key, "polled"))) {
if (isdigit(buf[0]))
cp = buf;
else {
pr2serr("%sonly mrq=NRQS or polled=NRQS which is a number "
"allowed here\n", my_name);
goto syn_err;
}
clp->mrq_num = sg_get_num(cp);
if (clp->mrq_num < 0) {
pr2serr("%sbad argument to 'mrq='\n", my_name);
goto syn_err;
}
if (0 == clp->mrq_num) {
clp->mrq_eq_0 = true;
clp->mrq_num = 1;
pr2serr("note: send single, non-mrq commands\n");
}
if ('m' != key[0])
clp->mrq_polled = true;
} else if ((0 == strcmp(key, "no_waitq")) ||
(0 == strcmp(key, "no-waitq"))) {
n = sg_get_num(buf);
if (-1 == n) {
pr2serr("%sbad argument to 'no_waitq=', expect 0 or 1\n",
my_name);
goto syn_err;
}
clp->in_flags.no_waitq = true;
clp->out_flags.no_waitq = true;
} else if (0 == strcmp(key, "obs")) {
obs = sg_get_num(buf);
if ((obs < 0) || (obs > MAX_BPT_VALUE)) {
pr2serr("%sbad argument to 'obs='\n", my_name);
goto syn_err;
}
} else if (strcmp(key, "ofreg") == 0) {
if ('\0' != outregf[0]) {
pr2serr("Second OFREG argument??\n");
contra = true;
goto syn_err;
} else {
memcpy(outregf, buf, INOUTF_SZ);
outregf[INOUTF_SZ - 1] = '\0'; /* noisy compiler */
}
} else if (strcmp(key, "of") == 0) {
if (clp->outf_v.size() > 0) {
pr2serr("Second 'of=' argument??\n");
goto syn_err;
} else {
cp = buf;
while ((ccp = strchr(cp, ','))) {
clp->outf_v.push_back(string(cp , ccp - cp));
cp = ccp + 1;
}
clp->outf_v.push_back(string(cp , strlen(cp)));
}
} else if (0 == strcmp(key, "oflag")) {
if (! process_flags(buf, &clp->out_flags)) {
pr2serr("%sbad argument to 'oflag='\n", my_name);
goto syn_err;
}
} else if (0 == strcmp(key, "sdt")) {
ccp = strchr(buf, ',');
n = sg_get_num(buf);
if (n < 0) {
pr2serr("%sbad argument to 'sdt=CRT[,ICT]'\n", my_name);
goto syn_err;
}
clp->sdt_crt = n;
if (ccp) {
n = sg_get_num(ccp + 1);
if (n < 0) {
pr2serr("%sbad 2nd argument to 'sdt=CRT,ICT'\n",
my_name);
goto syn_err;
}
clp->sdt_ict = n;
}
} else if (0 == strcmp(key, "seek")) {
n = strlen(buf);
if (n < 1) {
pr2serr("%sneed argument to 'seek='\n", my_name);
goto syn_err;
}
seek_buf = (char *)calloc(n + 16, 1);
if (NULL == seek_buf)
goto syn_err;
memcpy(seek_buf, buf, n + 1);
} else if (0 == strcmp(key, "skip")) {
n = strlen(buf);
if (n < 1) {
pr2serr("%sneed argument to 'skip='\n", my_name);
goto syn_err;
}
skip_buf = (char *)calloc(n + 16, 1);
if (NULL == skip_buf)
goto syn_err;
memcpy(skip_buf, buf, n + 1);
} else if (0 == strcmp(key, "sync"))
do_sync = !! sg_get_num(buf);
else if (0 == strcmp(key, "thr")) {
num_threads = sg_get_num(buf);
if ((num_threads < 0) || (num_threads > MAX_BPT_VALUE)) {
pr2serr("%sneed argument to 'skip='\n", my_name);
goto syn_err;
}
} else if (0 == strcmp(key, "time")) {
ccp = strchr(buf, ',');
do_time = sg_get_num(buf);
if (do_time < 0) {
pr2serr("%sbad argument to 'time=0|1|2'\n", my_name);
goto syn_err;
}
if (ccp) {
n = sg_get_num(ccp + 1);
if ((n < 0) || (n > (MAX_BPT_VALUE / 1000))) {
pr2serr("%sbad argument to 'time=0|1|2,TO'\n", my_name);
goto syn_err;
}
clp->cmd_timeout = n ? (n * 1000) : DEF_TIMEOUT;
}
} else if (0 == strncmp(key, "verb", 4))
clp->verbose = sg_get_num(buf);
else if ((keylen > 1) && ('-' == key[0]) && ('-' != key[1])) {
res = 0;
n = num_chs_in_str(key + 1, keylen - 1, 'c');
if (n > 0)
verify_given = true;
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'd');
clp->dry_run += n;
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'h');
clp->help += n;
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'p');
if (n > 0)
clp->prefetch = true;
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'v');
if (n > 0)
verbose_given = true;
clp->verbose += n; /* -v ---> --verbose */
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'V');
if (n > 0)
version_given = true;
res += n;
n = num_chs_in_str(key + 1, keylen - 1, 'x');
if (n > 0)
verify_given = true;
res += n;
if (res < (keylen - 1)) {
pr2serr("Unrecognised short option in '%s', try '--help'\n",
key);
goto syn_err;
}
} else if (0 == strncmp(key, "--compare", 6))
verify_given = true;
else if ((0 == strncmp(key, "--dry-run", 9)) ||
(0 == strncmp(key, "--dry_run", 9)))
++clp->dry_run;
else if ((0 == strncmp(key, "--help", 6)) ||
(0 == strcmp(key, "-?")))
++clp->help;
else if ((0 == strncmp(key, "--prefetch", 10)) ||
(0 == strncmp(key, "--pre-fetch", 11)))
clp->prefetch = true;
else if (0 == strncmp(key, "--verb", 6)) {
verbose_given = true;
++clp->verbose; /* --verbose */
} else if (0 == strncmp(key, "--veri", 6))
verify_given = true;
else if (0 == strncmp(key, "--vers", 6))
version_given = true;
else {
pr2serr("Unrecognized option '%s'\n", key);
pr2serr("For more information use '--help'\n");
goto syn_err;
}
} /* end of parsing for loop */
if (skip_buf) {
res = skip_seek(clp, "skip", skip_buf, true /* skip */, false);
free(skip_buf);
skip_buf = NULL;
if (res) {
pr2serr("%sbad argument to 'seek='\n", my_name);
goto syn_err;
}
}
if (seek_buf) {
res = skip_seek(clp, "seek", seek_buf, false /* skip */, false);
free(seek_buf);
seek_buf = NULL;
if (res) {
pr2serr("%sbad argument to 'seek='\n", my_name);
goto syn_err;
}
}
/* heap usage should be all freed up now */
#ifdef DEBUG
pr2serr("In DEBUG mode, ");
if (verbose_given && version_given) {
pr2serr("but override: '-vV' given, zero verbose and continue\n");
verbose_given = false;
version_given = false;
clp->verbose = 0;
} else if (! verbose_given) {
pr2serr("set '-vv'\n");
clp->verbose = 2;
} else
pr2serr("keep verbose=%d\n", clp->verbose);
#else
if (verbose_given && version_given)
pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
#endif
if (version_given) {
pr2serr("%s%s\n", my_name, version_str);
ret = SG_LIB_OK_FALSE;
goto oth_err;
}
if (clp->help > 0) {
usage(clp->help);
ret = SG_LIB_OK_FALSE;
goto oth_err;
}
if (clp->bs <= 0) {
clp->bs = DEF_BLOCK_SIZE;
pr2serr("Assume default 'bs' ((logical) block size) of %d bytes\n",
clp->bs);
}
if (verify_given) {
pr2serr("Doing verify/cmp rather than copy\n");
clp->verify = true;
}
if ((ibs && (ibs != clp->bs)) || (obs && (obs != clp->bs))) {
pr2serr("If 'ibs' or 'obs' given must be same as 'bs'\n");
usage(0);
goto syn_err;
}
if (clp->out_flags.append) {
if ((clp->o_sgl.lowest_lba > 0) ||
(clp->o_sgl.linearity != SGL_LINEAR)) {
pr2serr("Can't use both append and seek switches\n");
goto syn_err;
}
if (verify_given) {
pr2serr("Can't use both append and verify switches\n");
goto syn_err;
}
}
if (clp->bpt < 1) {
pr2serr("bpt must be greater than 0\n");
goto syn_err;
}
if (clp->in_flags.mmap && clp->out_flags.mmap) {
pr2serr("mmap flag on both IFILE and OFILE doesn't work\n");
goto syn_err;
}
/* defaulting transfer size to 128*2048 for CD/DVDs is too large
* for the block layer in lk 2.6 and results in an EIO on the
* SG_IO ioctl. So reduce it in that case. */
if ((clp->bs >= 2048) && (! bpt_given))
clp->bpt = DEF_BLOCKS_PER_2048TRANSFER;
if (clp->in_flags.order_wr && (! clp->out_flags.order_wr))
pr2serr("Warning iflag=order is ignored, use with oflag=\n");
if ((num_threads < 1) || (num_threads > MAX_NUM_THREADS)) {
pr2serr("too few or too many threads requested\n");
usage(1);
goto syn_err;
}
clp->unit_nanosec = (do_time > 1) || !!getenv("SG3_UTILS_LINUX_NANO");
return 0;
syn_err:
if (seek_buf)
free(seek_buf);
if (skip_buf)
free(skip_buf);
return contra ? SG_LIB_CONTRADICT : SG_LIB_SYNTAX_ERROR;
oth_err:
if (seek_buf)
free(seek_buf);
if (skip_buf)
free(skip_buf);
return ret;
}
static int
calc_count(struct global_collection * clp, const char * inf,
int64_t & in_num_sect, const char * outf, int64_t & out_num_sect)
{
int in_sect_sz, out_sect_sz, res;
if (clp->dd_count < 0) {
in_num_sect = -1;
out_num_sect = -1;
}
if (FT_SG == clp->in_type) {
res = scsi_read_capacity(clp->in0fd, &in_num_sect, &in_sect_sz);
if (2 == res) {
pr2serr("Unit attention, media changed(in), continuing\n");
res = scsi_read_capacity(clp->in0fd, &in_num_sect,
&in_sect_sz);
}
if (0 != res) {
if (res == SG_LIB_CAT_INVALID_OP)
pr2serr("read capacity not supported on %s\n", inf);
else if (res == SG_LIB_CAT_NOT_READY)
pr2serr("read capacity failed, %s not ready\n", inf);
else
pr2serr("Unable to read capacity on %s\n", inf);
return SG_LIB_FILE_ERROR;
} else if (clp->bs != in_sect_sz) {
pr2serr(">> warning: logical block size on %s confusion: "
"bs=%d, device claims=%d\n", clp->infp, clp->bs,
in_sect_sz);
return SG_LIB_FILE_ERROR;
}
}
if (FT_SG == clp->out_type) {
res = scsi_read_capacity(clp->out0fd, &out_num_sect, &out_sect_sz);
if (2 == res) {
pr2serr("Unit attention, media changed(out), continuing\n");
res = scsi_read_capacity(clp->out0fd, &out_num_sect,
&out_sect_sz);
}
if (0 != res) {
if (res == SG_LIB_CAT_INVALID_OP)
pr2serr("read capacity not supported on %s\n", outf);
else if (res == SG_LIB_CAT_NOT_READY)
pr2serr("read capacity failed, %s not ready\n", outf);
else
pr2serr("Unable to read capacity on %s\n", outf);
out_num_sect = -1;
return SG_LIB_FILE_ERROR;
} else if (clp->bs != out_sect_sz) {
pr2serr(">> warning: logical block size on %s confusion: "
"bs=%d, device claims=%d\n", clp->outfp, clp->bs,
out_sect_sz);
return SG_LIB_FILE_ERROR;
}
}
if (clp->dd_count < 0) {
if (FT_SG == clp->in_type)
;
else if (FT_BLOCK == clp->in_type) {
if (0 != read_blkdev_capacity(clp->in0fd, &in_num_sect,
&in_sect_sz)) {
pr2serr("Unable to read block capacity on %s\n", inf);
in_num_sect = -1;
}
if (clp->bs != in_sect_sz) {
pr2serr("logical block size on %s confusion; bs=%d, from "
"device=%d\n", inf, clp->bs, in_sect_sz);
in_num_sect = -1;
}
}
if (FT_SG == clp->out_type)
;
else if (FT_BLOCK == clp->out_type) {
if (0 != read_blkdev_capacity(clp->out0fd, &out_num_sect,
&out_sect_sz)) {
pr2serr("Unable to read block capacity on %s\n", outf);
out_num_sect = -1;
}
if (clp->bs != out_sect_sz) {
pr2serr("logical block size on %s confusion: bs=%d, from "
"device=%d\n", outf, clp->bs, out_sect_sz);
out_num_sect = -1;
}
}
}
return 0;
}
static int
do_count_work(struct global_collection * clp, const char * inf,
int64_t & in_num_sect, const char * outf,
int64_t & out_num_sect)
{
int res;
class scat_gath_list * isglp = &clp->i_sgl;
class scat_gath_list * osglp = &clp->o_sgl;
res = calc_count(clp, inf, in_num_sect, outf, out_num_sect);
if (res)
return res;
if ((-1 == in_num_sect) && (FT_OTHER == clp->in_type)) {
in_num_sect = clp->in_st_size / clp->bs;
if (clp->in_st_size % clp->bs) {
++in_num_sect;
pr2serr("Warning: the file size of %s is not a multiple of BS "
"[%d]\n", inf, clp->bs);
}
}
if ((in_num_sect > 0) && (isglp->high_lba_p1 > in_num_sect)) {
pr2serr("%shighest LBA [0x%" PRIx64 "] exceeds input length: %"
PRIx64 " blocks\n", my_name, isglp->high_lba_p1 - 1,
in_num_sect);
return SG_LIB_CAT_OTHER;
}
if ((out_num_sect > 0) && (osglp->high_lba_p1 > out_num_sect)) {
pr2serr("%shighest LBA [0x%" PRIx64 "] exceeds output length: %"
PRIx64 " blocks\n", my_name, osglp->high_lba_p1 - 1,
out_num_sect);
return SG_LIB_CAT_OTHER;
}
if (isglp->sum_hard || osglp->sum_hard) {
int64_t ccount;
if (isglp->sum_hard && osglp->sum_hard) {
if (isglp->sum != osglp->sum) {
pr2serr("%stwo hard sgl_s, sum of blocks differ: in=%" PRId64
", out=%" PRId64 "\n", my_name , isglp->sum,
osglp->sum);
return SG_LIB_CAT_OTHER;
}
ccount = isglp->sum;
} else if (isglp->sum_hard) {
if (osglp->sum > isglp->sum) {
pr2serr("%soutput sgl already too many blocks [%" PRId64
"]\n", my_name, osglp->sum);
return SG_LIB_CAT_OTHER;
}
if (osglp->linearity != SGL_NON_MONOTONIC)
osglp->append_1or(isglp->sum - osglp->sum);
else {
pr2serr("%soutput sgl non-montonic: can't extend\n",
my_name);
return SG_LIB_CAT_OTHER;
}
ccount = isglp->sum;
} else { /* only osglp hard */
if (isglp->sum > osglp->sum) {
pr2serr("%sinput sgl already too many blocks [%" PRId64
"]\n", my_name, isglp->sum);
return SG_LIB_CAT_OTHER;
}
if (isglp->linearity != SGL_NON_MONOTONIC)
isglp->append_1or(osglp->sum - isglp->sum);
else {
pr2serr("%sinput sgl non-monotonic: can't extend\n",
my_name);
return SG_LIB_CAT_OTHER;
}
ccount = osglp->sum;
}
if (SG_COUNT_INDEFINITE == clp->dd_count)
clp->dd_count = ccount;
else if (ccount != clp->dd_count) {
pr2serr("%scount=COUNT disagrees with scatter gather list "
"length [%" PRId64 "]\n", my_name, ccount);
return SG_LIB_CAT_OTHER;
}
} else if (clp->dd_count != 0) { /* and both input and output are soft */
int64_t iposs = INT64_MAX;
int64_t oposs = INT64_MAX;
if (clp->dd_count > 0) {
if (isglp->sum > clp->dd_count) {
pr2serr("%sskip sgl sum [%" PRId64 "] exceeds COUNT\n",
my_name, isglp->sum);
return SG_LIB_CAT_OTHER;
}
if (osglp->sum > clp->dd_count) {
pr2serr("%sseek sgl sum [%" PRId64 "] exceeds COUNT\n",
my_name, osglp->sum);
return SG_LIB_CAT_OTHER;
}
goto fini;
}
/* clp->dd_count == SG_COUNT_INDEFINITE */
if (in_num_sect > 0)
iposs = in_num_sect + isglp->sum - isglp->high_lba_p1;
if (out_num_sect > 0)
oposs = out_num_sect + osglp->sum - osglp->high_lba_p1;
clp->dd_count = iposs < oposs ? iposs : oposs;
if (INT64_MAX == clp->dd_count) {
pr2serr("%scan't deduce count=COUNT, please supply one\n",
my_name);
return SG_LIB_CAT_OTHER;
}
if (isglp->sum > clp->dd_count) {
pr2serr("%sdeduced COUNT [%" PRId64 "] exceeds skip sgl sum\n",
my_name, clp->dd_count);
return SG_LIB_CAT_OTHER;
}
if (osglp->sum > clp->dd_count) {
pr2serr("%sdeduced COUNT [%" PRId64 "] exceeds seek sgl sum\n",
my_name, clp->dd_count);
return SG_LIB_CAT_OTHER;
}
}
if (clp->dd_count == 0)
return 0;
fini:
if (clp->dd_count > isglp->sum)
isglp->append_1or(clp->dd_count - isglp->sum);
if (clp->dd_count > osglp->sum)
osglp->append_1or(clp->dd_count - osglp->sum);
return 0;
}
int
main(int argc, char * argv[])
{
bool fail_after_cli = false;
bool ifile_given = true;
// char inf[INOUTF_SZ];
// char outf[INOUTF_SZ];
char outregf[INOUTF_SZ];
int res, k, err;
size_t num_ifiles, num_ofiles, num_slices, inf0_sz;
int64_t in_num_sect = -1;
int64_t out_num_sect = -1;
const char * ccp = NULL;
const char * cc2p;
struct global_collection * clp = &gcoll;
thread sig_listen_thr;
vector<thread> work_thr_v;
vector<thread> listen_thr_v;
char ebuff[EBUFF_SZ];
#if 0 /* SG_LIB_ANDROID */
struct sigaction actions;
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
sigaction(SIGUSR1, &actions, NULL);
sigaction(SIGUSR2, &actions, NULL);
#endif
/* memset(clp, 0, sizeof(*clp)); */
clp->dd_count = SG_COUNT_INDEFINITE;
clp->bpt = DEF_BLOCKS_PER_TRANSFER;
clp->cmd_timeout = DEF_TIMEOUT;
clp->sdt_ict = DEF_SDT_ICT_MS;
clp->sdt_crt = DEF_SDT_CRT_SEC;
clp->in_type = FT_FIFO;
/* change dd's default: if of=OFILE not given, assume /dev/null */
clp->out_type = FT_DEV_NULL;
clp->cdbsz_in = DEF_SCSI_CDB_SZ;
clp->cdbsz_out = DEF_SCSI_CDB_SZ;
clp->mrq_num = DEF_MRQ_NUM;
// inf[0] = '\0';
// outf[0] = '\0';
outregf[0] = '\0';
fetch_sg_version();
if (sg_version >= 40045)
sg_version_ge_40045 = true;
else {
pr2serr(">>> %srequires an sg driver version of 4.0.45 or later\n\n",
my_name);
fail_after_cli = true;
}
res = parse_cmdline_sanity(argc, argv, clp, outregf);
if (SG_LIB_OK_FALSE == res)
return 0;
if (res)
return res;
if (fail_after_cli) {
pr2serr("%scommand line parsing was okay but sg driver is too old\n",
my_name);
return SG_LIB_SYNTAX_ERROR;
}
install_handler(SIGINT, interrupt_handler);
install_handler(SIGQUIT, interrupt_handler);
install_handler(SIGPIPE, interrupt_handler);
install_handler(SIGUSR1, siginfo_handler);
install_handler(SIGUSR2, siginfo2_handler);
num_ifiles = clp->inf_v.size();
num_ofiles = clp->outf_v.size();
if (num_ifiles > MAX_SLICES) {
pr2serr("%sonly support %d slices but given %zd IFILEs\n", my_name,
MAX_SLICES, num_ifiles);
return SG_LIB_SYNTAX_ERROR;
}
if (num_ofiles > MAX_SLICES) {
pr2serr("%sonly support %d slices but given %zd OFILEs\n", my_name,
MAX_SLICES, num_ifiles);
return SG_LIB_SYNTAX_ERROR;
}
if (0 == num_ofiles) {
if (0 == num_ifiles) {
pr2serr("%sexpect either if= or of= to be given\n", my_name);
return SG_LIB_SYNTAX_ERROR;
}
for (k = 0; k < (int)num_ifiles; ++k)
clp->outf_v.push_back("."); /* same as /dev/null */
}
if (0 == num_ifiles) {
ifile_given = false;
for (k = 0; k < (int)num_ofiles; ++k)
clp->inf_v.push_back("");
}
if ((num_ifiles > 1) && (num_ofiles > 1) && (num_ifiles != num_ofiles)) {
pr2serr("%snumber of IFILEs [%zd] and number of OFILEs [%zd] > 1 "
"and unequal\n", my_name, num_ifiles, num_ofiles);
return SG_LIB_SYNTAX_ERROR;
}
if ((num_ifiles > 1) && (1 == num_ofiles)) {
/* if many IFILEs and one OFILE, replicate OFILE till same size */
for (k = 1; k < (int)num_ifiles; ++k)
clp->outf_v.push_back(clp->outf_v[0]);
num_ofiles = clp->outf_v.size();
} else if ((num_ofiles > 1) && (1 == num_ifiles)) {
/* if many OFILEs and one IFILE, replicate IFILE till same size */
for (k = 1; k < (int)num_ofiles; ++k)
clp->inf_v.push_back(clp->inf_v[0]);
num_ifiles = clp->inf_v.size();
}
num_slices = (num_ifiles > num_ofiles) ? num_ifiles : num_ofiles;
if ((int)num_slices > num_threads) {
pr2serr("%sNumber of slices [%zd] exceeds number of threads [%d].\n",
my_name, num_slices, num_threads);
pr2serr("Number of threads needs to be increased.\n");
return SG_LIB_SYNTAX_ERROR;
}
k = 0;
for (auto && cvp : clp->cp_ver_arr) {
if (k >= (int)num_slices)
break;
cvp.my_index = k++;
cvp.state = cp_ver_pair_t::my_state::init;
}
clp->in0fd = STDIN_FILENO;
clp->out0fd = STDOUT_FILENO;
if (clp->in_flags.ff && clp->in_flags.zero) {
ccp = "<addr_as_data>";
cc2p = "addr_as_data";
} else if (clp->in_flags.ff) {
ccp = "<0xff bytes>";
cc2p = "ff";
} else if (clp->in_flags.random) {
ccp = "<random>";
cc2p = "random";
} else if (clp->in_flags.zero) {
ccp = "<zero bytes>";
cc2p = "00";
}
inf0_sz = clp->inf_v.size() ? clp->inf_v[0].size() : 0;
if (ccp) {
if (ifile_given) {
pr2serr("%siflag=%s and if=%s contradict\n", my_name, cc2p,
clp->inf_v[0].c_str());
return SG_LIB_CONTRADICT;
}
for (auto && cvp : clp->cp_ver_arr) {
if (cvp.state == cp_ver_pair_t::my_state::empty)
break;
cvp.in_type = FT_RANDOM_0_FF;
}
clp->in_type = FT_RANDOM_0_FF;
clp->infp = ccp;
clp->in0fd = -1;
} else if (inf0_sz && ('-' != clp->inf_v[0].c_str()[0])) {
const string & inf_s = clp->inf_v[0];
const char * infp = inf_s.c_str();
clp->in_type = dd_filetype(infp, clp->in_st_size);
if (FT_ERROR == clp->in_type) {
pr2serr("%sunable to access %s\n", my_name, infp);
return SG_LIB_FILE_ERROR;
} else if (FT_ST == clp->in_type) {
pr2serr("%sunable to use scsi tape device %s\n", my_name, infp);
return SG_LIB_FILE_ERROR;
} else if (FT_CHAR == clp->in_type) {
pr2serr("%sunable to use unknown char device %s\n", my_name, infp);
return SG_LIB_FILE_ERROR;
} else if (FT_SG == clp->in_type) {
clp->in0fd = sg_in_open(clp, inf_s, NULL, NULL);
if (clp->in0fd < 0)
return -clp->in0fd;
} else {
clp->in0fd = reg_file_open(clp, infp, false /* read */);
if (clp->in0fd < 0)
return sg_convert_errno(-clp->in0fd);
}
clp->infp = infp;
}
if (clp->cdl_given && (! clp->cdbsz_given)) {
bool changed = false;
if ((clp->cdbsz_in < 16) && (clp->in_flags.cdl > 0)) {
clp->cdbsz_in = 16;
changed = true;
}
if ((clp->cdbsz_out < 16) && (! clp->verify) &&
(clp->out_flags.cdl > 0)) {
clp->cdbsz_out = 16;
changed = true;
}
if (changed)
pr2serr(">> increasing cdbsz to 16 due to cdl > 0\n");
}
if ((clp->verbose > 0) &&
(clp->in_flags.no_waitq || clp->out_flags.no_waitq))
pr2serr("no_waitq=<n> operand is now ignored\n");
if (clp->outf_v.size()) {
const string & outf_s = clp->outf_v[0].c_str();
const char * outfp = outf_s.c_str();
clp->ofile_given = true;
if ('-' == outfp[0])
clp->out_type = FT_FIFO;
else
clp->out_type = dd_filetype(outfp, clp->out_st_size);
if ((FT_SG != clp->out_type) && clp->verify) {
pr2serr("%s --verify only supported by sg OFILEs\n", my_name);
return SG_LIB_FILE_ERROR;
}
if (FT_FIFO == clp->out_type)
;
else if (FT_ST == clp->out_type) {
pr2serr("%sunable to use scsi tape device %s\n", my_name, outfp);
return SG_LIB_FILE_ERROR;
} else if (FT_CHAR == clp->out_type) {
pr2serr("%sunable to use unknown char device %s\n", my_name,
outfp);
return SG_LIB_FILE_ERROR;
} else if (FT_SG == clp->out_type) {
clp->out0fd = sg_out_open(clp, outf_s, NULL, NULL);
if (clp->out0fd < 0)
return -clp->out0fd;
} else if (FT_DEV_NULL == clp->out_type)
clp->out0fd = -1; /* don't bother opening */
else {
clp->out0fd = reg_file_open(clp, outfp, true /* write */);
if (clp->out0fd < 0)
return sg_convert_errno(-clp->out0fd);
}
clp->outfp = outfp;
}
if (clp->verify && (clp->out_type == FT_DEV_NULL)) {
pr2serr("Can't do verify when OFILE not given\n");
return SG_LIB_SYNTAX_ERROR;
}
if ((FT_SG == clp->in_type) && (FT_SG == clp->out_type)) {
if (clp->in_flags.serial || clp->out_flags.serial)
pr2serr("serial flag ignored when both IFILE and OFILE are sg "
"devices\n");
if (clp->in_flags.order_wr && (num_threads > 1))
pr2serr("Warning: write ordering only guaranteed for single "
"thread\n");
} else if (clp->in_flags.order_wr)
pr2serr("Warning: oflag=order only active on sg->sg copies\n");
if (outregf[0]) {
int ftyp = dd_filetype(outregf, clp->outreg_st_size);
clp->outreg_type = ftyp;
if (! ((FT_OTHER == ftyp) || (FT_ERROR == ftyp) ||
(FT_DEV_NULL == ftyp))) {
pr2serr("File: %s can only be regular file or pipe (or "
"/dev/null)\n", outregf);
return SG_LIB_SYNTAX_ERROR;
}
if ((clp->outregfd = open(outregf, O_WRONLY | O_CREAT, 0666)) < 0) {
err = errno;
snprintf(ebuff, EBUFF_SZ, "could not open %s for writing",
outregf);
perror(ebuff);
return sg_convert_errno(err);
}
if (clp->verbose > 1)
pr2serr("ofreg=%s opened okay, fd=%d\n", outregf, clp->outregfd);
if (FT_ERROR == ftyp)
clp->outreg_type = FT_OTHER; /* regular file created */
} else
clp->outregfd = -1;
if ((STDIN_FILENO == clp->in0fd) && (STDOUT_FILENO == clp->out0fd)) {
pr2serr("Won't default both IFILE to stdin _and_ OFILE to "
"/dev/null\n");
pr2serr("For more information use '--help'\n");
return SG_LIB_SYNTAX_ERROR;
}
if ((clp->in_type == FT_FIFO) && (! clp->i_sgl.is_pipe_suitable())) {
pr2serr("The skip= argument is not suitable for a pipe\n");
return SG_LIB_SYNTAX_ERROR;
}
if ((clp->out_type == FT_FIFO) && (! clp->o_sgl.is_pipe_suitable())) {
pr2serr("The seek= argument is not suitable for a pipe\n");
return SG_LIB_SYNTAX_ERROR;
}
res = do_count_work(clp, clp->inf_v[0].c_str(), in_num_sect,
clp->outf_v[0].c_str(), out_num_sect);
if (res)
return res;
if (clp->verbose > 2)
pr2serr("Start of loop, count=%" PRId64 ", in_num_sect=%" PRId64
", out_num_sect=%" PRId64 "\n", clp->dd_count, in_num_sect,
out_num_sect);
if (clp->dd_count < 0) {
pr2serr("Couldn't calculate count, please give one\n");
return SG_LIB_CAT_OTHER;
}
if (! clp->cdbsz_given) {
if ((FT_SG == clp->in_type) && (MAX_SCSI_CDB_SZ != clp->cdbsz_in) &&
((clp->i_sgl.high_lba_p1 > UINT_MAX) || (clp->bpt > USHRT_MAX))) {
pr2serr("Note: SCSI command size increased to 16 bytes (for "
"'if')\n");
clp->cdbsz_in = MAX_SCSI_CDB_SZ;
}
if ((FT_SG == clp->out_type) && (MAX_SCSI_CDB_SZ != clp->cdbsz_out) &&
((clp->o_sgl.high_lba_p1 > UINT_MAX) || (clp->bpt > USHRT_MAX))) {
pr2serr("Note: SCSI command size increased to 16 bytes (for "
"'of')\n");
clp->cdbsz_out = MAX_SCSI_CDB_SZ;
}
}
for (auto && cvp : clp->cp_ver_arr) {
cvp.in_type = clp->in_type;
cvp.out_type = clp->out_type;
cvp.dd_count = clp->dd_count;
cvp.in_rem_count = clp->dd_count;
cvp.out_rem_count = clp->dd_count;
}
if (clp->dry_run > 0) {
pr2serr("Due to --dry-run option, bypass copy/read\n");
goto fini;
}
if (! clp->ofile_given)
pr2serr("of=OFILE not given so only read from IFILE, to output to "
"stdout use 'of=-'\n");
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGINT);
sigaddset(&signal_set, SIGUSR2);
res = sigprocmask(SIG_BLOCK, &signal_set, &orig_signal_set);
if (res < 0) {
pr2serr("sigprocmask failed: %s\n", safe_strerror(errno));
goto fini;
}
listen_thr_v.emplace_back(sig_listen_thread, clp);
if (do_time) {
start_tm.tv_sec = 0;
start_tm.tv_usec = 0;
gettimeofday(&start_tm, NULL);
}
/* vvvvvvvvvvv Start worker threads vvvvvvvvvvvvvvvvvvvvvvvv */
if (num_threads > 0) {
auto & cvp = clp->cp_ver_arr[0];
cvp.in_fd = clp->in0fd;
cvp.out_fd = clp->out0fd;
/* launch "infant" thread to catch early mortality, if any */
work_thr_v.emplace_back(read_write_thread, clp, 0, 0, true);
{
unique_lock<mutex> lk(clp->infant_mut);
clp->infant_cv.wait(lk, []{ return gcoll.processed; });
}
if (clp->cp_ver_arr[0].next_count_pos.load() < 0) {
/* infant thread error-ed out, join with it */
for (auto & t : work_thr_v) {
if (t.joinable())
t.join();
}
goto jump;
}
/* now start the rest of the threads */
for (k = 1; k < num_threads; ++k)
work_thr_v.emplace_back(read_write_thread, clp, k,
k % (int)num_slices, false);
/* now wait for worker threads to finish */
for (auto & t : work_thr_v) {
if (t.joinable())
t.join();
}
} /* worker threads hereafter have all exited */
jump:
if (do_time && (start_tm.tv_sec || start_tm.tv_usec))
calc_duration_throughput(0);
if (do_sync) {
if (FT_SG == clp->out_type) {
pr2serr_lk(">> Synchronizing cache on %s\n",
(clp->outf_v.size() ? clp->outf_v[0].c_str() : "" ));
res = sg_ll_sync_cache_10(clp->out0fd, 0, 0, 0, 0, 0, false, 0);
if (SG_LIB_CAT_UNIT_ATTENTION == res) {
pr2serr_lk("Unit attention(out), continuing\n");
res = sg_ll_sync_cache_10(clp->out0fd, 0, 0, 0, 0, 0, false,
0);
}
if (0 != res)
pr2serr_lk("Unable to synchronize cache\n");
}
}
shutting_down = true;
for (auto & t : listen_thr_v) {
if (t.joinable()) {
t.detach();
if (listen_t_tid > 0)
kill(listen_t_tid, SIGUSR2);
// t.~thread(); /* kill listening thread; doesn't work */
}
std::this_thread::yield(); // not enough it seems
{ /* allow time for SIGUSR2 signal to get through */
struct timespec tspec = {0, 1000000}; /* 1 msec */
struct timespec rem;
while ((nanosleep(&tspec, &rem) < 0) && (EINTR == errno))
tspec = rem;
}
}
fini:
if ((STDIN_FILENO != clp->in0fd) && (clp->in0fd >= 0))
close(clp->in0fd);
if ((STDOUT_FILENO != clp->out0fd) && (FT_DEV_NULL != clp->out_type) &&
(clp->out0fd >= 0))
close(clp->out0fd);
if ((clp->outregfd >= 0) && (STDOUT_FILENO != clp->outregfd) &&
(FT_DEV_NULL != clp->outreg_type))
close(clp->outregfd);
print_stats("");
if (clp->dio_incomplete_count.load()) {
int fd;
char c;
pr2serr(">> Direct IO requested but incomplete %d times\n",
clp->dio_incomplete_count.load());
if ((fd = open(sg_allow_dio, O_RDONLY)) >= 0) {
if (1 == read(fd, &c, 1)) {
if ('0' == c)
pr2serr(">>> %s set to '0' but should be set to '1' for "
"direct IO\n", sg_allow_dio);
}
close(fd);
}
}
k = 0;
for (auto && cvp : gcoll.cp_ver_arr) {
if (cvp.state == cp_ver_pair_t::my_state::empty)
break;
++k;
if (cvp.sum_of_resids.load())
pr2serr(">> slice: %d, Non-zero sum of residual counts=%d\n",
k, cvp.sum_of_resids.load());
}
if (clp->verbose && (num_start_eagain > 0))
pr2serr("Number of start EAGAINs: %d\n", num_start_eagain.load());
if (clp->verbose && (num_fin_eagain > 0))
pr2serr("Number of finish EAGAINs: %d\n", num_fin_eagain.load());
if (clp->verbose && (num_ebusy > 0))
pr2serr("Number of EBUSYs: %d\n", num_ebusy.load());
if (clp->verbose && (num_miscompare > 0))
pr2serr("Number of miscompare%s: %d\n",
(num_miscompare > 1) ? "s" : "", num_miscompare.load());
if (clp->verify && (SG_LIB_CAT_MISCOMPARE == res))
pr2serr("Verify/compare failed due to miscompare\n");
if (0 == res)
res = clp->reason_res.load();
sigprocmask(SIG_SETMASK, &orig_signal_set, NULL);
if (clp->verbose) {
int num_sigusr2 = num_fallthru_sigusr2.load();
if (num_sigusr2 > 0)
pr2serr("Number of fall-through SIGUSR2 signals caught: %d\n",
num_sigusr2);
}
return (res >= 0) ? res : SG_LIB_CAT_OTHER;
}
|