vos: more details in vos release -verbose output
[openafs.git] / src / volser / 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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
14 #include <roken.h>
15
16 #ifdef  AFS_AIX_ENV
17 #include <sys/statfs.h>
18 #endif
19
20 #include <lock.h>
21 #include <afs/voldefs.h>
22 #include <rx/xdr.h>
23 #include <rx/rx.h>
24 #include <rx/rx_queue.h>
25 #include <afs/vlserver.h>
26 #include <afs/nfs.h>
27 #include <afs/cellconfig.h>
28 #include <afs/keys.h>
29 #include <ubik.h>
30 #include <afs/afsint.h>
31 #include "volser.h"
32 #include "volint.h"
33 #include "lockdata.h"
34 #include <afs/com_err.h>
35 #include <rx/rxkad.h>
36 #include <afs/kautils.h>
37 #include <afs/cmd.h>
38 #include <afs/ihandle.h>
39 #ifdef AFS_NT40_ENV
40 #include <afs/ntops.h>
41 #endif
42 #include <afs/vnode.h>
43 #include <afs/volume.h>
44 #define ERRCODE_RANGE 8         /* from error_table.h */
45 #define CLOCKSKEW   2           /* not really skew, but resolution */
46 #define CLOCKADJ(x) (((x) < CLOCKSKEW) ? 0 : (x) - CLOCKSKEW)
47
48 /* for UV_MoveVolume() recovery */
49
50 #include <setjmp.h>
51
52 #include "volser_internal.h"
53 #include "volser_prototypes.h"
54 #include "vsutils_prototypes.h"
55 #include "lockprocs_prototypes.h"
56
57 extern struct ubik_client *cstruct;
58 int verbose = 0, noresolve = 0;
59
60 struct release {
61     afs_uint32 crtime;
62     afs_uint32 uptime;
63     afs_int32 vldbEntryIndex;
64 };
65
66 /* Utility macros used by rest of this source file */
67 #define EPRINT(ec, es) \
68 do { \
69         fprintf(STDERR, "\n"); \
70         fprintf(STDERR, (es)); \
71         PrintError("   ",ec); \
72 } while (0)
73
74 #define EPRINT1(ec, es, ep1) \
75 do { \
76         fprintf(STDERR, "\n"); \
77         fprintf(STDERR, (es), (ep1)); \
78         PrintError("   ",ec); \
79 } while (0)
80
81 #define EPRINT2(ec, es, ep1, ep2) \
82 do { \
83         fprintf(STDERR, "\n"); \
84         fprintf(STDERR, (es), (ep1), (ep2)); \
85         PrintError("   ",ec); \
86 } while (0)
87
88 #define EPRINT3(ec, es, ep1, ep2, ep3) \
89 do { \
90         fprintf(STDERR, "\n"); \
91         fprintf(STDERR, (es), (ep1), (ep2), (ep3)); \
92         PrintError("   ",ec); \
93 } while (0)
94
95 #define EGOTO(where, ec, es) \
96 do { \
97         if (ec) { \
98                 EPRINT((ec),(es)); \
99                 error = (ec); \
100                 goto where; \
101         } \
102 } while (0)
103
104 #define EGOTO1(where, ec, es, ep1) \
105 do { \
106         if (ec) { \
107                 EPRINT1((ec),(es),(ep1)); \
108                 error = (ec); \
109                 goto where; \
110         } \
111 } while (0)
112
113 #define EGOTO2(where, ec, es, ep1, ep2) \
114 do { \
115         if (ec) { \
116                 EPRINT2((ec),(es),(ep1),(ep2)); \
117                 error = (ec); \
118                 goto where; \
119         } \
120 } while (0)
121
122 #define EGOTO3(where, ec, es, ep1, ep2, ep3) \
123 do { \
124         if (ec) { \
125                 EPRINT3((ec),(es),(ep1),(ep2),(ep3)); \
126                 error = (ec); \
127                 goto where; \
128         } \
129 } while (0)
130
131 #define VPRINT(es) \
132         { if (verbose) { fprintf(STDOUT, (es)); fflush(STDOUT); } }
133 #define VPRINT1(es, p) \
134         { if (verbose) { fprintf(STDOUT, (es), (p)); fflush(STDOUT); } }
135 #define VPRINT2(es, p1, p2) \
136         { if (verbose) { fprintf(STDOUT, (es), (p1), (p2)); fflush(STDOUT); } }
137 #define VPRINT3(es, p1, p2, p3) \
138         { if (verbose) { fprintf(STDOUT, (es), (p1), (p2), (p3)); fflush(STDOUT); } }
139 #define VDONE \
140         { if (verbose) { fprintf(STDOUT, " done\n"); fflush(STDOUT); } }
141 #define VEPRINT(es) \
142         { if (verbose) { fprintf(STDERR, (es)); fflush(STDERR); } }
143 #define VEPRINT1(es, p) \
144         { if (verbose) { fprintf(STDERR, (es), (p)); fflush(STDERR); } }
145 #define VEPRINT2(es, p1, p2) \
146         { if (verbose) { fprintf(STDERR, (es), (p1), (p2)); fflush(STDERR); } }
147 #define VEPRINT3(es, p1, p2, p3) \
148         { if (verbose) { fprintf(STDERR, (es), (p1), (p2), (p3)); fflush(STDERR); } }
149 #define VEDONE \
150         { if (verbose) { fprintf(STDERR, " done\n"); fflush(STDERR); } }
151
152
153
154 /* getting rid of this */
155 #define ERROR_EXIT(code) do { \
156     error = (code); \
157     goto error_exit; \
158 } while (0)
159
160
161 /* Protos for static routines */
162 #if 0
163 static afs_int32 CheckAndDeleteVolume(struct rx_connection *aconn,
164                                       afs_int32 apart, afs_uint32 okvol,
165                                       afs_uint32 delvol);
166 #endif
167 static int GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
168                     struct rx_connection **connPtr, afs_int32 * transPtr,
169                     afs_uint32 * crtimePtr, afs_uint32 * uptimePtr,
170                     afs_int32 *origflags, afs_uint32 tmpVolId);
171 static int SimulateForwardMultiple(struct rx_connection *fromconn,
172                                    afs_int32 fromtid, afs_int32 fromdate,
173                                    manyDests * tr, afs_int32 flags,
174                                    void *cookie, manyResults * results);
175 static int DoVolOnline(struct nvldbentry *vldbEntryPtr, afs_uint32 avolid,
176                        int index, char *vname, struct rx_connection *connPtr);
177 static int DoVolClone(struct rx_connection *aconn, afs_uint32 avolid,
178                       afs_int32 apart, int type, afs_uint32 cloneid,
179                       char *typestring, char *pname, char *vname, char *suffix,
180                       struct volser_status *volstatus, afs_int32 *transPtr);
181 static int DoVolDelete(struct rx_connection *aconn, afs_uint32 avolid,
182                        afs_int32 apart, char *typestring, afs_uint32 atoserver,
183                        struct volser_status *volstatus, char *pprefix);
184 static afs_int32 CheckVolume(volintInfo * volumeinfo, afs_uint32 aserver,
185                              afs_int32 apart, afs_int32 * modentry,
186                              afs_uint32 * maxvolid, struct nvldbentry *aentry);
187 static afs_int32 VolumeExists(afs_uint32 server, afs_int32 partition,
188                               afs_uint32 volumeid);
189 static afs_int32 CheckVldbRWBK(struct nvldbentry * entry,
190                                afs_int32 * modified);
191 static afs_int32 CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified);
192 static afs_int32 CheckVldb(struct nvldbentry *entry, afs_int32 * modified,
193                            afs_int32 *deleted);
194 static void dump_sig_handler(int x);
195 static int sortVolumes(const void *a, const void *b);
196
197
198 /*map the partition <partId> into partition name <partName>*/
199 void
200 MapPartIdIntoName(afs_int32 partId, char *partName)
201 {
202     if (partId < 26) {          /* what if partId > = 26 ? */
203         strcpy(partName, "/vicep");
204         partName[6] = partId + 'a';
205         partName[7] = '\0';
206         return;
207     } else if (partId < VOLMAXPARTS) {
208         strcpy(partName, "/vicep");
209         partId -= 26;
210         partName[6] = 'a' + (partId / 26);
211         partName[7] = 'a' + (partId % 26);
212         partName[8] = '\0';
213         return;
214     }
215 }
216
217 int
218 PrintError(char *msg, afs_int32 errcode)
219 {
220     fprintf(STDERR, "%s", msg);
221     /*replace by a big switch statement */
222     switch (errcode) {
223     case 0:
224         break;
225     case -1:
226         fprintf(STDERR, "Possible communication failure\n");
227         break;
228     case VSALVAGE:
229         fprintf(STDERR, "Volume needs to be salvaged\n");
230         break;
231     case VNOVNODE:
232         fprintf(STDERR, "Bad vnode number quoted\n");
233         break;
234     case VNOVOL:
235         fprintf(STDERR,
236                 "Volume not attached, does not exist, or not on line\n");
237         break;
238     case VVOLEXISTS:
239         fprintf(STDERR, "Volume already exists\n");
240         break;
241     case VNOSERVICE:
242         fprintf(STDERR, "Volume is not in service\n");
243         break;
244     case VOFFLINE:
245         fprintf(STDERR, "Volume is off line\n");
246         break;
247     case VONLINE:
248         fprintf(STDERR, "Volume is already on line\n");
249         break;
250     case VDISKFULL:
251         fprintf(STDERR, "Partition is full\n");
252         break;
253     case VOVERQUOTA:
254         fprintf(STDERR, "Volume max quota exceeded\n");
255         break;
256     case VBUSY:
257         fprintf(STDERR, "Volume temporarily unavailable\n");
258         break;
259     case VMOVED:
260         fprintf(STDERR, "Volume has moved to another server\n");
261         break;
262     case VL_IDEXIST:
263         fprintf(STDERR, "VLDB: volume Id exists in the vldb\n");
264         break;
265     case VL_IO:
266         fprintf(STDERR, "VLDB: a read terminated too early\n");
267         break;
268     case VL_NAMEEXIST:
269         fprintf(STDERR, "VLDB: volume entry exists in the vldb\n");
270         break;
271     case VL_CREATEFAIL:
272         fprintf(STDERR, "VLDB: internal creation failure\n");
273         break;
274     case VL_NOENT:
275         fprintf(STDERR, "VLDB: no such entry\n");
276         break;
277     case VL_EMPTY:
278         fprintf(STDERR, "VLDB: vldb database is empty\n");
279         break;
280     case VL_ENTDELETED:
281         fprintf(STDERR, "VLDB: entry is deleted (soft delete)\n");
282         break;
283     case VL_BADNAME:
284         fprintf(STDERR, "VLDB: volume name is illegal\n");
285         break;
286     case VL_BADINDEX:
287         fprintf(STDERR, "VLDB: index was out of range\n");
288         break;
289     case VL_BADVOLTYPE:
290         fprintf(STDERR, "VLDB: bad volume type\n");
291         break;
292     case VL_BADSERVER:
293         fprintf(STDERR, "VLDB: illegal server number (not within limits)\n");
294         break;
295     case VL_BADPARTITION:
296         fprintf(STDERR, "VLDB: bad partition number\n");
297         break;
298     case VL_REPSFULL:
299         fprintf(STDERR, "VLDB: run out of space for replication sites\n");
300         break;
301     case VL_NOREPSERVER:
302         fprintf(STDERR, "VLDB: no such repsite server exists\n");
303         break;
304     case VL_DUPREPSERVER:
305         fprintf(STDERR, "VLDB: replication site server already exists\n");
306         break;
307     case VL_RWNOTFOUND:
308         fprintf(STDERR, "VLDB: parent r/w entry not found\n");
309         break;
310     case VL_BADREFCOUNT:
311         fprintf(STDERR, "VLDB: illegal reference count number\n");
312         break;
313     case VL_SIZEEXCEEDED:
314         fprintf(STDERR, "VLDB: vldb size for attributes exceeded\n");
315         break;
316     case VL_BADENTRY:
317         fprintf(STDERR, "VLDB: bad incoming vldb entry\n");
318         break;
319     case VL_BADVOLIDBUMP:
320         fprintf(STDERR, "VLDB: illegal max volid increment\n");
321         break;
322     case VL_IDALREADYHASHED:
323         fprintf(STDERR, "VLDB: (RO/BACK) Id already hashed\n");
324         break;
325     case VL_ENTRYLOCKED:
326         fprintf(STDERR, "VLDB: vldb entry is already locked\n");
327         break;
328     case VL_BADVOLOPER:
329         fprintf(STDERR, "VLDB: bad volume operation code\n");
330         break;
331     case VL_BADRELLOCKTYPE:
332         fprintf(STDERR, "VLDB: bad release lock type\n");
333         break;
334     case VL_RERELEASE:
335         fprintf(STDERR, "VLDB: status report: last release was aborted\n");
336         break;
337     case VL_BADSERVERFLAG:
338         fprintf(STDERR, "VLDB: invalid replication site server flag\n");
339         break;
340     case VL_PERM:
341         fprintf(STDERR, "VLDB: no permission access for call\n");
342         break;
343     case VOLSERREAD_DUMPERROR:
344         fprintf(STDERR,
345                 "VOLSER:  Problems encountered in reading the dump file !\n");
346         break;
347     case VOLSERDUMPERROR:
348         fprintf(STDERR, "VOLSER: Problems encountered in doing the dump !\n");
349         break;
350     case VOLSERATTACH_ERROR:
351         fprintf(STDERR, "VOLSER: Could not attach the volume\n");
352         break;
353     case VOLSERDETACH_ERROR:
354         fprintf(STDERR, "VOLSER: Could not detach the volume\n");
355         break;
356     case VOLSERILLEGAL_PARTITION:
357         fprintf(STDERR, "VOLSER: encountered illegal partition number\n");
358         break;
359     case VOLSERBAD_ACCESS:
360         fprintf(STDERR, "VOLSER: permission denied, not a super user\n");
361         break;
362     case VOLSERVLDB_ERROR:
363         fprintf(STDERR, "VOLSER: error detected in the VLDB\n");
364         break;
365     case VOLSERBADNAME:
366         fprintf(STDERR, "VOLSER: error in volume name\n");
367         break;
368     case VOLSERVOLMOVED:
369         fprintf(STDERR, "VOLSER: volume has moved\n");
370         break;
371     case VOLSERBADOP:
372         fprintf(STDERR, "VOLSER: illegal operation\n");
373         break;
374     case VOLSERBADRELEASE:
375         fprintf(STDERR, "VOLSER: release could not be completed\n");
376         break;
377     case VOLSERVOLBUSY:
378         fprintf(STDERR, "VOLSER: volume is busy\n");
379         break;
380     case VOLSERNO_MEMORY:
381         fprintf(STDERR, "VOLSER: volume server is out of memory\n");
382         break;
383     case VOLSERNOVOL:
384         fprintf(STDERR,
385                 "VOLSER: no such volume - location specified incorrectly or volume does not exist\n");
386         break;
387     case VOLSERMULTIRWVOL:
388         fprintf(STDERR,
389                 "VOLSER: multiple RW volumes with same ID, one of which should be deleted\n");
390         break;
391     case VOLSERFAILEDOP:
392         fprintf(STDERR,
393                 "VOLSER: not all entries were successfully processed\n");
394         break;
395     default:
396         {
397             initialize_RXK_error_table();
398             initialize_KTC_error_table();
399             initialize_ACFG_error_table();
400             initialize_VL_error_table();
401
402             fprintf(STDERR, "%s: %s\n", afs_error_table_name(errcode),
403                     afs_error_message(errcode));
404             break;
405         }
406     }
407     return 0;
408 }
409
410 void init_volintInfo(struct volintInfo *vinfo) {
411     memset(vinfo, 0, sizeof(struct volintInfo));
412
413     vinfo->maxquota = -1;
414     vinfo->dayUse = -1;
415     vinfo->creationDate = -1;
416     vinfo->updateDate = -1;
417     vinfo->flags = -1;
418     vinfo->spare0 = -1;
419     vinfo->spare1 = -1;
420     vinfo->spare2 = -1;
421     vinfo->spare3 = -1;
422 }
423
424 static struct rx_securityClass *uvclass = 0;
425 static int uvindex = -1;
426 /* called by VLDBClient_Init to set the security module to be used in the RPC */
427 int
428 UV_SetSecurity(struct rx_securityClass *as, afs_int32 aindex)
429 {
430     uvindex = aindex;
431     uvclass = as;
432     return 0;
433 }
434
435 /* bind to volser on <port> <aserver> */
436 /* takes server address in network order, port in host order.  dumb */
437 struct rx_connection *
438 UV_Bind(afs_uint32 aserver, afs_int32 port)
439 {
440     struct rx_connection *tc;
441
442     tc = rx_NewConnection(aserver, htons(port), VOLSERVICE_ID, uvclass,
443                           uvindex);
444     return tc;
445 }
446
447 static int
448 AFSVolCreateVolume_retry(struct rx_connection *z_conn,
449                        afs_int32 partition, char *name, afs_int32 type,
450                        afs_int32 parent, afs_uint32 *volid, afs_int32 *trans)
451 {
452     afs_int32 code;
453     int retries = 3;
454     while (retries) {
455         code = AFSVolCreateVolume(z_conn, partition, name, type, parent,
456                                   volid, trans);
457         if (code != VOLSERVOLBUSY)
458             break;
459         retries--;
460 #ifdef AFS_PTHREAD_ENV
461         sleep(3-retries);
462 #else
463         IOMGR_Sleep(3-retries);
464 #endif
465     }
466     return code;
467 }
468
469 static int
470 AFSVolTransCreate_retry(struct rx_connection *z_conn,
471                         afs_int32 volume, afs_int32 partition,
472                         afs_int32 flags, afs_int32 * trans)
473 {
474     afs_int32 code;
475     int retries = 3;
476     while (retries) {
477         code = AFSVolTransCreate(z_conn, volume, partition, flags, trans);
478         if (code != VOLSERVOLBUSY)
479             break;
480         retries--;
481 #ifdef AFS_PTHREAD_ENV
482         sleep(3-retries);
483 #else
484         IOMGR_Sleep(3-retries);
485 #endif
486     }
487     return code;
488 }
489
490 #if 0
491 /* if <okvol> is allright(indicated by beibg able to
492  * start a transaction, delete the <delvol> */
493 static afs_int32
494 CheckAndDeleteVolume(struct rx_connection *aconn, afs_int32 apart,
495                      afs_uint32 okvol, afs_uint32 delvol)
496 {
497     afs_int32 error, code, tid, rcode;
498     error = 0;
499     code = 0;
500
501     if (okvol == 0) {
502         code = AFSVolTransCreate_retry(aconn, delvol, apart, ITOffline, &tid);
503         if (!error && code)
504             error = code;
505         code = AFSVolDeleteVolume(aconn, tid);
506         if (!error && code)
507             error = code;
508         code = AFSVolEndTrans(aconn, tid, &rcode);
509         if (!code)
510             code = rcode;
511         if (!error && code)
512             error = code;
513         return error;
514     } else {
515         code = AFSVolTransCreate_retry(aconn, okvol, apart, ITOffline, &tid);
516         if (!code) {
517             code = AFSVolEndTrans(aconn, tid, &rcode);
518             if (!code)
519                 code = rcode;
520             if (!error && code)
521                 error = code;
522             code = AFSVolTransCreate_retry(aconn, delvol, apart, ITOffline, &tid);
523             if (!error && code)
524                 error = code;
525             code = AFSVolDeleteVolume(aconn, tid);
526             if (!error && code)
527                 error = code;
528             code = AFSVolEndTrans(aconn, tid, &rcode);
529             if (!code)
530                 code = rcode;
531             if (!error && code)
532                 error = code;
533         } else
534             error = code;
535         return error;
536     }
537 }
538
539 #endif
540
541 /* called by EmuerateEntry, show vldb entry in a reasonable format */
542 void
543 SubEnumerateEntry(struct nvldbentry *entry)
544 {
545     int i;
546     char pname[10];
547     int isMixed = 0;
548     char hoststr[16];
549
550 #ifdef notdef
551     fprintf(STDOUT, "   readWriteID %-10u ", entry->volumeId[RWVOL]);
552     if (entry->flags & RW_EXISTS)
553         fprintf(STDOUT, " valid \n");
554     else
555         fprintf(STDOUT, " invalid \n");
556     fprintf(STDOUT, "   readOnlyID  %-10u ", entry->volumeId[ROVOL]);
557     if (entry->flags & RO_EXISTS)
558         fprintf(STDOUT, " valid \n");
559     else
560         fprintf(STDOUT, " invalid \n");
561     fprintf(STDOUT, "   backUpID    %-10u ", entry->volumeId[BACKVOL]);
562     if (entry->flags & BACK_EXISTS)
563         fprintf(STDOUT, " valid \n");
564     else
565         fprintf(STDOUT, " invalid \n");
566     if ((entry->cloneId != 0) && (entry->flags & RO_EXISTS))
567         fprintf(STDOUT, "    releaseClone %-10u \n", entry->cloneId);
568 #else
569     if (entry->flags & RW_EXISTS)
570         fprintf(STDOUT, "    RWrite: %-10u", entry->volumeId[RWVOL]);
571     if (entry->flags & RO_EXISTS)
572         fprintf(STDOUT, "    ROnly: %-10u", entry->volumeId[ROVOL]);
573     if (entry->flags & BACK_EXISTS)
574         fprintf(STDOUT, "    Backup: %-10u", entry->volumeId[BACKVOL]);
575     if ((entry->cloneId != 0) && (entry->flags & RO_EXISTS))
576         fprintf(STDOUT, "    RClone: %-10lu", (unsigned long)entry->cloneId);
577     fprintf(STDOUT, "\n");
578 #endif
579     fprintf(STDOUT, "    number of sites -> %lu\n",
580             (unsigned long)entry->nServers);
581     for (i = 0; i < entry->nServers; i++) {
582         if (entry->serverFlags[i] & NEW_REPSITE)
583             isMixed = 1;
584     }
585     for (i = 0; i < entry->nServers; i++) {
586         MapPartIdIntoName(entry->serverPartition[i], pname);
587         fprintf(STDOUT, "       server %s partition %s ",
588                 noresolve ? afs_inet_ntoa_r(entry->serverNumber[i], hoststr) :
589                 hostutil_GetNameByINet(entry->serverNumber[i]), pname);
590         if (entry->serverFlags[i] & ITSRWVOL)
591             fprintf(STDOUT, "RW Site ");
592         else
593             fprintf(STDOUT, "RO Site ");
594         if (isMixed) {
595             if (entry->serverFlags[i] & NEW_REPSITE)
596                 fprintf(STDOUT," -- New release");
597             else
598                 if (!(entry->serverFlags[i] & ITSRWVOL))
599                     fprintf(STDOUT," -- Old release");
600         } else {
601             if (entry->serverFlags[i] & RO_DONTUSE)
602                 fprintf(STDOUT, " -- Not released");
603         }
604         fprintf(STDOUT, "\n");
605     }
606
607     return;
608
609 }
610
611 /*enumerate the vldb entry corresponding to <entry> */
612 void
613 EnumerateEntry(struct nvldbentry *entry)
614 {
615
616     fprintf(STDOUT, "\n");
617     fprintf(STDOUT, "%s \n", entry->name);
618     SubEnumerateEntry(entry);
619     return;
620 }
621
622 /* forcibly remove a volume.  Very dangerous call */
623 int
624 UV_NukeVolume(afs_uint32 server, afs_int32 partid, afs_uint32 volid)
625 {
626     struct rx_connection *tconn;
627     afs_int32 code;
628
629     tconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
630     if (tconn) {
631         code = AFSVolNukeVolume(tconn, partid, volid);
632         rx_DestroyConnection(tconn);
633     } else
634         code = 0;
635     return code;
636 }
637
638 /* like df. Return usage of <pname> on <server> in <partition> */
639 int
640 UV_PartitionInfo64(afs_uint32 server, char *pname,
641                    struct diskPartition64 *partition)
642 {
643     struct rx_connection *aconn;
644     afs_int32 code = 0;
645
646     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
647     code = AFSVolPartitionInfo64(aconn, pname, partition);
648     if (code == RXGEN_OPCODE) {
649         struct diskPartition *dpp = malloc(sizeof(struct diskPartition));
650         code = AFSVolPartitionInfo(aconn, pname, dpp);
651         if (!code) {
652             strncpy(partition->name, dpp->name, 32);
653             strncpy(partition->devName, dpp->devName, 32);
654             partition->lock_fd = dpp->lock_fd;
655             partition->free = dpp->free;
656             partition->minFree = dpp->minFree;
657         }
658         free(dpp);
659     }
660     if (code) {
661         fprintf(STDERR, "Could not get information on partition %s\n", pname);
662         PrintError("", code);
663     }
664     if (aconn)
665         rx_DestroyConnection(aconn);
666     return code;
667 }
668
669 /* old interface to create volumes */
670 int
671 UV_CreateVolume(afs_uint32 aserver, afs_int32 apart, char *aname,
672                 afs_uint32 * anewid)
673 {
674     afs_int32 code;
675     *anewid = 0;
676     code = UV_CreateVolume2(aserver, apart, aname, 5000, 0, 0, 0, 0, anewid);
677     return code;
678 }
679
680 /* less old interface to create volumes */
681 int
682 UV_CreateVolume2(afs_uint32 aserver, afs_int32 apart, char *aname,
683                  afs_int32 aquota, afs_int32 aspare1, afs_int32 aspare2,
684                  afs_int32 aspare3, afs_int32 aspare4, afs_uint32 * anewid)
685 {
686     afs_uint32 roid = 0, bkid = 0;
687     return UV_CreateVolume3(aserver, apart, aname, aquota, aspare1, aspare2,
688         aspare3, aspare4, anewid, &roid, &bkid);
689 }
690
691 /**
692  * Create a volume on the given server and partition
693  *
694  * @param aserver  server to create volume on
695  * @param spart  partition to create volume on
696  * @param aname  name of new volume
697  * @param aquota  quota for new volume
698  * @param anewid  contains the desired volume id for the new volume. If
699  *                *anewid == 0, a new id will be chosen, and will be placed
700  *                in *anewid when UV_CreateVolume3 returns.
701  * @param aroid  contains the desired RO volume id. If NULL, the RO id entry
702  *               will be unset. If *aroid == 0, an id will be chosen, and
703  *               will be placed in *anewid when UV_CreateVolume3 returns.
704  * @param abkid  same as aroid, except for the BK volume id instead of the
705  *               RO volume id.
706  * @return 0 on success, error code otherwise.
707  */
708 int
709 UV_CreateVolume3(afs_uint32 aserver, afs_int32 apart, char *aname,
710                  afs_int32 aquota, afs_int32 aspare1, afs_int32 aspare2,
711                  afs_int32 aspare3, afs_int32 aspare4, afs_uint32 * anewid,
712                  afs_uint32 * aroid, afs_uint32 * abkid)
713 {
714     struct rx_connection *aconn;
715     afs_int32 tid;
716     afs_int32 code;
717     afs_int32 error;
718     afs_int32 rcode, vcode;
719     afs_int32 lastid;
720     struct nvldbentry entry, storeEntry;        /*the new vldb entry */
721     struct volintInfo tstatus;
722
723     tid = 0;
724     error = 0;
725
726     init_volintInfo(&tstatus);
727     tstatus.maxquota = aquota;
728
729     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
730
731     if (aroid && *aroid) {
732         VPRINT1("Using RO volume ID %d.\n", *aroid);
733     }
734     if (abkid && *abkid) {
735         VPRINT1("Using BK volume ID %d.\n", *abkid);
736     }
737
738     if (*anewid) {
739         vcode = VLDB_GetEntryByID(*anewid, -1, &entry);
740         if (!vcode) {
741             fprintf(STDERR, "Volume ID %d already exists\n", *anewid);
742             return VVOLEXISTS;
743         }
744         VPRINT1("Using volume ID %d.\n", *anewid);
745     } else {
746         vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, anewid);
747         EGOTO1(cfail, vcode, "Could not get an Id for volume %s\n", aname);
748
749         if (aroid && *aroid == 0) {
750             vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, aroid);
751             EGOTO1(cfail, vcode, "Could not get an RO Id for volume %s\n", aname);
752         }
753
754         if (abkid && *abkid == 0) {
755             vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, abkid);
756             EGOTO1(cfail, vcode, "Could not get a BK Id for volume %s\n", aname);
757         }
758     }
759
760     /* rw,ro, bk id are related in the default case */
761     /* If caller specified RW id, but not RO/BK ids, have them be RW+1 and RW+2 */
762     lastid = *anewid;
763     if (aroid && *aroid != 0) {
764         lastid = max(lastid, *aroid);
765     }
766     if (abkid && *abkid != 0) {
767         lastid = max(lastid, *abkid);
768     }
769     if (aroid && *aroid == 0) {
770         *aroid = ++lastid;
771     }
772     if (abkid && *abkid == 0) {
773         *abkid = ++lastid;
774     }
775
776     code =
777         AFSVolCreateVolume_retry(aconn, apart, aname, volser_RW, 0, anewid, &tid);
778     EGOTO2(cfail, code, "Failed to create the volume %s %u \n", aname,
779            *anewid);
780
781     code = AFSVolSetInfo(aconn, tid, &tstatus);
782     if (code)
783         EPRINT(code, "Could not change quota, continuing...\n");
784
785     code = AFSVolSetFlags(aconn, tid, 0);       /* bring it online (mark it InService */
786     EGOTO2(cfail, code, "Could not bring the volume %s %u online \n", aname,
787            *anewid);
788
789     VPRINT2("Volume %s %u created and brought online\n", aname, *anewid);
790
791     /* set up the vldb entry for this volume */
792     strncpy(entry.name, aname, VOLSER_OLDMAXVOLNAME);
793     entry.nServers = 1;
794     entry.serverNumber[0] = aserver;    /* this should have another
795                                          * level of indirection later */
796     entry.serverPartition[0] = apart;   /* this should also have
797                                          * another indirection level */
798     entry.flags = RW_EXISTS;    /* this records that rw volume exists */
799     entry.serverFlags[0] = ITSRWVOL;    /*this rep site has rw  vol */
800     entry.volumeId[RWVOL] = *anewid;
801     entry.volumeId[ROVOL] = aroid ? *aroid : 0;
802     entry.volumeId[BACKVOL] = abkid ? *abkid : 0;
803     entry.cloneId = 0;
804     /*map into right byte order, before passing to xdr, the stuff has to be in host
805      * byte order. Xdr converts it into network order */
806     MapNetworkToHost(&entry, &storeEntry);
807     /* create the vldb entry */
808     vcode = VLDB_CreateEntry(&storeEntry);
809     if (vcode) {
810         fprintf(STDERR,
811                 "Could not create a VLDB entry for the volume %s %lu\n",
812                 aname, (unsigned long)*anewid);
813         /*destroy the created volume */
814         VPRINT1("Deleting the newly created volume %u\n", *anewid);
815         AFSVolDeleteVolume(aconn, tid);
816         error = vcode;
817         goto cfail;
818     }
819     VPRINT2("Created the VLDB entry for the volume %s %u\n", aname, *anewid);
820     /* volume created, now terminate the transaction and release the connection */
821     code = AFSVolEndTrans(aconn, tid, &rcode);  /*if it crashes before this
822                                                  * the volume will come online anyway when transaction timesout , so if
823                                                  * vldb entry exists then the volume is guaranteed to exist too wrt create */
824     tid = 0;
825     if (code) {
826         fprintf(STDERR,
827                 "Failed to end the transaction on the volume %s %lu\n", aname,
828                 (unsigned long)*anewid);
829         error = code;
830         goto cfail;
831     }
832
833   cfail:
834     if (tid) {
835         code = AFSVolEndTrans(aconn, tid, &rcode);
836         if (code)
837             fprintf(STDERR, "WARNING: could not end transaction\n");
838     }
839     if (aconn)
840         rx_DestroyConnection(aconn);
841     PrintError("", error);
842     return error;
843 }
844
845 /* create a volume, given a server, partition number, volume name --> sends
846 * back new vol id in <anewid>*/
847 int
848 UV_AddVLDBEntry(afs_uint32 aserver, afs_int32 apart, char *aname,
849                 afs_uint32 aid)
850 {
851     struct rx_connection *aconn;
852     afs_int32 error;
853     afs_int32 vcode;
854     struct nvldbentry entry, storeEntry;        /*the new vldb entry */
855
856     aconn = (struct rx_connection *)0;
857     error = 0;
858
859     /* set up the vldb entry for this volume */
860     strncpy(entry.name, aname, VOLSER_OLDMAXVOLNAME);
861     entry.nServers = 1;
862     entry.serverNumber[0] = aserver;    /* this should have another
863                                          * level of indirection later */
864     entry.serverPartition[0] = apart;   /* this should also have
865                                          * another indirection level */
866     entry.flags = RW_EXISTS;    /* this records that rw volume exists */
867     entry.serverFlags[0] = ITSRWVOL;    /*this rep site has rw  vol */
868     entry.volumeId[RWVOL] = aid;
869 #ifdef notdef
870     entry.volumeId[ROVOL] = anewid + 1; /* rw,ro, bk id are related in the default case */
871     entry.volumeId[BACKVOL] = *anewid + 2;
872 #else
873     entry.volumeId[ROVOL] = 0;
874     entry.volumeId[BACKVOL] = 0;
875 #endif
876     entry.cloneId = 0;
877     /*map into right byte order, before passing to xdr, the stuff has to be in host
878      * byte order. Xdr converts it into network order */
879     MapNetworkToHost(&entry, &storeEntry);
880     /* create the vldb entry */
881     vcode = VLDB_CreateEntry(&storeEntry);
882     if (vcode) {
883         fprintf(STDERR,
884                 "Could not create a VLDB entry for the  volume %s %lu\n",
885                 aname, (unsigned long)aid);
886         error = vcode;
887         goto cfail;
888     }
889     VPRINT2("Created the VLDB entry for the volume %s %u\n", aname, aid);
890
891   cfail:
892     if (aconn)
893         rx_DestroyConnection(aconn);
894     PrintError("", error);
895     return error;
896 }
897
898 /* Delete the volume <volid>on <aserver> <apart>
899  * the physical entry gets removed from the vldb only if the ref count
900  * becomes zero
901  */
902 int
903 UV_DeleteVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid)
904 {
905     struct rx_connection *aconn = (struct rx_connection *)0;
906     afs_int32 ttid = 0;
907     afs_int32 code, rcode;
908     afs_int32 error = 0;
909     struct nvldbentry entry, storeEntry;
910     int islocked = 0;
911     afs_int32 avoltype = -1, vtype;
912     int notondisk = 0, notinvldb = 0;
913
914     /* Find and read bhe VLDB entry for this volume */
915     code = ubik_VL_SetLock(cstruct, 0, avolid, avoltype, VLOP_DELETE);
916     if (code) {
917         if (code != VL_NOENT) {
918             EGOTO1(error_exit, code,
919                    "Could not lock VLDB entry for the volume %u\n", avolid);
920         }
921         notinvldb = 1;
922     } else {
923         islocked = 1;
924
925         code = VLDB_GetEntryByID(avolid, avoltype, &entry);
926         EGOTO1(error_exit, code, "Could not fetch VLDB entry for volume %u\n",
927                avolid);
928         MapHostToNetwork(&entry);
929
930         if (verbose)
931             EnumerateEntry(&entry);
932     }
933
934     /* Whether volume is in the VLDB or not. Delete the volume on disk */
935     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
936
937     code = DoVolDelete(aconn, avolid, apart, "the", 0, NULL, NULL);
938     if (code) {
939         if (code == VNOVOL)
940             notondisk = 1;
941         else {
942             error = code;
943             goto error_exit;
944         }
945     }
946
947     /* Now update the VLDB entry.
948      * But first, verify we have a VLDB entry.
949      * Whether volume is on disk or not. Delete the volume in VLDB.
950      */
951     if (notinvldb)
952         ERROR_EXIT(0);
953
954     if (avolid == entry.volumeId[BACKVOL]) {
955         /* Its a backup volume, modify the VLDB entry. Check that the
956          * backup volume is on the server/partition we asked to delete.
957          */
958         if (!(entry.flags & BACK_EXISTS) || !Lp_Match(aserver, apart, &entry)) {
959             notinvldb = 2;      /* Not on this server and partition */
960             ERROR_EXIT(0);
961         }
962
963         VPRINT1("Marking the backup volume %u deleted in the VLDB\n", avolid);
964
965         entry.flags &= ~BACK_EXISTS;
966         vtype = BACKVOL;
967     }
968
969     else if (avolid == entry.volumeId[ROVOL]) {
970         /* Its a read-only volume, modify the VLDB entry. Check that the
971          * readonly volume is on the server/partition we asked to delete.
972          * If flags does not have RO_EIXSTS set, then this may mean the RO
973          * hasn't been released (and could exist in VLDB).
974          */
975         if (!Lp_ROMatch(aserver, apart, &entry)) {
976             notinvldb = 2;      /* Not found on this server and partition */
977             ERROR_EXIT(0);
978         }
979
980         if (verbose)
981             fprintf(STDOUT,
982                     "Marking the readonly volume %lu deleted in the VLDB\n",
983                     (unsigned long)avolid);
984
985         Lp_SetROValue(&entry, aserver, apart, 0, 0);    /* delete the site */
986         entry.nServers--;
987         if (!Lp_ROMatch(0, 0, &entry))
988             entry.flags &= ~RO_EXISTS;  /* This was the last ro volume */
989         vtype = ROVOL;
990     }
991
992     else if (avolid == entry.volumeId[RWVOL]) {
993         /* It's a rw volume, delete the backup volume, modify the VLDB entry.
994          * Check that the readwrite volumes is on the server/partition we
995          * asked to delete.
996          */
997         if (!(entry.flags & RW_EXISTS) || !Lp_Match(aserver, apart, &entry)) {
998             notinvldb = 2;      /* Not found on this server and partition */
999             ERROR_EXIT(0);
1000         }
1001
1002         if (entry.volumeId[BACKVOL]) {
1003             /* Delete backup if it exists */
1004             code = DoVolDelete(aconn, entry.volumeId[BACKVOL], apart,
1005                                "the backup", 0, NULL, NULL);
1006             if (code && code != VNOVOL) {
1007                 error = code;
1008                 goto error_exit;
1009             }
1010         }
1011
1012         if (verbose)
1013             fprintf(STDOUT,
1014                     "Marking the readwrite volume %lu%s deleted in the VLDB\n",
1015                     (unsigned long)avolid,
1016                     ((entry.
1017                       flags & BACK_EXISTS) ? ", and its backup volume," :
1018                      ""));
1019
1020         Lp_SetRWValue(&entry, aserver, apart, 0L, 0L);
1021         entry.nServers--;
1022         entry.flags &= ~(BACK_EXISTS | RW_EXISTS);
1023         vtype = RWVOL;
1024
1025         if (entry.flags & RO_EXISTS)
1026             fprintf(STDERR, "WARNING: ReadOnly copy(s) may still exist\n");
1027     }
1028
1029     else {
1030         notinvldb = 2;          /* Not found on this server and partition */
1031         ERROR_EXIT(0);
1032     }
1033
1034     /* Either delete or replace the VLDB entry */
1035     if ((entry.nServers <= 0) || !(entry.flags & (RO_EXISTS | RW_EXISTS))) {
1036         if (verbose)
1037             fprintf(STDOUT,
1038                     "Last reference to the VLDB entry for %lu - deleting entry\n",
1039                     (unsigned long)avolid);
1040         code = ubik_VL_DeleteEntry(cstruct, 0, avolid, vtype);
1041         EGOTO1(error_exit, code,
1042                "Could not delete the VLDB entry for the volume %u \n",
1043                avolid);
1044     } else {
1045         MapNetworkToHost(&entry, &storeEntry);
1046         code =
1047             VLDB_ReplaceEntry(avolid, vtype, &storeEntry,
1048                               (LOCKREL_OPCODE | LOCKREL_AFSID |
1049                                LOCKREL_TIMESTAMP));
1050         EGOTO1(error_exit, code,
1051                "Could not update the VLDB entry for the volume %u \n",
1052                avolid);
1053     }
1054     islocked = 0;
1055
1056   error_exit:
1057     if (error)
1058         EPRINT(error, "\n");
1059
1060     if (notondisk && notinvldb) {
1061         EPRINT2(VOLSERNOVOL, "Volume %u does not exist %s\n", avolid,
1062                 ((notinvldb == 2) ? "on server and partition" : ""));
1063         if (!error)
1064             error = VOLSERNOVOL;
1065     } else if (notondisk) {
1066         fprintf(STDERR,
1067                 "WARNING: Volume %lu did not exist on the partition\n",
1068                 (unsigned long)avolid);
1069     } else if (notinvldb) {
1070         fprintf(STDERR, "WARNING: Volume %lu does not exist in VLDB %s\n",
1071                 (unsigned long)avolid,
1072                 ((notinvldb == 2) ? "on server and partition" : ""));
1073     }
1074
1075     if (ttid) {
1076         code = AFSVolEndTrans(aconn, ttid, &rcode);
1077         code = (code ? code : rcode);
1078         if (code) {
1079             fprintf(STDERR, "Could not end transaction on the volume %lu\n",
1080                     (unsigned long)avolid);
1081             PrintError("", code);
1082             if (!error)
1083                 error = code;
1084         }
1085     }
1086
1087     if (islocked) {
1088         code =
1089             ubik_VL_ReleaseLock(cstruct, 0, avolid, -1,
1090                                 (LOCKREL_OPCODE | LOCKREL_AFSID |
1091                                  LOCKREL_TIMESTAMP));
1092         if (code) {
1093             EPRINT1(code,
1094                     "Could not release the lock on the VLDB entry for the volume %u \n",
1095                     avolid);
1096             if (!error)
1097                 error = code;
1098         }
1099     }
1100
1101     if (aconn)
1102         rx_DestroyConnection(aconn);
1103     return error;
1104 }
1105
1106 /* add recovery to UV_MoveVolume */
1107
1108 #define TESTC   0               /* set to test recovery code, clear for production */
1109
1110 jmp_buf env;
1111 int interrupt = 0;
1112
1113 static void *
1114 do_interrupt(void * unused)
1115 {
1116     if (interrupt) {
1117 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
1118         /* Avoid UNIX LWP from getting confused that our stack has suddenly
1119          * changed. This will avoid some sanity checks, but until a better way
1120          * is found, the only alternative is always crashing and burning on at
1121          * least the stack-overflow check. */
1122         lwp_cpptr->stack = NULL;
1123 #endif
1124         longjmp(env, 0);
1125     }
1126
1127     fprintf(STDOUT, "\nSIGINT handler: vos move operation in progress\n");
1128     fprintf(STDOUT,
1129             "WARNING: may leave AFS storage and metadata in indeterminate state\n");
1130     fprintf(STDOUT, "enter second control-c to exit\n");
1131     fflush(STDOUT);
1132
1133     interrupt = 1;
1134     return NULL;
1135 }
1136
1137 static void
1138 sigint_handler(int x)
1139 {
1140 #ifdef AFS_PTHREAD_ENV
1141     do_interrupt(NULL);
1142 #else
1143     IOMGR_SoftSig(do_interrupt, 0);
1144 #endif
1145     (void)signal(SIGINT, sigint_handler);
1146 }
1147
1148 static int
1149 DoVolDelete(struct rx_connection *aconn, afs_uint32 avolid,
1150             afs_int32 apart, char *ptypestring, afs_uint32 atoserver,
1151             struct volser_status *volstatus, char *pprefix)
1152 {
1153     afs_int32 ttid = 0, code, rcode, error = 0;
1154     char *prefix, *typestring;
1155     int beverbose = 0;
1156
1157     if (pprefix)
1158         prefix = pprefix;
1159     else
1160         prefix = "";
1161
1162     if (ptypestring) {
1163         typestring = ptypestring;
1164         beverbose = 1;
1165     } else
1166         typestring = "the";
1167
1168     if (beverbose)
1169         VPRINT3("%sDeleting %s volume %u ...", prefix, typestring, avolid);
1170
1171     code =
1172         AFSVolTransCreate_retry(aconn, avolid, apart, ITOffline, &ttid);
1173
1174     /* return early and quietly for VNOVOL; don't continue the attempt to delete. */
1175     if (code == VNOVOL) {
1176         error = code;
1177         goto dfail;
1178     }
1179
1180     EGOTO2(dfail, code, "%sFailed to start transaction on %u\n",
1181            prefix, avolid);
1182
1183     if (volstatus) {
1184         code = AFSVolGetStatus(aconn, ttid, volstatus);
1185         EGOTO2(dfail, code, "%sCould not get timestamp from volume %u\n",
1186                prefix, avolid);
1187     }
1188
1189     code =
1190         AFSVolSetFlags(aconn, ttid,
1191                        VTDeleteOnSalvage | VTOutOfService);
1192
1193     EGOTO2(dfail, code, "%sCould not set flags on volume %u \n",
1194            prefix, avolid);
1195
1196     if (atoserver) {
1197         VPRINT1("%sSetting volume forwarding pointer ...", prefix);
1198         AFSVolSetForwarding(aconn, ttid, atoserver);
1199         VDONE;
1200     }
1201
1202     code = AFSVolDeleteVolume(aconn, ttid);
1203     EGOTO2(dfail, code, "%sCould not delete volume %u\n", prefix, avolid);
1204
1205 dfail:
1206     if (ttid) {
1207         code = AFSVolEndTrans(aconn, ttid, &rcode);
1208         ttid = 0;
1209         if (!code)
1210             code = rcode;
1211         if (code) {
1212             fprintf(STDERR, "%sCould not end transaction on %s volume %lu \n",
1213                     prefix, typestring, (unsigned long)avolid);
1214             if (!error)
1215                 error = code;
1216         }
1217     }
1218
1219     if (beverbose && !error)
1220         VDONE;
1221     return error;
1222 }
1223
1224 static int
1225 DoVolClone(struct rx_connection *aconn, afs_uint32 avolid,
1226            afs_int32 apart, int type, afs_uint32 cloneid,
1227            char *typestring, char *pname, char *vname, char *suffix,
1228            struct volser_status *volstatus, afs_int32 *transPtr)
1229 {
1230     char cname[64];
1231     afs_int32 ttid = 0, btid = 0;
1232     afs_int32 code = 0, rcode = 0;
1233     afs_int32 error = 0;
1234     int cloneexists = 1;
1235
1236     /* Test to see if the clone volume exists by trying to create
1237      * a transaction on the clone volume. We've assumed the clone exists.
1238      */
1239     code = AFSVolTransCreate_retry(aconn, cloneid, apart, ITOffline, &btid);
1240     if (code) {
1241         if (code != VNOVOL) {
1242             EPRINT2(code, "Could not reach the %s volume %lu\n",
1243                     typestring, (unsigned long)cloneid);
1244             error = code;
1245             goto cfail;
1246         }
1247         cloneexists = 0;         /* clone volume does not exist */
1248     }
1249     if (btid) {
1250         code = AFSVolEndTrans(aconn, btid, &rcode);
1251         btid = 0;
1252         if (code || rcode) {
1253             fprintf(STDERR,
1254                     "Could not end transaction on the previous %s volume %lu\n",
1255                     typestring, (unsigned long)cloneid);
1256             error = (code ? code : rcode);
1257             goto cfail;
1258         }
1259     }
1260
1261     /* Now go ahead and try to clone the RW volume.
1262      * First start a transaction on the RW volume
1263      */
1264     code = AFSVolTransCreate_retry(aconn, avolid, apart, ITBusy, &ttid);
1265     if (code) {
1266         fprintf(STDERR, "Could not start a transaction on the volume %lu\n",
1267                 (unsigned long)avolid);
1268         error = code;
1269         goto cfail;
1270     }
1271
1272     /* Clone or reclone the volume, depending on whether the clone
1273      * volume exists or not
1274      */
1275     if (cloneexists) {
1276         VPRINT2("Re-cloning %s volume %u ...", typestring, cloneid);
1277
1278         code = AFSVolReClone(aconn, ttid, cloneid);
1279         if (code) {
1280             EPRINT2(code, "Could not re-clone %s volume %lu\n",
1281                     typestring, (unsigned long)cloneid);
1282             error = code;
1283             goto cfail;
1284         }
1285     } else {
1286         VPRINT2("Creating a new %s clone %u ...", typestring, cloneid);
1287
1288         if (!vname) {
1289             strcpy(cname, pname);
1290             strcat(cname, suffix);
1291         }
1292
1293         code = AFSVolClone(aconn, ttid, 0, type, vname?vname:cname,
1294                            &cloneid);
1295         if (code) {
1296             fprintf(STDERR, "Failed to clone the volume %lu\n",
1297                     (unsigned long)avolid);
1298             error = code;
1299             goto cfail;
1300         }
1301     }
1302
1303     VDONE;
1304
1305     if (volstatus) {
1306         VPRINT1("Getting status of parent volume %u...", avolid);
1307         code = AFSVolGetStatus(aconn, ttid, volstatus);
1308         if (code) {
1309             fprintf(STDERR, "Failed to get the status of the parent volume %lu\n",
1310                     (unsigned long)avolid);
1311             error = code;
1312             goto cfail;
1313         }
1314         VDONE;
1315     }
1316
1317 cfail:
1318     if (ttid) {
1319         code = AFSVolEndTrans(aconn, ttid, &rcode);
1320         if (code || rcode) {
1321             fprintf(STDERR, "Could not end transaction on the volume %lu\n",
1322                     (unsigned long)avolid);
1323             if (!error)
1324                 error = (code ? code : rcode);
1325         }
1326     }
1327
1328     if (btid) {
1329         code = AFSVolEndTrans(aconn, btid, &rcode);
1330         if (code || rcode) {
1331             fprintf(STDERR,
1332                     "Could not end transaction on the %s volume %lu\n",
1333                     typestring, (unsigned long)cloneid);
1334             if (!error)
1335                 error = (code ? code : rcode);
1336         }
1337     }
1338     return error;
1339 }
1340
1341 /* Move volume <afromvol> on <afromserver> <afrompart> to <atoserver>
1342  * <atopart>.  The operation is almost idempotent.  The following
1343  * flags are recognized:
1344  *
1345  *     RV_NOCLONE - don't use a copy clone
1346  */
1347
1348 int
1349 UV_MoveVolume2(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
1350                afs_uint32 atoserver, afs_int32 atopart, int flags)
1351 {
1352     /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
1353      * be changing during the move */
1354     struct rx_connection * volatile toconn;
1355     struct rx_connection * volatile fromconn;
1356     afs_int32 volatile fromtid;
1357     afs_int32 volatile totid;
1358     afs_int32 volatile clonetid;
1359     afs_uint32 volatile newVol;
1360     afs_uint32 volatile volid;
1361     afs_uint32 volatile backupId;
1362     int volatile islocked;
1363     int volatile pntg;
1364
1365     char vname[64];
1366     char *volName = 0;
1367     char tmpName[VOLSER_MAXVOLNAME + 1];
1368     afs_int32 rcode;
1369     afs_int32 fromDate;
1370     afs_int32 tmp;
1371     afs_uint32 tmpVol;
1372     struct restoreCookie cookie;
1373     afs_int32 vcode, code;
1374     struct volser_status tstatus;
1375     struct destServer destination;
1376
1377     struct nvldbentry entry, storeEntry;
1378     int i;
1379     afs_int32 error;
1380     char in, lf;                /* for test code */
1381     int same;
1382     char hoststr[16];
1383
1384 #ifdef  ENABLE_BUGFIX_1165
1385     volEntries volumeInfo;
1386     struct volintInfo *infop = 0;
1387 #endif
1388
1389     islocked = 0;
1390     fromconn = (struct rx_connection *)0;
1391     toconn = (struct rx_connection *)0;
1392     fromtid = 0;
1393     totid = 0;
1394     clonetid = 0;
1395     error = 0;
1396     volid = 0;
1397     pntg = 0;
1398     backupId = 0;
1399     newVol = 0;
1400
1401     /* support control-c processing */
1402     if (setjmp(env))
1403         goto mfail;
1404     (void)signal(SIGINT, sigint_handler);
1405
1406     if (TESTC) {
1407         fprintf(STDOUT,
1408                 "\nThere are three tests points - verifies all code paths through recovery.\n");
1409         fprintf(STDOUT, "First test point - operation not started.\n");
1410         fprintf(STDOUT, "...test here (y, n)? ");
1411         fflush(STDOUT);
1412         fscanf(stdin, "%c", &in);
1413         fscanf(stdin, "%c", &lf);       /* toss away */
1414         if (in == 'y') {
1415             fprintf(STDOUT, "type control-c\n");
1416             while (1) {
1417                 fprintf(stdout, ".");
1418                 fflush(stdout);
1419                 sleep(1);
1420             }
1421         }
1422         /* or drop through */
1423     }
1424
1425     vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
1426     EGOTO1(mfail, vcode,
1427            "Could not fetch the entry for the volume  %u from the VLDB \n",
1428            afromvol);
1429
1430     if (entry.volumeId[RWVOL] != afromvol) {
1431         fprintf(STDERR, "Only RW volume can be moved\n");
1432         exit(1);
1433     }
1434
1435     vcode = ubik_VL_SetLock(cstruct, 0, afromvol, RWVOL, VLOP_MOVE);
1436     EGOTO1(mfail, vcode, "Could not lock entry for volume %u \n", afromvol);
1437     islocked = 1;
1438
1439     vcode = VLDB_GetEntryByID(afromvol, RWVOL, &entry);
1440     EGOTO1(mfail, vcode,
1441            "Could not fetch the entry for the volume  %u from the VLDB \n",
1442            afromvol);
1443
1444     backupId = entry.volumeId[BACKVOL];
1445     MapHostToNetwork(&entry);
1446
1447     if (!Lp_Match(afromserver, afrompart, &entry)) {
1448         /* the from server and partition do not exist in the vldb entry corresponding to volid */
1449         if (!Lp_Match(atoserver, atopart, &entry)) {
1450             /* the to server and partition do not exist in the vldb entry corresponding to volid */
1451             fprintf(STDERR, "The volume %lu is not on the specified site. \n",
1452                     (unsigned long)afromvol);
1453             fprintf(STDERR, "The current site is :");
1454             for (i = 0; i < entry.nServers; i++) {
1455                 if (entry.serverFlags[i] == ITSRWVOL) {
1456                     char pname[10];
1457                     MapPartIdIntoName(entry.serverPartition[i], pname);
1458                     fprintf(STDERR, " server %s partition %s \n",
1459                             noresolve ? afs_inet_ntoa_r(entry.serverNumber[i], hoststr) :
1460                             hostutil_GetNameByINet(entry.serverNumber[i]),
1461                             pname);
1462                 }
1463             }
1464             vcode =
1465                 ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
1466                           (LOCKREL_OPCODE | LOCKREL_AFSID |
1467                            LOCKREL_TIMESTAMP));
1468             EGOTO1(mfail, vcode,
1469                    " Could not release lock on the VLDB entry for the volume %u \n",
1470                    afromvol);
1471
1472             return VOLSERVOLMOVED;
1473         }
1474
1475         /* delete the volume afromvol on src_server */
1476         /* from-info does not exist but to-info does =>
1477          * we have already done the move, but the volume
1478          * may still be existing physically on from fileserver
1479          */
1480         fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
1481         pntg = 1;
1482
1483         code = DoVolDelete(fromconn, afromvol, afrompart,
1484                            "leftover", 0, NULL, NULL);
1485         if (code && code != VNOVOL) {
1486             error = code;
1487             goto mfail;
1488         }
1489
1490         code = DoVolDelete(fromconn, backupId, afrompart,
1491                            "leftover backup", 0, NULL, NULL);
1492         if (code && code != VNOVOL) {
1493             error = code;
1494             goto mfail;
1495         }
1496
1497         fromtid = 0;
1498         error = 0;
1499         goto mfail;
1500     }
1501
1502     /* From-info matches the vldb info about volid,
1503      * its ok start the move operation, the backup volume
1504      * on the old site is deleted in the process
1505      */
1506     if (afrompart == atopart) {
1507         same = VLDB_IsSameAddrs(afromserver, atoserver, &error);
1508         EGOTO2(mfail, error,
1509                "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
1510                afromserver, error);
1511
1512         if (same) {
1513             EGOTO1(mfail, VOLSERVOLMOVED,
1514                    "Warning: Moving volume %u to its home partition ignored!\n",
1515                    afromvol);
1516         }
1517     }
1518
1519     pntg = 1;
1520     toconn = UV_Bind(atoserver, AFSCONF_VOLUMEPORT);    /* get connections to the servers */
1521     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
1522     totid = 0;  /* initialize to uncreated */
1523
1524     /* ***
1525      * clone the read/write volume locally.
1526      * ***/
1527
1528     VPRINT1("Starting transaction on source volume %u ...", afromvol);
1529     code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
1530     fromtid = tmp;
1531     EGOTO1(mfail, code, "Failed to create transaction on the volume %u\n",
1532            afromvol);
1533     VDONE;
1534
1535     if (!(flags & RV_NOCLONE)) {
1536         /* Get a clone id */
1537         VPRINT1("Allocating new volume id for clone of volume %u ...",
1538                 afromvol);
1539         tmpVol = 0;
1540         vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
1541         newVol = tmpVol;
1542         EGOTO1(mfail, vcode,
1543                "Could not get an ID for the clone of volume %u from the VLDB\n",
1544                afromvol);
1545         VDONE;
1546
1547         /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
1548         VPRINT1("Cloning source volume %u ...", afromvol);
1549         strcpy(vname, "move-clone-temp");
1550         code =
1551             AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &tmpVol);
1552         newVol = tmpVol;
1553         EGOTO1(mfail, code, "Failed to clone the source volume %u\n",
1554                afromvol);
1555         VDONE;
1556     }
1557
1558     /* lookup the name of the volume we just cloned */
1559     volid = afromvol;
1560     code = AFSVolGetName(fromconn, fromtid, &volName);
1561     EGOTO1(mfail, code, "Failed to get the name of the volume %u\n",
1562            afromvol);
1563
1564     VPRINT1("Ending the transaction on the source volume %u ...", afromvol);
1565     rcode = 0;
1566     code = AFSVolEndTrans(fromconn, fromtid, &rcode);
1567     fromtid = 0;
1568     if (!code)
1569         code = rcode;
1570     EGOTO1(mfail, code,
1571            "Failed to end the transaction on the source volume %u\n",
1572            afromvol);
1573     VDONE;
1574
1575     /* ***
1576      * Create the destination volume
1577      * ***/
1578
1579     if (!(flags & RV_NOCLONE)) {
1580         /* All of this is to get the fromDate */
1581         VPRINT1("Starting transaction on the cloned volume %u ...", newVol);
1582         tmp = clonetid;
1583         code =
1584             AFSVolTransCreate_retry(fromconn, newVol, afrompart, ITOffline,
1585                               &tmp);
1586         clonetid = tmp;
1587         EGOTO1(mfail, code,
1588                "Failed to start a transaction on the cloned volume%u\n",
1589                newVol);
1590         VDONE;
1591
1592         VPRINT1("Setting flags on cloned volume %u ...", newVol);
1593         code =
1594             AFSVolSetFlags(fromconn, clonetid,
1595                            VTDeleteOnSalvage | VTOutOfService); /*redundant */
1596         EGOTO1(mfail, code, "Could not set flags on the cloned volume %u\n",
1597                newVol);
1598         VDONE;
1599
1600         /* remember time from which we've dumped the volume */
1601         VPRINT1("Getting status of cloned volume %u ...", newVol);
1602         code = AFSVolGetStatus(fromconn, clonetid, &tstatus);
1603         EGOTO1(mfail, code,
1604                "Failed to get the status of the cloned volume %u\n",
1605                newVol);
1606         VDONE;
1607
1608         fromDate = CLOCKADJ(tstatus.creationDate);
1609     } else {
1610         /* With RV_NOCLONE, just do a full copy from the source */
1611         fromDate = 0;
1612     }
1613
1614
1615 #ifdef  ENABLE_BUGFIX_1165
1616     /*
1617      * Get the internal volume state from the source volume. We'll use such info (i.e. dayUse)
1618      * to copy it to the new volume (via AFSSetInfo later on) so that when we move volumes we
1619      * don't use this information...
1620      */
1621     volumeInfo.volEntries_val = (volintInfo *) 0;       /*this hints the stub to allocate space */
1622     volumeInfo.volEntries_len = 0;
1623     code = AFSVolListOneVolume(fromconn, afrompart, afromvol, &volumeInfo);
1624     EGOTO1(mfail, code,
1625            "Failed to get the volint Info of the cloned volume %u\n",
1626            afromvol);
1627
1628     infop = (volintInfo *) volumeInfo.volEntries_val;
1629     infop->maxquota = -1;       /* Else it will replace the default quota */
1630     infop->creationDate = -1;   /* Else it will use the source creation date */
1631     infop->updateDate = -1;     /* Else it will use the source update date */
1632 #endif
1633
1634     /* create a volume on the target machine */
1635     volid = afromvol;
1636     code = DoVolDelete(toconn, volid, atopart,
1637                        "pre-existing destination", 0, NULL, NULL);
1638     if (code && code != VNOVOL) {
1639         error = code;
1640         goto mfail;
1641     }
1642
1643     VPRINT1("Creating the destination volume %u ...", volid);
1644     tmp = totid;
1645     tmpVol = volid;
1646     code =
1647         AFSVolCreateVolume(toconn, atopart, volName, volser_RW, volid, &tmpVol,
1648                            &tmp);
1649     totid = tmp;
1650     volid = tmpVol;
1651     EGOTO1(mfail, code, "Failed to create the destination volume %u\n",
1652            volid);
1653     VDONE;
1654
1655     strncpy(tmpName, volName, VOLSER_OLDMAXVOLNAME);
1656     free(volName);
1657     volName = NULL;
1658
1659     VPRINT1("Setting volume flags on destination volume %u ...", volid);
1660     code =
1661         AFSVolSetFlags(toconn, totid, (VTDeleteOnSalvage | VTOutOfService));
1662     EGOTO1(mfail, code,
1663            "Failed to set the flags on the destination volume %u\n", volid);
1664     VDONE;
1665
1666     /***
1667      * Now dump the clone to the new volume
1668      ***/
1669
1670     destination.destHost = ntohl(atoserver);
1671     destination.destPort = AFSCONF_VOLUMEPORT;
1672     destination.destSSID = 1;
1673
1674     strncpy(cookie.name, tmpName, VOLSER_OLDMAXVOLNAME);
1675     cookie.type = RWVOL;
1676     cookie.parent = entry.volumeId[RWVOL];
1677     cookie.clone = 0;
1678
1679     if (!(flags & RV_NOCLONE)) {
1680         /* Copy the clone to the new volume */
1681         VPRINT2("Dumping from clone %u on source to volume %u on destination ...",
1682                 newVol, afromvol);
1683         code =
1684             AFSVolForward(fromconn, clonetid, 0, &destination, totid,
1685                           &cookie);
1686         EGOTO1(mfail, code, "Failed to move data for the volume %u\n", volid);
1687         VDONE;
1688
1689         VPRINT1("Ending transaction on cloned volume %u ...", newVol);
1690         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
1691         if (!code)
1692             code = rcode;
1693         clonetid = 0;
1694         EGOTO1(mfail, code,
1695                "Failed to end the transaction on the cloned volume %u\n",
1696                newVol);
1697         VDONE;
1698     }
1699
1700     /* ***
1701      * reattach to the main-line volume, and incrementally dump it.
1702      * ***/
1703
1704     VPRINT1("Starting transaction on source volume %u ...", afromvol);
1705     tmp = fromtid;
1706     code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
1707     fromtid = tmp;
1708     EGOTO1(mfail, code,
1709            "Failed to create a transaction on the source volume %u\n",
1710            afromvol);
1711     VDONE;
1712
1713     /* now do the incremental */
1714     VPRINT2
1715         ("Doing the%s dump from source to destination for volume %u ... ",
1716          (flags & RV_NOCLONE) ? "" : " incremental",
1717          afromvol);
1718     code =
1719         AFSVolForward(fromconn, fromtid, fromDate, &destination, totid,
1720                       &cookie);
1721     EGOTO1(mfail, code,
1722            "Failed to do the%s dump from rw volume on old site to rw volume on newsite\n",
1723           (flags & RV_NOCLONE) ? "" : " incremental");
1724     VDONE;
1725
1726     /* now adjust the flags so that the new volume becomes official */
1727     VPRINT1("Setting volume flags on old source volume %u ...", afromvol);
1728     code = AFSVolSetFlags(fromconn, fromtid, VTOutOfService);
1729     EGOTO(mfail, code,
1730           "Failed to set the flags to make old source volume offline\n");
1731     VDONE;
1732
1733     VPRINT1("Setting volume flags on new source volume %u ...", afromvol);
1734     code = AFSVolSetFlags(toconn, totid, 0);
1735     EGOTO(mfail, code,
1736           "Failed to set the flags to make new source volume online\n");
1737     VDONE;
1738
1739 #ifdef  ENABLE_BUGFIX_1165
1740     VPRINT1("Setting volume status on destination volume %u ...", volid);
1741     code = AFSVolSetInfo(toconn, totid, infop);
1742     EGOTO1(mfail, code,
1743            "Failed to set volume status on the destination volume %u\n",
1744            volid);
1745     VDONE;
1746 #endif
1747
1748     /* put new volume online */
1749     VPRINT1("Ending transaction on destination volume %u ...", afromvol);
1750     code = AFSVolEndTrans(toconn, totid, &rcode);
1751     totid = 0;
1752     if (!code)
1753         code = rcode;
1754     EGOTO1(mfail, code,
1755            "Failed to end the transaction on the volume %u on the new site\n",
1756            afromvol);
1757     VDONE;
1758
1759     Lp_SetRWValue(&entry, afromserver, afrompart, atoserver, atopart);
1760     MapNetworkToHost(&entry, &storeEntry);
1761     storeEntry.flags &= ~BACK_EXISTS;
1762
1763     if (TESTC) {
1764         fprintf(STDOUT,
1765                 "Second test point - operation in progress but not complete.\n");
1766         fprintf(STDOUT, "...test here (y, n)? ");
1767         fflush(STDOUT);
1768         fscanf(stdin, "%c", &in);
1769         fscanf(stdin, "%c", &lf);       /* toss away */
1770         if (in == 'y') {
1771             fprintf(STDOUT, "type control-c\n");
1772             while (1) {
1773                 fprintf(stdout, ".");
1774                 fflush(stdout);
1775                 sleep(1);
1776             }
1777         }
1778         /* or drop through */
1779     }
1780
1781     VPRINT1("Releasing lock on VLDB entry for volume %u ...", afromvol);
1782     vcode =
1783         VLDB_ReplaceEntry(afromvol, -1, &storeEntry,
1784                           (LOCKREL_OPCODE | LOCKREL_AFSID |
1785                            LOCKREL_TIMESTAMP));
1786     if (vcode) {
1787         fprintf(STDERR,
1788                 " Could not release the lock on the VLDB entry for the volume %s %lu \n",
1789                 storeEntry.name, (unsigned long)afromvol);
1790         error = vcode;
1791         goto mfail;
1792     }
1793     islocked = 0;
1794     VDONE;
1795
1796     if (TESTC) {
1797         fprintf(STDOUT,
1798                 "Third test point - operation complete but no cleanup.\n");
1799         fprintf(STDOUT, "...test here (y, n)? ");
1800         fflush(STDOUT);
1801         fscanf(stdin, "%c", &in);
1802         fscanf(stdin, "%c", &lf);       /* toss away */
1803         if (in == 'y') {
1804             fprintf(STDOUT, "type control-c\n");
1805             while (1) {
1806                 fprintf(stdout, ".");
1807                 fflush(stdout);
1808                 sleep(1);
1809             }
1810         }
1811         /* or drop through */
1812     }
1813 #ifdef notdef
1814     /* This is tricky.  File server is very stupid, and if you mark the volume
1815      * as VTOutOfService, it may mark the *good* instance (if you're moving
1816      * between partitions on the same machine) as out of service.  Since
1817      * we're cleaning this code up in DEcorum, we're just going to kludge around
1818      * it for now by removing this call. */
1819     /* already out of service, just zap it now */
1820     code =
1821         AFSVolSetFlags(fromconn, fromtid, VTDeleteOnSalvage | VTOutOfService);
1822     if (code) {
1823         fprintf(STDERR,
1824                 "Failed to set the flags to make the old source volume offline\n");
1825         goto mfail;
1826     }
1827 #endif
1828     if (atoserver != afromserver) {
1829         /* set forwarding pointer for moved volumes */
1830         VPRINT1("Setting forwarding pointer for volume %u ...", afromvol);
1831         code = AFSVolSetForwarding(fromconn, fromtid, atoserver);
1832         EGOTO1(mfail, code,
1833                "Failed to set the forwarding pointer for the volume %u\n",
1834                afromvol);
1835         VDONE;
1836     }
1837
1838     VPRINT1("Deleting old volume %u on source ...", afromvol);
1839     code = AFSVolDeleteVolume(fromconn, fromtid);       /* zap original volume */
1840     EGOTO1(mfail, code, "Failed to delete the old volume %u on source\n",
1841            afromvol);
1842     VDONE;
1843
1844     VPRINT1("Ending transaction on old volume %u on the source ...",
1845             afromvol);
1846     code = AFSVolEndTrans(fromconn, fromtid, &rcode);
1847     fromtid = 0;
1848     if (!code)
1849         code = rcode;
1850     EGOTO1(mfail, code,
1851            "Failed to end the transaction on the old volume %u on the source\n",
1852            afromvol);
1853     VDONE;
1854
1855     code = DoVolDelete(fromconn, backupId, afrompart,
1856                        "source backup", 0, NULL, NULL);
1857     if (code && code != VNOVOL) {
1858         error = code;
1859         goto mfail;
1860     }
1861
1862     code = 0;           /* no backup volume? that's okay */
1863
1864     fromtid = 0;
1865     if (!(flags & RV_NOCLONE)) {
1866         code = DoVolDelete(fromconn, newVol, afrompart,
1867                            "cloned", 0, NULL, NULL);
1868         if (code) {
1869             if (code == VNOVOL) {
1870                 EPRINT1(code, "Failed to start transaction on %u\n", newVol);
1871             }
1872             error = code;
1873             goto mfail;
1874         }
1875     }
1876
1877     /* fall through */
1878     /* END OF MOVE */
1879
1880     if (TESTC) {
1881         fprintf(STDOUT, "Fourth test point - operation complete.\n");
1882         fprintf(STDOUT, "...test here (y, n)? ");
1883         fflush(STDOUT);
1884         fscanf(stdin, "%c", &in);
1885         fscanf(stdin, "%c", &lf);       /* toss away */
1886         if (in == 'y') {
1887             fprintf(STDOUT, "type control-c\n");
1888             while (1) {
1889                 fprintf(stdout, ".");
1890                 fflush(stdout);
1891                 sleep(1);
1892             }
1893         }
1894         /* or drop through */
1895     }
1896
1897     /* normal cleanup code */
1898
1899     if (entry.flags & RO_EXISTS)
1900         fprintf(STDERR, "WARNING : readOnly copies still exist \n");
1901
1902     if (islocked) {
1903         VPRINT1("Cleanup: Releasing VLDB lock on volume %u ...", afromvol);
1904         vcode =
1905             ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
1906                       (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
1907         if (vcode) {
1908             VPRINT("\n");
1909             fprintf(STDERR,
1910                     " Could not release the lock on the VLDB entry for the volume %lu \n",
1911                     (unsigned long)afromvol);
1912             if (!error)
1913                 error = vcode;
1914         }
1915         VDONE;
1916     }
1917
1918     if (fromtid) {
1919         VPRINT1("Cleanup: Ending transaction on source volume %u ...",
1920                 afromvol);
1921         code = AFSVolEndTrans(fromconn, fromtid, &rcode);
1922         if (code || rcode) {
1923             VPRINT("\n");
1924             fprintf(STDERR,
1925                     "Could not end transaction on the source volume %lu\n",
1926                     (unsigned long)afromvol);
1927             if (!error)
1928                 error = (code ? code : rcode);
1929         }
1930         VDONE;
1931     }
1932
1933     if (clonetid) {
1934         VPRINT1("Cleanup: Ending transaction on clone volume %u ...", newVol);
1935         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
1936         if (code || rcode) {
1937             VPRINT("\n");
1938             fprintf(STDERR,
1939                     "Could not end transaction on the source's clone volume %lu\n",
1940                     (unsigned long)newVol);
1941             if (!error)
1942                 error = (code ? code : rcode);
1943         }
1944         VDONE;
1945     }
1946
1947     if (totid) {
1948         VPRINT1("Cleanup: Ending transaction on destination volume %u ...",
1949                 afromvol);
1950         code = AFSVolEndTrans(toconn, totid, &rcode);
1951         if (code) {
1952             VPRINT("\n");
1953             fprintf(STDERR,
1954                     "Could not end transaction on destination volume %lu\n",
1955                     (unsigned long)afromvol);
1956             if (!error)
1957                 error = (code ? code : rcode);
1958         }
1959         VDONE;
1960     }
1961     if (volName)
1962         free(volName);
1963 #ifdef  ENABLE_BUGFIX_1165
1964     if (infop)
1965         free(infop);
1966 #endif
1967     if (fromconn)
1968         rx_DestroyConnection(fromconn);
1969     if (toconn)
1970         rx_DestroyConnection(toconn);
1971     PrintError("", error);
1972     return error;
1973
1974     /* come here only when the sky falls */
1975   mfail:
1976
1977     if (pntg) {
1978         fprintf(STDOUT,
1979                 "vos move: operation interrupted, cleanup in progress...\n");
1980         fprintf(STDOUT, "clear transaction contexts\n");
1981         fflush(STDOUT);
1982     }
1983
1984     /* unlock VLDB entry */
1985     if (islocked) {
1986         VPRINT1("Recovery: Releasing VLDB lock on volume %u ...", afromvol);
1987         ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
1988                   (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
1989         VDONE;
1990         islocked = 0;
1991     }
1992
1993     if (clonetid) {
1994         VPRINT("Recovery: Ending transaction on clone volume ...");
1995         AFSVolEndTrans(fromconn, clonetid, &rcode);
1996         VDONE;
1997     }
1998     if (totid) {
1999         VPRINT("Recovery: Ending transaction on destination volume ...");
2000         AFSVolEndTrans(toconn, totid, &rcode);
2001         VDONE;
2002     }
2003     if (fromtid) {              /* put it on-line */
2004         VPRINT("Recovery: Setting volume flags on source volume ...");
2005         AFSVolSetFlags(fromconn, fromtid, 0);
2006         VDONE;
2007
2008         VPRINT("Recovery: Ending transaction on source volume ...");
2009         AFSVolEndTrans(fromconn, fromtid, &rcode);
2010         VDONE;
2011     }
2012
2013     VPRINT("Recovery: Accessing VLDB.\n");
2014     vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
2015     if (vcode) {
2016         fprintf(STDOUT, "FATAL: VLDB access error: abort cleanup\n");
2017         fflush(STDOUT);
2018         goto done;
2019     }
2020     MapHostToNetwork(&entry);
2021
2022     /* Delete either the volume on the source location or the target location.
2023      * If the vldb entry still points to the source location, then we know the
2024      * volume move didn't finish so we remove the volume from the target
2025      * location. Otherwise, we remove the volume from the source location.
2026      */
2027     if (Lp_Match(afromserver, afrompart, &entry)) {     /* didn't move - delete target volume */
2028         if (pntg) {
2029             fprintf(STDOUT,
2030                     "move incomplete - attempt cleanup of target partition - no guarantee\n");
2031             fflush(STDOUT);
2032         }
2033
2034         if (volid && toconn) {
2035             code = DoVolDelete(toconn, volid, atopart,
2036                                "destination", 0, NULL, "Recovery:");
2037             if (code == VNOVOL) {
2038                 EPRINT1(code, "Recovery: Failed to start transaction on %u\n", volid);
2039             }
2040         }
2041
2042         /* put source volume on-line */
2043         if (fromconn) {
2044             VPRINT1("Recovery: Creating transaction on source volume %u ...",
2045                     afromvol);
2046             tmp = fromtid;
2047             code =
2048                 AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy,
2049                                   &tmp);
2050             fromtid = tmp;
2051             if (!code) {
2052                 VDONE;
2053
2054                 VPRINT1("Recovery: Setting flags on source volume %u ...",
2055                         afromvol);
2056                 AFSVolSetFlags(fromconn, fromtid, 0);
2057                 VDONE;
2058
2059                 VPRINT1
2060                     ("Recovery: Ending transaction on source volume %u ...",
2061                      afromvol);
2062                 AFSVolEndTrans(fromconn, fromtid, &rcode);
2063                 VDONE;
2064             } else {
2065                 VPRINT1
2066                     ("\nRecovery: Unable to start transaction on source volume %u.\n",
2067                      afromvol);
2068             }
2069         }
2070     } else {                    /* yep, move complete */
2071         if (pntg) {
2072             fprintf(STDOUT,
2073                     "move complete - attempt cleanup of source partition - no guarantee\n");
2074             fflush(STDOUT);
2075         }
2076
2077         /* delete backup volume */
2078         if (fromconn) {
2079             code = DoVolDelete(fromconn, backupId, afrompart,
2080                                "backup", 0, NULL, "Recovery:");
2081             if (code == VNOVOL) {
2082                 EPRINT1(code, "Recovery: Failed to start transaction on %u\n", backupId);
2083             }
2084
2085             code = DoVolDelete(fromconn, afromvol, afrompart, "source",
2086                                (atoserver != afromserver)?atoserver:0,
2087                         NULL, NULL);
2088             if (code == VNOVOL) {
2089                 EPRINT1(code, "Failed to start transaction on %u\n", afromvol);
2090             }
2091         }
2092     }
2093
2094     /* common cleanup - delete local clone */
2095     if (newVol) {
2096         code = DoVolDelete(fromconn, newVol, afrompart,
2097                            "clone", 0, NULL, "Recovery:");
2098         if (code == VNOVOL) {
2099             EPRINT1(code, "Recovery: Failed to start transaction on %u\n", newVol);
2100         }
2101     }
2102
2103     /* unlock VLDB entry */
2104     if (islocked) {
2105         VPRINT1("Recovery: Releasing lock on VLDB entry for volume %u ...",
2106                 afromvol);
2107         ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
2108                             (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
2109         VDONE;
2110     }
2111   done:                 /* routine cleanup */
2112     if (volName)
2113         free(volName);
2114 #ifdef  ENABLE_BUGFIX_1165
2115     if (infop)
2116         free(infop);
2117 #endif
2118     if (fromconn)
2119         rx_DestroyConnection(fromconn);
2120     if (toconn)
2121         rx_DestroyConnection(toconn);
2122
2123     if (pntg) {
2124         fprintf(STDOUT, "cleanup complete - user verify desired result\n");
2125         fflush(STDOUT);
2126     }
2127     exit(1);
2128 }
2129
2130
2131 int
2132 UV_MoveVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
2133               afs_uint32 atoserver, afs_int32 atopart)
2134 {
2135     return UV_MoveVolume2(afromvol, afromserver, afrompart,
2136                           atoserver, atopart, 0);
2137 }
2138
2139
2140 /* Copy volume <afromvol> from <afromserver> <afrompart> to <atoserver>
2141  * <atopart>.  The new volume is named by <atovolname>.  The new volume
2142  * has ID <atovolid> if that is nonzero; otherwise a new ID is allocated
2143  * from the VLDB.  the following flags are supported:
2144  *
2145  *     RV_RDONLY  - target volume is RO
2146  *     RV_OFFLINE - leave target volume offline
2147  *     RV_CPINCR  - do incremental dump if target exists
2148  *     RV_NOVLDB  - don't create/update VLDB entry
2149  *     RV_NOCLONE - don't use a copy clone
2150  */
2151 int
2152 UV_CopyVolume2(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
2153                char *atovolname, afs_uint32 atoserver, afs_int32 atopart,
2154                afs_uint32 atovolid, int flags)
2155 {
2156     /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
2157      * be changing during the copy */
2158     int volatile pntg;
2159     afs_int32 volatile clonetid;
2160     afs_int32 volatile totid;
2161     afs_int32 volatile fromtid;
2162     struct rx_connection * volatile fromconn;
2163     struct rx_connection * volatile toconn;
2164     afs_uint32 volatile cloneVol;
2165
2166     char vname[64];
2167     afs_int32 rcode;
2168     afs_int32 fromDate, cloneFromDate;
2169     struct restoreCookie cookie;
2170     afs_int32 vcode, code;
2171     afs_uint32 newVol;
2172     afs_int32 volflag;
2173     struct volser_status tstatus;
2174     struct destServer destination;
2175     struct nvldbentry entry, newentry, storeEntry;
2176     afs_int32 error;
2177     afs_int32 tmp;
2178     afs_uint32 tmpVol;
2179
2180     fromconn = (struct rx_connection *)0;
2181     toconn = (struct rx_connection *)0;
2182     fromtid = 0;
2183     totid = 0;
2184     clonetid = 0;
2185     error = 0;
2186     pntg = 0;
2187     newVol = 0;
2188
2189     /* support control-c processing */
2190     if (setjmp(env))
2191         goto mfail;
2192     (void)signal(SIGINT, sigint_handler);
2193
2194     vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
2195     EGOTO1(mfail, vcode,
2196            "Could not fetch the entry for the volume  %u from the VLDB \n",
2197            afromvol);
2198     MapHostToNetwork(&entry);
2199
2200     pntg = 1;
2201     toconn = UV_Bind(atoserver, AFSCONF_VOLUMEPORT);    /* get connections to the servers */
2202     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
2203     fromtid = totid = 0;        /* initialize to uncreated */
2204
2205     /* ***
2206      * clone the read/write volume locally.
2207      * ***/
2208
2209     cloneVol = 0;
2210     if (!(flags & RV_NOCLONE)) {
2211         VPRINT1("Starting transaction on source volume %u ...", afromvol);
2212         tmp = fromtid;
2213         code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy,
2214                                  &tmp);
2215         fromtid = tmp;
2216         EGOTO1(mfail, code, "Failed to create transaction on the volume %u\n",
2217                afromvol);
2218         VDONE;
2219
2220         /* Get a clone id */
2221         VPRINT1("Allocating new volume id for clone of volume %u ...",
2222                 afromvol);
2223         cloneVol = 0;
2224         tmpVol = cloneVol;
2225         vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
2226         cloneVol = tmpVol;
2227         EGOTO1(mfail, vcode,
2228            "Could not get an ID for the clone of volume %u from the VLDB\n",
2229            afromvol);
2230         VDONE;
2231     }
2232
2233     if (atovolid) {
2234         newVol = atovolid;
2235     } else {
2236         /* Get a new volume id */
2237         VPRINT1("Allocating new volume id for copy of volume %u ...", afromvol);
2238         newVol = 0;
2239         vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &newVol);
2240         EGOTO1(mfail, vcode,
2241                "Could not get an ID for the copy of volume %u from the VLDB\n",
2242                afromvol);
2243         VDONE;
2244     }
2245
2246     if (!(flags & RV_NOCLONE)) {
2247         /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
2248         VPRINT1("Cloning source volume %u ...", afromvol);
2249         strcpy(vname, "copy-clone-temp");
2250         tmpVol = cloneVol;
2251         code =
2252             AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname,
2253                         &tmpVol);
2254         cloneVol = tmpVol;
2255         EGOTO1(mfail, code, "Failed to clone the source volume %u\n",
2256                afromvol);
2257         VDONE;
2258
2259         VPRINT1("Ending the transaction on the source volume %u ...", afromvol);
2260         rcode = 0;
2261         code = AFSVolEndTrans(fromconn, fromtid, &rcode);
2262         fromtid = 0;
2263         if (!code)
2264             code = rcode;
2265         EGOTO1(mfail, code,
2266                "Failed to end the transaction on the source volume %u\n",
2267                afromvol);
2268         VDONE;
2269     }
2270
2271     /* ***
2272      * Create the destination volume
2273      * ***/
2274
2275     if (!(flags & RV_NOCLONE)) {
2276         VPRINT1("Starting transaction on the cloned volume %u ...", cloneVol);
2277         tmp = clonetid;
2278         code =
2279             AFSVolTransCreate_retry(fromconn, cloneVol, afrompart, ITOffline,
2280                           &tmp);
2281         clonetid = tmp;
2282         EGOTO1(mfail, code,
2283                "Failed to start a transaction on the cloned volume%u\n",
2284                cloneVol);
2285         VDONE;
2286
2287         VPRINT1("Setting flags on cloned volume %u ...", cloneVol);
2288         code =
2289             AFSVolSetFlags(fromconn, clonetid,
2290                            VTDeleteOnSalvage | VTOutOfService); /*redundant */
2291         EGOTO1(mfail, code, "Could not set flags on the cloned volume %u\n",
2292                cloneVol);
2293         VDONE;
2294
2295         /* remember time from which we've dumped the volume */
2296         VPRINT1("Getting status of cloned volume %u ...", cloneVol);
2297         code = AFSVolGetStatus(fromconn, clonetid, &tstatus);
2298         EGOTO1(mfail, code,
2299                "Failed to get the status of the cloned volume %u\n",
2300                cloneVol);
2301         VDONE;
2302
2303         fromDate = CLOCKADJ(tstatus.creationDate);
2304     } else {
2305         fromDate = 0;
2306     }
2307
2308     /* create a volume on the target machine */
2309     cloneFromDate = 0;
2310     tmp = totid;
2311     code = AFSVolTransCreate_retry(toconn, newVol, atopart, ITOffline, &tmp);
2312     totid = tmp;
2313     if (!code) {
2314         if ((flags & RV_CPINCR)) {
2315             VPRINT1("Getting status of pre-existing volume %u ...", newVol);
2316             code = AFSVolGetStatus(toconn, totid, &tstatus);
2317             EGOTO1(mfail, code,
2318                    "Failed to get the status of the pre-existing volume %u\n",
2319                    newVol);
2320             VDONE;
2321
2322             /* Using the update date should be OK here, but add some fudge */
2323             cloneFromDate = CLOCKADJ(tstatus.updateDate);
2324             if ((flags & RV_NOCLONE))
2325                 fromDate = cloneFromDate;
2326
2327             /* XXX We should check that the source volume's creationDate is
2328              * XXX not newer than the existing target volume, and if not,
2329              * XXX throw away the existing target and do a full dump. */
2330
2331             goto cpincr;
2332         }
2333
2334         /* Delete the existing volume.
2335          * While we are deleting the volume in these steps, the transaction
2336          * we started against the cloned volume (clonetid above) will be
2337          * sitting idle. It will get cleaned up after 600 seconds
2338          */
2339         VPRINT1("Deleting pre-existing volume %u on destination ...", newVol);
2340         code = AFSVolDeleteVolume(toconn, totid);
2341         EGOTO1(mfail, code,
2342                "Could not delete the pre-existing volume %u on destination\n",
2343                newVol);
2344         VDONE;
2345
2346         VPRINT1
2347             ("Ending transaction on pre-existing volume %u on destination ...",
2348              newVol);
2349         code = AFSVolEndTrans(toconn, totid, &rcode);
2350         totid = 0;
2351         if (!code)
2352             code = rcode;
2353         EGOTO1(mfail, code,
2354                "Could not end the transaction on pre-existing volume %u on destination\n",
2355                newVol);
2356         VDONE;
2357     }
2358
2359     VPRINT1("Creating the destination volume %u ...", newVol);
2360     tmp = totid;
2361     code =
2362         AFSVolCreateVolume(toconn, atopart, atovolname,
2363                            (flags & RV_RDONLY) ? volser_RO : volser_RW,
2364                            newVol, &newVol, &tmp);
2365     totid = tmp;
2366     EGOTO1(mfail, code, "Failed to create the destination volume %u\n",
2367            newVol);
2368     VDONE;
2369
2370     VPRINT1("Setting volume flags on destination volume %u ...", newVol);
2371     code =
2372         AFSVolSetFlags(toconn, totid, (VTDeleteOnSalvage | VTOutOfService));
2373     EGOTO1(mfail, code,
2374            "Failed to set the flags on the destination volume %u\n", newVol);
2375     VDONE;
2376
2377 cpincr:
2378
2379     destination.destHost = ntohl(atoserver);
2380     destination.destPort = AFSCONF_VOLUMEPORT;
2381     destination.destSSID = 1;
2382
2383     strncpy(cookie.name, atovolname, VOLSER_OLDMAXVOLNAME);
2384     cookie.type = (flags & RV_RDONLY) ? ROVOL : RWVOL;
2385     cookie.parent = 0;
2386     cookie.clone = 0;
2387
2388     /***
2389      * Now dump the clone to the new volume
2390      ***/
2391
2392     if (!(flags & RV_NOCLONE)) {
2393         /* XXX probably should have some code here that checks to see if
2394          * XXX we are copying to same server and partition - if so, just
2395          * XXX use a clone to save disk space */
2396
2397         /* Copy the clone to the new volume */
2398         VPRINT2("Dumping from clone %u on source to volume %u on destination ...",
2399             cloneVol, newVol);
2400         code =
2401             AFSVolForward(fromconn, clonetid, cloneFromDate, &destination,
2402                           totid, &cookie);
2403         EGOTO1(mfail, code, "Failed to move data for the volume %u\n",
2404                newVol);
2405         VDONE;
2406
2407         VPRINT1("Ending transaction on cloned volume %u ...", cloneVol);
2408         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
2409         if (!code)
2410             code = rcode;
2411         clonetid = 0;
2412         EGOTO1(mfail, code,
2413                "Failed to end the transaction on the cloned volume %u\n",
2414                cloneVol);
2415         VDONE;
2416     }
2417
2418     /* ***
2419      * reattach to the main-line volume, and incrementally dump it.
2420      * ***/
2421
2422     VPRINT1("Starting transaction on source volume %u ...", afromvol);
2423     tmp = fromtid;
2424     code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
2425     fromtid = tmp;
2426     EGOTO1(mfail, code,
2427            "Failed to create a transaction on the source volume %u\n",
2428            afromvol);
2429     VDONE;
2430
2431     /* now do the incremental */
2432     VPRINT2
2433         ("Doing the%s dump from source to destination for volume %u ... ",
2434          (flags & RV_NOCLONE) ? "" : " incremental",
2435          afromvol);
2436     code =
2437         AFSVolForward(fromconn, fromtid, fromDate, &destination, totid,
2438                       &cookie);
2439     EGOTO1(mfail, code,
2440            "Failed to do the%s dump from old site to new site\n",
2441            (flags & RV_NOCLONE) ? "" : " incremental");
2442     VDONE;
2443
2444     VPRINT1("Setting volume flags on destination volume %u ...", newVol);
2445     volflag = ((flags & RV_OFFLINE) ? VTOutOfService : 0);      /* off or on-line */
2446     code = AFSVolSetFlags(toconn, totid, volflag);
2447     EGOTO(mfail, code,
2448           "Failed to set the flags to make destination volume online\n");
2449     VDONE;
2450
2451     /* put new volume online */
2452     VPRINT1("Ending transaction on destination volume %u ...", newVol);
2453     code = AFSVolEndTrans(toconn, totid, &rcode);
2454     totid = 0;
2455     if (!code)
2456         code = rcode;
2457     EGOTO1(mfail, code,
2458            "Failed to end the transaction on the destination volume %u\n",
2459            newVol);
2460     VDONE;
2461
2462     VPRINT1("Ending transaction on source volume %u ...", afromvol);
2463     code = AFSVolEndTrans(fromconn, fromtid, &rcode);
2464     fromtid = 0;
2465     if (!code)
2466         code = rcode;
2467     EGOTO1(mfail, code,
2468            "Failed to end the transaction on the source volume %u\n",
2469            afromvol);
2470     VDONE;
2471
2472     fromtid = 0;
2473
2474     if (!(flags & RV_NOCLONE)) {
2475         code = DoVolDelete(fromconn, cloneVol, afrompart,
2476                            "cloned", 0, NULL, NULL);
2477         if (code) {
2478             if (code == VNOVOL) {
2479                 EPRINT1(code, "Failed to start transaction on %u\n", cloneVol);
2480             }
2481             error = code;
2482             goto mfail;
2483         }
2484     }
2485
2486     if (!(flags & RV_NOVLDB)) {
2487         /* create the vldb entry for the copied volume */
2488         strncpy(newentry.name, atovolname, VOLSER_OLDMAXVOLNAME);
2489         newentry.nServers = 1;
2490         newentry.serverNumber[0] = atoserver;
2491         newentry.serverPartition[0] = atopart;
2492         newentry.flags = (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
2493         newentry.serverFlags[0] = (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
2494         newentry.volumeId[RWVOL] = newVol;
2495         newentry.volumeId[ROVOL] = (flags & RV_RDONLY) ? newVol : 0;
2496         newentry.volumeId[BACKVOL] = 0;
2497         newentry.cloneId = 0;
2498         /*map into right byte order, before passing to xdr, the stuff has to be in host
2499          * byte order. Xdr converts it into network order */
2500         MapNetworkToHost(&newentry, &storeEntry);
2501         /* create the vldb entry */
2502         vcode = VLDB_CreateEntry(&storeEntry);
2503         if (vcode) {
2504             fprintf(STDERR,
2505                     "Could not create a VLDB entry for the volume %s %lu\n",
2506                     atovolname, (unsigned long)newVol);
2507             /*destroy the created volume */
2508             VPRINT1("Deleting the newly created volume %u\n", newVol);
2509             AFSVolDeleteVolume(toconn, totid);
2510             error = vcode;
2511             goto mfail;
2512         }
2513         VPRINT2("Created the VLDB entry for the volume %s %u\n", atovolname,
2514                 newVol);
2515     }
2516
2517     /* normal cleanup code */
2518
2519     if (fromtid) {
2520         VPRINT1("Cleanup: Ending transaction on source volume %u ...",
2521                 afromvol);
2522         code = AFSVolEndTrans(fromconn, fromtid, &rcode);
2523         if (code || rcode) {
2524             VPRINT("\n");
2525             fprintf(STDERR,
2526                     "Could not end transaction on the source volume %lu\n",
2527                     (unsigned long)afromvol);
2528             if (!error)
2529                 error = (code ? code : rcode);
2530         }
2531         VDONE;
2532     }
2533
2534     if (clonetid) {
2535         VPRINT1("Cleanup: Ending transaction on clone volume %u ...",
2536                 cloneVol);
2537         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
2538         if (code || rcode) {
2539             VPRINT("\n");
2540             fprintf(STDERR,
2541                     "Could not end transaction on the source's clone volume %lu\n",
2542                     (unsigned long)cloneVol);
2543             if (!error)
2544                 error = (code ? code : rcode);
2545         }
2546         VDONE;
2547     }
2548
2549     if (totid) {
2550         VPRINT1("Cleanup: Ending transaction on destination volume %u ...",
2551                 newVol);
2552         code = AFSVolEndTrans(toconn, totid, &rcode);
2553         if (code) {
2554             VPRINT("\n");
2555             fprintf(STDERR,
2556                     "Could not end transaction on destination volume %lu\n",
2557                     (unsigned long)newVol);
2558             if (!error)
2559                 error = (code ? code : rcode);
2560         }
2561         VDONE;
2562     }
2563     if (fromconn)
2564         rx_DestroyConnection(fromconn);
2565     if (toconn)
2566         rx_DestroyConnection(toconn);
2567     PrintError("", error);
2568     return error;
2569
2570     /* come here only when the sky falls */
2571   mfail:
2572
2573     if (pntg) {
2574         fprintf(STDOUT,
2575                 "vos copy: operation interrupted, cleanup in progress...\n");
2576         fprintf(STDOUT, "clear transaction contexts\n");
2577         fflush(STDOUT);
2578     }
2579
2580     if (clonetid) {
2581         VPRINT("Recovery: Ending transaction on clone volume ...");
2582         AFSVolEndTrans(fromconn, clonetid, &rcode);
2583         VDONE;
2584     }
2585     if (totid) {
2586         VPRINT("Recovery: Ending transaction on destination volume ...");
2587         AFSVolEndTrans(toconn, totid, &rcode);
2588         VDONE;
2589     }
2590     if (fromtid) {              /* put it on-line */
2591         VPRINT("Recovery: Ending transaction on source volume ...");
2592         AFSVolEndTrans(fromconn, fromtid, &rcode);
2593         VDONE;
2594     }
2595
2596     VPRINT("Recovery: Accessing VLDB.\n");
2597     vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
2598     if (vcode) {
2599         fprintf(STDOUT, "FATAL: VLDB access error: abort cleanup\n");
2600         fflush(STDOUT);
2601         goto done;
2602     }
2603     MapHostToNetwork(&entry);
2604
2605     /* common cleanup - delete local clone */
2606     if (cloneVol) {
2607         code = DoVolDelete(fromconn, cloneVol, afrompart,
2608                            "clone", 0, NULL, "Recovery:");
2609         if (code == VNOVOL) {
2610             EPRINT1(code, "Recovery: Failed to start transaction on %u\n", cloneVol);
2611         }
2612     }
2613
2614   done:                 /* routine cleanup */
2615     if (fromconn)
2616         rx_DestroyConnection(fromconn);
2617     if (toconn)
2618         rx_DestroyConnection(toconn);
2619
2620     if (pntg) {
2621         fprintf(STDOUT, "cleanup complete - user verify desired result\n");
2622         fflush(STDOUT);
2623     }
2624     exit(1);
2625 }
2626
2627
2628 int
2629 UV_CopyVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
2630               char *atovolname, afs_uint32 atoserver, afs_int32 atopart)
2631 {
2632     return UV_CopyVolume2(afromvol, afromserver, afrompart,
2633                           atovolname, atoserver, atopart, 0, 0);
2634 }
2635
2636
2637
2638 /* Make a new backup of volume <avolid> on <aserver> and <apart>
2639  * if one already exists, update it
2640  */
2641
2642 int
2643 UV_BackupVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid)
2644 {
2645     struct rx_connection *aconn = (struct rx_connection *)0;
2646     afs_int32 ttid = 0, btid = 0;
2647     afs_uint32 backupID;
2648     afs_int32 code = 0, rcode = 0;
2649     struct nvldbentry entry, storeEntry;
2650     afs_int32 error = 0;
2651     int vldblocked = 0, vldbmod = 0;
2652
2653     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
2654
2655     /* the calls to VLDB will succeed only if avolid is a RW volume,
2656      * since we are following the RW hash chain for searching */
2657     code = VLDB_GetEntryByID(avolid, RWVOL, &entry);
2658     if (code) {
2659         fprintf(STDERR,
2660                 "Could not fetch the entry for the volume %lu from the VLDB \n",
2661                 (unsigned long)avolid);
2662         error = code;
2663         goto bfail;
2664     }
2665     MapHostToNetwork(&entry);
2666
2667     /* These operations require the VLDB be locked since it means the VLDB
2668      * will change or the vldb is already locked.
2669      */
2670     if (!(entry.flags & BACK_EXISTS) || /* backup volume doesnt exist */
2671         (entry.flags & VLOP_ALLOPERS) ||        /* vldb lock already held */
2672         (entry.volumeId[BACKVOL] == INVALID_BID)) {     /* no assigned backup volume id */
2673
2674         code = ubik_VL_SetLock(cstruct, 0, avolid, RWVOL, VLOP_BACKUP);
2675         if (code) {
2676             fprintf(STDERR,
2677                     "Could not lock the VLDB entry for the volume %lu\n",
2678                     (unsigned long)avolid);
2679             error = code;
2680             goto bfail;
2681         }
2682         vldblocked = 1;
2683
2684         /* Reread the vldb entry */
2685         code = VLDB_GetEntryByID(avolid, RWVOL, &entry);
2686         if (code) {
2687             fprintf(STDERR,
2688                     "Could not fetch the entry for the volume %lu from the VLDB \n",
2689                     (unsigned long)avolid);
2690             error = code;
2691             goto bfail;
2692         }
2693         MapHostToNetwork(&entry);
2694     }
2695
2696     if (!ISNAMEVALID(entry.name)) {
2697         fprintf(STDERR, "Name of the volume %s exceeds the size limit\n",
2698                 entry.name);
2699         error = VOLSERBADNAME;
2700         goto bfail;
2701     }
2702
2703     backupID = entry.volumeId[BACKVOL];
2704     if (backupID == INVALID_BID) {
2705         /* Get a backup volume id from the VLDB and update the vldb
2706          * entry with it.
2707          */
2708         code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &backupID);
2709         if (code) {
2710             fprintf(STDERR,
2711                     "Could not allocate ID for the backup volume of  %lu from the VLDB\n",
2712                     (unsigned long)avolid);
2713             error = code;
2714             goto bfail;
2715         }
2716         entry.volumeId[BACKVOL] = backupID;
2717         vldbmod = 1;
2718     }
2719
2720     code = DoVolClone(aconn, avolid, apart, backupVolume, backupID, "backup",
2721                       entry.name, NULL, ".backup", NULL, NULL);
2722     if (code) {
2723         error = code;
2724         goto bfail;
2725     }
2726
2727     /* Mark vldb as backup exists */
2728     if (!(entry.flags & BACK_EXISTS)) {
2729         entry.flags |= BACK_EXISTS;
2730         vldbmod = 1;
2731     }
2732
2733     /* Now go back to the backup volume and bring it on line */
2734     code = AFSVolTransCreate_retry(aconn, backupID, apart, ITOffline, &btid);
2735     if (code) {
2736         fprintf(STDERR,
2737                 "Failed to start a transaction on the backup volume %lu\n",
2738                 (unsigned long)backupID);
2739         error = code;
2740         goto bfail;
2741     }
2742
2743     code = AFSVolSetFlags(aconn, btid, 0);
2744     if (code) {
2745         fprintf(STDERR, "Could not mark the backup volume %lu on line \n",
2746                 (unsigned long)backupID);
2747         error = code;
2748         goto bfail;
2749     }
2750
2751     code = AFSVolEndTrans(aconn, btid, &rcode);
2752     btid = 0;
2753     if (code || rcode) {
2754         fprintf(STDERR,
2755                 "Failed to end the transaction on the backup volume %lu\n",
2756                 (unsigned long)backupID);
2757         error = (code ? code : rcode);
2758         goto bfail;
2759     }
2760
2761     VDONE;
2762
2763     /* Will update the vldb below */
2764
2765   bfail:
2766     if (ttid) {
2767         code = AFSVolEndTrans(aconn, ttid, &rcode);
2768         if (code || rcode) {
2769             fprintf(STDERR, "Could not end transaction on the volume %lu\n",
2770                     (unsigned long)avolid);
2771             if (!error)
2772                 error = (code ? code : rcode);
2773         }
2774     }
2775
2776     if (btid) {
2777         code = AFSVolEndTrans(aconn, btid, &rcode);
2778         if (code || rcode) {
2779             fprintf(STDERR,
2780                     "Could not end transaction the backup volume %lu\n",
2781                     (unsigned long)backupID);
2782             if (!error)
2783                 error = (code ? code : rcode);
2784         }
2785     }
2786
2787     /* Now update the vldb - if modified */
2788     if (vldblocked) {
2789         if (vldbmod) {
2790             MapNetworkToHost(&entry, &storeEntry);
2791             code =
2792                 VLDB_ReplaceEntry(avolid, RWVOL, &storeEntry,
2793                                   (LOCKREL_OPCODE | LOCKREL_AFSID |
2794                                    LOCKREL_TIMESTAMP));
2795             if (code) {
2796                 fprintf(STDERR,
2797                         "Could not update the VLDB entry for the volume %lu \n",
2798                         (unsigned long)avolid);
2799                 if (!error)
2800                     error = code;
2801             }
2802         } else {
2803             code =
2804                 ubik_VL_ReleaseLock(cstruct, 0, avolid, RWVOL,
2805                           (LOCKREL_OPCODE | LOCKREL_AFSID |
2806                            LOCKREL_TIMESTAMP));
2807             if (code) {
2808                 fprintf(STDERR,
2809                         "Could not unlock the VLDB entry for the volume %lu \n",
2810                         (unsigned long)avolid);
2811                 if (!error)
2812                     error = code;
2813             }
2814         }
2815     }
2816
2817     if (aconn)
2818         rx_DestroyConnection(aconn);
2819
2820     PrintError("", error);
2821     return error;
2822 }
2823
2824 /* Make a new clone of volume <avolid> on <aserver> and <apart>
2825  * using volume ID <acloneid>, or a new ID allocated from the VLDB.
2826  * The new volume is named by <aname>, or by appending ".clone" to
2827  * the existing name if <aname> is NULL.  The following flags are
2828  * supported:
2829  *
2830  *     RV_RDONLY  - target volume is RO
2831  *     RV_OFFLINE - leave target volume offline
2832  */
2833
2834 int
2835 UV_CloneVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid,
2836                afs_uint32 acloneid, char *aname, int flags)
2837 {
2838     struct rx_connection *aconn = (struct rx_connection *)0;
2839     afs_int32 ttid = 0, btid = 0;
2840     afs_int32 code = 0, rcode = 0;
2841     char vname[VOLSER_MAXVOLNAME + 1];
2842     afs_int32 error = 0;
2843     volEntries volumeInfo;
2844     int type = 0;
2845
2846     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
2847
2848     if (!aname) {
2849         volumeInfo.volEntries_val = (volintInfo *) 0;
2850         volumeInfo.volEntries_len = 0;
2851         code = AFSVolListOneVolume(aconn, apart, avolid, &volumeInfo);
2852         if (code) {
2853             fprintf(stderr, "Could not get info for volume %lu\n",
2854                     (unsigned long)avolid);
2855             error = code;
2856             goto bfail;
2857         }
2858         strncpy(vname, volumeInfo.volEntries_val[0].name,
2859                 VOLSER_OLDMAXVOLNAME - 7);
2860         vname[VOLSER_OLDMAXVOLNAME - 7] = 0;
2861         strcat(vname, ".clone");
2862         aname = vname;
2863         if (volumeInfo.volEntries_val)
2864             free(volumeInfo.volEntries_val);
2865     }
2866
2867     if (!acloneid) {
2868         /* Get a clone id */
2869         VPRINT1("Allocating new volume id for clone of volume %u ...",
2870                 avolid);
2871         code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &acloneid);
2872         EGOTO1(bfail, code,
2873            "Could not get an ID for the clone of volume %u from the VLDB\n",
2874            avolid);
2875         VDONE;
2876     }
2877
2878     if (flags & RV_RWONLY)
2879         type = readwriteVolume;
2880     else if (flags & RV_RDONLY)
2881         type = readonlyVolume;
2882     else
2883         type = backupVolume;
2884
2885     code = DoVolClone(aconn, avolid, apart, type, acloneid, "clone",
2886                       NULL, ".clone", NULL, NULL, NULL);
2887     if (code) {
2888         error = code;
2889         goto bfail;
2890     }
2891
2892     /* Now go back to the backup volume and bring it on line */
2893     if (!(flags & RV_OFFLINE)) {
2894         code = AFSVolTransCreate_retry(aconn, acloneid, apart, ITOffline, &btid);
2895         if (code) {
2896             fprintf(STDERR,
2897                     "Failed to start a transaction on the clone volume %lu\n",
2898                     (unsigned long)acloneid);
2899             error = code;
2900             goto bfail;
2901         }
2902
2903         code = AFSVolSetFlags(aconn, btid, 0);
2904         if (code) {
2905             fprintf(STDERR, "Could not mark the clone volume %lu on line \n",
2906                     (unsigned long)acloneid);
2907             error = code;
2908             goto bfail;
2909         }
2910
2911         code = AFSVolEndTrans(aconn, btid, &rcode);
2912         btid = 0;
2913         if (code || rcode) {
2914             fprintf(STDERR,
2915                     "Failed to end the transaction on the clone volume %lu\n",
2916                     (unsigned long)acloneid);
2917             error = (code ? code : rcode);
2918             goto bfail;
2919         }
2920     }
2921
2922     VDONE;
2923
2924   bfail:
2925     if (ttid) {
2926         code = AFSVolEndTrans(aconn, ttid, &rcode);
2927         if (code || rcode) {
2928             fprintf(STDERR, "Could not end transaction on the volume %lu\n",
2929                     (unsigned long)avolid);
2930             if (!error)
2931                 error = (code ? code : rcode);
2932         }
2933     }
2934
2935     if (btid) {
2936         code = AFSVolEndTrans(aconn, btid, &rcode);
2937         if (code || rcode) {
2938             fprintf(STDERR,
2939                     "Could not end transaction on the clone volume %lu\n",
2940                     (unsigned long)acloneid);
2941             if (!error)
2942                 error = (code ? code : rcode);
2943         }
2944     }
2945
2946     if (aconn)
2947         rx_DestroyConnection(aconn);
2948
2949     PrintError("", error);
2950     return error;
2951 }
2952
2953 #define ONERROR(ec, ep, es) do { \
2954     if (ec) { \
2955         fprintf(STDERR, (es), (ep)); \
2956         error = (ec); \
2957         goto rfail; \
2958     } \
2959 } while (0)
2960 #define ONERROR0(ec, es) do { \
2961     if (ec) { \
2962         fprintf(STDERR, (es)); \
2963         error = (ec); \
2964         goto rfail; \
2965     } \
2966 } while (0)
2967 #define ERROREXIT(ec) do { \
2968     error = (ec); \
2969     goto rfail; \
2970 } while (0)
2971
2972 /* Get a "transaction" on this replica.  Create the volume
2973  * if necessary.  Return the time from which a dump should
2974  * be made (0 if it's a new volume)
2975  */
2976 static int
2977 GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
2978          struct rx_connection **connPtr, afs_int32 * transPtr,
2979          afs_uint32 * crtimePtr, afs_uint32 * uptimePtr,
2980          afs_int32 *origflags, afs_uint32 tmpVolId)
2981 {
2982     afs_uint32 volid;
2983     struct volser_status tstatus;
2984     int code = 0;
2985     int rcode, tcode;
2986     char hoststr[16];
2987
2988     *connPtr = (struct rx_connection *)0;
2989     *transPtr = 0;
2990     *crtimePtr = 0;
2991     *uptimePtr = 0;
2992
2993     /* get connection to the replication site */
2994     *connPtr = UV_Bind(vldbEntryPtr->serverNumber[index], AFSCONF_VOLUMEPORT);
2995     if (!*connPtr)
2996         goto fail;              /* server is down */
2997
2998     volid = vldbEntryPtr->volumeId[ROVOL];
2999
3000     if (volid) {
3001         code =
3002             AFSVolTransCreate_retry(*connPtr, volid,
3003                               vldbEntryPtr->serverPartition[index], ITOffline,
3004                               transPtr);
3005
3006         if (!code && (origflags[index] & RO_DONTUSE)) {
3007             /* If RO_DONTUSE is set, this is supposed to be an entirely new
3008              * site. Don't trust any data on it, since it is possible we
3009              * have encountered some temporary volume from some other
3010              * incomplete volume operation. It is difficult to detect if
3011              * that has happened vs if this is a legit volume, so just
3012              * delete it to be safe. */
3013
3014             VPRINT1("Deleting extant RO_DONTUSE site on %s...",
3015                     noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
3016                                                 serverNumber[index], hoststr) :
3017                     hostutil_GetNameByINet(vldbEntryPtr->
3018                                            serverNumber[index]));
3019
3020             code = AFSVolDeleteVolume(*connPtr, *transPtr);
3021             if (code) {
3022                 PrintError("Failed to delete RO_DONTUSE site: ", code);
3023                 goto fail;
3024             }
3025
3026             tcode = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
3027             *transPtr = 0;
3028             if (!tcode) {
3029                 tcode = rcode;
3030             }
3031             if (tcode) {
3032                 PrintError("Failed to end transaction on RO_DONTUSE site: ",
3033                            tcode);
3034                 goto fail;
3035             }
3036
3037             VDONE;
3038
3039             /* emulate what TransCreate would have returned, so we try to
3040              * create the volume below */
3041             code = VNOVOL;
3042         }
3043     }
3044
3045     /* If the volume does not exist, create it */
3046     if (!volid || code) {
3047         char volname[VL_MAXNAMELEN];
3048         char hoststr[16];
3049
3050         if (volid && (code != VNOVOL)) {
3051             PrintError("Failed to start a transaction on the RO volume.\n",
3052                        code);
3053             goto fail;
3054         }
3055
3056         strlcpy(volname, vldbEntryPtr->name, sizeof(volname));
3057
3058         if (strlcat(volname,
3059                     tmpVolId?".roclone":".readonly",
3060                     sizeof(volname)) >= sizeof(volname)) {
3061             code = ENOMEM;
3062             PrintError("Volume name is too long\n", code);
3063             goto fail;
3064         }
3065
3066         if (tmpVolId)
3067             strcat(volname, ".roclone");
3068         else
3069             strcat(volname, ".readonly");
3070
3071         if (verbose) {
3072             fprintf(STDOUT,
3073                     "Creating new volume %lu on replication site %s: ",
3074                     tmpVolId?(unsigned long)tmpVolId:(unsigned long)volid,
3075                     noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
3076                                                 serverNumber[index], hoststr) :
3077                     hostutil_GetNameByINet(vldbEntryPtr->
3078                                            serverNumber[index]));
3079             fflush(STDOUT);
3080         }
3081
3082         code =
3083           AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
3084                              volname, volser_RO,
3085                              vldbEntryPtr->volumeId[RWVOL],
3086                              tmpVolId?&tmpVolId:&volid,
3087                              transPtr);
3088         if (code) {
3089             PrintError("Failed to create the ro volume: ", code);
3090             goto fail;
3091         }
3092         vldbEntryPtr->volumeId[ROVOL] = volid;
3093
3094         VDONE;
3095
3096         /* The following is a bit redundant, since create sets these flags by default */
3097         code =
3098             AFSVolSetFlags(*connPtr, *transPtr,
3099                            VTDeleteOnSalvage | VTOutOfService);
3100         if (code) {
3101             PrintError("Failed to set flags on the ro volume: ", code);
3102             goto fail;
3103         }
3104     }
3105
3106     /* Otherwise, the transaction did succeed, so get the creation date of the
3107      * latest RO volume on the replication site
3108      */
3109     else {
3110         VPRINT2("Updating existing ro volume %u on %s ...\n", volid,
3111                 noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
3112                                             serverNumber[index], hoststr) :
3113                 hostutil_GetNameByINet(vldbEntryPtr->serverNumber[index]));
3114
3115         code = AFSVolGetStatus(*connPtr, *transPtr, &tstatus);
3116         if (code) {
3117             PrintError("Failed to get status of volume on destination: ",
3118                        code);
3119             goto fail;
3120         }
3121         if (tmpVolId) {
3122             code = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
3123             *transPtr = 0;
3124             if (!code)
3125                 code = rcode;
3126             if (!code)
3127                 code = DoVolClone(*connPtr, volid,
3128                                   vldbEntryPtr->serverPartition[index],
3129                                   readonlyVolume, tmpVolId, "temporary",
3130                                   vldbEntryPtr->name, NULL, ".roclone", NULL,
3131                                   transPtr);
3132             if (code)
3133                 goto fail;
3134         }
3135         *crtimePtr = CLOCKADJ(tstatus.creationDate);
3136         *uptimePtr = CLOCKADJ(tstatus.updateDate);
3137     }
3138
3139     return 0;
3140
3141   fail:
3142     if (*transPtr) {
3143         tcode = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
3144         *transPtr = 0;
3145         if (!tcode)
3146             tcode = rcode;
3147         if (tcode && tcode != ENOENT)
3148             PrintError("Could not end transaction on a ro volume: ", tcode);
3149     }
3150
3151     return code;
3152 }
3153
3154 static int
3155 SimulateForwardMultiple(struct rx_connection *fromconn, afs_int32 fromtid,
3156                         afs_int32 fromdate, manyDests * tr, afs_int32 flags,
3157                         void *cookie, manyResults * results)
3158 {
3159     unsigned int i;
3160
3161     for (i = 0; i < tr->manyDests_len; i++) {
3162         results->manyResults_val[i] =
3163             AFSVolForward(fromconn, fromtid, fromdate,
3164                           &(tr->manyDests_val[i].server),
3165                           tr->manyDests_val[i].trans, cookie);
3166     }
3167     return 0;
3168 }
3169
3170 /**
3171  * Check if a trans has timed out, and recreate it if necessary.
3172  *
3173  * @param[in] aconn  RX connection to the relevant server
3174  * @param[inout] atid  Transaction ID to check; if we recreated the trans,
3175  *                     contains the new trans ID on success
3176  * @param[in] apart  Partition for the transaction
3177  * @param[in] astat  The status of the original transaction
3178  *
3179  * @return operation status
3180  *  @retval 0 existing transaction is still valid, or we managed to recreate
3181  *            the trans successfully
3182  *  @retval nonzero Fatal error; bail out
3183  */
3184 static int
3185 CheckTrans(struct rx_connection *aconn, afs_int32 *atid, afs_int32 apart,
3186            struct volser_status *astat)
3187 {
3188     struct volser_status new_status;
3189     afs_int32 code;
3190
3191     memset(&new_status, 0, sizeof(new_status));
3192     code = AFSVolGetStatus(aconn, *atid, &new_status);
3193     if (code) {
3194         if (code == ENOENT) {
3195             *atid = 0;
3196             VPRINT1("Old transaction on cloned volume %lu timed out, "
3197                     "restarting transaction\n", (long unsigned) astat->volID);
3198             code = AFSVolTransCreate_retry(aconn, astat->volID, apart,
3199                                            ITBusy, atid);
3200             if (code) {
3201                 PrintError("Failed to recreate cloned RO volume transaction\n",
3202                            code);
3203                 return 1;
3204             }
3205
3206             memset(&new_status, 0, sizeof(new_status));
3207             code = AFSVolGetStatus(aconn, *atid, &new_status);
3208             if (code) {
3209                 PrintError("Failed to get status on recreated transaction\n",
3210                            code);
3211                 return 1;
3212             }
3213
3214             if (memcmp(&new_status, astat, sizeof(new_status)) != 0) {
3215                 PrintError("Recreated transaction on cloned RO volume, but "
3216                            "the volume has changed!\n", 0);
3217                 return 1;
3218             }
3219         } else {
3220             PrintError("Unable to get status of current cloned RO transaction\n",
3221                        code);
3222             return 1;
3223         }
3224     } else {
3225         if (memcmp(&new_status, astat, sizeof(new_status)) != 0) {
3226             /* sanity check */
3227             PrintError("Internal error: current GetStatus does not match "
3228                        "original GetStatus?\n", 0);
3229             return 1;
3230         }
3231     }
3232
3233     return 0;
3234 }
3235
3236 static void
3237 PutTrans(afs_int32 *vldbindex, struct replica *replicas,
3238          struct rx_connection **toconns, struct release *times,
3239          afs_int32 volcount)
3240 {
3241     afs_int32 s, code = 0, rcode = 0;
3242     /* End the transactions and destroy the connections */
3243     for (s = 0; s < volcount; s++) {
3244         if (replicas[s].trans) {
3245             code = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
3246
3247             replicas[s].trans = 0;
3248             if (!code)
3249                 code = rcode;
3250             if (code) {
3251                 if ((s == 0) || (code != ENOENT)) {
3252                     PrintError("Could not end transaction on a ro volume: ",
3253                                code);
3254                 } else {
3255                     PrintError
3256                         ("Transaction timed out on a ro volume. Will retry.\n",
3257                          0);
3258                     if (times[s].vldbEntryIndex < *vldbindex)
3259                         *vldbindex = times[s].vldbEntryIndex;
3260                 }
3261             }
3262         }
3263         if (toconns[s])
3264             rx_DestroyConnection(toconns[s]);
3265         toconns[s] = 0;
3266     }
3267 }
3268
3269 static int
3270 DoVolOnline(struct nvldbentry *vldbEntryPtr, afs_uint32 avolid, int index,
3271             char *vname, struct rx_connection *connPtr)
3272 {
3273     afs_int32 code = 0, rcode = 0, onlinetid = 0;
3274
3275     code =
3276         AFSVolTransCreate_retry(connPtr, avolid,
3277                                 vldbEntryPtr->serverPartition[index],
3278                                 ITOffline,
3279                                 &onlinetid);
3280     if (code)
3281       EPRINT(code, "Could not create transaction on readonly...\n");
3282
3283     else {
3284         code = AFSVolSetFlags(connPtr, onlinetid, 0);
3285         if (code)
3286             EPRINT(code, "Could not set flags on readonly...\n");
3287     }
3288
3289     if (!code) {
3290         code =
3291             AFSVolSetIdsTypes(connPtr, onlinetid, vname,
3292                               ROVOL, vldbEntryPtr->volumeId[RWVOL],
3293                               0, 0);
3294         if (code)
3295             EPRINT(code, "Could not set ids on readonly...\n");
3296     }
3297     if (!code)
3298         code = AFSVolEndTrans(connPtr, onlinetid, &rcode);
3299     if (!code)
3300         code = rcode;
3301     return code;
3302 }
3303
3304 /* UV_ReleaseVolume()
3305  *    Release volume <afromvol> on <afromserver> <afrompart> to all
3306  *    its RO sites (full release). Unless the previous release was
3307  *    incomplete: in which case we bring the remaining incomplete
3308  *    volumes up to date with the volumes that were released
3309  *    successfully.
3310  *    forceflag: Performs a full release.
3311  *
3312  *    Will create a clone from the RW, then dump the clone out to
3313  *    the remaining replicas. If there is more than 1 RO sites,
3314  *    ensure that the VLDB says at least one RO is available all
3315  *    the time: Influences when we write back the VLDB entry.
3316  */
3317
3318 int
3319 UV_ReleaseVolume(afs_uint32 afromvol, afs_uint32 afromserver,
3320                  afs_int32 afrompart, int forceflag, int stayUp)
3321 {
3322     char vname[64];
3323     afs_int32 code = 0;
3324     afs_int32 vcode, rcode, tcode;
3325     afs_uint32 cloneVolId = 0, roVolId;
3326     struct replica *replicas = 0;
3327     struct nvldbentry entry, storeEntry;
3328     int i, volcount = 0, m, vldbindex;
3329     int failure;
3330     struct restoreCookie cookie;
3331     struct rx_connection **toconns = 0;
3332     struct release *times = 0;
3333     int nservers = 0;
3334     struct rx_connection *fromconn = (struct rx_connection *)0;
3335     afs_int32 error = 0;
3336     int islocked = 0;
3337     afs_int32 clonetid = 0, onlinetid;
3338     afs_int32 fromtid = 0;
3339     afs_uint32 fromdate = 0;
3340     afs_uint32 thisdate;
3341     time_t tmv;
3342     int s;
3343     manyDests tr;
3344     manyResults results;
3345     int rwindex, roindex, roclone, roexists;
3346     afs_uint32 rwcrdate = 0, rwupdate = 0;
3347     afs_uint32 clcrdate;
3348     struct rtime {
3349         int validtime;
3350         afs_uint32 uptime;
3351     } remembertime[NMAXNSERVERS];
3352     int releasecount = 0;
3353     struct volser_status volstatus;
3354     char hoststr[16];
3355     afs_int32 origflags[NMAXNSERVERS];
3356     struct volser_status orig_status;
3357     int notreleased = 0;
3358     int tried_justnewsites = 0;
3359     int justnewsites = 0; /* are we just trying to release to new RO sites? */
3360     int sites = 0; /* number of ro sites */
3361     int new_sites = 0; /* number of ro sites markes as new */
3362
3363     typedef enum {
3364         CR_RECOVER    = 0x0000, /**< not complete: a recovery from a previous failed release */
3365         CR_FORCED     = 0x0001, /**< complete: forced by caller */
3366         CR_LAST_OK    = 0x0002, /**< complete: no sites have been marked as new release */
3367         CR_ALL_NEW    = 0x0004, /**< complete: all sites have been marked as new release */
3368         CR_NEW_RW     = 0x0008, /**< complete: read-write has changed */
3369         CR_RO_MISSING = 0x0010, /**< complete: ro clone is missing */
3370     } complete_release_t;
3371
3372     complete_release_t complete_release = CR_RECOVER;
3373
3374     memset(remembertime, 0, sizeof(remembertime));
3375     memset(&results, 0, sizeof(results));
3376     memset(origflags, 0, sizeof(origflags));
3377
3378     vcode = ubik_VL_SetLock(cstruct, 0, afromvol, RWVOL, VLOP_RELEASE);
3379     if (vcode != VL_RERELEASE)
3380         ONERROR(vcode, afromvol,
3381                 "Could not lock the VLDB entry for the volume %u.\n");
3382     islocked = 1;
3383
3384     /* Get the vldb entry in readable format */
3385     vcode = VLDB_GetEntryByID(afromvol, RWVOL, &entry);
3386     ONERROR(vcode, afromvol,
3387             "Could not fetch the entry for the volume %u from the VLDB.\n");
3388     MapHostToNetwork(&entry);
3389
3390     if (verbose)
3391         EnumerateEntry(&entry);
3392
3393     if (!ISNAMEVALID(entry.name))
3394         ONERROR(VOLSERBADOP, entry.name,
3395                 "Volume name %s is too long, rename before releasing.\n");
3396     if (entry.volumeId[RWVOL] != afromvol)
3397         ONERROR(VOLSERBADOP, afromvol,
3398                 "The volume %u being released is not a read-write volume.\n");
3399     if (entry.nServers <= 1)
3400         ONERROR(VOLSERBADOP, afromvol,
3401                 "Volume %u has no replicas - release operation is meaningless!\n");
3402     if (strlen(entry.name) > (VOLSER_OLDMAXVOLNAME - 10))
3403         ONERROR(VOLSERBADOP, entry.name,
3404                 "RO volume name %s exceeds (VOLSER_OLDMAXVOLNAME - 10) character limit\n");
3405
3406     /* roclone is true if one of the RO volumes is on the same
3407      * partition as the RW volume. In this case, we make the RO volume
3408      * on the same partition a clone instead of a complete copy.
3409      */
3410
3411     roindex = Lp_ROMatch(afromserver, afrompart, &entry) - 1;
3412     roclone = ((roindex == -1) ? 0 : 1);
3413     rwindex = Lp_GetRwIndex(&entry);
3414     if (rwindex < 0)
3415         ONERROR0(VOLSERNOVOL, "There is no RW volume \n");
3416
3417     /* Make sure we have a RO volume id to work with */
3418     if (entry.volumeId[ROVOL] == INVALID_BID) {
3419         /* need to get a new RO volume id */
3420         vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &roVolId);
3421         ONERROR(vcode, entry.name, "Cant allocate ID for RO volume of %s\n");
3422
3423         entry.volumeId[ROVOL] = roVolId;
3424         MapNetworkToHost(&entry, &storeEntry);
3425         vcode = VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry, 0);
3426         ONERROR(vcode, entry.name, "Could not update vldb entry for %s.\n");
3427     }
3428
3429     /*
3430      * Determine if this is to be a complete release or a recovery of a
3431      * previous unfinished release. The previous release is considered to be
3432      * unfinished when the clone was successfully distributed to at least one
3433      * (but not all) of the read-only sites, as indicated by the NEW_REPSITE
3434      * vldb flags.
3435      *
3436      * The caller can override the vldb flags check using the -force
3437      * flag, to force this to be a complete release.
3438      */
3439     for (i = 0; i < entry.nServers; i++) {
3440         if (entry.serverFlags[i] & ITSROVOL) {
3441             sites++;
3442             if (entry.serverFlags[i] & NEW_REPSITE)
3443                 new_sites++;
3444             if (entry.serverFlags[i] & RO_DONTUSE)
3445                 notreleased++;
3446         }
3447         origflags[i] = entry.serverFlags[i];
3448     }
3449
3450     if (forceflag) {
3451         complete_release |= CR_FORCED;
3452     }
3453
3454     if (new_sites == 0) {
3455         complete_release |= CR_LAST_OK;
3456     } else if (new_sites == sites) {
3457         complete_release |= CR_ALL_NEW;
3458     }
3459
3460     if ((complete_release & (CR_LAST_OK | CR_ALL_NEW))
3461         && !(complete_release & CR_FORCED)) {
3462         if (notreleased && notreleased != sites) {
3463             /* we have some new unreleased sites. try to just release to those,
3464              * if the RW has not changed. The caller can override with -force. */
3465             justnewsites = 1;
3466         }
3467     }
3468
3469     /* Determine which volume id to use and see if it exists */
3470     cloneVolId = (complete_release || entry.cloneId == 0)
3471                   ? entry.volumeId[ROVOL] : entry.cloneId;
3472
3473     code = VolumeExists(afromserver, afrompart, cloneVolId);
3474     roexists = ((code == ENODEV) ? 0 : 1);
3475
3476     /* For stayUp case, if roclone is the only site, bypass special handling */
3477     if (stayUp && roclone) {
3478         int e;
3479         error = 0;
3480
3481         for (e = 0; (e < entry.nServers) && !error; e++) {
3482             if ((entry.serverFlags[e] & ITSROVOL)) {
3483                 if (!(VLDB_IsSameAddrs(entry.serverNumber[e], afromserver,
3484                                        &error)))
3485                     break;
3486             }
3487         }
3488         if (e >= entry.nServers)
3489             stayUp = 0;
3490     }
3491
3492     /* If we had a previous release to complete, do so, else: */
3493     if (stayUp && (cloneVolId == entry.volumeId[ROVOL])) {
3494         code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &cloneVolId);
3495         ONERROR(code, afromvol,
3496                 "Cannot get temporary clone id for volume %u\n");
3497     }
3498
3499     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
3500     if (!fromconn)
3501         ONERROR(-1, afromserver,
3502                 "Cannot establish connection with server 0x%x\n");
3503
3504     if (!complete_release) {
3505         if (!roexists) {
3506             complete_release |= CR_RO_MISSING;  /* Do a complete release if RO clone does not exist */
3507         } else {
3508             /* Begin transaction on RW and mark it busy while we query it */
3509             code = AFSVolTransCreate_retry(
3510                         fromconn, afromvol, afrompart, ITBusy, &fromtid
3511                    );
3512             ONERROR(code, afromvol,
3513                     "Failed to start transaction on RW volume %u\n");
3514
3515             /* Query the creation date for the RW */
3516             code = AFSVolGetStatus(fromconn, fromtid, &volstatus);
3517             ONERROR(code, afromvol,
3518                     "Failed to get the status of RW volume %u\n");
3519             rwcrdate = volstatus.creationDate;
3520
3521             /* End transaction on RW */
3522             code = AFSVolEndTrans(fromconn, fromtid, &rcode);
3523             fromtid = 0;
3524             ONERROR((code ? code : rcode), afromvol,
3525                     "Failed to end transaction on RW volume %u\n");
3526
3527             /* Begin transaction on clone and mark it busy while we query it */
3528             code = AFSVolTransCreate_retry(
3529                         fromconn, cloneVolId, afrompart, ITBusy, &clonetid
3530                    );
3531             ONERROR(code, cloneVolId,
3532                     "Failed to start transaction on RW clone %u\n");
3533
3534             /* Query the creation date for the clone */
3535             code = AFSVolGetStatus(fromconn, clonetid, &volstatus);
3536             ONERROR(code, cloneVolId,
3537                     "Failed to get the status of RW clone %u\n");
3538             clcrdate = volstatus.creationDate;
3539
3540             /* End transaction on clone */
3541             code = AFSVolEndTrans(fromconn, clonetid, &rcode);
3542             clonetid = 0;
3543             ONERROR((code ? code : rcode), cloneVolId,
3544                     "Failed to end transaction on RW clone %u\n");
3545
3546             if (rwcrdate > clcrdate)
3547                 complete_release |= CR_NEW_RW; /* Do a complete release if RO clone older than RW */
3548         }
3549     }
3550
3551     if (!complete_release || (complete_release & CR_NEW_RW)) {
3552         /* in case the RW has changed, and just to be safe */
3553         justnewsites = 0;
3554     }
3555
3556     if (verbose) {
3557         if (!complete_release) {
3558             fprintf(STDOUT,
3559                     "This is a recovery of previously failed release\n");
3560         } else {
3561             fprintf(STDOUT, "This is a complete release of volume %u", afromvol);
3562             /* Give the reasons for a complete release, except if only CR_LAST_OK. */
3563             if (complete_release != CR_LAST_OK) {
3564                 char *sep = " (";
3565                 if (complete_release & CR_FORCED) {
3566                     fprintf(STDOUT, "%sforced", sep);
3567                     sep = ", ";
3568                 }
3569                 if (complete_release & CR_LAST_OK) {
3570                     fprintf(STDOUT, "%slast ok", sep);
3571                     sep = ", ";
3572                 }
3573                 if (complete_release & CR_ALL_NEW) {
3574                     fprintf(STDOUT, "%sall sites are new", sep);
3575                     sep = ", ";
3576                 }
3577                 if (complete_release & CR_NEW_RW) {
3578                     fprintf(STDOUT, "%srw %u changed", sep, afromvol);
3579                     sep = ", ";
3580                 }
3581                 if (complete_release & CR_RO_MISSING) {
3582                     fprintf(STDOUT, "%sro clone missing", sep);
3583                 }
3584                 fprintf(STDOUT, ")");
3585             }
3586             fprintf(STDOUT, "\n");
3587             if (justnewsites) {
3588                 tried_justnewsites = 1;
3589                 fprintf(STDOUT, "There are new RO sites; we will try to "
3590                         "only release to new sites\n");
3591             }
3592         }
3593     }
3594
3595     if (complete_release) {
3596         afs_int32 oldest = 0;
3597         /* If the RO clone exists, then if the clone is a temporary
3598          * clone, delete it. Or if the RO clone is marked RO_DONTUSE
3599          * (it was recently added), then also delete it. We do not
3600          * want to "reclone" a temporary RO clone.
3601          */
3602         if (stayUp) {
3603             code = VolumeExists(afromserver, afrompart, cloneVolId);
3604             if (!code) {
3605                 code = DoVolDelete(fromconn, cloneVolId, afrompart, "previous clone", 0,
3606                                    NULL, NULL);
3607                 if (code && (code != VNOVOL))
3608                     ERROREXIT(code);
3609                 VDONE;
3610             }
3611         }
3612         /* clean up any previous tmp clone before starting if staying up */
3613         if (roexists
3614             && (!roclone || (entry.serverFlags[roindex] & RO_DONTUSE))) {
3615             code = DoVolDelete(fromconn,
3616                                stayUp ? entry.volumeId[ROVOL] : cloneVolId,
3617                                afrompart, "the", 0, NULL, NULL);
3618             if (code && (code != VNOVOL))
3619                 ERROREXIT(code);
3620             roexists = 0;
3621         }
3622
3623         if (justnewsites) {
3624             VPRINT("Querying old RO sites for update times...");
3625             for (vldbindex = 0; vldbindex < entry.nServers; vldbindex++) {
3626                 volEntries volumeInfo;
3627                 struct rx_connection *conn;
3628                 afs_int32 crdate;
3629
3630                 if (!(entry.serverFlags[vldbindex] & ITSROVOL)) {
3631                     continue;
3632                 }
3633                 if ((entry.serverFlags[vldbindex] & RO_DONTUSE)) {
3634                     continue;
3635                 }
3636                 conn = UV_Bind(entry.serverNumber[vldbindex], AFSCONF_VOLUMEPORT);
3637                 if (!conn) {
3638                     fprintf(STDERR, "Cannot establish connection to server %s\n",
3639                                     hostutil_GetNameByINet(entry.serverNumber[vldbindex]));
3640                     justnewsites = 0;
3641                     break;
3642                 }
3643                 volumeInfo.volEntries_val = NULL;
3644                 volumeInfo.volEntries_len = 0;
3645                 code = AFSVolListOneVolume(conn, entry.serverPartition[vldbindex],
3646                                            entry.volumeId[ROVOL],
3647                                            &volumeInfo);
3648                 if (code) {
3649                     fprintf(STDERR, "Could not fetch information about RO vol %lu from server %s\n",
3650                                     (unsigned long)entry.volumeId[ROVOL],
3651                                     hostutil_GetNameByINet(entry.serverNumber[vldbindex]));
3652                     PrintError("", code);
3653                     justnewsites = 0;
3654                     rx_DestroyConnection(conn);
3655                     break;
3656                 }
3657
3658                 crdate = CLOCKADJ(volumeInfo.volEntries_val[0].creationDate);
3659
3660                 if (oldest == 0 || crdate < oldest) {
3661                     oldest = crdate;
3662                 }
3663
3664                 rx_DestroyConnection(conn);
3665                 free(volumeInfo.volEntries_val);
3666                 volumeInfo.volEntries_val = NULL;
3667                 volumeInfo.volEntries_len = 0;
3668             }
3669             VDONE;
3670         }
3671         if (justnewsites) {
3672             volEntries volumeInfo;
3673             volumeInfo.volEntries_val = NULL;
3674             volumeInfo.volEntries_len = 0;
3675             code = AFSVolListOneVolume(fromconn, afrompart, afromvol,
3676                                        &volumeInfo);
3677             if (code) {
3678                 fprintf(STDERR, "Could not fetch information about RW vol %lu from server %s\n",
3679                                 (unsigned long)afromvol,
3680                                 hostutil_GetNameByINet(afromserver));
3681                 PrintError("", code);
3682                 justnewsites = 0;
3683             } else {
3684                 rwupdate = volumeInfo.volEntries_val[0].updateDate;
3685
3686                 free(volumeInfo.volEntries_val);
3687                 volumeInfo.volEntries_val = NULL;
3688                 volumeInfo.volEntries_len = 0;
3689             }
3690         }
3691         if (justnewsites && oldest <= rwupdate) {
3692             /* RW has changed */
3693             justnewsites = 0;
3694         }
3695
3696         /* Mark all the ROs in the VLDB entry as RO_DONTUSE. We don't
3697          * write this entry out to the vlserver until after the first
3698          * RO volume is released (temp RO clones don't count).
3699          *
3700          * If 'justnewsites' is set, we're only updating sites that have
3701          * RO_DONTUSE set, so set NEW_REPSITE for all of the others.
3702          */
3703         for (i = 0; i < entry.nServers; i++) {
3704             if (justnewsites) {
3705                 if ((entry.serverFlags[i] & RO_DONTUSE)) {
3706                     entry.serverFlags[i] &= ~NEW_REPSITE;
3707                 } else {
3708                     entry.serverFlags[i] |= NEW_REPSITE;
3709                 }
3710             } else {
3711                 entry.serverFlags[i] &= ~NEW_REPSITE;
3712                 entry.serverFlags[i] |= RO_DONTUSE;
3713             }
3714         }
3715         entry.serverFlags[rwindex] |= NEW_REPSITE;
3716         entry.serverFlags[rwindex] &= ~RO_DONTUSE;
3717     }
3718
3719     if (justnewsites && roexists) {
3720         /* if 'justnewsites' and 'roexists' are set, we don't need to do
3721          * anything with the RO clone, so skip the reclone */
3722         /* noop */
3723
3724     } else if (complete_release) {
3725
3726         if (roclone) {
3727             strcpy(vname, entry.name);
3728             if (stayUp)
3729                 strcat(vname, ".roclone");
3730             else
3731                 strcat(vname, ".readonly");
3732         } else {
3733             strcpy(vname, "readonly-clone-temp");
3734         }
3735
3736         code = DoVolClone(fromconn, afromvol, afrompart, readonlyVolume,
3737                           cloneVolId, (roclone && !stayUp)?"permanent RO":
3738                           "temporary RO", NULL, vname, NULL, &volstatus, NULL);
3739         if (code) {
3740             error = code;
3741             goto rfail;
3742         }
3743
3744         if (justnewsites && rwupdate != volstatus.updateDate) {
3745             justnewsites = 0;
3746             /* reset the serverFlags as if 'justnewsites' had never been set */
3747             for (i = 0; i < entry.nServers; i++) {
3748                 entry.serverFlags[i] &= ~NEW_REPSITE;
3749                 entry.serverFlags[i] |= RO_DONTUSE;
3750             }
3751             entry.serverFlags[rwindex] |= NEW_REPSITE;
3752             entry.serverFlags[rwindex] &= ~RO_DONTUSE;
3753         }
3754
3755         rwcrdate = volstatus.creationDate;
3756
3757         /* Remember clone volume ID in case we fail or are interrupted */
3758         entry.cloneId = cloneVolId;
3759
3760         if (roclone && !stayUp) {
3761             /* Bring the RO clone online - though not if it's a temporary clone */
3762             VPRINT1("Starting transaction on RO clone volume %u...",
3763                     cloneVolId);
3764             code =
3765                 AFSVolTransCreate_retry(fromconn, cloneVolId, afrompart, ITOffline,
3766                                   &onlinetid);
3767             ONERROR(code, cloneVolId,
3768                     "Failed to start transaction on volume %u\n");
3769             VDONE;
3770
3771             VPRINT1("Setting volume flags for volume %u...", cloneVolId);
3772             tcode = AFSVolSetFlags(fromconn, onlinetid, 0);
3773             VDONE;
3774
3775             VPRINT1("Ending transaction on volume %u...", cloneVolId);
3776             code = AFSVolEndTrans(fromconn, onlinetid, &rcode);
3777             ONERROR((code ? code : rcode), cloneVolId,
3778                     "Failed to end transaction on RO clone %u\n");
3779             VDONE;
3780
3781             ONERROR(tcode, cloneVolId, "Could not bring volume %u on line\n");
3782
3783             /* Sleep so that a client searching for an online volume won't
3784              * find the clone offline and then the next RO offline while the
3785              * release brings the clone online and the next RO offline (race).
3786              * There is a fix in the 3.4 client that does not need this sleep
3787              * anymore, but we don't know what clients we have.
3788              */
3789             if (entry.nServers > 2 && !justnewsites)
3790                 sleep(5);
3791
3792             /* Mark the RO clone in the VLDB as a good site (already released) */
3793             entry.serverFlags[roindex] |= NEW_REPSITE;
3794             entry.serverFlags[roindex] &= ~RO_DONTUSE;
3795             entry.flags |= RO_EXISTS;
3796
3797             releasecount++;
3798
3799             /* Write out the VLDB entry only if the clone is not a temporary
3800              * clone. If we did this to a temporary clone then we would end
3801              * up marking all the ROs as "old release" making the ROs
3802              * temporarily unavailable.
3803              */
3804             MapNetworkToHost(&entry, &storeEntry);
3805             VPRINT1("Replacing VLDB entry for %s...", entry.name);
3806             vcode = VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry, 0);
3807             ONERROR(vcode, entry.name,
3808                     "Could not update vldb entry for %s.\n");
3809             VDONE;
3810         }
3811     }
3812
3813     if (justnewsites) {
3814         VPRINT("RW vol has not changed; only releasing to new RO sites\n");
3815         /* act like this is a completion of a previous release */
3816         complete_release = CR_RECOVER;
3817     } else if (tried_justnewsites) {
3818         VPRINT("RW vol has changed; releasing to all sites\n");
3819     }
3820
3821     /* Now we will release from the clone to the remaining RO replicas.
3822      * The first 2 ROs (counting the non-temporary RO clone) are released
3823      * individually: releasecount. This is to reduce the race condition
3824      * of clients trying to find an on-line RO volume. The remaining ROs
3825      * are released in parallel but no more than half the number of ROs
3826      * (rounded up) at a time: nservers.
3827      */
3828
3829     strcpy(vname, entry.name);
3830     if (stayUp)
3831         strcat(vname, ".roclone");
3832     else
3833         strcat(vname, ".readonly");
3834     memset(&cookie, 0, sizeof(cookie));
3835     strncpy(cookie.name, vname, VOLSER_OLDMAXVOLNAME);
3836     cookie.type = ROVOL;
3837     cookie.parent = entry.volumeId[RWVOL];
3838     cookie.clone = 0;
3839
3840     /* how many to do at once, excluding clone */
3841     if (stayUp || justnewsites)
3842         nservers = entry.nServers; /* can do all, none offline */
3843     else
3844         nservers = entry.nServers / 2;
3845     replicas = calloc(nservers + 1, sizeof(struct replica));
3846     times = calloc(nservers + 1, sizeof(struct release));
3847     toconns = calloc(nservers + 1, sizeof(struct rx_connection *));
3848     results.manyResults_val = calloc(nservers + 1, sizeof(afs_int32));
3849     if (!replicas || !times || !results.manyResults_val || !toconns)
3850         ONERROR0(ENOMEM,
3851                 "Failed to create transaction on the release clone\n");
3852
3853     /* Create a transaction on the cloned volume */
3854     VPRINT1("Starting transaction on cloned volume %u...", cloneVolId);
3855     code =
3856         AFSVolTransCreate_retry(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
3857     if (!code) {
3858         memset(&orig_status, 0, sizeof(orig_status));
3859         code = AFSVolGetStatus(fromconn, fromtid, &orig_status);
3860     }
3861     if (!complete_release && code)
3862         ONERROR(VOLSERNOVOL, afromvol,
3863                 "Old clone is inaccessible. Try vos release -f %u.\n");
3864     ONERROR0(code, "Failed to create transaction on the release clone\n");
3865     VDONE;
3866
3867     /* if we have a clone, treat this as done, for now */
3868     if (stayUp && !complete_release) {
3869         entry.serverFlags[roindex] |= NEW_REPSITE;
3870         entry.serverFlags[roindex] &= ~RO_DONTUSE;
3871         entry.flags |= RO_EXISTS;
3872
3873         releasecount++;
3874     }
3875
3876     /* For each index in the VLDB */
3877     for (vldbindex = 0; vldbindex < entry.nServers;) {
3878         /* Get a transaction on the replicas. Pick replicas which have an old release. */
3879         for (volcount = 0;
3880              ((volcount < nservers) && (vldbindex < entry.nServers));
3881              vldbindex++) {
3882             if (!stayUp && !justnewsites) {
3883                 /* The first two RO volumes will be released individually.
3884                  * The rest are then released in parallel. This is a hack
3885                  * for clients not recognizing right away when a RO volume
3886                  * comes back on-line.
3887                  */
3888                 if ((volcount == 1) && (releasecount < 2))
3889                     break;
3890             }
3891
3892             if (vldbindex == roindex)
3893                 continue;       /* the clone    */
3894             if ((entry.serverFlags[vldbindex] & NEW_REPSITE)
3895                 && !(entry.serverFlags[vldbindex] & RO_DONTUSE))
3896                 continue;
3897             if (!(entry.serverFlags[vldbindex] & ITSROVOL))
3898                 continue;       /* not a RO vol */
3899
3900
3901             /* Get a Transaction on this replica. Get a new connection if
3902              * necessary.  Create the volume if necessary.  Return the
3903              * time from which the dump should be made (0 if it's a new
3904              * volume).  Each volume might have a different time.
3905              */
3906             replicas[volcount].server.destHost =
3907                 ntohl(entry.serverNumber[vldbindex]);
3908             replicas[volcount].server.destPort = AFSCONF_VOLUMEPORT;
3909             replicas[volcount].server.destSSID = 1;
3910             times[volcount].vldbEntryIndex = vldbindex;
3911
3912             code =
3913                 GetTrans(&entry, vldbindex, &(toconns[volcount]),
3914                          &(replicas[volcount].trans),
3915                          &(times[volcount].crtime),
3916                          &(times[volcount].uptime),
3917                          origflags, stayUp?cloneVolId:0);
3918             if (code)
3919                 continue;
3920
3921             /* Thisdate is the date from which we want to pick up all changes */
3922             if (forceflag) {
3923                 /* Do a full dump when forced by the caller. */
3924                 VPRINT("This will be a full dump: forced\n");
3925                 thisdate = 0;
3926             } else if (!complete_release) {
3927                 /* If this release is a recovery of a failed release, we can't be
3928                  * sure the creation date is good, so do a full dump.
3929                  */
3930                 VPRINT("This will be a full dump: previous release failed\n");
3931                 thisdate = 0;
3932             } else if (times[volcount].crtime == 0) {
3933                 /* A full dump is needed for a new read-only volume. */
3934                 VPRINT
3935                     ("This will be a full dump: read-only volume needs to be created\n");
3936                 thisdate = 0;
3937             } else if ((rwcrdate > times[volcount].crtime)) {
3938                 /* If the RW volume was replaced (its creation date is newer than
3939                  * the last release), then we can't be sure what has changed (so
3940                  * we do a full dump).
3941                  */
3942                 VPRINT
3943                     ("This will be a full dump: read-write volume was replaced\n");
3944                 thisdate = 0;
3945             } else if (remembertime[vldbindex].validtime) {
3946                 /* Trans was prev ended. Use the time from the prev trans
3947                  * because, prev trans may have created the volume. In which
3948                  * case time[volcount].time would be now instead of 0.
3949                  */
3950                 thisdate =
3951                     (remembertime[vldbindex].uptime < times[volcount].uptime)
3952                         ? remembertime[vldbindex].uptime
3953                         : times[volcount].uptime;
3954             } else {
3955                 thisdate = times[volcount].uptime;
3956             }
3957             remembertime[vldbindex].validtime = 1;
3958             remembertime[vldbindex].uptime = thisdate;
3959
3960             if (volcount == 0) {
3961                 fromdate = thisdate;
3962             } else {
3963                 /* Include this volume if it is within 15 minutes of the earliest */
3964                 if (((fromdate >
3965                       thisdate) ? (fromdate - thisdate) : (thisdate -
3966                                                            fromdate)) > 900) {
3967                     AFSVolEndTrans(toconns[volcount],
3968                                    replicas[volcount].trans, &rcode);
3969                     replicas[volcount].trans = 0;
3970                     break;
3971                 }
3972                 if (thisdate < fromdate)
3973                     fromdate = thisdate;
3974             }
3975             volcount++;
3976         }
3977         if (!volcount)
3978             continue;
3979
3980         code = CheckTrans(fromconn, &fromtid, afrompart, &orig_status);
3981         if (code) {
3982             code = ENOENT;
3983             goto rfail;
3984         }
3985
3986         if (verbose) {
3987             fprintf(STDOUT, "Starting ForwardMulti from %lu to %u on %s",
3988                     (unsigned long)cloneVolId, stayUp?
3989                     cloneVolId:entry.volumeId[ROVOL],
3990                     noresolve ? afs_inet_ntoa_r(entry.serverNumber[times[0].
3991                                                 vldbEntryIndex], hoststr) :
3992                     hostutil_GetNameByINet(entry.
3993                                            serverNumber[times[0].
3994                                                         vldbEntryIndex]));
3995
3996             for (s = 1; s < volcount; s++) {
3997                 fprintf(STDOUT, " and %s",
3998                         noresolve ? afs_inet_ntoa_r(entry.serverNumber[times[s].
3999                                                     vldbEntryIndex], hoststr) :
4000                         hostutil_GetNameByINet(entry.
4001                                                serverNumber[times[s].
4002                                                             vldbEntryIndex]));
4003             }
4004
4005             if (fromdate == 0)
4006                 fprintf(STDOUT, " (entire volume)");
4007             else {
4008                 tmv = fromdate;
4009                 fprintf(STDOUT, " (as of %.24s)", ctime(&tmv));
4010             }
4011             fprintf(STDOUT, ".\n");
4012             fflush(STDOUT);
4013         }
4014
4015         /* Release the ones we have collected */
4016         tr.manyDests_val = &(replicas[0]);
4017         tr.manyDests_len = results.manyResults_len = volcount;
4018         code =
4019             AFSVolForwardMultiple(fromconn, fromtid, fromdate, &tr,
4020                                   0 /*spare */ , &cookie, &results);
4021         if (code == RXGEN_OPCODE) {     /* RPC Interface Mismatch */
4022             code =
4023                 SimulateForwardMultiple(fromconn, fromtid, fromdate, &tr,
4024                                         0 /*spare */ , &cookie, &results);
4025             nservers = 1;
4026         }
4027
4028         if (code) {
4029             PrintError("Release failed: ", code);
4030         } else {
4031             for (m = 0; m < volcount; m++) {
4032                 if (results.manyResults_val[m]) {
4033                     if ((m == 0) || (results.manyResults_val[m] != ENOENT)) {
4034                         /* we retry timed out transaction. When it is
4035                          * not the first volume and the transaction wasn't found
4036                          * (assume it timed out and was garbage collected by volser).
4037                          */
4038                         PrintError
4039                             ("Failed to dump volume from clone to a ro site: ",
4040                              results.manyResults_val[m]);
4041                     }
4042                     continue;
4043                 }
4044
4045                 code =
4046                     AFSVolSetIdsTypes(toconns[m], replicas[m].trans, vname,
4047                                       ROVOL, entry.volumeId[RWVOL], 0, 0);
4048                 if (code) {
4049                     if ((m == 0) || (code != ENOENT)) {
4050                         PrintError("Failed to set correct names and ids: ",
4051                                    code);
4052                     }
4053                     continue;
4054                 }
4055
4056                 /* have to clear dest. flags to ensure new vol goes online:
4057                  * because the restore (forwarded) operation copied
4058                  * the V_inService(=0) flag over to the destination.
4059                  */
4060                 code = AFSVolSetFlags(toconns[m], replicas[m].trans, 0);
4061                 if (code) {
4062                     if ((m == 0) || (code != ENOENT)) {
4063                         PrintError("Failed to set flags on ro volume: ",
4064                                    code);
4065                     }
4066                     continue;
4067                 }
4068
4069                 entry.serverFlags[times[m].vldbEntryIndex] |= NEW_REPSITE;
4070                 entry.serverFlags[times[m].vldbEntryIndex] &= ~RO_DONTUSE;
4071                 entry.flags |= RO_EXISTS;
4072                 releasecount++;
4073             }
4074         }
4075
4076         if (!stayUp) {
4077             PutTrans(&vldbindex, replicas, toconns, times, volcount);
4078             MapNetworkToHost(&entry, &storeEntry);
4079             vcode = VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry, 0);
4080             ONERROR(vcode, afromvol,
4081                     " Could not update VLDB entry for volume %u\n");
4082         }
4083     }                           /* for each index in the vldb */
4084
4085     /* for the stayup case, put back at the end */
4086     if (stayUp) {
4087         afs_uint32 tmpVol = entry.volumeId[ROVOL];
4088         strcpy(vname, entry.name);
4089         strcat(vname, ".readonly");
4090
4091         if (roclone) {
4092             /* have to clear flags to ensure new vol goes online
4093              */
4094             code = AFSVolSetFlags(fromconn, fromtid, 0);
4095             if (code && (code != ENOENT)) {
4096                 PrintError("Failed to set flags on ro volume: ",
4097                            code);
4098             }
4099
4100             VPRINT3("%sloning to permanent RO %u on %s...", roexists?"Re-c":"C", tmpVol,
4101                     noresolve ?
4102                     afs_inet_ntoa_r(entry.serverNumber[roindex],
4103                                     hoststr) :
4104                     hostutil_GetNameByINet(entry.serverNumber[roindex]));
4105
4106             code = AFSVolClone(fromconn, fromtid, roexists?tmpVol:0,
4107                                readonlyVolume, vname, &tmpVol);
4108
4109             if (!code) {
4110                 VDONE;
4111                 VPRINT("Bringing readonly online...");
4112                 code = DoVolOnline(&entry, tmpVol, roindex, vname,
4113                                    fromconn);
4114             }
4115             if (code) {
4116                 EPRINT(code, "Failed: ");
4117                 entry.serverFlags[roindex] &= ~NEW_REPSITE;
4118                 entry.serverFlags[roindex] |= RO_DONTUSE;
4119             } else {
4120                 entry.serverFlags[roindex] |= NEW_REPSITE;
4121                 entry.serverFlags[roindex] &= ~RO_DONTUSE;
4122                 entry.flags |= RO_EXISTS;
4123                 VDONE;
4124             }
4125
4126         }
4127         for (s = 0; s < volcount; s++) {
4128             if (replicas[s].trans) {
4129                 vldbindex = times[s].vldbEntryIndex;
4130
4131                 /* ok, so now we have to end the previous transaction */
4132                 code = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
4133                 if (!code)
4134                     code = rcode;
4135
4136                 if (!code) {
4137                     code = AFSVolTransCreate_retry(toconns[s],
4138                                                    cloneVolId,
4139                                                    entry.serverPartition[vldbindex],
4140                                                    ITBusy,
4141                                                    &(replicas[s].trans));
4142                     if (code) {
4143                         PrintError("Unable to begin transaction on temporary clone: ", code);
4144                     }
4145                 } else {
4146                     PrintError("Unable to end transaction on temporary clone: ", code);
4147                 }
4148
4149                 VPRINT3("%sloning to permanent RO %u on %s...", times[s].crtime?"Re-c":"C",
4150                         tmpVol, noresolve ?
4151                         afs_inet_ntoa_r(htonl(replicas[s].server.destHost),
4152                                         hoststr) :
4153                         hostutil_GetNameByINet(htonl(replicas[s].server.destHost)));
4154                 if (times[s].crtime)
4155                     code = AFSVolClone(toconns[s], replicas[s].trans, tmpVol,
4156                                        readonlyVolume, vname, &tmpVol);
4157                 else
4158                     code = AFSVolClone(toconns[s], replicas[s].trans, 0,
4159                                        readonlyVolume, vname, &tmpVol);
4160
4161                 if (code) {
4162                     if (!times[s].crtime) {
4163                         entry.serverFlags[vldbindex] |= RO_DONTUSE;
4164                     }
4165                     entry.serverFlags[vldbindex] &= ~NEW_REPSITE;
4166                     PrintError("Failed: ",
4167                                code);
4168                 } else
4169                     VDONE;
4170
4171                 if (entry.serverFlags[vldbindex] != RO_DONTUSE) {
4172                     /* bring it online (mark it InService) */
4173                     VPRINT1("Bringing readonly online on %s...",
4174                             noresolve ?
4175                             afs_inet_ntoa_r(
4176                                 htonl(replicas[s].server.destHost),
4177                                 hoststr) :
4178                             hostutil_GetNameByINet(
4179                                 htonl(replicas[s].server.destHost)));
4180
4181                     code = DoVolOnline(&entry, tmpVol, vldbindex, vname,
4182                                        toconns[s]);
4183                     /* needed to come online for cloning */
4184                     if (code) {
4185                         /* technically it's still new, just not online */
4186                         entry.serverFlags[s] &= ~NEW_REPSITE;
4187                         entry.serverFlags[s] |= RO_DONTUSE;
4188                         if (code != ENOENT) {
4189                             PrintError("Failed to set correct names and ids: ",
4190                                        code);
4191                         }
4192                     } else
4193                         VDONE;
4194                 }
4195
4196                 VPRINT("Marking temporary clone for deletion...\n");
4197                 code = AFSVolSetFlags(toconns[s],
4198                                       replicas[s].trans,
4199                                       VTDeleteOnSalvage |
4200                                       VTOutOfService);
4201                 if (code)
4202                   EPRINT(code, "Failed: ");
4203                 else
4204                   VDONE;
4205
4206                 VPRINT("Ending transaction on temporary clone...\n");
4207                 code = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
4208                 if (!code)
4209                     rcode = code;
4210                 if (code)
4211                     PrintError("Failed: ", code);
4212                 else {
4213                     VDONE;
4214                     /* ended successfully */
4215                     replicas[s].trans = 0;
4216
4217                     VPRINT2("Deleting temporary clone %u on %s...", cloneVolId,
4218                             noresolve ?
4219                             afs_inet_ntoa_r(htonl(replicas[s].server.destHost),
4220                                             hoststr) :
4221                             hostutil_GetNameByINet(htonl(replicas[s].server.destHost)));
4222                     code = DoVolDelete(toconns[s], cloneVolId,
4223                                        entry.serverPartition[vldbindex],
4224                                        NULL, 0, NULL, NULL);
4225                     if (code) {
4226                         EPRINT(code, "Failed: ");
4227                     } else
4228                         VDONE;
4229                 }
4230             }
4231         }
4232
4233         /* done. put the vldb entry in the success tail case*/
4234         PutTrans(&vldbindex, replicas, toconns, times, volcount);
4235     }
4236
4237     /* End the transaction on the cloned volume */
4238     code = AFSVolEndTrans(fromconn, fromtid, &rcode);
4239     fromtid = 0;
4240     if (!code)
4241         code = rcode;
4242     if (code)
4243         PrintError("Failed to end transaction on rw volume: ", code);
4244
4245     /* Figure out if any volume were not released and say so */
4246     for (failure = 0, i = 0; i < entry.nServers; i++) {
4247         if (!(entry.serverFlags[i] & NEW_REPSITE))
4248             failure++;
4249     }
4250     if (failure) {
4251         char pname[10];
4252         fprintf(STDERR,
4253                 "The volume %lu could not be released to the following %d sites:\n",
4254                 (unsigned long)afromvol, failure);
4255         for (i = 0; i < entry.nServers; i++) {
4256             if (!(entry.serverFlags[i] & NEW_REPSITE)) {
4257                 MapPartIdIntoName(entry.serverPartition[i], pname);
4258                 fprintf(STDERR, "\t%35s %s\n",
4259                         noresolve ? afs_inet_ntoa_r(entry.serverNumber[i], hoststr) :
4260                         hostutil_GetNameByINet(entry.serverNumber[i]), pname);
4261             }
4262         }
4263         MapNetworkToHost(&entry, &storeEntry);
4264         vcode =
4265             VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry,
4266                               LOCKREL_TIMESTAMP);
4267         ONERROR(vcode, afromvol,
4268                 " Could not update VLDB entry for volume %u\n");
4269
4270         ERROREXIT(VOLSERBADRELEASE);
4271     }
4272
4273     entry.cloneId = 0;
4274     /* All the ROs were release successfully. Remove the temporary clone */
4275     if (!roclone || stayUp) {
4276         if (verbose) {
4277             fprintf(STDOUT, "Deleting the releaseClone %lu ...",
4278                     (unsigned long)cloneVolId);
4279             fflush(STDOUT);
4280         }
4281         code = DoVolDelete(fromconn, cloneVolId, afrompart, NULL, 0, NULL,
4282                            NULL);
4283         ONERROR(code, cloneVolId, "Failed to delete volume %u.\n");
4284         VDONE;
4285     }
4286
4287     for (i = 0; i < entry.nServers; i++)
4288         entry.serverFlags[i] &= ~NEW_REPSITE;
4289
4290     /* Update the VLDB */
4291     VPRINT("updating VLDB ...");
4292
4293     MapNetworkToHost(&entry, &storeEntry);
4294     vcode =
4295         VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry,
4296                           LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
4297     ONERROR(vcode, afromvol, " Could not update VLDB entry for volume %u\n");
4298     VDONE;
4299
4300   rfail:
4301     if (clonetid) {
4302         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
4303         clonetid = 0;
4304         if (code) {
4305             fprintf(STDERR,
4306                     "Failed to end cloning transaction on the RW volume %lu\n",
4307                     (unsigned long)afromvol);
4308             if (!error)
4309                 error = code;
4310         }
4311     }
4312     if (fromtid) {
4313         code = AFSVolEndTrans(fromconn, fromtid, &rcode);
4314         fromtid = 0;
4315         if (code) {
4316             fprintf(STDERR,
4317                     "Failed to end transaction on the release clone %lu\n",
4318                     (unsigned long)cloneVolId);
4319             if (!error)
4320                 error = code;
4321         }
4322     }
4323     for (i = 0; i < nservers; i++) {
4324         if (replicas && replicas[i].trans) {
4325             code = AFSVolEndTrans(toconns[i], replicas[i].trans, &rcode);
4326             replicas[i].trans = 0;
4327             if (code) {
4328                 fprintf(STDERR,
4329                         "Failed to end transaction on ro volume %u at server %s\n",
4330                         entry.volumeId[ROVOL],
4331                         noresolve ? afs_inet_ntoa_r(htonl(replicas[i].server.
4332                                                         destHost), hoststr) :
4333                         hostutil_GetNameByINet(htonl
4334                                                (replicas[i].server.destHost)));
4335                 if (!error)
4336                     error = code;
4337             }
4338         }
4339         if (toconns && toconns[i]) {
4340             rx_DestroyConnection(toconns[i]);
4341             toconns[i] = 0;
4342         }
4343     }
4344     if (islocked) {
4345         vcode =
4346             ubik_VL_ReleaseLock(cstruct, 0, afromvol, RWVOL,
4347                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
4348         if (vcode) {
4349             fprintf(STDERR,
4350                     "Could not release lock on the VLDB entry for volume %lu\n",
4351                     (unsigned long)afromvol);
4352             if (!error)
4353                 error = vcode;
4354         }
4355     }
4356
4357     PrintError("", error);
4358
4359     if (fromconn)
4360         rx_DestroyConnection(fromconn);
4361     if (results.manyResults_val)
4362         free(results.manyResults_val);
4363     if (replicas)
4364         free(replicas);
4365     if (toconns)
4366         free(toconns);
4367     if (times)
4368         free(times);
4369     return error;
4370 }
4371
4372
4373 static void
4374 dump_sig_handler(int x)
4375 {
4376     fprintf(STDERR, "\nSignal handler: vos dump operation\n");
4377     longjmp(env, 0);
4378 }
4379
4380 /* Dump the volume <afromvol> on <afromserver> and
4381  * <afrompart> to <afilename> starting from <fromdate>.
4382  * DumpFunction does the real work behind the scenes after
4383  * extracting parameters from the rock
4384  */
4385 int
4386 UV_DumpVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
4387               afs_int32 fromdate,
4388               afs_int32(*DumpFunction) (struct rx_call *, void *), void *rock,
4389               afs_int32 flags)
4390 {
4391     /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
4392      * be changing during the dump */
4393     struct rx_call * volatile fromcall = NULL;
4394     struct rx_connection * volatile fromconn = NULL;
4395     afs_int32 volatile fromtid = 0;
4396
4397     afs_int32 rxError = 0, rcode = 0;
4398     afs_int32 code, error = 0;
4399     afs_int32 tmp;
4400     time_t tmv = fromdate;
4401
4402     if (setjmp(env))
4403         ERROR_EXIT(EPIPE);
4404 #ifndef AFS_NT40_ENV
4405     (void)signal(SIGPIPE, dump_sig_handler);
4406 #endif
4407     (void)signal(SIGINT, dump_sig_handler);
4408
4409     if (!fromdate) {
4410         VEPRINT("Full Dump ...\n");
4411     } else {
4412         VEPRINT1("Incremental Dump (as of %.24s)...\n",
4413                 ctime(&tmv));
4414     }
4415
4416     /* get connections to the servers */
4417     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
4418
4419     VEPRINT1("Starting transaction on volume %u...", afromvol);
4420     tmp = fromtid;
4421     code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
4422     fromtid = tmp;
4423     EGOTO1(error_exit, code,
4424            "Could not start transaction on the volume %u to be dumped\n",
4425            afromvol);
4426     VEDONE;
4427
4428     fromcall = rx_NewCall(fromconn);
4429
4430     VEPRINT1("Starting volume dump on volume %u...", afromvol);
4431     if (flags & VOLDUMPV2_OMITDIRS)
4432         code = StartAFSVolDumpV2(fromcall, fromtid, fromdate, flags);
4433     else
4434         code = StartAFSVolDump(fromcall, fromtid, fromdate);
4435     EGOTO(error_exit, code, "Could not start the dump process \n");
4436     VEDONE;
4437
4438     VEPRINT1("Dumping volume %u...", afromvol);
4439     code = DumpFunction(fromcall, rock);
4440     if (code == RXGEN_OPCODE)
4441         goto error_exit;
4442     EGOTO(error_exit, code, "Error while dumping volume \n");
4443     VEDONE;
4444
4445   error_exit:
4446     if (fromcall) {
4447         code = rx_EndCall(fromcall, rxError);
4448         if (code && code != RXGEN_OPCODE)
4449             fprintf(STDERR, "Error in rx_EndCall\n");
4450         if (code && !error)
4451             error = code;
4452     }
4453     if (fromtid) {
4454         VEPRINT1("Ending transaction on volume %u...", afromvol);
4455         code = AFSVolEndTrans(fromconn, fromtid, &rcode);
4456         if (code || rcode) {
4457             fprintf(STDERR, "Could not end transaction on the volume %lu\n",
4458                     (unsigned long)afromvol);
4459             if (!error)
4460                 error = (code ? code : rcode);
4461         }
4462         VEDONE;
4463     }
4464     if (fromconn)
4465         rx_DestroyConnection(fromconn);
4466
4467     if (error != RXGEN_OPCODE)
4468         PrintError("", error);
4469     return (error);
4470 }
4471
4472 /* Clone the volume <afromvol> on <afromserver> and
4473  * <afrompart>, and then dump the clone volume to
4474  * <afilename> starting from <fromdate>.
4475  * DumpFunction does the real work behind the scenes after
4476  * extracting parameters from the rock
4477  */
4478 int
4479 UV_DumpClonedVolume(afs_uint32 afromvol, afs_uint32 afromserver,
4480                     afs_int32 afrompart, afs_int32 fromdate,
4481                     afs_int32(*DumpFunction) (struct rx_call *, void *),
4482                     void *rock, afs_int32 flags)
4483 {
4484     /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
4485      * be changing during the dump */
4486     struct rx_connection * volatile fromconn = NULL;
4487     struct rx_call * volatile fromcall = NULL;
4488     afs_int32 volatile clonetid = 0;
4489     afs_uint32 volatile clonevol = 0;
4490
4491     afs_int32 tmp;
4492     afs_int32 fromtid = 0, rxError = 0, rcode = 0;
4493     afs_int32 code = 0, error = 0;
4494     afs_uint32 tmpVol;
4495     char vname[64];
4496     time_t tmv = fromdate;
4497
4498     if (setjmp(env))
4499         ERROR_EXIT(EPIPE);
4500 #ifndef AFS_NT40_ENV
4501     (void)signal(SIGPIPE, dump_sig_handler);
4502 #endif
4503     (void)signal(SIGINT, dump_sig_handler);
4504
4505     if (!fromdate) {
4506         VEPRINT("Full Dump ...\n");
4507     } else {
4508         VEPRINT1("Incremental Dump (as of %.24s)...\n",
4509                 ctime(&tmv));
4510     }
4511
4512     /* get connections to the servers */
4513     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
4514
4515     VEPRINT1("Starting transaction on volume %u...", afromvol);
4516     code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &fromtid);
4517     EGOTO1(error_exit, code,
4518            "Could not start transaction on the volume %u to be dumped\n",
4519            afromvol);
4520     VEDONE;
4521
4522     /* Get a clone id */
4523     VEPRINT1("Allocating new volume id for clone of volume %u ...", afromvol);
4524     tmpVol = clonevol;
4525     code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
4526     clonevol = tmpVol;
4527     EGOTO1(error_exit, code,
4528            "Could not get an ID for the clone of volume %u from the VLDB\n",
4529            afromvol);
4530     VEDONE;
4531
4532     /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
4533     VEPRINT2("Cloning source volume %u to clone volume %u...", afromvol,
4534             clonevol);
4535     strcpy(vname, "dump-clone-temp");
4536     tmpVol = clonevol;
4537     code =
4538         AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &tmpVol);
4539     clonevol = tmpVol;
4540     EGOTO1(error_exit, code, "Failed to clone the source volume %u\n",
4541            afromvol);
4542     VEDONE;
4543
4544     VEPRINT1("Ending the transaction on the volume %u ...", afromvol);
4545     rcode = 0;
4546     code = AFSVolEndTrans(fromconn, fromtid, &rcode);
4547     fromtid = 0;
4548     if (!code)
4549         code = rcode;
4550     EGOTO1(error_exit, code,
4551            "Failed to end the transaction on the volume %u\n", afromvol);
4552     VEDONE;
4553
4554
4555     VEPRINT1("Starting transaction on the cloned volume %u ...", clonevol);
4556     tmp = clonetid;
4557     code =
4558         AFSVolTransCreate_retry(fromconn, clonevol, afrompart, ITOffline,
4559                           &tmp);
4560     clonetid = tmp;
4561     EGOTO1(error_exit, code,
4562            "Failed to start a transaction on the cloned volume%u\n",
4563            clonevol);
4564     VEDONE;
4565
4566     VEPRINT1("Setting flags on cloned volume %u ...", clonevol);
4567     code = AFSVolSetFlags(fromconn, clonetid, VTDeleteOnSalvage | VTOutOfService);      /*redundant */
4568     EGOTO1(error_exit, code, "Could not set falgs on the cloned volume %u\n",
4569            clonevol);
4570     VEDONE;
4571
4572
4573     fromcall = rx_NewCall(fromconn);
4574
4575     VEPRINT1("Starting volume dump from cloned volume %u...", clonevol);
4576     if (flags & VOLDUMPV2_OMITDIRS)
4577         code = StartAFSVolDumpV2(fromcall, clonetid, fromdate, flags);
4578     else
4579         code = StartAFSVolDump(fromcall, clonetid, fromdate);
4580     EGOTO(error_exit, code, "Could not start the dump process \n");
4581     VEDONE;
4582
4583     VEPRINT1("Dumping volume %u...", afromvol);
4584     code = DumpFunction(fromcall, rock);
4585     EGOTO(error_exit, code, "Error while dumping volume \n");
4586     VEDONE;
4587
4588   error_exit:
4589     /* now delete the clone */
4590     VEPRINT1("Deleting the cloned volume %u ...", clonevol);
4591     code = AFSVolDeleteVolume(fromconn, clonetid);
4592     if (code) {
4593         fprintf(STDERR, "Failed to delete the cloned volume %lu\n",
4594                 (unsigned long)clonevol);
4595     } else {
4596         VEDONE;
4597     }
4598
4599     if (fromcall) {
4600         code = rx_EndCall(fromcall, rxError);
4601         if (code) {
4602             fprintf(STDERR, "Error in rx_EndCall\n");
4603             if (!error)
4604                 error = code;
4605         }
4606     }
4607     if (clonetid) {
4608         VEPRINT1("Ending transaction on cloned volume %u...", clonevol);
4609         code = AFSVolEndTrans(fromconn, clonetid, &rcode);
4610         if (code || rcode) {
4611             fprintf(STDERR,
4612                     "Could not end transaction on the cloned volume %lu\n",
4613                     (unsigned long)clonevol);
4614             if (!error)
4615                 error = (code ? code : rcode);
4616         }
4617         VEDONE;
4618     }
4619     if (fromconn)
4620         rx_DestroyConnection(fromconn);
4621
4622     PrintError("", error);
4623     return (error);
4624 }
4625
4626
4627
4628 /*
4629  * Restore a volume <tovolid> <tovolname> on <toserver> <topart> from
4630  * the dump file <afilename>. WriteData does all the real work
4631  * after extracting params from the rock
4632  */
4633 int
4634 UV_RestoreVolume2(afs_uint32 toserver, afs_int32 topart, afs_uint32 tovolid,
4635                   afs_uint32 toparentid, char tovolname[], int flags,
4636                   afs_int32(*WriteData) (struct rx_call *, void *),
4637                   void *rock)
4638 {
4639     struct rx_connection *toconn, *tempconn;
4640     struct rx_call *tocall;
4641     afs_int32 totid, code, rcode, vcode, terror = 0;
4642     afs_int32 rxError = 0;
4643     struct volser_status tstatus;
4644     struct volintInfo vinfo;
4645     char partName[10];
4646     char tovolreal[VOLSER_OLDMAXVOLNAME];
4647     afs_uint32 pvolid;
4648     afs_int32 temptid, pparentid;
4649     struct nvldbentry entry, storeEntry;
4650     afs_int32 error;
4651     int islocked;
4652     struct restoreCookie cookie;
4653     int reuseID;
4654     afs_int32 volflag, voltype, volsertype;
4655     afs_int32 oldCreateDate, oldUpdateDate, newCreateDate, newUpdateDate;
4656     int index, same, errcode;
4657     char apartName[10];
4658     char hoststr[16];
4659
4660     memset(&cookie, 0, sizeof(cookie));
4661     islocked = 0;
4662     error = 0;
4663     reuseID = 1;
4664     tocall = (struct rx_call *)0;
4665     tempconn = (struct rx_connection *)0;
4666     totid = 0;
4667     temptid = 0;
4668
4669     if (flags & RV_RDONLY) {
4670         voltype = ROVOL;
4671         volsertype = volser_RO;
4672     } else {
4673         voltype = RWVOL;
4674         volsertype = volser_RW;
4675     }
4676
4677     pvolid = tovolid;
4678     pparentid = toparentid;
4679     toconn = UV_Bind(toserver, AFSCONF_VOLUMEPORT);
4680     if (pvolid == 0) {          /*alot a new id if needed */
4681         vcode = VLDB_GetEntryByName(tovolname, &entry);
4682         if (vcode == VL_NOENT) {
4683             vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &pvolid);
4684             if (vcode) {
4685                 fprintf(STDERR, "Could not get an Id for the volume %s\n",
4686                         tovolname);
4687                 error = vcode;
4688                 goto refail;
4689             }
4690             reuseID = 0;
4691         } else if (flags & RV_RDONLY) {
4692             if (entry.flags & RW_EXISTS) {
4693                 fprintf(STDERR,
4694                         "Entry for ReadWrite volume %s already exists!\n",
4695                         entry.name);
4696                 error = VOLSERBADOP;
4697                 goto refail;
4698             }
4699             if (!entry.volumeId[ROVOL]) {
4700                 fprintf(STDERR,
4701                         "Existing entry for volume %s has no ReadOnly ID\n",
4702                         tovolname);
4703                 error = VOLSERBADOP;
4704                 goto refail;
4705             }
4706             pvolid = entry.volumeId[ROVOL];
4707             pparentid = entry.volumeId[RWVOL];
4708         } else {
4709             pvolid = entry.volumeId[RWVOL];
4710             pparentid = entry.volumeId[RWVOL];
4711         }
4712     }
4713     if (!pparentid) pparentid = pvolid;
4714     /* at this point we have a volume id to use/reuse for the volume to be restored */
4715     strncpy(tovolreal, tovolname, VOLSER_OLDMAXVOLNAME);
4716
4717     if (strlen(tovolname) > (VOLSER_OLDMAXVOLNAME - 1)) {
4718         EGOTO1(refail, VOLSERBADOP,
4719                "The volume name %s exceeds the maximum limit of (VOLSER_OLDMAXVOLNAME -1 ) bytes\n",
4720                tovolname);
4721     } else {
4722         if ((pparentid != pvolid) && (flags & RV_RDONLY)) {
4723             if (strlen(tovolname) > (VOLSER_OLDMAXVOLNAME - 10)) {
4724                 EGOTO1(refail, VOLSERBADOP,
4725                        "The volume name %s exceeds the maximum limit of (VOLSER_OLDMAXVOLNAME -1 ) bytes\n", tovolname);
4726             }
4727             snprintf(tovolreal, VOLSER_OLDMAXVOLNAME, "%s.readonly", tovolname);
4728         }
4729     }
4730     MapPartIdIntoName(topart, partName);
4731     fprintf(STDOUT, "Restoring volume %s Id %lu on server %s partition %s ..",
4732             tovolreal, (unsigned long)pvolid,
4733             noresolve ? afs_inet_ntoa_r(toserver, hoststr) :
4734             hostutil_GetNameByINet(toserver), partName);
4735     fflush(STDOUT);
4736     code =
4737         AFSVolCreateVolume(toconn, topart, tovolreal, volsertype, pparentid, &pvolid,
4738                            &totid);
4739     if (code) {
4740         if (flags & RV_FULLRST) {       /* full restore: delete then create anew */
4741             code = DoVolDelete(toconn, pvolid, topart, "the previous", 0,
4742                                &tstatus, NULL);
4743             if (code && code != VNOVOL) {
4744                 error = code;
4745                 goto refail;
4746             }
4747
4748             code =
4749                 AFSVolCreateVolume(toconn, topart, tovolreal, volsertype, pparentid,
4750                                    &pvolid, &totid);
4751             EGOTO1(refail, code, "Could not create new volume %u\n", pvolid);
4752         } else {
4753             code =
4754                 AFSVolTransCreate_retry(toconn, pvolid, topart, ITOffline, &totid);
4755             EGOTO1(refail, code, "Failed to start transaction on %u\n",
4756                    pvolid);
4757
4758             code = AFSVolGetStatus(toconn, totid, &tstatus);
4759             EGOTO1(refail, code, "Could not get timestamp from volume %u\n",
4760                    pvolid);
4761
4762         }
4763         oldCreateDate = tstatus.creationDate;
4764         oldUpdateDate = tstatus.updateDate;
4765     } else {
4766         oldCreateDate = 0;
4767         oldUpdateDate = 0;
4768     }
4769
4770     cookie.parent = pparentid;
4771     cookie.type = voltype;
4772     cookie.clone = 0;
4773     strncpy(cookie.name, tovolreal, VOLSER_OLDMAXVOLNAME);
4774
4775     tocall = rx_NewCall(toconn);
4776     terror = StartAFSVolRestore(tocall, totid, 1, &cookie);
4777     if (terror) {
4778         fprintf(STDERR, "Volume restore Failed \n");
4779         error = terror;
4780         goto refail;
4781     }
4782     code = WriteData(tocall, rock);
4783     if (code) {
4784         fprintf(STDERR, "Could not transmit data\n");
4785         error = code;
4786         goto refail;
4787     }
4788     terror = rx_EndCall(tocall, rxError);
4789     tocall = (struct rx_call *)0;
4790     if (terror) {
4791         fprintf(STDERR, "rx_EndCall Failed \n");
4792         error = terror;
4793         goto refail;
4794     }
4795     code = AFSVolGetStatus(toconn, totid, &tstatus);
4796     if (code) {
4797         fprintf(STDERR,
4798                 "Could not get status information about the volume %lu\n",
4799                 (unsigned long)pvolid);
4800         error = code;
4801         goto refail;
4802     }
4803     code = AFSVolSetIdsTypes(toconn, totid, tovolreal, voltype, pparentid, 0, 0);
4804     if (code) {
4805         fprintf(STDERR, "Could not set the right type and ID on %lu\n",
4806                 (unsigned long)pvolid);
4807         error = code;
4808         goto refail;
4809     }
4810
4811     if (flags & RV_CRDUMP)
4812         newCreateDate = tstatus.creationDate;
4813     else if (flags & RV_CRKEEP && oldCreateDate != 0)
4814         newCreateDate = oldCreateDate;
4815     else
4816         newCreateDate = time(0);
4817     if (flags & RV_LUDUMP)
4818         newUpdateDate = tstatus.updateDate;
4819     else if (flags & RV_LUKEEP)
4820         newUpdateDate = oldUpdateDate;
4821     else
4822         newUpdateDate = time(0);
4823     code = AFSVolSetDate(toconn,totid, newCreateDate);
4824     if (code) {
4825         fprintf(STDERR, "Could not set the 'creation' date on %u\n", pvolid);
4826         error = code;
4827         goto refail;
4828     }
4829
4830     init_volintInfo(&vinfo);
4831     vinfo.creationDate = newCreateDate;
4832     vinfo.updateDate = newUpdateDate;
4833     code = AFSVolSetInfo(toconn, totid, &vinfo);
4834     if (code) {
4835         fprintf(STDERR, "Could not set the 'last updated' date on %u\n",
4836                 pvolid);
4837         error = code;
4838         goto refail;
4839     }
4840
4841     volflag = ((flags & RV_OFFLINE) ? VTOutOfService : 0);      /* off or on-line */
4842     code = AFSVolSetFlags(toconn, totid, volflag);
4843     if (code) {
4844         fprintf(STDERR, "Could not mark %lu online\n", (unsigned long)pvolid);
4845         error = code;
4846         goto refail;
4847     }
4848
4849 /* It isn't handled right in refail */
4850     code = AFSVolEndTrans(toconn, totid, &rcode);
4851     totid = 0;
4852     if (!code)
4853         code = rcode;
4854     if (code) {
4855         fprintf(STDERR, "Could not end transaction on %lu\n",
4856                 (unsigned long)pvolid);
4857         error = code;
4858         goto refail;
4859     }
4860
4861     fprintf(STDOUT, " done\n");
4862     fflush(STDOUT);
4863     if (!reuseID || (flags & RV_FULLRST)) {
4864         /* Volume was restored on the file server, update the
4865          * VLDB to reflect the change.
4866          */
4867         vcode = VLDB_GetEntryByID(pvolid, voltype, &entry);
4868         if (vcode && vcode != VL_NOENT && vcode != VL_ENTDELETED) {
4869             fprintf(STDERR,
4870                     "Could not fetch the entry for volume number %lu from VLDB \n",
4871                     (unsigned long)pvolid);
4872             error = vcode;
4873             goto refail;
4874         }
4875         if (!vcode)
4876             MapHostToNetwork(&entry);
4877         if (vcode == VL_NOENT) {        /* it doesnot exist already */
4878             /*make the vldb return this indication specifically */
4879             VPRINT("------- Creating a new VLDB entry ------- \n");
4880             strcpy(entry.name, tovolname);
4881             entry.nServers = 1;
4882             entry.serverNumber[0] = toserver;   /*should be indirect */
4883             entry.serverPartition[0] = topart;
4884             entry.serverFlags[0] = (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
4885             entry.flags = (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
4886             if (flags & RV_RDONLY)
4887                 entry.volumeId[ROVOL] = pvolid;
4888             else if (tstatus.cloneID != 0) {
4889                 entry.volumeId[ROVOL] = tstatus.cloneID;        /*this should come from status info on the volume if non zero */
4890             } else
4891                 entry.volumeId[ROVOL] = INVALID_BID;
4892             entry.volumeId[RWVOL] = pparentid;
4893             entry.cloneId = 0;
4894             if (tstatus.backupID != 0) {
4895                 entry.volumeId[BACKVOL] = tstatus.backupID;
4896                 /*this should come from status info on the volume if non zero */
4897             } else
4898                 entry.volumeId[BACKVOL] = INVALID_BID;
4899             MapNetworkToHost(&entry, &storeEntry);
4900             vcode = VLDB_CreateEntry(&storeEntry);
4901             if (vcode) {
4902                 fprintf(STDERR,
4903                         "Could not create the VLDB entry for volume number %lu  \n",
4904                         (unsigned long)pvolid);
4905                 error = vcode;
4906                 goto refail;
4907             }
4908             islocked = 0;
4909             if (verbose)
4910                 EnumerateEntry(&entry);
4911         } else {                /*update the existing entry */
4912             if (verbose) {
4913                 fprintf(STDOUT, "Updating the existing VLDB entry\n");
4914                 fprintf(STDOUT, "------- Old entry -------\n");
4915                 EnumerateEntry(&entry);
4916                 fprintf(STDOUT, "------- New entry -------\n");
4917             }
4918             vcode =
4919                 ubik_VL_SetLock(cstruct, 0, pvolid, voltype,
4920                           VLOP_RESTORE);
4921             if (vcode) {
4922                 fprintf(STDERR,
4923                         "Could not lock the entry for volume number %lu \n",
4924                         (unsigned long)pvolid);
4925                 error = vcode;
4926                 goto refail;
4927             }
4928             islocked = 1;
4929             strcpy(entry.name, tovolname);
4930
4931             /* Update the vlentry with the new information */
4932             if (flags & RV_RDONLY)
4933                 index = Lp_ROMatch(toserver, topart, &entry) - 1;
4934             else
4935                 index = Lp_GetRwIndex(&entry);
4936             if (index == -1) {
4937                 /* Add the new site for the volume being restored */
4938                 entry.serverNumber[entry.nServers] = toserver;
4939                 entry.serverPartition[entry.nServers] = topart;
4940                 entry.serverFlags[entry.nServers] =
4941                     (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
4942                 entry.nServers++;
4943             } else {
4944                 /* This volume should be deleted on the old site
4945                  * if its different from new site.
4946                  */
4947                 same =
4948                     VLDB_IsSameAddrs(toserver, entry.serverNumber[index],
4949                                      &errcode);
4950                 if (errcode)
4951                     EPRINT2(errcode,
4952                             "Failed to get info about server's %d address(es) from vlserver (err=%d)\n",
4953                             toserver, errcode);
4954                 if ((!errcode && !same)
4955                     || (entry.serverPartition[index] != topart)) {
4956                     if (flags & RV_NODEL) {
4957                         VPRINT2
4958                             ("Not deleting the previous volume %u on server %s, ...",
4959                              pvolid,
4960                              noresolve ? afs_inet_ntoa_r(entry.serverNumber[index], hoststr) :
4961                              hostutil_GetNameByINet(entry.serverNumber[index]));
4962                     } else {
4963                         tempconn =
4964                             UV_Bind(entry.serverNumber[index],
4965                                     AFSCONF_VOLUMEPORT);
4966
4967                         MapPartIdIntoName(entry.serverPartition[index],
4968                                           apartName);
4969                         VPRINT3
4970                             ("Deleting the previous volume %u on server %s, partition %s ...",
4971                              pvolid,
4972                              noresolve ? afs_inet_ntoa_r(entry.serverNumber[index], hoststr) :
4973                              hostutil_GetNameByINet(entry.serverNumber[index]),
4974                              apartName);
4975                         code = DoVolDelete(tempconn, pvolid,
4976                                            entry.serverPartition[index],
4977                                            "the", 0, NULL, NULL);
4978                         if (code && code != VNOVOL) {
4979                             error = code;
4980                             goto refail;
4981                         }
4982                         MapPartIdIntoName(entry.serverPartition[index],
4983                                           partName);
4984                     }
4985                 }
4986                 entry.serverNumber[index] = toserver;
4987                 entry.serverPartition[index] = topart;
4988             }
4989
4990             entry.flags |= (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
4991             MapNetworkToHost(&entry, &storeEntry);
4992             vcode =
4993                 VLDB_ReplaceEntry(pvolid, voltype, &storeEntry,
4994                                   LOCKREL_OPCODE | LOCKREL_AFSID |
4995                                   LOCKREL_TIMESTAMP);
4996             if (vcode) {
4997                 fprintf(STDERR,
4998                         "Could not update the entry for volume number %lu  \n",
4999                         (unsigned long)pvolid);
5000                 error = vcode;
5001                 goto refail;
5002             }
5003             islocked = 0;
5004             if (verbose)
5005                 EnumerateEntry(&entry);
5006         }
5007
5008
5009     }
5010   refail:
5011     if (tocall) {
5012         code = rx_EndCall(tocall, rxError);
5013         if (!error)
5014             error = code;
5015     }
5016     if (islocked) {
5017         vcode =
5018             ubik_VL_ReleaseLock(cstruct, 0, pvolid, voltype,
5019                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5020         if (vcode) {
5021             fprintf(STDERR,
5022                     "Could not release lock on the VLDB entry for the volume %lu\n",
5023                     (unsigned long)pvolid);
5024             if (!error)
5025                 error = vcode;
5026         }
5027     }
5028     if (totid) {
5029         code = AFSVolEndTrans(toconn, totid, &rcode);
5030         if (!code)
5031             code = rcode;
5032         if (code) {
5033             fprintf(STDERR, "Could not end transaction on the volume %lu \n",
5034                     (unsigned long)pvolid);
5035             if (!error)
5036                 error = code;
5037         }
5038     }
5039     if (temptid) {
5040         code = AFSVolEndTrans(toconn, temptid, &rcode);
5041         if (!code)
5042             code = rcode;
5043         if (code) {
5044             fprintf(STDERR, "Could not end transaction on the volume %lu \n",
5045                     (unsigned long)pvolid);
5046             if (!error)
5047                 error = code;
5048         }
5049     }
5050     if (tempconn)
5051         rx_DestroyConnection(tempconn);
5052     if (toconn)
5053         rx_DestroyConnection(toconn);
5054     PrintError("", error);
5055     return error;
5056 }
5057
5058 int
5059 UV_RestoreVolume(afs_uint32 toserver, afs_int32 topart, afs_uint32 tovolid,
5060                  char tovolname[], int flags,
5061                  afs_int32(*WriteData) (struct rx_call *, void *),
5062                  void *rock)
5063 {
5064     return UV_RestoreVolume2(toserver, topart, tovolid, 0, tovolname, flags,
5065                              WriteData, rock);
5066 }
5067
5068
5069 /*unlocks the vldb entry associated with <volid> */
5070 int
5071 UV_LockRelease(afs_uint32 volid)
5072 {
5073     afs_int32 vcode;
5074
5075     VPRINT("Binding to the VLDB server\n");
5076     vcode =
5077         ubik_VL_ReleaseLock(cstruct, 0, volid, -1,
5078                   LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5079     if (vcode) {
5080         fprintf(STDERR,
5081                 "Could not unlock the entry for volume number %lu in VLDB \n",
5082                 (unsigned long)volid);
5083         PrintError("", vcode);
5084         return (vcode);
5085     }
5086     VPRINT("VLDB updated\n");
5087     return 0;
5088
5089 }
5090
5091 /* old interface to add rosites */
5092 int
5093 UV_AddSite(afs_uint32 server, afs_int32 part, afs_uint32 volid,
5094            afs_int32 valid)
5095 {
5096     return UV_AddSite2(server, part, volid, 0, valid);
5097 }
5098
5099 /*adds <server> and <part> as a readonly replication site for <volid>
5100 *in vldb */
5101 int
5102 UV_AddSite2(afs_uint32 server, afs_int32 part, afs_uint32 volid,
5103             afs_uint32 rovolid, afs_int32 valid)
5104 {
5105     int j, nro = 0, islocked = 0;
5106     struct nvldbentry entry, storeEntry, entry2;
5107     afs_int32 vcode, error = 0;
5108     char apartName[10];
5109
5110     error = ubik_VL_SetLock(cstruct, 0, volid, RWVOL, VLOP_ADDSITE);
5111     if (error) {
5112         fprintf(STDERR,
5113                 " Could not lock the VLDB entry for the volume %lu \n",
5114                 (unsigned long)volid);
5115         goto asfail;
5116     }
5117     islocked = 1;
5118
5119     error = VLDB_GetEntryByID(volid, RWVOL, &entry);
5120     if (error) {
5121         fprintf(STDERR,
5122                 "Could not fetch the VLDB entry for volume number %lu  \n",
5123                 (unsigned long)volid);
5124         goto asfail;
5125
5126     }
5127     if (!ISNAMEVALID(entry.name)) {
5128         fprintf(STDERR,
5129                 "Volume name %s is too long, rename before adding site\n",
5130                 entry.name);
5131         error = VOLSERBADOP;
5132         goto asfail;
5133     }
5134     MapHostToNetwork(&entry);
5135
5136     /* See if it's too many entries */
5137     if (entry.nServers >= NMAXNSERVERS) {
5138         fprintf(STDERR, "Total number of entries will exceed %u\n",
5139                 NMAXNSERVERS);
5140         error = VOLSERBADOP;
5141         goto asfail;
5142     }
5143
5144     /* See if it's on the same server */
5145     for (j = 0; j < entry.nServers; j++) {
5146         if (entry.serverFlags[j] & ITSROVOL) {
5147             nro++;
5148             if (VLDB_IsSameAddrs(server, entry.serverNumber[j], &error)) {
5149                 if (error) {
5150                     fprintf(STDERR,
5151                             "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
5152                             server, error);
5153                 } else {
5154                     MapPartIdIntoName(entry.serverPartition[j], apartName);
5155                     fprintf(STDERR,
5156                             "RO already exists on partition %s. Multiple ROs on a single server aren't allowed\n",
5157                             apartName);
5158                     error = VOLSERBADOP;
5159                 }
5160                 goto asfail;
5161             }
5162         }
5163     }
5164
5165     /* See if it's too many RO sites - leave one for the RW */
5166     if (nro >= NMAXNSERVERS - 1) {
5167         fprintf(STDERR, "Total number of sites will exceed %u\n",
5168                 NMAXNSERVERS - 1);
5169         error = VOLSERBADOP;
5170         goto asfail;
5171     }
5172
5173     /* if rovolid == 0, we leave the RO volume id alone. If the volume doesn't
5174      * have an RO volid at this point (i.e. entry.volumeId[ROVOL] ==
5175      * INVALID_BID) and we leave it alone, it gets an RO volid at release-time.
5176      */
5177     if (rovolid) {
5178         if (entry.volumeId[ROVOL] == INVALID_BID) {
5179             vcode = VLDB_GetEntryByID(rovolid, -1, &entry2);
5180             if (!vcode) {
5181                 fprintf(STDERR, "Volume ID %d already exists\n", rovolid);
5182                 return VVOLEXISTS;
5183             }
5184             VPRINT1("Using RO volume id %d.\n", rovolid);
5185             entry.volumeId[ROVOL] = rovolid;
5186         } else {
5187             fprintf(STDERR, "Ignoring given RO id %d, since volume already has RO id %d\n",
5188                 rovolid, entry.volumeId[ROVOL]);
5189         }
5190     }
5191
5192     VPRINT("Adding a new site ...");
5193     entry.serverNumber[entry.nServers] = server;
5194     entry.serverPartition[entry.nServers] = part;
5195     if (!valid) {
5196         entry.serverFlags[entry.nServers] = (ITSROVOL | RO_DONTUSE);
5197     } else {
5198         entry.serverFlags[entry.nServers] = (ITSROVOL);
5199     }
5200     entry.nServers++;
5201
5202     MapNetworkToHost(&entry, &storeEntry);
5203     error =
5204         VLDB_ReplaceEntry(volid, RWVOL, &storeEntry,
5205                           LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5206     if (error) {
5207         fprintf(STDERR, "Could not update entry for volume %lu \n",
5208                 (unsigned long)volid);
5209         goto asfail;
5210     }
5211     islocked = 0;
5212     VDONE;
5213
5214   asfail:
5215     if (islocked) {
5216         vcode =
5217             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5218                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5219         if (vcode) {
5220             fprintf(STDERR,
5221                     "Could not release lock on volume entry for %lu \n",
5222                     (unsigned long)volid);
5223             PrintError("", vcode);
5224         }
5225     }
5226
5227     PrintError("", error);
5228     return error;
5229 }
5230
5231 /*removes <server> <part> as read only site for <volid> from the vldb */
5232 int
5233 UV_RemoveSite(afs_uint32 server, afs_int32 part, afs_uint32 volid)
5234 {
5235     afs_int32 vcode;
5236     struct nvldbentry entry, storeEntry;
5237
5238     vcode = ubik_VL_SetLock(cstruct, 0, volid, RWVOL, VLOP_ADDSITE);
5239     if (vcode) {
5240         fprintf(STDERR, " Could not lock the VLDB entry for volume %lu \n",
5241                 (unsigned long)volid);
5242         PrintError("", vcode);
5243         return (vcode);
5244     }
5245     vcode = VLDB_GetEntryByID(volid, RWVOL, &entry);
5246     if (vcode) {
5247         fprintf(STDERR,
5248                 "Could not fetch the entry for volume number %lu from VLDB \n",
5249                 (unsigned long)volid);
5250         PrintError("", vcode);
5251         return (vcode);
5252     }
5253     MapHostToNetwork(&entry);
5254     if (!Lp_ROMatch(server, part, &entry)) {
5255         /*this site doesnot exist  */
5256         fprintf(STDERR, "This site is not a replication site \n");
5257         vcode =
5258             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5259                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5260         if (vcode) {
5261             fprintf(STDERR, "Could not update entry for volume %lu \n",
5262                     (unsigned long)volid);
5263             PrintError("", vcode);
5264             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5265                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5266             return (vcode);
5267         }
5268         return VOLSERBADOP;
5269     } else {                    /*remove the rep site */
5270         Lp_SetROValue(&entry, server, part, 0, 0);
5271         entry.nServers--;
5272         if ((entry.nServers == 1) && (entry.flags & RW_EXISTS))
5273             entry.flags &= ~RO_EXISTS;
5274         if (entry.nServers < 1) {       /*this is the last ref */
5275             VPRINT1("Deleting the VLDB entry for %u ...", volid);
5276             fflush(STDOUT);
5277             vcode = ubik_VL_DeleteEntry(cstruct, 0, volid, ROVOL);
5278             if (vcode) {
5279                 fprintf(STDERR,
5280                         "Could not delete VLDB entry for volume %lu \n",
5281                         (unsigned long)volid);
5282                 PrintError("", vcode);
5283                 return (vcode);
5284             }
5285             VDONE;
5286         }
5287         MapNetworkToHost(&entry, &storeEntry);
5288         fprintf(STDOUT, "Deleting the replication site for volume %lu ...",
5289                 (unsigned long)volid);
5290         fflush(STDOUT);
5291         vcode =
5292             VLDB_ReplaceEntry(volid, RWVOL, &storeEntry,
5293                               LOCKREL_OPCODE | LOCKREL_AFSID |
5294                               LOCKREL_TIMESTAMP);
5295         if (vcode) {
5296             fprintf(STDERR,
5297                     "Could not release lock on volume entry for %lu \n",
5298                     (unsigned long)volid);
5299             PrintError("", vcode);
5300             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5301                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5302             return (vcode);
5303         }
5304         VDONE;
5305     }
5306     return 0;
5307 }
5308
5309 /*sets <server> <part> as read/write site for <volid> in the vldb */
5310 int
5311 UV_ChangeLocation(afs_uint32 server, afs_int32 part, afs_uint32 volid)
5312 {
5313     afs_int32 vcode;
5314     struct nvldbentry entry, storeEntry;
5315     int index;
5316
5317     vcode = ubik_VL_SetLock(cstruct, 0, volid, RWVOL, VLOP_ADDSITE);
5318     if (vcode) {
5319         fprintf(STDERR, " Could not lock the VLDB entry for volume %lu \n",
5320                 (unsigned long)volid);
5321         PrintError("", vcode);
5322         return (vcode);
5323     }
5324     vcode = VLDB_GetEntryByID(volid, RWVOL, &entry);
5325     if (vcode) {
5326         fprintf(STDERR,
5327                 "Could not fetch the entry for volume number %lu from VLDB \n",
5328                 (unsigned long)volid);
5329         PrintError("", vcode);
5330         return (vcode);
5331     }
5332     MapHostToNetwork(&entry);
5333     index = Lp_GetRwIndex(&entry);
5334     if (index < 0) {
5335         /* no RW site exists  */
5336         fprintf(STDERR, "No existing RW site for volume %lu",
5337                 (unsigned long)volid);
5338         vcode =
5339             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5340                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5341         if (vcode) {
5342             fprintf(STDERR,
5343                     "Could not release lock on entry for volume %lu \n",
5344                     (unsigned long)volid);
5345             PrintError("", vcode);
5346             return (vcode);
5347         }
5348         return VOLSERBADOP;
5349     } else {                    /* change the RW site */
5350         entry.serverNumber[index] = server;
5351         entry.serverPartition[index] = part;
5352         MapNetworkToHost(&entry, &storeEntry);
5353         vcode =
5354             VLDB_ReplaceEntry(volid, RWVOL, &storeEntry,
5355                               LOCKREL_OPCODE | LOCKREL_AFSID |
5356                               LOCKREL_TIMESTAMP);
5357         if (vcode) {
5358             fprintf(STDERR, "Could not update entry for volume %lu \n",
5359                     (unsigned long)volid);
5360             PrintError("", vcode);
5361             ubik_VL_ReleaseLock(cstruct, 0, volid, RWVOL,
5362                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
5363             return (vcode);
5364         }
5365         VDONE;
5366     }
5367     return 0;
5368 }
5369
5370 /*list all the partitions on <aserver> */
5371 int
5372 UV_ListPartitions(afs_uint32 aserver, struct partList *ptrPartList,
5373                   afs_int32 * cntp)
5374 {
5375     struct rx_connection *aconn;
5376     struct pIDs partIds;
5377     struct partEntries partEnts;
5378     int i, j = 0, code;
5379
5380     *cntp = 0;
5381     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
5382
5383     partEnts.partEntries_len = 0;
5384     partEnts.partEntries_val = NULL;
5385     code = AFSVolXListPartitions(aconn, &partEnts);     /* this is available only on new servers */
5386     if (code == RXGEN_OPCODE) {
5387         for (i = 0; i < 26; i++)        /* try old interface */
5388             partIds.partIds[i] = -1;
5389         code = AFSVolListPartitions(aconn, &partIds);
5390         if (!code) {
5391             for (i = 0; i < 26; i++) {
5392                 if ((partIds.partIds[i]) != -1) {
5393                     ptrPartList->partId[j] = partIds.partIds[i];
5394                     ptrPartList->partFlags[j] = PARTVALID;
5395                     j++;
5396                 } else
5397                     ptrPartList->partFlags[i] = 0;
5398             }
5399             *cntp = j;
5400         }
5401     } else if (!code) {
5402         *cntp = partEnts.partEntries_len;
5403         if (*cntp > VOLMAXPARTS) {
5404             fprintf(STDERR,
5405                     "Warning: number of partitions on the server too high %d (process only %d)\n",
5406                     *cntp, VOLMAXPARTS);
5407             *cntp = VOLMAXPARTS;
5408         }
5409         for (i = 0; i < *cntp; i++) {
5410             ptrPartList->partId[i] = partEnts.partEntries_val[i];
5411             ptrPartList->partFlags[i] = PARTVALID;
5412         }
5413         free(partEnts.partEntries_val);
5414     }
5415
5416    /* out: */
5417     if (code)
5418         fprintf(STDERR,
5419                 "Could not fetch the list of partitions from the server\n");
5420     PrintError("", code);
5421     if (aconn)
5422         rx_DestroyConnection(aconn);
5423     return code;
5424 }
5425
5426
5427 /*zap the list of volumes specified by volPtrArray (the volCloneId field).
5428  This is used by the backup system */
5429 int
5430 UV_ZapVolumeClones(afs_uint32 aserver, afs_int32 apart,
5431                    struct volDescription *volPtr, afs_int32 arraySize)
5432 {
5433     struct rx_connection *aconn;
5434     struct volDescription *curPtr;
5435     int curPos;
5436     afs_int32 code = 0;
5437     afs_int32 success = 1;
5438
5439     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
5440     curPos = 0;
5441     for (curPtr = volPtr; curPos < arraySize; curPtr++) {
5442         if (curPtr->volFlags & CLONEVALID) {
5443             curPtr->volFlags &= ~CLONEZAPPED;
5444             success = 1;
5445
5446             code = DoVolDelete(aconn, curPtr->volCloneId, apart,
5447                                "clone", 0, NULL, NULL);
5448             if (code)
5449                 success = 0;
5450
5451             if (success)
5452                 curPtr->volFlags |= CLONEZAPPED;
5453             if (!success)
5454                 fprintf(STDERR, "Could not zap volume %lu\n",
5455                         (unsigned long)curPtr->volCloneId);
5456             if (success)
5457                 VPRINT2("Clone of %s %u deleted\n", curPtr->volName,
5458                         curPtr->volCloneId);
5459             curPos++;
5460         }
5461     }
5462     if (aconn)
5463         rx_DestroyConnection(aconn);
5464     return 0;
5465 }
5466
5467 /*return a list of clones of the volumes specified by volPtrArray. Used by the
5468  backup system */
5469 int
5470 UV_GenerateVolumeClones(afs_uint32 aserver, afs_int32 apart,
5471                         struct volDescription *volPtr, afs_int32 arraySize)
5472 {
5473     struct rx_connection *aconn;
5474     struct volDescription *curPtr;
5475     int curPos;
5476     afs_int32 code = 0;
5477     afs_int32 rcode = 0;
5478     afs_int32 tid;
5479     int reuseCloneId = 0;
5480     afs_uint32 curCloneId = 0;
5481     char cloneName[256];        /*max vol name */
5482
5483     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
5484     curPos = 0;
5485     if ((volPtr->volFlags & REUSECLONEID) && (volPtr->volFlags & ENTRYVALID))
5486         reuseCloneId = 1;
5487     else {                      /*get a bunch of id's from vldb */
5488         code =
5489             ubik_VL_GetNewVolumeId(cstruct, 0, arraySize, &curCloneId);
5490         if (code) {
5491             fprintf(STDERR, "Could not get ID's for the clone from VLDB\n");
5492             PrintError("", code);
5493             return code;
5494         }
5495     }
5496
5497     for (curPtr = volPtr; curPos < arraySize; curPtr++) {
5498         if (curPtr->volFlags & ENTRYVALID) {
5499
5500             curPtr->volFlags |= CLONEVALID;
5501             /*make a clone of curParentId and record as curPtr->volCloneId */
5502             code =
5503                 AFSVolTransCreate_retry(aconn, curPtr->volId, apart, ITOffline,
5504                                   &tid);
5505             if (code)
5506                 VPRINT2("Clone for volume %s %u failed \n", curPtr->volName,
5507                         curPtr->volId);
5508             if (code) {
5509                 curPtr->volFlags &= ~CLONEVALID;        /*cant clone */
5510                 curPos++;
5511                 continue;
5512             }
5513             if (strlen(curPtr->volName) < (VOLSER_OLDMAXVOLNAME - 9)) {
5514                 strcpy(cloneName, curPtr->volName);
5515                 strcat(cloneName, "-tmpClone-");
5516             } else
5517                 strcpy(cloneName, "-tmpClone");
5518             if (reuseCloneId) {
5519                 curPtr->volCloneId = curCloneId;
5520                 curCloneId++;
5521             }
5522
5523             code =
5524                 AFSVolClone(aconn, tid, 0, readonlyVolume, cloneName,
5525                             &(curPtr->volCloneId));
5526             if (code) {
5527                 curPtr->volFlags &= ~CLONEVALID;
5528                 curPos++;
5529                 fprintf(STDERR, "Could not clone %s due to error %lu\n",
5530                         curPtr->volName, (unsigned long)code);
5531                 code = AFSVolEndTrans(aconn, tid, &rcode);
5532                 if (code)
5533                     fprintf(STDERR, "WARNING: could not end transaction\n");
5534                 continue;
5535             }
5536             VPRINT2("********** Cloned %s temporary %u\n", cloneName,
5537                     curPtr->volCloneId);
5538             code = AFSVolEndTrans(aconn, tid, &rcode);
5539             if (code || rcode) {
5540                 curPtr->volFlags &= ~CLONEVALID;
5541                 curPos++;
5542                 continue;
5543             }
5544
5545             curPos++;
5546         }
5547     }
5548     if (aconn)
5549         rx_DestroyConnection(aconn);
5550     return 0;
5551 }
5552
5553
5554 /*list all the volumes on <aserver> and <apart>. If all = 1, then all the
5555 * relevant fields of the volume are also returned. This is a heavy weight operation.*/
5556 int
5557 UV_ListVolumes(afs_uint32 aserver, afs_int32 apart, int all,
5558                struct volintInfo **resultPtr, afs_int32 * size)
5559 {
5560     struct rx_connection *aconn;
5561     afs_int32 code = 0;
5562     volEntries volumeInfo;
5563
5564     code = 0;
5565     *size = 0;
5566     *resultPtr = (volintInfo *) 0;
5567     volumeInfo.volEntries_val = (volintInfo *) 0;       /*this hints the stub to allocate space */
5568     volumeInfo.volEntries_len = 0;
5569
5570     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
5571     code = AFSVolListVolumes(aconn, apart, all, &volumeInfo);
5572     if (code) {
5573         fprintf(STDERR,
5574                 "Could not fetch the list of volumes from the server\n");
5575     } else {
5576         *resultPtr = volumeInfo.volEntries_val;
5577         *size = volumeInfo.volEntries_len;
5578     }
5579
5580     if (aconn)
5581         rx_DestroyConnection(aconn);
5582     PrintError("", code);
5583     return code;
5584 }
5585
5586 /*------------------------------------------------------------------------
5587  * EXPORTED UV_XListVolumes
5588  *
5589  * Description:
5590  *      List the extended information for all the volumes on a particular
5591  *      File Server and partition.  We may either return the volume's ID
5592  *      or all of its extended information.
5593  *
5594  * Arguments:
5595  *      a_serverID         : Address of the File Server for which we want
5596  *                              extended volume info.
5597  *      a_partID           : Partition for which we want the extended
5598  *                              volume info.
5599  *      a_all              : If non-zero, fetch ALL the volume info,
5600  *                              otherwise just the volume ID.
5601  *      a_resultPP         : Ptr to the address of the area containing
5602  *                              the returned volume info.
5603  *      a_numEntsInResultP : Ptr for the value we set for the number of
5604  *                              entries returned.
5605  *
5606  * Returns:
5607  *      0 on success,
5608  *      Otherise, the return value of AFSVolXListVolumes.
5609  *
5610  * Environment:
5611  *      This routine is closely related to UV_ListVolumes, which returns
5612  *      only the standard level of detail on AFS volumes. It is a
5613  *      heavyweight operation, zipping through all the volume entries for
5614  *      a given server/partition.
5615  *
5616  * Side Effects:
5617  *      As advertised.
5618  *------------------------------------------------------------------------*/
5619
5620 int
5621 UV_XListVolumes(afs_uint32 a_serverID, afs_int32 a_partID, int a_all,
5622                 struct volintXInfo **a_resultPP,
5623                 afs_int32 * a_numEntsInResultP)
5624 {
5625     struct rx_connection *rxConnP;      /*Ptr to the Rx connection involved */
5626     afs_int32 code;             /*Error code to return */
5627     volXEntries volumeXInfo;    /*Area for returned extended vol info */
5628
5629     /*
5630      * Set up our error code and the area for returned extended volume info.
5631      * We set the val field to a null pointer as a hint for the stub to
5632      * allocate space.
5633      */
5634     *a_numEntsInResultP = 0;
5635     *a_resultPP = (volintXInfo *) 0;
5636     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
5637     volumeXInfo.volXEntries_len = 0;
5638
5639     /*
5640      * Bind to the Volume Server port on the File Server machine in question,
5641      * then go for it.
5642      */
5643     rxConnP = UV_Bind(a_serverID, AFSCONF_VOLUMEPORT);
5644     code = AFSVolXListVolumes(rxConnP, a_partID, a_all, &volumeXInfo);
5645     if (code)
5646         fprintf(STDERR, "[UV_XListVolumes] Couldn't fetch volume list\n");
5647     else {
5648         /*
5649          * We got the info; pull out the pointer to where the results lie
5650          * and how many entries are there.
5651          */
5652         *a_resultPP = volumeXInfo.volXEntries_val;
5653         *a_numEntsInResultP = volumeXInfo.volXEntries_len;
5654     }
5655
5656     /*
5657      * If we got an Rx connection, throw it away.
5658      */
5659     if (rxConnP)
5660         rx_DestroyConnection(rxConnP);
5661
5662     PrintError("", code);
5663     return (code);
5664 }                               /*UV_XListVolumes */
5665
5666 /* get all the information about volume <volid> on <aserver> and <apart> */
5667 int
5668 UV_ListOneVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 volid,
5669                  struct volintInfo **resultPtr)
5670 {
5671     struct rx_connection *aconn;
5672     afs_int32 code = 0;
5673     volEntries volumeInfo;
5674
5675     *resultPtr = (volintInfo *) 0;
5676     volumeInfo.volEntries_val = (volintInfo *) 0;       /*this hints the stub to allocate space */
5677     volumeInfo.volEntries_len = 0;
5678
5679     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
5680     code = AFSVolListOneVolume(aconn, apart, volid, &volumeInfo);
5681     if (code) {
5682         fprintf(STDERR,
5683                 "Could not fetch the information about volume %lu from the server\n",
5684                 (unsigned long)volid);
5685     } else {
5686         *resultPtr = volumeInfo.volEntries_val;
5687
5688     }
5689
5690     if (aconn)
5691         rx_DestroyConnection(aconn);
5692     PrintError("", code);
5693     return code;
5694 }
5695
5696 /*------------------------------------------------------------------------
5697  * EXPORTED UV_XListOneVolume
5698  *
5699  * Description:
5700  *      List the extended information for a volume on a particular File
5701  *      Server and partition.
5702  *
5703  * Arguments:
5704  *      a_serverID         : Address of the File Server for which we want
5705  *                              extended volume info.
5706  *      a_partID           : Partition for which we want the extended
5707  *                              volume info.
5708  *      a_volID            : Volume ID for which we want the info.
5709  *      a_resultPP         : Ptr to the address of the area containing
5710  *                              the returned volume info.
5711  *
5712  * Returns:
5713  *      0 on success,
5714  *      Otherise, the return value of AFSVolXListOneVolume.
5715  *
5716  * Environment:
5717  *      This routine is closely related to UV_ListOneVolume, which returns
5718  *      only the standard level of detail on the chosen AFS volume.
5719  *
5720  * Side Effects:
5721  *      As advertised.
5722  *------------------------------------------------------------------------*/
5723
5724 int
5725 UV_XListOneVolume(afs_uint32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
5726                   struct volintXInfo **a_resultPP)
5727 {
5728     struct rx_connection *rxConnP;      /*Rx connection to Volume Server */
5729     afs_int32 code;             /*Error code */
5730     volXEntries volumeXInfo;    /*Area for returned info */
5731
5732     /*
5733      * Set up our error code, and the area we're in which we are returning
5734      * the info.  Setting the val field to a null pointer tells the stub
5735      * to allocate space for us.
5736      */
5737     *a_resultPP = (volintXInfo *) 0;
5738     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
5739     volumeXInfo.volXEntries_len = 0;
5740
5741     /*
5742      * Bind to the Volume Server port on the File Server machine in question,
5743      * then go for it.
5744      */
5745     rxConnP = UV_Bind(a_serverID, AFSCONF_VOLUMEPORT);
5746     code = AFSVolXListOneVolume(rxConnP, a_partID, a_volID, &volumeXInfo);
5747     if (code)
5748         fprintf(STDERR,
5749                 "[UV_XListOneVolume] Couldn't fetch the volume information\n");
5750     else
5751         /*
5752          * We got the info; pull out the pointer to where the results lie.
5753          */
5754         *a_resultPP = volumeXInfo.volXEntries_val;
5755
5756     /*
5757      * If we got an Rx connection, throw it away.
5758      */
5759     if (rxConnP)
5760         rx_DestroyConnection(rxConnP);
5761
5762     PrintError("", code);
5763     return code;
5764 }
5765
5766 /* CheckVolume()
5767  *    Given a volume we read from a partition, check if it is
5768  *    represented in the VLDB correctly.
5769  *
5770  *    The VLDB is looked up by the RW volume id (not its name).
5771  *    The RW contains the true name of the volume (BK and RO set
5772  *       the name in the VLDB only on creation of the VLDB entry).
5773  *    We want rules strict enough that when we check all volumes
5774  *       on one partition, it does not need to be done again. IE:
5775  *       two volumes on different partitions won't constantly
5776  *       change a VLDB entry away from what the other set.
5777  *    For RW and BK volumes, we will always check the VLDB to see
5778  *       if the two exist on the server/partition. May seem redundant,
5779  *       but this is an easy check of the VLDB. IE: if the VLDB entry
5780  *       says the BK exists but no BK volume is there, we will detect
5781  *       this when we check the RW volume.
5782  *    VLDB entries are locked only when a change needs to be done.
5783  *    Output changed to look a lot like the "vos syncserv" otuput.
5784  */
5785 static afs_int32
5786 CheckVolume(volintInfo * volumeinfo, afs_uint32 aserver, afs_int32 apart,
5787             afs_int32 * modentry, afs_uint32 * maxvolid,
5788             struct nvldbentry *aentry)
5789 {
5790     int idx = 0;
5791     int j;
5792     afs_int32 code, error = 0;
5793     struct nvldbentry entry, storeEntry;
5794     char pname[10];
5795     int pass = 0, createentry, addvolume, modified, mod, doit = 1;
5796     afs_uint32 rwvolid;
5797     char hoststr[16];
5798
5799     if (modentry) {
5800         if (*modentry == 1)
5801             doit = 0;
5802         *modentry = 0;
5803     }
5804     rwvolid =
5805         ((volumeinfo->type ==
5806           RWVOL) ? volumeinfo->volid : volumeinfo->parentID);
5807
5808   retry:
5809     /* Check to see if the VLDB is ok without locking it (pass 1).
5810      * If it will change, then lock the VLDB entry, read it again,
5811      * then make the changes to it (pass 2).
5812      */
5813     if (++pass == 2) {
5814         code = ubik_VL_SetLock(cstruct, 0, rwvolid, RWVOL, VLOP_DELETE);
5815         if (code) {
5816             fprintf(STDERR, "Could not lock VLDB entry for %lu\n",
5817                     (unsigned long)rwvolid);
5818             ERROR_EXIT(code);
5819         }
5820     }
5821
5822     createentry = 0;            /* Do we need to create a VLDB entry */
5823     addvolume = 0;              /* Add this volume to the VLDB entry */
5824     modified = 0;               /* The VLDB entry was modified */
5825
5826     if (aentry) {
5827         memcpy(&entry, aentry, sizeof(entry));
5828     } else {
5829         /* Read the entry from VLDB by its RW volume id */
5830         code = VLDB_GetEntryByID(rwvolid, RWVOL, &entry);
5831         if (code) {
5832             if (code != VL_NOENT) {
5833                 fprintf(STDOUT,
5834                         "Could not retrieve the VLDB entry for volume %lu \n",
5835                         (unsigned long)rwvolid);
5836                 ERROR_EXIT(code);
5837             }
5838
5839             memset(&entry, 0, sizeof(entry));
5840             vsu_ExtractName(entry.name, volumeinfo->name);      /* Store name of RW */
5841
5842             createentry = 1;
5843         } else {
5844             MapHostToNetwork(&entry);
5845         }
5846     }
5847
5848     if (verbose && (pass == 1)) {
5849         fprintf(STDOUT, "_______________________________\n");
5850         fprintf(STDOUT, "\n-- status before -- \n");
5851         if (createentry) {
5852             fprintf(STDOUT, "\n**does not exist**\n");
5853         } else {
5854             if ((entry.flags & RW_EXISTS) || (entry.flags & RO_EXISTS)
5855                 || (entry.flags & BACK_EXISTS))
5856                 EnumerateEntry(&entry);
5857         }
5858         fprintf(STDOUT, "\n");
5859     }
5860
5861     if (volumeinfo->type == RWVOL) {    /* RW volume exists */
5862         if (createentry) {
5863             idx = 0;
5864             entry.nServers = 1;
5865             addvolume++;
5866         } else {
5867             /* Check existence of RW and BK volumes */
5868             code = CheckVldbRWBK(&entry, &mod);
5869             if (code)
5870                 ERROR_EXIT(code);
5871             if (mod)
5872                 modified++;
5873
5874             idx = Lp_GetRwIndex(&entry);
5875             if (idx == -1) {    /* RW index not found in the VLDB entry */
5876                 idx = entry.nServers;   /* put it into next index */
5877                 entry.nServers++;
5878                 addvolume++;
5879             } else {            /* RW index found in the VLDB entry. */
5880                 /* Verify if this volume's location matches where the VLDB says it is */
5881                 if (!Lp_Match(aserver, apart, &entry)) {
5882                     if (entry.flags & RW_EXISTS) {
5883                         /* The RW volume exists elsewhere - report this one a duplicate */
5884                         if (pass == 1) {
5885                             MapPartIdIntoName(apart, pname);
5886                             fprintf(STDERR,
5887                                     "*** Warning: Orphaned RW volume %lu exists on %s %s\n",
5888                                     (unsigned long)rwvolid,
5889                                     noresolve ?
5890                                     afs_inet_ntoa_r(aserver, hoststr) :
5891                                     hostutil_GetNameByINet(aserver), pname);
5892                             MapPartIdIntoName(entry.serverPartition[idx],
5893                                               pname);
5894                             fprintf(STDERR,
5895                                     "    VLDB reports RW volume %lu exists on %s %s\n",
5896                                     (unsigned long)rwvolid,
5897                                     noresolve ?
5898                                     afs_inet_ntoa_r(entry.serverNumber[idx], hoststr) :
5899                                     hostutil_GetNameByINet(entry.
5900                                                            serverNumber[idx]),
5901                                     pname);
5902                         }
5903                     } else {
5904                         /* The RW volume does not exist - have VLDB point to this one */
5905                         addvolume++;
5906
5907                         /* Check for orphaned BK volume on old partition */
5908                         if (entry.flags & BACK_EXISTS) {
5909                             if (pass == 1) {
5910                                 MapPartIdIntoName(entry.serverPartition[idx],
5911                                                   pname);
5912                                 fprintf(STDERR,
5913                                         "*** Warning: Orphaned BK volume %u exists on %s %s\n",
5914                                         entry.volumeId[BACKVOL],
5915                                         noresolve ?
5916                                         afs_inet_ntoa_r(entry.serverNumber[idx], hoststr) :
5917                                         hostutil_GetNameByINet(entry.
5918                                                                serverNumber
5919                                                                [idx]), pname);
5920                                 MapPartIdIntoName(apart, pname);
5921                                 fprintf(STDERR,
5922                                         "    VLDB reports its RW volume %lu exists on %s %s\n",
5923                                         (unsigned long)rwvolid,
5924                                         noresolve ?
5925                                         afs_inet_ntoa_r(aserver, hoststr) :
5926                                         hostutil_GetNameByINet(aserver),
5927                                         pname);
5928                             }
5929                         }
5930                     }
5931                 } else {
5932                     /* Volume location matches the VLDB location */
5933                     if ((volumeinfo->backupID && !entry.volumeId[BACKVOL])
5934                         || (volumeinfo->cloneID && !entry.volumeId[ROVOL])
5935                         ||
5936                         (strncmp
5937                          (entry.name, volumeinfo->name,
5938                           VOLSER_OLDMAXVOLNAME) != 0)) {
5939                         addvolume++;
5940                     }
5941                 }
5942             }
5943         }
5944
5945         if (addvolume) {
5946             entry.flags |= RW_EXISTS;
5947             entry.volumeId[RWVOL] = rwvolid;
5948             if (!entry.volumeId[BACKVOL])
5949                 entry.volumeId[BACKVOL] = volumeinfo->backupID;
5950             if (!entry.volumeId[ROVOL])
5951                 entry.volumeId[ROVOL] = volumeinfo->cloneID;
5952
5953             entry.serverFlags[idx] = ITSRWVOL;
5954             entry.serverNumber[idx] = aserver;
5955             entry.serverPartition[idx] = apart;
5956             strncpy(entry.name, volumeinfo->name, VOLSER_OLDMAXVOLNAME);
5957
5958             modified++;
5959
5960             /* One last check - to update BK if need to */
5961             code = CheckVldbRWBK(&entry, &mod);
5962             if (code)
5963                 ERROR_EXIT(code);
5964             if (mod)
5965                 modified++;
5966         }
5967     }
5968
5969     else if (volumeinfo->type == BACKVOL) {     /* A BK volume */
5970         if (createentry) {
5971             idx = 0;
5972             entry.nServers = 1;
5973             addvolume++;
5974         } else {
5975             /* Check existence of RW and BK volumes */
5976             code = CheckVldbRWBK(&entry, &mod);
5977             if (code)
5978                 ERROR_EXIT(code);
5979             if (mod)
5980                 modified++;
5981
5982             idx = Lp_GetRwIndex(&entry);
5983             if (idx == -1) {    /* RW index not found in the VLDB entry */
5984                 idx = entry.nServers;   /* Put it into next index */
5985                 entry.nServers++;
5986                 addvolume++;
5987             } else {            /* RW index found in the VLDB entry */
5988                 /* Verify if this volume's location matches where the VLDB says it is */
5989                 if (!Lp_Match(aserver, apart, &entry)) {
5990                     /* VLDB says RW and/or BK is elsewhere - report this BK volume orphaned */
5991                     if (pass == 1) {
5992                         MapPartIdIntoName(apart, pname);
5993                         fprintf(STDERR,
5994                                 "*** Warning: Orphaned BK volume %lu exists on %s %s\n",
5995                                 (unsigned long)volumeinfo->volid,
5996                                 noresolve ?
5997                                 afs_inet_ntoa_r(aserver, hoststr) :
5998                                 hostutil_GetNameByINet(aserver), pname);
5999                         MapPartIdIntoName(entry.serverPartition[idx], pname);
6000                         fprintf(STDERR,
6001                                 "    VLDB reports its RW/BK volume %lu exists on %s %s\n",
6002                                 (unsigned long)rwvolid,
6003                                 noresolve ?
6004                                 afs_inet_ntoa_r(entry.serverNumber[idx], hoststr) :
6005                                 hostutil_GetNameByINet(entry.
6006                                                        serverNumber[idx]),
6007                                 pname);
6008                     }
6009                 } else {
6010                     if (volumeinfo->volid != entry.volumeId[BACKVOL]) {
6011                         if (!(entry.flags & BACK_EXISTS)) {
6012                             addvolume++;
6013                         } else if (volumeinfo->volid >
6014                                    entry.volumeId[BACKVOL]) {
6015                             addvolume++;
6016
6017                             if (pass == 1) {
6018                                 MapPartIdIntoName(entry.serverPartition[idx],
6019                                                   pname);
6020                                 fprintf(STDERR,
6021                                         "*** Warning: Orphaned BK volume %u exists on %s %s\n",
6022                                         entry.volumeId[BACKVOL],
6023                                         noresolve ?
6024                                         afs_inet_ntoa_r(aserver, hoststr) :
6025                                         hostutil_GetNameByINet(aserver),
6026                                         pname);
6027                                 fprintf(STDERR,
6028                                         "    VLDB reports its BK volume ID is %lu\n",
6029                                         (unsigned long)volumeinfo->volid);
6030                             }
6031                         } else {
6032                             if (pass == 1) {
6033                                 MapPartIdIntoName(entry.serverPartition[idx],
6034                                                   pname);
6035                                 fprintf(STDERR,
6036                                         "*** Warning: Orphaned BK volume %lu exists on %s %s\n",
6037                                         (unsigned long)volumeinfo->volid,
6038                                         noresolve ?
6039                                         afs_inet_ntoa_r(aserver, hoststr) :
6040                                         hostutil_GetNameByINet(aserver),
6041                                         pname);
6042                                 fprintf(STDERR,
6043                                         "    VLDB reports its BK volume ID is %u\n",
6044                                         entry.volumeId[BACKVOL]);
6045                             }
6046                         }
6047                     } else if (!entry.volumeId[BACKVOL]) {
6048                         addvolume++;
6049                     }
6050                 }
6051             }
6052         }
6053         if (addvolume) {
6054             entry.flags |= BACK_EXISTS;
6055             entry.volumeId[RWVOL] = rwvolid;
6056             entry.volumeId[BACKVOL] = volumeinfo->volid;
6057
6058             entry.serverNumber[idx] = aserver;
6059             entry.serverPartition[idx] = apart;
6060             entry.serverFlags[idx] = ITSBACKVOL;
6061
6062             modified++;
6063         }
6064     }
6065
6066     else if (volumeinfo->type == ROVOL) {       /* A RO volume */
6067         if (volumeinfo->volid == entry.volumeId[ROVOL]) {
6068             /* This is a quick check to see if the RO entry exists in the
6069              * VLDB so we avoid the CheckVldbRO() call (which checks if each
6070              * RO volume listed in the VLDB exists).
6071              */
6072             idx = Lp_ROMatch(aserver, apart, &entry) - 1;
6073             if (idx == -1) {
6074                 idx = entry.nServers;
6075                 entry.nServers++;
6076                 addvolume++;
6077             } else {
6078                 if (!(entry.flags & RO_EXISTS)) {
6079                     addvolume++;
6080                 }
6081             }
6082         } else {
6083             /* Before we correct the VLDB entry, make sure all the
6084              * ROs listed in the VLDB exist.
6085              */
6086             code = CheckVldbRO(&entry, &mod);
6087             if (code)
6088                 ERROR_EXIT(code);
6089             if (mod)
6090                 modified++;
6091
6092             if (!(entry.flags & RO_EXISTS)) {
6093                 /* No RO exists in the VLDB entry - add this one */
6094                 idx = entry.nServers;
6095                 entry.nServers++;
6096                 addvolume++;
6097             } else if (volumeinfo->volid > entry.volumeId[ROVOL]) {
6098                 /* The volume headers's RO ID does not match that in the VLDB entry,
6099                  * and the vol hdr's ID is greater (implies more recent). So delete
6100                  * all the RO volumes listed in VLDB entry and add this volume.
6101                  */
6102                 for (j = 0; j < entry.nServers; j++) {
6103                     if (entry.serverFlags[j] & ITSROVOL) {
6104                         /* Verify this volume exists and print message we are orphaning it */
6105                         if (pass == 1) {
6106                             MapPartIdIntoName(apart, pname);
6107                             fprintf(STDERR,
6108                                     "*** Warning: Orphaned RO volume %u exists on %s %s\n",
6109                                     entry.volumeId[ROVOL],
6110                                     noresolve ?
6111                                     afs_inet_ntoa_r(entry.serverNumber[j], hoststr) :
6112                                     hostutil_GetNameByINet(entry.
6113                                                            serverNumber[j]),
6114                                     pname);
6115                             fprintf(STDERR,
6116                                     "    VLDB reports its RO volume ID is %lu\n",
6117                                     (unsigned long)volumeinfo->volid);
6118                         }
6119
6120                         Lp_SetRWValue(&entry, entry.serverNumber[idx],
6121                                       entry.serverPartition[idx], 0L, 0L);
6122                         entry.nServers--;
6123                         modified++;
6124                         j--;
6125                     }
6126                 }
6127
6128                 idx = entry.nServers;
6129                 entry.nServers++;
6130                 addvolume++;
6131             } else if (volumeinfo->volid < entry.volumeId[ROVOL]) {
6132                 /* The volume headers's RO ID does not match that in the VLDB entry,
6133                  * and the vol hdr's ID is lower (implies its older). So orphan it.
6134                  */
6135                 if (pass == 1) {
6136                     MapPartIdIntoName(apart, pname);
6137                     fprintf(STDERR,
6138                             "*** Warning: Orphaned RO volume %lu exists on %s %s\n",
6139                             (unsigned long)volumeinfo->volid,
6140                             noresolve ?
6141                             afs_inet_ntoa_r(aserver, hoststr) :
6142                             hostutil_GetNameByINet(aserver), pname);
6143                     fprintf(STDERR,
6144                             "    VLDB reports its RO volume ID is %u\n",
6145                             entry.volumeId[ROVOL]);
6146                 }
6147             } else {
6148                 /* The RO volume ID in the volume header match that in the VLDB entry,
6149                  * and there exist RO volumes in the VLDB entry. See if any of them
6150                  * are this one. If not, then we add it.
6151                  */
6152                 idx = Lp_ROMatch(aserver, apart, &entry) - 1;
6153                 if (idx == -1) {
6154                     idx = entry.nServers;
6155                     entry.nServers++;
6156                     addvolume++;
6157                 }
6158             }
6159         }
6160
6161         if (addvolume) {
6162             entry.flags |= RO_EXISTS;
6163             entry.volumeId[RWVOL] = rwvolid;
6164             entry.volumeId[ROVOL] = volumeinfo->volid;
6165
6166             entry.serverNumber[idx] = aserver;
6167             entry.serverPartition[idx] = apart;
6168             entry.serverFlags[idx] = ITSROVOL;
6169
6170             modified++;
6171         }
6172     }
6173
6174     /* Remember largest volume id */
6175     if (entry.volumeId[ROVOL] > *maxvolid)
6176         *maxvolid = entry.volumeId[ROVOL];
6177     if (entry.volumeId[BACKVOL] > *maxvolid)
6178         *maxvolid = entry.volumeId[BACKVOL];
6179     if (entry.volumeId[RWVOL] > *maxvolid)
6180         *maxvolid = entry.volumeId[RWVOL];
6181
6182     if (modified && doit) {
6183         MapNetworkToHost(&entry, &storeEntry);
6184
6185         if (createentry) {
6186             code = VLDB_CreateEntry(&storeEntry);
6187             if (code) {
6188                 fprintf(STDOUT,
6189                         "Could not create a VLDB entry for the volume %lu\n",
6190                         (unsigned long)rwvolid);
6191                 ERROR_EXIT(code);
6192             }
6193         } else {
6194             if (pass == 1)
6195                 goto retry;
6196             code =
6197                 VLDB_ReplaceEntry(rwvolid, RWVOL, &storeEntry,
6198                                   LOCKREL_OPCODE | LOCKREL_AFSID |
6199                                   LOCKREL_TIMESTAMP);
6200             if (code) {
6201                 fprintf(STDERR, "Could not update entry for %lu\n",
6202                         (unsigned long)rwvolid);
6203                 ERROR_EXIT(code);
6204             }
6205         }
6206     } else if (pass == 2) {
6207         code =
6208             ubik_VL_ReleaseLock(cstruct, 0, rwvolid, RWVOL,
6209                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
6210         if (code) {
6211             PrintError("Could not unlock VLDB entry ", code);
6212         }
6213     }
6214
6215     if (modified && modentry) {
6216         *modentry = 1;
6217     }
6218
6219     if (aentry) {
6220         memcpy(aentry, &entry, sizeof(entry));
6221     }
6222
6223     if (verbose) {
6224         fprintf(STDOUT, "-- status after --\n");
6225         if (modified)
6226             EnumerateEntry(&entry);
6227         else
6228             fprintf(STDOUT, "\n**no change**\n");
6229     }
6230
6231   error_exit:
6232     VPRINT("\n_______________________________\n");
6233     return (error);
6234 }
6235
6236 static int
6237 sortVolumes(const void *a, const void *b)
6238 {
6239     volintInfo *v1 = (volintInfo *) a;
6240     volintInfo *v2 = (volintInfo *) b;
6241     afs_uint32 rwvolid1, rwvolid2;
6242
6243     rwvolid1 = ((v1->type == RWVOL) ? v1->volid : v1->parentID);
6244     rwvolid2 = ((v2->type == RWVOL) ? v2->volid : v2->parentID);
6245
6246     if (rwvolid1 > rwvolid2)
6247         return -1;              /* lower RW id goes first */
6248     if (rwvolid1 < rwvolid2)
6249         return 1;
6250
6251     if (v1->type == RWVOL)
6252         return -1;              /* RW vols go first */
6253     if (v2->type == RWVOL)
6254         return 1;
6255
6256     if ((v1->type == BACKVOL) && (v2->type == ROVOL))
6257         return -1;              /* BK vols next */
6258     if ((v1->type == ROVOL) && (v2->type == BACKVOL))
6259         return 1;
6260
6261     if (v1->volid < v2->volid)
6262         return 1;               /* larger volids first */
6263     if (v1->volid > v2->volid)
6264         return -1;
6265     return 0;
6266 }
6267
6268 /* UV_SyncVolume()
6269  *      Synchronise <aserver> <apart>(if flags = 1) <avolid>.
6270  *      Synchronize an individual volume against a sever and partition.
6271  *      Checks the VLDB entry (similar to syncserv) as well as checks
6272  *      if the volume exists on specified servers (similar to syncvldb).
6273  */
6274 int
6275 UV_SyncVolume(afs_uint32 aserver, afs_int32 apart, char *avolname, int flags)
6276 {
6277     struct rx_connection *aconn = 0;
6278     afs_int32 j, k, code, vcode, error = 0;
6279     afs_int32 tverbose;
6280     afs_int32 mod, modified = 0, deleted = 0;
6281     struct nvldbentry vldbentry;
6282     afs_uint32 volumeid = 0;
6283     volEntries volumeInfo;
6284     struct partList PartList;
6285     afs_int32 pcnt;
6286     afs_uint32 maxvolid = 0;
6287
6288     volumeInfo.volEntries_val = (volintInfo *) 0;
6289     volumeInfo.volEntries_len = 0;
6290
6291     /* Turn verbose logging off and do our own verbose logging */
6292     /* tverbose must be set before we call ERROR_EXIT() */
6293
6294     tverbose = verbose;
6295     if (flags & 2)
6296         tverbose = 1;
6297     verbose = 0;
6298
6299     if (!aserver && (flags & 1)) {
6300         /* fprintf(STDERR,"Partition option requires a server option\n"); */
6301         ERROR_EXIT(EINVAL);
6302     }
6303
6304     /* Read the VLDB entry */
6305     vcode = VLDB_GetEntryByName(avolname, &vldbentry);
6306     if (vcode && (vcode != VL_NOENT)) {
6307         fprintf(STDERR, "Could not access the VLDB for volume %s\n",
6308                 avolname);
6309         ERROR_EXIT(vcode);
6310     } else if (!vcode) {
6311         MapHostToNetwork(&vldbentry);
6312     }
6313
6314     if (tverbose) {
6315         fprintf(STDOUT, "Processing VLDB entry %s ...\n", avolname);
6316         fprintf(STDOUT, "_______________________________\n");
6317         fprintf(STDOUT, "\n-- status before -- \n");
6318         if (vcode) {
6319             fprintf(STDOUT, "\n**does not exist**\n");
6320         } else {
6321             if ((vldbentry.flags & RW_EXISTS) || (vldbentry.flags & RO_EXISTS)
6322                 || (vldbentry.flags & BACK_EXISTS))
6323                 EnumerateEntry(&vldbentry);
6324         }
6325         fprintf(STDOUT, "\n");
6326     }
6327
6328     /* Verify that all of the VLDB entries exist on the repective servers
6329      * and partitions (this does not require that avolname be a volume ID).
6330      * Equivalent to a syncserv.
6331      */
6332     if (!vcode) {
6333         /* Tell CheckVldb not to update if appropriate */
6334         if (flags & 2)
6335             mod = 1;
6336         else
6337             mod = 0;
6338         code = CheckVldb(&vldbentry, &mod, &deleted);
6339         if (code) {
6340             fprintf(STDERR, "Could not process VLDB entry for volume %s\n",
6341                     vldbentry.name);
6342             ERROR_EXIT(code);
6343         }
6344         if (mod)
6345             modified++;
6346     }
6347
6348     /* If aserver is given, we will search for the desired volume on it */
6349     if (aserver) {
6350         /* Generate array of partitions on the server that we will check */
6351         if (!(flags & 1)) {
6352             code = UV_ListPartitions(aserver, &PartList, &pcnt);
6353             if (code) {
6354                 fprintf(STDERR,
6355                         "Could not fetch the list of partitions from the server\n");
6356                 ERROR_EXIT(code);
6357             }
6358         } else {
6359             PartList.partId[0] = apart;
6360             pcnt = 1;
6361         }
6362
6363         aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
6364
6365         /* If a volume ID were given, search for it on each partition */
6366         if ((volumeid = atol(avolname))) {
6367             for (j = 0; j < pcnt; j++) {
6368                 code =
6369                     AFSVolListOneVolume(aconn, PartList.partId[j], volumeid,
6370                                         &volumeInfo);
6371                 if (code) {
6372                     if (code != ENODEV) {
6373                         fprintf(STDERR, "Could not query server\n");
6374                         ERROR_EXIT(code);
6375                     }
6376                 } else {
6377                     if (flags & 2)
6378                         mod = 1;
6379                     else
6380                         mod = 0;
6381                     /* Found one, sync it with VLDB entry */
6382                     code =
6383                         CheckVolume(volumeInfo.volEntries_val, aserver,
6384                                     PartList.partId[j], &mod, &maxvolid, &vldbentry);
6385                     if (code)
6386                         ERROR_EXIT(code);
6387                     if (mod)
6388                         modified++;
6389                 }
6390
6391                 if (volumeInfo.volEntries_val)
6392                     free(volumeInfo.volEntries_val);
6393                 volumeInfo.volEntries_val = (volintInfo *) 0;
6394                 volumeInfo.volEntries_len = 0;
6395             }
6396         }
6397
6398         /* Check to see if the RW, BK, and RO IDs exist on any
6399          * partitions. We get the volume IDs from the VLDB.
6400          */
6401         for (j = 0; j < MAXTYPES; j++) {        /* for RW, RO, and BK IDs */
6402             if (vldbentry.volumeId[j] == 0)
6403                 continue;
6404
6405             for (k = 0; k < pcnt; k++) {        /* For each partition */
6406                 volumeInfo.volEntries_val = (volintInfo *) 0;
6407                 volumeInfo.volEntries_len = 0;
6408                 code =
6409                     AFSVolListOneVolume(aconn, PartList.partId[k],
6410                                         vldbentry.volumeId[j], &volumeInfo);
6411                 if (code) {
6412                     if (code != ENODEV) {
6413                         fprintf(STDERR, "Could not query server\n");
6414                         ERROR_EXIT(code);
6415                     }
6416                 } else {
6417                     if (flags & 2)
6418                         mod = 1;
6419                     else
6420                         mod = 0;
6421                     /* Found one, sync it with VLDB entry */
6422                     code =
6423                         CheckVolume(volumeInfo.volEntries_val, aserver,
6424                                     PartList.partId[k], &mod, &maxvolid, &vldbentry);
6425                     if (code)
6426                         ERROR_EXIT(code);
6427                     if (mod)
6428                         modified++;
6429                 }
6430
6431                 if (volumeInfo.volEntries_val)
6432                     free(volumeInfo.volEntries_val);
6433                 volumeInfo.volEntries_val = (volintInfo *) 0;
6434                 volumeInfo.volEntries_len = 0;
6435             }
6436         }
6437     }
6438
6439     /* if (aserver) */
6440     /* If verbose output, print a summary of what changed */
6441     if (tverbose) {
6442         fprintf(STDOUT, "-- status after --\n");
6443         if (deleted) {
6444             fprintf(STDOUT, "\n**entry deleted**\n");
6445         } else if (modified) {
6446             EnumerateEntry(&vldbentry);
6447         } else {
6448             fprintf(STDOUT, "\n**no change**\n");
6449         }
6450         fprintf(STDOUT, "\n_______________________________\n");
6451     }
6452
6453   error_exit:
6454     /* Now check if the maxvolid is larger than that stored in the VLDB */
6455     if (maxvolid) {
6456         afs_uint32 maxvldbid = 0;
6457         code = ubik_VL_GetNewVolumeId(cstruct, 0, 0, &maxvldbid);
6458         if (code) {
6459             fprintf(STDERR,
6460                     "Could not get the highest allocated volume id from the VLDB\n");
6461             if (!error)
6462                 error = code;
6463         } else if (maxvolid > maxvldbid) {
6464             afs_uint32 id, nid;
6465             id = maxvolid - maxvldbid + 1;
6466             code = ubik_VL_GetNewVolumeId(cstruct, 0, id, &nid);
6467             if (code) {
6468                 fprintf(STDERR,
6469                         "Error in increasing highest allocated volume id in VLDB\n");
6470                 if (!error)
6471                     error = code;
6472             }
6473         }
6474     }
6475
6476     verbose = tverbose;
6477     if (verbose) {
6478         if (error)
6479             fprintf(STDOUT, "...error encountered");
6480         else
6481             fprintf(STDOUT, "...done entry\n");
6482     }
6483     if (aconn)
6484         rx_DestroyConnection(aconn);
6485     if (volumeInfo.volEntries_val)
6486         free(volumeInfo.volEntries_val);
6487
6488     PrintError("", error);
6489     return error;
6490 }
6491
6492 /* UV_SyncVldb()
6493  *      Synchronise vldb with the file server <aserver> and,
6494  *      optionally, <apart>.
6495  */
6496 int
6497 UV_SyncVldb(afs_uint32 aserver, afs_int32 apart, int flags, int force)
6498 {
6499     struct rx_connection *aconn;
6500     afs_int32 code, error = 0;
6501     int i, pfail;
6502     unsigned int j;
6503     volEntries volumeInfo;
6504     struct partList PartList;
6505     afs_int32 pcnt;
6506     char pname[10];
6507     volintInfo *vi;
6508     afs_int32 failures = 0, modifications = 0, tentries = 0;
6509     afs_int32 modified;
6510     afs_uint32 maxvolid = 0;
6511     char hoststr[16];
6512
6513     volumeInfo.volEntries_val = (volintInfo *) 0;
6514     volumeInfo.volEntries_len = 0;
6515
6516     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
6517
6518     /* Generate array of partitions to check */
6519     if (!(flags & 1)) {
6520         code = UV_ListPartitions(aserver, &PartList, &pcnt);
6521         if (code) {
6522             fprintf(STDERR,
6523                     "Could not fetch the list of partitions from the server\n");
6524             ERROR_EXIT(code);
6525         }
6526     } else {
6527         PartList.partId[0] = apart;
6528         pcnt = 1;
6529     }
6530
6531     VPRINT("Processing volume entries ...\n");
6532
6533     /* Step through the array of partitions */
6534     for (i = 0; i < pcnt; i++) {
6535         apart = PartList.partId[i];
6536         MapPartIdIntoName(apart, pname);
6537
6538         volumeInfo.volEntries_val = (volintInfo *) 0;
6539         volumeInfo.volEntries_len = 0;
6540         code = AFSVolListVolumes(aconn, apart, 1, &volumeInfo);
6541         if (code) {
6542             fprintf(STDERR,
6543                     "Could not fetch the list of volumes from the server\n");
6544             ERROR_EXIT(code);
6545         }
6546
6547         /* May want to sort the entries: RW, BK (high to low), RO (high to low) */
6548         qsort((char *)volumeInfo.volEntries_val, volumeInfo.volEntries_len,
6549               sizeof(volintInfo), sortVolumes);
6550
6551         pfail = 0;
6552         for (vi = volumeInfo.volEntries_val, j = 0;
6553              j < volumeInfo.volEntries_len; j++, vi++) {
6554             if (!vi->status)
6555                 continue;
6556
6557             tentries++;
6558
6559             if (verbose) {
6560                 fprintf(STDOUT,
6561                         "Processing volume entry %d: %s (%lu) on server %s %s...\n",
6562                         j + 1, vi->name, (unsigned long)vi->volid,
6563                         noresolve ?
6564                         afs_inet_ntoa_r(aserver, hoststr) :
6565                         hostutil_GetNameByINet(aserver), pname);
6566                 fflush(STDOUT);
6567             }
6568
6569             if (flags & 2)
6570                 modified = 1;
6571             else
6572                 modified = 0;
6573             code = CheckVolume(vi, aserver, apart, &modified, &maxvolid, NULL);
6574             if (code) {
6575                 PrintError("", code);
6576                 failures++;
6577                 pfail++;
6578             } else if (modified) {
6579                 modifications++;
6580             }
6581
6582             if (verbose) {
6583                 if (code) {
6584                     fprintf(STDOUT, "...error encountered\n\n");
6585                 } else {
6586                     fprintf(STDOUT, "...done entry %d\n\n", j + 1);
6587                 }
6588             }
6589         }
6590
6591         if (pfail) {
6592             fprintf(STDERR,
6593                     "Could not process entries on server %s partition %s\n",
6594                     noresolve ?
6595                     afs_inet_ntoa_r(aserver, hoststr) :
6596                     hostutil_GetNameByINet(aserver), pname);
6597         }
6598         if (volumeInfo.volEntries_val) {
6599             free(volumeInfo.volEntries_val);
6600             volumeInfo.volEntries_val = 0;
6601         }
6602
6603     }                           /* thru all partitions */
6604
6605     if (flags & 2) {
6606         VPRINT3("Total entries: %u, Failed to process %d, Would change %d\n",
6607                 tentries, failures, modifications);
6608     } else {
6609         VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n",
6610                 tentries, failures, modifications);
6611     }
6612
6613   error_exit:
6614     /* Now check if the maxvolid is larger than that stored in the VLDB */
6615     if (maxvolid) {
6616         afs_uint32 maxvldbid = 0;
6617         code = ubik_VL_GetNewVolumeId(cstruct, 0, 0, &maxvldbid);
6618         if (code) {
6619             fprintf(STDERR,
6620                     "Could not get the highest allocated volume id from the VLDB\n");
6621             if (!error)
6622                 error = code;
6623         } else if (maxvolid > maxvldbid) {
6624             afs_uint32 id, nid;
6625             id = maxvolid - maxvldbid + 1;
6626             code = ubik_VL_GetNewVolumeId(cstruct, 0, id, &nid);
6627             if (code) {
6628                 fprintf(STDERR,
6629                         "Error in increasing highest allocated volume id in VLDB\n");
6630                 if (!error)
6631                     error = code;
6632             }
6633         }
6634     }
6635
6636     if (aconn)
6637         rx_DestroyConnection(aconn);
6638     if (volumeInfo.volEntries_val)
6639         free(volumeInfo.volEntries_val);
6640     PrintError("", error);
6641     return (error);
6642 }
6643
6644 /* VolumeExists()
6645  *      Determine if a volume exists on a server and partition.
6646  *      Try creating a transaction on the volume. If we can,
6647  *      the volume exists, if not, then return the error code.
6648  *      Some error codes mean the volume is unavailable but
6649  *      still exists - so we catch these error codes.
6650  */
6651 static afs_int32
6652 VolumeExists(afs_uint32 server, afs_int32 partition, afs_uint32 volumeid)
6653 {
6654     struct rx_connection *conn = (struct rx_connection *)0;
6655     afs_int32 code = -1;
6656     volEntries volumeInfo;
6657
6658     conn = UV_Bind(server, AFSCONF_VOLUMEPORT);
6659     if (conn) {
6660         volumeInfo.volEntries_val = (volintInfo *) 0;
6661         volumeInfo.volEntries_len = 0;
6662         code = AFSVolListOneVolume(conn, partition, volumeid, &volumeInfo);
6663         if (volumeInfo.volEntries_val)
6664             free(volumeInfo.volEntries_val);
6665         if (code == VOLSERILLEGAL_PARTITION)
6666             code = ENODEV;
6667         rx_DestroyConnection(conn);
6668     }
6669     return code;
6670 }
6671
6672 /* CheckVldbRWBK()
6673  *
6674  */
6675 static afs_int32
6676 CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
6677 {
6678     int modentry = 0;
6679     int idx;
6680     afs_int32 code, error = 0;
6681     char pname[10];
6682     char hoststr[16];
6683
6684     if (modified)
6685         *modified = 0;
6686     idx = Lp_GetRwIndex(entry);
6687
6688     /* Check to see if the RW volume exists and set the RW_EXISTS
6689      * flag accordingly.
6690      */
6691     if (idx == -1) {            /* Did not find a RW entry */
6692         if (entry->flags & RW_EXISTS) { /* ... yet entry says RW exists */
6693             entry->flags &= ~RW_EXISTS; /* ... so say RW does not exist */
6694             modentry++;
6695         }
6696     } else {
6697         code =
6698             VolumeExists(entry->serverNumber[idx],
6699                          entry->serverPartition[idx], entry->volumeId[RWVOL]);
6700         if (code == 0) {        /* RW volume exists */
6701             if (!(entry->flags & RW_EXISTS)) {  /* ... yet entry says RW does not exist */
6702                 entry->flags |= RW_EXISTS;      /* ... so say RW does exist */
6703                 modentry++;
6704             }
6705         } else if (code == ENODEV) {    /* RW volume does not exist */
6706             if (entry->flags & RW_EXISTS) {     /* ... yet entry says RW exists */
6707                 entry->flags &= ~RW_EXISTS;     /* ... so say RW does not exist */
6708                 modentry++;
6709             }
6710         } else {
6711             /* If VLDB says it didn't exist, then ignore error */
6712             if (entry->flags & RW_EXISTS) {
6713                 MapPartIdIntoName(entry->serverPartition[idx], pname);
6714                 fprintf(STDERR,
6715                         "Transaction call failed for RW volume %u on server %s %s\n",
6716                         entry->volumeId[RWVOL],
6717                         noresolve ?
6718                         afs_inet_ntoa_r(entry->serverNumber[idx], hoststr) :
6719                         hostutil_GetNameByINet(entry->serverNumber[idx]),
6720                         pname);
6721                 ERROR_EXIT(code);
6722             }
6723         }
6724     }
6725
6726     /* Check to see if the BK volume exists and set the BACK_EXISTS
6727      * flag accordingly. idx already ponts to the RW entry.
6728      */
6729     if (idx == -1) {            /* Did not find a RW entry */
6730         if (entry->flags & BACK_EXISTS) {       /* ... yet entry says BK exists */
6731             entry->flags &= ~BACK_EXISTS;       /* ... so say BK does not exist */
6732             modentry++;
6733         }
6734     } else {                    /* Found a RW entry */
6735         code =
6736             VolumeExists(entry->serverNumber[idx],
6737                          entry->serverPartition[idx],
6738                          entry->volumeId[BACKVOL]);
6739         if (code == 0) {        /* BK volume exists */
6740             if (!(entry->flags & BACK_EXISTS)) {        /* ... yet entry says BK does not exist */
6741                 entry->flags |= BACK_EXISTS;    /* ... so say BK does exist */
6742                 modentry++;
6743             }
6744         } else if (code == ENODEV) {    /* BK volume does not exist */
6745             if (entry->flags & BACK_EXISTS) {   /* ... yet entry says BK exists */
6746                 entry->flags &= ~BACK_EXISTS;   /* ... so say BK does not exist */
6747                 modentry++;
6748             }
6749         } else {
6750             /* If VLDB says it didn't exist, then ignore error */
6751             if (entry->flags & BACK_EXISTS) {
6752                 MapPartIdIntoName(entry->serverPartition[idx], pname);
6753                 fprintf(STDERR,
6754                         "Transaction call failed for BK volume %u on server %s %s\n",
6755                         entry->volumeId[BACKVOL],
6756                         noresolve ?
6757                         afs_inet_ntoa_r(entry->serverNumber[idx], hoststr) :
6758                         hostutil_GetNameByINet(entry->serverNumber[idx]),
6759                         pname);
6760                 ERROR_EXIT(code);
6761             }
6762         }
6763     }
6764
6765     /* If there is an idx but the BK and RW volumes no
6766      * longer exist, then remove the RW entry.
6767      */
6768     if ((idx != -1) && !(entry->flags & RW_EXISTS)
6769         && !(entry->flags & BACK_EXISTS)) {
6770         Lp_SetRWValue(entry, entry->serverNumber[idx],
6771                       entry->serverPartition[idx], 0L, 0L);
6772         entry->nServers--;
6773         modentry++;
6774     }
6775
6776   error_exit:
6777     if (modified)
6778         *modified = modentry;
6779     return (error);
6780 }
6781
6782 static afs_int32
6783 CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified)
6784 {
6785     int idx;
6786     int foundro = 0, modentry = 0;
6787     afs_int32 code, error = 0;
6788     char pname[10];
6789     char hoststr[16];
6790
6791     if (modified)
6792         *modified = 0;
6793
6794     /* Check to see if the RO volumes exist and set the RO_EXISTS
6795      * flag accordingly.
6796      */
6797     for (idx = 0; idx < entry->nServers; idx++) {
6798         if (!(entry->serverFlags[idx] & ITSROVOL)) {
6799             continue;           /* not a RO */
6800         }
6801
6802         code =
6803             VolumeExists(entry->serverNumber[idx],
6804                          entry->serverPartition[idx], entry->volumeId[ROVOL]);
6805         if (code == 0) {        /* RO volume exists */
6806             foundro++;
6807         } else if (code == ENODEV) {    /* RW volume does not exist */
6808             Lp_SetROValue(entry, entry->serverNumber[idx],
6809                           entry->serverPartition[idx], 0L, 0L);
6810             entry->nServers--;
6811             idx--;
6812             modentry++;
6813         } else {
6814             MapPartIdIntoName(entry->serverPartition[idx], pname);
6815             fprintf(STDERR,
6816                     "Transaction call failed for RO %u on server %s %s\n",
6817                     entry->volumeId[ROVOL],
6818                     noresolve ?
6819                     afs_inet_ntoa_r(entry->serverNumber[idx], hoststr) :
6820                     hostutil_GetNameByINet(entry->serverNumber[idx]), pname);
6821             ERROR_EXIT(code);
6822         }
6823     }
6824
6825     if (foundro) {              /* A RO volume exists */
6826         if (!(entry->flags & RO_EXISTS)) {      /* ... yet entry says RW does not exist */
6827             entry->flags |= RO_EXISTS;  /* ... so say RW does exist */
6828             modentry++;
6829         }
6830     } else {                    /* A RO volume does not exist */
6831         if (entry->flags & RO_EXISTS) { /* ... yet entry says RO exists */
6832             entry->flags &= ~RO_EXISTS; /* ... so say RO does not exist */
6833             modentry++;
6834         }
6835     }
6836
6837   error_exit:
6838     if (modified)
6839         *modified = modentry;
6840     return (error);
6841 }
6842
6843 /* CheckVldb()
6844  *      Ensure that <entry> matches with the info on file servers
6845  */
6846 static afs_int32
6847 CheckVldb(struct nvldbentry * entry, afs_int32 * modified, afs_int32 * deleted)
6848 {
6849     afs_int32 code, error = 0;
6850     struct nvldbentry storeEntry;
6851     int islocked = 0, mod, modentry, delentry = 0;
6852     int pass = 0, doit=1;
6853
6854     if (modified) {
6855         if (*modified == 1)
6856             doit = 0;
6857         *modified = 0;
6858     }
6859     if (verbose) {
6860         fprintf(STDOUT, "_______________________________\n");
6861         fprintf(STDOUT, "\n-- status before -- \n");
6862         if ((entry->flags & RW_EXISTS) || (entry->flags & RO_EXISTS)
6863             || (entry->flags & BACK_EXISTS))
6864             EnumerateEntry(entry);
6865         fprintf(STDOUT, "\n");
6866     }
6867
6868     if (strlen(entry->name) > (VOLSER_OLDMAXVOLNAME - 10)) {
6869         fprintf(STDERR, "Volume name %s exceeds limit of %d characters\n",
6870                 entry->name, VOLSER_OLDMAXVOLNAME - 10);
6871     }
6872
6873   retry:
6874     /* Check to see if the VLDB is ok without locking it (pass 1).
6875      * If it will change, then lock the VLDB entry, read it again,
6876      * then make the changes to it (pass 2).
6877      */
6878     if (++pass == 2) {
6879         code =
6880             ubik_VL_SetLock(cstruct, 0, entry->volumeId[RWVOL], RWVOL,
6881                       VLOP_DELETE);
6882         if (code) {
6883             fprintf(STDERR, "Could not lock VLDB entry for %u \n",
6884                     entry->volumeId[RWVOL]);
6885             ERROR_EXIT(code);
6886         }
6887         islocked = 1;
6888
6889         code = VLDB_GetEntryByID(entry->volumeId[RWVOL], RWVOL, entry);
6890         if (code) {
6891             fprintf(STDERR, "Could not read VLDB entry for volume %s\n",
6892                     entry->name);
6893             ERROR_EXIT(code);
6894         } else {
6895             MapHostToNetwork(entry);
6896         }
6897     }
6898
6899     modentry = 0;
6900
6901     /* Check if the RW and BK entries are ok */
6902     code = CheckVldbRWBK(entry, &mod);
6903     if (code)
6904         ERROR_EXIT(code);
6905     if (mod && (pass == 1) && doit)
6906         goto retry;
6907     if (mod)
6908         modentry++;
6909
6910     /* Check if the RO volumes entries are ok */
6911     code = CheckVldbRO(entry, &mod);
6912     if (code)
6913         ERROR_EXIT(code);
6914     if (mod && (pass == 1) && doit)
6915         goto retry;
6916     if (mod)
6917         modentry++;
6918
6919     /* The VLDB entry has been updated. If it as been modified, then
6920      * write the entry back out the the VLDB.
6921      */
6922     if (modentry && doit) {
6923         if (pass == 1)
6924             goto retry;
6925
6926         if (!(entry->flags & RW_EXISTS) && !(entry->flags & BACK_EXISTS)
6927             && !(entry->flags & RO_EXISTS) && doit) {
6928             /* The RW, BK, nor RO volumes do not exist. Delete the VLDB entry */
6929             code =
6930                 ubik_VL_DeleteEntry(cstruct, 0, entry->volumeId[RWVOL],
6931                           RWVOL);
6932             if (code) {
6933                 fprintf(STDERR,
6934                         "Could not delete VLDB entry for volume %u \n",
6935                         entry->volumeId[RWVOL]);
6936                 ERROR_EXIT(code);
6937             }
6938             delentry = 1;
6939         } else {
6940             /* Replace old entry with our new one */
6941             MapNetworkToHost(entry, &storeEntry);
6942             code =
6943                 VLDB_ReplaceEntry(entry->volumeId[RWVOL], RWVOL, &storeEntry,
6944                                   (LOCKREL_OPCODE | LOCKREL_AFSID |
6945                                    LOCKREL_TIMESTAMP));
6946             if (code) {
6947                 fprintf(STDERR, "Could not update VLDB entry for volume %u\n",
6948                         entry->volumeId[RWVOL]);
6949                 ERROR_EXIT(code);
6950             }
6951         }
6952         islocked = 0;
6953     }
6954
6955     if (modified && modentry) {
6956         *modified = 1;
6957     }
6958     if (deleted && delentry) {
6959         *deleted = 1;
6960     }
6961
6962     if (verbose) {
6963         fprintf(STDOUT, "-- status after --\n");
6964         if (delentry)
6965             fprintf(STDOUT, "\n**entry deleted**\n");
6966         else if (modentry)
6967             EnumerateEntry(entry);
6968         else
6969             fprintf(STDOUT, "\n**no change**\n");
6970     }
6971
6972   error_exit:
6973     VPRINT("\n_______________________________\n");
6974
6975     if (islocked) {
6976         code =
6977             ubik_VL_ReleaseLock(cstruct, 0, entry->volumeId[RWVOL],
6978                       RWVOL,
6979                       (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
6980         if (code) {
6981             fprintf(STDERR,
6982                     "Could not release lock on VLDB entry for volume %u\n",
6983                     entry->volumeId[RWVOL]);
6984             if (!error)
6985                 error = code;
6986         }
6987     }
6988     return error;
6989 }
6990
6991 /* UV_SyncServer()
6992  *      Synchronise <aserver> <apart>(if flags = 1) with the VLDB.
6993  */
6994 int
6995 UV_SyncServer(afs_uint32 aserver, afs_int32 apart, int flags, int force)
6996 {
6997     struct rx_connection *aconn;
6998     afs_int32 code, error = 0;
6999     afs_int32 nentries, tentries = 0;
7000     struct VldbListByAttributes attributes;
7001     nbulkentries arrayEntries;
7002     afs_int32 failures = 0, modified, modifications = 0;
7003     struct nvldbentry *vlentry;
7004     afs_int32 si, nsi, j;
7005
7006     if (flags & 2)
7007         verbose = 1;
7008
7009     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
7010
7011     /* Set up attributes to search VLDB  */
7012     attributes.server = ntohl(aserver);
7013     attributes.Mask = VLLIST_SERVER;
7014     if ((flags & 1)) {
7015         attributes.partition = apart;
7016         attributes.Mask |= VLLIST_PARTITION;
7017     }
7018
7019     VPRINT("Processing VLDB entries ...\n");
7020
7021     /* While we need to collect more VLDB entries */
7022     for (si = 0; si != -1; si = nsi) {
7023         memset(&arrayEntries, 0, sizeof(arrayEntries));
7024
7025         /* Collect set of VLDB entries */
7026         code =
7027             VLDB_ListAttributesN2(&attributes, 0, si, &nentries,
7028                                   &arrayEntries, &nsi);
7029         if (code == RXGEN_OPCODE) {
7030             code = VLDB_ListAttributes(&attributes, &nentries, &arrayEntries);
7031             nsi = -1;
7032         }
7033         if (code) {
7034             fprintf(STDERR, "Could not access the VLDB for attributes\n");
7035             ERROR_EXIT(code);
7036         }
7037         tentries += nentries;
7038
7039         for (j = 0; j < nentries; j++) {
7040             vlentry = &arrayEntries.nbulkentries_val[j];
7041             MapHostToNetwork(vlentry);
7042
7043             VPRINT1("Processing VLDB entry %d ...\n", j + 1);
7044
7045             /* Tell CheckVldb not to update if appropriate */
7046             if (flags & 2)
7047                 modified = 1;
7048             else
7049                 modified = 0;
7050             code = CheckVldb(vlentry, &modified, NULL);
7051             if (code) {
7052                 PrintError("", code);
7053                 fprintf(STDERR,
7054                         "Could not process VLDB entry for volume %s\n",
7055                         vlentry->name);
7056                 failures++;
7057             } else if (modified) {
7058                 modifications++;
7059             }
7060
7061             if (verbose) {
7062                 if (code) {
7063                     fprintf(STDOUT, "...error encountered\n\n");
7064                 } else {
7065                     fprintf(STDOUT, "...done entry %d\n\n", j + 1);
7066                 }
7067             }
7068         }
7069
7070         if (arrayEntries.nbulkentries_val) {
7071             free(arrayEntries.nbulkentries_val);
7072             arrayEntries.nbulkentries_val = 0;
7073         }
7074     }
7075
7076     if (flags & 2) {
7077         VPRINT3("Total entries: %u, Failed to process %d, Would change %d\n",
7078                 tentries, failures, modifications);
7079     } else {
7080         VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n",
7081                 tentries, failures, modifications);
7082     }
7083
7084   error_exit:
7085     if (aconn)
7086         rx_DestroyConnection(aconn);
7087     if (arrayEntries.nbulkentries_val)
7088         free(arrayEntries.nbulkentries_val);
7089
7090     if (failures)
7091         error = VOLSERFAILEDOP;
7092     return error;
7093 }
7094
7095 /*rename volume <oldname> to <newname>, changing the names of the related
7096  *readonly and backup volumes. This operation is also idempotent.
7097  *salvager is capable of recovering from rename operation stopping halfway.
7098  *to recover run syncserver on the affected machines,it will force renaming to completion. name clashes should have been detected before calling this proc */
7099 int
7100 UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
7101 {
7102     struct nvldbentry storeEntry;
7103     afs_int32 vcode, code, rcode, error;
7104     int i, index;
7105     char nameBuffer[256];
7106     afs_int32 tid;
7107     struct rx_connection *aconn;
7108     int islocked;
7109     char hoststr[16];
7110
7111     error = 0;
7112     aconn = (struct rx_connection *)0;
7113     tid = 0;
7114     islocked = 0;
7115
7116     vcode = ubik_VL_SetLock(cstruct, 0, entry->volumeId[RWVOL], RWVOL, VLOP_ADDSITE);   /*last param is dummy */
7117     if (vcode) {
7118         fprintf(STDERR,
7119                 " Could not lock the VLDB entry for the  volume %u \n",
7120                 entry->volumeId[RWVOL]);
7121         error = vcode;
7122         goto rvfail;
7123     }
7124     islocked = 1;
7125     strncpy(entry->name, newname, VOLSER_OLDMAXVOLNAME);
7126     MapNetworkToHost(entry, &storeEntry);
7127     vcode = VLDB_ReplaceEntry(entry->volumeId[RWVOL], RWVOL, &storeEntry, 0);
7128     if (vcode) {
7129         fprintf(STDERR, "Could not update VLDB entry for %u\n",
7130                 entry->volumeId[RWVOL]);
7131         error = vcode;
7132         goto rvfail;
7133     }
7134     VPRINT1("Recorded the new name %s in VLDB\n", newname);
7135     /*at this stage the intent to rename is recorded in the vldb, as far as the vldb
7136      * is concerned, oldname is lost */
7137     if (entry->flags & RW_EXISTS) {
7138         index = Lp_GetRwIndex(entry);
7139         if (index == -1) {      /* there is a serious discrepancy */
7140             fprintf(STDERR,
7141                     "There is a serious discrepancy in VLDB entry for volume %u\n",
7142                     entry->volumeId[RWVOL]);
7143             fprintf(STDERR, "try building VLDB from scratch\n");
7144             error = VOLSERVLDB_ERROR;
7145             goto rvfail;
7146         }
7147         aconn = UV_Bind(entry->serverNumber[index], AFSCONF_VOLUMEPORT);
7148         code =
7149             AFSVolTransCreate_retry(aconn, entry->volumeId[RWVOL],
7150                               entry->serverPartition[index], ITOffline, &tid);
7151         if (code) {             /*volume doesnot exist */
7152             fprintf(STDERR,
7153                     "Could not start transaction on the rw volume %u\n",
7154                     entry->volumeId[RWVOL]);
7155             error = code;
7156             goto rvfail;
7157         } else {                /*volume exists, process it */
7158
7159             code =
7160                 AFSVolSetIdsTypes(aconn, tid, newname, RWVOL,
7161                                   entry->volumeId[RWVOL],
7162                                   entry->volumeId[ROVOL],
7163                                   entry->volumeId[BACKVOL]);
7164             if (!code) {
7165                 VPRINT2("Renamed rw volume %s to %s\n", oldname, newname);
7166                 code = AFSVolEndTrans(aconn, tid, &rcode);
7167                 tid = 0;
7168                 if (code) {
7169                     fprintf(STDERR,
7170                             "Could not  end transaction on volume %s %u\n",
7171                             entry->name, entry->volumeId[RWVOL]);
7172                     error = code;
7173                     goto rvfail;
7174                 }
7175             } else {
7176                 fprintf(STDERR, "Could not  set parameters on volume %s %u\n",
7177                         entry->name, entry->volumeId[RWVOL]);
7178                 error = code;
7179                 goto rvfail;
7180             }
7181         }
7182         if (aconn)
7183             rx_DestroyConnection(aconn);
7184         aconn = (struct rx_connection *)0;
7185     }
7186     /*end rw volume processing */
7187     if (entry->flags & BACK_EXISTS) {   /*process the backup volume */
7188         index = Lp_GetRwIndex(entry);
7189         if (index == -1) {      /* there is a serious discrepancy */
7190             fprintf(STDERR,
7191                     "There is a serious discrepancy in the VLDB entry for the backup volume %u\n",
7192                     entry->volumeId[BACKVOL]);
7193             fprintf(STDERR, "try building VLDB from scratch\n");
7194             error = VOLSERVLDB_ERROR;
7195             goto rvfail;
7196         }
7197         aconn = UV_Bind(entry->serverNumber[index], AFSCONF_VOLUMEPORT);
7198         code =
7199             AFSVolTransCreate_retry(aconn, entry->volumeId[BACKVOL],
7200                               entry->serverPartition[index], ITOffline, &tid);
7201         if (code) {             /*volume doesnot exist */
7202             fprintf(STDERR,
7203                     "Could not start transaction on the backup volume  %u\n",
7204                     entry->volumeId[BACKVOL]);
7205             error = code;
7206             goto rvfail;
7207         } else {                /*volume exists, process it */
7208             if (strlen(newname) > (VOLSER_OLDMAXVOLNAME - 8)) {
7209                 fprintf(STDERR,
7210                         "Volume name %s.backup exceeds the limit of %u characters\n",
7211                         newname, VOLSER_OLDMAXVOLNAME);
7212                 error = code;
7213                 goto rvfail;
7214             }
7215             strcpy(nameBuffer, newname);
7216             strcat(nameBuffer, ".backup");
7217
7218             code =
7219                 AFSVolSetIdsTypes(aconn, tid, nameBuffer, BACKVOL,
7220                                   entry->volumeId[RWVOL], 0, 0);
7221             if (!code) {
7222                 VPRINT1("Renamed backup volume to %s \n", nameBuffer);
7223                 code = AFSVolEndTrans(aconn, tid, &rcode);
7224                 tid = 0;
7225                 if (code) {
7226                     fprintf(STDERR,
7227                             "Could not  end transaction on the backup volume %u\n",
7228                             entry->volumeId[BACKVOL]);
7229                     error = code;
7230                     goto rvfail;
7231                 }
7232             } else {
7233                 fprintf(STDERR,
7234                         "Could not  set parameters on the backup volume %u\n",
7235                         entry->volumeId[BACKVOL]);
7236                 error = code;
7237                 goto rvfail;
7238             }
7239         }
7240     }                           /* end backup processing */
7241     if (aconn)
7242         rx_DestroyConnection(aconn);
7243     aconn = (struct rx_connection *)0;
7244     if (entry->flags & RO_EXISTS) {     /*process the ro volumes */
7245         for (i = 0; i < entry->nServers; i++) {
7246             if (entry->serverFlags[i] & ITSROVOL) {
7247                 aconn = UV_Bind(entry->serverNumber[i], AFSCONF_VOLUMEPORT);
7248                 code =
7249                     AFSVolTransCreate_retry(aconn, entry->volumeId[ROVOL],
7250                                       entry->serverPartition[i], ITOffline,
7251                                       &tid);
7252                 if (code) {     /*volume doesnot exist */
7253                     fprintf(STDERR,
7254                             "Could not start transaction on the ro volume %u\n",
7255                             entry->volumeId[ROVOL]);
7256                     error = code;
7257                     goto rvfail;
7258                 } else {        /*volume exists, process it */
7259                     strcpy(nameBuffer, newname);
7260                     strcat(nameBuffer, ".readonly");
7261                     if (strlen(nameBuffer) > (VOLSER_OLDMAXVOLNAME - 1)) {
7262                         fprintf(STDERR,
7263                                 "Volume name %s exceeds the limit of %u characters\n",
7264                                 nameBuffer, VOLSER_OLDMAXVOLNAME);
7265                         error = code;
7266                         goto rvfail;
7267                     }
7268                     code =
7269                         AFSVolSetIdsTypes(aconn, tid, nameBuffer, ROVOL,
7270                                           entry->volumeId[RWVOL], 0, 0);
7271                     if (!code) {
7272                         VPRINT2("Renamed RO volume %s on host %s\n",
7273                                 nameBuffer,
7274                                 noresolve ?
7275                                 afs_inet_ntoa_r(entry->serverNumber[i], hoststr) :
7276                                 hostutil_GetNameByINet(entry->
7277                                                        serverNumber[i]));
7278                         code = AFSVolEndTrans(aconn, tid, &rcode);
7279                         tid = 0;
7280                         if (code) {
7281                             fprintf(STDERR,
7282                                     "Could not  end transaction on volume %u\n",
7283                                     entry->volumeId[ROVOL]);
7284                             error = code;
7285                             goto rvfail;
7286                         }
7287                     } else {
7288                         fprintf(STDERR,
7289                                 "Could not  set parameters on the ro volume %u\n",
7290                                 entry->volumeId[ROVOL]);
7291                         error = code;
7292                         goto rvfail;
7293                     }
7294                 }
7295                 if (aconn)
7296                     rx_DestroyConnection(aconn);
7297                 aconn = (struct rx_connection *)0;
7298             }
7299         }
7300     }
7301   rvfail:
7302     if (islocked) {
7303         vcode =
7304             ubik_VL_ReleaseLock(cstruct, 0, entry->volumeId[RWVOL],
7305                       RWVOL,
7306                       LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
7307         if (vcode) {
7308             fprintf(STDERR,
7309                     "Could not unlock the VLDB entry for the volume %s %u\n",
7310                     entry->name, entry->volumeId[RWVOL]);
7311             if (!error)
7312                 error = vcode;
7313         }
7314     }
7315     if (tid) {
7316         code = AFSVolEndTrans(aconn, tid, &rcode);
7317         if (!code)
7318             code = rcode;
7319         if (code) {
7320             fprintf(STDERR, "Failed to end transaction on a volume \n");
7321             if (!error)
7322                 error = code;
7323         }
7324     }
7325     if (aconn)
7326         rx_DestroyConnection(aconn);
7327     PrintError("", error);
7328     return error;
7329
7330 }
7331
7332 /*report on all the active transactions on volser */
7333 int
7334 UV_VolserStatus(afs_uint32 server, transDebugInfo ** rpntr, afs_int32 * rcount)
7335 {
7336     struct rx_connection *aconn;
7337     transDebugEntries transInfo;
7338     afs_int32 code = 0;
7339
7340     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
7341     transInfo.transDebugEntries_val = (transDebugInfo *) 0;
7342     transInfo.transDebugEntries_len = 0;
7343     code = AFSVolMonitor(aconn, &transInfo);
7344     if (code) {
7345         fprintf(STDERR,
7346                 "Could not access status information about the server\n");
7347         PrintError("", code);
7348         if (transInfo.transDebugEntries_val)
7349             free(transInfo.transDebugEntries_val);
7350         if (aconn)
7351             rx_DestroyConnection(aconn);
7352         return code;
7353     } else {
7354         *rcount = transInfo.transDebugEntries_len;
7355         *rpntr = transInfo.transDebugEntries_val;
7356         if (aconn)
7357             rx_DestroyConnection(aconn);
7358         return 0;
7359     }
7360
7361
7362 }
7363
7364 /*delete the volume without interacting with the vldb */
7365 int
7366 UV_VolumeZap(afs_uint32 server, afs_int32 part, afs_uint32 volid)
7367 {
7368     afs_int32 error;
7369     struct rx_connection *aconn;
7370
7371     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
7372     error = DoVolDelete(aconn, volid, part,
7373                         "the", 0, NULL, NULL);
7374     if (error == VNOVOL) {
7375         EPRINT1(error, "Failed to start transaction on %u\n", volid);
7376     }
7377
7378     PrintError("", error);
7379     if (aconn)
7380         rx_DestroyConnection(aconn);
7381     return error;
7382 }
7383
7384 int
7385 UV_SetVolume(afs_uint32 server, afs_int32 partition, afs_uint32 volid,
7386              afs_int32 transflag, afs_int32 setflag, int sleeptime)
7387 {
7388     struct rx_connection *conn = 0;
7389     afs_int32 tid = 0;
7390     afs_int32 code, error = 0, rcode;
7391
7392     conn = UV_Bind(server, AFSCONF_VOLUMEPORT);
7393     if (!conn) {
7394         fprintf(STDERR, "SetVolumeStatus: Bind Failed");
7395         ERROR_EXIT(-1);
7396     }
7397
7398     code = AFSVolTransCreate_retry(conn, volid, partition, transflag, &tid);
7399     if (code) {
7400         fprintf(STDERR, "SetVolumeStatus: TransCreate Failed\n");
7401         ERROR_EXIT(code);
7402     }
7403
7404     code = AFSVolSetFlags(conn, tid, setflag);
7405     if (code) {
7406         fprintf(STDERR, "SetVolumeStatus: SetFlags Failed\n");
7407         ERROR_EXIT(code);
7408     }
7409
7410     if (sleeptime) {
7411 #ifdef AFS_PTHREAD_ENV
7412         sleep(sleeptime);
7413 #else
7414         IOMGR_Sleep(sleeptime);
7415 #endif
7416     }
7417
7418   error_exit:
7419     if (tid) {
7420         rcode = 0;
7421         code = AFSVolEndTrans(conn, tid, &rcode);
7422         if (code || rcode) {
7423             fprintf(STDERR, "SetVolumeStatus: EndTrans Failed\n");
7424             if (!error)
7425                 error = (code ? code : rcode);
7426         }
7427     }
7428
7429     if (conn)
7430         rx_DestroyConnection(conn);
7431     return (error);
7432 }
7433
7434 int
7435 UV_SetVolumeInfo(afs_uint32 server, afs_int32 partition, afs_uint32 volid,
7436                  volintInfo * infop)
7437 {
7438     struct rx_connection *conn = 0;
7439     afs_int32 tid = 0;
7440     afs_int32 code, error = 0, rcode;
7441
7442     conn = UV_Bind(server, AFSCONF_VOLUMEPORT);
7443     if (!conn) {
7444         fprintf(STDERR, "SetVolumeInfo: Bind Failed");
7445         ERROR_EXIT(-1);
7446     }
7447
7448     code = AFSVolTransCreate_retry(conn, volid, partition, ITOffline, &tid);
7449     if (code) {
7450         fprintf(STDERR, "SetVolumeInfo: TransCreate Failed\n");
7451         ERROR_EXIT(code);
7452     }
7453
7454     code = AFSVolSetInfo(conn, tid, infop);
7455     if (code) {
7456         fprintf(STDERR, "SetVolumeInfo: SetInfo Failed\n");
7457         ERROR_EXIT(code);
7458     }
7459
7460   error_exit:
7461     if (tid) {
7462         rcode = 0;
7463         code = AFSVolEndTrans(conn, tid, &rcode);
7464         if (code || rcode) {
7465             fprintf(STDERR, "SetVolumeInfo: EndTrans Failed\n");
7466             if (!error)
7467                 error = (code ? code : rcode);
7468         }
7469     }
7470
7471     if (conn)
7472         rx_DestroyConnection(conn);
7473     return (error);
7474 }
7475
7476 int
7477 UV_GetSize(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
7478            afs_int32 fromdate, struct volintSize *vol_size)
7479 {
7480     struct rx_connection *aconn = (struct rx_connection *)0;
7481     afs_int32 tid = 0, rcode = 0;
7482     afs_int32 code, error = 0;
7483
7484
7485     /* get connections to the servers */
7486     aconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
7487
7488     VPRINT1("Starting transaction on volume %u...", afromvol);
7489     code = AFSVolTransCreate_retry(aconn, afromvol, afrompart, ITBusy, &tid);
7490     EGOTO1(error_exit, code,
7491            "Could not start transaction on the volume %u to be measured\n",
7492            afromvol);
7493     VDONE;
7494
7495     VPRINT1("Getting size of volume on volume %u...", afromvol);
7496     code = AFSVolGetSize(aconn, tid, fromdate, vol_size);
7497     EGOTO(error_exit, code, "Could not start the measurement process \n");
7498     VDONE;
7499
7500   error_exit:
7501     if (tid) {
7502         VPRINT1("Ending transaction on volume %u...", afromvol);
7503         code = AFSVolEndTrans(aconn, tid, &rcode);
7504         if (code || rcode) {
7505             fprintf(STDERR, "Could not end transaction on the volume %u\n",
7506                     afromvol);
7507             fprintf(STDERR, "error codes: %d and %d\n", code, rcode);
7508             if (!error)
7509                 error = (code ? code : rcode);
7510         }
7511         VDONE;
7512     }
7513     if (aconn)
7514         rx_DestroyConnection(aconn);
7515
7516     PrintError("", error);
7517     return (error);
7518 }
7519
7520 /*maps the host addresses in <old > (present in network byte order) to
7521  that in< new> (present in host byte order )*/
7522 void
7523 MapNetworkToHost(struct nvldbentry *old, struct nvldbentry *new)
7524 {
7525     int i, count;
7526
7527     /*copy all the fields */
7528     strcpy(new->name, old->name);
7529 /*    new->volumeType = old->volumeType;*/
7530     new->nServers = old->nServers;
7531     count = old->nServers;
7532     if (count < NMAXNSERVERS)
7533         count++;
7534     for (i = 0; i < count; i++) {
7535         new->serverNumber[i] = ntohl(old->serverNumber[i]);
7536         new->serverPartition[i] = old->serverPartition[i];
7537         new->serverFlags[i] = old->serverFlags[i];
7538     }
7539     new->volumeId[RWVOL] = old->volumeId[RWVOL];
7540     new->volumeId[ROVOL] = old->volumeId[ROVOL];
7541     new->volumeId[BACKVOL] = old->volumeId[BACKVOL];
7542     new->cloneId = old->cloneId;
7543     new->flags = old->flags;
7544 }
7545
7546 /*maps the host entries in <entry> which are present in host byte order to network byte order */
7547 void
7548 MapHostToNetwork(struct nvldbentry *entry)
7549 {
7550     int i, count;
7551
7552     count = entry->nServers;
7553     if (count < NMAXNSERVERS)
7554         count++;
7555     for (i = 0; i < count; i++) {
7556         entry->serverNumber[i] = htonl(entry->serverNumber[i]);
7557     }
7558 }