volser: improve error checking for 'vos restore'
[openafs.git] / src / volser / vsprocs.c
index 54cd748..de0b900 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
+#include <roken.h>
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
 #ifdef AFS_AIX_ENV
 #include <sys/statfs.h>
 #endif
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <winsock2.h>
-#else
-#include <sys/file.h>
-#include <netinet/in.h>
-#endif
 
 #include <lock.h>
 #include <afs/voldefs.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
+#include <rx/rx_queue.h>
 #include <afs/vlserver.h>
 #include <afs/nfs.h>
 #include <afs/cellconfig.h>
@@ -45,21 +35,26 @@ RCSID
 #include <rx/rxkad.h>
 #include <afs/kautils.h>
 #include <afs/cmd.h>
-#include <errno.h>
+#include <afs/ihandle.h>
+#ifdef AFS_NT40_ENV
+#include <afs/ntops.h>
+#endif
+#include <afs/vnode.h>
+#include <afs/volume.h>
 #define ERRCODE_RANGE 8                /* from error_table.h */
 #define        CLOCKSKEW   2           /* not really skew, but resolution */
 #define CLOCKADJ(x) (((x) < CLOCKSKEW) ? 0 : (x) - CLOCKSKEW)
 
 /* for UV_MoveVolume() recovery */
 
-#include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
 #include <setjmp.h>
 
+#include "volser_internal.h"
 #include "volser_prototypes.h"
 #include "vsutils_prototypes.h"
 #include "lockprocs_prototypes.h"
 
-struct ubik_client *cstruct;
+extern struct ubik_client *cstruct;
 int verbose = 0, noresolve = 0;
 
 struct release {
@@ -157,25 +152,40 @@ do { \
 
 
 /* getting rid of this */
-#define ERROR_EXIT(code) {error=(code); goto error_exit;}
+#define ERROR_EXIT(code) do { \
+    error = (code); \
+    goto error_exit; \
+} while (0)
 
 
 /* Protos for static routines */
-static afs_int32 CheckAndDeleteVolume(struct rx_connection *aconn,
-                                     afs_int32 apart, afs_uint32 okvol,
-                                     afs_uint32 delvol);
-static int DelVol(struct rx_connection *conn, afs_uint32 vid, afs_int32 part,
-                 afs_int32 flags);
 static int GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
                    struct rx_connection **connPtr, afs_int32 * transPtr,
-                   afs_int32 * crtimePtr, afs_int32 * uptimePtr);
+                   afs_uint32 * crtimePtr, afs_uint32 * uptimePtr,
+                   afs_int32 *origflags, afs_uint32 tmpVolId);
 static int SimulateForwardMultiple(struct rx_connection *fromconn,
                                   afs_int32 fromtid, afs_int32 fromdate,
                                   manyDests * tr, afs_int32 flags,
                                   void *cookie, manyResults * results);
-static afs_int32 CheckVolume(volintInfo * volumeinfo, afs_int32 aserver,
+static int DoVolClone(struct rx_connection *aconn, afs_uint32 avolid,
+                     afs_int32 apart, int type, afs_uint32 cloneid,
+                     char *typestring, char *pname, char *vname, char *suffix,
+                     struct volser_status *volstatus, afs_int32 *transPtr);
+static int DoVolDelete(struct rx_connection *aconn, afs_uint32 avolid,
+                      afs_int32 apart, char *typestring, afs_uint32 atoserver,
+                      struct volser_status *volstatus, char *pprefix);
+static afs_int32 CheckVolume(volintInfo * volumeinfo, afs_uint32 aserver,
                             afs_int32 apart, afs_int32 * modentry,
-                            afs_uint32 * maxvolid);
+                            afs_uint32 * maxvolid, struct nvldbentry *aentry);
+static afs_int32 VolumeExists(afs_uint32 server, afs_int32 partition,
+                              afs_uint32 volumeid);
+static afs_int32 CheckVldbRWBK(struct nvldbentry * entry,
+                               afs_int32 * modified);
+static afs_int32 CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified);
+static afs_int32 CheckVldb(struct nvldbentry *entry, afs_int32 * modified,
+                           afs_int32 *deleted);
+static void dump_sig_handler(int x);
+static int sortVolumes(const void *a, const void *b);
 
 
 /*map the partition <partId> into partition name <partName>*/
@@ -198,24 +208,9 @@ MapPartIdIntoName(afs_int32 partId, char *partName)
 }
 
 int
