venus: Remove dedebug
[openafs.git] / src / budb / procs.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 /* TBD
11  *      ht_lookupEntry - tape ids
12  *      Truncate Tape - tape id's
13  *      usetape - tape id's
14  */
15
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 #include <roken.h>
20
21 #include <afs/stds.h>
22 #include <afs/bubasics.h>
23 #include <lock.h>
24 #include <ubik.h>
25 #include <lwp.h>
26 #include <rx/xdr.h>
27 #include <rx/rx.h>
28 #include <rx/rxkad.h>
29 #include <afs/cellconfig.h>
30 #include <afs/auth.h>
31 #include <afs/audit.h>
32 #include <afs/afsutil.h>
33
34 #include "budb.h"
35 #include "budb_errs.h"
36 #include "database.h"
37 #include "budb_internal.h"
38 #include "error_macros.h"
39 #include "globals.h"
40
41 #undef min
42 #undef max
43
44
45 extern struct ubik_dbase *BU_dbase;
46 extern struct afsconf_dir *BU_conf;     /* for getting cell info */
47
48 afs_int32 AddVolume(struct rx_call *, struct budb_volumeEntry *);
49 afs_int32 AddVolumes(struct rx_call *, struct budb_volumeList *);
50 afs_int32 CreateDump(struct rx_call *, struct budb_dumpEntry *);
51 afs_int32 DoDeleteDump(struct rx_call *, dumpId, Date, Date, budb_dumpsList *);
52 afs_int32 DoDeleteTape(struct rx_call *, struct budb_tapeEntry *);
53 afs_int32 ListDumps(struct rx_call *, afs_int32, afs_int32, Date, Date,
54                     budb_dumpsList *, budb_dumpsList *);
55 afs_int32 DeleteVDP(struct rx_call *, char *, char *, afs_int32);
56 afs_int32 FindClone(struct rx_call *, afs_int32, char *, afs_int32 *);
57 afs_int32 FindDump(struct rx_call *, char *, afs_int32,
58                    struct budb_dumpEntry *);
59 afs_int32 FindLatestDump(struct rx_call *, char *, char *,
60                          struct budb_dumpEntry *);
61 afs_int32 FinishDump(struct rx_call *, struct budb_dumpEntry *);
62 afs_int32 FinishTape(struct rx_call *, struct budb_tapeEntry *);
63 afs_int32 GetDumps(struct rx_call *, afs_int32, afs_int32, char *,
64                    afs_int32, afs_int32, afs_int32, afs_int32 *,
65                    afs_int32 *, budb_dumpList *);
66 afs_int32 getExpiration(struct ubik_trans *ut, struct tape *);
67 afs_int32 makeAppended(struct ubik_trans *ut, afs_int32, afs_int32,
68                        afs_int32);
69 afs_int32 MakeDumpAppended(struct rx_call *, afs_int32, afs_int32,
70                            afs_int32);
71 afs_int32 FindLastTape(struct rx_call *, afs_int32, struct budb_dumpEntry *,
72                        struct budb_tapeEntry *, struct budb_volumeEntry *);
73 afs_int32 GetTapes(struct rx_call *, afs_int32, afs_int32, char *, afs_int32,
74                    afs_int32, afs_int32, afs_int32 *, afs_int32 *,
75                    budb_tapeList *);
76 afs_int32 GetVolumes(struct rx_call *, afs_int32, afs_int32, char *,
77                      afs_int32, afs_int32, afs_int32, afs_int32 *,
78                      afs_int32 *, budb_volumeList *);
79 afs_int32 UseTape(struct rx_call *, struct budb_tapeEntry *, int *);
80 afs_int32 T_DumpHashTable(struct rx_call *, int, char *);
81 afs_int32 T_GetVersion(struct rx_call *, int *);
82 afs_int32 T_DumpDatabase(struct rx_call *, char *);
83
84 int volFragsDump(struct ubik_trans *, FILE *, dbadr);
85
86 /* Text block management */
87
88 struct memTextBlock {
89     struct memTextBlock *mtb_next;      /* next in chain */
90     afs_int32 mtb_nbytes;       /* # of bytes in this block */
91     struct blockHeader mtb_blkHeader;   /* in memory header */
92     dbadr mtb_addr;             /* disk address of block */
93 };
94
95 typedef struct memTextBlock memTextBlockT;
96 typedef memTextBlockT *memTextBlockP;
97
98 /* These variable are for returning debugging info about the state of the
99    server.  If they get trashed during multi-threaded operation it doesn't
100    matter. */
101
102 /* This is global so COUNT_REQ in krb_udp.c can refer to it. */
103 char *lastOperation;            /* name of last operation */
104 static Date lastTrans;          /* time of last transaction */
105
106 /* procsInited is sort of a lock: during a transaction only one process runs
107    while procsInited is false. */
108
109 static int procsInited = 0;
110
111 /* This variable is protected by the procsInited flag. */
112
113 static int (*rebuildDatabase) (struct ubik_trans *);
114
115 /* AwaitInitialization
116  * Wait unitl budb has initialized (InitProcs). If it hasn't
117  * within 5 seconds, then return no quorum.
118  */
119 afs_int32
120 AwaitInitialization(void)
121 {
122     afs_int32 start = 0;
123
124     while (!procsInited) {
125         if (!start)
126             start = time(0);
127         else if (time(0) - start > 5)
128             return UNOQUORUM;
129 #ifdef AFS_PTHREAD_ENV
130         sleep(1);
131 #else
132         IOMGR_Sleep(1);
133 #endif
134     }
135     return 0;
136 }
137
138 /* tailCompPtr
139  *      name is a pathname style name, determine trailing name and return
140  *      pointer to it
141  */
142
143 char *
144 tailCompPtr(char *pathNamePtr)
145 {
146     char *ptr;
147     ptr = strrchr(pathNamePtr, '/');
148     if (ptr == 0) {
149         /* this should never happen */
150         LogError(0, "tailCompPtr: could not find / in name(%s)\n",
151                  pathNamePtr);
152         return (pathNamePtr);
153     } else
154         ptr++;                  /* skip the / */
155     return (ptr);
156 }
157
158 /* callPermitted
159  *      Check to see if the caller is a SuperUser.
160  * exit:
161  *      0 - no
162  *      1 - yes
163  */
164
165 int
166 callPermitted(struct rx_call *call)
167 {
168     int permitted = 0;
169     struct afsconf_dir *acdir;
170
171     acdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
172     if (!acdir)
173         return 0;
174
175     if (afsconf_SuperUser(acdir, call, NULL))
176         permitted = 1;
177
178     if (acdir)
179         afsconf_Close(acdir);
180     return (permitted);
181 }
182
183 /* InitRPC
184  *      This is called by every RPC interface to create a Ubik transaction
185  *      and read the database header into core
186  * entry:
187  *      ut
188  *      lock
189  *      this_op
190  * notes:
191  *      sets a lock on byte 1 of the database. Looks like it enforces
192  *      single threading by use of the lock.
193  */
194
195 afs_int32
196 InitRPC(struct ubik_trans **ut,
197         int lock,               /* indicate read/write transaction */
198         int this_op)            /* opcode of RCP, for COUNT_ABO */
199 {
200     int code;
201     float wait = 0.91;          /* start waiting for 1 second */
202
203   start:
204     /* wait for server initialization to finish if this is not InitProcs calling */
205     if (this_op)
206         if ((code = AwaitInitialization()))
207             return code;
208
209     for (code = UNOQUORUM; code == UNOQUORUM;) {
210         code =
211             ubik_BeginTrans(BU_dbase,
212                             ((lock ==
213                               LOCKREAD) ? UBIK_READTRANS : UBIK_WRITETRANS),
214                             ut);
215         if (code == UNOQUORUM) {        /* no quorum elected */
216             if (wait < 1)
217                 Log("Waiting for quorum election\n");
218             if (wait < 15.0)
219                 wait *= 1.1;
220 #ifdef AFS_PTHREAD_ENV
221             sleep((int)wait);
222 #else
223             IOMGR_Sleep((int)wait);
224 #endif
225         }
226     }
227     if (code)
228         return code;
229     if (wait > 1)
230         Log("Have established quorum\n");
231
232     /* set lock at posiion 1, for 1 byte of type lock */
233     if ((code = ubik_SetLock(*ut, 1, 1, lock))) {
234         ubik_AbortTrans(*ut);
235         return code;
236     }
237
238     /* check that dbase is initialized and setup cheader */
239     if (lock == LOCKREAD) {
240         /* init but don't fix because this is read only */
241         if ((code = CheckInit(*ut, 0))) {
242             ubik_AbortTrans(*ut);
243             if ((code = InitRPC(ut, LOCKWRITE, 0))) {   /* Now fix the database */
244                 LogError(code, "InitRPC: InitRPC failed\n");
245                 return code;
246             }
247             if ((code = ubik_EndTrans(*ut))) {
248                 LogError(code, "InitRPC: ubik_EndTrans failed\n");
249                 return code;
250             }
251             goto start;         /* now redo the read transaction */
252         }
253     } else {
254         if ((code = CheckInit(*ut, rebuildDatabase))) {
255             ubik_AbortTrans(*ut);
256             return code;
257         }
258     }
259     lastTrans = time(0);
260     return 0;
261 }
262
263 /* This is called to initialize a newly created database */
264 static int
265 initialize_database(struct ubik_trans *ut)
266 {
267     return 0;
268 }
269
270 static int noAuthenticationRequired;    /* global state */
271 static int recheckNoAuth;       /* global state */
272
273 afs_int32
274 InitProcs(void)
275 {
276     struct ubik_trans *ut;
277     afs_int32 code = 0;
278
279     procsInited = 0;
280
281     if ((globalConfPtr->myHost == 0) || (BU_conf == 0))
282         ERROR(BUDB_INTERNALERROR);
283
284     recheckNoAuth = 1;
285
286     if (globalConfPtr->debugFlags & DF_NOAUTH)
287         noAuthenticationRequired = 1;
288
289     if (globalConfPtr->debugFlags & DF_RECHECKNOAUTH)
290         recheckNoAuth = 0;
291
292     if (recheckNoAuth)
293         noAuthenticationRequired = afsconf_GetNoAuthFlag(BU_conf);
294
295     if (noAuthenticationRequired)
296         LogError(0, "Running server with security disabled\n");
297
298     InitDB();
299
300     rebuildDatabase = initialize_database;
301
302     if ((code = InitRPC(&ut, LOCKREAD, 0))) {
303         LogError(code, "InitProcs: InitRPC failed\n");
304         return code;
305     }
306     code = ubik_EndTrans(ut);
307     if (code) {
308         LogError(code, "InitProcs: ubik_EndTrans failed\n");
309         return code;
310     }
311
312     rebuildDatabase = 0;        /* only do this during init */
313     procsInited = 1;
314
315   error_exit:
316     return (code);
317 }
318
319 struct returnList {
320     int nElements;              /* number in list */
321     int allocSize;              /* number of elements allocated */
322     dbadr *elements;            /* array of addresses */
323 };
324
325 static void
326 InitReturnList(struct returnList *list)
327 {
328     list->nElements = 0;
329     list->allocSize = 0;
330     list->elements = (dbadr *) 0;
331 }
332
333 static void
334 FreeReturnList(struct returnList *list)
335 {
336     if (list->elements)
337         free(list->elements);
338     list->elements = (dbadr *) 0;
339     list->nElements = 0;
340 }
341
342 /* As entries are collected, they are added to a return list. Once all
343  * entries have been collected, it is then placed in the return buffer
344  * with SendReturnList(). The first *to_skipP are not recorded.
345  */
346 static afs_int32
347 AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP)
348 {
349     dbadr *tmp;
350     afs_int32 size;
351
352     if (a == 0)
353         return 0;
354     if (*to_skipP > 0) {
355         (*to_skipP)--;
356         return 0;
357     }
358
359     /* Up to 5 plus a maximum so SendReturnList() knows if we
360      * need to come back for more.
361      */
362     if (list->nElements >= BUDB_MAX_RETURN_LIST + 5)
363         return BUDB_LIST2BIG;
364
365     if (list->nElements >= list->allocSize) {
366         if (list->elements == 0) {
367             size = 10;
368             tmp = malloc(sizeof(dbadr) * size);
369         } else {
370             size = list->allocSize + 10;
371             tmp = realloc(list->elements, sizeof(dbadr) * size);
372         }
373         if (!tmp)
374             return BUDB_NOMEM;
375         list->elements = tmp;
376         list->allocSize = size;
377     }
378
379     list->elements[list->nElements] = a;
380     list->nElements++;
381     return 0;
382 }
383
384 afs_int32
385 FillVolEntry(struct ubik_trans *ut, dbadr va, void *rock)
386 {
387     struct budb_volumeEntry *vol = (struct budb_volumeEntry *) rock;
388     struct dump d;
389     struct tape t;
390     struct volInfo vi;
391     struct volFragment vf;
392
393     if (dbread(ut, va, &vf, sizeof(vf)))
394         return BUDB_IO;         /* The volFrag */
395     if (dbread(ut, ntohl(vf.vol), &vi, sizeof(vi)))
396         return BUDB_IO;         /* The volInfo */
397     if (dbread(ut, ntohl(vf.tape), &t, sizeof(t)))
398         return BUDB_IO;         /* The tape */
399     if (dbread(ut, ntohl(t.dump), &d, sizeof(d)))
400         return BUDB_IO;         /* The dump */
401
402     strcpy(vol->name, vi.name);
403     strcpy(vol->server, vi.server);
404     strcpy(vol->tape, t.name);
405     vol->tapeSeq = ntohl(t.seq);
406     vol->dump = ntohl(d.id);
407     vol->position = ntohl(vf.position);
408     vol->clone = ntohl(vf.clone);
409     vol->incTime = ntohl(vf.incTime);
410     vol->nBytes = ntohl(vf.nBytes);
411     vol->startByte = ntohl(vf.startByte);
412     vol->flags = (ntohs(vf.flags) & VOLFRAGMENTFLAGS);  /* low  16 bits here */
413     vol->flags |= (ntohl(vi.flags) & VOLINFOFLAGS);     /* high 16 bits here */
414     vol->seq = ntohs(vf.sequence);
415     vol->id = ntohl(vi.id);
416     vol->partition = ntohl(vi.partition);
417
418     return 0;
419 }
420
421 afs_int32
422 FillDumpEntry(struct ubik_trans *ut, dbadr da, void *rock)
423 {
424     struct budb_dumpEntry *dump = (struct budb_dumpEntry *)rock;
425     struct dump d, ad;
426
427     memset(dump, 0, sizeof(*dump));
428     if (dbread(ut, da, &d, sizeof(d)))
429         return BUDB_IO;
430     dump->id = ntohl(d.id);
431     dump->flags = ntohl(d.flags);
432     dump->created = ntohl(d.created);
433     strncpy(dump->name, d.dumpName, sizeof(dump->name));
434     strncpy(dump->dumpPath, d.dumpPath, sizeof(dump->dumpPath));
435     strncpy(dump->volumeSetName, d.volumeSet, sizeof(dump->volumeSetName));
436
437     dump->parent = ntohl(d.parent);
438     dump->level = ntohl(d.level);
439     dump->nVolumes = ntohl(d.nVolumes);
440
441     tapeSet_ntoh(&d.tapes, &dump->tapes);
442
443     if (strlen(d.dumper.name) < sizeof(dump->dumper.name))
444         strcpy(dump->dumper.name, d.dumper.name);
445     if (strlen(d.dumper.instance) < sizeof(dump->dumper.instance))
446         strcpy(dump->dumper.instance, d.dumper.instance);
447     if (strlen(d.dumper.cell) < sizeof(dump->dumper.cell))
448         strcpy(dump->dumper.cell, d.dumper.cell);
449
450     /* Get the initial dumpid and the appended dump id */
451     dump->initialDumpID = ntohl(d.initialDumpID);
452     if (d.appendedDumpChain) {
453         if (dbread(ut, ntohl(d.appendedDumpChain), &ad, sizeof(ad)))
454             return BUDB_IO;
455         dump->appendedDumpID = ntohl(ad.id);
456     } else
457         dump->appendedDumpID = 0;
458
459     /* printf("dump name %s, parent %d, level %d\n",
460      * d.dumpName, ntohl(d.parent), ntohl(d.level)); */
461
462     return 0;
463 }
464
465 afs_int32
466 FillTapeEntry(struct ubik_trans *ut, dbadr ta, void *rock)
467 {
468     struct budb_tapeEntry *tape=(struct budb_tapeEntry *) rock;
469     struct tape t;
470     struct dump d;
471     afs_int32 code;
472
473     if (dbread(ut, ta, &t, sizeof(t)))
474         return BUDB_IO;
475
476     /* Get the tape's expiration date */
477     if ((code = getExpiration(ut, &t)))
478         return (code);
479
480     strcpy(tape->name, t.name);
481     tape->flags = ntohl(t.flags);
482     tape->written = ntohl(t.written);
483     tape->expires = ntohl(t.expires);
484     tape->nMBytes = ntohl(t.nMBytes);
485     tape->nBytes = ntohl(t.nBytes);
486     tape->nFiles = ntohl(t.nFiles);
487     tape->nVolumes = ntohl(t.nVolumes);
488     tape->seq = ntohl(t.seq);
489     tape->labelpos = ntohl(t.labelpos);
490     tape->useCount = ntohl(t.useCount);
491     tape->useKBytes = ntohl(t.useKBytes);
492
493     if (dbread(ut, ntohl(t.dump), &d, sizeof(d)))
494         return BUDB_IO;
495     tape->dump = ntohl(d.id);
496     return 0;
497 }
498
499 #define returnList_t budb_dumpList *
500
501 /* SendReturnList
502  *      A list of elements of size e_size is in list, collected
503  *      with AddToReturnList(). We will move this to a correspoding
504  *      return list, eList, via FillProc(). nextInodeP tells us
505  *      if there are more and how many to skip on the next request.
506  */
507 static afs_int32
508 SendReturnList(struct ubik_trans *ut,
509                struct returnList *list, /* list of elements to return */
510                afs_int32(*FillProc) (struct ubik_trans *, dbadr da,
511                                      void *),
512                                         /* proc to fill entry */
513                int e_size,              /* size of each element */
514                afs_int32 index,         /* index from previous call */
515                afs_int32 *nextIndexP,   /* if more elements are available */
516                afs_int32 *dbTimeP,      /* time of last db update */
517                budb_dumpList *eList)    /* rxgen list structure (e.g.) */
518 {
519     afs_int32 code;
520     int to_return;
521     int i;
522     char *e;
523
524     *nextIndexP = -1;
525     *dbTimeP = ntohl(db.h.lastUpdate);
526
527     /* Calculate how many to return. Don't let if go over
528      * BUDB_MAX_RETURN_LIST nor the size of our return list.
529      */
530     to_return = list->nElements;
531     if (to_return > BUDB_MAX_RETURN_LIST)
532         to_return = BUDB_MAX_RETURN_LIST;
533     if (eList->budb_dumpList_len && (to_return > eList->budb_dumpList_len))
534         to_return = eList->budb_dumpList_len;
535
536     /* Allocate space for the return values if needed and zero it */
537     if (eList->budb_dumpList_val == 0) {
538         if (to_return > 0) {
539             eList->budb_dumpList_val = calloc(to_return, e_size);
540             if (!eList->budb_dumpList_val)
541                 return (BUDB_NOMEM);
542         } else
543             eList->budb_dumpList_val = NULL;
544     } else {
545         memset(eList->budb_dumpList_val, 0, e_size * to_return);
546     }
547
548     eList->budb_dumpList_len = to_return;
549
550     e = (char *)(eList->budb_dumpList_val);
551     for (i = 0; i < to_return; i++, e += e_size) {
552         code = (*FillProc) (ut, list->elements[i], (budb_dumpEntry *) e);
553         if (code)
554             return code;
555     }
556
557     if (list->nElements > i)
558         *nextIndexP = index + i;
559     return 0;
560 }
561
562 /* Come here to delete a volInfo structure. */
563
564 static afs_int32
565 DeleteVolInfo(struct ubik_trans *ut, dbadr via, struct volInfo *vi)
566 {
567     afs_int32 code;
568     dbadr hvia;
569     struct volInfo hvi;
570
571     if (vi->firstFragment)
572         return 0;               /* still some frags, don't free yet */
573     if (vi->sameNameHead == 0) {        /* this is the head */
574         if (vi->sameNameChain)
575             return 0;           /* empty head, some non-heads left */
576
577         code = ht_HashOut(ut, &db.volName, via, vi);
578         if (code)
579             return code;
580         code = FreeStructure(ut, volInfo_BLOCK, via);
581         return code;
582     }
583     hvia = ntohl(vi->sameNameHead);
584     if (dbread(ut, hvia, &hvi, sizeof(hvi)))
585         return BUDB_IO;
586     code =
587         RemoveFromList(ut, hvia, &hvi, &hvi.sameNameChain, via, vi,
588                        &vi->sameNameChain);
589     if (code == -1)
590         return BUDB_DATABASEINCONSISTENT;
591     if (code == 0)
592         code = FreeStructure(ut, volInfo_BLOCK, via);
593     return code;
594 }
595
596 /* Detach a volume fragment from its volInfo structure.  Its tape chain is
597    already freed.  This routine frees the structure and the caller must not
598    write it out. */
599
600 static afs_int32
601 DeleteVolFragment(struct ubik_trans *ut, dbadr va, struct volFragment *v)
602 {
603     afs_int32 code;
604     struct volInfo vi;
605     dbadr via;
606
607     via = ntohl(v->vol);
608     if (dbread(ut, via, &vi, sizeof(vi)))
609         return BUDB_IO;
610     code =
611         RemoveFromList(ut, via, &vi, &vi.firstFragment, va, v,
612                        &v->sameNameChain);
613     if (code == -1)
614         return BUDB_DATABASEINCONSISTENT;
615     if (code)
616         return code;
617     if (vi.firstFragment == 0)
618         if ((code = DeleteVolInfo(ut, via, &vi)))
619             return code;
620     if ((code = FreeStructure(ut, volFragment_BLOCK, va)))
621         return code;
622
623     /* decrement frag counter */
624     code =
625         set_word_addr(ut, via, &vi, &vi.nFrags, htonl(ntohl(vi.nFrags) - 1));
626     if (code)
627         return code;
628     return 0;
629 }
630
631 /* DeleteTape - by freeing all its volumes and removing it from its dump chain.
632  * The caller will remove it from the hash table if necessary.  The caller is
633  * also responsible for writing the tape out if necessary. */
634
635 static afs_int32
636 DeleteTape(struct ubik_trans *ut, dbadr ta, struct tape *t)
637 {
638     afs_int32 code;
639     struct dump d;
640     dbadr da;
641
642     da = ntohl(t->dump);
643     if (da == 0)
644         return BUDB_DATABASEINCONSISTENT;
645     if (dbread(ut, da, &d, sizeof(d)))
646         return BUDB_IO;
647     if (d.firstTape == 0)
648         return BUDB_DATABASEINCONSISTENT;
649
650     code = RemoveFromList(ut, da, &d, &d.firstTape, ta, t, &t->nextTape);
651     if (code == -1)
652         return BUDB_DATABASEINCONSISTENT;
653     if (code)
654         return code;
655
656     /* Since the tape should have been truncated there should never be any
657      * volumes in the tape. */
658     if (t->firstVol || t->nVolumes)
659         return BUDB_DATABASEINCONSISTENT;
660
661     return 0;
662 }
663
664 static afs_int32
665 DeleteDump(struct ubik_trans *ut, dbadr da, struct dump *d)
666 {
667     afs_int32 code = 0;
668
669     code = ht_HashOut(ut, &db.dumpIden, da, d);
670     if (code)
671         ERROR(code);
672
673     code = ht_HashOut(ut, &db.dumpName, da, d);
674     if (code)
675         ERROR(code);
676
677     /* Since the tape should have been truncated this should never happen. */
678     if (d->firstTape || d->nVolumes)
679         ERROR(BUDB_DATABASEINCONSISTENT);
680
681     code = FreeStructure(ut, dump_BLOCK, da);
682     if (code)
683         ERROR(code);
684
685   error_exit:
686     return code;
687 }
688
689 /*
690  * VolInfoMatch()
691  *
692  * This is called with a volumeEntry and a volInfo structure and compares
693  * them.  It returns non-zero if they are equal.  It is used by GetVolInfo to
694  * search volInfo structures once it has the head volInfo structure from the
695  * volName hash table.
696  *
697  * When called from GetVolInfo the name compare is redundant.
698  * Always AND the flags with VOLINFOFLAGS for backwards compatability (3.3).
699  */
700
701 static int
702 VolInfoMatch(struct budb_volumeEntry *vol, struct volInfo *vi)
703 {
704     return ((strcmp(vol->name, vi->name) == 0) &&       /* same volume name */
705             (vol->id == ntohl(vi->id)) &&       /* same volume id */
706             ((vol->flags & VOLINFOFLAGS) == (ntohl(vi->flags) & VOLINFOFLAGS)) &&       /* same flags */
707             (vol->partition == ntohl(vi->partition)) && /* same partition (N/A) */
708             (strcmp(vol->server, vi->server) == 0));    /* same server (N/A) */
709 }
710
711 /*
712  * GetVolInfo()
713  *     This routine takes a volumeEntry structure from an RPC interface and
714  * returns the corresponding volInfo structure, creating it if necessary.
715  *
716  *     The caller must write the entry out.
717  */
718
719 static afs_int32
720 GetVolInfo(struct ubik_trans *ut, struct budb_volumeEntry *volP, dbadr *viaP,
721            struct volInfo *viP)
722 {
723     dbadr hvia, via;
724     struct volInfo hvi;
725     afs_int32 eval, code = 0;
726
727     eval = ht_LookupEntry(ut, &db.volName, volP->name, &via, viP);
728     if (eval)
729         ERROR(eval);
730
731     if (!via) {
732         /* allocate a new volinfo structure */
733         eval = AllocStructure(ut, volInfo_BLOCK, 0, &via, viP);
734         if (eval)
735             ERROR(eval);
736
737         strcpy(viP->name, volP->name);
738         strcpy(viP->server, volP->server);
739         viP->sameNameHead = 0;  /* The head of same name chain */
740         viP->sameNameChain = 0; /* Same name chain is empty */
741         viP->firstFragment = 0;
742         viP->nFrags = 0;
743         viP->id = htonl(volP->id);
744         viP->partition = htonl(volP->partition);
745         viP->flags = htonl(volP->flags & VOLINFOFLAGS);
746
747         /* Chain onto volname hash table */
748         eval = ht_HashIn(ut, &db.volName, via, viP);
749         if (eval)
750             ERROR(eval);
751
752         LogDebug(4, "volume Info for %s placed at %d\n", volP->name, via);
753     }
754
755     else if (!VolInfoMatch(volP, viP)) {        /* Not the head volinfo struct */
756         hvia = via;             /* remember the head volinfo struct */
757         memcpy(&hvi, viP, sizeof(hvi));
758
759         /* Search the same name chain for the correct volinfo structure */
760         for (via = ntohl(viP->sameNameChain); via;
761              via = ntohl(viP->sameNameChain)) {
762             eval = dbread(ut, via, viP, sizeof(*viP));
763             if (eval)
764                 ERROR(eval);
765
766             if (VolInfoMatch(volP, viP))
767                 break;          /* found the one */
768         }
769
770         /* if the correct volinfo struct isn't found, create one */
771         if (!via) {
772             eval = AllocStructure(ut, volInfo_BLOCK, 0, &via, viP);
773             if (eval)
774                 ERROR(eval);
775
776             strcpy(viP->name, volP->name);
777             strcpy(viP->server, volP->server);
778             viP->nameHashChain = 0;     /* not in hash table */
779             viP->sameNameHead = htonl(hvia);    /* chain to head of sameNameChain */
780             viP->sameNameChain = hvi.sameNameChain;
781             viP->firstFragment = 0;
782             viP->nFrags = 0;
783             viP->id = htonl(volP->id);
784             viP->partition = htonl(volP->partition);
785             viP->flags = htonl(volP->flags & VOLINFOFLAGS);
786
787             /* write the head entry's sameNameChain link */
788             eval =
789                 set_word_addr(ut, hvia, &hvi, &hvi.sameNameChain, htonl(via));
790             if (eval)
791                 ERROR(eval);
792         }
793     }
794
795     *viaP = via;
796
797   error_exit:
798     return code;
799 }
800
801 /* deletesomevolumesfromtape
802  *      Deletes a specified number of volumes from a tape. The tape
803  *      and dump are modified to reflect the smaller number of volumes.
804  *      The transaction is not terminated, it is up to the caller to
805  *      finish the transaction and start a new one (if desired).
806  * entry:
807  *      maxvolumestodelete - don't delete more than this many volumes
808  */
809
810 afs_int32
811 deleteSomeVolumesFromTape(struct ubik_trans *ut, dbadr tapeAddr,
812                           struct tape *tapePtr, int maxVolumesToDelete)
813 {
814     dbadr volFragAddr, nextVolFragAddr, dumpAddr;
815     struct volFragment volFrag;
816     struct dump dump;
817     int volumesDeleted = 0;
818     afs_int32 eval, code = 0;
819
820     if (!tapePtr)
821         ERROR(0);
822
823     for (volFragAddr = ntohl(tapePtr->firstVol);
824          (volFragAddr && (maxVolumesToDelete > 0));
825          volFragAddr = nextVolFragAddr) {
826         eval = dbread(ut, volFragAddr, &volFrag, sizeof(volFrag));
827         if (eval)
828             ERROR(eval);
829
830         nextVolFragAddr = ntohl(volFrag.sameTapeChain);
831
832         eval = DeleteVolFragment(ut, volFragAddr, &volFrag);
833         if (eval)
834             ERROR(eval);
835
836         maxVolumesToDelete--;
837         volumesDeleted++;
838     }
839
840     /* reset the volume fragment pointer in the tape */
841     tapePtr->firstVol = htonl(volFragAddr);
842
843     /* diminish the tape's volume count */
844     tapePtr->nVolumes = htonl(ntohl(tapePtr->nVolumes) - volumesDeleted);
845
846     eval = dbwrite(ut, tapeAddr, tapePtr, sizeof(*tapePtr));
847     if (eval)
848         ERROR(eval);
849
850     /* diminish the dump's volume count */
851     dumpAddr = ntohl(tapePtr->dump);
852     eval = dbread(ut, dumpAddr, &dump, sizeof(dump));
853     if (eval)
854         ERROR(eval);
855
856     dump.nVolumes = htonl(ntohl(dump.nVolumes) - volumesDeleted);
857     eval = dbwrite(ut, dumpAddr, &dump, sizeof(dump));
858     if (eval)
859         ERROR(eval);
860
861   error_exit:
862     return (code);
863 }
864
865 /* deleteDump
866  *      deletes a dump in stages, by repeatedly deleting a small number of
867  *      volumes from the dump until none are left. The dump is then deleted.
868  *
869  *      In the case where multiple calls are made to delete the same
870  *      dump, the operation will succeed but contention for structures
871  *      will result in someone getting back an error.
872  *
873  * entry:
874  *      id - id of dump to delete
875  */
876
877 afs_int32
878 deleteDump(struct rx_call *call, dumpId id, budb_dumpsList *dumps)
879 {
880     struct ubik_trans *ut;
881     dbadr dumpAddr, tapeAddr, appendedDump;
882     struct dump dump;
883     struct tape tape;
884     dumpId dumpid;
885     afs_int32 eval, code = 0;
886     int partialDel = 0;
887
888     /* iterate until the dump is truly deleted */
889
890     dumpid = id;
891     while (dumpid) {
892         partialDel = 0;
893         while (1) {
894             eval = InitRPC(&ut, LOCKWRITE, 1);
895             if (eval)
896                 ERROR(eval);    /* can't start transaction */
897
898             eval =
899                 ht_LookupEntry(ut, &db.dumpIden, &dumpid, &dumpAddr, &dump);
900             if (eval)
901                 ABORT(eval);
902             if (!dumpAddr)
903                 ABORT(BUDB_NOENT);      /* can't find dump */
904
905             if ((dumpid == id) && (dump.initialDumpID)) /* can't be an appended dump */
906                 ABORT(BUDB_NOTINITIALDUMP);
907
908             tapeAddr = ntohl(dump.firstTape);
909             if (tapeAddr == 0)
910                 break;
911
912             /* there is a tape to delete */
913             eval = dbread(ut, tapeAddr, &tape, sizeof(tape));
914             if (eval)
915                 ABORT(eval);
916
917             if (ntohl(tape.nVolumes)) {
918                 /* tape is not empty */
919                 eval = deleteSomeVolumesFromTape(ut, tapeAddr, &tape, 10);
920                 if (eval)
921                     ABORT(eval);
922             }
923
924             if (ntohl(tape.nVolumes) == 0) {
925                 /* tape is now empty, delete it */
926                 eval = DeleteTape(ut, tapeAddr, &tape);
927                 if (eval)
928                     ABORT(eval);
929                 eval = ht_HashOut(ut, &db.tapeName, tapeAddr, &tape);
930                 if (eval)
931                     ABORT(eval);
932                 eval = FreeStructure(ut, tape_BLOCK, tapeAddr);
933                 if (eval)
934                     ABORT(eval);
935             }
936
937             eval = ubik_EndTrans(ut);
938             if (eval)
939                 ERROR(eval);
940             partialDel = 1;
941         }                       /* next deletion portion */
942
943         /* Record the dump just deleted */
944         if (dumps && (dumps->budb_dumpsList_len < BUDB_MAX_RETURN_LIST)) {
945             if (dumps->budb_dumpsList_len == 0)
946                 dumps->budb_dumpsList_val = malloc(sizeof(afs_int32));
947             else
948                 dumps->budb_dumpsList_val =
949                     realloc(dumps->budb_dumpsList_val,
950                             (dumps->budb_dumpsList_len + 1)
951                              * sizeof(afs_int32));
952
953             if (!dumps->budb_dumpsList_val)
954                 ABORT(BUDB_NOMEM);
955
956             dumps->budb_dumpsList_val[dumps->budb_dumpsList_len] = dumpid;
957             dumps->budb_dumpsList_len++;
958         }
959
960         appendedDump = ntohl(dump.appendedDumpChain);
961
962         /* finally done. No more tapes left in the dump. Delete the dump itself */
963         eval = DeleteDump(ut, dumpAddr, &dump);
964         if (eval)
965             ABORT(eval);
966
967         /* Now delete the appended dump too */
968         if (appendedDump) {
969             eval = dbread(ut, appendedDump, &dump, sizeof(dump));
970             if (eval)
971                 ABORT(eval);
972
973             dumpid = ntohl(dump.id);
974         } else
975             dumpid = 0;
976
977         eval = ubik_EndTrans(ut);
978         if (eval)
979             ERROR(eval);
980
981         Log("Delete dump %s (DumpID %u), path %s\n", dump.dumpName,
982             ntohl(dump.id), dump.dumpPath);
983     }
984
985   error_exit:
986     if (code && partialDel) {
987         Log("Delete dump %s (DumpID %u), path %s - INCOMPLETE (code = %u)\n",
988             dump.dumpName, ntohl(dump.id), dump.dumpPath, code);
989     }
990     return (code);
991
992   abort_exit:
993     ubik_AbortTrans(ut);
994     goto error_exit;
995 }
996
997 /* --------------
998  * dump selection routines - used by BUDB_GetDumps
999  * --------------
1000  */
1001
1002 /* most recent dump selection */
1003
1004 struct chosenDump {
1005     struct chosenDump *next;
1006     dbadr addr;
1007     afs_uint32 date;
1008 };
1009
1010 struct wantDumpRock {
1011     int maxDumps;               /* max wanted */
1012     int ndumps;                 /* actual in chain */
1013     struct chosenDump *chain;
1014 };
1015
1016
1017 int
1018 wantDump(dbadr dumpAddr, void *dumpParam, void *dumpListPtrParam)
1019 {
1020     struct dump *dumpPtr;
1021     struct wantDumpRock *rockPtr;
1022
1023     dumpPtr = (struct dump *)dumpParam;
1024     rockPtr = (struct wantDumpRock *)dumpListPtrParam;
1025
1026     /* if we don't have our full complement, just add another */
1027     if (rockPtr->ndumps < rockPtr->maxDumps)
1028         return (1);
1029
1030     /* got the number we need, select based on date */
1031     if ((afs_uint32) ntohl(dumpPtr->created) > rockPtr->chain->date)
1032         return (1);
1033
1034     return (0);
1035 }
1036
1037 int
1038 rememberDump(dbadr dumpAddr, void *dumpParam, void *dumpListPtrParam)
1039 {
1040     struct dump *dumpPtr;
1041     struct wantDumpRock *rockPtr;
1042     struct chosenDump *ptr, *deletedPtr, **nextPtr;
1043
1044     dumpPtr = (struct dump *)dumpParam;
1045     rockPtr = (struct wantDumpRock *)dumpListPtrParam;
1046
1047     ptr = calloc(1, sizeof(*ptr));
1048     if (!ptr)
1049         return (0);
1050     ptr->addr = dumpAddr;
1051     ptr->date = (afs_uint32) ntohl(dumpPtr->created);
1052
1053     /* Don't overflow the max */
1054     while (rockPtr->ndumps >= rockPtr->maxDumps) {
1055         /* have to drop one */
1056         deletedPtr = rockPtr->chain;
1057         rockPtr->chain = deletedPtr->next;
1058         free(deletedPtr);
1059         rockPtr->ndumps--;
1060     }
1061
1062     /* now insert in the right place */
1063     for (nextPtr = &rockPtr->chain; *nextPtr; nextPtr = &((*nextPtr)->next)) {
1064         if (ptr->date < (*nextPtr)->date)
1065             break;
1066     }
1067     ptr->next = *nextPtr;
1068     *nextPtr = ptr;
1069     rockPtr->ndumps++;
1070
1071     return (0);
1072 }
1073
1074
1075 /* ---------------------------------------------
1076  * general interface routines - alphabetic
1077  * ---------------------------------------------
1078  */
1079
1080 afs_int32
1081 SBUDB_AddVolume(struct rx_call *call, struct budb_volumeEntry *vol)
1082 {
1083     afs_int32 code;
1084
1085     code = AddVolume(call, vol);
1086     osi_auditU(call, BUDB_AddVolEvent, code, AUD_LONG, (vol ? vol->id : 0),
1087                AUD_END);
1088     return code;
1089 }
1090
1091 afs_int32
1092 AddVolume(struct rx_call *call, struct budb_volumeEntry *vol)
1093 {
1094     struct ubik_trans *ut;
1095     dbadr da, ta, via, va;
1096     struct dump d;
1097     struct tape t;
1098     struct volInfo vi;
1099     struct volFragment v;
1100     afs_uint32 bytes;
1101     afs_int32 eval, code = 0;
1102
1103     if (!callPermitted(call))
1104         return BUDB_NOTPERMITTED;
1105
1106     if ((strlen(vol->name) >= sizeof(vi.name))
1107         || (strlen(vol->server) >= sizeof(vi.server))
1108         || (strlen(vol->tape) >= sizeof(t.name)))
1109         return BUDB_BADARGUMENT;
1110
1111     eval = InitRPC(&ut, LOCKWRITE, 1);
1112     if (eval)
1113         return eval;
1114
1115     /* Find the dump in dumpid hash table */
1116     eval = ht_LookupEntry(ut, &db.dumpIden, &vol->dump, &da, &d);
1117     if (eval)
1118         ABORT(eval);
1119     if (!da)
1120         ABORT(BUDB_NODUMPID);
1121
1122     /* search for the right tape in the dump */
1123     for (ta = ntohl(d.firstTape); ta; ta = ntohl(t.nextTape)) {
1124         /* read the tape entry */
1125         eval = dbread(ut, ta, &t, sizeof(t));
1126         if (eval)
1127             ABORT(eval);
1128
1129         /* Check if the right tape name */
1130         if (strcmp(t.name, vol->tape) == 0)
1131             break;
1132     }
1133     if (!ta)
1134         ABORT(BUDB_NOTAPENAME);
1135
1136     if ((t.dump != htonl(da)) ||        /* tape must belong to dump   */
1137         ((ntohl(t.flags) & BUDB_TAPE_BEINGWRITTEN) == 0) ||     /* tape must be being written */
1138         ((ntohl(d.flags) & BUDB_DUMP_INPROGRESS) == 0)) /* dump must be in progress   */
1139         ABORT(BUDB_BADPROTOCOL);
1140
1141     /* find or create a volume info structure */
1142     eval = GetVolInfo(ut, vol, &via, &vi);
1143     if (eval)
1144         ABORT(eval);
1145
1146     /* Create a volume fragment */
1147     eval = AllocStructure(ut, volFragment_BLOCK, 0, &va, &v);
1148     if (eval)
1149         ABORT(eval);
1150
1151     v.vol = htonl(via);         /* vol frag points to vol info */
1152     v.sameNameChain = vi.firstFragment; /* vol frag is chained to vol info */
1153     vi.firstFragment = htonl(va);
1154     vi.nFrags = htonl(ntohl(vi.nFrags) + 1);
1155
1156     eval = dbwrite(ut, via, &vi, sizeof(vi));   /* write the vol info struct */
1157     if (eval)
1158         ABORT(eval);
1159
1160     v.tape = htonl(ta);         /* vol frag points to tape */
1161     v.sameTapeChain = t.firstVol;       /* vol frag is chained to tape info */
1162     t.firstVol = htonl(va);
1163     t.nVolumes = htonl(ntohl(t.nVolumes) + 1);
1164     bytes = ntohl(t.nBytes) + vol->nBytes;      /* update bytes on tape */
1165     t.nMBytes = htonl(ntohl(t.nMBytes) + bytes / (1024 * 1024));
1166     t.nBytes = htonl(bytes % (1024 * 1024));
1167
1168     eval = dbwrite(ut, ta, &t, sizeof(t));      /* write the tape structure */
1169     if (eval)
1170         ABORT(eval);
1171
1172     d.nVolumes = htonl(ntohl(d.nVolumes) + 1);  /* one more volume on dump */
1173
1174     eval = dbwrite(ut, da, &d, sizeof(d));      /* write out the dump structure */
1175     if (eval)
1176         ABORT(eval);
1177
1178     v.position = htonl(vol->position);  /* vol frag info */
1179     v.clone = htonl(vol->clone);
1180     v.incTime = htonl(vol->incTime);
1181     v.startByte = htonl(vol->startByte);
1182     v.nBytes = htonl(vol->nBytes);
1183     v.flags = htons(vol->flags & VOLFRAGMENTFLAGS);
1184     v.sequence = htons(vol->seq);
1185
1186     eval = dbwrite(ut, va, &v, sizeof(v));      /* write out the vol frag struct */
1187     if (eval)
1188         ABORT(eval);
1189
1190     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
1191     if (eval)
1192         ABORT(eval);
1193
1194     LogDebug(4, "added volume %s at %d\n", vol->name, va);
1195
1196     code = ubik_EndTrans(ut);
1197     return code;
1198
1199   abort_exit:
1200     ubik_AbortTrans(ut);
1201     return code;
1202 }
1203
1204
1205 afs_int32
1206 SBUDB_AddVolumes(struct rx_call *call, struct budb_volumeList *vols)
1207 {
1208     afs_int32 code;
1209
1210     code = AddVolumes(call, vols);
1211     osi_auditU(call, BUDB_AddVolEvent, code, AUD_LONG, 0, AUD_END);
1212     return code;
1213 }
1214
1215 afs_int32
1216 AddVolumes(struct rx_call *call, struct budb_volumeList *vols)
1217 {
1218     struct budb_volumeEntry *vol, *vol1;
1219     struct ubik_trans *ut;
1220     dbadr da, ta, via, va;
1221     struct dump d;
1222     struct tape t;
1223     struct volInfo vi;
1224     struct volFragment v;
1225     afs_uint32 bytes;
1226     afs_int32 eval, e, code = 0;
1227
1228     if (!callPermitted(call))
1229         return BUDB_NOTPERMITTED;
1230
1231     if (!vols || (vols->budb_volumeList_len <= 0)
1232         || !vols->budb_volumeList_val)
1233         return BUDB_BADARGUMENT;
1234
1235     /* The first volume in the list of volumes to add */
1236     vol1 = (struct budb_volumeEntry *)vols->budb_volumeList_val;
1237
1238     eval = InitRPC(&ut, LOCKWRITE, 1);
1239     if (eval)
1240         return eval;
1241
1242     /* Find the dump in dumpid hash table */
1243     eval = ht_LookupEntry(ut, &db.dumpIden, &vol1->dump, &da, &d);
1244     if (eval)
1245         ABORT(eval);
1246     if (!da)
1247         ABORT(BUDB_NODUMPID);
1248
1249     /* search for the right tape in the dump */
1250     for (ta = ntohl(d.firstTape); ta; ta = ntohl(t.nextTape)) {
1251         /* read the tape entry */
1252         eval = dbread(ut, ta, &t, sizeof(t));
1253         if (eval)
1254             ABORT(eval);
1255
1256         /* Check if the right tape name */
1257         if (strcmp(t.name, vol1->tape) == 0)
1258             break;
1259     }
1260     if (!ta)
1261         ABORT(BUDB_NOTAPENAME);
1262
1263     if ((t.dump != htonl(da)) ||        /* tape must belong to dump   */
1264         ((ntohl(t.flags) & BUDB_TAPE_BEINGWRITTEN) == 0) ||     /* tape must be being written */
1265         ((ntohl(d.flags) & BUDB_DUMP_INPROGRESS) == 0)) /* dump must be in progress   */
1266         ABORT(BUDB_BADPROTOCOL);
1267
1268     for (vol = vol1, e = 0; e < vols->budb_volumeList_len; vol++, e++) {
1269         /*v */
1270         if ((strlen(vol->name) >= sizeof(vi.name)) || (strcmp(vol->name, "") == 0) ||   /* no null volnames */
1271             (strlen(vol->server) >= sizeof(vi.server))
1272             || (strlen(vol->tape) >= sizeof(t.name))
1273             || (strcmp(vol->tape, vol1->tape) != 0)) {
1274             Log("Volume '%s' %u, tape '%s', dumpID %u is an invalid entry - not added\n", vol->name, vol->id, vol->tape, vol->dump);
1275             continue;
1276         }
1277
1278         /* find or create a volume info structure */
1279         eval = GetVolInfo(ut, vol, &via, &vi);
1280         if (eval)
1281             ABORT(eval);
1282         if (*(afs_int32 *) (&vi) == 0) {
1283             Log("Volume '%s', tape '%s', dumpID %u is an invalid entry - aborted\n", vol->name, vol->tape, vol->dump);
1284             ABORT(BUDB_BADARGUMENT);
1285         }
1286
1287         /* Create a volume fragment */
1288         eval = AllocStructure(ut, volFragment_BLOCK, 0, &va, &v);
1289         if (eval)
1290             ABORT(eval);
1291
1292         v.vol = htonl(via);     /* vol frag points to vol info */
1293         v.sameNameChain = vi.firstFragment;     /* vol frag is chained to vol info */
1294         vi.firstFragment = htonl(va);
1295         vi.nFrags = htonl(ntohl(vi.nFrags) + 1);
1296         eval = dbwrite(ut, via, &vi, sizeof(vi));       /* write the vol info struct */
1297         if (eval)
1298             ABORT(eval);
1299
1300         v.tape = htonl(ta);     /* vol frag points to tape */
1301         v.sameTapeChain = t.firstVol;   /* vol frag is chained to tape info */
1302         t.firstVol = htonl(va);
1303         t.nVolumes = htonl(ntohl(t.nVolumes) + 1);
1304         bytes = ntohl(t.nBytes) + vol->nBytes;  /* update bytes on tape */
1305         t.nMBytes = htonl(ntohl(t.nMBytes) + bytes / (1024 * 1024));
1306         t.nBytes = htonl(bytes % (1024 * 1024));
1307
1308         d.nVolumes = htonl(ntohl(d.nVolumes) + 1);      /* one more volume on dump */
1309
1310         v.position = htonl(vol->position);      /* vol frag info */
1311         v.clone = htonl(vol->clone);
1312         v.incTime = htonl(vol->incTime);
1313         v.startByte = htonl(vol->startByte);
1314         v.nBytes = htonl(vol->nBytes);
1315         v.flags = htons(vol->flags & VOLFRAGMENTFLAGS);
1316         v.sequence = htons(vol->seq);
1317
1318         eval = dbwrite(ut, va, &v, sizeof(v));  /* write out the vol frag struct */
1319         if (eval)
1320             ABORT(eval);
1321
1322         LogDebug(4, "added volume %s at %d\n", vol->name, va);
1323     }                           /*v */
1324
1325     eval = dbwrite(ut, ta, &t, sizeof(t));      /* write the tape structure */
1326     if (eval)
1327         ABORT(eval);
1328
1329     eval = dbwrite(ut, da, &d, sizeof(d));      /* write out the dump structure */
1330     if (eval)
1331         ABORT(eval);
1332
1333     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
1334     if (eval)
1335         ABORT(eval);
1336
1337     code = ubik_EndTrans(ut);
1338     return code;
1339
1340   abort_exit:
1341     ubik_AbortTrans(ut);
1342     return code;
1343 }
1344
1345
1346 /* BUDB_CreateDump
1347  *      records the existence of a dump in the database. This creates only
1348  *      the dump record, to which one must attach tape and volume records.
1349  * TBD
1350  *      1) record the volume set
1351  */
1352
1353 afs_int32
1354 SBUDB_CreateDump(struct rx_call *call, struct budb_dumpEntry *dump)
1355 {
1356     afs_int32 code;
1357
1358     code = CreateDump(call, dump);
1359     osi_auditU(call, BUDB_CrDmpEvent, code, AUD_DATE, (dump ? dump->id : 0),
1360                AUD_END);
1361     if (dump && !code) {
1362         Log("Create dump %s (DumpID %u), path %s\n", dump->name, dump->id,
1363             dump->dumpPath);
1364     }
1365     return code;
1366 }
1367
1368 afs_int32
1369 CreateDump(struct rx_call *call, struct budb_dumpEntry *dump)
1370 {
1371     struct ubik_trans *ut;
1372     dbadr findDumpAddr, da;
1373     struct dump findDump, d;
1374     afs_int32 eval, code = 0;
1375
1376     rxkad_level level;
1377     afs_int32 kvno;
1378     Date expiration;            /* checked by Security Module */
1379     struct ktc_principal principal;
1380
1381     if (!callPermitted(call))
1382         return BUDB_NOTPERMITTED;
1383
1384     if (strlen(dump->name) >= sizeof(d.dumpName))
1385         return BUDB_BADARGUMENT;
1386
1387     eval = InitRPC(&ut, LOCKWRITE, 1);
1388     if (eval)
1389         return eval;
1390
1391     eval =
1392         rxkad_GetServerInfo(rx_ConnectionOf(call), &level, &expiration,
1393                             principal.name, principal.instance,
1394                             principal.cell, &kvno);
1395
1396     if (eval) {
1397         if (eval != RXKADNOAUTH)
1398             ABORT(eval);
1399
1400         strcpy(principal.name, "");
1401         strcpy(principal.instance, "");
1402         strcpy(principal.cell, "");
1403         expiration = 0;
1404     } else {
1405         /* authenticated. Take user supplied principal information */
1406         if (strcmp(dump->dumper.name, "") != 0)
1407             strncpy(principal.name, dump->dumper.name,
1408                     sizeof(principal.name));
1409
1410         if (strcmp(dump->dumper.instance, "") != 0)
1411             strncpy(principal.instance, dump->dumper.instance,
1412                     sizeof(principal.instance));
1413
1414         if (strcmp(dump->dumper.cell, "") != 0)
1415             strncpy(principal.cell, dump->dumper.cell,
1416                     sizeof(principal.cell));
1417     }
1418
1419     /* dump id's are time stamps */
1420     if (!dump->id) {
1421         while (1) {             /* allocate a unique dump id *//*w */
1422             dump->id = time(0);
1423
1424             /* ensure it is unique - seach for dumpid in hash table */
1425             eval =
1426                 ht_LookupEntry(ut, &db.dumpIden, &dump->id, &findDumpAddr,
1427                                &findDump);
1428             if (eval)
1429                 ABORT(eval);
1430
1431             if (!findDumpAddr) {        /* dumpid not in use */
1432                 /* update the last dump id allocated */
1433                 eval = set_header_word(ut, lastDumpId, htonl(dump->id));
1434                 if (eval)
1435                     ABORT(eval);
1436                 break;
1437             }
1438
1439             /* dump id is in use - wait a while */
1440 #ifdef AFS_PTHREAD_ENV
1441             sleep(1);
1442 #else
1443             IOMGR_Sleep(1);
1444 #endif
1445         }                       /*w */
1446     } else {
1447         /* dump id supplied (e.g. for database restore) */
1448         eval =
1449             ht_LookupEntry(ut, &db.dumpIden, &dump->id, &findDumpAddr,
1450                            &findDump);
1451         if (eval)
1452             ABORT(eval);
1453
1454         /* Dump id must not already exist */
1455         if (findDumpAddr)
1456             ABORT(BUDB_DUMPIDEXISTS);
1457     }
1458
1459     /* Allocate a dump structure */
1460     memset(&d, 0, sizeof(d));
1461     eval = AllocStructure(ut, dump_BLOCK, 0, &da, &d);
1462     if (eval)
1463         ABORT(eval);
1464
1465     strcpy(d.dumpName, dump->name);     /* volset.dumpname */
1466     strcpy(d.dumpPath, dump->dumpPath); /* dump node path */
1467     strcpy(d.volumeSet, dump->volumeSetName);   /* volume set */
1468     d.id = htonl(dump->id);
1469     d.parent = htonl(dump->parent);     /* parent id */
1470     d.level = htonl(dump->level);
1471
1472     LogDebug(4, "dump name %s, parent %d level %d\n", dump->name,
1473              dump->parent, dump->level);
1474
1475     /* if creation time specified, use that.  Else use the dumpid time */
1476     if (dump->created == 0)
1477         dump->created = dump->id;
1478     d.created = htonl(dump->created);
1479
1480     d.dumper = principal;
1481     tapeSet_hton(&dump->tapes, &d.tapes);
1482
1483     d.flags = htonl(dump->flags | BUDB_DUMP_INPROGRESS);
1484
1485     eval = ht_HashIn(ut, &db.dumpName, da, &d); /* Into dump name hash table */
1486     if (eval)
1487         ABORT(eval);
1488
1489     eval = ht_HashIn(ut, &db.dumpIden, da, &d); /* Into dumpid hash table */
1490     if (eval)
1491         ABORT(eval);
1492
1493     eval = dbwrite(ut, da, (char *)&d, sizeof(d));      /* Write the dump structure */
1494     if (eval)
1495         ABORT(eval);
1496
1497     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
1498     if (eval)
1499         ABORT(eval);
1500
1501     /* If to append this dump, then append it - will write the appended dump */
1502     eval = makeAppended(ut, dump->id, dump->initialDumpID, dump->tapes.b);
1503     if (eval)
1504         ABORT(eval);
1505
1506     code = ubik_EndTrans(ut);
1507     LogDebug(5, "made dump %s, path %s\n", d.dumpName, d.dumpPath);
1508     return code;
1509
1510   abort_exit:
1511     ubik_AbortTrans(ut);
1512     return code;
1513 }
1514
1515 afs_int32
1516 SBUDB_DeleteDump(struct rx_call *call, dumpId id, Date fromTime, Date toTime,
1517                  budb_dumpsList *dumps)
1518 {
1519     afs_int32 code;
1520
1521     code = DoDeleteDump(call, id, fromTime, toTime, dumps);
1522     osi_auditU(call, BUDB_DelDmpEvent, code, AUD_DATE, id, AUD_END);
1523     return code;
1524 }
1525
1526 #define MAXOFFS 30
1527
1528 afs_int32
1529 DoDeleteDump(struct rx_call *call, dumpId id, Date fromTime, Date toTime,
1530              budb_dumpsList *dumps)
1531 {
1532     afs_int32 code = 0;
1533
1534     if (!callPermitted(call))
1535         return BUDB_NOTPERMITTED;
1536
1537     if (id)
1538         code = deleteDump(call, id, dumps);
1539     return (code);
1540 }
1541
1542 afs_int32
1543 SBUDB_ListDumps(struct rx_call *call, afs_int32 sflags, char *name,
1544                 afs_int32 groupid, Date fromTime, Date toTime,
1545                 budb_dumpsList *dumps, budb_dumpsList *flags)
1546 {
1547     afs_int32 code;
1548
1549     code = ListDumps(call, sflags, groupid, fromTime, toTime, dumps, flags);
1550     osi_auditU(call, BUDB_LstDmpEvent, code, AUD_LONG, flags, AUD_END);
1551     return code;
1552 }
1553
1554 afs_int32
1555 ListDumps(struct rx_call *call, afs_int32 sflags, afs_int32 groupid,
1556           Date fromTime, Date toTime, budb_dumpsList *dumps,
1557           budb_dumpsList *flags)
1558 {
1559     struct ubik_trans *ut;
1560     struct memoryHashTable *mht;
1561     struct dump diskDump, appDiskDump;
1562     dbadr dbAddr, dbAppAddr;
1563
1564     afs_int32 eval, code = 0;
1565     int old, hash, length, entrySize, count = 0;
1566
1567     if (!callPermitted(call))
1568         return BUDB_NOTPERMITTED;
1569
1570     eval = InitRPC(&ut, LOCKREAD, 1);
1571     if (eval)
1572         return (eval);
1573
1574     /* Search the database */
1575     mht = ht_GetType(HT_dumpIden_FUNCTION, &entrySize);
1576     if (!mht)
1577         return (BUDB_BADARGUMENT);
1578
1579     for (old = 0; old <= 1; old++) {    /*o *//* old and new hash tables */
1580         length = (old ? mht->oldLength : mht->length);
1581         if (length == 0)
1582             continue;
1583
1584         for (hash = 0; hash < length; hash++) { /*h *//* for each hash bucket */
1585             for (dbAddr = ht_LookupBucket(ut, mht, hash, old); dbAddr; dbAddr = ntohl(diskDump.idHashChain)) {  /*d */
1586
1587                 /* read the entry */
1588                 eval = dbread(ut, dbAddr, &diskDump, sizeof(diskDump));
1589                 if (eval)
1590                     ABORT(eval);
1591
1592                 /* Skip appended dumps */
1593                 if (ntohl(diskDump.initialDumpID) != 0) {
1594                     continue;
1595                 }
1596
1597                 /* Skip dumps with different goup id */
1598                 if ((sflags & BUDB_OP_GROUPID)
1599                     && (ntohl(diskDump.tapes.id) != groupid)) {
1600                     continue;   /*nope */
1601                 }
1602
1603                 /* Look at this dump to see if it meets the criteria for listing */
1604                 if (sflags & BUDB_OP_DATES) {
1605                     /* This and each appended dump should be in time */
1606                     for (dbAppAddr = dbAddr; dbAppAddr;
1607                          dbAppAddr = ntohl(appDiskDump.appendedDumpChain)) {
1608                         eval =
1609                             dbread(ut, dbAppAddr, &appDiskDump,
1610                                    sizeof(appDiskDump));
1611                         if (eval)
1612                             ABORT(eval);
1613
1614                         if ((ntohl(appDiskDump.id) < fromTime)
1615                             || (ntohl(appDiskDump.id) > toTime))
1616                             break;      /*nope */
1617                     }
1618                     if (dbAppAddr)
1619                         continue;       /*nope */
1620                 }
1621
1622                 /* Add it and each of its appended dump to our list to return */
1623                 for (dbAppAddr = dbAddr; dbAppAddr;
1624                      dbAppAddr = ntohl(appDiskDump.appendedDumpChain)) {
1625                     eval =
1626                         dbread(ut, dbAppAddr, &appDiskDump,
1627                                sizeof(appDiskDump));
1628                     if (eval)
1629                         ABORT(eval);
1630
1631                     /* Make sure we have space to list it */
1632                     if (dumps->budb_dumpsList_len >= count) {
1633                         count += 10;
1634                         if (count == 10) {
1635                             dumps->budb_dumpsList_val =
1636                                 malloc(count * sizeof(afs_int32));
1637                             flags->budb_dumpsList_val =
1638                                 malloc(count * sizeof(afs_int32));
1639                         } else {
1640                             dumps->budb_dumpsList_val =
1641                                 realloc(dumps->budb_dumpsList_val,
1642                                         count * sizeof(afs_int32));
1643                             flags->budb_dumpsList_val =
1644                                 realloc(flags->budb_dumpsList_val,
1645                                         count * sizeof(afs_int32));
1646                         }
1647                         if (!dumps->budb_dumpsList_val
1648                             || !flags->budb_dumpsList_val)
1649                             ABORT(BUDB_NOMEM);
1650                     }
1651
1652                     /* Add it to our list */
1653                     dumps->budb_dumpsList_val[dumps->budb_dumpsList_len] =
1654                         ntohl(appDiskDump.id);
1655                     flags->budb_dumpsList_val[flags->budb_dumpsList_len] = 0;
1656                     if (ntohl(appDiskDump.initialDumpID) != 0) {
1657                         flags->budb_dumpsList_val[flags->
1658                                                   budb_dumpsList_len] |=
1659                             BUDB_OP_APPDUMP;
1660                     }
1661                     if (strcmp(appDiskDump.dumpName, DUMP_TAPE_NAME) == 0) {
1662                         flags->budb_dumpsList_val[flags->
1663                                                   budb_dumpsList_len] |=
1664                             BUDB_OP_DBDUMP;
1665                     }
1666                     dumps->budb_dumpsList_len++;
1667                     flags->budb_dumpsList_len++;
1668                 }
1669             }                   /*d */
1670         }                       /*h */
1671     }                           /*o */
1672
1673     code = ubik_EndTrans(ut);
1674     return (code);
1675
1676   abort_exit:
1677     ubik_AbortTrans(ut);
1678     return (code);
1679 }
1680
1681 afs_int32
1682 SBUDB_DeleteTape(struct rx_call *call,
1683                  struct budb_tapeEntry *tape)   /* tape info */
1684 {
1685     afs_int32 code;
1686
1687     code = DoDeleteTape(call, tape);
1688     osi_auditU(call, BUDB_DelTpeEvent, code, AUD_DATE,
1689                (tape ? tape->dump : 0), AUD_END);
1690     return code;
1691 }
1692
1693 afs_int32
1694 DoDeleteTape(struct rx_call *call,
1695              struct budb_tapeEntry *tape)       /* tape info */
1696 {
1697     struct ubik_trans *ut;
1698     struct tape t;
1699     dbadr a;
1700     afs_int32 eval, code;
1701
1702     if (!callPermitted(call))
1703         return BUDB_NOTPERMITTED;
1704
1705     eval = InitRPC(&ut, LOCKWRITE, 1);
1706     if (eval)
1707         return eval;
1708
1709     eval = ht_LookupEntry(ut, &db.tapeName, tape->name, &a, &t);
1710     if (eval)
1711         ABORT(eval);
1712
1713     eval = DeleteTape(ut, a, &t);
1714     if (eval)
1715         ABORT(eval);
1716
1717     eval = FreeStructure(ut, tape_BLOCK, a);
1718     if (eval)
1719         ABORT(eval);
1720
1721     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
1722     if (eval)
1723         ABORT(eval);
1724
1725     code = ubik_EndTrans(ut);
1726     return code;
1727
1728   abort_exit:
1729     ubik_AbortTrans(ut);
1730     return code;
1731 }
1732
1733 /* BUDB_DeleteVDP
1734  *      Deletes old information from the database for a particular dump path
1735  *      and volumset. This supercedes the old policy implemented in
1736  *      UseTape, which simply matched on the volumeset.dump. Consequently
1737  *      it was unable to handle name re-use.
1738  * entry:
1739  *      dsname - dumpset name, i.e. volumeset.dumpname
1740  *      dumpPath - full path of dump node
1741  *      curDumpID - current dump in progress - so that is may be excluded
1742  * exit:
1743  *      0 - ok
1744  *      n - some error. May or may not have deleted information.
1745  */
1746
1747 afs_int32
1748 SBUDB_DeleteVDP(struct rx_call *call, char *dsname, char *dumpPath,
1749                 afs_int32 curDumpId)
1750 {
1751     afs_int32 code;
1752
1753     code = DeleteVDP(call, dsname, dumpPath, curDumpId);
1754     osi_auditU(call, BUDB_DelVDPEvent, code, AUD_STR, dsname, AUD_END);
1755     return code;
1756 }
1757
1758 afs_int32
1759 DeleteVDP(struct rx_call *call, char *dsname, char *dumpPath,
1760           afs_int32 curDumpId)
1761 {
1762     struct dump dump;
1763     dbadr dumpAddr;
1764
1765     struct ubik_trans *ut;
1766     afs_int32 eval, code = 0;
1767
1768     if (!callPermitted(call))
1769         return BUDB_NOTPERMITTED;
1770
1771     while (1) {
1772         eval = InitRPC(&ut, LOCKREAD, 1);
1773         if (eval)
1774             return (eval);
1775
1776         eval = ht_LookupEntry(ut, &db.dumpName, dsname, &dumpAddr, &dump);
1777         if (eval)
1778             ABORT(eval);
1779
1780         while (dumpAddr != 0) { /*wd */
1781             if ((strcmp(dump.dumpName, dsname) == 0)
1782                 && (strcmp(dump.dumpPath, dumpPath) == 0)
1783                 && (ntohl(dump.id) != curDumpId)) {
1784                 eval = ubik_EndTrans(ut);
1785                 if (eval)
1786                     return (eval);
1787
1788                 eval = deleteDump(call, ntohl(dump.id), 0);
1789                 if (eval)
1790                     return (eval);
1791
1792                 /* start the traversal over since the various chains may
1793                  * have changed
1794                  */
1795                 break;
1796             }
1797
1798             dumpAddr = ntohl(dump.nameHashChain);
1799             if (dumpAddr) {
1800                 eval = dbread(ut, dumpAddr, &dump, sizeof(dump));
1801                 if (eval)
1802                     ABORT(eval);
1803             }
1804         }                       /*wd */
1805
1806         /* check if all the dumps have been examined - can terminate */
1807         if (!dumpAddr) {
1808             eval = ubik_EndTrans(ut);
1809             return (eval);
1810         }
1811     }
1812
1813   abort_exit:
1814     ubik_AbortTrans(ut);
1815     return (code);
1816 }
1817
1818 /* BUDB_FindClone
1819  * notes:
1820  *      Given a volume name, and a dumpID, find the volume in that dump and
1821  *      return the clone date of the volume (this is the clone date of the
1822  *      volume at the time it was dumped).
1823  *
1824  *      Hashes on the volume name and traverses the fragments. Will need to read
1825  *      the volumes tape entry to determine if it belongs to the dump. If the
1826  *      volume is not found in the dump, then look for it in its parent dump.
1827  */
1828
1829 afs_int32
1830 SBUDB_FindClone(struct rx_call *call, afs_int32 dumpID, char *volName,
1831                 afs_int32 *clonetime)
1832 {
1833     afs_int32 code;
1834
1835     code = FindClone(call, dumpID, volName, clonetime);
1836     osi_auditU(call, BUDB_FndClnEvent, code, AUD_STR, volName, AUD_END);
1837     return code;
1838 }
1839
1840 afs_int32
1841 FindClone(struct rx_call *call, afs_int32 dumpID, char *volName,
1842           afs_int32 *clonetime)
1843 {
1844     struct ubik_trans *ut;
1845     dbadr da, hvia, via, vfa;
1846     struct dump d;
1847     struct tape t;
1848     struct volFragment vf;
1849     struct volInfo vi;
1850     int rvi;                    /* read the volInfo struct */
1851     afs_int32 eval, code = 0;
1852
1853     if (!callPermitted(call))
1854         return BUDB_NOTPERMITTED;
1855
1856     eval = InitRPC(&ut, LOCKREAD, 1);
1857     if (eval)
1858         return (eval);
1859
1860     *clonetime = 0;
1861
1862     /* Search for the volume by name */
1863     eval = ht_LookupEntry(ut, &db.volName, volName, &hvia, &vi);
1864     if (eval)
1865         ABORT(eval);
1866     if (!hvia)
1867         ABORT(BUDB_NOVOLUMENAME);
1868     rvi = 0;
1869
1870     /* Follw the dump levels up */
1871     for (; dumpID; dumpID = ntohl(d.parent)) {  /*d */
1872         /* Get the dump entry */
1873         eval = ht_LookupEntry(ut, &db.dumpIden, &dumpID, &da, &d);
1874         if (eval)
1875             ABORT(eval);
1876         if (!da)
1877             ABORT(BUDB_NODUMPID);
1878
1879         /* seach all the volInfo entries on the sameNameChain */
1880         for (via = hvia; via; via = ntohl(vi.sameNameChain)) {  /*via */
1881             if (rvi) {          /* Read the volInfo entry - except first time */
1882                 eval = dbread(ut, via, &vi, sizeof(vi));
1883                 if (eval)
1884                     ABORT(eval);
1885             }
1886             rvi = 1;
1887
1888             /* search all the volFrag entries on the volFrag */
1889             for (vfa = ntohl(vi.firstFragment); vfa; vfa = ntohl(vf.sameNameChain)) {   /*vfa */
1890                 eval = dbread(ut, vfa, &vf, sizeof(vf));        /* Read the volFrag entry */
1891                 if (eval)
1892                     ABORT(eval);
1893
1894                 eval = dbread(ut, ntohl(vf.tape), &t, sizeof(t));       /* Read the tape */
1895                 if (eval)
1896                     ABORT(eval);
1897
1898                 /* Now check to see if this fragment belongs to the dump we have */
1899                 if (ntohl(t.dump) == da) {
1900                     *clonetime = ntohl(vf.clone);       /* return the clone */
1901                     ERROR(0);
1902                 }
1903             }                   /*vfa */
1904         }                       /*via */
1905     }                           /*d */
1906
1907   error_exit:
1908     code = ubik_EndTrans(ut);
1909     return (code);
1910
1911   abort_exit:
1912     ubik_EndTrans(ut);
1913     return (code);
1914 }
1915
1916 /* BUDB_FindDump
1917  *      Find latest volume dump before adate.
1918  *      Used by restore code when restoring a user requested volume(s)
1919  * entry:
1920  *      volumeName - name of volume to match on
1921  *      beforeDate - look for dumps older than this date
1922  * exit:
1923  *      deptr - descriptor of most recent dump
1924  */
1925
1926 afs_int32
1927 SBUDB_FindDump(struct rx_call *call, char *volumeName, afs_int32 beforeDate,
1928                struct budb_dumpEntry *deptr)
1929 {
1930     afs_int32 code;
1931
1932     code = FindDump(call, volumeName, beforeDate, deptr);
1933     osi_auditU(call, BUDB_FndDmpEvent, code, AUD_STR, volumeName, AUD_END);
1934     return code;
1935 }
1936
1937 afs_int32
1938 FindDump(struct rx_call *call, char *volumeName, afs_int32 beforeDate,
1939          struct budb_dumpEntry *deptr)
1940 {
1941     struct ubik_trans *ut;
1942     dbadr volInfoAddr, volFragmentAddr;
1943     struct tape tape;
1944     struct volInfo volInfo;
1945     struct volFragment volFragment;
1946
1947     dbadr selectedDumpAddr = 0;
1948     afs_int32 selectedDate = 0;
1949     afs_int32 volCloned;
1950     int rvoli;
1951     afs_int32 eval, code = 0;
1952
1953     if (!callPermitted(call))
1954         return BUDB_NOTPERMITTED;
1955
1956     eval = InitRPC(&ut, LOCKREAD, 1);
1957     if (eval)
1958         return eval;
1959
1960     /* Find volinfo struct for volume name in hash table */
1961     eval =
1962         ht_LookupEntry(ut, &db.volName, volumeName, &volInfoAddr, &volInfo);
1963     if (eval)
1964         ABORT(eval);
1965     if (!volInfoAddr)
1966         ABORT(BUDB_NOVOLUMENAME);
1967
1968     /* Step through all the volinfo structures on the same name chain.
1969      * No need to read the first - we read it above.
1970      */
1971     for (rvoli = 0; volInfoAddr;
1972          rvoli = 1, volInfoAddr = ntohl(volInfo.sameNameChain)) {
1973         if (rvoli) {            /* read the volinfo structure */
1974             eval = dbread(ut, volInfoAddr, &volInfo, sizeof(volInfo));
1975             if (eval)
1976                 ABORT(eval);
1977         }
1978
1979         /* step through the volfrag structures */
1980         for (volFragmentAddr = ntohl(volInfo.firstFragment); volFragmentAddr;
1981              volFragmentAddr = ntohl(volFragment.sameNameChain)) {
1982             /* read the volfrag struct */
1983             eval =
1984                 dbread(ut, volFragmentAddr, &volFragment,
1985                        sizeof(volFragment));
1986             if (eval)
1987                 ABORT(eval);
1988
1989             volCloned = ntohl(volFragment.clone);
1990
1991             /* now we can examine the date for most recent dump */
1992             if ((volCloned > selectedDate) && (volCloned < beforeDate)) {
1993                 /* from the volfrag struct, read the tape struct */
1994                 eval =
1995                     dbread(ut, ntohl(volFragment.tape), &tape, sizeof(tape));
1996                 if (eval)
1997                     ABORT(eval);
1998
1999                 selectedDate = volCloned;
2000                 selectedDumpAddr = ntohl(tape.dump);
2001             }
2002         }
2003     }
2004
2005     if (!selectedDumpAddr)
2006         ABORT(BUDB_NOENT);
2007
2008     eval = FillDumpEntry(ut, selectedDumpAddr, deptr);
2009     if (eval)
2010         ABORT(eval);
2011
2012     code = ubik_EndTrans(ut);
2013     return (code);
2014
2015   abort_exit:
2016     ubik_EndTrans(ut);
2017     return (code);
2018 }
2019
2020 /* BUDB_FindLatestDump
2021  *      Find the latest dump of volumeset vsname with dump name dname.
2022  * entry:
2023  *      vsname - volumeset name
2024  *      dname - dumpname
2025  */
2026
2027 afs_int32
2028 SBUDB_FindLatestDump(struct rx_call *call, char *vsname, char *dumpPath,
2029                      struct budb_dumpEntry *dumpentry)
2030 {
2031     afs_int32 code;
2032
2033     code = FindLatestDump(call, vsname, dumpPath, dumpentry);
2034     osi_auditU(call, BUDB_FndLaDEvent, code, AUD_STR, vsname, AUD_END);
2035     return code;
2036 }
2037
2038 afs_int32
2039 FindLatestDump(struct rx_call *call, char *vsname, char *dumpPath,
2040                struct budb_dumpEntry *dumpentry)
2041 {
2042     struct ubik_trans *ut;
2043     dbadr curdbaddr, retdbaddr, firstdbaddr;
2044     struct dump d;
2045     Date latest;
2046     char dumpName[BU_MAXNAMELEN + 2];
2047     afs_int32 eval, code = 0;
2048
2049     if (!callPermitted(call))
2050         return BUDB_NOTPERMITTED;
2051
2052     eval = InitRPC(&ut, LOCKREAD, 1);
2053     if (eval)
2054         return (eval);
2055
2056     if ((strcmp(vsname, "") == 0) && (strcmp(dumpPath, "") == 0)) {
2057         /* Construct a database dump name */
2058         strcpy(dumpName, DUMP_TAPE_NAME);
2059     } else if (strchr(dumpPath, '/') == 0) {
2060         int level, old, length, hash;
2061         struct dump hostDump, diskDump;
2062         struct memoryHashTable *mht;
2063         int entrySize;
2064         dbadr dbAddr;
2065         afs_uint32 bestDumpId = 0;
2066
2067         level = atoi(dumpPath);
2068         if (level < 0) {
2069             ABORT(BUDB_BADARGUMENT);
2070         }
2071
2072         /* Brute force search of all the dumps in the database - yuck! */
2073
2074         retdbaddr = 0;
2075         mht = ht_GetType(HT_dumpIden_FUNCTION, &entrySize);
2076         if (!mht)
2077             ABORT(BUDB_BADARGUMENT);
2078
2079         for (old = 0; old <= 1; old++) {        /*fo */
2080             length = (old ? mht->oldLength : mht->length);
2081             if (!length)
2082                 continue;
2083
2084             for (hash = 0; hash < length; hash++) {
2085                 /*f */
2086                 for (dbAddr = ht_LookupBucket(ut, mht, hash, old); dbAddr;
2087                      dbAddr = hostDump.idHashChain) {
2088                     /*w */
2089                     eval = dbread(ut, dbAddr, &diskDump, sizeof(diskDump));
2090                     if (eval)
2091                         ABORT(eval);
2092                     dump_ntoh(&diskDump, &hostDump);
2093
2094                     if ((strcmp(hostDump.volumeSet, vsname) == 0) &&    /* the volumeset */
2095                         (hostDump.level == level) &&    /* same level */
2096                         (hostDump.id > bestDumpId)) {   /* more recent */
2097                         bestDumpId = hostDump.id;
2098                         retdbaddr = dbAddr;
2099                     }
2100                 }               /*w */
2101             }                   /*f */
2102         }                       /*fo */
2103         if (!retdbaddr)
2104             ABORT(BUDB_NODUMPNAME);
2105
2106         goto finished;
2107     } else {
2108         /* construct the name of the dump */
2109         if ((strlen(vsname) + strlen(tailCompPtr(dumpPath))) > BU_MAXNAMELEN)
2110             ABORT(BUDB_NODUMPNAME);
2111
2112         strcpy(dumpName, vsname);
2113         strcat(dumpName, ".");
2114         strcat(dumpName, tailCompPtr(dumpPath));
2115     }
2116
2117     LogDebug(5, "lookup on :%s:\n", dumpName);
2118
2119     /* Lookup on dumpname in hash table */
2120     eval = ht_LookupEntry(ut, &db.dumpName, dumpName, &firstdbaddr, &d);
2121     if (eval)
2122         ABORT(eval);
2123
2124     latest = 0;
2125     retdbaddr = 0;
2126
2127     /* folow remaining dumps in hash chain, looking for most latest dump */
2128     for (curdbaddr = firstdbaddr; curdbaddr;
2129          curdbaddr = ntohl(d.nameHashChain)) {
2130         if (curdbaddr != firstdbaddr) {
2131             eval = dbread(ut, curdbaddr, &d, sizeof(d));
2132             if (eval)
2133                 ABORT(eval);
2134         }
2135
2136         if ((strcmp(d.dumpPath, dumpPath) == 0) &&      /* Same dumppath */
2137             (strcmp(d.dumpName, dumpName) == 0) &&      /* Same dumpname */
2138             (ntohl(d.created) > latest)) {      /* most recent */
2139             latest = ntohl(d.created);
2140             retdbaddr = curdbaddr;
2141         }
2142     }
2143     if (!retdbaddr)
2144         ABORT(BUDB_NODUMPNAME);
2145
2146   finished:
2147     /* return the dump found */
2148     eval = FillDumpEntry(ut, retdbaddr, dumpentry);
2149     if (eval)
2150         ABORT(eval);
2151
2152     code = ubik_EndTrans(ut);
2153     return (code);
2154
2155   abort_exit:
2156     ubik_AbortTrans(ut);
2157     return (code);
2158 }
2159
2160
2161 afs_int32
2162 SBUDB_FinishDump(struct rx_call *call, struct budb_dumpEntry *dump)
2163 {
2164     afs_int32 code;
2165
2166     code = FinishDump(call, dump);
2167     osi_auditU(call, BUDB_FinDmpEvent, code, AUD_DATE, (dump ? dump->id : 0),
2168                AUD_END);
2169     return code;
2170 }
2171
2172 afs_int32
2173 FinishDump(struct rx_call *call, struct budb_dumpEntry *dump)
2174 {
2175     struct ubik_trans *ut;
2176     dbadr a;
2177     struct dump d;
2178     afs_int32 eval, code = 0;
2179
2180     if (!callPermitted(call))
2181         return BUDB_NOTPERMITTED;
2182
2183     eval = InitRPC(&ut, LOCKWRITE, 1);
2184     if (eval)
2185         return eval;
2186
2187     eval = ht_LookupEntry(ut, &db.dumpIden, &dump->id, &a, &d);
2188     if (eval)
2189         ABORT(eval);
2190     if (!a)
2191         ABORT(BUDB_NODUMPID);
2192
2193     if ((ntohl(d.flags) & BUDB_DUMP_INPROGRESS) == 0)
2194         ABORT(BUDB_DUMPNOTINUSE);
2195
2196     d.flags = htonl(dump->flags & ~BUDB_DUMP_INPROGRESS);
2197
2198     /* if creation time specified set it */
2199     if (dump->created)
2200         d.created = htonl(dump->created);
2201     dump->created = ntohl(d.created);
2202
2203     /* Write the dump entry out */
2204     eval = dbwrite(ut, a, &d, sizeof(d));
2205     if (eval)
2206         ABORT(eval);
2207
2208     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
2209     if (eval)
2210         ABORT(eval);
2211
2212     code = ubik_EndTrans(ut);
2213     return code;
2214
2215   abort_exit:
2216     ubik_AbortTrans(ut);
2217     return code;
2218 }
2219
2220 afs_int32
2221 SBUDB_FinishTape(struct rx_call *call, struct budb_tapeEntry *tape)
2222 {
2223     afs_int32 code;
2224
2225     code = FinishTape(call, tape);
2226     osi_auditU(call, BUDB_FinTpeEvent, code, AUD_DATE,
2227                (tape ? tape->dump : 0), AUD_END);
2228     return code;
2229 }
2230
2231 afs_int32
2232 FinishTape(struct rx_call *call, struct budb_tapeEntry *tape)
2233 {
2234     struct ubik_trans *ut;
2235     dbadr a;
2236     struct tape t;
2237     struct dump d;
2238     afs_int32 eval, code = 0;
2239
2240     if (!callPermitted(call))
2241         return BUDB_NOTPERMITTED;
2242
2243     eval = InitRPC(&ut, LOCKWRITE, 1);
2244     if (eval)
2245         return eval;
2246
2247     /* find the tape struct in the tapename hash chain */
2248     eval = ht_LookupEntry(ut, &db.tapeName, tape->name, &a, &t);
2249     if (eval)
2250         ABORT(eval);
2251     if (!a)
2252         ABORT(BUDB_NOTAPENAME);
2253
2254     /* Read the dump structure */
2255     eval = dbread(ut, ntohl(t.dump), &d, sizeof(d));
2256     if (eval)
2257         ABORT(eval);
2258
2259     /* search for the right tape on the rest of the chain */
2260     while (ntohl(d.id) != tape->dump) {
2261         a = ntohl(t.nameHashChain);
2262         if (!a)
2263             ABORT(BUDB_NOTAPENAME);
2264
2265         eval = dbread(ut, a, &t, sizeof(t));
2266         if (eval)
2267             ABORT(eval);
2268
2269         eval = dbread(ut, ntohl(t.dump), &d, sizeof(d));
2270         if (eval)
2271             ABORT(eval);
2272     }
2273
2274     if ((ntohl(t.flags) & BUDB_TAPE_BEINGWRITTEN) == 0)
2275         ABORT(BUDB_TAPENOTINUSE);
2276
2277     /* t.nBytes = htonl(tape->nBytes); */
2278     t.nFiles = htonl(tape->nFiles);
2279     t.useKBytes = htonl(tape->useKBytes);
2280     t.flags = htonl(tape->flags & ~BUDB_TAPE_BEINGWRITTEN);
2281
2282     eval = dbwrite(ut, a, &t, sizeof(t));
2283     if (eval)
2284         ABORT(BUDB_IO);
2285
2286     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
2287     if (eval)
2288         ABORT(eval);
2289
2290     code = ubik_EndTrans(ut);
2291     return code;
2292
2293   abort_exit:
2294     ubik_AbortTrans(ut);
2295     return code;
2296 }
2297
2298 /* BUDB_GetDumps
2299  *      return a set of dumps that match the specified criteria
2300  * entry:
2301  *      call - rx call
2302  *      majorVersion - version of interface structures. Permits compatibility
2303  *              checks to be made
2304  *      flags - for search and select operations. Broken down into flags
2305  *              for name, start point, end point and time.
2306  *      name - name to search for. Interpretation based on flags
2307  *      end
2308  *      index
2309  *      nextIndexP
2310  *      dbTimeP
2311  * exit:
2312  *      nextIndexP
2313  *      dbTimeP - time at which the database was last modified. Up to
2314  *              caller (client) to take appropriate action if database
2315  *              modified between successive calls
2316  *      dumps - list of matching dumps
2317  * notes:
2318  *      currently supported are:
2319  *      BUDB_OP_DUMPNAME
2320  *      BUDB_OP_DUMPID
2321  */
2322
2323 afs_int32
2324 SBUDB_GetDumps(struct rx_call *call,
2325                afs_int32 majorVersion,  /* version of interface structures */
2326                afs_int32 flags,         /* search & select controls */
2327                char *name,              /* s&s parameters */
2328                afs_int32 start,
2329                afs_int32 end,
2330                afs_int32 index,         /* start index of returned entries */
2331                afs_int32 *nextIndexP,   /* output index for next call */
2332                afs_int32 *dbTimeP,
2333                budb_dumpList *dumps)    /* pointer to buffer */
2334 {
2335     afs_int32 code;
2336
2337     code =
2338         GetDumps(call, majorVersion, flags, name, start, end, index,
2339                  nextIndexP, dbTimeP, dumps);
2340     osi_auditU(call, BUDB_GetDmpEvent, code, AUD_END);
2341     return code;
2342 }
2343
2344 afs_int32
2345 GetDumps(struct rx_call *call,
2346          afs_int32 majorVersion, /* version of interface structures */
2347          afs_int32 flags,        /* search & select controls */
2348          char *name,             /* s&s parameters */
2349          afs_int32 start,
2350          afs_int32 end,
2351          afs_int32 index,        /* start index of returned entries */
2352          afs_int32 *nextIndexP,  /* output index for next call */
2353          afs_int32 *dbTimeP,
2354          budb_dumpList *dumps)   /* pointer to buffer */
2355 {
2356     struct ubik_trans *ut;
2357     dbadr da;
2358     struct dump d;
2359     afs_int32 nameFlags, startFlags, endFlags, timeFlags;
2360     afs_int32 eval, code = 0;
2361     afs_int32 toskip;
2362     struct returnList list;
2363
2364     /* Don't check permissions when we look up a specific dump id */
2365     if (((flags & BUDB_OP_STARTS) != BUDB_OP_DUMPID) && !callPermitted(call))
2366         return BUDB_NOTPERMITTED;
2367
2368     if (majorVersion != BUDB_MAJORVERSION)
2369         return BUDB_OLDINTERFACE;
2370     if (index < 0)
2371         return BUDB_ENDOFLIST;
2372
2373     eval = InitRPC(&ut, LOCKREAD, 1);
2374     if (eval)
2375         return eval;
2376
2377     nameFlags = flags & BUDB_OP_NAMES;
2378     startFlags = flags & BUDB_OP_STARTS;
2379     endFlags = flags & BUDB_OP_ENDS;
2380     timeFlags = flags & BUDB_OP_TIMES;
2381
2382     InitReturnList(&list);
2383     toskip = index;
2384
2385     if (nameFlags == BUDB_OP_DUMPNAME) {
2386         /* not yet implemented */
2387         if (startFlags || endFlags || timeFlags)
2388             ABORT(BUDB_BADFLAGS);
2389
2390         eval = ht_LookupEntry(ut, &db.dumpName, name, &da, &d);
2391         if (eval)
2392             ABORT(eval);
2393         if (!da)
2394             ABORT(BUDB_NODUMPNAME);
2395
2396         while (1) {
2397             if (strcmp(d.dumpName, name) == 0) {
2398                 eval = AddToReturnList(&list, da, &toskip);
2399                 if (eval == BUDB_LIST2BIG)
2400                     break;
2401                 if (eval)
2402                     ABORT(eval);
2403             }
2404
2405             da = ntohl(d.nameHashChain);        /* get next dump w/ name */
2406             if (!da)
2407                 break;
2408
2409             eval = dbread(ut, da, &d, sizeof(d));
2410             if (eval)
2411                 ABORT(eval);
2412         }
2413     } else if (nameFlags == BUDB_OP_VOLUMENAME) {
2414 #ifdef PA
2415         struct volInfo vi;
2416
2417         LogError(0, "NYI, BUDB_OP_VOLUMENAME\n");
2418         ABORT(BUDB_BADFLAGS);
2419
2420
2421         if (startFlags != BUDB_OP_STARTTIME)
2422             ABORT(BUDB_BADFLAGS);
2423
2424         /* lookup a dump by volumename and time stamp. Find the most recent
2425          * dump of the specified volumename, that occured before the supplied
2426          * time
2427          */
2428
2429         /* get us a volInfo for name */
2430         eval = ht_LookupEntry(ut, &db.volName, name, &da, &vi);
2431         if (eval)
2432             ABORT(eval);
2433
2434         while (1) {
2435             /* now iterate over all the entries of this name */
2436             for (va = vi.firstFragment; va != 0; va = v.sameNameChain) {
2437                 va = ntohl(va);
2438                 eval = dbread(ut, va, &v, sizeof(v));
2439                 if (eval)
2440                     ABORT(eval);
2441
2442                 if date
2443                     on fragment > date ignore it - too recent;
2444
2445                 if (date on fragment < date && date on fragment > bestfound)
2446                     bestfound = date on fragment;
2447
2448             }                   /* for va */
2449
2450             da = vi.sameNameChain;
2451             if (da == 0)
2452                 break;
2453             da = ntohl(da);
2454             eval = dbread(ut, da, &vi, sizeof(vi));
2455             if (eval)
2456                 ABORT(eval);
2457         }
2458
2459 /*
2460         if nothing found
2461                 return error
2462
2463         from saved volfragment address, compute dump.
2464         otherwise, return dump found
2465 */
2466
2467 #endif /* PA */
2468
2469     } else if (startFlags == BUDB_OP_DUMPID) {
2470         if (endFlags || timeFlags)
2471             ABORT(BUDB_BADFLAGS);
2472         if (nameFlags)
2473             ABORT(BUDB_BADFLAGS);       /* NYI */
2474
2475         eval = ht_LookupEntry(ut, &db.dumpIden, &start, &da, &d);
2476         if (eval)
2477             ABORT(eval);
2478         if (!da)
2479             ABORT(BUDB_NODUMPID);
2480
2481         eval = AddToReturnList(&list, da, &toskip);
2482         if (eval)
2483             ABORT(eval);
2484     } else if (endFlags == BUDB_OP_NPREVIOUS) {
2485         struct wantDumpRock rock;
2486         struct chosenDump *ptr, *nextPtr;
2487
2488         /* no other flags should be set */
2489
2490         /* end specifies how many dumps */
2491         if (!end)
2492             ABORT(BUDB_BADFLAGS);
2493
2494         memset(&rock, 0, sizeof(rock));
2495         rock.maxDumps = end;
2496
2497         scanHashTable(ut, &db.dumpName, wantDump, rememberDump,
2498                       (char *)&rock);
2499
2500         for (ptr = rock.chain; ptr; ptr = nextPtr) {
2501             nextPtr = ptr->next;
2502             AddToReturnList(&list, ptr->addr, &toskip); /* ignore error for free */
2503             free(ptr);
2504         }
2505     } else {
2506         ABORT(BUDB_BADFLAGS);
2507     }
2508
2509     eval =
2510         SendReturnList(ut, &list, FillDumpEntry,
2511                        sizeof(struct budb_dumpEntry), index, nextIndexP,
2512                        dbTimeP, (returnList_t) dumps);
2513     if (eval)
2514         ABORT(eval);
2515
2516     FreeReturnList(&list);
2517     code = ubik_EndTrans(ut);
2518     return code;
2519
2520   abort_exit:
2521     FreeReturnList(&list);
2522     ubik_AbortTrans(ut);
2523     return code;
2524 }
2525
2526 /*
2527  * Get the expiration of a tape.  Since the dump could have appended dumps,
2528  * we should use the most recent expiration date. Put the most recent
2529  * expiration tape into the given tape structure.
2530  */
2531 afs_int32
2532 getExpiration(struct ubik_trans *ut, struct tape *tapePtr)
2533 {
2534     dbadr ad;
2535     struct dump d;
2536     struct tape t;
2537     afs_int32 initDump;
2538     afs_int32 eval, code = 0;
2539
2540     if (!tapePtr)
2541         ERROR(0);
2542
2543     /* Get the dump for this tape */
2544     ad = ntohl(tapePtr->dump);
2545     eval = dbread(ut, ad, &d, sizeof(d));
2546     if (eval)
2547         ERROR(eval);
2548
2549     /* If not an initial dump, get the initial dump */
2550     if (d.initialDumpID) {
2551         initDump = ntohl(d.initialDumpID);
2552         eval = ht_LookupEntry(ut, &db.dumpIden, &initDump, &ad, &d);
2553         if (eval)
2554             ERROR(eval);
2555     }
2556
2557     /* Cycle through the dumps and appended dumps */
2558     while (ad) {
2559         /* Get the first tape in this dump. No need to check the rest of the tapes */
2560         /* for this dump since they will all have the same expiration date */
2561         eval = dbread(ut, ntohl(d.firstTape), &t, sizeof(t));
2562         if (eval)
2563             ERROR(eval);
2564
2565         /* Take the greater of the expiration dates */
2566         if (ntohl(tapePtr->expires) < ntohl(t.expires))
2567             tapePtr->expires = t.expires;
2568
2569         /* Step to and read the next appended dump */
2570         if ((ad = ntohl(d.appendedDumpChain))) {
2571             eval = dbread(ut, ad, &d, sizeof(d));
2572             if (eval)
2573                 ERROR(eval);
2574         }
2575     }
2576
2577   error_exit:
2578     return (code);
2579 }
2580
2581 /* Mark the following dump as appended to another, intial dump */
2582 afs_int32
2583 makeAppended(struct ubik_trans *ut, afs_int32 appendedDumpID,
2584              afs_int32 initialDumpID, afs_int32 startTapeSeq)
2585 {
2586     dbadr ada, da, lastDumpAddr;
2587     struct dump ad, d;
2588     afs_int32 eval, code = 0;
2589
2590     if (!initialDumpID)
2591         ERROR(0);
2592     if (appendedDumpID == initialDumpID)
2593         ERROR(BUDB_INTERNALERROR);
2594
2595     /* If there is an initial dump, append this dump to it */
2596     /* Find the appended dump via its id */
2597     eval = ht_LookupEntry(ut, &db.dumpIden, &appendedDumpID, &ada, &ad);
2598     if (eval)
2599         ERROR(eval);
2600
2601     /* If the dump is already marked as appended,
2602      * then we have an internal error.
2603      */
2604     if (ad.initialDumpID) {
2605         if (ntohl(ad.initialDumpID) != initialDumpID)
2606             ERROR(BUDB_INTERNALERROR);
2607     }
2608
2609     /* Update the appended dump to point to the initial dump */
2610     ad.initialDumpID = htonl(initialDumpID);
2611     ad.tapes.b = htonl(startTapeSeq);
2612
2613     /* find the initial dump via its id */
2614     eval = ht_LookupEntry(ut, &db.dumpIden, &initialDumpID, &da, &d);
2615     if (eval)
2616         ERROR(eval);
2617
2618     /* Update the appended dump's tape format with that of the initial */
2619     strcpy(ad.tapes.format, d.tapes.format);
2620
2621     /* starting with the initial dump step through its appended dumps till
2622      * we reach the last appended dump.
2623      */
2624     lastDumpAddr = da;
2625     while (d.appendedDumpChain) {
2626         lastDumpAddr = ntohl(d.appendedDumpChain);
2627         if (lastDumpAddr == ada)
2628             ERROR(0);           /* Already appended */
2629         eval = dbread(ut, lastDumpAddr, &d, sizeof(d));
2630         if (eval)
2631             ERROR(eval);
2632     }
2633
2634     /* Update the last dump to point to our new appended dump.
2635      * The appended dump is the last one in the dump chain.
2636      */
2637     d.appendedDumpChain = htonl(ada);
2638     ad.appendedDumpChain = 0;
2639
2640     /* Write the appended dump and the initial dump */
2641     eval = dbwrite(ut, ada, (char *)&ad, sizeof(ad));
2642     if (eval)
2643         ERROR(eval);
2644
2645     eval = dbwrite(ut, lastDumpAddr, (char *)&d, sizeof(d));
2646     if (eval)
2647         ERROR(eval);
2648
2649     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
2650     if (eval)
2651         ERROR(eval);
2652
2653   error_exit:
2654     return (code);
2655 }
2656
2657 afs_int32
2658 SBUDB_MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
2659                        afs_int32 initialDumpID, afs_int32 startTapeSeq)
2660 {
2661     afs_int32 code;
2662
2663     code =
2664         MakeDumpAppended(call, appendedDumpID, initialDumpID, startTapeSeq);
2665     osi_auditU(call, BUDB_AppDmpEvent, code, AUD_LONG, appendedDumpID,
2666                AUD_END);
2667     return code;
2668 }
2669
2670 afs_int32
2671 MakeDumpAppended(struct rx_call *call, afs_int32 appendedDumpID,
2672                  afs_int32 initialDumpID, afs_int32 startTapeSeq)
2673 {
2674     struct ubik_trans *ut;
2675     afs_int32 eval, code = 0;
2676
2677     if (!callPermitted(call))
2678         return BUDB_NOTPERMITTED;
2679
2680     eval = InitRPC(&ut, LOCKWRITE, 1);
2681     if (eval)
2682         return (eval);
2683
2684     eval = makeAppended(ut, appendedDumpID, initialDumpID, startTapeSeq);
2685     if (eval)
2686         ABORT(eval);
2687
2688     code = ubik_EndTrans(ut);
2689     return (code);
2690
2691   abort_exit:
2692     ubik_AbortTrans(ut);
2693     return (code);
2694 }
2695
2696 /* Find the last tape of a dump-set. This includes any appended dumps */
2697 afs_int32
2698 SBUDB_FindLastTape(struct rx_call *call, afs_int32 dumpID,
2699                    struct budb_dumpEntry *dumpEntry,
2700                    struct budb_tapeEntry *tapeEntry,
2701                    struct budb_volumeEntry *volEntry)
2702 {
2703     afs_int32 code;
2704
2705     code = FindLastTape(call, dumpID, dumpEntry, tapeEntry, volEntry);
2706     osi_auditU(call, BUDB_FndLTpeEvent, code, AUD_LONG, dumpID, AUD_END);
2707     return code;
2708 }
2709
2710 afs_int32
2711 FindLastTape(struct rx_call *call, afs_int32 dumpID,
2712              struct budb_dumpEntry *dumpEntry,
2713              struct budb_tapeEntry *tapeEntry,
2714              struct budb_volumeEntry *volEntry)
2715 {
2716     struct ubik_trans *ut;
2717     struct dump d;
2718     dbadr lastDump;
2719     struct tape t;
2720     dbadr lastTape, thisTape;
2721     afs_int32 lastTapeSeq;
2722     struct volFragment vf;
2723     dbadr lastVol, thisVol;
2724     afs_int32 lastVolPos;
2725     afs_int32 eval, code = 0;
2726
2727     if (!callPermitted(call))
2728         return BUDB_NOTPERMITTED;
2729
2730     if (!dumpID)
2731         return (BUDB_BADARGUMENT);
2732
2733     eval = InitRPC(&ut, LOCKREAD, 1);
2734     if (eval)
2735         return (eval);
2736
2737     /* find and read its initial dump via its id */
2738     eval = ht_LookupEntry(ut, &db.dumpIden, &dumpID, &lastDump, &d);
2739     if (eval)
2740         ABORT(eval);
2741     if (!lastDump)
2742         ABORT(BUDB_NODUMPID);
2743
2744     /* Follow the append dumps link chain until we reach the last dump */
2745     while (d.appendedDumpChain) {
2746         lastDump = ntohl(d.appendedDumpChain);
2747         eval = dbread(ut, lastDump, &d, sizeof(d));
2748         if (eval)
2749             ABORT(eval);
2750     }
2751
2752     /* We now have the last dump of the last appended dump */
2753     /* Copy this into our return structure */
2754     eval = FillDumpEntry(ut, lastDump, dumpEntry);
2755     if (eval)
2756         ABORT(eval);
2757
2758     /* Fail if the last dump has no tapes */
2759     if (!d.firstTape)
2760         ABORT(BUDB_NOTAPENAME);
2761
2762     /* Follow the tapes in this dump until we reach the last tape */
2763     eval = dbread(ut, ntohl(d.firstTape), &t, sizeof(t));
2764     if (eval)
2765         ABORT(eval);
2766
2767     lastTape = ntohl(d.firstTape);
2768     lastTapeSeq = ntohl(t.seq);
2769     lastVol = ntohl(t.firstVol);
2770
2771     while (t.nextTape) {
2772         thisTape = ntohl(t.nextTape);
2773         eval = dbread(ut, thisTape, &t, sizeof(t));
2774         if (eval)
2775             ABORT(eval);
2776
2777         if (ntohl(t.seq) > lastTapeSeq) {
2778             lastTape = thisTape;
2779             lastTapeSeq = ntohl(t.seq);
2780             lastVol = ntohl(t.firstVol);
2781         }
2782     }
2783
2784     /* We now have the last tape of the last appended dump */
2785     /* Copy this into our return structure */
2786     eval = FillTapeEntry(ut, lastTape, tapeEntry);
2787     if (eval)
2788         ABORT(eval);
2789
2790     /* Zero volume entry if the last tape has no volumes */
2791     if (!lastVol) {
2792         memset(volEntry, 0, sizeof(*volEntry));
2793     } else {
2794         /* Follow the volumes until we reach the last volume */
2795         eval = dbread(ut, lastVol, &vf, sizeof(vf));
2796         if (eval)
2797             ABORT(eval);
2798
2799         lastVolPos = vf.position;
2800
2801         while (vf.sameTapeChain) {
2802             thisVol = ntohl(vf.sameTapeChain);
2803             eval = dbread(ut, thisVol, &vf, sizeof(vf));
2804             if (eval)
2805                 ABORT(eval);
2806
2807             if (vf.position > lastVolPos) {
2808                 lastVol = thisVol;
2809                 lastVolPos = vf.position;
2810             }
2811         }
2812
2813         /* We now have the last volume of this tape */
2814         /* Copy this into our return structure */
2815         eval = FillVolEntry(ut, lastVol, volEntry);
2816         if (eval)
2817             ABORT(eval);
2818     }
2819
2820     eval = ubik_EndTrans(ut);
2821     if (!code)
2822         code = eval;
2823     return (code);
2824
2825   abort_exit:
2826     ubik_AbortTrans(ut);
2827     return (code);
2828 }
2829
2830
2831 afs_int32
2832 SBUDB_GetTapes(struct rx_call *call,
2833                afs_int32 majorVersion,  /* version of interface structures */
2834                afs_int32 flags,         /* search & select controls */
2835                char *name,              /* s&s parameters */
2836                afs_int32 start,
2837                afs_int32 end,           /* reserved: MBZ */
2838                afs_int32 index,         /* start index of returned entries */
2839                afs_int32 *nextIndexP,   /* output index for next call */
2840                afs_int32 *dbTimeP,
2841                budb_tapeList *tapes)    /* pointer to buffer */
2842 {
2843     afs_int32 code;
2844
2845     code =
2846         GetTapes(call, majorVersion, flags, name, start, end, index,
2847                  nextIndexP, dbTimeP, tapes);
2848     osi_auditU(call, BUDB_GetTpeEvent, code, AUD_END);
2849     return code;
2850 }
2851
2852 afs_int32
2853 GetTapes(struct rx_call *call,
2854          afs_int32 majorVersion, /* version of interface structures */
2855          afs_int32 flags,        /* search & select controls */
2856          char *name,             /* s&s parameters */
2857          afs_int32 start,
2858          afs_int32 end,          /* reserved: MBZ */
2859          afs_int32 index,        /* start index of returned entries */
2860          afs_int32 *nextIndexP,  /* output index for next call */
2861          afs_int32 *dbTimeP,
2862          budb_tapeList *tapes)   /* pointer to buffer */
2863 {
2864     struct ubik_trans *ut;
2865     dbadr da, ta;
2866     struct dump d;
2867     struct tape t;
2868     afs_int32 nameFlags, startFlags, endFlags, timeFlags;
2869     struct returnList list;
2870     afs_int32 eval, code = 0;
2871     afs_int32 toskip;
2872
2873     if (!callPermitted(call))
2874         return BUDB_NOTPERMITTED;
2875
2876     if (majorVersion != BUDB_MAJORVERSION)
2877         return BUDB_OLDINTERFACE;
2878
2879     if (index < 0)
2880         return BUDB_ENDOFLIST;
2881
2882     eval = InitRPC(&ut, LOCKREAD, 1);
2883     if (eval)
2884         return eval;
2885
2886     nameFlags = flags & BUDB_OP_NAMES;
2887     startFlags = flags & BUDB_OP_STARTS;
2888     endFlags = flags & BUDB_OP_ENDS;
2889     timeFlags = flags & BUDB_OP_TIMES;
2890
2891     InitReturnList(&list);
2892     toskip = index;
2893
2894     if (nameFlags == BUDB_OP_TAPENAME) {        /*it */
2895         eval = ht_LookupEntry(ut, &db.tapeName, name, &ta, &t);
2896         if (eval)
2897             ABORT(eval);
2898         if (!ta)
2899             ABORT(BUDB_NOTAPENAME);
2900
2901         /* NYI */
2902         if ((startFlags & ~BUDB_OP_DUMPID) || endFlags || timeFlags)
2903             ABORT(BUDB_BADFLAGS);
2904
2905         /* follow the hash chain to the end */
2906         while (ta) {            /*w */
2907             if (startFlags & BUDB_OP_DUMPID) {
2908                 /* read in the dump */
2909                 eval = dbread(ut, ntohl(t.dump), &d, sizeof(d));
2910                 if (eval)
2911                     ABORT(eval);
2912
2913                 /* check if both name and dump id match */
2914                 if ((strcmp(name, t.name) == 0) && (ntohl(d.id) == start)) {
2915                     eval = AddToReturnList(&list, ta, &toskip);
2916                     if (eval && (eval != BUDB_LIST2BIG))
2917                         ABORT(eval);
2918                     break;
2919                 }
2920             } else {
2921                 /* Add to return list and continue search */
2922                 if (strcmp(name, t.name) == 0) {
2923                     eval = AddToReturnList(&list, ta, &toskip);
2924                     if (eval == BUDB_LIST2BIG)
2925                         break;
2926                     if (eval)
2927                         ABORT(eval);
2928                 }
2929             }
2930
2931             ta = ntohl(t.nameHashChain);
2932             if (ta)
2933                 dbread(ut, ta, &t, sizeof(t));
2934         }                       /*w */
2935     } /*it */
2936     else if (nameFlags == BUDB_OP_TAPESEQ) {
2937         eval = ht_LookupEntry(ut, &db.dumpIden, &start, &da, &d);
2938         if (eval)
2939             ABORT(eval);
2940         if (!da)
2941             ABORT(BUDB_NODUMPNAME);
2942
2943         /* search for the right tape */
2944         ta = ntohl(d.firstTape);
2945         for (ta = ntohl(d.firstTape); ta; ta = ntohl(t.nextTape)) {
2946             eval = dbread(ut, ta, &t, sizeof(t));
2947             if (eval)
2948                 ABORT(eval);
2949
2950             if (ntohl(t.seq) == end) {
2951                 eval = AddToReturnList(&list, ta, &toskip);
2952                 if (eval && (eval != BUDB_LIST2BIG))
2953                     ABORT(eval);
2954                 break;
2955             }
2956         }
2957     } else {
2958         ABORT(BUDB_BADFLAGS);
2959     }
2960
2961     eval =
2962         SendReturnList(ut, &list, FillTapeEntry,
2963                        sizeof(struct budb_tapeEntry), index, nextIndexP,
2964                        dbTimeP, (returnList_t) tapes);
2965     if (eval)
2966         ABORT(eval);
2967
2968     FreeReturnList(&list);
2969     code = ubik_EndTrans(ut);
2970     return code;
2971
2972   abort_exit:
2973     FreeReturnList(&list);
2974     ubik_AbortTrans(ut);
2975     return (code);
2976 }
2977
2978 /* BUDB_GetVolumes
2979  *      get a set of volumes according to the specified criteria.
2980  *      See BUDB_GetDumps for general information on parameters
2981  *      Currently supports:
2982  *      1) volume match - returns volumes based on volume name only.
2983  *      2) flags = BUDB_OP_DUMPID in which case name is a volume name
2984  *              and start is a dumpid. Returns all volumes of the specified
2985  *              name on the selected dumpid.
2986  */
2987
2988 afs_int32
2989 SBUDB_GetVolumes(struct rx_call *call,
2990                  afs_int32 majorVersion, /* version of interface structures */
2991                  afs_int32 flags,        /* search & select controls */
2992                  char *name,             /*  - parameters for search */
2993                  afs_int32 start,        /*  - usage depends which BUDP_OP */
2994                  afs_int32 end,          /*  - bits are set */
2995                  afs_int32 index,        /* start index of returned entries */
2996                  afs_int32 *nextIndexP,  /* output index for next call */
2997                  afs_int32 *dbTimeP,
2998                  budb_volumeList *volumes) /* pointer to buffer */
2999 {
3000     afs_int32 code;
3001
3002     code =
3003         GetVolumes(call, majorVersion, flags, name, start, end, index,
3004                    nextIndexP, dbTimeP, volumes);
3005     osi_auditU(call, BUDB_GetVolEvent, code, AUD_END);
3006     return code;
3007 }
3008
3009 afs_int32
3010 GetVolumes(struct rx_call *call,
3011            afs_int32 majorVersion,      /* version of interface structures */
3012            afs_int32 flags,             /* search & select controls */
3013            char *name,                  /*  - parameters for search */
3014            afs_int32 start,             /*  - usage depends which BUDP_OP_* */
3015            afs_int32 end,               /*  - bits are set */
3016            afs_int32 index,             /* start index of returned entries */
3017            afs_int32 *nextIndexP,       /* output index for next call */
3018            afs_int32 *dbTimeP,
3019            budb_volumeList *volumes)    /* pointer to buffer */
3020 {
3021     struct ubik_trans *ut;
3022     dbadr via;
3023     struct volInfo vi;
3024     afs_int32 nameFlags, startFlags, endFlags, timeFlags;
3025     afs_int32 eval, code = 0;
3026     struct returnList vollist;
3027     afs_int32 toskip;
3028
3029     /* Don't check permissions when we look up a specific volume name */
3030     if (((flags & BUDB_OP_NAMES) != BUDB_OP_VOLUMENAME)
3031         && !callPermitted(call))
3032         return BUDB_NOTPERMITTED;
3033
3034     if (majorVersion != BUDB_MAJORVERSION)
3035         return BUDB_OLDINTERFACE;
3036     if (index < 0)
3037         return BUDB_ENDOFLIST;
3038
3039     eval = InitRPC(&ut, LOCKREAD, 1);
3040     if (eval)
3041         return eval;
3042
3043     nameFlags = flags & BUDB_OP_NAMES;
3044     startFlags = flags & BUDB_OP_STARTS;
3045     endFlags = flags & BUDB_OP_ENDS;
3046     timeFlags = flags & BUDB_OP_TIMES;
3047
3048     InitReturnList(&vollist);
3049     toskip = index;
3050
3051     /* lookup a the volume (specified by name) in the dump (specified by id) */
3052     if (nameFlags == BUDB_OP_VOLUMENAME) {
3053         /* dumpid permissible, all others off */
3054         if (((startFlags & ~BUDB_OP_DUMPID) != 0) || endFlags || timeFlags)
3055             ABORT(BUDB_BADFLAGS);
3056
3057         /* returns ptr to volinfo of requested name */
3058         eval = ht_LookupEntry(ut, &db.volName, name, &via, &vi);
3059         if (eval)
3060             ABORT(eval);
3061         if (!via)
3062             ABORT(BUDB_NOVOLUMENAME);
3063
3064         /* Iterate over all volume fragments with this name */
3065         while (1) {
3066             struct volFragment v;
3067             afs_int32 va;
3068
3069             /* traverse all the volume fragments for this volume info structure */
3070             for (va = vi.firstFragment; va; va = v.sameNameChain) {
3071                 va = ntohl(va);
3072                 eval = dbread(ut, va, &v, sizeof(v));
3073                 if (eval)
3074                     ABORT(eval);
3075
3076                 if (startFlags & BUDB_OP_DUMPID) {
3077                     struct tape atape;
3078                     struct dump adump;
3079
3080                     /* get the dump id for this fragment */
3081                     eval = dbread(ut, ntohl(v.tape), &atape, sizeof(atape));
3082                     if (eval)
3083                         ABORT(eval);
3084
3085                     eval =
3086                         dbread(ut, ntohl(atape.dump), &adump, sizeof(adump));
3087                     if (eval)
3088                         ABORT(BUDB_IO);
3089
3090                     /* dump id does not match */
3091                     if (ntohl(adump.id) != start)
3092                         continue;
3093                 }
3094
3095                 eval = AddToReturnList(&vollist, va, &toskip);
3096                 if (eval == BUDB_LIST2BIG)
3097                     break;
3098                 if (eval)
3099                     ABORT(eval);
3100             }
3101             if (eval == BUDB_LIST2BIG)
3102                 break;
3103
3104             via = vi.sameNameChain;
3105             if (via == 0)
3106                 break;
3107             via = ntohl(via);
3108
3109             eval = dbread(ut, via, &vi, sizeof(vi));
3110             if (eval)
3111                 ABORT(eval);
3112         }
3113     } else if (((nameFlags == 0) || (nameFlags == BUDB_OP_TAPENAME))
3114                && (startFlags == BUDB_OP_DUMPID)) {
3115         struct dump dump;
3116         dbadr dumpAddr;
3117         struct tape tape;
3118         dbadr tapeAddr;
3119         struct volFragment volFrag;
3120         dbadr volFragAddr;
3121
3122         /* lookup all volumes for a specified dump id */
3123
3124         /* no other flags should be set */
3125         if (endFlags || timeFlags)
3126             ABORT(BUDB_BADFLAGS);
3127
3128         /* find the dump */
3129         eval = ht_LookupEntry(ut, &db.dumpIden, &start, &dumpAddr, &dump);
3130         if (eval)
3131             ABORT(eval);
3132
3133         /* traverse all the tapes */
3134         for (tapeAddr = ntohl(dump.firstTape); tapeAddr; tapeAddr = ntohl(tape.nextTape)) {     /*w */
3135             eval = dbread(ut, tapeAddr, &tape, sizeof(tape));
3136             if (eval)
3137                 ABORT(eval);
3138
3139             if ((nameFlags != BUDB_OP_TAPENAME)
3140                 || ((nameFlags == BUDB_OP_TAPENAME)
3141                     && (strcmp(tape.name, name) == 0))) {
3142                 /* now return all the volumes */
3143                 for (volFragAddr = ntohl(tape.firstVol); volFragAddr;
3144                      volFragAddr = ntohl(volFrag.sameTapeChain)) {
3145                     eval = dbread(ut, volFragAddr, &volFrag, sizeof(volFrag));
3146                     if (eval)
3147                         ABORT(eval);
3148
3149                     eval = AddToReturnList(&vollist, volFragAddr, &toskip);
3150                     if (eval == BUDB_LIST2BIG)
3151                         break;
3152                     if (eval)
3153                         ABORT(eval);
3154                 }
3155             }
3156             if (eval == BUDB_LIST2BIG)
3157                 break;
3158         }                       /*w */
3159     } else {
3160         ABORT(BUDB_BADFLAGS);
3161     }
3162
3163     eval =
3164         SendReturnList(ut, &vollist, FillVolEntry,
3165                        sizeof(struct budb_volumeEntry), index, nextIndexP,
3166                        dbTimeP, (returnList_t) volumes);
3167     if (eval)
3168         ABORT(eval);
3169
3170   /* error_exit: */
3171     FreeReturnList(&vollist);
3172     code = ubik_EndTrans(ut);
3173     return code;
3174
3175   abort_exit:
3176     FreeReturnList(&vollist);
3177     ubik_AbortTrans(ut);
3178     return code;
3179 }
3180
3181 afs_int32
3182 SBUDB_UseTape(struct rx_call *call,
3183               struct budb_tapeEntry *tape,      /* tape info */
3184               afs_int32 *new)                   /* set if tape is new */
3185 {
3186     afs_int32 code;
3187
3188     code = UseTape(call, tape, new);
3189     osi_auditU(call, BUDB_UseTpeEvent, code, AUD_DATE,
3190                (tape ? tape->dump : 0), AUD_END);
3191     return code;
3192 }
3193
3194 afs_int32
3195 UseTape(struct rx_call *call,
3196         struct budb_tapeEntry *tape,    /* tape info */
3197         int *new)                       /* set if tape is new */
3198 {
3199     struct ubik_trans *ut;
3200     dbadr da, a;
3201     struct dump d;
3202     struct tape t;
3203     afs_int32 eval, code;
3204
3205     if (!callPermitted(call))
3206         return BUDB_NOTPERMITTED;
3207
3208     if (strlen(tape->name) >= sizeof(t.name))
3209         return BUDB_BADARGUMENT;
3210
3211     eval = InitRPC(&ut, LOCKWRITE, 1);
3212     if (eval)
3213         return eval;
3214
3215     *new = 0;
3216
3217     memset(&t, 0, sizeof(t));
3218     eval = AllocStructure(ut, tape_BLOCK, 0, &a, &t);
3219     if (eval)
3220         ABORT(eval);
3221
3222     strcpy(t.name, tape->name);
3223
3224     eval = ht_HashIn(ut, &db.tapeName, a, &t);
3225     if (eval)
3226         ABORT(eval);
3227
3228     *new = 1;
3229
3230     /* Since deleting a tape may change the dump (if its the same one), read in
3231      * the dump after the call to DeleteTape. */
3232
3233     eval = ht_LookupEntry(ut, &db.dumpIden, &tape->dump, &da, &d);
3234     if (eval)
3235         ABORT(eval);
3236     if (!da)
3237         ABORT(BUDB_NODUMPID);
3238
3239     if (!tape->written)
3240         tape->written = time(0);        /* fill in tape struct */
3241     t.written = htonl(tape->written);
3242     t.expires = htonl(tape->expires);
3243     t.dump = htonl(da);
3244     t.seq = htonl(tape->seq);
3245     t.useCount = htonl(tape->useCount);
3246     t.labelpos = htonl(tape->labelpos);
3247     t.useKBytes = 0;
3248     t.flags = htonl(tape->flags | BUDB_TAPE_BEINGWRITTEN);
3249
3250     t.nextTape = d.firstTape;   /* Chain the tape to the dump */
3251     d.firstTape = htonl(a);
3252
3253     if (tape->seq >= ntohl(d.tapes.maxTapes))   /* inc # tapes in the dump */
3254         d.tapes.maxTapes = htonl(tape->seq);
3255
3256     eval = dbwrite(ut, a, &t, sizeof(t));       /* write tape struct */
3257     if (eval)
3258         ABORT(eval);
3259
3260     eval = dbwrite(ut, da, &d, sizeof(d));      /* write the dump struct */
3261     if (eval)
3262         ABORT(eval);
3263
3264     eval = set_header_word(ut, lastUpdate, htonl(time(0)));
3265     if (eval)
3266         ABORT(eval);
3267
3268     LogDebug(5, "added tape %s\n", tape->name);
3269
3270     code = ubik_EndTrans(ut);
3271     return code;
3272
3273   abort_exit:
3274     ubik_AbortTrans(ut);
3275     return code;
3276
3277 }
3278
3279
3280 /* ---------------------------------------------
3281  * debug interface routines
3282  * ---------------------------------------------
3283  */
3284
3285 afs_int32
3286 SBUDB_T_DumpHashTable(struct rx_call *call, afs_int32 type, char *filename)
3287 {
3288     afs_int32 code;
3289
3290     code = T_DumpHashTable(call, type, filename);
3291     osi_auditU(call, BUDB_TDmpHaEvent, code, AUD_STR, filename, AUD_END);
3292     return code;
3293 }
3294
3295 afs_int32
3296 T_DumpHashTable(struct rx_call *call, int type, char *filename)
3297 {
3298     struct ubik_trans *ut;
3299     struct memoryHashTable *mht;
3300     int ent;
3301     afs_int32 eval, code = 0;
3302     char path[64];
3303     FILE *DUMP;
3304
3305     int length;
3306     afs_uint32 hash;
3307     dbadr a, first_a;
3308     char e[sizeof(struct block)];       /* unnecessarily conservative */
3309     struct dump e_dump;
3310     struct tape e_tape;
3311     struct volInfo e_volinfo;
3312     int e_size;
3313     int old;
3314
3315     if (!callPermitted(call))
3316         return BUDB_NOTPERMITTED;
3317
3318     if (strlen(filename) >= sizeof(path) - 5)
3319         return BUDB_BADARGUMENT;
3320
3321     eval = InitRPC(&ut, LOCKWRITE, 1);
3322     if (eval)
3323         return eval;
3324
3325     if ((mht = ht_GetType(type, &e_size)) == 0)
3326         return BUDB_BADARGUMENT;
3327
3328     sprintf(path, "%s/%s", gettmpdir(), filename);
3329
3330     DUMP = fopen(path, "w");
3331     if (!DUMP)
3332         ABORT(BUDB_BADARGUMENT);
3333
3334     ent = 0;
3335     for (old = 0;; old++) {
3336         length = (old ? mht->oldLength : mht->length);
3337         if (length)
3338             fprintf(DUMP, "Dumping %sHash Table:\n", (old ? "Old " : ""));
3339
3340         for (hash = 0; hash < length; hash++) {
3341             a = ht_LookupBucket(ut, mht, hash, old);
3342             first_a = a;
3343             while (a) {
3344                 eval = dbread(ut, a, e, e_size);
3345                 if (eval)
3346                     ABORT(eval);
3347
3348                 ent++;
3349                 if (a == first_a)
3350                     fprintf(DUMP, "  in bucket %d at %d is ", hash, a);
3351                 else
3352                     fprintf(DUMP, "    at %d is ", a);
3353                 switch (type) {
3354                 case HT_dumpIden_FUNCTION:
3355                     memcpy(&e_dump, e, sizeof(e_dump));
3356                     fprintf(DUMP, "%d\n", ntohl(e_dump.id));
3357                     break;
3358                 case HT_dumpName_FUNCTION:
3359                     memcpy(&e_dump, e, sizeof(e_dump));
3360                     fprintf(DUMP, "%s\n", e_dump.dumpName);
3361                     break;
3362                 case HT_tapeName_FUNCTION:
3363                     memcpy(&e_tape, e, sizeof(e_tape));
3364                     fprintf(DUMP, "%s\n", e_tape.name);
3365                     break;
3366                 case HT_volName_FUNCTION:
3367                     memcpy(&e_volinfo, e, sizeof(e_volinfo));
3368                     fprintf(DUMP, "%s\n", e_volinfo.name);
3369                     break;
3370                 }
3371                 if ((ht_HashEntry(mht, e) % length) != hash)
3372                     ABORT(BUDB_DATABASEINCONSISTENT);
3373                 a = ntohl(*(dbadr *) (e + mht->threadOffset));
3374             }
3375         }
3376         if (old)
3377             break;
3378     }
3379
3380     fprintf(DUMP, "%d entries found\n", ent);
3381     if (ntohl(mht->ht->entries) != ent)
3382         ABORT(BUDB_DATABASEINCONSISTENT);
3383
3384     code = ubik_EndTrans(ut);
3385     if (DUMP)
3386         fclose(DUMP);
3387     return code;
3388
3389   abort_exit:
3390     ubik_AbortTrans(ut);
3391     if (DUMP)
3392         fclose(DUMP);
3393     return code;
3394 }
3395
3396 afs_int32
3397 SBUDB_T_GetVersion(struct rx_call *call, afs_int32 *majorVersion)
3398 {
3399     afs_int32 code;
3400
3401     code = T_GetVersion(call, majorVersion);
3402     osi_auditU(call, BUDB_TGetVrEvent, code, AUD_END);
3403     return code;
3404 }
3405
3406 afs_int32
3407 T_GetVersion(struct rx_call *call, int *majorVersion)
3408 {
3409     struct ubik_trans *ut;
3410     afs_int32 code;
3411
3412     code = InitRPC(&ut, LOCKREAD, 0);
3413     if (code)
3414         return (code);
3415
3416     *majorVersion = BUDB_MAJORVERSION;
3417
3418     code = ubik_EndTrans(ut);
3419     return (code);
3420 }
3421
3422 /* BUDB_T_DumpDatabase
3423  *      dump as much of the database as possible int /tmp/<filename>
3424  */
3425
3426 afs_int32
3427 SBUDB_T_DumpDatabase(struct rx_call *call, char *filename)
3428 {
3429     afs_int32 code;
3430
3431     code = T_DumpDatabase(call, filename);
3432     osi_auditU(call, BUDB_TDmpDBEvent, code, AUD_STR, filename, AUD_END);
3433     return code;
3434 }
3435
3436 afs_int32
3437 T_DumpDatabase(struct rx_call *call, char *filename)
3438 {
3439     FILE *dumpfid;
3440     int entrySize;
3441     struct ubik_trans *ut;
3442     char *path = 0;
3443     dbadr dbAddr;
3444     int type, old, length, hash;
3445     struct memoryHashTable *mht;
3446     afs_int32 eval, code = 0;
3447
3448     if (!callPermitted(call))
3449         return BUDB_NOTPERMITTED;
3450
3451     length = asprintf(&path, "%s/%s", gettmpdir(), filename);
3452     if (length < 0 || !path)
3453         return (BUDB_INTERNALERROR);
3454
3455     dumpfid = fopen(path, "w");
3456     if (!dumpfid)
3457         return (BUDB_BADARGUMENT);
3458
3459     eval = InitRPC(&ut, LOCKWRITE, 1);
3460     if (eval)
3461         return (eval);
3462
3463     /* dump all items in the database */
3464     for (type = 1; type <= HT_MAX_FUNCTION; type++) {   /*ft */
3465         mht = ht_GetType(type, &entrySize);
3466         if (!mht)
3467             ERROR(BUDB_BADARGUMENT);
3468
3469         for (old = 0; old <= 1; old++) {        /*fo */
3470             length = (old ? mht->oldLength : mht->length);
3471             if (!length)
3472                 continue;
3473
3474             fprintf(dumpfid, "Dumping %s Hash Table:\n", (old ? "Old " : ""));
3475
3476             for (hash = 0; hash < length; hash++) {     /*f */
3477                 dbAddr = ht_LookupBucket(ut, mht, hash, old);
3478
3479                 while (dbAddr) {        /*w */
3480                     switch (type) {     /*s */
3481                     case HT_dumpIden_FUNCTION:
3482                         {
3483                             struct dump hostDump, diskDump;
3484
3485                             eval =
3486                                 cdbread(ut, dump_BLOCK, dbAddr, &diskDump,
3487                                         sizeof(diskDump));
3488                             if (eval)
3489                                 ERROR(eval);
3490
3491                             fprintf(dumpfid,
3492                                     "\ndumpId hash %d, entry at %u\n",
3493                                     hash, dbAddr);
3494                             fprintf(dumpfid,
3495                                     "----------------------------\n");
3496                             dump_ntoh(&diskDump, &hostDump);
3497                             printDump(dumpfid, &hostDump);
3498                             dbAddr = hostDump.idHashChain;
3499                         }
3500                         break;
3501
3502                     case HT_dumpName_FUNCTION:
3503                         {
3504                             struct dump hostDump, diskDump;
3505
3506                             eval =
3507                                 cdbread(ut, dump_BLOCK, dbAddr, &diskDump,
3508                                         sizeof(diskDump));
3509                             if (eval)
3510                                 ERROR(eval);
3511
3512                             fprintf(dumpfid,
3513                                     "\ndumpname hash %d, entry at %u\n",
3514                                     hash, dbAddr);
3515                             fprintf(dumpfid,
3516                                     "----------------------------\n");
3517                             dump_ntoh(&diskDump, &hostDump);
3518                             printDump(dumpfid, &hostDump);
3519                             dbAddr = hostDump.nameHashChain;
3520                         }
3521                         break;
3522
3523                     case HT_tapeName_FUNCTION:
3524                         {
3525                             struct tape hostTape, diskTape;
3526
3527                             eval =
3528                                 cdbread(ut, tape_BLOCK, dbAddr, &diskTape,
3529                                         sizeof(diskTape));
3530                             if (eval)
3531                                 ERROR(eval);
3532
3533                             fprintf(dumpfid,
3534                                     "\ntapename hash %d, entry at %u\n",
3535                                     hash, dbAddr);
3536                             fprintf(dumpfid,
3537                                     "----------------------------\n");
3538                             tape_ntoh(&diskTape, &hostTape);
3539                             printTape(dumpfid, &hostTape);
3540                             dbAddr = hostTape.nameHashChain;
3541                         }
3542                         break;
3543
3544                     case HT_volName_FUNCTION:
3545                         {
3546                             struct volInfo hostVolInfo, diskVolInfo;
3547
3548                             eval =
3549                                 cdbread(ut, volInfo_BLOCK, dbAddr,
3550                                         &diskVolInfo, sizeof(diskVolInfo));
3551                             if (eval)
3552                                 ERROR(eval);
3553
3554                             fprintf(dumpfid,
3555                                     "\nvolname hash %d, entry at %u\n",
3556                                     hash, dbAddr);
3557                             fprintf(dumpfid,
3558                                     "----------------------------\n");
3559                             volInfo_ntoh(&diskVolInfo, &hostVolInfo);
3560                             printVolInfo(dumpfid, &hostVolInfo);
3561                             dbAddr = hostVolInfo.nameHashChain;
3562
3563                             volFragsDump(ut, dumpfid,
3564                                          hostVolInfo.firstFragment);
3565                         }
3566                         break;
3567
3568                     default:
3569                         fprintf(dumpfid, "unknown type %d\n", type);
3570                         break;
3571
3572                     }           /*s */
3573                 }               /*w */
3574             }                   /*f */
3575         }                       /*fo */
3576     }                           /*ft */
3577
3578   error_exit:
3579     code = ubik_EndTrans(ut);   /* is this safe if no ut started ? */
3580     if (dumpfid)
3581         fclose(dumpfid);
3582     if (path)
3583         free(path);
3584     return (code);
3585 }
3586
3587 int
3588 volFragsDump(struct ubik_trans *ut, FILE *dumpfid, dbadr dbAddr)
3589 {
3590     struct volFragment hostVolFragment, diskVolFragment;
3591     afs_int32 code;
3592
3593     while (dbAddr) {
3594         code =
3595             cdbread(ut, volFragment_BLOCK, dbAddr, &diskVolFragment,
3596                     sizeof(diskVolFragment));
3597         if (code) {             /* don't be fussy about errors */
3598             fprintf(dumpfid, "volFragsDump: Error reading database\n");
3599             return (0);
3600         }
3601
3602         fprintf(dumpfid, "\nvolfragment entry at %u\n", dbAddr);
3603         fprintf(dumpfid, "----------------------------\n");
3604         volFragment_ntoh(&diskVolFragment, &hostVolFragment);
3605         printVolFragment(dumpfid, &hostVolFragment);
3606         dbAddr = hostVolFragment.sameNameChain;
3607     }
3608     return (0);
3609 }
3610
3611 int
3612 checkHash(struct ubik_trans *ut, int hashType)
3613 {
3614     struct memoryHashTable *mhtPtr;
3615     int entrySize, hashTableLength;
3616     int bucket;
3617     int old;
3618     afs_int32 code = 0;
3619
3620     mhtPtr = ht_GetType(hashType, &entrySize);
3621     if (mhtPtr == 0)
3622         ERROR(-1);
3623
3624     for (old = 0; old < 1; old++) {
3625         LogDebug(5, "\nold = %d\n", old);
3626         printMemoryHashTable(stdout, mhtPtr);
3627         LogDebug(5, "\n");
3628         hashTableLength = (old ? mhtPtr->oldLength : mhtPtr->length);
3629
3630         for (bucket = 0; bucket < hashTableLength; bucket++) {
3631             dbadr entryAddr;
3632
3633             entryAddr = ht_LookupBucket(ut, mhtPtr, bucket, old);
3634             while (entryAddr != 0) {
3635                 LogDebug(6, "bucket %d has disk addr %d\n", bucket,
3636                          entryAddr);
3637                 switch (hashType) {
3638                 case HT_dumpIden_FUNCTION:
3639                     {
3640                         struct dump diskDump, hostDump;
3641
3642                         code = dbread(ut, entryAddr, &diskDump, entrySize);
3643                         if (code)
3644                             ERROR(-1);
3645
3646                         dump_ntoh(&diskDump, &hostDump);
3647                         printDump(stdout, &hostDump);
3648                         entryAddr = hostDump.idHashChain;
3649                     }
3650                     break;
3651
3652                 case HT_dumpName_FUNCTION:
3653                     break;
3654
3655                 case HT_tapeName_FUNCTION:
3656                     break;
3657
3658                 case HT_volName_FUNCTION:
3659                     {
3660                         struct volInfo diskVolInfo, hostVolInfo;
3661
3662                         code = dbread(ut, entryAddr, &diskVolInfo, entrySize);
3663                         if (code)
3664                             ERROR(-1);
3665
3666                         volInfo_ntoh(&diskVolInfo, &hostVolInfo);
3667                         printVolInfo(stdout, &hostVolInfo);
3668                         entryAddr = hostVolInfo.nameHashChain;
3669                         break;
3670                     }
3671                 }
3672             }
3673         }
3674     }
3675   error_exit:
3676     return (code);
3677 }