summaryrefslogtreecommitdiff
path: root/usr/src/lib/libshare/common/libshare.c
blob: 1d17d5fea79be71edd81b2e514bd59e7e66b9014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Share control API
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <libscf.h>
#include "scfutil.h"
#include <ctype.h>
#include <libintl.h>
#include <thread.h>
#include <synch.h>

#define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
#define	SA_STRSIZE	256	/* max string size for names */

/*
 * internal object type values returned by sa_get_object_type()
 */
#define	SA_TYPE_UNKNOWN		0
#define	SA_TYPE_GROUP		1
#define	SA_TYPE_SHARE		2
#define	SA_TYPE_RESOURCE	3
#define	SA_TYPE_OPTIONSET	4
#define	SA_TYPE_ALTSPACE	5

/*
 * internal data structures
 */

extern struct sa_proto_plugin *sap_proto_list;

/* current SMF/SVC repository handle */
extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
extern char *sa_fstype(char *);
extern int sa_is_share(void *);
extern int sa_is_resource(void *);
extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
extern int sa_group_is_zfs(sa_group_t);
extern int sa_path_is_zfs(char *);
extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
extern int sa_zfs_set_sharesmb(sa_group_t, char *, int);
extern void update_legacy_config(sa_handle_t);
extern int issubdir(char *, char *);
extern int sa_zfs_init(sa_handle_impl_t);
extern void sa_zfs_fini(sa_handle_impl_t);
extern void sablocksigs(sigset_t *);
extern void saunblocksigs(sigset_t *);
static sa_group_t sa_get_optionset_parent(sa_optionset_t);
static char *get_node_attr(void *, char *);
extern void sa_update_sharetab_ts(sa_handle_t);

/*
 * Data structures for finding/managing the document root to access
 * handle mapping. The list isn't expected to grow very large so a
 * simple list is acceptable. The purpose is to provide a way to start
 * with a group or share and find the library handle needed for
 * various operations.
 */
mutex_t sa_global_lock;
struct doc2handle {
	struct doc2handle	*next;
	xmlNodePtr		root;
	sa_handle_impl_t	handle;
};

mutex_t sa_dfstab_lock;

/* definitions used in a couple of property functions */
#define	SA_PROP_OP_REMOVE	1
#define	SA_PROP_OP_ADD		2
#define	SA_PROP_OP_UPDATE	3

static struct doc2handle *sa_global_handles = NULL;

/* helper functions */

/*
 * sa_errorstr(err)
 *
 * convert an error value to an error string
 */

char *
sa_errorstr(int err)
{
	static char errstr[32];
	char *ret = NULL;

	switch (err) {
	case SA_OK:
		ret = dgettext(TEXT_DOMAIN, "ok");
		break;
	case SA_NO_SUCH_PATH:
		ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
		break;
	case SA_NO_MEMORY:
		ret = dgettext(TEXT_DOMAIN, "no memory");
		break;
	case SA_DUPLICATE_NAME:
		ret = dgettext(TEXT_DOMAIN, "name in use");
		break;
	case SA_BAD_PATH:
		ret = dgettext(TEXT_DOMAIN, "bad path");
		break;
	case SA_NO_SUCH_GROUP:
		ret = dgettext(TEXT_DOMAIN, "no such group");
		break;
	case SA_CONFIG_ERR:
		ret = dgettext(TEXT_DOMAIN, "configuration error");
		break;
	case SA_SYSTEM_ERR:
		ret = dgettext(TEXT_DOMAIN, "system error");
		break;
	case SA_SYNTAX_ERR:
		ret = dgettext(TEXT_DOMAIN, "syntax error");
		break;
	case SA_NO_PERMISSION:
		ret = dgettext(TEXT_DOMAIN, "no permission");
		break;
	case SA_BUSY:
		ret = dgettext(TEXT_DOMAIN, "busy");
		break;
	case SA_NO_SUCH_PROP:
		ret = dgettext(TEXT_DOMAIN, "no such property");
		break;
	case SA_INVALID_NAME:
		ret = dgettext(TEXT_DOMAIN, "invalid name");
		break;
	case SA_INVALID_PROTOCOL:
		ret = dgettext(TEXT_DOMAIN, "invalid protocol");
		break;
	case SA_NOT_ALLOWED:
		ret = dgettext(TEXT_DOMAIN, "operation not allowed");
		break;
	case SA_BAD_VALUE:
		ret = dgettext(TEXT_DOMAIN, "bad property value");
		break;
	case SA_INVALID_SECURITY:
		ret = dgettext(TEXT_DOMAIN, "invalid security type");
		break;
	case SA_NO_SUCH_SECURITY:
		ret = dgettext(TEXT_DOMAIN, "security type not found");
		break;
	case SA_VALUE_CONFLICT:
		ret = dgettext(TEXT_DOMAIN, "property value conflict");
		break;
	case SA_NOT_IMPLEMENTED:
		ret = dgettext(TEXT_DOMAIN, "not implemented");
		break;
	case SA_INVALID_PATH:
		ret = dgettext(TEXT_DOMAIN, "invalid path");
		break;
	case SA_NOT_SUPPORTED:
		ret = dgettext(TEXT_DOMAIN, "operation not supported");
		break;
	case SA_PROP_SHARE_ONLY:
		ret = dgettext(TEXT_DOMAIN, "property not valid for group");
		break;
	case SA_NOT_SHARED:
		ret = dgettext(TEXT_DOMAIN, "not shared");
		break;
	case SA_NO_SUCH_RESOURCE:
		ret = dgettext(TEXT_DOMAIN, "no such resource");
		break;
	case SA_RESOURCE_REQUIRED:
		ret = dgettext(TEXT_DOMAIN, "resource name required");
		break;
	case SA_MULTIPLE_ERROR:
		ret = dgettext(TEXT_DOMAIN, "errors from multiple protocols");
		break;
	case SA_PATH_IS_SUBDIR:
		ret = dgettext(TEXT_DOMAIN, "path is a subpath of share");
		break;
	case SA_PATH_IS_PARENTDIR:
		ret = dgettext(TEXT_DOMAIN, "path is parent of a share");
		break;
	case SA_NO_SECTION:
		ret = dgettext(TEXT_DOMAIN, "protocol requires a section");
		break;
	case SA_NO_PROPERTIES:
		ret = dgettext(TEXT_DOMAIN, "properties not found");
		break;
	case SA_NO_SUCH_SECTION:
		ret = dgettext(TEXT_DOMAIN, "section not found");
		break;
	case SA_PASSWORD_ENC:
		ret = dgettext(TEXT_DOMAIN, "passwords must be encrypted");
		break;
	case SA_SHARE_EXISTS:
		ret = dgettext(TEXT_DOMAIN, "path or file is already shared");
		break;
	default:
		(void) snprintf(errstr, sizeof (errstr),
		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
		ret = errstr;
	}
	return (ret);
}

/*
 * Document root to active handle mapping functions.  These are only
 * used internally. A mutex is used to prevent access while the list
 * is changing. In general, the list will be relatively short - one
 * item per thread that has called sa_init().
 */

sa_handle_impl_t
get_handle_for_root(xmlNodePtr root)
{
	struct doc2handle *item;

	(void) mutex_lock(&sa_global_lock);
	for (item = sa_global_handles; item != NULL; item = item->next) {
		if (item->root == root)
			break;
	}
	(void) mutex_unlock(&sa_global_lock);
	if (item != NULL)
		return (item->handle);
	return (NULL);
}

static int
add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
{
	struct doc2handle *item;
	int ret = SA_NO_MEMORY;

	item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1);
	if (item != NULL) {
		item->root = root;
		item->handle = handle;
		(void) mutex_lock(&sa_global_lock);
		item->next = sa_global_handles;
		sa_global_handles = item;
		(void) mutex_unlock(&sa_global_lock);
		ret = SA_OK;
	}
	return (ret);
}

/*
 * remove_handle_for_root(root)
 *
 * Walks the list of handles and removes the one for this "root" from
 * the list. It is up to the caller to free the data.
 */

static void
remove_handle_for_root(xmlNodePtr root)
{
	struct doc2handle *item, *prev;

	(void) mutex_lock(&sa_global_lock);
	for (prev = NULL, item = sa_global_handles; item != NULL;
	    item = item->next) {
		if (item->root == root) {
			/* first in the list */
			if (prev == NULL)
				sa_global_handles = sa_global_handles->next;
			else
				prev->next = item->next;
			/* Item is out of the list so free the list structure */
			free(item);
			break;
		}
		prev = item;
	}
	(void) mutex_unlock(&sa_global_lock);
}

/*
 * sa_find_group_handle(sa_group_t group)
 *
 * Find the sa_handle_t for the configuration associated with this
 * group.
 */
sa_handle_t
sa_find_group_handle(sa_group_t group)
{
	xmlNodePtr node = (xmlNodePtr)group;
	sa_handle_t handle;

	while (node != NULL) {
		if (strcmp((char *)(node->name), "sharecfg") == 0) {
			/* have the root so get the handle */
			handle = (sa_handle_t)get_handle_for_root(node);
			return (handle);
		}
		node = node->parent;
	}
	return (NULL);
}

/*
 * set_legacy_timestamp(root, path, timevalue)
 *
 * add the current timestamp value to the configuration for use in
 * determining when to update the legacy files.  For SMF, this
 * property is kept in default/operation/legacy_timestamp
 */

static void
set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
{
	xmlNodePtr node;
	xmlChar *lpath = NULL;
	sa_handle_impl_t handle;

	/* Have to have a handle or else we weren't initialized. */
	handle = get_handle_for_root(root);
	if (handle == NULL)
		return;

	for (node = root->xmlChildrenNode; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
			/* a possible legacy node for this path */
			lpath = xmlGetProp(node, (xmlChar *)"path");
			if (lpath != NULL &&
			    xmlStrcmp(lpath, (xmlChar *)path) == 0) {
				xmlFree(lpath);
				break;
			}
			if (lpath != NULL)
				xmlFree(lpath);
		}
	}
	if (node == NULL) {
		/* need to create the first legacy timestamp node */
		node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
	}
	if (node != NULL) {
		char tstring[32];
		int ret;

		(void) snprintf(tstring, sizeof (tstring), "%lld", tval);
		(void) xmlSetProp(node, (xmlChar *)"timestamp",
		    (xmlChar *)tstring);
		(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
		/* now commit to SMF */
		ret = sa_get_instance(handle->scfhandle, "default");
		if (ret == SA_OK) {
			ret = sa_start_transaction(handle->scfhandle,
			    "operation");
			if (ret == SA_OK) {
				ret = sa_set_property(handle->scfhandle,
				    "legacy-timestamp", tstring);
				if (ret == SA_OK) {
					(void) sa_end_transaction(
					    handle->scfhandle, handle);
				} else {
					sa_abort_transaction(handle->scfhandle);
				}
			}
		}
	}
}

/*
 * is_shared(share)
 *
 * determine if the specified share is currently shared or not.
 */
static int
is_shared(sa_share_t share)
{
	char *shared;
	int result = 0; /* assume not */

	shared = sa_get_share_attr(share, "shared");
	if (shared != NULL) {
		if (strcmp(shared, "true") == 0)
			result = 1;
		sa_free_attr_string(shared);
	}
	return (result);
}

/*
 * excluded_protocol(share, proto)
 *
 * Returns B_TRUE if the specified protocol appears in the "exclude"
 * property. This is used to prevent sharing special case shares
 * (e.g. subdirs when SMB wants a subdir and NFS doesn't. B_FALSE is
 * returned if the protocol isn't in the list.
 */
static boolean_t
excluded_protocol(sa_share_t share, char *proto)
{
	char *protolist;
	char *str;
	char *token;

	protolist = sa_get_share_attr(share, "exclude");
	if (protolist != NULL) {
		str = protolist;
		while ((token = strtok(str, ",")) != NULL) {
			if (strcmp(token, proto) == 0) {
				sa_free_attr_string(protolist);
				return (B_TRUE);
			}
			str = NULL;
		}
		sa_free_attr_string(protolist);
	}
	return (B_FALSE);
}

/*
 * checksubdirgroup(group, newpath, strictness)
 *
 * check all the specified newpath against all the paths in the
 * group. This is a helper function for checksubdir to make it easier
 * to also check ZFS subgroups.
 * The strictness values mean:
 * SA_CHECK_NORMAL == only check newpath against shares that are active
 * SA_CHECK_STRICT == check newpath against both active shares and those
 *		      stored in the repository
 */
static int
checksubdirgroup(sa_group_t group, char *newpath, int strictness)
{
	sa_share_t share;
	char *path;
	int issub = SA_OK;
	int subdir;
	int parent;

	if (newpath == NULL)
		return (SA_INVALID_PATH);

	for (share = sa_get_share(group, NULL); share != NULL;
	    share = sa_get_next_share(share)) {
		/*
		 * The original behavior of share never checked
		 * against the permanent configuration
		 * (/etc/dfs/dfstab).  PIT has a number of cases where
		 * it depends on this older behavior even though it
		 * could be considered incorrect.  We may tighten this
		 * up in the future.
		 */
		if (strictness == SA_CHECK_NORMAL && !is_shared(share))
			continue;

		path = sa_get_share_attr(share, "path");
		/*
		 * If path is NULL, then a share is in the process of
		 * construction or someone has modified the property
		 * group inappropriately. It should be
		 * ignored. issubdir() comes from the original share
		 * implementation and does the difficult part of
		 * checking subdirectories.
		 */
		if (path == NULL)
			continue;

		if (strcmp(path, newpath) == 0) {
			issub = SA_INVALID_PATH;
		} else {
			subdir = issubdir(newpath, path);
			parent = issubdir(path, newpath);
			if (subdir || parent) {
				sa_free_attr_string(path);
				path = NULL;
				return (subdir ?
				    SA_PATH_IS_SUBDIR : SA_PATH_IS_PARENTDIR);
			}
		}
		sa_free_attr_string(path);
		path = NULL;
	}
	return (issub);
}

