File: vsprocs.c

package info (click to toggle)
openafs 1.6.9-2%2Bdeb8u7
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 46,032 kB
  • sloc: ansic: 465,356; xml: 70,737; sh: 11,709; makefile: 11,340; perl: 9,757; objc: 6,432; java: 5,638; cpp: 2,268; asm: 1,343; yacc: 435; tcl: 249; lex: 201; csh: 85
file content (4126 lines) | stat: -rw-r--r-- 106,037 bytes parent folder | download
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
/*
 * Copyright 2000, International Business Machines Corporation and others.
 * All Rights Reserved.
 * 
 * This software has been released under the terms of the IBM Public
 * License.  For details, see the LICENSE file in the top-level source
 * directory or online at http://www.openafs.org/dl/license10.html
 */

/*
 * This file is a reimplementation of volser/vsproc.s.  Every attempt
 * has been made to keep it as similar as possible to aid comprehension
 * of the code.  For most functions, two parameters have been added
 * the cell handle, and a status variable.  For those functions that
 * require one, a server handle may also be added.
 *
 * Other changes were made to provide thread safe functions and
 * eliminate the practice of reporting errors to STDOUT.
 */

#include <afsconfig.h>
#include <afs/param.h>


#include "vsprocs.h"
#include "vosutils.h"
#include "lockprocs.h"
#include "../adminutil/afs_AdminInternal.h"
#include <afs/afs_AdminErrors.h>
#include "afs_vosAdmin.h"
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef AFS_NT40_ENV
#include <io.h>
#endif

static afs_int32 GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
             struct qHead *myQueue, afs_int32 apart);

struct release {
    afs_int32 time;
    afs_int32 vldbEntryIndex;
};

static struct rx_connection *
UV_Bind(afs_cell_handle_p cellHandle, afs_int32 aserver, afs_int32 port)
{
    return rx_GetCachedConnection(htonl(aserver), htons(port), VOLSERVICE_ID,
				  cellHandle->tokens->afs_sc[cellHandle->
							     tokens->
							     sc_index],
				  cellHandle->tokens->sc_index);
}


/* if <okvol> is allright(indicated by beibg able to
 * start a transaction, delete the <delvol> */
static afs_int32
CheckAndDeleteVolume(struct rx_connection *aconn, afs_int32 apart,
		     afs_uint32 okvol, afs_uint32 delvol)
{
    afs_int32 error, code, tid, rcode;

    error = 0;
    code = 0;

    if (okvol == 0) {
	code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
	if (!error && code)
	    error = code;
	code = AFSVolDeleteVolume(aconn, tid);
	if (!error && code)
	    error = code;
	code = AFSVolEndTrans(aconn, tid, &rcode);
	if (!code)
	    code = rcode;
	if (!error && code)
	    error = code;
	return error;
    } else {
	code = AFSVolTransCreate(aconn, okvol, apart, ITOffline, &tid);
	if (!code) {
	    code = AFSVolEndTrans(aconn, tid, &rcode);
	    if (!code)
		code = rcode;
	    if (!error && code)
		error = code;
	    code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
	    if (!error && code)
		error = code;
	    code = AFSVolDeleteVolume(aconn, tid);
	    if (!error && code)
		error = code;
	    code = AFSVolEndTrans(aconn, tid, &rcode);
	    if (!code)
		code = rcode;
	    if (!error && code)
		error = code;
	} else
	    error = code;
	return error;
    }
}

/* forcibly remove a volume.  Very dangerous call */
int
UV_NukeVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
	      unsigned int partition, afs_uint32 volumeId, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;

    tst = AFSVolNukeVolume(server, partition, volumeId);

    if (!tst) {
	rc = 1;
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/* create a volume, given a server, partition number, volume name --> sends
* back new vol id in <anewid>*/
int
UV_CreateVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
		unsigned int partition, char *volumeName,
		unsigned int quota, afs_uint32 *volumeId, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_int32 tid = 0;
    afs_int32 rcode;
    struct nvldbentry entry;
    struct volintInfo tstatus;

    memset(&tstatus, 0, sizeof(tstatus));
    tstatus.dayUse = -1;
    tstatus.maxquota = quota;

    /* next the next 3 available ids from the VLDB */
    tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 3, volumeId);
    if (tst) {
	goto fail_UV_CreateVolume;
    }

    tst =
	AFSVolCreateVolume(server, partition, volumeName, volser_RW, 0,
			   volumeId, &tid);
    if (tst) {
	goto fail_UV_CreateVolume;
    }

    AFSVolSetInfo(server, tid, &tstatus);

    tst = AFSVolSetFlags(server, tid, 0);
    if (tst) {
	goto fail_UV_CreateVolume;
    }

    /* set up the vldb entry for this volume */
    strncpy(entry.name, volumeName, VOLSER_OLDMAXVOLNAME);
    entry.nServers = 1;
    entry.serverNumber[0] = ntohl(rx_HostOf(rx_PeerOf(server)));
    entry.serverPartition[0] = partition;
    entry.flags = RW_EXISTS;
    entry.serverFlags[0] = ITSRWVOL;
    entry.volumeId[RWVOL] = *volumeId;
    entry.volumeId[ROVOL] = *volumeId + 1;
    entry.volumeId[BACKVOL] = *volumeId + 2;
    entry.cloneId = 0;

    if (!VLDB_CreateEntry(cellHandle, &entry, &tst)) {
	AFSVolDeleteVolume(server, tid);
	goto fail_UV_CreateVolume;
    }

    tst = AFSVolEndTrans(server, tid, &rcode);
    tid = 0;
    if (tst) {
	goto fail_UV_CreateVolume;
    }
    rc = 1;

  fail_UV_CreateVolume:

    if (tid != 0) {
	AFSVolEndTrans(server, tid, &rcode);
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}


/* Delete the volume <volid>on <aserver> <apart>
 * the physical entry gets removed from the vldb only if the ref count 
 * becomes zero
 */
int
UV_DeleteVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
		unsigned int partition, afs_uint32 volumeId,
		afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t temp = 0;
    int serverAddr = ntohl(rx_HostOf(rx_PeerOf(server)));

    afs_int32 ttid = 0;
    afs_int32 rcode;
    struct nvldbentry entry;
    int islocked = 0;
    afs_int32 avoltype = -1, vtype;
    int notondisk = 0, notinvldb = 0;

    /* Find and read the VLDB entry for this volume */
    tst =
	ubik_VL_SetLock(cellHandle->vos, 0, volumeId, avoltype,
		  VLOP_DELETE);
    if (tst) {
	if (tst != VL_NOENT) {
	    goto fail_UV_DeleteVolume;
	}
	notinvldb = 1;
    } else {
	islocked = 1;

	if (!aVLDB_GetEntryByID(cellHandle, volumeId, avoltype, &entry, &tst)) {
	    goto fail_UV_DeleteVolume;
	}

    }

    /* Whether volume is in the VLDB or not. Delete the volume on disk */
    tst = AFSVolTransCreate(server, volumeId, partition, ITOffline, &ttid);
    if (tst) {
	if (tst == VNOVOL) {
	    notondisk = 1;
	} else {
	    goto fail_UV_DeleteVolume;
	}
    } else {
	tst = AFSVolDeleteVolume(server, ttid);
	if (tst) {
	    goto fail_UV_DeleteVolume;
	}
	tst = AFSVolEndTrans(server, ttid, &rcode);
	tst = (tst ? tst : rcode);
	ttid = 0;
	if (tst) {
	    goto fail_UV_DeleteVolume;
	}
    }

    if (notinvldb) {
	goto fail_UV_DeleteVolume;
    }

    if (volumeId == entry.volumeId[BACKVOL]) {
	if (!(entry.flags & BACK_EXISTS)
	    || !Lp_Match(cellHandle, &entry, serverAddr, partition, &tst)) {
	    notinvldb = 2;
	    goto fail_UV_DeleteVolume;
	}

	entry.flags &= ~BACK_EXISTS;
	vtype = BACKVOL;
    }

    else if (volumeId == entry.volumeId[ROVOL]) {
	if (!Lp_ROMatch(cellHandle, &entry, serverAddr, partition, &tst)) {
	    notinvldb = 2;
	    goto fail_UV_DeleteVolume;
	}

	Lp_SetROValue(cellHandle, &entry, serverAddr, partition, 0, 0);
	entry.nServers--;
	if (!Lp_ROMatch(cellHandle, &entry, 0, 0, &tst)) {
	    entry.flags &= ~RO_EXISTS;
	}
	vtype = ROVOL;
    }

    else if (volumeId == entry.volumeId[RWVOL]) {
	if (!(entry.flags & RW_EXISTS)
	    || !Lp_Match(cellHandle, &entry, serverAddr, partition, &tst)) {
	    notinvldb = 2;
	    goto fail_UV_DeleteVolume;
	}

	/* Delete backup if it exists */
	tst =
	    AFSVolTransCreate(server, entry.volumeId[BACKVOL], partition,
			      ITOffline, &ttid);
	if (!tst) {
	    tst = AFSVolDeleteVolume(server, ttid);
	    if (tst) {
		goto fail_UV_DeleteVolume;
	    }
	    tst = AFSVolEndTrans(server, ttid, &rcode);
	    ttid = 0;
	    tst = (tst ? tst : rcode);
	    if (tst) {
		goto fail_UV_DeleteVolume;
	    }
	}

	Lp_SetRWValue(cellHandle, &entry, serverAddr, partition, 0L, 0L);
	entry.nServers--;
	entry.flags &= ~(BACK_EXISTS | RW_EXISTS);
	vtype = RWVOL;

    }

    else {
	notinvldb = 2;		/* Not found on this server and partition */
	goto fail_UV_DeleteVolume;
    }

    if ((entry.nServers <= 0) || !(entry.flags & (RO_EXISTS | RW_EXISTS))) {
	tst = ubik_VL_DeleteEntry(cellHandle->vos, 0, volumeId, vtype);
	if (tst) {
	    goto fail_UV_DeleteVolume;
	}
    } else {
	if (!VLDB_ReplaceEntry
	    (cellHandle, volumeId, vtype, &entry,
	     (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
	    goto fail_UV_DeleteVolume;
	}
    }
    islocked = 0;
    rc = 1;

  fail_UV_DeleteVolume:

    if (notondisk && notinvldb) {
	if (!tst)
	    tst = ADMVOSVOLUMENOEXIST;
    }

    if (ttid) {
	temp = AFSVolEndTrans(server, ttid, &rcode);
	temp = (temp ? temp : rcode);
	if (temp) {
	    if (!tst)
		tst = temp;
	}
    }

    if (islocked) {
	temp =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, volumeId, -1,
		      (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
	if (temp) {
	    if (!tst)
		tst = temp;
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

#define ONERR(ec, es, ep) if (ec) { fprintf(STDERR, (es), (ep)); error = (ec); goto mfail; }

/* Move volume <afromvol> on <afromserver> <afrompart> to <atoserver>
 * <atopart>. The operation is almost idempotent 
 */

int
UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
	      afs_int32 afromserver, afs_int32 afrompart, afs_int32 atoserver,
	      afs_int32 atopart, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t etst = 0;
    struct rx_connection *toconn, *fromconn;
    afs_int32 fromtid, totid, clonetid;
    char vname[64];
    char *volName = 0;
    char tmpName[VOLSER_MAXVOLNAME + 1];
    afs_int32 rcode;
    afs_int32 fromDate;
    struct restoreCookie cookie;
    afs_uint32 newVol, volid, backupId;
    struct volser_status tstatus;
    struct destServer destination;

    struct nvldbentry entry;
    int islocked;
    int same;
    afs_int32 store_flags;

#ifdef	ENABLE_BUGFIX_1165
    volEntries volumeInfo;
    struct volintInfo *infop = 0;
#endif

    islocked = 0;
    fromconn = (struct rx_connection *)0;
    toconn = (struct rx_connection *)0;
    fromtid = 0;
    totid = 0;
    clonetid = 0;
    volid = 0;
    backupId = 0;
    newVol = 0;

    if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
	goto fail_UV_MoveVolume;
    }

    if (entry.volumeId[RWVOL] != afromvol) {
	tst = ADMVOSVOLUMEMOVERWONLY;
	goto fail_UV_MoveVolume;
    }

    tst =
	ubik_VL_SetLock(cellHandle->vos, 0, afromvol, RWVOL, VLOP_MOVE);
    if (tst) {
	goto fail_UV_MoveVolume;
    }
    islocked = 1;

    if (!aVLDB_GetEntryByID(cellHandle, afromvol, RWVOL, &entry, &tst)) {
	goto fail_UV_MoveVolume;
    }

    backupId = entry.volumeId[BACKVOL];

    if (!Lp_Match(cellHandle, &entry, afromserver, afrompart, &tst)) {
	/* the from server and partition do not exist in the vldb entry corresponding to volid */
	if (!Lp_Match(cellHandle, &entry, atoserver, atopart, &tst)) {
	    /* the to server and partition do not exist in the vldb entry corresponding to volid */
	    tst =
		ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
			  (LOCKREL_OPCODE | LOCKREL_AFSID |
			   LOCKREL_TIMESTAMP));
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }
	    tst = VOLSERVOLMOVED;
	    goto fail_UV_MoveVolume;
	}

	/* delete the volume afromvol on src_server */
	/* from-info does not exist but to-info does =>
	 * we have already done the move, but the volume
	 * may still be existing physically on from fileserver
	 */
	fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
	fromtid = 0;

	tst =
	    AFSVolTransCreate(fromconn, afromvol, afrompart, ITOffline,
			      &fromtid);
	if (!tst) {		/* volume exists - delete it */
	    tst =
		AFSVolSetFlags(fromconn, fromtid,
			       VTDeleteOnSalvage | VTOutOfService);
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }

	    tst = AFSVolDeleteVolume(fromconn, fromtid);
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }

	    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	    fromtid = 0;
	    if (!tst)
		tst = rcode;
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }
	}

	/*delete the backup volume now */
	fromtid = 0;
	tst =
	    AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
			      &fromtid);
	if (!tst) {		/* backup volume exists - delete it */
	    tst =
		AFSVolSetFlags(fromconn, fromtid,
			       VTDeleteOnSalvage | VTOutOfService);
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }

	    tst = AFSVolDeleteVolume(fromconn, fromtid);
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }

	    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	    fromtid = 0;
	    if (!tst)
		tst = rcode;
	    if (tst) {
		goto fail_UV_MoveVolume;
	    }
	}

	fromtid = 0;
	goto fail_UV_MoveVolume;
    }

    /* From-info matches the vldb info about volid,
     * its ok start the move operation, the backup volume 
     * on the old site is deleted in the process 
     */
    if (afrompart == atopart) {
	if (!VLDB_IsSameAddrs
	    (cellHandle, afromserver, atoserver, &same, &tst)) {
	    goto fail_UV_MoveVolume;
	}
	if (same) {
	    tst = VOLSERVOLMOVED;
	    goto fail_UV_MoveVolume;
	}
    }

    toconn = UV_Bind(cellHandle, atoserver, AFSCONF_VOLUMEPORT);	/* get connections to the servers */
    fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
    fromtid = totid = 0;	/* initialize to uncreated */

    /* ***
     * clone the read/write volume locally.
     * ***/

    tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* Get a clone id */
    newVol = 0;
    tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &newVol);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
    strcpy(vname, "move-clone-temp");
    tst = AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &newVol);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* lookup the name of the volume we just cloned */
    volid = afromvol;
    tst = AFSVolGetName(fromconn, fromtid, &volName);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    rcode = 0;
    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
    fromtid = 0;
    if (!tst)
	tst = rcode;
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* ***
     * Create the destination volume
     * ***/

    tst =
	AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline, &clonetid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }
    tst = AFSVolSetFlags(fromconn, clonetid, VTDeleteOnSalvage | VTOutOfService);	/*redundant */
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* remember time from which we've dumped the volume */
    tst = AFSVolGetStatus(fromconn, clonetid, &tstatus);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    fromDate = tstatus.creationDate - CLOCKSKEW;