-yesprompt(char *str)
-{
-    int response, c;
-    int code;
-
-    fprintf(STDERR, "Do you want to %s? [yn](n): ", str);
-    response = c = getchar();
-    while (!(c == EOF || c == '\n'))
-       c = getchar();          /*skip to end of line */
-    code = (response == 'y' || response == 'Y');
-    return code;
-}
-
-
-int
 PrintError(char *msg, afs_int32 errcode)
 {
-    fprintf(STDERR, msg);
+    fprintf(STDERR, "%s", msg);
     /*replace by a big switch statement */
     switch (errcode) {
     case 0:
@@ -392,17 +387,11 @@ PrintError(char *msg, afs_int32 errcode)
        break;
     default:
        {
-
-           afs_int32 offset;
-
-           initialize_KA_error_table();
            initialize_RXK_error_table();
            initialize_KTC_error_table();
            initialize_ACFG_error_table();
-           initialize_CMD_error_table();
            initialize_VL_error_table();
 
-           offset = errcode & ((1 << ERRCODE_RANGE) - 1);
            fprintf(STDERR, "%s: %s\n", afs_error_table_name(errcode),
                    afs_error_message(errcode));
            break;
@@ -429,7 +418,7 @@ static struct rx_securityClass *uvclass = 0;
 static int uvindex = -1;
 /* called by VLDBClient_Init to set the security module to be used in the RPC */
 int
-UV_SetSecurity(register struct rx_securityClass *as, afs_int32 aindex)
+UV_SetSecurity(struct rx_securityClass *as, afs_int32 aindex)
 {
     uvindex = aindex;
     uvclass = as;
@@ -439,62 +428,56 @@ UV_SetSecurity(register struct rx_securityClass *as, afs_int32 aindex)
 /* bind to volser on <port> <aserver> */
 /* takes server address in network order, port in host order.  dumb */
 struct rx_connection *
-UV_Bind(afs_int32 aserver, afs_int32 port)
+UV_Bind(afs_uint32 aserver, afs_int32 port)
 {
-    register struct rx_connection *tc;
+    struct rx_connection *tc;
 
     tc = rx_NewConnection(aserver, htons(port), VOLSERVICE_ID, uvclass,
                          uvindex);
     return tc;
 }
 
-/* if <okvol> is allright(indicated by beibg able to
- * start a transaction, delete the <delvol> */
-static afs_int32
-CheckAndDeleteVolume(struct rx_connection *aconn, afs_int32 apart,
-                    afs_uint32 okvol, afs_uint32 delvol)
+static int
+AFSVolCreateVolume_retry(struct rx_connection *z_conn,
+                      afs_int32 partition, char *name, afs_int32 type,
+                      afs_int32 parent, afs_uint32 *volid, afs_int32 *trans)
 {
-    afs_int32 error, code, tid, rcode;
-
-    error = 0;
-    code = 0;
+    afs_int32 code;
+    int retries = 3;
+    while (retries) {
+       code = AFSVolCreateVolume(z_conn, partition, name, type, parent,
+                                 volid, trans);
+        if (code != VOLSERVOLBUSY)
+            break;
+        retries--;
+#ifdef AFS_PTHREAD_ENV
+        sleep(3-retries);
+#else
+        IOMGR_Sleep(3-retries);
+#endif
+    }
+    return code;
+}
 
-    if (okvol == 0) {
-       code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
-       if (!error && code)
-           error = code;
-       code = AFSVolDeleteVolume(aconn, tid);
-       if (!error && code)
-           error = code;
-       code = AFSVolEndTrans(aconn, tid, &rcode);
-       if (!code)
-           code = rcode;
-       if (!error && code)
-           error = code;
-       return error;
-    } else {
-       code = AFSVolTransCreate(aconn, okvol, apart, ITOffline, &tid);
-       if (!code) {
-           code = AFSVolEndTrans(aconn, tid, &rcode);
-           if (!code)
-               code = rcode;
-           if (!error && code)
-               error = code;
-           code = AFSVolTransCreate(aconn, delvol, apart, ITOffline, &tid);
-           if (!error && code)
-               error = code;
-           code = AFSVolDeleteVolume(aconn, tid);
-           if (!error && code)
-               error = code;
-           code = AFSVolEndTrans(aconn, tid, &rcode);
-           if (!code)
-               code = rcode;
-           if (!error && code)
-               error = code;
-       } else
-           error = code;
-       return error;
+static int
+AFSVolTransCreate_retry(struct rx_connection *z_conn,
+                       afs_int32 volume, afs_int32 partition,
+                       afs_int32 flags, afs_int32 * trans)
+{
+    afs_int32 code;
+    int retries = 3;
+    while (retries) {
+       code = AFSVolTransCreate(z_conn, volume, partition, flags, trans);
+        if (code != VOLSERVOLBUSY)
+            break;
+        retries--;
+#ifdef AFS_PTHREAD_ENV
+        sleep(3-retries);
+#else
+        IOMGR_Sleep(3-retries);
+#endif
     }
+    return code;
 }
 
 /* called by EmuerateEntry, show vldb entry in a reasonable format */
@@ -506,58 +489,38 @@ SubEnumerateEntry(struct nvldbentry *entry)
     int isMixed = 0;
     char hoststr[16];
 
-#ifdef notdef
-    fprintf(STDOUT, "  readWriteID %-10u ", entry->volumeId[RWVOL]);
-    if (entry->flags & RW_EXISTS)
-       fprintf(STDOUT, " valid \n");
-    else
-       fprintf(STDOUT, " invalid \n");
-    fprintf(STDOUT, "  readOnlyID  %-10u ", entry->volumeId[ROVOL]);
-    if (entry->flags & RO_EXISTS)
-       fprintf(STDOUT, " valid \n");
-    else
-       fprintf(STDOUT, " invalid \n");
-    fprintf(STDOUT, "  backUpID    %-10u ", entry->volumeId[BACKVOL]);
-    if (entry->flags & BACK_EXISTS)
-       fprintf(STDOUT, " valid \n");
-    else
-       fprintf(STDOUT, " invalid \n");
-    if ((entry->cloneId != 0) && (entry->flags & RO_EXISTS))
-       fprintf(STDOUT, "    releaseClone %-10u \n", entry->cloneId);
-#else
-    if (entry->flags & RW_EXISTS)
+    if (entry->flags & VLF_RWEXISTS)
        fprintf(STDOUT, "    RWrite: %-10u", entry->volumeId[RWVOL]);
-    if (entry->flags & RO_EXISTS)
+    if (entry->flags & VLF_ROEXISTS)
        fprintf(STDOUT, "    ROnly: %-10u", entry->volumeId[ROVOL]);
-    if (entry->flags & BACK_EXISTS)
+    if (entry->flags & VLF_BACKEXISTS)
        fprintf(STDOUT, "    Backup: %-10u", entry->volumeId[BACKVOL]);
-    if ((entry->cloneId != 0) && (entry->flags & RO_EXISTS))
+    if ((entry->cloneId != 0) && (entry->flags & VLF_ROEXISTS))
        fprintf(STDOUT, "    RClone: %-10lu", (unsigned long)entry->cloneId);
     fprintf(STDOUT, "\n");
-#endif
     fprintf(STDOUT, "    number of sites -> %lu\n",
            (unsigned long)entry->nServers);
     for (i = 0; i < entry->nServers; i++) {
-       if (entry->serverFlags[i] & NEW_REPSITE)
+       if (entry->serverFlags[i] & VLSF_NEWREPSITE)
            isMixed = 1;
     }
     for (i = 0; i < entry->nServers; i++) {
        MapPartIdIntoName(entry->serverPartition[i], pname);
        fprintf(STDOUT, "       server %s partition %s ",
-               noresolve ? afs_inet_ntoa_r(entry->serverNumber[i], hoststr) : 
+               noresolve ? afs_inet_ntoa_r(entry->serverNumber[i], hoststr) :
                 hostutil_GetNameByINet(entry->serverNumber[i]), pname);
-       if (entry->serverFlags[i] & ITSRWVOL)
+       if (entry->serverFlags[i] & VLSF_RWVOL)
            fprintf(STDOUT, "RW Site ");
        else
            fprintf(STDOUT, "RO Site ");
        if (isMixed) {
-           if (entry->serverFlags[i] & NEW_REPSITE)
+           if (entry->serverFlags[i] & VLSF_NEWREPSITE)
                fprintf(STDOUT," -- New release");
            else
-               if (!(entry->serverFlags[i] & ITSRWVOL))
+               if (!(entry->serverFlags[i] & VLSF_RWVOL))
                    fprintf(STDOUT," -- Old release");
        } else {
-           if (entry->serverFlags[i] & RO_DONTUSE)
+           if (entry->serverFlags[i] & VLSF_DONTUSE)
                fprintf(STDOUT, " -- Not released");
        }
        fprintf(STDOUT, "\n");
@@ -580,10 +543,10 @@ EnumerateEntry(struct nvldbentry *entry)
 
 /* forcibly remove a volume.  Very dangerous call */
 int
-UV_NukeVolume(afs_int32 server, afs_int32 partid, afs_uint32 volid)
+UV_NukeVolume(afs_uint32 server, afs_int32 partid, afs_uint32 volid)
 {
-    register struct rx_connection *tconn;
-    register afs_int32 code;
+    struct rx_connection *tconn;
+    afs_int32 code;
 
     tconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
     if (tconn) {
@@ -596,18 +559,16 @@ UV_NukeVolume(afs_int32 server, afs_int32 partid, afs_uint32 volid)
 
 /* like df. Return usage of <pname> on <server> in <partition> */
 int
-UV_PartitionInfo64(afs_int32 server, char *pname,
+UV_PartitionInfo64(afs_uint32 server, char *pname,
                   struct diskPartition64 *partition)
 {
-    register struct rx_connection *aconn;
+    struct rx_connection *aconn;
     afs_int32 code = 0;
 
-    aconn = (struct rx_connection *)0;
     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
     code = AFSVolPartitionInfo64(aconn, pname, partition);
     if (code == RXGEN_OPCODE) {
-       struct diskPartition *dpp = 
-           (struct diskPartition *)malloc(sizeof(struct diskPartition));
+       struct diskPartition *dpp = malloc(sizeof(struct diskPartition));
        code = AFSVolPartitionInfo(aconn, pname, dpp);
        if (!code) {
            strncpy(partition->name, dpp->name, 32);
@@ -617,7 +578,7 @@ UV_PartitionInfo64(afs_int32 server, char *pname,
            partition->minFree = dpp->minFree;
        }
        free(dpp);
-    } 
+    }
     if (code) {
        fprintf(STDERR, "Could not get information on partition %s\n", pname);
        PrintError("", code);
@@ -627,47 +588,118 @@ UV_PartitionInfo64(afs_int32 server, char *pname,
     return code;
 }
 
-/* old interface to create volume */
+/* old interface to create volumes */
 int
-UV_CreateVolume(afs_int32 aserver, afs_int32 apart, char *aname,
+UV_CreateVolume(afs_uint32 aserver, afs_int32 apart, char *aname,
                afs_uint32 * anewid)
 {
     afs_int32 code;
+    *anewid = 0;
     code = UV_CreateVolume2(aserver, apart, aname, 5000, 0, 0, 0, 0, anewid);
     return code;
 }
 
-/* create a volume, given a server, partition number, volume name --> sends
-* back new vol id in <anewid>*/
+/* less old interface to create volumes */
 int
-UV_CreateVolume2(afs_int32 aserver, afs_int32 apart, char *aname,
+UV_CreateVolume2(afs_uint32 aserver, afs_int32 apart, char *aname,
                 afs_int32 aquota, afs_int32 aspare1, afs_int32 aspare2,
                 afs_int32 aspare3, afs_int32 aspare4, afs_uint32 * anewid)
 {
+    afs_uint32 roid = 0, bkid = 0;
+    return UV_CreateVolume3(aserver, apart, aname, aquota, aspare1, aspare2,
+       aspare3, aspare4, anewid, &roid, &bkid);
+}
 
-    register struct rx_connection *aconn;
+/**
+ * Create a volume on the given server and partition
+ *
+ * @param aserver  server to create volume on
+ * @param spart  partition to create volume on
+ * @param aname  name of new volume
+ * @param aquota  quota for new volume
+ * @param anewid  contains the desired volume id for the new volume. If
+ *                *anewid == 0, a new id will be chosen, and will be placed
+ *                in *anewid when UV_CreateVolume3 returns.
+ * @param aroid  contains the desired RO volume id. If NULL, the RO id entry
+ *               will be unset. If *aroid == 0, an id will be chosen, and
+ *               will be placed in *anewid when UV_CreateVolume3 returns.
+ * @param abkid  same as aroid, except for the BK volume id instead of the
+ *               RO volume id.
+ * @return 0 on success, error code otherwise.
+ */
+int
+UV_CreateVolume3(afs_uint32 aserver, afs_int32 apart, char *aname,
+                afs_int32 aquota, afs_int32 aspare1, afs_int32 aspare2,
+                afs_int32 aspare3, afs_int32 aspare4, afs_uint32 * anewid,
+                afs_uint32 * aroid, afs_uint32 * abkid)
+{
+    struct rx_connection *aconn;
     afs_int32 tid;
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 error;
     afs_int32 rcode, vcode;
+    afs_int32 lastid;
     struct nvldbentry entry, storeEntry;       /*the new vldb entry */
     struct volintInfo tstatus;
 
     tid = 0;
-    aconn = (struct rx_connection *)0;
     error = 0;
 
+    memset(&storeEntry, 0, sizeof(struct nvldbentry));
+
     init_volintInfo(&tstatus);
     tstatus.maxquota = aquota;
 
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
-    /* next the next 3 available ids from the VLDB */
-    vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 3, anewid);
-    EGOTO1(cfail, vcode, "Could not get an Id for volume %s\n", aname);
+
+    if (aroid && *aroid) {
+       VPRINT1("Using RO volume ID %d.\n", *aroid);
+    }
+    if (abkid && *abkid) {
+       VPRINT1("Using BK volume ID %d.\n", *abkid);
+    }
+
+    if (*anewid) {
+        vcode = VLDB_GetEntryByID(*anewid, -1, &entry);
+       if (!vcode) {
+           fprintf(STDERR, "Volume ID %d already exists\n", *anewid);
+           return VVOLEXISTS;
+       }
+       VPRINT1("Using volume ID %d.\n", *anewid);
+    } else {
+       vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, anewid);
+       EGOTO1(cfail, vcode, "Could not get an Id for volume %s\n", aname);
+
+       if (aroid && *aroid == 0) {
+           vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, aroid);
+           EGOTO1(cfail, vcode, "Could not get an RO Id for volume %s\n", aname);
+       }
+
+       if (abkid && *abkid == 0) {
+           vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, abkid);
+           EGOTO1(cfail, vcode, "Could not get a BK Id for volume %s\n", aname);
+       }
+    }
+
+    /* rw,ro, bk id are related in the default case */
+    /* If caller specified RW id, but not RO/BK ids, have them be RW+1 and RW+2 */
+    lastid = *anewid;
+    if (aroid && *aroid != 0) {
+       lastid = max(lastid, *aroid);
+    }
+    if (abkid && *abkid != 0) {
+       lastid = max(lastid, *abkid);
+    }
+    if (aroid && *aroid == 0) {
+       *aroid = ++lastid;
+    }
+    if (abkid && *abkid == 0) {
+       *abkid = ++lastid;
+    }
 
     code =
-       AFSVolCreateVolume(aconn, apart, aname, volser_RW, 0, anewid, &tid);
-    EGOTO2(cfail, vcode, "Failed to create the volume %s %u \n", aname,
+       AFSVolCreateVolume_retry(aconn, apart, aname, volser_RW, 0, anewid, &tid);
+    EGOTO2(cfail, code, "Failed to create the volume %s %u \n", aname,
           *anewid);
 
     code = AFSVolSetInfo(aconn, tid, &tstatus);
@@ -675,7 +707,7 @@ UV_CreateVolume2(afs_int32 aserver, afs_int32 apart, char *aname,
        EPRINT(code, "Could not change quota, continuing...\n");
 
     code = AFSVolSetFlags(aconn, tid, 0);      /* bring it online (mark it InService */
-    EGOTO2(cfail, vcode, "Could not bring the volume %s %u online \n", aname,
+    EGOTO2(cfail, code, "Could not bring the volume %s %u online \n", aname,
           *anewid);
 
     VPRINT2("Volume %s %u created and brought online\n", aname, *anewid);
@@ -683,15 +715,15 @@ UV_CreateVolume2(afs_int32 aserver, afs_int32 apart, char *aname,
     /* set up the vldb entry for this volume */
     strncpy(entry.name, aname, VOLSER_OLDMAXVOLNAME);
     entry.nServers = 1;
-    entry.serverNumber[0] = aserver;   /* this should have another 
+    entry.serverNumber[0] = aserver;   /* this should have another
                                         * level of indirection later */
-    entry.serverPartition[0] = apart;  /* this should also have 
+    entry.serverPartition[0] = apart;  /* this should also have
                                         * another indirection level */
-    entry.flags = RW_EXISTS;   /* this records that rw volume exists */
-    entry.serverFlags[0] = ITSRWVOL;   /*this rep site has rw  vol */
+    entry.flags = VLF_RWEXISTS;        /* this records that rw volume exists */
+    entry.serverFlags[0] = VLSF_RWVOL; /*this rep site has rw  vol */
     entry.volumeId[RWVOL] = *anewid;
-    entry.volumeId[ROVOL] = *anewid + 1;       /* rw,ro, bk id are related in the default case */
-    entry.volumeId[BACKVOL] = *anewid + 2;
+    entry.volumeId[ROVOL] = aroid ? *aroid : 0;
+    entry.volumeId[BACKVOL] = abkid ? *abkid : 0;
     entry.cloneId = 0;
     /*map into right byte order, before passing to xdr, the stuff has to be in host
      * byte order. Xdr converts it into network order */
@@ -732,41 +764,36 @@ UV_CreateVolume2(afs_int32 aserver, afs_int32 apart, char *aname,
        rx_DestroyConnection(aconn);
     PrintError("", error);
     return error;
-
-
 }
 
 /* create a volume, given a server, partition number, volume name --> sends
 * back new vol id in <anewid>*/
 int
-UV_AddVLDBEntry(afs_int32 aserver, afs_int32 apart, char *aname,
+UV_AddVLDBEntry(afs_uint32 aserver, afs_int32 apart, char *aname,
                afs_uint32 aid)
 {
-    register struct rx_connection *aconn;
+    struct rx_connection *aconn;
     afs_int32 error;
     afs_int32 vcode;
     struct nvldbentry entry, storeEntry;       /*the new vldb entry */
 
+    memset(&storeEntry, 0, sizeof(struct nvldbentry));
+
     aconn = (struct rx_connection *)0;
     error = 0;
 
     /* set up the vldb entry for this volume */
     strncpy(entry.name, aname, VOLSER_OLDMAXVOLNAME);
     entry.nServers = 1;
-    entry.serverNumber[0] = aserver;   /* this should have another 
+    entry.serverNumber[0] = aserver;   /* this should have another
                                         * level of indirection later */
-    entry.serverPartition[0] = apart;  /* this should also have 
+    entry.serverPartition[0] = apart;  /* this should also have
                                         * another indirection level */
-    entry.flags = RW_EXISTS;   /* this records that rw volume exists */
-    entry.serverFlags[0] = ITSRWVOL;   /*this rep site has rw  vol */
+    entry.flags = VLF_RWEXISTS;        /* this records that rw volume exists */
+    entry.serverFlags[0] = VLSF_RWVOL; /*this rep site has rw  vol */
     entry.volumeId[RWVOL] = aid;
-#ifdef notdef
-    entry.volumeId[ROVOL] = anewid + 1;        /* rw,ro, bk id are related in the default case */
-    entry.volumeId[BACKVOL] = *anewid + 2;
-#else
     entry.volumeId[ROVOL] = 0;
     entry.volumeId[BACKVOL] = 0;
-#endif
     entry.cloneId = 0;
     /*map into right byte order, before passing to xdr, the stuff has to be in host
      * byte order. Xdr converts it into network order */
@@ -790,11 +817,11 @@ UV_AddVLDBEntry(afs_int32 aserver, afs_int32 apart, char *aname,
 }
 
 /* Delete the volume <volid>on <aserver> <apart>
- * the physical entry gets removed from the vldb only if the ref count 
+ * the physical entry gets removed from the vldb only if the ref count
  * becomes zero
  */
 int
-UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
+UV_DeleteVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid)
 {
     struct rx_connection *aconn = (struct rx_connection *)0;
     afs_int32 ttid = 0;
@@ -805,6 +832,8 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     afs_int32 avoltype = -1, vtype;
     int notondisk = 0, notinvldb = 0;
 
+    memset(&storeEntry, 0, sizeof(struct nvldbentry));
+
     /* Find and read bhe VLDB entry for this volume */
     code = ubik_VL_SetLock(cstruct, 0, avolid, avoltype, VLOP_DELETE);
     if (code) {
@@ -827,26 +856,15 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
 
     /* Whether volume is in the VLDB or not. Delete the volume on disk */
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
-    code = AFSVolTransCreate(aconn, avolid, apart, ITOffline, &ttid);
+
+    code = DoVolDelete(aconn, avolid, apart, "the", 0, NULL, NULL);
     if (code) {
-       if (code == VNOVOL) {
+       if (code == VNOVOL)
            notondisk = 1;
-       } else {
-           EGOTO1(error_exit, code, "Transaction on volume %u failed\n",
-                  avolid);
+       else {
+           error = code;
+           goto error_exit;
        }
-    } else {
-       VPRINT1("Trying to delete the volume %u ...", avolid);
-
-       code = AFSVolDeleteVolume(aconn, ttid);
-       EGOTO1(error_exit, code, "Could not delete the volume %u \n", avolid);
-
-       code = AFSVolEndTrans(aconn, ttid, &rcode);
-       code = (code ? code : rcode);
-       ttid = 0;
-       EGOTO1(error_exit, code,
-              "Could not end the transaction for the volume %u \n", avolid);
-       VDONE;
     }
 
     /* Now update the VLDB entry.
@@ -860,21 +878,21 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
        /* Its a backup volume, modify the VLDB entry. Check that the
         * backup volume is on the server/partition we asked to delete.
         */
-       if (!(entry.flags & BACK_EXISTS) || !Lp_Match(aserver, apart, &entry)) {
+       if (!(entry.flags & VLF_BACKEXISTS) || !Lp_Match(aserver, apart, &entry)) {
            notinvldb = 2;      /* Not on this server and partition */
            ERROR_EXIT(0);
        }
 
        VPRINT1("Marking the backup volume %u deleted in the VLDB\n", avolid);
 
-       entry.flags &= ~BACK_EXISTS;
+       entry.flags &= ~VLF_BACKEXISTS;
        vtype = BACKVOL;
     }
 
     else if (avolid == entry.volumeId[ROVOL]) {
        /* Its a read-only volume, modify the VLDB entry. Check that the
         * readonly volume is on the server/partition we asked to delete.
-        * If flags does not have RO_EIXSTS set, then this may mean the RO 
+        * If flags does not have RO_EIXSTS set, then this may mean the RO
         * hasn't been released (and could exist in VLDB).
         */
        if (!Lp_ROMatch(aserver, apart, &entry)) {
@@ -890,7 +908,7 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
        Lp_SetROValue(&entry, aserver, apart, 0, 0);    /* delete the site */
        entry.nServers--;
        if (!Lp_ROMatch(0, 0, &entry))
-           entry.flags &= ~RO_EXISTS;  /* This was the last ro volume */
+           entry.flags &= ~VLF_ROEXISTS;       /* This was the last ro volume */
        vtype = ROVOL;
     }
 
@@ -899,33 +917,19 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
         * Check that the readwrite volumes is on the server/partition we
         * asked to delete.
         */
-       if (!(entry.flags & RW_EXISTS) || !Lp_Match(aserver, apart, &entry)) {
+       if (!(entry.flags & VLF_RWEXISTS) || !Lp_Match(aserver, apart, &entry)) {
            notinvldb = 2;      /* Not found on this server and partition */
            ERROR_EXIT(0);
        }
 
-       /* Delete backup if it exists */
-       code =
-           AFSVolTransCreate(aconn, entry.volumeId[BACKVOL], apart,
-                             ITOffline, &ttid);
-       if (!code) {
-           if (verbose) {
-               fprintf(STDOUT, "Trying to delete the backup volume %u ...",
-                       entry.volumeId[BACKVOL]);
-               fflush(STDOUT);
+       if (entry.volumeId[BACKVOL]) {
+           /* Delete backup if it exists */
+           code = DoVolDelete(aconn, entry.volumeId[BACKVOL], apart,
+                              "the backup", 0, NULL, NULL);
+           if (code && code != VNOVOL) {
+               error = code;
+               goto error_exit;
            }
-           code = AFSVolDeleteVolume(aconn, ttid);
-           EGOTO1(error_exit, code, "Could not delete the volume %u \n",
-                  entry.volumeId[BACKVOL]);
-
-           code = AFSVolEndTrans(aconn, ttid, &rcode);
-           ttid = 0;
-           code = (code ? code : rcode);
-           EGOTO1(error_exit, code,
-                  "Could not end the transaction for the volume %u \n",
-                  entry.volumeId[BACKVOL]);
-           if (verbose)
-               fprintf(STDOUT, " done\n");
        }
 
        if (verbose)
@@ -933,15 +937,15 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
                    "Marking the readwrite volume %lu%s deleted in the VLDB\n",
                    (unsigned long)avolid,
                    ((entry.
-                     flags & BACK_EXISTS) ? ", and its backup volume," :
+                     flags & VLF_BACKEXISTS) ? ", and its backup volume," :
                     ""));
 
        Lp_SetRWValue(&entry, aserver, apart, 0L, 0L);
        entry.nServers--;
-       entry.flags &= ~(BACK_EXISTS | RW_EXISTS);
+       entry.flags &= ~(VLF_BACKEXISTS | VLF_RWEXISTS);
        vtype = RWVOL;
 
-       if (entry.flags & RO_EXISTS)
+       if (entry.flags & VLF_ROEXISTS)
            fprintf(STDERR, "WARNING: ReadOnly copy(s) may still exist\n");
     }
 
@@ -951,7 +955,7 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     }
 
     /* Either delete or replace the VLDB entry */
-    if ((entry.nServers <= 0) || !(entry.flags & (RO_EXISTS | RW_EXISTS))) {
+    if ((entry.nServers <= 0) || !(entry.flags & (VLF_ROEXISTS | VLF_RWEXISTS))) {
        if (verbose)
            fprintf(STDOUT,
                    "Last reference to the VLDB entry for %lu - deleting entry\n",
@@ -1006,7 +1010,7 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     if (islocked) {
        code =
            ubik_VL_ReleaseLock(cstruct, 0, avolid, -1,
-                               (LOCKREL_OPCODE | LOCKREL_AFSID | 
+                               (LOCKREL_OPCODE | LOCKREL_AFSID |
                                 LOCKREL_TIMESTAMP));
        if (code) {
            EPRINT1(code,
@@ -1029,11 +1033,19 @@ UV_DeleteVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
 jmp_buf env;
 int interrupt = 0;
 
-void
-sigint_handler(int x)
+static void *
+do_interrupt(void * unused)
 {
-    if (interrupt)
+    if (interrupt) {
+#if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
+       /* Avoid UNIX LWP from getting confused that our stack has suddenly
+        * changed. This will avoid some sanity checks, but until a better way
+        * is found, the only alternative is always crashing and burning on at
+        * least the stack-overflow check. */
+       lwp_cpptr->stack = NULL;
+#endif
        longjmp(env, 0);
+    }
 
     fprintf(STDOUT, "\nSIGINT handler: vos move operation in progress\n");
     fprintf(STDOUT,
@@ -1042,207 +1054,525 @@ sigint_handler(int x)
     fflush(STDOUT);
 
     interrupt = 1;
-    (void)signal(SIGINT, sigint_handler);
-
-    return;
+    return NULL;
 }
 
-/* Move volume <afromvol> on <afromserver> <afrompart> to <atoserver>
- * <atopart>.  The operation is almost idempotent.  The following
- * flags are recognized:
- * 
- *     RV_NOCLONE - don't use a copy clone
- */
+static void
+sigint_handler(int x)
+{
+#ifdef AFS_PTHREAD_ENV
+    do_interrupt(NULL);
+#else
+    IOMGR_SoftSig(do_interrupt, 0);
+#endif
+    (void)signal(SIGINT, sigint_handler);
+}
 
-int
-UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
-              afs_int32 atoserver, afs_int32 atopart, int flags)
+static int
+DoVolDelete(struct rx_connection *aconn, afs_uint32 avolid,
+           afs_int32 apart, char *ptypestring, afs_uint32 atoserver,
+           struct volser_status *volstatus, char *pprefix)
 {
-    struct rx_connection *toconn, *fromconn;
-    afs_int32 fromtid, totid, clonetid;
-    char vname[64];
-    char *volName = 0;
-    char tmpName[VOLSER_MAXVOLNAME + 1];
-    afs_int32 rcode;
-    afs_int32 fromDate;
-    struct restoreCookie cookie;
-    register afs_int32 vcode, code;
-    afs_uint32 newVol, volid, backupId;
-    struct volser_status tstatus;
-    struct destServer destination;
+    afs_int32 ttid = 0, code, rcode, error = 0;
+    char *prefix, *typestring;
+    int beverbose = 0;
 
-    struct nvldbentry entry, storeEntry;
-    int i, islocked, pntg;
-    afs_int32 error;
-    char in, lf;               /* for test code */
-    int same;
-    char hoststr[16];
+    if (pprefix)
+       prefix = pprefix;
+    else
+       prefix = "";
 
-#ifdef ENABLE_BUGFIX_1165
-    volEntries volumeInfo;
-    struct volintInfo *infop = 0;
-#endif
+    if (ptypestring) {
+       typestring = ptypestring;
+       beverbose = 1;
+    } else
+       typestring = "the";
 
-    islocked = 0;
-    fromconn = (struct rx_connection *)0;
-    toconn = (struct rx_connection *)0;
-    fromtid = 0;
-    totid = 0;
-    clonetid = 0;
-    error = 0;
-    volid = 0;
-    pntg = 0;
-    backupId = 0;
-    newVol = 0;
+    if (beverbose)
+       VPRINT3("%sDeleting %s volume %u ...", prefix, typestring, avolid);
 
-    /* support control-c processing */
-    if (setjmp(env))
-       goto mfail;
-    (void)signal(SIGINT, sigint_handler);
+    code =
+       AFSVolTransCreate_retry(aconn, avolid, apart, ITOffline, &ttid);
 
-    if (TESTC) {
-       fprintf(STDOUT,
-               "\nThere are three tests points - verifies all code paths through recovery.\n");
-       fprintf(STDOUT, "First test point - operation not started.\n");
-       fprintf(STDOUT, "...test here (y, n)? ");
-       fflush(STDOUT);
-       fscanf(stdin, "%c", &in);
-       fscanf(stdin, "%c", &lf);       /* toss away */
-       if (in == 'y') {
-           fprintf(STDOUT, "type control-c\n");
-           while (1) {
-               fprintf(stdout, ".");
-               fflush(stdout);
-               sleep(1);
-           }
-       }
-       /* or drop through */
+    /* return early and quietly for VNOVOL; don't continue the attempt to delete. */
+    if (code == VNOVOL) {
+       error = code;
+       goto dfail;
     }
 
-    vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
-    EGOTO1(mfail, vcode,
-          "Could not fetch the entry for the volume  %u from the VLDB \n",
-          afromvol);
+    EGOTO2(dfail, code, "%sFailed to start transaction on %u\n",
+          prefix, avolid);
 
-    if (entry.volumeId[RWVOL] != afromvol) {
-       fprintf(STDERR, "Only RW volume can be moved\n");
-       exit(1);
+    if (volstatus) {
+       code = AFSVolGetStatus(aconn, ttid, volstatus);
+       EGOTO2(dfail, code, "%sCould not get timestamp from volume %u\n",
+              prefix, avolid);
     }
 
-    vcode = ubik_VL_SetLock(cstruct, 0, afromvol, RWVOL, VLOP_MOVE);
-    EGOTO1(mfail, vcode, "Could not lock entry for volume %u \n", afromvol);
-    islocked = 1;
+    code =
+       AFSVolSetFlags(aconn, ttid,
+                      VTDeleteOnSalvage | VTOutOfService);
 
-    vcode = VLDB_GetEntryByID(afromvol, RWVOL, &entry);
-    EGOTO1(mfail, vcode,
-          "Could not fetch the entry for the volume  %u from the VLDB \n",
-          afromvol);
+    EGOTO2(dfail, code, "%sCould not set flags on volume %u \n",
+          prefix, avolid);
 
-    backupId = entry.volumeId[BACKVOL];
-    MapHostToNetwork(&entry);
+    if (atoserver) {
+       VPRINT1("%sSetting volume forwarding pointer ...", prefix);
+       AFSVolSetForwarding(aconn, ttid, atoserver);
+       VDONE;
+    }
 
-    if (!Lp_Match(afromserver, afrompart, &entry)) {
-       /* the from server and partition do not exist in the vldb entry corresponding to volid */
-       if (!Lp_Match(atoserver, atopart, &entry)) {
-           /* the to server and partition do not exist in the vldb entry corresponding to volid */
-           fprintf(STDERR, "The volume %lu is not on the specified site. \n",
-                   (unsigned long)afromvol);
-           fprintf(STDERR, "The current site is :");
-           for (i = 0; i < entry.nServers; i++) {
-               if (entry.serverFlags[i] == ITSRWVOL) {
-                   char pname[10];
-                   MapPartIdIntoName(entry.serverPartition[i], pname);
-                   fprintf(STDERR, " server %s partition %s \n",
-                           noresolve ? afs_inet_ntoa_r(entry.serverNumber[i], hoststr) :
-                            hostutil_GetNameByINet(entry.serverNumber[i]),
-                           pname);
-               }
-           }
-           vcode =
-               ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
-                         (LOCKREL_OPCODE | LOCKREL_AFSID |
-                          LOCKREL_TIMESTAMP));
-           EGOTO1(mfail, vcode,
-                  " Could not release lock on the VLDB entry for the volume %u \n",
-                  afromvol);
+    code = AFSVolDeleteVolume(aconn, ttid);
+    EGOTO2(dfail, code, "%sCould not delete volume %u\n", prefix, avolid);
 
-           return VOLSERVOLMOVED;
-       }
+dfail:
+    if (ttid) {
+        code = AFSVolEndTrans(aconn, ttid, &rcode);
+       ttid = 0;
+        if (!code)
+            code = rcode;
+        if (code) {
+            fprintf(STDERR, "%sCould not end transaction on %s volume %lu \n",
+                    prefix, typestring, (unsigned long)avolid);
+            if (!error)
+                error = code;
+        }
+    }
 
-       /* delete the volume afromvol on src_server */
-       /* from-info does not exist but to-info does =>
-        * we have already done the move, but the volume
-        * may still be existing physically on from fileserver
-        */
-       fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
-       fromtid = 0;
-       pntg = 1;
+    if (beverbose && !error)
+       VDONE;
+    return error;
+}
 
-       code =
-           AFSVolTransCreate(fromconn, afromvol, afrompart, ITOffline,
-                             &fromtid);
-       if (!code) {            /* volume exists - delete it */
-           VPRINT1("Setting flags on leftover source volume %u ...",
-                   afromvol);
-           code =
-               AFSVolSetFlags(fromconn, fromtid,
-                              VTDeleteOnSalvage | VTOutOfService);
-           EGOTO1(mfail, code,
-                  "Failed to set flags on the leftover source volume %u\n",
-                  afromvol);
-           VDONE;
+static int
+DoVolClone(struct rx_connection *aconn, afs_uint32 avolid,
+          afs_int32 apart, int type, afs_uint32 cloneid,
+          char *typestring, char *pname, char *vname, char *suffix,
+          struct volser_status *volstatus, afs_int32 *transPtr)
+{
+    char cname[64];
+    afs_int32 ttid = 0, btid = 0;
+    afs_int32 code = 0, rcode = 0;
+    afs_int32 error = 0;
+    int cloneexists = 1;
 
-           VPRINT1("Deleting leftover source volume %u ...", afromvol);
-           code = AFSVolDeleteVolume(fromconn, fromtid);
-           EGOTO1(mfail, code,
-                  "Failed to delete the leftover source volume %u\n",
-                  afromvol);
-           VDONE;
+    /* Test to see if the clone volume exists by trying to create
+     * a transaction on the clone volume. We've assumed the clone exists.
+     */
+    code = AFSVolTransCreate_retry(aconn, cloneid, apart, ITOffline, &btid);
+    if (code) {
+        if (code != VNOVOL) {
+           EPRINT2(code, "Could not reach the %s volume %lu\n",
+                    typestring, (unsigned long)cloneid);
+            error = code;
+            goto cfail;
+        }
+        cloneexists = 0;         /* clone volume does not exist */
+    }
+    if (btid) {
+        code = AFSVolEndTrans(aconn, btid, &rcode);
+        btid = 0;
+        if (code || rcode) {
+            fprintf(STDERR,
+                    "Could not end transaction on the previous %s volume %lu\n",
+                    typestring, (unsigned long)cloneid);
+            error = (code ? code : rcode);
+            goto cfail;
+        }
+    }
 
-           VPRINT1("Ending transaction on leftover source volume %u ...",
-                   afromvol);
-           code = AFSVolEndTrans(fromconn, fromtid, &rcode);
-           fromtid = 0;
-           if (!code)
-               code = rcode;
-           EGOTO1(mfail, code,
-                  "Could not end the transaction for the leftover source volume %u \n",
+    /* Now go ahead and try to clone the RW volume.
+     * First start a transaction on the RW volume
+     */
+    code = AFSVolTransCreate_retry(aconn, avolid, apart, ITBusy, &ttid);
+    if (code) {
+        fprintf(STDERR, "Could not start a transaction on the volume %lu\n",
+                (unsigned long)avolid);
+        error = code;
+        goto cfail;
+    }
+
+    /* Clone or reclone the volume, depending on whether the clone
+     * volume exists or not
+     */
+    if (cloneexists) {
+        VPRINT2("Re-cloning %s volume %u ...", typestring, cloneid);
+
+        code = AFSVolReClone(aconn, ttid, cloneid);
+        if (code) {
+            EPRINT2(code, "Could not re-clone %s volume %lu\n",
+                    typestring, (unsigned long)cloneid);
+            error = code;
+            goto cfail;
+        }
+    } else {
+        VPRINT2("Creating a new %s clone %u ...", typestring, cloneid);
+
+       if (!vname) {
+           strcpy(cname, pname);
+           strcat(cname, suffix);
+       }
+
+        code = AFSVolClone(aconn, ttid, 0, type, vname?vname:cname,
+                          &cloneid);
+        if (code) {
+            fprintf(STDERR, "Failed to clone the volume %lu\n",
+                    (unsigned long)avolid);
+            error = code;
+            goto cfail;
+        }
+    }
+
+    VDONE;
+
+    if (volstatus) {
+       VPRINT1("Getting status of parent volume %u...", avolid);
+       code = AFSVolGetStatus(aconn, ttid, volstatus);
+       if (code) {
+           fprintf(STDERR, "Failed to get the status of the parent volume %lu\n",
+                   (unsigned long)avolid);
+           error = code;
+            goto cfail;
+       }
+       VDONE;
+    }
+
+cfail:
+    if (ttid) {
+        code = AFSVolEndTrans(aconn, ttid, &rcode);
+        if (code || rcode) {
+            fprintf(STDERR, "Could not end transaction on the volume %lu\n",
+                    (unsigned long)avolid);
+            if (!error)
+                error = (code ? code : rcode);
+        }
+    }
+
+    if (btid) {
+        code = AFSVolEndTrans(aconn, btid, &rcode);
+        if (code || rcode) {
+            fprintf(STDERR,
+                    "Could not end transaction on the %s volume %lu\n",
+                    typestring, (unsigned long)cloneid);
+            if (!error)
+                error = (code ? code : rcode);
+        }
+    }
+    return error;
+}
+
+/* Convert volume from RO to RW; adjust the VLDB entry to match.
+ * The nvldbentry passed to us has already been MapHostToNetwork'd
+ * by the caller.
+ */
+
+int
+UV_ConvertRO(afs_uint32 server, afs_uint32 partition, afs_uint32 volid,
+               struct nvldbentry *entry)
+{
+    afs_int32 code, i, same;
+    struct nvldbentry checkEntry, storeEntry;
+    afs_int32 vcode;
+    afs_int32 rwindex = 0;
+    afs_uint32 rwserver = 0;
+    afs_int32 roindex = 0;
+    afs_uint32 roserver = 0;
+    struct rx_connection *aconn;
+
+    memset(&storeEntry, 0, sizeof(struct nvldbentry));
+
+    vcode =
+       ubik_VL_SetLock(cstruct, 0, entry->volumeId[RWVOL], RWVOL,
+                 VLOP_MOVE);
+    if (vcode) {
+       fprintf(STDERR,
+               "Unable to lock volume %lu, code %d\n",
+               (unsigned long)entry->volumeId[RWVOL],vcode);
+       PrintError("", vcode);
+       return -1;
+    }
+
+    /* make sure the VLDB entry hasn't changed since we started */
+    memset(&checkEntry, 0, sizeof(checkEntry));
+    vcode = VLDB_GetEntryByID(volid, -1, &checkEntry);
+    if (vcode) {
+       fprintf(STDERR,
+                "Could not fetch the entry for volume %lu from VLDB\n",
+                (unsigned long)volid);
+       PrintError("convertROtoRW ", vcode);
+       code = vcode;
+       goto error_exit;
+    }
+
+    MapHostToNetwork(&checkEntry);
+    entry->flags &= ~VLOP_ALLOPERS;  /* clear any stale lock operation flags */
+    entry->flags |= VLOP_MOVE;        /* set to match SetLock operation above */
+    if (memcmp(entry, &checkEntry, sizeof(*entry)) != 0) {
+        fprintf(STDERR,
+                "VLDB entry for volume %lu has changed; please reissue the command.\n",
+                (unsigned long)volid);
+        code = -1;
+        goto error_exit;
+    }
+
+    /* extract information from the original entry */
+    for (i = 0; i < entry->nServers; i++) {
+       if (entry->serverFlags[i] & VLSF_RWVOL) {
+           rwindex = i;
+           rwserver = entry->serverNumber[i];
+       /*  rwpartition = entry->serverPartition[i]; */
+           if (roserver)
+               break;
+       } else if ((entry->serverFlags[i] & VLSF_ROVOL) && !roserver) {
+           same = VLDB_IsSameAddrs(server, entry->serverNumber[i], &code);
+           if (code) {
+               fprintf(STDERR,
+                       "Failed to get info about server's %d address(es) from vlserver (err=%d); aborting call!\n",
+                       server, code);
+               code = ENOENT;
+               goto error_exit;
+           }
+           if (same) {
+               roindex = i;
+               roserver = entry->serverNumber[i];
+       /*      ropartition = entry->serverPartition[i]; */
+               if (rwserver)
+                    break;
+           }
+       }
+    }
+
+    aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
+    code = AFSVolConvertROtoRWvolume(aconn, partition, volid);
+    if (code) {
+       fprintf(STDERR,
+               "Converting RO volume %lu to RW volume failed with code %d\n",
+               (unsigned long)volid, code);
+       PrintError("convertROtoRW ", code);
+       goto error_exit;
+    }
+    /* Update the VLDB to match what we did on disk as much as possible.  */
+    /* If the converted RO was in the VLDB, make it look like the new RW. */
+    if (roserver) {
+       entry->serverFlags[roindex] = VLSF_RWVOL;
+    } else {
+       /* Add a new site entry for the newly created RW.  It's possible
+        * (but unlikely) that we are already at MAXNSERVERS and that this
+        * new site will invalidate the whole VLDB entry;  however,
+        * VLDB_ReplaceEntry will detect this and return VL_BADSERVER,
+        * so we need no extra guard logic here.
+        */
+       afs_int32 newrwindex = entry->nServers;
+       (entry->nServers)++;
+       entry->serverNumber[newrwindex] = server;
+       entry->serverPartition[newrwindex] = partition;
+       entry->serverFlags[newrwindex] = VLSF_RWVOL;
+    }
+    entry->flags |= VLF_RWEXISTS;
+    entry->flags &= ~VLF_BACKEXISTS;
+
+    /* if the old RW was in the VLDB, remove it by decrementing the number */
+    /* of servers, replacing the RW entry with the last entry, and zeroing */
+    /* out the last entry. */
+    if (rwserver) {
+       (entry->nServers)--;
+       if (rwindex != entry->nServers) {
+           entry->serverNumber[rwindex] = entry->serverNumber[entry->nServers];
+           entry->serverPartition[rwindex] =
+               entry->serverPartition[entry->nServers];
+           entry->serverFlags[rwindex] = entry->serverFlags[entry->nServers];
+           entry->serverNumber[entry->nServers] = 0;
+           entry->serverPartition[entry->nServers] = 0;
+           entry->serverFlags[entry->nServers] = 0;
+       }
+    }
+    entry->flags &= ~VLF_ROEXISTS;
+    for (i = 0; i < entry->nServers; i++) {
+       if (entry->serverFlags[i] & VLSF_ROVOL) {
+           if (!(entry->serverFlags[i] & (VLSF_DONTUSE | VLSF_NEWREPSITE)))
+               entry->flags |= VLF_ROEXISTS;
+       }
+    }
+    MapNetworkToHost(entry, &storeEntry);
+    code =
+       VLDB_ReplaceEntry(entry->volumeId[RWVOL], RWVOL, &storeEntry,
+                         (LOCKREL_OPCODE | LOCKREL_AFSID |
+                          LOCKREL_TIMESTAMP));
+    if (code) {
+       fprintf(STDERR,
+               "Warning: volume converted, but vldb update failed with code %d!\n",
+               code);
+    }
+
+  error_exit:
+    vcode = UV_LockRelease(entry->volumeId[RWVOL]);
+    if (vcode) {
+       fprintf(STDERR,
+               "Unable to unlock volume %lu, code %d\n",
+               (unsigned long)entry->volumeId[RWVOL],vcode);
+       PrintError("", vcode);
+    }
+    return code;
+}
+
+
+/* Move volume <afromvol> on <afromserver> <afrompart> to <atoserver>
+ * <atopart>.  The operation is almost idempotent.  The following
+ * flags are recognized:
+ *
+ *     RV_NOCLONE - don't use a copy clone
+ */
+
+int
+UV_MoveVolume2(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
+              afs_uint32 atoserver, afs_int32 atopart, int flags)
+{
+    /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
+     * be changing during the move */
+    struct rx_connection * volatile toconn;
+    struct rx_connection * volatile fromconn;
+    afs_int32 volatile fromtid;
+    afs_int32 volatile totid;
+    afs_int32 volatile clonetid;
+    afs_uint32 volatile newVol;
+    afs_uint32 volatile volid;
+    afs_uint32 volatile backupId;
+    int volatile islocked;
+    int volatile pntg;
+
+    char vname[64];
+    char *volName = 0;
+    char tmpName[VOLSER_MAXVOLNAME + 1];
+    afs_int32 rcode;
+    afs_int32 fromDate;
+    afs_int32 tmp;
+    afs_uint32 tmpVol;
+    struct restoreCookie cookie;
+    afs_int32 vcode, code;
+    struct volser_status tstatus;
+    struct destServer destination;
+
+    struct nvldbentry entry, storeEntry;
+    int i;
+    afs_int32 error;
+    char in, lf;               /* for test code */
+    int same;
+    char hoststr[16];
+
+#ifdef ENABLE_BUGFIX_1165
+    volEntries volumeInfo;
+    struct volintInfo *infop = 0;
+#endif
+
+    islocked = 0;
+    fromconn = (struct rx_connection *)0;
+    toconn = (struct rx_connection *)0;
+    fromtid = 0;
+    totid = 0;
+    clonetid = 0;
+    error = 0;
+    volid = 0;
+    pntg = 0;
+    backupId = 0;
+    newVol = 0;
+
+    /* support control-c processing */
+    if (setjmp(env))
+       goto mfail;
+    (void)signal(SIGINT, sigint_handler);
+
+    if (TESTC) {
+       fprintf(STDOUT,
+               "\nThere are three tests points - verifies all code paths through recovery.\n");
+       fprintf(STDOUT, "First test point - operation not started.\n");
+       fprintf(STDOUT, "...test here (y, n)? ");
+       fflush(STDOUT);
+       if (fscanf(stdin, "%c", &in) < 1)
+           in = 0;
+       if (fscanf(stdin, "%c", &lf) < 0) {
+           /* toss away; don't care */
+       }
+       if (in == 'y') {
+           fprintf(STDOUT, "type control-c\n");
+           while (1) {
+               fprintf(stdout, ".");
+               fflush(stdout);
+               sleep(1);
+           }
+       }
+       /* or drop through */
+    }
+
+    vcode = VLDB_GetEntryByID(afromvol, -1, &entry);
+    EGOTO1(mfail, vcode,
+          "Could not fetch the entry for the volume  %u from the VLDB \n",
+          afromvol);
+
+    if (entry.volumeId[RWVOL] != afromvol) {
+       fprintf(STDERR, "Only RW volume can be moved\n");
+       exit(1);
+    }
+
+    vcode = ubik_VL_SetLock(cstruct, 0, afromvol, RWVOL, VLOP_MOVE);
+    EGOTO1(mfail, vcode, "Could not lock entry for volume %u \n", afromvol);
+    islocked = 1;
+
+    vcode = VLDB_GetEntryByID(afromvol, RWVOL, &entry);
+    EGOTO1(mfail, vcode,
+          "Could not fetch the entry for the volume  %u from the VLDB \n",
+          afromvol);
+
+    backupId = entry.volumeId[BACKVOL];
+    MapHostToNetwork(&entry);
+
+    if (!Lp_Match(afromserver, afrompart, &entry)) {
+       /* the from server and partition do not exist in the vldb entry corresponding to volid */
+       if (!Lp_Match(atoserver, atopart, &entry)) {
+           /* the to server and partition do not exist in the vldb entry corresponding to volid */
+           fprintf(STDERR, "The volume %lu is not on the specified site. \n",
+                   (unsigned long)afromvol);
+           fprintf(STDERR, "The current site is :");
+           for (i = 0; i < entry.nServers; i++) {
+               if (entry.serverFlags[i] == VLSF_RWVOL) {
+                   char pname[10];
+                   MapPartIdIntoName(entry.serverPartition[i], pname);
+                   fprintf(STDERR, " server %s partition %s \n",
+                           noresolve ? afs_inet_ntoa_r(entry.serverNumber[i], hoststr) :
+                            hostutil_GetNameByINet(entry.serverNumber[i]),
+                           pname);
+               }
+           }
+           vcode =
+               ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
+                         (LOCKREL_OPCODE | LOCKREL_AFSID |
+                          LOCKREL_TIMESTAMP));
+           EGOTO1(mfail, vcode,
+                  " Could not release lock on the VLDB entry for the volume %u \n",
                   afromvol);
-           VDONE;
+
+           return VOLSERVOLMOVED;
        }
 
-       /*delete the backup volume now */
-       fromtid = 0;
-       code =
-           AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
-                             &fromtid);
-       if (!code) {            /* backup volume exists - delete it */
-           VPRINT1("Setting flags on leftover backup volume %u ...",
-                   backupId);
-           code =
-               AFSVolSetFlags(fromconn, fromtid,
-                              VTDeleteOnSalvage | VTOutOfService);
-           EGOTO1(mfail, code,
-                  "Failed to set flags on the backup volume %u\n", backupId);
-           VDONE;
+       /* delete the volume afromvol on src_server */
+       /* from-info does not exist but to-info does =>
+        * we have already done the move, but the volume
+        * may still be existing physically on from fileserver
+        */
+       fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
+       pntg = 1;
 
-           VPRINT1("Deleting leftover backup volume %u ...", backupId);
-           code = AFSVolDeleteVolume(fromconn, fromtid);
-           EGOTO1(mfail, code,
-                  "Could not delete the leftover backup volume %u\n",
-                  backupId);
-           VDONE;
+       code = DoVolDelete(fromconn, afromvol, afrompart,
+                          "leftover", 0, NULL, NULL);
+       if (code && code != VNOVOL) {
+           error = code;
+           goto mfail;
+       }
 
-           VPRINT1("Ending transaction on leftover backup volume %u ...",
-                   backupId);
-           code = AFSVolEndTrans(fromconn, fromtid, &rcode);
-           fromtid = 0;
-           if (!code)
-               code = rcode;
-           EGOTO1(mfail, code,
-                  "Could not end the transaction for the leftover backup volume %u\n",
-                  backupId);
-           VDONE;
+       code = DoVolDelete(fromconn, backupId, afrompart,
+                          "leftover backup", 0, NULL, NULL);
+       if (code && code != VNOVOL) {
+           error = code;
+           goto mfail;
        }
 
        fromtid = 0;
@@ -1251,8 +1581,8 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     }
 
     /* From-info matches the vldb info about volid,
-     * its ok start the move operation, the backup volume 
-     * on the old site is deleted in the process 
+     * its ok start the move operation, the backup volume
+     * on the old site is deleted in the process
      */
     if (afrompart == atopart) {
        same = VLDB_IsSameAddrs(afromserver, atoserver, &error);
@@ -1270,14 +1600,15 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     pntg = 1;
     toconn = UV_Bind(atoserver, AFSCONF_VOLUMEPORT);   /* get connections to the servers */
     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
-    fromtid = totid = 0;       /* initialize to uncreated */
+    totid = 0; /* initialize to uncreated */
 
     /* ***
      * clone the read/write volume locally.
      * ***/
 
     VPRINT1("Starting transaction on source volume %u ...", afromvol);
-    code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
+    code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
+    fromtid = tmp;
     EGOTO1(mfail, code, "Failed to create transaction on the volume %u\n",
           afromvol);
     VDONE;
@@ -1286,8 +1617,9 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        /* Get a clone id */
        VPRINT1("Allocating new volume id for clone of volume %u ...",
                afromvol);
-       newVol = 0;
-       vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &newVol);
+       tmpVol = 0;
+       vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
+       newVol = tmpVol;
        EGOTO1(mfail, vcode,
               "Could not get an ID for the clone of volume %u from the VLDB\n",
               afromvol);
@@ -1297,7 +1629,8 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        VPRINT1("Cloning source volume %u ...", afromvol);
        strcpy(vname, "move-clone-temp");
        code =
-           AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &newVol);
+           AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &tmpVol);
+       newVol = tmpVol;
        EGOTO1(mfail, code, "Failed to clone the source volume %u\n",
               afromvol);
        VDONE;
@@ -1327,9 +1660,11 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     if (!(flags & RV_NOCLONE)) {
        /* All of this is to get the fromDate */
        VPRINT1("Starting transaction on the cloned volume %u ...", newVol);
+       tmp = clonetid;
        code =
-           AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline,
-                             &clonetid);
+           AFSVolTransCreate_retry(fromconn, newVol, afrompart, ITOffline,
+                             &tmp);
+       clonetid = tmp;
        EGOTO1(mfail, code,
               "Failed to start a transaction on the cloned volume%u\n",
               newVol);
@@ -1379,37 +1714,21 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
     /* create a volume on the target machine */
     volid = afromvol;
-    code = AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
-    if (!code) {
-       /* Delete the existing volume.
-        * While we are deleting the volume in these steps, the transaction
-        * we started against the cloned volume (clonetid above) will be
-        * sitting idle. It will get cleaned up after 600 seconds
-        */
-       VPRINT1("Deleting pre-existing volume %u on destination ...", volid);
-       code = AFSVolDeleteVolume(toconn, totid);
-       EGOTO1(mfail, code,
-              "Could not delete the pre-existing volume %u on destination\n",
-              volid);
-       VDONE;
-
-       VPRINT1
-           ("Ending transaction on pre-existing volume %u on destination ...",
-            volid);
-       code = AFSVolEndTrans(toconn, totid, &rcode);
-       totid = 0;
-       if (!code)
-           code = rcode;
-       EGOTO1(mfail, code,
-              "Could not end the transaction on pre-existing volume %u on destination\n",
-              volid);
-       VDONE;
+    code = DoVolDelete(toconn, volid, atopart,
+                      "pre-existing destination", 0, NULL, NULL);
+    if (code && code != VNOVOL) {
+       error = code;
+       goto mfail;
     }
 
     VPRINT1("Creating the destination volume %u ...", volid);
+    tmp = totid;
+    tmpVol = volid;
     code =
-       AFSVolCreateVolume(toconn, atopart, volName, volser_RW, volid, &volid,
-                          &totid);
+       AFSVolCreateVolume(toconn, atopart, volName, volser_RW, volid, &tmpVol,
+                          &tmp);
+    totid = tmp;
+    volid = tmpVol;
     EGOTO1(mfail, code, "Failed to create the destination volume %u\n",
           volid);
     VDONE;
@@ -1464,7 +1783,9 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
      * ***/
 
     VPRINT1("Starting transaction on source volume %u ...", afromvol);
-    code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
+    tmp = fromtid;
+    code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
+    fromtid = tmp;
     EGOTO1(mfail, code,
           "Failed to create a transaction on the source volume %u\n",
           afromvol);
@@ -1518,15 +1839,18 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
     Lp_SetRWValue(&entry, afromserver, afrompart, atoserver, atopart);
     MapNetworkToHost(&entry, &storeEntry);
-    storeEntry.flags &= ~BACK_EXISTS;
+    storeEntry.flags &= ~VLF_BACKEXISTS;
 
     if (TESTC) {
        fprintf(STDOUT,
                "Second test point - operation in progress but not complete.\n");
        fprintf(STDOUT, "...test here (y, n)? ");
        fflush(STDOUT);
-       fscanf(stdin, "%c", &in);
-       fscanf(stdin, "%c", &lf);       /* toss away */
+       if (fscanf(stdin, "%c", &in) < 1)
+           in = 0;
+       if (fscanf(stdin, "%c", &lf) < 0) {
+           /* toss away, don't care */
+       }
        if (in == 'y') {
            fprintf(STDOUT, "type control-c\n");
            while (1) {
@@ -1558,8 +1882,11 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
                "Third test point - operation complete but no cleanup.\n");
        fprintf(STDOUT, "...test here (y, n)? ");
        fflush(STDOUT);
-       fscanf(stdin, "%c", &in);
-       fscanf(stdin, "%c", &lf);       /* toss away */
+       if (fscanf(stdin, "%c", &in) < 1)
+           in = 0;
+       if (fscanf(stdin, "%c", &lf) < 0) {
+           /* toss away; don't care */
+       }
        if (in == 'y') {
            fprintf(STDOUT, "type control-c\n");
            while (1) {
@@ -1570,21 +1897,6 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        }
        /* or drop through */
     }
-#ifdef notdef
-    /* This is tricky.  File server is very stupid, and if you mark the volume
-     * as VTOutOfService, it may mark the *good* instance (if you're moving
-     * between partitions on the same machine) as out of service.  Since
-     * we're cleaning this code up in DEcorum, we're just going to kludge around
-     * it for now by removing this call. */
-    /* already out of service, just zap it now */
-    code =
-       AFSVolSetFlags(fromconn, fromtid, VTDeleteOnSalvage | VTOutOfService);
-    if (code) {
-       fprintf(STDERR,
-               "Failed to set the flags to make the old source volume offline\n");
-       goto mfail;
-    }
-#endif
     if (atoserver != afromserver) {
        /* set forwarding pointer for moved volumes */
        VPRINT1("Setting forwarding pointer for volume %u ...", afromvol);
@@ -1612,69 +1924,25 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
           afromvol);
     VDONE;
 
-    /* Delete the backup volume on the original site */
-    VPRINT1("Creating transaction for backup volume %u on source ...",
-           backupId);
-    code =
-       AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline, &fromtid);
-    VDONE;
-    if (!code) {
-       VPRINT1("Setting flags on backup volume %u on source ...", backupId);
-       code =
-           AFSVolSetFlags(fromconn, fromtid,
-                          VTDeleteOnSalvage | VTOutOfService);
-       EGOTO1(mfail, code,
-              "Failed to set the flags on the backup volume %u on the source\n",
-              backupId);
-       VDONE;
+    code = DoVolDelete(fromconn, backupId, afrompart,
+                      "source backup", 0, NULL, NULL);
+    if (code && code != VNOVOL) {
+       error = code;
+       goto mfail;
+    }
 
-       VPRINT1("Deleting the backup volume %u on the source ...", backupId);
-       code = AFSVolDeleteVolume(fromconn, fromtid);
-       EGOTO1(mfail, code,
-              "Failed to delete the backup volume %u on the source\n",
-              backupId);
-       VDONE;
-
-       VPRINT1("Ending transaction on backup volume %u on source ...",
-               backupId);
-       code = AFSVolEndTrans(fromconn, fromtid, &rcode);
-       fromtid = 0;
-       if (!code)
-           code = rcode;
-       EGOTO1(mfail, code,
-              "Failed to end the transaction on the backup volume %u on the source\n",
-              backupId);
-       VDONE;
-    } else
-       code = 0;               /* no backup volume? that's okay */
+    code = 0;          /* no backup volume? that's okay */
 
     fromtid = 0;
     if (!(flags & RV_NOCLONE)) {
-       VPRINT1("Starting transaction on the cloned volume %u ...", newVol);
-       code =
-           AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline,
-                             &clonetid);
-       EGOTO1(mfail, code,
-              "Failed to start a transaction on the cloned volume%u\n",
-              newVol);
-       VDONE;
-
-       /* now delete the clone */
-       VPRINT1("Deleting the cloned volume %u ...", newVol);
-       code = AFSVolDeleteVolume(fromconn, clonetid);
-       EGOTO1(mfail, code, "Failed to delete the cloned volume %u\n",
-              newVol);
-       VDONE;
+       code = DoVolDelete(fromconn, newVol, afrompart,
+                          "cloned", 0, NULL, NULL);
+       if (code && code != VNOVOL) {
+           error = code;
+           goto mfail;
+       }
 
-       VPRINT1("Ending transaction on cloned volume %u ...", newVol);
-       code = AFSVolEndTrans(fromconn, clonetid, &rcode);
-       if (!code)
-           code = rcode;
-       clonetid = 0;
-       EGOTO1(mfail, code,
-              "Failed to end the transaction on the cloned volume %u\n",
-              newVol);
-       VDONE;
+       code = 0;       /* clone missing? that's okay */
     }
 
     /* fall through */
@@ -1684,8 +1952,11 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        fprintf(STDOUT, "Fourth test point - operation complete.\n");
        fprintf(STDOUT, "...test here (y, n)? ");
        fflush(STDOUT);
-       fscanf(stdin, "%c", &in);
-       fscanf(stdin, "%c", &lf);       /* toss away */
+       if (fscanf(stdin, "%c", &in) < 1)
+           in = 0;
+       if (fscanf(stdin, "%c", &lf) < 0) {     /* toss away */
+           /* don't care */
+       }
        if (in == 'y') {
            fprintf(STDOUT, "type control-c\n");
            while (1) {
@@ -1699,7 +1970,7 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
     /* normal cleanup code */
 
-    if (entry.flags & RO_EXISTS)
+    if (entry.flags & VLF_ROEXISTS)
        fprintf(STDERR, "WARNING : readOnly copies still exist \n");
 
     if (islocked) {
@@ -1822,9 +2093,9 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     }
     MapHostToNetwork(&entry);
 
-    /* Delete either the volume on the source location or the target location. 
+    /* Delete either the volume on the source location or the target location.
      * If the vldb entry still points to the source location, then we know the
-     * volume move didn't finish so we remove the volume from the target 
+     * volume move didn't finish so we remove the volume from the target
      * location. Otherwise, we remove the volume from the source location.
      */
     if (Lp_Match(afromserver, afrompart, &entry)) {    /* didn't move - delete target volume */
@@ -1835,46 +2106,22 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        }
 
        if (volid && toconn) {
-           VPRINT1
-               ("Recovery: Creating transaction for destination volume %u ...",
-                volid);
-           code =
-               AFSVolTransCreate(toconn, volid, atopart, ITOffline, &totid);
-
-           if (!code) {
-               VDONE;
-
-               VPRINT1
-                   ("Recovery: Setting flags on destination volume %u ...",
-                    volid);
-               AFSVolSetFlags(toconn, totid,
-                              VTDeleteOnSalvage | VTOutOfService);
-               VDONE;
-
-               VPRINT1("Recovery: Deleting destination volume %u ...",
-                       volid);
-               AFSVolDeleteVolume(toconn, totid);
-               VDONE;
-
-               VPRINT1
-                   ("Recovery: Ending transaction on destination volume %u ...",
-                    volid);
-               AFSVolEndTrans(toconn, totid, &rcode);
-               VDONE;
-           } else {
-               VPRINT1
-                   ("\nRecovery: Unable to start transaction on destination volume %u.\n",
-                    afromvol);
+           code = DoVolDelete(toconn, volid, atopart,
+                              "destination", 0, NULL, "Recovery:");
+           if (code == VNOVOL) {
+               EPRINT1(code, "Recovery: Failed to start transaction on %u\n", volid);
            }
-       }
+        }
 
        /* put source volume on-line */
        if (fromconn) {
            VPRINT1("Recovery: Creating transaction on source volume %u ...",
                    afromvol);
+           tmp = fromtid;
            code =
-               AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
-                                 &fromtid);
+               AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy,
+                                 &tmp);
+           fromtid = tmp;
            if (!code) {
                VDONE;
 
@@ -1903,95 +2150,27 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
        /* delete backup volume */
        if (fromconn) {
-           VPRINT1("Recovery: Creating transaction on backup volume %u ...",
-                   backupId);
-           code =
-               AFSVolTransCreate(fromconn, backupId, afrompart, ITOffline,
-                                 &fromtid);
-           if (!code) {
-               VDONE;
-
-               VPRINT1("Recovery: Setting flags on backup volume %u ...",
-                       backupId);
-               AFSVolSetFlags(fromconn, fromtid,
-                              VTDeleteOnSalvage | VTOutOfService);
-               VDONE;
-
-               VPRINT1("Recovery: Deleting backup volume %u ...", backupId);
-               AFSVolDeleteVolume(fromconn, fromtid);
-               VDONE;
-
-               VPRINT1
-                   ("Recovery: Ending transaction on backup volume %u ...",
-                    backupId);
-               AFSVolEndTrans(fromconn, fromtid, &rcode);
-               VDONE;
-           } else {
-               VPRINT1
-                   ("\nRecovery: Unable to start transaction on backup volume %u.\n",
-                    backupId);
+           code = DoVolDelete(fromconn, backupId, afrompart,
+                              "backup", 0, NULL, "Recovery:");
+           if (code == VNOVOL) {
+               EPRINT1(code, "Recovery: Failed to start transaction on %u\n", backupId);
            }
 
-           /* delete source volume */
-           VPRINT1("Recovery: Creating transaction on source volume %u ...",
-                   afromvol);
-           code =
-               AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
-                                 &fromtid);
-           if (!code) {
-               VDONE;
-
-               VPRINT1("Recovery: Setting flags on backup volume %u ...",
-                       afromvol);
-               AFSVolSetFlags(fromconn, fromtid,
-                              VTDeleteOnSalvage | VTOutOfService);
-               VDONE;
-
-               if (atoserver != afromserver) {
-                   VPRINT("Recovery: Setting volume forwarding pointer ...");
-                   AFSVolSetForwarding(fromconn, fromtid, atoserver);
-                   VDONE;
-               }
-
-               VPRINT1("Recovery: Deleting source volume %u ...", afromvol);
-               AFSVolDeleteVolume(fromconn, fromtid);
-               VDONE;
-
-               VPRINT1
-                   ("Recovery: Ending transaction on source volume %u ...",
-                    afromvol);
-               AFSVolEndTrans(fromconn, fromtid, &rcode);
-               VDONE;
-           } else {
-               VPRINT1
-                   ("\nRecovery: Unable to start transaction on source volume %u.\n",
-                    afromvol);
+           code = DoVolDelete(fromconn, afromvol, afrompart, "source",
+                              (atoserver != afromserver)?atoserver:0,
+                       NULL, NULL);
+           if (code == VNOVOL) {
+               EPRINT1(code, "Failed to start transaction on %u\n", afromvol);
            }
        }
     }
 
     /* common cleanup - delete local clone */
     if (newVol) {
-       VPRINT1("Recovery: Creating transaction on clone volume %u ...",
-               newVol);
-       code =
-           AFSVolTransCreate(fromconn, newVol, afrompart, ITOffline,
-                             &clonetid);
-       if (!code) {
-           VDONE;
-
-           VPRINT1("Recovery: Deleting clone volume %u ...", newVol);
-           AFSVolDeleteVolume(fromconn, clonetid);
-           VDONE;
-
-           VPRINT1("Recovery: Ending transaction on clone volume %u ...",
-                   newVol);
-           AFSVolEndTrans(fromconn, clonetid, &rcode);
-           VDONE;
-       } else {
-           VPRINT1
-               ("\nRecovery: Unable to start transaction on source volume %u.\n",
-                afromvol);
+       code = DoVolDelete(fromconn, newVol, afrompart,
+                          "clone", 0, NULL, "Recovery:");
+       if (code == VNOVOL) {
+           EPRINT1(code, "Recovery: Failed to start transaction on %u\n", newVol);
        }
     }
 
@@ -2002,7 +2181,6 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        ubik_VL_ReleaseLock(cstruct, 0, afromvol, -1,
                            (LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP));
        VDONE;
-       islocked = 0;
     }
   done:                        /* routine cleanup */
     if (volName)
@@ -2025,8 +2203,8 @@ UV_MoveVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
 
 int
-UV_MoveVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
-             afs_int32 atoserver, afs_int32 atopart)
+UV_MoveVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
+             afs_uint32 atoserver, afs_int32 atopart)
 {
     return UV_MoveVolume2(afromvol, afromserver, afrompart,
                          atoserver, atopart, 0);
@@ -2037,7 +2215,7 @@ UV_MoveVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
  * <atopart>.  The new volume is named by <atovolname>.  The new volume
  * has ID <atovolid> if that is nonzero; otherwise a new ID is allocated
  * from the VLDB.  the following flags are supported:
- * 
+ *
  *     RV_RDONLY  - target volume is RO
  *     RV_OFFLINE - leave target volume offline
  *     RV_CPINCR  - do incremental dump if target exists
@@ -2045,28 +2223,34 @@ UV_MoveVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
  *     RV_NOCLONE - don't use a copy clone
  */
 int
-UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
-              char *atovolname, afs_int32 atoserver, afs_int32 atopart,
+UV_CopyVolume2(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
+              char *atovolname, afs_uint32 atoserver, afs_int32 atopart,
               afs_uint32 atovolid, int flags)
 {
-    struct rx_connection *toconn, *fromconn;
-    afs_int32 fromtid, totid, clonetid;
+    /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
+     * be changing during the copy */
+    int volatile pntg;
+    afs_int32 volatile clonetid;
+    afs_int32 volatile totid;
+    afs_int32 volatile fromtid;
+    struct rx_connection * volatile fromconn;
+    struct rx_connection * volatile toconn;
+    afs_uint32 volatile cloneVol;
+
     char vname[64];
     afs_int32 rcode;
     afs_int32 fromDate, cloneFromDate;
     struct restoreCookie cookie;
-    register afs_int32 vcode, code;
-    afs_uint32 cloneVol, newVol;
+    afs_int32 vcode, code;
+    afs_uint32 newVol;
     afs_int32 volflag;
     struct volser_status tstatus;
     struct destServer destination;
-
     struct nvldbentry entry, newentry, storeEntry;
-    int islocked, pntg;
     afs_int32 error;
-    int justclone = 0;
+    afs_int32 tmp;
+    afs_uint32 tmpVol;
 
-    islocked = 0;
     fromconn = (struct rx_connection *)0;
     toconn = (struct rx_connection *)0;
     fromtid = 0;
@@ -2092,12 +2276,6 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
     fromtid = totid = 0;       /* initialize to uncreated */
 
-
-    /* check if we can shortcut and use a local clone instead of a full copy */
-    if (afromserver == atoserver && afrompart == atopart) {
-       justclone = 1;
-    }
-
     /* ***
      * clone the read/write volume locally.
      * ***/
@@ -2105,8 +2283,10 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     cloneVol = 0;
     if (!(flags & RV_NOCLONE)) {
        VPRINT1("Starting transaction on source volume %u ...", afromvol);
-       code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
-                                &fromtid);
+       tmp = fromtid;
+       code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy,
+                                &tmp);
+       fromtid = tmp;
        EGOTO1(mfail, code, "Failed to create transaction on the volume %u\n",
               afromvol);
        VDONE;
@@ -2115,7 +2295,9 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        VPRINT1("Allocating new volume id for clone of volume %u ...",
                afromvol);
        cloneVol = 0;
-       vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &cloneVol);
+       tmpVol = cloneVol;
+       vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
+       cloneVol = tmpVol;
        EGOTO1(mfail, vcode,
           "Could not get an ID for the clone of volume %u from the VLDB\n",
           afromvol);
@@ -2139,9 +2321,11 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
        /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
        VPRINT1("Cloning source volume %u ...", afromvol);
        strcpy(vname, "copy-clone-temp");
+       tmpVol = cloneVol;
        code =
            AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname,
-                       &cloneVol);
+                       &tmpVol);
+       cloneVol = tmpVol;
        EGOTO1(mfail, code, "Failed to clone the source volume %u\n",
               afromvol);
        VDONE;
@@ -2164,9 +2348,11 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
     if (!(flags & RV_NOCLONE)) {
        VPRINT1("Starting transaction on the cloned volume %u ...", cloneVol);
+       tmp = clonetid;
        code =
-           AFSVolTransCreate(fromconn, cloneVol, afrompart, ITOffline,
-                         &clonetid);
+           AFSVolTransCreate_retry(fromconn, cloneVol, afrompart, ITOffline,
+                         &tmp);
+       clonetid = tmp;
        EGOTO1(mfail, code,
               "Failed to start a transaction on the cloned volume%u\n",
               cloneVol);
@@ -2195,7 +2381,9 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
     /* create a volume on the target machine */
     cloneFromDate = 0;
-    code = AFSVolTransCreate(toconn, newVol, atopart, ITOffline, &totid);
+    tmp = totid;
+    code = AFSVolTransCreate_retry(toconn, newVol, atopart, ITOffline, &tmp);
+    totid = tmp;
     if (!code) {
        if ((flags & RV_CPINCR)) {
            VPRINT1("Getting status of pre-existing volume %u ...", newVol);
@@ -2243,10 +2431,12 @@ UV_CopyVolume2(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     }
 
     VPRINT1("Creating the destination volume %u ...", newVol);
+    tmp = totid;
     code =
        AFSVolCreateVolume(toconn, atopart, atovolname,
                           (flags & RV_RDONLY) ? volser_RO : volser_RW,
-                          newVol, &newVol, &totid);
+                          newVol, &newVol, &tmp);
+    totid = tmp;
     EGOTO1(mfail, code, "Failed to create the destination volume %u\n",
           newVol);
     VDONE;
@@ -2304,7 +2494,9 @@ cpincr:
      * ***/
 
     VPRINT1("Starting transaction on source volume %u ...", afromvol);
-    code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
+    tmp = fromtid;
+    code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
+    fromtid = tmp;
     EGOTO1(mfail, code,
           "Failed to create a transaction on the source volume %u\n",
           afromvol);
@@ -2320,7 +2512,7 @@ cpincr:
                      &cookie);
     EGOTO1(mfail, code,
           "Failed to do the%s dump from old site to new site\n",
-          afromvol);
+          (flags & RV_NOCLONE) ? "" : " incremental");
     VDONE;
 
     VPRINT1("Setting volume flags on destination volume %u ...", newVol);
@@ -2353,32 +2545,15 @@ cpincr:
 
     fromtid = 0;
 
-    if (!(flags & RV_NOCLONE)) {
-       VPRINT1("Starting transaction on the cloned volume %u ...", cloneVol);
-       code =
-           AFSVolTransCreate(fromconn, cloneVol, afrompart, ITOffline,
-                             &clonetid);
-       EGOTO1(mfail, code,
-              "Failed to start a transaction on the cloned volume%u\n",
-              cloneVol);
-       VDONE;
-
-       /* now delete the clone */
-       VPRINT1("Deleting the cloned volume %u ...", cloneVol);
-       code = AFSVolDeleteVolume(fromconn, clonetid);
-       EGOTO1(mfail, code, "Failed to delete the cloned volume %u\n",
-              cloneVol);
-       VDONE;
-
-       VPRINT1("Ending transaction on cloned volume %u ...", cloneVol);
-       code = AFSVolEndTrans(fromconn, clonetid, &rcode);
-       if (!code)
-           code = rcode;
-       clonetid = 0;
-       EGOTO1(mfail, code,
-              "Failed to end the transaction on the cloned volume %u\n",
-              cloneVol);
-       VDONE;
+    if (!(flags & RV_NOCLONE)) {
+       code = DoVolDelete(fromconn, cloneVol, afrompart,
+                          "cloned", 0, NULL, NULL);
+       if (code && code != VNOVOL) {
+           error = code;
+           goto mfail;
+       }
+
+       code = 0;               /* clone missing? that's ok */
     }
 
     if (!(flags & RV_NOVLDB)) {
@@ -2387,8 +2562,8 @@ cpincr:
        newentry.nServers = 1;
        newentry.serverNumber[0] = atoserver;
        newentry.serverPartition[0] = atopart;
-       newentry.flags = (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
-       newentry.serverFlags[0] = (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
+       newentry.flags = (flags & RV_RDONLY) ? VLF_ROEXISTS : VLF_RWEXISTS;
+       newentry.serverFlags[0] = (flags & RV_RDONLY) ? VLSF_ROVOL : VLSF_RWVOL;
        newentry.volumeId[RWVOL] = newVol;
        newentry.volumeId[ROVOL] = (flags & RV_RDONLY) ? newVol : 0;
        newentry.volumeId[BACKVOL] = 0;
@@ -2502,27 +2677,8 @@ cpincr:
 
     /* common cleanup - delete local clone */
     if (cloneVol) {
-       VPRINT1("Recovery: Creating transaction on clone volume %u ...",
-               cloneVol);
-       code =
-           AFSVolTransCreate(fromconn, cloneVol, afrompart, ITOffline,
-                             &clonetid);
-       if (!code) {
-           VDONE;
-
-           VPRINT1("Recovery: Deleting clone volume %u ...", cloneVol);
-           AFSVolDeleteVolume(fromconn, clonetid);
-           VDONE;
-
-           VPRINT1("Recovery: Ending transaction on clone volume %u ...",
-                   cloneVol);
-           AFSVolEndTrans(fromconn, clonetid, &rcode);
-           VDONE;
-       } else {
-           VPRINT1
-               ("\nRecovery: Unable to start transaction on clone volume %u.\n",
-                cloneVol);
-       }
+       DoVolDelete(fromconn, cloneVol, afrompart, "clone", 0, NULL,
+                   "Recovery:");
     }
 
   done:                        /* routine cleanup */
@@ -2540,8 +2696,8 @@ cpincr:
 
 
 int
-UV_CopyVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
-             char *atovolname, afs_int32 atoserver, afs_int32 atopart)
+UV_CopyVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
+             char *atovolname, afs_uint32 atoserver, afs_int32 atopart)
 {
     return UV_CopyVolume2(afromvol, afromserver, afrompart,
                           atovolname, atoserver, atopart, 0, 0);
@@ -2549,21 +2705,20 @@ UV_CopyVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
 
 
 
-/* Make a new backup of volume <avolid> on <aserver> and <apart> 
- * if one already exists, update it 
+/* Make a new backup of volume <avolid> on <aserver> and <apart>
+ * if one already exists, update it
  */
 
 int
-UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
+UV_BackupVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid)
 {
     struct rx_connection *aconn = (struct rx_connection *)0;
     afs_int32 ttid = 0, btid = 0;
     afs_uint32 backupID;
     afs_int32 code = 0, rcode = 0;
-    char vname[VOLSER_MAXVOLNAME + 1];
     struct nvldbentry entry, storeEntry;
     afs_int32 error = 0;
-    int vldblocked = 0, vldbmod = 0, backexists = 1;
+    int vldblocked = 0, vldbmod = 0;
 
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
 
@@ -2582,7 +2737,7 @@ UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     /* These operations require the VLDB be locked since it means the VLDB
      * will change or the vldb is already locked.
      */
-    if (!(entry.flags & BACK_EXISTS) ||        /* backup volume doesnt exist */
+    if (!(entry.flags & VLF_BACKEXISTS) ||     /* backup volume doesnt exist */
        (entry.flags & VLOP_ALLOPERS) ||        /* vldb lock already held */
        (entry.volumeId[BACKVOL] == INVALID_BID)) {     /* no assigned backup volume id */
 
@@ -2618,7 +2773,7 @@ UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     backupID = entry.volumeId[BACKVOL];
     if (backupID == INVALID_BID) {
        /* Get a backup volume id from the VLDB and update the vldb
-        * entry with it. 
+        * entry with it.
         */
        code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &backupID);
        if (code) {
@@ -2632,89 +2787,21 @@ UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
        vldbmod = 1;
     }
 
-    /* Test to see if the backup volume exists by trying to create
-     * a transaction on the backup volume. We've assumed the backup exists.
-     */
-    code = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
-    if (code) {
-       if (code != VNOVOL) {
-           fprintf(STDERR, "Could not reach the backup volume %lu\n",
-                   (unsigned long)backupID);
-           error = code;
-           goto bfail;
-       }
-       backexists = 0;         /* backup volume does not exist */
-    }
-    if (btid) {
-       code = AFSVolEndTrans(aconn, btid, &rcode);
-       btid = 0;
-       if (code || rcode) {
-           fprintf(STDERR,
-                   "Could not end transaction on the previous backup volume %lu\n",
-                   (unsigned long)backupID);
-           error = (code ? code : rcode);
-           goto bfail;
-       }
-    }
-
-    /* Now go ahead and try to clone the RW volume.
-     * First start a transaction on the RW volume 
-     */
-    code = AFSVolTransCreate(aconn, avolid, apart, ITBusy, &ttid);
+    code = DoVolClone(aconn, avolid, apart, backupVolume, backupID, "backup",
+                     entry.name, NULL, ".backup", NULL, NULL);
     if (code) {
-       fprintf(STDERR, "Could not start a transaction on the volume %lu\n",
-               (unsigned long)avolid);
        error = code;
        goto bfail;
     }
 
-    /* Clone or reclone the volume, depending on whether the backup 
-     * volume exists or not
-     */
-    if (backexists) {
-       VPRINT1("Re-cloning backup volume %u ...", backupID);
-
-       code = AFSVolReClone(aconn, ttid, backupID);
-       if (code) {
-           fprintf(STDERR, "Could not re-clone backup volume %lu\n",
-                   (unsigned long)backupID);
-           error = code;
-           goto bfail;
-       }
-    } else {
-       VPRINT1("Creating a new backup clone %u ...", backupID);
-
-       strcpy(vname, entry.name);
-       strcat(vname, ".backup");
-
-       code = AFSVolClone(aconn, ttid, 0, backupVolume, vname, &backupID);
-       if (code) {
-           fprintf(STDERR, "Failed to clone the volume %lu\n",
-                   (unsigned long)avolid);
-           error = code;
-           goto bfail;
-       }
-    }
-
-    /* End the transaction on the RW volume */
-    code = AFSVolEndTrans(aconn, ttid, &rcode);
-    ttid = 0;
-    if (code || rcode) {
-       fprintf(STDERR,
-               "Failed to end the transaction on the rw volume %lu\n",
-               (unsigned long)avolid);
-       error = (code ? code : rcode);
-       goto bfail;
-    }
-
-    /* Mork vldb as backup exists */
-    if (!(entry.flags & BACK_EXISTS)) {
-       entry.flags |= BACK_EXISTS;
+    /* Mark vldb as backup exists */
+    if (!(entry.flags & VLF_BACKEXISTS)) {
+       entry.flags |= VLF_BACKEXISTS;
        vldbmod = 1;
     }
 
     /* Now go back to the backup volume and bring it on line */
-    code = AFSVolTransCreate(aconn, backupID, apart, ITOffline, &btid);
+    code = AFSVolTransCreate_retry(aconn, backupID, apart, ITOffline, &btid);
     if (code) {
        fprintf(STDERR,
                "Failed to start a transaction on the backup volume %lu\n",
@@ -2741,8 +2828,6 @@ UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
        goto bfail;
     }
 
-    VDONE;
-
     /* Will update the vldb below */
 
   bfail:
@@ -2804,18 +2889,18 @@ UV_BackupVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid)
     return error;
 }
 
-/* Make a new clone of volume <avolid> on <aserver> and <apart> 
+/* Make a new clone of volume <avolid> on <aserver> and <apart>
  * using volume ID <acloneid>, or a new ID allocated from the VLDB.
  * The new volume is named by <aname>, or by appending ".clone" to
  * the existing name if <aname> is NULL.  The following flags are
  * supported:
- * 
+ *
  *     RV_RDONLY  - target volume is RO
  *     RV_OFFLINE - leave target volume offline
  */
 
 int
-UV_CloneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid,
+UV_CloneVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 avolid,
               afs_uint32 acloneid, char *aname, int flags)
 {
     struct rx_connection *aconn = (struct rx_connection *)0;
@@ -2823,8 +2908,8 @@ UV_CloneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid,
     afs_int32 code = 0, rcode = 0;
     char vname[VOLSER_MAXVOLNAME + 1];
     afs_int32 error = 0;
-    int backexists = 1;
     volEntries volumeInfo;
+    int type = 0;
 
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
 
@@ -2858,84 +2943,23 @@ UV_CloneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid,
        VDONE;
     }
 
-    /* Test to see if the clone volume exists by trying to create
-     * a transaction on the clone volume. We've assumed the clone exists.
-     */
-    /* XXX I wonder what happens if the clone has some other parent... */
-    code = AFSVolTransCreate(aconn, acloneid, apart, ITOffline, &btid);
-    if (code) {
-       if (code != VNOVOL) {
-           fprintf(STDERR, "Could not reach the clone volume %lu\n",
-                   (unsigned long)acloneid);
-           error = code;
-           goto bfail;
-       }
-       backexists = 0;         /* backup volume does not exist */
-    }
-    if (btid) {
-       code = AFSVolEndTrans(aconn, btid, &rcode);
-       btid = 0;
-       if (code || rcode) {
-           fprintf(STDERR,
-                   "Could not end transaction on the previous clone volume %lu\n",
-                   (unsigned long)acloneid);
-           error = (code ? code : rcode);
-           goto bfail;
-       }
-    }
+    if (flags & RV_RWONLY)
+       type = readwriteVolume;
+    else if (flags & RV_RDONLY)
+       type = readonlyVolume;
+    else
+       type = backupVolume;
 
-    /* Now go ahead and try to clone the RW volume.
-     * First start a transaction on the RW volume 
-     */
-    code = AFSVolTransCreate(aconn, avolid, apart, ITBusy, &ttid);
+    code = DoVolClone(aconn, avolid, apart, type, acloneid, "clone",
+                     NULL, aname, NULL, NULL, NULL);
     if (code) {
-       fprintf(STDERR, "Could not start a transaction on the volume %lu\n",
-               (unsigned long)avolid);
        error = code;
        goto bfail;
     }
 
-    /* Clone or reclone the volume, depending on whether the backup 
-     * volume exists or not
-     */
-    if (backexists) {
-       VPRINT1("Re-cloning clone volume %u ...", acloneid);
-
-       code = AFSVolReClone(aconn, ttid, acloneid);
-       if (code) {
-           fprintf(STDERR, "Could not re-clone backup volume %lu\n",
-                   (unsigned long)acloneid);
-           error = code;
-           goto bfail;
-       }
-    } else {
-       VPRINT1("Creating a new clone %u ...", acloneid);
-
-       code = AFSVolClone(aconn, ttid, 0,
-                          (flags & RV_RDONLY) ? readonlyVolume : backupVolume,
-                          aname, &acloneid);
-       if (code) {
-           fprintf(STDERR, "Failed to clone the volume %lu\n",
-                   (unsigned long)avolid);
-           error = code;
-           goto bfail;
-       }
-    }
-
-    /* End the transaction on the RW volume */
-    code = AFSVolEndTrans(aconn, ttid, &rcode);
-    ttid = 0;
-    if (code || rcode) {
-       fprintf(STDERR,
-               "Failed to end the transaction on the rw volume %lu\n",
-               (unsigned long)avolid);
-       error = (code ? code : rcode);
-       goto bfail;
-    }
-
     /* Now go back to the backup volume and bring it on line */
     if (!(flags & RV_OFFLINE)) {
-       code = AFSVolTransCreate(aconn, acloneid, apart, ITOffline, &btid);
+       code = AFSVolTransCreate_retry(aconn, acloneid, apart, ITOffline, &btid);
        if (code) {
            fprintf(STDERR,
                    "Failed to start a transaction on the clone volume %lu\n",
@@ -2963,8 +2987,6 @@ UV_CloneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid,
        }
     }
 
-    VDONE;
-
   bfail:
     if (ttid) {
        code = AFSVolEndTrans(aconn, ttid, &rcode);
@@ -2994,45 +3016,34 @@ UV_CloneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 avolid,
     return error;
 }
 
-static int
-DelVol(struct rx_connection *conn, afs_uint32 vid, afs_int32 part,
-       afs_int32 flags)
-{
-    afs_int32 acode, ccode, rcode, tid;
-    ccode = rcode = tid = 0;
-
-    acode = AFSVolTransCreate(conn, vid, part, flags, &tid);
-    if (!acode) {              /* It really was there */
-       acode = AFSVolDeleteVolume(conn, tid);
-       if (acode) {
-           fprintf(STDERR, "Failed to delete volume %lu.\n",
-                   (unsigned long)vid);
-           PrintError("", acode);
-       }
-       ccode = AFSVolEndTrans(conn, tid, &rcode);
-       if (!ccode)
-           ccode = rcode;
-       if (ccode) {
-           fprintf(STDERR, "Failed to end transaction on volume %lu.\n",
-                   (unsigned long)vid);
-           PrintError("", ccode);
-       }
-    }
-
-    return acode;
-}
-
-#define ONERROR(ec, ep, es) if (ec) { fprintf(STDERR, (es), (ep)); error = (ec); goto rfail; }
-#define ERROREXIT(ec) { error = (ec); goto rfail; }
+#define ONERROR(ec, ep, es) do { \
+    if (ec) { \
+        fprintf(STDERR, (es), (ep)); \
+        error = (ec); \
+        goto rfail; \
+    } \
+} while (0)
+#define ONERROR0(ec, es) do { \
+    if (ec) { \
+        fprintf(STDERR, (es)); \
+        error = (ec); \
+        goto rfail; \
+    } \
+} while (0)
+#define ERROREXIT(ec) do { \
+    error = (ec); \
+    goto rfail; \
+} while (0)
 
-/* Get a "transaction" on this replica.  Create the volume 
+/* Get a "transaction" on this replica.  Create the volume
  * if necessary.  Return the time from which a dump should
  * be made (0 if it's a new volume)
  */
 static int
 GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
         struct rx_connection **connPtr, afs_int32 * transPtr,
-        afs_int32 * crtimePtr, afs_int32 * uptimePtr)
+        afs_uint32 * crtimePtr, afs_uint32 * uptimePtr,
+        afs_int32 *origflags, afs_uint32 tmpVolId)
 {
     afs_uint32 volid;
     struct volser_status tstatus;
@@ -3051,15 +3062,55 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
        goto fail;              /* server is down */
 
     volid = vldbEntryPtr->volumeId[ROVOL];
-    if (volid)
+
+    if (volid) {
        code =
-           AFSVolTransCreate(*connPtr, volid,
+           AFSVolTransCreate_retry(*connPtr, volid,
                              vldbEntryPtr->serverPartition[index], ITOffline,
                              transPtr);
 
+       if (!code && (origflags[index] & VLSF_DONTUSE)) {
+           /* If RO_DONTUSE is set, this is supposed to be an entirely new
+            * site. Don't trust any data on it, since it is possible we
+            * have encountered some temporary volume from some other
+            * incomplete volume operation. It is difficult to detect if
+            * that has happened vs if this is a legit volume, so just
+            * delete it to be safe. */
+
+           VPRINT1("Deleting extant RO_DONTUSE site on %s...",
+                    noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
+                                                serverNumber[index], hoststr) :
+                    hostutil_GetNameByINet(vldbEntryPtr->
+                                          serverNumber[index]));
+
+           code = AFSVolDeleteVolume(*connPtr, *transPtr);
+           if (code) {
+               PrintError("Failed to delete RO_DONTUSE site: ", code);
+               goto fail;
+           }
+
+           tcode = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
+           *transPtr = 0;
+           if (!tcode) {
+               tcode = rcode;
+           }
+           if (tcode) {
+               PrintError("Failed to end transaction on RO_DONTUSE site: ",
+                          tcode);
+               goto fail;
+           }
+
+           VDONE;
+
+           /* emulate what TransCreate would have returned, so we try to
+            * create the volume below */
+           code = VNOVOL;
+       }
+    }
+
     /* If the volume does not exist, create it */
     if (!volid || code) {
-       char volname[64];
+       char volname[VL_MAXNAMELEN];
         char hoststr[16];
 
        if (volid && (code != VNOVOL)) {
@@ -3068,13 +3119,20 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
            goto fail;
        }
 
-       strcpy(volname, vldbEntryPtr->name);
-       strcat(volname, ".readonly");
+       strlcpy(volname, vldbEntryPtr->name, sizeof(volname));
+
+       if (strlcat(volname,
+                   tmpVolId?".roclone":".readonly",
+                   sizeof(volname)) >= sizeof(volname)) {
+           code = ENOMEM;
+           PrintError("Volume name is too long\n", code);
+           goto fail;
+       }
 
        if (verbose) {
            fprintf(STDOUT,
                    "Creating new volume %lu on replication site %s: ",
-                   (unsigned long)volid,
+                   tmpVolId?(unsigned long)tmpVolId:(unsigned long)volid,
                     noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
                                                 serverNumber[index], hoststr) :
                     hostutil_GetNameByINet(vldbEntryPtr->
@@ -3083,10 +3141,11 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
        }
 
        code =
-           AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
-                              volname, volser_RO,
-                              vldbEntryPtr->volumeId[RWVOL], &volid,
-                              transPtr);
+         AFSVolCreateVolume(*connPtr, vldbEntryPtr->serverPartition[index],
+                            volname, volser_RO,
+                            vldbEntryPtr->volumeId[RWVOL],
+                            tmpVolId?&tmpVolId:&volid,
+                            transPtr);
        if (code) {
            PrintError("Failed to create the ro volume: ", code);
            goto fail;
@@ -3106,12 +3165,12 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
     }
 
     /* Otherwise, the transaction did succeed, so get the creation date of the
-     * latest RO volume on the replication site 
+     * latest RO volume on the replication site
      */
     else {
        VPRINT2("Updating existing ro volume %u on %s ...\n", volid,
                 noresolve ? afs_inet_ntoa_r(vldbEntryPtr->
-                                            serverNumber[index], hoststr) : 
+                                            serverNumber[index], hoststr) :
                 hostutil_GetNameByINet(vldbEntryPtr->serverNumber[index]));
 
        code = AFSVolGetStatus(*connPtr, *transPtr, &tstatus);
@@ -3120,6 +3179,20 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
                       code);
            goto fail;
        }
+       if (tmpVolId) {
+           code = AFSVolEndTrans(*connPtr, *transPtr, &rcode);
+           *transPtr = 0;
+           if (!code)
+               code = rcode;
+           if (!code)
+               code = DoVolClone(*connPtr, volid,
+                                 vldbEntryPtr->serverPartition[index],
+                                 readonlyVolume, tmpVolId, "temporary",
+                                 vldbEntryPtr->name, NULL, ".roclone", NULL,
+                                 transPtr);
+           if (code)
+               goto fail;
+       }
        *crtimePtr = CLOCKADJ(tstatus.creationDate);
        *uptimePtr = CLOCKADJ(tstatus.updateDate);
     }
@@ -3132,7 +3205,7 @@ GetTrans(struct nvldbentry *vldbEntryPtr, afs_int32 index,
        *transPtr = 0;
        if (!tcode)
            tcode = rcode;
-       if (tcode)
+       if (tcode && tcode != ENOENT)
            PrintError("Could not end transaction on a ro volume: ", tcode);
     }
 
@@ -3155,32 +3228,137 @@ SimulateForwardMultiple(struct rx_connection *fromconn, afs_int32 fromtid,
     return 0;
 }
 
-
-/* UV_ReleaseVolume()
- *    Release volume <afromvol> on <afromserver> <afrompart> to all
- *    its RO sites (full release). Unless the previous release was
- *    incomplete: in which case we bring the remaining incomplete
- *    volumes up to date with the volumes that were released
- *    successfully.
- *    forceflag: Performs a full release.
+/**
+ * Check if a trans has timed out, and recreate it if necessary.
+ *
+ * @param[in] aconn  RX connection to the relevant server
+ * @param[inout] atid  Transaction ID to check; if we recreated the trans,
+ *                     contains the new trans ID on success
+ * @param[in] apart  Partition for the transaction
+ * @param[in] astat  The status of the original transaction
  *
- *    Will create a clone from the RW, then dump the clone out to 
- *    the remaining replicas. If there is more than 1 RO sites,
- *    ensure that the VLDB says at least one RO is available all
- *    the time: Influences when we write back the VLDB entry.
+ * @return operation status
+ *  @retval 0 existing transaction is still valid, or we managed to recreate
+ *            the trans successfully
+ *  @retval nonzero Fatal error; bail out
  */
+static int
+CheckTrans(struct rx_connection *aconn, afs_int32 *atid, afs_int32 apart,
+           struct volser_status *astat)
+{
+    struct volser_status new_status;
+    afs_int32 code;
+
+    memset(&new_status, 0, sizeof(new_status));
+    code = AFSVolGetStatus(aconn, *atid, &new_status);
+    if (code) {
+       if (code == ENOENT) {
+           *atid = 0;
+           VPRINT1("Old transaction on cloned volume %lu timed out, "
+                   "restarting transaction\n", (long unsigned) astat->volID);
+           code = AFSVolTransCreate_retry(aconn, astat->volID, apart,
+                                          ITBusy, atid);
+           if (code) {
+               PrintError("Failed to recreate cloned RO volume transaction\n",
+                          code);
+               return 1;
+           }
+
+           memset(&new_status, 0, sizeof(new_status));
+           code = AFSVolGetStatus(aconn, *atid, &new_status);
+           if (code) {
+               PrintError("Failed to get status on recreated transaction\n",
+                          code);
+               return 1;
+           }
+
+           if (memcmp(&new_status, astat, sizeof(new_status)) != 0) {
+               PrintError("Recreated transaction on cloned RO volume, but "
+                          "the volume has changed!\n", 0);
+               return 1;
+           }
+       } else {
+           PrintError("Unable to get status of current cloned RO transaction\n",
+                      code);
+           return 1;
+       }
+    } else {
+       if (memcmp(&new_status, astat, sizeof(new_status)) != 0) {
+           /* sanity check */
+           PrintError("Internal error: current GetStatus does not match "
+                      "original GetStatus?\n", 0);
+           return 1;
+       }
+    }
+
+    return 0;
+}
+
+static void
+PutTrans(afs_int32 *vldbindex, struct replica *replicas,
+        struct rx_connection **toconns, struct release *times,
+        afs_int32 volcount)
+{
+    afs_int32 s, code = 0, rcode = 0;
+    /* End the transactions and destroy the connections */
+    for (s = 0; s < volcount; s++) {
+       if (replicas[s].trans) {
+           code = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
+
+           replicas[s].trans = 0;
+           if (!code)
+               code = rcode;
+           if (code) {
+               if ((s == 0) || (code != ENOENT)) {
+                   PrintError("Could not end transaction on a ro volume: ",
+                              code);
+               } else {
+                   PrintError
+                       ("Transaction timed out on a ro volume. Will retry.\n",
+                        0);
+                   if (times[s].vldbEntryIndex < *vldbindex)
+                       *vldbindex = times[s].vldbEntryIndex;
+               }
+           }
+       }
+       if (toconns[s])
+           rx_DestroyConnection(toconns[s]);
+       toconns[s] = 0;
+    }
+}
 
+/**
+ * Release a volume to read-only sites
+ *
+ * Release volume <afromvol> on <afromserver> <afrompart> to all its RO
+ * sites (complete release), unless the previous release was incomplete
+ * or new sites were added without changes to the read-write volume, in
+ * which case we bring the remaining volumes up to date with the volumes
+ * that were previously released successfully.
+ *
+ * Will create a clone from the RW, then dump the clone out to
+ * the remaining replicas. If there is more than 1 RO sites,
+ * ensure that the VLDB says at least one RO is available all
+ * the time: Influences when we write back the VLDB entry.
+ *
+ * @param[in] afromvol      volume to be released
+ * @param[in] afromserver   server containing afromvol
+ * @param[in] afrompart     partition containing afromvol
+ * @param[in] flags         bitmap of options
+ *                            REL_COMPLETE  - force a complete release
+ *                            REL_FULLDUMPS - force full dumps
+ */
 int
-UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
-                afs_int32 afrompart, int forceflag)
+UV_ReleaseVolume(afs_uint32 afromvol, afs_uint32 afromserver,
+                afs_int32 afrompart, int flags)
 {
     char vname[64];
     afs_int32 code = 0;
     afs_int32 vcode, rcode, tcode;
-    afs_uint32 cloneVolId, roVolId;
+    afs_uint32 cloneVolId = 0, roVolId;
     struct replica *replicas = 0;
     struct nvldbentry entry, storeEntry;
-    int i, volcount, m, fullrelease, vldbindex;
+    int i, volcount = 0, m, vldbindex;
     int failure;
     struct restoreCookie cookie;
     struct rx_connection **toconns = 0;
@@ -3198,8 +3376,8 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
     manyDests tr;
     manyResults results;
     int rwindex, roindex, roclone, roexists;
-    afs_uint32 rwcrdate = 0;
-    afs_uint32 rwupdate, clcrdate;
+    afs_uint32 rwcrdate = 0, rwupdate = 0;
+    afs_uint32 clcrdate;
     struct rtime {
        int validtime;
        afs_uint32 uptime;
@@ -3207,9 +3385,28 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
     int releasecount = 0;
     struct volser_status volstatus;
     char hoststr[16];
-
-    memset((char *)remembertime, 0, sizeof(remembertime));
-    memset((char *)&results, 0, sizeof(results));
+    afs_int32 origflags[NMAXNSERVERS];
+    struct volser_status orig_status;
+    int notreleased = 0;
+    int tried_justnewsites = 0;
+    int justnewsites = 0; /* are we just trying to release to new RO sites? */
+    int sites = 0; /* number of ro sites */
+    int new_sites = 0; /* number of ro sites markes as new */
+
+    typedef enum {
+        CR_PARTIAL    = 0x0000, /**< just new sites added or recover from a previous failed release */
+        CR_FORCED     = 0x0001, /**< complete: forced by caller */
+        CR_LAST_OK    = 0x0002, /**< complete: no sites have been marked as new release */
+        CR_ALL_NEW    = 0x0004, /**< complete: all sites have been marked as new release */
+        CR_NEW_RW     = 0x0008, /**< complete: read-write has changed */
+        CR_RO_MISSING = 0x0010, /**< complete: ro clone is missing */
+    } complete_release_t;
+
+    complete_release_t complete_release = CR_PARTIAL;
+
+    memset(remembertime, 0, sizeof(remembertime));
+    memset(&results, 0, sizeof(results));
+    memset(origflags, 0, sizeof(origflags));
 
     vcode = ubik_VL_SetLock(cstruct, 0, afromvol, RWVOL, VLOP_RELEASE);
     if (vcode != VL_RERELEASE)
@@ -3248,13 +3445,13 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
     roclone = ((roindex == -1) ? 0 : 1);
     rwindex = Lp_GetRwIndex(&entry);
     if (rwindex < 0)
-       ONERROR(VOLSERNOVOL, 0, "There is no RW volume \n");
+       ONERROR0(VOLSERNOVOL, "There is no RW volume \n");
 
     /* Make sure we have a RO volume id to work with */
     if (entry.volumeId[ROVOL] == INVALID_BID) {
        /* need to get a new RO volume id */
        vcode = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &roVolId);
-       ONERROR(vcode, entry.name, "Cant allocate ID for RO volume of %s\n");
+       ONERROR(vcode, entry.name, "Can't allocate ID for RO volume of %s\n");
 
        entry.volumeId[ROVOL] = roVolId;
        MapNetworkToHost(&entry, &storeEntry);
@@ -3262,20 +3459,51 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
        ONERROR(vcode, entry.name, "Could not update vldb entry for %s.\n");
     }
 
-    /* Will we be completing a previously unfinished release. -force overrides */
-    for (s = 0, m = 0, fullrelease=0, i=0; (i<entry.nServers); i++) {
-       if (entry.serverFlags[i] & ITSROVOL) {
-           m++;
-           if (entry.serverFlags[i] & NEW_REPSITE) s++;
+    /*
+     * Determine if this is to be a complete release or a recovery of a
+     * previous unfinished release. The previous release is considered to be
+     * unfinished when the clone was successfully distributed to at least one
+     * (but not all) of the read-only sites, as indicated by the NEW_REPSITE
+     * vldb flags.
+     *
+     * The caller can override the vldb flags check using the -force
+     * or -force-reclone flag, to force this to be a complete release.
+     */
+    for (i = 0; i < entry.nServers; i++) {
+       if (entry.serverFlags[i] & VLSF_ROVOL) {
+           sites++;
+           if (entry.serverFlags[i] & VLSF_NEWREPSITE)
+               new_sites++;
+           if (entry.serverFlags[i] & VLSF_DONTUSE)
+               notreleased++;
+       }
+       origflags[i] = entry.serverFlags[i];
+    }
+
+    if (flags & REL_COMPLETE) {
+       complete_release |= CR_FORCED;
+    }
+
+    if (new_sites == 0) {
+       complete_release |= CR_LAST_OK;
+    } else if (new_sites == sites) {
+       complete_release |= CR_ALL_NEW;
+    }
+
+    if ((complete_release & (CR_LAST_OK | CR_ALL_NEW))
+       && !(complete_release & CR_FORCED)) {
+       if (notreleased && notreleased != sites) {
+           /* we have some new unreleased sites. try to just release to those,
+            * if the RW has not changed. The caller can override with -force
+            * or with -force-reclone. */
+           justnewsites = 1;
        }
     }
-    if ((forceflag && !fullrelease) || (s == m) || (s == 0))
-       fullrelease = 1;
 
     /* Determine which volume id to use and see if it exists */
-    cloneVolId =
-       ((fullrelease
-         || (entry.cloneId == 0)) ? entry.volumeId[ROVOL] : entry.cloneId);
+    cloneVolId = (complete_release || entry.cloneId == 0)
+                 ? entry.volumeId[ROVOL] : entry.cloneId;
+
     code = VolumeExists(afromserver, afrompart, cloneVolId);
     roexists = ((code == ENODEV) ? 0 : 1);
 
@@ -3284,12 +3512,12 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
        ONERROR(-1, afromserver,
                "Cannot establish connection with server 0x%x\n");
 
-    if (!fullrelease) {
-       if (!roexists)
-           fullrelease = 1;    /* Do a full release if RO clone does not exist */
-       else {
+    if (!complete_release) {
+       if (!roexists) {
+           complete_release |= CR_RO_MISSING;  /* Do a complete release if RO clone does not exist */
+       } else {
            /* Begin transaction on RW and mark it busy while we query it */
-           code = AFSVolTransCreate(
+           code = AFSVolTransCreate_retry(
                        fromconn, afromvol, afrompart, ITBusy, &fromtid
                   );
            ONERROR(code, afromvol,
@@ -3300,7 +3528,6 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
            ONERROR(code, afromvol,
                    "Failed to get the status of RW volume %u\n");
            rwcrdate = volstatus.creationDate;
-           rwupdate = volstatus.updateDate;
 
            /* End transaction on RW */
            code = AFSVolEndTrans(fromconn, fromtid, &rcode);
@@ -3309,7 +3536,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                    "Failed to end transaction on RW volume %u\n");
 
            /* Begin transaction on clone and mark it busy while we query it */
-           code = AFSVolTransCreate(
+           code = AFSVolTransCreate_retry(
                        fromconn, cloneVolId, afrompart, ITBusy, &clonetid
                   );
            ONERROR(code, cloneVolId,
@@ -3328,95 +3555,203 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                    "Failed to end transaction on RW clone %u\n");
 
            if (rwcrdate > clcrdate)
-               fullrelease = 2;/* Do a full release if RO clone older than RW */
+               complete_release |= CR_NEW_RW; /* Do a complete release if RO clone older than RW */
        }
     }
 
+    if (!complete_release || (complete_release & CR_NEW_RW)) {
+       /* in case the RW has changed, and just to be safe */
+       justnewsites = 0;
+    }
+
     if (verbose) {
-       switch (fullrelease) {
-           case 2:
-               fprintf(STDOUT, "RW %lu changed, doing a complete release\n",
-                       (unsigned long)afromvol);
-               break;
-           case 1:
-               fprintf(STDOUT, "This is a complete release of volume %lu\n",
-                       (unsigned long)afromvol);
-               break;
-           case 0:
-               fprintf(STDOUT, "This is a completion of a previous release\n");
-               break;
+       if (!complete_release) {
+           fprintf(STDOUT,
+                   "This is a recovery of previously failed release\n");
+       } else {
+           fprintf(STDOUT, "This is a complete release of volume %u", afromvol);
+           if (complete_release == CR_LAST_OK) {
+               if (justnewsites) {
+                   tried_justnewsites = 1;
+                   fprintf(STDOUT, "\nThere are new RO sites; we will try to "
+                           "only release to new sites");
+               }
+           } else {
+               char *sep = " (";
+               if (complete_release & CR_FORCED) {
+                   fprintf(STDOUT, "%sforced", sep);
+                   sep = ", ";
+               }
+               if (complete_release & CR_LAST_OK) {
+                   fprintf(STDOUT, "%slast ok", sep);
+                   sep = ", ";
+               }
+               if (complete_release & CR_ALL_NEW) {
+                   fprintf(STDOUT, "%sall sites are new", sep);
+                   sep = ", ";
+               }
+               if (complete_release & CR_NEW_RW) {
+                   fprintf(STDOUT, "%srw %u changed", sep, afromvol);
+                   sep = ", ";
+               }
+               if (complete_release & CR_RO_MISSING) {
+                   fprintf(STDOUT, "%sro clone missing", sep);
+               }
+               fprintf(STDOUT, ")");
+           }
+           fprintf(STDOUT, "\n");
+       }
+    }
+
+    if (complete_release) {
+       afs_int32 oldest = 0;
+       /* If the RO clone exists, then if the clone is a temporary
+        * clone, delete it. Or if the RO clone is marked RO_DONTUSE
+        * (it was recently added), then also delete it. We do not
+        * want to "reclone" a temporary RO clone.
+        */
+
+       /* clean up any previous tmp clone before starting if staying up */
+       if (roexists
+           && (!roclone || (entry.serverFlags[roindex] & VLSF_DONTUSE))) {
+           code = DoVolDelete(fromconn,
+                              cloneVolId,
+                              afrompart, "the", 0, NULL, NULL);
+           if (code && (code != VNOVOL))
+               ERROREXIT(code);
+           roexists = 0;
+       }
+
+       if (justnewsites) {
+           VPRINT("Querying old RO sites for update times...");
+           for (vldbindex = 0; vldbindex < entry.nServers; vldbindex++) {
+               volEntries volumeInfo;
+               struct rx_connection *conn;
+               afs_int32 crdate;
+
+               if (!(entry.serverFlags[vldbindex] & VLSF_ROVOL)) {
+                   continue;
+               }
+               if ((entry.serverFlags[vldbindex] & VLSF_DONTUSE)) {
+                   continue;
+               }
+               conn = UV_Bind(entry.serverNumber[vldbindex], AFSCONF_VOLUMEPORT);
+               if (!conn) {
+                   fprintf(STDERR, "Cannot establish connection to server %s\n",
+                                   hostutil_GetNameByINet(entry.serverNumber[vldbindex]));
+                   justnewsites = 0;
+                   break;
+               }
+               volumeInfo.volEntries_val = NULL;
+               volumeInfo.volEntries_len = 0;
+               code = AFSVolListOneVolume(conn, entry.serverPartition[vldbindex],
+                                          entry.volumeId[ROVOL],
+                                          &volumeInfo);
+               if (code) {
+                   fprintf(STDERR, "Could not fetch information about RO vol %lu from server %s\n",
+                                   (unsigned long)entry.volumeId[ROVOL],
+                                   hostutil_GetNameByINet(entry.serverNumber[vldbindex]));
+                   PrintError("", code);
+                   justnewsites = 0;
+                   rx_DestroyConnection(conn);
+                   break;
+               }
+
+               crdate = CLOCKADJ(volumeInfo.volEntries_val[0].creationDate);
+
+               if (oldest == 0 || crdate < oldest) {
+                   oldest = crdate;
+               }
+
+               rx_DestroyConnection(conn);
+               free(volumeInfo.volEntries_val);
+               volumeInfo.volEntries_val = NULL;
+               volumeInfo.volEntries_len = 0;
+           }
+           VDONE;
        }
-    }
+       if (justnewsites) {
+           volEntries volumeInfo;
+           volumeInfo.volEntries_val = NULL;
+           volumeInfo.volEntries_len = 0;
+           code = AFSVolListOneVolume(fromconn, afrompart, afromvol,
+                                      &volumeInfo);
+           if (code) {
+               fprintf(STDERR, "Could not fetch information about RW vol %lu from server %s\n",
+                               (unsigned long)afromvol,
+                               hostutil_GetNameByINet(afromserver));
+               PrintError("", code);
+               justnewsites = 0;
+           } else {
+               rwupdate = volumeInfo.volEntries_val[0].updateDate;
 
-    if (fullrelease) {
-       /* If the RO clone exists, then if the clone is a temporary
-        * clone, delete it. Or if the RO clone is marked RO_DONTUSE
-        * (it was recently added), then also delete it. We do not
-        * want to "reclone" a temporary RO clone.
-        */
-       if (roexists
-           && (!roclone || (entry.serverFlags[roindex] & RO_DONTUSE))) {
-           code = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
-           if (code && (code != VNOVOL))
-               ERROREXIT(code);
-           roexists = 0;
+               free(volumeInfo.volEntries_val);
+               volumeInfo.volEntries_val = NULL;
+               volumeInfo.volEntries_len = 0;
+           }
+       }
+       if (justnewsites && oldest <= rwupdate) {
+           /* RW has changed */
+           justnewsites = 0;
        }
 
        /* Mark all the ROs in the VLDB entry as RO_DONTUSE. We don't
         * write this entry out to the vlserver until after the first
         * RO volume is released (temp RO clones don't count).
+        *
+        * If 'justnewsites' is set, we're only updating sites that have
+        * RO_DONTUSE set, so set NEW_REPSITE for all of the others.
         */
        for (i = 0; i < entry.nServers; i++) {
-           entry.serverFlags[i] &= ~NEW_REPSITE;
-           entry.serverFlags[i] |= RO_DONTUSE;
+           if (justnewsites) {
+               if ((entry.serverFlags[i] & VLSF_DONTUSE)) {
+                   entry.serverFlags[i] &= ~VLSF_NEWREPSITE;
+               } else {
+                   entry.serverFlags[i] |= VLSF_NEWREPSITE;
+               }
+           } else {
+               entry.serverFlags[i] &= ~VLSF_NEWREPSITE;
+               entry.serverFlags[i] |= VLSF_DONTUSE;
+           }
        }
-       entry.serverFlags[rwindex] |= NEW_REPSITE;
-       entry.serverFlags[rwindex] &= ~RO_DONTUSE;
+       entry.serverFlags[rwindex] |= VLSF_NEWREPSITE;
+       entry.serverFlags[rwindex] &= ~VLSF_DONTUSE;
+    }
 
-       /* Begin transaction on RW and mark it busy while we clone it */
-       code =
-           AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy,
-                             &clonetid);
-       ONERROR(code, afromvol, "Failed to start transaction on volume %u\n");
-
-       /* Clone or reclone the volume */
-       if (roexists) {
-           VPRINT1("Recloning RW volume %u...", cloneVolId);
-           code = AFSVolReClone(fromconn, clonetid, cloneVolId);
-           ONERROR(code, afromvol, "Failed to reclone the RW volume %u\n");
-           VDONE;
+    if (justnewsites && roexists) {
+       /* if 'justnewsites' and 'roexists' are set, we don't need to do
+        * anything with the RO clone, so skip the reclone */
+       /* noop */
+
+    } else if (complete_release) {
+
+       if (roclone) {
+           strcpy(vname, entry.name);
+           strcat(vname, ".readonly");
        } else {
-           if (roclone) {
-               strcpy(vname, entry.name);
-               strcat(vname, ".readonly");
-               VPRINT1("Cloning RW volume %u to permanent RO...", afromvol);
-           } else {
-               strcpy(vname, "readonly-clone-temp");
-               VPRINT1("Cloning RW volume %u to temporary RO...", afromvol);
+           strcpy(vname, "readonly-clone-temp");
+       }
+
+       code = DoVolClone(fromconn, afromvol, afrompart, readonlyVolume,
+                         cloneVolId, roclone?"permanent RO":
+                         "temporary RO", NULL, vname, NULL, &volstatus, NULL);
+       if (code) {
+           error = code;
+           goto rfail;
+       }
+
+       if (justnewsites && rwupdate != volstatus.updateDate) {
+           justnewsites = 0;
+           /* reset the serverFlags as if 'justnewsites' had never been set */
+           for (i = 0; i < entry.nServers; i++) {
+               entry.serverFlags[i] &= ~VLSF_NEWREPSITE;
+               entry.serverFlags[i] |= VLSF_DONTUSE;
            }
-           code =
-               AFSVolClone(fromconn, clonetid, 0, readonlyVolume, vname,
-                           &cloneVolId);
-           ONERROR(code, afromvol, "Failed to clone the RW volume %u\n");
-           VDONE;
+           entry.serverFlags[rwindex] |= VLSF_NEWREPSITE;
+           entry.serverFlags[rwindex] &= ~VLSF_DONTUSE;
        }
 
-       /* Get the time the RW was created for future information */
-       VPRINT1("Getting status of RW volume %u...", afromvol);
-       code = AFSVolGetStatus(fromconn, clonetid, &volstatus);
-       ONERROR(code, afromvol,
-               "Failed to get the status of the RW volume %u\n");
-       VDONE;
        rwcrdate = volstatus.creationDate;
-       rwupdate = volstatus.updateDate;
-
-       /* End the transaction on the RW volume */
-       VPRINT1("Ending cloning transaction on RW volume %u...", afromvol);
-       code = AFSVolEndTrans(fromconn, clonetid, &rcode);
-       clonetid = 0;
-       ONERROR((code ? code : rcode), afromvol,
-               "Failed to end cloning transaction on RW %u\n");
-       VDONE;
 
        /* Remember clone volume ID in case we fail or are interrupted */
        entry.cloneId = cloneVolId;
@@ -3426,7 +3761,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
            VPRINT1("Starting transaction on RO clone volume %u...",
                    cloneVolId);
            code =
-               AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITOffline,
+               AFSVolTransCreate_retry(fromconn, cloneVolId, afrompart, ITOffline,
                                  &onlinetid);
            ONERROR(code, cloneVolId,
                    "Failed to start transaction on volume %u\n");
@@ -3445,18 +3780,18 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
            ONERROR(tcode, cloneVolId, "Could not bring volume %u on line\n");
 
            /* Sleep so that a client searching for an online volume won't
-            * find the clone offline and then the next RO offline while the 
+            * find the clone offline and then the next RO offline while the
             * release brings the clone online and the next RO offline (race).
             * There is a fix in the 3.4 client that does not need this sleep
             * anymore, but we don't know what clients we have.
             */
-           if (entry.nServers > 2)
+           if (entry.nServers > 2 && !justnewsites)
                sleep(5);
 
            /* Mark the RO clone in the VLDB as a good site (already released) */
-           entry.serverFlags[roindex] |= NEW_REPSITE;
-           entry.serverFlags[roindex] &= ~RO_DONTUSE;
-           entry.flags |= RO_EXISTS;
+           entry.serverFlags[roindex] |= VLSF_NEWREPSITE;
+           entry.serverFlags[roindex] &= ~VLSF_DONTUSE;
+           entry.flags |= VLF_ROEXISTS;
 
            releasecount++;
 
@@ -3474,6 +3809,14 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
        }
     }
 
+    if (justnewsites) {
+       VPRINT("RW vol has not changed; only releasing to new RO sites\n");
+       /* act like this is a completion of a previous release */
+       complete_release = CR_PARTIAL;
+    } else if (tried_justnewsites) {
+       VPRINT("RW vol has changed; releasing to all sites\n");
+    }
+
     /* Now we will release from the clone to the remaining RO replicas.
      * The first 2 ROs (counting the non-temporary RO clone) are released
      * individually: releasecount. This is to reduce the race condition
@@ -3490,62 +3833,62 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
     cookie.parent = entry.volumeId[RWVOL];
     cookie.clone = 0;
 
-    nservers = entry.nServers / 2;     /* how many to do at once, excluding clone */
-    replicas =
-       (struct replica *)malloc(sizeof(struct replica) * nservers + 1);
-    times = (struct release *)malloc(sizeof(struct release) * nservers + 1);
-    toconns =
-       (struct rx_connection **)malloc(sizeof(struct rx_connection *) *
-                                       nservers + 1);
-    results.manyResults_val =
-       (afs_int32 *) malloc(sizeof(afs_int32) * nservers + 1);
-    if (!replicas || !times || !!!results.manyResults_val || !toconns)
-       ONERROR(ENOMEM, 0,
+    /* how many to do at once, excluding clone */
+    if (justnewsites)
+       nservers = entry.nServers; /* can do all, none offline */
+    else
+       nservers = entry.nServers / 2;
+    replicas = calloc(nservers + 1, sizeof(struct replica));
+    times = calloc(nservers + 1, sizeof(struct release));
+    toconns = calloc(nservers + 1, sizeof(struct rx_connection *));
+    results.manyResults_val = calloc(nservers + 1, sizeof(afs_int32));
+    if (!replicas || !times || !results.manyResults_val || !toconns)
+       ONERROR0(ENOMEM,
                "Failed to create transaction on the release clone\n");
 
-    memset(replicas, 0, (sizeof(struct replica) * nservers + 1));
-    memset(times, 0, (sizeof(struct release) * nservers + 1));
-    memset(toconns, 0, (sizeof(struct rx_connection *) * nservers + 1));
-    memset(results.manyResults_val, 0, (sizeof(afs_int32) * nservers + 1));
-
     /* Create a transaction on the cloned volume */
     VPRINT1("Starting transaction on cloned volume %u...", cloneVolId);
     code =
-       AFSVolTransCreate(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
-    if (!fullrelease && code)
+       AFSVolTransCreate_retry(fromconn, cloneVolId, afrompart, ITBusy, &fromtid);
+    if (!code) {
+       memset(&orig_status, 0, sizeof(orig_status));
+       code = AFSVolGetStatus(fromconn, fromtid, &orig_status);
+    }
+    if (!complete_release && code)
        ONERROR(VOLSERNOVOL, afromvol,
                "Old clone is inaccessible. Try vos release -f %u.\n");
-    ONERROR(code, 0, "Failed to create transaction on the release clone\n");
+    ONERROR0(code, "Failed to create transaction on the release clone\n");
     VDONE;
 
     /* For each index in the VLDB */
     for (vldbindex = 0; vldbindex < entry.nServers;) {
-
-       /* Get a transaction on the replicas. Pick replacas which have an old release. */
+       /* Get a transaction on the replicas. Pick replicas which have an old release. */
        for (volcount = 0;
             ((volcount < nservers) && (vldbindex < entry.nServers));
             vldbindex++) {
-           /* The first two RO volumes will be released individually.
-            * The rest are then released in parallel. This is a hack
-            * for clients not recognizing right away when a RO volume
-            * comes back on-line.
-            */
-           if ((volcount == 1) && (releasecount < 2))
-               break;
+           if (!justnewsites) {
+               /* The first two RO volumes will be released individually.
+                * The rest are then released in parallel. This is a hack
+                * for clients not recognizing right away when a RO volume
+                * comes back on-line.
+                */
+               if ((volcount == 1) && (releasecount < 2))
+                   break;
+           }
 
            if (vldbindex == roindex)
                continue;       /* the clone    */
-           if ((entry.serverFlags[vldbindex] & NEW_REPSITE)
-               && !(entry.serverFlags[vldbindex] & RO_DONTUSE))
+           if ((entry.serverFlags[vldbindex] & VLSF_NEWREPSITE)
+               && !(entry.serverFlags[vldbindex] & VLSF_DONTUSE))
                continue;
-           if (!(entry.serverFlags[vldbindex] & ITSROVOL))
+           if (!(entry.serverFlags[vldbindex] & VLSF_ROVOL))
                continue;       /* not a RO vol */
 
 
            /* Get a Transaction on this replica. Get a new connection if
             * necessary.  Create the volume if necessary.  Return the
             * time from which the dump should be made (0 if it's a new
-            * volume).  Each volume might have a different time. 
+            * volume).  Each volume might have a different time.
             */
            replicas[volcount].server.destHost =
                ntohl(entry.serverNumber[vldbindex]);
@@ -3557,20 +3900,39 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                GetTrans(&entry, vldbindex, &(toconns[volcount]),
                         &(replicas[volcount].trans),
                         &(times[volcount].crtime),
-                        &(times[volcount].uptime));
+                        &(times[volcount].uptime),
+                        origflags, 0);
            if (code)
                continue;
 
            /* Thisdate is the date from which we want to pick up all changes */
-           if (forceflag || !fullrelease
-               || (rwcrdate > times[volcount].crtime)) {
-               /* If the forceflag is set, then we want to do a full dump.
-                * If it's not a full release, we can't be sure that the creation
-                *  date is good (so we also do a full dump).
-                * If the RW volume was replaced (its creation date is newer than
-                *  the last release), then we can't be sure what has changed (so
-                *  we do a full dump).
+           if (flags & REL_FULLDUMPS) {
+               /* Do a full dump when forced by the caller. */
+               VPRINT("This will be a full dump: forced\n");
+               thisdate = 0;
+           } else if (complete_release == CR_PARTIAL) {
+               if (justnewsites) {
+                   VPRINT("This will be a full dump: read-only volume needs be created for new site\n");
+               } else {
+                   /*
+                    * We cannot be sure the creation date is good since the previous
+                    * release failed, so do a full dump.
+                    */
+                   VPRINT("This will be a full dump: previous release failed\n");
+               }
+               thisdate = 0;
+           } else if (times[volcount].crtime == 0) {
+               /* A full dump is needed for a new read-only volume. */
+               VPRINT
+                   ("This will be a full dump: read-only volume needs to be created\n");
+               thisdate = 0;
+           } else if ((rwcrdate > times[volcount].crtime)) {
+               /* If the RW volume was replaced (its creation date is newer than
+                * the last release), then we can't be sure what has changed (so
+                * we do a full dump).
                 */
+               VPRINT
+                   ("This will be a full dump: read-write volume was replaced\n");
                thisdate = 0;
            } else if (remembertime[vldbindex].validtime) {
                /* Trans was prev ended. Use the time from the prev trans
@@ -3607,9 +3969,16 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
        if (!volcount)
            continue;
 
+       code = CheckTrans(fromconn, &fromtid, afrompart, &orig_status);
+       if (code) {
+           error = ENOENT;
+           goto rfail;
+       }
+
        if (verbose) {
            fprintf(STDOUT, "Starting ForwardMulti from %lu to %u on %s",
-                   (unsigned long)cloneVolId, entry.volumeId[ROVOL],
+                   (unsigned long)cloneVolId,
+                   entry.volumeId[ROVOL],
                     noresolve ? afs_inet_ntoa_r(entry.serverNumber[times[0].
                                                 vldbEntryIndex], hoststr) :
                     hostutil_GetNameByINet(entry.
@@ -3626,7 +3995,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
            }
 
            if (fromdate == 0)
-               fprintf(STDOUT, " (full release)");
+               fprintf(STDOUT, " (entire volume)");
            else {
                tmv = fromdate;
                fprintf(STDOUT, " (as of %.24s)", ctime(&tmv));
@@ -3678,7 +4047,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
 
                /* have to clear dest. flags to ensure new vol goes online:
                 * because the restore (forwarded) operation copied
-                * the V_inService(=0) flag over to the destination. 
+                * the V_inService(=0) flag over to the destination.
                 */
                code = AFSVolSetFlags(toconns[m], replicas[m].trans, 0);
                if (code) {
@@ -3689,38 +4058,14 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                    continue;
                }
 
-               entry.serverFlags[times[m].vldbEntryIndex] |= NEW_REPSITE;
-               entry.serverFlags[times[m].vldbEntryIndex] &= ~RO_DONTUSE;
-               entry.flags |= RO_EXISTS;
+               entry.serverFlags[times[m].vldbEntryIndex] |= VLSF_NEWREPSITE;
+               entry.serverFlags[times[m].vldbEntryIndex] &= ~VLSF_DONTUSE;
+               entry.flags |= VLF_ROEXISTS;
                releasecount++;
            }
        }
 
-       /* End the transactions and destroy the connections */
-       for (s = 0; s < volcount; s++) {
-           if (replicas[s].trans)
-               code = AFSVolEndTrans(toconns[s], replicas[s].trans, &rcode);
-           replicas[s].trans = 0;
-           if (!code)
-               code = rcode;
-           if (code) {
-               if ((s == 0) || (code != ENOENT)) {
-                   PrintError("Could not end transaction on a ro volume: ",
-                              code);
-               } else {
-                   PrintError
-                       ("Transaction timed out on a ro volume. Will retry.\n",
-                        0);
-                   if (times[s].vldbEntryIndex < vldbindex)
-                       vldbindex = times[s].vldbEntryIndex;
-               }
-           }
-
-           if (toconns[s])
-               rx_DestroyConnection(toconns[s]);
-           toconns[s] = 0;
-       }
-
+       PutTrans(&vldbindex, replicas, toconns, times, volcount);
        MapNetworkToHost(&entry, &storeEntry);
        vcode = VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry, 0);
        ONERROR(vcode, afromvol,
@@ -3737,7 +4082,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
 
     /* Figure out if any volume were not released and say so */
     for (failure = 0, i = 0; i < entry.nServers; i++) {
-       if (!(entry.serverFlags[i] & NEW_REPSITE))
+       if (!(entry.serverFlags[i] & VLSF_NEWREPSITE))
            failure++;
     }
     if (failure) {
@@ -3746,14 +4091,13 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                "The volume %lu could not be released to the following %d sites:\n",
                (unsigned long)afromvol, failure);
        for (i = 0; i < entry.nServers; i++) {
-           if (!(entry.serverFlags[i] & NEW_REPSITE)) {
+           if (!(entry.serverFlags[i] & VLSF_NEWREPSITE)) {
                MapPartIdIntoName(entry.serverPartition[i], pname);
                fprintf(STDERR, "\t%35s %s\n",
                         noresolve ? afs_inet_ntoa_r(entry.serverNumber[i], hoststr) :
                         hostutil_GetNameByINet(entry.serverNumber[i]), pname);
            }
        }
-
        MapNetworkToHost(&entry, &storeEntry);
        vcode =
            VLDB_ReplaceEntry(afromvol, RWVOL, &storeEntry,
@@ -3764,6 +4108,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
        ERROREXIT(VOLSERBADRELEASE);
     }
 
+    entry.cloneId = 0;
     /* All the ROs were release successfully. Remove the temporary clone */
     if (!roclone) {
        if (verbose) {
@@ -3771,14 +4116,15 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
                    (unsigned long)cloneVolId);
            fflush(STDOUT);
        }
-       code = DelVol(fromconn, cloneVolId, afrompart, ITOffline);
-       ONERROR(code, cloneVolId, "Failed to delete volume %u.\n");
+       code = DoVolDelete(fromconn, cloneVolId, afrompart, NULL, 0, NULL,
+                          NULL);
+       if (code && code != VNOVOL)
+           ONERROR(code, cloneVolId, "Failed to delete volume %u.\n");
        VDONE;
     }
-    entry.cloneId = 0;
 
     for (i = 0; i < entry.nServers; i++)
-       entry.serverFlags[i] &= ~NEW_REPSITE;
+       entry.serverFlags[i] &= ~VLSF_NEWREPSITE;
 
     /* Update the VLDB */
     VPRINT("updating VLDB ...");
@@ -3863,7 +4209,7 @@ UV_ReleaseVolume(afs_uint32 afromvol, afs_int32 afromserver,
 }
 
 
-void
+static void
 dump_sig_handler(int x)
 {
     fprintf(STDERR, "\nSignal handler: vos dump operation\n");
@@ -3873,17 +4219,23 @@ dump_sig_handler(int x)
 /* Dump the volume <afromvol> on <afromserver> and
  * <afrompart> to <afilename> starting from <fromdate>.
  * DumpFunction does the real work behind the scenes after
- * extracting parameters from the rock 
+ * extracting parameters from the rock
  */
 int
-UV_DumpVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
-             afs_int32 fromdate, afs_int32(*DumpFunction) (), char *rock,
+UV_DumpVolume(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
+             afs_int32 fromdate,
+             afs_int32(*DumpFunction) (struct rx_call *, void *), void *rock,
              afs_int32 flags)
 {
-    struct rx_connection *fromconn = (struct rx_connection *)0;
-    struct rx_call *fromcall = (struct rx_call *)0;
-    afs_int32 fromtid = 0, rxError = 0, rcode = 0;
-    afs_int32 code, error = 0, retry = 0;
+    /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
+     * be changing during the dump */
+    struct rx_call * volatile fromcall = NULL;
+    struct rx_connection * volatile fromconn = NULL;
+    afs_int32 volatile fromtid = 0;
+
+    afs_int32 rcode = 0;
+    afs_int32 code, error = 0;
+    afs_int32 tmp;
     time_t tmv = fromdate;
 
     if (setjmp(env))
@@ -3904,7 +4256,9 @@ UV_DumpVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
 
     VEPRINT1("Starting transaction on volume %u...", afromvol);
-    code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
+    tmp = fromtid;
+    code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &tmp);
+    fromtid = tmp;
     EGOTO1(error_exit, code,
           "Could not start transaction on the volume %u to be dumped\n",
           afromvol);
@@ -3913,25 +4267,24 @@ UV_DumpVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     fromcall = rx_NewCall(fromconn);
 
     VEPRINT1("Starting volume dump on volume %u...", afromvol);
-    if (flags & VOLDUMPV2_OMITDIRS) 
+    if (flags & VOLDUMPV2_OMITDIRS)
        code = StartAFSVolDumpV2(fromcall, fromtid, fromdate, flags);
     else
-      retryold:
        code = StartAFSVolDump(fromcall, fromtid, fromdate);
     EGOTO(error_exit, code, "Could not start the dump process \n");
     VEDONE;
 
     VEPRINT1("Dumping volume %u...", afromvol);
     code = DumpFunction(fromcall, rock);
-    if (code == RXGEN_OPCODE) 
+    if (code == RXGEN_OPCODE)
        goto error_exit;
     EGOTO(error_exit, code, "Error while dumping volume \n");
     VEDONE;
 
   error_exit:
     if (fromcall) {
-       code = rx_EndCall(fromcall, rxError);
-       if (code && code != RXGEN_OPCODE) 
+       code = rx_EndCall(fromcall, 0);
+       if (code && code != RXGEN_OPCODE)
            fprintf(STDERR, "Error in rx_EndCall\n");
        if (code && !error)
            error = code;
@@ -3950,32 +4303,36 @@ UV_DumpVolume(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     if (fromconn)
        rx_DestroyConnection(fromconn);
 
-    if (retry)
-       goto retryold;
     if (error != RXGEN_OPCODE)
        PrintError("", error);
     return (error);
 }
 
 /* Clone the volume <afromvol> on <afromserver> and
- * <afrompart>, and then dump the clone volume to 
+ * <afrompart>, and then dump the clone volume to
  * <afilename> starting from <fromdate>.
  * DumpFunction does the real work behind the scenes after
- * extracting parameters from the rock 
+ * extracting parameters from the rock
  */
 int
-UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
+UV_DumpClonedVolume(afs_uint32 afromvol, afs_uint32 afromserver,
                    afs_int32 afrompart, afs_int32 fromdate,
-                   afs_int32(*DumpFunction) (), char *rock, afs_int32 flags)
+                   afs_int32(*DumpFunction) (struct rx_call *, void *),
+                   void *rock, afs_int32 flags)
 {
-    struct rx_connection *fromconn = (struct rx_connection *)0;
-    struct rx_call *fromcall = (struct rx_call *)0;
-    afs_int32 fromtid = 0, rxError = 0, rcode = 0;
-    afs_int32 clonetid = 0;
-    afs_int32 code = 0, vcode = 0, error = 0;
-    afs_uint32 clonevol = 0;
-    char vname[64];
+    /* declare stuff 'volatile' that may be used from setjmp/longjmp and may
+     * be changing during the dump */
+    struct rx_connection * volatile fromconn = NULL;
+    struct rx_call * volatile fromcall = NULL;
+    afs_int32 volatile clonetid = 0;
+    afs_uint32 volatile clonevol = 0;
+
+    afs_int32 tmp;
+    afs_int32 fromtid = 0, rcode = 0;
+    afs_int32 code = 0, error = 0;
+    afs_uint32 tmpVol;
     time_t tmv = fromdate;
+    char *volName = NULL;
 
     if (setjmp(env))
        ERROR_EXIT(EPIPE);
@@ -3995,15 +4352,23 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
     fromconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
 
     VEPRINT1("Starting transaction on volume %u...", afromvol);
-    code = AFSVolTransCreate(fromconn, afromvol, afrompart, ITBusy, &fromtid);
+    code = AFSVolTransCreate_retry(fromconn, afromvol, afrompart, ITBusy, &fromtid);
     EGOTO1(error_exit, code,
           "Could not start transaction on the volume %u to be dumped\n",
           afromvol);
     VEDONE;
 
+    VEPRINT1("Getting the name for volume %u ...", afromvol);
+    code = AFSVolGetName(fromconn, fromtid, &volName);
+    EGOTO1(error_exit, code,
+          "Failed to get the name of the volume %u\n",afromvol);
+    VEDONE;
+
     /* Get a clone id */
     VEPRINT1("Allocating new volume id for clone of volume %u ...", afromvol);
-    code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &clonevol);
+    tmpVol = clonevol;
+    code = ubik_VL_GetNewVolumeId(cstruct, 0, 1, &tmpVol);
+    clonevol = tmpVol;
     EGOTO1(error_exit, code,
           "Could not get an ID for the clone of volume %u from the VLDB\n",
           afromvol);
@@ -4012,9 +4377,10 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
     /* Do the clone. Default flags on clone are set to delete on salvage and out of service */
     VEPRINT2("Cloning source volume %u to clone volume %u...", afromvol,
            clonevol);
-    strcpy(vname, "dump-clone-temp");
+    tmpVol = clonevol;
     code =
-       AFSVolClone(fromconn, fromtid, 0, readonlyVolume, vname, &clonevol);
+       AFSVolClone(fromconn, fromtid, 0, readonlyVolume, volName, &tmpVol);
+    clonevol = tmpVol;
     EGOTO1(error_exit, code, "Failed to clone the source volume %u\n",
           afromvol);
     VEDONE;
@@ -4031,9 +4397,11 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
 
 
     VEPRINT1("Starting transaction on the cloned volume %u ...", clonevol);
+    tmp = clonetid;
     code =
-       AFSVolTransCreate(fromconn, clonevol, afrompart, ITOffline,
-                         &clonetid);
+       AFSVolTransCreate_retry(fromconn, clonevol, afrompart, ITOffline,
+                         &tmp);
+    clonetid = tmp;
     EGOTO1(error_exit, code,
           "Failed to start a transaction on the cloned volume%u\n",
           clonevol);
@@ -4049,7 +4417,7 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
     fromcall = rx_NewCall(fromconn);
 
     VEPRINT1("Starting volume dump from cloned volume %u...", clonevol);
-    if (flags & VOLDUMPV2_OMITDIRS) 
+    if (flags & VOLDUMPV2_OMITDIRS)
        code = StartAFSVolDumpV2(fromcall, clonetid, fromdate, flags);
     else
        code = StartAFSVolDump(fromcall, clonetid, fromdate);
@@ -4062,6 +4430,9 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
     VEDONE;
 
   error_exit:
+    if (volName)
+       free(volName);
+
     /* now delete the clone */
     VEPRINT1("Deleting the cloned volume %u ...", clonevol);
     code = AFSVolDeleteVolume(fromconn, clonetid);
@@ -4073,7 +4444,7 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
     }
 
     if (fromcall) {
-       code = rx_EndCall(fromcall, rxError);
+       code = rx_EndCall(fromcall, 0);
        if (code) {
            fprintf(STDERR, "Error in rx_EndCall\n");
            if (!error)
@@ -4104,24 +4475,23 @@ UV_DumpClonedVolume(afs_uint32 afromvol, afs_int32 afromserver,
 /*
  * Restore a volume <tovolid> <tovolname> on <toserver> <topart> from
  * the dump file <afilename>. WriteData does all the real work
- * after extracting params from the rock 
+ * after extracting params from the rock
  */
 int
-UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
-                 afs_int32 toparentid, char tovolname[], int flags,
-                 afs_int32(*WriteData) (), char *rock)
+UV_RestoreVolume2(afs_uint32 toserver, afs_int32 topart, afs_uint32 tovolid,
+                 afs_uint32 toparentid, char tovolname[], int flags,
+                 afs_int32(*WriteData) (struct rx_call *, void *),
+                 void *rock)
 {
     struct rx_connection *toconn, *tempconn;
     struct rx_call *tocall;
     afs_int32 totid, code, rcode, vcode, terror = 0;
-    afs_int32 rxError = 0;
     struct volser_status tstatus;
     struct volintInfo vinfo;
     char partName[10];
     char tovolreal[VOLSER_OLDMAXVOLNAME];
-    afs_uint32 pvolid; 
+    afs_uint32 pvolid;
     afs_int32 temptid, pparentid;
-    int success;
     struct nvldbentry entry, storeEntry;
     afs_int32 error;
     int islocked;
@@ -4129,17 +4499,17 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
     int reuseID;
     afs_int32 volflag, voltype, volsertype;
     afs_int32 oldCreateDate, oldUpdateDate, newCreateDate, newUpdateDate;
+    VolumeId oldCloneId = 0;
+    VolumeId oldBackupId = 0;
     int index, same, errcode;
     char apartName[10];
     char hoststr[16];
 
     memset(&cookie, 0, sizeof(cookie));
     islocked = 0;
-    success = 0;
     error = 0;
     reuseID = 1;
     tocall = (struct rx_call *)0;
-    toconn = (struct rx_connection *)0;
     tempconn = (struct rx_connection *)0;
     totid = 0;
     temptid = 0;
@@ -4166,8 +4536,13 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
                goto refail;
            }
            reuseID = 0;
+       } else if (vcode) {
+           fprintf(STDERR, "Could not fetch the VLDB entry for the volume %s\n",
+                   tovolname);
+           error = vcode;
+           goto refail;
        } else if (flags & RV_RDONLY) {
-           if (entry.flags & RW_EXISTS) {
+           if (entry.flags & VLF_RWEXISTS) {
                fprintf(STDERR,
                        "Entry for ReadWrite volume %s already exists!\n",
                        entry.name);
@@ -4191,7 +4566,7 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
     if (!pparentid) pparentid = pvolid;
     /* at this point we have a volume id to use/reuse for the volume to be restored */
     strncpy(tovolreal, tovolname, VOLSER_OLDMAXVOLNAME);
-           
+
     if (strlen(tovolname) > (VOLSER_OLDMAXVOLNAME - 1)) {
        EGOTO1(refail, VOLSERBADOP,
               "The volume name %s exceeds the maximum limit of (VOLSER_OLDMAXVOLNAME -1 ) bytes\n",
@@ -4211,63 +4586,40 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
             noresolve ? afs_inet_ntoa_r(toserver, hoststr) :
            hostutil_GetNameByINet(toserver), partName);
     fflush(STDOUT);
-    code =
-       AFSVolCreateVolume(toconn, topart, tovolreal, volsertype, pparentid, &pvolid,
-                          &totid);
-    if (code) {
-       if (flags & RV_FULLRST) {       /* full restore: delete then create anew */
-           VPRINT1("Deleting the previous volume %u ...", pvolid);
-
-           code =
-               AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
-           EGOTO1(refail, code, "Failed to start transaction on %u\n",
-                  pvolid);
 
+    /*
+     * Obtain a transaction and get the status of the target volume. Create a new
+     * volume if the target volume does not already exist.
+     */
+    memset(&tstatus, 0, sizeof(tstatus));
+    if ((flags & RV_FULLRST) != 0) {
+       /* Full restore: Delete existing volume then create anew. */
+       code = DoVolDelete(toconn, pvolid, topart, "the previous", 0, &tstatus, NULL);
+       if (code && code != VNOVOL) {
+           error = code;
+           goto refail;
+       }
+       code = AFSVolCreateVolume(toconn, topart, tovolreal, volsertype, pparentid,
+                                 &pvolid, &totid);
+       EGOTO1(refail, code, "Could not create new volume %u\n", pvolid);
+    } else {
+       /* Incremental restore: Obtain a transaction on the existing volume. */
+       code = AFSVolTransCreate_retry(toconn, pvolid, topart, ITOffline, &totid);
+       if (code == 0) {
            code = AFSVolGetStatus(toconn, totid, &tstatus);
-           EGOTO1(refail, code, "Could not get timestamp from volume %u\n",
-                  pvolid);
-
-           oldCreateDate = tstatus.creationDate;
-           oldUpdateDate = tstatus.updateDate;
-
-           code =
-               AFSVolSetFlags(toconn, totid,
-                              VTDeleteOnSalvage | VTOutOfService);
-           EGOTO1(refail, code, "Could not set flags on volume %u \n",
-                  pvolid);
-
-           code = AFSVolDeleteVolume(toconn, totid);
-           EGOTO1(refail, code, "Could not delete volume %u\n", pvolid);
-
-           code = AFSVolEndTrans(toconn, totid, &rcode);
-           totid = 0;
-           if (!code)
-               code = rcode;
-           EGOTO1(refail, code, "Could not end transaction on %u\n", pvolid);
-
-           VDONE;
-
-           code =
-               AFSVolCreateVolume(toconn, topart, tovolreal, volsertype, pparentid,
-                                  &pvolid, &totid);
+           EGOTO1(refail, code, "Could not get timestamp from volume %u\n", pvolid);
+       } else if (code == VNOVOL) {
+           code = AFSVolCreateVolume(toconn, topart, tovolreal, volsertype,
+                                     pparentid, &pvolid, &totid);
            EGOTO1(refail, code, "Could not create new volume %u\n", pvolid);
        } else {
-           code =
-               AFSVolTransCreate(toconn, pvolid, topart, ITOffline, &totid);
-           EGOTO1(refail, code, "Failed to start transaction on %u\n",
-                  pvolid);
-
-           code = AFSVolGetStatus(toconn, totid, &tstatus);
-           EGOTO1(refail, code, "Could not get timestamp from volume %u\n",
-                  pvolid);
-
-           oldCreateDate = tstatus.creationDate;
-           oldUpdateDate = tstatus.updateDate;
+           EGOTO1(refail, code, "Failed to start transaction on %u\n", pvolid);
        }
-    } else {
-       oldCreateDate = 0;
-       oldUpdateDate = 0;
     }
+    oldCreateDate = tstatus.creationDate;
+    oldUpdateDate = tstatus.updateDate;
+    oldCloneId = tstatus.cloneID;
+    oldBackupId = tstatus.backupID;
 
     cookie.parent = pparentid;
     cookie.type = voltype;
@@ -4287,7 +4639,7 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
        error = code;
        goto refail;
     }
-    terror = rx_EndCall(tocall, rxError);
+    terror = rx_EndCall(tocall, 0);
     tocall = (struct rx_call *)0;
     if (terror) {
        fprintf(STDERR, "rx_EndCall Failed \n");
@@ -4302,9 +4654,10 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
        error = code;
        goto refail;
     }
-    code = AFSVolSetIdsTypes(toconn, totid, tovolreal, voltype, pparentid, 0, 0);
+    code = AFSVolSetIdsTypes(toconn, totid, tovolreal, voltype, pparentid,
+                               oldCloneId, oldBackupId);
     if (code) {
-       fprintf(STDERR, "Could not set the right type and ID on %lu\n",
+       fprintf(STDERR, "Could not set the right type and IDs on %lu\n",
                (unsigned long)pvolid);
        error = code;
        goto refail;
@@ -4360,11 +4713,10 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
        goto refail;
     }
 
-    success = 1;
     fprintf(STDOUT, " done\n");
     fflush(STDOUT);
-    if (success && (!reuseID || (flags & RV_FULLRST))) {
-       /* Volume was restored on the file server, update the 
+    if (!reuseID || (flags & RV_FULLRST)) {
+       /* Volume was restored on the file server, update the
         * VLDB to reflect the change.
         */
        vcode = VLDB_GetEntryByID(pvolid, voltype, &entry);
@@ -4384,8 +4736,8 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
            entry.nServers = 1;
            entry.serverNumber[0] = toserver;   /*should be indirect */
            entry.serverPartition[0] = topart;
-           entry.serverFlags[0] = (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
-           entry.flags = (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
+           entry.serverFlags[0] = (flags & RV_RDONLY) ? VLSF_ROVOL : VLSF_RWVOL;
+           entry.flags = (flags & RV_RDONLY) ? VLF_ROEXISTS : VLF_RWEXISTS;
            if (flags & RV_RDONLY)
                entry.volumeId[ROVOL] = pvolid;
            else if (tstatus.cloneID != 0) {
@@ -4441,7 +4793,7 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
                entry.serverNumber[entry.nServers] = toserver;
                entry.serverPartition[entry.nServers] = topart;
                entry.serverFlags[entry.nServers] =
-                   (flags & RV_RDONLY) ? ITSROVOL : ITSRWVOL;
+                   (flags & RV_RDONLY) ? VLSF_ROVOL : VLSF_RWVOL;
                entry.nServers++;
            } else {
                /* This volume should be deleted on the old site
@@ -4466,7 +4818,7 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
                        tempconn =
                            UV_Bind(entry.serverNumber[index],
                                    AFSCONF_VOLUMEPORT);
-                       
+
                        MapPartIdIntoName(entry.serverPartition[index],
                                          apartName);
                        VPRINT3
@@ -4475,52 +4827,22 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
                              noresolve ? afs_inet_ntoa_r(entry.serverNumber[index], hoststr) :
                             hostutil_GetNameByINet(entry.serverNumber[index]),
                             apartName);
-                       code =
-                           AFSVolTransCreate(tempconn, pvolid,
-                                             entry.serverPartition[index],
-                                             ITOffline, &temptid);
-                       if (!code) {
-                           code =
-                               AFSVolSetFlags(tempconn, temptid,
-                                              VTDeleteOnSalvage |
-                                              VTOutOfService);
-                           if (code) {
-                               fprintf(STDERR,
-                                       "Could not set flags on volume %lu on the older site\n",
-                                       (unsigned long)pvolid);
-                               error = code;
-                               goto refail;
-                           }
-                           code = AFSVolDeleteVolume(tempconn, temptid);
-                           if (code) {
-                               fprintf(STDERR,
-                                       "Could not delete volume %lu on the older site\n",
-                                       (unsigned long)pvolid);
-                               error = code;
-                               goto refail;
-                           }
-                           code = AFSVolEndTrans(tempconn, temptid, &rcode);
-                           temptid = 0;
-                           if (!code)
-                               code = rcode;
-                           if (code) {
-                               fprintf(STDERR,
-                                       "Could not end transaction on volume %lu on the older site\n",
-                                       (unsigned long)pvolid);
-                               error = code;
-                               goto refail;
-                           }
-                           VDONE;
-                           MapPartIdIntoName(entry.serverPartition[index],
-                                             partName);
+                       code = DoVolDelete(tempconn, pvolid,
+                                          entry.serverPartition[index],
+                                          "the", 0, NULL, NULL);
+                       if (code && code != VNOVOL) {
+                           error = code;
+                           goto refail;
                        }
+                       MapPartIdIntoName(entry.serverPartition[index],
+                                         partName);
                    }
                }
                entry.serverNumber[index] = toserver;
                entry.serverPartition[index] = topart;
            }
 
-           entry.flags |= (flags & RV_RDONLY) ? RO_EXISTS : RW_EXISTS;
+           entry.flags |= (flags & RV_RDONLY) ? VLF_ROEXISTS : VLF_RWEXISTS;
            MapNetworkToHost(&entry, &storeEntry);
            vcode =
                VLDB_ReplaceEntry(pvolid, voltype, &storeEntry,
@@ -4542,7 +4864,7 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
     }
   refail:
     if (tocall) {
-       code = rx_EndCall(tocall, rxError);
+       code = rx_EndCall(tocall, 0);
        if (!error)
            error = code;
     }
@@ -4589,9 +4911,10 @@ UV_RestoreVolume2(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
 }
 
 int
-UV_RestoreVolume(afs_int32 toserver, afs_int32 topart, afs_uint32 tovolid,
-                char tovolname[], int flags, afs_int32(*WriteData) (),
-                char *rock)
+UV_RestoreVolume(afs_uint32 toserver, afs_int32 topart, afs_uint32 tovolid,
+                char tovolname[], int flags,
+                afs_int32(*WriteData) (struct rx_call *, void *),
+                void *rock)
 {
     return UV_RestoreVolume2(toserver, topart, tovolid, 0, tovolname, flags,
                             WriteData, rock);
@@ -4620,13 +4943,22 @@ UV_LockRelease(afs_uint32 volid)
 
 }
 
+/* old interface to add rosites */
+int
+UV_AddSite(afs_uint32 server, afs_int32 part, afs_uint32 volid,
+          afs_int32 valid)
+{
+    return UV_AddSite2(server, part, volid, 0, valid);
+}
+
 /*adds <server> and <part> as a readonly replication site for <volid>
 *in vldb */
 int
-UV_AddSite(afs_int32 server, afs_int32 part, afs_uint32 volid, afs_int32 valid)
+UV_AddSite2(afs_uint32 server, afs_int32 part, afs_uint32 volid,
+           afs_uint32 rovolid, afs_int32 valid)
 {
     int j, nro = 0, islocked = 0;
-    struct nvldbentry entry, storeEntry;
+    struct nvldbentry entry, storeEntry, entry2;
     afs_int32 vcode, error = 0;
     char apartName[10];
 
@@ -4666,7 +4998,7 @@ UV_AddSite(afs_int32 server, afs_int32 part, afs_uint32 volid, afs_int32 valid)
 
     /* See if it's on the same server */
     for (j = 0; j < entry.nServers; j++) {
-       if (entry.serverFlags[j] & ITSROVOL) {
+       if (entry.serverFlags[j] & VLSF_ROVOL) {
            nro++;
            if (VLDB_IsSameAddrs(server, entry.serverNumber[j], &error)) {
                if (error) {
@@ -4693,13 +5025,32 @@ UV_AddSite(afs_int32 server, afs_int32 part, afs_uint32 volid, afs_int32 valid)
        goto asfail;
     }
 
+    /* if rovolid == 0, we leave the RO volume id alone. If the volume doesn't
+     * have an RO volid at this point (i.e. entry.volumeId[ROVOL] ==
+     * INVALID_BID) and we leave it alone, it gets an RO volid at release-time.
+     */
+    if (rovolid) {
+       if (entry.volumeId[ROVOL] == INVALID_BID) {
+           vcode = VLDB_GetEntryByID(rovolid, -1, &entry2);
+           if (!vcode) {
+               fprintf(STDERR, "Volume ID %d already exists\n", rovolid);
+               return VVOLEXISTS;
+           }
+           VPRINT1("Using RO volume id %d.\n", rovolid);
+           entry.volumeId[ROVOL] = rovolid;
+       } else {
+           fprintf(STDERR, "Ignoring given RO id %d, since volume already has RO id %d\n",
+               rovolid, entry.volumeId[ROVOL]);
+       }
+    }
+
     VPRINT("Adding a new site ...");
     entry.serverNumber[entry.nServers] = server;
     entry.serverPartition[entry.nServers] = part;
     if (!valid) {
-       entry.serverFlags[entry.nServers] = (ITSROVOL | RO_DONTUSE);
+       entry.serverFlags[entry.nServers] = (VLSF_ROVOL | VLSF_DONTUSE);
     } else {
-       entry.serverFlags[entry.nServers] = (ITSROVOL);
+       entry.serverFlags[entry.nServers] = (VLSF_ROVOL);
     }
     entry.nServers++;
 
@@ -4734,11 +5085,10 @@ UV_AddSite(afs_int32 server, afs_int32 part, afs_uint32 volid, afs_int32 valid)
 
 /*removes <server> <part> as read only site for <volid> from the vldb */
 int
-UV_RemoveSite(afs_int32 server, afs_int32 part, afs_uint32 volid)
+UV_RemoveSite(afs_uint32 server, afs_int32 part, afs_uint32 volid)
 {
     afs_int32 vcode;
     struct nvldbentry entry, storeEntry;
-    int islocked;
 
     vcode = ubik_VL_SetLock(cstruct, 0, volid, RWVOL, VLOP_ADDSITE);
     if (vcode) {
@@ -4747,7 +5097,6 @@ UV_RemoveSite(afs_int32 server, afs_int32 part, afs_uint32 volid)
        PrintError("", vcode);
        return (vcode);
     }
-    islocked = 1;
     vcode = VLDB_GetEntryByID(volid, RWVOL, &entry);
     if (vcode) {
        fprintf(STDERR,
@@ -4775,8 +5124,8 @@ UV_RemoveSite(afs_int32 server, afs_int32 part, afs_uint32 volid)
     } else {                   /*remove the rep site */
        Lp_SetROValue(&entry, server, part, 0, 0);
        entry.nServers--;
-       if ((entry.nServers == 1) && (entry.flags & RW_EXISTS))
-           entry.flags &= ~RO_EXISTS;
+       if ((entry.nServers == 1) && (entry.flags & VLF_RWEXISTS))
+           entry.flags &= ~VLF_ROEXISTS;
        if (entry.nServers < 1) {       /*this is the last ref */
            VPRINT1("Deleting the VLDB entry for %u ...", volid);
            fflush(STDOUT);
@@ -4807,14 +5156,15 @@ UV_RemoveSite(afs_int32 server, afs_int32 part, afs_uint32 volid)
                      LOCKREL_OPCODE | LOCKREL_AFSID | LOCKREL_TIMESTAMP);
            return (vcode);
        }
-       VDONE;
+       fprintf(STDOUT, " done\n");
+       fflush(STDOUT);
     }
     return 0;
 }
 
 /*sets <server> <part> as read/write site for <volid> in the vldb */
 int
-UV_ChangeLocation(afs_int32 server, afs_int32 part, afs_uint32 volid)
+UV_ChangeLocation(afs_uint32 server, afs_int32 part, afs_uint32 volid)
 {
     afs_int32 vcode;
     struct nvldbentry entry, storeEntry;
@@ -4875,13 +5225,13 @@ UV_ChangeLocation(afs_int32 server, afs_int32 part, afs_uint32 volid)
 
 /*list all the partitions on <aserver> */
 int
-UV_ListPartitions(afs_int32 aserver, struct partList *ptrPartList,
+UV_ListPartitions(afs_uint32 aserver, struct partList *ptrPartList,
                  afs_int32 * cntp)
 {
     struct rx_connection *aconn;
     struct pIDs partIds;
     struct partEntries partEnts;
-    register int i, j = 0, code;
+    int i, j = 0, code;
 
     *cntp = 0;
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
@@ -4933,37 +5283,27 @@ UV_ListPartitions(afs_int32 aserver, struct partList *ptrPartList,
 /*zap the list of volumes specified by volPtrArray (the volCloneId field).
  This is used by the backup system */
 int
-UV_ZapVolumeClones(afs_int32 aserver, afs_int32 apart,
+UV_ZapVolumeClones(afs_uint32 aserver, afs_int32 apart,
                   struct volDescription *volPtr, afs_int32 arraySize)
 {
     struct rx_connection *aconn;
     struct volDescription *curPtr;
     int curPos;
     afs_int32 code = 0;
-    afs_int32 rcode = 0;
     afs_int32 success = 1;
-    afs_int32 tid;
 
-    aconn = (struct rx_connection *)0;
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
     curPos = 0;
     for (curPtr = volPtr; curPos < arraySize; curPtr++) {
        if (curPtr->volFlags & CLONEVALID) {
            curPtr->volFlags &= ~CLONEZAPPED;
            success = 1;
-           code =
-               AFSVolTransCreate(aconn, curPtr->volCloneId, apart, ITOffline,
-                                 &tid);
+
+           code = DoVolDelete(aconn, curPtr->volCloneId, apart,
+                              "clone", 0, NULL, NULL);
            if (code)
                success = 0;
-           else {
-               code = AFSVolDeleteVolume(aconn, tid);
-               if (code)
-                   success = 0;
-               code = AFSVolEndTrans(aconn, tid, &rcode);
-               if (code || rcode)
-                   success = 0;
-           }
+
            if (success)
                curPtr->volFlags |= CLONEZAPPED;
            if (!success)
@@ -4973,7 +5313,6 @@ UV_ZapVolumeClones(afs_int32 aserver, afs_int32 apart,
                VPRINT2("Clone of %s %u deleted\n", curPtr->volName,
                        curPtr->volCloneId);
            curPos++;
-           tid = 0;
        }
     }
     if (aconn)
@@ -4981,10 +5320,10 @@ UV_ZapVolumeClones(afs_int32 aserver, afs_int32 apart,
     return 0;
 }
 
-/*return a list of clones of the volumes specified by volPtrArray. Used by the 
+/*return a list of clones of the volumes specified by volPtrArray. Used by the
  backup system */
 int
-UV_GenerateVolumeClones(afs_int32 aserver, afs_int32 apart,
+UV_GenerateVolumeClones(afs_uint32 aserver, afs_int32 apart,
                        struct volDescription *volPtr, afs_int32 arraySize)
 {
     struct rx_connection *aconn;
@@ -4997,7 +5336,6 @@ UV_GenerateVolumeClones(afs_int32 aserver, afs_int32 apart,
     afs_uint32 curCloneId = 0;
     char cloneName[256];       /*max vol name */
 
-    aconn = (struct rx_connection *)0;
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
     curPos = 0;
     if ((volPtr->volFlags & REUSECLONEID) && (volPtr->volFlags & ENTRYVALID))
@@ -5018,13 +5356,13 @@ UV_GenerateVolumeClones(afs_int32 aserver, afs_int32 apart,
            curPtr->volFlags |= CLONEVALID;
            /*make a clone of curParentId and record as curPtr->volCloneId */
            code =
-               AFSVolTransCreate(aconn, curPtr->volId, apart, ITOffline,
+               AFSVolTransCreate_retry(aconn, curPtr->volId, apart, ITOffline,
                                  &tid);
            if (code)
                VPRINT2("Clone for volume %s %u failed \n", curPtr->volName,
                        curPtr->volId);
            if (code) {
-               curPtr->volFlags &= ~CLONEVALID;        /*cant clone */
+               curPtr->volFlags &= ~CLONEVALID;        /* can't clone */
                curPos++;
                continue;
            }
@@ -5072,14 +5410,13 @@ UV_GenerateVolumeClones(afs_int32 aserver, afs_int32 apart,
 /*list all the volumes on <aserver> and <apart>. If all = 1, then all the
 * relevant fields of the volume are also returned. This is a heavy weight operation.*/
 int
-UV_ListVolumes(afs_int32 aserver, afs_int32 apart, int all,
+UV_ListVolumes(afs_uint32 aserver, afs_int32 apart, int all,
               struct volintInfo **resultPtr, afs_int32 * size)
 {
     struct rx_connection *aconn;
     afs_int32 code = 0;
     volEntries volumeInfo;
 
-    code = 0;
     *size = 0;
     *resultPtr = (volintInfo *) 0;
     volumeInfo.volEntries_val = (volintInfo *) 0;      /*this hints the stub to allocate space */
@@ -5136,7 +5473,7 @@ UV_ListVolumes(afs_int32 aserver, afs_int32 apart, int all,
  *------------------------------------------------------------------------*/
 
 int
-UV_XListVolumes(afs_int32 a_serverID, afs_int32 a_partID, int a_all,
+UV_XListVolumes(afs_uint32 a_serverID, afs_int32 a_partID, int a_all,
                struct volintXInfo **a_resultPP,
                afs_int32 * a_numEntsInResultP)
 {
@@ -5149,7 +5486,6 @@ UV_XListVolumes(afs_int32 a_serverID, afs_int32 a_partID, int a_all,
      * We set the val field to a null pointer as a hint for the stub to
      * allocate space.
      */
-    code = 0;
     *a_numEntsInResultP = 0;
     *a_resultPP = (volintXInfo *) 0;
     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
@@ -5184,15 +5520,13 @@ UV_XListVolumes(afs_int32 a_serverID, afs_int32 a_partID, int a_all,
 
 /* get all the information about volume <volid> on <aserver> and <apart> */
 int
-UV_ListOneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 volid,
+UV_ListOneVolume(afs_uint32 aserver, afs_int32 apart, afs_uint32 volid,
                 struct volintInfo **resultPtr)
 {
     struct rx_connection *aconn;
     afs_int32 code = 0;
     volEntries volumeInfo;
 
-    code = 0;
-
     *resultPtr = (volintInfo *) 0;
     volumeInfo.volEntries_val = (volintInfo *) 0;      /*this hints the stub to allocate space */
     volumeInfo.volEntries_len = 0;
@@ -5243,7 +5577,7 @@ UV_ListOneVolume(afs_int32 aserver, afs_int32 apart, afs_uint32 volid,
  *------------------------------------------------------------------------*/
 
 int
-UV_XListOneVolume(afs_int32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
+UV_XListOneVolume(afs_uint32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
                  struct volintXInfo **a_resultPP)
 {
     struct rx_connection *rxConnP;     /*Rx connection to Volume Server */
@@ -5255,7 +5589,6 @@ UV_XListOneVolume(afs_int32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
      * the info.  Setting the val field to a null pointer tells the stub
      * to allocate space for us.
      */
-    code = 0;
     *a_resultPP = (volintXInfo *) 0;
     volumeXInfo.volXEntries_val = (volintXInfo *) 0;
     volumeXInfo.volXEntries_len = 0;
@@ -5286,17 +5619,17 @@ UV_XListOneVolume(afs_int32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
 }
 
 /* CheckVolume()
- *    Given a volume we read from a partition, check if it is 
+ *    Given a volume we read from a partition, check if it is
  *    represented in the VLDB correctly.
- * 
+ *
  *    The VLDB is looked up by the RW volume id (not its name).
  *    The RW contains the true name of the volume (BK and RO set
  *       the name in the VLDB only on creation of the VLDB entry).
  *    We want rules strict enough that when we check all volumes
  *       on one partition, it does not need to be done again. IE:
- *       two volumes on different partitions won't constantly 
+ *       two volumes on different partitions won't constantly
  *       change a VLDB entry away from what the other set.
- *    For RW and BK volumes, we will always check the VLDB to see 
+ *    For RW and BK volumes, we will always check the VLDB to see
  *       if the two exist on the server/partition. May seem redundant,
  *       but this is an easy check of the VLDB. IE: if the VLDB entry
  *       says the BK exists but no BK volume is there, we will detect
@@ -5305,17 +5638,18 @@ UV_XListOneVolume(afs_int32 a_serverID, afs_int32 a_partID, afs_uint32 a_volID,
  *    Output changed to look a lot like the "vos syncserv" otuput.
  */
 static afs_int32
-CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
-           afs_int32 * modentry, afs_uint32 * maxvolid)
+CheckVolume(volintInfo * volumeinfo, afs_uint32 aserver, afs_int32 apart,
+           afs_int32 * modentry, afs_uint32 * maxvolid,
+            struct nvldbentry *aentry)
 {
     int idx = 0;
     int j;
     afs_int32 code, error = 0;
     struct nvldbentry entry, storeEntry;
     char pname[10];
-    int pass = 0, islocked = 0, createentry, addvolume, modified, mod, doit = 1;
+    int pass = 0, createentry, addvolume, modified, mod, doit = 1;
     afs_uint32 rwvolid;
-    char hoststr[16]; 
+    char hoststr[16];
 
     if (modentry) {
        if (*modentry == 1)
@@ -5338,29 +5672,32 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                    (unsigned long)rwvolid);
            ERROR_EXIT(code);
        }
-       islocked = 1;
     }
 
     createentry = 0;           /* Do we need to create a VLDB entry */
     addvolume = 0;             /* Add this volume to the VLDB entry */
     modified = 0;              /* The VLDB entry was modified */
 
-    /* Read the entry from VLDB by its RW volume id */
-    code = VLDB_GetEntryByID(rwvolid, RWVOL, &entry);
-    if (code) {
-       if (code != VL_NOENT) {
-           fprintf(STDOUT,
-                   "Could not retreive the VLDB entry for volume %lu \n",
-                   (unsigned long)rwvolid);
-           ERROR_EXIT(code);
-       }
+    if (aentry) {
+       memcpy(&entry, aentry, sizeof(entry));
+    } else {
+       /* Read the entry from VLDB by its RW volume id */
+       code = VLDB_GetEntryByID(rwvolid, RWVOL, &entry);
+       if (code) {
+           if (code != VL_NOENT) {
+               fprintf(STDOUT,
+                       "Could not retrieve the VLDB entry for volume %lu \n",
+                       (unsigned long)rwvolid);
+               ERROR_EXIT(code);
+           }
 
-       memset(&entry, 0, sizeof(entry));
-       vsu_ExtractName(entry.name, volumeinfo->name);  /* Store name of RW */
+           memset(&entry, 0, sizeof(entry));
+           vsu_ExtractName(entry.name, volumeinfo->name);      /* Store name of RW */
 
-       createentry = 1;
-    } else {
-       MapHostToNetwork(&entry);
+           createentry = 1;
+       } else {
+           MapHostToNetwork(&entry);
+       }
     }
 
     if (verbose && (pass == 1)) {
@@ -5369,8 +5706,8 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
        if (createentry) {
            fprintf(STDOUT, "\n**does not exist**\n");
        } else {
-           if ((entry.flags & RW_EXISTS) || (entry.flags & RO_EXISTS)
-               || (entry.flags & BACK_EXISTS))
+           if ((entry.flags & VLF_RWEXISTS) || (entry.flags & VLF_ROEXISTS)
+               || (entry.flags & VLF_BACKEXISTS))
                EnumerateEntry(&entry);
        }
        fprintf(STDOUT, "\n");
@@ -5397,7 +5734,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
            } else {            /* RW index found in the VLDB entry. */
                /* Verify if this volume's location matches where the VLDB says it is */
                if (!Lp_Match(aserver, apart, &entry)) {
-                   if (entry.flags & RW_EXISTS) {
+                   if (entry.flags & VLF_RWEXISTS) {
                        /* The RW volume exists elsewhere - report this one a duplicate */
                        if (pass == 1) {
                            MapPartIdIntoName(apart, pname);
@@ -5412,7 +5749,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                            fprintf(STDERR,
                                    "    VLDB reports RW volume %lu exists on %s %s\n",
                                    (unsigned long)rwvolid,
-                                    noresolve ? 
+                                    noresolve ?
                                     afs_inet_ntoa_r(entry.serverNumber[idx], hoststr) :
                                    hostutil_GetNameByINet(entry.
                                                           serverNumber[idx]),
@@ -5423,7 +5760,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                        addvolume++;
 
                        /* Check for orphaned BK volume on old partition */
-                       if (entry.flags & BACK_EXISTS) {
+                       if (entry.flags & VLF_BACKEXISTS) {
                            if (pass == 1) {
                                MapPartIdIntoName(entry.serverPartition[idx],
                                                  pname);
@@ -5439,7 +5776,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                                fprintf(STDERR,
                                        "    VLDB reports its RW volume %lu exists on %s %s\n",
                                        (unsigned long)rwvolid,
-                                        noresolve ? 
+                                        noresolve ?
                                         afs_inet_ntoa_r(aserver, hoststr) :
                                        hostutil_GetNameByINet(aserver),
                                        pname);
@@ -5461,14 +5798,14 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
        }
 
        if (addvolume) {
-           entry.flags |= RW_EXISTS;
+           entry.flags |= VLF_RWEXISTS;
            entry.volumeId[RWVOL] = rwvolid;
            if (!entry.volumeId[BACKVOL])
                entry.volumeId[BACKVOL] = volumeinfo->backupID;
            if (!entry.volumeId[ROVOL])
                entry.volumeId[ROVOL] = volumeinfo->cloneID;
 
-           entry.serverFlags[idx] = ITSRWVOL;
+           entry.serverFlags[idx] = VLSF_RWVOL;
            entry.serverNumber[idx] = aserver;
            entry.serverPartition[idx] = apart;
            strncpy(entry.name, volumeinfo->name, VOLSER_OLDMAXVOLNAME);
@@ -5526,7 +5863,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                    }
                } else {
                    if (volumeinfo->volid != entry.volumeId[BACKVOL]) {
-                       if (!(entry.flags & BACK_EXISTS)) {
+                       if (!(entry.flags & VLF_BACKEXISTS)) {
                            addvolume++;
                        } else if (volumeinfo->volid >
                                   entry.volumeId[BACKVOL]) {
@@ -5569,13 +5906,13 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
            }
        }
        if (addvolume) {
-           entry.flags |= BACK_EXISTS;
+           entry.flags |= VLF_BACKEXISTS;
            entry.volumeId[RWVOL] = rwvolid;
            entry.volumeId[BACKVOL] = volumeinfo->volid;
 
            entry.serverNumber[idx] = aserver;
            entry.serverPartition[idx] = apart;
-           entry.serverFlags[idx] = ITSBACKVOL;
+           entry.serverFlags[idx] = VLSF_RWVOL;
 
            modified++;
        }
@@ -5583,7 +5920,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
 
     else if (volumeinfo->type == ROVOL) {      /* A RO volume */
        if (volumeinfo->volid == entry.volumeId[ROVOL]) {
-           /* This is a quick check to see if the RO entry exists in the 
+           /* This is a quick check to see if the RO entry exists in the
             * VLDB so we avoid the CheckVldbRO() call (which checks if each
             * RO volume listed in the VLDB exists).
             */
@@ -5593,7 +5930,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                entry.nServers++;
                addvolume++;
            } else {
-               if (!(entry.flags & RO_EXISTS)) {
+               if (!(entry.flags & VLF_ROEXISTS)) {
                    addvolume++;
                }
            }
@@ -5607,7 +5944,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
            if (mod)
                modified++;
 
-           if (!(entry.flags & RO_EXISTS)) {
+           if (!(entry.flags & VLF_ROEXISTS)) {
                /* No RO exists in the VLDB entry - add this one */
                idx = entry.nServers;
                entry.nServers++;
@@ -5618,7 +5955,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                 * all the RO volumes listed in VLDB entry and add this volume.
                 */
                for (j = 0; j < entry.nServers; j++) {
-                   if (entry.serverFlags[j] & ITSROVOL) {
+                   if (entry.serverFlags[j] & VLSF_ROVOL) {
                        /* Verify this volume exists and print message we are orphaning it */
                        if (pass == 1) {
                            MapPartIdIntoName(apart, pname);
@@ -5677,13 +6014,13 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
        }
 
        if (addvolume) {
-           entry.flags |= RO_EXISTS;
+           entry.flags |= VLF_ROEXISTS;
            entry.volumeId[RWVOL] = rwvolid;
            entry.volumeId[ROVOL] = volumeinfo->volid;
 
            entry.serverNumber[idx] = aserver;
            entry.serverPartition[idx] = apart;
-           entry.serverFlags[idx] = ITSROVOL;
+           entry.serverFlags[idx] = VLSF_ROVOL;
 
            modified++;
        }
@@ -5721,8 +6058,6 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
                ERROR_EXIT(code);
            }
        }
-       if (modentry)
-           *modentry = modified;
     } else if (pass == 2) {
        code =
            ubik_VL_ReleaseLock(cstruct, 0, rwvolid, RWVOL,
@@ -5732,7 +6067,15 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
        }
     }
 
-    if (verbose && doit) {
+    if (modified && modentry) {
+       *modentry = 1;
+    }
+
+    if (aentry) {
+       memcpy(aentry, &entry, sizeof(entry));
+    }
+
+    if (verbose) {
        fprintf(STDOUT, "-- status after --\n");
        if (modified)
            EnumerateEntry(&entry);
@@ -5745,7 +6088,7 @@ CheckVolume(volintInfo * volumeinfo, afs_int32 aserver, afs_int32 apart,
     return (error);
 }
 
-int
+static int
 sortVolumes(const void *a, const void *b)
 {
     volintInfo *v1 = (volintInfo *) a;
@@ -5784,17 +6127,17 @@ sortVolumes(const void *a, const void *b)
  *      if the volume exists on specified servers (similar to syncvldb).
  */
 int
-UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
+UV_SyncVolume(afs_uint32 aserver, afs_int32 apart, char *avolname, int flags)
 {
     struct rx_connection *aconn = 0;
     afs_int32 j, k, code, vcode, error = 0;
     afs_int32 tverbose;
-    afs_int32 mod, modified = 0;
+    afs_int32 mod, modified = 0, deleted = 0;
     struct nvldbentry vldbentry;
     afs_uint32 volumeid = 0;
     volEntries volumeInfo;
     struct partList PartList;
-    afs_int32 pcnt, rv;
+    afs_int32 pcnt;
     afs_uint32 maxvolid = 0;
 
     volumeInfo.volEntries_val = (volintInfo *) 0;
@@ -5802,9 +6145,9 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
 
     /* Turn verbose logging off and do our own verbose logging */
     /* tverbose must be set before we call ERROR_EXIT() */
-    
+
     tverbose = verbose;
-    if (flags & 2) 
+    if (flags & 2)
        tverbose = 1;
     verbose = 0;
 
@@ -5830,14 +6173,14 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
        if (vcode) {
            fprintf(STDOUT, "\n**does not exist**\n");
        } else {
-           if ((vldbentry.flags & RW_EXISTS) || (vldbentry.flags & RO_EXISTS)
-               || (vldbentry.flags & BACK_EXISTS))
+           if ((vldbentry.flags & VLF_RWEXISTS) || (vldbentry.flags & VLF_ROEXISTS)
+               || (vldbentry.flags & VLF_BACKEXISTS))
                EnumerateEntry(&vldbentry);
        }
        fprintf(STDOUT, "\n");
     }
 
-    /* Verify that all of the VLDB entries exist on the repective servers 
+    /* Verify that all of the VLDB entries exist on the repective servers
      * and partitions (this does not require that avolname be a volume ID).
      * Equivalent to a syncserv.
      */
@@ -5847,7 +6190,7 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
            mod = 1;
        else
            mod = 0;
-       code = CheckVldb(&vldbentry, &mod);
+       code = CheckVldb(&vldbentry, &mod, &deleted);
        if (code) {
            fprintf(STDERR, "Could not process VLDB entry for volume %s\n",
                    vldbentry.name);
@@ -5893,7 +6236,7 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
                    /* Found one, sync it with VLDB entry */
                    code =
                        CheckVolume(volumeInfo.volEntries_val, aserver,
-                                   PartList.partId[j], &mod, &maxvolid);
+                                   PartList.partId[j], &mod, &maxvolid, &vldbentry);
                    if (code)
                        ERROR_EXIT(code);
                    if (mod)
@@ -5910,21 +6253,7 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
        /* Check to see if the RW, BK, and RO IDs exist on any
         * partitions. We get the volume IDs from the VLDB.
         */
-       rv = 1;                 /* Read the VLDB entry ? */
        for (j = 0; j < MAXTYPES; j++) {        /* for RW, RO, and BK IDs */
-           if (rv) {
-               vcode = VLDB_GetEntryByName(avolname, &vldbentry);
-               if (vcode) {
-                   if (vcode == VL_NOENT)
-                       break;
-                   fprintf(STDERR,
-                           "Could not access the VLDB for volume %s\n",
-                           avolname);
-                   ERROR_EXIT(vcode);
-               }
-               rv = 0;
-           }
-
            if (vldbentry.volumeId[j] == 0)
                continue;
 
@@ -5947,11 +6276,11 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
                    /* Found one, sync it with VLDB entry */
                    code =
                        CheckVolume(volumeInfo.volEntries_val, aserver,
-                                   PartList.partId[k], &mod, &maxvolid);
+                                   PartList.partId[k], &mod, &maxvolid, &vldbentry);
                    if (code)
                        ERROR_EXIT(code);
                    if (mod)
-                       modified++, rv++;
+                       modified++;
                }
 
                if (volumeInfo.volEntries_val)
@@ -5964,15 +6293,9 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
 
     /* if (aserver) */
     /* If verbose output, print a summary of what changed */
-    if (tverbose && !(flags & 2)) {
+    if (tverbose) {
        fprintf(STDOUT, "-- status after --\n");
-       code = VLDB_GetEntryByName(avolname, &vldbentry);
-       if (code && (code != VL_NOENT)) {
-           fprintf(STDERR, "Could not access the VLDB for volume %s\n",
-                   avolname);
-           ERROR_EXIT(code);
-       }
-       if (modified && (code == VL_NOENT)) {
+       if (deleted) {
            fprintf(STDOUT, "\n**entry deleted**\n");
        } else if (modified) {
            EnumerateEntry(&vldbentry);
@@ -6026,7 +6349,7 @@ UV_SyncVolume(afs_int32 aserver, afs_int32 apart, char *avolname, int flags)
  *      optionally, <apart>.
  */
 int
-UV_SyncVldb(afs_int32 aserver, afs_int32 apart, int flags, int force)
+UV_SyncVldb(afs_uint32 aserver, afs_int32 apart, int flags, int force)
 {
     struct rx_connection *aconn;
     afs_int32 code, error = 0;
@@ -6102,7 +6425,7 @@ UV_SyncVldb(afs_int32 aserver, afs_int32 apart, int flags, int force)
                modified = 1;
            else
                modified = 0;
-           code = CheckVolume(vi, aserver, apart, &modified, &maxvolid);
+           code = CheckVolume(vi, aserver, apart, &modified, &maxvolid, NULL);
            if (code) {
                PrintError("", code);
                failures++;
@@ -6135,10 +6458,10 @@ UV_SyncVldb(afs_int32 aserver, afs_int32 apart, int flags, int force)
     }                          /* thru all partitions */
 
     if (flags & 2) {
-       VPRINT3("Total entries: %u, Failed to process %d, Would change %d\n", 
+       VPRINT3("Total entries: %u, Failed to process %d, Would change %d\n",
                tentries, failures, modifications);
     } else {
-       VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n", 
+       VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n",
                tentries, failures, modifications);
     }
 
@@ -6180,8 +6503,8 @@ UV_SyncVldb(afs_int32 aserver, afs_int32 apart, int flags, int force)
  *      Some error codes mean the volume is unavailable but
  *      still exists - so we catch these error codes.
  */
-afs_int32
-VolumeExists(afs_int32 server, afs_int32 partition, afs_uint32 volumeid)
+static afs_int32
+VolumeExists(afs_uint32 server, afs_int32 partition, afs_uint32 volumeid)
 {
     struct rx_connection *conn = (struct rx_connection *)0;
     afs_int32 code = -1;
@@ -6204,7 +6527,7 @@ VolumeExists(afs_int32 server, afs_int32 partition, afs_uint32 volumeid)
 /* CheckVldbRWBK()
  *
  */
-afs_int32
+static afs_int32
 CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
 {
     int modentry = 0;
@@ -6217,12 +6540,12 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
        *modified = 0;
     idx = Lp_GetRwIndex(entry);
 
-    /* Check to see if the RW volume exists and set the RW_EXISTS
+    /* Check to see if the RW volume exists and set the VLF_RWEXISTS
      * flag accordingly.
      */
     if (idx == -1) {           /* Did not find a RW entry */
-       if (entry->flags & RW_EXISTS) { /* ... yet entry says RW exists */
-           entry->flags &= ~RW_EXISTS; /* ... so say RW does not exist */
+       if (entry->flags & VLF_RWEXISTS) {      /* ... yet entry says RW exists */
+           entry->flags &= ~VLF_RWEXISTS;      /* ... so say RW does not exist */
            modentry++;
        }
     } else {
@@ -6230,18 +6553,18 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
            VolumeExists(entry->serverNumber[idx],
                         entry->serverPartition[idx], entry->volumeId[RWVOL]);
        if (code == 0) {        /* RW volume exists */
-           if (!(entry->flags & RW_EXISTS)) {  /* ... yet entry says RW does not exist */
-               entry->flags |= RW_EXISTS;      /* ... so say RW does exist */
+           if (!(entry->flags & VLF_RWEXISTS)) {       /* ... yet entry says RW does not exist */
+               entry->flags |= VLF_RWEXISTS;   /* ... so say RW does exist */
                modentry++;
            }
        } else if (code == ENODEV) {    /* RW volume does not exist */
-           if (entry->flags & RW_EXISTS) {     /* ... yet entry says RW exists */
-               entry->flags &= ~RW_EXISTS;     /* ... so say RW does not exist */
+           if (entry->flags & VLF_RWEXISTS) {  /* ... yet entry says RW exists */
+               entry->flags &= ~VLF_RWEXISTS;  /* ... so say RW does not exist */
                modentry++;
            }
        } else {
            /* If VLDB says it didn't exist, then ignore error */
-           if (entry->flags & RW_EXISTS) {
+           if (entry->flags & VLF_RWEXISTS) {
                MapPartIdIntoName(entry->serverPartition[idx], pname);
                fprintf(STDERR,
                        "Transaction call failed for RW volume %u on server %s %s\n",
@@ -6255,12 +6578,12 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
        }
     }
 
-    /* Check to see if the BK volume exists and set the BACK_EXISTS
+    /* Check to see if the BK volume exists and set the VLF_BACKEXISTS
      * flag accordingly. idx already ponts to the RW entry.
      */
     if (idx == -1) {           /* Did not find a RW entry */
-       if (entry->flags & BACK_EXISTS) {       /* ... yet entry says BK exists */
-           entry->flags &= ~BACK_EXISTS;       /* ... so say BK does not exist */
+       if (entry->flags & VLF_BACKEXISTS) {    /* ... yet entry says BK exists */
+           entry->flags &= ~VLF_BACKEXISTS;    /* ... so say BK does not exist */
            modentry++;
        }
     } else {                   /* Found a RW entry */
@@ -6269,18 +6592,18 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
                         entry->serverPartition[idx],
                         entry->volumeId[BACKVOL]);
        if (code == 0) {        /* BK volume exists */
-           if (!(entry->flags & BACK_EXISTS)) {        /* ... yet entry says BK does not exist */
-               entry->flags |= BACK_EXISTS;    /* ... so say BK does exist */
+           if (!(entry->flags & VLF_BACKEXISTS)) {     /* ... yet entry says BK does not exist */
+               entry->flags |= VLF_BACKEXISTS; /* ... so say BK does exist */
                modentry++;
            }
        } else if (code == ENODEV) {    /* BK volume does not exist */
-           if (entry->flags & BACK_EXISTS) {   /* ... yet entry says BK exists */
-               entry->flags &= ~BACK_EXISTS;   /* ... so say BK does not exist */
+           if (entry->flags & VLF_BACKEXISTS) {        /* ... yet entry says BK exists */
+               entry->flags &= ~VLF_BACKEXISTS;        /* ... so say BK does not exist */
                modentry++;
            }
        } else {
            /* If VLDB says it didn't exist, then ignore error */
-           if (entry->flags & BACK_EXISTS) {
+           if (entry->flags & VLF_BACKEXISTS) {
                MapPartIdIntoName(entry->serverPartition[idx], pname);
                fprintf(STDERR,
                        "Transaction call failed for BK volume %u on server %s %s\n",
@@ -6297,8 +6620,8 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
     /* If there is an idx but the BK and RW volumes no
      * longer exist, then remove the RW entry.
      */
-    if ((idx != -1) && !(entry->flags & RW_EXISTS)
-       && !(entry->flags & BACK_EXISTS)) {
+    if ((idx != -1) && !(entry->flags & VLF_RWEXISTS)
+       && !(entry->flags & VLF_BACKEXISTS)) {
        Lp_SetRWValue(entry, entry->serverNumber[idx],
                      entry->serverPartition[idx], 0L, 0L);
        entry->nServers--;
@@ -6311,7 +6634,7 @@ CheckVldbRWBK(struct nvldbentry * entry, afs_int32 * modified)
     return (error);
 }
 
-int
+static afs_int32
 CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified)
 {
     int idx;
@@ -6323,11 +6646,11 @@ CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified)
     if (modified)
        *modified = 0;
 
-    /* Check to see if the RO volumes exist and set the RO_EXISTS
-     * flag accordingly. 
+    /* Check to see if the RO volumes exist and set the VLF_ROEXISTS
+     * flag accordingly.
      */
     for (idx = 0; idx < entry->nServers; idx++) {
-       if (!(entry->serverFlags[idx] & ITSROVOL)) {
+       if (!(entry->serverFlags[idx] & VLSF_ROVOL)) {
            continue;           /* not a RO */
        }
 
@@ -6355,13 +6678,13 @@ CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified)
     }
 
     if (foundro) {             /* A RO volume exists */
-       if (!(entry->flags & RO_EXISTS)) {      /* ... yet entry says RW does not exist */
-           entry->flags |= RO_EXISTS;  /* ... so say RW does exist */
+       if (!(entry->flags & VLF_ROEXISTS)) {   /* ... yet entry says RW does not exist */
+           entry->flags |= VLF_ROEXISTS;       /* ... so say RW does exist */
            modentry++;
        }
     } else {                   /* A RO volume does not exist */
-       if (entry->flags & RO_EXISTS) { /* ... yet entry says RO exists */
-           entry->flags &= ~RO_EXISTS; /* ... so say RO does not exist */
+       if (entry->flags & VLF_ROEXISTS) {      /* ... yet entry says RO exists */
+           entry->flags &= ~VLF_ROEXISTS;      /* ... so say RO does not exist */
            modentry++;
        }
     }
@@ -6375,8 +6698,8 @@ CheckVldbRO(struct nvldbentry *entry, afs_int32 * modified)
 /* CheckVldb()
  *      Ensure that <entry> matches with the info on file servers
  */
-afs_int32
-CheckVldb(struct nvldbentry * entry, afs_int32 * modified)
+static afs_int32
+CheckVldb(struct nvldbentry * entry, afs_int32 * modified, afs_int32 * deleted)
 {
     afs_int32 code, error = 0;
     struct nvldbentry storeEntry;
@@ -6384,15 +6707,15 @@ CheckVldb(struct nvldbentry * entry, afs_int32 * modified)
     int pass = 0, doit=1;
 
     if (modified) {
-       if (*modified == 1) 
+       if (*modified == 1)
            doit = 0;
        *modified = 0;
     }
     if (verbose) {
        fprintf(STDOUT, "_______________________________\n");
        fprintf(STDOUT, "\n-- status before -- \n");
-       if ((entry->flags & RW_EXISTS) || (entry->flags & RO_EXISTS)
-           || (entry->flags & BACK_EXISTS))
+       if ((entry->flags & VLF_RWEXISTS) || (entry->flags & VLF_ROEXISTS)
+           || (entry->flags & VLF_BACKEXISTS))
            EnumerateEntry(entry);
        fprintf(STDOUT, "\n");
     }
@@ -6448,15 +6771,15 @@ CheckVldb(struct nvldbentry * entry, afs_int32 * modified)
     if (mod)
        modentry++;
 
-    /* The VLDB entry has been updated. If it as been modified, then 
+    /* The VLDB entry has been updated. If it as been modified, then
      * write the entry back out the the VLDB.
      */
     if (modentry && doit) {
        if (pass == 1)
            goto retry;
 
-       if (!(entry->flags & RW_EXISTS) && !(entry->flags & BACK_EXISTS)
-           && !(entry->flags & RO_EXISTS) && doit) {
+       if (!(entry->flags & VLF_RWEXISTS) && !(entry->flags & VLF_BACKEXISTS)
+           && !(entry->flags & VLF_ROEXISTS) && doit) {
            /* The RW, BK, nor RO volumes do not exist. Delete the VLDB entry */
            code =
                ubik_VL_DeleteEntry(cstruct, 0, entry->volumeId[RWVOL],
@@ -6481,12 +6804,17 @@ CheckVldb(struct nvldbentry * entry, afs_int32 * modified)
                ERROR_EXIT(code);
            }
        }
-       if (modified)
-           *modified = 1;
        islocked = 0;
     }
 
-    if (verbose && doit) {
+    if (modified && modentry) {
+       *modified = 1;
+    }
+    if (deleted && delentry) {
+       *deleted = 1;
+    }
+
+    if (verbose) {
        fprintf(STDOUT, "-- status after --\n");
        if (delentry)
            fprintf(STDOUT, "\n**entry deleted**\n");
@@ -6519,7 +6847,7 @@ CheckVldb(struct nvldbentry * entry, afs_int32 * modified)
  *      Synchronise <aserver> <apart>(if flags = 1) with the VLDB.
  */
 int
-UV_SyncServer(afs_int32 aserver, afs_int32 apart, int flags, int force)
+UV_SyncServer(afs_uint32 aserver, afs_int32 apart, int flags, int force)
 {
     struct rx_connection *aconn;
     afs_int32 code, error = 0;
@@ -6530,12 +6858,13 @@ UV_SyncServer(afs_int32 aserver, afs_int32 apart, int flags, int force)
     struct nvldbentry *vlentry;
     afs_int32 si, nsi, j;
 
-    if (flags & 2) 
+    if (flags & 2)
        verbose = 1;
 
     aconn = UV_Bind(aserver, AFSCONF_VOLUMEPORT);
 
     /* Set up attributes to search VLDB  */
+    memset(&attributes, 0, sizeof(attributes));
     attributes.server = ntohl(aserver);
     attributes.Mask = VLLIST_SERVER;
     if ((flags & 1)) {
@@ -6574,7 +6903,7 @@ UV_SyncServer(afs_int32 aserver, afs_int32 apart, int flags, int force)
                modified = 1;
            else
                modified = 0;
-           code = CheckVldb(vlentry, &modified);
+           code = CheckVldb(vlentry, &modified, NULL);
            if (code) {
                PrintError("", code);
                fprintf(STDERR,
@@ -6604,7 +6933,7 @@ UV_SyncServer(afs_int32 aserver, afs_int32 apart, int flags, int force)
        VPRINT3("Total entries: %u, Failed to process %d, Would change %d\n",
                tentries, failures, modifications);
     } else {
-       VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n", 
+       VPRINT3("Total entries: %u, Failed to process %d, Changed %d\n",
                tentries, failures, modifications);
     }
 
@@ -6619,7 +6948,7 @@ UV_SyncServer(afs_int32 aserver, afs_int32 apart, int flags, int force)
     return error;
 }
 
-/*rename volume <oldname> to <newname>, changing the names of the related 
+/*rename volume <oldname> to <newname>, changing the names of the related
  *readonly and backup volumes. This operation is also idempotent.
  *salvager is capable of recovering from rename operation stopping halfway.
  *to recover run syncserver on the affected machines,it will force renaming to completion. name clashes should have been detected before calling this proc */
@@ -6649,7 +6978,39 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
        goto rvfail;
     }
     islocked = 1;
+
+    /*
+     * Match the flags we just set via SetLock,
+     * so we don't invalidate our compare below.
+     */
+    entry->flags &= ~VLOP_ALLOPERS;
+    entry->flags |= VLOP_ADDSITE;
+
+    /*
+     * Now get the entry again (under lock) and
+     * verify the volume hasn't otherwise changed.
+     */
+    vcode = VLDB_GetEntryByID(entry->volumeId[RWVOL], RWVOL, &storeEntry);
+    if (vcode) {
+       fprintf(STDERR,
+               "Could not obtain the VLDB entry for the volume %u\n",
+               entry->volumeId[RWVOL]);
+       error = vcode;
+       goto rvfail;
+    }
+    /* Convert to net order to match entry, which was passed in net order. */
+    MapHostToNetwork(&storeEntry);
+    if (memcmp(entry, &storeEntry, sizeof(*entry)) != 0) {
+       fprintf(STDERR,
+               "VLDB entry for volume %u has changed; "
+               "please reissue the command.\n",
+               entry->volumeId[RWVOL]);
+       error = VL_BADENTRY;    /* an arbitrary choice, but closest to the truth */
+       goto rvfail;
+    }
+
     strncpy(entry->name, newname, VOLSER_OLDMAXVOLNAME);
+    /* Note that we are reusing storeEntry. */
     MapNetworkToHost(entry, &storeEntry);
     vcode = VLDB_ReplaceEntry(entry->volumeId[RWVOL], RWVOL, &storeEntry, 0);
     if (vcode) {
@@ -6659,9 +7020,9 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
        goto rvfail;
     }
     VPRINT1("Recorded the new name %s in VLDB\n", newname);
-    /*at this stage the intent to rename is recorded in the vldb, as far as the vldb 
+    /*at this stage the intent to rename is recorded in the vldb, as far as the vldb
      * is concerned, oldname is lost */
-    if (entry->flags & RW_EXISTS) {
+    if (entry->flags & VLF_RWEXISTS) {
        index = Lp_GetRwIndex(entry);
        if (index == -1) {      /* there is a serious discrepancy */
            fprintf(STDERR,
@@ -6673,7 +7034,7 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
        }
        aconn = UV_Bind(entry->serverNumber[index], AFSCONF_VOLUMEPORT);
        code =
-           AFSVolTransCreate(aconn, entry->volumeId[RWVOL],
+           AFSVolTransCreate_retry(aconn, entry->volumeId[RWVOL],
                              entry->serverPartition[index], ITOffline, &tid);
        if (code) {             /*volume doesnot exist */
            fprintf(STDERR,
@@ -6711,7 +7072,7 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
        aconn = (struct rx_connection *)0;
     }
     /*end rw volume processing */
-    if (entry->flags & BACK_EXISTS) {  /*process the backup volume */
+    if (entry->flags & VLF_BACKEXISTS) {       /*process the backup volume */
        index = Lp_GetRwIndex(entry);
        if (index == -1) {      /* there is a serious discrepancy */
            fprintf(STDERR,
@@ -6723,7 +7084,7 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
        }
        aconn = UV_Bind(entry->serverNumber[index], AFSCONF_VOLUMEPORT);
        code =
-           AFSVolTransCreate(aconn, entry->volumeId[BACKVOL],
+           AFSVolTransCreate_retry(aconn, entry->volumeId[BACKVOL],
                              entry->serverPartition[index], ITOffline, &tid);
        if (code) {             /*volume doesnot exist */
            fprintf(STDERR,
@@ -6768,12 +7129,12 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
     if (aconn)
        rx_DestroyConnection(aconn);
     aconn = (struct rx_connection *)0;
-    if (entry->flags & RO_EXISTS) {    /*process the ro volumes */
+    if (entry->flags & VLF_ROEXISTS) { /*process the ro volumes */
        for (i = 0; i < entry->nServers; i++) {
-           if (entry->serverFlags[i] & ITSROVOL) {
+           if (entry->serverFlags[i] & VLSF_ROVOL) {
                aconn = UV_Bind(entry->serverNumber[i], AFSCONF_VOLUMEPORT);
                code =
-                   AFSVolTransCreate(aconn, entry->volumeId[ROVOL],
+                   AFSVolTransCreate_retry(aconn, entry->volumeId[ROVOL],
                                      entry->serverPartition[i], ITOffline,
                                      &tid);
                if (code) {     /*volume doesnot exist */
@@ -6858,7 +7219,7 @@ UV_RenameVolume(struct nvldbentry *entry, char oldname[], char newname[])
 
 /*report on all the active transactions on volser */
 int
-UV_VolserStatus(afs_int32 server, transDebugInfo ** rpntr, afs_int32 * rcount)
+UV_VolserStatus(afs_uint32 server, transDebugInfo ** rpntr, afs_int32 * rcount)
 {
     struct rx_connection *aconn;
     transDebugEntries transInfo;
@@ -6890,48 +7251,18 @@ UV_VolserStatus(afs_int32 server, transDebugInfo ** rpntr, afs_int32 * rcount)
 
 /*delete the volume without interacting with the vldb */
 int
-UV_VolumeZap(afs_int32 server, afs_int32 part, afs_uint32 volid)
+UV_VolumeZap(afs_uint32 server, afs_int32 part, afs_uint32 volid)
 {
-    afs_int32 rcode, ttid, error, code;
+    afs_int32 error;
     struct rx_connection *aconn;
 
-    code = 0;
-    error = 0;
-    ttid = 0;
-
     aconn = UV_Bind(server, AFSCONF_VOLUMEPORT);
-    code = AFSVolTransCreate(aconn, volid, part, ITOffline, &ttid);
-    if (code) {
-       fprintf(STDERR, "Could not start transaction on volume %lu\n",
-               (unsigned long)volid);
-       error = code;
-       goto zfail;
-    }
-    code = AFSVolDeleteVolume(aconn, ttid);
-    if (code) {
-       fprintf(STDERR, "Could not delete volume %lu\n",
-               (unsigned long)volid);
-       error = code;
-       goto zfail;
-    }
-    code = AFSVolEndTrans(aconn, ttid, &rcode);
-    ttid = 0;
-    if (!code)
-       code = rcode;
-    if (code) {
-       fprintf(STDERR, "Could not end transaction on volume %lu\n",
-               (unsigned long)volid);
-       error = code;
-       goto zfail;
-    }
-  zfail:
-    if (ttid) {
-       code = AFSVolEndTrans(aconn, ttid, &rcode);
-       if (!code)
-           code = rcode;
-       if (!error)
-           error = code;
+    error = DoVolDelete(aconn, volid, part,
+                       "the", 0, NULL, NULL);
+    if (error == VNOVOL) {
+       EPRINT1(error, "Failed to start transaction on %u\n", volid);
     }
+
     PrintError("", error);
     if (aconn)
        rx_DestroyConnection(aconn);
@@ -6939,7 +7270,7 @@ UV_VolumeZap(afs_int32 server, afs_int32 part, afs_uint32 volid)
 }
 
 int
-UV_SetVolume(afs_int32 server, afs_int32 partition, afs_uint32 volid,
+UV_SetVolume(afs_uint32 server, afs_int32 partition, afs_uint32 volid,
             afs_int32 transflag, afs_int32 setflag, int sleeptime)
 {
     struct rx_connection *conn = 0;
@@ -6952,7 +7283,7 @@ UV_SetVolume(afs_int32 server, afs_int32 partition, afs_uint32 volid,
        ERROR_EXIT(-1);
     }
 
-    code = AFSVolTransCreate(conn, volid, partition, transflag, &tid);
+    code = AFSVolTransCreate_retry(conn, volid, partition, transflag, &tid);
     if (code) {
        fprintf(STDERR, "SetVolumeStatus: TransCreate Failed\n");
        ERROR_EXIT(code);
@@ -6989,7 +7320,7 @@ UV_SetVolume(afs_int32 server, afs_int32 partition, afs_uint32 volid,
 }
 
 int
-UV_SetVolumeInfo(afs_int32 server, afs_int32 partition, afs_uint32 volid,
+UV_SetVolumeInfo(afs_uint32 server, afs_int32 partition, afs_uint32 volid,
                 volintInfo * infop)
 {
     struct rx_connection *conn = 0;
@@ -7002,7 +7333,7 @@ UV_SetVolumeInfo(afs_int32 server, afs_int32 partition, afs_uint32 volid,
        ERROR_EXIT(-1);
     }
 
-    code = AFSVolTransCreate(conn, volid, partition, ITOffline, &tid);
+    code = AFSVolTransCreate_retry(conn, volid, partition, ITOffline, &tid);
     if (code) {
        fprintf(STDERR, "SetVolumeInfo: TransCreate Failed\n");
        ERROR_EXIT(code);
@@ -7031,7 +7362,7 @@ UV_SetVolumeInfo(afs_int32 server, afs_int32 partition, afs_uint32 volid,
 }
 
 int
-UV_GetSize(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
+UV_GetSize(afs_uint32 afromvol, afs_uint32 afromserver, afs_int32 afrompart,
           afs_int32 fromdate, struct volintSize *vol_size)
 {
     struct rx_connection *aconn = (struct rx_connection *)0;
@@ -7043,7 +7374,7 @@ UV_GetSize(afs_uint32 afromvol, afs_int32 afromserver, afs_int32 afrompart,
     aconn = UV_Bind(afromserver, AFSCONF_VOLUMEPORT);
 
     VPRINT1("Starting transaction on volume %u...", afromvol);
-    code = AFSVolTransCreate(aconn, afromvol, afrompart, ITBusy, &tid);
+    code = AFSVolTransCreate_retry(aconn, afromvol, afrompart, ITBusy, &tid);
     EGOTO1(error_exit, code,
           "Could not start transaction on the volume %u to be measured\n",
           afromvol);
@@ -7081,6 +7412,8 @@ MapNetworkToHost(struct nvldbentry *old, struct nvldbentry *new)
 {
     int i, count;
 
+    memset(new, 0, sizeof(struct nvldbentry));
+
     /*copy all the fields */
     strcpy(new->name, old->name);
 /*    new->volumeType = old->volumeType;*/