vlserver: always use the hostaddress table in GetAddrsU
[openafs.git] / src / vlserver / vlprocs.c
index aebc95c..0fba51c 100644 (file)
@@ -1263,7 +1263,7 @@ SVL_ListAttributes(struct rx_call *rxcall,
        return code;
     allocCount = VLDBALLOCCOUNT;
     Vldbentry = VldbentryFirst = vldbentries->bulkentries_val =
-       (vldbentry *) malloc(allocCount * sizeof(vldbentry));
+       malloc(allocCount * sizeof(vldbentry));
     if (Vldbentry == NULL) {
        code = VL_NOMEM;
        goto abort;
@@ -1348,9 +1348,8 @@ SVL_ListAttributes(struct rx_call *rxcall,
        && (allocCount > vldbentries->bulkentries_len)) {
 
        vldbentries->bulkentries_val =
-           (vldbentry *) realloc(vldbentries->bulkentries_val,
-                                 vldbentries->bulkentries_len *
-                                 sizeof(vldbentry));
+           realloc(vldbentries->bulkentries_val,
+                   vldbentries->bulkentries_len * sizeof(vldbentry));
        if (vldbentries->bulkentries_val == NULL) {
            code = VL_NOMEM;
            goto abort;
@@ -1394,7 +1393,7 @@ SVL_ListAttributesN(struct rx_call *rxcall,
        return code;
     allocCount = VLDBALLOCCOUNT;
     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val =
-       (nvldbentry *) malloc(allocCount * sizeof(nvldbentry));
+       malloc(allocCount * sizeof(nvldbentry));
     if (Vldbentry == NULL) {
        code = VL_NOMEM;
        goto abort;
@@ -1480,9 +1479,8 @@ SVL_ListAttributesN(struct rx_call *rxcall,
        && (allocCount > vldbentries->nbulkentries_len)) {
 
        vldbentries->nbulkentries_val =
-           (nvldbentry *) realloc(vldbentries->nbulkentries_val,
-                                  vldbentries->nbulkentries_len *
-                                  sizeof(nvldbentry));
+           realloc(vldbentries->nbulkentries_val,
+                   vldbentries->nbulkentries_len * sizeof(nvldbentry));
        if (vldbentries->nbulkentries_val == NULL) {
            code = VL_NOMEM;
            goto abort;
@@ -1545,7 +1543,7 @@ SVL_ListAttributesN2(struct rx_call *rxcall,
        return code;
 
     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val =
-       (nvldbentry *) malloc(maxCount * sizeof(nvldbentry));
+       malloc(maxCount * sizeof(nvldbentry));
     if (Vldbentry == NULL) {
        countAbort(this_op);
        ubik_AbortTrans(ctx.trans);
@@ -1814,7 +1812,7 @@ SVL_LinkedList(struct rx_call *rxcall,
            goto abort;
        }
 
-       vllist = (single_vldbentry *) malloc(sizeof(single_vldbentry));
+       vllist = malloc(sizeof(single_vldbentry));
        if (vllist == NULL) {
            code = VL_NOMEM;
            goto abort;
@@ -1887,7 +1885,7 @@ SVL_LinkedList(struct rx_call *rxcall,
                    continue;
            }
 
-           vllist = (single_vldbentry *) malloc(sizeof(single_vldbentry));
+           vllist = malloc(sizeof(single_vldbentry));
            if (vllist == NULL) {
                code = VL_NOMEM;
                goto abort;
@@ -1950,7 +1948,7 @@ SVL_LinkedListN(struct rx_call *rxcall,
            goto abort;
        }
 
-       vllist = (single_nvldbentry *) malloc(sizeof(single_nvldbentry));
+       vllist = malloc(sizeof(single_nvldbentry));
        if (vllist == NULL) {
            code = VL_NOMEM;
            goto abort;
@@ -2023,7 +2021,7 @@ SVL_LinkedListN(struct rx_call *rxcall,
                    continue;
            }
 
-           vllist = (single_nvldbentry *) malloc(sizeof(single_nvldbentry));
+           vllist = malloc(sizeof(single_nvldbentry));
            if (vllist == NULL) {
                code = VL_NOMEM;
                goto abort;
@@ -2109,7 +2107,7 @@ SVL_GetAddrs(struct rx_call *rxcall,
 
     VLog(5, ("GetAddrs\n"));
     addrsp->bulkaddrs_val = taddrp =
-       (afs_uint32 *) malloc(sizeof(afs_int32) * (MAXSERVERID + 1));
+       malloc(sizeof(afs_int32) * (MAXSERVERID + 1));
     nservers = *nentries = addrsp->bulkaddrs_len = 0;
 
     if (!taddrp) {
@@ -2133,7 +2131,16 @@ abort:
     return code;
 }
 
-#define PADDR(addr) VLog(0,("%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8) &0xff, addr&0xff));
+static_inline void
+append_addr(char *buffer, afs_uint32 addr, size_t buffer_size)
+{
+    int n = strlen(buffer);
+    if (buffer_size > n) {
+       snprintf(buffer + n, buffer_size - n, "%u.%u.%u.%u",
+                (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
+                addr & 0xff);
+    }
+}
 
 afs_int32
 SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
@@ -2144,6 +2151,7 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
     struct vl_ctx ctx;
     int cnt, h, i, j, k, m;
     struct extentaddr *exp = 0, *tex;
+    char addrbuf[256];
     afsUUID tuuid;
     afs_uint32 addrs[VL_MAXIPADDRS_PERMH];
     int base;
@@ -2248,28 +2256,26 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
        || (!foundUuidEntry && (count > 1))) {
        VLog(0,
             ("The following fileserver is being registered in the VLDB:\n"));
-       VLog(0, ("      ["));
-       for (k = 0; k < cnt; k++) {
+       for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
            if (k > 0)
-               VLog(0,(" "));
-           PADDR(addrs[k]);
+               strlcat(addrbuf, " ", sizeof(addrbuf));
+           append_addr(addrbuf, addrs[k], sizeof(addrbuf));
        }
-       VLog(0,("]\n"));
+       VLog(0, ("      [%s]\n", addrbuf));
 
        if (foundUuidEntry) {
            code = multiHomedExtent(&ctx, FoundUuid, &exp);
            if (code == 0) {
                VLog(0, ("   It would have replaced the existing VLDB server "
                         "entry:\n"));
-               VLog(0, ("      entry %d: [", FoundUuid));
-               for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
+               for (addrbuf[0] = '\0', mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
                    if (!exp->ex_addrs[mhidx])
                        continue;
                    if (mhidx > 0)
-                       VLog(0,(" "));
-                   PADDR(ntohl(exp->ex_addrs[mhidx]));
+                       strlcat(addrbuf, " ", sizeof(addrbuf));
+                   append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
                }
-               VLog(0, ("]\n"));
+               VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
            }
        }
 
@@ -2285,20 +2291,19 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
            if (code)
                goto abort;
 
+           addrbuf[0] = '\0';
            if (exp) {
-               VLog(0, ("["));
                for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
                    if (!exp->ex_addrs[mhidx])
                        continue;
                    if (mhidx > 0)
-                       VLog(0, (" "));
-                   PADDR(ntohl(exp->ex_addrs[mhidx]));
+                       strlcat(addrbuf, " ", sizeof(addrbuf));
+                   append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
                }
-               VLog(0, ("]"));
            } else {
-               PADDR(ctx.hostaddress[srvidx]);
+               append_addr(addrbuf, ctx.hostaddress[srvidx], sizeof(addrbuf));
            }
-           VLog(0, ("\n"));
+           VLog(0, ("      entry %d: [%s]\n", srvidx, addrbuf));
        }
 
        if (count == 1)
@@ -2341,26 +2346,24 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
     }
 
     VLog(0, ("The following fileserver is being registered in the VLDB:\n"));
-    VLog(0, ("      ["));
-    for (k = 0; k < cnt; k++) {
+    for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
        if (k > 0)
-           VLog(0, (" "));
-       PADDR(addrs[k]);
+           strlcat(addrbuf, " ", sizeof(addrbuf));
+       append_addr(addrbuf, addrs[k], sizeof(addrbuf));
     }
-    VLog(0, ("]\n"));
+    VLog(0, ("      [%s]\n", addrbuf));
 
     if (foundUuidEntry) {
        VLog(0,
            ("   It will replace the following existing entry in the VLDB (same uuid):\n"));
-       VLog(0, ("      entry %d: [", FoundUuid));
-       for (k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
+       for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
            if (exp->ex_addrs[k] == 0)
                continue;
            if (k > 0)
-               VLog(0, (" "));
-           PADDR(ntohl(exp->ex_addrs[k]));
+               strlcat(addrbuf, " ", sizeof(addrbuf));
+           append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
        }
-       VLog(0, ("]\n"));
+       VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
     } else if (willReplaceCnt || (count == 1)) {
        /* If we are not replacing an entry and there is only one entry to change,
         * then we will replace that entry.
@@ -2378,23 +2381,22 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
        if (exp) {
            VLog(0,
                ("   It will replace the following existing entry in the VLDB (new uuid):\n"));
-           VLog(0, ("      entry %d: [", ReplaceEntry));
-           for (k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
+           for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
                if (exp->ex_addrs[k] == 0)
                    continue;
                if (k > 0)
-                   VLog(0, (" "));
-               PADDR(ntohl(exp->ex_addrs[k]));
+                   strlcat(addrbuf, " ", sizeof(addrbuf));
+               append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
            }
-           VLog(0, ("]\n"));
+           VLog(0, ("      entry %d: [%s]\n", ReplaceEntry, addrbuf));
        } else {
            /* Not a mh entry. So we have to create a new mh entry and
             * put it on the ReplaceEntry slot of the ctx.hostaddress array.
             */
-           VLog(0, ("   It will replace existing entry %d, ", ReplaceEntry));
-           PADDR(ctx.hostaddress[ReplaceEntry]);
-           VLog(0,(", in the VLDB (new uuid):\n"));
-
+           addrbuf[0] = '\0';
+           append_addr(addrbuf, ctx.hostaddress[ReplaceEntry], sizeof(addrbuf));
+           VLog(0, ("   It will replace existing entry %d, %s,"
+                    " in the VLDB (new uuid):\n", ReplaceEntry, addrbuf));
            code =
                FindExtentBlock(&ctx, uuidp, 1, ReplaceEntry, &exp, &base);
            if (code || !exp) {
@@ -2459,12 +2461,11 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
            VLog(0,
                ("   The following existing entries in the VLDB will be updated:\n"));
 
-       VLog(0, ("      entry %d: [", WillChange[i]));
-       for (h = j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
+       for (addrbuf[0] = '\0', h = j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
            if (tex->ex_addrs[j]) {
                if (j > 0)
-                   printf(" ");
-               PADDR(ntohl(tex->ex_addrs[j]));
+                   strlcat(addrbuf, " ", sizeof(addrbuf));
+               append_addr(addrbuf, ntohl(tex->ex_addrs[j]), sizeof(addrbuf));
            }
 
            for (k = 0; k < cnt; k++) {
@@ -2480,7 +2481,7 @@ SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
        for (j = h; j < VL_MAXIPADDRS_PERMH; j++) {
            tex->ex_addrs[j] = 0;       /* zero rest of mh entry */
        }
-       VLog(0, ("]\n"));
+       VLog(0, ("      entry %d: [%s]\n", WillChange[i], addrbuf));
 
        /* Write out the modified mh entry */
        tex->ex_uniquifier = htonl(ntohl(tex->ex_uniquifier) + 1);
@@ -2510,7 +2511,7 @@ SVL_GetAddrsU(struct rx_call *rxcall,
              bulkaddrs *addrsp)
 {
     int this_op = VLGETADDRSU;
-    afs_int32 code, index = -1, offset;
+    afs_int32 code, index;
     struct vl_ctx ctx;
     int nservers, i, j, base = 0;
     struct extentaddr *exp = 0;
@@ -2530,15 +2531,13 @@ SVL_GetAddrsU(struct rx_call *rxcall,
            code = VL_BADMASK;
            goto abort;
        }
-       for (base = 0; base < VL_MAX_ADDREXTBLKS; base++) {
-           if (!ctx.ex_addr[base])
-               break;
-           for (i = 1; i < VL_MHSRV_PERBLK; i++) {
-               exp = &ctx.ex_addr[base][i];
-               tuuid = exp->ex_hostuuid;
-               afs_ntohuuid(&tuuid);
-               if (afs_uuid_is_nil(&tuuid))
-                   continue;
+       /* Search for a server registered with the VLDB with this ip address. */
+       for (index = 0; index <= MAXSERVERID; index++) {
+           code = multiHomedExtent(&ctx, index, &exp);
+           if (code)
+               continue;
+
+           if (exp) {
                for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
                    if (exp->ex_addrs[j]
                        && (ntohl(exp->ex_addrs[j]) == attributes->ipaddr)) {
@@ -2548,10 +2547,8 @@ SVL_GetAddrsU(struct rx_call *rxcall,
                if (j < VL_MAXIPADDRS_PERMH)
                    break;
            }
-           if (i < VL_MHSRV_PERBLK)
-               break;
        }
-       if (base >= VL_MAX_ADDREXTBLKS) {
+       if (index > MAXSERVERID) {
            code = VL_NOENT;
            goto abort;
        }
@@ -2560,22 +2557,17 @@ SVL_GetAddrsU(struct rx_call *rxcall,
            code = VL_BADMASK;
            goto abort;
        }
-       index = attributes->index;
-       if (index < 1 || index >= (VL_MAX_ADDREXTBLKS * VL_MHSRV_PERBLK)) {
+       /* VLADDR_INDEX index is one based */
+       if (attributes->index < 1 || attributes->index > MAXSERVERID) {
            code = VL_INDEXERANGE;
            goto abort;
        }
-       base = index / VL_MHSRV_PERBLK;
-       offset = index % VL_MHSRV_PERBLK;
-       if (offset == 0) {
+       index = attributes->index - 1;
+       code = multiHomedExtent(&ctx, index, &exp);
+       if (code) {
            code = VL_NOENT;
            goto abort;
        }
-       if (!ctx.ex_addr[base]) {
-           code = VL_INDEXERANGE;
-           goto abort;
-       }
-       exp = &ctx.ex_addr[base][offset];
     } else if (attributes->Mask & VLADDR_UUID) {
        if (attributes->Mask & (VLADDR_IPADDR | VLADDR_INDEX)) {
            code = VL_BADMASK;
@@ -2598,7 +2590,7 @@ SVL_GetAddrsU(struct rx_call *rxcall,
        goto abort;
     }
     addrsp->bulkaddrs_val = taddrp =
-       (afs_uint32 *) malloc(sizeof(afs_int32) * (MAXSERVERID + 1));
+       malloc(sizeof(afs_int32) * (MAXSERVERID + 1));
     nservers = *nentries = addrsp->bulkaddrs_len = 0;
     if (!taddrp) {
        code = VL_NOMEM;
@@ -2664,9 +2656,8 @@ put_attributeentry(struct vl_ctx *ctx,
         * then grow in increments of VLDBALLOCINCR.
         */
        allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
-       reall =
-           (vldbentry *) realloc(*VldbentryFirst,
-                                 (*alloccnt + allo) * sizeof(vldbentry));
+       reall = realloc(*VldbentryFirst,
+                       (*alloccnt + allo) * sizeof(vldbentry));
        if (reall == NULL)
            return VL_NOMEM;
 
@@ -2711,9 +2702,8 @@ put_nattributeentry(struct vl_ctx *ctx,
         * then grow in increments of VLDBALLOCINCR.
         */
        allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
-       reall =
-           (nvldbentry *) realloc(*VldbentryFirst,
-                                  (*alloccnt + allo) * sizeof(nvldbentry));
+       reall = realloc(*VldbentryFirst,
+                       (*alloccnt + allo) * sizeof(nvldbentry));
        if (reall == NULL)
            return VL_NOMEM;
 
@@ -3325,6 +3315,8 @@ ChangeIPAddr(struct vl_ctx *ctx, afs_uint32 ipaddr1, afs_uint32 ipaddr2)
     int pollcount = 0;
     struct nvlentry tentry;
     int ipaddr1_id = -1, ipaddr2_id = -1;
+    char addrbuf1[256];
+    char addrbuf2[256];
 
     /* Don't let addr change to 256.*.*.* : Causes internal error below */
     if ((ipaddr2 & 0xff000000) == 0xff000000)
@@ -3415,25 +3407,22 @@ ChangeIPAddr(struct vl_ctx *ctx, afs_uint32 ipaddr1, afs_uint32 ipaddr2)
     VLog(0,
         ("The following IP address is being %s:\n",
          (ipaddr2 ? "changed" : "removed")));
-    VLog(0, ("      entry %d: ", i));
+    addrbuf1[0] = addrbuf2[0] = '\0';
     if (exp) {
-       VLog(0, ("["));
        for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
            if (!exp->ex_addrs[mhidx])
                continue;
            if (mhidx > 0)
-               VLog(0, (" "));
-           PADDR(ntohl(exp->ex_addrs[mhidx]));
+               strlcat(addrbuf1, " ", sizeof(addrbuf1));
+           append_addr(addrbuf1, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf1));
        }
-       VLog(0, ("]"));
     } else {
-       PADDR(ipaddr1);
+       append_addr(addrbuf1, ipaddr1, sizeof(addrbuf1));
     }
     if (ipaddr2) {
-       VLog(0, (" -> "));
-       PADDR(ipaddr2);
+       append_addr(addrbuf2, ipaddr2, sizeof(addrbuf2));
     }
-    VLog(0, ("\n"));
+    VLog(0, ("      entry %d: [%s] -> [%s]\n", i, addrbuf1, addrbuf2));
 
     /* Change the registered uuuid addresses */
     if (exp) {