#ifdef	ENABLE_BUGFIX_1165
    /*
     * Get the internal volume state from the source volume. We'll use such info (i.e. dayUse)
     * to copy it to the new volume (via AFSSetInfo later on) so that when we move volumes we
     * don't use this information...
     */
    volumeInfo.volEntries_val = (volintInfo *) 0;	/*this hints the stub to allocate space */
    volumeInfo.volEntries_len = 0;
    tst = AFSVolListOneVolume(fromconn, afrompart, afromvol, &volumeInfo);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    infop = (volintInfo *) volumeInfo.volEntries_val;
    infop->maxquota = -1;	/* Else it will replace the default quota */
#endif

    /* create a volume on the target machine */
    volid = afromvol;
    tst = AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
    if (!tst) {			/*delete the existing volume */

	tst = AFSVolDeleteVolume(toconn, totid);
	if (tst) {
	    goto fail_UV_MoveVolume;
	}

	tst = AFSVolEndTrans(toconn, totid, &rcode);
	totid = 0;
	if (!tst)
	    tst = rcode;
	if (tst) {
	    goto fail_UV_MoveVolume;
	}

    }

    tst =
	AFSVolCreateVolume(toconn, atopart, volName, volser_RW, volid, &volid,
			   &totid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    strncpy(tmpName, volName, VOLSER_OLDMAXVOLNAME);
    free(volName);
    volName = NULL;

    tst = AFSVolSetFlags(toconn, totid, (VTDeleteOnSalvage | VTOutOfService));
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /***
     * Now dump the clone to the new volume
     ***/

    destination.destHost = atoserver;
    destination.destPort = AFSCONF_VOLUMEPORT;
    destination.destSSID = 1;

    /* Copy the clone to the new volume */
    strncpy(cookie.name, tmpName, VOLSER_OLDMAXVOLNAME);
    cookie.type = RWVOL;
    cookie.parent = entry.volumeId[RWVOL];
    cookie.clone = 0;
    tst = AFSVolForward(fromconn, clonetid, 0, &destination, totid, &cookie);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
    if (!tst)
	tst = rcode;
    clonetid = 0;
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* ***
     * reattach to the main-line volume, and incrementally dump it.
     * ***/

    tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* now do the incremental */
    tst =
	AFSVolForward(fromconn, fromtid, fromDate, &destination, totid,
		      &cookie);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* now adjust the flags so that the new volume becomes official */
    tst = AFSVolSetFlags(fromconn, fromtid, VTOutOfService);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    tst = AFSVolSetFlags(toconn, totid, 0);
    if (tst) {
	goto fail_UV_MoveVolume;
    }
#ifdef	ENABLE_BUGFIX_1165
    tst = AFSVolSetInfo(toconn, totid, infop);
    if (tst) {
	goto fail_UV_MoveVolume;
    }
#endif

    /* put new volume online */
    tst = AFSVolEndTrans(toconn, totid, &rcode);
    totid = 0;
    if (!tst)
	tst = rcode;
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    Lp_SetRWValue(cellHandle, &entry, afromserver, afrompart, atoserver,
		  atopart);
    store_flags = entry.flags;
    entry.flags &= ~BACK_EXISTS;

    if (!VLDB_ReplaceEntry
	(cellHandle, afromvol, -1, &entry,
	 (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
	goto fail_UV_MoveVolume;
    }
    entry.flags = store_flags;
    islocked = 0;

    if (atoserver != afromserver) {
	/* set forwarding pointer for moved volumes */
	tst = AFSVolSetForwarding(fromconn, fromtid, htonl(atoserver));
	if (tst) {
	    goto fail_UV_MoveVolume;
	}
    }

    tst = AFSVolDeleteVolume(fromconn, fromtid);	/* zap original volume */
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
    fromtid = 0;
    if (!tst)
	tst = rcode;
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* Delete the backup volume on the original site */
    tst =
	AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline, &fromtid);
    if (!tst) {
	tst =
	    AFSVolSetFlags(fromconn, fromtid,
			   VTDeleteOnSalvage | VTOutOfService);
	if (tst) {
	    goto fail_UV_MoveVolume;
	}

	tst = AFSVolDeleteVolume(fromconn, fromtid);
	if (tst) {
	    goto fail_UV_MoveVolume;
	}

	tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	fromtid = 0;
	if (!tst)
	    tst = rcode;
	if (tst) {
	    goto fail_UV_MoveVolume;
	}

    } else
	tst = 0;		/* no backup volume? that's okay */

    fromtid = 0;

    tst =
	AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline, &clonetid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* now delete the clone */

    tst = AFSVolDeleteVolume(fromconn, clonetid);
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
    if (!tst)
	tst = rcode;
    clonetid = 0;
    if (tst) {
	goto fail_UV_MoveVolume;
    }

    /* fall through */
    /* END OF MOVE */

    /* normal cleanup code */

    if (islocked) {
	etst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
		      (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }

    if (fromtid) {
	etst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	if (etst || rcode) {
	    if (!tst)
		tst = (etst ? etst : rcode);
	}
    }

    if (clonetid) {
	etst = AFSVolEndTrans(fromconn, clonetid, &rcode);
	if (etst || rcode) {
	    if (!tst)
		tst = (etst ? etst : rcode);
	}
    }

    if (totid) {
	etst = AFSVolEndTrans(toconn, totid, &rcode);
	if (etst) {
	    if (!tst)
		tst = (etst ? etst : rcode);
	}
    }
    if (volName)
	free(volName);
#ifdef	ENABLE_BUGFIX_1165
    if (infop)
	free(infop);
#endif
    if (fromconn)
	rx_ReleaseCachedConnection(fromconn);
    if (toconn)
	rx_ReleaseCachedConnection(toconn);

    rc = 1;
    if (st != NULL) {
	*st = tst;
    }
    return rc;

    /* come here only when the sky falls */

  fail_UV_MoveVolume:

    /* unlock VLDB entry */
    if (islocked)
	ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
		  (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));

    if (clonetid)
	AFSVolEndTrans(fromconn, clonetid, &rcode);
    if (totid)
	AFSVolEndTrans(toconn, totid, &rcode);
    if (fromtid) {		/* put it on-line */
	AFSVolSetFlags(fromconn, fromtid, 0);
	AFSVolEndTrans(fromconn, fromtid, &rcode);
    }

    if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
	goto done;
    }

    /* Delete either the volume on the source location or the target location. 
     * If the vldb entry still points to the source location, then we know the
     * volume move didn't finish so we remove the volume from the target 
     * location. Otherwise, we remove the volume from the source location.
     */
    if (Lp_Match(cellHandle, &entry, afromserver, afrompart, &tst)) {	/* didn't move - delete target volume */

	if (volid && toconn) {
	    tst =
		AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
	    if (!tst) {
		AFSVolSetFlags(toconn, totid,
			       VTDeleteOnSalvage | VTOutOfService);
		AFSVolDeleteVolume(toconn, totid);
		AFSVolEndTrans(toconn, totid, &rcode);
	    }
	}

	/* put source volume on-line */
	if (fromconn) {
	    tst =
		AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
				  &fromtid);
	    if (!tst) {
		AFSVolSetFlags(fromconn, fromtid, 0);
		AFSVolEndTrans(fromconn, fromtid, &rcode);
	    }
	}
    } else {			/* yep, move complete */
	/* delete backup volume */
	if (fromconn) {
	    tst =
		AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
				  &fromtid);
	    if (!tst) {
		AFSVolSetFlags(fromconn, fromtid,
			       VTDeleteOnSalvage | VTOutOfService);
		AFSVolDeleteVolume(fromconn, fromtid);
		AFSVolEndTrans(fromconn, fromtid, &rcode);
	    }

	    /* delete source volume */
	    tst =
		AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
				  &fromtid);
	    if (!tst) {
		AFSVolSetFlags(fromconn, fromtid,
			       VTDeleteOnSalvage | VTOutOfService);
		if (atoserver != afromserver)
		    AFSVolSetForwarding(fromconn, fromtid, htonl(atoserver));
		AFSVolDeleteVolume(fromconn, fromtid);
		AFSVolEndTrans(fromconn, fromtid, &rcode);
	    }
	}
    }

    /* common cleanup - delete local clone */
    if (newVol) {
	tst =
	    AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline,
			      &clonetid);
	if (!tst) {
	    AFSVolDeleteVolume(fromconn, clonetid);
	    AFSVolEndTrans(fromconn, clonetid, &rcode);
	}
    }

    /* unlock VLDB entry */
    ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
	      (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));

  done:			/* routine cleanup */
    if (volName)
	free(volName);
#ifdef	ENABLE_BUGFIX_1165
    if (infop)
	free(infop);
#endif
    if (fromconn)
	rx_ReleaseCachedConnection(fromconn);
    if (toconn)
	rx_ReleaseCachedConnection(toconn);

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/* Make a new backup of volume <avolid> on <aserver> and <apart> 
 * if one already exists, update it 
 */

