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