Revert "Convert from using nvldbentry to uvldbentry"
[openafs.git] / src / volser / vsutils.c
index 6af0b7c..13c7fd5 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
@@ -10,6 +10,7 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
 #include <afs/stds.h>
 #include <string.h>
@@ -54,7 +55,7 @@ static void
 ovlentry_to_nvlentry(struct vldbentry *oentryp,
                      struct nvldbentry *nentryp)
 {
-    register int i;
+    int i;
 
     memset(nentryp, 0, sizeof(struct nvldbentry));
     strncpy(nentryp->name, oentryp->name, sizeof(nentryp->name));
@@ -74,7 +75,7 @@ static int
 nvlentry_to_ovlentry(struct nvldbentry *nentryp,
                      struct vldbentry *oentryp)
 {
-    register int i;
+    int i;
 
     memset(oentryp, 0, sizeof(struct vldbentry));
     strncpy(oentryp->name, nentryp->name, sizeof(oentryp->name));
@@ -98,15 +99,22 @@ nvlentry_to_ovlentry(struct nvldbentry *nentryp,
     return 0;
 }
 
-static int newvlserver = 0;
+enum _vlserver_type {
+    vltype_unknown = 0,
+    vltype_old = 1,
+    vltype_new = 2,
+    vltype_uuid = 3
+};
+
+static enum _vlserver_type newvlserver = vltype_unknown;
 
 int
 VLDB_CreateEntry(struct nvldbentry *entryp)
 {
     struct vldbentry oentry;
-    register int code;
+    int code;
 
-    if (newvlserver == 1) {
+    if (newvlserver == vltype_old) {
       tryold:
        code = nvlentry_to_ovlentry(entryp, &oentry);
        if (code)
@@ -115,12 +123,12 @@ VLDB_CreateEntry(struct nvldbentry *entryp)
        return code;
     }
     code = ubik_VL_CreateEntryN(cstruct, 0, entryp);
-    if (!newvlserver) {
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           newvlserver = 1;    /* Doesn't support new interface */
+           newvlserver = vltype_old;   /* Doesn't support new interface */
            goto tryold;
        } else if (!code) {
-           newvlserver = 2;
+           newvlserver = vltype_new;
        }
     }
     return code;
@@ -130,9 +138,9 @@ int
 VLDB_GetEntryByID(afs_uint32 volid, afs_int32 voltype, struct nvldbentry *entryp)
 {
     struct vldbentry oentry;
-    register int code;
+    int code;
 
-    if (newvlserver == 1) {
+    if (newvlserver == vltype_old) {
       tryold:
        code =
            ubik_VL_GetEntryByID(cstruct, 0, volid, voltype, &oentry);
@@ -141,12 +149,12 @@ VLDB_GetEntryByID(afs_uint32 volid, afs_int32 voltype, struct nvldbentry *entryp
        return code;
     }
     code = ubik_VL_GetEntryByIDN(cstruct, 0, volid, voltype, entryp);
-    if (!newvlserver) {
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           newvlserver = 1;    /* Doesn't support new interface */
+           newvlserver = vltype_old;   /* Doesn't support new interface */
            goto tryold;
        } else if (!code) {
-           newvlserver = 2;
+           newvlserver = vltype_new;
        }
     }
     return code;
@@ -156,9 +164,9 @@ int
 VLDB_GetEntryByName(char *namep, struct nvldbentry *entryp)
 {
     struct vldbentry oentry;
-    register int code;
+    int code;
 
-    if (newvlserver == 1) {
+    if (newvlserver == vltype_old) {
       tryold:
        code = ubik_VL_GetEntryByNameO(cstruct, 0, namep, &oentry);
        if (!code)
@@ -166,24 +174,24 @@ VLDB_GetEntryByName(char *namep, struct nvldbentry *entryp)
        return code;
     }
     code = ubik_VL_GetEntryByNameN(cstruct, 0, namep, entryp);
-    if (!newvlserver) {
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           newvlserver = 1;    /* Doesn't support new interface */
+           newvlserver = vltype_old;   /* Doesn't support new interface */
            goto tryold;
        } else if (!code) {
-           newvlserver = 2;
+           newvlserver = vltype_new;
        }
     }
     return code;
 }
 
-int 
+int
 VLDB_ReplaceEntry(afs_uint32 volid, afs_int32 voltype, struct nvldbentry *entryp, afs_int32 releasetype)
 {
     struct vldbentry oentry;
-    register int code;
+    int code;
 
-    if (newvlserver == 1) {
+    if (newvlserver == vltype_old) {
       tryold:
        code = nvlentry_to_ovlentry(entryp, &oentry);
        if (code)
@@ -196,17 +204,33 @@ VLDB_ReplaceEntry(afs_uint32 volid, afs_int32 voltype, struct nvldbentry *entryp
     code =
        ubik_VL_ReplaceEntryN(cstruct, 0, volid, voltype, entryp,
                  releasetype);
-    if (!newvlserver) {
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           newvlserver = 1;    /* Doesn't support new interface */
+           newvlserver = vltype_old;   /* Doesn't support new interface */
            goto tryold;
        } else if (!code) {
-           newvlserver = 2;
+           newvlserver = vltype_new;
        }
     }
     return code;
 }
 
+static void
+convertBulkToNBulk(bulkentries *bulk, nbulkentries *nbulk) {
+    int i;
+
+    if (bulk->bulkentries_len == 0)
+       return;
+
+    nbulk->nbulkentries_len = bulk->bulkentries_len;
+    nbulk->nbulkentries_val =
+       xdr_alloc(bulk->bulkentries_len * sizeof(struct nvldbentry));
+
+    for (i = 0; i < bulk->bulkentries_len; i++) {
+       ovlentry_to_nvlentry(&bulk->bulkentries_val[i],
+                            &nbulk->nbulkentries_val[i]);
+    }
+}
 
 int
 VLDB_ListAttributes(VldbListByAttributes *attrp,
@@ -214,37 +238,46 @@ VLDB_ListAttributes(VldbListByAttributes *attrp,
                     nbulkentries *blkentriesp)
 {
     bulkentries arrayEntries;
-    register int code, i;
+    int code;
 
-    if (newvlserver == 1) {
+    if (newvlserver == vltype_old) {
       tryold:
-       memset(&arrayEntries, 0, sizeof(arrayEntries)); /*initialize to hint the stub  to alloc space */
+       memset(&arrayEntries, 0, sizeof(arrayEntries));
        code =
            ubik_VL_ListAttributes(cstruct, 0, attrp, entriesp,
                      &arrayEntries);
-       if (!code) {
-           blkentriesp->nbulkentries_val =
-               (nvldbentry *) malloc(*entriesp * sizeof(struct nvldbentry));
-           for (i = 0; i < *entriesp; i++) {   /* process each entry */
-               ovlentry_to_nvlentry(&arrayEntries.bulkentries_val[i],
-                                    &blkentriesp->nbulkentries_val[i]);
-           }
-       }
-       if (arrayEntries.bulkentries_val)
-           free(arrayEntries.bulkentries_val);
+
+       if (code)
+           return code;
+
+       /* Ensure the number of entries claimed matches the no. returned */
+       if (*entriesp < 0)
+           *entriesp = 0;
+       if (*entriesp > arrayEntries.bulkentries_len)
+           *entriesp = arrayEntries.bulkentries_len;
+
+       convertBulkToNBulk(&arrayEntries, blkentriesp);
+
+       xdr_free((xdrproc_t) xdr_bulkentries, &arrayEntries);
        return code;
     }
     code =
-       ubik_VL_ListAttributesN(cstruct, 0, attrp, entriesp,
-                 blkentriesp);
-    if (!newvlserver) {
+        ubik_VL_ListAttributesN(cstruct, 0, attrp, entriesp, blkentriesp);
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           newvlserver = 1;    /* Doesn't support new interface */
+           newvlserver = vltype_old;   /* Doesn't support new interface */
            goto tryold;
        } else if (!code) {
-           newvlserver = 2;
+           newvlserver = vltype_new;
        }
     }
+
+    /* Ensure the number of entries claimed matches the no. returned */
+    if (*entriesp < 0)
+       *entriesp = 0;
+    if (*entriesp > blkentriesp->nbulkentries_len)
+       *entriesp = blkentriesp->nbulkentries_len;
+
     return code;
 }
 
@@ -256,18 +289,26 @@ VLDB_ListAttributesN2(VldbListByAttributes *attrp,
                       nbulkentries *blkentriesp,
                       afs_int32 *nextindexp)
 {
-    afs_int32 code;
+    afs_int32 code = RXGEN_OPCODE;
 
-    code =
-       ubik_VL_ListAttributesN2(cstruct, 0, attrp, (name ? name : ""),
-                 thisindex, nentriesp, blkentriesp, nextindexp);
+    if (newvlserver != vltype_old) {
+        code =
+            ubik_VL_ListAttributesN2(cstruct, 0, attrp, (name ? name : ""),
+                                     thisindex, nentriesp, blkentriesp, nextindexp);
+       if (code)
+           return code;
+
+       /* Ensure the number of entries claimed matches the no. returned */
+       if (*nentriesp < 0)
+           *nentriesp = 0;
+       if (*nentriesp > blkentriesp->nbulkentries_len)
+           *nentriesp = blkentriesp->nbulkentries_len;
+    }
     return code;
 }
 
-
-static int vlserverv4 = -1;
 struct cacheips {
-    afs_int32 server;
+    afs_uint32 server;
     afs_int32 count;
     afs_uint32 addrs[16];
 };
@@ -281,9 +322,9 @@ struct cacheips cacheips[GETADDRUCACHESIZE];
 int cacheip_index = 0;
 
 int
-VLDB_IsSameAddrs(afs_int32 serv1, afs_int32 serv2, afs_int32 *errorp)
+VLDB_IsSameAddrs(afs_uint32 serv1, afs_uint32 serv2, afs_int32 *errorp)
 {
-    register int code;
+    int code;
     ListAddrByAttributes attrs;
     bulkaddrs addrs;
     afs_uint32 *addrp, i, j, f1, f2;
@@ -295,7 +336,7 @@ VLDB_IsSameAddrs(afs_int32 serv1, afs_int32 serv2, afs_int32 *errorp)
 
     if (serv1 == serv2)
        return 1;
-    if (vlserverv4 == 1) {
+    if (newvlserver == vltype_old || newvlserver == vltype_new) {
        return 0;
     }
     if (!initcache) {
@@ -331,12 +372,11 @@ VLDB_IsSameAddrs(afs_int32 serv1, afs_int32 serv2, afs_int32 *errorp)
     code =
        ubik_VL_GetAddrsU(cstruct, 0, &attrs, &uuid, &unique, &nentries,
                  &addrs);
-    if (vlserverv4 == -1) {
+    if (newvlserver == vltype_unknown) {
        if (code == RXGEN_OPCODE) {
-           vlserverv4 = 1;     /* Doesn't support new interface */
            return 0;
        } else if (!code) {
-           vlserverv4 = 2;
+           newvlserver = vltype_uuid;
        }
     }
     if (code == VL_NOENT)
@@ -389,7 +429,7 @@ vsu_ClientInit(int noAuthFlag, const char *confDir, char *cellName, afs_int32 sa
                struct ubik_client **uclientp,
               int (*secproc)(struct rx_securityClass *, afs_int32))
 {
-    return ugen_ClientInit(noAuthFlag, confDir, cellName, sauth, uclientp, 
+    return ugen_ClientInit(noAuthFlag, confDir, cellName, sauth, uclientp,
                           secproc, "vsu_ClientInit", vsu_rxkad_level,
                           VLDB_MAXSERVERS, AFSCONF_VLDBSERVICE, 90,
                           0, 0, USER_SERVICE_ID);