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
|
/*-
* Copyright (c) 2003, 2004 Silicon Graphics International Corp.
* Copyright (c) 1997-2007 Kenneth D. Merry
* Copyright (c) 2012 The FreeBSD Foundation
* All rights reserved.
*
* Portions of this software were developed by Edward Tomasz Napierala
* under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
*/
/*
* CAM Target Layer exercise program.
*
* Author: Ken Merry <ken@FreeBSD.org>
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/linker.h>
#include <sys/queue.h>
#include <sys/callout.h>
#include <sys/sbuf.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <ctype.h>
#include <bsdxml.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <cam/ctl/ctl.h>
#include <cam/ctl/ctl_io.h>
#include <cam/ctl/ctl_backend.h>
#include <cam/ctl/ctl_ioctl.h>
#include <cam/ctl/ctl_util.h>
#include <cam/ctl/ctl_scsi_all.h>
#include <camlib.h>
#include <libutil.h>
#include "ctladm.h"
#ifdef min
#undef min
#endif
#define min(x,y) (x < y) ? x : y
typedef enum {
CTLADM_CMD_TUR,
CTLADM_CMD_INQUIRY,
CTLADM_CMD_REQ_SENSE,
CTLADM_CMD_ARRAYLIST,
CTLADM_CMD_REPORT_LUNS,
CTLADM_CMD_HELP,
CTLADM_CMD_DEVLIST,
CTLADM_CMD_ADDDEV,
CTLADM_CMD_RM,
CTLADM_CMD_CREATE,
CTLADM_CMD_READ,
CTLADM_CMD_WRITE,
CTLADM_CMD_PORT,
CTLADM_CMD_PORTLIST,
CTLADM_CMD_READCAPACITY,
CTLADM_CMD_MODESENSE,
CTLADM_CMD_DUMPOOA,
CTLADM_CMD_DUMPSTRUCTS,
CTLADM_CMD_START,
CTLADM_CMD_STOP,
CTLADM_CMD_SYNC_CACHE,
CTLADM_CMD_LUNLIST,
CTLADM_CMD_DELAY,
CTLADM_CMD_ERR_INJECT,
CTLADM_CMD_PRES_IN,
CTLADM_CMD_PRES_OUT,
CTLADM_CMD_INQ_VPD_DEVID,
CTLADM_CMD_RTPG,
CTLADM_CMD_MODIFY,
CTLADM_CMD_ISLIST,
CTLADM_CMD_ISLOGOUT,
CTLADM_CMD_ISTERMINATE,
CTLADM_CMD_LUNMAP
} ctladm_cmdfunction;
typedef enum {
CTLADM_ARG_NONE = 0x0000000,
CTLADM_ARG_AUTOSENSE = 0x0000001,
CTLADM_ARG_DEVICE = 0x0000002,
CTLADM_ARG_ARRAYSIZE = 0x0000004,
CTLADM_ARG_BACKEND = 0x0000008,
CTLADM_ARG_CDBSIZE = 0x0000010,
CTLADM_ARG_DATALEN = 0x0000020,
CTLADM_ARG_FILENAME = 0x0000040,
CTLADM_ARG_LBA = 0x0000080,
CTLADM_ARG_PC = 0x0000100,
CTLADM_ARG_PAGE_CODE = 0x0000200,
CTLADM_ARG_PAGE_LIST = 0x0000400,
CTLADM_ARG_SUBPAGE = 0x0000800,
CTLADM_ARG_PAGELIST = 0x0001000,
CTLADM_ARG_DBD = 0x0002000,
CTLADM_ARG_TARG_LUN = 0x0004000,
CTLADM_ARG_BLOCKSIZE = 0x0008000,
CTLADM_ARG_IMMED = 0x0010000,
CTLADM_ARG_RELADR = 0x0020000,
CTLADM_ARG_RETRIES = 0x0040000,
CTLADM_ARG_ONOFFLINE = 0x0080000,
CTLADM_ARG_ONESHOT = 0x0100000,
CTLADM_ARG_TIMEOUT = 0x0200000,
CTLADM_ARG_INITIATOR = 0x0400000,
CTLADM_ARG_NOCOPY = 0x0800000,
CTLADM_ARG_NEED_TL = 0x1000000
} ctladm_cmdargs;
struct ctladm_opts {
const char *optname;
uint32_t cmdnum;
ctladm_cmdargs argnum;
const char *subopt;
};
typedef enum {
CC_OR_NOT_FOUND,
CC_OR_AMBIGUOUS,
CC_OR_FOUND
} ctladm_optret;
static const char rw_opts[] = "Nb:c:d:f:l:";
static const char startstop_opts[] = "i";
static struct ctladm_opts option_table[] = {
{"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
{"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
{"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
{"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
{"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
{"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
{"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
{"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
{"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
{"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
{"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
{"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
{"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
{"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
{"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
{"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
{"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
{"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
{"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
{"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
{"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
{"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
{"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
{"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
{"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
{"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
{"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
{"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
{"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
{"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
{"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
{"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
{"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
{"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
{NULL, 0, 0, NULL}
};
ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
ctladm_cmdargs *argnum, const char **subopt);
static int cctl_dump_ooa(int fd, int argc, char **argv);
static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
static int cctl_delay(int fd, int lun, int argc, char **argv,
char *combinedopt);
static int cctl_lunlist(int fd);
static int cctl_sync_cache(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt);
static int cctl_start_stop(int fd, int lun, int iid, int retries,
int start, int argc, char **argv, char *combinedopt);
static int cctl_mode_sense(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt);
static int cctl_read_capacity(int fd, int lun, int iid,
int retries, int argc, char **argv,
char *combinedopt);
static int cctl_read_write(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt,
ctladm_cmdfunction command);
static int cctl_get_luns(int fd, int lun, int iid, int retries,
struct scsi_report_luns_data **lun_data,
uint32_t *num_luns);
static int cctl_report_luns(int fd, int lun, int iid, int retries);
static int cctl_tur(int fd, int lun, int iid, int retries);
static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
char *path_str, int path_len,
struct scsi_inquiry_data *inq_data);
static int cctl_inquiry(int fd, int lun, int iid, int retries);
static int cctl_req_sense(int fd, int lun, int iid, int retries);
static int cctl_persistent_reserve_in(int fd, int lun,
int initiator, int argc, char **argv,
char *combinedopt, int retry_count);
static int cctl_persistent_reserve_out(int fd, int lun,
int initiator, int argc, char **argv,
char *combinedopt, int retry_count);
static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
static int cctl_report_target_port_group(int fd, int lun, int initiator);
static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
ctladm_optret
getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
ctladm_cmdargs *argnum, const char **subopt)
{
struct ctladm_opts *opts;
int num_matches = 0;
for (opts = table; (opts != NULL) && (opts->optname != NULL);
opts++) {
if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
*cmdnum = opts->cmdnum;
*argnum = opts->argnum;
*subopt = opts->subopt;
if (strcmp(opts->optname, arg) == 0)
return (CC_OR_FOUND);
if (++num_matches > 1)
return(CC_OR_AMBIGUOUS);
}
}
if (num_matches > 0)
return(CC_OR_FOUND);
else
return(CC_OR_NOT_FOUND);
}
static int
cctl_dump_ooa(int fd, int argc, char **argv)
{
struct ctl_ooa ooa;
long double cmd_latency;
int num_entries, len, lun = -1, retval = 0;
unsigned int i;
num_entries = 104;
if ((argc > 2) && (isdigit(argv[2][0])))
lun = strtol(argv[2], NULL, 0);
retry:
len = num_entries * sizeof(struct ctl_ooa_entry);
bzero(&ooa, sizeof(ooa));
ooa.entries = malloc(len);
if (ooa.entries == NULL) {
warn("%s: error mallocing %d bytes", __func__, len);
return (1);
}
if (lun >= 0) {
ooa.lun_num = lun;
} else
ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
ooa.alloc_len = len;
ooa.alloc_num = num_entries;
if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
warn("%s: CTL_GET_OOA ioctl failed", __func__);
retval = 1;
goto bailout;
}
if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
num_entries = num_entries * 2;
free(ooa.entries);
ooa.entries = NULL;
goto retry;
}
if (ooa.status != CTL_OOA_OK) {
warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
ooa.status);
retval = 1;
goto bailout;
}
fprintf(stdout, "Dumping OOA queues\n");
for (i = 0; i < ooa.fill_num; i++) {
struct ctl_ooa_entry *entry;
char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
struct bintime delta_bt;
struct timespec ts;
entry = &ooa.entries[i];
delta_bt = ooa.cur_bt;
bintime_sub(&delta_bt, &entry->start_bt);
bintime2timespec(&delta_bt, &ts);
cmd_latency = ts.tv_sec * 1000;
if (ts.tv_nsec > 0)
cmd_latency += ts.tv_nsec / 1000000;
fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
"(%0.0Lf ms)\n",
(intmax_t)entry->lun_num, entry->tag_num,
(entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
" BLOCKED" : "",
(entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
(entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
" DMAQUEUED" : "",
(entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
" ABORT" : "",
(entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
scsi_op_desc(entry->cdb[0], NULL),
scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
cmd_latency);
}
fprintf(stdout, "OOA queues dump done\n");
bailout:
free(ooa.entries);
return (retval);
}
static int
cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
{
if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
warn(__func__);
return (1);
}
return (0);
}
typedef enum {
CCTL_PORT_MODE_NONE,
CCTL_PORT_MODE_LIST,
CCTL_PORT_MODE_SET,
CCTL_PORT_MODE_ON,
CCTL_PORT_MODE_OFF
} cctl_port_mode;
static struct ctladm_opts cctl_fe_table[] = {
{"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
{"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
{"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
{"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
{"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
{"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
{NULL, 0, 0, NULL}
};
static int
cctl_port(int fd, int argc, char **argv, char *combinedopt)
{
int c;
int32_t targ_port = -1;
int retval = 0;
int wwnn_set = 0, wwpn_set = 0;
uint64_t wwnn = 0, wwpn = 0;
cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
struct ctl_port_entry entry;
ctl_port_type port_type = CTL_PORT_NONE;
int quiet = 0, xml = 0;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'l':
if (port_mode != CCTL_PORT_MODE_NONE)
goto bailout_badarg;
port_mode = CCTL_PORT_MODE_LIST;
break;
case 'o':
if (port_mode != CCTL_PORT_MODE_NONE)
goto bailout_badarg;
if (strcasecmp(optarg, "on") == 0)
port_mode = CCTL_PORT_MODE_ON;
else if (strcasecmp(optarg, "off") == 0)
port_mode = CCTL_PORT_MODE_OFF;
else {
warnx("Invalid -o argument %s, \"on\" or "
"\"off\" are the only valid args",
optarg);
retval = 1;
goto bailout;
}
break;
case 'p':
targ_port = strtol(optarg, NULL, 0);
break;
case 'q':
quiet = 1;
break;
case 't': {
ctladm_optret optret;
ctladm_cmdargs argnum;
const char *subopt;
ctl_port_type tmp_port_type;
optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
&argnum, &subopt);
if (optret == CC_OR_AMBIGUOUS) {
warnx("%s: ambiguous frontend type %s",
__func__, optarg);
retval = 1;
goto bailout;
} else if (optret == CC_OR_NOT_FOUND) {
warnx("%s: invalid frontend type %s",
__func__, optarg);
retval = 1;
goto bailout;
}
port_type |= tmp_port_type;
break;
}
case 'w':
if ((port_mode != CCTL_PORT_MODE_NONE)
&& (port_mode != CCTL_PORT_MODE_SET))
goto bailout_badarg;
port_mode = CCTL_PORT_MODE_SET;
wwnn = strtoull(optarg, NULL, 0);
wwnn_set = 1;
break;
case 'W':
if ((port_mode != CCTL_PORT_MODE_NONE)
&& (port_mode != CCTL_PORT_MODE_SET))
goto bailout_badarg;
port_mode = CCTL_PORT_MODE_SET;
wwpn = strtoull(optarg, NULL, 0);
wwpn_set = 1;
break;
case 'x':
xml = 1;
break;
}
}
/*
* The user can specify either one or more frontend types (-t), or
* a specific frontend, but not both.
*
* If the user didn't specify a frontend type or number, set it to
* all. This is primarily needed for the enable/disable ioctls.
* This will be a no-op for the listing code. For the set ioctl,
* we'll throw an error, since that only works on one port at a time.
*/
if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
warnx("%s: can only specify one of -t or -n", __func__);
retval = 1;
goto bailout;
} else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
port_type = CTL_PORT_ALL;
bzero(&entry, sizeof(entry));
/*
* These are needed for all but list/dump mode.
*/
entry.port_type = port_type;
entry.targ_port = targ_port;
switch (port_mode) {
case CCTL_PORT_MODE_LIST: {
char opts[] = "xq";
char argx[] = "-x";
char argq[] = "-q";
char *argvx[2];
int argcx = 0;
optind = 0;
optreset = 1;
if (xml)
argvx[argcx++] = argx;
if (quiet)
argvx[argcx++] = argq;
cctl_portlist(fd, argcx, argvx, opts);
break;
}
case CCTL_PORT_MODE_SET:
if (targ_port == -1) {
warnx("%s: -w and -W require -n", __func__);
retval = 1;
goto bailout;
}
if (wwnn_set) {
entry.flags |= CTL_PORT_WWNN_VALID;
entry.wwnn = wwnn;
}
if (wwpn_set) {
entry.flags |= CTL_PORT_WWPN_VALID;
entry.wwpn = wwpn;
}
if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
retval = 1;
goto bailout;
}
break;
case CCTL_PORT_MODE_ON:
if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
retval = 1;
goto bailout;
}
fprintf(stdout, "Front End Ports enabled\n");
break;
case CCTL_PORT_MODE_OFF:
if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
retval = 1;
goto bailout;
}
fprintf(stdout, "Front End Ports disabled\n");
break;
default:
warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
retval = 1;
goto bailout;
break;
}
bailout:
return (retval);
bailout_badarg:
warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
return (1);
}
static int
cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
{
do {
if (ioctl(fd, CTL_IO, io) == -1) {
warn("%s: error sending CTL_IO ioctl", func);
return (-1);
}
} while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
&& (retries-- > 0));
return (0);
}
static int
cctl_delay(int fd, int lun, int argc, char **argv,
char *combinedopt)
{
struct ctl_io_delay_info delay_info;
char *delayloc = NULL;
char *delaytype = NULL;
int delaytime = -1;
int retval;
int c;
retval = 0;
memset(&delay_info, 0, sizeof(delay_info));
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'T':
delaytype = strdup(optarg);
break;
case 'l':
delayloc = strdup(optarg);
break;
case 't':
delaytime = strtoul(optarg, NULL, 0);
break;
}
}
if (delaytime == -1) {
warnx("%s: you must specify the delaytime with -t", __func__);
retval = 1;
goto bailout;
}
if (strcasecmp(delayloc, "datamove") == 0)
delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
else if (strcasecmp(delayloc, "done") == 0)
delay_info.delay_loc = CTL_DELAY_LOC_DONE;
else {
warnx("%s: invalid delay location %s", __func__, delayloc);
retval = 1;
goto bailout;
}
if ((delaytype == NULL)
|| (strcmp(delaytype, "oneshot") == 0))
delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
else if (strcmp(delaytype, "cont") == 0)
delay_info.delay_type = CTL_DELAY_TYPE_CONT;
else {
warnx("%s: invalid delay type %s", __func__, delaytype);
retval = 1;
goto bailout;
}
delay_info.lun_id = lun;
delay_info.delay_secs = delaytime;
if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
warn("%s: CTL_DELAY_IO ioctl failed", __func__);
retval = 1;
goto bailout;
}
switch (delay_info.status) {
case CTL_DELAY_STATUS_NONE:
warnx("%s: no delay status??", __func__);
retval = 1;
break;
case CTL_DELAY_STATUS_OK:
break;
case CTL_DELAY_STATUS_INVALID_LUN:
warnx("%s: invalid lun %d", __func__, lun);
retval = 1;
break;
case CTL_DELAY_STATUS_INVALID_TYPE:
warnx("%s: invalid delay type %d", __func__,
delay_info.delay_type);
retval = 1;
break;
case CTL_DELAY_STATUS_INVALID_LOC:
warnx("%s: delay location %s not implemented?", __func__,
delayloc);
retval = 1;
break;
case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
warnx("%s: delay not implemented in the kernel", __func__);
warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
retval = 1;
break;
default:
warnx("%s: unknown delay return status %d", __func__,
delay_info.status);
retval = 1;
break;
}
bailout:
free(delayloc);
free(delaytype);
return (retval);
}
static struct ctladm_opts cctl_err_types[] = {
{"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
{"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
{"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
{"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
{NULL, 0, 0, NULL}
};
static struct ctladm_opts cctl_err_patterns[] = {
{"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
{"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
{"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
{"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
{"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
{"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
{"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
#if 0
{"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
#endif
{NULL, 0, 0, NULL}
};
static int
cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
char *combinedopt)
{
int retval = 0;
struct ctl_error_desc err_desc;
uint64_t lba = 0;
uint32_t len = 0;
uint64_t delete_id = 0;
int delete_id_set = 0;
int continuous = 0;
int sense_len = 0;
int fd_sense = 0;
int c;
bzero(&err_desc, sizeof(err_desc));
err_desc.lun_id = lun;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'c':
continuous = 1;
break;
case 'd':
delete_id = strtoull(optarg, NULL, 0);
delete_id_set = 1;
break;
case 'i':
case 'p': {
ctladm_optret optret;
ctladm_cmdargs argnum;
const char *subopt;
if (c == 'i') {
ctl_lun_error err_type;
if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
warnx("%s: can't specify multiple -i "
"arguments", __func__);
retval = 1;
goto bailout;
}
optret = getoption(cctl_err_types, optarg,
&err_type, &argnum, &subopt);
err_desc.lun_error = err_type;
} else {
ctl_lun_error_pattern pattern;
optret = getoption(cctl_err_patterns, optarg,
&pattern, &argnum, &subopt);
err_desc.error_pattern |= pattern;
}
if (optret == CC_OR_AMBIGUOUS) {
warnx("%s: ambiguous argument %s", __func__,
optarg);
retval = 1;
goto bailout;
} else if (optret == CC_OR_NOT_FOUND) {
warnx("%s: argument %s not found", __func__,
optarg);
retval = 1;
goto bailout;
}
break;
}
case 'r': {
char *tmpstr, *tmpstr2;
tmpstr = strdup(optarg);
if (tmpstr == NULL) {
warn("%s: error duplicating string %s",
__func__, optarg);
retval = 1;
goto bailout;
}
tmpstr2 = strsep(&tmpstr, ",");
if (tmpstr2 == NULL) {
warnx("%s: invalid -r argument %s", __func__,
optarg);
retval = 1;
free(tmpstr);
goto bailout;
}
lba = strtoull(tmpstr2, NULL, 0);
tmpstr2 = strsep(&tmpstr, ",");
if (tmpstr2 == NULL) {
warnx("%s: no len argument for -r lba,len, got"
" %s", __func__, optarg);
retval = 1;
free(tmpstr);
goto bailout;
}
len = strtoul(tmpstr2, NULL, 0);
free(tmpstr);
break;
}
case 's': {
struct get_hook hook;
char *sensestr;
sense_len = strtol(optarg, NULL, 0);
if (sense_len <= 0) {
warnx("invalid number of sense bytes %d",
sense_len);
retval = 1;
goto bailout;
}
sense_len = MIN(sense_len, SSD_FULL_SIZE);
hook.argc = argc - optind;
hook.argv = argv + optind;
hook.got = 0;
sensestr = cget(&hook, NULL);
if ((sensestr != NULL)
&& (sensestr[0] == '-')) {
fd_sense = 1;
} else {
buff_encode_visit(
(uint8_t *)&err_desc.custom_sense,
sense_len, sensestr, iget, &hook);
}
optind += hook.got;
break;
}
default:
break;
}
}
if (delete_id_set != 0) {
err_desc.serial = delete_id;
if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
__func__);
retval = 1;
}
goto bailout;
}
if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
warnx("%s: error injection command (-i) needed",
__func__);
retval = 1;
goto bailout;
} else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
&& (sense_len == 0)) {
warnx("%s: custom error requires -s", __func__);
retval = 1;
goto bailout;
}
if (continuous != 0)
err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
/*
* If fd_sense is set, we need to read the sense data the user
* wants returned from stdin.
*/
if (fd_sense == 1) {
ssize_t amt_read;
int amt_to_read = sense_len;
u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
for (amt_read = 0; amt_to_read > 0;
amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
if (amt_read == -1) {
warn("error reading sense data from stdin");
retval = 1;
goto bailout;
}
amt_to_read -= amt_read;
buf_ptr += amt_read;
}
}
if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
warnx("%s: command pattern (-p) needed", __func__);
retval = 1;
goto bailout;
}
if (len != 0) {
err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
/*
* We could check here to see whether it's a read/write
* command, but that will be pointless once we allow
* custom patterns. At that point, the user could specify
* a READ(6) CDB type, and we wouldn't have an easy way here
* to verify whether range checking is possible there. The
* user will just figure it out when his error never gets
* executed.
*/
#if 0
if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
warnx("%s: need read and/or write pattern if range "
"is specified", __func__);
retval = 1;
goto bailout;
}
#endif
err_desc.lba_range.lba = lba;
err_desc.lba_range.len = len;
}
if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
retval = 1;
} else {
printf("Error injection succeeded, serial number is %ju\n",
(uintmax_t)err_desc.serial);
}
bailout:
return (retval);
}
static int
cctl_lunlist(int fd)
{
struct scsi_report_luns_data *lun_data;
struct scsi_inquiry_data *inq_data;
uint32_t num_luns;
int initid;
unsigned int i;
int retval;
inq_data = NULL;
initid = 7;
/*
* XXX KDM assuming LUN 0 is fine, but we may need to change this
* if we ever acquire the ability to have multiple targets.
*/
if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
/*retries*/ 2, &lun_data, &num_luns)) != 0)
goto bailout;
inq_data = malloc(sizeof(*inq_data));
if (inq_data == NULL) {
warn("%s: couldn't allocate memory for inquiry data\n",
__func__);
retval = 1;
goto bailout;
}
for (i = 0; i < num_luns; i++) {
char scsi_path[40];
int lun_val;
switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
case RPL_LUNDATA_ATYP_PERIPH:
lun_val = lun_data->luns[i].lundata[1];
break;
case RPL_LUNDATA_ATYP_FLAT:
lun_val = (lun_data->luns[i].lundata[0] &
RPL_LUNDATA_FLAT_LUN_MASK) |
(lun_data->luns[i].lundata[1] <<
RPL_LUNDATA_FLAT_LUN_BITS);
break;
case RPL_LUNDATA_ATYP_LUN:
case RPL_LUNDATA_ATYP_EXTLUN:
default:
fprintf(stdout, "Unsupported LUN format %d\n",
lun_data->luns[i].lundata[0] &
RPL_LUNDATA_ATYP_MASK);
lun_val = -1;
break;
}
if (lun_val == -1)
continue;
if ((retval = cctl_get_inquiry(fd, lun_val, initid,
/*retries*/ 2, scsi_path,
sizeof(scsi_path),
inq_data)) != 0) {
goto bailout;
}
printf("%s", scsi_path);
scsi_print_inquiry(inq_data);
}
bailout:
if (lun_data != NULL)
free(lun_data);
if (inq_data != NULL)
free(inq_data);
return (retval);
}
static int
cctl_sync_cache(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt)
{
union ctl_io *io;
int cdb_size = -1;
int retval;
uint64_t our_lba = 0;
uint32_t our_block_count = 0;
int reladr = 0, immed = 0;
int c;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warnx("%s: can't allocate memory", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'b':
our_block_count = strtoul(optarg, NULL, 0);
break;
case 'c':
cdb_size = strtol(optarg, NULL, 0);
break;
case 'i':
immed = 1;
break;
case 'l':
our_lba = strtoull(optarg, NULL, 0);
break;
case 'r':
reladr = 1;
break;
default:
break;
}
}
if (cdb_size != -1) {
switch (cdb_size) {
case 10:
case 16:
break;
default:
warnx("%s: invalid cdbsize %d, valid sizes are 10 "
"and 16", __func__, cdb_size);
retval = 1;
goto bailout;
break; /* NOTREACHED */
}
} else
cdb_size = 10;
ctl_scsi_sync_cache(/*io*/ io,
/*immed*/ immed,
/*reladr*/ reladr,
/*minimum_cdb_size*/ cdb_size,
/*starting_lba*/ our_lba,
/*block_count*/ our_block_count,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
fprintf(stdout, "Cache synchronized successfully\n");
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
return (retval);
}
static int
cctl_start_stop(int fd, int lun, int iid, int retries, int start,
int argc, char **argv, char *combinedopt)
{
union ctl_io *io;
char scsi_path[40];
int immed = 0;
int retval, c;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warnx("%s: can't allocate memory", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'i':
immed = 1;
break;
default:
break;
}
}
/*
* Use an ordered tag for the stop command, to guarantee that any
* pending I/O will finish before the stop command executes. This
* would normally be the case anyway, since CTL will basically
* treat the start/stop command as an ordered command with respect
* to any other command except an INQUIRY. (See ctl_ser_table.c.)
*/
ctl_scsi_start_stop(/*io*/ io,
/*start*/ start,
/*load_eject*/ 0,
/*immediate*/ immed,
/*power_conditions*/ SSS_PC_START_VALID,
/*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
CTL_TAG_ORDERED,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
(start) ? "started" : "stopped");
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
return (retval);
}
static int
cctl_mode_sense(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt)
{
union ctl_io *io;
uint32_t datalen;
uint8_t *dataptr;
int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
int list = 0;
int page_code = -1;
int c;
cdbsize = 0;
retval = 0;
dataptr = NULL;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'P':
pc = strtoul(optarg, NULL, 0);
break;
case 'S':
subpage = strtoul(optarg, NULL, 0);
break;
case 'd':
dbd = 1;
break;
case 'l':
list = 1;
break;
case 'm':
page_code = strtoul(optarg, NULL, 0);
break;
case 'c':
cdbsize = strtol(optarg, NULL, 0);
break;
default:
break;
}
}
if (((list == 0) && (page_code == -1))
|| ((list != 0) && (page_code != -1))) {
warnx("%s: you must specify either a page code (-m) or -l",
__func__);
retval = 1;
goto bailout;
}
if ((page_code != -1)
&& ((page_code > SMS_ALL_PAGES_PAGE)
|| (page_code < 0))) {
warnx("%s: page code %d is out of range", __func__,
page_code);
retval = 1;
goto bailout;
}
if (list == 1) {
page_code = SMS_ALL_PAGES_PAGE;
if (pc != -1) {
warnx("%s: arg -P makes no sense with -l",
__func__);
retval = 1;
goto bailout;
}
if (subpage != -1) {
warnx("%s: arg -S makes no sense with -l", __func__);
retval = 1;
goto bailout;
}
}
if (pc == -1)
pc = SMS_PAGE_CTRL_CURRENT;
else {
if ((pc > 3)
|| (pc < 0)) {
warnx("%s: page control value %d is out of range: 0-3",
__func__, pc);
retval = 1;
goto bailout;
}
}
if ((subpage != -1)
&& ((subpage > 255)
|| (subpage < 0))) {
warnx("%s: subpage code %d is out of range: 0-255", __func__,
subpage);
retval = 1;
goto bailout;
}
if (cdbsize != 0) {
switch (cdbsize) {
case 6:
case 10:
break;
default:
warnx("%s: invalid cdbsize %d, valid sizes are 6 "
"and 10", __func__, cdbsize);
retval = 1;
goto bailout;
break;
}
} else
cdbsize = 6;
if (subpage == -1)
subpage = 0;
if (cdbsize == 6)
datalen = 255;
else
datalen = 65535;
dataptr = (uint8_t *)malloc(datalen);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes", __func__, datalen);
retval = 1;
goto bailout;
}
memset(dataptr, 0, datalen);
ctl_scsi_mode_sense(io,
/*data_ptr*/ dataptr,
/*data_len*/ datalen,
/*dbd*/ dbd,
/*llbaa*/ 0,
/*page_code*/ page_code,
/*pc*/ pc << 6,
/*subpage*/ subpage,
/*minimum_cdb_size*/ cdbsize,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
int pages_len, used_len;
uint32_t returned_len;
uint8_t *ndataptr;
if (io->scsiio.cdb[0] == MODE_SENSE_6) {
struct scsi_mode_hdr_6 *hdr6;
int bdlen;
hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
returned_len = hdr6->datalen + 1;
bdlen = hdr6->block_descr_len;
ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
} else {
struct scsi_mode_hdr_10 *hdr10;
int bdlen;
hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
returned_len = scsi_2btoul(hdr10->datalen) + 2;
bdlen = scsi_2btoul(hdr10->block_descr_len);
ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
}
/* just in case they can give us more than we allocated for */
returned_len = min(returned_len, datalen);
pages_len = returned_len - (ndataptr - dataptr);
#if 0
fprintf(stdout, "returned_len = %d, pages_len = %d\n",
returned_len, pages_len);
#endif
if (list == 1) {
fprintf(stdout, "Supported mode pages:\n");
for (used_len = 0; used_len < pages_len;) {
struct scsi_mode_page_header *header;
header = (struct scsi_mode_page_header *)
&ndataptr[used_len];
fprintf(stdout, "%d\n", header->page_code);
used_len += header->page_length + 2;
}
} else {
for (used_len = 0; used_len < pages_len; used_len++) {
fprintf(stdout, "0x%x ", ndataptr[used_len]);
if (((used_len+1) % 16) == 0)
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
}
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
static int
cctl_read_capacity(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt)
{
union ctl_io *io;
struct scsi_read_capacity_data *data;
struct scsi_read_capacity_data_long *longdata;
int cdbsize = -1, retval;
uint8_t *dataptr;
int c;
cdbsize = 10;
dataptr = NULL;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory\n", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'c':
cdbsize = strtol(optarg, NULL, 0);
break;
default:
break;
}
}
if (cdbsize != -1) {
switch (cdbsize) {
case 10:
case 16:
break;
default:
warnx("%s: invalid cdbsize %d, valid sizes are 10 "
"and 16", __func__, cdbsize);
retval = 1;
goto bailout;
break; /* NOTREACHED */
}
} else
cdbsize = 10;
dataptr = (uint8_t *)malloc(sizeof(*longdata));
if (dataptr == NULL) {
warn("%s: can't allocate %zd bytes\n", __func__,
sizeof(*longdata));
retval = 1;
goto bailout;
}
memset(dataptr, 0, sizeof(*longdata));
retry:
switch (cdbsize) {
case 10:
ctl_scsi_read_capacity(io,
/*data_ptr*/ dataptr,
/*data_len*/ sizeof(*longdata),
/*addr*/ 0,
/*reladr*/ 0,
/*pmi*/ 0,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
break;
case 16:
ctl_scsi_read_capacity_16(io,
/*data_ptr*/ dataptr,
/*data_len*/ sizeof(*longdata),
/*addr*/ 0,
/*reladr*/ 0,
/*pmi*/ 0,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
break;
}
io->io_hdr.nexus.initid = iid;
io->io_hdr.nexus.targ_lun = lun;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
uint64_t maxlba;
uint32_t blocksize;
if (cdbsize == 10) {
data = (struct scsi_read_capacity_data *)dataptr;
maxlba = scsi_4btoul(data->addr);
blocksize = scsi_4btoul(data->length);
if (maxlba == 0xffffffff) {
cdbsize = 16;
goto retry;
}
} else {
longdata=(struct scsi_read_capacity_data_long *)dataptr;
maxlba = scsi_8btou64(longdata->addr);
blocksize = scsi_4btoul(longdata->length);
}
fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
(uintmax_t)maxlba, blocksize);
} else {
ctl_io_error_print(io, NULL, stderr);
}
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
static int
cctl_read_write(int fd, int lun, int iid, int retries,
int argc, char **argv, char *combinedopt,
ctladm_cmdfunction command)
{
union ctl_io *io;
int file_fd, do_stdio;
int cdbsize = -1, databytes;
uint8_t *dataptr;
char *filename = NULL;
int datalen = -1, blocksize = -1;
uint64_t lba = 0;
int lba_set = 0;
int retval;
int c;
retval = 0;
do_stdio = 0;
dataptr = NULL;
file_fd = -1;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory\n", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'N':
io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
break;
case 'b':
blocksize = strtoul(optarg, NULL, 0);
break;
case 'c':
cdbsize = strtoul(optarg, NULL, 0);
break;
case 'd':
datalen = strtoul(optarg, NULL, 0);
break;
case 'f':
filename = strdup(optarg);
break;
case 'l':
lba = strtoull(optarg, NULL, 0);
lba_set = 1;
break;
default:
break;
}
}
if (filename == NULL) {
warnx("%s: you must supply a filename using -f", __func__);
retval = 1;
goto bailout;
}
if (datalen == -1) {
warnx("%s: you must specify the data length with -d", __func__);
retval = 1;
goto bailout;
}
if (lba_set == 0) {
warnx("%s: you must specify the LBA with -l", __func__);
retval = 1;
goto bailout;
}
if (blocksize == -1) {
warnx("%s: you must specify the blocksize with -b", __func__);
retval = 1;
goto bailout;
}
if (cdbsize != -1) {
switch (cdbsize) {
case 6:
case 10:
case 12:
case 16:
break;
default:
warnx("%s: invalid cdbsize %d, valid sizes are 6, "
"10, 12 or 16", __func__, cdbsize);
retval = 1;
goto bailout;
break; /* NOTREACHED */
}
} else
cdbsize = 6;
databytes = datalen * blocksize;
dataptr = (uint8_t *)malloc(databytes);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes\n", __func__, databytes);
retval = 1;
goto bailout;
}
if (strcmp(filename, "-") == 0) {
if (command == CTLADM_CMD_READ)
file_fd = STDOUT_FILENO;
else
file_fd = STDIN_FILENO;
do_stdio = 1;
} else {
file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (file_fd == -1) {
warn("%s: can't open file %s", __func__, filename);
retval = 1;
goto bailout;
}
}
memset(dataptr, 0, databytes);
if (command == CTLADM_CMD_WRITE) {
int bytes_read;
bytes_read = read(file_fd, dataptr, databytes);
if (bytes_read == -1) {
warn("%s: error reading file %s", __func__, filename);
retval = 1;
goto bailout;
}
if (bytes_read != databytes) {
warnx("%s: only read %d bytes from file %s",
__func__, bytes_read, filename);
retval = 1;
goto bailout;
}
}
ctl_scsi_read_write(io,
/*data_ptr*/ dataptr,
/*data_len*/ databytes,
/*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
/*byte2*/ 0,
/*minimum_cdb_size*/ cdbsize,
/*lba*/ lba,
/*num_blocks*/ datalen,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
&& (command == CTLADM_CMD_READ)) {
int bytes_written;
bytes_written = write(file_fd, dataptr, databytes);
if (bytes_written == -1) {
warn("%s: can't write to %s", __func__, filename);
goto bailout;
}
} else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
if ((do_stdio == 0)
&& (file_fd != -1))
close(file_fd);
return (retval);
}
static int
cctl_get_luns(int fd, int lun, int iid, int retries, struct
scsi_report_luns_data **lun_data, uint32_t *num_luns)
{
union ctl_io *io;
uint32_t nluns;
int lun_datalen;
int retval;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warnx("%s: can't allocate memory", __func__);
return (1);
}
/*
* lun_data includes space for 1 lun, allocate space for 4 initially.
* If that isn't enough, we'll allocate more.
*/
nluns = 4;
retry:
lun_datalen = sizeof(*lun_data) +
(nluns * sizeof(struct scsi_report_luns_lundata));
*lun_data = malloc(lun_datalen);
if (*lun_data == NULL) {
warnx("%s: can't allocate memory", __func__);
ctl_scsi_free_io(io);
return (1);
}
ctl_scsi_report_luns(io,
/*data_ptr*/ (uint8_t *)*lun_data,
/*data_len*/ lun_datalen,
/*select_report*/ RPL_REPORT_ALL,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.initid = iid;
io->io_hdr.nexus.targ_lun = lun;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
uint32_t returned_len, returned_luns;
returned_len = scsi_4btoul((*lun_data)->length);
returned_luns = returned_len / 8;
if (returned_luns > nluns) {
nluns = returned_luns;
free(*lun_data);
goto retry;
}
/* These should be the same */
*num_luns = MIN(returned_luns, nluns);
} else {
ctl_io_error_print(io, NULL, stderr);
retval = 1;
}
bailout:
ctl_scsi_free_io(io);
return (retval);
}
static int
cctl_report_luns(int fd, int lun, int iid, int retries)
{
struct scsi_report_luns_data *lun_data;
uint32_t num_luns, i;
int retval;
lun_data = NULL;
if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
&num_luns)) != 0)
goto bailout;
fprintf(stdout, "%u LUNs returned\n", num_luns);
for (i = 0; i < num_luns; i++) {
int lun_val;
/*
* XXX KDM figure out a way to share this code with
* cctl_lunlist()?
*/
switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
case RPL_LUNDATA_ATYP_PERIPH:
lun_val = lun_data->luns[i].lundata[1];
break;
case RPL_LUNDATA_ATYP_FLAT:
lun_val = (lun_data->luns[i].lundata[0] &
RPL_LUNDATA_FLAT_LUN_MASK) |
(lun_data->luns[i].lundata[1] <<
RPL_LUNDATA_FLAT_LUN_BITS);
break;
case RPL_LUNDATA_ATYP_LUN:
case RPL_LUNDATA_ATYP_EXTLUN:
default:
fprintf(stdout, "Unsupported LUN format %d\n",
lun_data->luns[i].lundata[0] &
RPL_LUNDATA_ATYP_MASK);
lun_val = -1;
break;
}
if (lun_val == -1)
continue;
fprintf(stdout, "%d\n", lun_val);
}
bailout:
if (lun_data != NULL)
free(lun_data);
return (retval);
}
static int
cctl_tur(int fd, int lun, int iid, int retries)
{
union ctl_io *io;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
fprintf(stderr, "can't allocate memory\n");
return (1);
}
ctl_scsi_tur(io,
/* tag_type */ CTL_TAG_SIMPLE,
/* control */ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
ctl_scsi_free_io(io);
return (1);
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
fprintf(stdout, "Unit is ready\n");
else
ctl_io_error_print(io, NULL, stderr);
return (0);
}
static int
cctl_get_inquiry(int fd, int lun, int iid, int retries,
char *path_str, int path_len,
struct scsi_inquiry_data *inq_data)
{
union ctl_io *io;
int retval;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warnx("cctl_inquiry: can't allocate memory\n");
return (1);
}
ctl_scsi_inquiry(/*io*/ io,
/*data_ptr*/ (uint8_t *)inq_data,
/*data_len*/ sizeof(*inq_data),
/*byte2*/ 0,
/*page_code*/ 0,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
retval = 1;
ctl_io_error_print(io, NULL, stderr);
} else if (path_str != NULL)
ctl_scsi_path_string(io, path_str, path_len);
bailout:
ctl_scsi_free_io(io);
return (retval);
}
static int
cctl_inquiry(int fd, int lun, int iid, int retries)
{
struct scsi_inquiry_data *inq_data;
char scsi_path[40];
int retval;
inq_data = malloc(sizeof(*inq_data));
if (inq_data == NULL) {
warnx("%s: can't allocate inquiry data", __func__);
retval = 1;
goto bailout;
}
if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
sizeof(scsi_path), inq_data)) != 0)
goto bailout;
printf("%s", scsi_path);
scsi_print_inquiry(inq_data);
bailout:
if (inq_data != NULL)
free(inq_data);
return (retval);
}
static int
cctl_req_sense(int fd, int lun, int iid, int retries)
{
union ctl_io *io;
struct scsi_sense_data *sense_data;
int retval;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warnx("cctl_req_sense: can't allocate memory\n");
return (1);
}
sense_data = malloc(sizeof(*sense_data));
memset(sense_data, 0, sizeof(*sense_data));
ctl_scsi_request_sense(/*io*/ io,
/*data_ptr*/ (uint8_t *)sense_data,
/*data_len*/ sizeof(*sense_data),
/*byte2*/ 0,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retries, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
io->scsiio.sense_len = sizeof(*sense_data);
ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
free(sense_data);
return (retval);
}
static int
cctl_report_target_port_group(int fd, int lun, int iid)
{
union ctl_io *io;
uint32_t datalen;
uint8_t *dataptr;
int retval;
dataptr = NULL;
retval = 0;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory", __func__);
return (1);
}
datalen = 64;
dataptr = (uint8_t *)malloc(datalen);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes", __func__, datalen);
retval = 1;
goto bailout;
}
memset(dataptr, 0, datalen);
ctl_scsi_maintenance_in(/*io*/ io,
/*data_ptr*/ dataptr,
/*data_len*/ datalen,
/*action*/ SA_RPRT_TRGT_GRP,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, 0, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
int returned_len, used_len;
returned_len = scsi_4btoul(&dataptr[0]) + 4;
for (used_len = 0; used_len < returned_len; used_len++) {
fprintf(stdout, "0x%02x ", dataptr[used_len]);
if (((used_len+1) % 8) == 0)
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
static int
cctl_inquiry_vpd_devid(int fd, int lun, int iid)
{
union ctl_io *io;
uint32_t datalen;
uint8_t *dataptr;
int retval;
retval = 0;
dataptr = NULL;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory", __func__);
return (1);
}
datalen = 256;
dataptr = (uint8_t *)malloc(datalen);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes", __func__, datalen);
retval = 1;
goto bailout;
}
memset(dataptr, 0, datalen);
ctl_scsi_inquiry(/*io*/ io,
/*data_ptr*/ dataptr,
/*data_len*/ datalen,
/*byte2*/ SI_EVPD,
/*page_code*/ SVPD_DEVICE_ID,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, 0, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
int returned_len, used_len;
returned_len = scsi_2btoul(&dataptr[2]) + 4;
for (used_len = 0; used_len < returned_len; used_len++) {
fprintf(stdout, "0x%02x ", dataptr[used_len]);
if (((used_len+1) % 8) == 0)
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
static int
cctl_persistent_reserve_in(int fd, int lun, int iid,
int argc, char **argv, char *combinedopt,
int retry_count)
{
union ctl_io *io;
uint32_t datalen;
uint8_t *dataptr;
int action = -1;
int retval;
int c;
retval = 0;
dataptr = NULL;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'a':
action = strtol(optarg, NULL, 0);
break;
default:
break;
}
}
if (action < 0 || action > 2) {
warn("action must be specified and in the range: 0-2");
retval = 1;
goto bailout;
}
datalen = 256;
dataptr = (uint8_t *)malloc(datalen);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes", __func__, datalen);
retval = 1;
goto bailout;
}
memset(dataptr, 0, datalen);
ctl_scsi_persistent_res_in(io,
/*data_ptr*/ dataptr,
/*data_len*/ datalen,
/*action*/ action,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
int returned_len, used_len;
switch (action) {
case 0:
returned_len = scsi_4btoul(&dataptr[4]) + 8;
returned_len = min(returned_len, 256);
break;
case 1:
returned_len = scsi_4btoul(&dataptr[4]) + 8;
break;
case 2:
returned_len = 8;
break;
default:
warnx("%s: invalid action %d", __func__, action);
goto bailout;
break; /* NOTREACHED */
}
for (used_len = 0; used_len < returned_len; used_len++) {
fprintf(stdout, "0x%02x ", dataptr[used_len]);
if (((used_len+1) % 8) == 0)
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
static int
cctl_persistent_reserve_out(int fd, int lun, int iid,
int argc, char **argv, char *combinedopt,
int retry_count)
{
union ctl_io *io;
uint32_t datalen;
uint64_t key = 0, sa_key = 0;
int action = -1, restype = -1;
uint8_t *dataptr;
int retval;
int c;
retval = 0;
dataptr = NULL;
io = ctl_scsi_alloc_io(iid);
if (io == NULL) {
warn("%s: can't allocate memory", __func__);
return (1);
}
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'a':
action = strtol(optarg, NULL, 0);
break;
case 'k':
key = strtoull(optarg, NULL, 0);
break;
case 'r':
restype = strtol(optarg, NULL, 0);
break;
case 's':
sa_key = strtoull(optarg, NULL, 0);
break;
default:
break;
}
}
if (action < 0 || action > 5) {
warn("action must be specified and in the range: 0-5");
retval = 1;
goto bailout;
}
if (restype < 0 || restype > 5) {
if (action != 0 && action != 5 && action != 3) {
warn("'restype' must specified and in the range: 0-5");
retval = 1;
goto bailout;
}
}
datalen = 24;
dataptr = (uint8_t *)malloc(datalen);
if (dataptr == NULL) {
warn("%s: can't allocate %d bytes", __func__, datalen);
retval = 1;
goto bailout;
}
memset(dataptr, 0, datalen);
ctl_scsi_persistent_res_out(io,
/*data_ptr*/ dataptr,
/*data_len*/ datalen,
/*action*/ action,
/*type*/ restype,
/*key*/ key,
/*sa key*/ sa_key,
/*tag_type*/ CTL_TAG_SIMPLE,
/*control*/ 0);
io->io_hdr.nexus.targ_lun = lun;
io->io_hdr.nexus.initid = iid;
if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
retval = 1;
goto bailout;
}
if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
char scsi_path[40];
ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
"successfully\n", scsi_path);
} else
ctl_io_error_print(io, NULL, stderr);
bailout:
ctl_scsi_free_io(io);
if (dataptr != NULL)
free(dataptr);
return (retval);
}
struct cctl_req_option {
char *name;
int namelen;
char *value;
int vallen;
STAILQ_ENTRY(cctl_req_option) links;
};
static int
cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_req req;
int device_type = -1;
uint64_t lun_size = 0;
uint32_t blocksize = 0, req_lun_id = 0;
char *serial_num = NULL;
char *device_id = NULL;
int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
char *backend_name = NULL;
STAILQ_HEAD(, cctl_req_option) option_list;
int num_options = 0;
int retval = 0, c;
STAILQ_INIT(&option_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'b':
backend_name = strdup(optarg);
break;
case 'B':
blocksize = strtoul(optarg, NULL, 0);
blocksize_set = 1;
break;
case 'd':
device_id = strdup(optarg);
break;
case 'l':
req_lun_id = strtoul(optarg, NULL, 0);
lun_id_set = 1;
break;
case 'o': {
struct cctl_req_option *option;
char *tmpstr;
char *name, *value;
tmpstr = strdup(optarg);
name = strsep(&tmpstr, "=");
if (name == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
value = strsep(&tmpstr, "=");
if (value == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
option = malloc(sizeof(*option));
if (option == NULL) {
warn("%s: error allocating %zd bytes",
__func__, sizeof(*option));
retval = 1;
goto bailout;
}
option->name = strdup(name);
option->namelen = strlen(name) + 1;
option->value = strdup(value);
option->vallen = strlen(value) + 1;
free(tmpstr);
STAILQ_INSERT_TAIL(&option_list, option, links);
num_options++;
break;
}
case 's':
if (strcasecmp(optarg, "auto") != 0) {
retval = expand_number(optarg, &lun_size);
if (retval != 0) {
warn("%s: invalid -s argument",
__func__);
retval = 1;
goto bailout;
}
}
lun_size_set = 1;
break;
case 'S':
serial_num = strdup(optarg);
break;
case 't':
device_type = strtoul(optarg, NULL, 0);
break;
default:
break;
}
}
if (backend_name == NULL) {
warnx("%s: backend name (-b) must be specified", __func__);
retval = 1;
goto bailout;
}
bzero(&req, sizeof(req));
strlcpy(req.backend, backend_name, sizeof(req.backend));
req.reqtype = CTL_LUNREQ_CREATE;
if (blocksize_set != 0)
req.reqdata.create.blocksize_bytes = blocksize;
if (lun_size_set != 0)
req.reqdata.create.lun_size_bytes = lun_size;
if (lun_id_set != 0) {
req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
req.reqdata.create.req_lun_id = req_lun_id;
}
req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
if (device_type != -1)
req.reqdata.create.device_type = device_type;
else
req.reqdata.create.device_type = T_DIRECT;
if (serial_num != NULL) {
strlcpy(req.reqdata.create.serial_num, serial_num,
sizeof(req.reqdata.create.serial_num));
req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
}
if (device_id != NULL) {
strlcpy(req.reqdata.create.device_id, device_id,
sizeof(req.reqdata.create.device_id));
req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
}
req.num_be_args = num_options;
if (num_options > 0) {
struct cctl_req_option *option, *next_option;
int i;
req.be_args = malloc(num_options * sizeof(*req.be_args));
if (req.be_args == NULL) {
warn("%s: error allocating %zd bytes", __func__,
num_options * sizeof(*req.be_args));
retval = 1;
goto bailout;
}
for (i = 0, option = STAILQ_FIRST(&option_list);
i < num_options; i++, option = next_option) {
next_option = STAILQ_NEXT(option, links);
req.be_args[i].namelen = option->namelen;
req.be_args[i].name = strdup(option->name);
req.be_args[i].vallen = option->vallen;
req.be_args[i].value = strdup(option->value);
/*
* XXX KDM do we want a way to specify a writeable
* flag of some sort? Do we want a way to specify
* binary data?
*/
req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
STAILQ_REMOVE(&option_list, option, cctl_req_option,
links);
free(option->name);
free(option->value);
free(option);
}
}
if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
retval = 1;
goto bailout;
}
switch (req.status) {
case CTL_LUN_ERROR:
warnx("LUN creation error: %s", req.error_str);
retval = 1;
goto bailout;
case CTL_LUN_WARNING:
warnx("LUN creation warning: %s", req.error_str);
break;
case CTL_LUN_OK:
break;
default:
warnx("unknown LUN creation status: %d", req.status);
retval = 1;
goto bailout;
}
fprintf(stdout, "LUN created successfully\n");
fprintf(stdout, "backend: %s\n", req.backend);
fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
fprintf(stdout, "LUN size: %ju bytes\n",
(uintmax_t)req.reqdata.create.lun_size_bytes);
fprintf(stdout, "blocksize %u bytes\n",
req.reqdata.create.blocksize_bytes);
fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
bailout:
return (retval);
}
static int
cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_req req;
uint32_t lun_id = 0;
int lun_id_set = 0;
char *backend_name = NULL;
STAILQ_HEAD(, cctl_req_option) option_list;
int num_options = 0;
int retval = 0, c;
STAILQ_INIT(&option_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'b':
backend_name = strdup(optarg);
break;
case 'l':
lun_id = strtoul(optarg, NULL, 0);
lun_id_set = 1;
break;
case 'o': {
struct cctl_req_option *option;
char *tmpstr;
char *name, *value;
tmpstr = strdup(optarg);
name = strsep(&tmpstr, "=");
if (name == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
value = strsep(&tmpstr, "=");
if (value == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
option = malloc(sizeof(*option));
if (option == NULL) {
warn("%s: error allocating %zd bytes",
__func__, sizeof(*option));
retval = 1;
goto bailout;
}
option->name = strdup(name);
option->namelen = strlen(name) + 1;
option->value = strdup(value);
option->vallen = strlen(value) + 1;
free(tmpstr);
STAILQ_INSERT_TAIL(&option_list, option, links);
num_options++;
break;
}
default:
break;
}
}
if (backend_name == NULL)
errx(1, "%s: backend name (-b) must be specified", __func__);
if (lun_id_set == 0)
errx(1, "%s: LUN id (-l) must be specified", __func__);
bzero(&req, sizeof(req));
strlcpy(req.backend, backend_name, sizeof(req.backend));
req.reqtype = CTL_LUNREQ_RM;
req.reqdata.rm.lun_id = lun_id;
req.num_be_args = num_options;
if (num_options > 0) {
struct cctl_req_option *option, *next_option;
int i;
req.be_args = malloc(num_options * sizeof(*req.be_args));
if (req.be_args == NULL) {
warn("%s: error allocating %zd bytes", __func__,
num_options * sizeof(*req.be_args));
retval = 1;
goto bailout;
}
for (i = 0, option = STAILQ_FIRST(&option_list);
i < num_options; i++, option = next_option) {
next_option = STAILQ_NEXT(option, links);
req.be_args[i].namelen = option->namelen;
req.be_args[i].name = strdup(option->name);
req.be_args[i].vallen = option->vallen;
req.be_args[i].value = strdup(option->value);
/*
* XXX KDM do we want a way to specify a writeable
* flag of some sort? Do we want a way to specify
* binary data?
*/
req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
STAILQ_REMOVE(&option_list, option, cctl_req_option,
links);
free(option->name);
free(option->value);
free(option);
}
}
if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
retval = 1;
goto bailout;
}
switch (req.status) {
case CTL_LUN_ERROR:
warnx("LUN removal error: %s", req.error_str);
retval = 1;
goto bailout;
case CTL_LUN_WARNING:
warnx("LUN removal warning: %s", req.error_str);
break;
case CTL_LUN_OK:
break;
default:
warnx("unknown LUN removal status: %d", req.status);
retval = 1;
goto bailout;
}
printf("LUN %d removed successfully\n", lun_id);
bailout:
return (retval);
}
static int
cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_req req;
uint64_t lun_size = 0;
uint32_t lun_id = 0;
int lun_id_set = 0, lun_size_set = 0;
char *backend_name = NULL;
STAILQ_HEAD(, cctl_req_option) option_list;
int num_options = 0;
int retval = 0, c;
STAILQ_INIT(&option_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'b':
backend_name = strdup(optarg);
break;
case 'l':
lun_id = strtoul(optarg, NULL, 0);
lun_id_set = 1;
break;
case 'o': {
struct cctl_req_option *option;
char *tmpstr;
char *name, *value;
tmpstr = strdup(optarg);
name = strsep(&tmpstr, "=");
if (name == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
value = strsep(&tmpstr, "=");
if (value == NULL) {
warnx("%s: option -o takes \"name=value\""
"argument", __func__);
retval = 1;
goto bailout;
}
option = malloc(sizeof(*option));
if (option == NULL) {
warn("%s: error allocating %zd bytes",
__func__, sizeof(*option));
retval = 1;
goto bailout;
}
option->name = strdup(name);
option->namelen = strlen(name) + 1;
option->value = strdup(value);
option->vallen = strlen(value) + 1;
free(tmpstr);
STAILQ_INSERT_TAIL(&option_list, option, links);
num_options++;
break;
}
case 's':
if (strcasecmp(optarg, "auto") != 0) {
retval = expand_number(optarg, &lun_size);
if (retval != 0) {
warn("%s: invalid -s argument",
__func__);
retval = 1;
goto bailout;
}
}
lun_size_set = 1;
break;
default:
break;
}
}
if (backend_name == NULL)
errx(1, "%s: backend name (-b) must be specified", __func__);
if (lun_id_set == 0)
errx(1, "%s: LUN id (-l) must be specified", __func__);
if (lun_size_set == 0 && num_options == 0)
errx(1, "%s: size (-s) or options (-o) must be specified",
__func__);
bzero(&req, sizeof(req));
strlcpy(req.backend, backend_name, sizeof(req.backend));
req.reqtype = CTL_LUNREQ_MODIFY;
req.reqdata.modify.lun_id = lun_id;
req.reqdata.modify.lun_size_bytes = lun_size;
req.num_be_args = num_options;
if (num_options > 0) {
struct cctl_req_option *option, *next_option;
int i;
req.be_args = malloc(num_options * sizeof(*req.be_args));
if (req.be_args == NULL) {
warn("%s: error allocating %zd bytes", __func__,
num_options * sizeof(*req.be_args));
retval = 1;
goto bailout;
}
for (i = 0, option = STAILQ_FIRST(&option_list);
i < num_options; i++, option = next_option) {
next_option = STAILQ_NEXT(option, links);
req.be_args[i].namelen = option->namelen;
req.be_args[i].name = strdup(option->name);
req.be_args[i].vallen = option->vallen;
req.be_args[i].value = strdup(option->value);
/*
* XXX KDM do we want a way to specify a writeable
* flag of some sort? Do we want a way to specify
* binary data?
*/
req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
STAILQ_REMOVE(&option_list, option, cctl_req_option,
links);
free(option->name);
free(option->value);
free(option);
}
}
if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
retval = 1;
goto bailout;
}
switch (req.status) {
case CTL_LUN_ERROR:
warnx("LUN modification error: %s", req.error_str);
retval = 1;
goto bailout;
case CTL_LUN_WARNING:
warnx("LUN modification warning: %s", req.error_str);
break;
case CTL_LUN_OK:
break;
default:
warnx("unknown LUN modification status: %d", req.status);
retval = 1;
goto bailout;
}
printf("LUN %d modified successfully\n", lun_id);
bailout:
return (retval);
}
struct cctl_islist_conn {
int connection_id;
char *initiator;
char *initiator_addr;
char *initiator_alias;
char *target;
char *target_alias;
char *header_digest;
char *data_digest;
char *max_data_segment_length;;
int immediate_data;
int iser;
STAILQ_ENTRY(cctl_islist_conn) links;
};
struct cctl_islist_data {
int num_conns;
STAILQ_HEAD(,cctl_islist_conn) conn_list;
struct cctl_islist_conn *cur_conn;
int level;
struct sbuf *cur_sb[32];
};
static void
cctl_islist_start_element(void *user_data, const char *name, const char **attr)
{
int i;
struct cctl_islist_data *islist;
struct cctl_islist_conn *cur_conn;
islist = (struct cctl_islist_data *)user_data;
cur_conn = islist->cur_conn;
islist->level++;
if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
sizeof(islist->cur_sb[0])))
errx(1, "%s: too many nesting levels, %zd max", __func__,
sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
islist->cur_sb[islist->level] = sbuf_new_auto();
if (islist->cur_sb[islist->level] == NULL)
err(1, "%s: Unable to allocate sbuf", __func__);
if (strcmp(name, "connection") == 0) {
if (cur_conn != NULL)
errx(1, "%s: improper connection element nesting",
__func__);
cur_conn = calloc(1, sizeof(*cur_conn));
if (cur_conn == NULL)
err(1, "%s: cannot allocate %zd bytes", __func__,
sizeof(*cur_conn));
islist->num_conns++;
islist->cur_conn = cur_conn;
STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
for (i = 0; attr[i] != NULL; i += 2) {
if (strcmp(attr[i], "id") == 0) {
cur_conn->connection_id =
strtoull(attr[i+1], NULL, 0);
} else {
errx(1,
"%s: invalid connection attribute %s = %s",
__func__, attr[i], attr[i+1]);
}
}
}
}
static void
cctl_islist_end_element(void *user_data, const char *name)
{
struct cctl_islist_data *islist;
struct cctl_islist_conn *cur_conn;
char *str;
islist = (struct cctl_islist_data *)user_data;
cur_conn = islist->cur_conn;
if ((cur_conn == NULL)
&& (strcmp(name, "ctlislist") != 0))
errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
if (islist->cur_sb[islist->level] == NULL)
errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
islist->level, name);
sbuf_finish(islist->cur_sb[islist->level]);
str = strdup(sbuf_data(islist->cur_sb[islist->level]));
if (str == NULL)
err(1, "%s can't allocate %zd bytes for string", __func__,
sbuf_len(islist->cur_sb[islist->level]));
sbuf_delete(islist->cur_sb[islist->level]);
islist->cur_sb[islist->level] = NULL;
islist->level--;
if (strcmp(name, "initiator") == 0) {
cur_conn->initiator = str;
str = NULL;
} else if (strcmp(name, "initiator_addr") == 0) {
cur_conn->initiator_addr = str;
str = NULL;
} else if (strcmp(name, "initiator_alias") == 0) {
cur_conn->initiator_alias = str;
str = NULL;
} else if (strcmp(name, "target") == 0) {
cur_conn->target = str;
str = NULL;
} else if (strcmp(name, "target_alias") == 0) {
cur_conn->target_alias = str;
str = NULL;
} else if (strcmp(name, "target_portal_group_tag") == 0) {
} else if (strcmp(name, "header_digest") == 0) {
cur_conn->header_digest = str;
str = NULL;
} else if (strcmp(name, "data_digest") == 0) {
cur_conn->data_digest = str;
str = NULL;
} else if (strcmp(name, "max_data_segment_length") == 0) {
cur_conn->max_data_segment_length = str;
str = NULL;
} else if (strcmp(name, "immediate_data") == 0) {
cur_conn->immediate_data = atoi(str);
} else if (strcmp(name, "iser") == 0) {
cur_conn->iser = atoi(str);
} else if (strcmp(name, "connection") == 0) {
islist->cur_conn = NULL;
} else if (strcmp(name, "ctlislist") == 0) {
/* Nothing. */
} else {
/*
* Unknown element; ignore it for forward compatiblity.
*/
}
free(str);
}
static void
cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
{
struct cctl_islist_data *islist;
islist = (struct cctl_islist_data *)user_data;
sbuf_bcat(islist->cur_sb[islist->level], str, len);
}
static int
cctl_islist(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_iscsi req;
struct cctl_islist_data islist;
struct cctl_islist_conn *conn;
XML_Parser parser;
char *conn_str;
int conn_len;
int dump_xml = 0;
int c, retval, verbose = 0;
retval = 0;
conn_len = 4096;
bzero(&islist, sizeof(islist));
STAILQ_INIT(&islist.conn_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'v':
verbose = 1;
break;
case 'x':
dump_xml = 1;
break;
default:
break;
}
}
retry:
conn_str = malloc(conn_len);
bzero(&req, sizeof(req));
req.type = CTL_ISCSI_LIST;
req.data.list.alloc_len = conn_len;
req.data.list.conn_xml = conn_str;
if (ioctl(fd, CTL_ISCSI, &req) == -1) {
warn("%s: error issuing CTL_ISCSI ioctl", __func__);
retval = 1;
goto bailout;
}
if (req.status == CTL_ISCSI_ERROR) {
warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
__func__, req.error_str);
} else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
conn_len = conn_len << 1;
goto retry;
}
if (dump_xml != 0) {
printf("%s", conn_str);
goto bailout;
}
parser = XML_ParserCreate(NULL);
if (parser == NULL) {
warn("%s: Unable to create XML parser", __func__);
retval = 1;
goto bailout;
}
XML_SetUserData(parser, &islist);
XML_SetElementHandler(parser, cctl_islist_start_element,
cctl_islist_end_element);
XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
if (retval != 1) {
warnx("%s: Unable to parse XML: Error %d", __func__,
XML_GetErrorCode(parser));
XML_ParserFree(parser);
retval = 1;
goto bailout;
}
retval = 0;
XML_ParserFree(parser);
if (verbose != 0) {
STAILQ_FOREACH(conn, &islist.conn_list, links) {
printf("Session ID: %d\n", conn->connection_id);
printf("Initiator name: %s\n", conn->initiator);
printf("Initiator portal: %s\n", conn->initiator_addr);
printf("Initiator alias: %s\n", conn->initiator_alias);
printf("Target name: %s\n", conn->target);
printf("Target alias: %s\n", conn->target_alias);
printf("Header digest: %s\n", conn->header_digest);
printf("Data digest: %s\n", conn->data_digest);
printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
printf("\n");
}
} else {
printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
"Target name");
STAILQ_FOREACH(conn, &islist.conn_list, links) {
printf("%4u %-16s %-36s %-36s\n",
conn->connection_id, conn->initiator_addr, conn->initiator,
conn->target);
}
}
bailout:
free(conn_str);
return (retval);
}
static int
cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_iscsi req;
int retval = 0, c;
int all = 0, connection_id = -1, nargs = 0;
char *initiator_name = NULL, *initiator_addr = NULL;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'a':
all = 1;
nargs++;
break;
case 'c':
connection_id = strtoul(optarg, NULL, 0);
nargs++;
break;
case 'i':
initiator_name = strdup(optarg);
if (initiator_name == NULL)
err(1, "%s: strdup", __func__);
nargs++;
break;
case 'p':
initiator_addr = strdup(optarg);
if (initiator_addr == NULL)
err(1, "%s: strdup", __func__);
nargs++;
break;
default:
break;
}
}
if (nargs == 0)
errx(1, "%s: either -a, -c, -i, or -p must be specified",
__func__);
if (nargs > 1)
errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
__func__);
bzero(&req, sizeof(req));
req.type = CTL_ISCSI_LOGOUT;
req.data.logout.connection_id = connection_id;
if (initiator_addr != NULL)
strlcpy(req.data.logout.initiator_addr,
initiator_addr, sizeof(req.data.logout.initiator_addr));
if (initiator_name != NULL)
strlcpy(req.data.logout.initiator_name,
initiator_name, sizeof(req.data.logout.initiator_name));
if (all != 0)
req.data.logout.all = 1;
if (ioctl(fd, CTL_ISCSI, &req) == -1) {
warn("%s: error issuing CTL_ISCSI ioctl", __func__);
retval = 1;
goto bailout;
}
if (req.status != CTL_ISCSI_OK) {
warnx("%s: error returned from CTL iSCSI logout request:\n%s",
__func__, req.error_str);
retval = 1;
goto bailout;
}
printf("iSCSI logout requests submitted\n");
bailout:
return (retval);
}
static int
cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_iscsi req;
int retval = 0, c;
int all = 0, connection_id = -1, nargs = 0;
char *initiator_name = NULL, *initiator_addr = NULL;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'a':
all = 1;
nargs++;
break;
case 'c':
connection_id = strtoul(optarg, NULL, 0);
nargs++;
break;
case 'i':
initiator_name = strdup(optarg);
if (initiator_name == NULL)
err(1, "%s: strdup", __func__);
nargs++;
break;
case 'p':
initiator_addr = strdup(optarg);
if (initiator_addr == NULL)
err(1, "%s: strdup", __func__);
nargs++;
break;
default:
break;
}
}
if (nargs == 0)
errx(1, "%s: either -a, -c, -i, or -p must be specified",
__func__);
if (nargs > 1)
errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
__func__);
bzero(&req, sizeof(req));
req.type = CTL_ISCSI_TERMINATE;
req.data.terminate.connection_id = connection_id;
if (initiator_addr != NULL)
strlcpy(req.data.terminate.initiator_addr,
initiator_addr, sizeof(req.data.terminate.initiator_addr));
if (initiator_name != NULL)
strlcpy(req.data.terminate.initiator_name,
initiator_name, sizeof(req.data.terminate.initiator_name));
if (all != 0)
req.data.terminate.all = 1;
if (ioctl(fd, CTL_ISCSI, &req) == -1) {
warn("%s: error issuing CTL_ISCSI ioctl", __func__);
retval = 1;
goto bailout;
}
if (req.status != CTL_ISCSI_OK) {
warnx("%s: error returned from CTL iSCSI connection "
"termination request:\n%s", __func__, req.error_str);
retval = 1;
goto bailout;
}
printf("iSCSI connections terminated\n");
bailout:
return (retval);
}
/*
* Name/value pair used for per-LUN attributes.
*/
struct cctl_lun_nv {
char *name;
char *value;
STAILQ_ENTRY(cctl_lun_nv) links;
};
/*
* Backend LUN information.
*/
struct cctl_lun {
uint64_t lun_id;
char *backend_type;
uint64_t size_blocks;
uint32_t blocksize;
char *serial_number;
char *device_id;
STAILQ_HEAD(,cctl_lun_nv) attr_list;
STAILQ_ENTRY(cctl_lun) links;
};
struct cctl_devlist_data {
int num_luns;
STAILQ_HEAD(,cctl_lun) lun_list;
struct cctl_lun *cur_lun;
int level;
struct sbuf *cur_sb[32];
};
static void
cctl_start_element(void *user_data, const char *name, const char **attr)
{
int i;
struct cctl_devlist_data *devlist;
struct cctl_lun *cur_lun;
devlist = (struct cctl_devlist_data *)user_data;
cur_lun = devlist->cur_lun;
devlist->level++;
if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
sizeof(devlist->cur_sb[0])))
errx(1, "%s: too many nesting levels, %zd max", __func__,
sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
devlist->cur_sb[devlist->level] = sbuf_new_auto();
if (devlist->cur_sb[devlist->level] == NULL)
err(1, "%s: Unable to allocate sbuf", __func__);
if (strcmp(name, "lun") == 0) {
if (cur_lun != NULL)
errx(1, "%s: improper lun element nesting", __func__);
cur_lun = calloc(1, sizeof(*cur_lun));
if (cur_lun == NULL)
err(1, "%s: cannot allocate %zd bytes", __func__,
sizeof(*cur_lun));
devlist->num_luns++;
devlist->cur_lun = cur_lun;
STAILQ_INIT(&cur_lun->attr_list);
STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
for (i = 0; attr[i] != NULL; i += 2) {
if (strcmp(attr[i], "id") == 0) {
cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
} else {
errx(1, "%s: invalid LUN attribute %s = %s",
__func__, attr[i], attr[i+1]);
}
}
}
}
static void
cctl_end_element(void *user_data, const char *name)
{
struct cctl_devlist_data *devlist;
struct cctl_lun *cur_lun;
char *str;
devlist = (struct cctl_devlist_data *)user_data;
cur_lun = devlist->cur_lun;
if ((cur_lun == NULL)
&& (strcmp(name, "ctllunlist") != 0))
errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
if (devlist->cur_sb[devlist->level] == NULL)
errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
devlist->level, name);
if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
err(1, "%s: sbuf_finish", __func__);
str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
if (str == NULL)
err(1, "%s can't allocate %zd bytes for string", __func__,
sbuf_len(devlist->cur_sb[devlist->level]));
if (strlen(str) == 0) {
free(str);
str = NULL;
}
sbuf_delete(devlist->cur_sb[devlist->level]);
devlist->cur_sb[devlist->level] = NULL;
devlist->level--;
if (strcmp(name, "backend_type") == 0) {
cur_lun->backend_type = str;
str = NULL;
} else if (strcmp(name, "size") == 0) {
cur_lun->size_blocks = strtoull(str, NULL, 0);
} else if (strcmp(name, "blocksize") == 0) {
cur_lun->blocksize = strtoul(str, NULL, 0);
} else if (strcmp(name, "serial_number") == 0) {
cur_lun->serial_number = str;
str = NULL;
} else if (strcmp(name, "device_id") == 0) {
cur_lun->device_id = str;
str = NULL;
} else if (strcmp(name, "lun") == 0) {
devlist->cur_lun = NULL;
} else if (strcmp(name, "ctllunlist") == 0) {
/* Nothing. */
} else {
struct cctl_lun_nv *nv;
nv = calloc(1, sizeof(*nv));
if (nv == NULL)
err(1, "%s: can't allocate %zd bytes for nv pair",
__func__, sizeof(*nv));
nv->name = strdup(name);
if (nv->name == NULL)
err(1, "%s: can't allocated %zd bytes for string",
__func__, strlen(name));
nv->value = str;
str = NULL;
STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
}
free(str);
}
static void
cctl_char_handler(void *user_data, const XML_Char *str, int len)
{
struct cctl_devlist_data *devlist;
devlist = (struct cctl_devlist_data *)user_data;
sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
}
static int
cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_list list;
struct cctl_devlist_data devlist;
struct cctl_lun *lun;
XML_Parser parser;
char *lun_str;
int lun_len;
int dump_xml = 0;
int retval, c;
char *backend = NULL;
int verbose = 0;
retval = 0;
lun_len = 4096;
bzero(&devlist, sizeof(devlist));
STAILQ_INIT(&devlist.lun_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'b':
backend = strdup(optarg);
break;
case 'v':
verbose++;
break;
case 'x':
dump_xml = 1;
break;
default:
break;
}
}
retry:
lun_str = malloc(lun_len);
bzero(&list, sizeof(list));
list.alloc_len = lun_len;
list.status = CTL_LUN_LIST_NONE;
list.lun_xml = lun_str;
if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
retval = 1;
goto bailout;
}
if (list.status == CTL_LUN_LIST_ERROR) {
warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
__func__, list.error_str);
} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
lun_len = lun_len << 1;
goto retry;
}
if (dump_xml != 0) {
printf("%s", lun_str);
goto bailout;
}
parser = XML_ParserCreate(NULL);
if (parser == NULL) {
warn("%s: Unable to create XML parser", __func__);
retval = 1;
goto bailout;
}
XML_SetUserData(parser, &devlist);
XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
XML_SetCharacterDataHandler(parser, cctl_char_handler);
retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
if (retval != 1) {
warnx("%s: Unable to parse XML: Error %d", __func__,
XML_GetErrorCode(parser));
XML_ParserFree(parser);
retval = 1;
goto bailout;
}
retval = 0;
XML_ParserFree(parser);
printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
"Serial Number", "Device ID");
STAILQ_FOREACH(lun, &devlist.lun_list, links) {
struct cctl_lun_nv *nv;
if ((backend != NULL)
&& (strcmp(lun->backend_type, backend) != 0))
continue;
printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
(uintmax_t)lun->lun_id,
lun->backend_type, (uintmax_t)lun->size_blocks,
lun->blocksize, lun->serial_number, lun->device_id);
if (verbose == 0)
continue;
STAILQ_FOREACH(nv, &lun->attr_list, links) {
printf(" %s=%s\n", nv->name, nv->value);
}
}
bailout:
free(lun_str);
return (retval);
}
/*
* Port information.
*/
struct cctl_port {
uint64_t port_id;
char *online;
char *frontend_type;
char *name;
int pp, vp;
char *target, *port, *lun_map;
STAILQ_HEAD(,cctl_lun_nv) init_list;
STAILQ_HEAD(,cctl_lun_nv) lun_list;
STAILQ_HEAD(,cctl_lun_nv) attr_list;
STAILQ_ENTRY(cctl_port) links;
};
struct cctl_portlist_data {
int num_ports;
STAILQ_HEAD(,cctl_port) port_list;
struct cctl_port *cur_port;
int level;
uint64_t cur_id;
struct sbuf *cur_sb[32];
};
static void
cctl_start_pelement(void *user_data, const char *name, const char **attr)
{
int i;
struct cctl_portlist_data *portlist;
struct cctl_port *cur_port;
portlist = (struct cctl_portlist_data *)user_data;
cur_port = portlist->cur_port;
portlist->level++;
if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
sizeof(portlist->cur_sb[0])))
errx(1, "%s: too many nesting levels, %zd max", __func__,
sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
portlist->cur_sb[portlist->level] = sbuf_new_auto();
if (portlist->cur_sb[portlist->level] == NULL)
err(1, "%s: Unable to allocate sbuf", __func__);
portlist->cur_id = 0;
for (i = 0; attr[i] != NULL; i += 2) {
if (strcmp(attr[i], "id") == 0) {
portlist->cur_id = strtoull(attr[i+1], NULL, 0);
break;
}
}
if (strcmp(name, "targ_port") == 0) {
if (cur_port != NULL)
errx(1, "%s: improper port element nesting", __func__);
cur_port = calloc(1, sizeof(*cur_port));
if (cur_port == NULL)
err(1, "%s: cannot allocate %zd bytes", __func__,
sizeof(*cur_port));
portlist->num_ports++;
portlist->cur_port = cur_port;
STAILQ_INIT(&cur_port->init_list);
STAILQ_INIT(&cur_port->lun_list);
STAILQ_INIT(&cur_port->attr_list);
cur_port->port_id = portlist->cur_id;
STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
}
}
static void
cctl_end_pelement(void *user_data, const char *name)
{
struct cctl_portlist_data *portlist;
struct cctl_port *cur_port;
char *str;
portlist = (struct cctl_portlist_data *)user_data;
cur_port = portlist->cur_port;
if ((cur_port == NULL)
&& (strcmp(name, "ctlportlist") != 0))
errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
if (portlist->cur_sb[portlist->level] == NULL)
errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
portlist->level, name);
if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
err(1, "%s: sbuf_finish", __func__);
str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
if (str == NULL)
err(1, "%s can't allocate %zd bytes for string", __func__,
sbuf_len(portlist->cur_sb[portlist->level]));
if (strlen(str) == 0) {
free(str);
str = NULL;
}
sbuf_delete(portlist->cur_sb[portlist->level]);
portlist->cur_sb[portlist->level] = NULL;
portlist->level--;
if (strcmp(name, "frontend_type") == 0) {
cur_port->frontend_type = str;
str = NULL;
} else if (strcmp(name, "port_name") == 0) {
cur_port->name = str;
str = NULL;
} else if (strcmp(name, "online") == 0) {
cur_port->online = str;
str = NULL;
} else if (strcmp(name, "physical_port") == 0) {
cur_port->pp = strtoull(str, NULL, 0);
} else if (strcmp(name, "virtual_port") == 0) {
cur_port->vp = strtoull(str, NULL, 0);
} else if (strcmp(name, "target") == 0) {
cur_port->target = str;
str = NULL;
} else if (strcmp(name, "port") == 0) {
cur_port->port = str;
str = NULL;
} else if (strcmp(name, "lun_map") == 0) {
cur_port->lun_map = str;
str = NULL;
} else if (strcmp(name, "targ_port") == 0) {
portlist->cur_port = NULL;
} else if (strcmp(name, "ctlportlist") == 0) {
/* Nothing. */
} else {
struct cctl_lun_nv *nv;
nv = calloc(1, sizeof(*nv));
if (nv == NULL)
err(1, "%s: can't allocate %zd bytes for nv pair",
__func__, sizeof(*nv));
if (strcmp(name, "initiator") == 0 ||
strcmp(name, "lun") == 0)
asprintf(&nv->name, "%ju", portlist->cur_id);
else
nv->name = strdup(name);
if (nv->name == NULL)
err(1, "%s: can't allocated %zd bytes for string",
__func__, strlen(name));
nv->value = str;
str = NULL;
if (strcmp(name, "initiator") == 0)
STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
else if (strcmp(name, "lun") == 0)
STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
else
STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
}
free(str);
}
static void
cctl_char_phandler(void *user_data, const XML_Char *str, int len)
{
struct cctl_portlist_data *portlist;
portlist = (struct cctl_portlist_data *)user_data;
sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
}
static int
cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_list list;
struct cctl_portlist_data portlist;
struct cctl_port *port;
XML_Parser parser;
char *port_str;
int port_len;
int dump_xml = 0;
int retval, c;
char *frontend = NULL;
uint64_t portarg = UINT64_MAX;
int verbose = 0, init = 0, lun = 0, quiet = 0;
retval = 0;
port_len = 4096;
bzero(&portlist, sizeof(portlist));
STAILQ_INIT(&portlist.port_list);
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'f':
frontend = strdup(optarg);
break;
case 'i':
init++;
break;
case 'l':
lun++;
break;
case 'p':
portarg = strtoll(optarg, NULL, 0);
break;
case 'q':
quiet++;
break;
case 'v':
verbose++;
break;
case 'x':
dump_xml = 1;
break;
default:
break;
}
}
retry:
port_str = malloc(port_len);
bzero(&list, sizeof(list));
list.alloc_len = port_len;
list.status = CTL_LUN_LIST_NONE;
list.lun_xml = port_str;
if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
retval = 1;
goto bailout;
}
if (list.status == CTL_LUN_LIST_ERROR) {
warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
__func__, list.error_str);
} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
port_len = port_len << 1;
goto retry;
}
if (dump_xml != 0) {
printf("%s", port_str);
goto bailout;
}
parser = XML_ParserCreate(NULL);
if (parser == NULL) {
warn("%s: Unable to create XML parser", __func__);
retval = 1;
goto bailout;
}
XML_SetUserData(parser, &portlist);
XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
XML_SetCharacterDataHandler(parser, cctl_char_phandler);
retval = XML_Parse(parser, port_str, strlen(port_str), 1);
if (retval != 1) {
warnx("%s: Unable to parse XML: Error %d", __func__,
XML_GetErrorCode(parser));
XML_ParserFree(parser);
retval = 1;
goto bailout;
}
retval = 0;
XML_ParserFree(parser);
if (quiet == 0)
printf("Port Online Frontend Name pp vp\n");
STAILQ_FOREACH(port, &portlist.port_list, links) {
struct cctl_lun_nv *nv;
if ((frontend != NULL)
&& (strcmp(port->frontend_type, frontend) != 0))
continue;
if ((portarg != UINT64_MAX) && (portarg != port->port_id))
continue;
printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
(uintmax_t)port->port_id, port->online,
port->frontend_type, port->name, port->pp, port->vp,
port->port ? port->port : "");
if (init || verbose) {
if (port->target)
printf(" Target: %s\n", port->target);
STAILQ_FOREACH(nv, &port->init_list, links) {
printf(" Initiator %s: %s\n",
nv->name, nv->value);
}
}
if (lun || verbose) {
if (port->lun_map) {
STAILQ_FOREACH(nv, &port->lun_list, links)
printf(" LUN %s: %s\n",
nv->name, nv->value);
if (STAILQ_EMPTY(&port->lun_list))
printf(" No LUNs mapped\n");
} else
printf(" All LUNs mapped\n");
}
if (verbose) {
STAILQ_FOREACH(nv, &port->attr_list, links) {
printf(" %s=%s\n", nv->name, nv->value);
}
}
}
bailout:
free(port_str);
return (retval);
}
static int
cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
{
struct ctl_lun_map lm;
int retval = 0, c;
retval = 0;
lm.port = UINT32_MAX;
lm.plun = UINT32_MAX;
lm.lun = UINT32_MAX;
while ((c = getopt(argc, argv, combinedopt)) != -1) {
switch (c) {
case 'p':
lm.port = strtoll(optarg, NULL, 0);
break;
case 'l':
lm.plun = strtoll(optarg, NULL, 0);
break;
case 'L':
lm.lun = strtoll(optarg, NULL, 0);
break;
default:
break;
}
}
if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
retval = 1;
}
return (retval);
}
void
usage(int error)
{
fprintf(error ? stderr : stdout,
"Usage:\n"
"Primary commands:\n"
" ctladm tur [dev_id][general options]\n"
" ctladm inquiry [dev_id][general options]\n"
" ctladm devid [dev_id][general options]\n"
" ctladm reqsense [dev_id][general options]\n"
" ctladm reportluns [dev_id][general options]\n"
" ctladm read [dev_id][general options] <-l lba> <-d len>\n"
" <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
" ctladm write [dev_id][general options] <-l lba> <-d len>\n"
" <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
" ctladm readcap [dev_id][general options] [-c cdbsize]\n"
" ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
" [-d] [-S subpage] [-c cdbsize]\n"
" ctladm prin [dev_id][general options] <-a action>\n"
" ctladm prout [dev_id][general options] <-a action>\n"
" <-r restype] [-k key] [-s sa_key]\n"
" ctladm rtpg [dev_id][general options]\n"
" ctladm start [dev_id][general options] [-i] [-o]\n"
" ctladm stop [dev_id][general options] [-i] [-o]\n"
" ctladm synccache [dev_id][general options] [-l lba]\n"
" [-b blockcount] [-r] [-i] [-c cdbsize]\n"
" ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
" [-l lun_id] [-o name=value] [-s size_bytes]\n"
" [-S serial_num] [-t dev_type]\n"
" ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
" ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
" ctladm devlist [-b backend] [-v] [-x]\n"
" ctladm lunlist\n"
" ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
" ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
" [-t secs]\n"
" ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
" [-s len fmt [args]] [-c] [-d delete_id]\n"
" ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
" [-p targ_port] [-t port_type]\n"
" ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
" ctladm islist [-v | -x]\n"
" ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
" ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
" ctladm dumpooa\n"
" ctladm dumpstructs\n"
" ctladm help\n"
"General Options:\n"
"-I intiator_id : defaults to 7, used to change the initiator id\n"
"-C retries : specify the number of times to retry this command\n"
"-D devicename : specify the device to operate on\n"
" : (default is %s)\n"
"read/write options:\n"
"-l lba : logical block address\n"
"-d len : read/write length, in blocks\n"
"-f file|- : write/read data to/from file or stdout/stdin\n"
"-b blocksize : block size, in bytes\n"
"-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
"-N : do not copy data to/from userland\n"
"readcapacity options:\n"
"-c cdbsize : specify minimum cdb size: 10 or 16\n"
"modesense options:\n"
"-m page : specify the mode page to view\n"
"-l : request a list of supported pages\n"
"-P pc : specify the page control value: 0-3 (current,\n"
" changeable, default, saved, respectively)\n"
"-d : disable block descriptors for mode sense\n"
"-S subpage : specify a subpage\n"
"-c cdbsize : specify minimum cdb size: 6 or 10\n"
"persistent reserve in options:\n"
"-a action : specify the action value: 0-2 (read key, read\n"
" reservation, read capabilities, respectively)\n"
"persistent reserve out options:\n"
"-a action : specify the action value: 0-5 (register, reserve,\n"
" release, clear, preempt, register and ignore)\n"
"-k key : key value\n"
"-s sa_key : service action value\n"
"-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
" wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
"start/stop options:\n"
"-i : set the immediate bit (CTL does not support this)\n"
"-o : set the on/offline bit\n"
"synccache options:\n"
"-l lba : set the starting LBA\n"
"-b blockcount : set the length to sync in blocks\n"
"-r : set the relative addressing bit\n"
"-i : set the immediate bit\n"
"-c cdbsize : specify minimum cdb size: 10 or 16\n"
"create options:\n"
"-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
"-B blocksize : LUN blocksize in bytes (some backends)\n"
"-d device_id : SCSI VPD page 0x83 ID\n"
"-l lun_id : requested LUN number\n"
"-o name=value : backend-specific options, multiple allowed\n"
"-s size_bytes : LUN size in bytes (some backends)\n"
"-S serial_num : SCSI VPD page 0x80 serial number\n"
"-t dev_type : SCSI device type (0=disk, 3=processor)\n"
"remove options:\n"
"-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
"-l lun_id : LUN number to delete\n"
"-o name=value : backend-specific options, multiple allowed\n"
"devlist options:\n"
"-b backend : list devices from specified backend only\n"
"-v : be verbose, show backend attributes\n"
"-x : dump raw XML\n"
"delay options:\n"
"-l datamove|done : delay command at datamove or done phase\n"
"-T oneshot : delay one command, then resume normal completion\n"
"-T cont : delay all commands\n"
"-t secs : number of seconds to delay\n"
"inject options:\n"
"-i error_action : action to perform\n"
"-p pattern : command pattern to look for\n"
"-r lba,len : LBA range for pattern\n"
"-s len fmt [args] : sense data for custom sense action\n"
"-c : continuous operation\n"
"-d delete_id : error id to delete\n"
"port options:\n"
"-l : list frontend ports\n"
"-o on|off : turn frontend ports on or off\n"
"-w wwnn : set WWNN for one frontend\n"
"-W wwpn : set WWPN for one frontend\n"
"-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
"-p targ_port : specify target port number\n"
"-q : omit header in list output\n"
"-x : output port list in XML format\n"
"portlist options:\n"
"-f fronetnd : specify frontend type\n"
"-i : report target and initiators addresses\n"
"-l : report LUN mapping\n"
"-p targ_port : specify target port number\n"
"-q : omit header in list output\n"
"-v : verbose output (report all port options)\n"
"-x : output port list in XML format\n"
"lunmap options:\n"
"-p targ_port : specify target port number\n"
"-L pLUN : specify port-visible LUN\n"
"-L cLUN : specify CTL LUN\n",
CTL_DEFAULT_DEV);
}
int
main(int argc, char **argv)
{
int c;
ctladm_cmdfunction command;
ctladm_cmdargs cmdargs;
ctladm_optret optreturn;
char *device;
const char *mainopt = "C:D:I:";
const char *subopt = NULL;
char combinedopt[256];
int lun;
int optstart = 2;
int retval, fd;
int retries;
int initid;
int saved_errno;
retval = 0;
cmdargs = CTLADM_ARG_NONE;
command = CTLADM_CMD_HELP;
device = NULL;
fd = -1;
retries = 0;
lun = 0;
initid = 7;
if (argc < 2) {
usage(1);
retval = 1;
goto bailout;
}
/*
* Get the base option.
*/
optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
if (optreturn == CC_OR_AMBIGUOUS) {
warnx("ambiguous option %s", argv[1]);
usage(0);
exit(1);
} else if (optreturn == CC_OR_NOT_FOUND) {
warnx("option %s not found", argv[1]);
usage(0);
exit(1);
}
if (cmdargs & CTLADM_ARG_NEED_TL) {
if ((argc < 3) || (!isdigit(argv[2][0]))) {
warnx("option %s requires a lun argument",
argv[1]);
usage(0);
exit(1);
}
lun = strtol(argv[2], NULL, 0);
cmdargs |= CTLADM_ARG_TARG_LUN;
optstart++;
}
/*
* Ahh, getopt(3) is a pain.
*
* This is a gross hack. There really aren't many other good
* options (excuse the pun) for parsing options in a situation like
* this. getopt is kinda braindead, so you end up having to run
* through the options twice, and give each invocation of getopt
* the option string for the other invocation.
*
* You would think that you could just have two groups of options.
* The first group would get parsed by the first invocation of
* getopt, and the second group would get parsed by the second
* invocation of getopt. It doesn't quite work out that way. When
* the first invocation of getopt finishes, it leaves optind pointing
* to the argument _after_ the first argument in the second group.
* So when the second invocation of getopt comes around, it doesn't
* recognize the first argument it gets and then bails out.
*
* A nice alternative would be to have a flag for getopt that says
* "just keep parsing arguments even when you encounter an unknown
* argument", but there isn't one. So there's no real clean way to
* easily parse two sets of arguments without having one invocation
* of getopt know about the other.
*
* Without this hack, the first invocation of getopt would work as
* long as the generic arguments are first, but the second invocation
* (in the subfunction) would fail in one of two ways. In the case
* where you don't set optreset, it would fail because optind may be
* pointing to the argument after the one it should be pointing at.
* In the case where you do set optreset, and reset optind, it would
* fail because getopt would run into the first set of options, which
* it doesn't understand.
*
* All of this would "sort of" work if you could somehow figure out
* whether optind had been incremented one option too far. The
* mechanics of that, however, are more daunting than just giving
* both invocations all of the expect options for either invocation.
*
* Needless to say, I wouldn't mind if someone invented a better
* (non-GPL!) command line parsing interface than getopt. I
* wouldn't mind if someone added more knobs to getopt to make it
* work better. Who knows, I may talk myself into doing it someday,
* if the standards weenies let me. As it is, it just leads to
* hackery like this and causes people to avoid it in some cases.
*
* KDM, September 8th, 1998
*/
if (subopt != NULL)
sprintf(combinedopt, "%s%s", mainopt, subopt);
else
sprintf(combinedopt, "%s", mainopt);
/*
* Start getopt processing at argv[2/3], since we've already
* accepted argv[1..2] as the command name, and as a possible
* device name.
*/
optind = optstart;
/*
* Now we run through the argument list looking for generic
* options, and ignoring options that possibly belong to
* subfunctions.
*/
while ((c = getopt(argc, argv, combinedopt))!= -1){
switch (c) {
case 'C':
cmdargs |= CTLADM_ARG_RETRIES;
retries = strtol(optarg, NULL, 0);
break;
case 'D':
device = strdup(optarg);
cmdargs |= CTLADM_ARG_DEVICE;
break;
case 'I':
cmdargs |= CTLADM_ARG_INITIATOR;
initid = strtol(optarg, NULL, 0);
break;
default:
break;
}
}
if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
initid = 7;
optind = optstart;
optreset = 1;
/*
* Default to opening the CTL device for now.
*/
if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
&& (command != CTLADM_CMD_HELP)) {
device = strdup(CTL_DEFAULT_DEV);
cmdargs |= CTLADM_ARG_DEVICE;
}
if ((cmdargs & CTLADM_ARG_DEVICE)
&& (command != CTLADM_CMD_HELP)) {
fd = open(device, O_RDWR);
if (fd == -1 && errno == ENOENT) {
saved_errno = errno;
retval = kldload("ctl");
if (retval != -1)
fd = open(device, O_RDWR);
else
errno = saved_errno;
}
if (fd == -1) {
fprintf(stderr, "%s: error opening %s: %s\n",
argv[0], device, strerror(errno));
retval = 1;
goto bailout;
}
} else if ((command != CTLADM_CMD_HELP)
&& ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
fprintf(stderr, "%s: you must specify a device with the "
"--device argument for this command\n", argv[0]);
command = CTLADM_CMD_HELP;
retval = 1;
}
switch (command) {
case CTLADM_CMD_TUR:
retval = cctl_tur(fd, lun, initid, retries);
break;
case CTLADM_CMD_INQUIRY:
retval = cctl_inquiry(fd, lun, initid, retries);
break;
case CTLADM_CMD_REQ_SENSE:
retval = cctl_req_sense(fd, lun, initid, retries);
break;
case CTLADM_CMD_REPORT_LUNS:
retval = cctl_report_luns(fd, lun, initid, retries);
break;
case CTLADM_CMD_CREATE:
retval = cctl_create_lun(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_RM:
retval = cctl_rm_lun(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_DEVLIST:
retval = cctl_devlist(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_READ:
case CTLADM_CMD_WRITE:
retval = cctl_read_write(fd, lun, initid, retries,
argc, argv, combinedopt, command);
break;
case CTLADM_CMD_PORT:
retval = cctl_port(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_PORTLIST:
retval = cctl_portlist(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_LUNMAP:
retval = cctl_lunmap(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_READCAPACITY:
retval = cctl_read_capacity(fd, lun, initid, retries,
argc, argv, combinedopt);
break;
case CTLADM_CMD_MODESENSE:
retval = cctl_mode_sense(fd, lun, initid, retries,
argc, argv, combinedopt);
break;
case CTLADM_CMD_START:
case CTLADM_CMD_STOP:
retval = cctl_start_stop(fd, lun, initid, retries,
(command == CTLADM_CMD_START) ? 1 : 0,
argc, argv, combinedopt);
break;
case CTLADM_CMD_SYNC_CACHE:
retval = cctl_sync_cache(fd, lun, initid, retries,
argc, argv, combinedopt);
break;
case CTLADM_CMD_LUNLIST:
retval = cctl_lunlist(fd);
break;
case CTLADM_CMD_DELAY:
retval = cctl_delay(fd, lun, argc, argv, combinedopt);
break;
case CTLADM_CMD_ERR_INJECT:
retval = cctl_error_inject(fd, lun, argc, argv,
combinedopt);
break;
case CTLADM_CMD_DUMPOOA:
retval = cctl_dump_ooa(fd, argc, argv);
break;
case CTLADM_CMD_DUMPSTRUCTS:
retval = cctl_dump_structs(fd, cmdargs);
break;
case CTLADM_CMD_PRES_IN:
retval = cctl_persistent_reserve_in(fd, lun, initid,
argc, argv, combinedopt,
retries);
break;
case CTLADM_CMD_PRES_OUT:
retval = cctl_persistent_reserve_out(fd, lun, initid,
argc, argv, combinedopt,
retries);
break;
case CTLADM_CMD_INQ_VPD_DEVID:
retval = cctl_inquiry_vpd_devid(fd, lun, initid);
break;
case CTLADM_CMD_RTPG:
retval = cctl_report_target_port_group(fd, lun, initid);
break;
case CTLADM_CMD_MODIFY:
retval = cctl_modify_lun(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_ISLIST:
retval = cctl_islist(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_ISLOGOUT:
retval = cctl_islogout(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_ISTERMINATE:
retval = cctl_isterminate(fd, argc, argv, combinedopt);
break;
case CTLADM_CMD_HELP:
default:
usage(retval);
break;
}
bailout:
if (fd != -1)
close(fd);
exit (retval);
}
/*
* vim: ts=8
*/
|