/*
 * checksubdir(newpath, strictness)
 *
 * checksubdir determines if the specified path (newpath) is a
 * subdirectory of another share. It calls checksubdirgroup() to do
 * the complicated work. The strictness parameter determines how
 * strict a check to make against the path. The strictness values
 * mean: SA_CHECK_NORMAL == only check newpath against shares that are
 * active SA_CHECK_STRICT == check newpath against both active shares
 * and those * stored in the repository
 */
static int
checksubdir(sa_handle_t handle, char *newpath, int strictness)
{
	sa_group_t group;
	int issub = SA_OK;
	char *path = NULL;

	for (group = sa_get_group(handle, NULL);
	    group != NULL && issub == SA_OK;
	    group = sa_get_next_group(group)) {
		if (sa_group_is_zfs(group)) {
			sa_group_t subgroup;
			for (subgroup = sa_get_sub_group(group);
			    subgroup != NULL && issub == SA_OK;
			    subgroup = sa_get_next_group(subgroup))
				issub = checksubdirgroup(subgroup, newpath,
				    strictness);
		} else {
			issub = checksubdirgroup(group, newpath, strictness);
		}
	}
	if (path != NULL)
		sa_free_attr_string(path);
	return (issub);
}

/*
 * validpath(path, strictness)
 * determine if the provided path is valid for a share. It shouldn't
 * be a sub-dir of an already shared path or the parent directory of a
 * share path.
 */
static int
validpath(sa_handle_t handle, char *path, int strictness)
{
	int error = SA_OK;
	struct stat st;
	sa_share_t share;
	char *fstype;

	if (*path != '/')
		return (SA_BAD_PATH);

	if (stat(path, &st) < 0) {
		error = SA_NO_SUCH_PATH;
	} else {
		share = sa_find_share(handle, path);
		if (share != NULL)
			error = SA_DUPLICATE_NAME;

		if (error == SA_OK) {
			/*
			 * check for special case with file system
			 * that might have restrictions.  For now, ZFS
			 * is the only case since it has its own idea
			 * of how to configure shares. We do this
			 * before subdir checking since things like
			 * ZFS will do that for us. This should also
			 * be done via plugin interface.
			 */
			fstype = sa_fstype(path);
			if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
				if (sa_zfs_is_shared(handle, path))
					error = SA_INVALID_NAME;
			}
			if (fstype != NULL)
				sa_free_fstype(fstype);
		}
		if (error == SA_OK)
			error = checksubdir(handle, path, strictness);
	}
	return (error);
}

/*
 * check to see if group/share is persistent.
 *
 * "group" can be either an sa_group_t or an sa_share_t. (void *)
 * works since both thse types are also void *.
 * If the share is a ZFS share, mark it as persistent.
 */
int
sa_is_persistent(void *group)
{
	char *type;
	int persist = 1;
	sa_group_t grp;

	type = sa_get_group_attr((sa_group_t)group, "type");
	if (type != NULL) {
		if (strcmp(type, "transient") == 0)
			persist = 0;
		sa_free_attr_string(type);
	}

	grp = (sa_is_share(group)) ? sa_get_parent_group(group) : group;
	if (sa_group_is_zfs(grp))
		persist = 1;

	return (persist);
}

/*
 * sa_valid_group_name(name)
 *
 * check that the "name" contains only valid characters and otherwise
 * fits the required naming conventions. Valid names must start with
 * an alphabetic and the remainder may consist of only alphanumeric
 * plus the '-' and '_' characters. This name limitation comes from
 * inherent limitations in SMF.
 */

int
sa_valid_group_name(char *name)
{
	int ret = 1;
	ssize_t len;

	if (name != NULL && isalpha(*name)) {
		char c;
		len = strlen(name);
		if (len < (scf_max_name_len - sizeof ("group:"))) {
			for (c = *name++; c != '\0' && ret != 0; c = *name++) {
				if (!isalnum(c) && c != '-' && c != '_')
					ret = 0;
			}
		} else {
			ret = 0;
		}
	} else {
		ret = 0;
	}
	return (ret);
}


/*
 * is_zfs_group(group)
 *	Determine if the specified group is a ZFS sharenfs group
 */
static int
is_zfs_group(sa_group_t group)
{
	int ret = 0;
	xmlNodePtr parent;
	xmlChar *zfs;

	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0)
		parent = (xmlNodePtr)sa_get_parent_group(group);
	else
		parent = (xmlNodePtr)group;
	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
	if (zfs != NULL) {
		xmlFree(zfs);
		ret = 1;
	}
	return (ret);
}

/*
 * sa_get_object_type(object)
 *
 * This function returns a numeric value representing the object
 * type. This allows using simpler checks when doing type specific
 * operations.
 */

static int
sa_get_object_type(void *object)
{
	xmlNodePtr node = (xmlNodePtr)object;
	int type;

	if (xmlStrcmp(node->name, (xmlChar *)"group") == 0)
		type = SA_TYPE_GROUP;
	else if (xmlStrcmp(node->name, (xmlChar *)"share") == 0)
		type = SA_TYPE_SHARE;
	else if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
		type = SA_TYPE_RESOURCE;
	else if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0)
		type = SA_TYPE_OPTIONSET;
	else if (xmlStrcmp(node->name, (xmlChar *)"security") == 0)
		type = SA_TYPE_ALTSPACE;
	else
		assert(0);
	return (type);
}

/*
 * sa_optionset_name(optionset, oname, len, id)
 *	return the SMF name for the optionset. If id is not NULL, it
 *	will have the GUID value for a share and should be used
 *	instead of the keyword "optionset" which is used for
 *	groups. If the optionset doesn't have a protocol type
 *	associated with it, "default" is used. This shouldn't happen
 *	at this point but may be desirable in the future if there are
 *	protocol independent properties added. The name is returned in
 *	oname.
 */

static int
sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
{
	char *proto;
	void *parent;
	int ptype;

	if (id == NULL)
		id = "optionset";

	parent = sa_get_optionset_parent(optionset);
	if (parent != NULL) {
		ptype = sa_get_object_type(parent);
		proto = sa_get_optionset_attr(optionset, "type");
		if (ptype != SA_TYPE_RESOURCE) {
			len = snprintf(oname, len, "%s_%s", id,
			    proto ? proto : "default");
		} else {
			char *index;
			index = get_node_attr((void *)parent, "id");
			if (index != NULL) {
				len = snprintf(oname, len, "%s_%s_%s", id,
				    proto ? proto : "default", index);
				sa_free_attr_string(index);
			} else {
				len = 0;
			}
		}

		if (proto != NULL)
			sa_free_attr_string(proto);
	} else {
		len = 0;
	}
	return (len);
}

/*
 * sa_security_name(optionset, oname, len, id)
 *
 * return the SMF name for the security. If id is not NULL, it will
 * have the GUID value for a share and should be used instead of the
 * keyword "optionset" which is used for groups. If the optionset
 * doesn't have a protocol type associated with it, "default" is
 * used. This shouldn't happen at this point but may be desirable in
 * the future if there are protocol independent properties added. The
 * name is returned in oname. The security type is also encoded into
 * the name. In the future, this wil *be handled a bit differently.
 */

static int
sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
{
	char *proto;
	char *sectype;

	if (id == NULL)
		id = "optionset";

	proto = sa_get_security_attr(security, "type");
	sectype = sa_get_security_attr(security, "sectype");
	len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default",
	    sectype ? sectype : "default");
	if (proto != NULL)
		sa_free_attr_string(proto);
	if (sectype != NULL)
		sa_free_attr_string(sectype);
	return (len);
}

/*
 * verifydefgroupopts(handle)
 *
 * Make sure a "default" group exists and has default protocols enabled.
 */
static void
verifydefgroupopts(sa_handle_t handle)
{
	sa_group_t defgrp;
	sa_optionset_t opt;

	defgrp = sa_get_group(handle, "default");
	if (defgrp != NULL) {
		opt = sa_get_optionset(defgrp, NULL);
		/*
		 * NFS is the default for default group
		 */
		if (opt == NULL)
			opt = sa_create_optionset(defgrp, "nfs");
	}
}

/*
 * sa_init(init_service)
 *	Initialize the API
 *	find all the shared objects
 *	init the tables with all objects
 *	read in the current configuration
 */

#define	GETPROP(prop)	scf_simple_prop_next_astring(prop)
#define	CHECKTSTAMP(st, tval)	stat(SA_LEGACY_DFSTAB, &st) >= 0 && \
	tval != TSTAMP(st.st_ctim)

sa_handle_t
sa_init(int init_service)
{
	struct stat st;
	int legacy = 0;
	uint64_t tval = 0;
	int lockfd;
	sigset_t old;
	int updatelegacy = B_FALSE;
	scf_simple_prop_t *prop;
	sa_handle_impl_t handle;
	int err;

	handle = calloc(sizeof (struct sa_handle_impl), 1);

	if (handle != NULL) {
		/*
		 * Get protocol specific structures, but only if this
		 * is the only handle.
		 */
		(void) mutex_lock(&sa_global_lock);
		if (sa_global_handles == NULL)
			(void) proto_plugin_init();
		(void) mutex_unlock(&sa_global_lock);
		if (init_service & SA_INIT_SHARE_API) {
			/*
			 * initialize access into libzfs. We use this
			 * when collecting info about ZFS datasets and
			 * shares.
			 */
			if (sa_zfs_init(handle) == B_FALSE) {
				free(handle);
				(void) mutex_lock(&sa_global_lock);
				(void) proto_plugin_fini();
				(void) mutex_unlock(&sa_global_lock);
				return (NULL);
			}
			/*
			 * since we want to use SMF, initialize an svc handle
			 * and find out what is there.
			 */
			handle->scfhandle = sa_scf_init(handle);
			if (handle->scfhandle != NULL) {
				/*
				 * Need to lock the extraction of the
				 * configuration if the dfstab file has
				 * changed. Lock everything now and release if
				 * not needed.  Use a file that isn't being
				 * manipulated by other parts of the system in
				 * order to not interfere with locking. Using
				 * dfstab doesn't work.
				 */
				sablocksigs(&old);
				lockfd = open(DFS_LOCK_FILE, O_RDWR);
				if (lockfd >= 0) {
					extern int errno;
					errno = 0;
					(void) lockf(lockfd, F_LOCK, 0);
					(void) mutex_lock(&sa_dfstab_lock);
					/*
					 * Check whether we are going to need
					 * to merge any dfstab changes. This
					 * is done by comparing the value of
					 * legacy-timestamp with the current
					 * st_ctim of the file. If they are
					 * different, an update is needed and
					 * the file must remain locked until
					 * the merge is done in order to
					 * prevent multiple startups from
					 * changing the SMF repository at the
					 * same time.  The first to get the
					 * lock will make any changes before
					 * the others can read the repository.
					 */
					prop = scf_simple_prop_get
					    (handle->scfhandle->handle,
					    (const char *)SA_SVC_FMRI_BASE
					    ":default", "operation",
					    "legacy-timestamp");
					if (prop != NULL) {
						char *i64;
						i64 = GETPROP(prop);
						if (i64 != NULL)
							tval = strtoull(i64,
							    NULL, 0);
						if (CHECKTSTAMP(st, tval))
							updatelegacy = B_TRUE;
						scf_simple_prop_free(prop);
					} else {
						/*
						 * We haven't set the
						 * timestamp before so do it.
						 */
						updatelegacy = B_TRUE;
					}
					if (updatelegacy == B_FALSE) {
						(void) mutex_unlock(
						    &sa_dfstab_lock);
						(void) lockf(lockfd, F_ULOCK,
						    0);
						(void) close(lockfd);
					}

				}
				/*
				 * It is essential that the document tree and
				 * the internal list of roots to handles be
				 * setup before anything that might try to
				 * create a new object is called. The document
				 * tree is the combination of handle->doc and
				 * handle->tree. This allows searches,
				 * etc. when all you have is an object in the
				 * tree.
				 */
				handle->doc = xmlNewDoc((xmlChar *)"1.0");
				handle->tree = xmlNewNode(NULL,
				    (xmlChar *)"sharecfg");
				if (handle->doc != NULL &&
				    handle->tree != NULL) {
					(void) xmlDocSetRootElement(handle->doc,
					    handle->tree);
					err = add_handle_for_root(handle->tree,
					    handle);
					if (err == SA_OK)
						err = sa_get_config(
						    handle->scfhandle,
						    handle->tree, handle);
				} else {
					if (handle->doc != NULL)
						xmlFreeDoc(handle->doc);
					if (handle->tree != NULL)
						xmlFreeNode(handle->tree);
					err = SA_NO_MEMORY;
				}

				saunblocksigs(&old);

				if (err != SA_OK) {
					/*
					 * If we couldn't add the tree handle
					 * to the list, then things are going
					 * to fail badly. Might as well undo
					 * everything now and fail the
					 * sa_init().
					 */
					sa_fini(handle);
					if (updatelegacy == B_TRUE) {
						(void) mutex_unlock(
						    &sa_dfstab_lock);
						(void) lockf(lockfd,
						    F_ULOCK, 0);
						(void) close(lockfd);
					}
					return (NULL);
				}

				if (tval == 0) {
					/*
					 * first time so make sure
					 * default is setup
					 */
					verifydefgroupopts(handle);
				}

				if (updatelegacy == B_TRUE) {
					sablocksigs(&old);
					getlegacyconfig((sa_handle_t)handle,
					    SA_LEGACY_DFSTAB, &handle->tree);
					if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
						set_legacy_timestamp(
						    handle->tree,
						    SA_LEGACY_DFSTAB,
						    TSTAMP(st.st_ctim));
					saunblocksigs(&old);
					/*
					 * Safe to unlock now to allow
					 * others to run
					 */
					(void) mutex_unlock(&sa_dfstab_lock);
					(void) lockf(lockfd, F_ULOCK, 0);
					(void) close(lockfd);
				}
				/* Get sharetab timestamp */
				sa_update_sharetab_ts((sa_handle_t)handle);

				/* Get lastupdate (transaction) timestamp */
				prop = scf_simple_prop_get(
				    handle->scfhandle->handle,
				    (const char *)SA_SVC_FMRI_BASE ":default",
				    "state", "lastupdate");
				if (prop != NULL) {
					char *str;
					str =
					    scf_simple_prop_next_astring(prop);
					if (str != NULL)
						handle->tstrans =
						    strtoull(str, NULL, 0);
					else
						handle->tstrans = 0;
					scf_simple_prop_free(prop);
				}
				legacy |= sa_get_zfs_shares(handle, "zfs");
				legacy |= gettransients(handle, &handle->tree);
			}
		}
	}
	return ((sa_handle_t)handle);
}

