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
|
/*
iscsi-test tool support
Copyright (C) 2012 by Lee Duncan <leeman.duncan@gmail.com>
Copyright (C) 2014 Ronnie Sahlberg <ronniesahlberg@gmail.com>
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 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#define _GNU_SOURCE
#include <assert.h>
#include <sys/syscall.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <poll.h>
#include <fnmatch.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_SG_IO
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#endif
#include "slist.h"
#include "iscsi.h"
#include "scsi-lowlevel.h"
#include "iscsi-private.h"
#include "iscsi-support.h"
#include "iscsi-multipath.h"
/*****************************************************************
* globals
*****************************************************************/
const char *initiatorname1 =
"iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-test";
const char *initiatorname2 =
"iqn.2007-10.com.github:sahlberg:libiscsi:iscsi-test-2";
int no_medium_ascqs[3] = {
SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT,
SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN,
SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED
};
int lba_oob_ascqs[1] = {
SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE
};
int invalid_cdb_ascqs[2] = {
SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB,
SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST
};
int param_list_len_err_ascqs[1] = {
SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR
};
int write_protect_ascqs[3] = {
SCSI_SENSE_ASCQ_WRITE_PROTECTED,
SCSI_SENSE_ASCQ_HARDWARE_WRITE_PROTECTED,
SCSI_SENSE_ASCQ_SOFTWARE_WRITE_PROTECTED
};
int sanitize_ascqs[1] = {
SCSI_SENSE_ASCQ_SANITIZE_IN_PROGRESS
};
int removal_ascqs[1] = {
SCSI_SENSE_ASCQ_MEDIUM_REMOVAL_PREVENTED
};
int miscompare_ascqs[1] = {
SCSI_SENSE_ASCQ_MISCOMPARE_DURING_VERIFY
};
int too_many_desc_ascqs[2] = {
SCSI_SENSE_ASCQ_TOO_MANY_TARGET_DESCRIPTORS,
SCSI_SENSE_ASCQ_TOO_MANY_SEGMENT_DESCRIPTORS,
};
int unsupp_desc_code_ascqs[2] = {
SCSI_SENSE_ASCQ_UNSUPPORTED_TARGET_DESCRIPTOR_TYPE_CODE,
SCSI_SENSE_ASCQ_UNSUPPORTED_SEGMENT_DESCRIPTOR_TYPE_CODE
};
int copy_aborted_ascqs[3] = {
SCSI_SENSE_ASCQ_NO_ADDL_SENSE,
SCSI_SENSE_ASCQ_UNREACHABLE_COPY_TARGET,
SCSI_SENSE_ASCQ_COPY_TARGET_DEVICE_NOT_REACHABLE
};
struct scsi_inquiry_standard *inq;
struct scsi_inquiry_logical_block_provisioning *inq_lbp;
struct scsi_inquiry_block_device_characteristics *inq_bdc;
struct scsi_inquiry_block_limits *inq_bl;
struct scsi_readcapacity16 *rc16;
struct scsi_report_supported_op_codes *rsop;
unsigned char *scratch;
size_t block_size;
uint64_t num_blocks;
int lbppb;
enum scsi_inquiry_peripheral_device_type device_type;
int data_loss;
int allow_sanitize;
int readonly;
int sbc3_support;
int maximum_transfer_length;
static const unsigned char zeroBlock[4096];
/**
* Returns 1 if and only if buf[0..size-1] is zero.
*/
int all_zero(const unsigned char *buf, unsigned size)
{
unsigned j, e;
for (j = 0; j < size; j += e) {
e = size - j;
if (e > sizeof(zeroBlock))
e = sizeof(zeroBlock);
if (memcmp(buf + j, zeroBlock, e) != 0)
return 0;
}
return 1;
}
static const char *scsi_status_str(int status)
{
switch(status) {
case SCSI_STATUS_GOOD: return "SUCCESS";
case SCSI_STATUS_TIMEOUT: return "TIMEOUT";
case SCSI_STATUS_CHECK_CONDITION: return "CHECK_CONDITION";
case SCSI_STATUS_CONDITION_MET: return "CONDITIONS_MET";
case SCSI_STATUS_BUSY: return "BUSY";
case SCSI_STATUS_RESERVATION_CONFLICT: return "RESERVATION_CONFLICT";
case SCSI_STATUS_TASK_SET_FULL: return "TASK_SET_FULL";
case SCSI_STATUS_ACA_ACTIVE: return "ACA_ACTIVE";
case SCSI_STATUS_TASK_ABORTED: return "TASK_ABORTED";
}
return "UNKNOWN";
}
/*
* There is no agreement among the T10 committee whether a SCSI target should
* report "invalid opcode", "invalid field in CDB" or "invalid field in
* parameter list" if the opcode consists of two bytes. Hence accept all three
* sense codes for two-byte opcodes. For more information see also Frederick
* Knight, RE: INVALID COMMAND OPERATION CODE, T10 Reflector, 16 May 2008
* (http://t10.org/ftp/t10/t10r/2008/r0805167.htm).
*/
static int status_is_invalid_opcode(struct scsi_task *task)
{
if (task->status == SCSI_STATUS_CHECK_CONDITION
&& task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST) {
if (task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE)
return 1;
switch (task->cdb[0]) {
case SCSI_OPCODE_MAINTENANCE_IN:
case SCSI_OPCODE_SERVICE_ACTION_IN:
switch (task->sense.ascq) {
case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB:
case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST:
return !task->sense.sense_specific ||
task->sense.field_pointer == 1;
}
}
}
return 0;
}
static int check_result(const char *opcode, struct scsi_device *sdev,
struct scsi_task *task,
int status, enum scsi_sense_key key,
int *ascq, int num_ascq)
{
int ascq_ok = 0;
if (task == NULL) {
logging(LOG_NORMAL, "[FAILED] Failed to send %s command: "
"%s", opcode, sdev->error_str);
return -1;
}
if (status_is_invalid_opcode(task)) {
logging(LOG_NORMAL, "[SKIPPED] %s is not implemented.",
opcode);
return -2;
}
if (status == SCSI_STATUS_GOOD && task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] %s command failed with status 0x%x / sense key %s(0x%02x) / ASCQ %s(0x%04x)",
opcode, task->status,
scsi_sense_key_str(task->sense.key), task->sense.key,
scsi_sense_ascq_str(task->sense.ascq), task->sense.ascq);
return -1;
}
if (status != SCSI_STATUS_GOOD && task->status == SCSI_STATUS_GOOD) {
logging(LOG_NORMAL, "[FAILED] %s successful but should "
"have failed with %s(0x%02x)/%s(0x%04x)",
opcode,
scsi_sense_key_str(key), key,
num_ascq ? scsi_sense_ascq_str(ascq[0]) : "NO ASCQ",
num_ascq ? ascq[0] : 0);
return -1;
}
if (status == SCSI_STATUS_RESERVATION_CONFLICT
&& task->status != SCSI_STATUS_RESERVATION_CONFLICT) {
logging(LOG_NORMAL, "[FAILED] %s command should have failed "
"with RESERVATION_CONFLICT.", opcode);
return -1;
}
/* did we get any of the expected ASCQs ?*/
if (status == SCSI_STATUS_CHECK_CONDITION) {
int i;
for (i = 0; i < num_ascq; i++) {
if (ascq[i] == task->sense.ascq) {
ascq_ok = 1;
}
}
if (num_ascq == 0) {
ascq_ok = 1;
}
}
if (status == SCSI_STATUS_CHECK_CONDITION &&
(task->status != status
|| task->sense.key != key
|| !ascq_ok)) {
logging(LOG_NORMAL, "[FAILED] %s failed with wrong sense. "
"Should have failed with %s(0x%02x)/%s(0x%04x) "
"but failed with Sense: %s(0x%02x)/(0x%04x)\n",
opcode,
scsi_sense_key_str(key), key,
num_ascq ? scsi_sense_ascq_str(ascq[0]) : "NO ASCQ",
num_ascq ? ascq[0] : 0,
sdev->error_str,
task->sense.key, task->sense.ascq);
return -1;
}
logging(LOG_VERBOSE, "[OK] %s returned %s %s(0x%02x) %s(0x%04x)",
opcode, scsi_status_str(status),
scsi_sense_key_str(task->sense.key), task->sense.key,
scsi_sense_ascq_str(task->sense.ascq), task->sense.ascq);
return 0;
}
#ifdef HAVE_SG_IO
static size_t iov_tot_len(struct scsi_iovec *iov, int niov)
{
size_t len = 0;
int i;
for (i = 0; i < niov; i++)
len += iov[i].iov_len;
return len;
}
static struct scsi_task *
sg_send_scsi_cmd(struct scsi_device *sdev, struct scsi_task *task)
{
sg_io_hdr_t io_hdr;
unsigned char sense[32];
const unsigned int sense_len = sizeof(sense);
char *buf;
memset(sense, 0, sizeof(sense));
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
/* CDB */
io_hdr.cmdp = task->cdb;
io_hdr.cmd_len = task->cdb_size;
/* Where to store the sense_data, if there was an error */
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sense_len;
/*
* Transfer direction, either in or out. Support for bidirectional SCSI
* transfers has been removed from the Linux kernel.
*/
switch (task->xfer_dir) {
case SCSI_XFER_WRITE:
io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
io_hdr.iovec_count = task->iovector_out.niov;
io_hdr.dxferp = task->iovector_out.iov;
io_hdr.dxfer_len = iov_tot_len(task->iovector_out.iov,
task->iovector_out.niov);
break;
case SCSI_XFER_READ:
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
task->datain.size = task->expxferlen;
task->datain.data = malloc(task->datain.size);
memset(task->datain.data, 0, task->datain.size);
io_hdr.dxferp = task->datain.data;
io_hdr.dxfer_len = task->datain.size;
break;
}
/* SCSI timeout in ms */
io_hdr.timeout = 5000;
if (ioctl(sdev->sgio_fd, SG_IO, &io_hdr) < 0) {
int err = errno;
free(sdev->error_str);
if (asprintf(&sdev->error_str, "SG_IO ioctl failed: %s",
strerror(err)) < 0)
sdev->error_str = NULL;
return NULL;
}
task->residual_status = SCSI_RESIDUAL_NO_RESIDUAL;
task->residual = 0;
if (io_hdr.resid) {
task->residual_status = SCSI_RESIDUAL_UNDERFLOW;
task->residual = io_hdr.resid;
}
if (task->xfer_dir == SCSI_XFER_READ)
task->datain.size -= task->residual;
/* now for the error processing */
if (io_hdr.sb_len_wr > 0) {
task->status = SCSI_STATUS_CHECK_CONDITION;
scsi_parse_sense_data(&task->sense, sense);
if (asprintf(&buf, "SENSE KEY:%s(%d) ASCQ:%s(0x%04x)",
scsi_sense_key_str(task->sense.key),
task->sense.key,
scsi_sense_ascq_str(task->sense.ascq),
task->sense.ascq) < 0)
buf = NULL;
free(sdev->error_str);
sdev->error_str = buf;
return task;
}
if (io_hdr.status == SCSI_STATUS_RESERVATION_CONFLICT) {
task->status = SCSI_STATUS_RESERVATION_CONFLICT;
free(sdev->error_str);
sdev->error_str = strdup("Reservation Conflict");
return task;
}
if (io_hdr.masked_status) {
task->status = SCSI_STATUS_ERROR;
task->sense.key = 0x0f;
task->sense.ascq = 0xffff;
free(sdev->error_str);
sdev->error_str = strdup("SCSI masked error");
return NULL;
}
if (io_hdr.host_status) {
task->status = SCSI_STATUS_ERROR;
task->sense.key = 0x0f;
task->sense.ascq = 0xffff;
if (asprintf(&buf, "SCSI host error. Status=0x%x",
io_hdr.host_status) < 0)
buf = NULL;
free(sdev->error_str);
sdev->error_str = buf;
return task;
}
if (io_hdr.driver_status) {
task->status = SCSI_STATUS_ERROR;
task->sense.key = 0x0f;
task->sense.ascq = 0xffff;
free(sdev->error_str);
sdev->error_str = strdup("SCSI driver error");
return NULL;
}
return task;
}
#endif
static struct scsi_task *send_scsi_command(struct scsi_device *sdev, struct scsi_task *task, struct iscsi_data *d)
{
static time_t last_time = 0;
/* We got an actual buffer from the application. Convert it to
* a data-out iovector.
* We need to attach the iovector to the task before calling
* iscsi_scsi_command_sync() as iSER needs all iovectors to be setup
* before we queue the task.
*/
if (d != NULL && d->data != NULL) {
struct scsi_iovec *iov;
iov = scsi_malloc(task, sizeof(struct scsi_iovec));
iov->iov_base = d->data;
iov->iov_len = d->size;
switch (task->xfer_dir) {
case SCSI_XFER_WRITE:
scsi_task_set_iov_out(task, iov, 1);
break;
case SCSI_XFER_READ:
scsi_task_set_iov_in(task, iov, 1);
break;
}
}
if (sdev->iscsi_url) {
time_t current_time = time(NULL);
free(sdev->error_str);
sdev->error_str = NULL;
task = iscsi_scsi_command_sync(sdev->iscsi_ctx, sdev->iscsi_lun, task, NULL);
if (task == NULL) {
sdev->error_str = strdup(iscsi_get_error(sdev->iscsi_ctx));
}
if (current_time > last_time + 1) {
int i;
/* Device [0] is where we are doing all the I/O
* so it will always work the socket and respond
* to NOPs from the target.
* But we need to trigger a service event every
* now and then on all the other devices to ensure that
* we detect and respond to any NOPs.
*/
for (i = 1; i < mp_num_sds; i++) {
iscsi_service(mp_sds[i]->iscsi_ctx, POLLIN|POLLOUT);
}
last_time = current_time;
}
return task;
}
#ifdef HAVE_SG_IO
if (sdev->sgio_dev)
return sg_send_scsi_cmd(sdev, task);
#endif
return NULL;
}
void logging(int level, const char *format, ...)
{
va_list ap;
static char message[1024];
int ret;
if (loglevel < level) {
return;
}
if (strncmp(LOG_BLANK_LINE, format, LOG_BLANK_LINE_CMP_LEN)==0) {
printf("\n");
return;
}
va_start(ap, format);
ret = vsnprintf(message, 1024, format, ap);
va_end(ap);
if (ret < 0) {
return;
}
printf(" %s\n", message);
}
struct iscsi_context *
iscsi_context_login(const char *initiatorname, const char *url, int *lun)
{
struct iscsi_context *iscsi;
struct iscsi_url *iscsi_url;
iscsi = iscsi_create_context(initiatorname);
if (iscsi == NULL) {
fprintf(stderr, "Failed to create context\n");
return NULL;
}
iscsi_url = iscsi_parse_full_url(iscsi, url);
if (iscsi_url == NULL) {
fprintf(stderr, "Failed to parse URL: %s\n",
iscsi_get_error(iscsi));
iscsi_destroy_context(iscsi);
return NULL;
}
iscsi_set_targetname(iscsi, iscsi_url->target);
iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
if (iscsi_url->user[0] != '\0') {
if (iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, iscsi_url->passwd) != 0) {
fprintf(stderr, "Failed to set initiator username and password\n");
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
return NULL;
}
}
if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
fprintf(stderr, "Login Failed. %s\n", iscsi_get_error(iscsi));
iscsi_destroy_url(iscsi_url);
iscsi_destroy_context(iscsi);
return NULL;
}
if (lun != NULL) {
*lun = iscsi_url->lun;
}
iscsi_destroy_url(iscsi_url);
return iscsi;
}
void
wait_until_test_finished(struct iscsi_context *iscsi, struct iscsi_async_state *state)
{
struct pollfd pfd;
int count = 0;
int ret;
while (state->finished == 0) {
pfd.fd = iscsi_get_fd(iscsi);
pfd.events = iscsi_which_events(iscsi);
ret = poll(&pfd, 1, 1000);
if (ret < 0) {
printf("Poll failed");
exit(10);
}
if (ret == 0) {
if (count++ > 5) {
struct iscsi_pdu *pdu;
state->finished = 1;
state->status = SCSI_STATUS_CANCELLED;
state->task->status = SCSI_STATUS_CANCELLED;
/* this may leak memory since we don't free the pdu */
while ((pdu = iscsi->outqueue)) {
ISCSI_LIST_REMOVE(&iscsi->outqueue, pdu);
}
while ((pdu = iscsi->waitpdu)) {
ISCSI_LIST_REMOVE(&iscsi->waitpdu, pdu);
}
return;
}
continue;
}
if (iscsi_service(iscsi, pfd.revents) < 0) {
printf("iscsi_service failed with : %s\n", iscsi_get_error(iscsi));
break;
}
}
}
int
orwrite(struct scsi_device *sdev, uint64_t lba,
uint32_t datalen, int blocksize, int wrprotect,
int dpo, int fua, int fua_nv, int group,
unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send ORWRITE (Expecting %s) LBA:%" PRIu64
" blocks:%d wrprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, fua, fua_nv, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_orwrite(lba, datalen, blocksize, wrprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("ORWRITE", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
prin_task(struct scsi_device *sdev, int service_action,
int success_expected)
{
const int buf_sz = 16384;
struct scsi_task *task;
int ret = 0;
logging(LOG_VERBOSE, "Send PRIN/SA=0x%02x, expect %s", service_action,
success_expected ? "success" : "failure");
task = scsi_cdb_persistent_reserve_in(service_action, buf_sz);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if (success_expected) {
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN/SA=0x%x failed: %s",
service_action, iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
} else {
if (task->status == SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN/SA=0x%x succeeded with invalid serviceaction",
service_action);
ret = -1;
}
}
scsi_free_scsi_task(task);
task = NULL;
return ret;
}
int
prin_read_keys(struct scsi_device *sdev,
struct scsi_task **tp,
struct scsi_persistent_reserve_in_read_keys **rkp,
uint16_t allocation_len)
{
struct scsi_persistent_reserve_in_read_keys *rk = NULL;
logging(LOG_VERBOSE, "Send PRIN/READ_KEYS");
*tp = scsi_cdb_persistent_reserve_in(SCSI_PERSISTENT_RESERVE_READ_KEYS,
allocation_len);
assert(*tp != NULL);
*tp = send_scsi_command(sdev, *tp, NULL);
if (*tp == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(*tp)) {
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if ((*tp)->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
rk = scsi_datain_unmarshall(*tp);
if (rk == NULL) {
logging(LOG_NORMAL,
"[FAIL] failed to unmarshall PRIN/READ_KEYS data. %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (rkp != NULL)
*rkp = rk;
return 0;
}
int
prout_register_and_ignore(struct scsi_device *sdev,
unsigned long long sark)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
/* register our reservation key with the target */
logging(LOG_VERBOSE,
"Send PROUT/REGISTER_AND_IGNORE to register init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.service_action_reservation_key = sark;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_REGISTER_AND_IGNORE_EXISTING_KEY,
SCSI_PERSISTENT_RESERVE_SCOPE_LU, 0, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
logging(LOG_NORMAL, "[SKIPPED] PROUT Not Supported");
ret = -2;
goto dun;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
dun:
scsi_free_scsi_task(task);
return ret;
}
int
prout_register_key(struct scsi_device *sdev,
unsigned long long sark, unsigned long long rk)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
/* register/unregister our reservation key with the target */
logging(LOG_VERBOSE, "Send PROUT/REGISTER to %s init=%s",
sark != 0 ? "register" : "unregister",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.service_action_reservation_key = sark;
poc.reservation_key = rk;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_REGISTER,
SCSI_PERSISTENT_RESERVE_SCOPE_LU, 0, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prin_verify_key_presence(struct scsi_device *sdev,
unsigned long long key, int present)
{
struct scsi_task *task;
const int buf_sz = 16384;
int i;
int key_found;
struct scsi_persistent_reserve_in_read_keys *rk = NULL;
int ret = 0;
logging(LOG_VERBOSE,
"Send PRIN/READ_KEYS to verify key %s init=%s... ",
present ? "present" : "absent",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_persistent_reserve_in(SCSI_PERSISTENT_RESERVE_READ_KEYS,
buf_sz);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
rk = scsi_datain_unmarshall(task);
if (rk == NULL) {
logging(LOG_NORMAL,
"[FAILED] failed to unmarshall PRIN/READ_KEYS data. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
key_found = 0;
for (i = 0; i < rk->num_keys; i++) {
if (rk->keys[i] == key)
key_found = 1;
}
if ((present && !key_found) || (!present && key_found)) {
if (present)
logging(LOG_NORMAL,
"[FAILED] Key found when none expected");
else
logging(LOG_NORMAL,
"[FAILED] Key not found when expected");
ret = -1;
}
dun:
scsi_free_scsi_task(task);
return ret;
}
int
prout_reregister_key_fails(struct scsi_device *sdev,
unsigned long long sark)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
logging(LOG_VERBOSE,
"Send PROUT/REGISTER to ensure reregister fails init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.service_action_reservation_key = sark;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_REGISTER,
SCSI_PERSISTENT_RESERVE_SCOPE_LU, 0, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_RESERVATION_CONFLICT) {
logging(LOG_NORMAL,
"[FAILED] Expected RESERVATION CONFLICT");
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prout_reserve(struct scsi_device *sdev,
unsigned long long key, enum scsi_persistent_out_type pr_type)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
/* reserve the target using specified reservation type */
logging(LOG_VERBOSE,
"Send PROUT/RESERVE to reserve, type=%d (%s) init=%s",
pr_type, scsi_pr_type_str(pr_type),
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.reservation_key = key;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_RESERVE,
SCSI_PERSISTENT_RESERVE_SCOPE_LU,
pr_type, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prout_release(struct scsi_device *sdev,
unsigned long long key, enum scsi_persistent_out_type pr_type)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
logging(LOG_VERBOSE,
"Send PROUT/RELEASE to release reservation, type=%d init=%s",
pr_type, sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.reservation_key = key;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_RELEASE,
SCSI_PERSISTENT_RESERVE_SCOPE_LU,
pr_type, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prout_clear(struct scsi_device *sdev, unsigned long long key)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
/* reserve the target using specified reservation type */
logging(LOG_VERBOSE,
"Send PROUT/CLEAR to clear all registrations and any PR "
"reservation");
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.reservation_key = key;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_CLEAR,
SCSI_PERSISTENT_RESERVE_SCOPE_LU,
0, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prout_preempt(struct scsi_device *sdev,
unsigned long long sark, unsigned long long rk,
enum scsi_persistent_out_type pr_type)
{
struct scsi_persistent_reserve_out_basic poc;
struct scsi_task *task;
int ret = 0;
/* reserve the target using specified reservation type */
logging(LOG_VERBOSE,
"Send PROUT/PREEMPT to preempt reservation and/or "
"registration");
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping PROUT\n");
return -1;
}
memset(&poc, 0, sizeof (poc));
poc.reservation_key = rk;
poc.service_action_reservation_key = sark;
task = scsi_cdb_persistent_reserve_out(
SCSI_PERSISTENT_RESERVE_PREEMPT,
SCSI_PERSISTENT_RESERVE_SCOPE_LU,
pr_type, &poc);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PROUT command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE OUT is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PROUT command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
prin_verify_reserved_as(struct scsi_device *sdev,
unsigned long long key, enum scsi_persistent_out_type pr_type)
{
struct scsi_task *task;
const int buf_sz = 16384;
struct scsi_persistent_reserve_in_read_reservation *rr = NULL;
int ret = 0;
logging(LOG_VERBOSE,
"Send PRIN/READ_RESERVATION to verify type=%d init=%s... ",
pr_type, sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_persistent_reserve_in(
SCSI_PERSISTENT_RESERVE_READ_RESERVATION, buf_sz);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN command: failed with sense: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
rr = scsi_datain_unmarshall(task);
if (rr == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to unmarshall PRIN/READ_RESERVATION data. %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
if (!rr->reserved) {
logging(LOG_NORMAL,
"[FAILED] Failed to find Target reserved as expected.");
ret = -1;
goto dun;
}
if (rr->reservation_key != key) {
logging(LOG_NORMAL,
"[FAILED] Failed to find reservation key 0x%llx: found 0x%"
PRIu64 ".",
key, rr->reservation_key);
ret = -1;
goto dun;
}
if (rr->pr_type != pr_type) {
logging(LOG_NORMAL,
"Failed to find reservation type %d: found %d.",
pr_type, rr->pr_type);
return -1;
ret = -1;
goto dun;
}
dun:
/* ??? free rr? */
scsi_free_scsi_task(task);
return ret;
}
int
prin_verify_not_reserved(struct scsi_device *sdev)
{
struct scsi_task *task;
const int buf_sz = 16384;
struct scsi_persistent_reserve_in_read_reservation *rr = NULL;
int ret = 0;
logging(LOG_VERBOSE,
"Send PRIN/READ_RESERVATION to verify not reserved init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_persistent_reserve_in(
SCSI_PERSISTENT_RESERVE_READ_RESERVATION, buf_sz);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
scsi_free_scsi_task(task);
logging(LOG_NORMAL, "[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN command: failed with sense: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
rr = scsi_datain_unmarshall(task);
if (rr == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to unmarshall PRIN/READ_RESERVATION data: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
if (rr->reserved) {
logging(LOG_NORMAL,
"[FAILED] Failed to find Target not reserved as expected.");
ret = -1;
goto dun;
}
dun:
/* ??? free rr? */
scsi_free_scsi_task(task);
return ret;
}
int
prin_report_caps(struct scsi_device *sdev, struct scsi_task **tp,
struct scsi_persistent_reserve_in_report_capabilities **_rcaps)
{
const int buf_sz = 16384;
struct scsi_persistent_reserve_in_report_capabilities *rcaps = NULL;
logging(LOG_VERBOSE, "Send PRIN/REPORT_CAPABILITIES");
*tp = scsi_cdb_persistent_reserve_in(
SCSI_PERSISTENT_RESERVE_REPORT_CAPABILITIES,
buf_sz);
assert(*tp != NULL);
*tp = send_scsi_command(sdev, *tp, NULL);
if (*tp == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send PRIN command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(*tp)) {
logging(LOG_NORMAL,
"[SKIPPED] PERSISTENT RESERVE IN is not implemented.");
return -2;
}
if ((*tp)->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] PRIN command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
rcaps = scsi_datain_unmarshall(*tp);
if (rcaps == NULL) {
logging(LOG_NORMAL,
"[FAIL] failed to unmarshall PRIN/REPORT_CAPABILITIES "
"data. %s", iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (_rcaps != NULL)
*_rcaps = rcaps;
return 0;
}
int
verify_read_works(struct scsi_device *sdev, unsigned char *buf)
{
struct scsi_task *task;
const uint32_t lba = 1;
const uint32_t datalen = 1 * block_size;
int ret = 0;
/*
* try to read the second block
*/
logging(LOG_VERBOSE, "Send READ10 to verify READ works init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_read10(lba, datalen, block_size, 0, 0, 0, 0, 0);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send READ10 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] READ10 command: failed with sense: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
goto dun;
}
memcpy(buf, task->datain.data, task->datain.size);
dun:
scsi_free_scsi_task(task);
return ret;
}
int
verify_write_works(struct scsi_device *sdev, unsigned char *buf)
{
struct scsi_task *task;
struct iscsi_data d;
const uint32_t lba = 1;
const uint32_t datalen = 1 * block_size;
int ret = 0;
/*
* try to write the second block
*/
logging(LOG_VERBOSE, "Send WRITE10 to verify WRITE works init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_write10(lba, datalen, block_size, 0, 0, 0, 0, 0);
assert(task != NULL);
d.data = buf;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send WRITE10 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] WRITE10 command: failed with sense: %s",
iscsi_get_error(sdev->iscsi_ctx));
ret = -1;
}
scsi_free_scsi_task(task);
return ret;
}
int
verify_read_fails(struct scsi_device *sdev, unsigned char *buf)
{
struct scsi_task *task;
const uint32_t lba = 1;
const uint32_t datalen = 1 * block_size;
int ret = 0;
/*
* try to read the second block -- should fail
*/
logging(LOG_VERBOSE,
"Send READ10 to verify READ does not work init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_read10(lba, datalen, block_size, 0, 0, 0, 0, 0);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send READ10 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (task->status == SCSI_STATUS_GOOD) {
memcpy(buf, task->datain.data, task->datain.size);
logging(LOG_NORMAL,
"[FAILED] READ10 command succeeded when expected to fail");
ret = -1;
goto dun;
}
/*
* XXX should we verify sense data?
*/
dun:
scsi_free_scsi_task(task);
return ret;
}
int
verify_write_fails(struct scsi_device *sdev, unsigned char *buf)
{
struct scsi_task *task;
struct iscsi_data d;
const uint32_t lba = 1;
const uint32_t datalen = 1 * block_size;
int ret = 0;
/*
* try to write the second block
*/
logging(LOG_VERBOSE,
"Send WRITE10 to verify WRITE does not work init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_write10(lba, datalen, block_size, 0, 0, 0, 0, 0);
assert(task != NULL);
d.data = buf;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send WRITE10 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (task->status == SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[FAILED] WRITE10 command: succeeded when exptec to fail");
ret = -1;
goto dun;
}
/*
* XXX should we verify sense data?
*/
dun:
scsi_free_scsi_task(task);
return ret;
}
int
synchronizecache10(struct scsi_device *sdev, uint32_t lba, int num, int sync_nv, int immed, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send SYNCHRONIZECACHE10 (Expecting %s) LBA:%d"
" blocks:%d sync_nv:%d immed:%d",
scsi_status_str(status),
lba, num, sync_nv, immed);
task = scsi_cdb_synchronizecache10(lba, num_blocks, sync_nv, immed);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("SYNCHRONIZECACHE10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
synchronizecache16(struct scsi_device *sdev, uint64_t lba, int num, int sync_nv, int immed, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send SYNCHRONIZECACHE16 (Expecting %s) LBA:%"
PRIu64 " blocks:%d sync_nv:%d immed:%d",
scsi_status_str(status),
lba, num, sync_nv, immed);
task = scsi_cdb_synchronizecache16(lba, num_blocks, sync_nv, immed);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("SYNCHRONIZECACHE16", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int sanitize(struct scsi_device *sdev, int immed, int ause, int sa, int param_len, struct iscsi_data *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send SANITIZE (Expecting %s) IMMED:%d AUSE:%d "
"SA:%d PARAM_LEN:%d",
scsi_status_str(status),
immed, ause, sa, param_len);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping sanitize\n");
return -1;
}
task = scsi_cdb_sanitize(immed, ause, sa, param_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, data);
ret = check_result("SANITIZE", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int startstopunit(struct scsi_device *sdev, int immed, int pcm, int pc, int no_flush, int loej, int start, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send STARTSTOPUNIT (Expecting %s) IMMED:%d "
"PCM:%d PC:%d NO_FLUSH:%d LOEJ:%d START:%d",
scsi_status_str(status),
immed, pcm, pc, no_flush, loej, start);
task = scsi_cdb_startstopunit(immed, pcm, pc, no_flush, loej, start);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("STARTSTOPUNIT", sdev, task, status, key, ascq,
num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
testunitready(struct scsi_device *sdev, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send TESTUNITREADY (Expecting %s)",
scsi_status_str(status));
task = scsi_cdb_testunitready();
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("TESTUNITREADY", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
testunitready_clear_ua(struct scsi_device *sdev)
{
struct scsi_task *task;
int ret = -1;
logging(LOG_VERBOSE,
"Send TESTUNITREADY (To Clear Possible UA) init=%s",
sdev->iscsi_ctx ? sdev->iscsi_ctx->initiator_name : sdev->sgio_dev);
task = scsi_cdb_testunitready();
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send TESTUNITREADY command: %s",
iscsi_get_error(sdev->iscsi_ctx));
goto out;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL,
"[INFO] TESTUNITREADY command: failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
goto out;
}
logging(LOG_VERBOSE, "[OK] TESTUNITREADY does not return unit "
"attention.");
ret = 0;
out:
scsi_free_scsi_task(task);
return ret;
}
/*
* Returns -1 if allocating a SCSI task failed or if a communication error
* occurred and a SCSI status if a SCSI response has been received.
*/
int modesense6(struct scsi_device *sdev, struct scsi_task **out_task, int dbd, enum scsi_modesense_page_control pc, enum scsi_modesense_page_code page_code, int sub_page_code, unsigned char alloc_len, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send MODESENSE6 (Expecting %s) ",
scsi_status_str(status));
task = scsi_cdb_modesense6(dbd, pc, page_code, sub_page_code, alloc_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("MODESENSE6", sdev, task, status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int modeselect6(struct scsi_device *sdev, int pf, int sp, struct scsi_mode_page *mp, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
struct scsi_data *data;
struct iscsi_data d;
logging(LOG_VERBOSE, "Send MODESELECT6 (Expecting %s) ",
scsi_status_str(status));
task = scsi_cdb_modeselect6(pf, sp, 255);
assert(task != NULL);
data = scsi_modesense_dataout_marshall(task, mp, 1);
if (data == NULL) {
logging(LOG_VERBOSE, "Failed to marshall MODESELECT6 data");
scsi_free_scsi_task(task);
return -1;
}
d.data = data->data;
d.size = data->size;
task->cdb[4] = data->size;
task->expxferlen = data->size;
task = send_scsi_command(sdev, task, &d);
ret = check_result("MODESELECT6", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int compareandwrite(struct scsi_device *sdev, uint64_t lba,
unsigned char *data, uint32_t datalen, int blocksize,
int wrprotect, int dpo,
int fua, int group_number,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send COMPAREANDWRITE (Expecting %s) LBA:%"
PRIu64 " LEN:%d WRPROTECT:%d",
scsi_status_str(status),
lba, datalen, wrprotect);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_compareandwrite(lba, datalen, blocksize, wrprotect,
dpo, fua, 0, group_number);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("COMPAREANDWRITE", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int get_lba_status(struct scsi_device *sdev, struct scsi_task **out_task, uint64_t lba, uint32_t len, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send GET_LBA_STATUS (Expecting %s) LBA:%" PRIu64
" alloc_len:%d",
scsi_status_str(status),
lba, len);
task = scsi_cdb_get_lba_status(lba, len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("GET_LBA_STATUS", sdev, task, status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
prefetch10(struct scsi_device *sdev, uint32_t lba, int num, int immed, int group, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send PREFETCH10 (Expecting %s) LBA:%d blocks:%d"
" immed:%d group:%d",
scsi_status_str(status),
lba, num, immed, group);
task = scsi_cdb_prefetch10(lba, num, immed, group);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("PREFETCH10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
prefetch16(struct scsi_device *sdev, uint64_t lba, int num, int immed, int group, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send PREFETCH16 (Expecting %s) LBA:%" PRIu64
" blocks:%d immed:%d group:%d",
scsi_status_str(status),
lba, num, immed, group);
task = scsi_cdb_prefetch16(lba, num, immed, group);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("PREFETCH16", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
preventallow(struct scsi_device *sdev, int prevent)
{
struct scsi_task *task;
logging(LOG_VERBOSE, "Send PREVENTALLOW prevent:%d", prevent);
task = scsi_cdb_preventallow(prevent);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL, "[FAILED] Failed to send PREVENTALLOW "
"command: %s", iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
if (status_is_invalid_opcode(task)) {
logging(LOG_NORMAL, "[SKIPPED] PREVENTALLOW is not implemented on target");
scsi_free_scsi_task(task);
return -2;
}
if (task->status != SCSI_STATUS_GOOD) {
logging(LOG_NORMAL, "[FAILED] PREVENTALLOW command: "
"failed with sense. %s", iscsi_get_error(sdev->iscsi_ctx));
scsi_free_scsi_task(task);
return -1;
}
scsi_free_scsi_task(task);
logging(LOG_VERBOSE, "[OK] PREVENTALLOW returned SUCCESS.");
return 0;
}
int
read6(struct scsi_device *sdev, struct scsi_task **out_task, uint32_t lba,
uint32_t datalen, int blocksize,
unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READ6 (Expecting %s) LBA:%d blocks:%d",
scsi_status_str(status),
lba, datalen / blocksize);
task = scsi_cdb_read6(lba, datalen, blocksize);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READ6", sdev, task, status, key, ascq, num_ascq);
if (data && task) {
memcpy(data, task->datain.data, task->datain.size);
}
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
read10(struct scsi_device *sdev, struct scsi_task **out_task,
uint32_t lba,
uint32_t datalen, int blocksize, int rdprotect,
int dpo, int fua, int fua_nv, int group,
unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READ10 (Expecting %s) LBA:%d"
" blocks:%d rdprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, rdprotect,
dpo, fua, fua_nv, group);
task = scsi_cdb_read10(lba, datalen, blocksize, rdprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READ10", sdev, task, status, key, ascq, num_ascq);
if (data && task) {
memcpy(data, task->datain.data, task->datain.size);
}
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
read12(struct scsi_device *sdev, struct scsi_task **out_task,
uint32_t lba,
uint32_t datalen, int blocksize, int rdprotect,
int dpo, int fua, int fua_nv, int group,
unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READ12 (Expecting %s) LBA:%d"
" blocks:%d rdprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, rdprotect,
dpo, fua, fua_nv, group);
task = scsi_cdb_read12(lba, datalen, blocksize, rdprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READ12", sdev, task, status, key, ascq, num_ascq);
if (data && task) {
memcpy(data, task->datain.data, task->datain.size);
}
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
read16(struct scsi_device *sdev, struct scsi_task **out_task,
uint64_t lba,
uint32_t datalen, int blocksize, int rdprotect,
int dpo, int fua, int fua_nv, int group,
unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READ16 (Expecting %s) LBA:%" PRIu64
" blocks:%d rdprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, rdprotect,
dpo, fua, fua_nv, group);
task = scsi_cdb_read16(lba, datalen, blocksize, rdprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READ16", sdev, task, status, key, ascq, num_ascq);
if (data && task) {
memcpy(data, task->datain.data, task->datain.size);
}
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
readcapacity10(struct scsi_device *sdev, struct scsi_task **out_task, uint32_t lba, int pmi, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READCAPACITY10 (Expecting %s) LBA:%d"
" pmi:%d",
scsi_status_str(status),
lba, pmi);
task = scsi_cdb_readcapacity10(lba, pmi);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READCAPACITY10", sdev, task, status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
readcapacity16(struct scsi_device *sdev, struct scsi_task **out_task, int alloc_len, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READCAPACITY16 (Expecting %s)",
scsi_status_str(status));
task = scsi_cdb_serviceactionin16(SCSI_READCAPACITY16, alloc_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READCAPACITY16", sdev, task, status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
readdefectdata10(struct scsi_device *sdev, struct scsi_task **out_task,
int req_plist, int req_glist,
int defect_list_format, uint16_t alloc_len,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READDEFECTDATA10 (Expecting %s)"
" req_plist:%d req_glist:%d format:%d",
scsi_status_str(status),
req_plist, req_glist, defect_list_format);
task = scsi_cdb_readdefectdata10(req_plist, req_glist,
defect_list_format, alloc_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READDEFECTDATA10", sdev, task,
status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
readdefectdata12(struct scsi_device *sdev, struct scsi_task **out_task,
int req_plist, int req_glist,
int defect_list_format,
uint32_t address_descriptor_index,
uint32_t alloc_len,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send READDEFECTDATA12 (Expecting %s)"
" req_plist:%d req_glist:%d format:%d",
scsi_status_str(status),
req_plist, req_glist, defect_list_format);
task = scsi_cdb_readdefectdata12(req_plist, req_glist,
defect_list_format,
address_descriptor_index,
alloc_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("READDEFECTDATA12", sdev, task,
status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
release6(struct scsi_device *sdev)
{
struct scsi_task *task;
int i, res = -1;
logging(LOG_VERBOSE, "Send RELEASE6");
for (i = 0; i < 3 && res != 0; ++i) {
task = scsi_cdb_release6();
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send RELEASE6 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
res = -1;
break;
}
if (task->status != SCSI_STATUS_GOOD &&
!(task->status == SCSI_STATUS_CHECK_CONDITION
&& task->sense.key == SCSI_SENSE_UNIT_ATTENTION
&& task->sense.ascq == SCSI_SENSE_ASCQ_BUS_RESET)) {
logging(LOG_NORMAL, "[FAILED] RELEASE6 command: "
"failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
res = -1;
} else {
res = 0;
}
scsi_free_scsi_task(task);
}
if (res == 0)
logging(LOG_VERBOSE, "[OK] RELEASE6 returned SUCCESS.");
return res;
}
int report_supported_opcodes(struct scsi_device *sdev, struct scsi_task **out_task, int rctd, int options, int opcode, int sa, int alloc_len, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send REPORT_SUPPORTED_OPCODE (Expecting %s) "
"RCTD:%d OPTIONS:%d OPCODE:0x%02x SA:%d ALLOC_LEN:%d",
scsi_status_str(status),
rctd, options, opcode, sa, alloc_len);
task = scsi_cdb_report_supported_opcodes(rctd, options, opcode, sa,
alloc_len);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("REPORT_SUPPORTED_OPCODES", sdev, task, status, key, ascq, num_ascq);
if (out_task && ret != -2 /* Not Supported */) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
reserve6(struct scsi_device *sdev)
{
struct scsi_task *task;
int i, res = -1;
logging(LOG_VERBOSE, "Send RESERVE6");
for (i = 0; i < 3 && res != 0; ++i) {
task = scsi_cdb_reserve6();
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send RESERVE6 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
res = -1;
break;
}
if (status_is_invalid_opcode(task)) {
logging(LOG_NORMAL, "[SKIPPED] RESERVE6 is not "
"implemented on target");
res = -2;
} else if (task->status != SCSI_STATUS_GOOD &&
!(task->status == SCSI_STATUS_CHECK_CONDITION
&& task->sense.key == SCSI_SENSE_UNIT_ATTENTION
&& task->sense.ascq == SCSI_SENSE_ASCQ_BUS_RESET)) {
logging(LOG_NORMAL, "[FAILED] RESERVE6 command: "
"failed with sense. %s",
iscsi_get_error(sdev->iscsi_ctx));
res = -1;
} else {
res = 0;
}
scsi_free_scsi_task(task);
}
if (res == 0)
logging(LOG_VERBOSE, "[OK] RESERVE6 returned SUCCESS.");
return res;
}
int
reserve6_conflict(struct scsi_device *sdev)
{
struct scsi_task *task;
int i, res = -1;
logging(LOG_VERBOSE, "Send RESERVE6 (Expecting RESERVATION_CONFLICT)");
for (i = 0; i < 3 && res != 0; ++i) {
task = scsi_cdb_reserve6();
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
if (task == NULL) {
logging(LOG_NORMAL,
"[FAILED] Failed to send RESERVE6 command: %s",
iscsi_get_error(sdev->iscsi_ctx));
res = -1;
break;
}
if (status_is_invalid_opcode(task)) {
logging(LOG_NORMAL, "[SKIPPED] RESERVE6 is not"
" implemented on target");
res = -2;
} else if (task->status != SCSI_STATUS_RESERVATION_CONFLICT &&
!(task->status == SCSI_STATUS_CHECK_CONDITION
&& task->sense.key == SCSI_SENSE_UNIT_ATTENTION
&& task->sense.ascq == SCSI_SENSE_ASCQ_BUS_RESET)) {
logging(LOG_NORMAL, "[FAILED] RESERVE6 command: "
"should have failed with RESERVATION_CONFLICT");
res = -1;
} else {
res = 0;
}
scsi_free_scsi_task(task);
}
if (res == 0)
logging(LOG_VERBOSE,
"[OK] RESERVE6 returned RESERVATION_CONFLICT.");
return res;
}
int
unmap(struct scsi_device *sdev, int anchor, struct unmap_list *list, int list_len, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
unsigned char *data;
struct iscsi_data d;
int xferlen;
int i;
int ret;
logging(LOG_VERBOSE, "Send UNMAP (Expecting %s) list_len:%d anchor:%d",
scsi_status_str(status),
list_len, anchor);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping unmap\n");
return -1;
}
xferlen = 8 + list_len * 16;
task = scsi_cdb_unmap(anchor, 0, xferlen);
assert(task != NULL);
data = scsi_malloc(task, xferlen);
if (data == NULL) {
logging(LOG_NORMAL, "Out-of-memory: Failed to create "
"unmap parameters.");
scsi_free_scsi_task(task);
return -1;
}
scsi_set_uint16(&data[0], xferlen - 2);
scsi_set_uint16(&data[2], xferlen - 8);
for (i = 0; i < list_len; i++) {
scsi_set_uint32(&data[8 + 16 * i], list[i].lba >> 32);
scsi_set_uint32(&data[8 + 16 * i + 4], list[i].lba & 0xffffffff);
scsi_set_uint32(&data[8 + 16 * i + 8], list[i].num);
}
d.data = data;
d.size = xferlen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("UNMAP", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
verify10(struct scsi_device *sdev, uint32_t lba, uint32_t datalen, int blocksize, int vprotect, int dpo, int bytchk, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send VERIFY10 (Expecting %s) LBA:%d "
"blocks:%d vprotect:%d dpo:%d bytchk:%d",
scsi_status_str(status),
lba, datalen / blocksize, vprotect, dpo, bytchk);
task = scsi_cdb_verify10(lba, datalen, vprotect, dpo, bytchk, blocksize);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("VERIFY10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
verify12(struct scsi_device *sdev, uint32_t lba, uint32_t datalen, int blocksize, int vprotect, int dpo, int bytchk, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send VERIFY12 (Expecting %s) LBA:%d "
"blocks:%d vprotect:%d dpo:%d bytchk:%d",
scsi_status_str(status),
lba, datalen / blocksize, vprotect, dpo, bytchk);
task = scsi_cdb_verify12(lba, datalen, vprotect, dpo, bytchk, blocksize);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("VERIFY12", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
verify16(struct scsi_device *sdev, uint64_t lba, uint32_t datalen, int blocksize, int vprotect, int dpo, int bytchk, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send VERIFY16 (Expecting %s) LBA:%" PRIu64
" blocks:%d vprotect:%d dpo:%d bytchk:%d",
scsi_status_str(status),
lba, datalen / blocksize, vprotect, dpo, bytchk);
task = scsi_cdb_verify16(lba, datalen, vprotect, dpo, bytchk, blocksize);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("VERIFY16", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
write10(struct scsi_device *sdev, uint32_t lba, uint32_t datalen, int blocksize, int wrprotect, int dpo, int fua, int fua_nv, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITE10 (Expecting %s) LBA:%d blocks:%d "
"wrprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, fua, fua_nv, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_write10(lba, datalen, blocksize, wrprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITE10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
write12(struct scsi_device *sdev, uint32_t lba, uint32_t datalen, int blocksize, int wrprotect, int dpo, int fua, int fua_nv, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITE12 (Expecting %s) LBA:%d blocks:%d "
"wrprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, fua, fua_nv, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_write12(lba, datalen, blocksize, wrprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITE12", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
write16(struct scsi_device *sdev, uint64_t lba, uint32_t datalen, int blocksize, int wrprotect, int dpo, int fua, int fua_nv, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITE16 (Expecting %s) LBA:%" PRIu64
" blocks:%d wrprotect:%d dpo:%d fua:%d fua_nv:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, fua, fua_nv, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_write16(lba, datalen, blocksize, wrprotect,
dpo, fua, fua_nv, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
send_scsi_command(sdev, task, &d);
ret = check_result("WRITE16", sdev, task, status, key, ascq, num_ascq);
scsi_free_scsi_task(task);
return ret;
}
int
writeatomic16(struct scsi_device *sdev, uint64_t lba, uint32_t datalen, int blocksize, int wrprotect, int dpo, int fua, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITEATOMIC16 (Expecting %s) LBA:%" PRIu64
" blocks:%d wrprotect:%d dpo:%d fua:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, fua, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writeatomic16(lba, datalen, blocksize, wrprotect,
dpo, fua, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
send_scsi_command(sdev, task, &d);
ret = check_result("WRITEATOMIC16", sdev, task, status, key, ascq, num_ascq);
scsi_free_scsi_task(task);
return ret;
}
int
writesame10(struct scsi_device *sdev, uint32_t lba, uint32_t datalen, int num, int anchor, int unmap_flag, int wrprotect, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITESAME10 (Expecting %s) LBA:%d blocks:%d "
"wrprotect:%d anchor:%d unmap:%d group:%d",
scsi_status_str(status),
lba, num, wrprotect, anchor, unmap_flag, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writesame10(wrprotect, anchor, unmap_flag, lba, group,
num, datalen);
assert(task != NULL);
if (data != NULL) {
task->expxferlen = datalen;
} else {
task->expxferlen = 0;
task->xfer_dir = SCSI_XFER_NONE;
}
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITESAME10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
writesame16(struct scsi_device *sdev, uint64_t lba, uint32_t datalen, int num, int anchor, int unmap_flag, int wrprotect, int group, unsigned char *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITESAME16 (Expecting %s) LBA:%" PRIu64
" blocks:%d wrprotect:%d anchor:%d unmap:%d group:%d",
scsi_status_str(status),
lba, num, wrprotect, anchor, unmap_flag, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writesame16(wrprotect, anchor, unmap_flag, lba, group,
num, datalen);
assert(task != NULL);
if (data != NULL) {
task->expxferlen = datalen;
} else {
task->expxferlen = 0;
task->xfer_dir = SCSI_XFER_NONE;
}
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITESAME16", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
writeverify10(struct scsi_device *sdev, uint32_t lba,
uint32_t datalen, int blocksize, int wrprotect,
int dpo, int bytchk, int group, unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITEVERIFY10 (Expecting %s) LBA:%d "
"blocks:%d wrprotect:%d dpo:%d bytchk:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, bytchk, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writeverify10(lba, datalen, blocksize, wrprotect,
dpo, bytchk, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITEVERIFY10", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
writeverify12(struct scsi_device *sdev, uint32_t lba,
uint32_t datalen, int blocksize, int wrprotect,
int dpo, int bytchk, int group, unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITEVERIFY12 (Expecting %s) LBA:%d "
"blocks:%d wrprotect:%d dpo:%d bytchk:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, bytchk, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writeverify12(lba, datalen, blocksize, wrprotect,
dpo, bytchk, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITEVERIFY12", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
writeverify16(struct scsi_device *sdev, uint64_t lba,
uint32_t datalen, int blocksize, int wrprotect,
int dpo, int bytchk, int group, unsigned char *data,
int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
struct iscsi_data d;
int ret;
logging(LOG_VERBOSE, "Send WRITEVERIFY16 (Expecting %s) LBA:%" PRIu64
" blocks:%d wrprotect:%d dpo:%d bytchk:%d group:%d",
scsi_status_str(status),
lba, datalen / blocksize, wrprotect,
dpo, bytchk, group);
if (!data_loss) {
printf("--dataloss flag is not set in. Skipping write\n");
return -1;
}
task = scsi_cdb_writeverify16(lba, datalen, blocksize, wrprotect,
dpo, bytchk, group);
assert(task != NULL);
d.data = data;
d.size = datalen;
task = send_scsi_command(sdev, task, &d);
ret = check_result("WRITEVERIFY16", sdev, task, status, key, ascq, num_ascq);
if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
int
inquiry(struct scsi_device *sdev, struct scsi_task **out_task, int evpd, int page_code, int maxsize, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send INQUIRY (Expecting %s) evpd:%d "
"page_code:%02x alloc_len:%d",
scsi_status_str(status),
evpd, page_code, maxsize);
task = scsi_cdb_inquiry(evpd, page_code, maxsize);
assert(task != NULL);
task = send_scsi_command(sdev, task, NULL);
ret = check_result("INQUIRY", sdev, task, status, key, ascq, num_ascq);
if (out_task) {
*out_task = task;
} else if (task) {
scsi_free_scsi_task(task);
}
return ret;
}
struct scsi_command_descriptor *
get_command_descriptor(int opcode, int sa)
{
int i;
if (rsop == NULL) {
return NULL;
}
for (i = 0; i < rsop->num_descriptors; i++) {
if (rsop->descriptors[i].opcode == opcode
&& rsop->descriptors[i].sa == sa) {
return &rsop->descriptors[i];
}
}
return NULL;
}
int set_swp(struct scsi_device *sdev)
{
int ret;
struct scsi_task *sense_task = NULL;
struct scsi_mode_sense *ms;
struct scsi_mode_page *mp;
logging(LOG_VERBOSE, "Read CONTROL page");
/* see if we can even change swp */
ret = modesense6(sdev, &sense_task, 1, SCSI_MODESENSE_PC_CHANGEABLE,
SCSI_MODEPAGE_CONTROL, 0, 255,
EXPECT_STATUS_GOOD);
if (ret) {
logging(LOG_NORMAL, "Failed to read CONTROL mode page.");
goto finished;
}
logging(LOG_VERBOSE, "[SUCCESS] CONTROL page fetched.");
ms = scsi_datain_unmarshall(sense_task);
if (ms == NULL || ms->pages == NULL) {
logging(LOG_NORMAL, "failed to unmarshall mode sense datain "
"blob");
ret = -1;
goto finished;
}
/* if we cannot change swp, we are done here */
if (ms->pages->control.swp == 0) {
logging(LOG_NORMAL, "SWP is not changeable");
ret = -2;
goto finished;
}
/* get the current control page */
ret = modesense6(sdev, &sense_task, 1, SCSI_MODESENSE_PC_CURRENT,
SCSI_MODEPAGE_CONTROL, 0, 255,
EXPECT_STATUS_GOOD);
if (ret) {
logging(LOG_NORMAL, "Failed to read CONTROL mode page.");
goto finished;
}
logging(LOG_VERBOSE, "[SUCCESS] CONTROL page fetched.");
ms = scsi_datain_unmarshall(sense_task);
if (ms == NULL) {
logging(LOG_NORMAL, "failed to unmarshall mode sense datain "
"blob");
ret = -1;
goto finished;
}
mp = scsi_modesense_get_page(ms, SCSI_MODEPAGE_CONTROL, 0);
if (mp == NULL) {
logging(LOG_NORMAL, "failed to read control mode page");
ret = -1;
goto finished;
}
/* For MODE SELECT PS is reserved and hence must be cleared */
mp->ps = 0;
logging(LOG_VERBOSE, "Turn SWP ON");
mp->control.swp = 1;
ret = modeselect6(sdev, 1, 0, mp,
EXPECT_STATUS_GOOD);
if (ret) {
logging(LOG_NORMAL, "Failed to write CONTROL mode page.");
goto finished;
}
logging(LOG_VERBOSE, "[SUCCESS] CONTROL page written.");
finished:
if (sense_task != NULL) {
scsi_free_scsi_task(sense_task);
}
return ret;
}
int clear_swp(struct scsi_device *sdev)
{
int ret;
struct scsi_task *sense_task = NULL;
struct scsi_mode_sense *ms;
struct scsi_mode_page *mp;
logging(LOG_VERBOSE, "Read CONTROL page");
ret = modesense6(sdev, &sense_task, 1, SCSI_MODESENSE_PC_CURRENT,
SCSI_MODEPAGE_CONTROL, 0, 255,
EXPECT_STATUS_GOOD);
if (ret) {
logging(LOG_NORMAL, "Failed to read CONTROL mode page.");
goto finished;
}
logging(LOG_VERBOSE, "[SUCCESS] CONTROL page fetched.");
ms = scsi_datain_unmarshall(sense_task);
if (ms == NULL) {
logging(LOG_NORMAL, "failed to unmarshall mode sense datain "
"blob");
ret = -1;
goto finished;
}
mp = scsi_modesense_get_page(ms, SCSI_MODEPAGE_CONTROL, 0);
if (mp == NULL) {
logging(LOG_NORMAL, "failed to read control mode page");
ret = -1;
goto finished;
}
/* For MODE SELECT PS is reserved and hence must be cleared */
mp->ps = 0;
logging(LOG_VERBOSE, "Turn SWP OFF");
mp->control.swp = 0;
ret = modeselect6(sdev, 1, 0, mp,
EXPECT_STATUS_GOOD);
if (ret) {
logging(LOG_NORMAL, "Failed to write CONTROL mode page.");
goto finished;
}
logging(LOG_VERBOSE, "[SUCCESS] CONTROL page written.");
finished:
if (sense_task != NULL) {
scsi_free_scsi_task(sense_task);
}
return ret;
}
/* Extended Copy */
int extendedcopy(struct scsi_device *sdev, struct iscsi_data *data, int status, enum scsi_sense_key key, int *ascq, int num_ascq)
{
struct scsi_task *task;
int ret;
logging(LOG_VERBOSE, "Send EXTENDED COPY (Expecting %s)",
scsi_status_str(status));
if (!data_loss) {
logging(LOG_NORMAL, "--dataloss flag is not set in. Skipping extendedcopy\n");
return -1;
}
task = scsi_cdb_extended_copy(data->size);
assert(task != NULL);
send_scsi_command(sdev, task, data);
ret = check_result("EXTENDEDCOPY", sdev, task, status, key, ascq, num_ascq);
scsi_free_scsi_task(task);
return ret;
}
/* return the length for a give descriptor type, including any SD header */
int get_desc_len(enum ec_descr_type_code desc_type)
{
int desc_len = 0;
switch (desc_type) {
/* Segment Descriptors */
case BLK_TO_STRM_SEG_DESCR:
case STRM_TO_BLK_SEG_DESCR:
desc_len = 0x14 + SEG_DESC_SRC_INDEX_OFFSET;
break;
case BLK_TO_BLK_SEG_DESCR:
desc_len = 0x18 + SEG_DESC_SRC_INDEX_OFFSET;
break;
case STRM_TO_STRM_SEG_DESCR:
desc_len = 0x10 + SEG_DESC_SRC_INDEX_OFFSET;
break;
case BLK_TO_BLK_OFF_SEG_DESCR:
desc_len = 0x1C + SEG_DESC_SRC_INDEX_OFFSET;
break;
/* Target Descriptors */
case IPV6_TGT_DESCR:
case IP_COPY_SVC_TGT_DESCR:
desc_len = 64;
break;
case IDENT_DESCR_TGT_DESCR:
default:
if (desc_type >= 0xE0 && desc_type <= 0xE9)
desc_len = 32;
}
return desc_len;
}
/*
* Populate a 24 byte SCSI designator. See also Table 529 — Designation
* descriptor in SPC-6.
*/
static int populate_ident_tgt_desc(uint8_t buf[24], struct scsi_device *dev)
{
int ret;
struct scsi_task *inq_di_task = NULL;
struct scsi_inquiry_device_identification *inq_di = NULL;
struct scsi_inquiry_device_designator *desig, *tgt_desig = NULL;
enum scsi_designator_type prev_type = 0;
ret = inquiry(dev, &inq_di_task, 1,
SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION, 255,
EXPECT_STATUS_GOOD);
if (ret < 0 || inq_di_task == NULL) {
logging(LOG_NORMAL,
"Failed to read Device Identification page");
goto err;
} else {
inq_di = scsi_datain_unmarshall(inq_di_task);
if (inq_di == NULL) {
logging(LOG_NORMAL,
"Failed to unmarshall inquiry datain blob");
goto err;
}
}
for (desig = inq_di->designators; desig; desig = desig->next) {
switch (desig->designator_type) {
case SCSI_DESIGNATOR_TYPE_VENDOR_SPECIFIC:
case SCSI_DESIGNATOR_TYPE_T10_VENDORT_ID:
case SCSI_DESIGNATOR_TYPE_EUI_64:
case SCSI_DESIGNATOR_TYPE_NAA:
if (prev_type <= desig->designator_type &&
desig->designator_length <= 20) {
tgt_desig = desig;
prev_type = desig->designator_type;
}
continue;
default:
continue;
}
}
if (tgt_desig == NULL) {
logging(LOG_NORMAL,
"No suitable target descriptor found");
goto err;
}
memset(buf, 0, 24);
buf[0] = tgt_desig->code_set;
buf[1] = (tgt_desig->designator_type & 0xF) |
((tgt_desig->association & 3) << 4);
buf[3] = tgt_desig->designator_length;
memcpy(buf + 4, tgt_desig->designator, tgt_desig->designator_length);
scsi_free_scsi_task(inq_di_task);
return 0;
err:
scsi_free_scsi_task(inq_di_task);
return -1;
}
int populate_tgt_desc(uint8_t desc[32], enum ec_descr_type_code desc_type,
int luid_type, int nul, int peripheral_type,
uint16_t rel_init_port_id, int pad,
struct scsi_device *dev)
{
assert(desc_type < 256);
assert(luid_type < 4);
assert(nul < 2);
assert(peripheral_type < 32);
desc[0] = desc_type;
desc[1] = (luid_type << 6) | (nul << 5) | peripheral_type;
scsi_set_uint16(&desc[2], rel_init_port_id);
if (desc_type == IDENT_DESCR_TGT_DESCR &&
populate_ident_tgt_desc(desc + 4, dev) < 0)
return -1;
if (peripheral_type == 0) {
// Issue read capacity for each sd if testing with different LUs
// If single LU, use block_size from prior readcapacity
// invocation.
desc[28] = pad << 2;
desc[29] = (block_size >> 16) & 0xFF;
desc[30] = (block_size >> 8) & 0xFF;
desc[31] = block_size & 0xFF;
}
return get_desc_len(desc_type);
}
int populate_seg_desc_hdr(unsigned char *hdr, enum ec_descr_type_code desc_type, int dc, int cat, uint16_t src_index, uint16_t dst_index)
{
int desc_len = get_desc_len(desc_type);
hdr[0] = desc_type;
hdr[1] = ((dc << 1) | cat) & 0xFF;
/* don't account for the first 4 bytes in descriptor header */
scsi_set_uint16(&hdr[2], (desc_len - SEG_DESC_SRC_INDEX_OFFSET));
scsi_set_uint16(&hdr[4], src_index);
scsi_set_uint16(&hdr[6], dst_index);
return desc_len;
}
int populate_seg_desc_b2b(unsigned char *desc, int dc, int cat, int src_index, int dst_index, uint16_t num_blks, uint64_t src_lba, uint64_t dst_lba)
{
int desc_len = populate_seg_desc_hdr(desc, BLK_TO_BLK_SEG_DESCR, dc, cat, src_index, dst_index);
scsi_set_uint16(&desc[10], num_blks);
scsi_set_uint64(&desc[12], src_lba);
scsi_set_uint64(&desc[20], dst_lba);
return desc_len;
}
int populate_seg_desc_b2b_off(unsigned char *desc, int cat, int src_index,
int dst_index, uint32_t num_bytes,
uint64_t src_lba, uint64_t dst_lba,
uint16_t src_byte_off, uint16_t dst_byte_off)
{
int desc_len = populate_seg_desc_hdr(desc, BLK_TO_BLK_OFF_SEG_DESCR, 0,
cat, src_index, dst_index);
scsi_set_uint32(&desc[8], num_bytes);
scsi_set_uint64(&desc[12], src_lba);
scsi_set_uint64(&desc[20], dst_lba);
scsi_set_uint16(&desc[28], src_byte_off);
scsi_set_uint16(&desc[30], dst_byte_off);
return desc_len;
}
void populate_param_header(unsigned char *buf, int list_id, int str, int list_id_usage, int prio, int tgt_desc_len, int seg_desc_len, int inline_data_len)
{
buf[0] = list_id;
buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
scsi_set_uint16(&buf[2], tgt_desc_len);
scsi_set_uint32(&buf[8], seg_desc_len);
scsi_set_uint32(&buf[12], inline_data_len);
}
int receive_copy_results(struct scsi_task **task, struct scsi_device *sdev,
enum scsi_copy_results_sa sa, int list_id,
void **datap, int status, enum scsi_sense_key key,
int *ascq, int num_ascq)
{
int ret;
logging(LOG_VERBOSE, "Send RECEIVE COPY RESULTS");
*task = scsi_cdb_receive_copy_results(sa, list_id, 1024);
assert(task != NULL);
*task = send_scsi_command(sdev, *task, NULL);
ret = check_result("RECEIVECOPYRESULT", sdev, *task, status, key, ascq,
num_ascq);
if (ret < 0)
return ret;
if ((*task)->status == SCSI_STATUS_GOOD && datap != NULL) {
*datap = scsi_datain_unmarshall(*task);
if (*datap == NULL) {
logging(LOG_NORMAL,
"[FAIL] failed to unmarshall RECEIVE COPY RESULTS data. %s",
iscsi_get_error(sdev->iscsi_ctx));
return -1;
}
}
return check_result("RECEIVECOPYRESULT", sdev, *task, status, key, ascq,
num_ascq);
}
#define TEST_ISCSI_TUR_MAX_RETRIES 5
int
test_iscsi_tur_until_good(struct scsi_device *iscsi_sd, int *num_uas)
{
int num_turs;
if (iscsi_sd->iscsi_ctx == NULL) {
logging(LOG_NORMAL, "invalid sd for tur_until_good");
return -EINVAL;
}
*num_uas = 0;
for (num_turs = 0; num_turs < TEST_ISCSI_TUR_MAX_RETRIES; num_turs++) {
struct scsi_task *tsk;
tsk = iscsi_testunitready_sync(iscsi_sd->iscsi_ctx,
iscsi_sd->iscsi_lun);
if (tsk->status == SCSI_STATUS_GOOD) {
logging(LOG_VERBOSE, "TUR good after %d retries",
num_turs);
return 0;
} else if ((tsk->status == SCSI_STATUS_CHECK_CONDITION)
&& (tsk->sense.key == SCSI_SENSE_UNIT_ATTENTION)) {
logging(LOG_VERBOSE, "Got UA for TUR");
(*num_uas)++;
} else {
logging(LOG_NORMAL, "unexpected non-UA failure: %d,%d",
tsk->status, tsk->sense.key);
}
}
return -ETIMEDOUT;
}
uint64_t
test_get_clock_sec(void)
{
uint64_t secs;
int res;
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
res = clock_gettime(CLOCK_MONOTONIC, &ts);
secs = ts.tv_sec;
#else
struct timeval tv;
res = gettimeofday(&tv, NULL);
secs = tv.tv_sec;
#endif
assert(res == 0);
return secs;
}
|