7eb99fe5289c597c676cade9b006650dbe2bcef1
[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, findname = 0;
1660     int pollcount = 0;
1661     int namematchRWBK, namematchRO, thismatch;
1662     int matchtype = 0;
1663     int size;
1664     char volumename[VL_MAXNAMELEN+3]; /* regex anchors */
1665     char rxstr[AFS_RXINFO_LEN];
1666 #ifdef HAVE_POSIX_REGEX
1667     regex_t re;
1668     int need_regfree = 0;
1669 #else
1670     char *t;
1671 #endif
1672
1673     countRequest(this_op);
1674
1675     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1676                                       restrictedQueryLevel))
1677         return VL_PERM;
1678
1679     vldbentries->nbulkentries_val = 0;
1680     vldbentries->nbulkentries_len = 0;
1681     *nentries = 0;
1682     *nextstartindex = -1;
1683
1684     code = Init_VLdbase(&ctx, LOCKREAD, this_op);
1685     if (code)
1686         return code;
1687
1688     Vldbentry = VldbentryFirst = vldbentries->nbulkentries_val =
1689         malloc(maxCount * sizeof(nvldbentry));
1690     if (Vldbentry == NULL) {
1691         countAbort(this_op);
1692         ubik_AbortTrans(ctx.trans);
1693         return VL_NOMEM;
1694     }
1695
1696     VldbentryLast = VldbentryFirst + maxCount;
1697
1698     /* Handle the attribute by volume id totally separate of the rest
1699      * (thus additional Mask values are ignored if VLLIST_VOLUMEID is set!)
1700      */
1701     if (attributes->Mask & VLLIST_VOLUMEID) {
1702         blockindex =
1703             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
1704         if (blockindex == 0) {
1705             if (!code)
1706                 code = VL_NOENT;
1707         } else {
1708             code =
1709                 put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1710                                     &VldbentryLast, vldbentries, &tentry, 0,
1711                                     0, nentries, &maxCount);
1712             if (code)
1713                 goto done;
1714         }
1715     }
1716
1717     /* Search each entry in the database and return all entries
1718      * that match the request. It checks volumename (with
1719      * wildcarding), entry flags, server, and partition.
1720      */
1721     else {
1722         /* Get the server index for matching server address */
1723         if (attributes->Mask & VLLIST_SERVER) {
1724             serverindex =
1725                 IpAddrToRelAddr(&ctx, attributes->server, 0);
1726             if (serverindex == -1)
1727                 goto done;
1728             findserver = 1;
1729         }
1730         findpartition = ((attributes->Mask & VLLIST_PARTITION) ? 1 : 0);
1731         findflag = ((attributes->Mask & VLLIST_FLAG) ? 1 : 0);
1732         if (name && (strcmp(name, ".*") != 0) && (strcmp(name, "") != 0)) {
1733             if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL)) {
1734                 code = VL_PERM;
1735                 goto done;
1736             }
1737             size = snprintf(volumename, sizeof(volumename), "^%s$", name);
1738             if (size < 0 || size >= sizeof(volumename)) {
1739                 code = VL_BADNAME;
1740                 goto done;
1741             }
1742 #ifdef HAVE_POSIX_REGEX
1743             if (regcomp(&re, volumename, REG_NOSUB) != 0) {
1744                 code = VL_BADNAME;
1745                 goto done;
1746             }
1747             need_regfree = 1;
1748 #else
1749             t = (char *)re_comp(volumename);
1750             if (t) {
1751                 code = VL_BADNAME;
1752                 goto done;
1753             }
1754 #endif
1755             findname = 1;
1756         }
1757
1758         /* Read each entry and see if it is the one we want */
1759         blockindex = startindex;
1760         while ((blockindex = NextEntry(&ctx, blockindex, &tentry, &count))) {
1761             if (++pollcount > 50) {
1762 #ifndef AFS_PTHREAD_ENV
1763                 IOMGR_Poll();
1764 #endif
1765                 pollcount = 0;
1766             }
1767
1768             /* Step through each server index searching for a match.
1769              * Match to an existing RW, BK, or RO volume name (preference
1770              * is in this order). Remember which index we matched against.
1771              */
1772             namematchRWBK = namematchRO = 0;    /* 0->notTried; 1->match; 2->noMatch */
1773             match = 0;
1774             for (k = 0;
1775                  (k < NMAXNSERVERS
1776                   && (tentry.serverNumber[k] != BADSERVERID)); k++) {
1777                 thismatch = 0;  /* does this index match */
1778
1779                 /* Match against the RW or BK volume name. Remember
1780                  * results in namematchRWBK. Prefer RW over BK.
1781                  */
1782                 if (tentry.serverFlags[k] & VLSF_RWVOL) {
1783                     /* Does the name match the RW name */
1784                     if (tentry.flags & VLF_RWEXISTS) {
1785                         if (findname) {
1786                             size = snprintf(volumename, sizeof(volumename),
1787                                             "%s", tentry.name);
1788                             if (size < 0 || size >= sizeof(volumename)) {
1789                                 code = VL_BADNAME;
1790                                 goto done;
1791                             }
1792 #ifdef HAVE_POSIX_REGEX
1793                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1794                                 thismatch = VLSF_RWVOL;
1795                             }
1796 #else
1797                             if (re_exec(volumename)) {
1798                                 thismatch = VLSF_RWVOL;
1799                             }
1800 #endif
1801                         } else {
1802                             thismatch = VLSF_RWVOL;
1803                         }
1804                     }
1805
1806                     /* Does the name match the BK name */
1807                     if (!thismatch && (tentry.flags & VLF_BACKEXISTS)) {
1808                         if (findname) {
1809                             /* If this fails, the tentry.name is invalid */
1810                             size = snprintf(volumename, sizeof(volumename),
1811                                             "%s.backup", tentry.name);
1812                             if (size < 0 || size >= sizeof(volumename)) {
1813                                 code = VL_BADNAME;
1814                                 goto done;
1815                             }
1816 #ifdef HAVE_POSIX_REGEX
1817                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1818                                 thismatch = VLSF_BACKVOL;
1819                             }
1820 #else
1821                             if (re_exec(volumename)) {
1822                                 thismatch = VLSF_BACKVOL;
1823                             }
1824 #endif
1825                         } else {
1826                             thismatch = VLSF_BACKVOL;
1827                         }
1828                     }
1829
1830                     namematchRWBK = (thismatch ? 1 : 2);
1831                 }
1832
1833                 /* Match with the RO volume name. Compare once and
1834                  * remember results in namematchRO. Note that this will
1835                  * pick up entries marked NEWREPSITEs and DONTUSE.
1836                  */
1837                 else {
1838                     if (tentry.flags & VLF_ROEXISTS) {
1839                         if (findname) {
1840                             if (namematchRO) {
1841                                 thismatch =
1842                                     ((namematchRO == 1) ? VLSF_ROVOL : 0);
1843                             } else {
1844                                 /* If this fails, the tentry.name is invalid */
1845                                 size = snprintf(volumename, sizeof(volumename),
1846                                                 "%s.readonly", tentry.name);
1847                                 if (size < 0 || size >= sizeof(volumename)) {
1848                                     code = VL_BADNAME;
1849                                     goto done;
1850                                 }
1851 #ifdef HAVE_POSIX_REGEX
1852                             if (regexec(&re, volumename, 0, NULL, 0) == 0) {
1853                                 thismatch = VLSF_ROVOL;
1854                             }
1855 #else
1856                                 if (re_exec(volumename))
1857                                     thismatch = VLSF_ROVOL;
1858 #endif
1859                             }
1860                         } else {
1861                             thismatch = VLSF_ROVOL;
1862                         }
1863                     }
1864                     namematchRO = (thismatch ? 1 : 2);
1865                 }
1866
1867                 /* Is there a server match */
1868                 if (thismatch && findserver
1869                     && (tentry.serverNumber[k] != serverindex))
1870                     thismatch = 0;
1871
1872                 /* Is there a partition match */
1873                 if (thismatch && findpartition
1874                     && (tentry.serverPartition[k] != attributes->partition))
1875                     thismatch = 0;
1876
1877                 /* Is there a flag match */
1878                 if (thismatch && findflag
1879                     && !(tentry.flags & attributes->flag))
1880                     thismatch = 0;
1881
1882                 /* We found a match. Remember the index, and type */
1883                 if (thismatch) {
1884                     match = 1;
1885                     matchindex = k;
1886                     matchtype = thismatch;
1887                 }
1888
1889                 /* Since we prefer RW and BK volume matches over RO matches,
1890                  * if we have already checked the RWBK name, then we already
1891                  * found the best match and so end the search.
1892                  *
1893                  * If we tried matching against the RW, BK, and RO volume names
1894                  * and both failed, then we end the search (none will match).
1895                  */
1896                 if ((match && namematchRWBK)
1897                     || ((namematchRWBK == 2) && (namematchRO == 2)))
1898                     break;
1899             }
1900
1901             /* Passed all the tests. Take it */
1902             if (match) {
1903                 code =
1904                     put_nattributeentry(&ctx, &Vldbentry, &VldbentryFirst,
1905                                         &VldbentryLast, vldbentries, &tentry,
1906                                         matchtype, matchindex, nentries,
1907                                         &maxCount);
1908                 if (code)
1909                     goto done;
1910
1911                 if (*nentries >= maxCount)
1912                     break;      /* collected the max */
1913             }
1914         }
1915         *nextstartindex = (blockindex ? blockindex : -1);
1916     }
1917
1918   done:
1919 #ifdef HAVE_POSIX_REGEX
1920     if (need_regfree)
1921         regfree(&re);
1922 #endif
1923
1924     if (code) {
1925         countAbort(this_op);
1926         ubik_AbortTrans(ctx.trans);
1927         if (vldbentries->nbulkentries_val)
1928             free(vldbentries->nbulkentries_val);
1929         vldbentries->nbulkentries_val = 0;
1930         vldbentries->nbulkentries_len = 0;
1931         *nextstartindex = -1;
1932     } else {
1933         VLog(5,
1934              ("N2ListAttrs nentries=%d %s\n", vldbentries->nbulkentries_len,
1935               rxinfo(rxstr, rxcall)));
1936         code = ubik_EndTrans(ctx.trans);
1937     }
1938
1939     return code;
1940 }
1941
1942 afs_int32
1943 SVL_ListAttributesN2(struct rx_call *rxcall,
1944                      struct VldbListByAttributes *attributes,
1945                      char *name,                /* Wildcarded volume name */
1946                      afs_int32 startindex,
1947                      afs_int32 *nentries,
1948                      nbulkentries *vldbentries,
1949                      afs_int32 *nextstartindex)
1950 {
1951     afs_int32 code;
1952
1953     code = ListAttributesN2(rxcall, attributes, name, startindex,
1954                             nentries, vldbentries, nextstartindex);
1955     osi_auditU(rxcall, VLListAttributesN2Event, code, AUD_END);
1956     return code;
1957 }
1958
1959 /* Retrieves in vldbentries all vldb entries that match the specified
1960  * attributes (by server number, partition, volume type, and flag); if
1961  * volume id is specified then the associated list for that entry is
1962  * returned. CAUTION: This could be a very expensive call since in most
1963  * cases sequential search of all vldb entries is performed.
1964  */
1965 static afs_int32
1966 LinkedList(struct rx_call *rxcall,
1967            struct VldbListByAttributes *attributes,
1968            afs_int32 *nentries,
1969            vldb_list *vldbentries)
1970 {
1971     int this_op = VLLINKEDLIST;
1972     int code;
1973     struct vl_ctx ctx;
1974     struct nvlentry tentry;
1975     vldblist vllist, *vllistptr;
1976     afs_int32 blockindex, count, match;
1977     afs_int32 k = 0;
1978     int serverindex;
1979     int pollcount = 0;
1980
1981     countRequest(this_op);
1982
1983     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
1984                                       restrictedQueryLevel))
1985         return VL_PERM;
1986
1987     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
1988         return code;
1989
1990     *nentries = 0;
1991     vldbentries->node = NULL;
1992     vllistptr = &vldbentries->node;
1993
1994     /* List by volumeid */
1995     if (attributes->Mask & VLLIST_VOLUMEID) {
1996         blockindex =
1997             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
1998         if (!blockindex) {
1999             if (!code)
2000                 code = VL_NOENT;
2001             goto abort;
2002         }
2003
2004         vllist = malloc(sizeof(single_vldbentry));
2005         if (vllist == NULL) {
2006             code = VL_NOMEM;
2007             goto abort;
2008         }
2009         code = vlentry_to_vldbentry(&ctx, &tentry, &vllist->VldbEntry);
2010         if (code)
2011             goto abort;
2012
2013         vllist->next_vldb = NULL;
2014
2015         *vllistptr = vllist;    /* Thread onto list */
2016         vllistptr = &vllist->next_vldb;
2017         (*nentries)++;
2018     }
2019
2020     /* Search by server, partition, and flags */
2021     else {
2022         for (blockindex = NextEntry(&ctx, 0, &tentry, &count); blockindex;
2023              blockindex = NextEntry(&ctx, blockindex, &tentry, &count)) {
2024             match = 0;
2025
2026             if (++pollcount > 50) {
2027 #ifndef AFS_PTHREAD_ENV
2028                 IOMGR_Poll();
2029 #endif
2030                 pollcount = 0;
2031             }
2032
2033             /* Does this volume exist on the desired server */
2034             if (attributes->Mask & VLLIST_SERVER) {
2035                 serverindex =
2036                     IpAddrToRelAddr(&ctx, attributes->server, 0);
2037                 if (serverindex == -1)
2038                     continue;
2039                 for (k = 0; k < OMAXNSERVERS; k++) {
2040                     if (tentry.serverNumber[k] == BADSERVERID)
2041                         break;
2042                     if (tentry.serverNumber[k] == serverindex) {
2043                         match = 1;
2044                         break;
2045                     }
2046                 }
2047                 if (!match)
2048                     continue;
2049             }
2050
2051             /* Does this volume exist on the desired partition */
2052             if (attributes->Mask & VLLIST_PARTITION) {
2053                 if (match) {
2054                     if (tentry.serverPartition[k] != attributes->partition)
2055                         match = 0;
2056                 } else {
2057                     for (k = 0; k < OMAXNSERVERS; k++) {
2058                         if (tentry.serverNumber[k] == BADSERVERID)
2059                             break;
2060                         if (tentry.serverPartition[k] ==
2061                             attributes->partition) {
2062                             match = 1;
2063                             break;
2064                         }
2065                     }
2066                 }
2067                 if (!match)
2068                     continue;
2069             }
2070
2071             /* Does this volume have the desired flags */
2072             if (attributes->Mask & VLLIST_FLAG) {
2073                 if (!(tentry.flags & attributes->flag))
2074                     continue;
2075             }
2076
2077             vllist = malloc(sizeof(single_vldbentry));
2078             if (vllist == NULL) {
2079                 code = VL_NOMEM;
2080                 goto abort;
2081             }
2082             code = vlentry_to_vldbentry(&ctx, &tentry, &vllist->VldbEntry);
2083             if (code)
2084                 goto abort;
2085
2086             vllist->next_vldb = NULL;
2087
2088             *vllistptr = vllist;        /* Thread onto list */
2089             vllistptr = &vllist->next_vldb;
2090             (*nentries)++;
2091             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
2092                 code = VL_SIZEEXCEEDED;
2093                 goto abort;
2094             }
2095         }
2096     }
2097     *vllistptr = NULL;
2098     return ubik_EndTrans(ctx.trans);
2099
2100 abort:
2101     countAbort(this_op);
2102     ubik_AbortTrans(ctx.trans);
2103     return code;
2104 }
2105
2106 afs_int32
2107 SVL_LinkedList(struct rx_call *rxcall,
2108                struct VldbListByAttributes *attributes,
2109                afs_int32 *nentries,
2110                vldb_list *vldbentries)
2111 {
2112     afs_int32 code;
2113
2114     code = LinkedList(rxcall, attributes, nentries, vldbentries);
2115     osi_auditU(rxcall, VLLinkedListEvent, code, AUD_END);
2116     return code;
2117 }
2118
2119 static afs_int32
2120 LinkedListN(struct rx_call *rxcall,
2121             struct VldbListByAttributes *attributes,
2122             afs_int32 *nentries,
2123             nvldb_list *vldbentries)
2124 {
2125     int this_op = VLLINKEDLISTN;
2126     int code;
2127     struct vl_ctx ctx;
2128     struct nvlentry tentry;
2129     nvldblist vllist, *vllistptr;
2130     afs_int32 blockindex, count, match;
2131     afs_int32 k = 0;
2132     int serverindex;
2133     int pollcount = 0;
2134
2135     countRequest(this_op);
2136
2137     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
2138                                       restrictedQueryLevel))
2139         return VL_PERM;
2140
2141     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2142         return code;
2143
2144     *nentries = 0;
2145     vldbentries->node = NULL;
2146     vllistptr = &vldbentries->node;
2147
2148     /* List by volumeid */
2149     if (attributes->Mask & VLLIST_VOLUMEID) {
2150         blockindex =
2151             FindByID(&ctx, attributes->volumeid, -1, &tentry, &code);
2152         if (!blockindex) {
2153             if (!code)
2154                 code = VL_NOENT;
2155             goto abort;
2156         }
2157
2158         vllist = malloc(sizeof(single_nvldbentry));
2159         if (vllist == NULL) {
2160             code = VL_NOMEM;
2161             goto abort;
2162         }
2163         code = vlentry_to_nvldbentry(&ctx, &tentry, &vllist->VldbEntry);
2164         if (code)
2165             goto abort;
2166
2167         vllist->next_vldb = NULL;
2168
2169         *vllistptr = vllist;    /* Thread onto list */
2170         vllistptr = &vllist->next_vldb;
2171         (*nentries)++;
2172     }
2173
2174     /* Search by server, partition, and flags */
2175     else {
2176         for (blockindex = NextEntry(&ctx, 0, &tentry, &count); blockindex;
2177              blockindex = NextEntry(&ctx, blockindex, &tentry, &count)) {
2178             match = 0;
2179
2180             if (++pollcount > 50) {
2181 #ifndef AFS_PTHREAD_ENV
2182                 IOMGR_Poll();
2183 #endif
2184                 pollcount = 0;
2185             }
2186
2187             /* Does this volume exist on the desired server */
2188             if (attributes->Mask & VLLIST_SERVER) {
2189                 serverindex =
2190                     IpAddrToRelAddr(&ctx, attributes->server, 0);
2191                 if (serverindex == -1)
2192                     continue;
2193                 for (k = 0; k < NMAXNSERVERS; k++) {
2194                     if (tentry.serverNumber[k] == BADSERVERID)
2195                         break;
2196                     if (tentry.serverNumber[k] == serverindex) {
2197                         match = 1;
2198                         break;
2199                     }
2200                 }
2201                 if (!match)
2202                     continue;
2203             }
2204
2205             /* Does this volume exist on the desired partition */
2206             if (attributes->Mask & VLLIST_PARTITION) {
2207                 if (match) {
2208                     if (tentry.serverPartition[k] != attributes->partition)
2209                         match = 0;
2210                 } else {
2211                     for (k = 0; k < NMAXNSERVERS; k++) {
2212                         if (tentry.serverNumber[k] == BADSERVERID)
2213                             break;
2214                         if (tentry.serverPartition[k] ==
2215                             attributes->partition) {
2216                             match = 1;
2217                             break;
2218                         }
2219                     }
2220                 }
2221                 if (!match)
2222                     continue;
2223             }
2224
2225             /* Does this volume have the desired flags */
2226             if (attributes->Mask & VLLIST_FLAG) {
2227                 if (!(tentry.flags & attributes->flag))
2228                     continue;
2229             }
2230
2231             vllist = malloc(sizeof(single_nvldbentry));
2232             if (vllist == NULL) {
2233                 code = VL_NOMEM;
2234                 goto abort;
2235             }
2236             code = vlentry_to_nvldbentry(&ctx, &tentry, &vllist->VldbEntry);
2237             if (code)
2238                 goto abort;
2239
2240             vllist->next_vldb = NULL;
2241
2242             *vllistptr = vllist;        /* Thread onto list */
2243             vllistptr = &vllist->next_vldb;
2244             (*nentries)++;
2245             if (smallMem && (*nentries >= VLDBALLOCCOUNT)) {
2246                 code = VL_SIZEEXCEEDED;
2247                 goto abort;
2248             }
2249         }
2250     }
2251     *vllistptr = NULL;
2252     return ubik_EndTrans(ctx.trans);
2253
2254 abort:
2255     countAbort(this_op);
2256     ubik_AbortTrans(ctx.trans);
2257     return code;
2258 }
2259
2260 afs_int32
2261 SVL_LinkedListN(struct rx_call *rxcall,
2262                 struct VldbListByAttributes *attributes,
2263                 afs_int32 *nentries,
2264                 nvldb_list *vldbentries)
2265 {
2266     afs_int32 code;
2267
2268     code = LinkedListN(rxcall, attributes, nentries, vldbentries);
2269     osi_auditU(rxcall, VLLinkedListNEvent, code, AUD_END);
2270     return code;
2271 }
2272
2273 /* Get back vldb header statistics (allocs, frees, maxvolumeid,
2274  * totalentries, etc) and dynamic statistics (number of requests and/or
2275  * aborts per remote procedure call, etc)
2276  */
2277 static afs_int32
2278 GetStats(struct rx_call *rxcall,
2279          vldstats *stats,
2280          vital_vlheader *vital_header)
2281 {
2282     int this_op = VLGETSTATS;
2283     afs_int32 code;
2284     struct vl_ctx ctx;
2285     char rxstr[AFS_RXINFO_LEN];
2286
2287     countRequest(this_op);
2288
2289     if (!afsconf_CheckRestrictedQuery(vldb_confdir, rxcall,
2290                                       restrictedQueryLevel))
2291         return VL_PERM;
2292
2293     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2294         return code;
2295     VLog(5, ("GetStats %s\n", rxinfo(rxstr, rxcall)));
2296     memcpy((char *)vital_header, (char *)&ctx.cheader->vital_header,
2297            sizeof(vital_vlheader));
2298     memcpy((char *)stats, (char *)&dynamic_statistics, sizeof(vldstats));
2299     return ubik_EndTrans(ctx.trans);
2300 }
2301
2302 afs_int32
2303 SVL_GetStats(struct rx_call *rxcall,
2304              vldstats *stats,
2305              vital_vlheader *vital_header)
2306 {
2307     afs_int32 code;
2308
2309     code = GetStats(rxcall, stats, vital_header);
2310     osi_auditU(rxcall, VLGetStatsEvent, code, AUD_END);
2311     return code;
2312 }
2313
2314 /* Get the list of file server addresses from the VLDB.  Currently it's pretty
2315  * easy to do.  In the future, it might require a little bit of grunging
2316  * through the VLDB, but that's life.
2317  */
2318 afs_int32
2319 SVL_GetAddrs(struct rx_call *rxcall,
2320              afs_int32 Handle,
2321              afs_int32 spare2,
2322              struct VLCallBack *spare3,
2323              afs_int32 *nentries,
2324              bulkaddrs *addrsp)
2325 {
2326     int this_op = VLGETADDRS;
2327     afs_int32 code;
2328     struct vl_ctx ctx;
2329     int nservers, i;
2330     afs_uint32 *taddrp;
2331
2332     countRequest(this_op);
2333     addrsp->bulkaddrs_len = *nentries = 0;
2334     addrsp->bulkaddrs_val = 0;
2335     memset(spare3, 0, sizeof(struct VLCallBack));
2336
2337     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2338         return code;
2339
2340     VLog(5, ("GetAddrs\n"));
2341     addrsp->bulkaddrs_val = taddrp =
2342         malloc(sizeof(afs_uint32) * (MAXSERVERID + 1));
2343     nservers = *nentries = addrsp->bulkaddrs_len = 0;
2344
2345     if (!taddrp) {
2346         code = VL_NOMEM;
2347         goto abort;
2348     }
2349
2350     for (i = 0; i <= MAXSERVERID; i++) {
2351         if ((*taddrp = ntohl(ctx.cheader->IpMappedAddr[i]))) {
2352             taddrp++;
2353             nservers++;
2354         }
2355     }
2356
2357     addrsp->bulkaddrs_len = *nentries = nservers;
2358     return (ubik_EndTrans(ctx.trans));
2359
2360 abort:
2361     countAbort(this_op);
2362     ubik_AbortTrans(ctx.trans);
2363     return code;
2364 }
2365
2366 static_inline void
2367 append_addr(char *buffer, afs_uint32 addr, size_t buffer_size)
2368 {
2369     int n = strlen(buffer);
2370     if (buffer_size > n) {
2371         snprintf(buffer + n, buffer_size - n, "%u.%u.%u.%u",
2372                  (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
2373                  addr & 0xff);
2374     }
2375 }
2376
2377 afs_int32
2378 SVL_RegisterAddrs(struct rx_call *rxcall, afsUUID *uuidp, afs_int32 spare1,
2379                   bulkaddrs *addrsp)
2380 {
2381     int this_op = VLREGADDR;
2382     afs_int32 code;
2383     struct vl_ctx ctx;
2384     int cnt, h, i, j, k, m;
2385     struct extentaddr *exp = 0, *tex;
2386     char addrbuf[256];
2387     afsUUID tuuid;
2388     afs_uint32 addrs[VL_MAXIPADDRS_PERMH];
2389     int base;
2390     int count, willChangeEntry, foundUuidEntry, willReplaceCnt;
2391     int WillReplaceEntry, WillChange[MAXSERVERID + 1];
2392     int FoundUuid = 0;
2393     int ReplaceEntry = 0;
2394     int srvidx, mhidx;
2395
2396     countRequest(this_op);
2397     if (!afsconf_SuperUser(vldb_confdir, rxcall, NULL))
2398         return (VL_PERM);
2399     if ((code = Init_VLdbase(&ctx, LOCKWRITE, this_op)))
2400         return code;
2401
2402     /* Eliminate duplicates from IP address list */
2403     for (k = 0, cnt = 0; k < addrsp->bulkaddrs_len; k++) {
2404         if (addrsp->bulkaddrs_val[k] == 0)
2405             continue;
2406         for (m = 0; m < cnt; m++) {
2407             if (addrs[m] == addrsp->bulkaddrs_val[k])
2408                 break;
2409         }
2410         if (m == cnt) {
2411             if (m == VL_MAXIPADDRS_PERMH) {
2412                 VLog(0,
2413                      ("Number of addresses exceeds %d. Cannot register IP addr 0x%x in VLDB\n",
2414                       VL_MAXIPADDRS_PERMH, addrsp->bulkaddrs_val[k]));
2415             } else {
2416                 addrs[m] = addrsp->bulkaddrs_val[k];
2417                 cnt++;
2418             }
2419         }
2420     }
2421     if (cnt <= 0) {
2422         code = VL_INDEXERANGE;
2423         goto abort;
2424     }
2425
2426     count = 0;
2427     willReplaceCnt = 0;
2428     foundUuidEntry = 0;
2429     /* For each server registered within the VLDB */
2430     for (srvidx = 0; srvidx <= MAXSERVERID; srvidx++) {
2431         willChangeEntry = 0;
2432         WillReplaceEntry = 1;
2433         code = multiHomedExtent(&ctx, srvidx, &exp);
2434         if (code)
2435              continue;
2436
2437         if (exp) {
2438             /* See if the addresses to register will change this server entry */
2439             tuuid = exp->ex_hostuuid;
2440             afs_ntohuuid(&tuuid);
2441             if (afs_uuid_equal(uuidp, &tuuid)) {
2442                 foundUuidEntry = 1;
2443                 FoundUuid = srvidx;
2444             } else {
2445                 for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2446                     if (!exp->ex_addrs[mhidx])
2447                         continue;
2448                     for (k = 0; k < cnt; k++) {
2449                         if (ntohl(exp->ex_addrs[mhidx]) == addrs[k]) {
2450                             willChangeEntry = 1;
2451                             WillChange[count] = srvidx;
2452                             break;
2453                         }
2454                     }
2455                     if (k >= cnt)
2456                         WillReplaceEntry = 0;
2457                 }
2458             }
2459         } else {
2460             /* The server is not registered as a multihomed.
2461              * See if the addresses to register will replace this server entry.
2462              */
2463             for (k = 0; k < cnt; k++) {
2464                 if (ctx.hostaddress[srvidx] == addrs[k]) {
2465                     willChangeEntry = 1;
2466                     WillChange[count] = srvidx;
2467                     WillReplaceEntry = 1;
2468                     break;
2469                 }
2470             }
2471         }
2472         if (willChangeEntry) {
2473             if (WillReplaceEntry) {
2474                 willReplaceCnt++;
2475                 ReplaceEntry = srvidx;
2476             }
2477             count++;
2478         }
2479     }
2480
2481     /* If we found the uuid in the VLDB and if we are replacing another
2482      * entire entry, then complain and fail. Also, if we did not find
2483      * the uuid in the VLDB and the IP addresses being registered was
2484      * found in more than one other entry, then we don't know which one
2485      * to replace and will complain and fail.
2486      */
2487     if ((foundUuidEntry && (willReplaceCnt > 0))
2488         || (!foundUuidEntry && (count > 1))) {
2489         VLog(0,
2490              ("The following fileserver is being registered in the VLDB:\n"));
2491         for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
2492             if (k > 0)
2493                 strlcat(addrbuf, " ", sizeof(addrbuf));
2494             append_addr(addrbuf, addrs[k], sizeof(addrbuf));
2495         }
2496         VLog(0, ("      [%s]\n", addrbuf));
2497
2498         if (foundUuidEntry) {
2499             code = multiHomedExtent(&ctx, FoundUuid, &exp);
2500             if (code == 0) {
2501                 VLog(0, ("   It would have replaced the existing VLDB server "
2502                          "entry:\n"));
2503                 for (addrbuf[0] = '\0', mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2504                     if (!exp->ex_addrs[mhidx])
2505                         continue;
2506                     if (mhidx > 0)
2507                         strlcat(addrbuf, " ", sizeof(addrbuf));
2508                     append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
2509                 }
2510                 VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
2511             }
2512         }
2513
2514         if (count == 1)
2515             VLog(0, ("   Yet another VLDB server entry exists:\n"));
2516         else
2517             VLog(0, ("   Yet other VLDB server entries exist:\n"));
2518         for (j = 0; j < count; j++) {
2519             srvidx = WillChange[j];
2520             VLog(0, ("      entry %d: ", srvidx));
2521
2522             code = multiHomedExtent(&ctx, srvidx, &exp);
2523             if (code)
2524                 goto abort;
2525
2526             addrbuf[0] = '\0';
2527             if (exp) {
2528                 for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
2529                     if (!exp->ex_addrs[mhidx])
2530                         continue;
2531                     if (mhidx > 0)
2532                         strlcat(addrbuf, " ", sizeof(addrbuf));
2533                     append_addr(addrbuf, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf));
2534                 }
2535             } else {
2536                 append_addr(addrbuf, ctx.hostaddress[srvidx], sizeof(addrbuf));
2537             }
2538             VLog(0, ("      entry %d: [%s]\n", srvidx, addrbuf));
2539         }
2540
2541         if (count == 1)
2542             VLog(0, ("   You must 'vos changeaddr' this other server entry\n"));
2543         else
2544             VLog(0,
2545                 ("   You must 'vos changeaddr' these other server entries\n"));
2546         if (foundUuidEntry)
2547             VLog(0,
2548                 ("   and/or remove the sysid file from the registering fileserver\n"));
2549         VLog(0, ("   before the fileserver can be registered in the VLDB.\n"));
2550
2551         code = VL_MULTIPADDR;
2552         goto abort;
2553     }
2554
2555     /* Passed the checks. Now find and update the existing mh entry, or create
2556      * a new mh entry.
2557      */
2558     if (foundUuidEntry) {
2559         /* Found the entry with same uuid. See if we need to change it */
2560         int change = 0;
2561
2562         code = multiHomedExtentBase(&ctx, FoundUuid, &exp, &base);
2563         if (code)
2564             goto abort;
2565
2566         /* Determine if the entry has changed */
2567         for (k = 0; ((k < cnt) && !change); k++) {
2568             if (ntohl(exp->ex_addrs[k]) != addrs[k])
2569                 change = 1;
2570         }
2571         for (; ((k < VL_MAXIPADDRS_PERMH) && !change); k++) {
2572             if (exp->ex_addrs[k] != 0)
2573                 change = 1;
2574         }
2575         if (!change) {
2576             return (ubik_EndTrans(ctx.trans));
2577         }
2578     }
2579
2580     VLog(0, ("The following fileserver is being registered in the VLDB:\n"));
2581     for (addrbuf[0] = '\0', k = 0; k < cnt; k++) {
2582         if (k > 0)
2583             strlcat(addrbuf, " ", sizeof(addrbuf));
2584         append_addr(addrbuf, addrs[k], sizeof(addrbuf));
2585     }
2586     VLog(0, ("      [%s]\n", addrbuf));
2587
2588     if (foundUuidEntry) {
2589         VLog(0,
2590             ("   It will replace the following existing entry in the VLDB (same uuid):\n"));
2591         for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
2592             if (exp->ex_addrs[k] == 0)
2593                 continue;
2594             if (k > 0)
2595                 strlcat(addrbuf, " ", sizeof(addrbuf));
2596             append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
2597         }
2598         VLog(0, ("      entry %d: [%s]\n", FoundUuid, addrbuf));
2599     } else if (willReplaceCnt || (count == 1)) {
2600         /* If we are not replacing an entry and there is only one entry to change,
2601          * then we will replace that entry.
2602          */
2603         if (!willReplaceCnt) {
2604             ReplaceEntry = WillChange[0];
2605             willReplaceCnt++;
2606         }
2607
2608         /* Have an entry that needs to be replaced */
2609         code = multiHomedExtentBase(&ctx, ReplaceEntry, &exp, &base);
2610         if (code)
2611             goto abort;
2612
2613         if (exp) {
2614             VLog(0,
2615                 ("   It will replace the following existing entry in the VLDB (new uuid):\n"));
2616             for (addrbuf[0] = '\0', k = 0; k < VL_MAXIPADDRS_PERMH; k++) {
2617                 if (exp->ex_addrs[k] == 0)
2618                     continue;
2619                 if (k > 0)
2620                     strlcat(addrbuf, " ", sizeof(addrbuf));
2621                 append_addr(addrbuf, ntohl(exp->ex_addrs[k]), sizeof(addrbuf));
2622             }
2623             VLog(0, ("      entry %d: [%s]\n", ReplaceEntry, addrbuf));
2624         } else {
2625             /* Not a mh entry. So we have to create a new mh entry and
2626              * put it on the ReplaceEntry slot of the ctx.hostaddress array.
2627              */
2628             addrbuf[0] = '\0';
2629             append_addr(addrbuf, ctx.hostaddress[ReplaceEntry], sizeof(addrbuf));
2630             VLog(0, ("   It will replace existing entry %d, %s,"
2631                      " in the VLDB (new uuid):\n", ReplaceEntry, addrbuf));
2632             code =
2633                 FindExtentBlock(&ctx, uuidp, 1, ReplaceEntry, &exp, &base);
2634             if (code || !exp) {
2635                 if (!code)
2636                     code = VL_IO;
2637                 goto abort;
2638             }
2639         }
2640     } else {
2641         /* There is no entry for this server, must create a new mh entry as
2642          * well as use a new slot of the ctx.hostaddress array.
2643          */
2644         VLog(0, ("   It will create a new entry in the VLDB.\n"));
2645         code = FindExtentBlock(&ctx, uuidp, 1, -1, &exp, &base);
2646         if (code || !exp) {
2647             if (!code)
2648                 code = VL_IO;
2649             goto abort;
2650         }
2651     }
2652
2653     /* Now we have a mh entry to fill in. Update the uuid, bump the
2654      * uniquifier, and fill in its IP addresses.
2655      */
2656     tuuid = *uuidp;
2657     afs_htonuuid(&tuuid);
2658     exp->ex_hostuuid = tuuid;
2659     exp->ex_uniquifier = htonl(ntohl(exp->ex_uniquifier) + 1);
2660     for (k = 0; k < cnt; k++) {
2661         exp->ex_addrs[k] = htonl(addrs[k]);
2662     }
2663     for (; k < VL_MAXIPADDRS_PERMH; k++) {
2664         exp->ex_addrs[k] = 0;
2665     }
2666
2667     /* Write the new mh entry out */
2668     if (vlwrite
2669         (ctx.trans,
2670          DOFFSET(ntohl(ctx.ex_addr[0]->ex_contaddrs[base]),
2671                  (char *)ctx.ex_addr[base], (char *)exp), (char *)exp,
2672          sizeof(*exp))) {
2673         code = VL_IO;
2674         goto abort;
2675     }
2676
2677     /* Remove any common addresses from other mh entres. We know these entries
2678      * are being changed and not replaced so they are mh entries.
2679      */
2680     m = 0;
2681     for (i = 0; i < count; i++) {
2682         afs_int32 doff;
2683
2684         /* Skip the entry we replaced */
2685         if (willReplaceCnt && (WillChange[i] == ReplaceEntry))
2686             continue;
2687
2688         code = multiHomedExtentBase(&ctx, WillChange[i], &tex, &base);
2689         if (code)
2690             goto abort;
2691
2692         if (++m == 1)
2693             VLog(0,
2694                 ("   The following existing entries in the VLDB will be updated:\n"));
2695
2696         for (addrbuf[0] = '\0', h = j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2697             if (tex->ex_addrs[j]) {
2698                 if (j > 0)
2699                     strlcat(addrbuf, " ", sizeof(addrbuf));
2700                 append_addr(addrbuf, ntohl(tex->ex_addrs[j]), sizeof(addrbuf));
2701             }
2702
2703             for (k = 0; k < cnt; k++) {
2704                 if (ntohl(tex->ex_addrs[j]) == addrs[k])
2705                     break;
2706             }
2707             if (k >= cnt) {
2708                 /* Not found, so we keep it */
2709                 tex->ex_addrs[h] = tex->ex_addrs[j];
2710                 h++;
2711             }
2712         }
2713         for (j = h; j < VL_MAXIPADDRS_PERMH; j++) {
2714             tex->ex_addrs[j] = 0;       /* zero rest of mh entry */
2715         }
2716         VLog(0, ("      entry %d: [%s]\n", WillChange[i], addrbuf));
2717
2718         /* Write out the modified mh entry */
2719         tex->ex_uniquifier = htonl(ntohl(tex->ex_uniquifier) + 1);
2720         doff =
2721             DOFFSET(ntohl(ctx.ex_addr[0]->ex_contaddrs[base]),
2722                     (char *)ctx.ex_addr[base], (char *)tex);
2723         if (vlwrite(ctx.trans, doff, (char *)tex, sizeof(*tex))) {
2724             code = VL_IO;
2725             goto abort;
2726         }
2727     }
2728
2729     return (ubik_EndTrans(ctx.trans));
2730
2731 abort:
2732     countAbort(this_op);
2733     ubik_AbortTrans(ctx.trans);
2734     return code;
2735 }
2736
2737 afs_int32
2738 SVL_GetAddrsU(struct rx_call *rxcall,
2739               struct ListAddrByAttributes *attributes,
2740               afsUUID *uuidpo,
2741               afs_int32 *uniquifier,
2742               afs_int32 *nentries,
2743               bulkaddrs *addrsp)
2744 {
2745     int this_op = VLGETADDRSU;
2746     afs_int32 code, index;
2747     struct vl_ctx ctx;
2748     int nservers, i, j, base = 0;
2749     struct extentaddr *exp = 0;
2750     afsUUID tuuid;
2751     afs_uint32 *taddrp, taddr;
2752     char rxstr[AFS_RXINFO_LEN];
2753
2754     countRequest(this_op);
2755     addrsp->bulkaddrs_len = *nentries = 0;
2756     addrsp->bulkaddrs_val = 0;
2757     VLog(5, ("GetAddrsU %s\n", rxinfo(rxstr, rxcall)));
2758     if ((code = Init_VLdbase(&ctx, LOCKREAD, this_op)))
2759         return code;
2760
2761     if (attributes->Mask & VLADDR_IPADDR) {
2762         if (attributes->Mask & (VLADDR_INDEX | VLADDR_UUID)) {
2763             code = VL_BADMASK;
2764             goto abort;
2765         }
2766         /* Search for a server registered with the VLDB with this ip address. */
2767         for (index = 0; index <= MAXSERVERID; index++) {
2768             code = multiHomedExtent(&ctx, index, &exp);
2769             if (code)
2770                 continue;
2771
2772             if (exp) {
2773                 for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
2774                     if (exp->ex_addrs[j]
2775                         && (ntohl(exp->ex_addrs[j]) == attributes->ipaddr)) {
2776                         break;
2777                     }
2778                 }
2779                 if (j < VL_MAXIPADDRS_PERMH)
2780                     break;
2781             }
2782         }
2783         if (index > MAXSERVERID) {
2784             code = VL_NOENT;
2785             goto abort;
2786         }
2787     } else if (attributes->Mask & VLADDR_INDEX) {
2788         if (attributes->Mask & (VLADDR_IPADDR | VLADDR_UUID)) {
2789             code = VL_BADMASK;
2790             goto abort;
2791         }
2792         /* VLADDR_INDEX index is one based */
2793         if (attributes->index < 1 || attributes->index > MAXSERVERID) {
2794             code = VL_INDEXERANGE;
2795             goto abort;
2796         }
2797         index = attributes->index - 1;
2798         code = multiHomedExtent(&ctx, index, &exp);
2799         if (code) {
2800             code = VL_NOENT;
2801             goto abort;
2802         }
2803     } else if (attributes->Mask & VLADDR_UUID) {
2804         if (attributes->Mask & (VLADDR_IPADDR | VLADDR_INDEX)) {
2805             code = VL_BADMASK;
2806             goto abort;
2807         }
2808         if (!ctx.ex_addr[0]) {  /* mh servers probably aren't setup on this vldb */
2809             code = VL_NOENT;
2810             goto abort;
2811         }
2812         code = FindExtentBlock(&ctx, &attributes->uuid, 0, -1, &exp, &base);
2813         if (code)
2814             goto abort;
2815     } else {
2816         code = VL_BADMASK;
2817         goto abort;
2818     }
2819
2820     if (exp == NULL) {
2821         code = VL_NOENT;
2822         goto abort;
2823     }
2824     addrsp->bulkaddrs_val = taddrp =
2825         malloc(sizeof(afs_uint32) * (MAXSERVERID + 1));
2826     nservers = *nentries = addrsp->bulkaddrs_len = 0;
2827     if (!taddrp) {
2828         code = VL_NOMEM;
2829         goto abort;
2830     }
2831     tuuid = exp->ex_hostuuid;
2832     afs_ntohuuid(&tuuid);
2833     if (afs_uuid_is_nil(&tuuid)) {
2834         code = VL_NOENT;
2835         goto abort;
2836     }
2837     if (uuidpo)
2838         *uuidpo = tuuid;
2839     if (uniquifier)
2840         *uniquifier = ntohl(exp->ex_uniquifier);
2841     for (i = 0; i < VL_MAXIPADDRS_PERMH; i++) {
2842         if (exp->ex_addrs[i]) {
2843             taddr = ntohl(exp->ex_addrs[i]);
2844             /* Weed out duplicates */
2845             for (j = 0; j < nservers; j++) {
2846                 if (taddrp[j] == taddr)
2847                     break;
2848             }
2849             if ((j == nservers) && (j <= MAXSERVERID)) {
2850                 taddrp[nservers] = taddr;
2851                 nservers++;
2852             }
2853         }
2854     }
2855     addrsp->bulkaddrs_len = *nentries = nservers;
2856     return (ubik_EndTrans(ctx.trans));
2857
2858 abort:
2859     countAbort(this_op);
2860     ubik_AbortTrans(ctx.trans);
2861     return code;
2862 }
2863
2864 /* ============> End of Exported vldb RPC functions <============= */
2865
2866
2867 /* Routine that copies the given vldb entry to the output buffer, vldbentries. */
2868 static int
2869 put_attributeentry(struct vl_ctx *ctx,
2870                    struct vldbentry **Vldbentry,
2871                    struct vldbentry **VldbentryFirst,
2872                    struct vldbentry **VldbentryLast,
2873                    bulkentries *vldbentries,
2874                    struct nvlentry *entry,
2875                    afs_int32 *nentries,
2876                    afs_int32 *alloccnt)
2877 {
2878     vldbentry *reall;
2879     afs_int32 allo;
2880     int code;
2881
2882     if (*Vldbentry == *VldbentryLast) {
2883         if (smallMem)
2884             return VL_SIZEEXCEEDED;     /* no growing if smallMem defined */
2885
2886         /* Allocate another set of memory; each time allocate twice as
2887          * many blocks as the last time. When we reach VLDBALLOCLIMIT,
2888          * then grow in increments of VLDBALLOCINCR.
2889          */
2890         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2891         reall = realloc(*VldbentryFirst,
2892                         (*alloccnt + allo) * sizeof(vldbentry));
2893         if (reall == NULL)
2894             return VL_NOMEM;
2895
2896         *VldbentryFirst = vldbentries->bulkentries_val = reall;
2897         *Vldbentry = *VldbentryFirst + *alloccnt;
2898         *VldbentryLast = *Vldbentry + allo;
2899         *alloccnt += allo;
2900     }
2901
2902     code = vlentry_to_vldbentry(ctx, entry, *Vldbentry);
2903     if (code)
2904         return code;
2905
2906     (*Vldbentry)++;
2907     (*nentries)++;
2908     vldbentries->bulkentries_len++;
2909     return 0;
2910 }
2911
2912 static int
2913 put_nattributeentry(struct vl_ctx *ctx,
2914                     struct nvldbentry **Vldbentry,
2915                     struct nvldbentry **VldbentryFirst,
2916                     struct nvldbentry **VldbentryLast,
2917                     nbulkentries *vldbentries,
2918                     struct nvlentry *entry,
2919                     afs_int32 matchtype,
2920                     afs_int32 matchindex,
2921                     afs_int32 *nentries,
2922                     afs_int32 *alloccnt)
2923 {
2924     nvldbentry *reall;
2925     afs_int32 allo;
2926     int code;
2927
2928     if (*Vldbentry == *VldbentryLast) {
2929         if (smallMem)
2930             return VL_SIZEEXCEEDED;     /* no growing if smallMem defined */
2931
2932         /* Allocate another set of memory; each time allocate twice as
2933          * many blocks as the last time. When we reach VLDBALLOCLIMIT,
2934          * then grow in increments of VLDBALLOCINCR.
2935          */
2936         allo = (*alloccnt > VLDBALLOCLIMIT) ? VLDBALLOCINCR : *alloccnt;
2937         reall = realloc(*VldbentryFirst,
2938                         (*alloccnt + allo) * sizeof(nvldbentry));
2939         if (reall == NULL)
2940             return VL_NOMEM;
2941
2942         *VldbentryFirst = vldbentries->nbulkentries_val = reall;
2943         *Vldbentry = *VldbentryFirst + *alloccnt;
2944         *VldbentryLast = *Vldbentry + allo;
2945         *alloccnt += allo;
2946     }
2947     code = vlentry_to_nvldbentry(ctx, entry, *Vldbentry);
2948     if (code)
2949         return code;
2950
2951     (*Vldbentry)->matchindex = (matchtype << 16) + matchindex;
2952     (*Vldbentry)++;
2953     (*nentries)++;
2954     vldbentries->nbulkentries_len++;
2955     return 0;
2956 }
2957
2958
2959 /* Common code to actually remove a vldb entry from the database. */
2960 static int
2961 RemoveEntry(struct vl_ctx *ctx, afs_int32 entryptr,
2962             struct nvlentry *tentry)
2963 {
2964     int code;
2965
2966     if ((code = UnthreadVLentry(ctx, entryptr, tentry)))
2967         return code;
2968     if ((code = FreeBlock(ctx, entryptr)))
2969         return code;
2970     return 0;
2971 }
2972
2973 static void
2974 ReleaseEntry(struct nvlentry *tentry, afs_int32 releasetype)
2975 {
2976     if (releasetype & LOCKREL_TIMESTAMP)
2977         tentry->LockTimestamp = 0;
2978     if (releasetype & LOCKREL_OPCODE)
2979         tentry->flags &= ~VLOP_ALLOPERS;
2980     if (releasetype & LOCKREL_AFSID)
2981         tentry->LockAfsId = 0;
2982 }
2983
2984
2985 /* Verify that the incoming vldb entry is valid; multi type of error codes
2986  * are returned. */
2987 static int
2988 check_vldbentry(struct vldbentry *aentry)
2989 {
2990     afs_int32 i;
2991
2992     if (InvalidVolname(aentry->name))
2993         return VL_BADNAME;
2994     if (aentry->nServers <= 0 || aentry->nServers > OMAXNSERVERS)
2995         return VL_BADSERVER;
2996     for (i = 0; i < aentry->nServers; i++) {
2997 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
2998             return VL_BADSERVER;        */
2999         if (aentry->serverPartition[i] < 0
3000             || aentry->serverPartition[i] > MAXPARTITIONID)
3001             return VL_BADPARTITION;
3002         if (aentry->serverFlags[i] < 0
3003             || aentry->serverFlags[i] > MAXSERVERFLAG)
3004             return VL_BADSERVERFLAG;
3005     }
3006     return 0;
3007 }
3008
3009 static int
3010 check_nvldbentry(struct nvldbentry *aentry)
3011 {
3012     afs_int32 i;
3013
3014     if (InvalidVolname(aentry->name))
3015         return VL_BADNAME;
3016     if (aentry->nServers <= 0 || aentry->nServers > NMAXNSERVERS)
3017         return VL_BADSERVER;
3018     for (i = 0; i < aentry->nServers; i++) {
3019 /*      if (aentry->serverNumber[i] < 0 || aentry->serverNumber[i] > MAXSERVERID)
3020             return VL_BADSERVER;        */
3021         if (aentry->serverPartition[i] < 0
3022             || aentry->serverPartition[i] > MAXPARTITIONID)
3023             return VL_BADPARTITION;
3024         if (aentry->serverFlags[i] < 0
3025             || aentry->serverFlags[i] > MAXSERVERFLAG)
3026             return VL_BADSERVERFLAG;
3027     }
3028     return 0;
3029 }
3030
3031
3032 /* Convert from the external vldb entry representation to its internal
3033    (more compact) form.  This call should not change the hash chains! */
3034 static int
3035 vldbentry_to_vlentry(struct vl_ctx *ctx,
3036                      struct vldbentry *VldbEntry,
3037                      struct nvlentry *VlEntry)
3038 {
3039     int i, serverindex;
3040
3041     if (strcmp(VlEntry->name, VldbEntry->name))
3042         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
3043     for (i = 0; i < VldbEntry->nServers; i++) {
3044         serverindex = IpAddrToRelAddr(ctx, VldbEntry->serverNumber[i], 1);
3045         if (serverindex == -1)
3046             return VL_BADSERVER;
3047         VlEntry->serverNumber[i] = serverindex;
3048         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
3049         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
3050     }
3051     for (; i < OMAXNSERVERS; i++)
3052         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] =
3053             VlEntry->serverFlags[i] = BADSERVERID;
3054     for (i = 0; i < MAXTYPES; i++)
3055         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
3056     VlEntry->cloneId = VldbEntry->cloneId;
3057     VlEntry->flags = VldbEntry->flags;
3058     return 0;
3059 }
3060
3061 static int
3062 nvldbentry_to_vlentry(struct vl_ctx *ctx,
3063                       struct nvldbentry *VldbEntry,
3064                       struct nvlentry *VlEntry)
3065 {
3066     int i, serverindex;
3067
3068     if (strcmp(VlEntry->name, VldbEntry->name))
3069         strncpy(VlEntry->name, VldbEntry->name, sizeof(VlEntry->name));
3070     for (i = 0; i < VldbEntry->nServers; i++) {
3071         serverindex = IpAddrToRelAddr(ctx, VldbEntry->serverNumber[i], 1);
3072         if (serverindex == -1)
3073             return VL_BADSERVER;
3074         VlEntry->serverNumber[i] = serverindex;
3075         VlEntry->serverPartition[i] = VldbEntry->serverPartition[i];
3076         VlEntry->serverFlags[i] = VldbEntry->serverFlags[i];
3077     }
3078     for (; i < NMAXNSERVERS; i++)
3079         VlEntry->serverNumber[i] = VlEntry->serverPartition[i] =
3080             VlEntry->serverFlags[i] = BADSERVERID;
3081     for (i = 0; i < MAXTYPES; i++)
3082         VlEntry->volumeId[i] = VldbEntry->volumeId[i];
3083     VlEntry->cloneId = VldbEntry->cloneId;
3084     VlEntry->flags = VldbEntry->flags;
3085     return 0;
3086 }
3087
3088
3089 /* Update the vldb entry with the new fields as indicated by the value of
3090  * the Mask entry in the updateentry structure. All necessary validation
3091  * checks are performed.
3092  */
3093 static int
3094 get_vldbupdateentry(struct vl_ctx *ctx,
3095                     afs_int32 blockindex,
3096                     struct VldbUpdateEntry *updateentry,
3097                     struct nvlentry *VlEntry)
3098 {
3099     int i, j, code, serverindex;
3100     afs_uint32 checkids[MAXTYPES];
3101
3102     /* check if any specified new IDs are already present in the db. Do
3103      * this check before doing anything else, so we don't get a half-
3104      * updated entry. */
3105     memset(&checkids, 0, sizeof(checkids));
3106     if (updateentry->Mask & VLUPDATE_RWID) {
3107         checkids[RWVOL] = updateentry->spares3; /* rw id */
3108     }
3109     if (updateentry->Mask & VLUPDATE_READONLYID) {
3110         checkids[ROVOL] = updateentry->ReadOnlyId;
3111     }
3112     if (updateentry->Mask & VLUPDATE_BACKUPID) {
3113         checkids[BACKVOL] = updateentry->BackupId;
3114     }
3115
3116     if (EntryIDExists(ctx, checkids, MAXTYPES, &code)) {
3117         return VL_IDEXIST;
3118     } else if (code) {
3119         return code;
3120     }
3121
3122     if (updateentry->Mask & VLUPDATE_VOLUMENAME) {
3123         struct nvlentry tentry;
3124
3125         if (InvalidVolname(updateentry->name))
3126             return VL_BADNAME;
3127
3128         if (FindByName(ctx, updateentry->name, &tentry, &code)) {
3129             return VL_NAMEEXIST;
3130         } else if (code) {
3131             return code;
3132         }
3133
3134         if ((code = UnhashVolname(ctx, blockindex, VlEntry)))
3135             return code;
3136         strncpy(VlEntry->name, updateentry->name, sizeof(VlEntry->name));
3137         HashVolname(ctx, blockindex, VlEntry);
3138     }
3139
3140     if (updateentry->Mask & VLUPDATE_VOLNAMEHASH) {
3141         if ((code = UnhashVolname(ctx, blockindex, VlEntry))) {
3142             if (code != VL_NOENT)
3143                 return code;
3144         }
3145         HashVolname(ctx, blockindex, VlEntry);
3146     }
3147
3148     if (updateentry->Mask & VLUPDATE_FLAGS) {
3149         VlEntry->flags = updateentry->flags;
3150     }
3151     if (updateentry->Mask & VLUPDATE_CLONEID) {
3152         VlEntry->cloneId = updateentry->cloneId;
3153     }
3154     if (updateentry->Mask & VLUPDATE_RWID) {
3155         if ((code = UnhashVolid(ctx, RWVOL, blockindex, VlEntry))) {
3156             if (code != VL_NOENT)
3157                 return code;
3158         }
3159         VlEntry->volumeId[RWVOL] = updateentry->spares3;        /* rw id */
3160         if ((code = HashVolid(ctx, RWVOL, blockindex, VlEntry)))
3161             return code;
3162     }
3163     if (updateentry->Mask & VLUPDATE_READONLYID) {
3164         if ((code = UnhashVolid(ctx, ROVOL, blockindex, VlEntry))) {
3165             if (code != VL_NOENT)
3166                 return code;
3167         }
3168         VlEntry->volumeId[ROVOL] = updateentry->ReadOnlyId;
3169         if ((code = HashVolid(ctx, ROVOL, blockindex, VlEntry)))
3170             return code;
3171     }
3172     if (updateentry->Mask & VLUPDATE_BACKUPID) {
3173         if ((code = UnhashVolid(ctx, BACKVOL, blockindex, VlEntry))) {
3174             if (code != VL_NOENT)
3175                 return code;
3176         }
3177         VlEntry->volumeId[BACKVOL] = updateentry->BackupId;
3178         if ((code = HashVolid(ctx, BACKVOL, blockindex, VlEntry)))
3179             return code;
3180     }
3181     if (updateentry->Mask & VLUPDATE_REPSITES) {
3182         if (updateentry->nModifiedRepsites <= 0
3183             || updateentry->nModifiedRepsites > OMAXNSERVERS)
3184             return VL_BADSERVER;
3185         for (i = 0; i < updateentry->nModifiedRepsites; i++) {
3186 /*          if (updateentry->RepsitesTargetServer[i] < 0 || updateentry->RepsitesTargetServer[i] > MAXSERVERID)
3187                 return VL_BADSERVER;    */
3188             if (updateentry->RepsitesTargetPart[i] < 0
3189                 || updateentry->RepsitesTargetPart[i] > MAXPARTITIONID)
3190                 return VL_BADPARTITION;
3191             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_DELETE) {
3192                 if ((j =
3193                      repsite_exists(VlEntry,
3194                                     IpAddrToRelAddr(ctx, updateentry->
3195                                                     RepsitesTargetServer[i],
3196                                                     1),
3197                                     updateentry->RepsitesTargetPart[i])) !=
3198                     -1)
3199                     repsite_compress(VlEntry, j);
3200                 else
3201                     return VL_NOREPSERVER;
3202             }
3203             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_ADD) {
3204 /*              if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
3205                     return VL_BADSERVER;                */
3206                 if (updateentry->RepsitesNewPart[i] < 0
3207                     || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
3208                     return VL_BADPARTITION;
3209                 if (repsite_exists
3210                     (VlEntry,
3211                      IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i], 1),
3212                      updateentry->RepsitesNewPart[i]) != -1)
3213                     return VL_DUPREPSERVER;
3214                 for (j = 0;
3215                      VlEntry->serverNumber[j] != BADSERVERID
3216                      && j < OMAXNSERVERS; j++);
3217                 if (j >= OMAXNSERVERS)
3218                     return VL_REPSFULL;
3219                 if ((serverindex =
3220                      IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i],
3221                                      1)) == -1)
3222                     return VL_BADSERVER;
3223                 VlEntry->serverNumber[j] = serverindex;
3224                 VlEntry->serverPartition[j] = updateentry->RepsitesNewPart[i];
3225                 if (updateentry->RepsitesNewFlags[i] < 0
3226                     || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
3227                     return VL_BADSERVERFLAG;
3228                 VlEntry->serverFlags[j] = updateentry->RepsitesNewFlags[i];
3229             }
3230             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODSERV) {
3231 /*n             if (updateentry->RepsitesNewServer[i] < 0 || updateentry->RepsitesNewServer[i] > MAXSERVERID)
3232                     return VL_BADSERVER;            */
3233                 if ((j =
3234                      repsite_exists(VlEntry,
3235                                     IpAddrToRelAddr(ctx, updateentry->
3236                                                     RepsitesTargetServer[i],
3237                                                     1),
3238                                     updateentry->RepsitesTargetPart[i])) !=
3239                     -1) {
3240                     VlEntry->serverNumber[j] =
3241                         IpAddrToRelAddr(ctx, updateentry->RepsitesNewServer[i],
3242                                         1);
3243                 } else
3244                     return VL_NOREPSERVER;
3245             }
3246             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODPART) {
3247                 if (updateentry->RepsitesNewPart[i] < 0
3248                     || updateentry->RepsitesNewPart[i] > MAXPARTITIONID)
3249                     return VL_BADPARTITION;
3250                 if ((j =
3251                      repsite_exists(VlEntry,
3252                                     IpAddrToRelAddr(ctx, updateentry->
3253                                                     RepsitesTargetServer[i],
3254                                                     1),
3255                                     updateentry->RepsitesTargetPart[i])) !=
3256                     -1)
3257                     VlEntry->serverPartition[j] =
3258                         updateentry->RepsitesNewPart[i];
3259                 else
3260                     return VL_NOREPSERVER;
3261             }
3262             if (updateentry->RepsitesMask[i] & VLUPDATE_REPS_MODFLAG) {
3263                 if ((j =
3264                      repsite_exists(VlEntry,
3265                                     IpAddrToRelAddr(ctx, updateentry->
3266                                                     RepsitesTargetServer[i],
3267                                                     1),
3268                                     updateentry->RepsitesTargetPart[i])) !=
3269                     -1) {
3270                     if (updateentry->RepsitesNewFlags[i] < 0
3271                         || updateentry->RepsitesNewFlags[i] > MAXSERVERFLAG)
3272                         return VL_BADSERVERFLAG;
3273                     VlEntry->serverFlags[j] =
3274                         updateentry->RepsitesNewFlags[i];
3275                 } else
3276                     return VL_NOREPSERVER;
3277             }
3278         }
3279     }
3280     return 0;
3281 }
3282
3283
3284 /* Check if the specified [server,partition] entry is found in the vldb
3285  * entry's repsite table; it's offset in the table is returned, if it's
3286  * present there. */
3287 static int
3288 repsite_exists(struct nvlentry *VlEntry, int server, int partition)
3289 {
3290     int i;
3291
3292     for (i = 0; VlEntry->serverNumber[i] != BADSERVERID && i < OMAXNSERVERS;
3293          i++) {
3294         if ((VlEntry->serverNumber[i] == server)
3295             && (VlEntry->serverPartition[i] == partition))
3296             return i;
3297     }
3298     return -1;
3299 }
3300
3301
3302
3303 /* Repsite table compression: used when deleting a repsite entry so that
3304  * all active repsite entries are on the top of the table. */
3305 static void
3306 repsite_compress(struct nvlentry *VlEntry, int offset)
3307 {
3308     int repsite_offset = offset;
3309     for (;
3310          VlEntry->serverNumber[repsite_offset] != BADSERVERID
3311          && repsite_offset < OMAXNSERVERS - 1; repsite_offset++) {
3312         VlEntry->serverNumber[repsite_offset] =
3313             VlEntry->serverNumber[repsite_offset + 1];
3314         VlEntry->serverPartition[repsite_offset] =
3315             VlEntry->serverPartition[repsite_offset + 1];
3316         VlEntry->serverFlags[repsite_offset] =
3317             VlEntry->serverFlags[repsite_offset + 1];
3318     }
3319     VlEntry->serverNumber[repsite_offset] = BADSERVERID;
3320 }
3321
3322
3323 /* Convert from the internal (compacted) vldb entry to the external
3324  * representation used by the interface. */
3325 static int
3326 vlentry_to_vldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3327                      struct vldbentry *VldbEntry)
3328 {
3329     int i, j, code;
3330     struct extentaddr *exp;
3331
3332     memset(VldbEntry, 0, sizeof(struct vldbentry));
3333     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3334     for (i = 0; i < OMAXNSERVERS; i++) {
3335         if (VlEntry->serverNumber[i] == BADSERVERID)
3336             break;
3337         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3338         if (code)
3339             return code;
3340         if (exp) {
3341             /* For now return the first ip address back */
3342             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3343                 if (exp->ex_addrs[j]) {
3344                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
3345                     break;
3346                 }
3347             }
3348         } else
3349             VldbEntry->serverNumber[i] =
3350                 ctx->hostaddress[VlEntry->serverNumber[i]];
3351         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3352         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3353     }
3354     VldbEntry->nServers = i;
3355     for (i = 0; i < MAXTYPES; i++)
3356         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3357     VldbEntry->cloneId = VlEntry->cloneId;
3358     VldbEntry->flags = VlEntry->flags;
3359
3360     return 0;
3361 }
3362
3363
3364 /* Convert from the internal (compacted) vldb entry to the external
3365  * representation used by the interface. */
3366 static int
3367 vlentry_to_nvldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3368                       struct nvldbentry *VldbEntry)
3369 {
3370     int i, j, code;
3371     struct extentaddr *exp;
3372
3373     memset(VldbEntry, 0, sizeof(struct nvldbentry));
3374     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3375     for (i = 0; i < NMAXNSERVERS; i++) {
3376         if (VlEntry->serverNumber[i] == BADSERVERID)
3377             break;
3378         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3379         if (code)
3380             return code;
3381
3382         if (exp) {
3383             /* For now return the first ip address back */
3384             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3385                 if (exp->ex_addrs[j]) {
3386                     VldbEntry->serverNumber[i] = ntohl(exp->ex_addrs[j]);
3387                     break;
3388                 }
3389             }
3390         } else
3391             VldbEntry->serverNumber[i] =
3392                 ctx->hostaddress[VlEntry->serverNumber[i]];
3393         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3394         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3395     }
3396     VldbEntry->nServers = i;
3397     for (i = 0; i < MAXTYPES; i++)
3398         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3399     VldbEntry->cloneId = VlEntry->cloneId;
3400     VldbEntry->flags = VlEntry->flags;
3401
3402     return 0;
3403 }
3404
3405 static int
3406 vlentry_to_uvldbentry(struct vl_ctx *ctx, struct nvlentry *VlEntry,
3407                       struct uvldbentry *VldbEntry)
3408 {
3409     int i, code;
3410     struct extentaddr *exp;
3411
3412     memset(VldbEntry, 0, sizeof(struct uvldbentry));
3413     strncpy(VldbEntry->name, VlEntry->name, sizeof(VldbEntry->name));
3414     for (i = 0; i < NMAXNSERVERS; i++) {
3415         if (VlEntry->serverNumber[i] == BADSERVERID)
3416             break;
3417         VldbEntry->serverFlags[i] = VlEntry->serverFlags[i];
3418         VldbEntry->serverUnique[i] = 0;
3419         code = multiHomedExtent(ctx, VlEntry->serverNumber[i], &exp);
3420         if (code)
3421             return code;
3422
3423         if (exp) {
3424             afsUUID tuuid;
3425
3426             tuuid = exp->ex_hostuuid;
3427             afs_ntohuuid(&tuuid);
3428             VldbEntry->serverFlags[i] |= VLSF_UUID;
3429             VldbEntry->serverNumber[i] = tuuid;
3430             VldbEntry->serverUnique[i] = ntohl(exp->ex_uniquifier);
3431         } else {
3432             VldbEntry->serverNumber[i].time_low =
3433                 ctx->hostaddress[VlEntry->serverNumber[i]];
3434         }
3435         VldbEntry->serverPartition[i] = VlEntry->serverPartition[i];
3436
3437     }
3438     VldbEntry->nServers = i;
3439     for (i = 0; i < MAXTYPES; i++)
3440         VldbEntry->volumeId[i] = VlEntry->volumeId[i];
3441     VldbEntry->cloneId = VlEntry->cloneId;
3442     VldbEntry->flags = VlEntry->flags;
3443
3444     return 0;
3445 }
3446
3447 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
3448
3449
3450 /* Verify that the volname is a valid volume name. */
3451 static int
3452 InvalidVolname(char *volname)
3453 {
3454     char *map;
3455     int slen;
3456
3457     map = LEGALCHARS;
3458     slen = strlen(volname);
3459     if (slen >= VL_MAXNAMELEN)
3460         return 1;
3461     return (slen != strspn(volname, map));
3462 }
3463
3464
3465 /* Verify that the given volume type is valid. */
3466 static int
3467 InvalidVoltype(afs_int32 voltype)
3468 {
3469     if (voltype != RWVOL && voltype != ROVOL && voltype != BACKVOL)
3470         return 1;
3471     return 0;
3472 }
3473
3474
3475 static int
3476 InvalidOperation(afs_int32 voloper)
3477 {
3478     if (voloper != VLOP_MOVE && voloper != VLOP_RELEASE
3479         && voloper != VLOP_BACKUP && voloper != VLOP_DELETE
3480         && voloper != VLOP_DUMP)
3481         return 1;
3482     return 0;
3483 }
3484
3485 static int
3486 InvalidReleasetype(afs_int32 releasetype)
3487 {
3488     if ((releasetype & LOCKREL_TIMESTAMP) || (releasetype & LOCKREL_OPCODE)
3489         || (releasetype & LOCKREL_AFSID))
3490         return 0;
3491     return 1;
3492 }
3493
3494 static int
3495 IpAddrToRelAddr(struct vl_ctx *ctx, afs_uint32 ipaddr, int create)
3496 {
3497     int i, j;
3498     afs_int32 code;
3499     struct extentaddr *exp;
3500
3501     for (i = 0; i <= MAXSERVERID; i++) {
3502         if (ctx->hostaddress[i] == ipaddr)
3503             return i;
3504         code = multiHomedExtent(ctx, i, &exp);
3505         if (code)
3506             return -1;
3507         if (exp) {
3508             for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
3509                 if (exp->ex_addrs[j] && (ntohl(exp->ex_addrs[j]) == ipaddr)) {
3510                     return i;
3511                 }
3512             }
3513         }
3514     }
3515
3516     /* allocate the new server a server id pronto */
3517     if (create) {
3518         for (i = 0; i <= MAXSERVERID; i++) {
3519             if (ctx->cheader->IpMappedAddr[i] == 0) {
3520                 ctx->cheader->IpMappedAddr[i] = htonl(ipaddr);
3521                 code =
3522                     vlwrite(ctx->trans,
3523                             DOFFSET(0, ctx->cheader, &ctx->cheader->IpMappedAddr[i]),
3524                             (char *)&ctx->cheader->IpMappedAddr[i],
3525                             sizeof(afs_int32));
3526                 ctx->hostaddress[i] = ipaddr;
3527                 if (code)
3528                     return -1;
3529                 return i;
3530             }
3531         }
3532     }
3533     return -1;
3534 }
3535
3536 static int
3537 ChangeIPAddr(struct vl_ctx *ctx, afs_uint32 ipaddr1, afs_uint32 ipaddr2)
3538 {
3539     int i, j;
3540     afs_int32 code;
3541     struct extentaddr *exp = NULL;
3542     int base = -1;
3543     int mhidx;
3544     afsUUID tuuid;
3545     afs_int32 blockindex, count;
3546     int pollcount = 0;
3547     struct nvlentry tentry;
3548     int ipaddr1_id = -1, ipaddr2_id = -1;
3549     char addrbuf1[256];
3550     char addrbuf2[256];
3551
3552     /* Don't let addr change to 255.*.*.* : Causes internal error below */
3553     if ((ipaddr2 & 0xff000000) == 0xff000000)
3554         return (VL_BADSERVER);
3555
3556     /* If we are removing an address, ip1 will be -1 and ip2 will be
3557      * the original address. This prevents an older revision vlserver
3558      * from removing the IP address (won't find server 0xfffffff in
3559      * the VLDB). An older revision vlserver does not have the check
3560      * to see if any volumes exist on the server being removed.
3561      */
3562     if (ipaddr1 == 0xffffffff) {
3563         ipaddr1 = ipaddr2;
3564         ipaddr2 = 0;
3565     }
3566
3567     for (i = 0; i <= MAXSERVERID; i++) {
3568         struct extentaddr *texp = NULL;
3569         int tbase;
3570
3571         code = multiHomedExtentBase(ctx, i, &texp, &tbase);
3572         if (code)
3573             return code;
3574
3575         if (texp) {
3576             for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
3577                 if (!texp->ex_addrs[mhidx])
3578                     continue;
3579                 if (ntohl(texp->ex_addrs[mhidx]) == ipaddr1) {
3580                     ipaddr1_id = i;
3581                     exp = texp;
3582                     base = tbase;
3583                 }
3584                 if (ipaddr2 != 0 && ntohl(texp->ex_addrs[mhidx]) == ipaddr2) {
3585                     ipaddr2_id = i;
3586                 }
3587             }
3588         } else {
3589             if (ctx->hostaddress[i] == ipaddr1) {
3590                 exp = NULL;
3591                 base = -1;
3592                 ipaddr1_id = i;
3593             }
3594             if (ipaddr2 != 0 && ctx->hostaddress[i] == ipaddr2) {
3595                 ipaddr2_id = i;
3596             }
3597         }
3598
3599         if (ipaddr1_id >= 0 && (ipaddr2 == 0 || ipaddr2_id >= 0)) {
3600             /* we've either found both IPs already in the VLDB, or we found
3601              * ipaddr1, and we're not going to find ipaddr2 because it's 0 */
3602             break;
3603         }
3604     }
3605
3606     if (ipaddr1_id < 0) {
3607         return VL_NOENT;        /* not found */
3608     }
3609
3610     if (ipaddr2_id >= 0 && ipaddr2_id != ipaddr1_id) {
3611         char buf1[16], buf2[16];
3612         VLog(0, ("Cannot change IP address from %s to %s because the latter "
3613                  "is in use by server id %d\n",
3614                  afs_inet_ntoa_r(htonl(ipaddr1), buf1),
3615                  afs_inet_ntoa_r(htonl(ipaddr2), buf2),
3616                  ipaddr2_id));
3617         return VL_MULTIPADDR;
3618     }
3619
3620     /* If we are removing a server entry, a volume cannot
3621      * exist on the server. If one does, don't remove the
3622      * server entry: return error "volume entry exists".
3623      */
3624     if (ipaddr2 == 0) {
3625         for (blockindex = NextEntry(ctx, 0, &tentry, &count); blockindex;
3626              blockindex = NextEntry(ctx, blockindex, &tentry, &count)) {
3627             if (++pollcount > 50) {
3628 #ifndef AFS_PTHREAD_ENV
3629                 IOMGR_Poll();
3630 #endif
3631                 pollcount = 0;
3632             }
3633             for (j = 0; j < NMAXNSERVERS; j++) {
3634                 if (tentry.serverNumber[j] == BADSERVERID)
3635                     break;
3636                 if (tentry.serverNumber[j] == ipaddr1_id) {
3637                     return VL_IDEXIST;
3638                 }
3639             }
3640         }
3641     } else if (exp) {
3642         /* Do not allow changing addresses in multi-homed entries.
3643            Older versions of this RPC would silently "downgrade" mh entries
3644            to single-homed entries and orphan the mh enties. */
3645         addrbuf1[0] = '\0';
3646         append_addr(addrbuf1, ipaddr1, sizeof(addrbuf1));
3647         VLog(0, ("Refusing to change address %s in multi-homed entry; "
3648                  "use RegisterAddrs instead.\n", addrbuf1));
3649         return VL_NOENT;        /* single-homed entry not found */
3650     }
3651
3652     /* Log a message saying we are changing/removing an IP address */
3653     VLog(0,
3654          ("The following IP address is being %s:\n",
3655           (ipaddr2 ? "changed" : "removed")));
3656     addrbuf1[0] = addrbuf2[0] = '\0';
3657     if (exp) {
3658         for (mhidx = 0; mhidx < VL_MAXIPADDRS_PERMH; mhidx++) {
3659             if (!exp->ex_addrs[mhidx])
3660                 continue;
3661             if (mhidx > 0)
3662                 strlcat(addrbuf1, " ", sizeof(addrbuf1));
3663             append_addr(addrbuf1, ntohl(exp->ex_addrs[mhidx]), sizeof(addrbuf1));
3664         }
3665     } else {
3666         append_addr(addrbuf1, ipaddr1, sizeof(addrbuf1));
3667     }
3668     if (ipaddr2) {
3669         append_addr(addrbuf2, ipaddr2, sizeof(addrbuf2));
3670     }
3671     VLog(0, ("      entry %d: [%s] -> [%s]\n", ipaddr1_id, addrbuf1, addrbuf2));
3672
3673     /* Change the registered uuuid addresses */
3674     if (exp && base != -1) {
3675         memset(&tuuid, 0, sizeof(afsUUID));
3676         afs_htonuuid(&tuuid);
3677         exp->ex_hostuuid = tuuid;
3678         code =
3679             vlwrite(ctx->trans,
3680                     DOFFSET(ntohl(ctx->ex_addr[0]->ex_contaddrs[base]),
3681                             (char *)ctx->ex_addr[base], (char *)exp),
3682                     (char *)&tuuid, sizeof(tuuid));
3683         if (code)
3684             return VL_IO;
3685     }
3686
3687     /* Now change the host address entry */
3688     ctx->cheader->IpMappedAddr[ipaddr1_id] = htonl(ipaddr2);
3689     code =
3690         vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->IpMappedAddr[ipaddr1_id]),
3691                 (char *)
3692                 &ctx->cheader->IpMappedAddr[ipaddr1_id], sizeof(afs_int32));
3693     ctx->hostaddress[ipaddr1_id] = ipaddr2;
3694     if (code)
3695         return VL_IO;
3696
3697     return 0;
3698 }
3699
3700 /* see if the vlserver is back yet */
3701 afs_int32
3702 SVL_ProbeServer(struct rx_call *rxcall)
3703 {
3704     return 0;
3705 }