/*
 * sa_fini(handle)
 *	Uninitialize the API structures including the configuration
 *	data structures and ZFS related data.
 */

void
sa_fini(sa_handle_t handle)
{
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	if (impl_handle != NULL) {
		/*
		 * Free the config trees and any other data structures
		 * used in the handle.
		 */
		if (impl_handle->doc != NULL)
			xmlFreeDoc(impl_handle->doc);

		/* Remove and free the entry in the global list. */
		remove_handle_for_root(impl_handle->tree);

		/*
		 * If this was the last handle to release, unload the
		 * plugins that were loaded. Use a mutex in case
		 * another thread is reinitializing.
		 */
		(void) mutex_lock(&sa_global_lock);
		if (sa_global_handles == NULL)
			(void) proto_plugin_fini();
		(void) mutex_unlock(&sa_global_lock);

		sa_scf_fini(impl_handle->scfhandle);
		sa_zfs_fini(impl_handle);

		/* Make sure we free the handle */
		free(impl_handle);

	}
}

/*
 * sa_get_protocols(char **protocol)
 *	Get array of protocols that are supported
 *	Returns pointer to an allocated and NULL terminated
 *	array of strings.  Caller must free.
 *	This really should be determined dynamically.
 *	If there aren't any defined, return -1.
 *	Use free() to return memory.
 */

int
sa_get_protocols(char ***protocols)
{
	int numproto = -1;

	if (protocols != NULL) {
		struct sa_proto_plugin *plug;
		for (numproto = 0, plug = sap_proto_list; plug != NULL;
		    plug = plug->plugin_next) {
			numproto++;
		}

		*protocols = calloc(numproto + 1,  sizeof (char *));
		if (*protocols != NULL) {
			int ret = 0;
			for (plug = sap_proto_list; plug != NULL;
			    plug = plug->plugin_next) {
				/* faking for now */
				(*protocols)[ret++] =
				    plug->plugin_ops->sa_protocol;
			}
		} else {
			numproto = -1;
		}
	}
	return (numproto);
}

/*
 * find_group_by_name(node, group)
 *
 * search the XML document subtree specified by node to find the group
 * specified by group. Searching subtree allows subgroups to be
 * searched for.
 */

static xmlNodePtr
find_group_by_name(xmlNodePtr node, xmlChar *group)
{
	xmlChar *name = NULL;

	for (node = node->xmlChildrenNode; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
			/* if no groupname, return the first found */
			if (group == NULL)
				break;
			name = xmlGetProp(node, (xmlChar *)"name");
			if (name != NULL && xmlStrcmp(name, group) == 0)
				break;
			if (name != NULL) {
				xmlFree(name);
				name = NULL;
			}
		}
	}
	if (name != NULL)
		xmlFree(name);
	return (node);
}

/*
 * sa_get_group(groupname)
 *	Return the "group" specified.  If groupname is NULL,
 *	return the first group of the list of groups.
 */
sa_group_t
sa_get_group(sa_handle_t handle, char *groupname)
{
	xmlNodePtr node = NULL;
	char *subgroup = NULL;
	char *group = NULL;
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	if (impl_handle != NULL && impl_handle->tree != NULL) {
		if (groupname != NULL) {
			group = strdup(groupname);
			if (group != NULL) {
				subgroup = strchr(group, '/');
				if (subgroup != NULL)
					*subgroup++ = '\0';
			}
		}
		/*
		 * We want to find the, possibly, named group. If
		 * group is not NULL, then lookup the name. If it is
		 * NULL, we only do the find if groupname is also
		 * NULL. This allows lookup of the "first" group in
		 * the internal list.
		 */
		if (group != NULL || groupname == NULL)
			node = find_group_by_name(impl_handle->tree,
			    (xmlChar *)group);

		/* if a subgroup, find it before returning */
		if (subgroup != NULL && node != NULL)
			node = find_group_by_name(node, (xmlChar *)subgroup);
	}
	if (node != NULL && (char *)group != NULL)
		(void) sa_get_instance(impl_handle->scfhandle, (char *)group);
	if (group != NULL)
		free(group);
	return ((sa_group_t)(node));
}

/*
 * sa_get_next_group(group)
 *	Return the "next" group after the specified group from
 *	the internal group list.  NULL if there are no more.
 */
sa_group_t
sa_get_next_group(sa_group_t group)
{
	xmlNodePtr ngroup = NULL;
	if (group != NULL) {
		for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
		    ngroup = ngroup->next) {
			if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
				break;
		}
	}
	return ((sa_group_t)ngroup);
}

/*
 * sa_get_share(group, sharepath)
 *	Return the share object for the share specified. The share
 *	must be in the specified group.  Return NULL if not found.
 */
sa_share_t
sa_get_share(sa_group_t group, char *sharepath)
{
	xmlNodePtr node = NULL;
	xmlChar *path;

	/*
	 * For future scalability, this should end up building a cache
	 * since it will get called regularly by the mountd and info
	 * services.
	 */
	if (group != NULL) {
		for (node = ((xmlNodePtr)group)->children; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
				if (sharepath == NULL) {
					break;
				} else {
					/* is it the correct share? */
					path = xmlGetProp(node,
					    (xmlChar *)"path");
					if (path != NULL &&
					    xmlStrcmp(path,
					    (xmlChar *)sharepath) == 0) {
						xmlFree(path);
						break;
					}
					xmlFree(path);
				}
			}
		}
	}
	return ((sa_share_t)node);
}

/*
 * sa_get_next_share(share)
 *	Return the next share following the specified share
 *	from the internal list of shares. Returns NULL if there
 *	are no more shares.  The list is relative to the same
 *	group.
 */
sa_share_t
sa_get_next_share(sa_share_t share)
{
	xmlNodePtr node = NULL;

	if (share != NULL) {
		for (node = ((xmlNodePtr)share)->next; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
				break;
			}
		}
	}
	return ((sa_share_t)node);
}

/*
 * _sa_get_child_node(node, type)
 *
 * find the child node of the specified node that has "type". This is
 * used to implement several internal functions.
 */

static xmlNodePtr
_sa_get_child_node(xmlNodePtr node, xmlChar *type)
{
	xmlNodePtr child;
	for (child = node->xmlChildrenNode; child != NULL;
	    child = child->next)
		if (xmlStrcmp(child->name, type) == 0)
			return (child);
	return ((xmlNodePtr)NULL);
}

/*
 *  find_share(group, path)
 *
 * Search all the shares in the specified group for one that has the
 * specified path.
 */

static sa_share_t
find_share(sa_group_t group, char *sharepath)
{
	sa_share_t share;
	char *path;

	for (share = sa_get_share(group, NULL); share != NULL;
	    share = sa_get_next_share(share)) {
		path = sa_get_share_attr(share, "path");
		if (path != NULL && strcmp(path, sharepath) == 0) {
			sa_free_attr_string(path);
			break;
		}
		if (path != NULL)
			sa_free_attr_string(path);
	}
	return (share);
}

/*
 * sa_get_sub_group(group)
 *
 * Get the first sub-group of group. The sa_get_next_group() function
 * can be used to get the rest. This is currently only used for ZFS
 * sub-groups but could be used to implement a more general mechanism.
 */

sa_group_t
sa_get_sub_group(sa_group_t group)
{
	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
	    (xmlChar *)"group"));
}

/*
 * sa_find_share(sharepath)
 *	Finds a share regardless of group.  In the future, this
 *	function should utilize a cache and hash table of some kind.
 *	The current assumption is that a path will only be shared
 *	once.  In the future, this may change as implementation of
 *	resource names comes into being.
 */
sa_share_t
sa_find_share(sa_handle_t handle, char *sharepath)
{
	sa_group_t group;
	sa_group_t zgroup;
	sa_share_t share = NULL;
	int done = 0;

	for (group = sa_get_group(handle, NULL); group != NULL && !done;
	    group = sa_get_next_group(group)) {
		if (is_zfs_group(group)) {
			for (zgroup =
			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
			    (xmlChar *)"group");
			    zgroup != NULL;
			    zgroup = sa_get_next_group(zgroup)) {
				share = find_share(zgroup, sharepath);
				if (share != NULL)
					break;
			}
		} else {
			share = find_share(group, sharepath);
		}
		if (share != NULL)
			break;
	}
	return (share);
}

/*
 *  sa_check_path(group, path, strictness)
 *
 * Check that path is a valid path relative to the group.  Currently,
 * we are ignoring the group and checking only the NFS rules. Later,
 * we may want to use the group to then check against the protocols
 * enabled on the group. The strictness values mean:
 * SA_CHECK_NORMAL == only check newpath against shares that are active
 * SA_CHECK_STRICT == check newpath against both active shares and those
 *		      stored in the repository
 */

int
sa_check_path(sa_group_t group, char *path, int strictness)
{
	sa_handle_t handle;

	handle = sa_find_group_handle(group);
	if (handle == NULL)
		return (SA_BAD_PATH);

	return (validpath(handle, path, strictness));
}

/*
 * mark_excluded_protos(group, share, flags)
 *
 * Walk through all the protocols enabled for the group and check to
 * see if the share has any of them should be in the exclude list
 * based on the featureset of the protocol. If there are any, add the
 * "exclude" property to the share.
 */
static void
mark_excluded_protos(sa_group_t group, xmlNodePtr share, uint64_t flags)
{
	sa_optionset_t optionset;
	char exclude_list[SA_STRSIZE];
	char *sep = "";

	exclude_list[0] = '\0';
	for (optionset = sa_get_optionset(group, NULL);
	    optionset != NULL;
	    optionset = sa_get_next_optionset(optionset)) {
		char *value;
		uint64_t features;
		value = sa_get_optionset_attr(optionset, "type");
		if (value == NULL)
			continue;
		features = sa_proto_get_featureset(value);
		if (!(features & flags)) {
			(void) strlcat(exclude_list, sep,
			    sizeof (exclude_list));
			(void) strlcat(exclude_list, value,
			    sizeof (exclude_list));
			sep = ",";
		}
		sa_free_attr_string(value);
	}
	if (exclude_list[0] != '\0')
		(void) xmlSetProp(share, (xmlChar *)"exclude",
		    (xmlChar *)exclude_list);
}

/*
 * get_all_features(group)
 *
 * Walk through all the protocols on the group and collect all
 * possible enabled features. This is the OR of all the featuresets.
 */
static uint64_t
get_all_features(sa_group_t group)
{
	sa_optionset_t optionset;
	uint64_t features = 0;

	for (optionset = sa_get_optionset(group, NULL);
	    optionset != NULL;
	    optionset = sa_get_next_optionset(optionset)) {
		char *value;
		value = sa_get_optionset_attr(optionset, "type");
		if (value == NULL)
			continue;
		features |= sa_proto_get_featureset(value);
		sa_free_attr_string(value);
	}
	return (features);
}


/*
 * _sa_add_share(group, sharepath, persist, *error, flags)
 *
 * Common code for all types of add_share. sa_add_share() is the
 * public API, we also need to be able to do this when parsing legacy
 * files and construction of the internal configuration while
 * extracting config info from SMF. "flags" indicates if some
 * protocols need relaxed rules while other don't. These values are
 * the featureset values defined in libshare.h.
 */

sa_share_t
_sa_add_share(sa_group_t group, char *sharepath, int persist, int *error,
    uint64_t flags)
{
	xmlNodePtr node = NULL;
	int err;

	err  = SA_OK; /* assume success */

	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL);
	if (node == NULL) {
		if (error != NULL)
			*error = SA_NO_MEMORY;
		return (node);
	}

	(void) xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
	(void) xmlSetProp(node, (xmlChar *)"type",
	    persist ? (xmlChar *)"persist" : (xmlChar *)"transient");
	if (flags != 0)
		mark_excluded_protos(group, node, flags);
	if (persist != SA_SHARE_TRANSIENT) {
		/*
		 * persistent shares come in two flavors: SMF and
		 * ZFS. Sort this one out based on target group and
		 * path type. Both NFS and SMB are supported. First,
		 * check to see if the protocol is enabled on the
		 * subgroup and then setup the share appropriately.
		 */
		if (sa_group_is_zfs(group) &&
		    sa_path_is_zfs(sharepath)) {
			if (sa_get_optionset(group, "nfs") != NULL)
				err = sa_zfs_set_sharenfs(group, sharepath, 1);
			else if (sa_get_optionset(group, "smb") != NULL)
				err = sa_zfs_set_sharesmb(group, sharepath, 1);
		} else {
			sa_handle_impl_t impl_handle;
			impl_handle =
			    (sa_handle_impl_t)sa_find_group_handle(group);
			if (impl_handle != NULL) {
				err = sa_commit_share(impl_handle->scfhandle,
				    group, (sa_share_t)node);
			} else {
				err = SA_SYSTEM_ERR;
			}
		}
	}
	if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER)
		/* called by the dfstab parser so could be a show */
		err = SA_OK;

	if (err != SA_OK) {
		/*
		 * we couldn't commit to the repository so undo
		 * our internal state to reflect reality.
		 */
		xmlUnlinkNode(node);
		xmlFreeNode(node);
		node = NULL;
	}

	if (error != NULL)
		*error = err;

	return (node);
}

