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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Virtual disk server
*/
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/crc32.h>
#include <sys/ddi.h>
#include <sys/dkio.h>
#include <sys/file.h>
#include <sys/mdeg.h>
#include <sys/modhash.h>
#include <sys/note.h>
#include <sys/pathname.h>
#include <sys/sunddi.h>
#include <sys/sunldi.h>
#include <sys/sysmacros.h>
#include <sys/vio_common.h>
#include <sys/vdsk_mailbox.h>
#include <sys/vdsk_common.h>
#include <sys/vtoc.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <vm/seg_map.h>
/* Virtual disk server initialization flags */
#define VDS_LDI 0x01
#define VDS_MDEG 0x02
/* Virtual disk server tunable parameters */
#define VDS_RETRIES 5
#define VDS_LDC_DELAY 1000 /* 1 msecs */
#define VDS_DEV_DELAY 10000000 /* 10 secs */
#define VDS_NCHAINS 32
/* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
#define VDS_NAME "virtual-disk-server"
#define VD_NAME "vd"
#define VD_VOLUME_NAME "vdisk"
#define VD_ASCIILABEL "Virtual Disk"
#define VD_CHANNEL_ENDPOINT "channel-endpoint"
#define VD_ID_PROP "id"
#define VD_BLOCK_DEVICE_PROP "vds-block-device"
#define VD_REG_PROP "reg"
/* Virtual disk initialization flags */
#define VD_DISK_READY 0x01
#define VD_LOCKING 0x02
#define VD_LDC 0x04
#define VD_DRING 0x08
#define VD_SID 0x10
#define VD_SEQ_NUM 0x20
/* Flags for opening/closing backing devices via LDI */
#define VD_OPEN_FLAGS (FEXCL | FREAD | FWRITE)
/* Flags for writing to a vdisk which is a file */
#define VD_FILE_WRITE_FLAGS SM_ASYNC
/*
* By Solaris convention, slice/partition 2 represents the entire disk;
* unfortunately, this convention does not appear to be codified.
*/
#define VD_ENTIRE_DISK_SLICE 2
/* Return a cpp token as a string */
#define STRINGIZE(token) #token
/*
* Print a message prefixed with the current function name to the message log
* (and optionally to the console for verbose boots); these macros use cpp's
* concatenation of string literals and C99 variable-length-argument-list
* macros
*/
#define PRN(...) _PRN("?%s(): "__VA_ARGS__, "")
#define _PRN(format, ...) \
cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
/* Return a pointer to the "i"th vdisk dring element */
#define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \
(vd->dring + (i)*vd->descriptor_size))
/* Return the virtual disk client's type as a string (for use in messages) */
#define VD_CLIENT(vd) \
(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \
(((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" : \
(((vd)->xfer_mode == 0) ? "null client" : \
"unsupported client")))
/* For IO to raw disk on file */
#define VD_FILE_SLICE_NONE -1
/* Read disk label from a disk on file */
#define VD_FILE_LABEL_READ(vd, labelp) \
vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
0, sizeof (struct dk_label))
/* Write disk label to a disk on file */
#define VD_FILE_LABEL_WRITE(vd, labelp) \
vd_file_rw(vd, VD_FILE_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
0, sizeof (struct dk_label))
/*
* Specification of an MD node passed to the MDEG to filter any
* 'vport' nodes that do not belong to the specified node. This
* template is copied for each vds instance and filled in with
* the appropriate 'cfg-handle' value before being passed to the MDEG.
*/
static mdeg_prop_spec_t vds_prop_template[] = {
{ MDET_PROP_STR, "name", VDS_NAME },
{ MDET_PROP_VAL, "cfg-handle", NULL },
{ MDET_LIST_END, NULL, NULL }
};
#define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
/*
* Matching criteria passed to the MDEG to register interest
* in changes to 'virtual-device-port' nodes identified by their
* 'id' property.
*/
static md_prop_match_t vd_prop_match[] = {
{ MDET_PROP_VAL, VD_ID_PROP },
{ MDET_LIST_END, NULL }
};
static mdeg_node_match_t vd_match = {"virtual-device-port",
vd_prop_match};
/* Debugging macros */
#ifdef DEBUG
static int vd_msglevel = 0;
#define PR0 if (vd_msglevel > 0) PRN
#define PR1 if (vd_msglevel > 1) PRN
#define PR2 if (vd_msglevel > 2) PRN
#define VD_DUMP_DRING_ELEM(elem) \
PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \
elem->hdr.dstate, \
elem->payload.operation, \
elem->payload.status, \
elem->payload.nbytes, \
elem->payload.addr, \
elem->payload.ncookies);
char *
vd_decode_state(int state)
{
char *str;
#define CASE_STATE(_s) case _s: str = #_s; break;
switch (state) {
CASE_STATE(VD_STATE_INIT)
CASE_STATE(VD_STATE_VER)
CASE_STATE(VD_STATE_ATTR)
CASE_STATE(VD_STATE_DRING)
CASE_STATE(VD_STATE_RDX)
CASE_STATE(VD_STATE_DATA)
default: str = "unknown"; break;
}
#undef CASE_STATE
return (str);
}
void
vd_decode_tag(vio_msg_t *msg)
{
char *tstr, *sstr, *estr;
#define CASE_TYPE(_s) case _s: tstr = #_s; break;
switch (msg->tag.vio_msgtype) {
CASE_TYPE(VIO_TYPE_CTRL)
CASE_TYPE(VIO_TYPE_DATA)
CASE_TYPE(VIO_TYPE_ERR)
default: tstr = "unknown"; break;
}
#undef CASE_TYPE
#define CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
switch (msg->tag.vio_subtype) {
CASE_SUBTYPE(VIO_SUBTYPE_INFO)
CASE_SUBTYPE(VIO_SUBTYPE_ACK)
CASE_SUBTYPE(VIO_SUBTYPE_NACK)
default: sstr = "unknown"; break;
}
#undef CASE_SUBTYPE
#define CASE_ENV(_s) case _s: estr = #_s; break;
switch (msg->tag.vio_subtype_env) {
CASE_ENV(VIO_VER_INFO)
CASE_ENV(VIO_ATTR_INFO)
CASE_ENV(VIO_DRING_REG)
CASE_ENV(VIO_DRING_UNREG)
CASE_ENV(VIO_RDX)
CASE_ENV(VIO_PKT_DATA)
CASE_ENV(VIO_DESC_DATA)
CASE_ENV(VIO_DRING_DATA)
default: estr = "unknown"; break;
}
#undef CASE_ENV
PR1("(%x/%x/%x) message : (%s/%s/%s)",
msg->tag.vio_msgtype, msg->tag.vio_subtype,
msg->tag.vio_subtype_env, tstr, sstr, estr);
}
#else /* !DEBUG */
#define PR0(...)
#define PR1(...)
#define PR2(...)
#define VD_DUMP_DRING_ELEM(elem)
#define vd_decode_state(_s) (NULL)
#define vd_decode_tag(_s) (NULL)
#endif /* DEBUG */
/*
* Soft state structure for a vds instance
*/
typedef struct vds {
uint_t initialized; /* driver inst initialization flags */
dev_info_t *dip; /* driver inst devinfo pointer */
ldi_ident_t ldi_ident; /* driver's identifier for LDI */
mod_hash_t *vd_table; /* table of virtual disks served */
mdeg_node_spec_t *ispecp; /* mdeg node specification */
mdeg_handle_t mdeg; /* handle for MDEG operations */
} vds_t;
/*
* Types of descriptor-processing tasks
*/
typedef enum vd_task_type {
VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */
VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */
} vd_task_type_t;
/*
* Structure describing the task for processing a descriptor
*/
typedef struct vd_task {
struct vd *vd; /* vd instance task is for */
vd_task_type_t type; /* type of descriptor task */
int index; /* dring elem index for task */
vio_msg_t *msg; /* VIO message task is for */
size_t msglen; /* length of message content */
vd_dring_payload_t *request; /* request task will perform */
struct buf buf; /* buf(9s) for I/O request */
ldc_mem_handle_t mhdl; /* task memory handle */
} vd_task_t;
/*
* Soft state structure for a virtual disk instance
*/
typedef struct vd {
uint_t initialized; /* vdisk initialization flags */
vds_t *vds; /* server for this vdisk */
ddi_taskq_t *startq; /* queue for I/O start tasks */
ddi_taskq_t *completionq; /* queue for completion tasks */
ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */
char device_path[MAXPATHLEN + 1]; /* vdisk device */
dev_t dev[V_NUMPAR]; /* dev numbers for slices */
uint_t nslices; /* number of slices */
size_t vdisk_size; /* number of blocks in vdisk */
vd_disk_type_t vdisk_type; /* slice or entire disk */
vd_disk_label_t vdisk_label; /* EFI or VTOC label */
ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */
boolean_t pseudo; /* underlying pseudo dev */
boolean_t file; /* underlying file */
vnode_t *file_vnode; /* file vnode */
size_t file_size; /* file size */
struct dk_efi dk_efi; /* synthetic for slice type */
struct dk_geom dk_geom; /* synthetic for slice type */
struct vtoc vtoc; /* synthetic for slice type */
ldc_status_t ldc_state; /* LDC connection state */
ldc_handle_t ldc_handle; /* handle for LDC comm */
size_t max_msglen; /* largest LDC message len */
vd_state_t state; /* client handshake state */
uint8_t xfer_mode; /* transfer mode with client */
uint32_t sid; /* client's session ID */
uint64_t seq_num; /* message sequence number */
uint64_t dring_ident; /* identifier of dring */
ldc_dring_handle_t dring_handle; /* handle for dring ops */
uint32_t descriptor_size; /* num bytes in desc */
uint32_t dring_len; /* number of dring elements */
caddr_t dring; /* address of dring */
caddr_t vio_msgp; /* vio msg staging buffer */
vd_task_t inband_task; /* task for inband descriptor */
vd_task_t *dring_task; /* tasks dring elements */
kmutex_t lock; /* protects variables below */
boolean_t enabled; /* is vdisk enabled? */
boolean_t reset_state; /* reset connection state? */
boolean_t reset_ldc; /* reset LDC channel? */
} vd_t;
typedef struct vds_operation {
char *namep;
uint8_t operation;
int (*start)(vd_task_t *task);
void (*complete)(void *arg);
} vds_operation_t;
typedef struct vd_ioctl {
uint8_t operation; /* vdisk operation */
const char *operation_name; /* vdisk operation name */
size_t nbytes; /* size of operation buffer */
int cmd; /* corresponding ioctl cmd */
const char *cmd_name; /* ioctl cmd name */
void *arg; /* ioctl cmd argument */
/* convert input vd_buf to output ioctl_arg */
void (*copyin)(void *vd_buf, void *ioctl_arg);
/* convert input ioctl_arg to output vd_buf */
void (*copyout)(void *ioctl_arg, void *vd_buf);
} vd_ioctl_t;
/* Define trivial copyin/copyout conversion function flag */
#define VD_IDENTITY ((void (*)(void *, void *))-1)
static int vds_ldc_retries = VDS_RETRIES;
static int vds_ldc_delay = VDS_LDC_DELAY;
static int vds_dev_retries = VDS_RETRIES;
static int vds_dev_delay = VDS_DEV_DELAY;
static void *vds_state;
static uint64_t vds_operations; /* see vds_operation[] definition below */
static int vd_open_flags = VD_OPEN_FLAGS;
static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS;
/*
* Supported protocol version pairs, from highest (newest) to lowest (oldest)
*
* Each supported major version should appear only once, paired with (and only
* with) its highest supported minor version number (as the protocol requires
* supporting all lower minor version numbers as well)
*/
static const vio_ver_t vds_version[] = {{1, 0}};
static const size_t vds_num_versions =
sizeof (vds_version)/sizeof (vds_version[0]);
static void vd_free_dring_task(vd_t *vdp);
static int vd_setup_vd(vd_t *vd);
static boolean_t vd_enabled(vd_t *vd);
/*
* Function:
* vd_file_rw
*
* Description:
* Read or write to a disk on file.
*
* Parameters:
* vd - disk on which the operation is performed.
* slice - slice on which the operation is performed,
* VD_FILE_SLICE_NONE indicates that the operation
* is done on the raw disk.
* operation - operation to execute: read (VD_OP_BREAD) or
* write (VD_OP_BWRITE).
* data - buffer where data are read to or written from.
* blk - starting block for the operation.
* len - number of bytes to read or write.
*
* Return Code:
* n >= 0 - success, n indicates the number of bytes read
* or written.
* -1 - error.
*/
static ssize_t
vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
size_t len)
{
caddr_t maddr;
size_t offset, maxlen, moffset, mlen, n;
uint_t smflags;
enum seg_rw srw;
ASSERT(vd->file);
ASSERT(len > 0);
if (slice == VD_FILE_SLICE_NONE) {
/* raw disk access */
offset = blk * DEV_BSIZE;
} else {
ASSERT(slice >= 0 && slice < V_NUMPAR);
if (blk >= vd->vtoc.v_part[slice].p_size) {
/* address past the end of the slice */
PR0("req_addr (0x%lx) > psize (0x%lx)",
blk, vd->vtoc.v_part[slice].p_size);
return (0);
}
offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE;
/*
* If the requested size is greater than the size
* of the partition, truncate the read/write.
*/
maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE;
if (len > maxlen) {
PR0("I/O size truncated to %lu bytes from %lu bytes",
maxlen, len);
len = maxlen;
}
}
/*
* We have to ensure that we are reading/writing into the mmap
* range. If we have a partial disk image (e.g. an image of
* s0 instead s2) the system can try to access slices that
* are not included into the disk image.
*/
if ((offset + len) >= vd->file_size) {
PR0("offset + nbytes (0x%lx + 0x%lx) >= "
"file_size (0x%lx)", offset, len, vd->file_size);
return (-1);
}
srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
smflags = (operation == VD_OP_BREAD)? 0 :
(SM_WRITE | vd_file_write_flags);
n = len;
do {
/*
* segmap_getmapflt() returns a MAXBSIZE chunk which is
* MAXBSIZE aligned.
*/
moffset = offset & MAXBOFFSET;
mlen = MIN(MAXBSIZE - moffset, n);
maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
mlen, 1, srw);
/*
* Fault in the pages so we can check for error and ensure
* that we can safely used the mapped address.
*/
if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
F_SOFTLOCK, srw) != 0) {
(void) segmap_release(segkmap, maddr, 0);
return (-1);
}
if (operation == VD_OP_BREAD)
bcopy(maddr + moffset, data, mlen);
else
bcopy(data, maddr + moffset, mlen);
if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
F_SOFTUNLOCK, srw) != 0) {
(void) segmap_release(segkmap, maddr, 0);
return (-1);
}
if (segmap_release(segkmap, maddr, smflags) != 0)
return (-1);
n -= mlen;
offset += mlen;
data += mlen;
} while (n > 0);
return (len);
}
static int
vd_start_bio(vd_task_t *task)
{
int rv, status = 0;
vd_t *vd = task->vd;
vd_dring_payload_t *request = task->request;
struct buf *buf = &task->buf;
uint8_t mtype;
int slice;
ASSERT(vd != NULL);
ASSERT(request != NULL);
slice = request->slice;
ASSERT(slice < vd->nslices);
ASSERT((request->operation == VD_OP_BREAD) ||
(request->operation == VD_OP_BWRITE));
if (request->nbytes == 0)
return (EINVAL); /* no service for trivial requests */
PR1("%s %lu bytes at block %lu",
(request->operation == VD_OP_BREAD) ? "Read" : "Write",
request->nbytes, request->addr);
bioinit(buf);
buf->b_flags = B_BUSY;
buf->b_bcount = request->nbytes;
buf->b_lblkno = request->addr;
buf->b_edev = vd->dev[slice];
mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
/* Map memory exported by client */
status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
&(buf->b_un.b_addr), NULL);
if (status != 0) {
PR0("ldc_mem_map() returned err %d ", status);
biofini(buf);
return (status);
}
status = ldc_mem_acquire(task->mhdl, 0, buf->b_bcount);
if (status != 0) {
(void) ldc_mem_unmap(task->mhdl);
PR0("ldc_mem_acquire() returned err %d ", status);
biofini(buf);
return (status);
}
buf->b_flags |= (request->operation == VD_OP_BREAD) ? B_READ : B_WRITE;
/* Start the block I/O */
if (vd->file) {
rv = vd_file_rw(vd, slice, request->operation, buf->b_un.b_addr,
request->addr, request->nbytes);
if (rv < 0) {
request->nbytes = 0;
status = EIO;
} else {
request->nbytes = rv;
status = 0;
}
} else {
status = ldi_strategy(vd->ldi_handle[slice], buf);
if (status == 0)
return (EINPROGRESS); /* will complete on completionq */
}
/* Clean up after error */
rv = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
if (rv) {
PR0("ldc_mem_release() returned err %d ", rv);
}
rv = ldc_mem_unmap(task->mhdl);
if (rv) {
PR0("ldc_mem_unmap() returned err %d ", status);
}
biofini(buf);
return (status);
}
static int
send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
{
int status;
size_t nbytes;
do {
nbytes = msglen;
status = ldc_write(ldc_handle, msg, &nbytes);
if (status != EWOULDBLOCK)
break;
drv_usecwait(vds_ldc_delay);
} while (status == EWOULDBLOCK);
if (status != 0) {
if (status != ECONNRESET)
PR0("ldc_write() returned errno %d", status);
return (status);
} else if (nbytes != msglen) {
PR0("ldc_write() performed only partial write");
return (EIO);
}
PR1("SENT %lu bytes", msglen);
return (0);
}
static void
vd_need_reset(vd_t *vd, boolean_t reset_ldc)
{
mutex_enter(&vd->lock);
vd->reset_state = B_TRUE;
vd->reset_ldc = reset_ldc;
mutex_exit(&vd->lock);
}
/*
* Reset the state of the connection with a client, if needed; reset the LDC
* transport as well, if needed. This function should only be called from the
* "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
*/
static void
vd_reset_if_needed(vd_t *vd)
{
int status = 0;
mutex_enter(&vd->lock);
if (!vd->reset_state) {
ASSERT(!vd->reset_ldc);
mutex_exit(&vd->lock);
return;
}
mutex_exit(&vd->lock);
PR0("Resetting connection state with %s", VD_CLIENT(vd));
/*
* Let any asynchronous I/O complete before possibly pulling the rug
* out from under it; defer checking vd->reset_ldc, as one of the
* asynchronous tasks might set it
*/
ddi_taskq_wait(vd->completionq);
if (vd->file) {
status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred);
if (status) {
PR0("VOP_FSYNC returned errno %d", status);
}
}
if ((vd->initialized & VD_DRING) &&
((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
PR0("ldc_mem_dring_unmap() returned errno %d", status);
vd_free_dring_task(vd);
/* Free the staging buffer for msgs */
if (vd->vio_msgp != NULL) {
kmem_free(vd->vio_msgp, vd->max_msglen);
vd->vio_msgp = NULL;
}
/* Free the inband message buffer */
if (vd->inband_task.msg != NULL) {
kmem_free(vd->inband_task.msg, vd->max_msglen);
vd->inband_task.msg = NULL;
}
mutex_enter(&vd->lock);
if (vd->reset_ldc)
PR0("taking down LDC channel");
if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
PR0("ldc_down() returned errno %d", status);
vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
vd->state = VD_STATE_INIT;
vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */
/* Allocate the staging buffer */
vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
PR0("calling ldc_up\n");
(void) ldc_up(vd->ldc_handle);
vd->reset_state = B_FALSE;
vd->reset_ldc = B_FALSE;
mutex_exit(&vd->lock);
}
static void vd_recv_msg(void *arg);
static void
vd_mark_in_reset(vd_t *vd)
{
int status;
PR0("vd_mark_in_reset: marking vd in reset\n");
vd_need_reset(vd, B_FALSE);
status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
vd_need_reset(vd, B_TRUE);
return;
}
}
static int
vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
{
boolean_t accepted;
int status;
vd_dring_entry_t *elem = VD_DRING_ELEM(idx);
if (vd->reset_state)
return (0);
/* Acquire the element */
if (!vd->reset_state &&
(status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
if (status == ECONNRESET) {
vd_mark_in_reset(vd);
return (0);
} else {
PR0("ldc_mem_dring_acquire() returned errno %d",
status);
return (status);
}
}
/* Set the element's status and mark it done */
accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
if (accepted) {
elem->payload.nbytes = elem_nbytes;
elem->payload.status = elem_status;
elem->hdr.dstate = VIO_DESC_DONE;
} else {
/* Perhaps client timed out waiting for I/O... */
PR0("element %u no longer \"accepted\"", idx);
VD_DUMP_DRING_ELEM(elem);
}
/* Release the element */
if (!vd->reset_state &&
(status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
if (status == ECONNRESET) {
vd_mark_in_reset(vd);
return (0);
} else {
PR0("ldc_mem_dring_release() returned errno %d",
status);
return (status);
}
}
return (accepted ? 0 : EINVAL);
}
static void
vd_complete_bio(void *arg)
{
int status = 0;
vd_task_t *task = (vd_task_t *)arg;
vd_t *vd = task->vd;
vd_dring_payload_t *request = task->request;
struct buf *buf = &task->buf;
ASSERT(vd != NULL);
ASSERT(request != NULL);
ASSERT(task->msg != NULL);
ASSERT(task->msglen >= sizeof (*task->msg));
ASSERT(!vd->file);
/* Wait for the I/O to complete */
request->status = biowait(buf);
/* return back the number of bytes read/written */
request->nbytes = buf->b_bcount - buf->b_resid;
/* Release the buffer */
if (!vd->reset_state)
status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
if (status) {
PR0("ldc_mem_release() returned errno %d copying to "
"client", status);
if (status == ECONNRESET) {
vd_mark_in_reset(vd);
}
}
/* Unmap the memory, even if in reset */
status = ldc_mem_unmap(task->mhdl);
if (status) {
PR0("ldc_mem_unmap() returned errno %d copying to client",
status);
if (status == ECONNRESET) {
vd_mark_in_reset(vd);
}
}
biofini(buf);
/* Update the dring element for a dring client */
if (!vd->reset_state && (status == 0) &&
(vd->xfer_mode == VIO_DRING_MODE)) {
status = vd_mark_elem_done(vd, task->index,
request->status, request->nbytes);
if (status == ECONNRESET)
vd_mark_in_reset(vd);
}
/*
* If a transport error occurred, arrange to "nack" the message when
* the final task in the descriptor element range completes
*/
if (status != 0)
task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
/*
* Only the final task for a range of elements will respond to and
* free the message
*/
if (task->type == VD_NONFINAL_RANGE_TASK) {
return;
}
/*
* Send the "ack" or "nack" back to the client; if sending the message
* via LDC fails, arrange to reset both the connection state and LDC
* itself
*/
PR1("Sending %s",
(task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
if (!vd->reset_state) {
status = send_msg(vd->ldc_handle, task->msg, task->msglen);
switch (status) {
case 0:
break;
case ECONNRESET:
vd_mark_in_reset(vd);
break;
default:
PR0("initiating full reset");
vd_need_reset(vd, B_TRUE);
break;
}
}
}
static void
vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
{
VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
}
static void
vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
{
VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
}
static void
dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
{
DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
}
static void
vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
{
VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
}
static void
vd_get_efi_in(void *vd_buf, void *ioctl_arg)
{
vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
dk_efi->dki_lba = vd_efi->lba;
dk_efi->dki_length = vd_efi->length;
dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
}
static void
vd_get_efi_out(void *ioctl_arg, void *vd_buf)
{
int len;
vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
len = vd_efi->length;
DK_EFI2VD_EFI(dk_efi, vd_efi);
kmem_free(dk_efi->dki_data, len);
}
static void
vd_set_efi_in(void *vd_buf, void *ioctl_arg)
{
vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
VD_EFI2DK_EFI(vd_efi, dk_efi);
}
static void
vd_set_efi_out(void *ioctl_arg, void *vd_buf)
{
vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
kmem_free(dk_efi->dki_data, vd_efi->length);
}
static int
vd_read_vtoc(ldi_handle_t handle, struct vtoc *vtoc, vd_disk_label_t *label)
{
int status, rval;
struct dk_gpt *efi;
size_t efi_len;
*label = VD_DISK_LABEL_UNK;
status = ldi_ioctl(handle, DKIOCGVTOC, (intptr_t)vtoc,
(vd_open_flags | FKIOCTL), kcred, &rval);
if (status == 0) {
*label = VD_DISK_LABEL_VTOC;
return (0);
} else if (status != ENOTSUP) {
PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status);
return (status);
}
status = vds_efi_alloc_and_read(handle, &efi, &efi_len);
if (status) {
PR0("vds_efi_alloc_and_read returned error %d", status);
return (status);
}
*label = VD_DISK_LABEL_EFI;
vd_efi_to_vtoc(efi, vtoc);
vd_efi_free(efi, efi_len);
return (0);
}
static ushort_t
vd_lbl2cksum(struct dk_label *label)
{
int count;
ushort_t sum, *sp;
count = (sizeof (struct dk_label)) / (sizeof (short)) - 1;
sp = (ushort_t *)label;
sum = 0;
while (count--) {
sum ^= *sp++;
}
return (sum);
}
static int
vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
{
dk_efi_t *dk_ioc;
struct dk_label label;
struct vtoc *vtoc;
int i;
switch (vd->vdisk_label) {
case VD_DISK_LABEL_VTOC:
switch (cmd) {
case DKIOCGGEOM:
ASSERT(ioctl_arg != NULL);
bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
return (0);
case DKIOCGVTOC:
ASSERT(ioctl_arg != NULL);
bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
return (0);
case DKIOCSVTOC:
if (!vd->file)
return (ENOTSUP);
ASSERT(ioctl_arg != NULL);
vtoc = (struct vtoc *)ioctl_arg;
if (vtoc->v_sanity != VTOC_SANE ||
vtoc->v_sectorsz != DEV_BSIZE ||
vtoc->v_nparts != V_NUMPAR)
return (EINVAL);
bzero(&label, sizeof (label));
label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
label.dkl_acyl = vd->dk_geom.dkg_acyl;
label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
label.dkl_nhead = vd->dk_geom.dkg_nhead;
label.dkl_nsect = vd->dk_geom.dkg_nsect;
label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
label.dkl_apc = vd->dk_geom.dkg_apc;
label.dkl_rpm = vd->dk_geom.dkg_rpm;
label.dkl_write_reinstruct =
vd->dk_geom.dkg_write_reinstruct;
label.dkl_read_reinstruct =
vd->dk_geom.dkg_read_reinstruct;
label.dkl_vtoc.v_nparts = vtoc->v_nparts;
label.dkl_vtoc.v_sanity = vtoc->v_sanity;
label.dkl_vtoc.v_version = vtoc->v_version;
for (i = 0; i < vtoc->v_nparts; i++) {
label.dkl_vtoc.v_timestamp[i] =
vtoc->timestamp[i];
label.dkl_vtoc.v_part[i].p_tag =
vtoc->v_part[i].p_tag;
label.dkl_vtoc.v_part[i].p_flag =
vtoc->v_part[i].p_flag;
label.dkl_map[i].dkl_cylno =
vtoc->v_part[i].p_start /
(label.dkl_nhead * label.dkl_nsect);
label.dkl_map[i].dkl_nblk =
vtoc->v_part[i].p_size;
}
bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
LEN_DKL_ASCII);
bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
LEN_DKL_VVOL);
bcopy(vtoc->v_bootinfo, label.dkl_vtoc.v_bootinfo,
sizeof (vtoc->v_bootinfo));
/* re-compute checksum */
label.dkl_magic = DKL_MAGIC;
label.dkl_cksum = vd_lbl2cksum(&label);
/* write label to file */
if (VD_FILE_LABEL_WRITE(vd, &label) < 0)
return (EIO);
/* update the cached vdisk VTOC */
bcopy(vtoc, &vd->vtoc, sizeof (vd->vtoc));
return (0);
default:
return (ENOTSUP);
}
case VD_DISK_LABEL_EFI:
switch (cmd) {
case DKIOCGETEFI:
ASSERT(ioctl_arg != NULL);
dk_ioc = (dk_efi_t *)ioctl_arg;
if (dk_ioc->dki_length < vd->dk_efi.dki_length)
return (EINVAL);
bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data,
vd->dk_efi.dki_length);
return (0);
default:
return (ENOTSUP);
}
default:
return (ENOTSUP);
}
}
static int
vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
{
int rval = 0, status;
size_t nbytes = request->nbytes; /* modifiable copy */
ASSERT(request->slice < vd->nslices);
PR0("Performing %s", ioctl->operation_name);
/* Get data from client and convert, if necessary */
if (ioctl->copyin != NULL) {
ASSERT(nbytes != 0 && buf != NULL);
PR1("Getting \"arg\" data from client");
if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
request->cookie, request->ncookies,
LDC_COPY_IN)) != 0) {
PR0("ldc_mem_copy() returned errno %d "
"copying from client", status);
return (status);
}
/* Convert client's data, if necessary */
if (ioctl->copyin == VD_IDENTITY) /* use client buffer */
ioctl->arg = buf;
else /* convert client vdisk operation data to ioctl data */
(ioctl->copyin)(buf, (void *)ioctl->arg);
}
/*
* Handle single-slice block devices internally; otherwise, have the
* real driver perform the ioctl()
*/
if (vd->file || (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo)) {
if ((status = vd_do_slice_ioctl(vd, ioctl->cmd,
(void *)ioctl->arg)) != 0)
return (status);
} else if ((status = ldi_ioctl(vd->ldi_handle[request->slice],
ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
kcred, &rval)) != 0) {
PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status);
return (status);
}
#ifdef DEBUG
if (rval != 0) {
PR0("%s set rval = %d, which is not being returned to client",
ioctl->cmd_name, rval);
}
#endif /* DEBUG */
/* Convert data and send to client, if necessary */
if (ioctl->copyout != NULL) {
ASSERT(nbytes != 0 && buf != NULL);
PR1("Sending \"arg\" data to client");
/* Convert ioctl data to vdisk operation data, if necessary */
if (ioctl->copyout != VD_IDENTITY)
(ioctl->copyout)((void *)ioctl->arg, buf);
if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
request->cookie, request->ncookies,
LDC_COPY_OUT)) != 0) {
PR0("ldc_mem_copy() returned errno %d "
"copying to client", status);
return (status);
}
}
return (status);
}
#define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
static int
vd_ioctl(vd_task_t *task)
{
int i, status, rc;
void *buf = NULL;
struct dk_geom dk_geom = {0};
struct vtoc vtoc = {0};
struct dk_efi dk_efi = {0};
vd_t *vd = task->vd;
vd_dring_payload_t *request = task->request;
vd_ioctl_t ioctl[] = {
/* Command (no-copy) operations */
{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
NULL, NULL, NULL},
/* "Get" (copy-out) operations */
{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
NULL, VD_IDENTITY, VD_IDENTITY},
{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
RNDSIZE(vd_geom_t),
DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
&dk_geom, NULL, dk_geom2vd_geom},
{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
&vtoc, NULL, vtoc2vd_vtoc},
{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
&dk_efi, vd_get_efi_in, vd_get_efi_out},
/* "Set" (copy-in) operations */
{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
NULL, VD_IDENTITY, VD_IDENTITY},
{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
RNDSIZE(vd_geom_t),
DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
&dk_geom, vd_geom2dk_geom, NULL},
{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
&vtoc, vd_vtoc2vtoc, NULL},
{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
&dk_efi, vd_set_efi_in, vd_set_efi_out},
};
size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
ASSERT(vd != NULL);
ASSERT(request != NULL);
ASSERT(request->slice < vd->nslices);
/*
* Determine ioctl corresponding to caller's "operation" and
* validate caller's "nbytes"
*/
for (i = 0; i < nioctls; i++) {
if (request->operation == ioctl[i].operation) {
/* LDC memory operations require 8-byte multiples */
ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
if (request->operation == VD_OP_GET_EFI ||
request->operation == VD_OP_SET_EFI) {
if (request->nbytes >= ioctl[i].nbytes)
break;
PR0("%s: Expected at least nbytes = %lu, "
"got %lu", ioctl[i].operation_name,
ioctl[i].nbytes, request->nbytes);
return (EINVAL);
}
if (request->nbytes != ioctl[i].nbytes) {
PR0("%s: Expected nbytes = %lu, got %lu",
ioctl[i].operation_name, ioctl[i].nbytes,
request->nbytes);
return (EINVAL);
}
break;
}
}
ASSERT(i < nioctls); /* because "operation" already validated */
if (request->nbytes)
buf = kmem_zalloc(request->nbytes, KM_SLEEP);
status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
if (request->nbytes)
kmem_free(buf, request->nbytes);
if (!vd->file && vd->vdisk_type == VD_DISK_TYPE_DISK &&
(request->operation == VD_OP_SET_VTOC ||
request->operation == VD_OP_SET_EFI)) {
/* update disk information */
rc = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc,
&vd->vdisk_label);
if (rc != 0)
PR0("vd_read_vtoc return error %d", rc);
}
PR0("Returning %d", status);
return (status);
}
static int
vd_get_devid(vd_task_t *task)
{
vd_t *vd = task->vd;
vd_dring_payload_t *request = task->request;
vd_devid_t *vd_devid;
impl_devid_t *devid;
int status, bufid_len, devid_len, len;
int bufbytes;
PR1("Get Device ID, nbytes=%ld", request->nbytes);
if (vd->file) {
/* no devid for disk on file */
return (ENOENT);
}
if (ddi_lyr_get_devid(vd->dev[request->slice],
(ddi_devid_t *)&devid) != DDI_SUCCESS) {
/* the most common failure is that no devid is available */
PR2("No Device ID");
return (ENOENT);
}
bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
devid_len = DEVID_GETLEN(devid);
/*
* Save the buffer size here for use in deallocation.
* The actual number of bytes copied is returned in
* the 'nbytes' field of the request structure.
*/
bufbytes = request->nbytes;
vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
vd_devid->length = devid_len;
vd_devid->type = DEVID_GETTYPE(devid);
len = (devid_len > bufid_len)? bufid_len : devid_len;
bcopy(devid->did_id, vd_devid->id, len);
/* LDC memory operations require 8-byte multiples */
ASSERT(request->nbytes % sizeof (uint64_t) == 0);
if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
&request->nbytes, request->cookie, request->ncookies,
LDC_COPY_OUT)) != 0) {
PR0("ldc_mem_copy() returned errno %d copying to client",
status);
}
PR1("post mem_copy: nbytes=%ld", request->nbytes);
kmem_free(vd_devid, bufbytes);
ddi_devid_free((ddi_devid_t)devid);
return (status);
}
/*
* Define the supported operations once the functions for performing them have
* been defined
*/
static const vds_operation_t vds_operation[] = {
#define X(_s) #_s, _s
{X(VD_OP_BREAD), vd_start_bio, vd_complete_bio},
{X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio},
{X(VD_OP_FLUSH), vd_ioctl, NULL},
{X(VD_OP_GET_WCE), vd_ioctl, NULL},
{X(VD_OP_SET_WCE), vd_ioctl, NULL},
{X(VD_OP_GET_VTOC), vd_ioctl, NULL},
{X(VD_OP_SET_VTOC), vd_ioctl, NULL},
{X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL},
{X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL},
{X(VD_OP_GET_EFI), vd_ioctl, NULL},
{X(VD_OP_SET_EFI), vd_ioctl, NULL},
{X(VD_OP_GET_DEVID), vd_get_devid, NULL},
#undef X
};
static const size_t vds_noperations =
(sizeof (vds_operation))/(sizeof (vds_operation[0]));
/*
* Process a task specifying a client I/O request
*/
static int
vd_process_task(vd_task_t *task)
{
int i, status;
vd_t *vd = task->vd;
vd_dring_payload_t *request = task->request;
ASSERT(vd != NULL);
ASSERT(request != NULL);
/* Find the requested operation */
for (i = 0; i < vds_noperations; i++)
if (request->operation == vds_operation[i].operation)
break;
if (i == vds_noperations) {
PR0("Unsupported operation %u", request->operation);
return (ENOTSUP);
}
/* Handle client using absolute disk offsets */
if ((vd->vdisk_type == VD_DISK_TYPE_DISK) &&
(request->slice == UINT8_MAX))
request->slice = VD_ENTIRE_DISK_SLICE;
/* Range-check slice */
if (request->slice >= vd->nslices) {
PR0("Invalid \"slice\" %u (max %u) for virtual disk",
request->slice, (vd->nslices - 1));
return (EINVAL);
}
PR1("operation : %s", vds_operation[i].namep);
/* Start the operation */
if ((status = vds_operation[i].start(task)) != EINPROGRESS) {
PR0("operation : %s returned status %d",
vds_operation[i].namep, status);
request->status = status; /* op succeeded or failed */
return (0); /* but request completed */
}
ASSERT(vds_operation[i].complete != NULL); /* debug case */
if (vds_operation[i].complete == NULL) { /* non-debug case */
PR0("Unexpected return of EINPROGRESS "
"with no I/O completion handler");
request->status = EIO; /* operation failed */
return (0); /* but request completed */
}
PR1("operation : kick off taskq entry for %s", vds_operation[i].namep);
/* Queue a task to complete the operation */
status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete,
task, DDI_SLEEP);
/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
ASSERT(status == DDI_SUCCESS);
PR1("Operation in progress");
return (EINPROGRESS); /* completion handler will finish request */
}
/*
* Return true if the "type", "subtype", and "env" fields of the "tag" first
* argument match the corresponding remaining arguments; otherwise, return false
*/
boolean_t
vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
{
return ((tag->vio_msgtype == type) &&
(tag->vio_subtype == subtype) &&
(tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
}
/*
* Check whether the major/minor version specified in "ver_msg" is supported
* by this server.
*/
static boolean_t
vds_supported_version(vio_ver_msg_t *ver_msg)
{
for (int i = 0; i < vds_num_versions; i++) {
ASSERT(vds_version[i].major > 0);
ASSERT((i == 0) ||
(vds_version[i].major < vds_version[i-1].major));
/*
* If the major versions match, adjust the minor version, if
* necessary, down to the highest value supported by this
* server and return true so this message will get "ack"ed;
* the client should also support all minor versions lower
* than the value it sent
*/
if (ver_msg->ver_major == vds_version[i].major) {
if (ver_msg->ver_minor > vds_version[i].minor) {
PR0("Adjusting minor version from %u to %u",
ver_msg->ver_minor, vds_version[i].minor);
ver_msg->ver_minor = vds_version[i].minor;
}
return (B_TRUE);
}
/*
* If the message contains a higher major version number, set
* the message's major/minor versions to the current values
* and return false, so this message will get "nack"ed with
* these values, and the client will potentially try again
* with the same or a lower version
*/
if (ver_msg->ver_major > vds_version[i].major) {
ver_msg->ver_major = vds_version[i].major;
ver_msg->ver_minor = vds_version[i].minor;
return (B_FALSE);
}
/*
* Otherwise, the message's major version is less than the
* current major version, so continue the loop to the next
* (lower) supported version
*/
}
/*
* No common version was found; "ground" the version pair in the
* message to terminate negotiation
*/
ver_msg->ver_major = 0;
ver_msg->ver_minor = 0;
return (B_FALSE);
}
/*
* Process a version message from a client. vds expects to receive version
* messages from clients seeking service, but never issues version messages
* itself; therefore, vds can ACK or NACK client version messages, but does
* not expect to receive version-message ACKs or NACKs (and will treat such
* messages as invalid).
*/
static int
vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
VIO_VER_INFO)) {
return (ENOMSG); /* not a version message */
}
if (msglen != sizeof (*ver_msg)) {
PR0("Expected %lu-byte version message; "
"received %lu bytes", sizeof (*ver_msg), msglen);
return (EBADMSG);
}
if (ver_msg->dev_class != VDEV_DISK) {
PR0("Expected device class %u (disk); received %u",
VDEV_DISK, ver_msg->dev_class);
return (EBADMSG);
}
/*
* We're talking to the expected kind of client; set our device class
* for "ack/nack" back to the client
*/
ver_msg->dev_class = VDEV_DISK_SERVER;
/*
* Check whether the (valid) version message specifies a version
* supported by this server. If the version is not supported, return
* EBADMSG so the message will get "nack"ed; vds_supported_version()
* will have updated the message with a supported version for the
* client to consider
*/
if (!vds_supported_version(ver_msg))
return (EBADMSG);
/*
* A version has been agreed upon; use the client's SID for
* communication on this channel now
*/
ASSERT(!(vd->initialized & VD_SID));
vd->sid = ver_msg->tag.vio_sid;
vd->initialized |= VD_SID;
/*
* When multiple versions are supported, this function should store
* the negotiated major and minor version values in the "vd" data
* structure to govern further communication; in particular, note that
* the client might have specified a lower minor version for the
* agreed major version than specifed in the vds_version[] array. The
* following assertions should help remind future maintainers to make
* the appropriate changes to support multiple versions.
*/
ASSERT(vds_num_versions == 1);
ASSERT(ver_msg->ver_major == vds_version[0].major);
ASSERT(ver_msg->ver_minor == vds_version[0].minor);
PR0("Using major version %u, minor version %u",
ver_msg->ver_major, ver_msg->ver_minor);
return (0);
}
static int
vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg;
int status, retry = 0;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
VIO_ATTR_INFO)) {
PR0("Message is not an attribute message");
return (ENOMSG);
}
if (msglen != sizeof (*attr_msg)) {
PR0("Expected %lu-byte attribute message; "
"received %lu bytes", sizeof (*attr_msg), msglen);
return (EBADMSG);
}
if (attr_msg->max_xfer_sz == 0) {
PR0("Received maximum transfer size of 0 from client");
return (EBADMSG);
}
if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
(attr_msg->xfer_mode != VIO_DRING_MODE)) {
PR0("Client requested unsupported transfer mode");
return (EBADMSG);
}
/*
* check if the underlying disk is ready, if not try accessing
* the device again. Open the vdisk device and extract info
* about it, as this is needed to respond to the attr info msg
*/
if ((vd->initialized & VD_DISK_READY) == 0) {
PR0("Retry setting up disk (%s)", vd->device_path);
do {
status = vd_setup_vd(vd);
if (status != EAGAIN || ++retry > vds_dev_retries)
break;
/* incremental delay */
delay(drv_usectohz(vds_dev_delay));
/* if vdisk is no longer enabled - return error */
if (!vd_enabled(vd))
return (ENXIO);
} while (status == EAGAIN);
if (status)
return (ENXIO);
vd->initialized |= VD_DISK_READY;
ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
(vd->pseudo ? "yes" : "no"),
(vd->file ? "yes" : "no"),
vd->nslices);
}
/* Success: valid message and transfer mode */
vd->xfer_mode = attr_msg->xfer_mode;
if (vd->xfer_mode == VIO_DESC_MODE) {
/*
* The vd_dring_inband_msg_t contains one cookie; need room
* for up to n-1 more cookies, where "n" is the number of full
* pages plus possibly one partial page required to cover
* "max_xfer_sz". Add room for one more cookie if
* "max_xfer_sz" isn't an integral multiple of the page size.
* Must first get the maximum transfer size in bytes.
*/
size_t max_xfer_bytes = attr_msg->vdisk_block_size ?
attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
attr_msg->max_xfer_sz;
size_t max_inband_msglen =
sizeof (vd_dring_inband_msg_t) +
((max_xfer_bytes/PAGESIZE +
((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
(sizeof (ldc_mem_cookie_t)));
/*
* Set the maximum expected message length to
* accommodate in-band-descriptor messages with all
* their cookies
*/
vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
/*
* Initialize the data structure for processing in-band I/O
* request descriptors
*/
vd->inband_task.vd = vd;
vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
vd->inband_task.index = 0;
vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */
}
/* Return the device's block size and max transfer size to the client */
attr_msg->vdisk_block_size = DEV_BSIZE;
attr_msg->max_xfer_sz = vd->max_xfer_sz;
attr_msg->vdisk_size = vd->vdisk_size;
attr_msg->vdisk_type = vd->vdisk_type;
attr_msg->operations = vds_operations;
PR0("%s", VD_CLIENT(vd));
ASSERT(vd->dring_task == NULL);
return (0);
}
static int
vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
int status;
size_t expected;
ldc_mem_info_t dring_minfo;
vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
VIO_DRING_REG)) {
PR0("Message is not a register-dring message");
return (ENOMSG);
}
if (msglen < sizeof (*reg_msg)) {
PR0("Expected at least %lu-byte register-dring message; "
"received %lu bytes", sizeof (*reg_msg), msglen);
return (EBADMSG);
}
expected = sizeof (*reg_msg) +
(reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
if (msglen != expected) {
PR0("Expected %lu-byte register-dring message; "
"received %lu bytes", expected, msglen);
return (EBADMSG);
}
if (vd->initialized & VD_DRING) {
PR0("A dring was previously registered; only support one");
return (EBADMSG);
}
if (reg_msg->num_descriptors > INT32_MAX) {
PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
return (EBADMSG);
}
if (reg_msg->ncookies != 1) {
/*
* In addition to fixing the assertion in the success case
* below, supporting drings which require more than one
* "cookie" requires increasing the value of vd->max_msglen
* somewhere in the code path prior to receiving the message
* which results in calling this function. Note that without
* making this change, the larger message size required to
* accommodate multiple cookies cannot be successfully
* received, so this function will not even get called.
* Gracefully accommodating more dring cookies might
* reasonably demand exchanging an additional attribute or
* making a minor protocol adjustment
*/
PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
return (EBADMSG);
}
status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
reg_msg->ncookies, reg_msg->num_descriptors,
reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
if (status != 0) {
PR0("ldc_mem_dring_map() returned errno %d", status);
return (status);
}
/*
* To remove the need for this assertion, must call
* ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
* successful call to ldc_mem_dring_map()
*/
ASSERT(reg_msg->ncookies == 1);
if ((status =
ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
PR0("ldc_mem_dring_info() returned errno %d", status);
if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
PR0("ldc_mem_dring_unmap() returned errno %d", status);
return (status);
}
if (dring_minfo.vaddr == NULL) {
PR0("Descriptor ring virtual address is NULL");
return (ENXIO);
}
/* Initialize for valid message and mapped dring */
PR1("descriptor size = %u, dring length = %u",
vd->descriptor_size, vd->dring_len);
vd->initialized |= VD_DRING;
vd->dring_ident = 1; /* "There Can Be Only One" */
vd->dring = dring_minfo.vaddr;
vd->descriptor_size = reg_msg->descriptor_size;
vd->dring_len = reg_msg->num_descriptors;
reg_msg->dring_ident = vd->dring_ident;
/*
* Allocate and initialize a "shadow" array of data structures for
* tasks to process I/O requests in dring elements
*/
vd->dring_task =
kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
for (int i = 0; i < vd->dring_len; i++) {
vd->dring_task[i].vd = vd;
vd->dring_task[i].index = i;
vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload;
status = ldc_mem_alloc_handle(vd->ldc_handle,
&(vd->dring_task[i].mhdl));
if (status) {
PR0("ldc_mem_alloc_handle() returned err %d ", status);
return (ENXIO);
}
vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
}
return (0);
}
static int
vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
VIO_DRING_UNREG)) {
PR0("Message is not an unregister-dring message");
return (ENOMSG);
}
if (msglen != sizeof (*unreg_msg)) {
PR0("Expected %lu-byte unregister-dring message; "
"received %lu bytes", sizeof (*unreg_msg), msglen);
return (EBADMSG);
}
if (unreg_msg->dring_ident != vd->dring_ident) {
PR0("Expected dring ident %lu; received %lu",
vd->dring_ident, unreg_msg->dring_ident);
return (EBADMSG);
}
return (0);
}
static int
process_rdx_msg(vio_msg_t *msg, size_t msglen)
{
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
PR0("Message is not an RDX message");
return (ENOMSG);
}
if (msglen != sizeof (vio_rdx_msg_t)) {
PR0("Expected %lu-byte RDX message; received %lu bytes",
sizeof (vio_rdx_msg_t), msglen);
return (EBADMSG);
}
PR0("Valid RDX message");
return (0);
}
static int
vd_check_seq_num(vd_t *vd, uint64_t seq_num)
{
if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
PR0("Received seq_num %lu; expected %lu",
seq_num, (vd->seq_num + 1));
PR0("initiating soft reset");
vd_need_reset(vd, B_FALSE);
return (1);
}
vd->seq_num = seq_num;
vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */
return (0);
}
/*
* Return the expected size of an inband-descriptor message with all the
* cookies it claims to include
*/
static size_t
expected_inband_size(vd_dring_inband_msg_t *msg)
{
return ((sizeof (*msg)) +
(msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
}
/*
* Process an in-band descriptor message: used with clients like OBP, with
* which vds exchanges descriptors within VIO message payloads, rather than
* operating on them within a descriptor ring
*/
static int
vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
size_t expected;
vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
VIO_DESC_DATA)) {
PR1("Message is not an in-band-descriptor message");
return (ENOMSG);
}
if (msglen < sizeof (*desc_msg)) {
PR0("Expected at least %lu-byte descriptor message; "
"received %lu bytes", sizeof (*desc_msg), msglen);
return (EBADMSG);
}
if (msglen != (expected = expected_inband_size(desc_msg))) {
PR0("Expected %lu-byte descriptor message; "
"received %lu bytes", expected, msglen);
return (EBADMSG);
}
if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
return (EBADMSG);
/*
* Valid message: Set up the in-band descriptor task and process the
* request. Arrange to acknowledge the client's message, unless an
* error processing the descriptor task results in setting
* VIO_SUBTYPE_NACK
*/
PR1("Valid in-band-descriptor message");
msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
ASSERT(vd->inband_task.msg != NULL);
bcopy(msg, vd->inband_task.msg, msglen);
vd->inband_task.msglen = msglen;
/*
* The task request is now the payload of the message
* that was just copied into the body of the task.
*/
desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
vd->inband_task.request = &desc_msg->payload;
return (vd_process_task(&vd->inband_task));
}
static int
vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
vio_msg_t *msg, size_t msglen)
{
int status;
boolean_t ready;
vd_dring_entry_t *elem = VD_DRING_ELEM(idx);
/* Accept the updated dring element */
if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
PR0("ldc_mem_dring_acquire() returned errno %d", status);
return (status);
}
ready = (elem->hdr.dstate == VIO_DESC_READY);
if (ready) {
elem->hdr.dstate = VIO_DESC_ACCEPTED;
} else {
PR0("descriptor %u not ready", idx);
VD_DUMP_DRING_ELEM(elem);
}
if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
PR0("ldc_mem_dring_release() returned errno %d", status);
return (status);
}
if (!ready)
return (EBUSY);
/* Initialize a task and process the accepted element */
PR1("Processing dring element %u", idx);
vd->dring_task[idx].type = type;
/* duplicate msg buf for cookies etc. */
bcopy(msg, vd->dring_task[idx].msg, msglen);
vd->dring_task[idx].msglen = msglen;
if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS)
status = vd_mark_elem_done(vd, idx,
vd->dring_task[idx].request->status,
vd->dring_task[idx].request->nbytes);
return (status);
}
static int
vd_process_element_range(vd_t *vd, int start, int end,
vio_msg_t *msg, size_t msglen)
{
int i, n, nelem, status = 0;
boolean_t inprogress = B_FALSE;
vd_task_type_t type;
ASSERT(start >= 0);
ASSERT(end >= 0);
/*
* Arrange to acknowledge the client's message, unless an error
* processing one of the dring elements results in setting
* VIO_SUBTYPE_NACK
*/
msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
/*
* Process the dring elements in the range
*/
nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
((vio_dring_msg_t *)msg)->end_idx = i;
type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
status = vd_process_element(vd, type, i, msg, msglen);
if (status == EINPROGRESS)
inprogress = B_TRUE;
else if (status != 0)
break;
}
/*
* If some, but not all, operations of a multi-element range are in
* progress, wait for other operations to complete before returning
* (which will result in "ack" or "nack" of the message). Note that
* all outstanding operations will need to complete, not just the ones
* corresponding to the current range of dring elements; howevever, as
* this situation is an error case, performance is less critical.
*/
if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
ddi_taskq_wait(vd->completionq);
return (status);
}
static int
vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg;
ASSERT(msglen >= sizeof (msg->tag));
if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
VIO_DRING_DATA)) {
PR1("Message is not a dring-data message");
return (ENOMSG);
}
if (msglen != sizeof (*dring_msg)) {
PR0("Expected %lu-byte dring message; received %lu bytes",
sizeof (*dring_msg), msglen);
return (EBADMSG);
}
if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
return (EBADMSG);
if (dring_msg->dring_ident != vd->dring_ident) {
PR0("Expected dring ident %lu; received ident %lu",
vd->dring_ident, dring_msg->dring_ident);
return (EBADMSG);
}
if (dring_msg->start_idx >= vd->dring_len) {
PR0("\"start_idx\" = %u; must be less than %u",
dring_msg->start_idx, vd->dring_len);
return (EBADMSG);
}
if ((dring_msg->end_idx < 0) ||
(dring_msg->end_idx >= vd->dring_len)) {
PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
dring_msg->end_idx, vd->dring_len);
return (EBADMSG);
}
/* Valid message; process range of updated dring elements */
PR1("Processing descriptor range, start = %u, end = %u",
dring_msg->start_idx, dring_msg->end_idx);
return (vd_process_element_range(vd, dring_msg->start_idx,
dring_msg->end_idx, msg, msglen));
}
static int
recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
{
int retry, status;
size_t size = *nbytes;
for (retry = 0, status = ETIMEDOUT;
retry < vds_ldc_retries && status == ETIMEDOUT;
retry++) {
PR1("ldc_read() attempt %d", (retry + 1));
*nbytes = size;
status = ldc_read(ldc_handle, msg, nbytes);
}
if (status) {
PR0("ldc_read() returned errno %d", status);
if (status != ECONNRESET)
return (ENOMSG);
return (status);
} else if (*nbytes == 0) {
PR1("ldc_read() returned 0 and no message read");
return (ENOMSG);
}
PR1("RCVD %lu-byte message", *nbytes);
return (0);
}
static int
vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
int status;
PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
msg->tag.vio_subtype, msg->tag.vio_subtype_env);
#ifdef DEBUG
vd_decode_tag(msg);
#endif
/*
* Validate session ID up front, since it applies to all messages
* once set
*/
if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
PR0("Expected SID %u, received %u", vd->sid,
msg->tag.vio_sid);
return (EBADMSG);
}
PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
/*
* Process the received message based on connection state
*/
switch (vd->state) {
case VD_STATE_INIT: /* expect version message */
if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
return (status);
/* Version negotiated, move to that state */
vd->state = VD_STATE_VER;
return (0);
case VD_STATE_VER: /* expect attribute message */
if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
return (status);
/* Attributes exchanged, move to that state */
vd->state = VD_STATE_ATTR;
return (0);
case VD_STATE_ATTR:
switch (vd->xfer_mode) {
case VIO_DESC_MODE: /* expect RDX message */
if ((status = process_rdx_msg(msg, msglen)) != 0)
return (status);
/* Ready to receive in-band descriptors */
vd->state = VD_STATE_DATA;
return (0);
case VIO_DRING_MODE: /* expect register-dring message */
if ((status =
vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
return (status);
/* One dring negotiated, move to that state */
vd->state = VD_STATE_DRING;
return (0);
default:
ASSERT("Unsupported transfer mode");
PR0("Unsupported transfer mode");
return (ENOTSUP);
}
case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */
if ((status = process_rdx_msg(msg, msglen)) == 0) {
/* Ready to receive data */
vd->state = VD_STATE_DATA;
return (0);
} else if (status != ENOMSG) {
return (status);
}
/*
* If another register-dring message is received, stay in
* dring state in case the client sends RDX; although the
* protocol allows multiple drings, this server does not
* support using more than one
*/
if ((status =
vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
return (status);
/*
* Acknowledge an unregister-dring message, but reset the
* connection anyway: Although the protocol allows
* unregistering drings, this server cannot serve a vdisk
* without its only dring
*/
status = vd_process_dring_unreg_msg(vd, msg, msglen);
return ((status == 0) ? ENOTSUP : status);
case VD_STATE_DATA:
switch (vd->xfer_mode) {
case VIO_DESC_MODE: /* expect in-band-descriptor message */
return (vd_process_desc_msg(vd, msg, msglen));
case VIO_DRING_MODE: /* expect dring-data or unreg-dring */
/*
* Typically expect dring-data messages, so handle
* them first
*/
if ((status = vd_process_dring_msg(vd, msg,
msglen)) != ENOMSG)
return (status);
/*
* Acknowledge an unregister-dring message, but reset
* the connection anyway: Although the protocol
* allows unregistering drings, this server cannot
* serve a vdisk without its only dring
*/
status = vd_process_dring_unreg_msg(vd, msg, msglen);
return ((status == 0) ? ENOTSUP : status);
default:
ASSERT("Unsupported transfer mode");
PR0("Unsupported transfer mode");
return (ENOTSUP);
}
default:
ASSERT("Invalid client connection state");
PR0("Invalid client connection state");
return (ENOTSUP);
}
}
static int
vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
{
int status;
boolean_t reset_ldc = B_FALSE;
/*
* Check that the message is at least big enough for a "tag", so that
* message processing can proceed based on tag-specified message type
*/
if (msglen < sizeof (vio_msg_tag_t)) {
PR0("Received short (%lu-byte) message", msglen);
/* Can't "nack" short message, so drop the big hammer */
PR0("initiating full reset");
vd_need_reset(vd, B_TRUE);
return (EBADMSG);
}
/*
* Process the message
*/
switch (status = vd_do_process_msg(vd, msg, msglen)) {
case 0:
/* "ack" valid, successfully-processed messages */
msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
break;
case EINPROGRESS:
/* The completion handler will "ack" or "nack" the message */
return (EINPROGRESS);
case ENOMSG:
PR0("Received unexpected message");
_NOTE(FALLTHROUGH);
case EBADMSG:
case ENOTSUP:
/* "nack" invalid messages */
msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
break;
default:
/* "nack" failed messages */
msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
/* An LDC error probably occurred, so try resetting it */
reset_ldc = B_TRUE;
break;
}
PR1("\tResulting in state %d (%s)", vd->state,
vd_decode_state(vd->state));
/* Send the "ack" or "nack" to the client */
PR1("Sending %s",
(msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
if (send_msg(vd->ldc_handle, msg, msglen) != 0)
reset_ldc = B_TRUE;
/* Arrange to reset the connection for nack'ed or failed messages */
if ((status != 0) || reset_ldc) {
PR0("initiating %s reset",
(reset_ldc) ? "full" : "soft");
vd_need_reset(vd, reset_ldc);
}
return (status);
}
static boolean_t
vd_enabled(vd_t *vd)
{
boolean_t enabled;
mutex_enter(&vd->lock);
enabled = vd->enabled;
mutex_exit(&vd->lock);
return (enabled);
}
static void
vd_recv_msg(void *arg)
{
vd_t *vd = (vd_t *)arg;
int rv = 0, status = 0;
ASSERT(vd != NULL);
PR2("New task to receive incoming message(s)");
while (vd_enabled(vd) && status == 0) {
size_t msglen, msgsize;
ldc_status_t lstatus;
/*
* Receive and process a message
*/
vd_reset_if_needed(vd); /* can change vd->max_msglen */
/*
* check if channel is UP - else break out of loop
*/
status = ldc_status(vd->ldc_handle, &lstatus);
if (lstatus != LDC_UP) {
PR0("channel not up (status=%d), exiting recv loop\n",
lstatus);
break;
}
ASSERT(vd->max_msglen != 0);
msgsize = vd->max_msglen; /* stable copy for alloc/free */
msglen = msgsize; /* actual len after recv_msg() */
status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
switch (status) {
case 0:
rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
msglen);
/* check if max_msglen changed */
if (msgsize != vd->max_msglen) {
PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
msgsize, vd->max_msglen);
kmem_free(vd->vio_msgp, msgsize);
vd->vio_msgp =
kmem_alloc(vd->max_msglen, KM_SLEEP);
}
if (rv == EINPROGRESS)
continue;
break;
case ENOMSG:
break;
case ECONNRESET:
PR0("initiating soft reset (ECONNRESET)\n");
vd_need_reset(vd, B_FALSE);
status = 0;
break;
default:
/* Probably an LDC failure; arrange to reset it */
PR0("initiating full reset (status=0x%x)", status);
vd_need_reset(vd, B_TRUE);
break;
}
}
PR2("Task finished");
}
static uint_t
vd_handle_ldc_events(uint64_t event, caddr_t arg)
{
vd_t *vd = (vd_t *)(void *)arg;
int status;
ASSERT(vd != NULL);
if (!vd_enabled(vd))
return (LDC_SUCCESS);
if (event & LDC_EVT_DOWN) {
PR0("LDC_EVT_DOWN: LDC channel went down");
vd_need_reset(vd, B_TRUE);
status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
DDI_SLEEP);
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
vd_need_reset(vd, B_TRUE);
}
}
if (event & LDC_EVT_RESET) {
PR0("LDC_EVT_RESET: LDC channel was reset");
if (vd->state != VD_STATE_INIT) {
PR0("scheduling full reset");
vd_need_reset(vd, B_FALSE);
status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
vd, DDI_SLEEP);
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
vd_need_reset(vd, B_TRUE);
}
} else {
PR0("channel already reset, ignoring...\n");
PR0("doing ldc up...\n");
(void) ldc_up(vd->ldc_handle);
}
return (LDC_SUCCESS);
}
if (event & LDC_EVT_UP) {
PR0("EVT_UP: LDC is up\nResetting client connection state");
PR0("initiating soft reset");
vd_need_reset(vd, B_FALSE);
status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
vd, DDI_SLEEP);
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
vd_need_reset(vd, B_TRUE);
return (LDC_SUCCESS);
}
}
if (event & LDC_EVT_READ) {
int status;
PR1("New data available");
/* Queue a task to receive the new data */
status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
DDI_SLEEP);
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
vd_need_reset(vd, B_TRUE);
}
}
return (LDC_SUCCESS);
}
static uint_t
vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
{
_NOTE(ARGUNUSED(key, val))
(*((uint_t *)arg))++;
return (MH_WALK_TERMINATE);
}
static int
vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
uint_t vd_present = 0;
minor_t instance;
vds_t *vds;
switch (cmd) {
case DDI_DETACH:
/* the real work happens below */
break;
case DDI_SUSPEND:
PR0("No action required for DDI_SUSPEND");
return (DDI_SUCCESS);
default:
PR0("Unrecognized \"cmd\"");
return (DDI_FAILURE);
}
ASSERT(cmd == DDI_DETACH);
instance = ddi_get_instance(dip);
if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
PR0("Could not get state for instance %u", instance);
ddi_soft_state_free(vds_state, instance);
return (DDI_FAILURE);
}
/* Do no detach when serving any vdisks */
mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
if (vd_present) {
PR0("Not detaching because serving vdisks");
return (DDI_FAILURE);
}
PR0("Detaching");
if (vds->initialized & VDS_MDEG) {
(void) mdeg_unregister(vds->mdeg);
kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
vds->ispecp = NULL;
vds->mdeg = NULL;
}
if (vds->initialized & VDS_LDI)
(void) ldi_ident_release(vds->ldi_ident);
mod_hash_destroy_hash(vds->vd_table);
ddi_soft_state_free(vds_state, instance);
return (DDI_SUCCESS);
}
static boolean_t
is_pseudo_device(dev_info_t *dip)
{
dev_info_t *parent, *root = ddi_root_node();
for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
parent = ddi_get_parent(parent)) {
if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
return (B_TRUE);
}
return (B_FALSE);
}
static int
vd_setup_full_disk(vd_t *vd)
{
int rval, status;
major_t major = getmajor(vd->dev[0]);
minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
struct dk_minfo dk_minfo;
/*
* At this point, vdisk_size is set to the size of partition 2 but
* this does not represent the size of the disk because partition 2
* may not cover the entire disk and its size does not include reserved
* blocks. So we update vdisk_size to be the size of the entire disk.
*/
if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
(intptr_t)&dk_minfo, (vd_open_flags | FKIOCTL),
kcred, &rval)) != 0) {
PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
status);
return (status);
}
vd->vdisk_size = dk_minfo.dki_capacity;
/* Set full-disk parameters */
vd->vdisk_type = VD_DISK_TYPE_DISK;
vd->nslices = (sizeof (vd->dev))/(sizeof (vd->dev[0]));
/* Move dev number and LDI handle to entire-disk-slice array elements */
vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0];
vd->dev[0] = 0;
vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0];
vd->ldi_handle[0] = NULL;
/* Initialize device numbers for remaining slices and open them */
for (int slice = 0; slice < vd->nslices; slice++) {
/*
* Skip the entire-disk slice, as it's already open and its
* device known
*/
if (slice == VD_ENTIRE_DISK_SLICE)
continue;
ASSERT(vd->dev[slice] == 0);
ASSERT(vd->ldi_handle[slice] == NULL);
/*
* Construct the device number for the current slice
*/
vd->dev[slice] = makedevice(major, (minor + slice));
/*
* Open all slices of the disk to serve them to the client.
* Slices are opened exclusively to prevent other threads or
* processes in the service domain from performing I/O to
* slices being accessed by a client. Failure to open a slice
* results in vds not serving this disk, as the client could
* attempt (and should be able) to access any slice immediately.
* Any slices successfully opened before a failure will get
* closed by vds_destroy_vd() as a result of the error returned
* by this function.
*
* We need to do the open with FNDELAY so that opening an empty
* slice does not fail.
*/
PR0("Opening device major %u, minor %u = slice %u",
major, minor, slice);
if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
vd_open_flags | FNDELAY, kcred, &vd->ldi_handle[slice],
vd->vds->ldi_ident)) != 0) {
PRN("ldi_open_by_dev() returned errno %d "
"for slice %u", status, slice);
/* vds_destroy_vd() will close any open slices */
vd->ldi_handle[slice] = NULL;
return (status);
}
}
return (0);
}
static int
vd_setup_partition_efi(vd_t *vd)
{
efi_gpt_t *gpt;
efi_gpe_t *gpe;
struct uuid uuid = EFI_RESERVED;
uint32_t crc;
int length;
length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t);
gpt = kmem_zalloc(length, KM_SLEEP);
gpe = (efi_gpe_t *)(gpt + 1);
gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL);
gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
vd->dk_efi.dki_lba = 0;
vd->dk_efi.dki_length = length;
vd->dk_efi.dki_data = gpt;
return (0);
}
static int
vd_setup_file(vd_t *vd)
{
int i, rval, status;
ushort_t sum;
vattr_t vattr;
dev_t dev;
char *file_path = vd->device_path;
char dev_path[MAXPATHLEN + 1];
ldi_handle_t lhandle;
struct dk_cinfo dk_cinfo;
struct dk_label label;
/* make sure the file is valid */
if ((status = lookupname(file_path, UIO_SYSSPACE, FOLLOW,
NULLVPP, &vd->file_vnode)) != 0) {
PRN("Cannot lookup file(%s) errno %d", file_path, status);
return (status);
}
if (vd->file_vnode->v_type != VREG) {
PRN("Invalid file type (%s)\n", file_path);
VN_RELE(vd->file_vnode);
return (EBADF);
}
VN_RELE(vd->file_vnode);
if ((status = vn_open(file_path, UIO_SYSSPACE, vd_open_flags | FOFFMAX,
0, &vd->file_vnode, 0, 0)) != 0) {
PRN("vn_open(%s) = errno %d", file_path, status);
return (status);
}
/*
* We set vd->file now so that vds_destroy_vd will take care of
* closing the file and releasing the vnode in case of an error.
*/
vd->file = B_TRUE;
vd->pseudo = B_FALSE;
vattr.va_mask = AT_SIZE;
if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred)) != 0) {
PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
return (EIO);
}
vd->file_size = vattr.va_size;
/* size should be at least sizeof(dk_label) */
if (vd->file_size < sizeof (struct dk_label)) {
PRN("Size of file has to be at least %ld bytes",
sizeof (struct dk_label));
return (EIO);
}
if (vd->file_vnode->v_flag & VNOMAP) {
PRN("File %s cannot be mapped", file_path);
return (EIO);
}
/* read label from file */
if (VD_FILE_LABEL_READ(vd, &label) < 0) {
PRN("Can't read label from %s", file_path);
return (EIO);
}
/* label checksum */
sum = vd_lbl2cksum(&label);
if (label.dkl_magic != DKL_MAGIC || label.dkl_cksum != sum) {
PR0("%s has an invalid disk label "
"(magic=%x cksum=%x (expect %x))",
file_path, label.dkl_magic, label.dkl_cksum, sum);
/* default label */
bzero(&label, sizeof (struct dk_label));
/*
* We must have a resonable number of cylinders and sectors so
* that newfs can run using default values.
*
* if (disk_size < 2MB)
* phys_cylinders = disk_size / 100K
* else
* phys_cylinders = disk_size / 300K
*
* phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
* alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
* data_cylinders = phys_cylinders - alt_cylinders
*
* sectors = disk_size / (phys_cylinders * blk_size)
*/
if (vd->file_size < (2 * 1024 * 1024))
label.dkl_pcyl = vd->file_size / (100 * 1024);
else
label.dkl_pcyl = vd->file_size / (300 * 1024);
if (label.dkl_pcyl == 0)
label.dkl_pcyl = 1;
if (label.dkl_pcyl > 2)
label.dkl_acyl = 2;
else
label.dkl_acyl = 0;
label.dkl_nsect = vd->file_size /
(DEV_BSIZE * label.dkl_pcyl);
label.dkl_ncyl = label.dkl_pcyl - label.dkl_acyl;
label.dkl_nhead = 1;
label.dkl_write_reinstruct = 0;
label.dkl_read_reinstruct = 0;
label.dkl_rpm = 7200;
label.dkl_apc = 0;
label.dkl_intrlv = 0;
label.dkl_magic = DKL_MAGIC;
PR0("requested disk size: %ld bytes\n", vd->file_size);
PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label.dkl_pcyl,
label.dkl_nhead, label.dkl_nsect);
PR0("provided disk size: %ld bytes\n", (uint64_t)
(label.dkl_pcyl *
label.dkl_nhead * label.dkl_nsect * DEV_BSIZE));
/*
* We must have a correct label name otherwise format(1m) will
* not recognized the disk as labeled.
*/
(void) snprintf(label.dkl_asciilabel, LEN_DKL_ASCII,
"SUNVDSK cyl %d alt %d hd %d sec %d",
label.dkl_ncyl, label.dkl_acyl, label.dkl_nhead,
label.dkl_nsect);
/* default VTOC */
label.dkl_vtoc.v_version = V_VERSION;
label.dkl_vtoc.v_nparts = V_NUMPAR;
label.dkl_vtoc.v_sanity = VTOC_SANE;
label.dkl_vtoc.v_part[2].p_tag = V_BACKUP;
label.dkl_map[2].dkl_cylno = 0;
label.dkl_map[2].dkl_nblk = label.dkl_ncyl *
label.dkl_nhead * label.dkl_nsect;
label.dkl_map[0] = label.dkl_map[2];
label.dkl_map[0] = label.dkl_map[2];
label.dkl_cksum = vd_lbl2cksum(&label);
/* write default label to file */
if (VD_FILE_LABEL_WRITE(vd, &label) < 0) {
PRN("Can't write label to %s", file_path);
return (EIO);
}
}
vd->nslices = label.dkl_vtoc.v_nparts;
/* sector size = block size = DEV_BSIZE */
vd->vdisk_size = (label.dkl_pcyl *
label.dkl_nhead * label.dkl_nsect) / DEV_BSIZE;
vd->vdisk_type = VD_DISK_TYPE_DISK;
vd->vdisk_label = VD_DISK_LABEL_VTOC;
vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
/* Get max_xfer_sz from the device where the file is */
dev = vd->file_vnode->v_vfsp->vfs_dev;
dev_path[0] = NULL;
if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
PR0("underlying device = %s\n", dev_path);
}
if ((status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD,
kcred, &lhandle, vd->vds->ldi_ident)) != 0) {
PR0("ldi_open_by_dev() returned errno %d for device %s",
status, dev_path);
} else {
if ((status = ldi_ioctl(lhandle, DKIOCINFO,
(intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
&rval)) != 0) {
PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
status, dev_path);
} else {
/*
* Store the device's max transfer size for
* return to the client
*/
vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
}
PR0("close the device %s", dev_path);
(void) ldi_close(lhandle, FREAD, kcred);
}
PR0("using for file %s, dev %s, max_xfer = %u blks",
file_path, dev_path, vd->max_xfer_sz);
vd->dk_geom.dkg_ncyl = label.dkl_ncyl;
vd->dk_geom.dkg_acyl = label.dkl_acyl;
vd->dk_geom.dkg_pcyl = label.dkl_pcyl;
vd->dk_geom.dkg_nhead = label.dkl_nhead;
vd->dk_geom.dkg_nsect = label.dkl_nsect;
vd->dk_geom.dkg_intrlv = label.dkl_intrlv;
vd->dk_geom.dkg_apc = label.dkl_apc;
vd->dk_geom.dkg_rpm = label.dkl_rpm;
vd->dk_geom.dkg_write_reinstruct = label.dkl_write_reinstruct;
vd->dk_geom.dkg_read_reinstruct = label.dkl_read_reinstruct;
vd->vtoc.v_sanity = label.dkl_vtoc.v_sanity;
vd->vtoc.v_version = label.dkl_vtoc.v_version;
vd->vtoc.v_sectorsz = DEV_BSIZE;
vd->vtoc.v_nparts = label.dkl_vtoc.v_nparts;
bcopy(label.dkl_vtoc.v_volume, vd->vtoc.v_volume,
LEN_DKL_VVOL);
bcopy(label.dkl_asciilabel, vd->vtoc.v_asciilabel,
LEN_DKL_ASCII);
for (i = 0; i < vd->nslices; i++) {
vd->vtoc.timestamp[i] = label.dkl_vtoc.v_timestamp[i];
vd->vtoc.v_part[i].p_tag = label.dkl_vtoc.v_part[i].p_tag;
vd->vtoc.v_part[i].p_flag = label.dkl_vtoc.v_part[i].p_flag;
vd->vtoc.v_part[i].p_start = label.dkl_map[i].dkl_cylno *
label.dkl_nhead * label.dkl_nsect;
vd->vtoc.v_part[i].p_size = label.dkl_map[i].dkl_nblk;
vd->ldi_handle[i] = NULL;
vd->dev[i] = NULL;
}
return (0);
}
static int
vd_setup_vd(vd_t *vd)
{
int rval, status;
dev_info_t *dip;
struct dk_cinfo dk_cinfo;
char *device_path = vd->device_path;
/*
* We need to open with FNDELAY so that opening an empty partition
* does not fail.
*/
if ((status = ldi_open_by_name(device_path, vd_open_flags | FNDELAY,
kcred, &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
vd->ldi_handle[0] = NULL;
/* this may not be a device try opening as a file */
if (status == ENXIO || status == ENODEV)
status = vd_setup_file(vd);
if (status) {
PRN("Cannot use device/file (%s), errno=%d\n",
device_path, status);
if (status == ENXIO || status == ENODEV ||
status == ENOENT) {
return (EAGAIN);
}
}
return (status);
}
/*
* nslices must be updated now so that vds_destroy_vd() will close
* the slice we have just opened in case of an error.
*/
vd->nslices = 1;
vd->file = B_FALSE;
/* Get device number and size of backing device */
if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
PRN("ldi_get_dev() returned errno %d for %s",
status, device_path);
return (status);
}
if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
PRN("ldi_get_size() failed for %s", device_path);
return (EIO);
}
vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */
/* Verify backing device supports dk_cinfo, dk_geom, and vtoc */
if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
(intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
&rval)) != 0) {
PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
status, device_path);
return (status);
}
if (dk_cinfo.dki_partition >= V_NUMPAR) {
PRN("slice %u >= maximum slice %u for %s",
dk_cinfo.dki_partition, V_NUMPAR, device_path);
return (EIO);
}
status = vd_read_vtoc(vd->ldi_handle[0], &vd->vtoc, &vd->vdisk_label);
if (status != 0) {
PRN("vd_read_vtoc returned errno %d for %s",
status, device_path);
return (status);
}
if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
(status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
(intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL),
kcred, &rval)) != 0) {
PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
status, device_path);
return (status);
}
/* Store the device's max transfer size for return to the client */
vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
/* Determine if backing device is a pseudo device */
if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
dev_to_instance(vd->dev[0]), 0)) == NULL) {
PRN("%s is no longer accessible", device_path);
return (EIO);
}
vd->pseudo = is_pseudo_device(dip);
ddi_release_devi(dip);
if (vd->pseudo) {
vd->vdisk_type = VD_DISK_TYPE_SLICE;
vd->nslices = 1;
return (0); /* ...and we're done */
}
/* If slice is entire-disk slice, initialize for full disk */
if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
return (vd_setup_full_disk(vd));
/* Otherwise, we have a non-entire slice of a device */
vd->vdisk_type = VD_DISK_TYPE_SLICE;
vd->nslices = 1;
if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
status = vd_setup_partition_efi(vd);
return (status);
}
/* Initialize dk_geom structure for single-slice device */
if (vd->dk_geom.dkg_nsect == 0) {
PRN("%s geometry claims 0 sectors per track", device_path);
return (EIO);
}
if (vd->dk_geom.dkg_nhead == 0) {
PRN("%s geometry claims 0 heads", device_path);
return (EIO);
}
vd->dk_geom.dkg_ncyl =
vd->vdisk_size/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
vd->dk_geom.dkg_acyl = 0;
vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
/* Initialize vtoc structure for single-slice device */
bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
vd->vtoc.v_nparts = 1;
vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
vd->vtoc.v_part[0].p_flag = 0;
vd->vtoc.v_part[0].p_start = 0;
vd->vtoc.v_part[0].p_size = vd->vdisk_size;
bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
return (0);
}
static int
vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id,
vd_t **vdp)
{
char tq_name[TASKQ_NAMELEN];
int status;
ddi_iblock_cookie_t iblock = NULL;
ldc_attr_t ldc_attr;
vd_t *vd;
ASSERT(vds != NULL);
ASSERT(device_path != NULL);
ASSERT(vdp != NULL);
PR0("Adding vdisk for %s", device_path);
if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
PRN("No memory for virtual disk");
return (EAGAIN);
}
*vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */
vd->vds = vds;
(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
/* Open vdisk and initialize parameters */
if ((status = vd_setup_vd(vd)) == 0) {
vd->initialized |= VD_DISK_READY;
ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
(vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
vd->nslices);
} else {
if (status != EAGAIN)
return (status);
}
/* Initialize locking */
if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
&iblock) != DDI_SUCCESS) {
PRN("Could not get iblock cookie.");
return (EIO);
}
mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
vd->initialized |= VD_LOCKING;
/* Create start and completion task queues for the vdisk */
(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
PR1("tq_name = %s", tq_name);
if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
TASKQ_DEFAULTPRI, 0)) == NULL) {
PRN("Could not create task queue");
return (EIO);
}
(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
PR1("tq_name = %s", tq_name);
if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
TASKQ_DEFAULTPRI, 0)) == NULL) {
PRN("Could not create task queue");
return (EIO);
}
vd->enabled = 1; /* before callback can dispatch to startq */
/* Bring up LDC */
ldc_attr.devclass = LDC_DEV_BLK_SVC;
ldc_attr.instance = ddi_get_instance(vds->dip);
ldc_attr.mode = LDC_MODE_UNRELIABLE;
ldc_attr.mtu = VD_LDC_MTU;
if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
PRN("Could not initialize LDC channel %lu, "
"init failed with error %d", ldc_id, status);
return (status);
}
vd->initialized |= VD_LDC;
if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
(caddr_t)vd)) != 0) {
PRN("Could not initialize LDC channel %lu,"
"reg_callback failed with error %d", ldc_id, status);
return (status);
}
if ((status = ldc_open(vd->ldc_handle)) != 0) {
PRN("Could not initialize LDC channel %lu,"
"open failed with error %d", ldc_id, status);
return (status);
}
if ((status = ldc_up(vd->ldc_handle)) != 0) {
PR0("ldc_up() returned errno %d", status);
}
/* Allocate the inband task memory handle */
status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
if (status) {
PRN("Could not initialize LDC channel %lu,"
"alloc_handle failed with error %d", ldc_id, status);
return (ENXIO);
}
/* Add the successfully-initialized vdisk to the server's table */
if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
PRN("Error adding vdisk ID %lu to table", id);
return (EIO);
}
/* Allocate the staging buffer */
vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */
vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
/* store initial state */
vd->state = VD_STATE_INIT;
return (0);
}
static void
vd_free_dring_task(vd_t *vdp)
{
if (vdp->dring_task != NULL) {
ASSERT(vdp->dring_len != 0);
/* Free all dring_task memory handles */
for (int i = 0; i < vdp->dring_len; i++) {
(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
vdp->dring_task[i].msg = NULL;
}
kmem_free(vdp->dring_task,
(sizeof (*vdp->dring_task)) * vdp->dring_len);
vdp->dring_task = NULL;
}
}
/*
* Destroy the state associated with a virtual disk
*/
static void
vds_destroy_vd(void *arg)
{
vd_t *vd = (vd_t *)arg;
int retry = 0, rv;
if (vd == NULL)
return;
PR0("Destroying vdisk state");
if (vd->dk_efi.dki_data != NULL)
kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length);
/* Disable queuing requests for the vdisk */
if (vd->initialized & VD_LOCKING) {
mutex_enter(&vd->lock);
vd->enabled = 0;
mutex_exit(&vd->lock);
}
/* Drain and destroy start queue (*before* destroying completionq) */
if (vd->startq != NULL)
ddi_taskq_destroy(vd->startq); /* waits for queued tasks */
/* Drain and destroy completion queue (*before* shutting down LDC) */
if (vd->completionq != NULL)
ddi_taskq_destroy(vd->completionq); /* waits for tasks */
vd_free_dring_task(vd);
/* Free the inband task memory handle */
(void) ldc_mem_free_handle(vd->inband_task.mhdl);
/* Shut down LDC */
if (vd->initialized & VD_LDC) {
/* unmap the dring */
if (vd->initialized & VD_DRING)
(void) ldc_mem_dring_unmap(vd->dring_handle);
/* close LDC channel - retry on EAGAIN */
while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
if (++retry > vds_ldc_retries) {
PR0("Timed out closing channel");
break;
}
drv_usecwait(vds_ldc_delay);
}
if (rv == 0) {
(void) ldc_unreg_callback(vd->ldc_handle);
(void) ldc_fini(vd->ldc_handle);
} else {
/*
* Closing the LDC channel has failed. Ideally we should
* fail here but there is no Zeus level infrastructure
* to handle this. The MD has already been changed and
* we have to do the close. So we try to do as much
* clean up as we can.
*/
(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
drv_usecwait(vds_ldc_delay);
}
}
/* Free the staging buffer for msgs */
if (vd->vio_msgp != NULL) {
kmem_free(vd->vio_msgp, vd->max_msglen);
vd->vio_msgp = NULL;
}
/* Free the inband message buffer */
if (vd->inband_task.msg != NULL) {
kmem_free(vd->inband_task.msg, vd->max_msglen);
vd->inband_task.msg = NULL;
}
if (vd->file) {
/* Close file */
(void) VOP_CLOSE(vd->file_vnode, vd_open_flags, 1,
0, kcred);
VN_RELE(vd->file_vnode);
} else {
/* Close any open backing-device slices */
for (uint_t slice = 0; slice < vd->nslices; slice++) {
if (vd->ldi_handle[slice] != NULL) {
PR0("Closing slice %u", slice);
(void) ldi_close(vd->ldi_handle[slice],
vd_open_flags | FNDELAY, kcred);
}
}
}
/* Free lock */
if (vd->initialized & VD_LOCKING)
mutex_destroy(&vd->lock);
/* Finally, free the vdisk structure itself */
kmem_free(vd, sizeof (*vd));
}
static int
vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t ldc_id)
{
int status;
vd_t *vd = NULL;
if ((status = vds_do_init_vd(vds, id, device_path, ldc_id, &vd)) != 0)
vds_destroy_vd(vd);
return (status);
}
static int
vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
uint64_t *ldc_id)
{
int num_channels;
/* Look for channel endpoint child(ren) of the vdisk MD node */
if ((num_channels = md_scan_dag(md, vd_node,
md_find_name(md, VD_CHANNEL_ENDPOINT),
md_find_name(md, "fwd"), channel)) <= 0) {
PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
return (-1);
}
/* Get the "id" value for the first channel endpoint node */
if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
PRN("No \"%s\" property found for \"%s\" of vdisk",
VD_ID_PROP, VD_CHANNEL_ENDPOINT);
return (-1);
}
if (num_channels > 1) {
PRN("Using ID of first of multiple channels for this vdisk");
}
return (0);
}
static int
vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
{
int num_nodes, status;
size_t size;
mde_cookie_t *channel;
if ((num_nodes = md_node_count(md)) <= 0) {
PRN("Invalid node count in Machine Description subtree");
return (-1);
}
size = num_nodes*(sizeof (*channel));
channel = kmem_zalloc(size, KM_SLEEP);
status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
kmem_free(channel, size);
return (status);
}
static void
vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
{
char *device_path = NULL;
uint64_t id = 0, ldc_id = 0;
if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
return;
}
PR0("Adding vdisk ID %lu", id);
if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
&device_path) != 0) {
PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
return;
}
if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
PRN("Error getting LDC ID for vdisk %lu", id);
return;
}
if (vds_init_vd(vds, id, device_path, ldc_id) != 0) {
PRN("Failed to add vdisk ID %lu", id);
return;
}
}
static void
vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
{
uint64_t id = 0;
if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
PRN("Unable to get \"%s\" property from vdisk's MD node",
VD_ID_PROP);
return;
}
PR0("Removing vdisk ID %lu", id);
if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
PRN("No vdisk entry found for vdisk ID %lu", id);
}
static void
vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
md_t *curr_md, mde_cookie_t curr_vd_node)
{
char *curr_dev, *prev_dev;
uint64_t curr_id = 0, curr_ldc_id = 0;
uint64_t prev_id = 0, prev_ldc_id = 0;
size_t len;
/* Validate that vdisk ID has not changed */
if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
PRN("Error getting previous vdisk \"%s\" property",
VD_ID_PROP);
return;
}
if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
return;
}
if (curr_id != prev_id) {
PRN("Not changing vdisk: ID changed from %lu to %lu",
prev_id, curr_id);
return;
}
/* Validate that LDC ID has not changed */
if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
PRN("Error getting LDC ID for vdisk %lu", prev_id);
return;
}
if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
PRN("Error getting LDC ID for vdisk %lu", curr_id);
return;
}
if (curr_ldc_id != prev_ldc_id) {
_NOTE(NOTREACHED); /* lint is confused */
PRN("Not changing vdisk: "
"LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
return;
}
/* Determine whether device path has changed */
if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
&prev_dev) != 0) {
PRN("Error getting previous vdisk \"%s\"",
VD_BLOCK_DEVICE_PROP);
return;
}
if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
&curr_dev) != 0) {
PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
return;
}
if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
(strncmp(curr_dev, prev_dev, len) == 0))
return; /* no relevant (supported) change */
PR0("Changing vdisk ID %lu", prev_id);
/* Remove old state, which will close vdisk and reset */
if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
PRN("No entry found for vdisk ID %lu", prev_id);
/* Re-initialize vdisk with new state */
if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
PRN("Failed to change vdisk ID %lu", curr_id);
return;
}
}
static int
vds_process_md(void *arg, mdeg_result_t *md)
{
int i;
vds_t *vds = arg;
if (md == NULL)
return (MDEG_FAILURE);
ASSERT(vds != NULL);
for (i = 0; i < md->removed.nelem; i++)
vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
for (i = 0; i < md->match_curr.nelem; i++)
vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
md->match_curr.mdp, md->match_curr.mdep[i]);
for (i = 0; i < md->added.nelem; i++)
vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
return (MDEG_SUCCESS);
}
static int
vds_do_attach(dev_info_t *dip)
{
int status, sz;
int cfg_handle;
minor_t instance = ddi_get_instance(dip);
vds_t *vds;
mdeg_prop_spec_t *pspecp;
mdeg_node_spec_t *ispecp;
/*
* The "cfg-handle" property of a vds node in an MD contains the MD's
* notion of "instance", or unique identifier, for that node; OBP
* stores the value of the "cfg-handle" MD property as the value of
* the "reg" property on the node in the device tree it builds from
* the MD and passes to Solaris. Thus, we look up the devinfo node's
* "reg" property value to uniquely identify this device instance when
* registering with the MD event-generation framework. If the "reg"
* property cannot be found, the device tree state is presumably so
* broken that there is no point in continuing.
*/
if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
VD_REG_PROP)) {
PRN("vds \"%s\" property does not exist", VD_REG_PROP);
return (DDI_FAILURE);
}
/* Get the MD instance for later MDEG registration */
cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
VD_REG_PROP, -1);
if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
PRN("Could not allocate state for instance %u", instance);
return (DDI_FAILURE);
}
if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
PRN("Could not get state for instance %u", instance);
ddi_soft_state_free(vds_state, instance);
return (DDI_FAILURE);
}
vds->dip = dip;
vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
vds_destroy_vd,
sizeof (void *));
ASSERT(vds->vd_table != NULL);
if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
PRN("ldi_ident_from_dip() returned errno %d", status);
return (DDI_FAILURE);
}
vds->initialized |= VDS_LDI;
/* Register for MD updates */
sz = sizeof (vds_prop_template);
pspecp = kmem_alloc(sz, KM_SLEEP);
bcopy(vds_prop_template, pspecp, sz);
VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
/* initialize the complete prop spec structure */
ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
ispecp->namep = "virtual-device";
ispecp->specp = pspecp;
if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
&vds->mdeg) != MDEG_SUCCESS) {
PRN("Unable to register for MD updates");
kmem_free(ispecp, sizeof (mdeg_node_spec_t));
kmem_free(pspecp, sz);
return (DDI_FAILURE);
}
vds->ispecp = ispecp;
vds->initialized |= VDS_MDEG;
/* Prevent auto-detaching so driver is available whenever MD changes */
if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
DDI_PROP_SUCCESS) {
PRN("failed to set \"%s\" property for instance %u",
DDI_NO_AUTODETACH, instance);
}
ddi_report_dev(dip);
return (DDI_SUCCESS);
}
static int
vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int status;
switch (cmd) {
case DDI_ATTACH:
PR0("Attaching");
if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
(void) vds_detach(dip, DDI_DETACH);
return (status);
case DDI_RESUME:
PR0("No action required for DDI_RESUME");
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static struct dev_ops vds_ops = {
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
ddi_no_info, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
vds_attach, /* devo_attach */
vds_detach, /* devo_detach */
nodev, /* devo_reset */
NULL, /* devo_cb_ops */
NULL, /* devo_bus_ops */
nulldev /* devo_power */
};
static struct modldrv modldrv = {
&mod_driverops,
"virtual disk server v%I%",
&vds_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
int
_init(void)
{
int i, status;
if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
return (status);
if ((status = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&vds_state);
return (status);
}
/* Fill in the bit-mask of server-supported operations */
for (i = 0; i < vds_noperations; i++)
vds_operations |= 1 << (vds_operation[i].operation - 1);
return (0);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
int status;
if ((status = mod_remove(&modlinkage)) != 0)
return (status);
ddi_soft_state_fini(&vds_state);
return (0);
}
|