int
UV_BackupVolume(afs_cell_handle_p cellHandle, afs_int32 aserver,
		afs_int32 apart, afs_uint32 avolid, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0, temp = 0;
    afs_int32 ttid = 0, btid = 0;
    afs_uint32 backupID;
    afs_int32 rcode = 0;
    char vname[VOLSER_MAXVOLNAME + 1];
    struct nvldbentry entry;
    int vldblocked = 0, vldbmod = 0, backexists = 1;
    struct rx_connection *aconn = UV_Bind(cellHandle, aserver,
					  AFSCONF_VOLUMEPORT);


    /* the calls to VLDB will succeed only if avolid is a RW volume,
     * since we are following the RW hash chain for searching */
    if (!aVLDB_GetEntryByID(cellHandle, avolid, RWVOL, &entry, &tst)) {
	goto fail_UV_BackupVolume;
    }

    /* These operations require the VLDB be locked since it means the VLDB
     * will change or the vldb is already locked.
     */
    if (!(entry.flags & BACK_EXISTS) ||	/* backup volume doesnt exist */
	(entry.flags & VLOP_ALLOPERS) ||	/* vldb lock already held */
	(entry.volumeId[BACKVOL] == INVALID_BID)) {
	/* no assigned backup volume id */

	tst =
	    ubik_VL_SetLock(cellHandle->vos, 0, avolid, RWVOL,
		      VLOP_BACKUP);
	if (tst) {
	    goto fail_UV_BackupVolume;
	}
	vldblocked = 1;

	/* Reread the vldb entry */
	if (!aVLDB_GetEntryByID(cellHandle, avolid, RWVOL, &entry, &tst)) {
	    goto fail_UV_BackupVolume;
	}
    }

    if (!ISNAMEVALID(entry.name)) {
	tst = VOLSERBADNAME;
	goto fail_UV_BackupVolume;
    }

    backupID = entry.volumeId[BACKVOL];
    if (backupID == INVALID_BID) {
	/* Get a backup volume id from the VLDB and update the vldb
	 * entry with it. 
	 */
	tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &backupID);
	if (tst) {
	    goto fail_UV_BackupVolume;
	}
	entry.volumeId[BACKVOL] = backupID;
	vldbmod = 1;
    }

    /* Test to see if the backup volume exists by trying to create
     * a transaction on the backup volume. We've assumed the backup exists.
     */
    tst = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
    if (tst) {
	if (tst != VNOVOL) {
	    goto fail_UV_BackupVolume;
	}
	backexists = 0;		/* backup volume does not exist */
    }
    if (btid) {
	tst = AFSVolEndTrans(aconn, btid, &rcode);
	btid = 0;
	if (tst || rcode) {
	    tst = (tst ? tst : rcode);
	    goto fail_UV_BackupVolume;
	}
    }

    /* Now go ahead and try to clone the RW volume.
     * First start a transaction on the RW volume 
     */
    tst = AFSVolTransCreate(aconn, avolid, apart, ITBusy, &ttid);
    if (tst) {
	goto fail_UV_BackupVolume;
    }

    /* Clone or reclone the volume, depending on whether the backup 
     * volume exists or not
     */
    if (backexists) {
	tst = AFSVolReClone(aconn, ttid, backupID);
	if (tst) {
	    goto fail_UV_BackupVolume;
	}
    } else {
	strcpy(vname, entry.name);
	strcat(vname, ".backup");

	tst = AFSVolClone(aconn, ttid, 0, backupVolume, vname, &backupID);
	if (tst) {
	    goto fail_UV_BackupVolume;
	}
    }

    /* End transaction on the RW volume */
    tst = AFSVolEndTrans(aconn, ttid, &rcode);
    ttid = 0;
    if (tst || rcode) {
	tst = (tst ? tst : rcode);
	goto fail_UV_BackupVolume;
    }

    /* Mork vldb as backup exists */
    if (!(entry.flags & BACK_EXISTS)) {
	entry.flags |= BACK_EXISTS;
	vldbmod = 1;
    }

    /* Now go back to the backup volume and bring it on line */
    tst = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
    if (tst) {
	goto fail_UV_BackupVolume;
    }

    tst = AFSVolSetFlags(aconn, btid, 0);
    if (tst) {
	goto fail_UV_BackupVolume;
    }

    tst = AFSVolEndTrans(aconn, btid, &rcode);
    btid = 0;
    if (tst || rcode) {
	tst = (tst ? tst : rcode);
	goto fail_UV_BackupVolume;
    }
    rc = 1;

    /* Will update the vldb below */

  fail_UV_BackupVolume:

    if (ttid) {
	temp = AFSVolEndTrans(aconn, ttid, &rcode);
	if (temp || rcode) {
	    if (!tst)
		tst = (temp ? temp : rcode);
	}
    }

    if (btid) {
	temp = AFSVolEndTrans(aconn, btid, &rcode);
	if (temp || rcode) {
	    if (!tst)
		tst = (temp ? temp : rcode);
	}
    }

    /* Now update the vldb - if modified */
    if (vldblocked) {
	if (vldbmod) {
	    if (!VLDB_ReplaceEntry
		(cellHandle, avolid, RWVOL, &entry,
		 (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP),
		 &temp)) {
		if (!tst) {
		    tst = temp;
		}
	    }
	} else {
	    temp =
		ubik_VL_ReleaseLock(cellHandle->vos, 0, avolid, RWVOL,
			  (LOCKREL_OPCODE | LOCKREL_AFSID |
			   LOCKREL_TIMESTAMP));
	    if (temp) {
		if (!tst) {
		    tst = temp;
		}
	    }
	}
    }

    if (aconn) {
	rx_ReleaseCachedConnection(aconn);
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

static int
DelVol(struct rx_connection *conn, afs_uint32 vid, afs_int32 part,
       afs_int32 flags)
{
    afs_int32 acode, ccode, rcode, tid;
    ccode = rcode = tid = 0;

    acode = AFSVolTransCreate(conn, vid, part, flags, &tid);
    if (!acode) {		/* It really was there */
	acode = AFSVolDeleteVolume(conn, tid);
	ccode = AFSVolEndTrans(conn, tid, &rcode);
	if (!ccode)
	    ccode = rcode;
    }

    return acode;
}

#define ONERROR(ec, ep, es) if (ec) { fprintf(STDERR, (es), (ep)); error = (ec); goto rfail; }
#define ERROREXIT(ec) { error = (ec); goto rfail; }

#if 0				/* doesn't appear to be used, why compile it */
static int
CloneVol(afs_cell_handle_p cellHandle, struct rx_connection *conn,
	 afs_uint32 rwvid, afs_int32 part, afs_uint32 * rovidp, int nottemp,
	 struct nvldbentry *entry, afs_int32 * vidCreateDate, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0, etst = 0;
    afs_int32 rcode = 0, tid = 0;
    struct volser_status volstatus;
    char vname[64];

    /* Begin transaction on RW volume marking it busy (clients will wait) */
    tst = AFSVolTransCreate(conn, rwvid, part, ITBusy, &tid);
    if (tst) {
	goto fail_CloneVol;
    }

    /* Get the RO volume id. Allocate a new one if need to */
    *rovidp = entry->volumeId[ROVOL];
    if (*rovidp == INVALID_BID) {
	tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, rovidp);
	if (tst) {
	    goto fail_CloneVol;
	}

	entry->volumeId[ROVOL] = *rovidp;
    }

    /* If we are creating the ro clone, what are we calling it.
     * Depends on whether its a temporary clone or not.
     */
    if (nottemp) {
	strcpy(vname, entry->name);
	strcat(vname, ".readonly");
    } else {
	strcpy(vname, "readonly-clone-temp");	/* Should be unique? */
    }

    /* Create the new clone. If it exists, then reclone it */
    tst = AFSVolClone(conn, tid, 0, readonlyVolume, vname, rovidp);
    if (tst == VVOLEXISTS) {
	tst = AFSVolReClone(conn, tid, *rovidp);
	if (tst) {
	    goto fail_CloneVol;
	}
    }
    if (tst) {
	goto fail_CloneVol;
    }

    /* Bring the volume back on-line as soon as possible */
    if (nottemp) {
	afs_int32 fromtid = 0;

	/* Now bring the RO clone on-line */
	tst = AFSVolTransCreate(conn, *rovidp, part, ITOffline, &fromtid);
	if (tst) {
	    goto fail_CloneVol;
	}

	tst = AFSVolSetFlags(conn, fromtid, 0);
	if (tst) {
	    goto fail_CloneVol;
	}

	tst = AFSVolEndTrans(conn, fromtid, &rcode);
	fromtid = 0;
	if (!tst)
	    tst = rcode;
	if (tst) {
	    goto fail_CloneVol;
	}
    }

    /* Get the time the RW was created for return information */
    tst = AFSVolGetStatus(conn, tid, &volstatus);
    if (tst) {
	goto fail_CloneVol;
    }
    *vidCreateDate = volstatus.creationDate;
    rc = 1;

  fail_CloneVol:

    if (tid) {
	tst = AFSVolEndTrans(conn, tid, &rcode);
	tid = 0;
	if (!tst)
	    tst = rcode;
	if (tst) {
	    rc = 0;
	    goto fail_CloneVol;
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}
#endif

/* Get a "transaction" on this replica.  Create the volume 
 * if necessary.  Return the time from which a dump should
 * be made (0 if it's a new volume)
 */
static int
GetTrans(afs_cell_handle_p cellHandle, struct nvldbentry *vldbEntryPtr,
	 afs_int32 index, struct rx_connection **connPtr,
	 afs_int32 * transPtr, afs_int32 * timePtr, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0, etst = 0;
    afs_uint32 volid;
    struct volser_status tstatus;
    int rcode;

    *connPtr = (struct rx_connection *)0;
    *timePtr = 0;
    *transPtr = 0;

    /* get connection to the replication site */
    *connPtr =
	UV_Bind(cellHandle, vldbEntryPtr->serverNumber[index],
		AFSCONF_VOLUMEPORT);
    if (!*connPtr) {
	/* server is down */
	tst = -1;
	goto fail_GetTrans;
    }

    volid = vldbEntryPtr->volumeId[ROVOL];
    if (volid) {
	tst =
	    AFSVolTransCreate(*connPtr, volid,
			      vldbEntryPtr->serverPartition[index], ITOffline,
			      transPtr);
    }

    /* If the volume does not exist, create it */
    if (!volid || tst) {
	char volname[64];

	if (volid && (tst != VNOVOL)) {
	    goto fail_GetTrans;
	}

	strcpy(volname, vldbEntryPtr->name);
	strcat(volname, ".readonly");

	tst =
	    AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
			       volname, volser_RO,
			       vldbEntryPtr->volumeId[RWVOL], &volid,
			       transPtr);
	if (tst) {
	    goto fail_GetTrans;
	}
	vldbEntryPtr->volumeId[ROVOL] = volid;

	/* The following is a bit redundant, since create sets these flags by default */
	tst =
	    AFSVolSetFlags(*connPtr, *transPtr,
			   VTDeleteOnSalvage | VTOutOfService);
	if (tst) {
	    goto fail_GetTrans;
	}
    }

    /* Otherwise, the transaction did succeed, so get the creation date of the
     * latest RO volume on the replication site 
     */
    else {
	tst = AFSVolGetStatus(*connPtr, *transPtr, &tstatus);
	if (tst) {
	    goto fail_GetTrans;
	}
	*timePtr = tstatus.creationDate - CLOCKSKEW;
    }
    rc = 1;

  fail_GetTrans:

    if ((rc == 0) && (*transPtr)) {
	etst = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
	*transPtr = 0;
	if (!etst)
	    etst = rcode;
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

static int
SimulateForwardMultiple(struct rx_connection *fromconn, afs_int32 fromtid,
			afs_int32 fromdate, manyDests * tr, afs_int32 flags,
			void *cookie, manyResults * results)
{
    int i;

    for (i = 0; i < tr->manyDests_len; i++) {
	results->manyResults_val[i] =
	    AFSVolForward(fromconn, fromtid, fromdate,
			  &(tr->manyDests_val[i].server),
			  tr->manyDests_val[i].trans, cookie);
    }
    return 0;
}


/* VolumeExists()
 *      Determine if a volume exists on a server and partition.
 *      Try creating a transaction on the volume. If we can,
 *      the volume exists, if not, then return the error code.
 *      Some error codes mean the volume is unavailable but
 *      still exists - so we catch these error codes.
 */
static afs_int32
VolumeExists(afs_cell_handle_p cellHandle, afs_int32 server,
	     afs_int32 partition, afs_int32 volumeid, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    struct rx_connection *conn = (struct rx_connection *)0;
    volEntries volumeInfo;

    conn = UV_Bind(cellHandle, server, AFSCONF_VOLUMEPORT);
    if (conn) {
	volumeInfo.volEntries_val = (volintInfo *) 0;
	volumeInfo.volEntries_len = 0;
	tst = AFSVolListOneVolume(conn, partition, volumeid, &volumeInfo);
	if (volumeInfo.volEntries_val)
	    free(volumeInfo.volEntries_val);
	if (tst == VOLSERILLEGAL_PARTITION) {
	    tst = ENODEV;
	}
	rx_ReleaseCachedConnection(conn);
    }
    rc = 1;

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/* release volume <afromvol> on <afromserver> <afrompart> to all the
 * sites if forceflag is 1.If its 0 complete the release if the previous
 * release aborted else start a new release */
int
UV_ReleaseVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
		 afs_int32 afromserver, afs_int32 afrompart, int forceflag,
		 afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0, etst = 0;

    char vname[64];
    afs_int32 rcode;
    afs_uint32 cloneVolId, roVolId;
    struct replica *replicas = 0;
    struct nvldbentry entry;
    int i, volcount, m, fullrelease, vldbindex;
    int failure;
    struct restoreCookie cookie;
    struct rx_connection **toconns = 0;
    struct release *times = 0;
    int nservers = 0;
    struct rx_connection *fromconn = (struct rx_connection *)0;
    int islocked = 0;
    afs_int32 clonetid = 0, onlinetid;
    afs_int32 fromtid = 0;
    afs_uint32 fromdate = 0, thisdate;
    int s;
    manyDests tr;
    manyResults results;
    int rwindex, roindex, roclone, roexists;
    afs_int32 rwcrdate = 0;
    struct rtime {
	int validtime;
	afs_uint32 time;
    } remembertime[NMAXNSERVERS];
    int releasecount = 0;
    struct volser_status volstatus;

    memset(remembertime, 0, sizeof(remembertime));
    memset(&results, 0, sizeof(results));

    tst =
	ubik_VL_SetLock(cellHandle->vos, 0, afromvol, RWVOL,
		  VLOP_RELEASE);
    if ((tst) && (tst != VL_RERELEASE)) {
	goto fail_UV_ReleaseVolume;
    }
    islocked = 1;

    /* Get the vldb entry in readable format */
    if (!aVLDB_GetEntryByID(cellHandle, afromvol, RWVOL, &entry, &tst)) {
	goto fail_UV_ReleaseVolume;
    }

    if (!ISNAMEVALID(entry.name)) {
	tst = VOLSERBADNAME;
	goto fail_UV_ReleaseVolume;
    }

    if (entry.volumeId[RWVOL] != afromvol) {
	tst = ADMVOSVOLUMERELEASERWONLY;
	goto fail_UV_ReleaseVolume;
    }

    if (entry.nServers <= 1) {
	tst = ADMVOSVOLUMENOREPLICAS;
	goto fail_UV_ReleaseVolume;
    }

    if (strlen(entry.name) > (VOLSER_OLDMAXVOLNAME - 10)) {
	tst = VOLSERBADNAME;
	goto fail_UV_ReleaseVolume;
    }

    /* roclone is true if one of the RO volumes is on the same
     * partition as the RW volume. In this case, we make the RO volume
     * on the same partition a clone instead of a complete copy.
     */

    roindex =
	Lp_ROMatch(cellHandle, &entry, afromserver, afrompart, &tst) - 1;
    roclone = ((roindex == -1) ? 0 : 1);
    rwindex = Lp_GetRwIndex(cellHandle, &entry, 0);
    if (rwindex < 0) {
	tst = VOLSERNOVOL;
	goto fail_UV_ReleaseVolume;
    }

    /* Make sure we have a RO volume id to work with */
    if (entry.volumeId[ROVOL] == INVALID_BID) {
	/* need to get a new RO volume id */
	tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &roVolId);
	if (tst) {
	    goto fail_UV_ReleaseVolume;
	}

	entry.volumeId[ROVOL] = roVolId;
	if (!VLDB_ReplaceEntry(cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
	    goto fail_UV_ReleaseVolume;
	}
    }

    /* Will we be completing a previously unfinished release. -force overrides */
    for (fullrelease = 1, i = 0; (fullrelease && (i < entry.nServers)); i++) {
	if (entry.serverFlags[i] & NEW_REPSITE)
	    fullrelease = 0;
    }
    if (forceflag && !fullrelease)
	fullrelease = 1;

    /* Determine which volume id to use and see if it exists */
    cloneVolId =
	((fullrelease
	  || (entry.cloneId == 0)) ? entry.volumeId[ROVOL] : entry.cloneId);
    VolumeExists(cellHandle, afromserver, afrompart, cloneVolId, &tst);
    roexists = ((tst == ENODEV) ? 0 : 1);
    if (!roexists && !fullrelease)
	fullrelease = 1;	/* Do a full release if RO clone does not exist */

    fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
    if (!fromconn) {
	tst = -1;
	goto fail_UV_ReleaseVolume;
    }

    if (fullrelease) {
	/* If the RO clone exists, then if the clone is a temporary
	 * clone, delete it. Or if the RO clone is marked RO_DONTUSE
	 * (it was recently added), then also delete it. We do not
	 * want to "reclone" a temporary RO clone.
	 */
	if (roexists
	    && (!roclone || (entry.serverFlags[roindex] & RO_DONTUSE))) {
	    tst = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
	    if (tst && (tst != VNOVOL)) {
		goto fail_UV_ReleaseVolume;
	    }
	    roexists = 0;
	}

	/* Mark all the ROs in the VLDB entry as RO_DONTUSE. We don't
	 * write this entry out to the vlserver until after the first
	 * RO volume is released (temp RO clones don't count).
	 */
	for (i = 0; i < entry.nServers; i++) {
	    entry.serverFlags[i] &= ~NEW_REPSITE;
	    entry.serverFlags[i] |= RO_DONTUSE;
	}
	entry.serverFlags[rwindex] |= NEW_REPSITE;
	entry.serverFlags[rwindex] &= ~RO_DONTUSE;

	/* Begin transaction on RW and mark it busy while we clone it */
	tst =
	    AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
			      &clonetid);
	if (tst) {
	    goto fail_UV_ReleaseVolume;
	}

	/* Clone or reclone the volume */
	if (roexists) {
	    tst = AFSVolReClone(fromconn, clonetid, cloneVolId);
	    if (tst) {
		goto fail_UV_ReleaseVolume;
	    }
	} else {
	    if (roclone) {
		strcpy(vname, entry.name);
		strcat(vname, ".readonly");
	    } else {
		strcpy(vname, "readonly-clone-temp");
	    }
	    tst =
		AFSVolClone(fromconn, clonetid, 0, readonlyVolume, vname,
			    &cloneVolId);
	    if (tst) {
		goto fail_UV_ReleaseVolume;
	    }
	}

	/* Get the time the RW was created for future information */
	tst = AFSVolGetStatus(fromconn, clonetid, &volstatus);
	if (tst) {
	    goto fail_UV_ReleaseVolume;
	}
	rwcrdate = volstatus.creationDate;

	/* End the transaction on the RW volume */
	tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
	clonetid = 0;
	tst = (tst ? tst : rcode);
	if (tst) {
	    goto fail_UV_ReleaseVolume;
	}

	/* Remember clone volume ID in case we fail or are interrupted */
	entry.cloneId = cloneVolId;

	if (roclone) {
	    /* Bring the RO clone online - though not if it's a temporary clone */
	    tst =
		AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITOffline,
				  &onlinetid);
	    if (tst) {
		goto fail_UV_ReleaseVolume;
	    }

	    etst = AFSVolSetFlags(fromconn, onlinetid, 0);

	    tst = AFSVolEndTrans(fromconn, onlinetid, &rcode);
	    tst = (tst ? tst : rcode);
	    if (tst) {
		goto fail_UV_ReleaseVolume;
	    }
	    if (etst) {
		tst = etst;
		goto fail_UV_ReleaseVolume;
	    }

	    /* Sleep so that a client searching for an online volume won't
	     * find the clone offline and then the next RO offline while the 
	     * release brings the clone online and the next RO offline (race).
	     * There is a fix in the 3.4 client that does not need this sleep
	     * anymore, but we don't know what clients we have.
	     */
	    if (entry.nServers > 2)
		sleep(5);

	    /* Mark the RO clone in the VLDB as a good site (already released) */
	    entry.serverFlags[roindex] |= NEW_REPSITE;
	    entry.serverFlags[roindex] &= ~RO_DONTUSE;
	    entry.flags |= RO_EXISTS;

	    releasecount++;

	    /* Write out the VLDB entry only if the clone is not a temporary
	     * clone. If we did this to a temporary clone then we would end
	     * up marking all the ROs as "old release" making the ROs
	     * temporarily unavailable.
	     */
	    if (!VLDB_ReplaceEntry
		(cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
		goto fail_UV_ReleaseVolume;
	    }
	}
    }

    /* Now we will release from the clone to the remaining RO replicas.
     * The first 2 ROs (counting the non-temporary RO clone) are released
     * individually: releasecount. This is to reduce the race condition
     * of clients trying to find an on-line RO volume. The remaining ROs
     * are released in parallel but no more than half the number of ROs
     * (rounded up) at a time: nservers.
     */

    strcpy(vname, entry.name);
    strcat(vname, ".readonly");
    memset(&cookie, 0, sizeof(cookie));
    strncpy(cookie.name, vname, VOLSER_OLDMAXVOLNAME);
    cookie.type = ROVOL;
    cookie.parent = entry.volumeId[RWVOL];
    cookie.clone = 0;

    nservers = entry.nServers / 2;	/* how many to do at once, excluding clone */
    replicas =
	(struct replica *)malloc(sizeof(struct replica) * nservers + 1);
    times = (struct release *)malloc(sizeof(struct release) * nservers + 1);
    toconns =
	(struct rx_connection **)malloc(sizeof(struct rx_connection *) *
					nservers + 1);
    results.manyResults_val =
	(afs_int32 *) malloc(sizeof(afs_int32) * nservers + 1);
    if (!replicas || !times || !!!results.manyResults_val || !toconns) {
	tst = ADMNOMEM;
	goto fail_UV_ReleaseVolume;
    }

    memset(replicas, 0, (sizeof(struct replica) * nservers + 1));
    memset(times, 0, (sizeof(struct release) * nservers + 1));
    memset(toconns, 0, (sizeof(struct rx_connection *) * nservers + 1));
    memset(results.manyResults_val, 0, (sizeof(afs_int32) * nservers + 1));

    /* Create a transaction on the cloned volume */
    tst =
	AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
    if (tst) {
	goto fail_UV_ReleaseVolume;
    }

    /* For each index in the VLDB */
    for (vldbindex = 0; vldbindex < entry.nServers;) {

	/* Get a transaction on the replicas. Pick replacas which have an old release. */
	for (volcount = 0;
	     ((volcount < nservers) && (vldbindex < entry.nServers));
	     vldbindex++) {
	    /* The first two RO volumes will be released individually.
	     * The rest are then released in parallel. This is a hack
	     * for clients not recognizing right away when a RO volume
	     * comes back on-line.
	     */
	    if ((volcount == 1) && (releasecount < 2))
		break;

	    if (vldbindex == roindex)
		continue;	/* the clone    */
	    if ((entry.serverFlags[vldbindex] & NEW_REPSITE)
		&& !(entry.serverFlags[vldbindex] & RO_DONTUSE))
		continue;
	    if (!(entry.serverFlags[vldbindex] & ITSROVOL))
		continue;	/* not a RO vol */


	    /* Get a Transaction on this replica. Get a new connection if
	     * necessary.  Create the volume if necessary.  Return the
	     * time from which the dump should be made (0 if it's a new
	     * volume).  Each volume might have a different time. 
	     */
	    replicas[volcount].server.destHost =
		entry.serverNumber[vldbindex];
	    replicas[volcount].server.destPort = AFSCONF_VOLUMEPORT;
	    replicas[volcount].server.destSSID = 1;
	    times[volcount].vldbEntryIndex = vldbindex;

	    if (!GetTrans
		(cellHandle, &entry, vldbindex, &(toconns[volcount]),
		 &(replicas[volcount].trans), &(times[volcount].time),
		 &tst)) {
		continue;
	    }

	    /* Thisdate is the date from which we want to pick up all changes */
	    if (forceflag || !fullrelease
		|| (rwcrdate > times[volcount].time)) {
		/* If the forceflag is set, then we want to do a full dump.
		 * If it's not a full release, we can't be sure that the creation
		 *  date is good (so we also do a full dump).
		 * If the RW volume was replaced (its creation date is newer than
		 *  the last release), then we can't be sure what has changed (so
		 *  we do a full dump).
		 */
		thisdate = 0;
	    } else if (remembertime[vldbindex].validtime) {
		/* Trans was prev ended. Use the time from the prev trans
		 * because, prev trans may have created the volume. In which
		 * case time[volcount].time would be now instead of 0.
		 */
		thisdate =
		    (remembertime[vldbindex].time <
		     times[volcount].time) ? remembertime[vldbindex].
		    time : times[volcount].time;
	    } else {
		thisdate = times[volcount].time;
	    }
	    remembertime[vldbindex].validtime = 1;
	    remembertime[vldbindex].time = thisdate;

	    if (volcount == 0) {
		fromdate = thisdate;
	    } else {
		/* Include this volume if it is within 15 minutes of the earliest */
		if (((fromdate >
		      thisdate) ? (fromdate - thisdate) : (thisdate -
							   fromdate)) > 900) {
		    AFSVolEndTrans(toconns[volcount],
				   replicas[volcount].trans, &rcode);
		    replicas[volcount].trans = 0;
		    break;
		}
		if (thisdate < fromdate)
		    fromdate = thisdate;
	    }
	    volcount++;
	}
	if (!volcount)
	    continue;

	/* Release the ones we have collected */
	tr.manyDests_val = &(replicas[0]);
	tr.manyDests_len = results.manyResults_len = volcount;
	tst =
	    AFSVolForwardMultiple(fromconn, fromtid, fromdate, &tr,
				  0 /*spare */ , &cookie, &results);
	if (tst == RXGEN_OPCODE) {	/* RPC Interface Mismatch */
	    tst =
		SimulateForwardMultiple(fromconn, fromtid, fromdate, &tr,
					0 /*spare */ , &cookie, &results);
	    nservers = 1;
	}

	if (tst) {
	    goto fail_UV_ReleaseVolume;
	} else {
	    for (m = 0; m < volcount; m++) {
		if (results.manyResults_val[m]) {
		    continue;
		}

		tst =
		    AFSVolSetIdsTypes(toconns[m], replicas[m].trans, vname,
				      ROVOL, entry.volumeId[RWVOL], 0, 0);
		if (tst) {
		    continue;
		}

		/* have to clear dest. flags to ensure new vol goes online:
		 * because the restore (forwarded) operation copied
		 * the V_inService(=0) flag over to the destination. 
		 */
		tst = AFSVolSetFlags(toconns[m], replicas[m].trans, 0);
		if (tst) {
		    continue;
		}

		entry.serverFlags[times[m].vldbEntryIndex] |= NEW_REPSITE;
		entry.serverFlags[times[m].vldbEntryIndex] &= ~RO_DONTUSE;
		entry.flags |= RO_EXISTS;
		releasecount++;
	    }
	}

	/* End the transactions and destroy the connections */
	for (s = 0; s < volcount; s++) {
	    if (replicas[s].trans)
		tst = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
	    replicas[s].trans = 0;
	    if (!tst)
		tst = rcode;
	    if (tst) {
		if ((s == 0) || (tst != ENOENT)) {
		} else {
		    if (times[s].vldbEntryIndex < vldbindex)
			vldbindex = times[s].vldbEntryIndex;
		}
	    }

	    if (toconns[s])
		rx_ReleaseCachedConnection(toconns[s]);
	    toconns[s] = 0;
	}

	if (!VLDB_ReplaceEntry(cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
	    goto fail_UV_ReleaseVolume;
	}
    }				/* for each index in the vldb */

    /* End the transaction on the cloned volume */
    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
    fromtid = 0;

    /* Figure out if any volume were not released and say so */
    for (failure = 0, i = 0; i < entry.nServers; i++) {
	if (!(entry.serverFlags[i] & NEW_REPSITE))
	    failure++;
    }
    if (failure) {
	if (!VLDB_ReplaceEntry
	    (cellHandle, afromvol, RWVOL, &entry, LOCKREL_TIMESTAMP, &tst)) {
	    goto fail_UV_ReleaseVolume;
	}

	tst = VOLSERBADRELEASE;
	goto fail_UV_ReleaseVolume;
    }

    /* All the ROs were release successfully. Remove the temporary clone */
    if (!roclone) {
	tst = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
	if (tst) {
	    goto fail_UV_ReleaseVolume;
	}
    }
    entry.cloneId = 0;

    for (i = 0; i < entry.nServers; i++)
	entry.serverFlags[i] &= ~NEW_REPSITE;

    /* Update the VLDB */
    if (!VLDB_ReplaceEntry
	(cellHandle, afromvol, RWVOL, &entry,
	 LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
	goto fail_UV_ReleaseVolume;
    }
    rc = 1;

  fail_UV_ReleaseVolume:

    if (clonetid) {
	tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
	clonetid = 0;
	if (tst) {
	    rc = 0;
	}
    }
    if (fromtid) {
	tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	fromtid = 0;
	if (tst) {
	    rc = 0;
	}
    }
    for (i = 0; i < nservers; i++) {
	if (replicas && replicas[i].trans) {
	    tst = AFSVolEndTrans(toconns[i], replicas[i].trans, &rcode);
	    replicas[i].trans = 0;
	    if (tst) {
		rc = 0;
	    }
	}
	if (toconns && toconns[i]) {
	    rx_ReleaseCachedConnection(toconns[i]);
	    toconns[i] = 0;
	}
    }
    if (islocked) {
	tst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, RWVOL,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
	if (tst) {
	    rc = 0;
	}
    }

    if (fromconn)
	rx_ReleaseCachedConnection(fromconn);
    if (results.manyResults_val)
	free(results.manyResults_val);
    if (replicas)
	free(replicas);
    if (toconns)
	free(toconns);
    if (times)
	free(times);

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

static int
ReceiveFile(int fd, struct rx_call *call,
	    struct stat *status)
{
    char *buffer = (char *)0;
    int blockSize;
    afs_int32 bytesread, nbytes, bytesleft, w;
    fd_set out;
    afs_int32 error = 0;

#ifdef AFS_NT40_ENV
    blockSize = 4096;
#else
    if (fd != 1) {
#ifdef  AFS_AIX_ENV
	struct statfs tstatfs;

/* Unfortunately in AIX valuable fields such as st_blksize are gone from the sta
t structure!! */
	fstatfs(fd, &tstatfs);
	blockSize = tstatfs.f_bsize;
#else
	blockSize = status->st_blksize;
#endif
    } else {
	blockSize = 4096;
    }
#endif
    nbytes = blockSize;
    buffer = (char *)malloc(blockSize);
    if (!buffer) {
	return ADMNOMEM;
    }
    bytesread = 1;
    while (!error && (bytesread > 0)) {
	bytesread = rx_Read(call, buffer, nbytes);
	bytesleft = bytesread;
	while (!error && (bytesleft > 0)) {
	    FD_ZERO(&out);
	    FD_SET(fd, &out);
#ifndef AFS_NT40_ENV		/* NT csn't select on non-socket fd's */
	    select(fd + 1, 0, &out, 0, 0);	/* don't timeout if write bl
						 * ocks */
#endif
	    w = write(fd, &buffer[bytesread - bytesleft], bytesleft);
	    if (w < 0) {
		error = ADMVOSDUMPFILEWRITEFAIL;
	    } else {
		bytesleft -= w;
	    }
	}
    }
    if (buffer)
	free(buffer);
    if (fd != 1)
	if (!error)
	    fstat(fd, status);
    return error;
}


static afs_int32
DumpFunction(struct rx_call *call, const char *filename)
{
    int fd;
    struct stat status;
    afs_int32 error, code;

    error = 0;
    fd = -1;

    fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666);
    if (fd < 0 || fstat(fd, &status) < 0) {
	error = VOLSERBADOP;
	goto dffail;
    }
    code = ReceiveFile(fd, call, &status);
    if (code) {
	error = code;
	goto dffail;
    }
  dffail:
    if (fd >= 0)
	code = close(fd);
    else
	code = 0;
    if (code) {
	if (!error)
	    error = code;
    }
    return error;
}


/*dump the volume <afromvol> on <afromserver> and
* <afrompart> to <afilename> starting from <fromdate>.
* DumpFunction does the real work behind the scenes after
* extracting parameters from the rock  */
int
UV_DumpVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
	      afs_int32 afromserver, afs_int32 afrompart, afs_int32 fromdate,
	      const char *filename, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t etst = 0;
    struct rx_connection *fromconn;
    struct rx_call *fromcall;
    afs_int32 fromtid;
    afs_int32 rxError;
    afs_int32 rcode;

    struct nvldbentry entry;
    int islocked;

    islocked = 0;
    rxError = 0;
    fromcall = (struct rx_call *)0;
    fromconn = (struct rx_connection *)0;
    fromtid = 0;
    fromcall = (struct rx_call *)0;

    islocked = 0;
    if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
	goto fail_UV_DumpVolume;
    }

    /* get connections to the servers */
    fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
    tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
    if (tst) {
	goto fail_UV_DumpVolume;
    }
    fromcall = rx_NewCall(fromconn);
    tst = StartAFSVolDump(fromcall, fromtid, fromdate);
    if (tst) {
	goto fail_UV_DumpVolume;
    }
    if ((tst = DumpFunction(fromcall, filename))) {
	goto fail_UV_DumpVolume;
    }
    tst = rx_EndCall(fromcall, rxError);
    fromcall = (struct rx_call *)0;
    if (tst) {
	goto fail_UV_DumpVolume;
    }
    tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
    fromtid = 0;
    if (!tst)
	tst = rcode;
    if (tst) {
	goto fail_UV_DumpVolume;
    }
    rc = 1;

  fail_UV_DumpVolume:

    if (islocked) {
	etst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }

    if (fromcall) {
	etst = rx_EndCall(fromcall, rxError);
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }

    if (fromtid) {
	etst = AFSVolEndTrans(fromconn, fromtid, &rcode);
	if (!tst)
	    tst = etst;
	if (rcode) {
	    if (!tst)
		tst = rcode;
	}
    }

    if (fromconn) {
	rx_ReleaseCachedConnection(fromconn);
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

int
SendFile(int fd, struct rx_call *call,
	 struct stat *status)
{
    char *buffer = (char *)0;
    int blockSize;
    fd_set in;
    afs_int32 error = 0;
    int done = 0;
    int nbytes;

#ifdef AFS_NT40_ENV
    blockSize = 4096;
#else
    if (fd != 0) {
#ifdef  AFS_AIX_ENV
	struct statfs tstatfs;

/* Unfortunately in AIX valuable fields such as st_blksize are gone from the sta
t structure!! */
	fstatfs(fd, &tstatfs);
	blockSize = tstatfs.f_bsize;
#else
	blockSize = status->st_blksize;
#endif
    } else {
	blockSize = 4096;
    }
#endif
    buffer = (char *)malloc(blockSize);
    if (!buffer) {
	return ADMNOMEM;
    }

    while (!error && !done) {
	FD_ZERO(&in);
	FD_SET(fd, &in);
#ifndef AFS_NT40_ENV		/* NT csn't select on non-socket fd's */
	select(fd + 1, &in, 0, 0, 0);	/* don't timeout if read blocks */
#endif
	nbytes = read(fd, buffer, blockSize);
	if (nbytes < 0) {
	    error = ADMVOSRESTOREFILEREADFAIL;
	    break;
	}
	if (nbytes == 0) {
	    done = 1;
	    break;
	}
	if (rx_Write(call, buffer, nbytes) != nbytes) {
	    error = ADMVOSRESTOREFILEWRITEFAIL;
	    break;
	}
    }
    if (buffer)
	free(buffer);
    return error;
}

static afs_int32
WriteData(struct rx_call *call, const char *filename)
{
    int fd;
    struct stat status;
    afs_int32 error, code;

    error = 0;
    fd = -1;

    fd = open(filename, 0);
    if (fd < 0 || fstat(fd, &status) < 0) {
	fprintf(STDERR, "Could access file '%s'\n", filename);
	error = ADMVOSRESTOREFILEOPENFAIL;
	goto fail_WriteData;
    }
    code = SendFile(fd, call, &status);
    if (code) {
	error = code;
	goto fail_WriteData;
    }

  fail_WriteData:

    if (fd >= 0)
	code = close(fd);
    else
	code = 0;
    if (code) {
	if (!error)
	    error = ADMVOSRESTOREFILECLOSEFAIL;
    }
    return error;
}

/*
 * Restore a volume <tovolid> <tovolname> on <toserver> <topart> from
 * the dump file <afilename>. WriteData does all the real work
 * after extracting params from the rock 
 */
int
UV_RestoreVolume(afs_cell_handle_p cellHandle, afs_int32 toserver,
		 afs_int32 topart, afs_uint32 tovolid, char *tovolname,
		 int flags, const char *dumpFile, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t etst = 0;
    struct rx_connection *toconn, *tempconn;
    struct rx_call *tocall;
    afs_int32 totid, rcode;
    afs_int32 rxError = 0;
    struct volser_status tstatus;
    char partName[10];
    afs_uint32 pvolid;
    afs_int32 temptid;
    int success;
    struct nvldbentry entry;
    int islocked;
    struct restoreCookie cookie;
    int reuseID;
    afs_int32 newDate, volflag;
    int index, same;


    memset(&cookie, 0, sizeof(cookie));
    islocked = 0;
    success = 0;
    reuseID = 1;
    tocall = (struct rx_call *)0;
    toconn = (struct rx_connection *)0;
    tempconn = (struct rx_connection *)0;
    totid = 0;
    temptid = 0;

    pvolid = tovolid;
    toconn = UV_Bind(cellHandle, toserver, AFSCONF_VOLUMEPORT);
    if (pvolid == 0) {		/*alot a new id if needed */
	aVLDB_GetEntryByName(cellHandle, tovolname, &entry, &tst);
	if (tst == VL_NOENT) {
	    tst =
		ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &pvolid);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    reuseID = 0;
	} else {
	    pvolid = entry.volumeId[RWVOL];
	}
    }

    /* 
     * at this point we have a volume id to use/reuse for the
     * volume to be restored
     */
    if (strlen(tovolname) > (VOLSER_OLDMAXVOLNAME - 1)) {
	tst = ADMVOSRESTOREVOLUMENAMETOOBIG;
	goto fail_UV_RestoreVolume;
    }

    if (!vos_PartitionIdToName(topart, partName, &tst)) {
	goto fail_UV_RestoreVolume;
    }
    /*what should the volume be restored as ? rw or ro or bk ?
     * right now the default is rw always */
    tst =
	AFSVolCreateVolume(toconn, topart, tovolname, volser_RW, 0, &pvolid,
			   &totid);
    if (tst) {
	if (flags & RV_FULLRST) {	/* full restore: delete then create anew */
	    tst =
		AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    tst =
		AFSVolSetFlags(toconn, totid,
			       VTDeleteOnSalvage | VTOutOfService);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    tst = AFSVolDeleteVolume(toconn, totid);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    tst = AFSVolEndTrans(toconn, totid, &rcode);
	    totid = 0;
	    if (!tst)
		tst = rcode;
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    tst =
		AFSVolCreateVolume(toconn, topart, tovolname, volser_RW, 0,
				   &pvolid, &totid);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	} else {
	    tst =
		AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	}
    }
    cookie.parent = pvolid;
    cookie.type = RWVOL;
    cookie.clone = 0;
    strncpy(cookie.name, tovolname, VOLSER_OLDMAXVOLNAME);

    tocall = rx_NewCall(toconn);
    tst = StartAFSVolRestore(tocall, totid, 1, &cookie);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }
    tst = WriteData(tocall, dumpFile);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }
    tst = rx_EndCall(tocall, rxError);
    tocall = (struct rx_call *)0;
    if (tst) {
	goto fail_UV_RestoreVolume;
    }
    tst = AFSVolGetStatus(toconn, totid, &tstatus);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }
    tst = AFSVolSetIdsTypes(toconn, totid, tovolname, RWVOL, pvolid, 0, 0);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }
    newDate = time(0);
    tst = AFSVolSetDate(toconn, totid, newDate);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }

    volflag = ((flags & RV_OFFLINE) ? VTOutOfService : 0);	/* off or on-line */
    tst = AFSVolSetFlags(toconn, totid, volflag);
    if (tst) {
	goto fail_UV_RestoreVolume;
    }