/*
 * sa_add_share(group, sharepath, persist, *error)
 *
 *	Add a new share object to the specified group.  The share will
 *	have the specified sharepath and will only be constructed if
 *	it is a valid path to be shared.  NULL is returned on error
 *	and a detailed error value will be returned via the error
 *	pointer.
 */
sa_share_t
sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
{
	xmlNodePtr node = NULL;
	int strictness = SA_CHECK_NORMAL;
	sa_handle_t handle;
	uint64_t special = 0;
	uint64_t features;

	/*
	 * If the share is to be permanent, use strict checking so a
	 * bad config doesn't get created. Transient shares only need
	 * to check against the currently active
	 * shares. SA_SHARE_PARSER is a modifier used internally to
	 * indicate that we are being called by the dfstab parser and
	 * that we need strict checking in all cases. Normally persist
	 * is in integer value but SA_SHARE_PARSER may be or'd into
	 * it as an override.
	 */
	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
		strictness = SA_CHECK_STRICT;

	handle = sa_find_group_handle(group);

	/*
	 * need to determine if the share is valid. The rules are:
	 *	- The path must not already exist
	 *	- The path must not be a subdir or parent dir of an
	 *	  existing path unless at least one protocol allows it.
	 * The sub/parent check is done in sa_check_path().
	 */

	if (sa_find_share(handle, sharepath) == NULL) {
		*error = sa_check_path(group, sharepath, strictness);
		features = get_all_features(group);
		switch (*error) {
		case SA_PATH_IS_SUBDIR:
			if (features & SA_FEATURE_ALLOWSUBDIRS)
				special |= SA_FEATURE_ALLOWSUBDIRS;
			break;
		case SA_PATH_IS_PARENTDIR:
			if (features & SA_FEATURE_ALLOWPARDIRS)
				special |= SA_FEATURE_ALLOWPARDIRS;
			break;
		}
		if (*error == SA_OK || special != SA_FEATURE_NONE)
			node = _sa_add_share(group, sharepath, persist,
			    error, special);
	} else {
		*error = SA_DUPLICATE_NAME;
	}

	return ((sa_share_t)node);
}

/*
 * sa_enable_share(share, protocol)
 *	Enable the specified share to the specified protocol.
 *	If protocol is NULL, then all protocols.
 */
int
sa_enable_share(sa_share_t share, char *protocol)
{
	char *sharepath;
	struct stat st;
	int err = SA_OK;
	int ret;

	sharepath = sa_get_share_attr(share, "path");
	if (sharepath == NULL)
		return (SA_NO_MEMORY);
	if (stat(sharepath, &st) < 0) {
		err = SA_NO_SUCH_PATH;
	} else {
		/* tell the server about the share */
		if (protocol != NULL) {
			if (excluded_protocol(share, protocol))
				goto done;

			/* lookup protocol specific handler */
			err = sa_proto_share(protocol, share);
			if (err == SA_OK)
				(void) sa_set_share_attr(share,
				    "shared", "true");
		} else {
			/* Tell all protocols about the share */
			sa_group_t group;
			sa_optionset_t optionset;

			group = sa_get_parent_group(share);

			for (optionset = sa_get_optionset(group, NULL);
			    optionset != NULL;
			    optionset = sa_get_next_optionset(optionset)) {
				char *proto;
				proto = sa_get_optionset_attr(optionset,
				    "type");
				if (proto != NULL) {
					if (!excluded_protocol(share, proto)) {
						ret = sa_proto_share(proto,
						    share);
						if (ret != SA_OK)
							err = ret;
					}
					sa_free_attr_string(proto);
				}
			}
			(void) sa_set_share_attr(share, "shared", "true");
		}
	}
done:
	if (sharepath != NULL)
		sa_free_attr_string(sharepath);
	return (err);
}

/*
 * sa_disable_share(share, protocol)
 *	Disable the specified share to the specified protocol.  If
 *	protocol is NULL, then all protocols that are enabled for the
 *	share should be disabled.
 */
int
sa_disable_share(sa_share_t share, char *protocol)
{
	char *path;
	int err = SA_OK;
	int ret = SA_OK;

	path = sa_get_share_attr(share, "path");

	if (protocol != NULL) {
		ret = sa_proto_unshare(share, protocol, path);
	} else {
		/* need to do all protocols */
		sa_group_t group;
		sa_optionset_t optionset;

		group = sa_get_parent_group(share);

		/* Tell all protocols about the share */
		for (optionset = sa_get_optionset(group, NULL);
		    optionset != NULL;
		    optionset = sa_get_next_optionset(optionset)) {
			char *proto;

			proto = sa_get_optionset_attr(optionset, "type");
			if (proto != NULL) {
				err = sa_proto_unshare(share, proto, path);
				if (err != SA_OK)
					ret = err;
				sa_free_attr_string(proto);
			}
		}
	}
	if (ret == SA_OK)
		(void) sa_set_share_attr(share, "shared", NULL);
	if (path != NULL)
		sa_free_attr_string(path);
	return (ret);
}

/*
 * sa_remove_share(share)
 *
 * remove the specified share from its containing group.
 * Remove from the SMF or ZFS configuration space.
 */

int
sa_remove_share(sa_share_t share)
{
	sa_group_t group;
	int ret = SA_OK;
	char *type;
	int transient = 0;
	char *groupname;
	char *zfs;

	type = sa_get_share_attr(share, "type");
	group = sa_get_parent_group(share);
	zfs = sa_get_group_attr(group, "zfs");
	groupname = sa_get_group_attr(group, "name");
	if (type != NULL && strcmp(type, "persist") != 0)
		transient = 1;
	if (type != NULL)
		sa_free_attr_string(type);

	/* remove the node from its group then free the memory */

	/*
	 * need to test if "busy"
	 */
	/* only do SMF action if permanent */
	if (!transient || zfs != NULL) {
		/* remove from legacy dfstab as well as possible SMF */
		ret = sa_delete_legacy(share, NULL);
		if (ret == SA_OK) {
			if (!sa_group_is_zfs(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle = (sa_handle_impl_t)
				    sa_find_group_handle(group);
				if (impl_handle != NULL) {
					ret = sa_delete_share(
					    impl_handle->scfhandle, group,
					    share);
				} else {
					ret = SA_SYSTEM_ERR;
				}
			} else {
				char *sharepath = sa_get_share_attr(share,
				    "path");
				if (sharepath != NULL) {
					ret = sa_zfs_set_sharenfs(group,
					    sharepath, 0);
					sa_free_attr_string(sharepath);
				}
			}
		}
	}
	if (groupname != NULL)
		sa_free_attr_string(groupname);
	if (zfs != NULL)
		sa_free_attr_string(zfs);

	xmlUnlinkNode((xmlNodePtr)share);
	xmlFreeNode((xmlNodePtr)share);
	return (ret);
}

/*
 * sa_move_share(group, share)
 *
 * move the specified share to the specified group.  Update SMF
 * appropriately.
 */

int
sa_move_share(sa_group_t group, sa_share_t share)
{
	sa_group_t oldgroup;
	int ret = SA_OK;

	/* remove the node from its group then free the memory */

	oldgroup = sa_get_parent_group(share);
	if (oldgroup != group) {
		sa_handle_impl_t impl_handle;
		xmlUnlinkNode((xmlNodePtr)share);
		/*
		 * now that the share isn't in its old group, add to
		 * the new one
		 */
		(void) xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
		/* need to deal with SMF */
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			/*
			 * need to remove from old group first and then add to
			 * new group. Ideally, we would do the other order but
			 * need to avoid having the share in two groups at the
			 * same time.
			 */
			ret = sa_delete_share(impl_handle->scfhandle, oldgroup,
			    share);
			if (ret == SA_OK)
				ret = sa_commit_share(impl_handle->scfhandle,
				    group, share);
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * sa_get_parent_group(share)
 *
 * Return the containing group for the share. If a group was actually
 * passed in, we don't want a parent so return NULL.
 */

sa_group_t
sa_get_parent_group(sa_share_t share)
{
	xmlNodePtr node = NULL;
	if (share != NULL) {
		node = ((xmlNodePtr)share)->parent;
		/*
		 * make sure parent is a group and not sharecfg since
		 * we may be cheating and passing in a group.
		 * Eventually, groups of groups might come into being.
		 */
		if (node == NULL ||
		    xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
			node = NULL;
	}
	return ((sa_group_t)node);
}

/*
 * _sa_create_group(impl_handle, groupname)
 *
 * Create a group in the document. The caller will need to deal with
 * configuration store and activation.
 */

sa_group_t
_sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
{
	xmlNodePtr node = NULL;

	if (sa_valid_group_name(groupname)) {
		node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group",
		    NULL);
		if (node != NULL) {
			(void) xmlSetProp(node, (xmlChar *)"name",
			    (xmlChar *)groupname);
			(void) xmlSetProp(node, (xmlChar *)"state",
			    (xmlChar *)"enabled");
		}
	}
	return ((sa_group_t)node);
}

/*
 * _sa_create_zfs_group(group, groupname)
 *
 * Create a ZFS subgroup under the specified group. This may
 * eventually form the basis of general sub-groups, but is currently
 * restricted to ZFS.
 */
sa_group_t
_sa_create_zfs_group(sa_group_t group, char *groupname)
{
	xmlNodePtr node = NULL;

	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL);
	if (node != NULL) {
		(void) xmlSetProp(node, (xmlChar *)"name",
		    (xmlChar *)groupname);
		(void) xmlSetProp(node, (xmlChar *)"state",
		    (xmlChar *)"enabled");
	}

	return ((sa_group_t)node);
}

/*
 * sa_create_group(groupname, *error)
 *
 * Create a new group with groupname.  Need to validate that it is a
 * legal name for SMF and the construct the SMF service instance of
 * svc:/network/shares/group to implement the group. All necessary
 * operational properties must be added to the group at this point
 * (via the SMF transaction model).
 */
sa_group_t
sa_create_group(sa_handle_t handle, char *groupname, int *error)
{
	xmlNodePtr node = NULL;
	sa_group_t group;
	int ret;
	char rbacstr[SA_STRSIZE];
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	ret = SA_OK;

	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
		ret = SA_SYSTEM_ERR;
		goto err;
	}

	group = sa_get_group(handle, groupname);
	if (group != NULL) {
		ret = SA_DUPLICATE_NAME;
	} else {
		if (sa_valid_group_name(groupname)) {
			node = xmlNewChild(impl_handle->tree, NULL,
			    (xmlChar *)"group", NULL);
			if (node != NULL) {
				(void) xmlSetProp(node, (xmlChar *)"name",
				    (xmlChar *)groupname);
				/* default to the group being enabled */
				(void) xmlSetProp(node, (xmlChar *)"state",
				    (xmlChar *)"enabled");
				ret = sa_create_instance(impl_handle->scfhandle,
				    groupname);
				if (ret == SA_OK) {
					ret = sa_start_transaction(
					    impl_handle->scfhandle,
					    "operation");
				}
				if (ret == SA_OK) {
					ret = sa_set_property(
					    impl_handle->scfhandle,
					    "state", "enabled");
					if (ret == SA_OK) {
						ret = sa_end_transaction(
						    impl_handle->scfhandle,
						    impl_handle);
					} else {
						sa_abort_transaction(
						    impl_handle->scfhandle);
					}
				}
				if (ret == SA_OK) {
					/* initialize the RBAC strings */
					ret = sa_start_transaction(
					    impl_handle->scfhandle,
					    "general");
					if (ret == SA_OK) {
						(void) snprintf(rbacstr,
						    sizeof (rbacstr), "%s.%s",
						    SA_RBAC_MANAGE, groupname);
						ret = sa_set_property(
						    impl_handle->scfhandle,
						    "action_authorization",
						    rbacstr);
					}
					if (ret == SA_OK) {
						(void) snprintf(rbacstr,
						    sizeof (rbacstr), "%s.%s",
						    SA_RBAC_VALUE, groupname);
						ret = sa_set_property(
						    impl_handle->scfhandle,
						    "value_authorization",
						    rbacstr);
					}
					if (ret == SA_OK) {
						ret = sa_end_transaction(
						    impl_handle->scfhandle,
						    impl_handle);
					} else {
						sa_abort_transaction(
						    impl_handle->scfhandle);
					}
				}
				if (ret != SA_OK) {
					/*
					 * Couldn't commit the group
					 * so we need to undo
					 * internally.
					 */
					xmlUnlinkNode(node);
					xmlFreeNode(node);
					node = NULL;
				}
			} else {
				ret = SA_NO_MEMORY;
			}
		} else {
			ret = SA_INVALID_NAME;
		}
	}
err:
	if (error != NULL)
		*error = ret;
	return ((sa_group_t)node);
}

/*
 * sa_remove_group(group)
 *
 * Remove the specified group. This deletes from the SMF repository.
 * All property groups and properties are removed.
 */

int
sa_remove_group(sa_group_t group)
{
	char *name;
	int ret = SA_OK;
	sa_handle_impl_t impl_handle;

	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle != NULL) {
		name = sa_get_group_attr(group, "name");
		if (name != NULL) {
			ret = sa_delete_instance(impl_handle->scfhandle, name);
			sa_free_attr_string(name);
		}
		xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
		xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
	} else {
		ret = SA_SYSTEM_ERR;
	}
	return (ret);
}

/*
 * sa_update_config()
 *
 * Used to update legacy files that need to be updated in bulk
 * Currently, this is a placeholder and will go away in a future
 * release.
 */

int
sa_update_config(sa_handle_t handle)
{
	/*
	 * do legacy files first so we can tell when they change.
	 * This will go away when we start updating individual records
	 * rather than the whole file.
	 */
	update_legacy_config(handle);
	return (SA_OK);
}

