death to register
[openafs.git] / src / libadmin / vos / vsprocs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * This file is a reimplementation of volser/vsproc.s.  Every attempt
12  * has been made to keep it as similar as possible to aid comprehension
13  * of the code.  For most functions, two parameters have been added
14  * the cell handle, and a status variable.  For those functions that
15  * require one, a server handle may also be added.
16  *
17  * Other changes were made to provide thread safe functions and
18  * eliminate the practice of reporting errors to STDOUT.
19  */
20
21 #include <afsconfig.h>
22 #include <afs/param.h>
23
24
25 #include "vsprocs.h"
26 #include "vosutils.h"
27 #include "lockprocs.h"
28 #include "../adminutil/afs_AdminInternal.h"
29 #include <afs/afs_AdminErrors.h>
30 #include "afs_vosAdmin.h"
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef AFS_NT40_ENV
36 #include <io.h>
37 #endif
38
39 static afs_int32 GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
40              struct qHead *myQueue, afs_int32 apart);
41
42 struct release {
43     afs_int32 time;
44     afs_int32 vldbEntryIndex;
45 };
46
47 static struct rx_connection *
48 UV_Bind(afs_cell_handle_p cellHandle, afs_int32 aserver, afs_int32 port)
49 {
50     return rx_GetCachedConnection(htonl(aserver), htons(port), VOLSERVICE_ID,
51                                   cellHandle->tokens->afs_sc[cellHandle->
52                                                              tokens->
53                                                              sc_index],
54                                   cellHandle->tokens->sc_index);
55 }
56
57
58 /* if <okvol> is allright(indicated by beibg able to
59  * start a transaction, delete the <delvol> */
60 static afs_int32
61 CheckAndDeleteVolume(struct rx_connection *aconn, afs_int32 apart,
62                      afs_uint32 okvol, afs_uint32 delvol)
63 {
64     afs_int32 error, code, tid, rcode;
65
66     error = 0;
67     code = 0;
68
69     if (okvol == 0) {
70         code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
71         if (!error && code)
72             error = code;
73         code = AFSVolDeleteVolume(aconn, tid);
74         if (!error && code)
75             error = code;
76         code = AFSVolEndTrans(aconn, tid, &rcode);
77         if (!code)
78             code = rcode;
79         if (!error && code)
80             error = code;
81         return error;
82     } else {
83         code = AFSVolTransCreate(aconn, okvol, apart, ITOffline, &tid);
84         if (!code) {
85             code = AFSVolEndTrans(aconn, tid, &rcode);
86             if (!code)
87                 code = rcode;
88             if (!error && code)
89                 error = code;
90             code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
91             if (!error && code)
92                 error = code;
93             code = AFSVolDeleteVolume(aconn, tid);
94             if (!error && code)
95                 error = code;
96             code = AFSVolEndTrans(aconn, tid, &rcode);
97             if (!code)
98                 code = rcode;
99             if (!error && code)
100                 error = code;
101         } else
102             error = code;
103         return error;
104     }
105 }
106
107 /* forcibly remove a volume.  Very dangerous call */
108 int
109 UV_NukeVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
110               unsigned int partition, afs_uint32 volumeId, afs_status_p st)
111 {
112     int rc = 0;
113     afs_status_t tst = 0;
114
115     tst = AFSVolNukeVolume(server, partition, volumeId);
116
117     if (!tst) {
118         rc = 1;
119     }
120
121     if (st != NULL) {
122         *st = tst;
123     }
124     return rc;
125 }
126
127 /* create a volume, given a server, partition number, volume name --> sends
128 * back new vol id in <anewid>*/
129 int
130 UV_CreateVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
131                 unsigned int partition, char *volumeName,
132                 unsigned int quota, afs_uint32 *volumeId, afs_status_p st)
133 {
134     int rc = 0;
135     afs_status_t tst = 0;
136     afs_int32 tid = 0;
137     afs_int32 rcode;
138     struct nvldbentry entry;
139     struct volintInfo tstatus;
140
141     memset(&tstatus, 0, sizeof(tstatus));
142     tstatus.dayUse = -1;
143     tstatus.maxquota = quota;
144
145     /* next the next 3 available ids from the VLDB */
146     tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 3, volumeId);
147     if (tst) {
148         goto fail_UV_CreateVolume;
149     }
150
151     tst =
152         AFSVolCreateVolume(server, partition, volumeName, volser_RW, 0,
153                            volumeId, &tid);
154     if (tst) {
155         goto fail_UV_CreateVolume;
156     }
157
158     AFSVolSetInfo(server, tid, &tstatus);
159
160     tst = AFSVolSetFlags(server, tid, 0);
161     if (tst) {
162         goto fail_UV_CreateVolume;
163     }
164
165     /* set up the vldb entry for this volume */
166     strncpy(entry.name, volumeName, VOLSER_OLDMAXVOLNAME);
167     entry.nServers = 1;
168     entry.serverNumber[0] = ntohl(rx_HostOf(rx_PeerOf(server)));
169     entry.serverPartition[0] = partition;
170     entry.flags = RW_EXISTS;
171     entry.serverFlags[0] = ITSRWVOL;
172     entry.volumeId[RWVOL] = *volumeId;
173     entry.volumeId[ROVOL] = *volumeId + 1;
174     entry.volumeId[BACKVOL] = *volumeId + 2;
175     entry.cloneId = 0;
176
177     if (!VLDB_CreateEntry(cellHandle, &entry, &tst)) {
178         AFSVolDeleteVolume(server, tid);
179         goto fail_UV_CreateVolume;
180     }
181
182     tst = AFSVolEndTrans(server, tid, &rcode);
183     tid = 0;
184     if (tst) {
185         goto fail_UV_CreateVolume;
186     }
187     rc = 1;
188
189   fail_UV_CreateVolume:
190
191     if (tid != 0) {
192         AFSVolEndTrans(server, tid, &rcode);
193     }
194
195     if (st != NULL) {
196         *st = tst;
197     }
198     return rc;
199 }
200
201
202 /* Delete the volume <volid>on <aserver> <apart>
203  * the physical entry gets removed from the vldb only if the ref count 
204  * becomes zero
205  */
206 int
207 UV_DeleteVolume(afs_cell_handle_p cellHandle, struct rx_connection *server,
208                 unsigned int partition, afs_uint32 volumeId,
209                 afs_status_p st)
210 {
211     int rc = 0;
212     afs_status_t tst = 0;
213     afs_status_t temp = 0;
214     int serverAddr = ntohl(rx_HostOf(rx_PeerOf(server)));
215
216     afs_int32 ttid = 0;
217     afs_int32 rcode;
218     struct nvldbentry entry;
219     int islocked = 0;
220     afs_int32 avoltype = -1, vtype;
221     int notondisk = 0, notinvldb = 0;
222
223     /* Find and read the VLDB entry for this volume */
224     tst =
225         ubik_VL_SetLock(cellHandle->vos, 0, volumeId, avoltype,
226                   VLOP_DELETE);
227     if (tst) {
228         if (tst != VL_NOENT) {
229             goto fail_UV_DeleteVolume;
230         }
231         notinvldb = 1;
232     } else {
233         islocked = 1;
234
235         if (!aVLDB_GetEntryByID(cellHandle, volumeId, avoltype, &entry, &tst)) {
236             goto fail_UV_DeleteVolume;
237         }
238
239     }
240
241     /* Whether volume is in the VLDB or not. Delete the volume on disk */
242     tst = AFSVolTransCreate(server, volumeId, partition, ITOffline, &ttid);
243     if (tst) {
244         if (tst == VNOVOL) {
245             notondisk = 1;
246         } else {
247             goto fail_UV_DeleteVolume;
248         }
249     } else {
250         tst = AFSVolDeleteVolume(server, ttid);
251         if (tst) {
252             goto fail_UV_DeleteVolume;
253         }
254         tst = AFSVolEndTrans(server, ttid, &rcode);
255         tst = (tst ? tst : rcode);
256         ttid = 0;
257         if (tst) {
258             goto fail_UV_DeleteVolume;
259         }
260     }
261
262     if (notinvldb) {
263         goto fail_UV_DeleteVolume;
264     }
265
266     if (volumeId == entry.volumeId[BACKVOL]) {
267         if (!(entry.flags & BACK_EXISTS)
268             || !Lp_Match(cellHandle, &entry, serverAddr, partition, &tst)) {
269             notinvldb = 2;
270             goto fail_UV_DeleteVolume;
271         }
272
273         entry.flags &= ~BACK_EXISTS;
274         vtype = BACKVOL;
275     }
276
277     else if (volumeId == entry.volumeId[ROVOL]) {
278         if (!Lp_ROMatch(cellHandle, &entry, serverAddr, partition, &tst)) {
279             notinvldb = 2;
280             goto fail_UV_DeleteVolume;
281         }
282
283         Lp_SetROValue(cellHandle, &entry, serverAddr, partition, 0, 0);
284         entry.nServers--;
285         if (!Lp_ROMatch(cellHandle, &entry, 0, 0, &tst)) {
286             entry.flags &= ~RO_EXISTS;
287         }
288         vtype = ROVOL;
289     }
290
291     else if (volumeId == entry.volumeId[RWVOL]) {
292         if (!(entry.flags & RW_EXISTS)
293             || !Lp_Match(cellHandle, &entry, serverAddr, partition, &tst)) {
294             notinvldb = 2;
295             goto fail_UV_DeleteVolume;
296         }
297
298         /* Delete backup if it exists */
299         tst =
300             AFSVolTransCreate(server, entry.volumeId[BACKVOL], partition,
301                               ITOffline, &ttid);
302         if (!tst) {
303             tst = AFSVolDeleteVolume(server, ttid);
304             if (tst) {
305                 goto fail_UV_DeleteVolume;
306             }
307             tst = AFSVolEndTrans(server, ttid, &rcode);
308             ttid = 0;
309             tst = (tst ? tst : rcode);
310             if (tst) {
311                 goto fail_UV_DeleteVolume;
312             }
313         }
314
315         Lp_SetRWValue(cellHandle, &entry, serverAddr, partition, 0L, 0L);
316         entry.nServers--;
317         entry.flags &= ~(BACK_EXISTS | RW_EXISTS);
318         vtype = RWVOL;
319
320     }
321
322     else {
323         notinvldb = 2;          /* Not found on this server and partition */
324         goto fail_UV_DeleteVolume;
325     }
326
327     if ((entry.nServers <= 0) || !(entry.flags & (RO_EXISTS | RW_EXISTS))) {
328         tst = ubik_VL_DeleteEntry(cellHandle->vos, 0, volumeId, vtype);
329         if (tst) {
330             goto fail_UV_DeleteVolume;
331         }
332     } else {
333         if (!VLDB_ReplaceEntry
334             (cellHandle, volumeId, vtype, &entry,
335              (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
336             goto fail_UV_DeleteVolume;
337         }
338     }
339     islocked = 0;
340     rc = 1;
341
342   fail_UV_DeleteVolume:
343
344     if (notondisk && notinvldb) {
345         if (!tst)
346             tst = ADMVOSVOLUMENOEXIST;
347     }
348
349     if (ttid) {
350         temp = AFSVolEndTrans(server, ttid, &rcode);
351         temp = (temp ? temp : rcode);
352         if (temp) {
353             if (!tst)
354                 tst = temp;
355         }
356     }
357
358     if (islocked) {
359         temp =
360             ubik_VL_ReleaseLock(cellHandle->vos, 0, volumeId, -1,
361                       (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
362         if (temp) {
363             if (!tst)
364                 tst = temp;
365         }
366     }
367
368     if (st != NULL) {
369         *st = tst;
370     }
371     return rc;
372 }
373
374 #define ONERR(ec, es, ep) if (ec) { fprintf(STDERR, (es), (ep)); error = (ec); goto mfail; }
375
376 /* Move volume <afromvol> on <afromserver> <afrompart> to <atoserver>
377  * <atopart>. The operation is almost idempotent 
378  */
379
380 int
381 UV_MoveVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
382               afs_int32 afromserver, afs_int32 afrompart, afs_int32 atoserver,
383               afs_int32 atopart, afs_status_p st)
384 {
385     int rc = 0;
386     afs_status_t tst = 0;
387     afs_status_t etst = 0;
388     struct rx_connection *toconn, *fromconn;
389     afs_int32 fromtid, totid, clonetid;
390     char vname[64];
391     char *volName = 0;
392     char tmpName[VOLSER_MAXVOLNAME + 1];
393     afs_int32 rcode;
394     afs_int32 fromDate;
395     struct restoreCookie cookie;
396     afs_uint32 newVol, volid, backupId;
397     struct volser_status tstatus;
398     struct destServer destination;
399
400     struct nvldbentry entry;
401     int islocked, pntg;
402     afs_int32 error;
403     int same;
404     afs_int32 store_flags;
405
406 #ifdef  ENABLE_BUGFIX_1165
407     volEntries volumeInfo;
408     struct volintInfo *infop = 0;
409 #endif
410
411     islocked = 0;
412     fromconn = (struct rx_connection *)0;
413     toconn = (struct rx_connection *)0;
414     fromtid = 0;
415     totid = 0;
416     clonetid = 0;
417     error = 0;
418     volid = 0;
419     pntg = 0;
420     backupId = 0;
421     newVol = 0;
422
423     if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
424         goto fail_UV_MoveVolume;
425     }
426
427     if (entry.volumeId[RWVOL] != afromvol) {
428         tst = ADMVOSVOLUMEMOVERWONLY;
429         goto fail_UV_MoveVolume;
430     }
431
432     tst =
433         ubik_VL_SetLock(cellHandle->vos, 0, afromvol, RWVOL, VLOP_MOVE);
434     if (tst) {
435         goto fail_UV_MoveVolume;
436     }
437     islocked = 1;
438
439     if (!aVLDB_GetEntryByID(cellHandle, afromvol, RWVOL, &entry, &tst)) {
440         goto fail_UV_MoveVolume;
441     }
442
443     backupId = entry.volumeId[BACKVOL];
444
445     if (!Lp_Match(cellHandle, &entry, afromserver, afrompart, &tst)) {
446         /* the from server and partition do not exist in the vldb entry corresponding to volid */
447         if (!Lp_Match(cellHandle, &entry, atoserver, atopart, &tst)) {
448             /* the to server and partition do not exist in the vldb entry corresponding to volid */
449             tst =
450                 ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
451                           (LOCKREL_OPCODE | LOCKREL_AFSID |
452                            LOCKREL_TIMESTAMP));
453             if (tst) {
454                 goto fail_UV_MoveVolume;
455             }
456             tst = VOLSERVOLMOVED;
457             goto fail_UV_MoveVolume;
458         }
459
460         /* delete the volume afromvol on src_server */
461         /* from-info does not exist but to-info does =>
462          * we have already done the move, but the volume
463          * may still be existing physically on from fileserver
464          */
465         fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
466         fromtid = 0;
467         pntg = 1;
468
469         tst =
470             AFSVolTransCreate(fromconn, afromvol, afrompart, ITOffline,
471                               &fromtid);
472         if (!tst) {             /* volume exists - delete it */
473             tst =
474                 AFSVolSetFlags(fromconn, fromtid,
475                                VTDeleteOnSalvage | VTOutOfService);
476             if (tst) {
477                 goto fail_UV_MoveVolume;
478             }
479
480             tst = AFSVolDeleteVolume(fromconn, fromtid);
481             if (tst) {
482                 goto fail_UV_MoveVolume;
483             }
484
485             tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
486             fromtid = 0;
487             if (!tst)
488                 tst = rcode;
489             if (tst) {
490                 goto fail_UV_MoveVolume;
491             }
492         }
493
494         /*delete the backup volume now */
495         fromtid = 0;
496         tst =
497             AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
498                               &fromtid);
499         if (!tst) {             /* backup volume exists - delete it */
500             tst =
501                 AFSVolSetFlags(fromconn, fromtid,
502                                VTDeleteOnSalvage | VTOutOfService);
503             if (tst) {
504                 goto fail_UV_MoveVolume;
505             }
506
507             tst = AFSVolDeleteVolume(fromconn, fromtid);
508             if (tst) {
509                 goto fail_UV_MoveVolume;
510             }
511
512             tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
513             fromtid = 0;
514             if (!tst)
515                 tst = rcode;
516             if (tst) {
517                 goto fail_UV_MoveVolume;
518             }
519         }
520
521         fromtid = 0;
522         error = 0;
523         goto fail_UV_MoveVolume;
524     }
525
526     /* From-info matches the vldb info about volid,
527      * its ok start the move operation, the backup volume 
528      * on the old site is deleted in the process 
529      */
530     if (afrompart == atopart) {
531         if (!VLDB_IsSameAddrs
532             (cellHandle, afromserver, atoserver, &same, &tst)) {
533             goto fail_UV_MoveVolume;
534         }
535         if (same) {
536             tst = VOLSERVOLMOVED;
537             goto fail_UV_MoveVolume;
538         }
539     }
540
541     pntg = 1;
542     toconn = UV_Bind(cellHandle, atoserver, AFSCONF_VOLUMEPORT);        /* get connections to the servers */
543     fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
544     fromtid = totid = 0;        /* initialize to uncreated */
545
546     /* ***
547      * clone the read/write volume locally.
548      * ***/
549
550     tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
551     if (tst) {
552         goto fail_UV_MoveVolume;
553     }
554
555     /* Get a clone id */
556     newVol = 0;
557     tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &newVol);
558     if (tst) {
559         goto fail_UV_MoveVolume;
560     }
561
562     /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
563     strcpy(vname, "move-clone-temp");
564     tst = AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &newVol);
565     if (tst) {
566         goto fail_UV_MoveVolume;
567     }
568
569     /* lookup the name of the volume we just cloned */
570     volid = afromvol;
571     tst = AFSVolGetName(fromconn, fromtid, &volName);
572     if (tst) {
573         goto fail_UV_MoveVolume;
574     }
575
576     rcode = 0;
577     tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
578     fromtid = 0;
579     if (!tst)
580         tst = rcode;
581     if (tst) {
582         goto fail_UV_MoveVolume;
583     }
584
585     /* ***
586      * Create the destination volume
587      * ***/
588
589     tst =
590         AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline, &clonetid);
591     if (tst) {
592         goto fail_UV_MoveVolume;
593     }
594     tst = AFSVolSetFlags(fromconn, clonetid, VTDeleteOnSalvage | VTOutOfService);       /*redundant */
595     if (tst) {
596         goto fail_UV_MoveVolume;
597     }
598
599     /* remember time from which we've dumped the volume */
600     tst = AFSVolGetStatus(fromconn, clonetid, &tstatus);
601     if (tst) {
602         goto fail_UV_MoveVolume;
603     }
604
605     fromDate = tstatus.creationDate - CLOCKSKEW;
606
607 #ifdef  ENABLE_BUGFIX_1165
608     /*
609      * Get the internal volume state from the source volume. We'll use such info (i.e. dayUse)
610      * to copy it to the new volume (via AFSSetInfo later on) so that when we move volumes we
611      * don't use this information...
612      */
613     volumeInfo.volEntries_val = (volintInfo *) 0;       /*this hints the stub to allocate space */
614     volumeInfo.volEntries_len = 0;
615     tst = AFSVolListOneVolume(fromconn, afrompart, afromvol, &volumeInfo);
616     if (tst) {
617         goto fail_UV_MoveVolume;
618     }
619
620     infop = (volintInfo *) volumeInfo.volEntries_val;
621     infop->maxquota = -1;       /* Else it will replace the default quota */
622 #endif
623
624     /* create a volume on the target machine */
625     volid = afromvol;
626     tst = AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
627     if (!tst) {                 /*delete the existing volume */
628
629         tst = AFSVolDeleteVolume(toconn, totid);
630         if (tst) {
631             goto fail_UV_MoveVolume;
632         }
633
634         tst = AFSVolEndTrans(toconn, totid, &rcode);
635         totid = 0;
636         if (!tst)
637             tst = rcode;
638         if (tst) {
639             goto fail_UV_MoveVolume;
640         }
641
642     }
643
644     tst =
645         AFSVolCreateVolume(toconn, atopart, volName, volser_RW, volid, &volid,
646                            &totid);
647     if (tst) {
648         goto fail_UV_MoveVolume;
649     }
650
651     strncpy(tmpName, volName, VOLSER_OLDMAXVOLNAME);
652     free(volName);
653     volName = NULL;
654
655     tst = AFSVolSetFlags(toconn, totid, (VTDeleteOnSalvage | VTOutOfService));
656     if (tst) {
657         goto fail_UV_MoveVolume;
658     }
659
660     /***
661      * Now dump the clone to the new volume
662      ***/
663
664     destination.destHost = atoserver;
665     destination.destPort = AFSCONF_VOLUMEPORT;
666     destination.destSSID = 1;
667
668     /* Copy the clone to the new volume */
669     strncpy(cookie.name, tmpName, VOLSER_OLDMAXVOLNAME);
670     cookie.type = RWVOL;
671     cookie.parent = entry.volumeId[RWVOL];
672     cookie.clone = 0;
673     tst = AFSVolForward(fromconn, clonetid, 0, &destination, totid, &cookie);
674     if (tst) {
675         goto fail_UV_MoveVolume;
676     }
677
678     tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
679     if (!tst)
680         tst = rcode;
681     clonetid = 0;
682     if (tst) {
683         goto fail_UV_MoveVolume;
684     }
685
686     /* ***
687      * reattach to the main-line volume, and incrementally dump it.
688      * ***/
689
690     tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
691     if (tst) {
692         goto fail_UV_MoveVolume;
693     }
694
695     /* now do the incremental */
696     tst =
697         AFSVolForward(fromconn, fromtid, fromDate, &destination, totid,
698                       &cookie);
699     if (tst) {
700         goto fail_UV_MoveVolume;
701     }
702
703     /* now adjust the flags so that the new volume becomes official */
704     tst = AFSVolSetFlags(fromconn, fromtid, VTOutOfService);
705     if (tst) {
706         goto fail_UV_MoveVolume;
707     }
708
709     tst = AFSVolSetFlags(toconn, totid, 0);
710     if (tst) {
711         goto fail_UV_MoveVolume;
712     }
713 #ifdef  ENABLE_BUGFIX_1165
714     tst = AFSVolSetInfo(toconn, totid, infop);
715     if (tst) {
716         goto fail_UV_MoveVolume;
717     }
718 #endif
719
720     /* put new volume online */
721     tst = AFSVolEndTrans(toconn, totid, &rcode);
722     totid = 0;
723     if (!tst)
724         tst = rcode;
725     if (tst) {
726         goto fail_UV_MoveVolume;
727     }
728
729     Lp_SetRWValue(cellHandle, &entry, afromserver, afrompart, atoserver,
730                   atopart);
731     store_flags = entry.flags;
732     entry.flags &= ~BACK_EXISTS;
733
734     if (!VLDB_ReplaceEntry
735         (cellHandle, afromvol, -1, &entry,
736          (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
737         goto fail_UV_MoveVolume;
738     }
739     entry.flags = store_flags;
740     islocked = 0;
741
742     if (atoserver != afromserver) {
743         /* set forwarding pointer for moved volumes */
744         tst = AFSVolSetForwarding(fromconn, fromtid, htonl(atoserver));
745         if (tst) {
746             goto fail_UV_MoveVolume;
747         }
748     }
749
750     tst = AFSVolDeleteVolume(fromconn, fromtid);        /* zap original volume */
751     if (tst) {
752         goto fail_UV_MoveVolume;
753     }
754
755     tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
756     fromtid = 0;
757     if (!tst)
758         tst = rcode;
759     if (tst) {
760         goto fail_UV_MoveVolume;
761     }
762
763     /* Delete the backup volume on the original site */
764     tst =
765         AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline, &fromtid);
766     if (!tst) {
767         tst =
768             AFSVolSetFlags(fromconn, fromtid,
769                            VTDeleteOnSalvage | VTOutOfService);
770         if (tst) {
771             goto fail_UV_MoveVolume;
772         }
773
774         tst = AFSVolDeleteVolume(fromconn, fromtid);
775         if (tst) {
776             goto fail_UV_MoveVolume;
777         }
778
779         tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
780         fromtid = 0;
781         if (!tst)
782             tst = rcode;
783         if (tst) {
784             goto fail_UV_MoveVolume;
785         }
786
787     } else
788         tst = 0;                /* no backup volume? that's okay */
789
790     fromtid = 0;
791
792     tst =
793         AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline, &clonetid);
794     if (tst) {
795         goto fail_UV_MoveVolume;
796     }
797
798     /* now delete the clone */
799
800     tst = AFSVolDeleteVolume(fromconn, clonetid);
801     if (tst) {
802         goto fail_UV_MoveVolume;
803     }
804
805     tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
806     if (!tst)
807         tst = rcode;
808     clonetid = 0;
809     if (tst) {
810         goto fail_UV_MoveVolume;
811     }
812
813     /* fall through */
814     /* END OF MOVE */
815
816     /* normal cleanup code */
817
818     if (islocked) {
819         etst =
820             ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
821                       (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
822         if (etst) {
823             if (!tst)
824                 tst = etst;
825         }
826     }
827
828     if (fromtid) {
829         etst = AFSVolEndTrans(fromconn, fromtid, &rcode);
830         if (etst || rcode) {
831             if (!tst)
832                 tst = (etst ? etst : rcode);
833         }
834     }
835
836     if (clonetid) {
837         etst = AFSVolEndTrans(fromconn, clonetid, &rcode);
838         if (etst || rcode) {
839             if (!tst)
840                 tst = (etst ? etst : rcode);
841         }
842     }
843
844     if (totid) {
845         etst = AFSVolEndTrans(toconn, totid, &rcode);
846         if (etst) {
847             if (!tst)
848                 tst = (etst ? etst : rcode);
849         }
850     }
851     if (volName)
852         free(volName);
853 #ifdef  ENABLE_BUGFIX_1165
854     if (infop)
855         free(infop);
856 #endif
857     if (fromconn)
858         rx_ReleaseCachedConnection(fromconn);
859     if (toconn)
860         rx_ReleaseCachedConnection(toconn);
861
862     rc = 1;
863     if (st != NULL) {
864         *st = tst;
865     }
866     return rc;
867
868     /* come here only when the sky falls */
869
870   fail_UV_MoveVolume:
871
872     /* unlock VLDB entry */
873     if (islocked)
874         ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
875                   (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
876
877     if (clonetid)
878         AFSVolEndTrans(fromconn, clonetid, &rcode);
879     if (totid)
880         AFSVolEndTrans(toconn, totid, &rcode);
881     if (fromtid) {              /* put it on-line */
882         AFSVolSetFlags(fromconn, fromtid, 0);
883         AFSVolEndTrans(fromconn, fromtid, &rcode);
884     }
885
886     if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
887         goto done;
888     }
889
890     /* Delete either the volume on the source location or the target location. 
891      * If the vldb entry still points to the source location, then we know the
892      * volume move didn't finish so we remove the volume from the target 
893      * location. Otherwise, we remove the volume from the source location.
894      */
895     if (Lp_Match(cellHandle, &entry, afromserver, afrompart, &tst)) {   /* didn't move - delete target volume */
896
897         if (volid && toconn) {
898             tst =
899                 AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
900             if (!tst) {
901                 AFSVolSetFlags(toconn, totid,
902                                VTDeleteOnSalvage | VTOutOfService);
903                 AFSVolDeleteVolume(toconn, totid);
904                 AFSVolEndTrans(toconn, totid, &rcode);
905             }
906         }
907
908         /* put source volume on-line */
909         if (fromconn) {
910             tst =
911                 AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
912                                   &fromtid);
913             if (!tst) {
914                 AFSVolSetFlags(fromconn, fromtid, 0);
915                 AFSVolEndTrans(fromconn, fromtid, &rcode);
916             }
917         }
918     } else {                    /* yep, move complete */
919         /* delete backup volume */
920         if (fromconn) {
921             tst =
922                 AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
923                                   &fromtid);
924             if (!tst) {
925                 AFSVolSetFlags(fromconn, fromtid,
926                                VTDeleteOnSalvage | VTOutOfService);
927                 AFSVolDeleteVolume(fromconn, fromtid);
928                 AFSVolEndTrans(fromconn, fromtid, &rcode);
929             }
930
931             /* delete source volume */
932             tst =
933                 AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
934                                   &fromtid);
935             if (!tst) {
936                 AFSVolSetFlags(fromconn, fromtid,
937                                VTDeleteOnSalvage | VTOutOfService);
938                 if (atoserver != afromserver)
939                     AFSVolSetForwarding(fromconn, fromtid, htonl(atoserver));
940                 AFSVolDeleteVolume(fromconn, fromtid);
941                 AFSVolEndTrans(fromconn, fromtid, &rcode);
942             }
943         }
944     }
945
946     /* common cleanup - delete local clone */
947     if (newVol) {
948         tst =
949             AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline,
950                               &clonetid);
951         if (!tst) {
952             AFSVolDeleteVolume(fromconn, clonetid);
953             AFSVolEndTrans(fromconn, clonetid, &rcode);
954         }
955     }
956
957     /* unlock VLDB entry */
958     ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
959               (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
960
961   done:                 /* routine cleanup */
962     if (volName)
963         free(volName);
964 #ifdef  ENABLE_BUGFIX_1165
965     if (infop)
966         free(infop);
967 #endif
968     if (fromconn)
969         rx_ReleaseCachedConnection(fromconn);
970     if (toconn)
971         rx_ReleaseCachedConnection(toconn);
972
973     if (st != NULL) {
974         *st = tst;
975     }
976     return rc;
977 }
978
979 /* Make a new backup of volume <avolid> on <aserver> and <apart> 
980  * if one already exists, update it 
981  */
982
983 int
984 UV_BackupVolume(afs_cell_handle_p cellHandle, afs_int32 aserver,
985                 afs_int32 apart, afs_uint32 avolid, afs_status_p st)
986 {
987     int rc = 0;
988     afs_status_t tst = 0, temp = 0;
989     afs_int32 ttid = 0, btid = 0;
990     afs_uint32 backupID;
991     afs_int32 rcode = 0;
992     char vname[VOLSER_MAXVOLNAME + 1];
993     struct nvldbentry entry;
994     int vldblocked = 0, vldbmod = 0, backexists = 1;
995     struct rx_connection *aconn = UV_Bind(cellHandle, aserver,
996                                           AFSCONF_VOLUMEPORT);
997
998
999     /* the calls to VLDB will succeed only if avolid is a RW volume,
1000      * since we are following the RW hash chain for searching */
1001     if (!aVLDB_GetEntryByID(cellHandle, avolid, RWVOL, &entry, &tst)) {
1002         goto fail_UV_BackupVolume;
1003     }
1004
1005     /* These operations require the VLDB be locked since it means the VLDB
1006      * will change or the vldb is already locked.
1007      */
1008     if (!(entry.flags & BACK_EXISTS) || /* backup volume doesnt exist */
1009         (entry.flags & VLOP_ALLOPERS) ||        /* vldb lock already held */
1010         (entry.volumeId[BACKVOL] == INVALID_BID)) {
1011         /* no assigned backup volume id */
1012
1013         tst =
1014             ubik_VL_SetLock(cellHandle->vos, 0, avolid, RWVOL,
1015                       VLOP_BACKUP);
1016         if (tst) {
1017             goto fail_UV_BackupVolume;
1018         }
1019         vldblocked = 1;
1020
1021         /* Reread the vldb entry */
1022         if (!aVLDB_GetEntryByID(cellHandle, avolid, RWVOL, &entry, &tst)) {
1023             goto fail_UV_BackupVolume;
1024         }
1025     }
1026
1027     if (!ISNAMEVALID(entry.name)) {
1028         tst = VOLSERBADNAME;
1029         goto fail_UV_BackupVolume;
1030     }
1031
1032     backupID = entry.volumeId[BACKVOL];
1033     if (backupID == INVALID_BID) {
1034         /* Get a backup volume id from the VLDB and update the vldb
1035          * entry with it. 
1036          */
1037         tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &backupID);
1038         if (tst) {
1039             goto fail_UV_BackupVolume;
1040         }
1041         entry.volumeId[BACKVOL] = backupID;
1042         vldbmod = 1;
1043     }
1044
1045     /* Test to see if the backup volume exists by trying to create
1046      * a transaction on the backup volume. We've assumed the backup exists.
1047      */
1048     tst = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
1049     if (tst) {
1050         if (tst != VNOVOL) {
1051             goto fail_UV_BackupVolume;
1052         }
1053         backexists = 0;         /* backup volume does not exist */
1054     }
1055     if (btid) {
1056         tst = AFSVolEndTrans(aconn, btid, &rcode);
1057         btid = 0;
1058         if (tst || rcode) {
1059             tst = (tst ? tst : rcode);
1060             goto fail_UV_BackupVolume;
1061         }
1062     }
1063
1064     /* Now go ahead and try to clone the RW volume.
1065      * First start a transaction on the RW volume 
1066      */
1067     tst = AFSVolTransCreate(aconn, avolid, apart, ITBusy, &ttid);
1068     if (tst) {
1069         goto fail_UV_BackupVolume;
1070     }
1071
1072     /* Clone or reclone the volume, depending on whether the backup 
1073      * volume exists or not
1074      */
1075     if (backexists) {
1076         tst = AFSVolReClone(aconn, ttid, backupID);
1077         if (tst) {
1078             goto fail_UV_BackupVolume;
1079         }
1080     } else {
1081         strcpy(vname, entry.name);
1082         strcat(vname, ".backup");
1083
1084         tst = AFSVolClone(aconn, ttid, 0, backupVolume, vname, &backupID);
1085         if (tst) {
1086             goto fail_UV_BackupVolume;
1087         }
1088     }
1089
1090     /* End transaction on the RW volume */
1091     tst = AFSVolEndTrans(aconn, ttid, &rcode);
1092     ttid = 0;
1093     if (tst || rcode) {
1094         tst = (tst ? tst : rcode);
1095         goto fail_UV_BackupVolume;
1096     }
1097
1098     /* Mork vldb as backup exists */
1099     if (!(entry.flags & BACK_EXISTS)) {
1100         entry.flags |= BACK_EXISTS;
1101         vldbmod = 1;
1102     }
1103
1104     /* Now go back to the backup volume and bring it on line */
1105     tst = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
1106     if (tst) {
1107         goto fail_UV_BackupVolume;
1108     }
1109
1110     tst = AFSVolSetFlags(aconn, btid, 0);
1111     if (tst) {
1112         goto fail_UV_BackupVolume;
1113     }
1114
1115     tst = AFSVolEndTrans(aconn, btid, &rcode);
1116     btid = 0;
1117     if (tst || rcode) {
1118         tst = (tst ? tst : rcode);
1119         goto fail_UV_BackupVolume;
1120     }
1121     rc = 1;
1122
1123     /* Will update the vldb below */
1124
1125   fail_UV_BackupVolume:
1126
1127     if (ttid) {
1128         temp = AFSVolEndTrans(aconn, ttid, &rcode);
1129         if (temp || rcode) {
1130             if (!tst)
1131                 tst = (temp ? temp : rcode);
1132         }
1133     }
1134
1135     if (btid) {
1136         temp = AFSVolEndTrans(aconn, btid, &rcode);
1137         if (temp || rcode) {
1138             if (!tst)
1139                 tst = (temp ? temp : rcode);
1140         }
1141     }
1142
1143     /* Now update the vldb - if modified */
1144     if (vldblocked) {
1145         if (vldbmod) {
1146             if (!VLDB_ReplaceEntry
1147                 (cellHandle, avolid, RWVOL, &entry,
1148                  (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP),
1149                  &temp)) {
1150                 if (!tst) {
1151                     tst = temp;
1152                 }
1153             }
1154         } else {
1155             temp =
1156                 ubik_VL_ReleaseLock(cellHandle->vos, 0, avolid, RWVOL,
1157                           (LOCKREL_OPCODE | LOCKREL_AFSID |
1158                            LOCKREL_TIMESTAMP));
1159             if (temp) {
1160                 if (!tst) {
1161                     tst = temp;
1162                 }
1163             }
1164         }
1165     }
1166
1167     if (aconn) {
1168         rx_ReleaseCachedConnection(aconn);
1169     }
1170
1171     if (st != NULL) {
1172         *st = tst;
1173     }
1174     return rc;
1175 }
1176
1177 static int
1178 DelVol(struct rx_connection *conn, afs_uint32 vid, afs_int32 part,
1179        afs_int32 flags)
1180 {
1181     afs_int32 acode, ccode, rcode, tid;
1182     ccode = rcode = tid = 0;
1183
1184     acode = AFSVolTransCreate(conn, vid, part, flags, &tid);
1185     if (!acode) {               /* It really was there */
1186         acode = AFSVolDeleteVolume(conn, tid);
1187         ccode = AFSVolEndTrans(conn, tid, &rcode);
1188         if (!ccode)
1189             ccode = rcode;
1190     }
1191
1192     return acode;
1193 }
1194
1195 #define ONERROR(ec, ep, es) if (ec) { fprintf(STDERR, (es), (ep)); error = (ec); goto rfail; }
1196 #define ERROREXIT(ec) { error = (ec); goto rfail; }
1197
1198 #if 0                           /* doesn't appear to be used, why compile it */
1199 static int
1200 CloneVol(afs_cell_handle_p cellHandle, struct rx_connection *conn,
1201          afs_uint32 rwvid, afs_int32 part, afs_uint32 * rovidp, int nottemp,
1202          struct nvldbentry *entry, afs_int32 * vidCreateDate, afs_status_p st)
1203 {
1204     int rc = 0;
1205     afs_status_t tst = 0, etst = 0;
1206     afs_int32 rcode = 0, tid = 0;
1207     struct volser_status volstatus;
1208     char vname[64];
1209
1210     /* Begin transaction on RW volume marking it busy (clients will wait) */
1211     tst = AFSVolTransCreate(conn, rwvid, part, ITBusy, &tid);
1212     if (tst) {
1213         goto fail_CloneVol;
1214     }
1215
1216     /* Get the RO volume id. Allocate a new one if need to */
1217     *rovidp = entry->volumeId[ROVOL];
1218     if (*rovidp == INVALID_BID) {
1219         tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, rovidp);
1220         if (tst) {
1221             goto fail_CloneVol;
1222         }
1223
1224         entry->volumeId[ROVOL] = *rovidp;
1225     }
1226
1227     /* If we are creating the ro clone, what are we calling it.
1228      * Depends on whether its a temporary clone or not.
1229      */
1230     if (nottemp) {
1231         strcpy(vname, entry->name);
1232         strcat(vname, ".readonly");
1233     } else {
1234         strcpy(vname, "readonly-clone-temp");   /* Should be unique? */
1235     }
1236
1237     /* Create the new clone. If it exists, then reclone it */
1238     tst = AFSVolClone(conn, tid, 0, readonlyVolume, vname, rovidp);
1239     if (tst == VVOLEXISTS) {
1240         tst = AFSVolReClone(conn, tid, *rovidp);
1241         if (tst) {
1242             goto fail_CloneVol;
1243         }
1244     }
1245     if (tst) {
1246         goto fail_CloneVol;
1247     }
1248
1249     /* Bring the volume back on-line as soon as possible */
1250     if (nottemp) {
1251         afs_int32 fromtid = 0;
1252
1253         /* Now bring the RO clone on-line */
1254         tst = AFSVolTransCreate(conn, *rovidp, part, ITOffline, &fromtid);
1255         if (tst) {
1256             goto fail_CloneVol;
1257         }
1258
1259         tst = AFSVolSetFlags(conn, fromtid, 0);
1260         if (tst) {
1261             goto fail_CloneVol;
1262         }
1263
1264         tst = AFSVolEndTrans(conn, fromtid, &rcode);
1265         fromtid = 0;
1266         if (!tst)
1267             tst = rcode;
1268         if (tst) {
1269             goto fail_CloneVol;
1270         }
1271     }
1272
1273     /* Get the time the RW was created for return information */
1274     tst = AFSVolGetStatus(conn, tid, &volstatus);
1275     if (tst) {
1276         goto fail_CloneVol;
1277     }
1278     *vidCreateDate = volstatus.creationDate;
1279     rc = 1;
1280
1281   fail_CloneVol:
1282
1283     if (tid) {
1284         tst = AFSVolEndTrans(conn, tid, &rcode);
1285         tid = 0;
1286         if (!tst)
1287             tst = rcode;
1288         if (tst) {
1289             rc = 0;
1290             goto fail_CloneVol;
1291         }
1292     }
1293
1294     if (st != NULL) {
1295         *st = tst;
1296     }
1297     return rc;
1298 }
1299 #endif
1300
1301 /* Get a "transaction" on this replica.  Create the volume 
1302  * if necessary.  Return the time from which a dump should
1303  * be made (0 if it's a new volume)
1304  */
1305 static int
1306 GetTrans(afs_cell_handle_p cellHandle, struct nvldbentry *vldbEntryPtr,
1307          afs_int32 index, struct rx_connection **connPtr,
1308          afs_int32 * transPtr, afs_int32 * timePtr, afs_status_p st)
1309 {
1310     int rc = 0;
1311     afs_status_t tst = 0, etst = 0;
1312     afs_uint32 volid;
1313     struct volser_status tstatus;
1314     int rcode;
1315
1316     *connPtr = (struct rx_connection *)0;
1317     *timePtr = 0;
1318     *transPtr = 0;
1319
1320     /* get connection to the replication site */
1321     *connPtr =
1322         UV_Bind(cellHandle, vldbEntryPtr->serverNumber[index],
1323                 AFSCONF_VOLUMEPORT);
1324     if (!*connPtr) {
1325         /* server is down */
1326         tst = -1;
1327         goto fail_GetTrans;
1328     }
1329
1330     volid = vldbEntryPtr->volumeId[ROVOL];
1331     if (volid) {
1332         tst =
1333             AFSVolTransCreate(*connPtr, volid,
1334                               vldbEntryPtr->serverPartition[index], ITOffline,
1335                               transPtr);
1336     }
1337
1338     /* If the volume does not exist, create it */
1339     if (!volid || tst) {
1340         char volname[64];
1341
1342         if (volid && (tst != VNOVOL)) {
1343             goto fail_GetTrans;
1344         }
1345
1346         strcpy(volname, vldbEntryPtr->name);
1347         strcat(volname, ".readonly");
1348
1349         tst =
1350             AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
1351                                volname, volser_RO,
1352                                vldbEntryPtr->volumeId[RWVOL], &volid,
1353                                transPtr);
1354         if (tst) {
1355             goto fail_GetTrans;
1356         }
1357         vldbEntryPtr->volumeId[ROVOL] = volid;
1358
1359         /* The following is a bit redundant, since create sets these flags by default */
1360         tst =
1361             AFSVolSetFlags(*connPtr, *transPtr,
1362                            VTDeleteOnSalvage | VTOutOfService);
1363         if (tst) {
1364             goto fail_GetTrans;
1365         }
1366     }
1367
1368     /* Otherwise, the transaction did succeed, so get the creation date of the
1369      * latest RO volume on the replication site 
1370      */
1371     else {
1372         tst = AFSVolGetStatus(*connPtr, *transPtr, &tstatus);
1373         if (tst) {
1374             goto fail_GetTrans;
1375         }
1376         *timePtr = tstatus.creationDate - CLOCKSKEW;
1377     }
1378     rc = 1;
1379
1380   fail_GetTrans:
1381
1382     if ((rc == 0) && (*transPtr)) {
1383         etst = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
1384         *transPtr = 0;
1385         if (!etst)
1386             etst = rcode;
1387     }
1388
1389     if (st != NULL) {
1390         *st = tst;
1391     }
1392     return rc;
1393 }
1394
1395 static int
1396 SimulateForwardMultiple(struct rx_connection *fromconn, afs_int32 fromtid,
1397                         afs_int32 fromdate, manyDests * tr, afs_int32 flags,
1398                         void *cookie, manyResults * results)
1399 {
1400     int i;
1401
1402     for (i = 0; i < tr->manyDests_len; i++) {
1403         results->manyResults_val[i] =
1404             AFSVolForward(fromconn, fromtid, fromdate,
1405                           &(tr->manyDests_val[i].server),
1406                           tr->manyDests_val[i].trans, cookie);
1407     }
1408     return 0;
1409 }
1410
1411
1412 /* VolumeExists()
1413  *      Determine if a volume exists on a server and partition.
1414  *      Try creating a transaction on the volume. If we can,
1415  *      the volume exists, if not, then return the error code.
1416  *      Some error codes mean the volume is unavailable but
1417  *      still exists - so we catch these error codes.
1418  */
1419 static afs_int32
1420 VolumeExists(afs_cell_handle_p cellHandle, afs_int32 server,
1421              afs_int32 partition, afs_int32 volumeid, afs_status_p st)
1422 {
1423     int rc = 0;
1424     afs_status_t tst = 0;
1425     struct rx_connection *conn = (struct rx_connection *)0;
1426     volEntries volumeInfo;
1427
1428     conn = UV_Bind(cellHandle, server, AFSCONF_VOLUMEPORT);
1429     if (conn) {
1430         volumeInfo.volEntries_val = (volintInfo *) 0;
1431         volumeInfo.volEntries_len = 0;
1432         tst = AFSVolListOneVolume(conn, partition, volumeid, &volumeInfo);
1433         if (volumeInfo.volEntries_val)
1434             free(volumeInfo.volEntries_val);
1435         if (tst == VOLSERILLEGAL_PARTITION) {
1436             tst = ENODEV;
1437         }
1438         rx_ReleaseCachedConnection(conn);
1439     }
1440     rc = 1;
1441
1442     if (st != NULL) {
1443         *st = tst;
1444     }
1445     return rc;
1446 }
1447
1448 /* release volume <afromvol> on <afromserver> <afrompart> to all the
1449  * sites if forceflag is 1.If its 0 complete the release if the previous
1450  * release aborted else start a new release */
1451 int
1452 UV_ReleaseVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
1453                  afs_int32 afromserver, afs_int32 afrompart, int forceflag,
1454                  afs_status_p st)
1455 {
1456     int rc = 0;
1457     afs_status_t tst = 0, etst = 0;
1458
1459     char vname[64];
1460     afs_int32 rcode;
1461     afs_uint32 cloneVolId, roVolId;
1462     struct replica *replicas = 0;
1463     struct nvldbentry entry;
1464     int i, volcount, m, fullrelease, vldbindex;
1465     int failure;
1466     struct restoreCookie cookie;
1467     struct rx_connection **toconns = 0;
1468     struct release *times = 0;
1469     int nservers = 0;
1470     struct rx_connection *fromconn = (struct rx_connection *)0;
1471     int islocked = 0;
1472     afs_int32 clonetid = 0, onlinetid;
1473     afs_int32 fromtid = 0;
1474     afs_uint32 fromdate = 0, thisdate;
1475     int s;
1476     manyDests tr;
1477     manyResults results;
1478     int rwindex, roindex, roclone, roexists;
1479     afs_int32 rwcrdate = 0;
1480     struct rtime {
1481         int validtime;
1482         afs_uint32 time;
1483     } remembertime[NMAXNSERVERS];
1484     int releasecount = 0;
1485     struct volser_status volstatus;
1486
1487     memset(remembertime, 0, sizeof(remembertime));
1488     memset(&results, 0, sizeof(results));
1489
1490     tst =
1491         ubik_VL_SetLock(cellHandle->vos, 0, afromvol, RWVOL,
1492                   VLOP_RELEASE);
1493     if ((tst) && (tst != VL_RERELEASE)) {
1494         goto fail_UV_ReleaseVolume;
1495     }
1496     islocked = 1;
1497
1498     /* Get the vldb entry in readable format */
1499     if (!aVLDB_GetEntryByID(cellHandle, afromvol, RWVOL, &entry, &tst)) {
1500         goto fail_UV_ReleaseVolume;
1501     }
1502
1503     if (!ISNAMEVALID(entry.name)) {
1504         tst = VOLSERBADNAME;
1505         goto fail_UV_ReleaseVolume;
1506     }
1507
1508     if (entry.volumeId[RWVOL] != afromvol) {
1509         tst = ADMVOSVOLUMERELEASERWONLY;
1510         goto fail_UV_ReleaseVolume;
1511     }
1512
1513     if (entry.nServers <= 1) {
1514         tst = ADMVOSVOLUMENOREPLICAS;
1515         goto fail_UV_ReleaseVolume;
1516     }
1517
1518     if (strlen(entry.name) > (VOLSER_OLDMAXVOLNAME - 10)) {
1519         tst = VOLSERBADNAME;
1520         goto fail_UV_ReleaseVolume;
1521     }
1522
1523     /* roclone is true if one of the RO volumes is on the same
1524      * partition as the RW volume. In this case, we make the RO volume
1525      * on the same partition a clone instead of a complete copy.
1526      */
1527
1528     roindex =
1529         Lp_ROMatch(cellHandle, &entry, afromserver, afrompart, &tst) - 1;
1530     roclone = ((roindex == -1) ? 0 : 1);
1531     rwindex = Lp_GetRwIndex(cellHandle, &entry, 0);
1532     if (rwindex < 0) {
1533         tst = VOLSERNOVOL;
1534         goto fail_UV_ReleaseVolume;
1535     }
1536
1537     /* Make sure we have a RO volume id to work with */
1538     if (entry.volumeId[ROVOL] == INVALID_BID) {
1539         /* need to get a new RO volume id */
1540         tst = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &roVolId);
1541         if (tst) {
1542             goto fail_UV_ReleaseVolume;
1543         }
1544
1545         entry.volumeId[ROVOL] = roVolId;
1546         if (!VLDB_ReplaceEntry(cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
1547             goto fail_UV_ReleaseVolume;
1548         }
1549     }
1550
1551     /* Will we be completing a previously unfinished release. -force overrides */
1552     for (fullrelease = 1, i = 0; (fullrelease && (i < entry.nServers)); i++) {
1553         if (entry.serverFlags[i] & NEW_REPSITE)
1554             fullrelease = 0;
1555     }
1556     if (forceflag && !fullrelease)
1557         fullrelease = 1;
1558
1559     /* Determine which volume id to use and see if it exists */
1560     cloneVolId =
1561         ((fullrelease
1562           || (entry.cloneId == 0)) ? entry.volumeId[ROVOL] : entry.cloneId);
1563     VolumeExists(cellHandle, afromserver, afrompart, cloneVolId, &tst);
1564     roexists = ((tst == ENODEV) ? 0 : 1);
1565     if (!roexists && !fullrelease)
1566         fullrelease = 1;        /* Do a full release if RO clone does not exist */
1567
1568     fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
1569     if (!fromconn) {
1570         tst = -1;
1571         goto fail_UV_ReleaseVolume;
1572     }
1573
1574     if (fullrelease) {
1575         /* If the RO clone exists, then if the clone is a temporary
1576          * clone, delete it. Or if the RO clone is marked RO_DONTUSE
1577          * (it was recently added), then also delete it. We do not
1578          * want to "reclone" a temporary RO clone.
1579          */
1580         if (roexists
1581             && (!roclone || (entry.serverFlags[roindex] & RO_DONTUSE))) {
1582             tst = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
1583             if (tst && (tst != VNOVOL)) {
1584                 goto fail_UV_ReleaseVolume;
1585             }
1586             roexists = 0;
1587         }
1588
1589         /* Mark all the ROs in the VLDB entry as RO_DONTUSE. We don't
1590          * write this entry out to the vlserver until after the first
1591          * RO volume is released (temp RO clones don't count).
1592          */
1593         for (i = 0; i < entry.nServers; i++) {
1594             entry.serverFlags[i] &= ~NEW_REPSITE;
1595             entry.serverFlags[i] |= RO_DONTUSE;
1596         }
1597         entry.serverFlags[rwindex] |= NEW_REPSITE;
1598         entry.serverFlags[rwindex] &= ~RO_DONTUSE;
1599
1600         /* Begin transaction on RW and mark it busy while we clone it */
1601         tst =
1602             AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
1603                               &clonetid);
1604         if (tst) {
1605             goto fail_UV_ReleaseVolume;
1606         }
1607
1608         /* Clone or reclone the volume */
1609         if (roexists) {
1610             tst = AFSVolReClone(fromconn, clonetid, cloneVolId);
1611             if (tst) {
1612                 goto fail_UV_ReleaseVolume;
1613             }
1614         } else {
1615             if (roclone) {
1616                 strcpy(vname, entry.name);
1617                 strcat(vname, ".readonly");
1618             } else {
1619                 strcpy(vname, "readonly-clone-temp");
1620             }
1621             tst =
1622                 AFSVolClone(fromconn, clonetid, 0, readonlyVolume, vname,
1623                             &cloneVolId);
1624             if (tst) {
1625                 goto fail_UV_ReleaseVolume;
1626             }
1627         }
1628
1629         /* Get the time the RW was created for future information */
1630         tst = AFSVolGetStatus(fromconn, clonetid, &volstatus);
1631         if (tst) {
1632             goto fail_UV_ReleaseVolume;
1633         }
1634         rwcrdate = volstatus.creationDate;
1635
1636         /* End the transaction on the RW volume */
1637         tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
1638         clonetid = 0;
1639         tst = (tst ? tst : rcode);
1640         if (tst) {
1641             goto fail_UV_ReleaseVolume;
1642         }
1643
1644         /* Remember clone volume ID in case we fail or are interrupted */
1645         entry.cloneId = cloneVolId;
1646
1647         if (roclone) {
1648             /* Bring the RO clone online - though not if it's a temporary clone */
1649             tst =
1650                 AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITOffline,
1651                                   &onlinetid);
1652             if (tst) {
1653                 goto fail_UV_ReleaseVolume;
1654             }
1655
1656             etst = AFSVolSetFlags(fromconn, onlinetid, 0);
1657
1658             tst = AFSVolEndTrans(fromconn, onlinetid, &rcode);
1659             tst = (tst ? tst : rcode);
1660             if (tst) {
1661                 goto fail_UV_ReleaseVolume;
1662             }
1663             if (etst) {
1664                 tst = etst;
1665                 goto fail_UV_ReleaseVolume;
1666             }
1667
1668             /* Sleep so that a client searching for an online volume won't
1669              * find the clone offline and then the next RO offline while the 
1670              * release brings the clone online and the next RO offline (race).
1671              * There is a fix in the 3.4 client that does not need this sleep
1672              * anymore, but we don't know what clients we have.
1673              */
1674             if (entry.nServers > 2)
1675                 sleep(5);
1676
1677             /* Mark the RO clone in the VLDB as a good site (already released) */
1678             entry.serverFlags[roindex] |= NEW_REPSITE;
1679             entry.serverFlags[roindex] &= ~RO_DONTUSE;
1680             entry.flags |= RO_EXISTS;
1681
1682             releasecount++;
1683
1684             /* Write out the VLDB entry only if the clone is not a temporary
1685              * clone. If we did this to a temporary clone then we would end
1686              * up marking all the ROs as "old release" making the ROs
1687              * temporarily unavailable.
1688              */
1689             if (!VLDB_ReplaceEntry
1690                 (cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
1691                 goto fail_UV_ReleaseVolume;
1692             }
1693         }
1694     }
1695
1696     /* Now we will release from the clone to the remaining RO replicas.
1697      * The first 2 ROs (counting the non-temporary RO clone) are released
1698      * individually: releasecount. This is to reduce the race condition
1699      * of clients trying to find an on-line RO volume. The remaining ROs
1700      * are released in parallel but no more than half the number of ROs
1701      * (rounded up) at a time: nservers.
1702      */
1703
1704     strcpy(vname, entry.name);
1705     strcat(vname, ".readonly");
1706     memset(&cookie, 0, sizeof(cookie));
1707     strncpy(cookie.name, vname, VOLSER_OLDMAXVOLNAME);
1708     cookie.type = ROVOL;
1709     cookie.parent = entry.volumeId[RWVOL];
1710     cookie.clone = 0;
1711
1712     nservers = entry.nServers / 2;      /* how many to do at once, excluding clone */
1713     replicas =
1714         (struct replica *)malloc(sizeof(struct replica) * nservers + 1);
1715     times = (struct release *)malloc(sizeof(struct release) * nservers + 1);
1716     toconns =
1717         (struct rx_connection **)malloc(sizeof(struct rx_connection *) *
1718                                         nservers + 1);
1719     results.manyResults_val =
1720         (afs_int32 *) malloc(sizeof(afs_int32) * nservers + 1);
1721     if (!replicas || !times || !!!results.manyResults_val || !toconns) {
1722         tst = ADMNOMEM;
1723         goto fail_UV_ReleaseVolume;
1724     }
1725
1726     memset(replicas, 0, (sizeof(struct replica) * nservers + 1));
1727     memset(times, 0, (sizeof(struct release) * nservers + 1));
1728     memset(toconns, 0, (sizeof(struct rx_connection *) * nservers + 1));
1729     memset(results.manyResults_val, 0, (sizeof(afs_int32) * nservers + 1));
1730
1731     /* Create a transaction on the cloned volume */
1732     tst =
1733         AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
1734     if (tst) {
1735         goto fail_UV_ReleaseVolume;
1736     }
1737
1738     /* For each index in the VLDB */
1739     for (vldbindex = 0; vldbindex < entry.nServers;) {
1740
1741         /* Get a transaction on the replicas. Pick replacas which have an old release. */
1742         for (volcount = 0;
1743              ((volcount < nservers) && (vldbindex < entry.nServers));
1744              vldbindex++) {
1745             /* The first two RO volumes will be released individually.
1746              * The rest are then released in parallel. This is a hack
1747              * for clients not recognizing right away when a RO volume
1748              * comes back on-line.
1749              */
1750             if ((volcount == 1) && (releasecount < 2))
1751                 break;
1752
1753             if (vldbindex == roindex)
1754                 continue;       /* the clone    */
1755             if ((entry.serverFlags[vldbindex] & NEW_REPSITE)
1756                 && !(entry.serverFlags[vldbindex] & RO_DONTUSE))
1757                 continue;
1758             if (!(entry.serverFlags[vldbindex] & ITSROVOL))
1759                 continue;       /* not a RO vol */
1760
1761
1762             /* Get a Transaction on this replica. Get a new connection if
1763              * necessary.  Create the volume if necessary.  Return the
1764              * time from which the dump should be made (0 if it's a new
1765              * volume).  Each volume might have a different time. 
1766              */
1767             replicas[volcount].server.destHost =
1768                 entry.serverNumber[vldbindex];
1769             replicas[volcount].server.destPort = AFSCONF_VOLUMEPORT;
1770             replicas[volcount].server.destSSID = 1;
1771             times[volcount].vldbEntryIndex = vldbindex;
1772
1773             if (!GetTrans
1774                 (cellHandle, &entry, vldbindex, &(toconns[volcount]),
1775                  &(replicas[volcount].trans), &(times[volcount].time),
1776                  &tst)) {
1777                 continue;
1778             }
1779
1780             /* Thisdate is the date from which we want to pick up all changes */
1781             if (forceflag || !fullrelease
1782                 || (rwcrdate > times[volcount].time)) {
1783                 /* If the forceflag is set, then we want to do a full dump.
1784                  * If it's not a full release, we can't be sure that the creation
1785                  *  date is good (so we also do a full dump).
1786                  * If the RW volume was replaced (its creation date is newer than
1787                  *  the last release), then we can't be sure what has changed (so
1788                  *  we do a full dump).
1789                  */
1790                 thisdate = 0;
1791             } else if (remembertime[vldbindex].validtime) {
1792                 /* Trans was prev ended. Use the time from the prev trans
1793                  * because, prev trans may have created the volume. In which
1794                  * case time[volcount].time would be now instead of 0.
1795                  */
1796                 thisdate =
1797                     (remembertime[vldbindex].time <
1798                      times[volcount].time) ? remembertime[vldbindex].
1799                     time : times[volcount].time;
1800             } else {
1801                 thisdate = times[volcount].time;
1802             }
1803             remembertime[vldbindex].validtime = 1;
1804             remembertime[vldbindex].time = thisdate;
1805
1806             if (volcount == 0) {
1807                 fromdate = thisdate;
1808             } else {
1809                 /* Include this volume if it is within 15 minutes of the earliest */
1810                 if (((fromdate >
1811                       thisdate) ? (fromdate - thisdate) : (thisdate -
1812                                                            fromdate)) > 900) {
1813                     AFSVolEndTrans(toconns[volcount],
1814                                    replicas[volcount].trans, &rcode);
1815                     replicas[volcount].trans = 0;
1816                     break;
1817                 }
1818                 if (thisdate < fromdate)
1819                     fromdate = thisdate;
1820             }
1821             volcount++;
1822         }
1823         if (!volcount)
1824             continue;
1825
1826         /* Release the ones we have collected */
1827         tr.manyDests_val = &(replicas[0]);
1828         tr.manyDests_len = results.manyResults_len = volcount;
1829         tst =
1830             AFSVolForwardMultiple(fromconn, fromtid, fromdate, &tr,
1831                                   0 /*spare */ , &cookie, &results);
1832         if (tst == RXGEN_OPCODE) {      /* RPC Interface Mismatch */
1833             tst =
1834                 SimulateForwardMultiple(fromconn, fromtid, fromdate, &tr,
1835                                         0 /*spare */ , &cookie, &results);
1836             nservers = 1;
1837         }
1838
1839         if (tst) {
1840             goto fail_UV_ReleaseVolume;
1841         } else {
1842             for (m = 0; m < volcount; m++) {
1843                 if (results.manyResults_val[m]) {
1844                     continue;
1845                 }
1846
1847                 tst =
1848                     AFSVolSetIdsTypes(toconns[m], replicas[m].trans, vname,
1849                                       ROVOL, entry.volumeId[RWVOL], 0, 0);
1850                 if (tst) {
1851                     continue;
1852                 }
1853
1854                 /* have to clear dest. flags to ensure new vol goes online:
1855                  * because the restore (forwarded) operation copied
1856                  * the V_inService(=0) flag over to the destination. 
1857                  */
1858                 tst = AFSVolSetFlags(toconns[m], replicas[m].trans, 0);
1859                 if (tst) {
1860                     continue;
1861                 }
1862
1863                 entry.serverFlags[times[m].vldbEntryIndex] |= NEW_REPSITE;
1864                 entry.serverFlags[times[m].vldbEntryIndex] &= ~RO_DONTUSE;
1865                 entry.flags |= RO_EXISTS;
1866                 releasecount++;
1867             }
1868         }
1869
1870         /* End the transactions and destroy the connections */
1871         for (s = 0; s < volcount; s++) {
1872             if (replicas[s].trans)
1873                 tst = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
1874             replicas[s].trans = 0;
1875             if (!tst)
1876                 tst = rcode;
1877             if (tst) {
1878                 if ((s == 0) || (tst != ENOENT)) {
1879                 } else {
1880                     if (times[s].vldbEntryIndex < vldbindex)
1881                         vldbindex = times[s].vldbEntryIndex;
1882                 }
1883             }
1884
1885             if (toconns[s])
1886                 rx_ReleaseCachedConnection(toconns[s]);
1887             toconns[s] = 0;
1888         }
1889
1890         if (!VLDB_ReplaceEntry(cellHandle, afromvol, RWVOL, &entry, 0, &tst)) {
1891             goto fail_UV_ReleaseVolume;
1892         }
1893     }                           /* for each index in the vldb */
1894
1895     /* End the transaction on the cloned volume */
1896     tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
1897     fromtid = 0;
1898
1899     /* Figure out if any volume were not released and say so */
1900     for (failure = 0, i = 0; i < entry.nServers; i++) {
1901         if (!(entry.serverFlags[i] & NEW_REPSITE))
1902             failure++;
1903     }
1904     if (failure) {
1905         if (!VLDB_ReplaceEntry
1906             (cellHandle, afromvol, RWVOL, &entry, LOCKREL_TIMESTAMP, &tst)) {
1907             goto fail_UV_ReleaseVolume;
1908         }
1909
1910         tst = VOLSERBADRELEASE;
1911         goto fail_UV_ReleaseVolume;
1912     }
1913
1914     /* All the ROs were release successfully. Remove the temporary clone */
1915     if (!roclone) {
1916         tst = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
1917         if (tst) {
1918             goto fail_UV_ReleaseVolume;
1919         }
1920     }
1921     entry.cloneId = 0;
1922
1923     for (i = 0; i < entry.nServers; i++)
1924         entry.serverFlags[i] &= ~NEW_REPSITE;
1925
1926     /* Update the VLDB */
1927     if (!VLDB_ReplaceEntry
1928         (cellHandle, afromvol, RWVOL, &entry,
1929          LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
1930         goto fail_UV_ReleaseVolume;
1931     }
1932     rc = 1;
1933
1934   fail_UV_ReleaseVolume:
1935
1936     if (clonetid) {
1937         tst = AFSVolEndTrans(fromconn, clonetid, &rcode);
1938         clonetid = 0;
1939         if (tst) {
1940             rc = 0;
1941         }
1942     }
1943     if (fromtid) {
1944         tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
1945         fromtid = 0;
1946         if (tst) {
1947             rc = 0;
1948         }
1949     }
1950     for (i = 0; i < nservers; i++) {
1951         if (replicas && replicas[i].trans) {
1952             tst = AFSVolEndTrans(toconns[i], replicas[i].trans, &rcode);
1953             replicas[i].trans = 0;
1954             if (tst) {
1955                 rc = 0;
1956             }
1957         }
1958         if (toconns && toconns[i]) {
1959             rx_ReleaseCachedConnection(toconns[i]);
1960             toconns[i] = 0;
1961         }
1962     }
1963     if (islocked) {
1964         tst =
1965             ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, RWVOL,
1966                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
1967         if (tst) {
1968             rc = 0;
1969         }
1970     }
1971
1972     if (fromconn)
1973         rx_ReleaseCachedConnection(fromconn);
1974     if (results.manyResults_val)
1975         free(results.manyResults_val);
1976     if (replicas)
1977         free(replicas);
1978     if (toconns)
1979         free(toconns);
1980     if (times)
1981         free(times);
1982
1983     if (st != NULL) {
1984         *st = tst;
1985     }
1986     return rc;
1987 }
1988
1989 static int
1990 ReceiveFile(int fd, struct rx_call *call,
1991             struct stat *status)
1992 {
1993     char *buffer = (char *)0;
1994     int blockSize;
1995     afs_int32 bytesread, nbytes, bytesleft, w;
1996     fd_set out;
1997     afs_int32 error = 0;
1998
1999 #ifdef AFS_NT40_ENV
2000     blockSize = 4096;
2001 #else
2002     if (fd != 1) {
2003 #ifdef  AFS_AIX_ENV
2004         struct statfs tstatfs;
2005
2006 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the sta
2007 t structure!! */
2008         fstatfs(fd, &tstatfs);
2009         blockSize = tstatfs.f_bsize;
2010 #else
2011         blockSize = status->st_blksize;
2012 #endif
2013     } else {
2014         blockSize = 4096;
2015     }
2016 #endif
2017     nbytes = blockSize;
2018     buffer = (char *)malloc(blockSize);
2019     if (!buffer) {
2020         return ADMNOMEM;
2021     }
2022     bytesread = 1;
2023     while (!error && (bytesread > 0)) {
2024         bytesread = rx_Read(call, buffer, nbytes);
2025         bytesleft = bytesread;
2026         while (!error && (bytesleft > 0)) {
2027             FD_ZERO(&out);
2028             FD_SET(fd, &out);
2029 #ifndef AFS_NT40_ENV            /* NT csn't select on non-socket fd's */
2030             select(fd + 1, 0, &out, 0, 0);      /* don't timeout if write bl
2031                                                  * ocks */
2032 #endif
2033             w = write(fd, &buffer[bytesread - bytesleft], bytesleft);
2034             if (w < 0) {
2035                 error = ADMVOSDUMPFILEWRITEFAIL;
2036             } else {
2037                 bytesleft -= w;
2038             }
2039         }
2040     }
2041     if (buffer)
2042         free(buffer);
2043     if (fd != 1)
2044         if (!error)
2045             fstat(fd, status);
2046     return error;
2047 }
2048
2049
2050 static afs_int32
2051 DumpFunction(struct rx_call *call, const char *filename)
2052 {
2053     int fd;
2054     struct stat status;
2055     afs_int32 error, code;
2056
2057     error = 0;
2058     fd = -1;
2059
2060     fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666);
2061     if (fd < 0 || fstat(fd, &status) < 0) {
2062         error = VOLSERBADOP;
2063         goto dffail;
2064     }
2065     code = ReceiveFile(fd, call, &status);
2066     if (code) {
2067         error = code;
2068         goto dffail;
2069     }
2070   dffail:
2071     if (fd >= 0)
2072         code = close(fd);
2073     else
2074         code = 0;
2075     if (code) {
2076         if (!error)
2077             error = code;
2078     }
2079     return error;
2080 }
2081
2082
2083 /*dump the volume <afromvol> on <afromserver> and
2084 * <afrompart> to <afilename> starting from <fromdate>.
2085 * DumpFunction does the real work behind the scenes after
2086 * extracting parameters from the rock  */
2087 int
2088 UV_DumpVolume(afs_cell_handle_p cellHandle, afs_uint32 afromvol,
2089               afs_int32 afromserver, afs_int32 afrompart, afs_int32 fromdate,
2090               const char *filename, afs_status_p st)
2091 {
2092     int rc = 0;
2093     afs_status_t tst = 0;
2094     afs_status_t etst = 0;
2095     struct rx_connection *fromconn;
2096     struct rx_call *fromcall;
2097     afs_int32 fromtid;
2098     afs_int32 rxError;
2099     afs_int32 rcode;
2100
2101     struct nvldbentry entry;
2102     afs_int32 error;
2103     int islocked;
2104
2105     islocked = 0;
2106     error = 0;
2107     rxError = 0;
2108     fromcall = (struct rx_call *)0;
2109     fromconn = (struct rx_connection *)0;
2110     fromtid = 0;
2111     fromcall = (struct rx_call *)0;
2112
2113     islocked = 0;
2114     if (!aVLDB_GetEntryByID(cellHandle, afromvol, -1, &entry, &tst)) {
2115         goto fail_UV_DumpVolume;
2116     }
2117
2118     /* get connections to the servers */
2119     fromconn = UV_Bind(cellHandle, afromserver, AFSCONF_VOLUMEPORT);
2120     tst = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
2121     if (tst) {
2122         goto fail_UV_DumpVolume;
2123     }
2124     fromcall = rx_NewCall(fromconn);
2125     tst = StartAFSVolDump(fromcall, fromtid, fromdate);
2126     if (tst) {
2127         goto fail_UV_DumpVolume;
2128     }
2129     if ((tst = DumpFunction(fromcall, filename))) {
2130         goto fail_UV_DumpVolume;
2131     }
2132     tst = rx_EndCall(fromcall, rxError);
2133     fromcall = (struct rx_call *)0;
2134     if (tst) {
2135         goto fail_UV_DumpVolume;
2136     }
2137     tst = AFSVolEndTrans(fromconn, fromtid, &rcode);
2138     fromtid = 0;
2139     if (!tst)
2140         tst = rcode;
2141     if (tst) {
2142         goto fail_UV_DumpVolume;
2143     }
2144     rc = 1;
2145
2146   fail_UV_DumpVolume:
2147
2148     if (islocked) {
2149         etst =
2150             ubik_VL_ReleaseLock(cellHandle->vos, 0, afromvol, -1,
2151                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
2152         if (etst) {
2153             if (!tst)
2154                 tst = etst;
2155         }
2156     }
2157
2158     if (fromcall) {
2159         etst = rx_EndCall(fromcall, rxError);
2160         if (etst) {
2161             if (!tst)
2162                 tst = etst;
2163         }
2164     }
2165
2166     if (fromtid) {
2167         etst = AFSVolEndTrans(fromconn, fromtid, &rcode);
2168         if (!tst)
2169             tst = etst;
2170         if (rcode) {
2171             if (!tst)
2172                 tst = rcode;
2173         }
2174     }
2175
2176     if (fromconn) {
2177         rx_ReleaseCachedConnection(fromconn);
2178     }
2179
2180     if (st != NULL) {
2181         *st = tst;
2182     }
2183     return rc;
2184 }
2185
2186 int
2187 SendFile(int fd, struct rx_call *call,
2188          struct stat *status)
2189 {
2190     char *buffer = (char *)0;
2191     int blockSize;
2192     fd_set in;
2193     afs_int32 error = 0;
2194     int done = 0;
2195     int nbytes;
2196
2197 #ifdef AFS_NT40_ENV
2198     blockSize = 4096;
2199 #else
2200     if (fd != 0) {
2201 #ifdef  AFS_AIX_ENV
2202         struct statfs tstatfs;
2203
2204 /* Unfortunately in AIX valuable fields such as st_blksize are gone from the sta
2205 t structure!! */
2206         fstatfs(fd, &tstatfs);
2207         blockSize = tstatfs.f_bsize;
2208 #else
2209         blockSize = status->st_blksize;
2210 #endif
2211     } else {
2212         blockSize = 4096;
2213     }
2214 #endif
2215     buffer = (char *)malloc(blockSize);
2216     if (!buffer) {
2217         return ADMNOMEM;
2218     }
2219
2220     while (!error && !done) {
2221         FD_ZERO(&in);
2222         FD_SET(fd, &in);
2223 #ifndef AFS_NT40_ENV            /* NT csn't select on non-socket fd's */
2224         select(fd + 1, &in, 0, 0, 0);   /* don't timeout if read blocks */
2225 #endif
2226         nbytes = read(fd, buffer, blockSize);
2227         if (nbytes < 0) {
2228             error = ADMVOSRESTOREFILEREADFAIL;
2229             break;
2230         }
2231         if (nbytes == 0) {
2232             done = 1;
2233             break;
2234         }
2235         if (rx_Write(call, buffer, nbytes) != nbytes) {
2236             error = ADMVOSRESTOREFILEWRITEFAIL;
2237             break;
2238         }
2239     }
2240     if (buffer)
2241         free(buffer);
2242     return error;
2243 }
2244
2245 static afs_int32
2246 WriteData(struct rx_call *call, const char *filename)
2247 {
2248     int fd;
2249     struct stat status;
2250     afs_int32 error, code;
2251
2252     error = 0;
2253     fd = -1;
2254
2255     fd = open(filename, 0);
2256     if (fd < 0 || fstat(fd, &status) < 0) {
2257         fprintf(STDERR, "Could access file '%s'\n", filename);
2258         error = ADMVOSRESTOREFILEOPENFAIL;
2259         goto fail_WriteData;
2260     }
2261     code = SendFile(fd, call, &status);
2262     if (code) {
2263         error = code;
2264         goto fail_WriteData;
2265     }
2266
2267   fail_WriteData:
2268
2269     if (fd >= 0)
2270         code = close(fd);
2271     else
2272         code = 0;
2273     if (code) {
2274         if (!error)
2275             error = ADMVOSRESTOREFILECLOSEFAIL;
2276     }
2277     return error;
2278 }
2279
2280 /*
2281  * Restore a volume <tovolid> <tovolname> on <toserver> <topart> from
2282  * the dump file <afilename>. WriteData does all the real work
2283  * after extracting params from the rock 
2284  */
2285 int
2286 UV_RestoreVolume(afs_cell_handle_p cellHandle, afs_int32 toserver,
2287                  afs_int32 topart, afs_uint32 tovolid, char *tovolname,
2288                  int flags, const char *dumpFile, afs_status_p st)
2289 {
2290     int rc = 0;
2291     afs_status_t tst = 0;
2292     afs_status_t etst = 0;
2293     struct rx_connection *toconn, *tempconn;
2294     struct rx_call *tocall;
2295     afs_int32 totid, rcode;
2296     afs_int32 rxError = 0;
2297     struct volser_status tstatus;
2298     char partName[10];
2299     afs_uint32 pvolid;
2300     afs_int32 temptid;
2301     int success;
2302     struct nvldbentry entry;
2303     afs_int32 error;
2304     int islocked;
2305     struct restoreCookie cookie;
2306     int reuseID;
2307     afs_int32 newDate, volflag;
2308     int index, same;
2309
2310
2311     memset(&cookie, 0, sizeof(cookie));
2312     islocked = 0;
2313     success = 0;
2314     error = 0;
2315     reuseID = 1;
2316     tocall = (struct rx_call *)0;
2317     toconn = (struct rx_connection *)0;
2318     tempconn = (struct rx_connection *)0;
2319     totid = 0;
2320     temptid = 0;
2321
2322     pvolid = tovolid;
2323     toconn = UV_Bind(cellHandle, toserver, AFSCONF_VOLUMEPORT);
2324     if (pvolid == 0) {          /*alot a new id if needed */
2325         aVLDB_GetEntryByName(cellHandle, tovolname, &entry, &tst);
2326         if (tst == VL_NOENT) {
2327             tst =
2328                 ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 1, &pvolid);
2329             if (tst) {
2330                 goto fail_UV_RestoreVolume;
2331             }
2332             reuseID = 0;
2333         } else {
2334             pvolid = entry.volumeId[RWVOL];
2335         }
2336     }
2337
2338     /* 
2339      * at this point we have a volume id to use/reuse for the
2340      * volume to be restored
2341      */
2342     if (strlen(tovolname) > (VOLSER_OLDMAXVOLNAME - 1)) {
2343         tst = ADMVOSRESTOREVOLUMENAMETOOBIG;
2344         goto fail_UV_RestoreVolume;
2345     }
2346
2347     if (!vos_PartitionIdToName(topart, partName, &tst)) {
2348         goto fail_UV_RestoreVolume;
2349     }
2350     /*what should the volume be restored as ? rw or ro or bk ?
2351      * right now the default is rw always */
2352     tst =
2353         AFSVolCreateVolume(toconn, topart, tovolname, volser_RW, 0, &pvolid,
2354                            &totid);
2355     if (tst) {
2356         if (flags & RV_FULLRST) {       /* full restore: delete then create anew */
2357             tst =
2358                 AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
2359             if (tst) {
2360                 goto fail_UV_RestoreVolume;
2361             }
2362             tst =
2363                 AFSVolSetFlags(toconn, totid,
2364                                VTDeleteOnSalvage | VTOutOfService);
2365             if (tst) {
2366                 goto fail_UV_RestoreVolume;
2367             }
2368             tst = AFSVolDeleteVolume(toconn, totid);
2369             if (tst) {
2370                 goto fail_UV_RestoreVolume;
2371             }
2372             tst = AFSVolEndTrans(toconn, totid, &rcode);
2373             totid = 0;
2374             if (!tst)
2375                 tst = rcode;
2376             if (tst) {
2377                 goto fail_UV_RestoreVolume;
2378             }
2379             tst =
2380                 AFSVolCreateVolume(toconn, topart, tovolname, volser_RW, 0,
2381                                    &pvolid, &totid);
2382             if (tst) {
2383                 goto fail_UV_RestoreVolume;
2384             }
2385         } else {
2386             tst =
2387                 AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
2388             if (tst) {
2389                 goto fail_UV_RestoreVolume;
2390             }
2391         }
2392     }
2393     cookie.parent = pvolid;
2394     cookie.type = RWVOL;
2395     cookie.clone = 0;
2396     strncpy(cookie.name, tovolname, VOLSER_OLDMAXVOLNAME);
2397
2398     tocall = rx_NewCall(toconn);
2399     tst = StartAFSVolRestore(tocall, totid, 1, &cookie);
2400     if (tst) {
2401         goto fail_UV_RestoreVolume;
2402     }
2403     tst = WriteData(tocall, dumpFile);
2404     if (tst) {
2405         goto fail_UV_RestoreVolume;
2406     }
2407     tst = rx_EndCall(tocall, rxError);
2408     tocall = (struct rx_call *)0;
2409     if (tst) {
2410         goto fail_UV_RestoreVolume;
2411     }
2412     tst = AFSVolGetStatus(toconn, totid, &tstatus);
2413     if (tst) {
2414         goto fail_UV_RestoreVolume;
2415     }
2416     tst = AFSVolSetIdsTypes(toconn, totid, tovolname, RWVOL, pvolid, 0, 0);
2417     if (tst) {
2418         goto fail_UV_RestoreVolume;
2419     }
2420     newDate = time(0);
2421     tst = AFSVolSetDate(toconn, totid, newDate);
2422     if (tst) {
2423         goto fail_UV_RestoreVolume;
2424     }
2425
2426     volflag = ((flags & RV_OFFLINE) ? VTOutOfService : 0);      /* off or on-line */
2427     tst = AFSVolSetFlags(toconn, totid, volflag);
2428     if (tst) {
2429         goto fail_UV_RestoreVolume;
2430     }
2431
2432 /* It isn't handled right in fail_UV_RestoreVolume */
2433     tst = AFSVolEndTrans(toconn, totid, &rcode);
2434     totid = 0;
2435     if (!tst)
2436         tst = rcode;
2437     if (tst) {
2438         goto fail_UV_RestoreVolume;
2439     }
2440
2441     success = 1;
2442     if (success && (!reuseID || (flags & RV_FULLRST))) {
2443         /* Volume was restored on the file server, update the 
2444          * VLDB to reflect the change.
2445          */
2446         aVLDB_GetEntryByID(cellHandle, pvolid, RWVOL, &entry, &tst);
2447         if (tst && tst != VL_NOENT && tst != VL_ENTDELETED) {
2448             goto fail_UV_RestoreVolume;
2449         }
2450         if (tst == VL_NOENT) {  /* it doesnot exist already */
2451             /*make the vldb return this indication specifically */
2452             strcpy(entry.name, tovolname);
2453             entry.nServers = 1;
2454             entry.serverNumber[0] = toserver;   /*should be indirect */
2455             entry.serverPartition[0] = topart;
2456             entry.serverFlags[0] = ITSRWVOL;
2457             entry.flags = RW_EXISTS;
2458             if (tstatus.cloneID != 0) {
2459                 entry.volumeId[ROVOL] = tstatus.cloneID;        /*this should come from status info on the volume if non zero */
2460             } else
2461                 entry.volumeId[ROVOL] = INVALID_BID;
2462             entry.volumeId[RWVOL] = pvolid;
2463             entry.cloneId = 0;
2464             if (tstatus.backupID != 0) {
2465                 entry.volumeId[BACKVOL] = tstatus.backupID;
2466                 /*this should come from status info on the volume if non zero */
2467             } else
2468                 entry.volumeId[BACKVOL] = INVALID_BID;
2469             if (!VLDB_CreateEntry(cellHandle, &entry, &tst)) {
2470                 goto fail_UV_RestoreVolume;
2471             }
2472             islocked = 0;
2473         } else {                /*update the existing entry */
2474             tst =
2475                 ubik_VL_SetLock(cellHandle->vos, 0, pvolid, RWVOL,
2476                           VLOP_RESTORE);
2477             if (tst) {
2478                 goto fail_UV_RestoreVolume;
2479             }
2480             islocked = 1;
2481             strcpy(entry.name, tovolname);
2482
2483             /* Update the vlentry with the new information */
2484             index = Lp_GetRwIndex(cellHandle, &entry, 0);
2485             if (index == -1) {
2486                 /* Add the rw site for the volume being restored */
2487                 entry.serverNumber[entry.nServers] = toserver;
2488                 entry.serverPartition[entry.nServers] = topart;
2489                 entry.serverFlags[entry.nServers] = ITSRWVOL;
2490                 entry.nServers++;
2491             } else {
2492                 /* This volume should be deleted on the old site
2493                  * if its different from new site.
2494                  */
2495                 VLDB_IsSameAddrs(cellHandle, toserver,
2496                                  entry.serverNumber[index], &same, &tst);
2497                 if ((!tst && !same)
2498                     || (entry.serverPartition[index] != topart)) {
2499                     tempconn =
2500                         UV_Bind(cellHandle, entry.serverNumber[index],
2501                                 AFSCONF_VOLUMEPORT);
2502                     tst =
2503                         AFSVolTransCreate(tempconn, pvolid,
2504                                           entry.serverPartition[index],
2505                                           ITOffline, &temptid);
2506                     if (!tst) {
2507                         tst =
2508                             AFSVolSetFlags(tempconn, temptid,
2509                                            VTDeleteOnSalvage |
2510                                            VTOutOfService);
2511                         if (tst) {
2512                             goto fail_UV_RestoreVolume;
2513                         }
2514                         tst = AFSVolDeleteVolume(tempconn, temptid);
2515                         if (tst) {
2516                             goto fail_UV_RestoreVolume;
2517                         }
2518                         tst = AFSVolEndTrans(tempconn, temptid, &rcode);
2519                         temptid = 0;
2520                         if (!tst)
2521                             tst = rcode;
2522                         if (tst) {
2523                             goto fail_UV_RestoreVolume;
2524                         }
2525                         vos_PartitionIdToName(entry.serverPartition[index],
2526                                               partName, &tst);
2527                     }
2528                 }
2529                 entry.serverNumber[index] = toserver;
2530                 entry.serverPartition[index] = topart;
2531             }
2532
2533             entry.flags |= RW_EXISTS;
2534             if (!VLDB_ReplaceEntry
2535                 (cellHandle, pvolid, RWVOL, &entry,
2536                  LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
2537                 goto fail_UV_RestoreVolume;
2538             }
2539             islocked = 0;
2540         }
2541     }
2542     rc = 1;
2543
2544   fail_UV_RestoreVolume:
2545
2546     if (tocall) {
2547         etst = rx_EndCall(tocall, rxError);
2548         if (!tst)
2549             tst = etst;
2550     }
2551     if (islocked) {
2552         etst =
2553             ubik_VL_ReleaseLock(cellHandle->vos, 0, pvolid, RWVOL,
2554                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
2555         if (etst) {
2556             if (!tst)
2557                 tst = etst;
2558         }
2559     }
2560     if (totid) {
2561         etst = AFSVolEndTrans(toconn, totid, &rcode);
2562         if (!etst)
2563             etst = rcode;
2564         if (etst) {
2565             if (!tst)
2566                 tst = etst;
2567         }
2568     }
2569     if (temptid) {
2570         etst = AFSVolEndTrans(toconn, temptid, &rcode);
2571         if (!etst)
2572             etst = rcode;
2573         if (etst) {
2574             if (!tst)
2575                 tst = etst;
2576         }
2577     }
2578
2579     if (tempconn)
2580         rx_ReleaseCachedConnection(tempconn);
2581     if (toconn)
2582         rx_ReleaseCachedConnection(toconn);
2583
2584     if (st != NULL) {
2585         *st = tst;
2586     }
2587     return rc;
2588 }
2589
2590 /*adds <server> and <part> as a readonly replication site for <volid>
2591 *in vldb */
2592 int
2593 UV_AddSite(afs_cell_handle_p cellHandle, afs_int32 server, afs_int32 part,
2594            afs_uint32 volid, afs_status_p st)
2595 {
2596     int rc = 0;
2597     afs_status_t tst = 0;
2598     int j, nro = 0, islocked = 0;
2599     struct nvldbentry entry;
2600     int same = 0;
2601
2602     tst =
2603         ubik_VL_SetLock(cellHandle->vos, 0, volid, RWVOL, VLOP_ADDSITE);
2604     if (tst) {
2605         goto fail_UV_AddSite;
2606     }
2607     islocked = 1;
2608
2609     if (!aVLDB_GetEntryByID(cellHandle, volid, RWVOL, &entry, &tst)) {
2610         goto fail_UV_AddSite;
2611     }
2612     if (!ISNAMEVALID(entry.name)) {
2613         tst = VOLSERBADOP;
2614         goto fail_UV_AddSite;
2615     }
2616
2617     /* See if it's too many entries */
2618     if (entry.nServers >= NMAXNSERVERS) {
2619         tst = VOLSERBADOP;
2620         goto fail_UV_AddSite;
2621     }
2622
2623     /* See if it's on the same server */
2624     for (j = 0; j < entry.nServers; j++) {
2625         if (entry.serverFlags[j] & ITSROVOL) {
2626             nro++;
2627             if (!VLDB_IsSameAddrs
2628                 (cellHandle, server, entry.serverNumber[j], &same, &tst)) {
2629                 goto fail_UV_AddSite;
2630             }
2631             if (same) {
2632                 tst = VOLSERBADOP;
2633                 goto fail_UV_AddSite;
2634             }
2635         }
2636     }
2637
2638     /* See if it's too many RO sites - leave one for the RW */
2639     if (nro >= NMAXNSERVERS - 1) {
2640         tst = VOLSERBADOP;
2641         goto fail_UV_AddSite;
2642     }
2643
2644     entry.serverNumber[entry.nServers] = server;
2645     entry.serverPartition[entry.nServers] = part;
2646     entry.serverFlags[entry.nServers] = (ITSROVOL | RO_DONTUSE);
2647     entry.nServers++;
2648
2649     if (!VLDB_ReplaceEntry
2650         (cellHandle, volid, RWVOL, &entry,
2651          LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP, &tst)) {
2652         goto fail_UV_AddSite;
2653     }
2654     islocked = 0;
2655     rc = 1;
2656
2657   fail_UV_AddSite:
2658
2659     if (islocked) {
2660         tst =
2661             ubik_VL_ReleaseLock(cellHandle->vos, 0, volid, RWVOL,
2662                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
2663     }
2664
2665     if (st != NULL) {
2666         *st = tst;
2667     }
2668     return rc;
2669 }
2670
2671 /*removes <server> <part> as read only site for <volid> from the vldb */
2672 int
2673 UV_RemoveSite(afs_cell_handle_p cellHandle, afs_int32 server, afs_int32 part,
2674               afs_uint32 volid, afs_status_p st)
2675 {
2676     int rc = 0;
2677     afs_status_t tst = 0;
2678     struct nvldbentry entry;
2679     int islocked = 0;
2680
2681     tst =
2682         ubik_VL_SetLock(cellHandle->vos, 0, volid, RWVOL, VLOP_ADDSITE);
2683     if (tst) {
2684         goto fail_UV_RemoveSite;
2685     }
2686     islocked = 1;
2687
2688     if (!aVLDB_GetEntryByID(cellHandle, volid, RWVOL, &entry, &tst)) {
2689         goto fail_UV_RemoveSite;
2690     }
2691     if (!Lp_ROMatch(cellHandle, &entry, server, part, &tst)) {
2692         /*this site doesnot exist  */
2693         goto fail_UV_RemoveSite;
2694     } else {                    /*remove the rep site */
2695         Lp_SetROValue(cellHandle, &entry, server, part, 0, 0);
2696         entry.nServers--;
2697         if ((entry.nServers == 1) && (entry.flags & RW_EXISTS))
2698             entry.flags &= ~RO_EXISTS;
2699         if (entry.nServers < 1) {       /*this is the last ref */
2700             tst = ubik_VL_DeleteEntry(cellHandle->vos, 0, volid, ROVOL);
2701             if (tst) {
2702                 goto fail_UV_RemoveSite;
2703             }
2704         }
2705         if (!VLDB_ReplaceEntry
2706             (cellHandle, volid, RWVOL, &entry,
2707              (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP), &tst)) {
2708             goto fail_UV_RemoveSite;
2709         }
2710     }
2711     rc = 1;
2712
2713   fail_UV_RemoveSite:
2714
2715     if (islocked) {
2716         afs_status_t t;
2717         t = ubik_VL_ReleaseLock(cellHandle->vos, 0, volid, RWVOL,
2718                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
2719         if (tst == 0) {
2720             tst = t;
2721         }
2722     }
2723
2724     if (st != NULL) {
2725         *st = tst;
2726     }
2727     return rc;
2728 }
2729
2730 /*list all the partitions on <aserver> */
2731 int
2732 UV_ListPartitions(struct rx_connection *server, struct partList *ptrPartList,
2733                   afs_int32 * cntp, afs_status_p st)
2734 {
2735     int rc = 0;
2736     afs_status_t tst = 0;
2737     struct pIDs partIds;
2738     struct partEntries partEnts;
2739     int i, j = 0;
2740
2741     *cntp = 0;
2742
2743     partEnts.partEntries_len = 0;
2744     partEnts.partEntries_val = NULL;
2745     /* this is available only on new servers */
2746     tst = AFSVolXListPartitions(server, &partEnts);
2747
2748     /* next, try old interface */
2749     if (tst == RXGEN_OPCODE) {
2750         for (i = 0; i < 26; i++)
2751             partIds.partIds[i] = -1;
2752         tst = AFSVolListPartitions(server, &partIds);
2753         if (!tst) {
2754             for (i = 0; i < 26; i++) {
2755                 if ((partIds.partIds[i]) != -1) {
2756                     ptrPartList->partId[j] = partIds.partIds[i];
2757                     ptrPartList->partFlags[j] = PARTVALID;
2758                     j++;
2759                 } else
2760                     ptrPartList->partFlags[i] = 0;
2761             }
2762             *cntp = j;
2763         } else {
2764             goto fail_UV_ListPartitions;
2765         }
2766     } else if (!tst) {
2767         *cntp = partEnts.partEntries_len;
2768         if (*cntp > VOLMAXPARTS) {
2769             *cntp = VOLMAXPARTS;
2770         }
2771         for (i = 0; i < *cntp; i++) {
2772             ptrPartList->partId[i] = partEnts.partEntries_val[i];
2773             ptrPartList->partFlags[i] = PARTVALID;
2774         }
2775         free(partEnts.partEntries_val);
2776     } else {
2777         goto fail_UV_ListPartitions;
2778     }
2779     rc = 1;
2780
2781   fail_UV_ListPartitions:
2782
2783     if (st != NULL) {
2784         *st = tst;
2785     }
2786     return rc;
2787 }
2788
2789 /*------------------------------------------------------------------------
2790  * EXPORTED UV_XListVolumes
2791  *
2792  * Description:
2793  *      List the extended information for all the volumes on a particular
2794  *      File Server and partition.  We may either return the volume's ID
2795  *      or all of its extended information.
2796  *
2797  * Arguments:
2798  *      a_serverID         : Address of the File Server for which we want
2799  *                              extended volume info.
2800  *      a_partID           : Partition for which we want the extended
2801  *                              volume info.
2802  *      a_all              : If non-zero, fetch ALL the volume info,
2803  *                              otherwise just the volume ID.
2804  *      a_resultPP         : Ptr to the address of the area containing
2805  *                              the returned volume info.
2806  *      a_numEntsInResultP : Ptr for the value we set for the number of
2807  *                              entries returned.
2808  *
2809  * Returns:
2810  *      0 on success,
2811  *      Otherise, the return value of AFSVolXListVolumes.
2812  *
2813  * Environment:
2814  *      This routine is closely related to UV_ListVolumes, which returns
2815  *      only the standard level of detail on AFS volumes. It is a
2816  *      heavyweight operation, zipping through all the volume entries for
2817  *      a given server/partition.
2818  *
2819  * Side Effects:
2820  *      As advertised.
2821  *------------------------------------------------------------------------*/
2822
2823 int
2824 UV_XListVolumes(struct rx_connection *server, afs_int32 a_partID, int a_all,
2825                 struct volintXInfo **a_resultPP,
2826                 afs_int32 * a_numEntsInResultP, afs_status_p st)
2827 {
2828     int rc = 0;
2829     afs_status_t tst = 0;
2830
2831     volXEntries volumeXInfo;    /*Area for returned extended vol info */
2832
2833     /*
2834      * Set up our error code and the area for returned extended volume info.
2835      * We set the val field to a null pointer as a hint for the stub to
2836      * allocate space.
2837      */
2838     *a_numEntsInResultP = 0;
2839     *a_resultPP = (volintXInfo *) 0;
2840     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
2841     volumeXInfo.volXEntries_len = 0;
2842
2843     /*
2844      * Bind to the Volume Server port on the File Server machine in question,
2845      * then go for it.
2846      */
2847     tst = AFSVolXListVolumes(server, a_partID, a_all, &volumeXInfo);
2848     if (tst) {
2849         goto fail_UV_XListVolumes;
2850     } else {
2851         /*
2852          * We got the info; pull out the pointer to where the results lie
2853          * and how many entries are there.
2854          */
2855         *a_resultPP = volumeXInfo.volXEntries_val;
2856         *a_numEntsInResultP = volumeXInfo.volXEntries_len;
2857     }
2858     rc = 1;
2859
2860   fail_UV_XListVolumes:
2861
2862     if (st != NULL) {
2863         *st = tst;
2864     }
2865     return rc;
2866 }
2867
2868 /*------------------------------------------------------------------------
2869  * EXPORTED UV_XListOneVolume
2870  *
2871  * Description:
2872  *      List the extended information for a volume on a particular File
2873  *      Server and partition.
2874  *
2875  * Arguments:
2876  *      server     : a handle to the server where the volume resides.
2877  *      a_partID           : Partition for which we want the extended
2878  *                              volume info.
2879  *      a_volID            : Volume ID for which we want the info.
2880  *      a_resultPP         : Ptr to the address of the area containing
2881  *                              the returned volume info.
2882  *
2883  * Returns:
2884  *      0 on success,
2885  *      Otherise, the return value of AFSVolXListOneVolume.
2886  *
2887  * Environment:
2888  *      This routine is closely related to UV_ListOneVolume, which returns
2889  *      only the standard level of detail on the chosen AFS volume.
2890  *
2891  * Side Effects:
2892  *      As advertised.
2893  *------------------------------------------------------------------------*/
2894
2895 int
2896 UV_XListOneVolume(struct rx_connection *server, afs_int32 a_partID,
2897                   afs_uint32 a_volID, struct volintXInfo **a_resultPP,
2898                   afs_status_p st)
2899 {
2900     int rc = 0;
2901     afs_status_t tst = 0;
2902     volXEntries volumeXInfo;    /*Area for returned info */
2903
2904     /*
2905      * Set the area we're in which we are returning
2906      * the info.  Setting the val field to a null pointer tells the stub
2907      * to allocate space for us.
2908      */
2909     *a_resultPP = (volintXInfo *) 0;
2910     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
2911     volumeXInfo.volXEntries_len = 0;
2912
2913     tst = AFSVolXListOneVolume(server, a_partID, a_volID, &volumeXInfo);
2914
2915     if (tst) {
2916         goto fail_UV_XListOneVolume;
2917     } else {
2918         /*
2919          * We got the info; pull out the pointer to where the results lie.
2920          */
2921         *a_resultPP = volumeXInfo.volXEntries_val;
2922     }
2923     rc = 1;
2924
2925   fail_UV_XListOneVolume:
2926
2927     if (st != NULL) {
2928         *st = tst;
2929     }
2930     return rc;
2931
2932 }                               /*UV_XListOneVolume */
2933
2934 /*------------------------------------------------------------------------
2935  * EXPORTED UV_ListOneVolume
2936  *
2937  * Description:
2938  *      List the volume information for a volume on a particular File
2939  *      Server and partition.
2940  *
2941  * Arguments:
2942  *      server     : a handle to the server where the volume resides.
2943  *      a_partID           : Partition for which we want the extended
2944  *                              volume info.
2945  *      a_volID            : Volume ID for which we want the info.
2946  *      a_resultPP         : Ptr to the address of the area containing
2947  *                              the returned volume info.
2948  *
2949  * Returns:
2950  *      0 on success,
2951  *      Otherise, the return value of AFSVolXListOneVolume.
2952  *
2953  * Side Effects:
2954  *      As advertised.
2955  *------------------------------------------------------------------------*/
2956
2957 int UV_ListOneVolume(struct rx_connection *server, afs_int32 a_partID,
2958                   afs_uint32 a_volID, struct volintInfo **a_resultPP,
2959                   afs_status_p st)
2960 {
2961     int rc = 0;
2962     afs_status_t tst = 0;
2963     volEntries volumeInfo;      /*Area for returned info */
2964
2965     /*
2966      * Set the area we're in which we are returning
2967      * the info.  Setting the val field to a null pointer tells the stub
2968      * to allocate space for us.
2969      */
2970     *a_resultPP = (volintInfo *) 0;
2971     volumeInfo.volEntries_val = (volintInfo *) 0;
2972     volumeInfo.volEntries_len = 0;
2973
2974     tst = AFSVolListOneVolume(server, a_partID, a_volID, &volumeInfo);
2975
2976     if (tst) {
2977         goto fail_UV_ListOneVolume;
2978     } else {
2979         /*
2980          * We got the info; pull out the pointer to where the results lie.
2981          */
2982         *a_resultPP = volumeInfo.volEntries_val;
2983     }
2984     rc = 1;
2985
2986   fail_UV_ListOneVolume:
2987
2988     if (st != NULL) {
2989         *st = tst;
2990     }
2991     return rc;
2992 }/*UV_ListOneVolume*/
2993
2994 /*sync vldb with all the entries on <myQueue> on <aserver> and <apart>*/
2995 static afs_int32
2996 ProcessEntries(afs_cell_handle_p cellHandle, struct qHead *myQueue,
2997                struct rx_connection *server, afs_int32 apart, afs_int32 force)
2998 {
2999     struct aqueue elem;
3000     int success, temp;
3001     afs_uint32 temp1, temp2;
3002     afs_int32 vcode;
3003     afs_uint32 maxVolid = 0;
3004     struct nvldbentry entry;
3005     int noError = 1, error, same;
3006     int totalC, totalU, totalCE, totalUE, totalG;
3007     int counter;
3008     int aserver = ntohl(rx_HostOf(rx_PeerOf(server)));
3009     afs_status_t tst;
3010
3011     totalC = totalU = totalCE = totalUE = totalG = 0;
3012     counter = 0;
3013
3014     /* get the next  available id's from the vldb server */
3015     vcode = ubik_VL_GetNewVolumeId(cellHandle->vos, 0, 0, &maxVolid);
3016     if (vcode) {
3017         return (vcode);
3018     }
3019     totalG = myQueue->count;
3020     if (totalG == 0)
3021         return 0;
3022     while (1) {
3023         Lp_QEnumerate(myQueue, &success, &elem, 0);
3024         if (!success)
3025             break;
3026         counter++;
3027
3028         if (!elem.isValid[RWVOL] && !elem.isValid[ROVOL] && !elem.isValid[BACKVOL]) {   /*something is wrong with elem */
3029             noError = 0;
3030             continue;
3031         }
3032         if (maxVolid <= elem.ids[RWVOL]) {
3033             temp1 = maxVolid;
3034             temp2 = elem.ids[RWVOL] - maxVolid + 1;
3035             maxVolid = 0;
3036             vcode =
3037                 ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
3038                           &maxVolid);
3039             maxVolid += temp2;
3040         }
3041         if (maxVolid <= elem.ids[ROVOL]) {
3042             temp1 = maxVolid;
3043             temp2 = elem.ids[ROVOL] - maxVolid + 1;
3044             maxVolid = 0;
3045             vcode =
3046                 ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
3047                           &maxVolid);
3048             maxVolid += temp2;
3049         }
3050         if (maxVolid <= elem.ids[BACKVOL]) {
3051             temp1 = maxVolid;
3052             temp2 = elem.ids[BACKVOL] - temp1 + 1;
3053             maxVolid = 0;
3054             vcode =
3055                 ubik_VL_GetNewVolumeId(cellHandle->vos, 0, temp2,
3056                           &maxVolid);
3057             maxVolid += temp2;
3058         }
3059         aVLDB_GetEntryByID(cellHandle, elem.ids[RWVOL], RWVOL, &entry, &tst);
3060         if (tst && (tst != VL_NOENT)) {
3061             noError = 0;
3062             totalCE++;
3063         } else if (tst && (tst == VL_NOENT)) {  /*entry doesnot exist */
3064             /*set up a vldb entry for elem */
3065             memset(&entry, 0, sizeof(entry));
3066             strncpy(entry.name, elem.name, VOLSER_OLDMAXVOLNAME);
3067             if (elem.isValid[RWVOL]) {  /*rw exists */
3068                 entry.flags |= RW_EXISTS;
3069                 entry.serverFlags[entry.nServers] = ITSRWVOL;
3070                 entry.serverNumber[entry.nServers] = aserver;
3071                 entry.serverPartition[entry.nServers] = apart;
3072                 entry.nServers += 1;
3073                 entry.volumeId[RWVOL] = elem.ids[RWVOL];
3074                 entry.volumeId[ROVOL] = elem.ids[ROVOL];
3075                 entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
3076             }
3077             if (elem.isValid[ROVOL]) {  /*ro volume exists */
3078                 entry.flags |= RO_EXISTS;
3079                 entry.serverFlags[entry.nServers] = ITSROVOL;
3080                 entry.serverNumber[entry.nServers] = aserver;
3081                 entry.serverPartition[entry.nServers] = apart;
3082                 entry.nServers += 1;
3083                 entry.volumeId[RWVOL] = elem.ids[RWVOL];
3084                 entry.volumeId[ROVOL] = elem.ids[ROVOL];
3085
3086             }
3087             if (elem.isValid[BACKVOL]) {        /*backup volume exists */
3088                 entry.flags |= BACK_EXISTS;
3089                 if (!(entry.flags & RW_EXISTS)) {       /*this helps to check for a stray backup if parent moves */
3090                     entry.serverFlags[entry.nServers] = ITSRWVOL;
3091                     entry.serverNumber[entry.nServers] = aserver;
3092                     entry.serverPartition[entry.nServers] = apart;
3093                     entry.nServers += 1;
3094                 }
3095
3096                 entry.volumeId[RWVOL] = elem.ids[RWVOL];
3097                 entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
3098
3099             }
3100             VLDB_CreateEntry(cellHandle, &entry, &tst);
3101             if (tst) {
3102                 noError = 0;
3103                 totalCE++;
3104             } else
3105                 totalC++;
3106         } else {                /* Update the existing entry */
3107             strncpy(entry.name, elem.name, VOLSER_OLDMAXVOLNAME);       /*the name Could have changed */
3108
3109             if (elem.isValid[RWVOL]) {  /* A RW volume */
3110                 temp = Lp_GetRwIndex(cellHandle, &entry, 0);
3111                 if (temp == -1) {
3112                     /* A RW index is not found in the VLDB entry - will add it */
3113
3114                     entry.flags |= RW_EXISTS;
3115                     entry.serverNumber[entry.nServers] = aserver;
3116                     entry.serverPartition[entry.nServers] = apart;
3117                     entry.serverFlags[entry.nServers] = ITSRWVOL;
3118                     entry.nServers++;
3119                 } else {
3120                     /* A RW index is found in the VLDB entry.
3121                      * Verify that the volume location matches the VLDB location.
3122                      * Fix the VLDB entry if it is not correct.
3123                      */
3124
3125                     error =
3126                         VLDB_IsSameAddrs(cellHandle, aserver,
3127                                          entry.serverNumber[temp], &same,
3128                                          &tst);
3129                     if (!error) {
3130                         continue;
3131                     }
3132                     if (!same || (apart != entry.serverPartition[temp])) {
3133                         /* VLDB says volume is in another place. Fix the VLDB entry */
3134                         entry.serverNumber[temp] = aserver;
3135                         entry.serverPartition[temp] = apart;
3136
3137                     }
3138                     entry.flags |= RW_EXISTS;
3139                 }
3140                 if ((elem.ids[BACKVOL] != 0) && elem.isValid[BACKVOL])
3141                     entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
3142                 if ((elem.ids[ROVOL] != 0) && elem.isValid[ROVOL])
3143                     entry.volumeId[ROVOL] = elem.ids[ROVOL];
3144             }
3145
3146             if (elem.isValid[ROVOL]) {
3147                 /*tackle a ro volume */
3148
3149                 if (!Lp_ROMatch(cellHandle, &entry, aserver, apart, 0)) {
3150                     /*add this site */
3151                     if (elem.ids[ROVOL] > entry.volumeId[ROVOL]) {
3152                         /*there is a conflict of ids, keep the later volume */
3153                         /*delete all the ro volumes listed in vldb entry since they 
3154                          * are older */
3155
3156                         int j, count, rwsite;
3157
3158
3159                         count = entry.nServers;
3160                         rwsite = -1;
3161                         for (j = 0; j < count; j++) {
3162                             if (entry.serverFlags[j] & ITSROVOL) {
3163
3164                                 /*delete the site */
3165                                 entry.serverNumber[j] = 0;
3166                                 entry.serverPartition[j] = 0;
3167                                 entry.serverFlags[j] = 0;
3168
3169                             } else if (entry.serverFlags[j] & ITSRWVOL)
3170                                 rwsite = j;
3171                         }
3172                         entry.nServers = 0;
3173                         if (rwsite != -1) {
3174                             entry.serverNumber[entry.nServers] =
3175                                 entry.serverNumber[rwsite];
3176                             entry.serverPartition[entry.nServers] =
3177                                 entry.serverPartition[rwsite];
3178                             entry.serverFlags[entry.nServers] =
3179                                 entry.serverFlags[rwsite];
3180                             entry.nServers++;
3181                         }
3182                         entry.serverNumber[entry.nServers] = aserver;
3183                         entry.serverPartition[entry.nServers] = apart;
3184                         entry.serverFlags[entry.nServers] = ITSROVOL;
3185                         entry.nServers++;
3186                         entry.volumeId[ROVOL] = elem.ids[ROVOL];
3187                         entry.flags |= RO_EXISTS;
3188
3189                     } else if (elem.ids[ROVOL] < entry.volumeId[ROVOL]) {
3190                         if (!(entry.flags & RO_EXISTS)) {
3191                             entry.volumeId[ROVOL] = elem.ids[ROVOL];
3192                             entry.serverNumber[entry.nServers] = aserver;
3193                             entry.serverPartition[entry.nServers] = apart;
3194                             entry.serverFlags[entry.nServers] = ITSROVOL;
3195                             entry.nServers++;
3196                             entry.flags |= RO_EXISTS;
3197                         }
3198
3199                     }
3200
3201                     else if (elem.ids[ROVOL] == entry.volumeId[ROVOL]) {
3202                         entry.serverNumber[entry.nServers] = aserver;
3203                         entry.serverPartition[entry.nServers] = apart;
3204                         entry.serverFlags[entry.nServers] = ITSROVOL;
3205                         entry.nServers++;
3206                         entry.flags |= RO_EXISTS;
3207                         entry.volumeId[ROVOL] = elem.ids[ROVOL];
3208                     }
3209                 }
3210                 if (entry.volumeId[ROVOL] == INVALID_BID)
3211                     entry.volumeId[ROVOL] = elem.ids[ROVOL];
3212             }
3213
3214             if (elem.isValid[BACKVOL]) {
3215                 temp = Lp_GetRwIndex(cellHandle, &entry, 0);
3216                 if (temp != -1) {       /*check if existing backup site matches with the given arguments */
3217                     error =
3218                         VLDB_IsSameAddrs(cellHandle, aserver,
3219                                          entry.serverNumber[temp], &same,
3220                                          &tst);
3221                     if (!error) {
3222                         continue;
3223                     }
3224                 } else {
3225                     /*tackle the backup volume */
3226                     entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
3227                     entry.flags |= BACK_EXISTS;
3228                 }
3229                 if (entry.volumeId[BACKVOL] == INVALID_BID)
3230                     entry.volumeId[BACKVOL] = elem.ids[BACKVOL];
3231             }
3232
3233             VLDB_ReplaceEntry(cellHandle, elem.ids[RWVOL], RWVOL, &entry,
3234                               LOCKREL_OPCODE | LOCKREL_AFSID |
3235                               LOCKREL_TIMESTAMP, &tst);
3236             if (tst) {
3237                 noError = 0;
3238                 totalUE++;
3239
3240                 vcode =
3241                     ubik_VL_ReleaseLock(cellHandle->vos, 0,
3242                               elem.ids[RWVOL], RWVOL,
3243                               LOCKREL_OPCODE | LOCKREL_AFSID |
3244                               LOCKREL_TIMESTAMP);
3245                 if (vcode) {
3246                     noError = 0;
3247                 }
3248             }
3249         }                       /* else update the existing entry */
3250
3251     }                           /* End of while(1) */
3252
3253     if (noError)
3254         return 0;
3255     else
3256         return VOLSERBADOP;
3257 }
3258
3259 /*synchronise vldb with the file server <aserver> and <apart>(if flags=1).
3260 *else synchronise with all the valid partitions on <aserver>
3261 */
3262 int
3263 UV_SyncVldb(afs_cell_handle_p cellHandle, struct rx_connection *server,
3264             afs_int32 apart, int flags, int force, afs_status_p st)
3265 {
3266     int rc = 0;
3267     afs_status_t tst = 0;
3268     afs_int32 count;
3269     int i;
3270     volEntries volumeInfo;
3271     volintInfo *pntr;
3272     struct qHead myQueue;
3273     struct partList PartList;
3274     int noError = 1;
3275     afs_int32 cnt;
3276     char pname[10];
3277
3278     /*this hints the stub to allocate space */
3279     volumeInfo.volEntries_val = (volintInfo *) 0;
3280     volumeInfo.volEntries_len = 0;
3281
3282     if (!flags) {               /*generate all the valid partitions */
3283         UV_ListPartitions(server, &PartList, &cnt, &tst);
3284         if (tst) {
3285             goto fail_UV_SyncVldb;
3286         }
3287     } else {
3288         PartList.partId[0] = apart;
3289         cnt = 1;
3290     }
3291
3292     for (i = 0; i < cnt; i++) {
3293         apart = PartList.partId[i];
3294         /*this hints the stub to allocate space */
3295         volumeInfo.volEntries_val = (volintInfo *) 0;
3296         volumeInfo.volEntries_len = 0;
3297         tst = AFSVolListVolumes(server, apart, 1, &volumeInfo);
3298         if (tst) {
3299             goto fail_UV_SyncVldb;
3300         }
3301         count = volumeInfo.volEntries_len;
3302         pntr = volumeInfo.volEntries_val;
3303
3304         if (!vos_PartitionIdToName(apart, pname, &tst)) {
3305             goto fail_UV_SyncVldb;
3306         }
3307         /*collect all vol entries by their parentid */
3308         tst = GroupEntries(server, pntr, count, &myQueue, apart);
3309         if (tst) {
3310             noError = 0;
3311             if (volumeInfo.volEntries_val) {
3312                 /*free the space allocated by the stub */
3313                 free(volumeInfo.volEntries_val);
3314                 volumeInfo.volEntries_val = 0;
3315             }
3316             continue;
3317         }
3318         tst = ProcessEntries(cellHandle, &myQueue, server, apart, force);
3319         if (tst) {
3320             tst = VOLSERFAILEDOP;
3321             if (volumeInfo.volEntries_val) {
3322                 /*free the space allocated by the stub */
3323                 free(volumeInfo.volEntries_val);
3324                 volumeInfo.volEntries_val = 0;
3325             }
3326             continue;
3327         }
3328         if (noError)
3329             tst = 0;
3330         else
3331             tst = VOLSERFAILEDOP;
3332     }                           /* thru all partitions */
3333     rc = 1;
3334
3335   fail_UV_SyncVldb:
3336
3337     if (volumeInfo.volEntries_val)
3338         free(volumeInfo.volEntries_val);
3339
3340     if (tst != 0) {
3341         rc = 0;
3342     }
3343
3344     if (st != NULL) {
3345         *st = tst;
3346     }
3347     return rc;
3348 }
3349
3350 static afs_int32
3351 CheckVldbRWBK(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
3352               afs_int32 * modified, afs_status_p st)
3353 {
3354     int rc = 0;
3355     afs_status_t tst = 0;
3356     int modentry = 0;
3357     int idx;
3358
3359     if (modified)
3360         *modified = 0;
3361     idx = Lp_GetRwIndex(cellHandle, entry, 0);
3362
3363     /* Check to see if the RW volume exists and set the RW_EXISTS
3364      * flag accordingly.
3365      */
3366     if (idx == -1) {            /* Did not find a RW entry */
3367         if (entry->flags & RW_EXISTS) { /* ... yet entry says RW exists */
3368             entry->flags &= ~RW_EXISTS; /* ... so say RW does not exist */
3369             modentry++;
3370         }
3371     } else {
3372         if (VolumeExists
3373             (cellHandle, entry->serverNumber[idx],
3374              entry->serverPartition[idx], entry->volumeId[RWVOL], &tst)) {
3375             if (!(entry->flags & RW_EXISTS)) {  /* ... yet entry says RW does no
3376                                                  * t exist */
3377                 entry->flags |= RW_EXISTS;      /* ... so say RW does exist */
3378                 modentry++;
3379             }
3380         } else if (tst == ENODEV) {     /* RW volume does not exist */
3381             if (entry->flags & RW_EXISTS) {     /* ... yet entry says RW exists
3382                                                  */
3383                 entry->flags &= ~RW_EXISTS;     /* ... so say RW does not exist
3384                                                  */
3385                 modentry++;
3386             }
3387         } else {
3388             /* If VLDB says it didn't exist, then ignore error */
3389             if (entry->flags & RW_EXISTS) {
3390                 goto fail_CheckVldbRWBK;
3391             }
3392         }
3393     }
3394
3395     /* Check to see if the BK volume exists and set the BACK_EXISTS
3396      * flag accordingly. idx already ponts to the RW entry.
3397      */
3398     if (idx == -1) {            /* Did not find a RW entry */
3399         if (entry->flags & BACK_EXISTS) {       /* ... yet entry says BK exists */
3400             entry->flags &= ~BACK_EXISTS;       /* ... so say BK does not exist */
3401             modentry++;
3402         }
3403     } else {                    /* Found a RW entry */
3404         if (VolumeExists
3405             (cellHandle, entry->serverNumber[idx],
3406              entry->serverPartition[idx], entry->volumeId[BACKVOL], &tst)) {
3407             if (!(entry->flags & BACK_EXISTS)) {        /* ... yet entry says BK does n
3408                                                          * ot exist */
3409                 entry->flags |= BACK_EXISTS;    /* ... so say BK does exist */
3410                 modentry++;
3411             }
3412         } else if (tst == ENODEV) {     /* BK volume does not exist */
3413             if (entry->flags & BACK_EXISTS) {   /* ... yet entry says BK exists
3414                                                  */
3415                 entry->flags &= ~BACK_EXISTS;   /* ... so say BK does not exist
3416                                                  */
3417                 modentry++;
3418             }
3419         } else {
3420             /* If VLDB says it didn't exist, then ignore error */
3421             if (entry->flags & BACK_EXISTS) {
3422                 goto fail_CheckVldbRWBK;
3423             }
3424         }
3425     }
3426
3427     /* If there is an idx but the BK and RW volumes no
3428      * longer exist, then remove the RW entry.
3429      */
3430     if ((idx != -1) && !(entry->flags & RW_EXISTS)
3431         && !(entry->flags & BACK_EXISTS)) {
3432         Lp_SetRWValue(cellHandle, entry, entry->serverNumber[idx],
3433                       entry->serverPartition[idx], 0L, 0L);
3434         entry->nServers--;
3435         modentry++;
3436     }
3437     rc = 1;
3438
3439   fail_CheckVldbRWBK:
3440
3441     if (modified)
3442         *modified = modentry;
3443
3444     if (st != NULL) {
3445         *st = tst;
3446     }
3447     return rc;
3448 }
3449
3450
3451 static int
3452 CheckVldbRO(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
3453             afs_int32 * modified, afs_status_p st)
3454 {
3455     int rc = 0;
3456     afs_status_t tst = 0;
3457     int idx;
3458     int foundro = 0, modentry = 0;
3459
3460     if (modified)
3461         *modified = 0;
3462
3463     /* Check to see if the RO volumes exist and set the RO_EXISTS
3464      * flag accordingly.
3465      */
3466     for (idx = 0; idx < entry->nServers; idx++) {
3467         if (!(entry->serverFlags[idx] & ITSROVOL)) {
3468             continue;           /* not a RO */
3469         }
3470
3471         if (VolumeExists
3472             (cellHandle, entry->serverNumber[idx],
3473              entry->serverPartition[idx], entry->volumeId[ROVOL], &tst)) {
3474             foundro++;
3475         } else if (tst == ENODEV) {     /* RW volume does not exist */
3476             Lp_SetROValue(cellHandle, entry, entry->serverNumber[idx],
3477                           entry->serverPartition[idx], 0L, 0L);
3478             entry->nServers--;
3479             idx--;
3480             modentry++;
3481         } else {
3482             goto fail_CheckVldbRO;
3483         }
3484     }
3485
3486     if (foundro) {              /* A RO volume exists */
3487         if (!(entry->flags & RO_EXISTS)) {      /* ... yet entry says RW does not e
3488                                                  * xist */
3489             entry->flags |= RO_EXISTS;  /* ... so say RW does exist */
3490             modentry++;
3491         }
3492     } else {                    /* A RO volume does not exist */
3493         if (entry->flags & RO_EXISTS) { /* ... yet entry says RO exists */
3494             entry->flags &= ~RO_EXISTS; /* ... so say RO does not exist */
3495             modentry++;
3496         }
3497     }
3498     rc = 1;
3499
3500   fail_CheckVldbRO:
3501
3502     if (modified)
3503         *modified = modentry;
3504
3505     if (st != NULL) {
3506         *st = tst;
3507     }
3508     return rc;
3509 }
3510
3511 /*ensure that <entry> matches with the info on file servers */
3512 int
3513 CheckVldb(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
3514           afs_int32 * modified, afs_status_p st)
3515 {
3516     int rc = 0;
3517     afs_status_t tst = 0;
3518     afs_int32 vcode;
3519     int islocked = 0;
3520     int pass = 0;
3521     afs_int32 modentry = 0;
3522     afs_int32 delentry = 0;
3523
3524     if (modified) {
3525         *modified = 0;
3526     }
3527
3528     if (strlen(entry->name) > (VOLSER_OLDMAXVOLNAME - 10)) {
3529         tst = VOLSERBADOP;
3530         goto fail_CheckVldb;
3531     }
3532
3533   retry:
3534
3535     /* Check to see if the VLDB is ok without locking it (pass 1).
3536      * If it will change, then lock the VLDB entry, read it again,
3537      * then make the changes to it (pass 2).
3538      */
3539     if (++pass == 2) {
3540         tst =
3541             ubik_VL_SetLock(cellHandle->vos, 0, entry->volumeId[RWVOL],
3542                       RWVOL, VLOP_DELETE);
3543         if (tst) {
3544             goto fail_CheckVldb;
3545         }
3546         islocked = 1;
3547
3548         if (!aVLDB_GetEntryByID
3549             (cellHandle, entry->volumeId[RWVOL], RWVOL, entry, &tst)) {
3550             goto fail_CheckVldb;
3551         }
3552     }
3553
3554     modentry = 0;
3555
3556     /* Check if the RW and BK entries are ok */
3557     if (!CheckVldbRWBK(cellHandle, entry, &modentry, &tst)) {
3558         goto fail_CheckVldb;
3559     }
3560     if (modentry && (pass == 1))
3561         goto retry;
3562
3563     /* Check if the RO volumes entries are ok */
3564     if (!CheckVldbRO(cellHandle, entry, &modentry, &tst)) {
3565         goto fail_CheckVldb;
3566     }
3567     if (modentry && (pass == 1))
3568         goto retry;
3569
3570     /* The VLDB entry has been updated. If it as been modified, then
3571      * write the entry back out the the VLDB.
3572      */
3573     if (modentry) {
3574         if (pass == 1)
3575             goto retry;
3576
3577         if (!(entry->flags & RW_EXISTS) && !(entry->flags & BACK_EXISTS)
3578             && !(entry->flags & RO_EXISTS)) {
3579             /* The RW, BK, nor RO volumes do not exist. Delete the VLDB entry */
3580             tst =
3581                 ubik_VL_DeleteEntry(cellHandle->vos, 0,
3582                           entry->volumeId[RWVOL], RWVOL);
3583             if (tst) {
3584                 goto fail_CheckVldb;
3585             }
3586             delentry = 1;
3587         } else {
3588             /* Replace old entry with our new one */
3589             if (!VLDB_ReplaceEntry
3590                 (cellHandle, entry->volumeId[RWVOL], RWVOL, entry,
3591                  (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP),
3592                  &tst)) {
3593                 goto fail_CheckVldb;
3594             }
3595         }
3596         if (modified)
3597             *modified = 1;
3598         islocked = 0;
3599     }
3600     rc = 1;
3601
3602   fail_CheckVldb:
3603
3604     if (islocked) {
3605         vcode =
3606             ubik_VL_ReleaseLock(cellHandle->vos, 0,
3607                                 entry->volumeId[RWVOL], RWVOL,
3608                                 (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
3609         if (vcode) {
3610             if (!tst)
3611                 tst = vcode;
3612         }
3613     }
3614
3615     if (st != NULL) {
3616         *st = tst;
3617     }
3618     return rc;
3619 }
3620
3621 /*synchronise <aserver> <apart>(if flags = 1) with the vldb .
3622 *if flags = 0, synchronise all the valid partitions on <aserver>*/
3623 int
3624 UV_SyncServer(afs_cell_handle_p cellHandle, struct rx_connection *server,
3625               afs_int32 apart, int flags, afs_status_p st)
3626 {
3627     int rc = 0;
3628     afs_status_t tst = 0;
3629     afs_int32 code, vcode;
3630     int noError;
3631     afs_int32 nentries, tentries = 0;
3632     struct VldbListByAttributes attributes;
3633     nbulkentries arrayEntries;
3634     int totalF;
3635     struct nvldbentry *vllist;
3636     int j;
3637     afs_int32 si, nsi;
3638     afs_int32 modified = 0;
3639
3640     code = 0;
3641     vcode = 0;
3642     noError = 1;
3643     arrayEntries.nbulkentries_val = 0;
3644
3645     /* Set up attributes to search VLDB  */
3646     attributes.server = ntohl(rx_HostOf(rx_PeerOf(server)));
3647     attributes.Mask = VLLIST_SERVER;
3648     if (flags) {
3649         attributes.partition = apart;
3650         attributes.Mask |= VLLIST_PARTITION;
3651     }
3652
3653     for (si = 0; si != -1; si = nsi) {
3654         /*initialize to hint the stub  to alloc space */
3655         memset(&arrayEntries, 0, sizeof(arrayEntries));
3656         if (!VLDB_ListAttributes
3657             (cellHandle, &attributes, &nentries, &arrayEntries, &tst)) {
3658             goto fail_UV_SyncServer;
3659         }
3660         nsi = -1;
3661         tentries += nentries;
3662         totalF = 0;
3663         for (j = 0; j < nentries; j++) {        /* process each entry */
3664             vllist = &arrayEntries.nbulkentries_val[j];
3665             if (!CheckVldb(cellHandle, vllist, &modified, &tst)) {
3666                 noError = 0;
3667                 totalF++;
3668             }
3669         }
3670         if (arrayEntries.nbulkentries_val) {
3671             free(arrayEntries.nbulkentries_val);
3672             arrayEntries.nbulkentries_val = 0;
3673         }
3674     }
3675     rc = 1;
3676
3677   fail_UV_SyncServer:
3678
3679     if (arrayEntries.nbulkentries_val) {
3680         free(arrayEntries.nbulkentries_val);
3681     }
3682     if (!noError)
3683         tst = VOLSERFAILEDOP;
3684     if (st != NULL) {
3685         *st = tst;
3686     }
3687     return rc;
3688 }
3689
3690 /* rename volume <oldname> to <newname>, changing the names of the related
3691  * readonly and backup volumes. This operation is also idempotent.
3692  * salvager is capable of recovering from rename operation stopping halfway.
3693  * to recover run syncserver on the affected machines,it will force
3694  * renaming to completion. name clashes should have been detected before
3695  * calling this proc
3696  */
3697 int
3698 UV_RenameVolume(afs_cell_handle_p cellHandle, struct nvldbentry *entry,
3699                 char *newname, afs_status_p st)
3700 {
3701     int rc = 0;
3702     afs_status_t tst = 0;
3703     afs_status_t etst = 0;
3704     afs_int32 rcode, error;
3705     int i, index;
3706     char nameBuffer[256];
3707     afs_int32 tid;
3708     struct rx_connection *aconn;
3709     int islocked;
3710
3711     error = 0;
3712     aconn = (struct rx_connection *)0;
3713     tid = 0;
3714     islocked = 0;
3715
3716     tst = ubik_VL_SetLock(cellHandle->vos, 0, entry->volumeId[RWVOL], RWVOL, VLOP_ADDSITE);     /*last param is dummy */
3717     if (tst) {
3718         goto fail_UV_RenameVolume;
3719     }
3720     islocked = 1;
3721
3722     strncpy(entry->name, newname, VOLSER_OLDMAXVOLNAME);
3723
3724     if (!VLDB_ReplaceEntry
3725         (cellHandle, entry->volumeId[RWVOL], RWVOL, entry, 0, &tst)) {
3726         goto fail_UV_RenameVolume;
3727     }
3728     /*at this stage the intent to rename is recorded in the vldb, as far
3729      * as the vldb 
3730      * is concerned, oldname is lost */
3731     if (entry->flags & RW_EXISTS) {
3732         index = Lp_GetRwIndex(cellHandle, entry, 0);
3733         if (index == -1) {      /* there is a serious discrepancy */
3734             tst = VOLSERVLDB_ERROR;
3735             goto fail_UV_RenameVolume;
3736         }
3737         aconn =
3738             UV_Bind(cellHandle, entry->serverNumber[index],
3739                     AFSCONF_VOLUMEPORT);
3740         tst =
3741             AFSVolTransCreate(aconn, entry->volumeId[RWVOL],
3742                               entry->serverPartition[index], ITOffline, &tid);
3743         if (tst) {              /*volume doesnot exist */
3744             goto fail_UV_RenameVolume;
3745         } else {                /*volume exists, process it */
3746
3747             tst =
3748                 AFSVolSetIdsTypes(aconn, tid, newname, RWVOL,
3749                                   entry->volumeId[RWVOL],
3750                                   entry->volumeId[ROVOL],
3751                                   entry->volumeId[BACKVOL]);
3752             if (!tst) {
3753                 tst = AFSVolEndTrans(aconn, tid, &rcode);
3754                 tid = 0;
3755                 if (tst) {
3756                     goto fail_UV_RenameVolume;
3757                 }
3758             } else {
3759                 goto fail_UV_RenameVolume;
3760             }
3761         }
3762         if (aconn)
3763             rx_ReleaseCachedConnection(aconn);
3764         aconn = (struct rx_connection *)0;
3765     }
3766     /*end rw volume processing */
3767     if (entry->flags & BACK_EXISTS) {   /*process the backup volume */
3768         index = Lp_GetRwIndex(cellHandle, entry, 0);
3769         if (index == -1) {      /* there is a serious discrepancy */
3770             tst = VOLSERVLDB_ERROR;
3771             goto fail_UV_RenameVolume;
3772         }
3773         aconn =
3774             UV_Bind(cellHandle, entry->serverNumber[index],
3775                     AFSCONF_VOLUMEPORT);
3776         tst =
3777             AFSVolTransCreate(aconn, entry->volumeId[BACKVOL],
3778                               entry->serverPartition[index], ITOffline, &tid);
3779         if (tst) {              /*volume doesnot exist */
3780             goto fail_UV_RenameVolume;
3781         } else {                /*volume exists, process it */
3782             if (strlen(newname) > (VOLSER_OLDMAXVOLNAME - 8)) {
3783                 goto fail_UV_RenameVolume;
3784             }
3785             strcpy(nameBuffer, newname);
3786             strcat(nameBuffer, ".backup");
3787
3788             tst =
3789                 AFSVolSetIdsTypes(aconn, tid, nameBuffer, BACKVOL,
3790                                   entry->volumeId[RWVOL], 0, 0);
3791             if (!tst) {
3792                 tst = AFSVolEndTrans(aconn, tid, &rcode);
3793                 tid = 0;
3794                 if (tst) {
3795                     goto fail_UV_RenameVolume;
3796                 }
3797             } else {
3798                 goto fail_UV_RenameVolume;
3799             }
3800         }
3801     }                           /* end backup processing */
3802     if (aconn)
3803         rx_ReleaseCachedConnection(aconn);
3804     aconn = (struct rx_connection *)0;
3805     if (entry->flags & RO_EXISTS) {     /*process the ro volumes */
3806         for (i = 0; i < entry->nServers; i++) {
3807             if (entry->serverFlags[i] & ITSROVOL) {
3808                 aconn =
3809                     UV_Bind(cellHandle, entry->serverNumber[i],
3810                             AFSCONF_VOLUMEPORT);
3811                 tst =
3812                     AFSVolTransCreate(aconn, entry->volumeId[ROVOL],
3813                                       entry->serverPartition[i], ITOffline,
3814                                       &tid);
3815                 if (tst) {      /*volume doesnot exist */
3816                     goto fail_UV_RenameVolume;
3817                 } else {        /*volume exists, process it */
3818                     strcpy(nameBuffer, newname);
3819                     strcat(nameBuffer, ".readonly");
3820                     if (strlen(nameBuffer) > (VOLSER_OLDMAXVOLNAME - 1)) {
3821                         goto fail_UV_RenameVolume;
3822                     }
3823                     tst =
3824                         AFSVolSetIdsTypes(aconn, tid, nameBuffer, ROVOL,
3825                                           entry->volumeId[RWVOL], 0, 0);
3826                     if (!tst) {
3827                         tst = AFSVolEndTrans(aconn, tid, &rcode);
3828                         tid = 0;
3829                         if (tst) {
3830                             goto fail_UV_RenameVolume;
3831                         }
3832                     } else {
3833                         goto fail_UV_RenameVolume;
3834                     }
3835                 }
3836                 if (aconn)
3837                     rx_ReleaseCachedConnection(aconn);
3838                 aconn = (struct rx_connection *)0;
3839             }
3840         }
3841     }
3842     rc = 1;
3843
3844   fail_UV_RenameVolume:
3845
3846     if (islocked) {
3847         etst =
3848             ubik_VL_ReleaseLock(cellHandle->vos, 0,
3849                       entry->volumeId[RWVOL], RWVOL,
3850                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
3851         if (etst) {
3852             if (!tst)
3853                 tst = etst;
3854         }
3855     }
3856     if (tid) {
3857         etst = AFSVolEndTrans(aconn, tid, &rcode);
3858         if (etst) {
3859             if (!tst)
3860                 tst = etst;
3861         }
3862     }
3863     if (aconn)
3864         rx_ReleaseCachedConnection(aconn);
3865
3866     if (st != NULL) {
3867         *st = tst;
3868     }
3869     return rc;
3870 }
3871
3872 /*group all the volume entries on< apart >by their parentid or by their ids'
3873 *if the volume is rw. <count> is the number of entries to be processesd.
3874 *<pntr> points to the first entry.< myQueue> is the queue with entries 
3875 *grouped */
3876 static afs_int32
3877 GroupEntries(struct rx_connection *server, volintInfo * pntr, afs_int32 count,
3878              struct qHead *myQueue, afs_int32 apart)
3879 {
3880     struct aqueue *qPtr;
3881     int success;
3882     afs_int32 curId, code;
3883     int i;
3884     afs_int32 error = 0;
3885
3886
3887     Lp_QInit(myQueue);
3888     if (count == 0)
3889         return 0;
3890     for (i = 0; i < count; i++) {       /*process each entry */
3891         if (pntr->status) {     /* entry is valid */
3892             if (pntr->type == RWVOL)
3893                 curId = pntr->volid;
3894             else
3895                 curId = pntr->parentID;
3896             Lp_QScan(myQueue, curId, &success, &qPtr, 0);
3897             if (success) {      /*entry exists in the queue */
3898                 if (pntr->type == RWVOL) {
3899                     /*check if the rw exists already, if so hang on the
3900                      * later version if the present version is ok */
3901                     if (qPtr->isValid[RWVOL]) {
3902                         /*this should not happen, there is a serious error here */
3903                         if (!error)
3904                             error = VOLSERMULTIRWVOL;
3905                     } else {
3906                         qPtr->isValid[RWVOL] = 1;
3907                         qPtr->copyDate[RWVOL] = pntr->copyDate;
3908                         if (!qPtr->isValid[BACKVOL])
3909                             qPtr->ids[BACKVOL] = pntr->backupID;
3910                         if (!qPtr->isValid[ROVOL])
3911                             qPtr->ids[ROVOL] = pntr->cloneID;
3912                     }
3913                 } else if (pntr->type == BACKVOL) {
3914                     if (qPtr->isValid[BACKVOL]) {
3915                         /*do different checks, delete superflous volume */
3916                         if (qPtr->copyDate[BACKVOL] > pntr->copyDate) {
3917                             /*delete the present volume . */
3918                             code =
3919                                 CheckAndDeleteVolume(server, apart, 0,
3920                                                      pntr->volid);
3921                             if (code) {
3922                                 if (!error)
3923                                     error = code;
3924                             }
3925
3926                         } else {
3927                             /*delete the older volume after making sure, current one is ok */
3928                             code =
3929                                 CheckAndDeleteVolume(server, apart,
3930                                                      pntr->volid,
3931                                                      qPtr->ids[BACKVOL]);
3932                             if (code) {
3933                                 if (!error)
3934                                     error = code;
3935                             }
3936
3937                             qPtr->copyDate[BACKVOL] = pntr->copyDate;
3938                             qPtr->ids[BACKVOL] = pntr->volid;
3939
3940                         }
3941                     } else {
3942                         qPtr->isValid[BACKVOL] = 1;
3943                         qPtr->ids[BACKVOL] = pntr->volid;
3944                         qPtr->copyDate[BACKVOL] = pntr->copyDate;
3945                     }
3946                 } else if (pntr->type == ROVOL) {
3947                     if (qPtr->isValid[ROVOL]) {
3948                         /*do different checks, delete superflous volume */
3949                         if (qPtr->copyDate[ROVOL] > pntr->copyDate) {
3950                             /*delete the present volume . */
3951                             /*a hack */
3952                             code =
3953                                 CheckAndDeleteVolume(server, apart, 0,
3954                                                      pntr->volid);
3955                             if (code) {
3956                                 if (!error)
3957                                     error = code;
3958                             }
3959                         } else {
3960                             /*delete the older volume after making sure, current one is ok */
3961                             code =
3962                                 CheckAndDeleteVolume(server, apart,
3963                                                      pntr->volid,
3964                                                      qPtr->ids[ROVOL]);
3965                             if (code) {
3966                                 if (!error)
3967                                     error = code;
3968                             }
3969
3970                             qPtr->copyDate[ROVOL] = pntr->copyDate;
3971                             qPtr->ids[ROVOL] = pntr->volid;
3972
3973                         }
3974                     } else {
3975                         qPtr->isValid[ROVOL] = 1;
3976                         qPtr->ids[ROVOL] = pntr->volid;
3977                         qPtr->copyDate[ROVOL] = pntr->copyDate;
3978                     }
3979                 } else {
3980                     if (!error)
3981                         error = VOLSERBADOP;
3982                 }
3983             } else {            /*create a fresh entry */
3984                 qPtr = (struct aqueue *)malloc(sizeof(struct aqueue));
3985                 if (pntr->type == RWVOL) {
3986                     qPtr->isValid[RWVOL] = 1;
3987                     qPtr->isValid[BACKVOL] = 0;
3988                     qPtr->isValid[ROVOL] = 0;
3989                     qPtr->ids[RWVOL] = pntr->volid;
3990                     qPtr->ids[BACKVOL] = pntr->backupID;
3991                     qPtr->ids[ROVOL] = pntr->cloneID;
3992                     qPtr->copyDate[RWVOL] = pntr->copyDate;
3993                     strncpy(qPtr->name, pntr->name, VOLSER_OLDMAXVOLNAME);
3994                     qPtr->next = NULL;
3995                 } else if (pntr->type == BACKVOL) {
3996                     qPtr->isValid[RWVOL] = 0;
3997                     qPtr->isValid[BACKVOL] = 1;
3998                     qPtr->isValid[ROVOL] = 0;
3999                     qPtr->ids[RWVOL] = pntr->parentID;
4000                     qPtr->ids[BACKVOL] = pntr->volid;
4001                     qPtr->ids[ROVOL] = 0;
4002                     qPtr->copyDate[BACKVOL] = pntr->copyDate;
4003                     vsu_ExtractName(qPtr->name, pntr->name);
4004                     qPtr->next = NULL;
4005                 } else if (pntr->type == ROVOL) {
4006                     qPtr->isValid[RWVOL] = 0;
4007                     qPtr->isValid[BACKVOL] = 0;
4008                     qPtr->isValid[ROVOL] = 1;
4009                     qPtr->ids[RWVOL] = pntr->parentID;
4010                     qPtr->ids[BACKVOL] = 0;
4011                     qPtr->ids[ROVOL] = pntr->volid;
4012                     qPtr->copyDate[ROVOL] = pntr->copyDate;
4013                     vsu_ExtractName(qPtr->name, pntr->name);
4014                     qPtr->next = NULL;
4015
4016                 }
4017                 Lp_QAdd(myQueue, qPtr);
4018             }
4019             pntr++;             /*get next entry */
4020         } else {
4021             pntr++;
4022             continue;
4023         }
4024     }                           /* for loop */
4025
4026     return error;
4027 }
4028
4029 /*report on all the active transactions on volser */
4030 int
4031 UV_VolserStatus(struct rx_connection *server, transDebugInfo ** rpntr,
4032                 afs_int32 * rcount, afs_status_p st)
4033 {
4034     int rc = 0;
4035     afs_status_t tst = 0;
4036     transDebugEntries transInfo;
4037
4038     transInfo.transDebugEntries_val = (transDebugInfo *) 0;
4039     transInfo.transDebugEntries_len = 0;
4040     tst = AFSVolMonitor(server, &transInfo);
4041     if (tst) {
4042         goto fail_UV_VolserStatus;
4043     }
4044
4045     *rcount = transInfo.transDebugEntries_len;
4046     *rpntr = transInfo.transDebugEntries_val;
4047     rc = 1;
4048
4049   fail_UV_VolserStatus:
4050
4051     if (rc == 0) {
4052         if (transInfo.transDebugEntries_val) {
4053             free(transInfo.transDebugEntries_val);
4054         }
4055     }
4056
4057     if (st != NULL) {
4058         *st = tst;
4059     }
4060     return rc;
4061 }
4062
4063 /*delete the volume without interacting with the vldb */
4064 int
4065 UV_VolumeZap(afs_cell_handle_p cellHandle, struct rx_connection *server,
4066              unsigned int partition, afs_uint32 volumeId, afs_status_p st)
4067 {
4068     afs_int32 rcode, ttid, error, code;
4069     int rc = 0;
4070     afs_status_t tst = 0;
4071
4072     code = 0;
4073     error = 0;
4074     ttid = 0;
4075
4076     tst = AFSVolTransCreate(server, volumeId, partition, ITOffline, &ttid);
4077     if (!tst) {
4078         tst = AFSVolDeleteVolume(server, ttid);
4079         if (!tst) {
4080             tst = AFSVolEndTrans(server, ttid, &rcode);
4081             if (!tst) {
4082                 if (rcode) {
4083                     tst = rcode;
4084                 }
4085             }
4086         } else {
4087             /*
4088              * We failed to delete the volume, but we still need
4089              * to end the transaction.
4090              */
4091             AFSVolEndTrans(server, ttid, &rcode);
4092         }
4093         rc = 1;
4094     }
4095
4096     if (st != NULL) {
4097         *st = tst;
4098     }
4099     return rc;
4100 }
4101
4102 int
4103 UV_SetVolume(struct rx_connection *server, afs_int32 partition,
4104              afs_uint32 volid, afs_int32 transflag, afs_int32 setflag,
4105              unsigned int sleepTime, afs_status_p st)
4106 {
4107     int rc = 0;
4108     afs_status_t tst = 0;
4109     afs_status_t etst = 0;
4110     afs_int32 tid = 0;
4111     afs_int32 rcode;
4112
4113     tst = AFSVolTransCreate(server, volid, partition, transflag, &tid);
4114     if (tst) {
4115         goto fail_UV_SetVolume;
4116     }
4117
4118     tst = AFSVolSetFlags(server, tid, setflag);
4119     if (tst) {
4120         goto fail_UV_SetVolume;
4121     }
4122
4123     if (sleepTime) {
4124         sleep(sleepTime);
4125     }
4126     rc = 1;
4127
4128   fail_UV_SetVolume:
4129
4130     if (tid) {
4131         etst = AFSVolEndTrans(server, tid, &rcode);
4132         /* FIXME: this looks like a typo */
4133         if (etst || etst) {
4134             if (!tst)
4135                 tst = (etst ? etst : rcode);
4136         }
4137     }
4138
4139     if (st != NULL) {
4140         *st = tst;
4141     }
4142     return rc;
4143 }