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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <assert.h>
#include <string.h>
#include <libintl.h>
#include "volume_error.h"
#include "volume_defaults.h"
#include "volume_dlist.h"
#include "volume_output.h"
#include "volume_request.h"
#include "layout_device_cache.h"
#include "layout_discovery.h"
#include "layout_dlist_util.h"
#include "layout_request.h"
#include "layout_slice.h"
#include "layout_validate.h"
#define _LAYOUT_REQUEST_C
static char *_request_diskset = NULL;
static devconfig_t *_toplevel_request = NULL;
static defaults_t *_defaults = NULL;
/*
* This file contains code which handles various aspects of the
* request and defaults devconfig_t structs passed to the layout
* module.
*
* Functions are provided which determine what devices are available
* for use by the various volume layout mechanisms. These are based
* on the user specified available/unavailable devices included in
* a request or in the defaults associated with the destination diskset.
*/
/*
* A struct to hold device "specifications" extracted from a user
* specified device name. This struct is used to compare the user's
* available and unavailable device specifications against physical
* devices attached to the system.
*
* The spec struct holds one of two different specifications: if the
* user supplied device name is parsable as a CTD name, it is parsed
* into the component ids. Otherwise, it is stored as is.
*
* The CTD name space implies a device hierarchy and metassist
* supports an implied wildcarding scheme for the CTD name space.
* A CTD specification from the user is of the form cX, cXdX,
* cXdXsX, cXtX, cXtXdX, or cXtXdXsX, so it may or may nor
* correspond to an individual physical device depending on
* the context.
*
* For example, "c1" can mean the controller/HBA with the
* name "c1" or it can mean all devices attached to the
* controller named "c1".
*
* The ctd specs make matching physical devices against a
* user specification easier since the matching is based on
* the numeric values extracted from the cXtXdXsX string
* and not on the strings themselves. The strings are
* troublesome because of situations like "c1" being
* compared to "c11t1d0s0" and getting false matches.
*
* The ID_UNSPECIFIED value is used to flag components
* that were not in the CTD name:
*
* "c3" -> { ctrl=3, target=ID_UNSPECIFIED,
* lun=ID_UNSPECIFIED, slice=ID_UNSPECIFIED }
*
* "c3t2" -> { ctrl=3, target=2,
* lun=ID_UNSPECIFIED, slice=ID_UNSPECIFIED }
*/
#define ID_UNSPECIFIED -1
typedef struct {
int ctrl;
int target;
int lun;
int slice;
boolean_t is_ide;
} ctd_spec_t;
typedef enum {
SPEC_TYPE_CTD = 0,
SPEC_TYPE_RAW,
SPEC_TYPE_OTHER
} spec_type_t;
typedef struct {
spec_type_t type;
union {
ctd_spec_t *ctd;
char *raw;
} data;
} device_spec_t;
static int get_spec_for_name(
char *name,
device_spec_t **id);
static int create_device_spec(
char *name,
device_spec_t **spec);
static int create_device_ctd_spec(
char *name,
device_spec_t **spec);
static int create_device_raw_spec(
char *name,
device_spec_t **spec);
static void destroy_device_spec(
device_spec_t *spec);
static boolean_t ctd_spec_includes_device(
device_spec_t *spec,
device_spec_t *device);
static boolean_t raw_spec_includes_device(
device_spec_t *spec,
device_spec_t *device);
/*
* get_spec_for_name builds up a cached mapping of device
* names to the corresponding device_spec_t structs.
*
* This saves repeatedly converting the device names, which
* could get expensive since devices are checked against the
* user specified available/unavailable devices a lot.
*
* The cache is implemented as a list of these structs:
*/
typedef struct {
char *name;
device_spec_t *device_spec;
} spec_cache_t;
static dlist_t *_spec_cache = NULL;
static int destroy_spec_cache();
static int compare_name_to_spec_cache_name(
void *name, void *list_item);
/*
* The user specified available/unavailable devices are
* accessed frequently during layout. To make this more
* efficient, the char *arrays of available/unavailable
* specifications for a request or defaults devconfig_t
* object are converted to device_spec_ts the first time
* they're accessed and then cached using this struct:
*/
typedef struct {
devconfig_t *request;
/*
* avail_specs_list is a list of device spec_t
* corresponding to available devices specified
* in the request object
*/
dlist_t *avail_specs_list;
/*
* unavail_specs_list is a list of device spec_t
* corresponding to unavailable devices specified
* in the request object
*/
dlist_t *unavail_specs_list;
} request_spec_list_t;
dlist_t *_request_spec_list_cache = NULL;
static int destroy_request_spec_list_cache();
static void destroy_request_spec_list_entry(void *obj);
static int compare_request_to_request_spec_list_request(
void *object,
void *list_item);
static int convert_usernames_to_specs(
char **specs,
dlist_t **list);
/* other private functions */
static int is_device_avail(
dm_descriptor_t desc,
devconfig_t *request,
boolean_t *avail);
static int is_named_device_avail(
devconfig_t *request,
char *device_name,
boolean_t check_aliases,
boolean_t *avail);
static int avail_list_includes_device_name(
dlist_t *list,
char *device_name,
boolean_t check_aliases,
boolean_t *includes);
static int unavail_list_includes_device_name(
dlist_t *list,
char *device_name,
boolean_t check_aliases,
boolean_t *includes);
static int spec_includes_device_name(
device_spec_t *spec,
char *device_name,
boolean_t check_aliases,
boolean_t *includes);
static boolean_t spec_includes_device(
device_spec_t *spec,
device_spec_t *device);
static int disk_get_avail_space(
devconfig_t *request,
dm_descriptor_t disk,
uint64_t *avail);
static int compare_hba_n_avail_disks(
void *obj1,
void *obj2);
/*
* FUNCTION: release_request_caches()
*
* RETURNS: 0
*
* PURPOSE: cleanup the module private caches.
*/
int
release_request_caches()
{
(void) destroy_request_spec_list_cache();
(void) destroy_spec_cache();
return (0);
}
/*
* FUNCTION: int set_request_diskset(char *)
*
* INPUT: char * - pointer to the diskset name
* OUTPUT: 0 - success
* !0 - validation failure
* RETURNS:
*
* PURPOSE: set the module global diskset name.
*/
int
set_request_diskset(
char *dsname)
{
_request_diskset = dsname;
if (dsname == NULL || dsname[0] == '\0') {
volume_set_error(
gettext("No disk set specified in request\n"));
return (-1);
}
return (0);
}
/*
* FUNCTION: char *get_request_diskset()
*
* INPUT: none -
* OUTPUT: none -
* RETURNS: char * - pointer to the currently set diskset name
*
* PURPOSE: get the global name of the current diskset.
*/
char *
get_request_diskset()
{
return (_request_diskset);
}
/*
* FUNCTION: void unset_request_diskset()
*
* PURPOSE: unset the module global diskset name.
*/
void
unset_request_diskset(
char *dsname)
{
_request_diskset = NULL;
}
/*
* FUNCTION: int set_toplevel_request(devconfig_t *)
*
* INPUT: devconfig_t * - pointer to the diskset request
* OUTPUT: 0 - success
* !0 - validation failure
* RETURNS:
*
* PURPOSE: set the module global toplevel request struct.
* this will be set within the only public entry
* point to the module -- get_layout()
*
* SIDEEFFECT: The devconfig_t's list of available and unavailable
* devices will be validated.
*/
int
set_toplevel_request(
devconfig_t *req)
{
_toplevel_request = req;
return (validate_request_avail_unavail(req));
}
/*
* FUNCTION: void unset_toplevel_request()
*
* PURPOSE: unset the layout module global toplevel request struct.
*
*/
void
unset_toplevel_request()
{
_toplevel_request = NULL;
}
/*
* FUNCTION: int set_defaults(devconfig_t *)
*
* INPUT: devconfig_t * - pointer to the global defaults devconfig_t
* OUTPUT: 0 - success
* !0 - validation failure
* RETURNS:
*
* PURPOSE: set the module global defaults struct.
* this will be set within the only public entry
* point to the module -- get_layout()
*
* SIDEEFFECT: The devconfig_t's list of available and unavailable
* devices will be validated.
*/
int
set_request_defaults(
defaults_t *defaults)
{
int error = 0;
devconfig_t *diskset = NULL;
_defaults = defaults;
if ((error = defaults_get_diskset_by_name(
_defaults, get_request_diskset(), &diskset)) == 0) {
error = validate_request_avail_unavail(diskset);
} else if (error == ENOENT) {
/* no defaults to verify */
error = 0;
}
return (error);
}
/*
* FUNCTION: void unset_request_defaults()
*
* PURPOSE: unset the layout module global defaults struct.
*
*/
void
unset_request_defaults()
{
_defaults = NULL;
}
/*
* FUNCTION: get_stripe_min_comp(devconfig_t *req, uint16_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint64_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the minimum of components
* for striped volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_stripe_min_comp(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_stripe_mincomp(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_stripe_mincomp(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_stripe_max_comp(devconfig_t *req, uint16_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint64_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the maximum number of components
* for striped volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_stripe_max_comp(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_stripe_maxcomp(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_stripe_maxcomp(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_stripe_interlace(devconfig_t *req, uint64_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint64_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the interlace value for striped
* volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*
* If no value is explictly specified, ERR_ATTR_UNSET is
* returned.
*/
int
get_stripe_interlace(
devconfig_t *req,
uint64_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_stripe_interlace(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
error = 0;
}
if (*val == 0) {
if ((error = defaults_get_stripe_interlace(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_mirror_read_strategy(devconfig_t *req,
* mirror_read_strategy_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a mirror_read_strategy_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the write strategy mirrored volumes
* should have for volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*
* If no value is explictly specified, ERR_ATTR_UNSET is
* returned.
*/
int
get_mirror_read_strategy(
devconfig_t *req,
mirror_read_strategy_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_mirror_read(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_mirror_read(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_mirror_write_strategy(devconfig_t *req,
* mirror_write_strategy_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a mirror_write_strategy_t to hold result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the write strategy mirrored volumes
* should have for volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*
* If no value is explictly specified, ERR_ATTR_UNSET is
* returned.
*/
int
get_mirror_write_strategy(
devconfig_t *req,
mirror_write_strategy_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_mirror_write(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_mirror_write(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_mirror_pass(devconfig_t *req, uint16_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint16_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the resync pass mirrored volumes
* should have for volumes satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*
* If no value is explictly specified, ERR_ATTR_UNSET is
* returned.
*/
int
get_mirror_pass(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_mirror_pass(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_mirror_pass(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_mirror_nsubs(devconfig_t *req, uint16_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint16_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines how many submirrors mirrored
* volumes should have for volumes satisfying the input
* request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_mirror_nsubs(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_mirror_nsubs(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_mirror_nsubs(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_volume_faultrecov(devconfig_t *req, boolean_t *val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a boolean_t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines whether data redundant volumes
* should also have fault recovery (e.g., HSPs) for volumes
* satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_volume_faultrecov(
devconfig_t *req,
boolean_t *val)
{
int error = 0;
*val = B_FALSE;
if ((error = devconfig_get_volume_usehsp(req, val)) != 0) {
if (error == ERR_ATTR_UNSET) {
component_type_t type = TYPE_UNKNOWN;
(void) devconfig_get_type(req, &type);
switch (type) {
case TYPE_MIRROR:
error = defaults_get_mirror_usehsp(
_defaults, get_request_diskset(), val);
break;
case TYPE_STRIPE:
error = defaults_get_stripe_usehsp(
_defaults, get_request_diskset(), val);
break;
case TYPE_CONCAT:
error = defaults_get_concat_usehsp(
_defaults, get_request_diskset(), val);
break;
case TYPE_VOLUME:
error = defaults_get_volume_usehsp(
_defaults, get_request_diskset(), val);
break;
}
}
}
return (error);
}
/*
* FUNCTION: get_volume_redundancy_level(devconfig_t *req, uint16_t val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint16-t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the appropriate level of data
* redundancy a volume should have for volumes satisfying
* the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_volume_redundancy_level(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_volume_redundancy_level(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_volume_redundancy_level(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_volume_npaths(devconfig_t *req, uint16_t val)
* INPUT: req - a devconfig_t pointer to the current request
* val - pointer to a uint16-t to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the appropriate level of datapath
* redundancy a slice component should have for volumes
* satisfying the input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_volume_npaths(
devconfig_t *req,
uint16_t *val)
{
int error = 0;
*val = 0;
if ((error = devconfig_get_volume_npaths(req, val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
if (*val == 0) {
if ((error = defaults_get_volume_npaths(
_defaults, get_request_diskset(), val)) != 0) {
if (error != ERR_ATTR_UNSET) {
return (error);
}
}
}
return (error);
}
/*
* FUNCTION: get_default_hsp_name(devconfig_t *req, char **hspname)
* INPUT: req - a devconfig_t pointer to the current request
* hspname - pointer to a char * to hold the result, if any
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which determines the default HSP name for the
* input request.
*
* The value to use is taken from the input request, the
* toplevel diskset request, the diskset defaults or the
* global defaults.
*/
int
get_default_hsp_name(
devconfig_t *req,
char **name)
{
int error = 0;
*name = NULL;
if ((error = defaults_get_hsp_name(_defaults,
get_request_diskset(), name)) != 0) {
if (error != ENOENT) {
return (error);
}
error = 0;
}
return (error);
}
/*
* FUNCTION: slice_is_available(char *sname, devconfig_t *request,
* boolean_t bool)
* INPUT: sname - a slice name
* request - pointer to a devconfig_t struct representing
* the current layout request being processed
* bool - pointer to a boolean to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: Validation helper which determines if the named slice can
* be used as a volume component when satisfying the input
* request.
*
* Check if the slice appears in the known slice list,
* then check the request's available and unavailable
* device specifications.
*/
int
slice_is_available(
char *sname,
devconfig_t *request,
boolean_t *bool)
{
dm_descriptor_t slice = (dm_descriptor_t)0;
int error = 0;
*bool = B_FALSE;
if ((error = slice_get_by_name(sname, &slice)) != 0) {
return (error);
}
if (slice == (dm_descriptor_t)0) {
/* no slice found */
return (ENODEV);
}
if (error == 0) {
error = is_named_device_avail(request, sname, B_TRUE, bool);
}
return (error);
}
/*
* FUNCTION: get_disks_for_target(char *name, dlist_t **disks)
*
* INPUT: name - a char* device CTD name
*
* OUTPUT: disks - disks matching the input target name
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: Validation helper function which finds all disks "on" the
* input target.
*
* The input name is assumed to be a target name, cXtX, and
* the list of known disks is searched to find any disk that
* looks to be "on" that target.
*
* "On" is determined by comparing a disk's name and
* aliases to the target to see if they match.
*/
int
get_disks_for_target(
char *name,
dlist_t **disks)
{
int error = 0;
device_spec_t *targetid = NULL;
error = get_spec_for_name(name, &targetid);
if (error == 0) {
dlist_t *known_disks = NULL;
dlist_t *iter = NULL;
get_known_disks(&known_disks);
for (iter = known_disks;
(iter != NULL) && (error == 0);
iter = iter->next) {
dm_descriptor_t disk = (uintptr_t)iter->obj;
device_spec_t *diskid = NULL;
char *diskname = NULL;
dlist_t *diskaliases = NULL;
dlist_t *item;
((error = get_display_name(disk, &diskname)) != 0) ||
(error = get_aliases(disk, &diskaliases)) ||
(error = get_spec_for_name(diskname, &diskid));
if (error == 0) {
if (spec_includes_device(targetid, diskid) == B_TRUE) {
/* add disk */
if ((item = dlist_new_item((void *)(uintptr_t)disk)) ==
NULL) {
error = ENOMEM;
} else {
*disks = dlist_append(item, *disks, AT_HEAD);
}
} else {
/* check disk's aliases */
dlist_t *iter2;
for (iter2 = diskaliases;
(iter2 != NULL) && (error == 0);
iter2 = iter2->next) {
char *aliasname = NULL;
device_spec_t *aliasid = NULL;
error = get_display_name(disk, &aliasname);
error = get_spec_for_name(aliasname, &aliasid);
if (spec_includes_device(
targetid, aliasid) == B_TRUE) {
/* alias matched, add disk */
item = dlist_new_item((void *)(uintptr_t)disk);
if (item == NULL) {
error = ENOMEM;
} else {
*disks =
dlist_append(item, *disks, AT_HEAD);
}
}
}
}
}
}
}
return (error);
}
/*
* FUNCTION: select_hbas_with_n_disks(devconfig_t *request,
* dlist_t *hbas, int mindisks, dlist_t **selhbas,
* dlist_t **seldisks)
*
* INPUT: request - pointer to a devconfig_t struct representing
* the current layout request being processed
* hbas - pointer to a list of HBAs
* mindisks - minimum number of disks required on the HBAs
*
* OUTPUT: selhbas - pointer to a list containing the HBAs with at
* least mindisks available disks.
* seldisks - pointer to a list containing the available disks
* for the HBAs in selhbas
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which counts the number of available disks associated
* with each of the input HBAs and adds those that have at
* least mindisks to the output list.
*
* Only available disks that have available space are counted.
*
* Disks connected thru multiple HBAs are only counted for
* the first HBA they're accessed through.
*
* The list of HBAs returned will be in descending order,
* i.e., HBAs with more disks come before those with fewer.
*
* The returned lists of HBAs and disks must be passed to
* dlist_free_items() to recover the space allocated to hold
* each list item.
*
* for (each HBA) {
*
* select HBA
* get available disks on HBA
*
* for (each disk) {
* if (disk is not in selected disk list)
* add it to the list
* else
* count it as a distinct disk on this HBA
* }
*
* if (this HBA has >= mindisks distinct disks)
* add this HBA to the list of returned HBAs
*
* }
*/
int
select_hbas_with_n_disks(
devconfig_t *request,
dlist_t *hbas,
int mindisks,
dlist_t **selhbas,
dlist_t **seldisks)
{
dlist_t *iter = NULL;
int error = 0;
*selhbas = NULL;
*seldisks = NULL;
/* for each input HBA */
for (iter = hbas; (error == 0) && (iter != NULL); iter = iter->next) {
dm_descriptor_t hba = (uintptr_t)iter->obj;
dlist_t *iter2 = NULL;
dlist_t *disks = NULL;
uint64_t space = 0;
uint16_t ndistinct = 0;
error = hba_get_avail_disks_and_space(request, hba, &disks, &space);
/* for each of this HBA's disks */
for (iter2 = disks;
(iter2 != NULL) && (error == 0);
iter2 = iter2->next) {
dm_descriptor_t disk = (uintptr_t)iter2->obj;
/* unique disk? has it been seen thru some other HBA? */
if (dlist_contains(*seldisks, (void *)(uintptr_t)disk,
compare_descriptor_names) != B_TRUE) {
/* distinct, add to list of all_distinct */
dlist_t *item = dlist_new_item((void *)(uintptr_t)disk);
if (item == NULL) {
error = ENOMEM;
} else {
*seldisks =
dlist_append(item, *seldisks, AT_HEAD);
/* increment this HBA's distinct disk count */
++ndistinct;
}
}
}
if (ndistinct >= mindisks) {
/* this HBA has minimum # of disks, add to output list */
dlist_t *item = dlist_new_item((void *)(uintptr_t)hba);
if (item == NULL) {
error = ENOMEM;
} else {
*selhbas =
dlist_insert_ordered(
item, *selhbas, DESCENDING,
compare_hba_n_avail_disks);
/* save # of disks for ordering the list */
hba_set_n_avail_disks(hba, ndistinct);
}
}
dlist_free_items(disks, NULL);
}
if (error != 0) {
oprintf(OUTPUT_TERSE,
gettext("failed selecting HBAs with n disks: %d\n"),
error);
dlist_free_items(*selhbas, NULL);
*selhbas = NULL;
dlist_free_items(*seldisks, NULL);
*seldisks = NULL;
}
return (error);
}
/*
* FUNCTION: hba_get_avail_disks_and_space(devconfig_t *request,
* dm_descriptor_t hba, dlist_t **disks, uint64_t *space)
*
* INPUT: request - pointer to a devconfig_t struct representing
* the current layout request being processed
* hba - dm_descriptor_t handle for an HBA
*
* OUTPUT: disks - pointer to a list to hold the computed available
* disks
* avail - pointer to a uint64_t to hold the aggregate
* available space on the available disks
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which examines the disks associated with the
* input HBA and assembles a list of those that are available.
*
* Available is defined as being in the usable list, having
* unused space and not specifically excluded by the request's
* list of unavailable devices.
*
* The returned list must be passed to dlist_free_items()
* to recover the memory allocated to hold each list item.
*/
int
hba_get_avail_disks_and_space(
devconfig_t *request,
dm_descriptor_t hba,
dlist_t **disks,
uint64_t *space)
{
dlist_t *usable_disks = NULL;
dlist_t *iter = NULL;
int error = 0;
*disks = NULL;
/* for each usable disk */
error = get_usable_disks(&usable_disks);
for (iter = usable_disks;
(error == 0) && (iter != NULL);
iter = iter->next) {
dm_descriptor_t disk = (uintptr_t)iter->obj;
boolean_t avail = B_FALSE;
dlist_t *hbas = NULL;
/* is disk attached to HBA in question? */
error = disk_get_hbas(disk, &hbas);
if (error != 0) {
continue;
}
if (dlist_contains(hbas, (void *)(uintptr_t)hba,
compare_descriptor_names) == B_TRUE) {
/* is disk available? */
error = is_device_avail(disk, request, &avail);
if ((error == 0) && (avail == B_TRUE)) {
uint64_t disk_space = 0;
/* does disk have available space? */
error = disk_get_avail_space(request, disk, &disk_space);
if ((error == 0) && (disk_space > 0)) {
dlist_t *item = dlist_new_item((void *)(uintptr_t)disk);
if (item == NULL) {
error = ENOMEM;
} else {
*disks = dlist_append(item, *disks, AT_HEAD);
}
*space += disk_space;
}
}
}
dlist_free_items(hbas, NULL);
}
if (error != 0) {
dlist_free_items(*disks, NULL);
*disks = NULL;
}
return (error);
}
/*
* FUNCTION: disk_get_avail_space(devconfig_t *request,
* dlist_t *disks, uint64_t space)
*
* INPUT: request - pointer to a devconfig_t struct representing
* the current layout request being processed
* disks - pointer to a list of disks
* space - pointer to a uint64_t to hold the computed available
* space
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which iterates the input list of disks and determines
* the aggregate amount of available space they represent.
*
* Only disk slices that are in the usable slice list and not
* specifically excluded by the request's list of unavailable
* devices will contribute to the aggregate space computation.
*/
static int
disk_get_avail_space(
devconfig_t *request,
dm_descriptor_t disk,
uint64_t *space)
{
dlist_t *usable_slices = NULL;
dlist_t *iter = NULL;
int error = 0;
*space = 0;
/* for each usable slice */
error = get_usable_slices(&usable_slices);
for (iter = usable_slices;
(error == 0) && (iter != NULL);
iter = iter->next) {
dm_descriptor_t slice = (uintptr_t)iter->obj;
dm_descriptor_t slice_disk;
boolean_t avail = B_FALSE;
boolean_t reserved = B_FALSE;
boolean_t used = B_FALSE;
/* is slice on disk in question? */
if (((error = slice_get_disk(slice, &slice_disk)) != 0) ||
(compare_descriptor_names((void *)(uintptr_t)slice_disk,
(void *)(uintptr_t)disk) != 0)) {
continue;
}
/* is slice reserved by an explicit layout request? */
if (((error = is_reserved_slice(slice, &reserved)) != 0) ||
(reserved == B_TRUE)) {
continue;
}
/* is slice used by a pending layout request? */
if (((error = is_used_slice(slice, &used)) != 0) ||
(used == B_TRUE)) {
continue;
}
/* is slice available? */
if (((error = is_device_avail(slice, request, &avail)) == 0) &&
(avail == B_TRUE)) {
/* does slice have usable space? */
uint64_t size = 0;
if ((error = slice_get_size(slice, &size)) == 0) {
*space += size;
}
}
}
return (error);
}
/*
* FUNCTION: disks_get_avail_slices(devconfig_t *request,
* dlist_t *disks, dlist_t **slices)
*
* INPUT: request - pointer to a devconfig_t struct representing
* the current layout request being processed
* disks - pointer to a list of disks
* slices - pointer to an output list of disks
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: helper which iterates the input list of disks and builds a
* new list which contains disks that are determined to be
* available for satisfying the input request.
*
* A disk must contain at least one slice in the available
* slice list as well as have available space in order
* to be available.
*/
int
disks_get_avail_slices(
devconfig_t *request,
dlist_t *disks,
dlist_t **slices)
{
dlist_t *usable_slices = NULL;
dlist_t *iter = NULL;
int error = 0;
*slices = NULL;
/* for each usable slice */
error = get_usable_slices(&usable_slices);
for (iter = usable_slices;
(error == 0) && (iter != NULL);
iter = iter->next) {
dm_descriptor_t slice = (uintptr_t)iter->obj;
dm_descriptor_t disk = (dm_descriptor_t)0;
boolean_t avail = B_FALSE;
boolean_t reserved = B_FALSE;
boolean_t used = B_FALSE;
/* is slice on a disk in the input list? */
if (((error = slice_get_disk(slice, &disk)) != 0) ||
(dlist_contains(disks, (void *)(uintptr_t)disk,
compare_descriptor_names) != B_TRUE)) {
continue;
}
/* is slice reserved by an explicit layout request? */
if (((error = is_reserved_slice(slice, &reserved)) != 0) ||
(reserved == B_TRUE)) {
continue;
}
/* is slice used by a pending layout request? */
if (((error = is_used_slice(slice, &used)) != 0) ||
(used == B_TRUE)) {
continue;
}
/* is slice available? */
if (((error = is_device_avail(slice, request, &avail)) == 0) &&
(avail == B_TRUE)) {
/* does slice have available space? */
uint64_t size = 0;
error = slice_get_size(slice, &size);
if ((error == 0) && (size > 0)) {
dlist_t *item = dlist_new_item((void *)(uintptr_t)slice);
if (item == NULL) {
error = ENOMEM;
} else {
*slices = dlist_append(item, *slices, AT_TAIL);
}
}
}
}
if (error != 0) {
dlist_free_items(*slices, NULL);
*slices = NULL;
}
return (error);
}
/*
* FUNCTION: get_hbas_and_disks_used_by_volumes(dlist_t *volumes,
* dlist_t **hbas, dlist_t **disks)
*
* INPUT: volumes - pointer to a list of devconfig_t volumes
*
* OUTPUT: hbas - a list of HBAs utilized by the input volumes
* disks - a list of disks utilized by the input volumes
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: An aggregate list of HBAs and disks used by the input volumes
* is built up by iterating the list of volumes and calling
* get_hbas_disks_used_by_volume() to determine the HBAs and disk
* used by each volume.
*
* The returned lists of HBAs and disks may contain duplicates.
*/
int
get_hbas_and_disks_used_by_volumes(
dlist_t *volumes,
dlist_t **hbas,
dlist_t **disks)
{
dlist_t *iter = NULL;
int error = 0;
for (iter = volumes;
(iter != NULL) && (error == 0);
iter = iter->next) {
error = get_hbas_and_disks_used_by_volume(
(devconfig_t *)iter->obj, hbas, disks);
}
return (error);
}
/*
* FUNCTION: get_hbas_and_disks_used_by_volume(devconfig_t *volume,
* dlist_t **hbas, dlist_t **disks)
*
* INPUT: volume - pointer to a devconfig_t volume
*
* OUTPUT: hbas - a list of HBAs updated to include those utilized
* by the input volume
* disks - a list of disks updated to inlclude those utilized
* by the input volume
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: The volume's components are iterated and the disks and HBAs
* for each component are determined and appended to the input
* lists of HBAs and disks.
*
* The returned lists of HBAs and disks may contain duplicates.
*/
int
get_hbas_and_disks_used_by_volume(
devconfig_t *volume,
dlist_t **hbas,
dlist_t **disks)
{
dlist_t *iter = NULL;
int error = 0;
for (iter = devconfig_get_components(volume);
(iter != NULL) && (error == 0);
iter = iter->next) {
devconfig_t *dev = (devconfig_t *)iter->obj;
if (devconfig_isA(dev, TYPE_SLICE)) {
dm_descriptor_t disk = NULL;
char *name = NULL;
/* get disk for component slice */
((error = devconfig_get_name(dev, &name)) != 0) ||
(error = get_disk_for_named_slice(name, &disk));
if (error == 0) {
dlist_t *item = dlist_new_item((void *)(uintptr_t)disk);
if (item == NULL) {
error = ENOMEM;
} else {
*disks = dlist_append(item, *disks, AT_HEAD);
}
}
/* get HBAs for disk */
if (error == 0) {
dlist_t *disk_hbas = NULL;
if ((error = disk_get_hbas(disk, &disk_hbas)) == 0) {
/* the hba list may contain dups, but that's ok */
*hbas = dlist_append(disk_hbas, *hbas, AT_HEAD);
}
}
} else if (devconfig_isA(dev, TYPE_MIRROR)) {
/* collect info for submirrors */
dlist_t *iter1;
for (iter1 = devconfig_get_components(dev);
(iter1 != NULL) && (error == 0);
iter1 = iter1->next) {
error = get_hbas_and_disks_used_by_volume(
(devconfig_t *)iter1->obj, hbas, disks);
}
}
}
return (error);
}
/*
* FUNCTION: compare_hba_n_avail_disks(void *obj1, void *obj2)
*
* INPUT: obj1 - opaque pointer
* obj2 - opaque pointer
*
* RETURNS: int - <0 - if obj1 has fewer available disks than obj2
* 0 - if obj1 has the same # of available disks as obj2
* >0 - if obj1 has more available disks than obj2
*
* PURPOSE: dlist_t helper which compares the number of available disks
* for two HBAs represented as dm_descriptor_t handles.
*
* Both input objects are assumed to be dm_descriptor_t handles.
*
* The number of available disks associated with the HBAs was
* computed and saved in select_hbas_with_n_disks(), this
* function just checks the saved values.
*/
static int
compare_hba_n_avail_disks(
void *obj1,
void *obj2)
{
uint16_t n1 = 0;
uint16_t n2 = 0;
assert(obj1 != NULL);
assert(obj2 != NULL);
(void) hba_get_n_avail_disks((uintptr_t)obj1, &n1);
(void) hba_get_n_avail_disks((uintptr_t)obj2, &n2);
return ((int)n1 - n2);
}
/*
* FUNCTION: is_device_avail(dm_descriptor_t desc,
* devconfig_t *request, boolean_t *avail)
*
* INPUT: desc - a dm_descriptor_t device handle
* request - pointer to a devconfig_t struct representing
* the current layout request being processed
* avail - pointer to a boolean to hold the result
*
* RETURNS: int - 0 - on success
* !0 - otherwise
*
* PURPOSE: Internal helper which determines if the input device can
* be used as a volume component when satisfying the input
* request.
*
* The device is assumed to be a known valid device.
*
* The function checks if the device passes the request's
* available and unavailable device specifications.
*
* The input device name may be either a DID name or a CTD
* name. All name comparisons are done using the CTD name.
*/
static int
is_device_avail(
dm_descriptor_t desc,
devconfig_t *request,
boolean_t *avail)
{
char *name = NULL;
int error = 0;
*avail = B_FALSE;
if ((error = get_display_name(desc, &name)) == 0) {
error = is_named_device_avail(request, name, B_TRUE, avail);
}
return (error);
}
/*
* FUNCTION: compare_request_to_request_spec_list_request(
* void *request, void *list_item)
*
* INPUT: request - opaque pointer to a devconfig_t
* list_item - opaque pointer to a request_spec_list_t
*
* RETURNS: int - 0 - if request is the same as list_item->request
* !0 - otherwise
*
* PURPOSE: dlist_t helper which compares the input request pointer
* to the list_item's request pointer for equality.
*
* This function is the lookup mechanism for the lists of
* cached device_spec_ts representing available/unavailable
* devices for a given defaults_t request/defaults struct.
*
* The defaults_t struct pointer is the lookup key.
*/
static int
compare_request_to_request_spec_list_request(
void *request,
void *list_item)
{
request_spec_list_t *entry =
(request_spec_list_t *)list_item;
assert(request != NULL);
assert(entry != NULL);
/* compare two devconfig_t pointers, if identical, return 0 */
return ((devconfig_t *)request != entry->request);
}
/*
* FUNCTION: compare_device_spec_specificity(void *spec1, void *spec2)
*
* INPUT: spec1 - opaque pointer to a device_spec_t
* spec2 - opaque pointer to a device_spec_t
*
* RETURNS: int - <0 - if spec1 is less specific than spec2
* 0 - if spec1 is as specific than spec2
* >0 - if spec1 is more specific than spec2
*
* PURPOSE: dlist_t helper which compares the level of specificity
* in the two input device_spec_t structs. The one
* which specifies more "components" of a cXtXdXsX device
* name is considered more specific.
*/
static int
compare_device_spec_specificity(
void *spec1,
void *spec2)
{
if (spec1 == NULL || spec2 == NULL) {
return (-1);
}
if ((((device_spec_t *)spec1)->data.ctd->slice != ID_UNSPECIFIED) &&
(((device_spec_t *)spec2)->data.ctd->slice == ID_UNSPECIFIED)) {
/* spec1 has slice, spec2 does not, spec1 more specific */
return (1);
}
if ((((device_spec_t *)spec2)->data.ctd->slice != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->slice == ID_UNSPECIFIED)) {
/* spec2 has slice, spec1 does not, spec2 more specific */
return (-1);
}
if ((((device_spec_t *)spec2)->data.ctd->slice != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->slice != ID_UNSPECIFIED)) {
/* both spec1 and spec2 have slice */
return (0);
}
if ((((device_spec_t *)spec1)->data.ctd->lun != ID_UNSPECIFIED) &&
(((device_spec_t *)spec2)->data.ctd->lun == ID_UNSPECIFIED)) {
/* spec1 has lun, spec2 does not, spec1 more specific */
return (1);
}
if ((((device_spec_t *)spec2)->data.ctd->lun != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->lun == ID_UNSPECIFIED)) {
/* spec2 has lun, spec1 does not, spec2 more specific */
return (-1);
}
if ((((device_spec_t *)spec2)->data.ctd->lun != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->lun != ID_UNSPECIFIED)) {
/* both spec1 and spec2 have lun */
return (0);
}
if ((((device_spec_t *)spec1)->data.ctd->target != ID_UNSPECIFIED) &&
(((device_spec_t *)spec2)->data.ctd->target == ID_UNSPECIFIED)) {
/* spec1 has target, spec2 does not, spec1 more specific */
return (1);
}
if ((((device_spec_t *)spec2)->data.ctd->target != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->target == ID_UNSPECIFIED)) {
/* spec2 has target, spec1 does not, spec2 more specific */
return (-1);
}
if ((((device_spec_t *)spec2)->data.ctd->target != ID_UNSPECIFIED) &&
(((device_spec_t *)spec1)->data.ctd->target != ID_UNSPECIFIED)) {
/* both spec1 and spec2 have target */
return (0);
}
/* both specify just ctrl */
return (0);
}
/*
* FUNCTION: find_request_spec_list_entry(devconfig_t *request)
*
* INPUT: request - pointer to a devconfig_t struct
*
* RETURNS: request_spec_list_entry - pointer to a
* request_spec_list_entry struct
*
* PURPOSE: Lookup function which encapsulates the details of locating
* the device_spec_list_t cache entry for the input request.
*/
static request_spec_list_t *
find_request_spec_list_entry(
devconfig_t *request)
{
dlist_t *list_item = NULL;
request_spec_list_t *entry = NULL;
list_item = dlist_find(
_request_spec_list_cache,
(void *)request,
compare_request_to_request_spec_list_request);
if (list_item != NULL) {
entry = (request_spec_list_t *)list_item->obj;
}
return (entry);
}
/*
* FUNCTION: add_request_spec_list_entry(devconfig_t *request,
* char **avail_device_specs, char **unavail_device_specs,
* request_spec_list_entry_t **entry)
*
* INPUT: entry - pointer to the request_spec_list_entry struct to be
* added to the cache.
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which encapsulates the details of adding a
* device_spec_list_t cache entry.
*/
static int
add_request_spec_list_entry(
request_spec_list_t *entry)
{
dlist_t *list_item = dlist_new_item((void *)entry);
if (list_item == NULL) {
return (ENOMEM);
}
_request_spec_list_cache = dlist_append(list_item,
_request_spec_list_cache, AT_HEAD);
return (0);
}
/*
* FUNCTION: make_request_spec_list_entry(devconfig_t *request,
* char **avail_device_specs, char **unavail_device_specs,
* request_spec_list_entry_t **entry)
*
* INPUT: request - pointer to a devconfig_t struct
* avail_device_specs - char * array of user specified available
* devices associated with the input request
* unavail_device_specs - char * array of user specified
* unavailable devices associated with the input
* request
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which encapsulates the details of generating a new
* the device_spec_list_t cache entry for the input request
* and its lists of avail/unavail devices.
*
* Converts the input arrays of (un)available device names into
* equivalent lists of device_spec_t structs.
*
* Creates a new cache entry, populates it and adds it to the
* cache.
*/
static int
make_request_spec_list_entry(
devconfig_t *request,
char **avail_device_specs,
char **unavail_device_specs,
request_spec_list_t **entry)
{
int error = 0;
dlist_t *list = NULL;
*entry = calloc(1, sizeof (request_spec_list_t));
if (*entry == NULL) {
return (ENOMEM);
}
(*entry)->request = request;
/*
* map the avail_device_name array into a list of device_spec_t
* and save the list as the entry's available list
*/
error = convert_usernames_to_specs(
avail_device_specs, &list);
if (error == 0) {
(*entry)->avail_specs_list = list;
}
/*
* map the unavail_device_name array into a list of device_spec_t
* and save the list as the entry's unavailable list
*/
list = NULL;
error = convert_usernames_to_specs(
unavail_device_specs, &list);
if (error == 0) {
(*entry)->unavail_specs_list = list;
}
if (error != 0) {
/* delete the partial entry */
destroy_request_spec_list_entry((void *)*entry);
*entry = NULL;
}
return (error);
}
/*
* FUNCTION: convert_usernames_to_specs(char **specs, dlist_t **list)
*
* INPUT: specs - char * array of device CTD names
*
* OUTPUT: list - pointer to a list of device_spec_t corresponding
* to each name in the input array
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which converts the input CTD device names to the
* equivalent device_spec_t structs.
*
* Iterates the input array and converts each CTD name to a
* device_spec_t using get_spec_for_name().
*/
static int
convert_usernames_to_specs(
char **specs,
dlist_t **list)
{
int i = 0;
int error = 0;
/*
* For each spec in the array, get the corresponding
* device_spec_t and add it to the list.
*
* Any spec in the array that looks to be a DID name
* is first converted to its equivalent CTD name.
*/
for (i = 0;
(specs != NULL) && (specs[i] != NULL) && (error == 0);
i++) {
device_spec_t *spec = NULL;
char *userspec = specs[i];
error = get_spec_for_name(userspec, &spec);
if ((error == 0) && (spec != NULL)) {
dlist_t *list_item = dlist_new_item((void *)spec);
if (spec == NULL) {
error = ENOMEM;
} else {
*list = dlist_insert_ordered
(list_item, *list, DESCENDING,
compare_device_spec_specificity);
}
}
}
if (error != 0) {
/* the device_spec_t in the list items are maintained */
/* in a cache elsewhere, so don't free them here. */
dlist_free_items(*list, NULL);
*list = NULL;
}
return (error);
}
/*
* FUNCTION: destroy_request_spec_list_entry(void *entry)
*
* INPUT: entry - opaque pointer to a request_spec_list_t
*
* RETURNS: nothing
*
* PURPOSE: Function which reclaims memory allocated to a
* request_spec_list_t.
*
* Frees memory allocated to the avail_spec_list and
* unavail_spec_list. Entries in the list are not freed,
* since they are owned by the device_spec cache.
*/
static void
destroy_request_spec_list_entry(
void *obj)
{
request_spec_list_t *entry = (request_spec_list_t *)obj;
if (entry != NULL) {
/* items in the list are in the spec_cache and will */
/* be cleaned up when it is destroyed. */
dlist_free_items(entry->avail_specs_list, NULL);
dlist_free_items(entry->unavail_specs_list, NULL);
free(entry);
}
}
/*
* FUNCTION: destroy_request_spec_list_cache()
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which destroys all entries in the request_spec_list
* cache.
*/
static int
destroy_request_spec_list_cache()
{
dlist_free_items(_request_spec_list_cache,
destroy_request_spec_list_entry);
_request_spec_list_cache = NULL;
return (0);
}
/*
* FUNCTION: get_request_avail_spec_list(devconfig_t *request,
* dlist_t **list)
*
* INPUT: request - a pointer to a devconfig_t
*
* OUTPUT: list - pointer to a list of device_spec_t corresponding
* to the devices specified as available by the
* input request.
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which locates or builds the list of device_spec_t
* for the available devices specified in the input request.
*
* Looks up the input request in the request_spec_list cache.
* If there is currently no entry in the cache for the request,
* an entry is built and added.
*
* The entry's list of available device_spec_t is returned.
*/
static int
get_request_avail_spec_list(
devconfig_t *request,
dlist_t **list)
{
request_spec_list_t *entry = NULL;
int error = 0;
if ((entry = find_request_spec_list_entry(request)) == NULL) {
/* create cache entry for this request */
error = make_request_spec_list_entry(
request,
devconfig_get_available(request),
devconfig_get_unavailable(request),
&entry);
if ((error == 0) && (entry != NULL)) {
if ((error = add_request_spec_list_entry(entry)) != 0) {
destroy_request_spec_list_entry(entry);
entry = NULL;
}
}
}
if ((error == 0) && (entry != NULL)) {
*list = entry->avail_specs_list;
}
return (error);
}
/*
* FUNCTION: get_request_unavail_spec_list(devconfig_t *request,
* dlist_t **list)
*
* INPUT: request - a pointer to a devconfig_t
*
* OUTPUT: list - pointer to a list of device_spec_t corresponding
* to the devices specified as unavailable by the
* input request.
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which locates or builds the list of device_spec_t
* for the unavailable devices specified in the input request.
*
* Looks up the input request in the request_spec_list cache.
* If there is currently no entry in the cache for the request,
* an entry is built and added.
*
* The entry's list of unavailable device_spec_t is returned.
*/
static int
get_request_unavail_spec_list(
devconfig_t *request,
dlist_t **list)
{
request_spec_list_t *entry = NULL;
int error = 0;
if ((entry = find_request_spec_list_entry(request)) == NULL) {
/* create new entry for this request */
error = make_request_spec_list_entry(
request,
devconfig_get_available(request),
devconfig_get_unavailable(request),
&entry);
if ((error == 0) && (entry != NULL)) {
if ((error = add_request_spec_list_entry(entry)) != 0) {
destroy_request_spec_list_entry(entry);
entry = NULL;
}
}
}
if ((error == 0) && (entry != NULL)) {
*list = entry->unavail_specs_list;
}
return (error);
}
/*
* FUNCTION: get_default_avail_spec_list(defaults_t *defaults,
* char *dsname, dlist_t **list)
*
* INPUT: defaults - a pointer to a defaults_t struct
* dsname - the name of the diskset whose defaults should be used
*
* OUTPUT: list - pointer to a list of device_spec_t corresponding
* to the devices specified as available by the
* defaults for the named diskset, or the global
* defaults for all disksets.
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which locates or builds the list of device_spec_t
* for the available devices for the named diskset.
*
* Locates the defaults for the named diskset, if there are none,
* locates the global defaults for all disksets.
*
* The defaults devconfig_t struct is then used to look up the
* the corresponding entry in the request_spec_list cache.
*
* If there is currently no entry in the cache for the defaults,
* an entry is built and added.
*
* The entry's list of available device_spec_t is returned.
*/
static int
get_default_avail_spec_list(
defaults_t *alldefaults,
char *dsname,
dlist_t **list)
{
request_spec_list_t *entry = NULL;
devconfig_t *defaults = NULL;
int error = 0;
/* Get diskset defaults, or global if none for diskset */
error = defaults_get_diskset_by_name(
alldefaults, dsname, &defaults);
if (error != 0) {
if (error == ENOENT) {
/* to get global defaults, pass a NULL diskset name */
error = defaults_get_diskset_by_name(
alldefaults, NULL, &defaults);
}
if (error != 0) {
if (error != ENOENT) {
oprintf(OUTPUT_DEBUG,
gettext("get defaults for %s returned %d\n"),
dsname, error);
} else {
error = 0;
}
}
}
if ((entry = find_request_spec_list_entry(defaults)) == NULL) {
/* create new entry for these defaults */
error = make_request_spec_list_entry(
defaults,
devconfig_get_available(defaults),
devconfig_get_unavailable(defaults),
&entry);
if ((error == 0) && (entry != NULL)) {
if ((error = add_request_spec_list_entry(entry)) != 0) {
destroy_request_spec_list_entry(entry);
entry = NULL;
}
}
}
if ((error == 0) && (entry != NULL)) {
*list = entry->avail_specs_list;
}
return (error);
}
/*
* FUNCTION: get_default_unavail_spec_list(defaults_t *defaults,
* char *dsname, dlist_t **list)
*
* INPUT: defaults - a pointer to a defaults_t struct
* dsname - the name of the diskset whose defaults should be used
*
* OUTPUT: list - pointer to a list of device_spec_t corresponding
* to the devices specified as unavailable by the
* defaults for the named diskset, or the global
* defaults for all disksets.
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which locates or builds the list of device_spec_t
* for the unavailable devices for the named diskset.
*
* Locates the defaults for the named diskset, if there are none,
* locates the global defaults for all disksets.
*
* The defaults devconfig_t struct is then used to look up the
* the corresponding entry in the request_spec_list cache.
*
* If there is currently no entry in the cache for the defaults,
* an entry is built and added.
*
* The entry's list of unavailable device_spec_t is returned.
*/
static int
get_default_unavail_spec_list(
defaults_t *alldefaults,
char *dsname,
dlist_t **list)
{
request_spec_list_t *entry = NULL;
devconfig_t *defaults = NULL;
int error = 0;
/* Get diskset defaults, or global if none for diskset */
error = defaults_get_diskset_by_name(
alldefaults, dsname, &defaults);
if (error != 0) {
if (error == ENOENT) {
/* to get global defaults, pass a NULL diskset name */
error = defaults_get_diskset_by_name(
alldefaults, NULL, &defaults);
}
if (error != 0) {
if (error != ENOENT) {
oprintf(OUTPUT_DEBUG,
gettext("get defaults for %s returned %d\n"),
dsname, error);
} else {
error = 0;
}
}
}
if ((entry = find_request_spec_list_entry(defaults)) == NULL) {
/* create new entry for these defaults */
error = make_request_spec_list_entry(
defaults,
devconfig_get_available(defaults),
devconfig_get_unavailable(defaults),
&entry);
if ((error == 0) && (entry != NULL)) {
if ((error = add_request_spec_list_entry(entry)) != 0) {
destroy_request_spec_list_entry(entry);
entry = NULL;
}
}
}
if ((error == 0) && (entry != NULL)) {
*list = entry->unavail_specs_list;
}
return (error);
}
/*
* FUNCTION: is_named_device_avail(devconfig_t *request, char *device_name,
* boolean_t check_aliases, boolean_t *avail)
*
* INPUT: request - the current request devconfig_t
* device_name - char * device name
* check_aliases - boolean_t which indicates whether the device's
* aliases should be considered by the availability checks.
*
* OUTPUT: avail - a boolean_t * to hold the result
*
* RETURNS: int - !0 on error
*
* avail is set to B_TRUE if the named device is available for
* the input request, B_FALSE otherwise.
*
* PURPOSE: Determine if the named device can be used to satisfy the
* input request.
*
* There are several levels at which device availabiity or
* unavailability may be specifed:
*
* 1. the volume subrequest,
* 2. the toplevel (diskset) request,
* 3. the diskset-specific defaults
* 4. the global defaults
*
* If the diskset-specific defaults exist, only they are checked.
*
* The precedence ordering that is enforced:
*
* 1. if request has an avail list, the name must be in it
* and not in the request's unavail list.
* 2. if request has an unavail list, the name must not be in it.
* 3. if toplevel request has an avail list, the name must be
* in it and not in the toplevel request's unavailable
* list.
* 4. if toplevel request has an unavail list, the name must
* not be in it.
* 5. if defaults have an avail list, the name must be in it
* and not in the defaults unavailable list.
* 6. if defaults have an unavail list, the name must not be
* in it.
*/
static int
is_named_device_avail(
devconfig_t *request,
char *device_name,
boolean_t check_aliases,
boolean_t *avail)
{
typedef enum check_types {
DEVICE_REQUEST = 0,
DISKSET_REQUEST,
DEFAULTS,
N_CHECKS
} check_type_t;
check_type_t check_type;
typedef enum list_types {
AVAIL = 0,
UNAVAIL,
N_LISTS
} list_type_t;
dlist_t *lists[N_CHECKS][N_LISTS];
boolean_t includes;
int error = 0;
memset(lists, 0, (N_CHECKS * N_LISTS) * sizeof (dlist_t *));
if (request != NULL) {
/* get avail/unavail specs for request */
((error = get_request_avail_spec_list(
request, &lists[DEVICE_REQUEST][AVAIL])) != 0) ||
(error = get_request_unavail_spec_list(
request, &lists[DEVICE_REQUEST][UNAVAIL]));
}
if ((error == 0) && (_toplevel_request != NULL)) {
/* diskset request */
((error = get_request_avail_spec_list(
_toplevel_request, &lists[DISKSET_REQUEST][AVAIL])) != 0) ||
(error = get_request_unavail_spec_list(
_toplevel_request, &lists[DISKSET_REQUEST][UNAVAIL]));
}
if ((error == 0) && (_defaults != NULL)) {
/* and diskset/global defaults */
((error = get_default_avail_spec_list(_defaults,
get_request_diskset(), &lists[DEFAULTS][AVAIL])) != 0) ||
(error = get_default_unavail_spec_list(_defaults,
get_request_diskset(), &lists[DEFAULTS][UNAVAIL]));
}
if (error != 0) {
return (error);
}
*avail = B_TRUE;
for (check_type = DEVICE_REQUEST;
(check_type < N_CHECKS) && (error == 0);
check_type++) {
if (lists[check_type][AVAIL] != NULL) {
/* does avail spec list include named device? */
if ((error = avail_list_includes_device_name(
lists[check_type][AVAIL], device_name, check_aliases,
&includes)) == 0) {
if (includes != B_TRUE) {
*avail = B_FALSE;
}
if ((includes == B_TRUE) &&
(lists[check_type][UNAVAIL] != NULL)) {
/* device is available, is it in the unavail list? */
if ((error = unavail_list_includes_device_name(
lists[check_type][UNAVAIL], device_name,
check_aliases, &includes)) == 0) {
if (includes == B_TRUE) {
*avail = B_FALSE;
}
}
}
}
/* lists at this level checked, skip remainder */
break;
} else if (lists[check_type][UNAVAIL] != NULL) {
/* does unavail spec list include named device? */
if ((error = unavail_list_includes_device_name(
lists[check_type][UNAVAIL], device_name,
check_aliases, &includes)) == 0) {
if (includes == B_TRUE) {
*avail = B_FALSE;
}
}
/* list at this level checked, skip remainder */
break;
}
}
return (error);
}
/*
* FUNCTION: avail_list_includes_device_name(dlist_t *list,
* char *device_name, boolean_t check_aliases,
* boolean_t *includes)
*
* INPUT: list - a dlist_t list of available device_spec_t
* device_name - a char * device CTD name
* check_aliases - boolean_t which indicates if the device's
* aliases should be considered in the availability
* checking.
*
* OUTPUT: includes - B_TRUE - if named device is "included" by any
* specification in the input list
* B_FALSE - otherwise
*
* RETURNS: int - 0 on success
* - !0 otherwise
*
* PURPOSE: Helper used by is_named_device_avail that determines
* if the input list of device specifications "includes"
* a specific device.
*
* Iterates the elements of the input array and searches
* for a match using spec_includes_device_name().
*/
static int
avail_list_includes_device_name(
dlist_t *list,
char *device_name,
boolean_t check_aliases,
boolean_t *includes)
{
dlist_t *iter = NULL;
int error = 0;
*includes = B_FALSE;
for (iter = list;
(*includes == B_FALSE) && (iter != NULL) && (error == 0);
iter = iter->next) {
device_spec_t *spec = (device_spec_t *)iter->obj;
error = spec_includes_device_name(spec, device_name,
check_aliases, includes);
}
return (0);
}
/*
* FUNCTION: unavail_list_includes_device_name(dlist_t *list,
* char *device_name, boolean_t check_aliases,
* boolean_t *includes)
*
* INPUT: list - a dlist_t list of unavailable device_spec_t
* device_name - a char * device CTD name
* check_aliases - boolean_t which indicates if the device's
* aliases should be considered in the availability
* checking.
*
* OUTPUT: includes - B_TRUE - if named device is "included" by any
* specification in the input list
* B_FALSE - otherwise
*
* RETURNS: int - 0 on success
* - !0 otherwise
*
* PURPOSE: Helper used by is_named_device_avail that determines
* if the input list of device specifications "includes"
* a specific device.
*
* Iterates the elements of the input array and searches
* for a match using spec_includes_device_name_or_alias().
*/
static int
unavail_list_includes_device_name(
dlist_t *list,
char *device_name,
boolean_t check_aliases,
boolean_t *includes)
{
dlist_t *iter = NULL;
int error = 0;
device_spec_t *unavail_spec;
boolean_t check_for_alternate_hba = B_FALSE;
*includes = B_FALSE;
/*
* the specs in the list are in descending order of specificity.
* so a more exact spec will rule the device out before a less
* exact spec.
*
* Meaning: if the list has { "c3t0d0", ..., "c3", ... } and the
* input device name is "c3t0d0s0", it will match "c3t0d0"
* before "c3".
*
* This is important for the multi-path alias checking below.
* If the input device name is ruled out by a non-controller
* specification, it is really unavailable.
*/
for (iter = list;
(*includes == B_FALSE) && (iter != NULL);
iter = iter->next) {
unavail_spec = (device_spec_t *)iter->obj;
error = spec_includes_device_name(
unavail_spec, device_name, check_aliases, includes);
}
if ((error == 0) && (*includes == B_TRUE)) {
/* matched an unavailable spec, was it a controller/HBA? */
oprintf(OUTPUT_DEBUG,
"device \"%s\" is unavailable, "
"it matched \"c(%d)t(%d)d(%d)s(%d)\"\n",
device_name,
unavail_spec->data.ctd->ctrl,
unavail_spec->data.ctd->target,
unavail_spec->data.ctd->lun,
unavail_spec->data.ctd->slice);
if ((unavail_spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(unavail_spec->data.ctd->target == ID_UNSPECIFIED) &&
(unavail_spec->data.ctd->lun == ID_UNSPECIFIED) &&
(unavail_spec->data.ctd->slice == ID_UNSPECIFIED)) {
/*
* Need to see if the named device is a disk or slice,
* and if so check to see if the it is multipathed
* and possibly accessible thru another controller/HBA.
*/
check_for_alternate_hba = B_TRUE;
}
}
if ((error == 0) && (check_for_alternate_hba == B_TRUE)) {
dm_descriptor_t slice = (dm_descriptor_t)0;
dm_descriptor_t disk = (dm_descriptor_t)0;
((error = slice_get_by_name(device_name, &slice)) != 0) ||
(error = disk_get_by_name(device_name, &disk));
if (error != 0) {
return (error);
}
/* if it is a slice, get its disk */
if ((error == 0) && (slice != (dm_descriptor_t)0)) {
error = slice_get_disk(slice, &disk);
}
if ((error == 0) && (disk != (dm_descriptor_t)0)) {
/* see if all the disk's HBAs are unavailable */
dlist_t *hbas = NULL;
dlist_t *iter = NULL;
error = disk_get_hbas(disk, &hbas);
if (hbas != NULL) {
oprintf(OUTPUT_DEBUG,
gettext(" checking alternate paths for %s\n"),
device_name);
} else {
oprintf(OUTPUT_DEBUG,
gettext(" no alternate paths for %s\n"),
device_name);
}
/* for each of the disk's HBAs */
for (iter = hbas;
(iter != NULL) && (*includes == B_TRUE) && (error == 0);
iter = iter->next) {
dm_descriptor_t hba = (uintptr_t)iter->obj;
device_spec_t *hbaspec;
char *hbaname = NULL;
dlist_t *iter2 = NULL;
*includes = B_FALSE;
((error = get_display_name(hba, &hbaname)) != 0) ||
(error = get_spec_for_name(hbaname, &hbaspec));
/* is HBA unavailable? */
for (iter2 = list;
(iter2 != NULL) && (error == 0) &&
(*includes == B_FALSE);
iter2 = iter2->next) {
device_spec_t *spec =
(device_spec_t *)iter2->obj;
*includes = spec_includes_device(spec, hbaspec);
}
}
dlist_free_items(hbas, NULL);
/* if *includes==B_TRUE here, all HBAs are unavailable */
}
}
return (error);
}
/*
* FUNCTION: spec_includes_device_name(device_spec_t *spec,
* char *device_name, boolean_t check_aliases,
* boolean_t *includes)
*
* INPUT: spec - a device_spec_t CTD specification.
* device_name - a char * device CTD name
* check_aliases - boolean_t which indicates if the device's
* aliases should be considered in the checking.
*
* OUTPUT: includes - B_TRUE - if device is "included" by the input
* specification
* B_FALSE - otherwise
*
* RETURNS: int - 0 on success
* - !0 otherwise
*
* PURPOSE: Helper used by (un)avail_specs_includes_device_name() that
* determines if the input device specification "includes"
* the named device.
*
* If check_aliases is true and the named device is a slice or
* a disk drive, its multi-pathed aliases are also checked
* against the spec.
*/
static int
spec_includes_device_name(
device_spec_t *spec,
char *device_name,
boolean_t check_aliases,
boolean_t *includes)
{
device_spec_t *device_spec;
int error = 0;
error = get_spec_for_name(device_name, &device_spec);
if (error == 0) {
*includes = spec_includes_device(spec, device_spec);
if ((*includes == B_FALSE) && (check_aliases == B_TRUE)) {
/* spec doesn't include name, check aliases */
dm_descriptor_t device = (dm_descriptor_t)0;
dlist_t *aliases = NULL;
/* only slices and disks have aliases */
error = slice_get_by_name(device_name, &device);
if (device != (dm_descriptor_t)0) {
error = get_aliases(device, &aliases);
} else if (error == 0) {
error = disk_get_by_name(device_name, &device);
if (device != (dm_descriptor_t)0) {
error = get_aliases(device, &aliases);
}
}
if ((error == 0) && (aliases != NULL)) {
dlist_t *iter;
for (iter = aliases;
(iter != NULL) && (*includes == B_FALSE) &&
(error == 0);
iter = iter->next) {
char *alias = (char *)iter->obj;
device_spec_t *alias_spec;
error = get_spec_for_name(alias, &alias_spec);
if (error == 0) {
/* does spec include alias? */
*includes = spec_includes_device(spec, alias_spec);
}
}
}
dlist_free_items(aliases, free);
}
}
return (error);
}
/*
* FUNCTION: destroy_device_spec(device_spec_t *spec)
*
* INPUT: spec - pointer to a device_spec_t
*
* RETURNS: nothing
*
* PURPOSE: Function which reclaims memory allocated to a device_spec_t.
*
* Frees memory allocated to hold the specific data in the spec.
*/
static void
destroy_device_spec(
device_spec_t *spec)
{
if (spec != NULL) {
if (spec->type == SPEC_TYPE_CTD) {
free(spec->data.ctd);
} else if (spec->type == SPEC_TYPE_RAW) {
free(spec->data.raw);
}
free(spec);
}
}
/*
* FUNCTION: create_device_spec(char *name, device_spec_t **spec);
*
* INPUT: name - pointer to a char* device name
*
* OUTPUT: spec - pointer to a device_spec_t to hold the result
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: Function which creates a device_spec_t for the input
* device name.
*
*/
static int
create_device_spec(
char *name,
device_spec_t **spec)
{
int error = 0;
/* allocate the device spec and try various parsing schemes */
*spec = (device_spec_t *)calloc(1, sizeof (device_spec_t));
if (*spec == NULL) {
error = ENOMEM;
} else {
if (((error = create_device_ctd_spec(name, spec)) != 0) &&
(error != ENOMEM)) {
/* CTD failed, try other parsing schemes */
error = create_device_raw_spec(name, spec);
}
}
return (error);
}
/*
* FUNCTION: create_device_ctd_spec(char *name, device_spec_t **spec);
*
* INPUT: name - pointer to a char* device name
*
* OUTPUT: spec - pointer to a device_spec_t updated with the parsed
* CTD spec, if successful
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: Function which atttempts to parse the input device name into
* cXtXdXsX component ids. The ids are the integer values of each
* specified segment of the input name.
*
* If the name doesn't contain a segment, the id is set to
* ID_UNSPECIFIED.
*
* The input name must be well-formed.
*
* These are the acceptable forms:
*
* cXtXdXsX
* cXtXdX
* cXtX
* cXdXsX
* cXdX
* cX
*/
static int
create_device_ctd_spec(
char *name,
device_spec_t **spec)
{
uint_t ctrl;
uint_t target;
uint_t lun;
uint_t slice;
uint_t nscan;
uint_t nchars;
char *device_str;
char *target_str;
char *ctd_str;
char *t_ptr;
char *d_ptr;
char *s_ptr;
boolean_t is_ide = B_FALSE;
boolean_t got_slice = B_FALSE;
boolean_t got_lun = B_FALSE;
boolean_t got_target = B_FALSE;
boolean_t got_ctrl = B_FALSE;
int error = 0;
ctd_str = strdup(name);
if (ctd_str == NULL) {
return (ENOMEM);
}
/* trim any leading path (/dev/dsk/cXtXdXsX) */
if ((device_str = strrchr(ctd_str, '/')) != NULL) {
++device_str;
} else {
device_str = ctd_str;
}
/* find each segment start position */
t_ptr = strrchr(device_str, 't');
d_ptr = strrchr(device_str, 'd');
s_ptr = strrchr(device_str, 's');
/*
* scan ids from each existing segment working backwards
* so as to leave the device_str in the correct state
* for the next expected segment
*/
if (s_ptr != NULL) {
/* found 's', try to get slice */
nchars = strlen(s_ptr);
if ((sscanf(s_ptr, "s%u%n", &slice, &nscan) != 1) ||
(nscan != nchars)) {
error = -1;
oprintf(OUTPUT_DEBUG,
gettext("no slice component in device "
"name \"%s\".\n"),
name);
} else {
got_slice = B_TRUE;
*s_ptr = '\0';
}
}
if ((error == 0) && (d_ptr != NULL)) {
/* found 'd', try to get disk/lun */
nchars = strlen(d_ptr);
if ((sscanf(d_ptr, "d%u%n", &lun, &nscan) != 1) ||
(nscan != nchars)) {
error = -1;
oprintf(OUTPUT_DEBUG,
gettext("no disk/lun component "
"in device name \"%s\".\n"),
name);
} else {
got_lun = B_TRUE;
*d_ptr = '\0';
}
}
if ((error == 0) && (t_ptr != NULL)) {
/* found 't', try to get target, it may be a hex WWN id */
/* skip leading 't' and add two for the 'OX' */
nchars = strlen(t_ptr + 1) + 2;
if ((target_str = (char *)malloc(nchars+1)) == NULL) {
error = ENOMEM;
} else {
strcpy(target_str, "0X");
strcpy(target_str+2, t_ptr + 1);
target_str[nchars] = '\0';
if ((sscanf(target_str, "%x%n", &target, &nscan) != 1) ||
(nscan != nchars)) {
error = -1;
oprintf(OUTPUT_DEBUG,
gettext("no target/WWN component "
"in device name \"%s\".\n"),
name);
} else {
got_target = B_TRUE;
*t_ptr = '\0';
}
free(target_str);
}
} else {
is_ide = B_TRUE;
}
if ((error == 0) && (device_str != NULL)) {
/* get controller/hba/channel */
nchars = strlen(device_str);
if ((sscanf(device_str, "c%u%n", &ctrl, &nscan) != 1) ||
(nscan != nchars)) {
error = -1;
oprintf(OUTPUT_DEBUG,
gettext("no channel/HBA component "
"in device name \"%s\".\n"),
name);
} else {
got_ctrl = B_TRUE;
}
}
free(ctd_str);
if (error == 0) {
/* allocate the ctd_spec_t struct and store the ids */
(*spec)->type = SPEC_TYPE_CTD;
(*spec)->data.ctd = (ctd_spec_t *)calloc(1, sizeof (ctd_spec_t));
if ((*spec)->data.ctd == NULL) {
error = ENOMEM;
}
(*spec)->data.ctd->slice = ID_UNSPECIFIED;
(*spec)->data.ctd->lun = ID_UNSPECIFIED;
(*spec)->data.ctd->target = ID_UNSPECIFIED;
(*spec)->data.ctd->ctrl = ID_UNSPECIFIED;
if (got_slice == B_TRUE) {
(*spec)->data.ctd->slice = slice;
}
if (got_lun == B_TRUE) {
(*spec)->data.ctd->lun = lun;
}
if (got_target == B_TRUE) {
(*spec)->data.ctd->target = target;
}
if (got_ctrl == B_TRUE) {
(*spec)->data.ctd->ctrl = ctrl;
}
(*spec)->data.ctd->is_ide = is_ide;
}
return (error);
}
/*
* FUNCTION: create_device_raw_spec(char *name, device_spec_t **spec);
*
* INPUT: name - pointer to a char* device name
*
* OUTPUT: spec - pointer to a device_spec_t updated with the raw spec
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: Function which creates a "raw" spec for the input name.
*
* This is a last resort if all other spec parsing schemes failed,
* the "raw" spec is just the input device name.
*/
static int
create_device_raw_spec(
char *name,
device_spec_t **spec)
{
int error = 0;
char *ctd_str = strdup(name);
if (ctd_str == NULL) {
return (ENOMEM);
}
(*spec)->type = SPEC_TYPE_RAW;
(*spec)->data.raw = ctd_str;
oprintf(OUTPUT_DEBUG,
gettext("made raw device spec for \"%s\"\n"), ctd_str);
return (error);
}
/*
* FUNCTION: get_spec_for_name(char *name, device_spec_t **id);
*
* INPUT: name - pointer to a char* device name
*
* OUTPUT: id - pointer to a device_spec_t to hold the result
*
* RETURNS: int - 0 on success
* !0 otherwise
*
* PURPOSE: Function which finds the device_spec_t that already
* exists for the input name or creates it.
*
* The returned struct should not be freed, it is maintained
* in a cache that will be purged when the layout process
* is complete.
*/
int
get_spec_for_name(
char *name,
device_spec_t **id)
{
dlist_t *item;
int error = 0;
item = dlist_find(_spec_cache, (void *)name,
compare_name_to_spec_cache_name);
if (item == NULL) {
if ((error = create_device_spec(name, id)) == 0) {
spec_cache_t *entry = (spec_cache_t *)
calloc(1, sizeof (spec_cache_t));
if (entry == NULL) {
destroy_device_spec(*id);
error = ENOMEM;
} else {
char *dup = strdup(name);
if (dup == NULL) {
free(entry);
destroy_device_spec(*id);
*id = NULL;
error = ENOMEM;
} else {
entry->name = dup;
entry->device_spec = *id;
}
if (error == 0) {
dlist_t *item = dlist_new_item((void *)entry);
if (item == NULL) {
free(entry);
destroy_device_spec(*id);
*id = NULL;
error = ENOMEM;
} else {
_spec_cache =
dlist_append(item, _spec_cache, AT_HEAD);
}
}
}
}
} else {
*id = ((spec_cache_t *)item->obj)->device_spec;
}
return (error);
}
/*
* FUNCTION: spec_includes_device(device_spec_t *spec,
* device_spec_t *device)
*
* INPUT: spec - pointer to a device_spec struct
* device - pointer to a device_spec struct
*
* RETURNS: boolean_t - B_TRUE if the device is included in the spec
* B_FALSE otherwise
*
* PURPOSE: Function which determines if the input device matches the
* input spec.
*
* If both specs are of the same type, the appropriate
* comparison function is called.
*
* If the two specs are of different types, no comparison
* is done and B_FALSE is returned.
*/
boolean_t
spec_includes_device(
device_spec_t *spec,
device_spec_t *device)
{
if ((spec->type == SPEC_TYPE_CTD) && (device->type == SPEC_TYPE_CTD)) {
return (ctd_spec_includes_device(spec, device));
} else if ((spec->type == SPEC_TYPE_RAW) &&
(device->type == SPEC_TYPE_RAW)) {
return (raw_spec_includes_device(spec, device));
}
return (B_FALSE);
}
/*
* FUNCTION: ctd_spec_includes_device(device_spec_t *spec,
* device_spec_t *device)
*
* INPUT: spec - pointer to a device_spec struct
* device - pointer to a device_spec struct
*
* RETURNS: boolean_t - B_TRUE if the device is included in the spec
* B_FALSE otherwise
*
* PURPOSE: Function which determines if the input CTD device spec
* matches the input CTD spec.
*
* The device_spec_t structs contain component "ids" for
* both the specification and the device.
*
* The device must match each of the ids in the spec that
* are specified.
*
* spec devices matched
* --------------------------------------------------------
* cX cX, cXtX, cXtXdX, cXtXdXsX, cXdX, cXdXsX
* cXtX cXtX, cXtXdX, cXtXdXsX
* cXtXdX cXtXdX, cXtXdXsX
* cXtXdXsX cXtXdXsX
* cXdX cXdX, cXdXsX
* cXdXsX cXdXsX
*/
static boolean_t
ctd_spec_includes_device(
device_spec_t *spec,
device_spec_t *device)
{
boolean_t match = B_FALSE;
if (spec->data.ctd->is_ide) {
/* valid IDE names are cX, cXdX, cXdXsX, no target */
if ((spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(spec->data.ctd->lun != ID_UNSPECIFIED) &&
(spec->data.ctd->slice != ID_UNSPECIFIED)) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl) &&
(spec->data.ctd->lun == device->data.ctd->lun) &&
(spec->data.ctd->slice == device->data.ctd->slice);
} else if ((spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(spec->data.ctd->lun != ID_UNSPECIFIED)) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl) &&
(spec->data.ctd->lun == device->data.ctd->lun);
} else if (spec->data.ctd->ctrl != ID_UNSPECIFIED) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl);
}
} else {
/* valid names are cX, cXtX, cXtXdX, cXtXdXsX */
if ((spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(spec->data.ctd->target != ID_UNSPECIFIED) &&
(spec->data.ctd->lun != ID_UNSPECIFIED) &&
(spec->data.ctd->slice != ID_UNSPECIFIED)) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl) &&
(spec->data.ctd->target == device->data.ctd->target) &&
(spec->data.ctd->lun == device->data.ctd->lun) &&
(spec->data.ctd->slice == device->data.ctd->slice);
} else if ((spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(spec->data.ctd->target != ID_UNSPECIFIED) &&
(spec->data.ctd->lun != ID_UNSPECIFIED)) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl) &&
(spec->data.ctd->target == device->data.ctd->target) &&
(spec->data.ctd->lun == device->data.ctd->lun);
} else if ((spec->data.ctd->ctrl != ID_UNSPECIFIED) &&
(spec->data.ctd->target != ID_UNSPECIFIED)) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl) &&
(spec->data.ctd->target == device->data.ctd->target);
} else if (spec->data.ctd->ctrl != ID_UNSPECIFIED) {
match = (spec->data.ctd->ctrl == device->data.ctd->ctrl);
}
}
oprintf(OUTPUT_DEBUG,
gettext("spec: c(%d) t(%d) d(%d) s(%d) "
"%s: c(%d) t(%d) d(%d) s(%d)\n"),
spec->data.ctd->ctrl, spec->data.ctd->target,
spec->data.ctd->lun, spec->data.ctd->slice,
(match ? gettext("includes") : gettext("does not include")),
device->data.ctd->ctrl, device->data.ctd->target,
device->data.ctd->lun, device->data.ctd->slice);
return (match);
}
/*
* FUNCTION: raw_spec_includes_device(device_spec_t *spec,
* device_spec_t *device)
*
* INPUT: spec - pointer to a device_spec struct
* device - pointer to a device_spec struct
*
* RETURNS: boolean_t - B_TRUE if the device is included in the spec
* B_FALSE otherwise
*
* PURPOSE: Function which determines if the input raw device spec
* matches the input spec.
*
* The device_spec_t raw elements are checked.
*
* If the spec's raw device name is exactly contained at the
* beginning of the device spec's raw name, then the function
* evaluates to true.
*/
static boolean_t
raw_spec_includes_device(
device_spec_t *spec,
device_spec_t *device)
{
return (strncasecmp(spec->data.raw,
device->data.raw, strlen(spec->data.raw)) == 0);
}
/*
* FUNCTION: compare_name_to_spec_cache_name(void *name, void *list_item)
*
* INPUT: name - opaque pointer to a char * device name
* list_item - opaque pointer to a spec_cache_t entry
*
* RETURNS: int - 0 - if request is the same as list_item->request
* !0 - otherwise
*
* PURPOSE: dlist_t helper which compares the input device name
* to the list_item's device name for equality.
*
* This function is the lookup mechanism for the device_spec
* associated with the name.
*/
static int
compare_name_to_spec_cache_name(
void *name,
void *list_item)
{
spec_cache_t *entry = (spec_cache_t *)list_item;
assert(name != NULL);
assert(entry != NULL);
return (string_case_compare((char *)name, entry->name));
}
/*
* FUNCTION: destroy_spec_cache_entry(void *entry)
*
* INPUT: entry - opaque pointer to a spec_cache_t
*
* RETURNS: nothing
*
* PURPOSE: Function which reclaims memory allocated to a
* spec_cache_t entry.
*
* Frees memory allocated to hold the CTD name and the
* corresponding device_spec_t.
*/
static void
destroy_spec_cache_entry(
void *obj)
{
spec_cache_t *entry = (spec_cache_t *)obj;
if (entry != NULL) {
free(entry->name);
destroy_device_spec(entry->device_spec);
free(entry);
}
}
/*
* FUNCTION: destroy_spec_cache()
*
* RETURNS: int - 0 on success
* !0 otherwise.
*
* PURPOSE: Function which destroys all entries in the device_spec
* cache.
*/
static int
destroy_spec_cache()
{
dlist_free_items(_spec_cache, destroy_spec_cache_entry);
_spec_cache = NULL;
return (0);
}
/*
* FUNCTION: get_device_access_name(devconfig_t *request,
* dm_descriptor_t desc, char **name)
*
* INPUT: request - a devconfig_t request
* desc - a dm_descriptor_t device handle
*
* OUTPUT: name - a char * pointer to hold the preferred name
*
* RETURNS: int - 0 - if request is the same as list_item->request
* !0 - otherwise
*
* PURPOSE: Utility function to determine which of the possible device
* names should be used to access a known available device.
*
* Devices handled are slices and disks.
*
* If the input device is a multipathed disk or slice, it
* can have several possible names. Determine which of the
* names should be used based on the input request's available
* or unavailable device specifications.
*
*/
int
get_device_access_name(
devconfig_t *request,
dm_descriptor_t desc,
char **name)
{
int error = 0;
boolean_t avail = B_FALSE;
dlist_t *aliases = NULL;
assert(desc != (dm_descriptor_t)0);
*name = NULL;
if ((error = get_display_name(desc, name)) != 0) {
return (error);
}
if (is_did_name(*name) == B_TRUE) {
oprintf(OUTPUT_DEBUG,
gettext("device DID name %s is preferred\n"),
*name);
return (0);
}
error = is_named_device_avail(request, *name, B_FALSE, &avail);
if (error != 0) {
return (error);
}
if (avail == B_TRUE) {
oprintf(OUTPUT_DEBUG,
gettext("device name %s is accessible\n"),
*name);
return (0);
}
/* search aliases for an 'available' name, prefer DID names */
if ((error = get_aliases(desc, &aliases)) == 0) {
dlist_t *iter = aliases;
char *availname = NULL;
char *didname = NULL;
for (; (iter != NULL) && (error == 0); iter = iter->next) {
char *alias = (char *)iter->obj;
error = is_named_device_avail(request, alias, B_FALSE, &avail);
if ((error == 0) && (avail == B_TRUE)) {
oprintf(OUTPUT_DEBUG,
gettext("device alias %s is accessible for %s\n"),
alias, *name);
availname = alias;
if (is_did_name(availname) == B_TRUE) {
didname = alias;
break;
}
}
}
if (error == 0) {
if (didname != NULL) {
*name = didname;
} else if (availname != NULL) {
*name = availname;
}
}
}
return (error);
}
|