/*
 * get_node_attr(node, tag)
 *
 * Get the specified tag(attribute) if it exists on the node.  This is
 * used internally by a number of attribute oriented functions.
 */

static char *
get_node_attr(void *nodehdl, char *tag)
{
	xmlNodePtr node = (xmlNodePtr)nodehdl;
	xmlChar *name = NULL;

	if (node != NULL)
		name = xmlGetProp(node, (xmlChar *)tag);
	return ((char *)name);
}

/*
 * set_node_attr(node, tag)
 *
 * Set the specified tag(attribute) to the specified value This is
 * used internally by a number of attribute oriented functions. It
 * doesn't update the repository, only the internal document state.
 */

void
set_node_attr(void *nodehdl, char *tag, char *value)
{
	xmlNodePtr node = (xmlNodePtr)nodehdl;
	if (node != NULL && tag != NULL) {
		if (value != NULL)
			(void) xmlSetProp(node, (xmlChar *)tag,
			    (xmlChar *)value);
		else
			(void) xmlUnsetProp(node, (xmlChar *)tag);
	}
}

/*
 * sa_get_group_attr(group, tag)
 *
 * Get the specied attribute, if defined, for the group.
 */

char *
sa_get_group_attr(sa_group_t group, char *tag)
{
	return (get_node_attr((void *)group, tag));
}

/*
 * sa_set_group_attr(group, tag, value)
 *
 * set the specified tag/attribute on the group using value as its
 * value.
 *
 * This will result in setting the property in the SMF repository as
 * well as in the internal document.
 */

int
sa_set_group_attr(sa_group_t group, char *tag, char *value)
{
	int ret;
	char *groupname;
	sa_handle_impl_t impl_handle;

	/*
	 * ZFS group/subgroup doesn't need the handle so shortcut.
	 */
	if (sa_group_is_zfs(group)) {
		set_node_attr((void *)group, tag, value);
		return (SA_OK);
	}

	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle != NULL) {
		groupname = sa_get_group_attr(group, "name");
		ret = sa_get_instance(impl_handle->scfhandle, groupname);
		if (ret == SA_OK) {
			set_node_attr((void *)group, tag, value);
			ret = sa_start_transaction(impl_handle->scfhandle,
			    "operation");
			if (ret == SA_OK) {
				ret = sa_set_property(impl_handle->scfhandle,
				    tag, value);
				if (ret == SA_OK)
					ret = sa_end_transaction(
					    impl_handle->scfhandle,
					    impl_handle);
				else
					sa_abort_transaction(
					    impl_handle->scfhandle);
			}
			if (ret == SA_SYSTEM_ERR)
				ret = SA_NO_PERMISSION;
		}
		if (groupname != NULL)
			sa_free_attr_string(groupname);
	} else {
		ret = SA_SYSTEM_ERR;
	}
	return (ret);
}

/*
 * sa_get_share_attr(share, tag)
 *
 * Return the value of the tag/attribute set on the specified
 * share. Returns NULL if the tag doesn't exist.
 */

char *
sa_get_share_attr(sa_share_t share, char *tag)
{
	return (get_node_attr((void *)share, tag));
}

/*
 * _sa_set_share_description(share, description)
 *
 * Add a description tag with text contents to the specified share.  A
 * separate XML tag is used rather than a property. This can also be
 * used with resources.
 */

xmlNodePtr
_sa_set_share_description(void *share, char *content)
{
	xmlNodePtr node;
	node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description",
	    NULL);
	xmlNodeSetContent(node, (xmlChar *)content);
	return (node);
}

/*
 * sa_set_share_attr(share, tag, value)
 *
 * Set the share attribute specified by tag to the specified value. In
 * the case of "resource", enforce a no duplicates in a group rule. If
 * the share is not transient, commit the changes to the repository
 * else just update the share internally.
 */

int
sa_set_share_attr(sa_share_t share, char *tag, char *value)
{
	sa_group_t group;
	sa_share_t resource;
	int ret = SA_OK;

	group = sa_get_parent_group(share);

	/*
	 * There are some attributes that may have specific
	 * restrictions on them. Initially, only "resource" has
	 * special meaning that needs to be checked. Only one instance
	 * of a resource name may exist within a group.
	 */

	if (strcmp(tag, "resource") == 0) {
		resource = sa_get_resource(group, value);
		if (resource != share && resource != NULL)
			ret = SA_DUPLICATE_NAME;
	}
	if (ret == SA_OK) {
		set_node_attr((void *)share, tag, value);
		if (group != NULL) {
			char *type;
			/* we can probably optimize this some */
			type = sa_get_share_attr(share, "type");
			if (type == NULL || strcmp(type, "transient") != 0) {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				if (impl_handle != NULL) {
					ret = sa_commit_share(
					    impl_handle->scfhandle, group,
					    share);
				} else {
					ret = SA_SYSTEM_ERR;
				}
			}
			if (type != NULL)
				sa_free_attr_string(type);
		}
	}
	return (ret);
}

/*
 * sa_get_property_attr(prop, tag)
 *
 * Get the value of the specified property attribute. Standard
 * attributes are "type" and "value".
 */

char *
sa_get_property_attr(sa_property_t prop, char *tag)
{
	return (get_node_attr((void *)prop, tag));
}

/*
 * sa_get_optionset_attr(prop, tag)
 *
 * Get the value of the specified property attribute. Standard
 * attribute is "type".
 */

char *
sa_get_optionset_attr(sa_property_t optionset, char *tag)
{
	return (get_node_attr((void *)optionset, tag));

}

/*
 * sa_set_optionset_attr(optionset, tag, value)
 *
 * Set the specified attribute(tag) to the specified value on the
 * optionset.
 */

void
sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
{
	set_node_attr((void *)optionset, tag, value);
}

/*
 * sa_free_attr_string(string)
 *
 * Free the string that was returned in one of the sa_get_*_attr()
 * functions.
 */

void
sa_free_attr_string(char *string)
{
	xmlFree((xmlChar *)string);
}

/*
 * sa_get_optionset(group, proto)
 *
 * Return the optionset, if it exists, that is associated with the
 * specified protocol.
 */

sa_optionset_t
sa_get_optionset(void *group, char *proto)
{
	xmlNodePtr node;
	xmlChar *value = NULL;

	for (node = ((xmlNodePtr)group)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
			value = xmlGetProp(node, (xmlChar *)"type");
			if (proto != NULL) {
				if (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)proto) == 0) {
					break;
				}
				if (value != NULL) {
					xmlFree(value);
					value = NULL;
				}
			} else {
				break;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	return ((sa_optionset_t)node);
}

/*
 * sa_get_next_optionset(optionset)
 *
 * Return the next optionset in the group. NULL if this was the last.
 */

sa_optionset_t
sa_get_next_optionset(sa_optionset_t optionset)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
			break;
		}
	}
	return ((sa_optionset_t)node);
}

/*
 * sa_get_security(group, sectype, proto)
 *
 * Return the security optionset. The internal name is a hold over
 * from the implementation and will be changed before the API is
 * finalized. This is really a named optionset that can be negotiated
 * as a group of properties (like NFS security options).
 */

sa_security_t
sa_get_security(sa_group_t group, char *sectype, char *proto)
{
	xmlNodePtr node;
	xmlChar *value = NULL;

	for (node = ((xmlNodePtr)group)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
			if (proto != NULL) {
				value = xmlGetProp(node, (xmlChar *)"type");
				if (value == NULL ||
				    (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)proto) != 0)) {
					/* it doesn't match so continue */
					xmlFree(value);
					value = NULL;
					continue;
				}
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
			/* potential match */
			if (sectype != NULL) {
				value = xmlGetProp(node, (xmlChar *)"sectype");
				if (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)sectype) == 0) {
					break;
				}
			} else {
				break;
			}
		}
		if (value != NULL) {
			xmlFree(value);
			value = NULL;
		}
	}
	if (value != NULL)
		xmlFree(value);
	return ((sa_security_t)node);
}

/*
 * sa_get_next_security(security)
 *
 * Get the next security optionset if one exists.
 */

sa_security_t
sa_get_next_security(sa_security_t security)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)security)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
			break;
		}
	}
	return ((sa_security_t)node);
}

/*
 * sa_get_property(optionset, prop)
 *
 * Get the property object with the name specified in prop from the
 * optionset.
 */

sa_property_t
sa_get_property(sa_optionset_t optionset, char *prop)
{
	xmlNodePtr node = (xmlNodePtr)optionset;
	xmlChar *value = NULL;

	if (optionset == NULL)
		return (NULL);

	for (node = node->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			if (prop == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"type");
			if (value != NULL &&
			    xmlStrcmp(value, (xmlChar *)prop) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
		/*
		 * avoid a non option node -- it is possible to be a
		 * text node
		 */
		node = NULL;
	}
	return ((sa_property_t)node);
}

/*
 * sa_get_next_property(property)
 *
 * Get the next property following the specified property. NULL if
 * this was the last.
 */

sa_property_t
sa_get_next_property(sa_property_t property)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)property)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			break;
		}
	}
	return ((sa_property_t)node);
}

/*
 * sa_set_share_description(share, content)
 *
 * Set the description of share to content.
 */

int
sa_set_share_description(sa_share_t share, char *content)
{
	xmlNodePtr node;
	sa_group_t group;
	int ret = SA_OK;

	for (node = ((xmlNodePtr)share)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
			break;
		}
	}
	/* no existing description but want to add */
	if (node == NULL && content != NULL) {
		/* add a description */
		node = _sa_set_share_description(share, content);
	} else if (node != NULL && content != NULL) {
		/* update a description */
		xmlNodeSetContent(node, (xmlChar *)content);
	} else if (node != NULL && content == NULL) {
		/* remove an existing description */
		xmlUnlinkNode(node);
		xmlFreeNode(node);
	}
	group = sa_get_parent_group(share);
	if (group != NULL &&
	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
		sa_handle_impl_t impl_handle;
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			ret = sa_commit_share(impl_handle->scfhandle, group,
			    share);
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * fixproblemchars(string)
 *
 * don't want any newline or tab characters in the text since these
 * could break display of data and legacy file formats.
 */
static void
fixproblemchars(char *str)
{
	int c;
	for (c = *str; c != '\0'; c = *++str) {
		if (c == '\t' || c == '\n')
			*str = ' ';
		else if (c == '"')
			*str = '\'';
	}
}

/*
 * sa_get_share_description(share)
 *
 * Return the description text for the specified share if it
 * exists. NULL if no description exists.
 */

char *
sa_get_share_description(sa_share_t share)
{
	xmlChar *description = NULL;
	xmlNodePtr node;

	for (node = ((xmlNodePtr)share)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
			break;
		}
	}
	if (node != NULL) {
		description = xmlNodeGetContent(node);
		fixproblemchars((char *)description);
	}
	return ((char *)description);
}

/*
 * sa_free(share_description(description)
 *
 * Free the description string.
 */

void
sa_free_share_description(char *description)
{
	xmlFree((xmlChar *)description);
}

/*
 * sa_create_optionset(group, proto)
 *
 * Create an optionset for the specified protocol in the specied
 * group. This is manifested as a property group within SMF.
 */

sa_optionset_t
sa_create_optionset(sa_group_t group, char *proto)
{
	sa_optionset_t optionset;
	sa_group_t parent = group;
	sa_share_t share = NULL;
	int err = SA_OK;
	char *id = NULL;

	optionset = sa_get_optionset(group, proto);
	if (optionset != NULL) {
		/* can't have a duplicate protocol */
		optionset = NULL;
	} else {
		/*
		 * Account for resource names being slightly
		 * different.
		 */
		if (sa_is_share(group)) {
			/*
			 * Transient shares do not have an "id" so not an
			 * error to not find one.
			 */
			id = sa_get_share_attr((sa_share_t)group, "id");
		} else if (sa_is_resource(group)) {
			share = sa_get_resource_parent(
			    (sa_resource_t)group);
			id = sa_get_resource_attr(share, "id");

			/* id can be NULL if the group is transient (ZFS) */
			if (id == NULL && sa_is_persistent(group))
				err = SA_NO_MEMORY;
		}
		if (err == SA_NO_MEMORY) {
			/*
			 * Couldn't get the id for the share or
			 * resource. While this could be a
			 * configuration issue, it is most likely an
			 * out of memory. In any case, fail the create.
			 */
			return (NULL);
		}

		optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
		    NULL, (xmlChar *)"optionset", NULL);
		/*
		 * only put to repository if on a group and we were
		 * able to create an optionset.
		 */
		if (optionset != NULL) {
			char oname[SA_STRSIZE];
			char *groupname;

			/*
			 * Need to get parent group in all cases, but also get
			 * the share if this is a resource.
			 */
			if (sa_is_share(group)) {
				parent = sa_get_parent_group((sa_share_t)group);
			} else if (sa_is_resource(group)) {
				share = sa_get_resource_parent(
				    (sa_resource_t)group);
				parent = sa_get_parent_group(share);
			}

			sa_set_optionset_attr(optionset, "type", proto);

			(void) sa_optionset_name(optionset, oname,
			    sizeof (oname), id);
			groupname = sa_get_group_attr(parent, "name");
			if (groupname != NULL && sa_is_persistent(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				assert(impl_handle != NULL);
				if (impl_handle != NULL) {
					(void) sa_get_instance(
					    impl_handle->scfhandle, groupname);
					(void) sa_create_pgroup(
					    impl_handle->scfhandle, oname);
				}
			}
			if (groupname != NULL)
				sa_free_attr_string(groupname);
		}
	}

	if (id != NULL)
		sa_free_attr_string(id);
	return (optionset);
}

/*
 * sa_get_property_parent(property)
 *
 * Given a property, return the object it is a property of. This will
 * be an optionset of some type.
 */