/* It isn't handled right in fail_UV_RestoreVolume */
    tst = AFSVolEndTrans(toconn, totid, &rcode);
    totid = 0;
    if (!tst)
	tst = rcode;
    if (tst) {
	goto fail_UV_RestoreVolume;
    }

    success = 1;
    if (success && (!reuseID || (flags & RV_FULLRST))) {
	/* Volume was restored on the file server, update the 
	 * VLDB to reflect the change.
	 */
	aVLDB_GetEntryByID(cellHandle, pvolid, RWVOL, &entry, &tst);
	if (tst && tst != VL_NOENT && tst != VL_ENTDELETED) {
	    goto fail_UV_RestoreVolume;
	}
	if (tst == VL_NOENT) {	/* it doesnot exist already */
	    /*make the vldb return this indication specifically */
	    strcpy(entry.name, tovolname);
	    entry.nServers = 1;
	    entry.serverNumber[0] = toserver;	/*should be indirect */
	    entry.serverPartition[0] = topart;
	    entry.serverFlags[0] = ITSRWVOL;
	    entry.flags = RW_EXISTS;
	    if (tstatus.cloneID != 0) {
		entry.volumeId[ROVOL] = tstatus.cloneID;	/*this should come from status info on the volume if non zero */
	    } else
		entry.volumeId[ROVOL] = INVALID_BID;
	    entry.volumeId[RWVOL] = pvolid;
	    entry.cloneId = 0;
	    if (tstatus.backupID != 0) {
		entry.volumeId[BACKVOL] = tstatus.backupID;
		/*this should come from status info on the volume if non zero */
	    } else
		entry.volumeId[BACKVOL] = INVALID_BID;
	    if (!VLDB_CreateEntry(cellHandle, &entry, &tst)) {
		goto fail_UV_RestoreVolume;
	    }
	    islocked = 0;
	} else {		/*update the existing entry */
	    tst =
		ubik_VL_SetLock(cellHandle->vos, 0, pvolid, RWVOL,
			  VLOP_RESTORE);
	    if (tst) {
		goto fail_UV_RestoreVolume;
	    }
	    islocked = 1;
	    strcpy(entry.name, tovolname);

	    /* Update the vlentry with the new information */
	    index = Lp_GetRwIndex(cellHandle, &entry, 0);
	    if (index == -1) {
		/* Add the rw site for the volume being restored */
		entry.serverNumber[entry.nServers] = toserver;
		entry.serverPartition[entry.nServers] = topart;
		entry.serverFlags[entry.nServers] = ITSRWVOL;
		entry.nServers++;
	    } else {
		/* This volume should be deleted on the old site
		 * if its different from new site.
		 */
		VLDB_IsSameAddrs(cellHandle, toserver,
				 entry.serverNumber[index], &same, &tst);
		if ((!tst && !same)
		    || (entry.serverPartition[index] != topart)) {
		    tempconn =
			UV_Bind(cellHandle, entry.serverNumber[index],
				AFSCONF_VOLUMEPORT);
		    tst =
			AFSVolTransCreate(tempconn, pvolid,
					  entry.serverPartition[index],
					  ITOffline, &temptid);
		    if (!tst) {
			tst =
			    AFSVolSetFlags(tempconn, temptid,
					   VTDeleteOnSalvage |
					   VTOutOfService);
			if (tst) {
			    goto fail_UV_RestoreVolume;
			}
			tst = AFSVolDeleteVolume(tempconn, temptid);
			if (tst) {
			    goto fail_UV_RestoreVolume;
			}
			tst = AFSVolEndTrans(tempconn, temptid, &rcode);
			temptid = 0;
			if (!tst)
			    tst = rcode;
			if (tst) {
			    goto fail_UV_RestoreVolume;
			}
			vos_PartitionIdToName(entry.serverPartition[index],
					      partName, &tst);
		    }
		}
		entry.serverNumber[index] = toserver;
		entry.serverPartition[index] = topart;
	    }

	    entry.flags |= RW_EXISTS;
	    if (!VLDB_ReplaceEntry
		(cellHandle, pvolid, RWVOL, &entry,
		 LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
		goto fail_UV_RestoreVolume;
	    }
	    islocked = 0;
	}
    }
    rc = 1;

  fail_UV_RestoreVolume:

    if (tocall) {
	etst = rx_EndCall(tocall, rxError);
	if (!tst)
	    tst = etst;
    }
    if (islocked) {
	etst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, pvolid, RWVOL,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }
    if (totid) {
	etst = AFSVolEndTrans(toconn, totid, &rcode);
	if (!etst)
	    etst = rcode;
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }
    if (temptid) {
	etst = AFSVolEndTrans(toconn, temptid, &rcode);
	if (!etst)
	    etst = rcode;
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }

    if (tempconn)
	rx_ReleaseCachedConnection(tempconn);
    if (toconn)
	rx_ReleaseCachedConnection(toconn);

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*adds <server> and <part> as a readonly replication site for <volid>
*in vldb */
int
UV_AddSite(afs_cell_handle_p cellHandle, afs_int32 server, afs_int32 part,
	   afs_uint32 volid, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    int j, nro = 0, islocked = 0;
    struct nvldbentry entry;
    int same = 0;

    tst =
	ubik_VL_SetLock(cellHandle->vos, 0, volid, RWVOL, VLOP_ADDSITE);
    if (tst) {
	goto fail_UV_AddSite;
    }
    islocked = 1;

    if (!aVLDB_GetEntryByID(cellHandle, volid, RWVOL, &entry, &tst)) {
	goto fail_UV_AddSite;
    }
    if (!ISNAMEVALID(entry.name)) {
	tst = VOLSERBADOP;
	goto fail_UV_AddSite;
    }

    /* See if it's too many entries */
    if (entry.nServers >= NMAXNSERVERS) {
	tst = VOLSERBADOP;
	goto fail_UV_AddSite;
    }

    /* See if it's on the same server */
    for (j = 0; j < entry.nServers; j++) {
	if (entry.serverFlags[j] & ITSROVOL) {
	    nro++;
	    if (!VLDB_IsSameAddrs
		(cellHandle, server, entry.serverNumber[j], &same, &tst)) {
		goto fail_UV_AddSite;
	    }
	    if (same) {
		tst = VOLSERBADOP;
		goto fail_UV_AddSite;
	    }
	}
    }

    /* See if it's too many RO sites - leave one for the RW */
    if (nro >= NMAXNSERVERS - 1) {
	tst = VOLSERBADOP;
	goto fail_UV_AddSite;
    }

    entry.serverNumber[entry.nServers] = server;
    entry.serverPartition[entry.nServers] = part;
    entry.serverFlags[entry.nServers] = (ITSROVOL | RO_DONTUSE);
    entry.nServers++;

    if (!VLDB_ReplaceEntry
	(cellHandle, volid, RWVOL, &entry,
	 LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
	goto fail_UV_AddSite;
    }
    islocked = 0;
    rc = 1;

  fail_UV_AddSite:

    if (islocked) {
	tst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0, volid, RWVOL,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*removes <server> <part> as read only site for <volid> from the vldb */
int
UV_RemoveSite(afs_cell_handle_p cellHandle, afs_int32 server, afs_int32 part,
	      afs_uint32 volid, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    struct nvldbentry entry;
    int islocked = 0;

    tst =
	ubik_VL_SetLock(cellHandle->vos, 0, volid, RWVOL, VLOP_ADDSITE);
    if (tst) {
	goto fail_UV_RemoveSite;
    }
    islocked = 1;

    if (!aVLDB_GetEntryByID(cellHandle, volid, RWVOL, &entry, &tst)) {
	goto fail_UV_RemoveSite;
    }
    if (!Lp_ROMatch(cellHandle, &entry, server, part, &tst)) {
	/*this site doesnot exist  */
	goto fail_UV_RemoveSite;
    } else {			/*remove the rep site */
	Lp_SetROValue(cellHandle, &entry, server, part, 0, 0);
	entry.nServers--;
	if ((entry.nServers == 1) && (entry.flags & RW_EXISTS))
	    entry.flags &= ~RO_EXISTS;
	if (entry.nServers < 1) {	/*this is the last ref */
	    tst = ubik_VL_DeleteEntry(cellHandle->vos, 0, volid, ROVOL);
	    if (tst) {
		goto fail_UV_RemoveSite;
	    }
	}
	if (!VLDB_ReplaceEntry
	    (cellHandle, volid, RWVOL, &entry,
	     (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
	    goto fail_UV_RemoveSite;
	}
    }
    rc = 1;

  fail_UV_RemoveSite:

    if (islocked) {
	afs_status_t t;
	t = ubik_VL_ReleaseLock(cellHandle->vos, 0, volid, RWVOL,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
	if (tst == 0) {
	    tst = t;
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*list all the partitions on <aserver> */
int
UV_ListPartitions(struct rx_connection *server, struct partList *ptrPartList,
		  afs_int32 * cntp, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    struct pIDs partIds;
    struct partEntries partEnts;
    int i, j = 0;

    *cntp = 0;

    partEnts.partEntries_len = 0;
    partEnts.partEntries_val = NULL;
    /* this is available only on new servers */
    tst = AFSVolXListPartitions(server, &partEnts);

    /* next, try old interface */
    if (tst == RXGEN_OPCODE) {
	for (i = 0; i < 26; i++)
	    partIds.partIds[i] = -1;
	tst = AFSVolListPartitions(server, &partIds);
	if (!tst) {
	    for (i = 0; i < 26; i++) {
		if ((partIds.partIds[i]) != -1) {
		    ptrPartList->partId[j] = partIds.partIds[i];
		    ptrPartList->partFlags[j] = PARTVALID;
		    j++;
		} else
		    ptrPartList->partFlags[i] = 0;
	    }
	    *cntp = j;
	} else {
	    goto fail_UV_ListPartitions;
	}
    } else if (!tst) {
	*cntp = partEnts.partEntries_len;
	if (*cntp > VOLMAXPARTS) {
	    *cntp = VOLMAXPARTS;
	}
	for (i = 0; i < *cntp; i++) {
	    ptrPartList->partId[i] = partEnts.partEntries_val[i];
	    ptrPartList->partFlags[i] = PARTVALID;
	}
	free(partEnts.partEntries_val);
    } else {
	goto fail_UV_ListPartitions;
    }
    rc = 1;

  fail_UV_ListPartitions:

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*------------------------------------------------------------------------
 * EXPORTED UV_XListVolumes
 *
 * Description:
 *	List the extended information for all the volumes on a particular
 *	File Server and partition.  We may either return the volume's ID
 *	or all of its extended information.
 *
 * Arguments:
 *	a_serverID	   : Address of the File Server for which we want
 *				extended volume info.
 *	a_partID	   : Partition for which we want the extended
 *				volume info.
 *	a_all		   : If non-zero, fetch ALL the volume info,
 *				otherwise just the volume ID.
 *	a_resultPP	   : Ptr to the address of the area containing
 *				the returned volume info.
 *	a_numEntsInResultP : Ptr for the value we set for the number of
 *				entries returned.
 *
 * Returns:
 *	0 on success,
 *	Otherise, the return value of AFSVolXListVolumes.
 *
 * Environment:
 *	This routine is closely related to UV_ListVolumes, which returns
 *	only the standard level of detail on AFS volumes. It is a
 *	heavyweight operation, zipping through all the volume entries for
 *	a given server/partition.
 *
 * Side Effects:
 *	As advertised.
 *------------------------------------------------------------------------*/

int
UV_XListVolumes(struct rx_connection *server, afs_int32 a_partID, int a_all,
		struct volintXInfo **a_resultPP,
		afs_int32 * a_numEntsInResultP, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;

    volXEntries volumeXInfo;	/*Area for returned extended vol info */

    /*
     * Set up our error code and the area for returned extended volume info.
     * We set the val field to a null pointer as a hint for the stub to
     * allocate space.
     */
    *a_numEntsInResultP = 0;
    *a_resultPP = (volintXInfo *) 0;
    volumeXInfo.volXEntries_val = (volintXInfo *) 0;
    volumeXInfo.volXEntries_len = 0;

    /*
     * Bind to the Volume Server port on the File Server machine in question,
     * then go for it.
     */
    tst = AFSVolXListVolumes(server, a_partID, a_all, &volumeXInfo);
    if (tst) {
	goto fail_UV_XListVolumes;
    } else {
	/*
	 * We got the info; pull out the pointer to where the results lie
	 * and how many entries are there.
	 */
	*a_resultPP = volumeXInfo.volXEntries_val;
	*a_numEntsInResultP = volumeXInfo.volXEntries_len;
    }
    rc = 1;

  fail_UV_XListVolumes:

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*------------------------------------------------------------------------
 * EXPORTED UV_XListOneVolume
 *
 * Description:
 *	List the extended information for a volume on a particular File
 *	Server and partition.
 *
 * Arguments:
 *	server	   : a handle to the server where the volume resides.
 *	a_partID	   : Partition for which we want the extended
 *				volume info.
 *	a_volID		   : Volume ID for which we want the info.
 *	a_resultPP	   : Ptr to the address of the area containing
 *				the returned volume info.
 *
 * Returns:
 *	0 on success,
 *	Otherise, the return value of AFSVolXListOneVolume.
 *
 * Environment:
 *	This routine is closely related to UV_ListOneVolume, which returns
 *	only the standard level of detail on the chosen AFS volume.
 *
 * Side Effects:
 *	As advertised.
 *------------------------------------------------------------------------*/

int
UV_XListOneVolume(struct rx_connection *server, afs_int32 a_partID,
		  afs_uint32 a_volID, struct volintXInfo **a_resultPP,
		  afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    volXEntries volumeXInfo;	/*Area for returned info */

    /*
     * Set the area we're in which we are returning
     * the info.  Setting the val field to a null pointer tells the stub
     * to allocate space for us.
     */
    *a_resultPP = (volintXInfo *) 0;
    volumeXInfo.volXEntries_val = (volintXInfo *) 0;
    volumeXInfo.volXEntries_len = 0;

    tst = AFSVolXListOneVolume(server, a_partID, a_volID, &volumeXInfo);

    if (tst) {
	goto fail_UV_XListOneVolume;
    } else {
	/*
	 * We got the info; pull out the pointer to where the results lie.
	 */
	*a_resultPP = volumeXInfo.volXEntries_val;
    }
    rc = 1;

  fail_UV_XListOneVolume:

    if (st != NULL) {
	*st = tst;
    }
    return rc;

}				/*UV_XListOneVolume */

/*------------------------------------------------------------------------
 * EXPORTED UV_ListOneVolume
 *
 * Description:
 *	List the volume information for a volume on a particular File
 *	Server and partition.
 *
 * Arguments:
 *	server	   : a handle to the server where the volume resides.
 *	a_partID	   : Partition for which we want the extended
 *				volume info.
 *	a_volID		   : Volume ID for which we want the info.
 *	a_resultPP	   : Ptr to the address of the area containing
 *				the returned volume info.
 *
 * Returns:
 *	0 on success,
 *	Otherise, the return value of AFSVolXListOneVolume.
 *
 * Side Effects:
 *	As advertised.
 *------------------------------------------------------------------------*/

int UV_ListOneVolume(struct rx_connection *server, afs_int32 a_partID,
		  afs_uint32 a_volID, struct volintInfo **a_resultPP,
		  afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    volEntries volumeInfo;	/*Area for returned info */

    /*
     * Set the area we're in which we are returning
     * the info.  Setting the val field to a null pointer tells the stub
     * to allocate space for us.
     */
    *a_resultPP = (volintInfo *) 0;
    volumeInfo.volEntries_val = (volintInfo *) 0;
    volumeInfo.volEntries_len = 0;

    tst = AFSVolListOneVolume(server, a_partID, a_volID, &volumeInfo);

    if (tst) {
	goto fail_UV_ListOneVolume;
    } else {
	/*
	 * We got the info; pull out the pointer to where the results lie.
	 */
	*a_resultPP = volumeInfo.volEntries_val;
    }
    rc = 1;

  fail_UV_ListOneVolume:

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}/*UV_ListOneVolume*/

/*sync vldb with all the entries on <myQueue> on <aserver> and <apart>*/
static afs_int32
ProcessEntries(afs_cell_handle_p cellHandle, struct qHead *myQueue,
	       struct rx_connection *server, afs_int32 apart, afs_int32 force)
{
    struct aqueue elem;
    int success, temp;
    afs_uint32 temp1, temp2;
    afs_int32 vcode;
    afs_uint32 maxVolid = 0;
    struct nvldbentry entry;
    int noError = 1, error, same;
    int totalC, totalU, totalCE, totalUE, totalG;
    int counter;
    int aserver = ntohl(rx_HostOf(rx_PeerOf(server)));
    afs_status_t tst;

    totalC = totalU = totalCE = totalUE = totalG = 0;
    counter = 0;

    /* get the next  available id's from the vldb server */
    vcode = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 0, &maxVolid);
    if (vcode) {
	return (vcode);
    }
    totalG = myQueue->count;
    if (totalG == 0)
	return 0;
    while (1) {
	Lp_QEnumerate(myQueue, &success, &elem, 0);
	if (!success)
	    break;
	counter++;

	if (!elem.isValid[RWVOL] && !elem.isValid[ROVOL] && !elem.isValid[BACKVOL]) {	/*something is wrong with elem */
	    noError = 0;
	    continue;
	}
	if (maxVolid <= elem.ids[RWVOL]) {
	    temp1 = maxVolid;
	    temp2 = elem.ids[RWVOL] - maxVolid + 1;
	    maxVolid = 0;
	    vcode =
		ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
			  &maxVolid);
	    maxVolid += temp2;
	}
	if (maxVolid <= elem.ids[ROVOL]) {
	    temp1 = maxVolid;
	    temp2 = elem.ids[ROVOL] - maxVolid + 1;
	    maxVolid = 0;
	    vcode =
		ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
			  &maxVolid);
	    maxVolid += temp2;
	}
	if (maxVolid <= elem.ids[BACKVOL]) {
	    temp1 = maxVolid;
	    temp2 = elem.ids[BACKVOL] - temp1 + 1;
	    maxVolid = 0;
	    vcode =
		ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
			  &maxVolid);
	    maxVolid += temp2;
	}
	aVLDB_GetEntryByID(cellHandle, elem.ids[RWVOL], RWVOL, &entry, &tst);
	if (tst && (tst != VL_NOENT)) {
	    noError = 0;
	    totalCE++;
	} else if (tst && (tst == VL_NOENT)) {	/*entry doesnot exist */
	    /*set up a vldb entry for elem */
	    memset(&entry, 0, sizeof(entry));
	    strncpy(entry.name, elem.name, VOLSER_OLDMAXVOLNAME);
	    if (elem.isValid[RWVOL]) {	/*rw exists */
		entry.flags |= RW_EXISTS;
		entry.serverFlags[entry.nServers] = ITSRWVOL;
		entry.serverNumber[entry.nServers] = aserver;
		entry.serverPartition[entry.nServers] = apart;
		entry.nServers += 1;
		entry.volumeId[RWVOL] = elem.ids[RWVOL];
		entry.volumeId[ROVOL] = elem.ids[ROVOL];
		entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
	    }
	    if (elem.isValid[ROVOL]) {	/*ro volume exists */
		entry.flags |= RO_EXISTS;
		entry.serverFlags[entry.nServers] = ITSROVOL;
		entry.serverNumber[entry.nServers] = aserver;
		entry.serverPartition[entry.nServers] = apart;
		entry.nServers += 1;
		entry.volumeId[RWVOL] = elem.ids[RWVOL];
		entry.volumeId[ROVOL] = elem.ids[ROVOL];

	    }
	    if (elem.isValid[BACKVOL]) {	/*backup volume exists */
		entry.flags |= BACK_EXISTS;
		if (!(entry.flags & RW_EXISTS)) {	/*this helps to check for a stray backup if parent moves */
		    entry.serverFlags[entry.nServers] = ITSRWVOL;
		    entry.serverNumber[entry.nServers] = aserver;
		    entry.serverPartition[entry.nServers] = apart;
		    entry.nServers += 1;
		}

		entry.volumeId[RWVOL] = elem.ids[RWVOL];
		entry.volumeId[BACKVOL] = elem.ids[BACKVOL];

	    }
	    VLDB_CreateEntry(cellHandle, &entry, &tst);
	    if (tst) {
		noError = 0;
		totalCE++;
	    } else
		totalC++;
	} else {		/* Update the existing entry */
	    strncpy(entry.name, elem.name, VOLSER_OLDMAXVOLNAME);	/*the name Could have changed */

	    if (elem.isValid[RWVOL]) {	/* A RW volume */
		temp = Lp_GetRwIndex(cellHandle, &entry, 0);
		if (temp == -1) {
		    /* A RW index is not found in the VLDB entry - will add it */

		    entry.flags |= RW_EXISTS;
		    entry.serverNumber[entry.nServers] = aserver;
		    entry.serverPartition[entry.nServers] = apart;
		    entry.serverFlags[entry.nServers] = ITSRWVOL;
		    entry.nServers++;
		} else {
		    /* A RW index is found in the VLDB entry.
		     * Verify that the volume location matches the VLDB location.
		     * Fix the VLDB entry if it is not correct.
		     */

		    error =
			VLDB_IsSameAddrs(cellHandle, aserver,
					 entry.serverNumber[temp], &same,
					 &tst);
		    if (!error) {
			continue;
		    }
		    if (!same || (apart != entry.serverPartition[temp])) {
			/* VLDB says volume is in another place. Fix the VLDB entry */
			entry.serverNumber[temp] = aserver;
			entry.serverPartition[temp] = apart;

		    }
		    entry.flags |= RW_EXISTS;
		}
		if ((elem.ids[BACKVOL] != 0) && elem.isValid[BACKVOL])
		    entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
		if ((elem.ids[ROVOL] != 0) && elem.isValid[ROVOL])
		    entry.volumeId[ROVOL] = elem.ids[ROVOL];
	    }

	    if (elem.isValid[ROVOL]) {
		/*tackle a ro volume */

		if (!Lp_ROMatch(cellHandle, &entry, aserver, apart, 0)) {
		    /*add this site */
		    if (elem.ids[ROVOL] > entry.volumeId[ROVOL]) {
			/*there is a conflict of ids, keep the later volume */
			/*delete all the ro volumes listed in vldb entry since they 
			 * are older */

			int j, count, rwsite;


			count = entry.nServers;
			rwsite = -1;
			for (j = 0; j < count; j++) {
			    if (entry.serverFlags[j] & ITSROVOL) {

				/*delete the site */
				entry.serverNumber[j] = 0;
				entry.serverPartition[j] = 0;
				entry.serverFlags[j] = 0;

			    } else if (entry.serverFlags[j] & ITSRWVOL)
				rwsite = j;
			}
			entry.nServers = 0;
			if (rwsite != -1) {
			    entry.serverNumber[entry.nServers] =
				entry.serverNumber[rwsite];
			    entry.serverPartition[entry.nServers] =
				entry.serverPartition[rwsite];
			    entry.serverFlags[entry.nServers] =
				entry.serverFlags[rwsite];
			    entry.nServers++;
			}
			entry.serverNumber[entry.nServers] = aserver;
			entry.serverPartition[entry.nServers] = apart;
			entry.serverFlags[entry.nServers] = ITSROVOL;
			entry.nServers++;
			entry.volumeId[ROVOL] = elem.ids[ROVOL];
			entry.flags |= RO_EXISTS;

		    } else if (elem.ids[ROVOL] < entry.volumeId[ROVOL]) {
			if (!(entry.flags & RO_EXISTS)) {
			    entry.volumeId[ROVOL] = elem.ids[ROVOL];
			    entry.serverNumber[entry.nServers] = aserver;
			    entry.serverPartition[entry.nServers] = apart;
			    entry.serverFlags[entry.nServers] = ITSROVOL;
			    entry.nServers++;
			    entry.flags |= RO_EXISTS;
			}

		    }

		    else if (elem.ids[ROVOL] == entry.volumeId[ROVOL]) {
			entry.serverNumber[entry.nServers] = aserver;
			entry.serverPartition[entry.nServers] = apart;
			entry.serverFlags[entry.nServers] = ITSROVOL;
			entry.nServers++;
			entry.flags |= RO_EXISTS;
			entry.volumeId[ROVOL] = elem.ids[ROVOL];
		    }
		}
		if (entry.volumeId[ROVOL] == INVALID_BID)
		    entry.volumeId[ROVOL] = elem.ids[ROVOL];
	    }

	    if (elem.isValid[BACKVOL]) {
		temp = Lp_GetRwIndex(cellHandle, &entry, 0);
		if (temp != -1) {	/*check if existing backup site matches with the given arguments */
		    error =
			VLDB_IsSameAddrs(cellHandle, aserver,
					 entry.serverNumber[temp], &same,
					 &tst);
		    if (!error) {
			continue;
		    }
		} else {
		    /*tackle the backup volume */
		    entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
		    entry.flags |= BACK_EXISTS;
		}
		if (entry.volumeId[BACKVOL] == INVALID_BID)
		    entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
	    }

	    VLDB_ReplaceEntry(cellHandle, elem.ids[RWVOL], RWVOL, &entry,
			      LOCKREL_OPCODE | LOCKREL_AFSID |
			      LOCKREL_TIMESTAMP, &tst);
	    if (tst) {
		noError = 0;
		totalUE++;

		vcode =
		    ubik_VL_ReleaseLock(cellHandle->vos, 0,
			      elem.ids[RWVOL], RWVOL,
			      LOCKREL_OPCODE | LOCKREL_AFSID |
			      LOCKREL_TIMESTAMP);
		if (vcode) {
		    noError = 0;
		}
	    }
	}			/* else update the existing entry */

    }				/* End of while(1) */

    if (noError)
	return 0;
    else
	return VOLSERBADOP;
}

/*synchronise vldb with the file server <aserver> and <apart>(if flags=1).
*else synchronise with all the valid partitions on <aserver>
*/
int
UV_SyncVldb(afs_cell_handle_p cellHandle, struct rx_connection *server,
	    afs_int32 apart, int flags, int force, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_int32 count;
    int i;
    volEntries volumeInfo;
    volintInfo *pntr;
    struct qHead myQueue;
    struct partList PartList;
    int noError = 1;
    afs_int32 cnt;
    char pname[10];

    /*this hints the stub to allocate space */
    volumeInfo.volEntries_val = (volintInfo *) 0;
    volumeInfo.volEntries_len = 0;

    if (!flags) {		/*generate all the valid partitions */
	UV_ListPartitions(server, &PartList, &cnt, &tst);
	if (tst) {
	    goto fail_UV_SyncVldb;
	}
    } else {
	PartList.partId[0] = apart;
	cnt = 1;
    }

    for (i = 0; i < cnt; i++) {
	apart = PartList.partId[i];
	/*this hints the stub to allocate space */
	volumeInfo.volEntries_val = (volintInfo *) 0;
	volumeInfo.volEntries_len = 0;
	tst = AFSVolListVolumes(server, apart, 1, &volumeInfo);
	if (tst) {
	    goto fail_UV_SyncVldb;
	}
	count = volumeInfo.volEntries_len;
	pntr = volumeInfo.volEntries_val;

	if (!vos_PartitionIdToName(apart, pname, &tst)) {
	    goto fail_UV_SyncVldb;
	}
	/*collect all vol entries by their parentid */
	tst = GroupEntries(server, pntr, count, &myQueue, apart);
	if (tst) {
	    noError = 0;
	    if (volumeInfo.volEntries_val) {
		/*free the space allocated by the stub */
		free(volumeInfo.volEntries_val);
		volumeInfo.volEntries_val = 0;
	    }
	    continue;
	}
	tst = ProcessEntries(cellHandle, &myQueue, server, apart, force);
	if (tst) {
	    tst = VOLSERFAILEDOP;
	    if (volumeInfo.volEntries_val) {
		/*free the space allocated by the stub */
		free(volumeInfo.volEntries_val);
		volumeInfo.volEntries_val = 0;
	    }
	    continue;
	}
	if (noError)
	    tst = 0;
	else
	    tst = VOLSERFAILEDOP;
    }				/* thru all partitions */
    rc = 1;

  fail_UV_SyncVldb:

    if (volumeInfo.volEntries_val)
	free(volumeInfo.volEntries_val);

    if (tst != 0) {
	rc = 0;
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

static afs_int32
CheckVldbRWBK(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
	      afs_int32 * modified, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    int modentry = 0;
    int idx;

    if (modified)
	*modified = 0;
    idx = Lp_GetRwIndex(cellHandle, entry, 0);

    /* Check to see if the RW volume exists and set the RW_EXISTS
     * flag accordingly.
     */
    if (idx == -1) {		/* Did not find a RW entry */
	if (entry->flags & RW_EXISTS) {	/* ... yet entry says RW exists */
	    entry->flags &= ~RW_EXISTS;	/* ... so say RW does not exist */
	    modentry++;
	}
    } else {
	if (VolumeExists
	    (cellHandle, entry->serverNumber[idx],
	     entry->serverPartition[idx], entry->volumeId[RWVOL], &tst)) {
	    if (!(entry->flags & RW_EXISTS)) {	/* ... yet entry says RW does no
						 * t exist */
		entry->flags |= RW_EXISTS;	/* ... so say RW does exist */
		modentry++;
	    }
	} else if (tst == ENODEV) {	/* RW volume does not exist */
	    if (entry->flags & RW_EXISTS) {	/* ... yet entry says RW exists
						 */
		entry->flags &= ~RW_EXISTS;	/* ... so say RW does not exist
						 */
		modentry++;
	    }
	} else {
	    /* If VLDB says it didn't exist, then ignore error */
	    if (entry->flags & RW_EXISTS) {
		goto fail_CheckVldbRWBK;
	    }
	}
    }

    /* Check to see if the BK volume exists and set the BACK_EXISTS
     * flag accordingly. idx already ponts to the RW entry.
     */
    if (idx == -1) {		/* Did not find a RW entry */
	if (entry->flags & BACK_EXISTS) {	/* ... yet entry says BK exists */
	    entry->flags &= ~BACK_EXISTS;	/* ... so say BK does not exist */
	    modentry++;
	}
    } else {			/* Found a RW entry */
	if (VolumeExists
	    (cellHandle, entry->serverNumber[idx],
	     entry->serverPartition[idx], entry->volumeId[BACKVOL], &tst)) {
	    if (!(entry->flags & BACK_EXISTS)) {	/* ... yet entry says BK does n
							 * ot exist */
		entry->flags |= BACK_EXISTS;	/* ... so say BK does exist */
		modentry++;
	    }
	} else if (tst == ENODEV) {	/* BK volume does not exist */
	    if (entry->flags & BACK_EXISTS) {	/* ... yet entry says BK exists
						 */
		entry->flags &= ~BACK_EXISTS;	/* ... so say BK does not exist
						 */
		modentry++;
	    }
	} else {
	    /* If VLDB says it didn't exist, then ignore error */
	    if (entry->flags & BACK_EXISTS) {
		goto fail_CheckVldbRWBK;
	    }
	}
    }

    /* If there is an idx but the BK and RW volumes no
     * longer exist, then remove the RW entry.
     */
    if ((idx != -1) && !(entry->flags & RW_EXISTS)
	&& !(entry->flags & BACK_EXISTS)) {
	Lp_SetRWValue(cellHandle, entry, entry->serverNumber[idx],
		      entry->serverPartition[idx], 0L, 0L);
	entry->nServers--;
	modentry++;
    }
    rc = 1;

  fail_CheckVldbRWBK:

    if (modified)
	*modified = modentry;

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}


static int
CheckVldbRO(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
	    afs_int32 * modified, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    int idx;
    int foundro = 0, modentry = 0;

    if (modified)
	*modified = 0;

    /* Check to see if the RO volumes exist and set the RO_EXISTS
     * flag accordingly.
     */
    for (idx = 0; idx < entry->nServers; idx++) {
	if (!(entry->serverFlags[idx] & ITSROVOL)) {
	    continue;		/* not a RO */
	}

	if (VolumeExists
	    (cellHandle, entry->serverNumber[idx],
	     entry->serverPartition[idx], entry->volumeId[ROVOL], &tst)) {
	    foundro++;
	} else if (tst == ENODEV) {	/* RW volume does not exist */
	    Lp_SetROValue(cellHandle, entry, entry->serverNumber[idx],
			  entry->serverPartition[idx], 0L, 0L);
	    entry->nServers--;
	    idx--;
	    modentry++;
	} else {
	    goto fail_CheckVldbRO;
	}
    }

    if (foundro) {		/* A RO volume exists */
	if (!(entry->flags & RO_EXISTS)) {	/* ... yet entry says RW does not e
						 * xist */
	    entry->flags |= RO_EXISTS;	/* ... so say RW does exist */
	    modentry++;
	}
    } else {			/* A RO volume does not exist */
	if (entry->flags & RO_EXISTS) {	/* ... yet entry says RO exists */
	    entry->flags &= ~RO_EXISTS;	/* ... so say RO does not exist */
	    modentry++;
	}
    }
    rc = 1;

  fail_CheckVldbRO:

    if (modified)
	*modified = modentry;

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*ensure that <entry> matches with the info on file servers */
int
CheckVldb(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
	  afs_int32 * modified, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_int32 vcode;
    int islocked = 0;
    int pass = 0;
    afs_int32 modentry = 0;

    if (modified) {
	*modified = 0;
    }

    if (strlen(entry->name) > (VOLSER_OLDMAXVOLNAME - 10)) {
	tst = VOLSERBADOP;
	goto fail_CheckVldb;
    }

  retry:

    /* Check to see if the VLDB is ok without locking it (pass 1).
     * If it will change, then lock the VLDB entry, read it again,
     * then make the changes to it (pass 2).
     */
    if (++pass == 2) {
	tst =
	    ubik_VL_SetLock(cellHandle->vos, 0, entry->volumeId[RWVOL],
		      RWVOL, VLOP_DELETE);
	if (tst) {
	    goto fail_CheckVldb;
	}
	islocked = 1;

	if (!aVLDB_GetEntryByID
	    (cellHandle, entry->volumeId[RWVOL], RWVOL, entry, &tst)) {
	    goto fail_CheckVldb;
	}
    }

    modentry = 0;

    /* Check if the RW and BK entries are ok */
    if (!CheckVldbRWBK(cellHandle, entry, &modentry, &tst)) {
	goto fail_CheckVldb;
    }
    if (modentry && (pass == 1))
	goto retry;

    /* Check if the RO volumes entries are ok */
    if (!CheckVldbRO(cellHandle, entry, &modentry, &tst)) {
	goto fail_CheckVldb;
    }
    if (modentry && (pass == 1))
	goto retry;

    /* The VLDB entry has been updated. If it as been modified, then
     * write the entry back out the the VLDB.
     */
    if (modentry) {
	if (pass == 1)
	    goto retry;

	if (!(entry->flags & RW_EXISTS) && !(entry->flags & BACK_EXISTS)
	    && !(entry->flags & RO_EXISTS)) {
	    /* The RW, BK, nor RO volumes do not exist. Delete the VLDB entry */
	    tst =
		ubik_VL_DeleteEntry(cellHandle->vos, 0,
			  entry->volumeId[RWVOL], RWVOL);
	    if (tst) {
		goto fail_CheckVldb;
	    }
	} else {
	    /* Replace old entry with our new one */
	    if (!VLDB_ReplaceEntry
		(cellHandle, entry->volumeId[RWVOL], RWVOL, entry,
		 (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP),
		 &tst)) {
		goto fail_CheckVldb;
	    }
	}
	if (modified)
	    *modified = 1;
	islocked = 0;
    }
    rc = 1;

  fail_CheckVldb:

    if (islocked) {
	vcode =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0,
				entry->volumeId[RWVOL], RWVOL,
				(LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
	if (vcode) {
	    if (!tst)
		tst = vcode;
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*synchronise <aserver> <apart>(if flags = 1) with the vldb .
*if flags = 0, synchronise all the valid partitions on <aserver>*/
int
UV_SyncServer(afs_cell_handle_p cellHandle, struct rx_connection *server,
	      afs_int32 apart, int flags, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    int noError;
    afs_int32 nentries, tentries = 0;
    struct VldbListByAttributes attributes;
    nbulkentries arrayEntries;
    int totalF;
    struct nvldbentry *vllist;
    int j;
    afs_int32 si, nsi;
    afs_int32 modified = 0;

    noError = 1;
    arrayEntries.nbulkentries_val = 0;
    memset(&attributes, 0, sizeof(attributes));

    /* Set up attributes to search VLDB  */
    attributes.server = ntohl(rx_HostOf(rx_PeerOf(server)));
    attributes.Mask = VLLIST_SERVER;
    if (flags) {
	attributes.partition = apart;
	attributes.Mask |= VLLIST_PARTITION;
    }

    for (si = 0; si != -1; si = nsi) {
	/*initialize to hint the stub  to alloc space */
	memset(&arrayEntries, 0, sizeof(arrayEntries));
	if (!VLDB_ListAttributes
	    (cellHandle, &attributes, &nentries, &arrayEntries, &tst)) {
	    goto fail_UV_SyncServer;
	}
	nsi = -1;
	tentries += nentries;
	totalF = 0;
	for (j = 0; j < nentries; j++) {	/* process each entry */
	    vllist = &arrayEntries.nbulkentries_val[j];
	    if (!CheckVldb(cellHandle, vllist, &modified, &tst)) {
		noError = 0;
		totalF++;
	    }
	}
	if (arrayEntries.nbulkentries_val) {
	    free(arrayEntries.nbulkentries_val);
	    arrayEntries.nbulkentries_val = 0;
	}
    }
    rc = 1;

  fail_UV_SyncServer:

    if (arrayEntries.nbulkentries_val) {
	free(arrayEntries.nbulkentries_val);
    }
    if (!noError)
	tst = VOLSERFAILEDOP;
    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/* rename volume <oldname> to <newname>, changing the names of the related
 * readonly and backup volumes. This operation is also idempotent.
 * salvager is capable of recovering from rename operation stopping halfway.
 * to recover run syncserver on the affected machines,it will force
 * renaming to completion. name clashes should have been detected before
 * calling this proc
 */
int
UV_RenameVolume(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
		char *newname, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t etst = 0;
    afs_int32 rcode;
    int i, index;
    char nameBuffer[256];
    afs_int32 tid;
    struct rx_connection *aconn;
    int islocked;

    aconn = (struct rx_connection *)0;
    tid = 0;
    islocked = 0;

    tst = ubik_VL_SetLock(cellHandle->vos, 0, entry->volumeId[RWVOL], RWVOL, VLOP_ADDSITE);	/*last param is dummy */
    if (tst) {
	goto fail_UV_RenameVolume;
    }
    islocked = 1;

    strncpy(entry->name, newname, VOLSER_OLDMAXVOLNAME);

    if (!VLDB_ReplaceEntry
	(cellHandle, entry->volumeId[RWVOL], RWVOL, entry, 0, &tst)) {
	goto fail_UV_RenameVolume;
    }
    /*at this stage the intent to rename is recorded in the vldb, as far
     * as the vldb 
     * is concerned, oldname is lost */
    if (entry->flags & RW_EXISTS) {
	index = Lp_GetRwIndex(cellHandle, entry, 0);
	if (index == -1) {	/* there is a serious discrepancy */
	    tst = VOLSERVLDB_ERROR;
	    goto fail_UV_RenameVolume;
	}
	aconn =
	    UV_Bind(cellHandle, entry->serverNumber[index],
		    AFSCONF_VOLUMEPORT);
	tst =
	    AFSVolTransCreate(aconn, entry->volumeId[RWVOL],
			      entry->serverPartition[index], ITOffline, &tid);
	if (tst) {		/*volume doesnot exist */
	    goto fail_UV_RenameVolume;
	} else {		/*volume exists, process it */

	    tst =
		AFSVolSetIdsTypes(aconn, tid, newname, RWVOL,
				  entry->volumeId[RWVOL],
				  entry->volumeId[ROVOL],
				  entry->volumeId[BACKVOL]);
	    if (!tst) {
		tst = AFSVolEndTrans(aconn, tid, &rcode);
		tid = 0;
		if (tst) {
		    goto fail_UV_RenameVolume;
		}
	    } else {
		goto fail_UV_RenameVolume;
	    }
	}
	if (aconn)
	    rx_ReleaseCachedConnection(aconn);
	aconn = (struct rx_connection *)0;
    }
    /*end rw volume processing */
    if (entry->flags & BACK_EXISTS) {	/*process the backup volume */
	index = Lp_GetRwIndex(cellHandle, entry, 0);
	if (index == -1) {	/* there is a serious discrepancy */
	    tst = VOLSERVLDB_ERROR;
	    goto fail_UV_RenameVolume;
	}
	aconn =
	    UV_Bind(cellHandle, entry->serverNumber[index],
		    AFSCONF_VOLUMEPORT);
	tst =
	    AFSVolTransCreate(aconn, entry->volumeId[BACKVOL],
			      entry->serverPartition[index], ITOffline, &tid);
	if (tst) {		/*volume doesnot exist */
	    goto fail_UV_RenameVolume;
	} else {		/*volume exists, process it */
	    if (strlen(newname) > (VOLSER_OLDMAXVOLNAME - 8)) {
		goto fail_UV_RenameVolume;
	    }
	    strcpy(nameBuffer, newname);
	    strcat(nameBuffer, ".backup");

	    tst =
		AFSVolSetIdsTypes(aconn, tid, nameBuffer, BACKVOL,
				  entry->volumeId[RWVOL], 0, 0);
	    if (!tst) {
		tst = AFSVolEndTrans(aconn, tid, &rcode);
		tid = 0;
		if (tst) {
		    goto fail_UV_RenameVolume;
		}
	    } else {
		goto fail_UV_RenameVolume;
	    }
	}
    }				/* end backup processing */
    if (aconn)
	rx_ReleaseCachedConnection(aconn);
    aconn = (struct rx_connection *)0;
    if (entry->flags & RO_EXISTS) {	/*process the ro volumes */
	for (i = 0; i < entry->nServers; i++) {
	    if (entry->serverFlags[i] & ITSROVOL) {
		aconn =
		    UV_Bind(cellHandle, entry->serverNumber[i],
			    AFSCONF_VOLUMEPORT);
		tst =
		    AFSVolTransCreate(aconn, entry->volumeId[ROVOL],
				      entry->serverPartition[i], ITOffline,
				      &tid);
		if (tst) {	/*volume doesnot exist */
		    goto fail_UV_RenameVolume;
		} else {	/*volume exists, process it */
		    strcpy(nameBuffer, newname);
		    strcat(nameBuffer, ".readonly");
		    if (strlen(nameBuffer) > (VOLSER_OLDMAXVOLNAME - 1)) {
			goto fail_UV_RenameVolume;
		    }
		    tst =
			AFSVolSetIdsTypes(aconn, tid, nameBuffer, ROVOL,
					  entry->volumeId[RWVOL], 0, 0);
		    if (!tst) {
			tst = AFSVolEndTrans(aconn, tid, &rcode);
			tid = 0;
			if (tst) {
			    goto fail_UV_RenameVolume;
			}
		    } else {
			goto fail_UV_RenameVolume;
		    }
		}
		if (aconn)
		    rx_ReleaseCachedConnection(aconn);
		aconn = (struct rx_connection *)0;
	    }
	}
    }
    rc = 1;

  fail_UV_RenameVolume:

    if (islocked) {
	etst =
	    ubik_VL_ReleaseLock(cellHandle->vos, 0,
		      entry->volumeId[RWVOL], RWVOL,
		      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }
    if (tid) {
	etst = AFSVolEndTrans(aconn, tid, &rcode);
	if (etst) {
	    if (!tst)
		tst = etst;
	}
    }
    if (aconn)
	rx_ReleaseCachedConnection(aconn);

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*group all the volume entries on< apart >by their parentid or by their ids'
*if the volume is rw. <count> is the number of entries to be processesd.
*<pntr> points to the first entry.< myQueue> is the queue with entries 
*grouped */
static afs_int32
GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
	     struct qHead *myQueue, afs_int32 apart)
{
    struct aqueue *qPtr;
    int success;
    afs_int32 curId, code;
    int i;
    afs_int32 error = 0;


    Lp_QInit(myQueue);
    if (count == 0)
	return 0;
    for (i = 0; i < count; i++) {	/*process each entry */
	if (pntr->status) {	/* entry is valid */
	    if (pntr->type == RWVOL)
		curId = pntr->volid;
	    else
		curId = pntr->parentID;
	    Lp_QScan(myQueue, curId, &success, &qPtr, 0);
	    if (success) {	/*entry exists in the queue */
		if (pntr->type == RWVOL) {
		    /*check if the rw exists already, if so hang on the
		     * later version if the present version is ok */
		    if (qPtr->isValid[RWVOL]) {
			/*this should not happen, there is a serious error here */
			if (!error)
			    error = VOLSERMULTIRWVOL;
		    } else {
			qPtr->isValid[RWVOL] = 1;
			qPtr->copyDate[RWVOL] = pntr->copyDate;
			if (!qPtr->isValid[BACKVOL])
			    qPtr->ids[BACKVOL] = pntr->backupID;
			if (!qPtr->isValid[ROVOL])
			    qPtr->ids[ROVOL] = pntr->cloneID;
		    }
		} else if (pntr->type == BACKVOL) {
		    if (qPtr->isValid[BACKVOL]) {
			/*do different checks, delete superflous volume */
			if (qPtr->copyDate[BACKVOL] > pntr->copyDate) {
			    /*delete the present volume . */
			    code =
				CheckAndDeleteVolume(server, apart, 0,
						     pntr->volid);
			    if (code) {
				if (!error)
				    error = code;
			    }

			} else {
			    /*delete the older volume after making sure, current one is ok */
			    code =
				CheckAndDeleteVolume(server, apart,
						     pntr->volid,
						     qPtr->ids[BACKVOL]);
			    if (code) {
				if (!error)
				    error = code;
			    }

			    qPtr->copyDate[BACKVOL] = pntr->copyDate;
			    qPtr->ids[BACKVOL] = pntr->volid;

			}
		    } else {
			qPtr->isValid[BACKVOL] = 1;
			qPtr->ids[BACKVOL] = pntr->volid;
			qPtr->copyDate[BACKVOL] = pntr->copyDate;
		    }
		} else if (pntr->type == ROVOL) {
		    if (qPtr->isValid[ROVOL]) {
			/*do different checks, delete superflous volume */
			if (qPtr->copyDate[ROVOL] > pntr->copyDate) {
			    /*delete the present volume . */
			    /*a hack */
			    code =
				CheckAndDeleteVolume(server, apart, 0,
						     pntr->volid);
			    if (code) {
				if (!error)
				    error = code;
			    }
			} else {
			    /*delete the older volume after making sure, current one is ok */
			    code =
				CheckAndDeleteVolume(server, apart,
						     pntr->volid,
						     qPtr->ids[ROVOL]);
			    if (code) {
				if (!error)
				    error = code;
			    }

			    qPtr->copyDate[ROVOL] = pntr->copyDate;
			    qPtr->ids[ROVOL] = pntr->volid;

			}
		    } else {
			qPtr->isValid[ROVOL] = 1;
			qPtr->ids[ROVOL] = pntr->volid;
			qPtr->copyDate[ROVOL] = pntr->copyDate;
		    }
		} else {
		    if (!error)
			error = VOLSERBADOP;
		}
	    } else {		/*create a fresh entry */
		qPtr = (struct aqueue *)malloc(sizeof(struct aqueue));
		if (pntr->type == RWVOL) {
		    qPtr->isValid[RWVOL] = 1;
		    qPtr->isValid[BACKVOL] = 0;
		    qPtr->isValid[ROVOL] = 0;
		    qPtr->ids[RWVOL] = pntr->volid;
		    qPtr->ids[BACKVOL] = pntr->backupID;
		    qPtr->ids[ROVOL] = pntr->cloneID;
		    qPtr->copyDate[RWVOL] = pntr->copyDate;
		    strncpy(qPtr->name, pntr->name, VOLSER_OLDMAXVOLNAME);
		    qPtr->next = NULL;
		} else if (pntr->type == BACKVOL) {
		    qPtr->isValid[RWVOL] = 0;
		    qPtr->isValid[BACKVOL] = 1;
		    qPtr->isValid[ROVOL] = 0;
		    qPtr->ids[RWVOL] = pntr->parentID;
		    qPtr->ids[BACKVOL] = pntr->volid;
		    qPtr->ids[ROVOL] = 0;
		    qPtr->copyDate[BACKVOL] = pntr->copyDate;
		    vsu_ExtractName(qPtr->name, pntr->name);
		    qPtr->next = NULL;
		} else if (pntr->type == ROVOL) {
		    qPtr->isValid[RWVOL] = 0;
		    qPtr->isValid[BACKVOL] = 0;
		    qPtr->isValid[ROVOL] = 1;
		    qPtr->ids[RWVOL] = pntr->parentID;
		    qPtr->ids[BACKVOL] = 0;
		    qPtr->ids[ROVOL] = pntr->volid;
		    qPtr->copyDate[ROVOL] = pntr->copyDate;
		    vsu_ExtractName(qPtr->name, pntr->name);
		    qPtr->next = NULL;

		}
		Lp_QAdd(myQueue, qPtr);
	    }
	    pntr++;		/*get next entry */
	} else {
	    pntr++;
	    continue;
	}
    }				/* for loop */

    return error;
}

/*report on all the active transactions on volser */
int
UV_VolserStatus(struct rx_connection *server, transDebugInfo ** rpntr,
		afs_int32 * rcount, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    transDebugEntries transInfo;

    transInfo.transDebugEntries_val = (transDebugInfo *) 0;
    transInfo.transDebugEntries_len = 0;
    tst = AFSVolMonitor(server, &transInfo);
    if (tst) {
	goto fail_UV_VolserStatus;
    }

    *rcount = transInfo.transDebugEntries_len;
    *rpntr = transInfo.transDebugEntries_val;
    rc = 1;

  fail_UV_VolserStatus:

    if (rc == 0) {
	if (transInfo.transDebugEntries_val) {
	    free(transInfo.transDebugEntries_val);
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

/*delete the volume without interacting with the vldb */
int
UV_VolumeZap(afs_cell_handle_p cellHandle, struct rx_connection *server,
	     unsigned int partition, afs_uint32 volumeId, afs_status_p st)
{
    afs_int32 rcode, ttid;
    int rc = 0;
    afs_status_t tst = 0;

    ttid = 0;

    tst = AFSVolTransCreate(server, volumeId, partition, ITOffline, &ttid);
    if (!tst) {
	tst = AFSVolDeleteVolume(server, ttid);
	if (!tst) {
	    tst = AFSVolEndTrans(server, ttid, &rcode);
	    if (!tst) {
		if (rcode) {
		    tst = rcode;
		}
	    }
	} else {
	    /*
	     * We failed to delete the volume, but we still need
	     * to end the transaction.
	     */
	    AFSVolEndTrans(server, ttid, &rcode);
	}
	rc = 1;
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}

int
UV_SetVolume(struct rx_connection *server, afs_int32 partition,
	     afs_uint32 volid, afs_int32 transflag, afs_int32 setflag,
	     unsigned int sleepTime, afs_status_p st)
{
    int rc = 0;
    afs_status_t tst = 0;
    afs_status_t etst = 0;
    afs_int32 tid = 0;
    afs_int32 rcode;

    tst = AFSVolTransCreate(server, volid, partition, transflag, &tid);
    if (tst) {
	goto fail_UV_SetVolume;
    }

    tst = AFSVolSetFlags(server, tid, setflag);
    if (tst) {
	goto fail_UV_SetVolume;
    }

    if (sleepTime) {
	sleep(sleepTime);
    }
    rc = 1;

  fail_UV_SetVolume:

    if (tid) {
	etst = AFSVolEndTrans(server, tid, &rcode);
	/* FIXME: this looks like a typo */
	if (etst || etst) {
	    if (!tst)
		tst = (etst ? etst : rcode);
	}
    }

    if (st != NULL) {
	*st = tst;
    }
    return rc;
}