2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Interface and supporting routines for the backup system's ubik database */
12 #include <afs/param.h>
14 #include <sys/types.h>
18 #elif defined(AFS_SUN5_ENV)
21 #include <sys/param.h> /* for hostnames etc */
24 #include <afs/cellconfig.h>
26 #include <afs/volser.h>
27 #include <afs/afsutil.h>
28 #include <afs/bubasics.h>
29 #include <afs/budb_client.h>
34 #include "error_macros.h"
38 /* -------------------------------------
40 * -------------------------------------
43 struct udbHandleS udbHandle;
45 /* -------------------------------------
46 * interface routines (alphabetic)
47 * -------------------------------------
51 register struct budb_volumeEntry *veptr;
55 code = ubik_Call( BUDB_AddVolume, udbHandle.uh_client, 0, veptr);
59 bcdb_AddVolumes(veptr, count)
60 register struct budb_volumeEntry *veptr;
63 struct budb_volumeList volumeList;
66 volumeList.budb_volumeList_len = count;
67 volumeList.budb_volumeList_val = veptr;
68 code = ubik_Call(BUDB_AddVolumes, udbHandle.uh_client, 0, &volumeList);
73 bcdb_CreateDump(deptr)
74 register struct budb_dumpEntry *deptr;
78 code = ubik_Call(BUDB_CreateDump, udbHandle.uh_client, 0, deptr);
82 bcdb_deleteDump(dumpID, fromTime, toTime, dumps)
86 budb_dumpsList *dumps;
89 budb_dumpsList dumpsList, *dumpsPtr;
91 dumpsList.budb_dumpsList_len = 0;
92 dumpsList.budb_dumpsList_val = 0;
93 dumpsPtr = ( dumps ? dumps : &dumpsList );
95 code = ubik_Call(BUDB_DeleteDump, udbHandle.uh_client, 0, dumpID, fromTime, toTime, dumpsPtr);
96 if (dumpsList.budb_dumpsList_val) free(dumpsList.budb_dumpsList_val);
100 bcdb_listDumps(sflags, groupId, fromTime, toTime, dumps, flags)
104 budb_dumpsList *dumps;
105 budb_dumpsList *flags;
107 afs_int32 code, sflag=0;
108 budb_dumpsList dumpsList, *dumpsPtr;
109 budb_dumpsList flagsList, *flagsPtr;
111 dumpsList.budb_dumpsList_len = 0;
112 dumpsList.budb_dumpsList_val = 0;
113 dumpsPtr = (dumps ? dumps : &dumpsList);
115 flagsList.budb_dumpsList_len = 0;
116 flagsList.budb_dumpsList_val = 0;
117 flagsPtr = (flags ? flags : &flagsList);
119 code = ubik_Call(BUDB_ListDumps, udbHandle.uh_client, 0,
120 sflags, "", groupId, fromTime, toTime, dumpsPtr, flagsPtr);
122 if (dumpsList.budb_dumpsList_val) free(dumpsList.budb_dumpsList_val);
123 if (flagsList.budb_dumpsList_val) free(flagsList.budb_dumpsList_val);
128 bcdb_DeleteVDP(dumpSetName, dumpPath, dumpID)
135 code = ubik_Call( BUDB_DeleteVDP, udbHandle.uh_client, 0, dumpSetName, dumpPath, dumpID);
140 * Returns the clone time of a volume by going up the parent chain.
141 * If no clone time is found, a clone time of 0 is returned, forcing
144 * dumpID - of the first dump to examine.
145 * volName - name of the volume for whom a clone time is required
146 * clonetime - ptr to vbl for returning result
148 * 0 - clonetime set appropriately
149 * -1 - error occured in traversing chain, clone time set to 0.
150 * -2 - no clone times found, clone time set to 0
154 bcdb_FindClone(dumpID, volName, clonetime)
157 afs_int32 *clonetime;
160 code = ubik_Call( BUDB_FindClone, udbHandle.uh_client, 0, dumpID, volName, clonetime);
165 * scan entire database for latest volume dump before adate. Optimize
166 * further by reading only the first line of the dump and if it is older
167 * than the oldest acceptable dump we've found so far, we don't bother
168 * scanning the dump file we've just opened
170 * Used by restore code when restoring a user requested volume(s)
172 * volumeName - name of volume to match on
173 * beforeDate - look for dumps older than this date
175 * deptr - desciptor of most recent dump
177 * should be able to implement this in a single call rather than
178 * the current multiple bcdb_ call algorithm.
181 bcdb_FindDump(volumeName, beforeDate, deptr)
183 afs_int32 beforeDate;
184 struct budb_dumpEntry *deptr;
187 code = ubik_Call( BUDB_FindDump, udbHandle.uh_client, 0, volumeName, beforeDate, deptr);
192 * find a dump by id. Currently insists on a single return value.
194 * dumpID - id to lookup
197 bcdb_FindDumpByID(dumpID, deptr)
199 register struct budb_dumpEntry *deptr;
201 register afs_int32 code;
206 /* initialize the dump list */
207 dl.budb_dumpList_len = 0;
208 dl.budb_dumpList_val = 0;
210 /* outline algorithm */
211 code = ubik_Call (BUDB_GetDumps, udbHandle.uh_client, 0,
223 || (dl.budb_dumpList_len != 1) /* single retn val expected */
226 /* printf("bcdb_FindDumpByID: code %d, nvalues %d\n",
227 code, dl.budb_dumpList_len); */
229 code = 1; /* multiple id's */
233 bcopy(dl.budb_dumpList_val, deptr, sizeof(*deptr));
236 if ( dl.budb_dumpList_val )
238 /* free any allocated structures */
239 free(dl.budb_dumpList_val);
244 bzero(deptr, sizeof(*deptr));
248 /* bcdb_FindLastVolClone
249 * Returns the clone time, from the most recent dump of volName, when
250 * dumped in the volume set volSetName, with dump schedule dumpName.
251 * The clone time can be used to check if the volume has been correctly
252 * re-cloned, and also is used as the time from which to do the current
255 * volSetName - name of volume set
256 * dumpName - full path of dump node
257 * volName - name of volume for whom a clone time is required
258 * clonetime - ptr to vbl for result
260 * 0 - clonetime set appropriately
262 * used only for warning generation. Suggest that this be omitted.
266 bcdb_FindLastVolClone(volSetName, dumpName, volName, clonetime)
270 afs_int32 *clonetime;
274 match on volumeset and dump path
275 search for the volume name
280 /* bcdb_FindLatestDump
281 * find the latest dump with volume set component avname and the
282 * specified dump pathname. Used to find a dump, relative to which an
283 * incremental dump can be done. Defines the parent <-> child relations
286 * avname: volume set name
287 * dumpPath: full path of dump node
289 * 0: adentry: dump entry structure filled in.
290 * -1: probably an internal error
293 * Need to store volumeset name in dump in order to implement this.
294 * Need new routine since params are two strings
297 bcdb_FindLatestDump(volSetName, dumpPath, deptr)
300 struct budb_dumpEntry *deptr;
303 code = ubik_Call( BUDB_FindLatestDump, udbHandle.uh_client, 0,
304 volSetName, dumpPath, deptr);
312 * dumpid: dump id to which tape beint32s
313 * tapeName: name of tape
316 bcdb_FindTape(dumpid, tapeName, teptr)
319 struct budb_tapeEntry *teptr;
326 bzero(teptr, sizeof(*teptr));
327 tl.budb_tapeList_len = 0;
328 tl.budb_tapeList_val = 0;
330 code = ubik_Call( BUDB_GetTapes, udbHandle.uh_client, 0,
332 BUDB_OP_TAPENAME|BUDB_OP_DUMPID,
333 tapeName, dumpid, 0, 0, &next, &dbTime, &tl);
335 if (code) ERROR(code);
337 if (tl.budb_tapeList_len != 1)
338 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
340 bcopy(tl.budb_tapeList_val, teptr, sizeof(*teptr));
343 if ( tl.budb_tapeList_val )
344 free(tl.budb_tapeList_val);
348 bcdb_FindTapeSeq(dumpid, tapeSeq, teptr)
351 struct budb_tapeEntry *teptr;
358 bzero(teptr, sizeof(*teptr));
359 tl.budb_tapeList_len = 0;
360 tl.budb_tapeList_val = 0;
362 code = ubik_Call( BUDB_GetTapes, udbHandle.uh_client, 0,
364 BUDB_OP_TAPESEQ|BUDB_OP_DUMPID,
365 "", dumpid, tapeSeq, 0, &next, &dbTime, &tl);
366 if (code) ERROR(code);
368 if (tl.budb_tapeList_len != 1)
369 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
371 bcopy(tl.budb_tapeList_val, teptr, sizeof(*teptr));
374 if ( tl.budb_tapeList_val )
375 free(tl.budb_tapeList_val);
381 * - this is part of dblookup. The existing semantics will not work since
382 * they do lookups based on dump id.
383 * - in the restore code, it uses this to extract information about
384 * the volume. Need current semantics. Could filter the output, selecting
386 * - Suggest that the lookup be based on volume name only, with optional
387 * match on backup, and readonly volumes.
388 * - Further, need to check if the volume structure returns enough
393 bcdb_FindVolumes(dumpID, volumeName, returnArray, last, next, maxa, nEntries)
396 struct budb_volumeEntry *returnArray;
406 vl.budb_volumeList_len = maxa;
407 vl.budb_volumeList_val = returnArray;
409 /* outline algorithm */
410 code = ubik_Call (BUDB_GetVolumes, udbHandle.uh_client, 0,
412 BUDB_OP_VOLUMENAME|BUDB_OP_DUMPID,
413 volumeName, /* name */
417 next, /* nextindex */
421 *nEntries = vl.budb_volumeList_len;
426 bcdb_FinishDump(deptr)
427 register struct budb_dumpEntry *deptr;
430 code = ubik_Call (BUDB_FinishDump, udbHandle.uh_client, 0, deptr);
434 bcdb_FinishTape(teptr)
435 register struct budb_tapeEntry *teptr;
438 code = ubik_Call (BUDB_FinishTape, udbHandle.uh_client, 0, teptr);
443 /* bcdb_LookupVolumes
447 bcdb_LookupVolume(volumeName, returnArray, last, next, maxa, nEntries)
449 struct budb_volumeEntry *returnArray;
459 vl.budb_volumeList_len = maxa;
460 vl.budb_volumeList_val = returnArray;
462 /* outline algorithm */
463 code = ubik_Call (BUDB_GetVolumes, udbHandle.uh_client, 0,
466 volumeName, /* name */
470 next, /* nextindex */
478 *nEntries = vl.budb_volumeList_len;
483 bcdb_UseTape(teptr, newFlag)
484 register struct budb_tapeEntry *teptr;
488 code = ubik_Call (BUDB_UseTape, udbHandle.uh_client, 0, teptr, newFlag);
493 /* ---- text configuration handling routines ----
496 * The caller should pass in/out a fid for an unlinked, open file to prevent
497 * tampering with the files contents;
501 * extract the specified textType and put it in a temporary, local
504 * ctPtr - ptr to client structure with all the required information
507 bcdb_GetTextFile(register udbClientTextP ctPtr)
509 afs_int32 bufferSize;
510 afs_int32 offset, nextOffset, chunkSize;
514 /* Initialize charlistT_val. We try to deallocate this structure based on
516 memset((void *)&charList, 0, sizeof(charList));
518 /* check params and cleanup any previous state */
519 if ( ctPtr->lockHandle == 0 )
520 ERROR(BUDB_INTERNALERROR);
522 if ( ctPtr->textStream == NULL ) /* Should have an open stream */
523 ERROR(BUDB_INTERNALERROR);
525 /* allocate a buffer */
527 charList.charListT_val = (char *) malloc(bufferSize);
528 if ( charList.charListT_val == 0 )
529 ERROR(BUDB_INTERNALERROR);
530 charList.charListT_len = bufferSize;
535 while ( nextOffset != -1 )
538 charList.charListT_len = bufferSize;
539 code = ubik_Call(BUDB_GetText, udbHandle.uh_client, 0,
540 ctPtr->lockHandle, ctPtr->textType, bufferSize,
541 offset, &nextOffset, &charList);
546 code = fwrite(charList.charListT_val, sizeof(char),
547 charList.charListT_len, ctPtr->textStream);
548 if ( ferror(ctPtr->textStream) )
549 ERROR(BUDB_INTERNALERROR);
551 ctPtr->textSize += charList.charListT_len;
554 /* get text version */
555 code = ubik_Call(BUDB_GetTextVersion, udbHandle.uh_client, 0,
556 ctPtr->textType, &ctPtr->textVersion);
561 fflush(ctPtr->textStream); /* debug */
563 /* exit, leaving the configuration text file open */
564 if ( charList.charListT_val )
565 free(charList.charListT_val);
569 if ( ctPtr->textStream != NULL )
571 fclose(ctPtr->textStream);
572 ctPtr->textStream = NULL;
579 * save the text file in ubik database
581 * textType - identifies type of configuration file
582 * filename - where to get the text from
585 bcdb_SaveTextFile(ctPtr)
586 udbClientTextP ctPtr;
588 afs_int32 bufferSize;
589 afs_int32 offset, chunkSize, fileSize;
592 afs_int32 filesize();
594 /* allocate a buffer */
596 charList.charListT_val = (char *) malloc(bufferSize);
597 if ( charList.charListT_val == 0 )
598 ERROR(BUDB_INTERNALERROR);
599 charList.charListT_len = bufferSize;
601 if ( ctPtr->textStream == NULL )
602 ERROR(BUDB_INTERNALERROR);
603 rewind(ctPtr->textStream);
605 fileSize = (afs_int32) filesize(ctPtr->textStream);
607 dprintf(("filesize is %d\n", fileSize));
609 rewind(ctPtr->textStream);
611 /* special case empty files */
614 charList.charListT_len = 0;
615 code = ubik_Call(BUDB_SaveText, udbHandle.uh_client, 0,
616 ctPtr->lockHandle, ctPtr->textType, 0,
617 BUDB_TEXT_COMPLETE, &charList);
622 while ( fileSize != 0 )
624 chunkSize = MIN(fileSize, bufferSize);
625 code = fread(charList.charListT_val, sizeof(char), chunkSize,
628 if ( code != chunkSize )
629 printf("code = %d\n", code );
630 if ( ferror(ctPtr->textStream ) )
631 ERROR(BUDB_INTERNALERROR);
633 charList.charListT_len = chunkSize;
634 code = ubik_Call(BUDB_SaveText, udbHandle.uh_client, 0,
635 ctPtr->lockHandle, ctPtr->textType, offset,
636 (chunkSize == fileSize) ? BUDB_TEXT_COMPLETE: 0,
641 fileSize -= chunkSize;
646 /* if ( ctPtr->textStream >= 0 )
647 close(ctPtr->textStream); */
648 if ( charList.charListT_val )
649 free(charList.charListT_val);
653 bcdb_FindLastTape(dumpID, dumpEntry, tapeEntry, volEntry)
655 struct budb_dumpEntry *dumpEntry;
656 struct budb_tapeEntry *tapeEntry;
657 struct budb_volumeEntry *volEntry;
659 return( ubik_Call(BUDB_FindLastTape,udbHandle.uh_client,0,
660 dumpID,dumpEntry,tapeEntry,volEntry) );
663 bcdb_MakeDumpAppended(appendedDumpID, initialDumpID, startTapeSeq)
664 afs_int32 appendedDumpID;
665 afs_int32 initialDumpID;
666 afs_int32 startTapeSeq;
668 return( ubik_Call(BUDB_MakeDumpAppended,udbHandle.uh_client,0,
669 appendedDumpID,initialDumpID,startTapeSeq) );
673 /* -------------------------------------
674 * misc. support routines
675 * -------------------------------------
685 offset = ftell(stream);
686 fseek(stream, (afs_int32) 0, 2); /* end of file */
687 size = ftell(stream);
688 fseek(stream, offset, 0);
693 /* ------------------------------------
694 * misc. support routines - general text management
695 * ------------------------------------
700 * locks the text described by the ctPtr
702 * ctptr - client text ptr
709 register udbClientTextP ctPtr;
712 afs_int32 timeout, j = 0;
714 if (ctPtr->lockHandle != 0) return(1); /* already locked */
716 timeout = ( (ctPtr->textSize == 0) ? 30 : ((ctPtr->textSize/50000) + 10) );
719 code = ubik_Call(BUDB_GetLock, udbHandle.uh_client, 0,
720 udbHandle.uh_instanceId, ctPtr->textType, timeout,
722 if ( (code != BUDB_LOCKED) && (code != BUDB_SELFLOCKED) ) {
726 /* Mention something every 30 seconds */
728 com_err(whoami, code,"; Waiting for db configuration text unlock");
731 #ifdef AFS_PTHREAD_ENV
739 if (code) ctPtr->lockHandle = 0;
744 * unlocks the text described by the ctPtr
746 * ctptr - client text ptr
753 register udbClientTextP ctPtr;
757 if ( ctPtr->lockHandle == 0 )
760 code = ubik_Call(BUDB_FreeLock, udbHandle.uh_client, 0, ctPtr->lockHandle);
761 ctPtr->lockHandle = 0;
763 /* Don't try to analyse the error. Let the lock timeout */
767 /* bc_CheckTextVersion
770 * n - out of date or error
773 bc_CheckTextVersion(ctPtr)
774 register udbClientTextP ctPtr;
779 if ( ctPtr->textVersion == -1 )
780 return(BC_VERSIONMISMATCH);
782 code = ubik_Call(BUDB_GetTextVersion, udbHandle.uh_client, 0,
783 ctPtr->textType, &tversion);
786 if ( tversion != ctPtr->textVersion )
787 return(BC_VERSIONMISMATCH);
791 /* -------------------------------------
792 * initialization routines
793 * -------------------------------------
797 * Initialize a client for the vl ubik database.
799 vldbClientInit(noAuthFlag, localauth, cellName, cstruct, ttoken)
803 struct ubik_client **cstruct;
804 struct ktc_token *ttoken;
807 struct afsconf_dir *acdir;
808 struct rc_securityClass *sc;
809 afs_int32 i, scIndex = 0; /* Index of Rx security object - noauth */
810 struct afsconf_cell info;
811 struct ktc_principal sname;
812 struct rx_connection *serverconns[VLDB_MAXSERVERS];
815 /* Find out about the given cell */
816 acdir = afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
817 AFSDIR_CLIENT_ETC_DIRPATH));
820 com_err(whoami,0,"Can't open configuration directory '%s'",
821 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
822 AFSDIR_CLIENT_ETC_DIRPATH));
823 ERROR(BC_NOCELLCONFIG);
830 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
833 com_err(whoami, code, "; Can't get the local cell name - check %s/%s", (localauth ? AFSDIR_SERVER_ETC_DIRPATH : AFSDIR_CLIENT_ETC_DIRPATH),
834 AFSDIR_THISCELL_FILE);
837 strcpy(cellName, cname);
840 code = afsconf_GetCellInfo(acdir, cellName, AFSCONF_VLDBSERVICE, &info);
843 com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
844 cellName, (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
845 AFSDIR_CLIENT_ETC_DIRPATH),
846 AFSDIR_CELLSERVDB_FILE);
847 ERROR(BC_NOCELLCONFIG);
851 * Grab tickets if we care about authentication.
856 code = afsconf_GetLatestKey (acdir, 0, 0);
859 com_err(whoami, code, "; Can't get key from local key file");
864 code = afsconf_ClientAuth(acdir, &sc, &scIndex);
867 com_err(whoami, code, "; Calling ClientAuth");
871 ttoken->endTime = NEVERDATE;
878 strcpy(sname.cell, info.name);
879 sname.instance[0] = 0;
880 strcpy(sname.name, "afs");
882 code = ktc_GetToken(&sname, ttoken, sizeof(struct ktc_token), (char *)0);
885 com_err(whoami,code, 0,"; Can't get AFS tokens - running unauthenticated");
889 if ( (ttoken->kvno < 0) || (ttoken->kvno > 255) )
890 com_err(whoami, 0, "Funny kvno (%d) in ticket, proceeding", ttoken->kvno);
899 sc = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
902 sc = (struct rx_securityClass *)
903 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken->sessionKey,
904 ttoken->kvno, ttoken->ticketLen,
908 com_err(whoami, 0, "Unsupported authentication type %d", scIndex);
916 com_err(whoami, 0, "Can't create a security object with security index %d",
921 /* tell UV module about default authentication */
922 UV_SetSecurity(sc, scIndex);
924 if (info.numServers > VLDB_MAXSERVERS)
927 "Warning: %d VLDB servers exist for cell '%s', can only remember the first %d",
928 info.numServers, cellName, VLDB_MAXSERVERS);
929 info.numServers = VLDB_MAXSERVERS;
932 for (i=0; i<info.numServers; i++)
933 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
934 info.hostAddr[i].sin_port,
935 USER_SERVICE_ID, sc, scIndex);
939 code = ubik_ClientInit(serverconns, cstruct);
942 com_err(whoami,code,"; Can't initialize ubik connection to vldb");
947 if (acdir) afsconf_Close(acdir);
952 * initialize a client for the backup systems ubik database.
956 udbClientInit(noAuthFlag, localauth, cellName)
961 afs_int32 serverList[MAXSERVERS];
963 struct ktc_principal principal;
964 struct ktc_token token;
965 struct afsconf_cell info;
966 struct afsconf_dir *acdir;
970 acdir = afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
971 AFSDIR_CLIENT_ETC_DIRPATH));
974 com_err(whoami,0,"Can't open configuration directory '%s'",
975 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
976 AFSDIR_CLIENT_ETC_DIRPATH));
977 ERROR(BC_NOCELLCONFIG);
984 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
987 com_err(whoami, code, "; Can't get the local cell name - check %s/%s", (localauth ? AFSDIR_SERVER_ETC_DIRPATH : AFSDIR_CLIENT_ETC_DIRPATH),
988 AFSDIR_THISCELL_FILE);
991 strcpy(cellName, cname);
994 code = afsconf_GetCellInfo(acdir, cellName, 0, &info);
997 com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
998 cellName, (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
999 AFSDIR_CLIENT_ETC_DIRPATH),
1000 AFSDIR_CELLSERVDB_FILE);
1001 ERROR(BC_NOCELLCONFIG);
1004 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
1008 code = afsconf_GetLatestKey (acdir, 0, 0);
1011 com_err(whoami, code, "; Can't get key from local key file");
1016 code = afsconf_ClientAuth(acdir, &udbHandle.uh_secobj, &udbHandle.uh_scIndex);
1019 com_err(whoami, code, "; Calling ClientAuth");
1028 /* setup principal */
1029 strcpy(principal.cell, info.name);
1030 principal.instance[0] = 0;
1031 strcpy(principal.name, "afs");
1034 code = ktc_GetToken(&principal, &token, sizeof(token), (char *)0);
1037 com_err(whoami, code, "; Can't get tokens - running unauthenticated");
1041 if ((token.kvno < 0) || (token.kvno > 255))
1042 com_err(whoami, 0, "Unexpected kvno (%d) in ticket - proceeding",
1044 udbHandle.uh_scIndex = RX_SCINDEX_KAD; /* Kerberos */
1048 switch (udbHandle.uh_scIndex)
1051 udbHandle.uh_secobj = (struct rx_securityClass *) rxnull_NewClientSecurityObject();
1055 udbHandle.uh_secobj = (struct rx_securityClass *)
1056 rxkad_NewClientSecurityObject(rxkad_clear, &token.sessionKey,
1057 token.kvno, token.ticketLen,
1062 com_err(whoami, 0, "Unsupported authentication type %d", udbHandle.uh_scIndex );
1068 if (!udbHandle.uh_secobj)
1070 com_err(whoami, 0, "Can't create a security object with security index %d",
1071 udbHandle.uh_secobj);
1075 if (info.numServers > MAXSERVERS)
1078 "Warning: %d BDB servers exist for cell '%s', can only remember the first %d",
1079 info.numServers, cellName, MAXSERVERS);
1080 info.numServers = MAXSERVERS;
1083 /* establish connections to the servers. Check for failed connections? */
1084 for (i = 0; i < info.numServers; i++) {
1085 udbHandle.uh_serverConn[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
1086 htons(AFSCONF_BUDBPORT),
1088 udbHandle.uh_secobj,
1089 udbHandle.uh_scIndex);
1091 udbHandle.uh_serverConn[i] = 0;
1093 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1095 com_err(whoami,code,"; Can't initialize ubik connection to backup database");
1099 /* Try to quickly find a good site by setting deadtime low */
1100 for (i = 0; i < info.numServers; i++)
1101 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 1);
1102 code = ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0, &udbHandle.uh_instanceId);
1104 /* Reset dead time back up to default */
1105 for (i = 0; i < info.numServers; i++)
1106 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 60);
1108 /* If did not find a site on first quick pass, try again */
1110 code = ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0, &udbHandle.uh_instanceId);
1112 com_err(whoami,code,"; Can't access backup database");
1117 if (acdir) afsconf_Close(acdir);
1121 /* -------------------------------------
1122 * specialized ubik support
1123 * -------------------------------------
1131 * 1) first call with SINGLESERVER set, record the server to be used.
1132 * 2) subsequent calls use that server. If a failure is encountered,
1133 * the state is cleaned up and the error returned back to the caller.
1134 * 3) upon completion, the user must make a dummy call with
1135 * END_SINGLESERVER set, to clean up state.
1136 * 4) if the vanilla ubik_Call is ever modified, the END_SINGLESERVER
1137 * flag can be discarded. The first call without SINGLESERVER set
1138 * can clean up the state.
1141 struct ubikCallState
1143 afs_int32 ucs_flags; /* state flags */
1144 afs_int32 ucs_selectedServer; /* which server selected */
1147 static struct ubikCallState uServer;
1149 /* ubik_Call_SingleServer
1150 * variant of ubik_Call. This is used by the backup system to initiate
1151 * a series of calls to a single ubik server. The first call sets up
1152 * process state (not recorded in ubik) that requires all further calls
1153 * of that group to be made to the same server process.
1155 * call this instead of stub and we'll guarantee to find a host that's up.
1156 * in the future, we should also put in a protocol to find the sync site
1160 ubik_Call_SingleServer(aproc, aclient, aflags, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16)
1161 register struct ubik_client *aclient;
1164 char *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10, *p11, *p12, *p13, *p14, *p15, *p16;
1166 register afs_int32 code;
1167 afs_int32 someCode, newHost, thisHost;
1168 register afs_int32 i;
1169 register afs_int32 count;
1172 struct rx_connection *tc;
1173 struct rx_peer *rxp;
1175 if ( (aflags & (UF_SINGLESERVER | UF_END_SINGLESERVER)) != 0 )
1177 if ( ((aflags & UF_SINGLESERVER) != 0 )
1178 && ((uServer.ucs_flags & UF_SINGLESERVER ) != 0)
1182 /* have a selected server */
1183 tc = aclient->conns[uServer.ucs_selectedServer];
1185 code = (*aproc)(tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
1186 p12, p13, p14, p15, p16);
1189 /* error. Clean up single server state */
1190 bzero(&uServer, sizeof(uServer));
1195 if ( (aflags & UF_END_SINGLESERVER) != 0 )
1197 bzero(&uServer, sizeof(uServer));
1202 someCode = UNOSERVERS;
1209 /* tc is the next conn to try */
1210 tc = aclient->conns[count];
1214 pass = 1; /* in pass 1, we look at down hosts, too */
1218 else break; /* nothing left to try */
1220 if (pass == 0 && (aclient->states[count] & CFLastFailed)) {
1222 continue; /* this guy's down, try someone else first */
1225 code = (*aproc)(tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16);
1227 /* note that getting a UNOTSYNC error code back does *not* guarantee
1228 * that there is a sync site yet elected. However, if there is a
1229 * sync site out there somewhere, and you're trying an operation that
1230 * requires a sync site, ubik will return UNOTSYNC, indicating the
1231 * operation won't work until you find a sync site
1233 if (code == UNOTSYNC)
1235 /* means that this requires a sync site to work */
1236 someCode = code; /* remember an error, if this fails */
1238 /* now see if we can find the sync site host */
1239 code = VOTE_GetSyncSite(tc, &newHost);
1240 if (code == 0 && newHost != 0) {
1241 newHost = htonl(newHost); /* convert back to network order */
1243 /* position count at the appropriate slot in the client
1244 * structure and retry. If we can't find in slot, we'll just
1245 * continue through the whole list
1247 for(i=0;i<MAXSERVERS;i++)
1249 rxp = rx_PeerOf(aclient->conns[i]);
1250 if (!(thisHost = rx_HostOf(rxp))) {
1251 count++; /* host not found, try the next dude */
1254 if (thisHost == newHost)
1256 /* avoid asking in a loop */
1257 if (chaseCount++ > 2)
1259 count = i; /* we were told to use this one */
1264 else count++; /* not directed, keep looking for a sync site */
1267 else if (code == UNOQUORUM) { /* this guy is still recovering */
1272 else if (code < 0) { /* network errors */
1274 aclient->states[count] |= CFLastFailed;
1280 /* ok, operation worked */
1281 aclient->states[count] &= ~CFLastFailed;
1282 /* either misc ubik code, or misc application code (incl success)
1285 /* if the call succeeded, setup connection state for subsequent
1289 && (aflags & UF_SINGLESERVER != 0)
1292 /* need to save state */
1293 uServer.ucs_flags = UF_SINGLESERVER;
1294 uServer.ucs_selectedServer = count;
1304 /* -------------------------------------
1305 * debug and test routines
1306 * -------------------------------------
1310 * For testing only. Open a connect to the database server running on
1313 * 0 - ubik connection established in the global udbHandle structure
1319 afs_int32 serverList[MAXSERVERS];
1325 /* get our host name */
1326 gethostname(hostname, sizeof(hostname));
1327 /* strcpy(hostname, "hops"); */
1330 args[1] = "-servers";
1333 code = ubik_ParseClientList(3, args, serverList);
1336 com_err(whoami, code, "; udbLocalInit: parsing ubik server list");
1340 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
1341 udbHandle.uh_secobj = (struct rx_securityClass *)
1342 rxnull_NewClientSecurityObject();
1344 for ( i = 0; serverList[i] != 0; i++ )
1346 udbHandle.uh_serverConn[i] = rx_NewConnection (serverList[i],
1347 htons(AFSCONF_BUDBPORT),
1349 udbHandle.uh_secobj,
1350 udbHandle.uh_scIndex);
1351 if ( udbHandle.uh_serverConn[i] == 0 )
1353 com_err(whoami, 0, "connection %d failed", i);
1357 udbHandle.uh_serverConn[i] = 0;
1358 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1361 com_err(whoami, code, "; in ubik_ClientInit");
1365 code = ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0, &udbHandle.uh_instanceId);
1368 com_err(whoami, code, "; Can't estblish instance Id");
1376 /* bc_openTextFile - This function opens a temp file to read in the
1377 * config text recd from the bu server. On Unix, an unlink() is done on
1378 * the file as soon as it is opened, so when the program exits, the file will
1379 * be removed automatically, while being invisible while in use.
1380 * On NT, however, the file must be explicitly deleted after use with an unlink()
1382 * Pointer to a udhClientTextP struct. The open stream ptr is stored in
1383 * the udbClientTextP.textStream member.
1384 * Output: The temp file name is returned in tmpFileName. This should be used
1385 * to delete the file when done with it.
1390 int bc_openTextFile(udbClientTextP ctPtr, char *tmpFileName)
1394 if ( ctPtr->textStream != NULL )
1395 fclose(ctPtr->textStream);
1397 sprintf(tmpFileName, "%s/bu_XXXXXX", gettmpdir());
1398 mktemp(tmpFileName);
1399 ctPtr->textStream = fopen(tmpFileName, "w+");
1400 if ( ctPtr->textStream == NULL )
1401 ERROR(BUDB_INTERNALERROR);
1403 #ifndef AFS_NT40_ENV /* This can't be done on NT */
1404 /* make the file invisible to others */
1405 code = unlink(tmpFileName);
1410 dprintf(("file is %s\n", tmpFileName));
1416 if ( ctPtr->textStream != NULL )
1418 fclose(ctPtr->textStream);
1419 ctPtr->textStream = NULL;
1425 /* bc_closeTextFile: This function closes any actual temp files associated with
1426 * a udbClientText structure.
1427 * Input: ctPtr->textStream - stream to close
1428 * tmpFileName - temp file name to delete
1433 int bc_closeTextFile(udbClientTextP ctPtr, char *tmpFileName)
1437 if (ctPtr->textStream)
1438 fclose(ctPtr->textStream);
1440 if (*tmpFileName) { /* we have a valid name */
1441 code = unlink(tmpFileName);