static sa_optionset_t
sa_get_property_parent(sa_property_t property)
{
	xmlNodePtr node = NULL;

	if (property != NULL)
		node = ((xmlNodePtr)property)->parent;
	return ((sa_optionset_t)node);
}

/*
 * sa_get_optionset_parent(optionset)
 *
 * Return the parent of the specified optionset. This could be a group
 * or a share.
 */

static sa_group_t
sa_get_optionset_parent(sa_optionset_t optionset)
{
	xmlNodePtr node = NULL;

	if (optionset != NULL)
		node = ((xmlNodePtr)optionset)->parent;
	return ((sa_group_t)node);
}

/*
 * zfs_needs_update(share)
 *
 * In order to avoid making multiple updates to a ZFS share when
 * setting properties, the share attribute "changed" will be set to
 * true when a property is added or modified.  When done adding
 * properties, we can then detect that an update is needed.  We then
 * clear the state here to detect additional changes.
 */

static int
zfs_needs_update(sa_share_t share)
{
	char *attr;
	int result = 0;

	attr = sa_get_share_attr(share, "changed");
	if (attr != NULL) {
		sa_free_attr_string(attr);
		result = 1;
	}
	set_node_attr((void *)share, "changed", NULL);
	return (result);
}

/*
 * zfs_set_update(share)
 *
 * Set the changed attribute of the share to true.
 */

static void
zfs_set_update(sa_share_t share)
{
	set_node_attr((void *)share, "changed", "true");
}

/*
 * sa_commit_properties(optionset, clear)
 *
 * Check if SMF or ZFS config and either update or abort the pending
 * changes.
 */

int
sa_commit_properties(sa_optionset_t optionset, int clear)
{
	sa_group_t group;
	sa_group_t parent;
	int zfs = 0;
	int needsupdate = 0;
	int ret = SA_OK;
	sa_handle_impl_t impl_handle;

	group = sa_get_optionset_parent(optionset);
	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
		/* only update ZFS if on a share */
		parent = sa_get_parent_group(group);
		zfs++;
		if (parent != NULL && is_zfs_group(parent))
			needsupdate = zfs_needs_update(group);
		else
			zfs = 0;
	}
	if (zfs) {
		if (!clear && needsupdate)
			ret = sa_zfs_update((sa_share_t)group);
	} else {
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			if (clear) {
				(void) sa_abort_transaction(
				    impl_handle->scfhandle);
			} else {
				ret = sa_end_transaction(
				    impl_handle->scfhandle, impl_handle);
			}
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * sa_destroy_optionset(optionset)
 *
 * Remove the optionset from its group. Update the repository to
 * reflect this change.
 */

int
sa_destroy_optionset(sa_optionset_t optionset)
{
	char name[SA_STRSIZE];
	int len;
	int ret;
	char *id = NULL;
	sa_group_t group;
	int ispersist = 1;

	/* now delete the prop group */
	group = sa_get_optionset_parent(optionset);
	if (group != NULL) {
		if (sa_is_resource(group)) {
			sa_resource_t resource = group;
			sa_share_t share = sa_get_resource_parent(resource);
			group = sa_get_parent_group(share);
			id = sa_get_share_attr(share, "id");
		} else if (sa_is_share(group)) {
			id = sa_get_share_attr((sa_share_t)group, "id");
		}
		ispersist = sa_is_persistent(group);
	}
	if (ispersist) {
		sa_handle_impl_t impl_handle;
		len = sa_optionset_name(optionset, name, sizeof (name), id);
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			if (len > 0) {
				ret = sa_delete_pgroup(impl_handle->scfhandle,
				    name);
			}
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	xmlUnlinkNode((xmlNodePtr)optionset);
	xmlFreeNode((xmlNodePtr)optionset);
	if (id != NULL)
		sa_free_attr_string(id);
	return (ret);
}

/* private to the implementation */
int
_sa_remove_optionset(sa_optionset_t optionset)
{
	int ret = SA_OK;

	xmlUnlinkNode((xmlNodePtr)optionset);
	xmlFreeNode((xmlNodePtr)optionset);
	return (ret);
}

/*
 * sa_create_security(group, sectype, proto)
 *
 * Create a security optionset (one that has a type name and a
 * proto). Security is left over from a pure NFS implementation. The
 * naming will change in the future when the API is released.
 */
sa_security_t
sa_create_security(sa_group_t group, char *sectype, char *proto)
{
	sa_security_t security;
	char *id = NULL;
	sa_group_t parent;
	char *groupname = NULL;

	if (group != NULL && sa_is_share(group)) {
		id = sa_get_share_attr((sa_share_t)group, "id");
		parent = sa_get_parent_group(group);
		if (parent != NULL)
			groupname = sa_get_group_attr(parent, "name");
	} else if (group != NULL) {
		groupname = sa_get_group_attr(group, "name");
	}

	security = sa_get_security(group, sectype, proto);
	if (security != NULL) {
		/* can't have a duplicate security option */
		security = NULL;
	} else {
		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
		    NULL, (xmlChar *)"security", NULL);
		if (security != NULL) {
			char oname[SA_STRSIZE];
			sa_set_security_attr(security, "type", proto);

			sa_set_security_attr(security, "sectype", sectype);
			(void) sa_security_name(security, oname,
			    sizeof (oname), id);
			if (groupname != NULL && sa_is_persistent(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				if (impl_handle != NULL) {
					(void) sa_get_instance(
					    impl_handle->scfhandle, groupname);
					(void) sa_create_pgroup(
					    impl_handle->scfhandle, oname);
				}
			}
		}
	}
	if (id != NULL)
		sa_free_attr_string(id);
	if (groupname != NULL)
		sa_free_attr_string(groupname);
	return (security);
}

/*
 * sa_destroy_security(security)
 *
 * Remove the specified optionset from the document and the
 * configuration.
 */

int
sa_destroy_security(sa_security_t security)
{
	char name[SA_STRSIZE];
	int len;
	int ret = SA_OK;
	char *id = NULL;
	sa_group_t group;
	int iszfs = 0;
	int ispersist = 1;

	group = sa_get_optionset_parent(security);

	if (group != NULL)
		iszfs = sa_group_is_zfs(group);

	if (group != NULL && !iszfs) {
		if (sa_is_share(group))
			ispersist = sa_is_persistent(group);
		id = sa_get_share_attr((sa_share_t)group, "id");
	}
	if (ispersist) {
		len = sa_security_name(security, name, sizeof (name), id);
		if (!iszfs && len > 0) {
			sa_handle_impl_t impl_handle;
			impl_handle =
			    (sa_handle_impl_t)sa_find_group_handle(group);
			if (impl_handle != NULL) {
				ret = sa_delete_pgroup(impl_handle->scfhandle,
				    name);
			} else {
				ret = SA_SYSTEM_ERR;
			}
		}
	}
	xmlUnlinkNode((xmlNodePtr)security);
	xmlFreeNode((xmlNodePtr)security);
	if (iszfs)
		ret = sa_zfs_update(group);
	if (id != NULL)
		sa_free_attr_string(id);
	return (ret);
}

/*
 * sa_get_security_attr(optionset, tag)
 *
 * Return the specified attribute value from the optionset.
 */

char *
sa_get_security_attr(sa_property_t optionset, char *tag)
{
	return (get_node_attr((void *)optionset, tag));

}

/*
 * sa_set_security_attr(optionset, tag, value)
 *
 * Set the optioset attribute specied by tag to the specified value.
 */

void
sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
{
	set_node_attr((void *)optionset, tag, value);
}

/*
 * is_nodetype(node, type)
 *
 * Check to see if node is of the type specified.
 */

static int
is_nodetype(void *node, char *type)
{
	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
}

/*
 * add_or_update()
 *
 * Add or update a property. Pulled out of sa_set_prop_by_prop for
 * readability.
 */
static int
add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value,
    scf_transaction_entry_t *entry, char *name, char *valstr)
{
	int ret = SA_SYSTEM_ERR;

	if (value != NULL) {
		if (type == SA_PROP_OP_ADD)
			ret = scf_transaction_property_new(scf_handle->trans,
			    entry, name, SCF_TYPE_ASTRING);
		else
			ret = scf_transaction_property_change(scf_handle->trans,
			    entry, name, SCF_TYPE_ASTRING);
		if (ret == 0) {
			ret = scf_value_set_astring(value, valstr);
			if (ret == 0)
				ret = scf_entry_add_value(entry, value);
			if (ret == 0)
				return (ret);
			scf_value_destroy(value);
		} else {
			scf_entry_destroy(entry);
		}
	}
	return (SA_SYSTEM_ERR);
}

/*
 * sa_set_prop_by_prop(optionset, group, prop, type)
 *
 * Add/remove/update the specified property prop into the optionset or
 * share. If a share, sort out which property group based on GUID. In
 * all cases, the appropriate transaction is set (or ZFS share is
 * marked as needing an update)
 */

static int
sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
			sa_property_t prop, int type)
{
	char *name;
	char *valstr;
	int ret = SA_OK;
	scf_transaction_entry_t *entry;
	scf_value_t *value;
	int opttype; /* 1 == optionset, 0 == security */
	char *id = NULL;
	int iszfs = 0;
	sa_group_t parent = NULL;
	sa_share_t share = NULL;
	sa_handle_impl_t impl_handle;
	scfutilhandle_t  *scf_handle;

	if (!sa_is_persistent(group)) {
		/*
		 * if the group/share is not persistent we don't need
		 * to do anything here
		 */
		return (SA_OK);
	}
	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle == NULL || impl_handle->scfhandle == NULL)
		return (SA_SYSTEM_ERR);
	scf_handle = impl_handle->scfhandle;
	name = sa_get_property_attr(prop, "type");
	valstr = sa_get_property_attr(prop, "value");
	entry = scf_entry_create(scf_handle->handle);
	opttype = is_nodetype((void *)optionset, "optionset");

	/*
	 * Check for share vs. resource since they need slightly
	 * different treatment given the hierarchy.
	 */
	if (valstr != NULL && entry != NULL) {
		if (sa_is_share(group)) {
			parent = sa_get_parent_group(group);
			share = (sa_share_t)group;
			if (parent != NULL)
				iszfs = is_zfs_group(parent);
		} else if (sa_is_resource(group)) {
			share = sa_get_parent_group(group);
			if (share != NULL)
				parent = sa_get_parent_group(share);
		} else {
			iszfs = is_zfs_group(group);
		}
		if (!iszfs) {
			if (scf_handle->trans == NULL) {
				char oname[SA_STRSIZE];
				char *groupname = NULL;
				if (share != NULL) {
					if (parent != NULL)
						groupname =
						    sa_get_group_attr(parent,
						    "name");
					id = sa_get_share_attr(
					    (sa_share_t)share, "id");
				} else {
					groupname = sa_get_group_attr(group,
					    "name");
				}
				if (groupname != NULL) {
					ret = sa_get_instance(scf_handle,
					    groupname);
					sa_free_attr_string(groupname);
				}
				if (opttype)
					(void) sa_optionset_name(optionset,
					    oname, sizeof (oname), id);
				else
					(void) sa_security_name(optionset,
					    oname, sizeof (oname), id);
				ret = sa_start_transaction(scf_handle, oname);
				if (id != NULL)
					sa_free_attr_string(id);
			}
			if (ret == SA_OK) {
				switch (type) {
				case SA_PROP_OP_REMOVE:
					ret = scf_transaction_property_delete(
					    scf_handle->trans, entry, name);
					break;
				case SA_PROP_OP_ADD:
				case SA_PROP_OP_UPDATE:
					value = scf_value_create(
					    scf_handle->handle);
					ret = add_or_update(scf_handle, type,
					    value, entry, name, valstr);
					break;
				}
			}
		} else {
			/*
			 * ZFS update. The calling function would have updated
			 * the internal XML structure. Just need to flag it as
			 * changed for ZFS.
			 */
			zfs_set_update((sa_share_t)group);
		}
	}

	if (name != NULL)
		sa_free_attr_string(name);
	if (valstr != NULL)
		sa_free_attr_string(valstr);
	else if (entry != NULL)
		scf_entry_destroy(entry);

	if (ret == -1)
		ret = SA_SYSTEM_ERR;

	return (ret);
}

/*
 * sa_create_section(name, value)
 *
 * Create a new section with the specified name and extra data.
 */

sa_property_t
sa_create_section(char *name, char *extra)
{
	xmlNodePtr node;

	node = xmlNewNode(NULL, (xmlChar *)"section");
	if (node != NULL) {
		if (name != NULL)
			(void) xmlSetProp(node, (xmlChar *)"name",
			    (xmlChar *)name);
		if (extra != NULL)
			(void) xmlSetProp(node, (xmlChar *)"extra",
			    (xmlChar *)extra);
	}
	return ((sa_property_t)node);
}

void
sa_set_section_attr(sa_property_t sect, char *name, char *value)
{
	(void) xmlSetProp(sect, (xmlChar *)name, (xmlChar *)value);
}

/*
 * sa_create_property(section, name, value)
 *
 * Create a new property with the specified name and value.
 */

sa_property_t
sa_create_property(char *name, char *value)
{
	xmlNodePtr node;

	node = xmlNewNode(NULL, (xmlChar *)"option");
	if (node != NULL) {
		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
		(void) xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
	}
	return ((sa_property_t)node);
}

/*
 * sa_add_property(object, property)
 *
 * Add the specified property to the object. Issue the appropriate
 * transaction or mark a ZFS object as needing an update.
 */

