afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / vlserver / vlprocs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afsconfig.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #include <stdio.h>
17 #include <lock.h>
18 #include <afs/afsutil.h>
19 #include <ubik.h>
20 #include <rx/xdr.h>
21 #include <rx/rx.h>
22 #ifdef AFS_NT40_ENV
23 #include <winsock2.h>
24 #else
25 #include <netinet/in.h>
26 #endif
27 #include <afs/keys.h>
28 #include "vlserver.h"
29 #include "afs/audit.h"
30 #ifndef AFS_NT40_ENV
31 #include <unistd.h>
32 #endif
33
34 extern int smallMem;
35 extern extent_mod;
36 extern struct afsconf_dir *vldb_confdir;
37 extern struct ubik_dbase *VL_dbase;
38 struct vlheader cheader;        /* kept in network byte order */
39 extern afs_uint32 HostAddress[];        /* host addresses kept in host byte order */
40 int maxnservers;
41 struct extentaddr *ex_addr[VL_MAX_ADDREXTBLKS] = { 0, 0, 0, 0};
42 static char rxinfo_str[128];    /* Need rxinfo string to be non-local */
43 #define ABORT(c) { errorcode = (c); goto abort; }
44 #undef END
45 #define END(c) { errorcode = (c); goto end; }
46
47 #define VLDBALLOCLIMIT  10000
48 #define VLDBALLOCINCR   2048
49
50 static int put_attributeentry();
51 static int put_nattributeentry();
52 static int RemoveEntry();
53 static ReleaseEntry();
54 static int check_vldbentry();
55 static int check_nvldbentry();
56 static int vldbentry_to_vlentry();
57 static int nvldbentry_to_vlentry();
58 static get_vldbupdateentry();
59 static int repsite_exists();
60 static repsite_compress();
61 static vlentry_to_vldbentry();
62 static vlentry_to_nvldbentry();
63 static vlentry_to_uvldbentry();
64 static int InvalidVolname();
65 static int InvalidVoltype();
66 static int InvalidOperation();
67 static int InvalidReleasetype();
68 static int IpAddrToRelAddr();
69 static int ChangeIPAddr();
70
71 char *rxinfo(rxcall)
72     struct rx_call      *rxcall;
73 {
74     int code;
75     register struct rx_connection *tconn;
76     char tname[64];
77     char tinst[64];
78     char tcell[64];
79     afs_uint32 exp;
80     struct in_addr hostAddr;
81     
82     tconn = rx_ConnectionOf(rxcall);
83     hostAddr.s_addr = rx_HostOf(rx_PeerOf(tconn));
84     code = rxkad_GetServerInfo(rxcall->conn, (afs_int32 *) 0, &exp, tname, tinst, tcell, (afs_int32 *) 0);
85     if (!code)
86         sprintf(rxinfo_str,"%s %s", inet_ntoa(hostAddr), tname);
87     else
88         sprintf(rxinfo_str,"%s noauth",inet_ntoa(hostAddr));
89     return(rxinfo_str);
90 }
91
92 /* This is called to initialize the database, set the appropriate locks and make sure that the vldb header is valid */
93 int Init_VLdbase (trans, locktype, this_op)
94   struct ubik_trans     **trans;
95   int                   locktype;               /* indicate read or write transaction */
96   int                   this_op;
97
98   int errorcode=0, pass, wl;
99
100   for (pass=1; pass<=3; pass++) {
101       if (pass == 2) { /* take write lock to rebuild the db */
102           errorcode = ubik_BeginTrans (VL_dbase, UBIK_WRITETRANS, trans);
103           wl = 1;
104       }
105       else if (locktype == LOCKREAD) {
106           errorcode = ubik_BeginTransReadAny (VL_dbase, UBIK_READTRANS, trans);
107           wl = 0;
108       }
109       else {
110           errorcode = ubik_BeginTrans (VL_dbase, UBIK_WRITETRANS, trans);
111           wl = 1;
112       }
113       if (errorcode) 
114           return errorcode;
115
116       errorcode = ubik_SetLock (*trans, 1, 1, locktype);
117       if (errorcode) {
118           COUNT_ABO;
119           ubik_AbortTrans (*trans);
120           return errorcode;
121       }
122
123       /* check that dbase is initialized and setup cheader */
124       /* 2nd pass we try to rebuild the header */
125       errorcode = CheckInit(*trans, ((pass==2)?1:0));
126       if (!errorcode && wl && extent_mod)
127          errorcode = readExtents(*trans); /* Fix the mh extent blocks */
128       if (errorcode) {
129           COUNT_ABO;
130           ubik_AbortTrans(*trans);
131           /* Only rebuld if the database is empty */
132           /* Exit if can't rebuild */
133           if ((pass == 1) && (errorcode != VL_EMPTY)) return errorcode;
134           if (pass == 2) return errorcode;
135       }
136       else {                                 /* No errorcode */
137           if (pass == 2) {
138               ubik_EndTrans(*trans);         /* Rebuilt db. End trans, then retake original lock */
139           }
140           else {
141               break;                         /* didn't rebuild and successful - exit */
142           }
143       }
144   }
145   return errorcode;
146 }
147
148
149 /* Create a new vldb entry; both new volume id and name must be unique (non-existant in vldb). */
150
151 VL_CreateEntry(rxcall, newentry)
152   struct rx_call        *rxcall;
153   struct vldbentry      *newentry;
154 {   struct ubik_trans   *trans;
155     afs_int32           errorcode, blockindex;
156     struct nvlentry     tentry;
157
158     COUNT_REQ (VLCREATEENTRY);
159     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0)) {
160         errorcode = VL_PERM;
161         goto end;
162       }
163
164     /* Do some validity tests on new entry */
165     if (   (errorcode = check_vldbentry(newentry))
166         || (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op)))
167         goto end;
168
169     VLog(1, ("OCreate Volume %d %s\n", newentry->volumeId[RWVOL], rxinfo(rxcall)));
170     /* XXX shouldn't we check to see if the r/o volume is duplicated? */
171     if (newentry->volumeId[RWVOL] 
172         && FindByID(trans, newentry->volumeId[RWVOL], RWVOL, &tentry,
173                     &errorcode)) {      /* entry already exists, we fail */
174         errorcode =  VL_IDEXIST;
175         goto abort;
176     } else if (errorcode) {
177         goto abort;
178     }
179
180     /* Is this following check (by volume name) necessary?? */
181     /* If entry already exists, we fail */
182     if (FindByName(trans, newentry->name, &tentry, &errorcode)) {
183       errorcode =   VL_NAMEEXIST;
184       goto abort;
185     } else if (errorcode) {
186       goto abort;
187     }   
188
189     blockindex = AllocBlock(trans, &tentry);
190     if (blockindex == 0) {
191       errorcode = VL_CREATEFAIL;
192       goto abort;
193     }
194
195     bzero(&tentry, sizeof(struct nvlentry));
196     /* Convert to its internal representation; both in host byte order */
197     if (errorcode = vldbentry_to_vlentry(trans, newentry, &tentry))     {  
198         FreeBlock(trans, blockindex);
199         goto abort;
200     }
201
202     /* Actually insert the entry in vldb */
203     errorcode = ThreadVLentry(trans, blockindex, &tentry);
204     if (errorcode) {
205         FreeBlock(trans, blockindex);
206         goto abort;
207     }
208     else {
209       errorcode = ubik_EndTrans(trans);
210       goto end;
211     }
212
213   abort:
214     COUNT_ABO;
215     ubik_AbortTrans(trans);
216
217   end:
218     osi_auditU (rxcall, VLCreateEntryEvent, errorcode,
219                 AUD_STR, (newentry ? newentry->name : (char *)0), AUD_END);
220     return errorcode;
221 }
222
223
224 VL_CreateEntryN(rxcall, newentry)
225   struct rx_call        *rxcall;
226   struct nvldbentry     *newentry;
227 {   struct ubik_trans   *trans;
228     afs_int32           errorcode, blockindex;
229     struct nvlentry     tentry;
230
231     COUNT_REQ (VLCREATEENTRYN);
232     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0)) {
233         errorcode = VL_PERM;
234         goto end;
235       }
236
237     /* Do some validity tests on new entry */
238     if (   (errorcode = check_nvldbentry(newentry))
239         || (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op)))
240         goto end;
241
242     VLog(1, ("Create Volume %d %s\n", newentry->volumeId[RWVOL], rxinfo(rxcall)));
243     /* XXX shouldn't we check to see if the r/o volume is duplicated? */
244     if (newentry->volumeId[RWVOL] 
245         && FindByID(trans, newentry->volumeId[RWVOL], RWVOL, &tentry,
246                     &errorcode)) {      /* entry already exists, we fail */
247         errorcode =  VL_IDEXIST;
248         goto abort;
249     } else if (errorcode) {
250         goto abort;
251     }
252
253     /* Is this following check (by volume name) necessary?? */
254     /* If entry already exists, we fail */
255     if (FindByName(trans, newentry->name, &tentry, &errorcode)) {
256       errorcode =   VL_NAMEEXIST;
257       goto abort;
258     } else if (errorcode) {
259       goto abort;
260     }   
261
262     blockindex = AllocBlock(trans, &tentry);
263     if (blockindex == 0) {
264       errorcode = VL_CREATEFAIL;
265       goto abort;
266     }
267
268     bzero(&tentry, sizeof(struct nvlentry));
269     /* Convert to its internal representation; both in host byte order */
270     if (errorcode = nvldbentry_to_vlentry(trans, newentry, &tentry))    {  
271         FreeBlock(trans, blockindex);
272         goto abort;
273     }
274
275     /* Actually insert the entry in vldb */
276     errorcode = ThreadVLentry(trans, blockindex, &tentry);
277     if (errorcode) {
278         FreeBlock(trans, blockindex);
279         goto abort;
280     }
281     else {
282       errorcode = ubik_EndTrans(trans);
283       goto end;
284     }
285
286   abort:
287     COUNT_ABO;
288     ubik_AbortTrans(trans);
289
290   end:
291     osi_auditU (rxcall, VLCreateEntryEvent, errorcode,
292                 AUD_STR, (newentry ? newentry->name : (char *)0), AUD_END);
293     return errorcode;
294 }
295
296
297 VL_ChangeAddr(rxcall, ip1, ip2)
298   struct rx_call        *rxcall;
299   afs_int32 ip1, ip2;
300 {   struct ubik_trans   *trans;
301     afs_int32           errorcode, blockindex;
302     struct nvlentry     tentry;
303
304     COUNT_REQ (VLCHANGEADDR);
305     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0)) {
306         errorcode = VL_PERM;
307         goto end;
308       }
309
310     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op))
311         goto end;
312
313     VLog(1, ("Change Addr %d -> %d %s\n", ip1, ip2, rxinfo(rxcall)));
314     if (errorcode = ChangeIPAddr(ip1, ip2, trans))
315        goto abort;
316     else {
317       errorcode = ubik_EndTrans(trans);
318       goto end;
319     }
320
321   abort:
322     COUNT_ABO;
323     ubik_AbortTrans(trans);
324
325   end:
326     osi_auditU (rxcall, VLChangeAddrEvent, errorcode, AUD_LONG, ip1,
327                 AUD_LONG, ip2, AUD_END);
328     return errorcode;
329 }
330
331 /* Delete a vldb entry given the volume id. */
332 VL_DeleteEntry(rxcall, volid, voltype)
333   struct rx_call        *rxcall;
334   afs_int32                     volid;
335   afs_int32                     voltype;
336 {   struct ubik_trans   *trans;
337     afs_int32           blockindex, errorcode;
338     struct nvlentry     tentry;
339
340     COUNT_REQ (VLDELETEENTRY);
341     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0)) 
342         END(VL_PERM);
343
344     if ((voltype != -1 ) && (InvalidVoltype(voltype)))
345         END( VL_BADVOLTYPE );
346
347     if (errorcode = Init_VLdbase (&trans, LOCKWRITE, this_op))
348         goto end;
349
350     VLog(1, ("Delete Volume %d %s\n", volid, rxinfo(rxcall)));
351     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
352     if (blockindex == 0) {                      /* volid not found */
353         if (!errorcode) errorcode = VL_NOENT;
354         goto abort;
355     }
356
357     if (tentry.flags & VLDELETED) {     /* Already deleted; return */
358         ABORT( VL_ENTDELETED );
359     }
360     if (errorcode = RemoveEntry(trans, blockindex, &tentry)) {
361         goto abort;
362     }
363     errorcode = (ubik_EndTrans(trans));
364     goto end;
365
366   abort:
367     COUNT_ABO;
368     ubik_AbortTrans(trans);
369
370   end:
371     osi_auditU (rxcall, VLDeleteEntryEvent, errorcode, AUD_LONG, volid, AUD_END);
372     return errorcode;
373 }
374
375
376 /* Get a vldb entry given its volume id; make sure it's not a deleted entry. */
377 GetEntryByID (rxcall, volid, voltype, aentry, new, this_op)
378   struct rx_call        *rxcall;
379   afs_int32                     volid;
380   afs_int32                     voltype, new, this_op;
381   char                  *aentry;                /* entry data copied here */
382 {   struct ubik_trans   *trans;
383     afs_int32           blockindex, errorcode;
384     struct nvlentry     tentry;
385
386     if ((voltype != -1 ) && (InvalidVoltype(voltype)))
387         return VL_BADVOLTYPE;
388     if (errorcode = Init_VLdbase (&trans, LOCKREAD, this_op)) 
389         return errorcode;
390
391     VLog(5, ("GetVolumeByID %d (%d) %s\n", volid, new, rxinfo(rxcall)));
392     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
393     if (blockindex == 0) {                      /* entry not found */
394         if (!errorcode) errorcode = VL_NOENT;
395         COUNT_ABO;
396         ubik_AbortTrans (trans);
397         return errorcode;
398     }
399     if (tentry.flags & VLDELETED) {     /* Entry is deleted! */
400         COUNT_ABO;
401         ubik_AbortTrans (trans);
402         return VL_ENTDELETED;
403     }
404   /* Convert from the internal to external form */
405     if (new == 1)
406         vlentry_to_nvldbentry(&tentry, (struct nvldbentry *)aentry);
407     else if (new == 2)
408         vlentry_to_uvldbentry(&tentry, (struct uvldbentry *)aentry);
409     else
410         vlentry_to_vldbentry(&tentry, (struct vldbentry *)aentry);
411     return(ubik_EndTrans(trans));
412 }
413
414 VL_GetEntryByID (rxcall, volid, voltype, aentry)
415   struct rx_call        *rxcall;
416   afs_int32                     volid, voltype;
417   vldbentry             *aentry;                /* entry data copied here */
418 {
419     COUNT_REQ (VLGETENTRYBYID);
420     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 0, this_op));
421 }
422
423 VL_GetEntryByIDN (rxcall, volid, voltype, aentry)
424   struct rx_call        *rxcall;
425   afs_int32                     volid, voltype;
426   nvldbentry            *aentry;                /* entry data copied here */
427 {
428     COUNT_REQ (VLGETENTRYBYIDN);
429     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 1, this_op));
430 }
431
432 VL_GetEntryByIDU (rxcall, volid, voltype, aentry)
433   struct rx_call        *rxcall;
434   afs_int32                     volid, voltype;
435   uvldbentry            *aentry;                /* entry data copied here */
436 {
437     COUNT_REQ (VLGETENTRYBYIDU);
438     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 2, this_op));
439 }
440
441
442
443 /* returns true if the id is a decimal integer, in which case we interpret it
444     as an id.  make the cache manager much simpler */
445 static int NameIsId(aname)
446 register char *aname; {
447     register int tc;
448     while (tc = *aname++) {
449         if (tc > '9' || tc < '0') return 0;
450     }
451     return 1;
452 }
453
454 /* Get a vldb entry given the volume's name; of course, very similar to VLGetEntryByID() above. */
455 GetEntryByName (rxcall, volname, aentry, new, this_op)
456   struct rx_call        *rxcall;
457   char                  *volname;
458   char                  *aentry;                /* entry data copied here */
459   int                   new, this_op;
460 {   struct ubik_trans   *trans;
461     afs_int32           blockindex, errorcode;
462     struct nvlentry     tentry;
463
464     if (NameIsId(volname)) {
465         return GetEntryByID(rxcall, atoi(volname), -1, aentry, new, this_op);
466     }
467     if (InvalidVolname(volname))
468         return VL_BADNAME;
469     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op)) 
470         return errorcode;
471     VLog(5, ("GetVolumeByName %s (%d) %s\n", volname, new, rxinfo(rxcall)));
472     blockindex = FindByName(trans, volname, &tentry, &errorcode);
473     if (blockindex == 0) {                      /* entry not found */
474         if (!errorcode) errorcode = VL_NOENT;
475         COUNT_ABO;
476         ubik_AbortTrans(trans);
477         return errorcode;
478     }
479     if (tentry.flags & VLDELETED) {     /* Entry is deleted */
480         COUNT_ABO;
481         ubik_AbortTrans(trans);
482         return VL_ENTDELETED;
483     }
484     /* Convert to external entry representation */
485     if (new == 1)
486         vlentry_to_nvldbentry(&tentry, (struct nvldbentry *)aentry); 
487     else if (new == 2)
488         vlentry_to_uvldbentry(&tentry, (struct uvldbentry *)aentry);
489     else
490         vlentry_to_vldbentry(&tentry, (struct vldbentry *)aentry); 
491     return(ubik_EndTrans(trans));
492 }
493
494 VL_GetEntryByNameO (rxcall, volname, aentry)
495   struct rx_call        *rxcall;
496   char                  *volname;
497   struct vldbentry      *aentry;                /* entry data copied here */
498 {
499     COUNT_REQ (VLGETENTRYBYNAME);
500     return (GetEntryByName(rxcall, volname, (char *)aentry, 0, this_op));
501 }
502
503
504 VL_GetEntryByNameN (rxcall, volname, aentry)
505   struct rx_call        *rxcall;
506   char                  *volname;
507   struct nvldbentry     *aentry;                /* entry data copied here */
508 {
509     COUNT_REQ (VLGETENTRYBYNAMEN);
510     return (GetEntryByName(rxcall, volname, (char *)aentry, 1, this_op));
511 }
512
513 VL_GetEntryByNameU (rxcall, volname, aentry)
514   struct rx_call        *rxcall;
515   char                  *volname;
516   struct uvldbentry     *aentry;                /* entry data copied here */
517 {
518     COUNT_REQ (VLGETENTRYBYNAMEU);
519     return (GetEntryByName(rxcall, volname, (char *)aentry, 2, this_op));
520 }
521
522
523
524 /* Get the current value of the maximum volume id and bump the volume id counter by Maxvolidbump. */
525 VL_GetNewVolumeId (rxcall, Maxvolidbump, newvolumeid)
526 struct rx_call          *rxcall;
527 afs_int32                       Maxvolidbump;
528 afs_int32                       *newvolumeid;
529 {   register afs_int32      errorcode, maxvolumeid; 
530     struct ubik_trans *trans;
531
532     COUNT_REQ (VLGETNEWVOLUMEID);
533     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0)) 
534         END( VL_PERM );
535
536     if (Maxvolidbump < 0 || Maxvolidbump > MAXBUMPCOUNT)
537         END ( VL_BADVOLIDBUMP );
538
539     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op)) 
540       goto end;
541
542     *newvolumeid = maxvolumeid = ntohl(cheader.vital_header.MaxVolumeId);
543     maxvolumeid += Maxvolidbump;
544     VLog(1, ("GetNewVolid newmax=%d %s\n", maxvolumeid, rxinfo(rxcall)));
545     cheader.vital_header.MaxVolumeId = htonl(maxvolumeid);
546     if (write_vital_vlheader(trans)) {
547       ABORT( VL_IO );
548     }
549     errorcode = (ubik_EndTrans(trans));
550     goto end;
551
552   abort:
553     COUNT_ABO;
554     ubik_AbortTrans(trans);
555
556   end:
557     osi_auditU (rxcall, VLGetNewVolumeIdEvent, errorcode, AUD_END);
558     return errorcode;
559 }
560
561
562 /* Simple replace the contents of the vldb entry, volid, with
563  * newentry. No individual checking/updating per field (alike
564  * VLUpdateEntry) is done. */
565
566 VL_ReplaceEntry (rxcall, volid, voltype, newentry, releasetype)
567 struct rx_call          *rxcall;
568 afs_int32                       volid;
569 afs_int32                       voltype;
570 struct vldbentry        *newentry;
571 afs_int32                       releasetype;
572 {   struct ubik_trans   *trans;
573     afs_int32           blockindex, errorcode, typeindex;
574     int                 hashnewname;
575     int                 hashVol[MAXTYPES];
576     struct nvlentry     tentry;
577
578     COUNT_REQ (VLREPLACEENTRY);
579     for(typeindex = 0; typeindex < MAXTYPES; typeindex++)
580         hashVol[typeindex] = 0;
581     hashnewname = 0;
582     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))  
583         END ( VL_PERM );
584
585     if (errorcode = check_vldbentry(newentry))
586         goto end;
587
588     if (voltype != -1 && InvalidVoltype(voltype))
589       END ( VL_BADVOLTYPE );
590
591     if (releasetype && InvalidReleasetype(releasetype))
592       END( VL_BADRELLOCKTYPE );
593     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op))
594       goto end;
595
596     VLog(1, ("OReplace Volume %d %s\n", volid, rxinfo(rxcall)));
597     /* find vlentry we're changing */
598     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
599     if (blockindex == 0) {                      /* entry not found */
600         if (!errorcode) errorcode = VL_NOENT;
601         goto abort;
602     }
603
604     /* check that we're not trying to change the RW vol ID */
605     if (newentry->volumeId[RWVOL] != tentry.volumeId[RWVOL]) {
606         ABORT( VL_BADENTRY );
607     }
608
609     /* unhash volid entries if they're disappearing or changing.
610      * Remember if we need to hash in the new value (we don't have to
611      * rehash if volid stays same */
612     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
613         if (tentry.volumeId[typeindex] != newentry->volumeId[typeindex]) {
614             if (tentry.volumeId[typeindex])
615                 if (errorcode = UnhashVolid(trans, typeindex, blockindex,
616                                             &tentry)) {
617                     goto abort;
618                 }
619             /* we must rehash new id if the id is different and the ID is nonzero */
620             hashVol[typeindex] = 1;     /* must rehash this guy if he exists */
621         }
622     }
623    
624     /* Rehash volname if it changes */
625     if (strcmp(newentry->name, tentry.name)) {  /* Name changes; redo hashing */
626         if (errorcode = UnhashVolname(trans, blockindex, &tentry)) {
627             goto abort;
628         }
629         hashnewname = 1;
630     }
631
632     /* after this, tentry is new entry, not old one.  vldbentry_to_vlentry
633        doesn't touch hash chains */
634     if (errorcode = vldbentry_to_vlentry(trans, newentry, &tentry)) {
635         goto abort;
636     }
637
638     for(typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
639         if (hashVol[typeindex] && tentry.volumeId[typeindex]) {
640             if (errorcode = HashVolid(trans, typeindex, blockindex, &tentry)) {
641                 goto abort;
642             }
643         }
644     }
645
646     if (hashnewname)
647         HashVolname(trans, blockindex, &tentry);
648
649     if (releasetype)
650         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
651     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
652         ABORT( VL_IO );
653     }
654
655     END (ubik_EndTrans(trans));
656
657   abort:
658     COUNT_ABO;
659     ubik_AbortTrans(trans);
660
661   end:
662     osi_auditU (rxcall, VLReplaceVLEntryEvent, errorcode, AUD_LONG, volid, AUD_END);
663     return errorcode;
664 }
665
666 VL_ReplaceEntryN (rxcall, volid, voltype, newentry, releasetype)
667 struct rx_call          *rxcall;
668 afs_int32                       volid;
669 afs_int32                       voltype;
670 struct nvldbentry       *newentry;
671 afs_int32                       releasetype;
672 {   struct ubik_trans   *trans;
673     afs_int32           blockindex, errorcode, typeindex;
674     int                 hashnewname;
675     int                 hashVol[MAXTYPES];
676     struct nvlentry     tentry;
677
678     COUNT_REQ (VLREPLACEENTRYN);
679     for(typeindex = 0; typeindex < MAXTYPES; typeindex++)
680         hashVol[typeindex] = 0;
681     hashnewname = 0;
682     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))  
683         END ( VL_PERM );
684
685     if (errorcode = check_nvldbentry(newentry))
686         goto end;
687
688     if (voltype != -1 && InvalidVoltype(voltype))
689       END ( VL_BADVOLTYPE );
690
691     if (releasetype && InvalidReleasetype(releasetype))
692       END( VL_BADRELLOCKTYPE );
693     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op))
694       goto end;
695
696     VLog(1, ("Replace Volume %d %s\n", volid, rxinfo(rxcall)));
697     /* find vlentry we're changing */
698     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
699     if (blockindex == 0) {                      /* entry not found */
700         if (!errorcode) errorcode = VL_NOENT;
701         goto abort;
702     }
703
704     /* check that we're not trying to change the RW vol ID */
705     if (newentry->volumeId[RWVOL] != tentry.volumeId[RWVOL]) {
706         ABORT( VL_BADENTRY );
707     }
708
709     /* unhash volid entries if they're disappearing or changing.
710      * Remember if we need to hash in the new value (we don't have to
711      * rehash if volid stays same */
712     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
713         if (tentry.volumeId[typeindex] != newentry->volumeId[typeindex]) {
714             if (tentry.volumeId[typeindex])
715                 if (errorcode = UnhashVolid(trans, typeindex, blockindex,
716                                             &tentry)) {
717                     goto abort;
718                 }
719             /* we must rehash new id if the id is different and the ID is nonzero */
720             hashVol[typeindex] = 1;     /* must rehash this guy if he exists */
721         }
722     }
723    
724     /* Rehash volname if it changes */
725     if (strcmp(newentry->name, tentry.name)) {  /* Name changes; redo hashing */
726         if (errorcode = UnhashVolname(trans, blockindex, &tentry)) {
727             goto abort;
728         }
729         hashnewname = 1;
730     }
731
732     /* after this, tentry is new entry, not old one.  vldbentry_to_vlentry
733        doesn't touch hash chains */
734     if (errorcode = nvldbentry_to_vlentry(trans, newentry, &tentry)) {
735         goto abort;
736     }
737
738     for(typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
739         if (hashVol[typeindex] && tentry.volumeId[typeindex]) {
740             if (errorcode = HashVolid(trans, typeindex, blockindex, &tentry)) {
741                 goto abort;
742             }
743         }
744     }
745
746     if (hashnewname)
747         HashVolname(trans, blockindex, &tentry);
748
749     if (releasetype)
750         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
751     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
752         ABORT( VL_IO );
753     }
754
755     END (ubik_EndTrans(trans));
756
757   abort:
758     COUNT_ABO;
759     ubik_AbortTrans(trans);
760
761   end:
762     osi_auditU (rxcall, VLReplaceVLEntryEvent, errorcode, AUD_LONG, volid, AUD_END);
763     return errorcode;
764 }
765
766
767 /* Update a vldb entry (accessed thru its volume id). Almost all of the entry's fields can be modified in a single call by setting the appropriate bits in the Mask field in VldbUpdateentry. */
768 /* this routine may never have been tested; use replace entry instead unless you're brave */
769 VL_UpdateEntry (rxcall, volid, voltype, updateentry, releasetype)
770   struct rx_call            *rxcall;
771   afs_int32                         volid;
772   afs_int32                         voltype;
773   afs_int32                         releasetype;
774   struct VldbUpdateEntry    *updateentry;       /* Update entry copied here */
775 {   struct ubik_trans   *trans;
776     afs_int32           blockindex, errorcode;
777     struct nvlentry     tentry;
778
779     COUNT_REQ (VLUPDATEENTRY);
780     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))
781         END( VL_PERM );
782     if ((voltype != -1 ) && (InvalidVoltype(voltype)))
783         END( VL_BADVOLTYPE );
784     if (releasetype && InvalidReleasetype(releasetype))
785         END( VL_BADRELLOCKTYPE );
786     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op)) 
787       goto end;
788
789     VLog(1, ("Update Volume %d %s\n", volid, rxinfo(rxcall)));
790     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
791     if (blockindex == 0) {                      /* entry not found */
792         if (!errorcode) errorcode = VL_NOENT;
793         goto abort;
794     }
795
796     /* Do the actual updating of the entry, tentry. */
797     if (errorcode = get_vldbupdateentry(trans, blockindex, updateentry, &tentry)) {
798         goto abort;
799     }
800     if (releasetype)
801         ReleaseEntry(&tentry, releasetype);                 /* Unlock entry if necessary */
802     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
803         ABORT( VL_IO );
804     }
805     END(ubik_EndTrans(trans));
806
807   abort:
808     COUNT_ABO;
809     ubik_AbortTrans(trans);
810
811   end:
812     osi_auditU (rxcall, VLUpdateEntryEvent, errorcode, AUD_LONG, volid, AUD_END);
813     return errorcode;
814 }
815
816
817 VL_UpdateEntryByName (rxcall, volname, updateentry, releasetype)
818   struct rx_call            *rxcall;
819   char                      *volname;
820   afs_int32                         releasetype;
821   struct VldbUpdateEntry    *updateentry;       /* Update entry copied here */
822 {   struct ubik_trans   *trans;
823     afs_int32           blockindex, errorcode;
824     struct nvlentry     tentry;
825
826     COUNT_REQ (VLUPDATEENTRYBYNAME);
827     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))
828         END( VL_PERM );
829     if (releasetype && InvalidReleasetype(releasetype))
830         END( VL_BADRELLOCKTYPE );
831     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op)) 
832       goto end;
833
834     blockindex = FindByName(trans, volname, &tentry, &errorcode);
835     if (blockindex == 0) {                      /* entry not found */
836         if (!errorcode) errorcode = VL_NOENT;
837         goto abort;
838     }
839
840     /* Do the actual updating of the entry, tentry. */
841     if (errorcode = get_vldbupdateentry(trans, blockindex, updateentry, &tentry)) {
842         goto abort;
843     }
844     if (releasetype)
845         ReleaseEntry(&tentry, releasetype);                 /* Unlock entry if necessary */
846     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
847         ABORT( VL_IO );
848     }
849     END(ubik_EndTrans(trans));
850
851   abort:
852     COUNT_ABO;
853     ubik_AbortTrans(trans);
854
855   end:
856     osi_auditU (rxcall, VLUpdateEntryEvent, errorcode, AUD_LONG, -1, AUD_END);
857     return errorcode;
858 }
859
860
861 /* Set a lock to the vldb entry for volid (of type voltype if not -1). */
862 VL_SetLock (rxcall, volid, voltype, voloper)
863 struct rx_call          *rxcall;
864 afs_int32                       volid;
865 afs_int32                       voltype;
866 afs_int32                       voloper;
867 {   afs_int32           timestamp, blockindex, errorcode;
868     struct ubik_trans   *trans;
869     struct nvlentry     tentry;
870
871     COUNT_REQ(VLSETLOCK);
872     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))
873         END( VL_PERM );
874     if ((voltype != -1 ) && (InvalidVoltype(voltype)))
875         END( VL_BADVOLTYPE );
876     if (InvalidOperation(voloper))
877         END( VL_BADVOLOPER );
878     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op))
879       goto end;
880
881     VLog(1, ("SetLock Volume %d %s\n", volid, rxinfo(rxcall)));
882     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
883     if (blockindex == NULLO) {
884         if (!errorcode) errorcode = VL_NOENT;
885         goto abort;
886     }
887     if (tentry.flags & VLDELETED) {
888         ABORT( VL_ENTDELETED );
889     }
890     timestamp = FT_ApproxTime();
891
892     /* Check if entry is already locked; note that we unlock any entry
893      * locked more than MAXLOCKTIME seconds */
894     if ((tentry.LockTimestamp) 
895         && ((timestamp - tentry.LockTimestamp) < MAXLOCKTIME)) {           
896       ABORT( VL_ENTRYLOCKED );
897     }
898
899     /* Consider it an unlocked entry: set current timestamp, caller
900      * and active vol operation */
901     tentry.LockTimestamp = timestamp;
902     tentry.LockAfsId = 0;                   /* Not implemented yet */
903     if (tentry.flags & VLOP_RELEASE) {
904         ABORT( VL_RERELEASE );
905     }
906     tentry.flags &= ~VLOP_ALLOPERS;  /* Clear any possible older operation bit */
907     tentry.flags |= voloper;
908
909     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
910         ABORT( VL_IO );
911     }
912     END(ubik_EndTrans(trans));
913
914   abort:
915     COUNT_ABO;
916     ubik_AbortTrans(trans);
917
918   end:
919     osi_auditU (rxcall, VLSetLockEvent, errorcode, AUD_LONG, volid, AUD_END);
920     return errorcode;
921 }
922
923
924 /* Release an already locked vldb entry. Releasetype determines what
925  * fields (afsid and/or volume operation) will be cleared along with
926  * the lock time stamp. */
927
928 VL_ReleaseLock (rxcall, volid, voltype, releasetype)
929 struct rx_call          *rxcall;
930 afs_int32                       volid;
931 afs_int32                       voltype;
932 afs_int32                       releasetype;
933 {   afs_int32           blockindex, errorcode;
934     struct ubik_trans   *trans;
935     struct nvlentry     tentry;
936
937     COUNT_REQ(VLRELEASELOCK);
938     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))
939         END( VL_PERM );
940     if ((voltype != -1 ) && (InvalidVoltype(voltype)))
941         END( VL_BADVOLTYPE );
942     if (releasetype && InvalidReleasetype(releasetype))
943         END( VL_BADRELLOCKTYPE );
944     if (errorcode = Init_VLdbase(&trans, LOCKWRITE, this_op))
945         goto end;
946
947     VLog(1, ("ReleaseLock Volume %d %s\n", volid, rxinfo(rxcall)));
948     blockindex = FindByID(trans, volid, voltype, &tentry, &errorcode);
949     if (blockindex == NULLO) {
950         if (!errorcode) errorcode = VL_NOENT;
951         goto abort;
952     }
953     if (tentry.flags & VLDELETED) {
954         ABORT( VL_ENTDELETED );
955     }
956     if (releasetype)
957         ReleaseEntry(&tentry, releasetype); /* Unlock the appropriate fields */
958     if (vlentrywrite(trans, blockindex, &tentry, sizeof(tentry))) {
959         ABORT( VL_IO );
960     }
961     END(ubik_EndTrans(trans));
962
963   abort:
964     COUNT_ABO;
965     ubik_AbortTrans(trans);
966
967   end:
968     osi_auditU (rxcall, VLReleaseLockEvent, errorcode, AUD_LONG, volid, AUD_END);
969     return errorcode;
970 }
971   
972
973 /* ListEntry returns a single vldb entry, aentry, with offset previous_index; the remaining parameters (i.e. next_index) are used so that sequential calls to this routine will get the next (all) vldb entries. */
974 VL_ListEntry (rxcall, previous_index, count, next_index, aentry)
975 struct rx_call          *rxcall;
976 afs_int32                       previous_index;
977 afs_int32                       *count;
978 afs_int32                       *next_index;
979 struct vldbentry        *aentry;
980 {   int                 errorcode;
981     struct ubik_trans   *trans;
982     struct nvlentry     tentry;
983
984     COUNT_REQ(VLLISTENTRY);
985     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
986         return errorcode;
987     VLog(25, ("OListEntry index=%d %s\n", previous_index, rxinfo(rxcall)));
988     *next_index = NextEntry(trans, previous_index, &tentry, count);
989     if (*next_index) 
990         vlentry_to_vldbentry(&tentry, aentry);
991     return(ubik_EndTrans(trans));
992 }
993
994 /* ListEntry returns a single vldb entry, aentry, with offset previous_index; the remaining parameters (i.e. next_index) are used so that sequential calls to this routine will get the next (all) vldb entries. */
995 VL_ListEntryN (rxcall, previous_index, count, next_index, aentry)
996 struct rx_call          *rxcall;
997 afs_int32                       previous_index;
998 afs_int32                       *count;
999 afs_int32                       *next_index;
1000 struct nvldbentry       *aentry;
1001 {   int                 errorcode;
1002     struct ubik_trans   *trans;
1003     struct nvlentry     tentry;
1004
1005     COUNT_REQ(VLLISTENTRYN);
1006     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1007         return errorcode;
1008     VLog(25, ("ListEntry index=%d %s\n", previous_index, rxinfo(rxcall)));
1009     *next_index = NextEntry(trans, previous_index, &tentry, count);
1010     if (*next_index) 
1011         vlentry_to_nvldbentry(&tentry, aentry);
1012     return(ubik_EndTrans(trans));
1013 }
1014
1015
1016 /* Retrieves in vldbentries all vldb entries that match the specified attributes (by server number, partition, volume type, and flag); if volume id is specified then the associated list for that entry is returned. CAUTION: This could be a very expensive call since in most cases sequential search of all vldb entries is performed. */
1017 VL_ListAttributes(rxcall, attributes, nentries, vldbentries)
1018 struct rx_call              *rxcall;
1019 struct VldbListByAttributes *attributes;
1020 afs_int32                           *nentries;
1021 bulkentries                 *vldbentries;
1022 {   int                 errorcode, allocCount = 0;
1023     struct ubik_trans   *trans;
1024     struct nvlentry     tentry;
1025     struct vldbentry    *Vldbentry = 0, *VldbentryFirst = 0, *VldbentryLast = 0;
1026     int                 pollcount = 0;
1027
1028     COUNT_REQ (VLLISTATTRIBUTES);
1029     vldbentries->bulkentries_val = 0;
1030     vldbentries->bulkentries_len = *nentries = 0;
1031     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1032         return errorcode;
1033     allocCount = VLDBALLOCCOUNT;
1034     Vldbentry = VldbentryFirst = vldbentries->bulkentries_val = (vldbentry *)malloc(allocCount * sizeof(vldbentry));
1035     if (Vldbentry == NULL) {
1036         COUNT_ABO;
1037         ubik_AbortTrans(trans);
1038         return VL_NOMEM;
1039     }
1040     VldbentryLast = VldbentryFirst + allocCount;
1041     /* Handle the attribute by volume id totally separate of the rest (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!) */
1042     if (attributes->Mask & VLLIST_VOLUMEID) {
1043         afs_int32       blockindex, chain;
1044         struct nvlentry tempentry;
1045
1046         blockindex = FindByID(trans, attributes->volumeid, -1, &tentry, &errorcode);
1047         if (blockindex == 0) {
1048             if (!errorcode) errorcode = VL_NOENT;
1049             COUNT_ABO;
1050             ubik_AbortTrans(trans);
1051             if(vldbentries->bulkentries_val)free((char *)vldbentries->bulkentries_val);
1052             vldbentries->bulkentries_val = 0;
1053             vldbentries->bulkentries_len = 0;
1054             return errorcode;
1055         }
1056         if (errorcode = put_attributeentry(&Vldbentry, &VldbentryFirst, &VldbentryLast, vldbentries, &tentry, nentries, &allocCount)) {
1057             COUNT_ABO;
1058             ubik_AbortTrans(trans);
1059             if(vldbentries->bulkentries_val)free((char *)vldbentries->bulkentries_val);
1060             vldbentries->bulkentries_val = 0;
1061             vldbentries->bulkentries_len = 0;
1062             return VL_SIZEEXCEEDED;
1063         }
1064     } else {
1065         afs_int32       nextblockindex=0, count=0, k, match=0;
1066         while (nextblockindex = NextEntry(trans, nextblockindex, &tentry, &count)) {
1067             if (++pollcount > 50) {
1068                IOMGR_Poll();
1069                pollcount = 0;
1070             }
1071             match = 0;
1072             if (attributes->Mask & VLLIST_SERVER) {
1073                 int serverindex;
1074                 if ((serverindex = IpAddrToRelAddr(attributes->server, (struct ubik_trans *) 0)) == -1) continue;
1075                 for (k=0; k<OMAXNSERVERS;k++) {
1076                     if (tentry.serverNumber[k] == BADSERVERID) break;
1077                     if (tentry.serverNumber[k] == serverindex) {
1078                         match = 1;
1079                         break;
1080                     }
1081                 }
1082                 if (!match) continue;
1083             }
1084             if (attributes->Mask & VLLIST_PARTITION) {
1085                 if (match) {
1086                     if (tentry.serverPartition[k] != attributes->partition) continue;
1087                 } else {
1088                     for (k=0; k<OMAXNSERVERS;k++) {
1089                         if (tentry.serverNumber[k] == BADSERVERID) break;
1090                         if (tentry.serverPartition[k] == attributes->partition) {
1091                             match = 1;
1092                             break;
1093                         }
1094                     }
1095                     if (!match) continue;       
1096                 }
1097             }
1098
1099             if (attributes->Mask & VLLIST_FLAG) {
1100                 if (!(tentry.flags & attributes->flag)) continue;
1101             }
1102             if (errorcode = put_attributeentry(&Vldbentry, &VldbentryFirst, &VldbentryLast, vldbentries, &tentry, nentries, &allocCount)) {
1103                 COUNT_ABO;
1104                 ubik_AbortTrans(trans);
1105                 if(vldbentries->bulkentries_val)free((char *)vldbentries->bulkentries_val);
1106                 vldbentries->bulkentries_val = 0;
1107                 vldbentries->bulkentries_len = 0;
1108                 return errorcode;
1109             }
1110         }
1111     }
1112     if (vldbentries->bulkentries_len && (allocCount > vldbentries->bulkentries_len)) {
1113
1114         vldbentries->bulkentries_val = (vldbentry *) realloc(vldbentries->bulkentries_val, vldbentries->bulkentries_len * sizeof(vldbentry)); 
1115         if (vldbentries->bulkentries_val == NULL) {
1116             COUNT_ABO;
1117             ubik_AbortTrans(trans);
1118             return VL_NOMEM;
1119         }
1120     }
1121     VLog(5, ("ListAttrs nentries=%d %s\n", vldbentries->bulkentries_len, rxinfo(rxcall)));
1122     return(ubik_EndTrans(trans));
1123 }
1124
1125 VL_ListAttributesN(rxcall, attributes, nentries, vldbentries)
1126 struct rx_call              *rxcall;
1127 struct VldbListByAttributes *attributes;
1128 afs_int32                           *nentries;
1129 nbulkentries                *vldbentries;
1130 {   int                 errorcode, allocCount = 0;
1131     struct ubik_trans   *trans;
1132     struct nvlentry     tentry;
1133     struct nvldbentry   *Vldbentry = 0, *VldbentryFirst = 0, *VldbentryLast = 0;
1134     int                 pollcount = 0;
1135
1136     COUNT_REQ (VLLISTATTRIBUTESN);
1137     vldbentries->nbulkentries_val = 0;
1138     vldbentries->nbulkentries_len = *nentries = 0;
1139     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1140         return errorcode;
1141     allocCount = VLDBALLOCCOUNT;
1142     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val = (nvldbentry *)malloc(allocCount * sizeof(nvldbentry));
1143     if (Vldbentry == NULL) {
1144         COUNT_ABO;
1145         ubik_AbortTrans(trans);
1146         return VL_NOMEM;
1147     }
1148     VldbentryLast = VldbentryFirst + allocCount;
1149     /* Handle the attribute by volume id totally separate of the rest (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!) */
1150     if (attributes->Mask & VLLIST_VOLUMEID) {
1151         afs_int32       blockindex, chain;
1152         struct nvlentry tempentry;
1153
1154         blockindex = FindByID(trans, attributes->volumeid, -1, &tentry, &errorcode);
1155         if (blockindex == 0) {
1156             if (!errorcode) errorcode = VL_NOENT;
1157             COUNT_ABO;
1158             ubik_AbortTrans(trans);
1159             if(vldbentries->nbulkentries_val)free((char *)vldbentries->nbulkentries_val);
1160             vldbentries->nbulkentries_val = 0;
1161             vldbentries->nbulkentries_len = 0;
1162             return errorcode;
1163         }
1164         if (errorcode = put_nattributeentry(&Vldbentry, &VldbentryFirst, &VldbentryLast, vldbentries,
1165                                             &tentry, 0, 0, nentries, &allocCount)) {
1166             COUNT_ABO;
1167             ubik_AbortTrans(trans);
1168             if(vldbentries->nbulkentries_val)free((char *)vldbentries->nbulkentries_val);
1169             vldbentries->nbulkentries_val = 0;
1170             vldbentries->nbulkentries_len = 0;
1171             return VL_SIZEEXCEEDED;
1172         }
1173     } else {
1174         afs_int32       nextblockindex=0, count=0, k, match=0;
1175         while (nextblockindex = NextEntry(trans, nextblockindex, &tentry, &count)) {
1176             if (++pollcount > 50) {
1177                IOMGR_Poll();
1178                pollcount = 0;
1179             }
1180
1181             match = 0;
1182             if (attributes->Mask & VLLIST_SERVER) {
1183                 int serverindex;
1184                 if ((serverindex = IpAddrToRelAddr(attributes->server, (struct ubik_trans *) 0)) == -1) continue;
1185                 for (k=0; k<NMAXNSERVERS;k++) {
1186                     if (tentry.serverNumber[k] == BADSERVERID) break;
1187                     if (tentry.serverNumber[k] == serverindex) {
1188                         match = 1;
1189                         break;
1190                     }
1191                 }
1192                 if (!match) continue;
1193             }
1194             if (attributes->Mask & VLLIST_PARTITION) {
1195                 if (match) {
1196                     if (tentry.serverPartition[k] != attributes->partition) continue;
1197                 } else {
1198                     for (k=0; k<NMAXNSERVERS;k++) {
1199                         if (tentry.serverNumber[k] == BADSERVERID) break;
1200                         if (tentry.serverPartition[k] == attributes->partition) {
1201                             match = 1;
1202                             break;
1203                         }
1204                     }
1205                     if (!match) continue;       
1206                 }
1207             }
1208
1209             if (attributes->Mask & VLLIST_FLAG) {
1210                 if (!(tentry.flags & attributes->flag)) continue;
1211             }
1212             if (errorcode = put_nattributeentry(&Vldbentry, &VldbentryFirst, &VldbentryLast,
1213                                                 vldbentries, &tentry, 0, 0, nentries, &allocCount)) {
1214                 COUNT_ABO;
1215                 ubik_AbortTrans(trans);
1216                 if(vldbentries->nbulkentries_val)free((char *)vldbentries->nbulkentries_val);
1217                 vldbentries->nbulkentries_val = 0;
1218                 vldbentries->nbulkentries_len = 0;
1219                 return errorcode;
1220             }
1221         }
1222     }
1223     if (vldbentries->nbulkentries_len && (allocCount > vldbentries->nbulkentries_len)) {
1224
1225         vldbentries->nbulkentries_val = (nvldbentry *) realloc(vldbentries->nbulkentries_val, 
1226                                                              vldbentries->nbulkentries_len * sizeof(nvldbentry)); 
1227         if (vldbentries->nbulkentries_val == NULL) {
1228             COUNT_ABO;
1229             ubik_AbortTrans(trans);
1230             return VL_NOMEM;
1231         }
1232     }
1233     VLog(5, ("NListAttrs nentries=%d %s\n", vldbentries->nbulkentries_len, rxinfo(rxcall)));
1234     return(ubik_EndTrans(trans));
1235 }
1236
1237
1238 VL_ListAttributesN2(rxcall, attributes, name, startindex, nentries, vldbentries, nextstartindex)
1239   struct rx_call              *rxcall;
1240   struct VldbListByAttributes *attributes;
1241   char *                      name;              /* Wildcarded volume name */
1242   afs_int32                       startindex;
1243   afs_int32                           *nentries;
1244   nbulkentries                *vldbentries;
1245   afs_int32                       *nextstartindex;
1246 {
1247   int                   errorcode=0, maxCount=VLDBALLOCCOUNT;
1248   struct ubik_trans     *trans;
1249   struct nvlentry       tentry;
1250   struct nvldbentry     *Vldbentry=0, *VldbentryFirst=0, *VldbentryLast=0, tVldbentry;
1251   afs_int32                     blockindex=0, count=0, k, match, matchindex;
1252   int                   serverindex=-1; /* no server found */
1253   int                   findserver=0, findpartition=0, findflag=0, findname=0;
1254   char                  *t;
1255   int                   pollcount=0;
1256   int                   namematchRWBK,  namematchRO, thismatch, matchtype;
1257   char                  volumename[VL_MAXNAMELEN];
1258
1259   COUNT_REQ (VLLISTATTRIBUTESN2);
1260   vldbentries->nbulkentries_val = 0;
1261   vldbentries->nbulkentries_len = 0;
1262   *nentries = 0;
1263   *nextstartindex = -1;
1264
1265   errorcode = Init_VLdbase(&trans, LOCKREAD, this_op);
1266   if (errorcode) return errorcode;
1267
1268   Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val = (nvldbentry *)malloc(maxCount * sizeof(nvldbentry));
1269   if (Vldbentry == NULL) {
1270      COUNT_ABO;
1271      ubik_AbortTrans(trans);
1272      return VL_NOMEM;
1273   }
1274
1275   VldbentryLast = VldbentryFirst + maxCount;
1276
1277   /* Handle the attribute by volume id totally separate of the rest
1278    * (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!)
1279    */
1280   if (attributes->Mask & VLLIST_VOLUMEID) {
1281      blockindex = FindByID(trans, attributes->volumeid, -1, &tentry, &errorcode);
1282      if (blockindex == 0) {
1283         if (!errorcode) errorcode = VL_NOENT;
1284      } else {
1285         errorcode = put_nattributeentry(&Vldbentry, &VldbentryFirst, &VldbentryLast,
1286                                         vldbentries, &tentry, 0, 0, nentries, &maxCount);
1287         if (errorcode) goto done;
1288      }
1289   }
1290
1291   /* Search each entry in the database and return all entries
1292    * that match the request. It checks volumename (with
1293    * wildcarding), entry flags, server, and partition.
1294    */
1295   else {
1296      /* Get the server index for matching server address */
1297      if (attributes->Mask & VLLIST_SERVER) {
1298         serverindex = IpAddrToRelAddr(attributes->server, (struct ubik_trans *)0);
1299         if (serverindex == -1) goto done;
1300         findserver = 1; 
1301     }
1302      findpartition = ((attributes->Mask & VLLIST_PARTITION) ? 1 : 0);
1303      findflag      = ((attributes->Mask & VLLIST_FLAG)      ? 1 : 0);
1304      if (name && (strcmp(name,".*") != 0) && (strcmp(name,"") != 0)) {
1305        sprintf(volumename, "^%s$", name);
1306        t = (char *)re_comp(volumename);
1307        if (t) {
1308           errorcode = VL_BADNAME;
1309           goto done;
1310        }
1311        findname = 1;
1312      }
1313
1314      /* Read each entry and see if it is the one we want */
1315      blockindex = startindex;
1316      while (blockindex = NextEntry(trans, blockindex, &tentry, &count)) {
1317         if (++pollcount > 50) {
1318            IOMGR_Poll();
1319            pollcount = 0;
1320         }
1321
1322         /* Step through each server index searching for a match.
1323          * Match to an existing RW, BK, or RO volume name (preference
1324          * is in this order). Remember which index we matched against.
1325          */
1326         namematchRWBK = namematchRO = 0;   /* 0->notTried; 1->match; 2->noMatch */
1327         match = 0;
1328         for (k=0; (k<NMAXNSERVERS && (tentry.serverNumber[k]!=BADSERVERID)); k++) {
1329            thismatch = 0;                         /* does this index match */
1330
1331            /* Match against the RW or BK volume name. Remember
1332             * results in namematchRWBK. Prefer RW over BK.
1333             */
1334            if (tentry.serverFlags[k] & VLSF_RWVOL) {
1335               /* Does the name match the RW name */
1336               if (tentry.flags & VLF_RWEXISTS) {
1337                  if (findname) {
1338                     sprintf(volumename, "%s", tentry.name);
1339                     if (re_exec(volumename)) {
1340                        thismatch = VLSF_RWVOL;
1341                     }
1342                  } else {
1343                     thismatch = VLSF_RWVOL;
1344                  }
1345               }
1346
1347               /* Does the name match the BK name */
1348               if (!thismatch && (tentry.flags & VLF_BACKEXISTS)) {
1349                  if (findname) {
1350                     sprintf(volumename, "%s.backup", tentry.name);
1351                     if (re_exec(volumename)) {
1352                        thismatch = VLSF_BACKVOL;
1353                     }
1354                  } else {
1355                     thismatch = VLSF_BACKVOL;
1356                  }
1357               } 
1358
1359               namematchRWBK = (thismatch?1:2);
1360            }
1361
1362            /* Match with the RO volume name. Compare once and 
1363             * remember results in namematchRO. Note that this will
1364             * pick up entries marked NEWREPSITEs and DONTUSE.
1365             */
1366            else {
1367               if (tentry.flags & VLF_ROEXISTS) {
1368                  if (findname) {
1369                     if (namematchRO) {
1370                        thismatch = ((namematchRO == 1)?VLSF_ROVOL:0);
1371                     } else {
1372                        sprintf(volumename, "%s.readonly", tentry.name);
1373                        if (re_exec(volumename))
1374                           thismatch = VLSF_ROVOL;
1375                     }
1376                  } else {
1377                     thismatch = VLSF_ROVOL;
1378                  }
1379               }
1380               namematchRO = (thismatch?1:2);
1381            }
1382
1383            /* Is there a server match */
1384            if (thismatch && findserver &&
1385                (tentry.serverNumber[k] != serverindex))
1386               thismatch = 0;
1387
1388            /* Is there a partition match */
1389            if (thismatch && findpartition && 
1390                (tentry.serverPartition[k] != attributes->partition))
1391               thismatch = 0;
1392               
1393            /* Is there a flag match */
1394            if (thismatch && findflag && !(tentry.flags & attributes->flag))
1395               thismatch = 0;
1396
1397            /* We found a match. Remember the index, and type */
1398            if (thismatch) {
1399               match      = 1;
1400               matchindex = k;
1401               matchtype  = thismatch;
1402            }
1403
1404            /* Since we prefer RW and BK volume matches over RO matches,
1405             * if we have already checked the RWBK name, then we already
1406             * found the best match and so end the search.
1407             *
1408             * If we tried matching against the RW, BK, and RO volume names
1409             * and both failed, then we end the search (none will match).
1410             */
1411            if ( (match && namematchRWBK) ||
1412                 ((namematchRWBK==2) && (namematchRO==2)) )
1413               break;
1414         }
1415
1416         /* Passed all the tests. Take it */
1417         if (match) {
1418            errorcode = put_nattributeentry(&Vldbentry,
1419                                            &VldbentryFirst, &VldbentryLast, 
1420                                            vldbentries, &tentry, matchtype, matchindex, 
1421                                            nentries, &maxCount);
1422            if (errorcode) goto done;
1423
1424            if (*nentries >= maxCount) break;    /* collected the max */
1425         }
1426      }
1427      *nextstartindex = (blockindex ? blockindex : -1);
1428   }
1429   
1430  done:
1431   if (errorcode) {
1432      COUNT_ABO;
1433      ubik_AbortTrans(trans);
1434      if (vldbentries->nbulkentries_val)
1435         free((char *)vldbentries->nbulkentries_val);
1436      vldbentries->nbulkentries_val = 0;
1437      vldbentries->nbulkentries_len = 0;
1438      *nextstartindex = -1;
1439      return errorcode;
1440   } else {
1441      VLog(5, ("N2ListAttrs nentries=%d %s\n", vldbentries->nbulkentries_len, rxinfo(rxcall)));
1442      return(ubik_EndTrans(trans));
1443   }
1444 }
1445
1446
1447 /* Retrieves in vldbentries all vldb entries that match the specified
1448  * attributes (by server number, partition, volume type, and flag); if
1449  * volume id is specified then the associated list for that entry is
1450  * returned. CAUTION: This could be a very expensive call since in most
1451  * cases sequential search of all vldb entries is performed.
1452  */
1453 VL_LinkedList(rxcall, attributes, nentries, vldbentries)
1454     struct rx_call              *rxcall;
1455     struct VldbListByAttributes *attributes;
1456     afs_int32                   *nentries;
1457     vldb_list                   *vldbentries;
1458 {   
1459     int                 errorcode;
1460     struct ubik_trans   *trans;
1461     struct nvlentry     tentry;
1462     vldblist            vllist, *vllistptr;
1463     afs_int32               blockindex, count,  k, match;
1464     int                 serverindex;
1465     int                 pollcount = 0;
1466
1467     COUNT_REQ (VLLINKEDLIST);
1468     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1469         return errorcode;
1470
1471     *nentries = 0;
1472     vldbentries->node = NULL;
1473     vllistptr = &vldbentries->node;
1474
1475     /* List by volumeid */
1476     if (attributes->Mask & VLLIST_VOLUMEID) {
1477         blockindex = FindByID(trans, attributes->volumeid, -1, &tentry, &errorcode);
1478         if (!blockindex) {
1479             COUNT_ABO;
1480             ubik_AbortTrans(trans);
1481             return (errorcode ? errorcode : VL_NOENT);
1482         }
1483
1484         vllist = (single_vldbentry *) malloc(sizeof(single_vldbentry));
1485         if (vllist == NULL) {
1486            COUNT_ABO;
1487            ubik_AbortTrans(trans);
1488            return VL_NOMEM;
1489         }
1490         vlentry_to_vldbentry(&tentry, &vllist->VldbEntry);
1491         vllist->next_vldb = NULL;
1492
1493         *vllistptr = vllist;                 /* Thread onto list */
1494         vllistptr = &vllist->next_vldb;
1495         (*nentries)++;
1496     }
1497
1498     /* Search by server, partition, and flags */
1499     else {
1500         for (blockindex = NextEntry(trans, 0, &tentry, &count);
1501              blockindex;
1502              blockindex = NextEntry(trans, blockindex, &tentry, &count)) {
1503             match = 0;
1504            
1505             if (++pollcount > 50) {
1506                IOMGR_Poll();
1507                pollcount = 0;
1508             }
1509
1510             /* Does this volume exist on the desired server */
1511             if (attributes->Mask & VLLIST_SERVER) {
1512                 serverindex = IpAddrToRelAddr(attributes->server, (struct ubik_trans *)0);
1513                 if (serverindex == -1) continue;
1514                 for (k=0; k < OMAXNSERVERS; k++) {
1515                     if (tentry.serverNumber[k] == BADSERVERID) break;
1516                     if (tentry.serverNumber[k] == serverindex) {
1517                        match = 1;
1518                        break;
1519                     }
1520                 }
1521                 if (!match) continue;
1522             }
1523
1524             /* Does this volume exist on the desired partition */
1525             if (attributes->Mask & VLLIST_PARTITION) {
1526                 if (match) {
1527                     if (tentry.serverPartition[k] != attributes->partition)
1528                        match = 0;
1529                 } else {
1530                     for (k=0; k < OMAXNSERVERS; k++) {
1531                         if (tentry.serverNumber[k] == BADSERVERID) break;
1532                         if (tentry.serverPartition[k] == attributes->partition) {
1533                             match = 1;
1534                             break;
1535                         }
1536                     }
1537                 }
1538                 if (!match) continue;   
1539             }
1540
1541             /* Does this volume have the desired flags */
1542             if (attributes->Mask & VLLIST_FLAG) {
1543                 if (!(tentry.flags & attributes->flag))
1544                    continue;
1545             }
1546
1547             vllist = (single_vldbentry *) malloc(sizeof(single_vldbentry));
1548             if (vllist == NULL) {
1549                 COUNT_ABO;
1550                 ubik_AbortTrans(trans);
1551                 return VL_NOMEM;
1552             }
1553             vlentry_to_vldbentry(&tentry, &vllist->VldbEntry);
1554             vllist->next_vldb = NULL;
1555
1556             *vllistptr = vllist;                 /* Thread onto list */
1557             vllistptr = &vllist->next_vldb;
1558             (*nentries)++;
1559             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
1560                 COUNT_ABO;
1561                 ubik_AbortTrans(trans);
1562                 return VL_SIZEEXCEEDED;
1563             }
1564         }
1565     }
1566     *vllistptr = NULL;
1567     return(ubik_EndTrans(trans));
1568 }
1569
1570 VL_LinkedListN(rxcall, attributes, nentries, vldbentries)
1571     struct rx_call                  *rxcall;
1572     struct VldbListByAttributes *attributes;
1573     afs_int32                       *nentries;
1574     nvldb_list              *vldbentries;
1575 {
1576     int                 errorcode;
1577     struct ubik_trans   *trans;
1578     struct nvlentry     tentry;
1579     nvldblist           vllist, *vllistptr;
1580     afs_int32               blockindex, count,  k, match;
1581     int                 serverindex;
1582     int                 pollcount = 0;
1583
1584     COUNT_REQ (VLLINKEDLISTN);
1585     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1586         return errorcode;
1587
1588     *nentries = 0;
1589     vldbentries->node = NULL;
1590     vllistptr = &vldbentries->node;
1591
1592     /* List by volumeid */
1593     if (attributes->Mask & VLLIST_VOLUMEID) {
1594         blockindex = FindByID(trans, attributes->volumeid, -1, &tentry, &errorcode);
1595         if (!blockindex) {
1596             COUNT_ABO;
1597             ubik_AbortTrans(trans);
1598             return (errorcode ? errorcode : VL_NOENT);
1599         }
1600
1601         vllist = (single_nvldbentry *) malloc(sizeof(single_nvldbentry));
1602         if (vllist == NULL) {
1603            COUNT_ABO;
1604            ubik_AbortTrans(trans);
1605            return VL_NOMEM;
1606         }
1607         vlentry_to_nvldbentry(&tentry, &vllist->VldbEntry);
1608         vllist->next_vldb = NULL;
1609
1610         *vllistptr = vllist;                 /* Thread onto list */
1611         vllistptr = &vllist->next_vldb;
1612         (*nentries)++;
1613     }
1614
1615     /* Search by server, partition, and flags */
1616     else {
1617         for (blockindex = NextEntry(trans, 0, &tentry, &count);
1618              blockindex;
1619              blockindex = NextEntry(trans, blockindex, &tentry, &count)) {
1620             match = 0;
1621            
1622             if (++pollcount > 50) {
1623                IOMGR_Poll();
1624                pollcount = 0;
1625             }
1626
1627             /* Does this volume exist on the desired server */
1628             if (attributes->Mask & VLLIST_SERVER) {
1629                 serverindex = IpAddrToRelAddr(attributes->server, (struct ubik_trans *)0);
1630                 if (serverindex == -1) continue;
1631                 for (k=0; k < NMAXNSERVERS; k++) {
1632                     if (tentry.serverNumber[k] == BADSERVERID) break;
1633                     if (tentry.serverNumber[k] == serverindex) {
1634                        match = 1;
1635                        break;
1636                     }
1637                 }
1638                 if (!match) continue;
1639             }
1640
1641             /* Does this volume exist on the desired partition */
1642             if (attributes->Mask & VLLIST_PARTITION) {
1643                 if (match) {
1644                     if (tentry.serverPartition[k] != attributes->partition)
1645                        match = 0;
1646                 } else {
1647                     for (k=0; k < NMAXNSERVERS; k++) {
1648                         if (tentry.serverNumber[k] == BADSERVERID) break;
1649                         if (tentry.serverPartition[k] == attributes->partition) {
1650                             match = 1;
1651                             break;
1652                         }
1653                     }
1654                 }
1655                 if (!match) continue;   
1656             }
1657
1658             /* Does this volume have the desired flags */
1659             if (attributes->Mask & VLLIST_FLAG) {
1660                 if (!(tentry.flags & attributes->flag))
1661                    continue;
1662             }
1663
1664             vllist = (single_nvldbentry *) malloc(sizeof(single_nvldbentry));
1665             if (vllist == NULL) {
1666                 COUNT_ABO;
1667                 ubik_AbortTrans(trans);
1668                 return VL_NOMEM;
1669             }
1670             vlentry_to_nvldbentry(&tentry, &vllist->VldbEntry);
1671             vllist->next_vldb = NULL;
1672
1673             *vllistptr = vllist;                 /* Thread onto list */
1674             vllistptr = &vllist->next_vldb;
1675             (*nentries)++;
1676             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
1677                 COUNT_ABO;
1678                 ubik_AbortTrans(trans);
1679                 return VL_SIZEEXCEEDED;
1680             }
1681         }
1682     }
1683     *vllistptr = NULL;
1684     return(ubik_EndTrans(trans));
1685 }
1686
1687 /* Get back vldb header statistics (allocs, frees, maxvolumeid, totalentries, etc) and dynamic statistics (number of requests and/or aborts per remote procedure call, etc) */
1688 VL_GetStats(rxcall, stats, vital_header)
1689 struct rx_call  *rxcall;
1690 vldstats        *stats;
1691 vital_vlheader  *vital_header;
1692 {   register afs_int32  errorcode;
1693     struct ubik_trans   *trans;
1694
1695     COUNT_REQ(VLGETSTATS);
1696 #ifdef  notdef
1697     /* Allow users to get statistics freely */
1698     if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))   /* Must be in 'UserList' to use */
1699         return VL_PERM;
1700 #endif
1701     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1702         return errorcode;
1703     VLog(5, ("GetStats %\n", rxinfo(rxcall)));
1704     bcopy((char *)&cheader.vital_header, (char *) vital_header, sizeof(vital_vlheader));
1705     bcopy((char *)&dynamic_statistics, (char *) stats, sizeof(vldstats));
1706     return(ubik_EndTrans(trans));
1707 }
1708
1709 /* Get the list of file server addresses from the VLDB.  Currently it's pretty
1710  * easy to do.  In the future, it might require a little bit of grunging
1711  * through the VLDB, but that's life.
1712  */
1713 VL_GetAddrs(rxcall, Handle, spare2, spare3, nentries, addrsp)
1714 struct rx_call  *rxcall;
1715 afs_int32 Handle, spare2;
1716 struct VLCallBack  *spare3;
1717 afs_int32                           *nentries;
1718 bulkaddrs                   *addrsp;
1719 {   register afs_int32       errorcode;
1720     struct ubik_trans   *trans;
1721     int nservers, i;
1722     afs_uint32 *taddrp;
1723
1724     COUNT_REQ(VLGETADDRS);
1725     addrsp->bulkaddrs_len = *nentries = 0;
1726     addrsp->bulkaddrs_val = 0;
1727     bzero (spare3, sizeof (struct VLCallBack));
1728
1729     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
1730         return errorcode;
1731
1732     VLog(5, ("GetAddrs\n"));
1733     addrsp->bulkaddrs_val = taddrp = (afs_uint32 *)malloc(sizeof(afs_int32)*(MAXSERVERID+1));
1734     nservers= *nentries = addrsp->bulkaddrs_len = 0;
1735
1736     if (!taddrp) {
1737         COUNT_ABO;
1738         ubik_AbortTrans(trans);
1739         return VL_NOMEM;
1740     }
1741
1742     for (i=0; i <= MAXSERVERID; i++) {
1743        if (*taddrp = ntohl(cheader.IpMappedAddr[i])) {
1744          taddrp++;
1745          nservers++;
1746        }
1747      }
1748
1749     addrsp->bulkaddrs_len = *nentries = nservers;
1750     return(ubik_EndTrans(trans));
1751 }
1752
1753 #define PADDR(addr) printf("%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8) &0xff, addr&0xff);
1754
1755 VL_RegisterAddrs(rxcall, uuidp, spare1, addrsp)
1756   struct rx_call        *rxcall;
1757   afsUUID *uuidp;
1758   afs_int32 spare1;
1759   bulkaddrs  *addrsp;
1760 {
1761   afs_int32 code;
1762   struct ubik_trans *trans;
1763   int cnt, h, i, j, k, m, hostslot, base, index;
1764   struct extentaddr *exp = 0, *tex;
1765   afsUUID tuuid;
1766   afs_uint32 addrs[VL_MAXIPADDRS_PERMH];
1767   afs_int32 fbase, findex;
1768   int count, willChangeEntry, foundUuidEntry, willReplaceCnt;
1769   int WillReplaceEntry, WillChange[MAXSERVERID+1], FoundUuid, ReplaceEntry;
1770   int srvidx, mhidx;
1771
1772   COUNT_REQ(VLREGADDR);
1773   if (!afsconf_SuperUser(vldb_confdir, rxcall, (char *)0))
1774      return (VL_PERM);
1775   if (code = Init_VLdbase(&trans, LOCKWRITE, this_op))
1776      return code;
1777
1778   /* Eliminate duplicates from IP address list */
1779   for (k=0, cnt=0; k<addrsp->bulkaddrs_len; k++) {
1780      if (addrsp->bulkaddrs_val[k] == 0)
1781         continue;
1782      for (m=0; m < cnt; m++) {
1783         if (addrs[m] == addrsp->bulkaddrs_val[k])
1784            break;
1785      }
1786      if (m == cnt) {
1787         if (m == VL_MAXIPADDRS_PERMH) {
1788            VLog(0, ("Number of addresses exceeds %d. Cannot register IP addr 0x%x in VLDB\n", 
1789                     VL_MAXIPADDRS_PERMH, addrsp->bulkaddrs_val[k]));
1790         } else {
1791            addrs[m] = addrsp->bulkaddrs_val[k];
1792            cnt++;
1793         }
1794      }
1795   }
1796   if (cnt <= 0) {
1797      ubik_AbortTrans(trans);
1798      return VL_INDEXERANGE;
1799   }
1800
1801   count = 0;
1802   willReplaceCnt = 0;
1803   foundUuidEntry = 0;
1804   /* For each server registered within the VLDB */
1805   for (srvidx=0; srvidx <= MAXSERVERID;srvidx++) {
1806      willChangeEntry  = 0;
1807      WillReplaceEntry = 1;
1808      if ((HostAddress[srvidx] & 0xff000000) == 0xff000000) {
1809         /* The server is registered as a multihomed */
1810         base = (HostAddress[srvidx] >> 16) & 0xff;
1811         index = HostAddress[srvidx] & 0x0000ffff;
1812         if (base >= VL_MAX_ADDREXTBLKS) {
1813            VLog(0, ("Internal error: Multihome extent base is too large. Base %d index %d\n",
1814                     base, index));
1815            continue;
1816         }
1817         if (index >= VL_MHSRV_PERBLK) {
1818            VLog(0, ("Internal error: Multihome extent index is too large. Base %d index %d\n",
1819                     base, index));
1820            continue;
1821         }
1822         if (!ex_addr[base]) {
1823            VLog(0, ("Internal error: Multihome extent does not exist. Base %d\n", base));
1824            continue;
1825         }
1826
1827         /* See if the addresses to register will change this server entry */
1828         exp = &ex_addr[base][index];
1829         tuuid = exp->ex_hostuuid;
1830         afs_ntohuuid(&tuuid);
1831         if (afs_uuid_equal(uuidp, &tuuid)) {
1832            foundUuidEntry = 1;
1833            FoundUuid = srvidx;
1834         } else {
1835            for (mhidx=0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
1836               if (!exp->ex_addrs[mhidx]) continue;
1837               for (k=0; k<cnt; k++) {
1838                  if (ntohl(exp->ex_addrs[mhidx]) == addrs[k]) {
1839                     willChangeEntry = 1;
1840                     WillChange[count] = srvidx;
1841                     break;
1842                  } 
1843               }
1844               if (k >= cnt) WillReplaceEntry = 0;
1845            }
1846         }
1847      } else {
1848         /* The server is not registered as a multihomed.
1849          * See if the addresses to register will replace this server entry.
1850          */
1851         for (k=0; k<cnt; k++) {
1852            if (HostAddress[srvidx] == addrs[k]) {
1853               willChangeEntry = 1;
1854               WillChange[count] = srvidx;
1855               WillReplaceEntry = 1;
1856               break;
1857            }
1858         }
1859      }
1860      if (willChangeEntry) {
1861         if (WillReplaceEntry) {
1862            willReplaceCnt++;
1863            ReplaceEntry = srvidx;
1864         }
1865         count++;
1866      }
1867   }
1868
1869   /* If we found the uuid in the VLDB and if we are replacing another
1870    * entire entry, then complain and fail. Also, if we did not find
1871    * the uuid in the VLDB and the IP addresses being registered was
1872    * found in more than one other entry, then we don't know which one
1873    * to replace and will complain and fail.
1874    */
1875   if ( ( foundUuidEntry && (willReplaceCnt > 0)) || 
1876        (!foundUuidEntry && (count > 1))          ) {
1877      VLog(0, ("The following fileserver is being registered in the VLDB:\n"));
1878      printf("      [");
1879      for (k=0; k<cnt; k++) {
1880         if (k>0) printf(" ");
1881         PADDR(addrs[k]);
1882      }
1883      printf("]\n");
1884
1885      if (foundUuidEntry) {
1886         printf("   It would have replaced the existing VLDB server entry:\n");
1887         printf("      entry %d: [", FoundUuid);
1888         base = (HostAddress[FoundUuid] >> 16) & 0xff;
1889         index = HostAddress[FoundUuid] & 0x0000ffff;
1890         exp = &ex_addr[base][index];
1891         for (mhidx=0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
1892            if (!exp->ex_addrs[mhidx]) continue;
1893            if (mhidx > 0) printf(" ");
1894            PADDR(ntohl(exp->ex_addrs[mhidx]));
1895         }
1896         printf("]\n");
1897      }
1898
1899      if (count == 1) printf("   Yet another VLDB server entry exists:\n");
1900      else            printf("   Yet other VLDB server entries exist:\n");
1901      for (j=0; j<count; j++) {
1902         srvidx = WillChange[j];
1903         printf("      entry %d: ", srvidx);
1904         if ((HostAddress[srvidx] & 0xff000000) == 0xff000000) {
1905            printf("[");
1906            base = (HostAddress[srvidx] >> 16) & 0xff;
1907            index = HostAddress[srvidx] & 0x0000ffff;
1908            exp = &ex_addr[base][index];
1909            for (mhidx=0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
1910               if (!exp->ex_addrs[mhidx]) continue;
1911               if (mhidx > 0) printf(" ");
1912               PADDR(ntohl(exp->ex_addrs[mhidx]));
1913            }
1914            printf("]");
1915         } else {
1916            PADDR(HostAddress[srvidx]);
1917         }
1918         printf("\n");
1919      }
1920
1921      if (count == 1) 
1922         printf("   You must 'vos changeaddr' this other server entry\n");
1923      else 
1924         printf("   You must 'vos changeaddr' these other server entries\n");
1925      if (foundUuidEntry) 
1926         printf("   and/or remove the sysid file from the registering fileserver\n");
1927      printf("   before the fileserver can be registered in the VLDB.\n");
1928
1929      ubik_AbortTrans(trans);
1930      return VL_MULTIPADDR;
1931   }
1932
1933   /* Passed the checks. Now find and update the existing mh entry, or create
1934    * a new mh entry.
1935    */
1936   if (foundUuidEntry) {
1937      /* Found the entry with same uuid. See if we need to change it */
1938      int change = 0;
1939
1940      fbase = (HostAddress[FoundUuid] >> 16) & 0xff;
1941      index = HostAddress[FoundUuid] & 0x0000ffff;
1942      exp = &ex_addr[fbase][index];
1943
1944      /* Determine if the entry has changed */
1945      for (k=0; ((k < cnt) && !change); k++) {
1946         if (ntohl(exp->ex_addrs[k]) != addrs[k])
1947            change = 1;
1948      }
1949      for (; ((k < VL_MAXIPADDRS_PERMH) && !change); k++) {
1950         if (exp->ex_addrs[k] != 0)
1951            change = 1;
1952      }
1953      if (!change) {
1954         return(ubik_EndTrans(trans));
1955      }
1956   }
1957
1958   VLog(0, ("The following fileserver is being registered in the VLDB:\n"));
1959   printf("      [");
1960   for (k=0; k<cnt; k++) {
1961      if (k>0) printf(" ");
1962      PADDR(addrs[k]);
1963   }
1964   printf("]\n");
1965
1966   if (foundUuidEntry) {
1967      printf("   It will replace the following existing entry in the VLDB (same uuid):\n");
1968      printf("      entry %d: [", FoundUuid);
1969      for (k=0; k < VL_MAXIPADDRS_PERMH; k++) {
1970         if (exp->ex_addrs[k] == 0) continue;
1971         if (k>0) printf(" ");
1972         PADDR(ntohl(exp->ex_addrs[k]));
1973      }
1974      printf("]\n");
1975   }
1976   else if (willReplaceCnt || (count == 1)) {
1977      /* If we are not replacing an entry and there is only one entry to change,
1978       * then we will replace that entry.
1979       */
1980      if (!willReplaceCnt) {
1981         ReplaceEntry = WillChange[0];
1982         willReplaceCnt++;
1983      }
1984
1985      /* Have an entry that needs to be replaced */
1986      if ((HostAddress[ReplaceEntry] & 0xff000000) == 0xff000000) {
1987         fbase = (HostAddress[ReplaceEntry] >> 16) & 0xff;
1988         index = HostAddress[ReplaceEntry] & 0x0000ffff;
1989         exp = &ex_addr[fbase][index];
1990
1991         printf("   It will replace the following existing entry in the VLDB (new uuid):\n");
1992         printf("      entry %d: [", ReplaceEntry);
1993         for (k=0; k < VL_MAXIPADDRS_PERMH; k++) {
1994            if (exp->ex_addrs[k] == 0) continue;
1995            if (k>0) printf(" ");
1996            PADDR(ntohl(exp->ex_addrs[k]));
1997         }
1998         printf("]\n");
1999      } else {
2000         /* Not a mh entry. So we have to create a new mh entry and 
2001          * put it on the ReplaceEntry slot of the HostAddress array.
2002          */
2003         printf("   It will replace existing entry %d, ", ReplaceEntry);
2004         PADDR(HostAddress[ReplaceEntry]);
2005         printf(", in the VLDB (new uuid):\n");
2006
2007         code = FindExtentBlock(trans, uuidp, 1, ReplaceEntry, &exp, &fbase);
2008         if (code || !exp) {
2009            ubik_AbortTrans(trans);
2010            return (code ? code : VL_IO);
2011         }
2012      }
2013   } else {
2014      /* There is no entry for this server, must create a new mh entry as
2015       * well as use a new slot of the HostAddress array.
2016       */
2017      printf("   It will create a new entry in the VLDB.\n");
2018      code = FindExtentBlock(trans, uuidp, 1, -1, &exp, &fbase);
2019      if (code || !exp) {
2020         ubik_AbortTrans(trans);
2021         return (code ? code : VL_IO);
2022      }
2023   }
2024
2025   /* Now we have a mh entry to fill in. Update the uuid, bump the
2026    * uniquifier, and fill in its IP addresses.
2027    */
2028   tuuid = *uuidp;
2029   afs_htonuuid(&tuuid);
2030   exp->ex_hostuuid = tuuid;
2031   exp->ex_uniquifier = htonl(ntohl(exp->ex_uniquifier)+1);
2032   for (k=0; k < cnt; k++) {
2033      exp->ex_addrs[k] = htonl(addrs[k]);
2034   }
2035   for (; k < VL_MAXIPADDRS_PERMH; k++) {
2036      exp->ex_addrs[k] = 0;
2037   }
2038
2039   /* Write the new mh entry out */
2040   if (vlwrite(trans, DOFFSET(ntohl(ex_addr[0]->ex_contaddrs[fbase]),
2041                              (char *)ex_addr[fbase], (char *)exp),
2042               (char *)exp, sizeof(*exp))) {    
2043      ubik_AbortTrans(trans);
2044      return VL_IO;
2045   }
2046
2047   /* Remove any common addresses from other mh entres. We know these entries 
2048    * are being changed and not replaced so they are mh entries.
2049    */
2050   m = 0;
2051   for (i=0; i<count; i++) {
2052      afs_int32 doff;
2053
2054      /* Skip the entry we replaced */
2055      if (willReplaceCnt && (WillChange[i] == ReplaceEntry))
2056         continue;
2057
2058      base = (HostAddress[WillChange[i]] >> 16) & 0xff;
2059      index = HostAddress[WillChange[i]] & 0x0000ffff;
2060      tex = &ex_addr[fbase][index];
2061      
2062      if (++m == 1)
2063         printf("   The following existing entries in the VLDB will be updated:\n");
2064
2065      printf("      entry %d: [", WillChange[i]);
2066      for (h=j=0; j < VL_MAXIPADDRS_PERMH; j++) {
2067         if (tex->ex_addrs[j]) {
2068            if (j>0) printf(" ");
2069            PADDR(ntohl(tex->ex_addrs[j]));
2070         }
2071
2072         for (k=0; k<cnt; k++) {
2073            if (ntohl(tex->ex_addrs[j]) == addrs[k]) break;
2074         }
2075         if (k >= cnt) { 
2076            /* Not found, so we keep it */
2077            tex->ex_addrs[h] = tex->ex_addrs[j];
2078            h++;
2079         }
2080      }
2081      for (j=h; j < VL_MAXIPADDRS_PERMH; j++) {
2082         tex->ex_addrs[j] = 0; /* zero rest of mh entry */
2083      }
2084      printf("]\n");
2085
2086      /* Write out the modified mh entry */
2087      tex->ex_uniquifier = htonl(ntohl(tex->ex_uniquifier)+1);
2088      doff=DOFFSET(ntohl(ex_addr[0]->ex_contaddrs[base]),
2089                   (char *)ex_addr[base], (char *)tex);
2090      if (vlwrite(trans, doff, (char*)tex, sizeof(*tex))) {
2091         ubik_AbortTrans(trans);
2092         return VL_IO;
2093      }
2094   }
2095
2096   return(ubik_EndTrans(trans));
2097 }
2098
2099 VL_GetAddrsU(rxcall, attributes, uuidpo, uniquifier, nentries, addrsp)
2100 struct rx_call  *rxcall;
2101 struct ListAddrByAttributes *attributes;
2102 afsUUID *uuidpo;
2103 afs_int32  *uniquifier, *nentries;
2104 bulkaddrs                   *addrsp;
2105 {   register afs_int32   errorcode, index=-1, op, offset;
2106     struct ubik_trans   *trans;
2107     int nservers, i, j, k, base=0;
2108     struct extentaddr *exp=0;
2109     afsUUID tuuid;
2110     afs_uint32 *taddrp, taddr;
2111
2112     COUNT_REQ(VLGETADDRSU);
2113     addrsp->bulkaddrs_len = *nentries = 0;
2114     addrsp->bulkaddrs_val = 0;
2115     VLog(5, ("GetAddrsU %s\n", rxinfo(rxcall)));
2116     if (errorcode = Init_VLdbase(&trans, LOCKREAD, this_op))
2117         return errorcode;
2118
2119     if (attributes->Mask & VLADDR_IPADDR) {
2120         if (attributes->Mask & (VLADDR_INDEX|VLADDR_UUID)) {
2121             ubik_AbortTrans(trans);
2122             return VL_BADMASK;
2123         }
2124         for (base = 0; base < VL_MAX_ADDREXTBLKS; base++) {
2125             if (!ex_addr[base])
2126                 break;
2127             for (i = 1; i < VL_MHSRV_PERBLK; i++) {
2128                 exp = &ex_addr[base][i];
2129                 tuuid = exp->ex_hostuuid;
2130                 afs_ntohuuid(&tuuid);
2131                 if (afs_uuid_is_nil(&tuuid)) continue;
2132                 for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2133                     if (exp->ex_addrs[j] && (ntohl(exp->ex_addrs[j]) == attributes->ipaddr)) {
2134                         break;
2135                     }
2136                 }
2137                 if (j < VL_MAXIPADDRS_PERMH) break;
2138             }
2139             if (i < VL_MHSRV_PERBLK) break;
2140         }
2141         if (base >= VL_MAX_ADDREXTBLKS) {
2142             ubik_AbortTrans(trans);
2143             return VL_NOENT;
2144         }
2145     } else if (attributes->Mask & VLADDR_INDEX) {
2146         if (attributes->Mask & (VLADDR_IPADDR|VLADDR_UUID)) {
2147             ubik_AbortTrans(trans);
2148             return VL_BADMASK;
2149         }
2150         index = attributes->index;
2151         if (index < 1 || index >= (VL_MAX_ADDREXTBLKS*VL_MHSRV_PERBLK)) {
2152             ubik_AbortTrans(trans);
2153             return VL_INDEXERANGE;
2154         }
2155         base = index / VL_MHSRV_PERBLK;
2156         offset = index % VL_MHSRV_PERBLK;
2157         if (offset == 0) {
2158             ubik_AbortTrans(trans);
2159             return VL_NOENT;
2160         }
2161         if (!ex_addr[base]) {
2162             ubik_AbortTrans(trans);
2163             return VL_INDEXERANGE;
2164         }
2165         exp = &ex_addr[base][offset];
2166     } else if (attributes->Mask & VLADDR_UUID) {
2167         if (attributes->Mask & (VLADDR_IPADDR|VLADDR_INDEX)) {
2168             ubik_AbortTrans(trans);
2169             return VL_BADMASK;
2170         }
2171         if (!ex_addr[0]) {      /* mh servers probably aren't setup on this vldb */
2172             ubik_AbortTrans(trans);
2173             return VL_NOENT;
2174         }
2175         if (errorcode = FindExtentBlock(trans, &attributes->uuid, 0, -1, &exp, &base)) {
2176             ubik_AbortTrans(trans);
2177             return errorcode;
2178         }
2179     } else {
2180         ubik_AbortTrans(trans);
2181         return VL_BADMASK;
2182     }
2183
2184     if (exp == (struct extentaddr *)0) {  
2185         ubik_AbortTrans(trans);
2186         return VL_NOENT;
2187     }
2188     addrsp->bulkaddrs_val = taddrp = (afs_uint32 *)malloc(sizeof(afs_int32)*(MAXSERVERID+1));
2189     nservers= *nentries = addrsp->bulkaddrs_len = 0;
2190     if (!taddrp) {
2191         COUNT_ABO;
2192         ubik_AbortTrans(trans);
2193         return VL_NOMEM;
2194     }
2195     tuuid = exp->ex_hostuuid;
2196     afs_ntohuuid(&tuuid);
2197     if (afs_uuid_is_nil(&tuuid)) {
2198         ubik_AbortTrans(trans);
2199         return VL_NOENT;
2200     }
2201     if (uuidpo)  *uuidpo = tuuid;
2202     if (uniquifier) *uniquifier = ntohl(exp->ex_uniquifier);
2203     for (i = 0; i < VL_MAXIPADDRS_PERMH; i++) {
2204        if (exp->ex_addrs[i]) {
2205           taddr = ntohl(exp->ex_addrs[i]);
2206           /* Weed out duplicates */
2207           for (j = 0; j < nservers; j++) {
2208              if (taddrp[j] == taddr)
2209                break;
2210           }
2211           if ((j == nservers) && (j <= MAXSERVERID)) {
2212              taddrp[nservers] = taddr;
2213              nservers++;
2214           }
2215        }
2216     }
2217     addrsp->bulkaddrs_len = *nentries = nservers;
2218     return(ubik_EndTrans(trans));
2219 }
2220
2221 /* ============> End of Exported vldb RPC functions <============= */
2222
2223
2224 /* Routine that copies the given vldb entry to the output buffer, vldbentries. */
2225 static int
2226 put_attributeentry(Vldbentry, VldbentryFirst, VldbentryLast, vldbentries, entry, nentries, alloccnt)
2227 struct vldbentry        **Vldbentry, **VldbentryFirst, **VldbentryLast;
2228 bulkentries             *vldbentries;
2229 struct nvlentry         *entry;
2230 afs_int32                       *nentries, *alloccnt;
2231 {
2232     vldbentry *reall;
2233     afs_int32 allo;
2234
2235     if (*Vldbentry == *VldbentryLast) {
2236         if (smallMem) return VL_SIZEEXCEEDED;   /* no growing if smallMem defined */
2237
2238         /* Allocate another set of memory; each time allocate twice as
2239          * many blocks as the last time. When we reach VLDBALLOCLIMIT, 
2240          * then grow in increments of VLDBALLOCINCR.
2241          */
2242         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2243         reall = (vldbentry *) realloc(*VldbentryFirst, (*alloccnt+allo)*sizeof(vldbentry));
2244         if (reall == NULL) return VL_NOMEM;
2245
2246         *VldbentryFirst = vldbentries->bulkentries_val = reall;
2247         *Vldbentry = *VldbentryFirst + *alloccnt;
2248         *VldbentryLast = *Vldbentry + allo;
2249         *alloccnt += allo;
2250     }
2251     vlentry_to_vldbentry(entry, *Vldbentry);
2252     (*Vldbentry)++;
2253     (*nentries)++;
2254     vldbentries->bulkentries_len++;
2255     return 0;
2256 }
2257
2258 static int
2259 put_nattributeentry(Vldbentry, VldbentryFirst, VldbentryLast, vldbentries, entry,
2260                     matchtype, matchindex, nentries, alloccnt)
2261 struct nvldbentry       **Vldbentry, **VldbentryFirst, **VldbentryLast;
2262 nbulkentries            *vldbentries;
2263 struct nvlentry         *entry;
2264 afs_int32                       matchtype, matchindex, *nentries, *alloccnt;
2265 {
2266     nvldbentry *reall;
2267     afs_int32 allo;
2268
2269     if (*Vldbentry == *VldbentryLast) {
2270         if (smallMem) return VL_SIZEEXCEEDED;   /* no growing if smallMem defined */
2271
2272         /* Allocate another set of memory; each time allocate twice as
2273          * many blocks as the last time. When we reach VLDBALLOCLIMIT, 
2274          * then grow in increments of VLDBALLOCINCR.
2275          */
2276         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2277         reall = (nvldbentry *)realloc(*VldbentryFirst, (*alloccnt+allo)*sizeof(nvldbentry));
2278         if (reall == NULL) return VL_NOMEM;
2279
2280         *VldbentryFirst = vldbentries->nbulkentries_val = reall;
2281         *Vldbentry = *VldbentryFirst + *alloccnt;
2282         *VldbentryLast = *Vldbentry + allo;
2283         *alloccnt += allo;
2284     }
2285     vlentry_to_nvldbentry(entry, *Vldbentry);
2286     (*Vldbentry)->matchindex  = (matchtype << 16) + matchindex;
2287     (*Vldbentry)++;
2288     (*nentries)++;
2289     vldbentries->nbulkentries_len++;
2290     return 0;
2291 }
2292
2293
2294 /* Common code to actually remove a vldb entry from the database. */
2295 static int
2296 RemoveEntry(trans, entryptr, tentry)
2297 struct ubik_trans   *trans;
2298 afs_int32                   entryptr;
2299 struct nvlentry     *tentry;
2300 {
2301     register int    errorcode;
2302
2303     if (errorcode = UnthreadVLentry(trans, entryptr, tentry))
2304         return errorcode;
2305     if (errorcode = FreeBlock(trans, entryptr))
2306         return errorcode;
2307     return 0;
2308 }
2309
2310 static
2311 ReleaseEntry(tentry, releasetype)
2312 struct nvlentry *tentry;
2313 afs_int32               releasetype;
2314 {
2315     if (releasetype & LOCKREL_TIMESTAMP)
2316         tentry->LockTimestamp = 0;
2317     if (releasetype & LOCKREL_OPCODE)
2318         tentry->flags &= ~VLOP_ALLOPERS;
2319     if (releasetype & LOCKREL_AFSID)
2320         tentry->LockAfsId = 0;
2321 }
2322
2323
2324 /* Verify that the incoming vldb entry is valid; multi type of error codes are returned. */
2325 static int 
2326 check_vldbentry(aentry)
2327 struct vldbentry        *aentry;
2328 {
2329     afs_int32   i;
2330
2331     if (InvalidVolname(aentry->name))
2332         return VL_BADNAME;
2333     if (aentry->nServers <= 0 || aentry->nServers > OMAXNSERVERS)
2334         return VL_BADSERVER;
2335     for (i=0; i < aentry->nServers; i++) {
2336 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
2337             return VL_BADSERVER;        */
2338         if (aentry->serverPartition[i] < 0 || aentry->serverPartition[i] > MAXPARTITIONID)
2339             return VL_BADPARTITION;
2340         if (aentry->serverFlags[i] < 0 || aentry->serverFlags[i] > MAXSERVERFLAG)
2341             return VL_BADSERVERFLAG;
2342     }
2343     return 0;
2344 }
2345
2346 static int 
2347 check_nvldbentry(aentry)
2348 struct nvldbentry       *aentry;
2349 {
2350     afs_int32   i;
2351
2352     if (InvalidVolname(aentry->name))
2353         return VL_BADNAME;
2354     if (aentry->nServers <= 0 || aentry->nServers > NMAXNSERVERS)
2355         return VL_BADSERVER;
2356     for (i=0; i < aentry->nServers; i++) {
2357 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
2358             return VL_BADSERVER;        */
2359         if (aentry->serverPartition[i] < 0 || aentry->serverPartition[i] > MAXPARTITIONID)
2360             return VL_BADPARTITION;
2361         if (aentry->serverFlags[i] < 0 || aentry->serverFlags[i] > MAXSERVERFLAG)
2362             return VL_BADSERVERFLAG;
2363     }
2364     return 0;
2365 }
2366
2367
2368 /* Convert from the external vldb entry representation to its internal
2369    (more compact) form.  This call should not change the hash chains! */
2370 static int
2371 vldbentry_to_vlentry(atrans, VldbEntry, VlEntry)
2372 struct ubik_trans *atrans;
2373 struct vldbentry    *VldbEntry;
2374 struct nvlentry     *VlEntry;
2375 {
2376     int i, serverindex;
2377
2378     if (strcmp(VlEntry->name, VldbEntry->name)) 
2379         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
2380     for (i=0; i<VldbEntry->nServers; i++) {
2381         serverindex = IpAddrToRelAddr(VldbEntry->serverNumber[i], atrans);
2382         if (serverindex == -1)  return VL_BADSERVER;
2383         VlEntry->serverNumber[i] = serverindex;
2384         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
2385         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
2386     }
2387     for (;i < OMAXNSERVERS; i++)
2388         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] = VlEntry->serverFlags[i] = BADSERVERID;
2389     for (i=0; i < MAXTYPES; i++)
2390         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
2391     VlEntry->cloneId = VldbEntry->cloneId;
2392     VlEntry->flags = VldbEntry->flags;
2393     return 0;
2394 }
2395
2396 static int
2397 nvldbentry_to_vlentry(atrans, VldbEntry, VlEntry)
2398 struct ubik_trans *atrans;
2399 struct nvldbentry    *VldbEntry;
2400 struct nvlentry     *VlEntry;
2401 {
2402     int i, serverindex;
2403
2404     if (strcmp(VlEntry->name, VldbEntry->name)) 
2405         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
2406     for (i=0; i<VldbEntry->nServers; i++) {
2407         serverindex = IpAddrToRelAddr(VldbEntry->serverNumber[i], atrans);
2408         if (serverindex == -1)  return VL_BADSERVER;
2409         VlEntry->serverNumber[i] = serverindex;
2410         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
2411         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
2412     }
2413     for (;i < NMAXNSERVERS; i++)
2414         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] = VlEntry->serverFlags[i] = BADSERVERID;
2415     for (i=0; i < MAXTYPES; i++)
2416         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
2417     VlEntry->cloneId = VldbEntry->cloneId;
2418     VlEntry->flags = VldbEntry->flags;
2419     return 0;
2420 }
2421
2422
2423 /* Update the vldb entry with the new fields as indicated by the value of the Mask entry in the updateentry structure. All necessary validation checks are performed. */
2424 static
2425 get_vldbupdateentry(trans, blockindex, updateentry, VlEntry)
2426 struct ubik_trans       *trans;
2427 afs_int32                       blockindex;
2428 struct VldbUpdateEntry  *updateentry;
2429 struct nvlentry         *VlEntry;
2430 {
2431     int i, j, errorcode, serverindex;
2432
2433     if (updateentry->Mask & VLUPDATE_VOLUMENAME) {
2434         if (InvalidVolname(updateentry->name))
2435             return VL_BADNAME;
2436         if (errorcode = UnhashVolname(trans, blockindex, VlEntry))
2437             return errorcode;
2438         strncpy(VlEntry->name, updateentry->name, sizeof(VlEntry->name));
2439         HashVolname(trans, blockindex, VlEntry);
2440     }
2441
2442     if (updateentry->Mask & VLUPDATE_VOLNAMEHASH) {
2443         if (errorcode = UnhashVolname(trans, blockindex, VlEntry)) {
2444             if (errorcode != VL_NOENT)
2445                 return errorcode;
2446         }
2447         HashVolname(trans, blockindex, VlEntry);
2448     }
2449
2450     if (updateentry->Mask & VLUPDATE_FLAGS) {
2451         VlEntry->flags = updateentry->flags;
2452     }
2453     if (updateentry->Mask & VLUPDATE_CLONEID) {
2454         VlEntry->cloneId = updateentry->cloneId;
2455     }
2456     if (updateentry->Mask & VLUPDATE_RWID) {
2457         if (errorcode = UnhashVolid(trans, RWVOL, blockindex, VlEntry)) {
2458             if (errorcode != VL_NOENT)
2459                 return errorcode;
2460         }
2461         VlEntry->volumeId[RWVOL] = updateentry->spares3;        /* rw id */
2462         if (errorcode = HashVolid(trans, RWVOL, blockindex, VlEntry))
2463             return errorcode;
2464     }
2465     if (updateentry->Mask & VLUPDATE_READONLYID) {
2466         if (errorcode = UnhashVolid(trans, ROVOL, blockindex, VlEntry)) {
2467             if (errorcode != VL_NOENT)
2468                 return errorcode;
2469         }
2470         VlEntry->volumeId[ROVOL] = updateentry->ReadOnlyId;
2471         if (errorcode = HashVolid(trans, ROVOL, blockindex, VlEntry))
2472             return errorcode;
2473     }
2474     if (updateentry->Mask & VLUPDATE_BACKUPID) {
2475         if (errorcode = UnhashVolid(trans, BACKVOL, blockindex, VlEntry)) {
2476             if (errorcode != VL_NOENT)
2477                 return errorcode;
2478         }
2479         VlEntry->volumeId[BACKVOL] = updateentry->BackupId;
2480         if (errorcode = HashVolid(trans, BACKVOL, blockindex, VlEntry))
2481             return errorcode;
2482     }
2483     if (updateentry->Mask & VLUPDATE_REPSITES) {
2484         if (updateentry->nModifiedRepsites <= 0 || updateentry->nModifiedRepsites > OMAXNSERVERS)
2485             return VL_BADSERVER;        
2486         for (i=0; i < updateentry->nModifiedRepsites; i++) {
2487 /*          if (updateentry->RepsitesTargetServer[i] < 0 || updateentry->RepsitesTargetServer[i] > MAXSERVERID)
2488                 return VL_BADSERVER;    */
2489             if (updateentry->RepsitesTargetPart[i] < 0 || updateentry->RepsitesTargetPart[i] > MAXPARTITIONID)
2490                 return VL_BADPARTITION;
2491             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_DELETE) {
2492                 if ((j = repsite_exists(VlEntry, IpAddrToRelAddr(updateentry->RepsitesTargetServer[i], trans), updateentry->RepsitesTargetPart[i])) != -1)
2493                     repsite_compress(VlEntry, j);
2494                 else return VL_NOREPSERVER;
2495             }
2496             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_ADD) {
2497 /*              if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
2498                     return VL_BADSERVER;                */
2499                 if (updateentry->RepsitesNewPart[i] < 0 || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
2500                     return VL_BADPARTITION;
2501                 if (repsite_exists(VlEntry, IpAddrToRelAddr(updateentry->RepsitesNewServer[i], trans), updateentry->RepsitesNewPart[i]) != -1)
2502                     return VL_DUPREPSERVER;
2503                 for (j=0; VlEntry->serverNumber[j] != BADSERVERID && j < OMAXNSERVERS; j++);
2504                 if (j >= OMAXNSERVERS) return VL_REPSFULL;
2505                 if ((serverindex = IpAddrToRelAddr(updateentry->RepsitesNewServer[i], trans)) == -1)
2506                     return VL_BADSERVER;
2507                 VlEntry->serverNumber[j] = serverindex;
2508                 VlEntry->serverPartition[j] = updateentry->RepsitesNewPart[i];
2509                 if (updateentry->RepsitesNewFlags[i] < 0 || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
2510                     return VL_BADSERVERFLAG;
2511                 VlEntry->serverFlags[j] = updateentry->RepsitesNewFlags[i];             
2512             }
2513             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODSERV) {
2514 /*n             if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
2515                     return VL_BADSERVER;            */
2516                 if ((j = repsite_exists(VlEntry, IpAddrToRelAddr(updateentry->RepsitesTargetServer[i], trans), updateentry->RepsitesTargetPart[i])) != -1) {
2517                     VlEntry->serverNumber[j] = IpAddrToRelAddr(updateentry->RepsitesNewServer[i], trans);
2518                 }
2519                 else return VL_NOREPSERVER;
2520             }
2521             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODPART) {
2522                 if (updateentry->RepsitesNewPart[i] < 0 || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
2523                     return VL_BADPARTITION;
2524                 if ((j = repsite_exists(VlEntry, IpAddrToRelAddr(updateentry->RepsitesTargetServer[i], trans), updateentry->RepsitesTargetPart[i])) != -1)
2525                     VlEntry->serverPartition[j] = updateentry->RepsitesNewPart[i];
2526                 else return VL_NOREPSERVER;
2527             }
2528             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODFLAG) {
2529                 if ((j = repsite_exists(VlEntry, IpAddrToRelAddr(updateentry->RepsitesTargetServer[i], trans), updateentry->RepsitesTargetPart[i])) != -1) {
2530                     if (updateentry->RepsitesNewFlags[i] < 0 || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
2531                         return VL_BADSERVERFLAG;
2532                     VlEntry->serverFlags[j] = updateentry->RepsitesNewFlags[i];
2533                 } else return VL_NOREPSERVER;
2534             }
2535         }
2536     }
2537     return 0;
2538 }
2539         
2540
2541 /* Check if the specified [server,partition] entry is found in the vldb entry's repsite table; it's offset in the table is returned, if it's present there. */
2542 static int
2543 repsite_exists(VlEntry, server, partition)
2544 struct nvlentry *VlEntry;
2545 int             server, partition;
2546 {
2547     int i;
2548
2549     for (i=0; VlEntry->serverNumber[i] != BADSERVERID && i < OMAXNSERVERS; i++) {
2550         if ((VlEntry->serverNumber[i] == server) && (VlEntry->serverPartition[i] == partition))
2551             return i;
2552     }
2553     return -1;
2554 }
2555
2556
2557
2558 /* Repsite table compression: used when deleting a repsite entry so that all active repsite entries are on the top of the table. */
2559 static
2560 repsite_compress(VlEntry, offset)
2561 struct nvlentry *VlEntry;
2562 int             offset;
2563 {
2564     int repsite_offset = offset;
2565      for (; VlEntry->serverNumber[repsite_offset] != BADSERVERID && repsite_offset < OMAXNSERVERS-1; repsite_offset++) {
2566         VlEntry->serverNumber[repsite_offset] = VlEntry->serverNumber[repsite_offset+1];
2567         VlEntry->serverPartition[repsite_offset] = VlEntry->serverPartition[repsite_offset+1];
2568         VlEntry->serverFlags[repsite_offset] = VlEntry->serverFlags[repsite_offset+1];
2569     }
2570     VlEntry->serverNumber[repsite_offset] = BADSERVERID;
2571 }
2572
2573
2574 /* Convert from the internal (compacted) vldb entry to the external representation used by the interface. */
2575 static
2576 vlentry_to_vldbentry(VlEntry, VldbEntry)
2577 struct nvlentry     *VlEntry;
2578 struct vldbentry    *VldbEntry;
2579 {
2580     int i, j;
2581     
2582     bzero(VldbEntry, sizeof(struct vldbentry));
2583     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
2584     for (i=0; i < OMAXNSERVERS; i++) {
2585         if (VlEntry->serverNumber[i] == BADSERVERID) break;
2586         if ((HostAddress[j = VlEntry->serverNumber[i]] & 0xff000000) == 0xff000000) {
2587             struct extentaddr *exp;
2588             int base, index;
2589
2590             base = (HostAddress[j] >> 16) & 0xff;
2591             index = HostAddress[j] & 0x0000ffff;
2592             exp = &ex_addr[base][index];
2593             /* For now return the first ip address back */
2594             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2595                 if (exp->ex_addrs[j]) {
2596                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
2597                     break;
2598                 }
2599             }
2600         } else
2601             VldbEntry->serverNumber[i] = HostAddress[VlEntry->serverNumber[i]];
2602         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
2603         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
2604     }
2605     VldbEntry->nServers = i;
2606     for (i=0; i<MAXTYPES; i++)
2607         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
2608     VldbEntry->cloneId = VlEntry->cloneId;
2609     VldbEntry->flags = VlEntry->flags;
2610 }
2611
2612
2613 /* Convert from the internal (compacted) vldb entry to the external representation used by the interface. */
2614 static vlentry_to_nvldbentry(VlEntry, VldbEntry)
2615 struct nvlentry     *VlEntry;
2616 struct nvldbentry    *VldbEntry;
2617 {
2618     int i, j;
2619     
2620     bzero(VldbEntry, sizeof(struct vldbentry));
2621     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
2622     for (i=0; i < NMAXNSERVERS; i++) {
2623         if (VlEntry->serverNumber[i] == BADSERVERID) break;
2624         if ((HostAddress[j = VlEntry->serverNumber[i]] & 0xff000000) == 0xff000000) {
2625             struct extentaddr *exp;
2626             int base, index;
2627
2628             base = (HostAddress[j] >> 16) & 0xff;
2629             index = HostAddress[j] & 0x0000ffff;
2630             exp = &ex_addr[base][index];
2631             /* For now return the first ip address back */
2632             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2633                 if (exp->ex_addrs[j]) {
2634                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
2635                     break;
2636                 }
2637             }
2638         } else
2639             VldbEntry->serverNumber[i] = HostAddress[VlEntry->serverNumber[i]];
2640         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
2641         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
2642     }
2643     VldbEntry->nServers = i;
2644     for (i=0; i<MAXTYPES; i++)
2645         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
2646     VldbEntry->cloneId = VlEntry->cloneId;
2647     VldbEntry->flags = VlEntry->flags;
2648 }
2649
2650 static vlentry_to_uvldbentry(VlEntry, VldbEntry)
2651 struct nvlentry     *VlEntry;
2652 struct uvldbentry    *VldbEntry;
2653 {
2654     int i, j;
2655     
2656     bzero(VldbEntry, sizeof(struct vldbentry));
2657     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
2658     for (i=0; i < NMAXNSERVERS; i++) {
2659         if (VlEntry->serverNumber[i] == BADSERVERID) break;
2660         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
2661         VldbEntry->serverUnique[i] = 0;
2662         if ((HostAddress[j = VlEntry->serverNumber[i]] & 0xff000000) == 0xff000000) {
2663             struct extentaddr *exp;
2664             int base, index;
2665             afsUUID tuuid;
2666
2667             base = (HostAddress[j] >> 16) & 0xff;
2668             index = HostAddress[j] & 0x0000ffff;
2669             exp = &ex_addr[base][index];
2670             tuuid = exp->ex_hostuuid;
2671             afs_ntohuuid(&tuuid);
2672             VldbEntry->serverFlags[i] |= VLSERVER_FLAG_UUID;
2673             VldbEntry->serverNumber[i] = tuuid;
2674             VldbEntry->serverUnique[i] = ntohl(exp->ex_uniquifier);
2675         } else {
2676             VldbEntry->serverNumber[i].time_low = HostAddress[VlEntry->serverNumber[i]];
2677         }
2678         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
2679
2680     }
2681     VldbEntry->nServers = i;
2682     for (i=0; i<MAXTYPES; i++)
2683         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
2684     VldbEntry->cloneId = VlEntry->cloneId;
2685     VldbEntry->flags = VlEntry->flags;
2686 }
2687
2688 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
2689
2690
2691 /* Verify that the volname is a valid volume name. */
2692 static int
2693 InvalidVolname(volname)
2694 char *volname;
2695 {
2696     char *map;
2697     int slen;
2698
2699     map = LEGALCHARS;
2700     slen = strlen(volname);
2701     if (slen >= VL_MAXNAMELEN) return 1;
2702     return(slen != strspn(volname, map));
2703 }
2704
2705
2706 /* Verify that the given volume type is valid. */
2707 static int
2708 InvalidVoltype(voltype)
2709 afs_int32 voltype;
2710 {
2711     if (voltype != RWVOL && voltype != ROVOL && voltype != BACKVOL)
2712         return 1;
2713     return 0;
2714 }
2715
2716
2717 static int
2718 InvalidOperation(voloper)
2719 afs_int32 voloper;
2720 {
2721     if (voloper != VLOP_MOVE && voloper != VLOP_RELEASE && voloper != VLOP_BACKUP && voloper != VLOP_DELETE && voloper != VLOP_DUMP)
2722         return 1;
2723     return 0;
2724 }
2725
2726 static int
2727 InvalidReleasetype(releasetype)
2728 afs_int32       releasetype;
2729 {
2730     if ((releasetype & LOCKREL_TIMESTAMP) || (releasetype & LOCKREL_OPCODE) || (releasetype & LOCKREL_AFSID))
2731         return 0;
2732     return 1;
2733 }
2734
2735 static int
2736 IpAddrToRelAddr(ipaddr, atrans)
2737 struct ubik_trans *atrans;
2738 register afs_uint32     ipaddr;
2739 {
2740     register int i, j;
2741     register afs_int32 code, base, index;
2742     struct extentaddr *exp;
2743
2744     for (i = 0; i <= MAXSERVERID; i++) {
2745         if (HostAddress[i] == ipaddr)
2746             return i;
2747         if ((HostAddress[i] & 0xff000000) == 0xff000000) {
2748             base = (HostAddress[i] >> 16) & 0xff;
2749             index = HostAddress[i] & 0x0000ffff;
2750             if (base >= VL_MAX_ADDREXTBLKS) {
2751                 VLog(0, ("Internal error: Multihome extent base is too large. Base %d index %d\n", base, index));
2752                 return -1;      /* EINVAL */
2753             }
2754             if (index >= VL_MHSRV_PERBLK) {
2755                 VLog(0, ("Internal error: Multihome extent index is too large. Base %d index %d\n", base, index));
2756                 return -1;      /* EINVAL */
2757             }
2758             if (!ex_addr[base]) {
2759                 VLog(0, ("Internal error: Multihome extent does not exist. Base %d\n", base));
2760                 return -1;      /* EINVAL */
2761             }
2762             exp = &ex_addr[base][index];
2763             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2764                 if (exp->ex_addrs[j] && (ntohl(exp->ex_addrs[j]) == ipaddr)) {
2765                     return i;
2766                 }
2767             }
2768         }
2769     }
2770
2771     /* allocate the new server a server id pronto */
2772     if (atrans) {
2773         for(i=0;i <= MAXSERVERID;i++) {
2774             if (cheader.IpMappedAddr[i] == 0) {
2775                 cheader.IpMappedAddr[i] = htonl(ipaddr);
2776                 code = vlwrite(atrans, DOFFSET(0, &cheader, &cheader.IpMappedAddr[i]), (char *) &cheader.IpMappedAddr[i], sizeof(afs_int32));
2777                 HostAddress[i] = ipaddr;
2778                 if (code) return -1;
2779                 return i;
2780             }
2781         }
2782     }
2783     return -1;
2784 }
2785
2786 static int
2787 ChangeIPAddr(ipaddr1, ipaddr2, atrans)
2788 struct ubik_trans *atrans;
2789 register afs_uint32     ipaddr1, ipaddr2;
2790 {
2791     int i, j;
2792     afs_int32 code;
2793     struct extentaddr *exp;
2794     int base, index, mhidx;
2795     afsUUID tuuid;
2796     afs_int32 blockindex, count;
2797     int   pollcount;
2798     struct nvlentry tentry;
2799
2800     if (!atrans)
2801     return VL_CREATEFAIL;
2802
2803     /* Don't let addr change to 256.*.*.* : Causes internal error below */
2804     if ((ipaddr2 & 0xff000000) == 0xff000000)
2805        return(VL_BADSERVER);
2806
2807      /* If we are removing an address, ip1 will be -1 and ip2 will be
2808       * the original address. This prevents an older revision vlserver
2809       * from removing the IP address (won't find server 0xfffffff in
2810       * the VLDB). An older revision vlserver does not have the check
2811       * to see if any volumes exist on the server being removed.
2812       */
2813      if (ipaddr1 == 0xffffffff) {
2814         ipaddr1 = ipaddr2;
2815         ipaddr2 = 0;
2816      }
2817
2818     for (i = 0; i <= MAXSERVERID; i++) {
2819        if ((HostAddress[i] & 0xff000000) == 0xff000000) {
2820           base = (HostAddress[i] >> 16) & 0xff;
2821           index = HostAddress[i] & 0x0000ffff;
2822           if ((base >= VL_MAX_ADDREXTBLKS) || (index >= VL_MHSRV_PERBLK)) {
2823              VLog(0, ("Internal error: Multihome extent addr is too large. Base %d index %d\n",
2824                       base, index));
2825              return -1; /* EINVAL */
2826           }
2827
2828           exp = &ex_addr[base][index];
2829           for (mhidx=0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2830              if (!exp->ex_addrs[mhidx]) continue;
2831              if (ntohl(exp->ex_addrs[mhidx]) == ipaddr1)
2832                 break;
2833           }
2834           if (mhidx < VL_MAXIPADDRS_PERMH) {
2835              break;
2836           }
2837        } 
2838        else if (HostAddress[i] == ipaddr1) {
2839           exp = (struct extentaddr *)0;
2840           break;
2841        }
2842     }
2843
2844     if (i >= MAXSERVERID) {
2845        return VL_NOENT;  /* not found */
2846     }
2847
2848     /* If we are removing a server entry, a volume cannot
2849      * exist on the server. If one does, don't remove the
2850      * server entry: return error "volume entry exists".
2851      */
2852     if (ipaddr2 == 0) {
2853        for (blockindex = NextEntry(atrans, 0, &tentry, &count);
2854             blockindex;
2855             blockindex = NextEntry(atrans, blockindex, &tentry, &count)) {
2856           if (++pollcount > 50) {
2857              IOMGR_Poll();
2858              pollcount = 0;
2859           }
2860           for (j=0; j<NMAXNSERVERS; j++) {
2861              if (tentry.serverNumber[j] == BADSERVERID) break;
2862              if (tentry.serverNumber[j] == i) {
2863                 return VL_IDEXIST;
2864              }
2865           }
2866        }
2867     }
2868
2869     /* Log a message saying we are changing/removing an IP address */
2870     VLog(0, ("The following IP address is being %s:\n",
2871              (ipaddr2?"changed":"removed")));
2872     printf("      entry %d: ", i);
2873     if (exp) {
2874        printf("[");
2875        for (mhidx=0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2876           if (!exp->ex_addrs[mhidx]) continue;
2877           if (mhidx>0) printf(" ");
2878           PADDR(ntohl(exp->ex_addrs[mhidx]));
2879        }
2880        printf("]");
2881     } else {
2882        PADDR(ipaddr1);
2883     }
2884     if (ipaddr2) {
2885        printf(" -> ");
2886        PADDR(ipaddr2);
2887     }
2888     printf("\n");
2889
2890     /* Change the registered uuuid addresses */
2891     if (exp) {
2892        bzero(&tuuid, sizeof(afsUUID));
2893        afs_htonuuid(&tuuid);
2894        exp->ex_hostuuid = tuuid;
2895        code = vlwrite(atrans, DOFFSET(ntohl(ex_addr[0]->ex_contaddrs[base]),
2896                                       (char *)ex_addr[base], (char *)exp),
2897                       (char *)&tuuid, sizeof(tuuid));
2898        if (code)
2899           return VL_IO;
2900     }
2901
2902     /* Now change the host address entry */
2903     cheader.IpMappedAddr[i] = htonl(ipaddr2);
2904     code = vlwrite(atrans, DOFFSET(0, &cheader, &cheader.IpMappedAddr[i]), (char *)
2905                    &cheader.IpMappedAddr[i], sizeof(afs_int32));
2906     HostAddress[i] = ipaddr2;
2907     if (code)
2908        return VL_IO;
2909
2910     return 0;
2911 }
2912
2913 /* see if the vlserver is back yet */
2914 VL_ProbeServer(rxcall)
2915 struct rx_call *rxcall; {
2916     return 0;
2917 }