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