int
sa_add_property(void *object, sa_property_t property)
{
	int ret = SA_OK;
	sa_group_t parent;
	sa_group_t group;
	char *proto;

	if (property != NULL) {
		sa_handle_t handle;
		handle = sa_find_group_handle((sa_group_t)object);
		/* It is legitimate to not find a handle */
		proto = sa_get_optionset_attr(object, "type");
		if ((ret = sa_valid_property(handle, object, proto,
		    property)) == SA_OK) {
			property = (sa_property_t)xmlAddChild(
			    (xmlNodePtr)object, (xmlNodePtr)property);
		} else {
			if (proto != NULL)
				sa_free_attr_string(proto);
			return (ret);
		}
		if (proto != NULL)
			sa_free_attr_string(proto);
	}


	parent = sa_get_parent_group(object);
	if (!sa_is_persistent(parent))
		return (ret);

	if (sa_is_resource(parent)) {
		/*
		 * Resources are children of share.  Need to go up two
		 * levels to find the group but the parent needs to be
		 * the share at this point in order to get the "id".
		 */
		parent = sa_get_parent_group(parent);
		group = sa_get_parent_group(parent);
	} else if (sa_is_share(parent)) {
		group = sa_get_parent_group(parent);
	} else {
		group = parent;
	}

	if (property == NULL) {
		ret = SA_NO_MEMORY;
	} else {
		char oname[SA_STRSIZE];

		if (!is_zfs_group(group)) {
			char *id = NULL;
			sa_handle_impl_t impl_handle;
			scfutilhandle_t  *scf_handle;

			impl_handle = (sa_handle_impl_t)sa_find_group_handle(
			    group);
			if (impl_handle == NULL ||
			    impl_handle->scfhandle == NULL)
				ret = SA_SYSTEM_ERR;
			if (ret == SA_OK) {
				scf_handle = impl_handle->scfhandle;
				if (sa_is_share((sa_group_t)parent)) {
					id = sa_get_share_attr(
					    (sa_share_t)parent, "id");
				}
				if (scf_handle->trans == NULL) {
					if (is_nodetype(object, "optionset")) {
						(void) sa_optionset_name(
						    (sa_optionset_t)object,
						    oname, sizeof (oname), id);
					} else {
						(void) sa_security_name(
						    (sa_optionset_t)object,
						    oname, sizeof (oname), id);
					}
					ret = sa_start_transaction(scf_handle,
					    oname);
				}
				if (ret == SA_OK) {
					char *name;
					char *value;
					name = sa_get_property_attr(property,
					    "type");
					value = sa_get_property_attr(property,
					    "value");
					if (name != NULL && value != NULL) {
						if (scf_handle->scf_state ==
						    SCH_STATE_INIT) {
							ret = sa_set_property(
							    scf_handle, name,
							    value);
						}
					} else {
						ret = SA_CONFIG_ERR;
					}
					if (name != NULL)
						sa_free_attr_string(
						    name);
					if (value != NULL)
						sa_free_attr_string(value);
				}
				if (id != NULL)
					sa_free_attr_string(id);
			}
		} else {
			/*
			 * ZFS is a special case. We do want
			 * to allow editing property/security
			 * lists since we can have a better
			 * syntax and we also want to keep
			 * things consistent when possible.
			 *
			 * Right now, we defer until the
			 * sa_commit_properties so we can get
			 * them all at once. We do need to
			 * mark the share as "changed"
			 */
			zfs_set_update((sa_share_t)parent);
		}
	}
	return (ret);
}

/*
 * sa_remove_property(property)
 *
 * Remove the specied property from its containing object. Update the
 * repository as appropriate.
 */

int
sa_remove_property(sa_property_t property)
{
	int ret = SA_OK;

	if (property != NULL) {
		sa_optionset_t optionset;
		sa_group_t group;
		optionset = sa_get_property_parent(property);
		if (optionset != NULL) {
			group = sa_get_optionset_parent(optionset);
			if (group != NULL) {
				ret = sa_set_prop_by_prop(optionset, group,
				    property, SA_PROP_OP_REMOVE);
			}
		}
		xmlUnlinkNode((xmlNodePtr)property);
		xmlFreeNode((xmlNodePtr)property);
	} else {
		ret = SA_NO_SUCH_PROP;
	}
	return (ret);
}

/*
 * sa_update_property(property, value)
 *
 * Update the specified property to the new value.  If value is NULL,
 * we currently treat this as a remove.
 */

int
sa_update_property(sa_property_t property, char *value)
{
	int ret = SA_OK;
	if (value == NULL) {
		return (sa_remove_property(property));
	} else {
		sa_optionset_t optionset;
		sa_group_t group;
		set_node_attr((void *)property, "value", value);
		optionset = sa_get_property_parent(property);
		if (optionset != NULL) {
			group = sa_get_optionset_parent(optionset);
			if (group != NULL) {
				ret = sa_set_prop_by_prop(optionset, group,
				    property, SA_PROP_OP_UPDATE);
			}
		} else {
			ret = SA_NO_SUCH_PROP;
		}
	}
	return (ret);
}

/*
 * sa_get_protocol_section(propset, prop)
 *
 * Get the specified protocol specific section. These are global to
 * the protocol and not specific to a group or share.
 */

sa_protocol_properties_t
sa_get_protocol_section(sa_protocol_properties_t propset, char *section)
{
	xmlNodePtr node = (xmlNodePtr)propset;
	xmlChar *value = NULL;
	char *proto;

	proto = sa_get_optionset_attr(propset, "type");
	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
		if (proto != NULL)
			sa_free_attr_string(proto);
		return (propset);
	}

	for (node = node->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
			if (section == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"name");
			if (value != NULL &&
			    xmlStrcasecmp(value, (xmlChar *)section) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	if (proto != NULL)
		sa_free_attr_string(proto);
	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"section") != 0) {
		/*
		 * avoid a non option node -- it is possible to be a
		 * text node
		 */
		node = NULL;
	}
	return ((sa_protocol_properties_t)node);
}

/*
 * sa_get_next_protocol_section(prop, find)
 *
 * Get the next protocol specific section in the list.
 */

sa_property_t
sa_get_next_protocol_section(sa_property_t prop, char *find)
{
	xmlNodePtr node;
	xmlChar *value = NULL;
	char *proto;

	proto = sa_get_optionset_attr(prop, "type");
	if ((sa_proto_get_featureset(proto) & SA_FEATURE_HAS_SECTIONS) == 0) {
		if (proto != NULL)
			sa_free_attr_string(proto);
		return ((sa_property_t)NULL);
	}

	for (node = ((xmlNodePtr)prop)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"section") == 0) {
			if (find == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"name");
			if (value != NULL &&
			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}

		}
	}
	if (value != NULL)
		xmlFree(value);
	if (proto != NULL)
		sa_free_attr_string(proto);
	return ((sa_property_t)node);
}

/*
 * sa_get_protocol_property(propset, prop)
 *
 * Get the specified protocol specific property. These are global to
 * the protocol and not specific to a group or share.
 */

sa_property_t
sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
{
	xmlNodePtr node = (xmlNodePtr)propset;
	xmlChar *value = NULL;

	if (propset == NULL)
		return (NULL);

	for (node = node->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			if (prop == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"type");
			if (value != NULL &&
			    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
		/*
		 * avoid a non option node -- it is possible to be a
		 * text node
		 */
		node = NULL;
	}
	return ((sa_property_t)node);
}

/*
 * sa_get_next_protocol_property(prop)
 *
 * Get the next protocol specific property in the list.
 */

sa_property_t
sa_get_next_protocol_property(sa_property_t prop, char *find)
{
	xmlNodePtr node;
	xmlChar *value = NULL;

	for (node = ((xmlNodePtr)prop)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			if (find == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"type");
			if (value != NULL &&
			    xmlStrcasecmp(value, (xmlChar *)find) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}

		}
	}
	if (value != NULL)
		xmlFree(value);
	return ((sa_property_t)node);
}

/*
 * sa_set_protocol_property(prop, value)
 *
 * Set the specified property to have the new value.  The protocol
 * specific plugin will then be called to update the property.
 */

int
sa_set_protocol_property(sa_property_t prop, char *section, char *value)
{
	sa_protocol_properties_t propset;
	char *proto;
	int ret = SA_INVALID_PROTOCOL;

	propset = ((xmlNodePtr)prop)->parent;
	if (propset != NULL) {
		proto = sa_get_optionset_attr(propset, "type");
		if (proto != NULL) {
			if (section != NULL)
				set_node_attr((xmlNodePtr)prop, "section",
				    section);
			set_node_attr((xmlNodePtr)prop, "value", value);
			ret = sa_proto_set_property(proto, prop);
			sa_free_attr_string(proto);
		}
	}
	return (ret);
}

/*
 * sa_add_protocol_property(propset, prop)
 *
 * Add a new property to the protocol specific property set.
 */

int
sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
{
	xmlNodePtr node;

	/* should check for legitimacy */
	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
	if (node != NULL)
		return (SA_OK);
	return (SA_NO_MEMORY);
}

/*
 * sa_create_protocol_properties(proto)
 *
 * Create a protocol specific property set.
 */

sa_protocol_properties_t
sa_create_protocol_properties(char *proto)
{
	xmlNodePtr node;

	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
	if (node != NULL)
		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
	return (node);
}

/*
 * sa_get_share_resource(share, resource)
 *
 * Get the named resource from the share, if it exists. If resource is
 * NULL, get the first resource.
 */

sa_resource_t
sa_get_share_resource(sa_share_t share, char *resource)
{
	xmlNodePtr node = NULL;
	xmlChar *name;

	if (share != NULL) {
		for (node = ((xmlNodePtr)share)->children; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0) {
				if (resource == NULL) {
					/*
					 * We are looking for the first
					 * resource node and not a names
					 * resource.
					 */
					break;
				} else {
					/* is it the correct share? */
					name = xmlGetProp(node,
					    (xmlChar *)"name");
					if (name != NULL &&
					    xmlStrcasecmp(name,
					    (xmlChar *)resource) == 0) {
						xmlFree(name);
						break;
					}
					xmlFree(name);
				}
			}
		}
	}
	return ((sa_resource_t)node);
}

/*
 * sa_get_next_resource(resource)
 *	Return the next share following the specified share
 *	from the internal list of shares. Returns NULL if there
 *	are no more shares.  The list is relative to the same
 *	group.
 */
sa_share_t
sa_get_next_resource(sa_resource_t resource)
{
	xmlNodePtr node = NULL;

	if (resource != NULL) {
		for (node = ((xmlNodePtr)resource)->next; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"resource") == 0)
				break;
		}
	}
	return ((sa_share_t)node);
}

/*
 * _sa_get_next_resource_index(share)
 *
 * get the next resource index number (one greater then current largest)
 */

static int
_sa_get_next_resource_index(sa_share_t share)
{
	sa_resource_t resource;
	int index = 0;
	char *id;

	for (resource = sa_get_share_resource(share, NULL);
	    resource != NULL;
	    resource = sa_get_next_resource(resource)) {
		id = get_node_attr((void *)resource, "id");
		if (id != NULL) {
			int val;
			val = atoi(id);
			if (val > index)
				index = val;
			sa_free_attr_string(id);
		}
	}
	return (index + 1);
}


/*
 * sa_add_resource(share, resource, persist, &err)
 *
 * Adds a new resource name associated with share. The resource name
 * must be unique in the system and will be case insensitive (eventually).
 */

sa_resource_t
sa_add_resource(sa_share_t share, char *resource, int persist, int *error)
{
	xmlNodePtr node;
	int err = SA_OK;
	sa_resource_t res;
	sa_group_t group;
	sa_handle_t handle;
	char istring[8]; /* just big enough for an integer value */
	int index;

	group = sa_get_parent_group(share);
	handle = sa_find_group_handle(group);
	res = sa_find_resource(handle, resource);
	if (res != NULL) {
		err = SA_DUPLICATE_NAME;
		res = NULL;
	} else {
		node = xmlNewChild((xmlNodePtr)share, NULL,
		    (xmlChar *)"resource", NULL);
		if (node != NULL) {
			(void) xmlSetProp(node, (xmlChar *)"name",
			    (xmlChar *)resource);
			(void) xmlSetProp(node, (xmlChar *)"type", persist ?
			    (xmlChar *)"persist" : (xmlChar *)"transient");
			if (persist != SA_SHARE_TRANSIENT) {
				index = _sa_get_next_resource_index(share);
				(void) snprintf(istring, sizeof (istring), "%d",
				    index);
				(void) xmlSetProp(node, (xmlChar *)"id",
				    (xmlChar *)istring);

				if (!sa_is_persistent((sa_group_t)share))
					goto done;

				if (!sa_group_is_zfs(group)) {
					/* ZFS doesn't use resource names */
					sa_handle_impl_t ihandle;

					ihandle = (sa_handle_impl_t)
					    sa_find_group_handle(
					    group);
					if (ihandle != NULL)
						err = sa_commit_share(
						    ihandle->scfhandle, group,
						    share);
					else
						err = SA_SYSTEM_ERR;
				} else {
					err = sa_zfs_update((sa_share_t)group);
				}
			}
		}
	}
done:
	if (error != NULL)
		*error = err;
	return ((sa_resource_t)node);
}

/*
 * sa_remove_resource(resource)
 *
 * Remove the resource name from the share (and the system)
 */

int
sa_remove_resource(sa_resource_t resource)
{
	sa_share_t share;
	sa_group_t group;
	char *type;
	int ret = SA_OK;
	boolean_t transient = B_FALSE;
	sa_optionset_t opt;

	share = sa_get_resource_parent(resource);
	type = sa_get_share_attr(share, "type");
	group = sa_get_parent_group(share);


	if (type != NULL) {
		if (strcmp(type, "persist") != 0)
			transient = B_TRUE;
		sa_free_attr_string(type);
	}

	/* Disable the resource for all protocols. */
	(void) sa_disable_resource(resource, NULL);

	/* Remove any optionsets from the resource. */
	for (opt = sa_get_optionset(resource, NULL);
	    opt != NULL;
	    opt = sa_get_next_optionset(opt))
		(void) sa_destroy_optionset(opt);

	/* Remove from the share */
	xmlUnlinkNode((xmlNode *)resource);
	xmlFreeNode((xmlNode *)resource);

	/* only do SMF action if permanent and not ZFS */
	if (transient)
		return (ret);

	if (!sa_group_is_zfs(group)) {
		sa_handle_impl_t ihandle;
		ihandle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (ihandle != NULL)
			ret = sa_commit_share(ihandle->scfhandle, group, share);
		else
			ret = SA_SYSTEM_ERR;
	} else {
		ret = sa_zfs_update((sa_share_t)group);
	}

	return (ret);
}

