LWP fileserver is no more
[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 <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <lock.h>
16 #include <afs/afsutil.h>
17 #include <ubik.h>
18 #include <rx/xdr.h>
19 #include <rx/rx.h>
20 #include <rx/rxkad.h>
21 #include <afs/keys.h>
22 #include <afs/cellconfig.h>
23
24 #include "vlserver.h"
25 #include "vlserver_internal.h"
26 #include "afs/audit.h"
27
28 #ifdef HAVE_POSIX_REGEX         /* use POSIX regexp library */
29 #include <regex.h>
30 #endif
31
32 extern int smallMem;
33 extern int restrictedQueryLevel;
34 extern int extent_mod;
35 extern struct afsconf_dir *vldb_confdir;
36 extern struct ubik_dbase *VL_dbase;
37 int maxnservers;
38 #define ABORT(c) do { \
39     code = (c); \
40     goto abort; \
41 } while (0)
42
43 #define VLDBALLOCLIMIT  10000
44 #define VLDBALLOCINCR   2048
45
46 static int put_attributeentry(struct vl_ctx *ctx,
47                               struct vldbentry **, struct vldbentry **,
48                               struct vldbentry **, bulkentries *,
49                               struct nvlentry *, afs_int32 *, afs_int32 *);
50 static int put_nattributeentry(struct vl_ctx *ctx,
51                                struct nvldbentry **, struct nvldbentry **,
52                                struct nvldbentry **, nbulkentries *,
53                                struct nvlentry *, afs_int32, afs_int32,
54                                afs_int32 *, afs_int32 *);
55 static int RemoveEntry(struct vl_ctx *ctx, afs_int32 entryptr,
56                        struct nvlentry *tentry);
57 static void ReleaseEntry(struct nvlentry *tentry, afs_int32 releasetype);
58 static int check_vldbentry(struct vldbentry *aentry);
59 static int check_nvldbentry(struct nvldbentry *aentry);
60 static int vldbentry_to_vlentry(struct vl_ctx *ctx,
61                                 struct vldbentry *VldbEntry,
62                                 struct nvlentry *VlEntry);
63 static int nvldbentry_to_vlentry(struct vl_ctx *ctx,
64                                  struct nvldbentry *VldbEntry,
65                                  struct nvlentry *VlEntry);
66 static int get_vldbupdateentry(struct vl_ctx *ctx, afs_int32 blockindex,
67                                struct VldbUpdateEntry *updateentry,
68                                struct nvlentry *VlEntry);
69 static int repsite_exists(struct nvlentry *VlEntry, int server, int partition);
70 static void repsite_compress(struct nvlentry *VlEntry, int offset);
71 static int vlentry_to_vldbentry(struct vl_ctx *ctx,
72                                 struct nvlentry *VlEntry,
73                                 struct vldbentry *VldbEntry);
74 static int vlentry_to_nvldbentry(struct vl_ctx *ctx,
75                                  struct nvlentry *VlEntry,
76                                  struct nvldbentry *VldbEntry);
77 static int vlentry_to_uvldbentry(struct vl_ctx *ctx,
78                                  struct nvlentry *VlEntry,
79                                  struct uvldbentry *VldbEntry);
80 static int InvalidVolname(char *volname);
81 static int InvalidVoltype(afs_int32 voltype);
82 static int InvalidOperation(afs_int32 voloper);
83 static int InvalidReleasetype(afs_int32 releasetype);
84 static int IpAddrToRelAddr(struct vl_ctx *ctx, afs_uint32 ipaddr, int create);
85 static int ChangeIPAddr(struct vl_ctx *ctx, afs_uint32 ipaddr1,
86                         afs_uint32 ipaddr2);
87
88 static_inline void
89 countRequest(int opcode)
90 {
91     if (opcode != 0) {
92         dynamic_statistics.requests[opcode - VL_LOWEST_OPCODE]++;
93     }
94 }
95
96 static_inline void
97 countAbort(int opcode)
98 {
99     if (opcode != 0) {
100         dynamic_statistics.aborts[opcode - VL_LOWEST_OPCODE]++;
101     }
102 }
103
104
105 static_inline int
106 multiHomedExtentBase(struct vl_ctx *ctx, int srvidx, struct extentaddr **exp,
107                      int *basePtr)
108 {
109     int base;
110     int index;
111
112     *exp = NULL;
113     *basePtr = 0;
114
115     if ((ctx->hostaddress[srvidx] & 0xff000000) == 0xff000000) {
116         base = (ctx->hostaddress[srvidx] >> 16) & 0xff;
117         index = ctx->hostaddress[srvidx] & 0x0000ffff;
118         if (base >= VL_MAX_ADDREXTBLKS) {
119             VLog(0, ("Internal error: Multihome extent base is too large. "
120                      "Base %d index %d\n", base, index));
121             return VL_IO;
122         }
123         if (index >= VL_MHSRV_PERBLK) {
124             VLog(0, ("Internal error: Multihome extent index is too large. "
125                      "Base %d index %d\n", base, index));
126             return VL_IO;
127         }
128         if (!ctx->ex_addr[base]) {
129             VLog(0, ("Internal error: Multihome extent does not exist. "
130                      "Base %d\n", base));
131             return VL_IO;
132         }
133
134         *basePtr = base;
135         *exp = &ctx->ex_addr[base][index];
136     }
137
138     return 0;
139 }
140
141 static_inline int
142 multiHomedExtent(struct vl_ctx *ctx, int srvidx, struct extentaddr **exp)
143 {
144     int base;
145
146     return multiHomedExtentBase(ctx, srvidx, exp, &base);
147 }
148
149 #define AFS_RXINFO_LEN 128
150 static char *
151 rxkadInfo(char *str, struct rx_connection *conn, struct in_addr hostAddr)
152 {
153     int code;
154     char tname[64] = "";
155     char tinst[64] = "";
156     char tcell[64] = "";
157     afs_uint32 exp;
158
159     code = rxkad_GetServerInfo(conn, NULL, &exp, tname, tinst, tcell,
160                                NULL);
161     if (!code)
162         snprintf(str, AFS_RXINFO_LEN,
163                  "%s rxkad:%s%s%s%s%s", inet_ntoa(hostAddr), tname,
164                 (tinst[0] == '\0') ? "" : ".",
165                 (tinst[0] == '\0') ? "" : tinst,
166                 (tcell[0] == '\0') ? "" : "@",
167                 (tcell[0] == '\0') ? "" : tcell);
168     else
169         snprintf(str, AFS_RXINFO_LEN, "%s noauth", inet_ntoa(hostAddr));
170     return (str);
171 }
172
173 static char *
174 rxinfo(char *str, struct rx_call *rxcall)
175 {
176     struct rx_connection *conn;
177     struct in_addr hostAddr;
178     rx_securityIndex authClass;
179
180     conn = rx_ConnectionOf(rxcall);
181     authClass = rx_SecurityClassOf(conn);
182     hostAddr.s_addr = rx_HostOf(rx_PeerOf(conn));
183
184     switch(authClass) {
185     case RX_SECIDX_KAD:
186         return rxkadInfo(str, conn, hostAddr);
187     default:
188         ;
189     }
190
191     snprintf(str, AFS_RXINFO_LEN, "%s noauth", inet_ntoa(hostAddr));
192     return str;
193 }
194
195
196 /* This is called to initialize the database, set the appropriate locks and make sure that the vldb header is valid */
197 int
198 Init_VLdbase(struct vl_ctx *ctx,
199              int locktype,      /* indicate read or write transaction */
200              int opcode)
201 {
202     int code = 0, pass, wl;
203
204     for (pass = 1; pass <= 3; pass++) {
205         if (pass == 2) {        /* take write lock to rebuild the db */
206             code = ubik_BeginTrans(VL_dbase, UBIK_WRITETRANS, &ctx->trans);
207             wl = 1;
208         } else if (locktype == LOCKREAD) {
209             code =
210                 ubik_BeginTransReadAnyWrite(VL_dbase, UBIK_READTRANS, &ctx->trans);
211             wl = 0;
212         } else {
213             code = ubik_BeginTrans(VL_dbase, UBIK_WRITETRANS, &ctx->trans);
214             wl = 1;
215         }
216         if (code)
217             return code;
218
219         code = ubik_SetLock(ctx->trans, 1, 1, locktype);
220         if (code) {
221             countAbort(opcode);
222             ubik_AbortTrans(ctx->trans);
223             return code;
224         }
225
226         /* check that dbase is initialized and setup cheader */
227         /* 2nd pass we try to rebuild the header */
228         code = CheckInit(ctx->trans, ((pass == 2) ? 1 : 0));
229         if (!code && wl && extent_mod)
230             code = readExtents(ctx->trans);     /* Fix the mh extent blocks */
231         if (code) {
232             countAbort(opcode);
233             ubik_AbortTrans(ctx->trans);
234             /* Only rebuld if the database is empty */
235             /* Exit if can't rebuild */
236             if ((pass == 1) && (code != VL_EMPTY))
237                 return code;
238             if (pass == 2)
239                 return code;
240         } else {                /* No code */
241             if (pass == 2) {
242                 /* The database header was rebuilt; end the write transaction.
243                  * This will call vlsynccache() to copy the write header buffers
244                  * to the read header buffers, before calling vlsetache().
245                  * Do a third pass to re-acquire the original lock, which
246                  * may be a read lock. */
247                 ubik_EndTrans(ctx->trans);
248             } else {
249                 break;          /* didn't rebuild and successful - exit */
250             }
251         }
252     }
253     if (code == 0) {
254         code = vlsetcache(ctx, locktype);
255     }
256     return code;
257 }
258
259
260 /* Create a new vldb entry; both new volume id and name must be unique
261  * (non-existant in vldb).
262  */
263
264 static afs_int32
265 CreateEntry(struct rx_call *rxcall, struct vldbentry *newentry)
266 {
267     int this_op = VLCREATEENTRY;
268     struct vl_ctx ctx;
269     afs_int32 code, blockindex;
270     struct nvlentry tentry;
271     char rxstr[AFS_RXINFO_LEN];
272
273     countRequest(this_op);
274     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL)) {
275         return VL_PERM;
276     }
277
278     /* Do some validity tests on new entry */
279     if ((code = check_vldbentry(newentry))
280         || (code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
281         return code;
282
283     VLog(1,
284          ("OCreate Volume %d %s\n", newentry->volumeId[RWVOL],
285           rxinfo(rxstr, rxcall)));
286     if (EntryIDExists(&ctx, newentry->volumeId, MAXTYPES, &code)) {
287         /* at least one of the specified IDs already exists; we fail */
288         code = VL_IDEXIST;
289         goto abort;
290     } else if (code) {
291         goto abort;
292     }
293
294     /* Is this following check (by volume name) necessary?? */
295     /* If entry already exists, we fail */
296     if (FindByName(&ctx, newentry->name, &tentry, &code)) {
297         code = VL_NAMEEXIST;
298         goto abort;
299     } else if (code) {
300         goto abort;
301     }
302
303     blockindex = AllocBlock(&ctx, &tentry);
304     if (blockindex == 0) {
305         code = VL_CREATEFAIL;
306         goto abort;
307     }
308
309     memset(&tentry, 0, sizeof(struct nvlentry));
310     /* Convert to its internal representation; both in host byte order */
311     if ((code = vldbentry_to_vlentry(&ctx, newentry, &tentry))) {
312         FreeBlock(&ctx, blockindex);
313         goto abort;
314     }
315
316     /* Actually insert the entry in vldb */
317     code = ThreadVLentry(&ctx, blockindex, &tentry);
318     if (code) {
319         FreeBlock(&ctx, blockindex);
320         goto abort;
321     } else {
322         return ubik_EndTrans(ctx.trans);
323     }
324
325   abort:
326     countAbort(this_op);
327     ubik_AbortTrans(ctx.trans);
328     return code;
329 }
330
331 afs_int32
332 SVL_CreateEntry(struct rx_call *rxcall, struct vldbentry *newentry)
333 {
334     afs_int32 code;
335
336     code = CreateEntry(rxcall, newentry);
337     osi_auditU(rxcall, VLCreateEntryEvent, code, AUD_STR,
338                (newentry ? newentry->name : NULL), AUD_END);
339     return code;
340 }
341
342 static afs_int32
343 CreateEntryN(struct rx_call *rxcall, struct nvldbentry *newentry)
344 {
345     int this_op = VLCREATEENTRYN;
346     struct vl_ctx ctx;
347     afs_int32 code, blockindex;
348     struct nvlentry tentry;
349     char rxstr[AFS_RXINFO_LEN];
350
351     countRequest(this_op);
352     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL)) {
353         return VL_PERM;
354     }
355
356     /* Do some validity tests on new entry */
357     if ((code = check_nvldbentry(newentry))
358         || (code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
359         return code;
360
361     VLog(1,
362          ("Create Volume %d %s\n", newentry->volumeId[RWVOL],
363           rxinfo(rxstr, rxcall)));
364     if (EntryIDExists(&ctx, newentry->volumeId, MAXTYPES, &code)) {
365         /* at least one of the specified IDs already exists; we fail */
366         code = VL_IDEXIST;
367         goto abort;
368     } else if (code) {
369         goto abort;
370     }
371
372     /* Is this following check (by volume name) necessary?? */
373     /* If entry already exists, we fail */
374     if (FindByName(&ctx, newentry->name, &tentry, &code)) {
375         code = VL_NAMEEXIST;
376         goto abort;
377     } else if (code) {
378         goto abort;
379     }
380
381     blockindex = AllocBlock(&ctx, &tentry);
382     if (blockindex == 0) {
383         code = VL_CREATEFAIL;
384         goto abort;
385     }
386
387     memset(&tentry, 0, sizeof(struct nvlentry));
388     /* Convert to its internal representation; both in host byte order */
389     if ((code = nvldbentry_to_vlentry(&ctx, newentry, &tentry))) {
390         FreeBlock(&ctx, blockindex);
391         goto abort;
392     }
393
394     /* Actually insert the entry in vldb */
395     code = ThreadVLentry(&ctx, blockindex, &tentry);
396     if (code) {
397         FreeBlock(&ctx, blockindex);
398         goto abort;
399     } else {
400         return ubik_EndTrans(ctx.trans);
401     }
402
403   abort:
404     countAbort(this_op);
405     ubik_AbortTrans(ctx.trans);
406     return code;
407 }
408
409 afs_int32
410 SVL_CreateEntryN(struct rx_call *rxcall, struct nvldbentry *newentry)
411 {
412     afs_int32 code;
413
414     code = CreateEntryN(rxcall, newentry);
415     osi_auditU(rxcall, VLCreateEntryEvent, code, AUD_STR,
416                (newentry ? newentry->name : NULL), AUD_END);
417     return code;
418 }
419
420 static afs_int32
421 ChangeAddr(struct rx_call *rxcall, afs_uint32 ip1, afs_uint32 ip2)
422 {
423     int this_op = VLCHANGEADDR;
424     struct vl_ctx ctx;
425     afs_int32 code;
426     char rxstr[AFS_RXINFO_LEN];
427
428     countRequest(this_op);
429     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL)) {
430         return VL_PERM;
431     }
432
433     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
434         return code;
435
436     VLog(1, ("Change Addr %u -> %u %s\n", ip1, ip2, rxinfo(rxstr, rxcall)));
437     if ((code = ChangeIPAddr(&ctx, ip1, ip2)))
438         goto abort;
439     else {
440         code = ubik_EndTrans(ctx.trans);
441         return code;
442     }
443
444   abort:
445     countAbort(this_op);
446     ubik_AbortTrans(ctx.trans);
447     return code;
448 }
449
450 afs_int32
451 SVL_ChangeAddr(struct rx_call *rxcall, afs_uint32 ip1, afs_uint32 ip2)
452 {
453     afs_int32 code;
454
455     code = ChangeAddr(rxcall, ip1, ip2);
456     osi_auditU(rxcall, VLChangeAddrEvent, code, AUD_LONG, ip1, AUD_LONG,
457                ip2, AUD_END);
458     return code;
459 }
460
461 /* Delete a vldb entry given the volume id. */
462 static afs_int32
463 DeleteEntry(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype)
464 {
465     int this_op = VLDELETEENTRY;
466     struct vl_ctx ctx;
467     afs_int32 blockindex, code;
468     struct nvlentry tentry;
469     char rxstr[AFS_RXINFO_LEN];
470
471     countRequest(this_op);
472     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
473         return VL_PERM;
474
475     if ((voltype != -1) && (InvalidVoltype(voltype)))
476         return VL_BADVOLTYPE;
477
478     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
479         return code;
480
481     VLog(1, ("Delete Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
482     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
483     if (blockindex == 0) {      /* volid not found */
484         if (!code)
485             code = VL_NOENT;
486         goto abort;
487     }
488
489     if (tentry.flags & VLDELETED) {     /* Already deleted; return */
490         ABORT(VL_ENTDELETED);
491     }
492     if ((code = RemoveEntry(&ctx, blockindex, &tentry))) {
493         goto abort;
494     }
495     return ubik_EndTrans(ctx.trans);
496
497   abort:
498     countAbort(this_op);
499     ubik_AbortTrans(ctx.trans);
500     return code;
501 }
502
503 afs_int32
504 SVL_DeleteEntry(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype)
505 {
506     afs_int32 code;
507
508     code = DeleteEntry(rxcall, volid, voltype);
509     osi_auditU(rxcall, VLDeleteEntryEvent, code, AUD_LONG, volid,
510                AUD_END);
511     return code;
512 }
513
514
515 /* Get a vldb entry given its volume id; make sure it's not a deleted entry. */
516 static int
517 GetEntryByID(struct rx_call *rxcall,
518              afs_uint32 volid,
519              afs_int32 voltype,
520              char *aentry,      /* entry data copied here */
521              afs_int32 new,
522              afs_int32 this_op)
523 {
524     struct vl_ctx ctx;
525     afs_int32 blockindex, code;
526     struct nvlentry tentry;
527     char rxstr[AFS_RXINFO_LEN];
528
529     countRequest(this_op);
530
531     if ((voltype != -1) && (InvalidVoltype(voltype)))
532         return VL_BADVOLTYPE;
533     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
534         return code;
535
536     VLog(5, ("GetVolumeByID %u (%d) %s\n", volid, new,
537              rxinfo(rxstr, rxcall)));
538     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
539     if (blockindex == 0) {      /* entry not found */
540         if (!code)
541             code = VL_NOENT;
542         goto abort;
543     }
544     if (tentry.flags & VLDELETED) {     /* Entry is deleted! */
545         code = VL_ENTDELETED;
546         goto abort;
547     }
548     /* Convert from the internal to external form */
549     if (new == 1)
550         code = vlentry_to_nvldbentry(&ctx, &tentry, (struct nvldbentry *)aentry);
551     else if (new == 2)
552         code = vlentry_to_uvldbentry(&ctx, &tentry, (struct uvldbentry *)aentry);
553     else
554         code = vlentry_to_vldbentry(&ctx, &tentry, (struct vldbentry *)aentry);
555
556     if (code)
557         goto abort;
558
559     return (ubik_EndTrans(ctx.trans));
560
561 abort:
562     countAbort(this_op);
563     ubik_AbortTrans(ctx.trans);
564     return code;
565 }
566
567 afs_int32
568 SVL_GetEntryByID(struct rx_call *rxcall,
569                  afs_uint32 volid,
570                  afs_int32 voltype,
571                  vldbentry *aentry)             /* entry data copied here */
572 {
573     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 0,
574                          VLGETENTRYBYID));
575 }
576
577 afs_int32
578 SVL_GetEntryByIDN(struct rx_call *rxcall,
579                   afs_uint32 volid,
580                   afs_int32 voltype,
581                   nvldbentry *aentry)   /* entry data copied here */
582 {
583     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 1,
584                          VLGETENTRYBYIDN));
585 }
586
587 afs_int32
588 SVL_GetEntryByIDU(struct rx_call *rxcall,
589                   afs_uint32 volid,
590                   afs_int32 voltype,
591                   uvldbentry *aentry)   /* entry data copied here */
592 {
593     return (GetEntryByID(rxcall, volid, voltype, (char *)aentry, 2,
594                          VLGETENTRYBYIDU));
595 }
596
597 /* returns true if the id is a decimal integer, in which case we interpret
598  * it as an id.  make the cache manager much simpler */
599 static int
600 NameIsId(char *aname)
601 {
602     int tc;
603     while ((tc = *aname++)) {
604         if (tc > '9' || tc < '0')
605             return 0;
606     }
607     return 1;
608 }
609
610 /* Get a vldb entry given the volume's name; of course, very similar to
611  * VLGetEntryByID() above. */
612 static afs_int32
613 GetEntryByName(struct rx_call *rxcall,
614                char *volname,
615                char *aentry,            /* entry data copied here */
616                int new,
617                int this_op)
618 {
619     struct vl_ctx ctx;
620     afs_int32 blockindex, code;
621     struct nvlentry tentry;
622     char rxstr[AFS_RXINFO_LEN];
623
624     if (NameIsId(volname)) {
625         return GetEntryByID(rxcall, strtoul(volname, NULL, 10), -1, aentry, new, this_op);
626     }
627
628     countRequest(this_op);
629
630     if (InvalidVolname(volname))
631         return VL_BADNAME;
632     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
633         return code;
634     VLog(5, ("GetVolumeByName %s (%d) %s\n", volname, new, rxinfo(rxstr, rxcall)));
635     blockindex = FindByName(&ctx, volname, &tentry, &code);
636     if (blockindex == 0) {      /* entry not found */
637         if (!code)
638             code = VL_NOENT;
639         goto abort;
640     }
641     if (tentry.flags & VLDELETED) {     /* Entry is deleted */
642         code = VL_ENTDELETED;
643         goto abort;
644     }
645     /* Convert to external entry representation */
646     if (new == 1)
647         code = vlentry_to_nvldbentry(&ctx, &tentry, (struct nvldbentry *)aentry);
648     else if (new == 2)
649         code = vlentry_to_uvldbentry(&ctx, &tentry, (struct uvldbentry *)aentry);
650     else
651         code = vlentry_to_vldbentry(&ctx, &tentry, (struct vldbentry *)aentry);
652
653     if (code)
654         goto abort;
655
656     return (ubik_EndTrans(ctx.trans));
657
658 abort:
659     countAbort(this_op);
660     ubik_AbortTrans(ctx.trans);
661     return code;
662
663 }
664
665 afs_int32
666 SVL_GetEntryByNameO(struct rx_call *rxcall,
667                     char *volname,
668                     struct vldbentry *aentry)   /* entry data copied here */
669 {
670     return (GetEntryByName(rxcall, volname, (char *)aentry, 0,
671                            VLGETENTRYBYNAME));
672 }
673
674 afs_int32
675 SVL_GetEntryByNameN(struct rx_call *rxcall,
676                     char *volname,
677                     struct nvldbentry *aentry)  /* entry data copied here */
678 {
679     return (GetEntryByName(rxcall, volname, (char *)aentry, 1,
680                            VLGETENTRYBYNAMEN));
681 }
682
683 afs_int32
684 SVL_GetEntryByNameU(struct rx_call *rxcall,
685                     char *volname,
686                     struct uvldbentry *aentry)  /* entry data copied here */
687 {
688     return (GetEntryByName(rxcall, volname, (char *)aentry, 2,
689                            VLGETENTRYBYNAMEU));
690 }
691
692 /* Get the current value of the maximum volume id and bump the volume id counter by Maxvolidbump. */
693 static afs_int32
694 getNewVolumeId(struct rx_call *rxcall, afs_uint32 Maxvolidbump,
695                    afs_uint32 *newvolumeid)
696 {
697     int this_op = VLGETNEWVOLUMEID;
698     afs_int32 code;
699     afs_uint32 maxvolumeid;
700     struct vl_ctx ctx;
701     char rxstr[AFS_RXINFO_LEN];
702
703     countRequest(this_op);
704     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
705         return VL_PERM;
706
707     if (Maxvolidbump > MAXBUMPCOUNT)
708         return VL_BADVOLIDBUMP;
709
710     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
711         return code;
712
713     *newvolumeid = maxvolumeid = NextUnusedID(&ctx,
714         ntohl(ctx.cheader->vital_header.MaxVolumeId), Maxvolidbump, &code);
715     if (code) {
716         goto abort;
717     }
718
719     maxvolumeid += Maxvolidbump;
720     VLog(1, ("GetNewVolid newmax=%u %s\n", maxvolumeid, rxinfo(rxstr, rxcall)));
721     ctx.cheader->vital_header.MaxVolumeId = htonl(maxvolumeid);
722     if (write_vital_vlheader(&ctx)) {
723         ABORT(VL_IO);
724     }
725     return ubik_EndTrans(ctx.trans);
726
727   abort:
728     countAbort(this_op);
729     ubik_AbortTrans(ctx.trans);
730     return code;
731 }
732
733 afs_int32
734 SVL_GetNewVolumeId(struct rx_call *rxcall, afs_uint32 Maxvolidbump,
735                    afs_uint32 *newvolumeid)
736 {
737     afs_int32 code;
738
739     code = getNewVolumeId(rxcall, Maxvolidbump, newvolumeid);
740     osi_auditU(rxcall, VLGetNewVolumeIdEvent, code, AUD_END);
741     return code;
742 }
743
744
745 /* Simple replace the contents of the vldb entry, volid, with
746  * newentry. No individual checking/updating per field (alike
747  * VLUpdateEntry) is done. */
748
749 static afs_int32
750 ReplaceEntry(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
751                  struct vldbentry *newentry, afs_int32 releasetype)
752 {
753     int this_op = VLREPLACEENTRY;
754     struct vl_ctx ctx;
755     afs_int32 blockindex, code, typeindex;
756     int hashnewname;
757     int hashVol[MAXTYPES];
758     struct nvlentry tentry;
759     afs_uint32 checkids[MAXTYPES];
760     char rxstr[AFS_RXINFO_LEN];
761
762     countRequest(this_op);
763     for (typeindex = 0; typeindex < MAXTYPES; typeindex++)
764         hashVol[typeindex] = 0;
765     hashnewname = 0;
766     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
767         return VL_PERM;
768
769     if ((code = check_vldbentry(newentry)))
770         return code;
771
772     if (voltype != -1 && InvalidVoltype(voltype))
773         return VL_BADVOLTYPE;
774
775     if (releasetype && InvalidReleasetype(releasetype))
776         return VL_BADRELLOCKTYPE;
777     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
778         return code;
779
780     VLog(1, ("OReplace Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
781     /* find vlentry we're changing */
782     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
783     if (blockindex == 0) {      /* entry not found */
784         if (!code)
785             code = VL_NOENT;
786         goto abort;
787     }
788
789     /* check that we're not trying to change the RW vol ID */
790     if (newentry->volumeId[RWVOL] != tentry.volumeId[RWVOL]) {
791         ABORT(VL_BADENTRY);
792     }
793
794     /* make sure none of the IDs we are changing to are already in use */
795     memset(&checkids, 0, sizeof(checkids));
796     for (typeindex = ROVOL; typeindex < MAXTYPES; typeindex++) {
797         if (tentry.volumeId[typeindex] != newentry->volumeId[typeindex]) {
798             checkids[typeindex] = newentry->volumeId[typeindex];
799         }
800     }
801     if (EntryIDExists(&ctx, checkids, MAXTYPES, &code)) {
802         ABORT(VL_IDEXIST);
803     } else if (code) {
804         goto abort;
805     }
806
807     /* make sure the name we're changing to doesn't already exist */
808     if (strcmp(newentry->name, tentry.name)) {
809         struct nvlentry tmp_entry;
810         if (FindByName(&ctx, newentry->name, &tmp_entry, &code)) {
811             ABORT(VL_NAMEEXIST);
812         } else if (code) {
813             goto abort;
814         }
815     }
816
817     /* unhash volid entries if they're disappearing or changing.
818      * Remember if we need to hash in the new value (we don't have to
819      * rehash if volid stays same */
820     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
821         if (tentry.volumeId[typeindex] != newentry->volumeId[typeindex]) {
822             if (tentry.volumeId[typeindex])
823                 if ((code =
824                     UnhashVolid(&ctx, typeindex, blockindex, &tentry))) {
825                     goto abort;
826                 }
827             /* we must rehash new id if the id is different and the ID is nonzero */
828             hashVol[typeindex] = 1;     /* must rehash this guy if he exists */
829         }
830     }
831
832     /* Rehash volname if it changes */
833     if (strcmp(newentry->name, tentry.name)) {  /* Name changes; redo hashing */
834         if ((code = UnhashVolname(&ctx, blockindex, &tentry))) {
835             goto abort;
836         }
837         hashnewname = 1;
838     }
839
840     /* after this, tentry is new entry, not old one.  vldbentry_to_vlentry
841      * doesn't touch hash chains */
842     if ((code = vldbentry_to_vlentry(&ctx, newentry, &tentry))) {
843         goto abort;
844     }
845
846     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
847         if (hashVol[typeindex] && tentry.volumeId[typeindex]) {
848             if ((code = HashVolid(&ctx, typeindex, blockindex, &tentry))) {
849                 goto abort;
850             }
851         }
852     }
853
854     if (hashnewname)
855         HashVolname(&ctx, blockindex, &tentry);
856
857     if (releasetype)
858         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
859     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
860         ABORT(VL_IO);
861     }
862
863     return ubik_EndTrans(ctx.trans);
864
865   abort:
866     countAbort(this_op);
867     ubik_AbortTrans(ctx.trans);
868     return code;
869 }
870
871 afs_int32
872 SVL_ReplaceEntry(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
873                  struct vldbentry *newentry, afs_int32 releasetype)
874 {
875     afs_int32 code;
876
877     code = ReplaceEntry(rxcall, volid, voltype, newentry, releasetype);
878     osi_auditU(rxcall, VLReplaceVLEntryEvent, code, AUD_LONG, volid, AUD_END);
879     return code;
880 }
881
882 static afs_int32
883 ReplaceEntryN(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
884                   struct nvldbentry *newentry, afs_int32 releasetype)
885 {
886     int this_op = VLREPLACEENTRYN;
887     struct vl_ctx ctx;
888     afs_int32 blockindex, code, typeindex;
889     int hashnewname;
890     int hashVol[MAXTYPES];
891     struct nvlentry tentry;
892     char rxstr[AFS_RXINFO_LEN];
893
894     countRequest(this_op);
895     for (typeindex = 0; typeindex < MAXTYPES; typeindex++)
896         hashVol[typeindex] = 0;
897     hashnewname = 0;
898     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
899         return VL_PERM;
900
901     if ((code = check_nvldbentry(newentry)))
902         return code;
903
904     if (voltype != -1 && InvalidVoltype(voltype))
905         return VL_BADVOLTYPE;
906
907     if (releasetype && InvalidReleasetype(releasetype))
908         return VL_BADRELLOCKTYPE;
909     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
910         return code;
911
912     VLog(1, ("Replace Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
913     /* find vlentry we're changing */
914     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
915     if (blockindex == 0) {      /* entry not found */
916         if (!code)
917             code = VL_NOENT;
918         goto abort;
919     }
920
921     /* check that we're not trying to change the RW vol ID */
922     if (newentry->volumeId[RWVOL] != tentry.volumeId[RWVOL]) {
923         ABORT(VL_BADENTRY);
924     }
925
926     /* unhash volid entries if they're disappearing or changing.
927      * Remember if we need to hash in the new value (we don't have to
928      * rehash if volid stays same */
929     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
930         if (tentry.volumeId[typeindex] != newentry->volumeId[typeindex]) {
931             if (tentry.volumeId[typeindex])
932                 if ((code =
933                     UnhashVolid(&ctx, typeindex, blockindex, &tentry))) {
934                     goto abort;
935                 }
936             /* we must rehash new id if the id is different and the ID is nonzero */
937             hashVol[typeindex] = 1;     /* must rehash this guy if he exists */
938         }
939     }
940
941     /* Rehash volname if it changes */
942     if (strcmp(newentry->name, tentry.name)) {  /* Name changes; redo hashing */
943         if ((code = UnhashVolname(&ctx, blockindex, &tentry))) {
944             goto abort;
945         }
946         hashnewname = 1;
947     }
948
949     /* after this, tentry is new entry, not old one.  vldbentry_to_vlentry
950      * doesn't touch hash chains */
951     if ((code = nvldbentry_to_vlentry(&ctx, newentry, &tentry))) {
952         goto abort;
953     }
954
955     for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
956         if (hashVol[typeindex] && tentry.volumeId[typeindex]) {
957             if ((code = HashVolid(&ctx, typeindex, blockindex, &tentry))) {
958                 goto abort;
959             }
960         }
961     }
962
963     if (hashnewname)
964         HashVolname(&ctx, blockindex, &tentry);
965
966     if (releasetype)
967         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
968     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
969         ABORT(VL_IO);
970     }
971
972     return ubik_EndTrans(ctx.trans);
973
974   abort:
975     countAbort(this_op);
976     ubik_AbortTrans(ctx.trans);
977     return code;
978 }
979
980 afs_int32
981 SVL_ReplaceEntryN(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
982                   struct nvldbentry *newentry, afs_int32 releasetype)
983 {
984     afs_int32 code;
985
986     code = ReplaceEntryN(rxcall, volid, voltype, newentry, releasetype);
987     osi_auditU(rxcall, VLReplaceVLEntryEvent, code, AUD_LONG, volid, AUD_END);
988     return code;
989 }
990
991
992 /* Update a vldb entry (accessed thru its volume id). Almost all of the
993  * entry's fields can be modified in a single call by setting the
994  * appropriate bits in the Mask field in VldbUpdateentry. */
995 /* this routine may never have been tested; use replace entry instead
996  * unless you're brave */
997 static afs_int32
998 UpdateEntry(struct rx_call *rxcall,
999             afs_uint32 volid,
1000             afs_int32 voltype,
1001             struct VldbUpdateEntry *updateentry,        /* Update entry copied here */
1002             afs_int32 releasetype)
1003 {
1004     int this_op = VLUPDATEENTRY;
1005     struct vl_ctx ctx;
1006     afs_int32 blockindex, code;
1007     struct nvlentry tentry;
1008     char rxstr[AFS_RXINFO_LEN];
1009
1010     countRequest(this_op);
1011     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
1012         return VL_PERM;
1013     if ((voltype != -1) && (InvalidVoltype(voltype)))
1014         return VL_BADVOLTYPE;
1015     if (releasetype && InvalidReleasetype(releasetype))
1016         return VL_BADRELLOCKTYPE;
1017     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
1018         return code;
1019
1020     VLog(1, ("Update Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
1021     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
1022     if (blockindex == 0) {      /* entry not found */
1023         if (!code)
1024             code = VL_NOENT;
1025         goto abort;
1026     }
1027
1028     /* Do the actual updating of the entry, tentry. */
1029     if ((code =
1030         get_vldbupdateentry(&ctx, blockindex, updateentry, &tentry))) {
1031         goto abort;
1032     }
1033     if (releasetype)
1034         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
1035     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
1036         ABORT(VL_IO);
1037     }
1038     return ubik_EndTrans(ctx.trans);
1039
1040   abort:
1041     countAbort(this_op);
1042     ubik_AbortTrans(ctx.trans);
1043     return code;
1044 }
1045
1046 afs_int32
1047 SVL_UpdateEntry(struct rx_call *rxcall,
1048                 afs_uint32 volid,
1049                 afs_int32 voltype,
1050                 struct VldbUpdateEntry *updateentry,
1051                 afs_int32 releasetype)
1052 {
1053     afs_int32 code;
1054
1055     code = UpdateEntry(rxcall, volid, voltype, updateentry, releasetype);
1056     osi_auditU(rxcall, VLUpdateEntryEvent, code, AUD_LONG, volid, AUD_END);
1057     return code;
1058 }
1059
1060 static afs_int32
1061 UpdateEntryByName(struct rx_call *rxcall,
1062                   char *volname,
1063                   struct VldbUpdateEntry *updateentry, /* Update entry copied here */
1064                   afs_int32 releasetype)
1065 {
1066     int this_op = VLUPDATEENTRYBYNAME;
1067     struct vl_ctx ctx;
1068     afs_int32 blockindex, code;
1069     struct nvlentry tentry;
1070
1071     countRequest(this_op);
1072     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
1073         return VL_PERM;
1074     if (releasetype && InvalidReleasetype(releasetype))
1075         return VL_BADRELLOCKTYPE;
1076     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
1077         return code;
1078
1079     blockindex = FindByName(&ctx, volname, &tentry, &code);
1080     if (blockindex == 0) {      /* entry not found */
1081         if (!code)
1082             code = VL_NOENT;
1083         goto abort;
1084     }
1085
1086     /* Do the actual updating of the entry, tentry. */
1087     if ((code =
1088         get_vldbupdateentry(&ctx, blockindex, updateentry, &tentry))) {
1089         goto abort;
1090     }
1091     if (releasetype)
1092         ReleaseEntry(&tentry, releasetype);     /* Unlock entry if necessary */
1093     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
1094         ABORT(VL_IO);
1095     }
1096     return ubik_EndTrans(ctx.trans);
1097
1098   abort:
1099     countAbort(this_op);
1100     ubik_AbortTrans(ctx.trans);
1101     return code;
1102 }
1103
1104 afs_int32
1105 SVL_UpdateEntryByName(struct rx_call *rxcall,
1106                       char *volname,
1107                       struct VldbUpdateEntry *updateentry, /* Update entry copied here */
1108                       afs_int32 releasetype)
1109 {
1110     afs_int32 code;
1111
1112     code = UpdateEntryByName(rxcall, volname, updateentry, releasetype);
1113     osi_auditU(rxcall, VLUpdateEntryEvent, code, AUD_LONG, -1, AUD_END);
1114     return code;
1115 }
1116
1117 /* Set a lock to the vldb entry for volid (of type voltype if not -1). */
1118 static afs_int32
1119 SetLock(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
1120         afs_int32 voloper)
1121 {
1122     int this_op = VLSETLOCK;
1123     afs_int32 timestamp, blockindex, code;
1124     struct vl_ctx ctx;
1125     struct nvlentry tentry;
1126     char rxstr[AFS_RXINFO_LEN];
1127
1128     countRequest(this_op);
1129     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
1130         return VL_PERM;
1131     if ((voltype != -1) && (InvalidVoltype(voltype)))
1132         return VL_BADVOLTYPE;
1133     if (InvalidOperation(voloper))
1134         return VL_BADVOLOPER;
1135     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
1136         return code;
1137
1138     VLog(1, ("SetLock Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
1139     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
1140     if (blockindex == NULLO) {
1141         if (!code)
1142             code = VL_NOENT;
1143         goto abort;
1144     }
1145     if (tentry.flags & VLDELETED) {
1146         ABORT(VL_ENTDELETED);
1147     }
1148     timestamp = FT_ApproxTime();
1149
1150     /* Check if entry is already locked; note that we unlock any entry
1151      * locked more than MAXLOCKTIME seconds */
1152     if ((tentry.LockTimestamp)
1153         && ((timestamp - tentry.LockTimestamp) < MAXLOCKTIME)) {
1154         ABORT(VL_ENTRYLOCKED);
1155     }
1156
1157     /* Consider it an unlocked entry: set current timestamp, caller
1158      * and active vol operation */
1159     tentry.LockTimestamp = timestamp;
1160     tentry.LockAfsId = 0;       /* Not implemented yet */
1161     if (tentry.flags & VLOP_RELEASE) {
1162         ABORT(VL_RERELEASE);
1163     }
1164     tentry.flags &= ~VLOP_ALLOPERS;     /* Clear any possible older operation bit */
1165     tentry.flags |= voloper;
1166
1167     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
1168         ABORT(VL_IO);
1169     }
1170     return ubik_EndTrans(ctx.trans);
1171
1172   abort:
1173     countAbort(this_op);
1174     ubik_AbortTrans(ctx.trans);
1175     return code;
1176 }
1177
1178 afs_int32
1179 SVL_SetLock(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
1180             afs_int32 voloper)
1181 {
1182     afs_int32 code;
1183
1184     code = SetLock(rxcall, volid, voltype, voloper);
1185     osi_auditU(rxcall, VLSetLockEvent, code, AUD_LONG, volid, AUD_END);
1186     return code;
1187 }
1188
1189 /* Release an already locked vldb entry. Releasetype determines what
1190  * fields (afsid and/or volume operation) will be cleared along with
1191  * the lock time stamp. */
1192
1193 static afs_int32
1194 ReleaseLock(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
1195             afs_int32 releasetype)
1196 {
1197     int this_op = VLRELEASELOCK;
1198     afs_int32 blockindex, code;
1199     struct vl_ctx ctx;
1200     struct nvlentry tentry;
1201     char rxstr[AFS_RXINFO_LEN];
1202
1203     countRequest(this_op);
1204     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
1205         return VL_PERM;
1206     if ((voltype != -1) && (InvalidVoltype(voltype)))
1207         return VL_BADVOLTYPE;
1208     if (releasetype && InvalidReleasetype(releasetype))
1209         return VL_BADRELLOCKTYPE;
1210     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
1211         return code;
1212
1213     VLog(1, ("ReleaseLock Volume %u %s\n", volid, rxinfo(rxstr, rxcall)));
1214     blockindex = FindByID(&ctx, volid, voltype, &tentry, &code);
1215     if (blockindex == NULLO) {
1216         if (!code)
1217             code = VL_NOENT;
1218         goto abort;
1219     }
1220     if (tentry.flags & VLDELETED) {
1221         ABORT(VL_ENTDELETED);
1222     }
1223     if (releasetype)
1224         ReleaseEntry(&tentry, releasetype);     /* Unlock the appropriate fields */
1225     if (vlentrywrite(ctx.trans, blockindex, &tentry, sizeof(tentry))) {
1226         ABORT(VL_IO);
1227     }
1228     return ubik_EndTrans(ctx.trans);
1229
1230   abort:
1231     countAbort(this_op);
1232     ubik_AbortTrans(ctx.trans);
1233     return code;
1234 }
1235
1236 afs_int32
1237 SVL_ReleaseLock(struct rx_call *rxcall, afs_uint32 volid, afs_int32 voltype,
1238                 afs_int32 releasetype)
1239 {
1240     afs_int32 code;
1241
1242     code = ReleaseLock(rxcall, volid, voltype, releasetype);
1243     osi_auditU(rxcall, VLReleaseLockEvent, code, AUD_LONG, volid, AUD_END);
1244     return code;
1245 }
1246
1247 /* ListEntry returns a single vldb entry, aentry, with offset previous_index;
1248  * the remaining parameters (i.e. next_index) are used so that sequential
1249  * calls to this routine will get the next (all) vldb entries.
1250  */
1251 static afs_int32
1252 ListEntry(struct rx_call *rxcall, afs_int32 previous_index,
1253           afs_int32 *count, afs_int32 *next_index,
1254           struct vldbentry *aentry)
1255 {
1256     int this_op = VLLISTENTRY;
1257     int code;
1258     struct vl_ctx ctx;
1259     struct nvlentry tentry;
1260     char rxstr[AFS_RXINFO_LEN];
1261
1262     countRequest(this_op);
1263
1264     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1265                                       restrictedQueryLevel))
1266         return VL_PERM;
1267
1268     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1269         return code;
1270     VLog(25, ("OListEntry index=%d %s\n", previous_index,
1271               rxinfo(rxstr, rxcall)));
1272     *next_index = NextEntry(&ctx, previous_index, &tentry, count);
1273     if (*next_index) {
1274         code = vlentry_to_vldbentry(&ctx, &tentry, aentry);
1275         if (code) {
1276             countAbort(this_op);
1277             ubik_AbortTrans(ctx.trans);
1278             return code;
1279         }
1280     }
1281     return ubik_EndTrans(ctx.trans);
1282 }
1283
1284 afs_int32
1285 SVL_ListEntry(struct rx_call *rxcall, afs_int32 previous_index,
1286               afs_int32 *count, afs_int32 *next_index,
1287               struct vldbentry *aentry)
1288 {
1289     afs_int32 code;
1290
1291     code = ListEntry(rxcall, previous_index, count, next_index, aentry);
1292     osi_auditU(rxcall, VLListEntryEvent, code, AUD_LONG, previous_index, AUD_END);
1293     return code;
1294 }
1295
1296 /* ListEntry returns a single vldb entry, aentry, with offset previous_index;
1297  * the remaining parameters (i.e. next_index) are used so that sequential
1298  * calls to this routine will get the next (all) vldb entries.
1299  */
1300 static afs_int32
1301 ListEntryN(struct rx_call *rxcall, afs_int32 previous_index,
1302            afs_int32 *count, afs_int32 *next_index,
1303            struct nvldbentry *aentry)
1304 {
1305     int this_op = VLLISTENTRYN;
1306     int code;
1307     struct vl_ctx ctx;
1308     struct nvlentry tentry;
1309     char rxstr[AFS_RXINFO_LEN];
1310
1311     countRequest(this_op);
1312
1313     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1314                                       restrictedQueryLevel))
1315         return VL_PERM;
1316
1317     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1318         return code;
1319     VLog(25, ("ListEntry index=%d %s\n", previous_index, rxinfo(rxstr, rxcall)));
1320     *next_index = NextEntry(&ctx, previous_index, &tentry, count);
1321     if (*next_index) {
1322         code = vlentry_to_nvldbentry(&ctx, &tentry, aentry);
1323         if (code) {
1324             countAbort(this_op);
1325             ubik_AbortTrans(ctx.trans);
1326             return code;
1327         }
1328     }
1329
1330     return ubik_EndTrans(ctx.trans);
1331 }
1332
1333 afs_int32
1334 SVL_ListEntryN(struct rx_call *rxcall, afs_int32 previous_index,
1335                afs_int32 *count, afs_int32 *next_index,
1336                struct nvldbentry *aentry)
1337 {
1338     afs_int32 code;
1339
1340     code = ListEntryN(rxcall, previous_index, count, next_index, aentry);
1341     osi_auditU(rxcall, VLListEntryEventN, code, AUD_LONG, previous_index, AUD_END);
1342     return code;
1343 }
1344
1345 /* Retrieves in vldbentries all vldb entries that match the specified
1346  * attributes (by server number, partition, volume type, and flag); if volume
1347  * id is specified then the associated list for that entry is returned.
1348  * CAUTION: This could be a very expensive call since in most cases
1349  * sequential search of all vldb entries is performed.
1350  */
1351 static afs_int32
1352 ListAttributes(struct rx_call *rxcall,
1353                struct VldbListByAttributes *attributes,
1354                afs_int32 *nentries,
1355                bulkentries *vldbentries)
1356 {
1357     int this_op = VLLISTATTRIBUTES;
1358     int code, allocCount = 0;
1359     struct vl_ctx ctx;
1360     struct nvlentry tentry;
1361     struct vldbentry *Vldbentry = 0, *VldbentryFirst = 0, *VldbentryLast = 0;
1362     int pollcount = 0;
1363     char rxstr[AFS_RXINFO_LEN];
1364
1365     countRequest(this_op);
1366
1367     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1368                                       restrictedQueryLevel))
1369         return VL_PERM;
1370
1371     vldbentries->bulkentries_val = 0;
1372     vldbentries->bulkentries_len = *nentries = 0;
1373     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1374         return code;
1375     allocCount = VLDBALLOCCOUNT;
1376     Vldbentry = VldbentryFirst = vldbentries->bulkentries_val =
1377         malloc(allocCount * sizeof(vldbentry));
1378     if (Vldbentry == NULL) {
1379         code = VL_NOMEM;
1380         goto abort;
1381     }
1382     VldbentryLast = VldbentryFirst + allocCount;
1383     /* Handle the attribute by volume id totally separate of the rest
1384      * (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!) */
1385     if (attributes->Mask & VLLIST_VOLUMEID) {
1386         afs_int32 blockindex;
1387
1388         blockindex =
1389             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
1390         if (blockindex == 0) {
1391             if (!code)
1392                 code = VL_NOENT;
1393             goto abort;
1394         }
1395
1396         code = put_attributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1397                                   &VldbentryLast, vldbentries, &tentry,
1398                                   nentries, &allocCount);
1399         if (code)
1400             goto abort;
1401     } else {
1402         afs_int32 nextblockindex = 0, count = 0, k = 0, match = 0;
1403         while ((nextblockindex =
1404                NextEntry(&ctx, nextblockindex, &tentry, &count))) {
1405             if (++pollcount > 50) {
1406 #ifndef AFS_PTHREAD_ENV
1407                 IOMGR_Poll();
1408 #endif
1409                 pollcount = 0;
1410             }
1411             match = 0;
1412             if (attributes->Mask & VLLIST_SERVER) {
1413                 int serverindex;
1414                 if ((serverindex =
1415                      IpAddrToRelAddr(&ctx, attributes->server, 0)) == -1)
1416                     continue;
1417                 for (k = 0; k < OMAXNSERVERS; k++) {
1418                     if (tentry.serverNumber[k] == BADSERVERID)
1419                         break;
1420                     if (tentry.serverNumber[k] == serverindex) {
1421                         match = 1;
1422                         break;
1423                     }
1424                 }
1425                 if (!match)
1426                     continue;
1427             }
1428             if (attributes->Mask & VLLIST_PARTITION) {
1429                 if (match) {
1430                     if (tentry.serverPartition[k] != attributes->partition)
1431                         continue;
1432                 } else {
1433                     for (k = 0; k < OMAXNSERVERS; k++) {
1434                         if (tentry.serverNumber[k] == BADSERVERID)
1435                             break;
1436                         if (tentry.serverPartition[k] ==
1437                             attributes->partition) {
1438                             match = 1;
1439                             break;
1440                         }
1441                     }
1442                     if (!match)
1443                         continue;
1444                 }
1445             }
1446
1447             if (attributes->Mask & VLLIST_FLAG) {
1448                 if (!(tentry.flags & attributes->flag))
1449                     continue;
1450             }
1451             code = put_attributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1452                                       &VldbentryLast, vldbentries, &tentry,
1453                                       nentries, &allocCount);
1454             if (code)
1455                 goto abort;
1456         }
1457     }
1458     if (vldbentries->bulkentries_len
1459         && (allocCount > vldbentries->bulkentries_len)) {
1460
1461         vldbentries->bulkentries_val =
1462             realloc(vldbentries->bulkentries_val,
1463                     vldbentries->bulkentries_len * sizeof(vldbentry));
1464         if (vldbentries->bulkentries_val == NULL) {
1465             code = VL_NOMEM;
1466             goto abort;
1467         }
1468     }
1469     VLog(5,
1470          ("ListAttrs nentries=%d %s\n", vldbentries->bulkentries_len,
1471           rxinfo(rxstr, rxcall)));
1472     return ubik_EndTrans(ctx.trans);
1473
1474 abort:
1475     if (vldbentries->bulkentries_val)
1476         free(vldbentries->bulkentries_val);
1477     vldbentries->bulkentries_val = 0;
1478     vldbentries->bulkentries_len = 0;
1479
1480     countAbort(this_op);
1481     ubik_AbortTrans(ctx.trans);
1482     return code;
1483 }
1484
1485 afs_int32
1486 SVL_ListAttributes(struct rx_call *rxcall,
1487                    struct VldbListByAttributes *attributes,
1488                    afs_int32 *nentries,
1489                    bulkentries *vldbentries)
1490 {
1491     afs_int32 code;
1492
1493     code = ListAttributes(rxcall, attributes, nentries, vldbentries);
1494     osi_auditU(rxcall, VLListAttributesEvent, code, AUD_END);
1495     return code;
1496 }
1497
1498 static afs_int32
1499 ListAttributesN(struct rx_call *rxcall,
1500                 struct VldbListByAttributes *attributes,
1501                 afs_int32 *nentries,
1502                 nbulkentries *vldbentries)
1503 {
1504     int this_op = VLLISTATTRIBUTESN;
1505     int code, allocCount = 0;
1506     struct vl_ctx ctx;
1507     struct nvlentry tentry;
1508     struct nvldbentry *Vldbentry = 0, *VldbentryFirst = 0, *VldbentryLast = 0;
1509     int pollcount = 0;
1510     char rxstr[AFS_RXINFO_LEN];
1511
1512     countRequest(this_op);
1513
1514     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1515                                       restrictedQueryLevel))
1516         return VL_PERM;
1517
1518     vldbentries->nbulkentries_val = 0;
1519     vldbentries->nbulkentries_len = *nentries = 0;
1520     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1521         return code;
1522     allocCount = VLDBALLOCCOUNT;
1523     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val =
1524         malloc(allocCount * sizeof(nvldbentry));
1525     if (Vldbentry == NULL) {
1526         code = VL_NOMEM;
1527         goto abort;
1528     }
1529     VldbentryLast = VldbentryFirst + allocCount;
1530     /* Handle the attribute by volume id totally separate of the rest
1531      * (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!) */
1532     if (attributes->Mask & VLLIST_VOLUMEID) {
1533         afs_int32 blockindex;
1534
1535         blockindex =
1536             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
1537         if (blockindex == 0) {
1538             if (!code)
1539                 code = VL_NOENT;
1540             goto abort;
1541         }
1542
1543         code = put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1544                                    &VldbentryLast, vldbentries, &tentry,
1545                                    0, 0, nentries, &allocCount);
1546         if (code)
1547             goto abort;
1548     } else {
1549         afs_int32 nextblockindex = 0, count = 0, k = 0, match = 0;
1550         while ((nextblockindex =
1551                NextEntry(&ctx, nextblockindex, &tentry, &count))) {
1552             if (++pollcount > 50) {
1553 #ifndef AFS_PTHREAD_ENV
1554                 IOMGR_Poll();
1555 #endif
1556                 pollcount = 0;
1557             }
1558
1559             match = 0;
1560             if (attributes->Mask & VLLIST_SERVER) {
1561                 int serverindex;
1562                 if ((serverindex =
1563                      IpAddrToRelAddr(&ctx, attributes->server, 0)) == -1)
1564                     continue;
1565                 for (k = 0; k < NMAXNSERVERS; k++) {
1566                     if (tentry.serverNumber[k] == BADSERVERID)
1567                         break;
1568                     if (tentry.serverNumber[k] == serverindex) {
1569                         match = 1;
1570                         break;
1571                     }
1572                 }
1573                 if (!match)
1574                     continue;
1575             }
1576             if (attributes->Mask & VLLIST_PARTITION) {
1577                 if (match) {
1578                     if (tentry.serverPartition[k] != attributes->partition)
1579                         continue;
1580                 } else {
1581                     for (k = 0; k < NMAXNSERVERS; k++) {
1582                         if (tentry.serverNumber[k] == BADSERVERID)
1583                             break;
1584                         if (tentry.serverPartition[k] ==
1585                             attributes->partition) {
1586                             match = 1;
1587                             break;
1588                         }
1589                     }
1590                     if (!match)
1591                         continue;
1592                 }
1593             }
1594
1595             if (attributes->Mask & VLLIST_FLAG) {
1596                 if (!(tentry.flags & attributes->flag))
1597                     continue;
1598             }
1599             code = put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1600                                        &VldbentryLast, vldbentries,
1601                                        &tentry, 0, 0, nentries, &allocCount);
1602             if (code)
1603                 goto abort;
1604         }
1605     }
1606     if (vldbentries->nbulkentries_len
1607         && (allocCount > vldbentries->nbulkentries_len)) {
1608
1609         vldbentries->nbulkentries_val =
1610             realloc(vldbentries->nbulkentries_val,
1611                     vldbentries->nbulkentries_len * sizeof(nvldbentry));
1612         if (vldbentries->nbulkentries_val == NULL) {
1613             code = VL_NOMEM;
1614             goto abort;
1615         }
1616     }
1617     VLog(5,
1618          ("NListAttrs nentries=%d %s\n", vldbentries->nbulkentries_len,
1619           rxinfo(rxstr, rxcall)));
1620     return ubik_EndTrans(ctx.trans);
1621
1622 abort:
1623     countAbort(this_op);
1624     ubik_AbortTrans(ctx.trans);
1625     if (vldbentries->nbulkentries_val)
1626         free(vldbentries->nbulkentries_val);
1627     vldbentries->nbulkentries_val = 0;
1628     vldbentries->nbulkentries_len = 0;
1629     return code;
1630 }
1631
1632 afs_int32
1633 SVL_ListAttributesN(struct rx_call *rxcall,
1634                     struct VldbListByAttributes *attributes,
1635                     afs_int32 *nentries,
1636                     nbulkentries *vldbentries)
1637 {
1638     afs_int32 code;
1639
1640     code = ListAttributesN(rxcall, attributes, nentries, vldbentries);
1641     osi_auditU(rxcall, VLListAttributesNEvent, code, AUD_END);
1642     return code;
1643 }
1644
1645 static afs_int32
1646 ListAttributesN2(struct rx_call *rxcall,
1647                  struct VldbListByAttributes *attributes,
1648                  char *name,            /* Wildcarded volume name */
1649                  afs_int32 startindex,
1650                  afs_int32 *nentries,
1651                  nbulkentries *vldbentries,
1652                  afs_int32 *nextstartindex)
1653 {
1654     int this_op = VLLISTATTRIBUTESN2;
1655     int code = 0, maxCount = VLDBALLOCCOUNT;
1656     struct vl_ctx ctx;
1657     struct nvlentry tentry;
1658     struct nvldbentry *Vldbentry = 0, *VldbentryFirst = 0, *VldbentryLast = 0;
1659     afs_int32 blockindex = 0, count = 0, k, match;
1660     afs_int32 matchindex = 0;
1661     int serverindex = -1;       /* no server found */
1662     int findserver = 0, findpartition = 0, findflag = 0, findname = 0;
1663     int pollcount = 0;
1664     int namematchRWBK, namematchRO, thismatch;
1665     int matchtype = 0;
1666     int size;
1667     char volumename[VL_MAXNAMELEN+3]; /* regex anchors */
1668     char rxstr[AFS_RXINFO_LEN];
1669 #ifdef HAVE_POSIX_REGEX
1670     regex_t re;
1671     int need_regfree = 0;
1672 #else
1673     char *t;
1674 #endif
1675
1676     countRequest(this_op);
1677
1678     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1679                                       restrictedQueryLevel))
1680         return VL_PERM;
1681
1682     vldbentries->nbulkentries_val = 0;
1683     vldbentries->nbulkentries_len = 0;
1684     *nentries = 0;
1685     *nextstartindex = -1;
1686
1687     code = Init_VLdbase(&ctx, LOCKREAD, this_op);
1688     if (code)
1689         return code;
1690
1691     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val =
1692         malloc(maxCount * sizeof(nvldbentry));
1693     if (Vldbentry == NULL) {
1694         countAbort(this_op);
1695         ubik_AbortTrans(ctx.trans);
1696         return VL_NOMEM;
1697     }
1698
1699     VldbentryLast = VldbentryFirst + maxCount;
1700
1701     /* Handle the attribute by volume id totally separate of the rest
1702      * (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!)
1703      */
1704     if (attributes->Mask & VLLIST_VOLUMEID) {
1705         blockindex =
1706             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
1707         if (blockindex == 0) {
1708             if (!code)
1709                 code = VL_NOENT;
1710         } else {
1711             code =
1712                 put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1713                                     &VldbentryLast, vldbentries, &tentry, 0,
1714                                     0, nentries, &maxCount);
1715             if (code)
1716                 goto done;
1717         }
1718     }
1719
1720     /* Search each entry in the database and return all entries
1721      * that match the request. It checks volumename (with
1722      * wildcarding), entry flags, server, and partition.
1723      */
1724     else {
1725         /* Get the server index for matching server address */
1726         if (attributes->Mask & VLLIST_SERVER) {
1727             serverindex =
1728                 IpAddrToRelAddr(&ctx, attributes->server, 0);
1729             if (serverindex == -1)
1730                 goto done;
1731             findserver = 1;
1732         }
1733         findpartition = ((attributes->Mask & VLLIST_PARTITION) ? 1 : 0);
1734         findflag = ((attributes->Mask & VLLIST_FLAG) ? 1 : 0);
1735         if (name && (strcmp(name, ".*") != 0) && (strcmp(name, "") != 0)) {
1736             if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL)) {
1737                 code = VL_PERM;
1738                 goto done;
1739             }
1740             size = snprintf(volumename, sizeof(volumename), "^%s$", name);
1741             if (size < 0 || size >= sizeof(volumename)) {
1742                 code = VL_BADNAME;
1743                 goto done;
1744             }
1745 #ifdef HAVE_POSIX_REGEX
1746             if (regcomp(&re, volumename, REG_NOSUB) != 0) {
1747                 code = VL_BADNAME;
1748                 goto done;
1749             }
1750             need_regfree = 1;
1751 #else
1752             t = (char *)re_comp(volumename);
1753             if (t) {
1754                 code = VL_BADNAME;
1755                 goto done;
1756             }
1757 #endif
1758             findname = 1;
1759         }
1760
1761         /* Read each entry and see if it is the one we want */
1762         blockindex = startindex;
1763         while ((blockindex = NextEntry(&ctx, blockindex, &tentry, &count))) {
1764             if (++pollcount > 50) {
1765 #ifndef AFS_PTHREAD_ENV
1766                 IOMGR_Poll();
1767 #endif
1768                 pollcount = 0;
1769             }
1770
1771             /* Step through each server index searching for a match.
1772              * Match to an existing RW, BK, or RO volume name (preference
1773              * is in this order). Remember which index we matched against.
1774              */
1775             namematchRWBK = namematchRO = 0;    /* 0->notTried; 1->match; 2->noMatch */
1776             match = 0;
1777             for (k = 0;
1778                  (k < NMAXNSERVERS
1779                   && (tentry.serverNumber[k] != BADSERVERID)); k++) {
1780                 thismatch = 0;  /* does this index match */
1781
1782                 /* Match against the RW or BK volume name. Remember
1783                  * results in namematchRWBK. Prefer RW over BK.
1784                  */
1785                 if (tentry.serverFlags[k] & VLSF_RWVOL) {
1786                     /* Does the name match the RW name */
1787                     if (tentry.flags & VLF_RWEXISTS) {
1788                         if (findname) {
1789                             size = snprintf(volumename, sizeof(volumename),
1790                                             "%s", tentry.name);
1791                             if (size < 0 || size >= sizeof(volumename)) {
1792                                 code = VL_BADNAME;
1793                                 goto done;
1794                             }
1795 #ifdef HAVE_POSIX_REGEX
1796                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1797                                 thismatch = VLSF_RWVOL;
1798                             }
1799 #else
1800                             if (re_exec(volumename)) {
1801                                 thismatch = VLSF_RWVOL;
1802                             }
1803 #endif
1804                         } else {
1805                             thismatch = VLSF_RWVOL;
1806                         }
1807                     }
1808
1809                     /* Does the name match the BK name */
1810                     if (!thismatch && (tentry.flags & VLF_BACKEXISTS)) {
1811                         if (findname) {
1812                             /* If this fails, the tentry.name is invalid */
1813                             size = snprintf(volumename, sizeof(volumename),
1814                                             "%s.backup", tentry.name);
1815                             if (size < 0 || size >= sizeof(volumename)) {
1816                                 code = VL_BADNAME;
1817                                 goto done;
1818                             }
1819 #ifdef HAVE_POSIX_REGEX
1820                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1821                                 thismatch = VLSF_BACKVOL;
1822                             }
1823 #else
1824                             if (re_exec(volumename)) {
1825                                 thismatch = VLSF_BACKVOL;
1826                             }
1827 #endif
1828                         } else {
1829                             thismatch = VLSF_BACKVOL;
1830                         }
1831                     }
1832
1833                     namematchRWBK = (thismatch ? 1 : 2);
1834                 }
1835
1836                 /* Match with the RO volume name. Compare once and
1837                  * remember results in namematchRO. Note that this will
1838                  * pick up entries marked NEWREPSITEs and DONTUSE.
1839                  */
1840                 else {
1841                     if (tentry.flags & VLF_ROEXISTS) {
1842                         if (findname) {
1843                             if (namematchRO) {
1844                                 thismatch =
1845                                     ((namematchRO == 1) ? VLSF_ROVOL : 0);
1846                             } else {
1847                                 /* If this fails, the tentry.name is invalid */
1848                                 size = snprintf(volumename, sizeof(volumename),
1849                                                 "%s.readonly", tentry.name);
1850                                 if (size < 0 || size >= sizeof(volumename)) {
1851                                     code = VL_BADNAME;
1852                                     goto done;
1853                                 }
1854 #ifdef HAVE_POSIX_REGEX
1855                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1856                                 thismatch = VLSF_ROVOL;
1857                             }
1858 #else
1859                                 if (re_exec(volumename))
1860                                     thismatch = VLSF_ROVOL;
1861 #endif
1862                             }
1863                         } else {
1864                             thismatch = VLSF_ROVOL;
1865                         }
1866                     }
1867                     namematchRO = (thismatch ? 1 : 2);
1868                 }
1869
1870                 /* Is there a server match */
1871                 if (thismatch && findserver
1872                     && (tentry.serverNumber[k] != serverindex))
1873                     thismatch = 0;
1874
1875                 /* Is there a partition match */
1876                 if (thismatch && findpartition
1877                     && (tentry.serverPartition[k] != attributes->partition))
1878                     thismatch = 0;
1879
1880                 /* Is there a flag match */
1881                 if (thismatch && findflag
1882                     && !(tentry.flags & attributes->flag))
1883                     thismatch = 0;
1884
1885                 /* We found a match. Remember the index, and type */
1886                 if (thismatch) {
1887                     match = 1;
1888                     matchindex = k;
1889                     matchtype = thismatch;
1890                 }
1891
1892                 /* Since we prefer RW and BK volume matches over RO matches,
1893                  * if we have already checked the RWBK name, then we already
1894                  * found the best match and so end the search.
1895                  *
1896                  * If we tried matching against the RW, BK, and RO volume names
1897                  * and both failed, then we end the search (none will match).
1898                  */
1899                 if ((match && namematchRWBK)
1900                     || ((namematchRWBK == 2) && (namematchRO == 2)))
1901                     break;
1902             }
1903
1904             /* Passed all the tests. Take it */
1905             if (match) {
1906                 code =
1907                     put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1908                                         &VldbentryLast, vldbentries, &tentry,
1909                                         matchtype, matchindex, nentries,
1910                                         &maxCount);
1911                 if (code)
1912                     goto done;
1913
1914                 if (*nentries >= maxCount)
1915                     break;      /* collected the max */
1916             }
1917         }
1918         *nextstartindex = (blockindex ? blockindex : -1);
1919     }
1920
1921   done:
1922 #ifdef HAVE_POSIX_REGEX
1923     if (need_regfree)
1924         regfree(&re);
1925 #endif
1926
1927     if (code) {
1928         countAbort(this_op);
1929         ubik_AbortTrans(ctx.trans);
1930         if (vldbentries->nbulkentries_val)
1931             free(vldbentries->nbulkentries_val);
1932         vldbentries->nbulkentries_val = 0;
1933         vldbentries->nbulkentries_len = 0;
1934         *nextstartindex = -1;
1935     } else {
1936         VLog(5,
1937              ("N2ListAttrs nentries=%d %s\n", vldbentries->nbulkentries_len,
1938               rxinfo(rxstr, rxcall)));
1939         code = ubik_EndTrans(ctx.trans);
1940     }
1941
1942     return code;
1943 }
1944
1945 afs_int32
1946 SVL_ListAttributesN2(struct rx_call *rxcall,
1947                      struct VldbListByAttributes *attributes,
1948                      char *name,                /* Wildcarded volume name */
1949                      afs_int32 startindex,
1950                      afs_int32 *nentries,
1951                      nbulkentries *vldbentries,
1952                      afs_int32 *nextstartindex)
1953 {
1954     afs_int32 code;
1955
1956     code = ListAttributesN2(rxcall, attributes, name, startindex,
1957                             nentries, vldbentries, nextstartindex);
1958     osi_auditU(rxcall, VLListAttributesN2Event, code, AUD_END);
1959     return code;
1960 }
1961
1962 /* Retrieves in vldbentries all vldb entries that match the specified
1963  * attributes (by server number, partition, volume type, and flag); if
1964  * volume id is specified then the associated list for that entry is
1965  * returned. CAUTION: This could be a very expensive call since in most
1966  * cases sequential search of all vldb entries is performed.
1967  */
1968 static afs_int32
1969 LinkedList(struct rx_call *rxcall,
1970            struct VldbListByAttributes *attributes,
1971            afs_int32 *nentries,
1972            vldb_list *vldbentries)
1973 {
1974     int this_op = VLLINKEDLIST;
1975     int code;
1976     struct vl_ctx ctx;
1977     struct nvlentry tentry;
1978     vldblist vllist, *vllistptr;
1979     afs_int32 blockindex, count, match;
1980     afs_int32 k = 0;
1981     int serverindex;
1982     int pollcount = 0;
1983
1984     countRequest(this_op);
1985
1986     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1987                                       restrictedQueryLevel))
1988         return VL_PERM;
1989
1990     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1991         return code;
1992
1993     *nentries = 0;
1994     vldbentries->node = NULL;
1995     vllistptr = &vldbentries->node;
1996
1997     /* List by volumeid */
1998     if (attributes->Mask & VLLIST_VOLUMEID) {
1999         blockindex =
2000             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
2001         if (!blockindex) {
2002             if (!code)
2003                 code = VL_NOENT;
2004             goto abort;
2005         }
2006
2007         vllist = malloc(sizeof(single_vldbentry));
2008         if (vllist == NULL) {
2009             code = VL_NOMEM;
2010             goto abort;
2011         }
2012         code = vlentry_to_vldbentry(&ctx, &tentry, &vllist->VldbEntry);
2013         if (code)
2014             goto abort;
2015
2016         vllist->next_vldb = NULL;
2017
2018         *vllistptr = vllist;    /* Thread onto list */
2019         vllistptr = &vllist->next_vldb;
2020         (*nentries)++;
2021     }
2022
2023     /* Search by server, partition, and flags */
2024     else {
2025         for (blockindex = NextEntry(&ctx, 0, &tentry, &count); blockindex;
2026              blockindex = NextEntry(&ctx, blockindex, &tentry, &count)) {
2027             match = 0;
2028
2029             if (++pollcount > 50) {
2030 #ifndef AFS_PTHREAD_ENV
2031                 IOMGR_Poll();
2032 #endif
2033                 pollcount = 0;
2034             }
2035
2036             /* Does this volume exist on the desired server */
2037             if (attributes->Mask & VLLIST_SERVER) {
2038                 serverindex =
2039                     IpAddrToRelAddr(&ctx, attributes->server, 0);
2040                 if (serverindex == -1)
2041                     continue;
2042                 for (k = 0; k < OMAXNSERVERS; k++) {
2043                     if (tentry.serverNumber[k] == BADSERVERID)
2044                         break;
2045                     if (tentry.serverNumber[k] == serverindex) {
2046                         match = 1;
2047                         break;
2048                     }
2049                 }
2050                 if (!match)
2051                     continue;
2052             }
2053
2054             /* Does this volume exist on the desired partition */
2055             if (attributes->Mask & VLLIST_PARTITION) {
2056                 if (match) {
2057                     if (tentry.serverPartition[k] != attributes->partition)
2058                         match = 0;
2059                 } else {
2060                     for (k = 0; k < OMAXNSERVERS; k++) {
2061                         if (tentry.serverNumber[k] == BADSERVERID)
2062                             break;
2063                         if (tentry.serverPartition[k] ==
2064                             attributes->partition) {
2065                             match = 1;
2066                             break;
2067                         }
2068                     }
2069                 }
2070                 if (!match)
2071                     continue;
2072             }
2073
2074             /* Does this volume have the desired flags */
2075             if (attributes->Mask & VLLIST_FLAG) {
2076                 if (!(tentry.flags & attributes->flag))
2077                     continue;
2078             }
2079
2080             vllist = malloc(sizeof(single_vldbentry));
2081             if (vllist == NULL) {
2082                 code = VL_NOMEM;
2083                 goto abort;
2084             }
2085             code = vlentry_to_vldbentry(&ctx, &tentry, &vllist->VldbEntry);
2086             if (code)
2087                 goto abort;
2088
2089             vllist->next_vldb = NULL;
2090
2091             *vllistptr = vllist;        /* Thread onto list */
2092             vllistptr = &vllist->next_vldb;
2093             (*nentries)++;
2094             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
2095                 code = VL_SIZEEXCEEDED;
2096                 goto abort;
2097             }
2098         }
2099     }
2100     *vllistptr = NULL;
2101     return ubik_EndTrans(ctx.trans);
2102
2103 abort:
2104     countAbort(this_op);
2105     ubik_AbortTrans(ctx.trans);
2106     return code;
2107 }
2108
2109 afs_int32
2110 SVL_LinkedList(struct rx_call *rxcall,
2111                struct VldbListByAttributes *attributes,
2112                afs_int32 *nentries,
2113                vldb_list *vldbentries)
2114 {
2115     afs_int32 code;
2116
2117     code = LinkedList(rxcall, attributes, nentries, vldbentries);
2118     osi_auditU(rxcall, VLLinkedListEvent, code, AUD_END);
2119     return code;
2120 }
2121
2122 static afs_int32
2123 LinkedListN(struct rx_call *rxcall,
2124             struct VldbListByAttributes *attributes,
2125             afs_int32 *nentries,
2126             nvldb_list *vldbentries)
2127 {
2128     int this_op = VLLINKEDLISTN;
2129     int code;
2130     struct vl_ctx ctx;
2131     struct nvlentry tentry;
2132     nvldblist vllist, *vllistptr;
2133     afs_int32 blockindex, count, match;
2134     afs_int32 k = 0;
2135     int serverindex;
2136     int pollcount = 0;
2137
2138     countRequest(this_op);
2139
2140     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
2141                                       restrictedQueryLevel))
2142         return VL_PERM;
2143
2144     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2145         return code;
2146
2147     *nentries = 0;
2148     vldbentries->node = NULL;
2149     vllistptr = &vldbentries->node;
2150
2151     /* List by volumeid */
2152     if (attributes->Mask & VLLIST_VOLUMEID) {
2153         blockindex =
2154             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
2155         if (!blockindex) {
2156             if (!code)
2157                 code = VL_NOENT;
2158             goto abort;
2159         }
2160
2161         vllist = malloc(sizeof(single_nvldbentry));
2162         if (vllist == NULL) {
2163             code = VL_NOMEM;
2164             goto abort;
2165         }
2166         code = vlentry_to_nvldbentry(&ctx, &tentry, &vllist->VldbEntry);
2167         if (code)
2168             goto abort;
2169
2170         vllist->next_vldb = NULL;
2171
2172         *vllistptr = vllist;    /* Thread onto list */
2173         vllistptr = &vllist->next_vldb;
2174         (*nentries)++;
2175     }
2176
2177     /* Search by server, partition, and flags */
2178     else {
2179         for (blockindex = NextEntry(&ctx, 0, &tentry, &count); blockindex;
2180              blockindex = NextEntry(&ctx, blockindex, &tentry, &count)) {
2181             match = 0;
2182
2183             if (++pollcount > 50) {
2184 #ifndef AFS_PTHREAD_ENV
2185                 IOMGR_Poll();
2186 #endif
2187                 pollcount = 0;
2188             }
2189
2190             /* Does this volume exist on the desired server */
2191             if (attributes->Mask & VLLIST_SERVER) {
2192                 serverindex =
2193                     IpAddrToRelAddr(&ctx, attributes->server, 0);
2194                 if (serverindex == -1)
2195                     continue;
2196                 for (k = 0; k < NMAXNSERVERS; k++) {
2197                     if (tentry.serverNumber[k] == BADSERVERID)
2198                         break;
2199                     if (tentry.serverNumber[k] == serverindex) {
2200                         match = 1;
2201                         break;
2202                     }
2203                 }
2204                 if (!match)
2205                     continue;
2206             }
2207
2208             /* Does this volume exist on the desired partition */
2209             if (attributes->Mask & VLLIST_PARTITION) {
2210                 if (match) {
2211                     if (tentry.serverPartition[k] != attributes->partition)
2212                         match = 0;
2213                 } else {
2214                     for (k = 0; k < NMAXNSERVERS; k++) {
2215                         if (tentry.serverNumber[k] == BADSERVERID)
2216                             break;
2217                         if (tentry.serverPartition[k] ==
2218                             attributes->partition) {
2219                             match = 1;
2220                             break;
2221                         }
2222                     }
2223                 }
2224                 if (!match)
2225                     continue;
2226             }
2227
2228             /* Does this volume have the desired flags */
2229             if (attributes->Mask & VLLIST_FLAG) {
2230                 if (!(tentry.flags & attributes->flag))
2231                     continue;
2232             }
2233
2234             vllist = malloc(sizeof(single_nvldbentry));
2235             if (vllist == NULL) {
2236                 code = VL_NOMEM;
2237                 goto abort;
2238             }
2239             code = vlentry_to_nvldbentry(&ctx, &tentry, &vllist->VldbEntry);
2240             if (code)
2241                 goto abort;
2242
2243             vllist->next_vldb = NULL;
2244
2245             *vllistptr = vllist;        /* Thread onto list */
2246             vllistptr = &vllist->next_vldb;
2247             (*nentries)++;
2248             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
2249                 code = VL_SIZEEXCEEDED;
2250                 goto abort;
2251             }
2252         }
2253     }
2254     *vllistptr = NULL;
2255     return ubik_EndTrans(ctx.trans);
2256
2257 abort:
2258     countAbort(this_op);
2259     ubik_AbortTrans(ctx.trans);
2260     return code;
2261 }
2262
2263 afs_int32
2264 SVL_LinkedListN(struct rx_call *rxcall,
2265                 struct VldbListByAttributes *attributes,
2266                 afs_int32 *nentries,
2267                 nvldb_list *vldbentries)
2268 {
2269     afs_int32 code;
2270
2271     code = LinkedListN(rxcall, attributes, nentries, vldbentries);
2272     osi_auditU(rxcall, VLLinkedListNEvent, code, AUD_END);
2273     return code;
2274 }
2275
2276 /* Get back vldb header statistics (allocs, frees, maxvolumeid,
2277  * totalentries, etc) and dynamic statistics (number of requests and/or
2278  * aborts per remote procedure call, etc)
2279  */
2280 static afs_int32
2281 GetStats(struct rx_call *rxcall,
2282          vldstats *stats,
2283          vital_vlheader *vital_header)
2284 {
2285     int this_op = VLGETSTATS;
2286     afs_int32 code;
2287     struct vl_ctx ctx;
2288     char rxstr[AFS_RXINFO_LEN];
2289
2290     countRequest(this_op);
2291
2292     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
2293                                       restrictedQueryLevel))
2294         return VL_PERM;
2295
2296     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2297         return code;
2298     VLog(5, ("GetStats %s\n", rxinfo(rxstr, rxcall)));
2299     memcpy((char *)vital_header, (char *)&ctx.cheader->vital_header,
2300            sizeof(vital_vlheader));
2301     memcpy((char *)stats, (char *)&dynamic_statistics, sizeof(vldstats));
2302     return ubik_EndTrans(ctx.trans);
2303 }
2304
2305 afs_int32
2306 SVL_GetStats(struct rx_call *rxcall,
2307              vldstats *stats,
2308              vital_vlheader *vital_header)
2309 {
2310     afs_int32 code;
2311
2312     code = GetStats(rxcall, stats, vital_header);
2313     osi_auditU(rxcall, VLGetStatsEvent, code, AUD_END);
2314     return code;
2315 }
2316
2317 /* Get the list of file server addresses from the VLDB.  Currently it's pretty
2318  * easy to do.  In the future, it might require a little bit of grunging
2319  * through the VLDB, but that's life.
2320  */
2321 afs_int32
2322 SVL_GetAddrs(struct rx_call *rxcall,
2323              afs_int32 Handle,
2324              afs_int32 spare2,
2325              struct VLCallBack *spare3,
2326              afs_int32 *nentries,
2327              bulkaddrs *addrsp)
2328 {
2329     int this_op = VLGETADDRS;
2330     afs_int32 code;
2331     struct vl_ctx ctx;
2332     int nservers, i;
2333     afs_uint32 *taddrp;
2334
2335     countRequest(this_op);
2336     addrsp->bulkaddrs_len = *nentries = 0;
2337     addrsp->bulkaddrs_val = 0;
2338     memset(spare3, 0, sizeof(struct VLCallBack));
2339
2340     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2341         return code;
2342
2343     VLog(5, ("GetAddrs\n"));
2344     addrsp->bulkaddrs_val = taddrp =
2345         malloc(sizeof(afs_uint32) * (MAXSERVERID + 1));
2346     nservers = *nentries = addrsp->bulkaddrs_len = 0;
2347
2348     if (!taddrp) {
2349         code = VL_NOMEM;
2350         goto abort;
2351     }
2352
2353     for (i = 0; i <= MAXSERVERID; i++) {
2354         if ((*taddrp = ntohl(ctx.cheader->IpMappedAddr[i]))) {
2355             taddrp++;
2356             nservers++;
2357         }
2358     }
2359
2360     addrsp->bulkaddrs_len = *nentries = nservers;
2361     return (ubik_EndTrans(ctx.trans));
2362
2363 abort:
2364     countAbort(this_op);
2365     ubik_AbortTrans(ctx.trans);
2366     return code;
2367 }
2368
2369 static_inline void
2370 append_addr(char *buffer, afs_uint32 addr, size_t buffer_size)
2371 {
2372     int n = strlen(buffer);
2373     if (buffer_size > n) {
2374         snprintf(buffer + n, buffer_size - n, "%u.%u.%u.%u",
2375                  (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
2376                  addr & 0xff);
2377     }
2378 }
2379
2380 afs_int32
2381 SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
2382                   bulkaddrs *addrsp)
2383 {
2384     int this_op = VLREGADDR;
2385     afs_int32 code;
2386     struct vl_ctx ctx;
2387     int cnt, h, i, j, k, m;
2388     struct extentaddr *exp = 0, *tex;
2389     char addrbuf[256];
2390     afsUUID tuuid;
2391     afs_uint32 addrs[VL_MAXIPADDRS_PERMH];
2392     int base;
2393     int count, willChangeEntry, foundUuidEntry, willReplaceCnt;
2394     int WillReplaceEntry, WillChange[MAXSERVERID + 1];
2395     int FoundUuid = 0;
2396     int ReplaceEntry = 0;
2397     int srvidx, mhidx;
2398
2399     countRequest(this_op);
2400     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
2401         return (VL_PERM);
2402     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
2403         return code;
2404
2405     /* Eliminate duplicates from IP address list */
2406     for (k = 0, cnt = 0; k < addrsp->bulkaddrs_len; k++) {
2407         if (addrsp->bulkaddrs_val[k] == 0)
2408             continue;
2409         for (m = 0; m < cnt; m++) {
2410             if (addrs[m] == addrsp->bulkaddrs_val[k])
2411                 break;
2412         }
2413         if (m == cnt) {
2414             if (m == VL_MAXIPADDRS_PERMH) {
2415                 VLog(0,
2416                      ("Number of addresses exceeds %d. Cannot register IP addr 0x%x in VLDB\n",
2417                       VL_MAXIPADDRS_PERMH, addrsp->bulkaddrs_val[k]));
2418             } else {
2419                 addrs[m] = addrsp->bulkaddrs_val[k];
2420                 cnt++;
2421             }
2422         }
2423     }
2424     if (cnt <= 0) {
2425         code = VL_INDEXERANGE;
2426         goto abort;
2427     }
2428
2429     count = 0;
2430     willReplaceCnt = 0;
2431     foundUuidEntry = 0;
2432     /* For each server registered within the VLDB */
2433     for (srvidx = 0; srvidx <= MAXSERVERID; srvidx++) {
2434         willChangeEntry = 0;
2435         WillReplaceEntry = 1;
2436         code = multiHomedExtent(&ctx, srvidx, &exp);
2437         if (code)
2438              continue;
2439
2440         if (exp) {
2441             /* See if the addresses to register will change this server entry */
2442             tuuid = exp->ex_hostuuid;
2443             afs_ntohuuid(&tuuid);
2444             if (afs_uuid_equal(uuidp, &tuuid)) {
2445                 foundUuidEntry = 1;
2446                 FoundUuid = srvidx;
2447             } else {
2448                 for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2449                     if (!exp->ex_addrs[mhidx])
2450                         continue;
2451                     for (k = 0; k < cnt; k++) {
2452                         if (ntohl(exp->ex_addrs[mhidx]) == addrs[k]) {
2453                             willChangeEntry = 1;
2454                             WillChange[count] = srvidx;
2455                             break;
2456                         }
2457                     }
2458                     if (k >= cnt)
2459                         WillReplaceEntry = 0;
2460                 }
2461             }
2462         } else {
2463             /* The server is not registered as a multihomed.
2464              * See if the addresses to register will replace this server entry.
2465              */
2466             for (k = 0; k < cnt; k++) {
2467                 if (ctx.hostaddress[srvidx] == addrs[k]) {
2468                     willChangeEntry = 1;
2469                     WillChange[count] = srvidx;
2470                     WillReplaceEntry = 1;
2471                     break;
2472                 }
2473             }
2474         }
2475         if (willChangeEntry) {
2476             if (WillReplaceEntry) {
2477                 willReplaceCnt++;
2478                 ReplaceEntry = srvidx;
2479             }
2480             count++;
2481         }
2482     }
2483
2484     /* If we found the uuid in the VLDB and if we are replacing another
2485      * entire entry, then complain and fail. Also, if we did not find
2486      * the uuid in the VLDB and the IP addresses being registered was
2487      * found in more than one other entry, then we don't know which one
2488      * to replace and will complain and fail.
2489      */
2490     if ((foundUuidEntry && (willReplaceCnt > 0))
2491         || (!foundUuidEntry && (count > 1))) {
2492         VLog(0,
2493              ("The following fileserver is being registered in the VLDB:\n"));
2494         for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
2495             if (k > 0)
2496                 strlcat(addrbuf, " ", sizeof(addrbuf));
2497             append_addr(addrbuf, addrs[k], sizeof(addrbuf));
2498         }
2499         VLog(0, ("      [%s]\n", addrbuf));
2500
2501         if (foundUuidEntry) {
2502             code = multiHomedExtent(&ctx, FoundUuid, &exp);
2503             if (code == 0) {
2504                 VLog(0, ("   It would have replaced the existing VLDB server "
2505                          "entry:\n"));
2506                 for (addrbuf[0] = '\0', mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2507                     if (!exp->ex_addrs[mhidx])
2508                         continue;
2509                     if (mhidx > 0)
2510                         strlcat(addrbuf, " ", sizeof(addrbuf));
2511                     append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
2512                 }
2513                 VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
2514             }
2515         }
2516
2517         if (count == 1)
2518             VLog(0, ("   Yet another VLDB server entry exists:\n"));
2519         else
2520             VLog(0, ("   Yet other VLDB server entries exist:\n"));
2521         for (j = 0; j < count; j++) {
2522             srvidx = WillChange[j];
2523             VLog(0, ("      entry %d: ", srvidx));
2524
2525             code = multiHomedExtent(&ctx, srvidx, &exp);
2526             if (code)
2527                 goto abort;
2528
2529             addrbuf[0] = '\0';
2530             if (exp) {
2531                 for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2532                     if (!exp->ex_addrs[mhidx])
2533                         continue;
2534                     if (mhidx > 0)
2535                         strlcat(addrbuf, " ", sizeof(addrbuf));
2536                     append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
2537                 }
2538             } else {
2539                 append_addr(addrbuf, ctx.hostaddress[srvidx], sizeof(addrbuf));
2540             }
2541             VLog(0, ("      entry %d: [%s]\n", srvidx, addrbuf));
2542         }
2543
2544         if (count == 1)
2545             VLog(0, ("   You must 'vos changeaddr' this other server entry\n"));
2546         else
2547             VLog(0,
2548                 ("   You must 'vos changeaddr' these other server entries\n"));
2549         if (foundUuidEntry)
2550             VLog(0,
2551                 ("   and/or remove the sysid file from the registering fileserver\n"));
2552         VLog(0, ("   before the fileserver can be registered in the VLDB.\n"));
2553
2554         code = VL_MULTIPADDR;
2555         goto abort;
2556     }
2557
2558     /* Passed the checks. Now find and update the existing mh entry, or create
2559      * a new mh entry.
2560      */
2561     if (foundUuidEntry) {
2562         /* Found the entry with same uuid. See if we need to change it */
2563         int change = 0;
2564
2565         code = multiHomedExtentBase(&ctx, FoundUuid, &exp, &base);
2566         if (code)
2567             goto abort;
2568
2569         /* Determine if the entry has changed */
2570         for (k = 0; ((k < cnt) && !change); k++) {
2571             if (ntohl(exp->ex_addrs[k]) != addrs[k])
2572                 change = 1;
2573         }
2574         for (; ((k < VL_MAXIPADDRS_PERMH) && !change); k++) {
2575             if (exp->ex_addrs[k] != 0)
2576                 change = 1;
2577         }
2578         if (!change) {
2579             return (ubik_EndTrans(ctx.trans));
2580         }
2581     }
2582
2583     VLog(0, ("The following fileserver is being registered in the VLDB:\n"));
2584     for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
2585         if (k > 0)
2586             strlcat(addrbuf, " ", sizeof(addrbuf));
2587         append_addr(addrbuf, addrs[k], sizeof(addrbuf));
2588     }
2589     VLog(0, ("      [%s]\n", addrbuf));
2590
2591     if (foundUuidEntry) {
2592         VLog(0,
2593             ("   It will replace the following existing entry in the VLDB (same uuid):\n"));
2594         for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
2595             if (exp->ex_addrs[k] == 0)
2596                 continue;
2597             if (k > 0)
2598                 strlcat(addrbuf, " ", sizeof(addrbuf));
2599             append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
2600         }
2601         VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
2602     } else if (willReplaceCnt || (count == 1)) {
2603         /* If we are not replacing an entry and there is only one entry to change,
2604          * then we will replace that entry.
2605          */
2606         if (!willReplaceCnt) {
2607             ReplaceEntry = WillChange[0];
2608             willReplaceCnt++;
2609         }
2610
2611         /* Have an entry that needs to be replaced */
2612         code = multiHomedExtentBase(&ctx, ReplaceEntry, &exp, &base);
2613         if (code)
2614             goto abort;
2615
2616         if (exp) {
2617             VLog(0,
2618                 ("   It will replace the following existing entry in the VLDB (new uuid):\n"));
2619             for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
2620                 if (exp->ex_addrs[k] == 0)
2621                     continue;
2622                 if (k > 0)
2623                     strlcat(addrbuf, " ", sizeof(addrbuf));
2624                 append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
2625             }
2626             VLog(0, ("      entry %d: [%s]\n", ReplaceEntry, addrbuf));
2627         } else {
2628             /* Not a mh entry. So we have to create a new mh entry and
2629              * put it on the ReplaceEntry slot of the ctx.hostaddress array.
2630              */
2631             addrbuf[0] = '\0';
2632             append_addr(addrbuf, ctx.hostaddress[ReplaceEntry], sizeof(addrbuf));
2633             VLog(0, ("   It will replace existing entry %d, %s,"
2634                      " in the VLDB (new uuid):\n", ReplaceEntry, addrbuf));
2635             code =
2636                 FindExtentBlock(&ctx, uuidp, 1, ReplaceEntry, &exp, &base);
2637             if (code || !exp) {
2638                 if (!code)
2639                     code = VL_IO;
2640                 goto abort;
2641             }
2642         }
2643     } else {
2644         /* There is no entry for this server, must create a new mh entry as
2645          * well as use a new slot of the ctx.hostaddress array.
2646          */
2647         VLog(0, ("   It will create a new entry in the VLDB.\n"));
2648         code = FindExtentBlock(&ctx, uuidp, 1, -1, &exp, &base);
2649         if (code || !exp) {
2650             if (!code)
2651                 code = VL_IO;
2652             goto abort;
2653         }
2654     }
2655
2656     /* Now we have a mh entry to fill in. Update the uuid, bump the
2657      * uniquifier, and fill in its IP addresses.
2658      */
2659     tuuid = *uuidp;
2660     afs_htonuuid(&tuuid);
2661     exp->ex_hostuuid = tuuid;
2662     exp->ex_uniquifier = htonl(ntohl(exp->ex_uniquifier) + 1);
2663     for (k = 0; k < cnt; k++) {
2664         exp->ex_addrs[k] = htonl(addrs[k]);
2665     }
2666     for (; k < VL_MAXIPADDRS_PERMH; k++) {
2667         exp->ex_addrs[k] = 0;
2668     }
2669
2670     /* Write the new mh entry out */
2671     if (vlwrite
2672         (ctx.trans,
2673          DOFFSET(ntohl(ctx.ex_addr[0]->ex_contaddrs[base]),
2674                  (char *)ctx.ex_addr[base], (char *)exp), (char *)exp,
2675          sizeof(*exp))) {
2676         code = VL_IO;
2677         goto abort;
2678     }
2679
2680     /* Remove any common addresses from other mh entres. We know these entries
2681      * are being changed and not replaced so they are mh entries.
2682      */
2683     m = 0;
2684     for (i = 0; i < count; i++) {
2685         afs_int32 doff;
2686
2687         /* Skip the entry we replaced */
2688         if (willReplaceCnt && (WillChange[i] == ReplaceEntry))
2689             continue;
2690
2691         code = multiHomedExtentBase(&ctx, WillChange[i], &tex, &base);
2692         if (code)
2693             goto abort;
2694
2695         if (++m == 1)
2696             VLog(0,
2697                 ("   The following existing entries in the VLDB will be updated:\n"));
2698
2699         for (addrbuf[0] = '\0', h = j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2700             if (tex->ex_addrs[j]) {
2701                 if (j > 0)
2702                     strlcat(addrbuf, " ", sizeof(addrbuf));
2703                 append_addr(addrbuf, ntohl(tex->ex_addrs[j]), sizeof(addrbuf));
2704             }
2705
2706             for (k = 0; k < cnt; k++) {
2707                 if (ntohl(tex->ex_addrs[j]) == addrs[k])
2708                     break;
2709             }
2710             if (k >= cnt) {
2711                 /* Not found, so we keep it */
2712                 tex->ex_addrs[h] = tex->ex_addrs[j];
2713                 h++;
2714             }
2715         }
2716         for (j = h; j < VL_MAXIPADDRS_PERMH; j++) {
2717             tex->ex_addrs[j] = 0;       /* zero rest of mh entry */
2718         }
2719         VLog(0, ("      entry %d: [%s]\n", WillChange[i], addrbuf));
2720
2721         /* Write out the modified mh entry */
2722         tex->ex_uniquifier = htonl(ntohl(tex->ex_uniquifier) + 1);
2723         doff =
2724             DOFFSET(ntohl(ctx.ex_addr[0]->ex_contaddrs[base]),
2725                     (char *)ctx.ex_addr[base], (char *)tex);
2726         if (vlwrite(ctx.trans, doff, (char *)tex, sizeof(*tex))) {
2727             code = VL_IO;
2728             goto abort;
2729         }
2730     }
2731
2732     return (ubik_EndTrans(ctx.trans));
2733
2734 abort:
2735     countAbort(this_op);
2736     ubik_AbortTrans(ctx.trans);
2737     return code;
2738 }
2739
2740 afs_int32
2741 SVL_GetAddrsU(struct rx_call *rxcall,
2742               struct ListAddrByAttributes *attributes,
2743               afsUUID *uuidpo,
2744               afs_int32 *uniquifier,
2745               afs_int32 *nentries,
2746               bulkaddrs *addrsp)
2747 {
2748     int this_op = VLGETADDRSU;
2749     afs_int32 code, index;
2750     struct vl_ctx ctx;
2751     int nservers, i, j, base = 0;
2752     struct extentaddr *exp = 0;
2753     afsUUID tuuid;
2754     afs_uint32 *taddrp, taddr;
2755     char rxstr[AFS_RXINFO_LEN];
2756
2757     countRequest(this_op);
2758     addrsp->bulkaddrs_len = *nentries = 0;
2759     addrsp->bulkaddrs_val = 0;
2760     VLog(5, ("GetAddrsU %s\n", rxinfo(rxstr, rxcall)));
2761     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2762         return code;
2763
2764     if (attributes->Mask & VLADDR_IPADDR) {
2765         if (attributes->Mask & (VLADDR_INDEX | VLADDR_UUID)) {
2766             code = VL_BADMASK;
2767             goto abort;
2768         }
2769         /* Search for a server registered with the VLDB with this ip address. */
2770         for (index = 0; index <= MAXSERVERID; index++) {
2771             code = multiHomedExtent(&ctx, index, &exp);
2772             if (code)
2773                 continue;
2774
2775             if (exp) {
2776                 for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2777                     if (exp->ex_addrs[j]
2778                         && (ntohl(exp->ex_addrs[j]) == attributes->ipaddr)) {
2779                         break;
2780                     }
2781                 }
2782                 if (j < VL_MAXIPADDRS_PERMH)
2783                     break;
2784             }
2785         }
2786         if (index > MAXSERVERID) {
2787             code = VL_NOENT;
2788             goto abort;
2789         }
2790     } else if (attributes->Mask & VLADDR_INDEX) {
2791         if (attributes->Mask & (VLADDR_IPADDR | VLADDR_UUID)) {
2792             code = VL_BADMASK;
2793             goto abort;
2794         }
2795         /* VLADDR_INDEX index is one based */
2796         if (attributes->index < 1 || attributes->index > MAXSERVERID) {
2797             code = VL_INDEXERANGE;
2798             goto abort;
2799         }
2800         index = attributes->index - 1;
2801         code = multiHomedExtent(&ctx, index, &exp);
2802         if (code) {
2803             code = VL_NOENT;
2804             goto abort;
2805         }
2806     } else if (attributes->Mask & VLADDR_UUID) {
2807         if (attributes->Mask & (VLADDR_IPADDR | VLADDR_INDEX)) {
2808             code = VL_BADMASK;
2809             goto abort;
2810         }
2811         if (!ctx.ex_addr[0]) {  /* mh servers probably aren't setup on this vldb */
2812             code = VL_NOENT;
2813             goto abort;
2814         }
2815         code = FindExtentBlock(&ctx, &attributes->uuid, 0, -1, &exp, &base);
2816         if (code)
2817             goto abort;
2818     } else {
2819         code = VL_BADMASK;
2820         goto abort;
2821     }
2822
2823     if (exp == NULL) {
2824         code = VL_NOENT;
2825         goto abort;
2826     }
2827     addrsp->bulkaddrs_val = taddrp =
2828         malloc(sizeof(afs_uint32) * (MAXSERVERID + 1));
2829     nservers = *nentries = addrsp->bulkaddrs_len = 0;
2830     if (!taddrp) {
2831         code = VL_NOMEM;
2832         goto abort;
2833     }
2834     tuuid = exp->ex_hostuuid;
2835     afs_ntohuuid(&tuuid);
2836     if (afs_uuid_is_nil(&tuuid)) {
2837         code = VL_NOENT;
2838         goto abort;
2839     }
2840     if (uuidpo)
2841         *uuidpo = tuuid;
2842     if (uniquifier)
2843         *uniquifier = ntohl(exp->ex_uniquifier);
2844     for (i = 0; i < VL_MAXIPADDRS_PERMH; i++) {
2845         if (exp->ex_addrs[i]) {
2846             taddr = ntohl(exp->ex_addrs[i]);
2847             /* Weed out duplicates */
2848             for (j = 0; j < nservers; j++) {
2849                 if (taddrp[j] == taddr)
2850                     break;
2851             }
2852             if ((j == nservers) && (j <= MAXSERVERID)) {
2853                 taddrp[nservers] = taddr;
2854                 nservers++;
2855             }
2856         }
2857     }
2858     addrsp->bulkaddrs_len = *nentries = nservers;
2859     return (ubik_EndTrans(ctx.trans));
2860
2861 abort:
2862     countAbort(this_op);
2863     ubik_AbortTrans(ctx.trans);
2864     return code;
2865 }
2866
2867 /* ============> End of Exported vldb RPC functions <============= */
2868
2869
2870 /* Routine that copies the given vldb entry to the output buffer, vldbentries. */
2871 static int
2872 put_attributeentry(struct vl_ctx *ctx,
2873                    struct vldbentry **Vldbentry,
2874                    struct vldbentry **VldbentryFirst,
2875                    struct vldbentry **VldbentryLast,
2876                    bulkentries *vldbentries,
2877                    struct nvlentry *entry,
2878                    afs_int32 *nentries,
2879                    afs_int32 *alloccnt)
2880 {
2881     vldbentry *reall;
2882     afs_int32 allo;
2883     int code;
2884
2885     if (*Vldbentry == *VldbentryLast) {
2886         if (smallMem)
2887             return VL_SIZEEXCEEDED;     /* no growing if smallMem defined */
2888
2889         /* Allocate another set of memory; each time allocate twice as
2890          * many blocks as the last time. When we reach VLDBALLOCLIMIT,
2891          * then grow in increments of VLDBALLOCINCR.
2892          */
2893         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2894         reall = realloc(*VldbentryFirst,
2895                         (*alloccnt + allo) * sizeof(vldbentry));
2896         if (reall == NULL)
2897             return VL_NOMEM;
2898
2899         *VldbentryFirst = vldbentries->bulkentries_val = reall;
2900         *Vldbentry = *VldbentryFirst + *alloccnt;
2901         *VldbentryLast = *Vldbentry + allo;
2902         *alloccnt += allo;
2903     }
2904
2905     code = vlentry_to_vldbentry(ctx, entry, *Vldbentry);
2906     if (code)
2907         return code;
2908
2909     (*Vldbentry)++;
2910     (*nentries)++;
2911     vldbentries->bulkentries_len++;
2912     return 0;
2913 }
2914
2915 static int
2916 put_nattributeentry(struct vl_ctx *ctx,
2917                     struct nvldbentry **Vldbentry,
2918                     struct nvldbentry **VldbentryFirst,
2919                     struct nvldbentry **VldbentryLast,
2920                     nbulkentries *vldbentries,
2921                     struct nvlentry *entry,
2922                     afs_int32 matchtype,
2923                     afs_int32 matchindex,
2924                     afs_int32 *nentries,
2925                     afs_int32 *alloccnt)
2926 {
2927     nvldbentry *reall;
2928     afs_int32 allo;
2929     int code;
2930
2931     if (*Vldbentry == *VldbentryLast) {
2932         if (smallMem)
2933             return VL_SIZEEXCEEDED;     /* no growing if smallMem defined */
2934
2935         /* Allocate another set of memory; each time allocate twice as
2936          * many blocks as the last time. When we reach VLDBALLOCLIMIT,
2937          * then grow in increments of VLDBALLOCINCR.
2938          */
2939         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2940         reall = realloc(*VldbentryFirst,
2941                         (*alloccnt + allo) * sizeof(nvldbentry));
2942         if (reall == NULL)
2943             return VL_NOMEM;
2944
2945         *VldbentryFirst = vldbentries->nbulkentries_val = reall;
2946         *Vldbentry = *VldbentryFirst + *alloccnt;
2947         *VldbentryLast = *Vldbentry + allo;
2948         *alloccnt += allo;
2949     }
2950     code = vlentry_to_nvldbentry(ctx, entry, *Vldbentry);
2951     if (code)
2952         return code;
2953
2954     (*Vldbentry)->matchindex = (matchtype << 16) + matchindex;
2955     (*Vldbentry)++;
2956     (*nentries)++;
2957     vldbentries->nbulkentries_len++;
2958     return 0;
2959 }
2960
2961
2962 /* Common code to actually remove a vldb entry from the database. */
2963 static int
2964 RemoveEntry(struct vl_ctx *ctx, afs_int32 entryptr,
2965             struct nvlentry *tentry)
2966 {
2967     int code;
2968
2969     if ((code = UnthreadVLentry(ctx, entryptr, tentry)))
2970         return code;
2971     if ((code = FreeBlock(ctx, entryptr)))
2972         return code;
2973     return 0;
2974 }
2975
2976 static void
2977 ReleaseEntry(struct nvlentry *tentry, afs_int32 releasetype)
2978 {
2979     if (releasetype & LOCKREL_TIMESTAMP)
2980         tentry->LockTimestamp = 0;
2981     if (releasetype & LOCKREL_OPCODE)
2982         tentry->flags &= ~VLOP_ALLOPERS;
2983     if (releasetype & LOCKREL_AFSID)
2984         tentry->LockAfsId = 0;
2985 }
2986
2987
2988 /* Verify that the incoming vldb entry is valid; multi type of error codes
2989  * are returned. */
2990 static int
2991 check_vldbentry(struct vldbentry *aentry)
2992 {
2993     afs_int32 i;
2994
2995     if (InvalidVolname(aentry->name))
2996         return VL_BADNAME;
2997     if (aentry->nServers <= 0 || aentry->nServers > OMAXNSERVERS)
2998         return VL_BADSERVER;
2999     for (i = 0; i < aentry->nServers; i++) {
3000 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
3001             return VL_BADSERVER;        */
3002         if (aentry->serverPartition[i] < 0
3003             || aentry->serverPartition[i] > MAXPARTITIONID)
3004             return VL_BADPARTITION;
3005         if (aentry->serverFlags[i] < 0
3006             || aentry->serverFlags[i] > MAXSERVERFLAG)
3007             return VL_BADSERVERFLAG;
3008     }
3009     return 0;
3010 }
3011
3012 static int
3013 check_nvldbentry(struct nvldbentry *aentry)
3014 {
3015     afs_int32 i;
3016
3017     if (InvalidVolname(aentry->name))
3018         return VL_BADNAME;
3019     if (aentry->nServers <= 0 || aentry->nServers > NMAXNSERVERS)
3020         return VL_BADSERVER;
3021     for (i = 0; i < aentry->nServers; i++) {
3022 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
3023             return VL_BADSERVER;        */
3024         if (aentry->serverPartition[i] < 0
3025             || aentry->serverPartition[i] > MAXPARTITIONID)
3026             return VL_BADPARTITION;
3027         if (aentry->serverFlags[i] < 0
3028             || aentry->serverFlags[i] > MAXSERVERFLAG)
3029             return VL_BADSERVERFLAG;
3030     }
3031     return 0;
3032 }
3033
3034
3035 /* Convert from the external vldb entry representation to its internal
3036    (more compact) form.  This call should not change the hash chains! */
3037 static int
3038 vldbentry_to_vlentry(struct vl_ctx *ctx,
3039                      struct vldbentry *VldbEntry,
3040                      struct nvlentry *VlEntry)
3041 {
3042     int i, serverindex;
3043
3044     if (strcmp(VlEntry->name, VldbEntry->name))
3045         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
3046     for (i = 0; i < VldbEntry->nServers; i++) {
3047         serverindex = IpAddrToRelAddr(ctx, VldbEntry->serverNumber[i], 1);
3048         if (serverindex == -1)
3049             return VL_BADSERVER;
3050         VlEntry->serverNumber[i] = serverindex;
3051         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
3052         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
3053     }
3054     for (; i < OMAXNSERVERS; i++)
3055         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] =
3056             VlEntry->serverFlags[i] = BADSERVERID;
3057     for (i = 0; i < MAXTYPES; i++)
3058         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
3059     VlEntry->cloneId = VldbEntry->cloneId;
3060     VlEntry->flags = VldbEntry->flags;
3061     return 0;
3062 }
3063
3064 static int
3065 nvldbentry_to_vlentry(struct vl_ctx *ctx,
3066                       struct nvldbentry *VldbEntry,
3067                       struct nvlentry *VlEntry)
3068 {
3069     int i, serverindex;
3070
3071     if (strcmp(VlEntry->name, VldbEntry->name))
3072         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
3073     for (i = 0; i < VldbEntry->nServers; i++) {
3074         serverindex = IpAddrToRelAddr(ctx, VldbEntry->serverNumber[i], 1);
3075         if (serverindex == -1)
3076             return VL_BADSERVER;
3077         VlEntry->serverNumber[i] = serverindex;
3078         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
3079         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
3080     }
3081     for (; i < NMAXNSERVERS; i++)
3082         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] =
3083             VlEntry->serverFlags[i] = BADSERVERID;
3084     for (i = 0; i < MAXTYPES; i++)
3085         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
3086     VlEntry->cloneId = VldbEntry->cloneId;
3087     VlEntry->flags = VldbEntry->flags;
3088     return 0;
3089 }
3090
3091
3092 /* Update the vldb entry with the new fields as indicated by the value of
3093  * the Mask entry in the updateentry structure. All necessary validation
3094  * checks are performed.
3095  */
3096 static int
3097 get_vldbupdateentry(struct vl_ctx *ctx,
3098                     afs_int32 blockindex,
3099                     struct VldbUpdateEntry *updateentry,
3100                     struct nvlentry *VlEntry)
3101 {
3102     int i, j, code, serverindex;
3103     afs_uint32 checkids[MAXTYPES];
3104
3105     /* check if any specified new IDs are already present in the db. Do
3106      * this check before doing anything else, so we don't get a half-
3107      * updated entry. */
3108     memset(&checkids, 0, sizeof(checkids));
3109     if (updateentry->Mask & VLUPDATE_RWID) {
3110         checkids[RWVOL] = updateentry->spares3; /* rw id */
3111     }
3112     if (updateentry->Mask & VLUPDATE_READONLYID) {
3113         checkids[ROVOL] = updateentry->ReadOnlyId;
3114     }
3115     if (updateentry->Mask & VLUPDATE_BACKUPID) {
3116         checkids[BACKVOL] = updateentry->BackupId;
3117     }
3118
3119     if (EntryIDExists(ctx, checkids, MAXTYPES, &code)) {
3120         return VL_IDEXIST;
3121     } else if (code) {
3122         return code;
3123     }
3124
3125     if (updateentry->Mask & VLUPDATE_VOLUMENAME) {
3126         struct nvlentry tentry;
3127
3128         if (InvalidVolname(updateentry->name))
3129             return VL_BADNAME;
3130
3131         if (FindByName(ctx, updateentry->name, &tentry, &code)) {
3132             return VL_NAMEEXIST;
3133         } else if (code) {
3134             return code;
3135         }
3136
3137         if ((code = UnhashVolname(ctx, blockindex, VlEntry)))
3138             return code;
3139         strncpy(VlEntry->name, updateentry->name, sizeof(VlEntry->name));
3140         HashVolname(ctx, blockindex, VlEntry);
3141     }
3142
3143     if (updateentry->Mask & VLUPDATE_VOLNAMEHASH) {
3144         if ((code = UnhashVolname(ctx, blockindex, VlEntry))) {
3145             if (code != VL_NOENT)
3146                 return code;
3147         }
3148         HashVolname(ctx, blockindex, VlEntry);
3149     }
3150
3151     if (updateentry->Mask & VLUPDATE_FLAGS) {
3152         VlEntry->flags = updateentry->flags;
3153     }
3154     if (updateentry->Mask & VLUPDATE_CLONEID) {
3155         VlEntry->cloneId = updateentry->cloneId;
3156     }
3157     if (updateentry->Mask & VLUPDATE_RWID) {
3158         if ((code = UnhashVolid(ctx, RWVOL, blockindex, VlEntry))) {
3159             if (code != VL_NOENT)
3160                 return code;
3161         }
3162         VlEntry->volumeId[RWVOL] = updateentry->spares3;        /* rw id */
3163         if ((code = HashVolid(ctx, RWVOL, blockindex, VlEntry)))
3164             return code;
3165     }
3166     if (updateentry->Mask & VLUPDATE_READONLYID) {
3167         if ((code = UnhashVolid(ctx, ROVOL, blockindex, VlEntry))) {
3168             if (code != VL_NOENT)
3169                 return code;
3170         }
3171         VlEntry->volumeId[ROVOL] = updateentry->ReadOnlyId;
3172         if ((code = HashVolid(ctx, ROVOL, blockindex, VlEntry)))
3173             return code;
3174     }
3175     if (updateentry->Mask & VLUPDATE_BACKUPID) {
3176         if ((code = UnhashVolid(ctx, BACKVOL, blockindex, VlEntry))) {
3177             if (code != VL_NOENT)
3178                 return code;
3179         }
3180         VlEntry->volumeId[BACKVOL] = updateentry->BackupId;
3181         if ((code = HashVolid(ctx, BACKVOL, blockindex, VlEntry)))
3182             return code;
3183     }
3184     if (updateentry->Mask & VLUPDATE_REPSITES) {
3185         if (updateentry->nModifiedRepsites <= 0
3186             || updateentry->nModifiedRepsites > OMAXNSERVERS)
3187             return VL_BADSERVER;
3188         for (i = 0; i < updateentry->nModifiedRepsites; i++) {
3189 /*          if (updateentry->RepsitesTargetServer[i] < 0 || updateentry->RepsitesTargetServer[i] > MAXSERVERID)
3190                 return VL_BADSERVER;    */
3191             if (updateentry->RepsitesTargetPart[i] < 0
3192                 || updateentry->RepsitesTargetPart[i] > MAXPARTITIONID)
3193                 return VL_BADPARTITION;
3194             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_DELETE) {
3195                 if ((j =
3196                      repsite_exists(VlEntry,
3197                                     IpAddrToRelAddr(ctx, updateentry->
3198                                                     RepsitesTargetServer[i],
3199                                                     1),
3200                                     updateentry->RepsitesTargetPart[i])) !=
3201                     -1)
3202                     repsite_compress(VlEntry, j);
3203                 else
3204                     return VL_NOREPSERVER;
3205             }
3206             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_ADD) {
3207 /*              if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
3208                     return VL_BADSERVER;                */
3209                 if (updateentry->RepsitesNewPart[i] < 0
3210                     || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
3211                     return VL_BADPARTITION;
3212                 if (repsite_exists
3213                     (VlEntry,
3214                      IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i], 1),
3215                      updateentry->RepsitesNewPart[i]) != -1)
3216                     return VL_DUPREPSERVER;
3217                 for (j = 0;
3218                      VlEntry->serverNumber[j] != BADSERVERID
3219                      && j < OMAXNSERVERS; j++);
3220                 if (j >= OMAXNSERVERS)
3221                     return VL_REPSFULL;
3222                 if ((serverindex =
3223                      IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i],
3224                                      1)) == -1)
3225                     return VL_BADSERVER;
3226                 VlEntry->serverNumber[j] = serverindex;
3227                 VlEntry->serverPartition[j] = updateentry->RepsitesNewPart[i];
3228                 if (updateentry->RepsitesNewFlags[i] < 0
3229                     || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
3230                     return VL_BADSERVERFLAG;
3231                 VlEntry->serverFlags[j] = updateentry->RepsitesNewFlags[i];
3232             }
3233             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODSERV) {
3234 /*n             if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
3235                     return VL_BADSERVER;            */
3236                 if ((j =
3237                      repsite_exists(VlEntry,
3238                                     IpAddrToRelAddr(ctx, updateentry->
3239                                                     RepsitesTargetServer[i],
3240                                                     1),
3241                                     updateentry->RepsitesTargetPart[i])) !=
3242                     -1) {
3243                     VlEntry->serverNumber[j] =
3244                         IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i],
3245                                         1);
3246                 } else
3247                     return VL_NOREPSERVER;
3248             }
3249             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODPART) {
3250                 if (updateentry->RepsitesNewPart[i] < 0
3251                     || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
3252                     return VL_BADPARTITION;
3253                 if ((j =
3254                      repsite_exists(VlEntry,
3255                                     IpAddrToRelAddr(ctx, updateentry->
3256                                                     RepsitesTargetServer[i],
3257                                                     1),
3258                                     updateentry->RepsitesTargetPart[i])) !=
3259                     -1)
3260                     VlEntry->serverPartition[j] =
3261                         updateentry->RepsitesNewPart[i];
3262                 else
3263                     return VL_NOREPSERVER;
3264             }
3265             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODFLAG) {
3266                 if ((j =
3267                      repsite_exists(VlEntry,
3268                                     IpAddrToRelAddr(ctx, updateentry->
3269                                                     RepsitesTargetServer[i],
3270                                                     1),
3271                                     updateentry->RepsitesTargetPart[i])) !=
3272                     -1) {
3273                     if (updateentry->RepsitesNewFlags[i] < 0
3274                         || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
3275                         return VL_BADSERVERFLAG;
3276                     VlEntry->serverFlags[j] =
3277                         updateentry->RepsitesNewFlags[i];
3278                 } else
3279                     return VL_NOREPSERVER;
3280             }
3281         }
3282     }
3283     return 0;
3284 }
3285
3286
3287 /* Check if the specified [server,partition] entry is found in the vldb
3288  * entry's repsite table; it's offset in the table is returned, if it's
3289  * present there. */
3290 static int
3291 repsite_exists(struct nvlentry *VlEntry, int server, int partition)
3292 {
3293     int i;
3294
3295     for (i = 0; VlEntry->serverNumber[i] != BADSERVERID && i < OMAXNSERVERS;
3296          i++) {
3297         if ((VlEntry->serverNumber[i] == server)
3298             && (VlEntry->serverPartition[i] == partition))
3299             return i;
3300     }
3301     return -1;
3302 }
3303
3304
3305
3306 /* Repsite table compression: used when deleting a repsite entry so that
3307  * all active repsite entries are on the top of the table. */
3308 static void
3309 repsite_compress(struct nvlentry *VlEntry, int offset)
3310 {
3311     int repsite_offset = offset;
3312     for (;
3313          VlEntry->serverNumber[repsite_offset] != BADSERVERID
3314          && repsite_offset < OMAXNSERVERS - 1; repsite_offset++) {
3315         VlEntry->serverNumber[repsite_offset] =
3316             VlEntry->serverNumber[repsite_offset + 1];
3317         VlEntry->serverPartition[repsite_offset] =
3318             VlEntry->serverPartition[repsite_offset + 1];
3319         VlEntry->serverFlags[repsite_offset] =
3320             VlEntry->serverFlags[repsite_offset + 1];
3321     }
3322     VlEntry->serverNumber[repsite_offset] = BADSERVERID;
3323 }
3324
3325
3326 /* Convert from the internal (compacted) vldb entry to the external
3327  * representation used by the interface. */
3328 static int
3329 vlentry_to_vldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3330                      struct vldbentry *VldbEntry)
3331 {
3332     int i, j, code;
3333     struct extentaddr *exp;
3334
3335     memset(VldbEntry, 0, sizeof(struct vldbentry));
3336     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3337     for (i = 0; i < OMAXNSERVERS; i++) {
3338         if (VlEntry->serverNumber[i] == BADSERVERID)
3339             break;
3340         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3341         if (code)
3342             return code;
3343         if (exp) {
3344             /* For now return the first ip address back */
3345             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3346                 if (exp->ex_addrs[j]) {
3347                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
3348                     break;
3349                 }
3350             }
3351         } else
3352             VldbEntry->serverNumber[i] =
3353                 ctx->hostaddress[VlEntry->serverNumber[i]];
3354         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3355         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3356     }
3357     VldbEntry->nServers = i;
3358     for (i = 0; i < MAXTYPES; i++)
3359         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3360     VldbEntry->cloneId = VlEntry->cloneId;
3361     VldbEntry->flags = VlEntry->flags;
3362
3363     return 0;
3364 }
3365
3366
3367 /* Convert from the internal (compacted) vldb entry to the external
3368  * representation used by the interface. */
3369 static int
3370 vlentry_to_nvldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3371                       struct nvldbentry *VldbEntry)
3372 {
3373     int i, j, code;
3374     struct extentaddr *exp;
3375
3376     memset(VldbEntry, 0, sizeof(struct nvldbentry));
3377     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3378     for (i = 0; i < NMAXNSERVERS; i++) {
3379         if (VlEntry->serverNumber[i] == BADSERVERID)
3380             break;
3381         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3382         if (code)
3383             return code;
3384
3385         if (exp) {
3386             /* For now return the first ip address back */
3387             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3388                 if (exp->ex_addrs[j]) {
3389                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
3390                     break;
3391                 }
3392             }
3393         } else
3394             VldbEntry->serverNumber[i] =
3395                 ctx->hostaddress[VlEntry->serverNumber[i]];
3396         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3397         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3398     }
3399     VldbEntry->nServers = i;
3400     for (i = 0; i < MAXTYPES; i++)
3401         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3402     VldbEntry->cloneId = VlEntry->cloneId;
3403     VldbEntry->flags = VlEntry->flags;
3404
3405     return 0;
3406 }
3407
3408 static int
3409 vlentry_to_uvldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3410                       struct uvldbentry *VldbEntry)
3411 {
3412     int i, code;
3413     struct extentaddr *exp;
3414
3415     memset(VldbEntry, 0, sizeof(struct uvldbentry));
3416     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3417     for (i = 0; i < NMAXNSERVERS; i++) {
3418         if (VlEntry->serverNumber[i] == BADSERVERID)
3419             break;
3420         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3421         VldbEntry->serverUnique[i] = 0;
3422         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3423         if (code)
3424             return code;
3425
3426         if (exp) {
3427             afsUUID tuuid;
3428
3429             tuuid = exp->ex_hostuuid;
3430             afs_ntohuuid(&tuuid);
3431             VldbEntry->serverFlags[i] |= VLSF_UUID;
3432             VldbEntry->serverNumber[i] = tuuid;
3433             VldbEntry->serverUnique[i] = ntohl(exp->ex_uniquifier);
3434         } else {
3435             VldbEntry->serverNumber[i].time_low =
3436                 ctx->hostaddress[VlEntry->serverNumber[i]];
3437         }
3438         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3439
3440     }
3441     VldbEntry->nServers = i;
3442     for (i = 0; i < MAXTYPES; i++)
3443         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3444     VldbEntry->cloneId = VlEntry->cloneId;
3445     VldbEntry->flags = VlEntry->flags;
3446
3447     return 0;
3448 }
3449
3450 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
3451
3452
3453 /* Verify that the volname is a valid volume name. */
3454 static int
3455 InvalidVolname(char *volname)
3456 {
3457     char *map;
3458     int slen;
3459
3460     map = LEGALCHARS;
3461     slen = strlen(volname);
3462     if (slen >= VL_MAXNAMELEN)
3463         return 1;
3464     return (slen != strspn(volname, map));
3465 }
3466
3467
3468 /* Verify that the given volume type is valid. */
3469 static int
3470 InvalidVoltype(afs_int32 voltype)
3471 {
3472     if (voltype != RWVOL && voltype != ROVOL && voltype != BACKVOL)
3473         return 1;
3474     return 0;
3475 }
3476
3477
3478 static int
3479 InvalidOperation(afs_int32 voloper)
3480 {
3481     if (voloper != VLOP_MOVE && voloper != VLOP_RELEASE
3482         && voloper != VLOP_BACKUP && voloper != VLOP_DELETE
3483         && voloper != VLOP_DUMP)
3484         return 1;
3485     return 0;
3486 }
3487
3488 static int
3489 InvalidReleasetype(afs_int32 releasetype)
3490 {
3491     if ((releasetype & LOCKREL_TIMESTAMP) || (releasetype & LOCKREL_OPCODE)
3492         || (releasetype & LOCKREL_AFSID))
3493         return 0;
3494     return 1;
3495 }
3496
3497 static int
3498 IpAddrToRelAddr(struct vl_ctx *ctx, afs_uint32 ipaddr, int create)
3499 {
3500     int i, j;
3501     afs_int32 code;
3502     struct extentaddr *exp;
3503
3504     for (i = 0; i <= MAXSERVERID; i++) {
3505         if (ctx->hostaddress[i] == ipaddr)
3506             return i;
3507         code = multiHomedExtent(ctx, i, &exp);
3508         if (code)
3509             return -1;
3510         if (exp) {
3511             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3512                 if (exp->ex_addrs[j] && (ntohl(exp->ex_addrs[j]) == ipaddr)) {
3513                     return i;
3514                 }
3515             }
3516         }
3517     }
3518
3519     /* allocate the new server a server id pronto */
3520     if (create) {
3521         for (i = 0; i <= MAXSERVERID; i++) {
3522             if (ctx->cheader->IpMappedAddr[i] == 0) {
3523                 ctx->cheader->IpMappedAddr[i] = htonl(ipaddr);
3524                 code =
3525                     vlwrite(ctx->trans,
3526                             DOFFSET(0, ctx->cheader, &ctx->cheader->IpMappedAddr[i]),
3527                             (char *)&ctx->cheader->IpMappedAddr[i],
3528                             sizeof(afs_int32));
3529                 ctx->hostaddress[i] = ipaddr;
3530                 if (code)
3531                     return -1;
3532                 return i;
3533             }
3534         }
3535     }
3536     return -1;
3537 }
3538
3539 static int
3540 ChangeIPAddr(struct vl_ctx *ctx, afs_uint32 ipaddr1, afs_uint32 ipaddr2)
3541 {
3542     int i, j;
3543     afs_int32 code;
3544     struct extentaddr *exp = NULL;
3545     int base = -1;
3546     int mhidx;
3547     afsUUID tuuid;
3548     afs_int32 blockindex, count;
3549     int pollcount = 0;
3550     struct nvlentry tentry;
3551     int ipaddr1_id = -1, ipaddr2_id = -1;
3552     char addrbuf1[256];
3553     char addrbuf2[256];
3554
3555     /* Don't let addr change to 255.*.*.* : Causes internal error below */
3556     if ((ipaddr2 & 0xff000000) == 0xff000000)
3557         return (VL_BADSERVER);
3558
3559     /* If we are removing an address, ip1 will be -1 and ip2 will be
3560      * the original address. This prevents an older revision vlserver
3561      * from removing the IP address (won't find server 0xfffffff in
3562      * the VLDB). An older revision vlserver does not have the check
3563      * to see if any volumes exist on the server being removed.
3564      */
3565     if (ipaddr1 == 0xffffffff) {
3566         ipaddr1 = ipaddr2;
3567         ipaddr2 = 0;
3568     }
3569
3570     for (i = 0; i <= MAXSERVERID; i++) {
3571         struct extentaddr *texp = NULL;
3572         int tbase;
3573
3574         code = multiHomedExtentBase(ctx, i, &texp, &tbase);
3575         if (code)
3576             return code;
3577
3578         if (texp) {
3579             for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
3580                 if (!texp->ex_addrs[mhidx])
3581                     continue;
3582                 if (ntohl(texp->ex_addrs[mhidx]) == ipaddr1) {
3583                     ipaddr1_id = i;
3584                     exp = texp;
3585                     base = tbase;
3586                 }
3587                 if (ipaddr2 != 0 && ntohl(texp->ex_addrs[mhidx]) == ipaddr2) {
3588                     ipaddr2_id = i;
3589                 }
3590             }
3591         } else {
3592             if (ctx->hostaddress[i] == ipaddr1) {
3593                 exp = NULL;
3594                 base = -1;
3595                 ipaddr1_id = i;
3596             }
3597             if (ipaddr2 != 0 && ctx->hostaddress[i] == ipaddr2) {
3598                 ipaddr2_id = i;
3599             }
3600         }
3601
3602         if (ipaddr1_id >= 0 && (ipaddr2 == 0 || ipaddr2_id >= 0)) {
3603             /* we've either found both IPs already in the VLDB, or we found
3604              * ipaddr1, and we're not going to find ipaddr2 because it's 0 */
3605             break;
3606         }
3607     }
3608
3609     if (ipaddr1_id < 0) {
3610         return VL_NOENT;        /* not found */
3611     }
3612
3613     if (ipaddr2_id >= 0 && ipaddr2_id != ipaddr1_id) {
3614         char buf1[16], buf2[16];
3615         VLog(0, ("Cannot change IP address from %s to %s because the latter "
3616                  "is in use by server id %d\n",
3617                  afs_inet_ntoa_r(htonl(ipaddr1), buf1),
3618                  afs_inet_ntoa_r(htonl(ipaddr2), buf2),
3619                  ipaddr2_id));
3620         return VL_MULTIPADDR;
3621     }
3622
3623     /* If we are removing a server entry, a volume cannot
3624      * exist on the server. If one does, don't remove the
3625      * server entry: return error "volume entry exists".
3626      */
3627     if (ipaddr2 == 0) {
3628         for (blockindex = NextEntry(ctx, 0, &tentry, &count); blockindex;
3629              blockindex = NextEntry(ctx, blockindex, &tentry, &count)) {
3630             if (++pollcount > 50) {
3631 #ifndef AFS_PTHREAD_ENV
3632                 IOMGR_Poll();
3633 #endif
3634                 pollcount = 0;
3635             }
3636             for (j = 0; j < NMAXNSERVERS; j++) {
3637                 if (tentry.serverNumber[j] == BADSERVERID)
3638                     break;
3639                 if (tentry.serverNumber[j] == ipaddr1_id) {
3640                     return VL_IDEXIST;
3641                 }
3642             }
3643         }
3644     } else if (exp) {
3645         /* Do not allow changing addresses in multi-homed entries.
3646            Older versions of this RPC would silently "downgrade" mh entries
3647            to single-homed entries and orphan the mh enties. */
3648         addrbuf1[0] = '\0';
3649         append_addr(addrbuf1, ipaddr1, sizeof(addrbuf1));
3650         VLog(0, ("Refusing to change address %s in multi-homed entry; "
3651                  "use RegisterAddrs instead.\n", addrbuf1));
3652         return VL_NOENT;        /* single-homed entry not found */
3653     }
3654
3655     /* Log a message saying we are changing/removing an IP address */
3656     VLog(0,
3657          ("The following IP address is being %s:\n",
3658           (ipaddr2 ? "changed" : "removed")));
3659     addrbuf1[0] = addrbuf2[0] = '\0';
3660     if (exp) {
3661         for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
3662             if (!exp->ex_addrs[mhidx])
3663                 continue;
3664             if (mhidx > 0)
3665                 strlcat(addrbuf1, " ", sizeof(addrbuf1));
3666             append_addr(addrbuf1, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf1));
3667         }
3668     } else {
3669         append_addr(addrbuf1, ipaddr1, sizeof(addrbuf1));
3670     }
3671     if (ipaddr2) {
3672         append_addr(addrbuf2, ipaddr2, sizeof(addrbuf2));
3673     }
3674     VLog(0, ("      entry %d: [%s] -> [%s]\n", ipaddr1_id, addrbuf1, addrbuf2));
3675
3676     /* Change the registered uuuid addresses */
3677     if (exp && base != -1) {
3678         memset(&tuuid, 0, sizeof(afsUUID));
3679         afs_htonuuid(&tuuid);
3680         exp->ex_hostuuid = tuuid;
3681         code =
3682             vlwrite(ctx->trans,
3683                     DOFFSET(ntohl(ctx->ex_addr[0]->ex_contaddrs[base]),
3684                             (char *)ctx->ex_addr[base], (char *)exp),
3685                     (char *)&tuuid, sizeof(tuuid));
3686         if (code)
3687             return VL_IO;
3688     }
3689
3690     /* Now change the host address entry */
3691     ctx->cheader->IpMappedAddr[ipaddr1_id] = htonl(ipaddr2);
3692     code =
3693         vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->IpMappedAddr[ipaddr1_id]),
3694                 (char *)
3695                 &ctx->cheader->IpMappedAddr[ipaddr1_id], sizeof(afs_int32));
3696     ctx->hostaddress[ipaddr1_id] = ipaddr2;
3697     if (code)
3698         return VL_IO;
3699
3700     return 0;
3701 }
3702
3703 /* see if the vlserver is back yet */
3704 afs_int32
3705 SVL_ProbeServer(struct rx_call *rxcall)
3706 {
3707     return 0;
3708 }