/*
 * proto_rename_resource(handle, group, resource, newname)
 *
 * Helper function for sa_rename_resource that notifies the protocol
 * of a resource name change prior to a config repository update.
 */
static int
proto_rename_resource(sa_handle_t handle, sa_group_t group,
    sa_resource_t resource, char *newname)
{
	sa_optionset_t optionset;
	int ret = SA_OK;
	int err;

	for (optionset = sa_get_optionset(group, NULL);
	    optionset != NULL;
	    optionset = sa_get_next_optionset(optionset)) {
		char *type;
		type = sa_get_optionset_attr(optionset, "type");
		if (type != NULL) {
			err = sa_proto_rename_resource(handle, type, resource,
			    newname);
			if (err != SA_OK)
				ret = err;
			sa_free_attr_string(type);
		}
	}
	return (ret);
}

/*
 * sa_rename_resource(resource, newname)
 *
 * Rename the resource to the new name, if it is unique.
 */

int
sa_rename_resource(sa_resource_t resource, char *newname)
{
	sa_share_t share;
	sa_group_t group = NULL;
	sa_resource_t target;
	int ret = SA_CONFIG_ERR;
	sa_handle_t handle = NULL;

	share = sa_get_resource_parent(resource);
	if (share == NULL)
		return (ret);

	group = sa_get_parent_group(share);
	if (group == NULL)
		return (ret);

	handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (handle == NULL)
		return (ret);

	target = sa_find_resource(handle, newname);
	if (target != NULL) {
		ret = SA_DUPLICATE_NAME;
	} else {
		/*
		 * Everything appears to be valid at this
		 * point. Change the name of the active share and then
		 * update the share in the appropriate repository.
		 */
		ret = proto_rename_resource(handle, group, resource, newname);
		set_node_attr(resource, "name", newname);

		if (!sa_is_persistent((sa_group_t)share))
			return (ret);

		if (!sa_group_is_zfs(group)) {
			sa_handle_impl_t ihandle = (sa_handle_impl_t)handle;
			ret = sa_commit_share(ihandle->scfhandle, group,
			    share);
		} else {
			ret = sa_zfs_update((sa_share_t)group);
		}
	}
	return (ret);
}

/*
 * sa_get_resource_attr(resource, tag)
 *
 * Get the named attribute of the resource. "name" and "id" are
 * currently defined.  NULL if tag not defined.
 */

char *
sa_get_resource_attr(sa_resource_t resource, char *tag)
{
	return (get_node_attr((void *)resource, tag));
}

/*
 * sa_set_resource_attr(resource, tag, value)
 *
 * Get the named attribute of the resource. "name" and "id" are
 * currently defined.  NULL if tag not defined. Currently we don't do
 * much, but additional checking may be needed in the future.
 */

int
sa_set_resource_attr(sa_resource_t resource, char *tag, char *value)
{
	set_node_attr((void *)resource, tag, value);
	return (SA_OK);
}

/*
 * sa_get_resource_parent(resource_t)
 *
 * Returns the share associated with the resource.
 */

sa_share_t
sa_get_resource_parent(sa_resource_t resource)
{
	sa_share_t share = NULL;

	if (resource != NULL)
		share = (sa_share_t)((xmlNodePtr)resource)->parent;
	return (share);
}

/*
 * find_resource(group, name)
 *
 * Find the resource within the group.
 */

static sa_resource_t
find_resource(sa_group_t group, char *resname)
{
	sa_share_t share;
	sa_resource_t resource = NULL;
	char *name;

	/* Iterate over all the shares and resources in the group. */
	for (share = sa_get_share(group, NULL);
	    share != NULL && resource == NULL;
	    share = sa_get_next_share(share)) {
		for (resource = sa_get_share_resource(share, NULL);
		    resource != NULL;
		    resource = sa_get_next_resource(resource)) {
			name = sa_get_resource_attr(resource, "name");
			if (name != NULL && xmlStrcasecmp((xmlChar*)name,
			    (xmlChar*)resname) == 0) {
				sa_free_attr_string(name);
				break;
			}
			if (name != NULL) {
				sa_free_attr_string(name);
			}
		}
	}
	return (resource);
}

/*
 * sa_find_resource(name)
 *
 * Find the named resource in the system.
 */

sa_resource_t
sa_find_resource(sa_handle_t handle, char *name)
{
	sa_group_t group;
	sa_group_t zgroup;
	sa_resource_t resource = NULL;

	/*
	 * Iterate over all groups and zfs subgroups and check for
	 * resource name in them.
	 */
	for (group = sa_get_group(handle, NULL); group != NULL;
	    group = sa_get_next_group(group)) {

		if (is_zfs_group(group)) {
			for (zgroup =
			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
			    (xmlChar *)"group");
			    zgroup != NULL && resource == NULL;
			    zgroup = sa_get_next_group(zgroup)) {
				resource = find_resource(zgroup, name);
			}
		} else {
			resource = find_resource(group, name);
		}
		if (resource != NULL)
			break;
	}
	return (resource);
}

/*
 * sa_get_resource(group, resource)
 *
 * Search all the shares in the specified group for a share with a
 * resource name matching the one specified.
 *
 * In the future, it may be advantageous to allow group to be NULL and
 * search all groups but that isn't needed at present.
 */

sa_resource_t
sa_get_resource(sa_group_t group, char *resource)
{
	sa_share_t share = NULL;
	sa_resource_t res = NULL;

	if (resource != NULL) {
		for (share = sa_get_share(group, NULL);
		    share != NULL && res == NULL;
		    share = sa_get_next_share(share)) {
			res = sa_get_share_resource(share, resource);
		}
	}
	return (res);
}

/*
 * get_protocol_list(optionset, object)
 *
 * Get the protocol optionset list for the object and add them as
 * properties to optionset.
 */
static int
get_protocol_list(sa_optionset_t optionset, void *object)
{
	sa_property_t prop;
	sa_optionset_t opts;
	int ret = SA_OK;

	for (opts = sa_get_optionset(object, NULL);
	    opts != NULL;
	    opts = sa_get_next_optionset(opts)) {
		char *type;
		type = sa_get_optionset_attr(opts, "type");
		/*
		 * It is possible to have a non-protocol optionset. We
		 * skip any of those found.
		 */
		if (type == NULL)
			continue;
		prop = sa_create_property(type, "true");
		sa_free_attr_string(type);
		if (prop != NULL)
			prop = (sa_property_t)xmlAddChild((xmlNodePtr)optionset,
			    (xmlNodePtr)prop);
		/* If prop is NULL, don't bother continuing */
		if (prop == NULL) {
			ret = SA_NO_MEMORY;
			break;
		}
	}
	return (ret);
}

/*
 * sa_free_protoset(optionset)
 *
 * Free the protocol property optionset.
 */
static void
sa_free_protoset(sa_optionset_t optionset)
{
	if (optionset != NULL) {
		xmlUnlinkNode((xmlNodePtr) optionset);
		xmlFreeNode((xmlNodePtr) optionset);
	}
}

/*
 * sa_optionset_t sa_get_active_protocols(object)
 *
 * Return a list of the protocols that are active for the object.
 * This is currently an internal helper function, but could be
 * made visible if there is enough demand for it.
 *
 * The function finds the parent group and extracts the protocol
 * optionsets creating a new optionset with the protocols as properties.
 *
 * The caller must free the returned optionset.
 */

static sa_optionset_t
sa_get_active_protocols(void *object)
{
	sa_optionset_t options;
	sa_share_t share = NULL;
	sa_group_t group = NULL;
	sa_resource_t resource = NULL;
	int ret = SA_OK;

	if (object == NULL)
		return (NULL);
	options = (sa_optionset_t)xmlNewNode(NULL, (xmlChar *)"optionset");
	if (options == NULL)
		return (NULL);

	/*
	 * Find the objects up the tree that might have protocols
	 * enabled on them.
	 */
	if (sa_is_resource(object)) {
		resource = (sa_resource_t)object;
		share = sa_get_resource_parent(resource);
		group = sa_get_parent_group(share);
	} else if (sa_is_share(object)) {
		share = (sa_share_t)object;
		group = sa_get_parent_group(share);
	} else {
		group = (sa_group_t)group;
	}
	if (resource != NULL)
		ret = get_protocol_list(options, resource);
	if (ret == SA_OK && share != NULL)
		ret = get_protocol_list(options, share);
	if (ret == SA_OK && group != NULL)
		ret = get_protocol_list(options, group);

	/*
	 * If there was an error, we won't have a complete list so
	 * abandon everything.  The caller will have to deal with the
	 * issue.
	 */
	if (ret != SA_OK) {
		sa_free_protoset(options);
		options = NULL;
	}
	return (options);
}

/*
 * sa_enable_resource, protocol)
 *	Disable the specified share to the specified protocol.
 *	If protocol is NULL, then all protocols.
 */
int
sa_enable_resource(sa_resource_t resource, char *protocol)
{
	int ret = SA_OK;

	if (protocol != NULL) {
		ret = sa_proto_share_resource(protocol, resource);
	} else {
		sa_optionset_t protoset;
		sa_property_t prop;
		char *proto;
		int err;

		/* need to do all protocols */
		protoset = sa_get_active_protocols(resource);
		if (protoset == NULL)
			return (SA_NO_MEMORY);
		for (prop = sa_get_property(protoset, NULL);
		    prop != NULL;
		    prop = sa_get_next_property(prop)) {
			proto = sa_get_property_attr(prop, "type");
			if (proto == NULL) {
				ret = SA_NO_MEMORY;
				continue;
			}
			err = sa_proto_share_resource(proto, resource);
			if (err != SA_OK)
				ret = err;
			sa_free_attr_string(proto);
		}
		sa_free_protoset(protoset);
	}
	if (ret == SA_OK)
		(void) sa_set_resource_attr(resource, "shared", NULL);

	return (ret);
}

/*
 * sa_disable_resource(resource, protocol)
 *
 *	Disable the specified share for the specified protocol.  If
 *	protocol is NULL, then all protocols.  If the underlying
 *	protocol doesn't implement disable at the resource level, we
 *	disable at the share level.
 */
int
sa_disable_resource(sa_resource_t resource, char *protocol)
{
	int ret = SA_OK;

	if (protocol != NULL) {
		ret = sa_proto_unshare_resource(protocol, resource);
		if (ret == SA_NOT_IMPLEMENTED) {
			sa_share_t parent;
			/*
			 * The protocol doesn't implement unshare
			 * resource. That implies that resource names are
			 * simple aliases for this protocol so we need to
			 * unshare the share.
			 */
			parent = sa_get_resource_parent(resource);
			if (parent != NULL)
				ret = sa_disable_share(parent, protocol);
			else
				ret = SA_CONFIG_ERR;
		}
	} else {
		sa_optionset_t protoset;
		sa_property_t prop;
		char *proto;
		int err;

		/* need to do all protocols */
		protoset = sa_get_active_protocols(resource);
		if (protoset == NULL)
			return (SA_NO_MEMORY);
		for (prop = sa_get_property(protoset, NULL);
		    prop != NULL;
		    prop = sa_get_next_property(prop)) {
			proto = sa_get_property_attr(prop, "type");
			if (proto == NULL) {
				ret = SA_NO_MEMORY;
				continue;
			}
			err = sa_proto_unshare_resource(proto, resource);
			if (err == SA_NOT_SUPPORTED) {
				sa_share_t parent;
				parent = sa_get_resource_parent(resource);
				if (parent != NULL)
					err = sa_disable_share(parent, proto);
				else
					err = SA_CONFIG_ERR;
			}
			if (err != SA_OK)
				ret = err;
			sa_free_attr_string(proto);
		}
		sa_free_protoset(protoset);
	}
	if (ret == SA_OK)
		(void) sa_set_resource_attr(resource, "shared", NULL);

	return (ret);
}

/*
 * sa_set_resource_description(resource, content)
 *
 * Set the description of share to content.
 */

int
sa_set_resource_description(sa_resource_t resource, char *content)
{
	xmlNodePtr node;
	sa_group_t group;
	sa_share_t share;
	int ret = SA_OK;

	for (node = ((xmlNodePtr)resource)->children;
	    node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
			break;
		}
	}

	/* no existing description but want to add */
	if (node == NULL && content != NULL) {
		/* add a description */
		node = _sa_set_share_description(resource, content);
	} else if (node != NULL && content != NULL) {
		/* update a description */
		xmlNodeSetContent(node, (xmlChar *)content);
	} else if (node != NULL && content == NULL) {
		/* remove an existing description */
		xmlUnlinkNode(node);
		xmlFreeNode(node);
	}

	share = sa_get_resource_parent(resource);
	group = sa_get_parent_group(share);
	if (group != NULL &&
	    sa_is_persistent(share) && (!sa_group_is_zfs(group))) {
		sa_handle_impl_t impl_handle;
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL)
			ret = sa_commit_share(impl_handle->scfhandle,
			    group, share);
		else
			ret = SA_SYSTEM_ERR;
	}
	return (ret);
}

/*
 * sa_get_resource_description(share)
 *
 * Return the description text for the specified share if it
 * exists. NULL if no description exists.
 */

char *
sa_get_resource_description(sa_resource_t resource)
{
	xmlChar *description = NULL;
	xmlNodePtr node;

	for (node = ((xmlNodePtr)resource)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0)
			break;
	}
	if (node != NULL) {
		description = xmlNodeGetContent(node);
		fixproblemchars((char *)description);
	}
	return ((char *)description);
}