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 <afsconfig.h>
17 #ifdef IGNORE_SOME_GCC_WARNINGS
18 # pragma GCC diagnostic warning "-Wstrict-prototypes"
22 #include <afs/cellconfig.h>
24 #include <afs/volser.h>
25 #include <afs/volser_prototypes.h>
26 #include <afs/afsutil.h>
27 #include <afs/bubasics.h>
28 #include <afs/budb_client.h>
30 #include <afs/com_err.h>
33 #include "error_macros.h"
34 #include "bucoord_internal.h"
35 #include "bucoord_prototypes.h"
39 /* -------------------------------------
41 * -------------------------------------
44 struct udbHandleS udbHandle;
46 /* -------------------------------------
47 * interface routines (alphabetic)
48 * -------------------------------------
51 afs_int32 bcdb_AddVolume(struct budb_volumeEntry *veptr)
55 code = ubik_BUDB_AddVolume(udbHandle.uh_client, 0, veptr);
59 afs_int32 bcdb_AddVolumes(struct budb_volumeEntry *veptr, afs_int32 count)
61 struct budb_volumeList volumeList;
64 volumeList.budb_volumeList_len = count;
65 volumeList.budb_volumeList_val = veptr;
66 code = ubik_BUDB_AddVolumes(udbHandle.uh_client, 0, &volumeList);
71 afs_int32 bcdb_CreateDump(struct budb_dumpEntry *deptr)
75 code = ubik_BUDB_CreateDump(udbHandle.uh_client, 0, deptr);
79 afs_int32 bcdb_deleteDump(afs_int32 dumpID, afs_int32 fromTime, afs_int32 toTime,
80 budb_dumpsList *dumps)
83 budb_dumpsList dumpsList, *dumpsPtr;
85 dumpsList.budb_dumpsList_len = 0;
86 dumpsList.budb_dumpsList_val = 0;
87 dumpsPtr = (dumps ? dumps : &dumpsList);
90 ubik_BUDB_DeleteDump(udbHandle.uh_client, 0, dumpID, fromTime,
92 if (dumpsList.budb_dumpsList_val)
93 free(dumpsList.budb_dumpsList_val);
97 afs_int32 bcdb_listDumps (afs_int32 sflags, afs_int32 groupId,
98 afs_int32 fromTime, afs_int32 toTime,
99 budb_dumpsList *dumps, budb_dumpsList *flags)
102 budb_dumpsList dumpsList, *dumpsPtr;
103 budb_dumpsList flagsList, *flagsPtr;
105 dumpsList.budb_dumpsList_len = 0;
106 dumpsList.budb_dumpsList_val = 0;
107 dumpsPtr = (dumps ? dumps : &dumpsList);
109 flagsList.budb_dumpsList_len = 0;
110 flagsList.budb_dumpsList_val = 0;
111 flagsPtr = (flags ? flags : &flagsList);
114 ubik_BUDB_ListDumps(udbHandle.uh_client, 0, sflags, "", groupId,
115 fromTime, toTime, dumpsPtr, flagsPtr);
117 if (dumpsList.budb_dumpsList_val)
118 free(dumpsList.budb_dumpsList_val);
119 if (flagsList.budb_dumpsList_val)
120 free(flagsList.budb_dumpsList_val);
125 afs_int32 bcdb_DeleteVDP(char *dumpSetName, char *dumpPath, afs_int32 dumpID)
130 ubik_BUDB_DeleteVDP(udbHandle.uh_client, 0, dumpSetName,
136 * Returns the clone time of a volume by going up the parent chain.
137 * If no clone time is found, a clone time of 0 is returned, forcing
140 * dumpID - of the first dump to examine.
141 * volName - name of the volume for whom a clone time is required
142 * clonetime - ptr to vbl for returning result
144 * 0 - clonetime set appropriately
145 * -1 - error occured in traversing chain, clone time set to 0.
146 * -2 - no clone times found, clone time set to 0
149 afs_int32 bcdb_FindClone(afs_int32 dumpID, char *volName, afs_int32 *clonetime)
153 ubik_BUDB_FindClone(udbHandle.uh_client, 0, dumpID, volName,
159 * scan entire database for latest volume dump before adate. Optimize
160 * further by reading only the first line of the dump and if it is older
161 * than the oldest acceptable dump we've found so far, we don't bother
162 * scanning the dump file we've just opened
164 * Used by restore code when restoring a user requested volume(s)
166 * volumeName - name of volume to match on
167 * beforeDate - look for dumps older than this date
169 * deptr - desciptor of most recent dump
171 * should be able to implement this in a single call rather than
172 * the current multiple bcdb_ call algorithm.
176 bcdb_FindDump(char *volumeName, afs_int32 beforeDate,
177 struct budb_dumpEntry *deptr)
181 ubik_BUDB_FindDump(udbHandle.uh_client, 0, volumeName,
187 * find a dump by id. Currently insists on a single return value.
189 * dumpID - id to lookup
193 bcdb_FindDumpByID(afs_int32 dumpID, struct budb_dumpEntry *deptr)
200 /* initialize the dump list */
201 dl.budb_dumpList_len = 0;
202 dl.budb_dumpList_val = 0;
204 /* outline algorithm */
205 code = ubik_BUDB_GetDumps(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_DUMPID, "", /* no name */
209 &nextindex, &dbTime, &dl);
212 || (dl.budb_dumpList_len != 1) /* single retn val expected */
214 /* printf("bcdb_FindDumpByID: code %d, nvalues %d\n",
215 code, dl.budb_dumpList_len); */
217 code = 1; /* multiple id's */
221 memcpy(deptr, dl.budb_dumpList_val, sizeof(*deptr));
224 if (dl.budb_dumpList_val) {
225 /* free any allocated structures */
226 free(dl.budb_dumpList_val);
231 memset(deptr, 0, sizeof(*deptr));
235 /* bcdb_FindLastVolClone
236 * Returns the clone time, from the most recent dump of volName, when
237 * dumped in the volume set volSetName, with dump schedule dumpName.
238 * The clone time can be used to check if the volume has been correctly
239 * re-cloned, and also is used as the time from which to do the current
242 * volSetName - name of volume set
243 * dumpName - full path of dump node
244 * volName - name of volume for whom a clone time is required
245 * clonetime - ptr to vbl for result
247 * 0 - clonetime set appropriately
249 * used only for warning generation. Suggest that this be omitted.
253 bcdb_FindLastVolClone(char *volSetName, char *dumpName, char *volName,
254 afs_int32 *clonetime)
258 * match on volumeset and dump path
259 * search for the volume name
264 /* bcdb_FindLatestDump
265 * find the latest dump with volume set component avname and the
266 * specified dump pathname. Used to find a dump, relative to which an
267 * incremental dump can be done. Defines the parent <-> child relations
270 * avname: volume set name
271 * dumpPath: full path of dump node
273 * 0: adentry: dump entry structure filled in.
274 * -1: probably an internal error
277 * Need to store volumeset name in dump in order to implement this.
278 * Need new routine since params are two strings
282 bcdb_FindLatestDump(char *volSetName, char *dumpPath,
283 struct budb_dumpEntry *deptr)
287 ubik_BUDB_FindLatestDump(udbHandle.uh_client, 0, volSetName,
296 * dumpid: dump id to which tape beint32s
297 * tapeName: name of tape
301 bcdb_FindTape(afs_int32 dumpid, char *tapeName,
302 struct budb_tapeEntry *teptr)
309 memset(teptr, 0, sizeof(*teptr));
310 tl.budb_tapeList_len = 0;
311 tl.budb_tapeList_val = 0;
314 ubik_BUDB_GetTapes(udbHandle.uh_client, 0, BUDB_MAJORVERSION,
315 BUDB_OP_TAPENAME | BUDB_OP_DUMPID, tapeName, dumpid, 0, 0,
316 &next, &dbTime, &tl);
321 if (tl.budb_tapeList_len != 1)
322 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
324 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
327 if (tl.budb_tapeList_val)
328 free(tl.budb_tapeList_val);
333 bcdb_FindTapeSeq(afs_int32 dumpid, afs_int32 tapeSeq,
334 struct budb_tapeEntry *teptr)
341 memset(teptr, 0, sizeof(*teptr));
342 tl.budb_tapeList_len = 0;
343 tl.budb_tapeList_val = 0;
346 ubik_BUDB_GetTapes(udbHandle.uh_client, 0, BUDB_MAJORVERSION,
347 BUDB_OP_TAPESEQ | BUDB_OP_DUMPID, "", dumpid, tapeSeq, 0,
348 &next, &dbTime, &tl);
352 if (tl.budb_tapeList_len != 1)
353 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
355 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
358 if (tl.budb_tapeList_val)
359 free(tl.budb_tapeList_val);
365 * - this is part of dblookup. The existing semantics will not work since
366 * they do lookups based on dump id.
367 * - in the restore code, it uses this to extract information about
368 * the volume. Need current semantics. Could filter the output, selecting
370 * - Suggest that the lookup be based on volume name only, with optional
371 * match on backup, and readonly volumes.
372 * - Further, need to check if the volume structure returns enough
377 bcdb_FindVolumes(afs_int32 dumpID, char *volumeName,
378 struct budb_volumeEntry *returnArray,
379 afs_int32 last, afs_int32 *next, afs_int32 maxa,
386 vl.budb_volumeList_len = maxa;
387 vl.budb_volumeList_val = returnArray;
389 /* outline algorithm */
390 code = ubik_BUDB_GetVolumes(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME | BUDB_OP_DUMPID, volumeName, /* name */
394 next, /* nextindex */
397 *nEntries = vl.budb_volumeList_len;
402 bcdb_FinishDump(struct budb_dumpEntry *deptr)
405 code = ubik_BUDB_FinishDump(udbHandle.uh_client, 0, deptr);
410 bcdb_FinishTape(struct budb_tapeEntry *teptr)
413 code = ubik_BUDB_FinishTape(udbHandle.uh_client, 0, teptr);
418 /* bcdb_LookupVolumes
422 bcdb_LookupVolume(char *volumeName, struct budb_volumeEntry *returnArray,
423 afs_int32 last, afs_int32 *next, afs_int32 maxa,
430 vl.budb_volumeList_len = maxa;
431 vl.budb_volumeList_val = returnArray;
433 /* outline algorithm */
434 code = ubik_BUDB_GetVolumes(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME, volumeName, /* name */
438 next, /* nextindex */
444 *nEntries = vl.budb_volumeList_len;
449 bcdb_UseTape(struct budb_tapeEntry *teptr, afs_int32 *newFlag)
452 code = ubik_BUDB_UseTape(udbHandle.uh_client, 0, teptr, newFlag);
457 /* ---- text configuration handling routines ----
460 * The caller should pass in/out a fid for an unlinked, open file to prevent
461 * tampering with the files contents;
465 * extract the specified textType and put it in a temporary, local
468 * ctPtr - ptr to client structure with all the required information
472 bcdb_GetTextFile(udbClientTextP ctPtr)
474 afs_int32 bufferSize;
475 afs_int32 offset, nextOffset;
479 /* Initialize charlistT_val. We try to deallocate this structure based on
481 memset((void *)&charList, 0, sizeof(charList));
483 /* check params and cleanup any previous state */
484 if (ctPtr->lockHandle == 0)
485 ERROR(BUDB_INTERNALERROR);
487 if (ctPtr->textStream == NULL) /* Should have an open stream */
488 ERROR(BUDB_INTERNALERROR);
490 /* allocate a buffer */
492 charList.charListT_val = (char *)malloc(bufferSize);
493 if (charList.charListT_val == 0)
494 ERROR(BUDB_INTERNALERROR);
495 charList.charListT_len = bufferSize;
500 while (nextOffset != -1) {
502 charList.charListT_len = bufferSize;
504 ubik_BUDB_GetText(udbHandle.uh_client, 0, ctPtr->lockHandle,
505 ctPtr->textType, bufferSize, offset, &nextOffset,
512 fwrite(charList.charListT_val, sizeof(char),
513 charList.charListT_len, ctPtr->textStream);
514 if (ferror(ctPtr->textStream))
515 ERROR(BUDB_INTERNALERROR);
517 ctPtr->textSize += charList.charListT_len;
520 /* get text version */
522 ubik_BUDB_GetTextVersion(udbHandle.uh_client, 0,
523 ctPtr->textType, &ctPtr->textVersion);
528 fflush(ctPtr->textStream); /* debug */
530 /* exit, leaving the configuration text file open */
531 if (charList.charListT_val)
532 free(charList.charListT_val);
536 if (ctPtr->textStream != NULL) {
537 fclose(ctPtr->textStream);
538 ctPtr->textStream = NULL;
545 * save the text file in ubik database
547 * textType - identifies type of configuration file
548 * filename - where to get the text from
552 bcdb_SaveTextFile(udbClientTextP ctPtr)
554 afs_int32 bufferSize;
555 afs_int32 offset, chunkSize, fileSize;
559 /* allocate a buffer */
561 charList.charListT_val = (char *)malloc(bufferSize);
562 if (charList.charListT_val == 0)
563 ERROR(BUDB_INTERNALERROR);
564 charList.charListT_len = bufferSize;
566 if (ctPtr->textStream == NULL)
567 ERROR(BUDB_INTERNALERROR);
568 rewind(ctPtr->textStream);
570 fileSize = (afs_int32) filesize(ctPtr->textStream);
572 afs_dprintf(("filesize is %d\n", fileSize));
574 rewind(ctPtr->textStream);
576 /* special case empty files */
578 charList.charListT_len = 0;
580 ubik_BUDB_SaveText(udbHandle.uh_client, 0,
581 ctPtr->lockHandle, ctPtr->textType, 0,
582 BUDB_TEXT_COMPLETE, &charList);
587 while (fileSize != 0) {
588 chunkSize = MIN(fileSize, bufferSize);
590 fread(charList.charListT_val, sizeof(char), chunkSize,
593 if (code != chunkSize)
594 printf("code = %d\n", code);
595 if (ferror(ctPtr->textStream))
596 ERROR(BUDB_INTERNALERROR);
598 charList.charListT_len = chunkSize;
600 ubik_BUDB_SaveText(udbHandle.uh_client, 0,
601 ctPtr->lockHandle, ctPtr->textType, offset,
602 (chunkSize == fileSize) ? BUDB_TEXT_COMPLETE : 0,
607 fileSize -= chunkSize;
612 /* if ( ctPtr->textStream >= 0 )
613 * close(ctPtr->textStream); */
614 if (charList.charListT_val)
615 free(charList.charListT_val);
620 bcdb_FindLastTape(afs_int32 dumpID, struct budb_dumpEntry *dumpEntry,
621 struct budb_tapeEntry *tapeEntry,
622 struct budb_volumeEntry *volEntry)
624 return (ubik_BUDB_FindLastTape(udbHandle.uh_client, 0, dumpID, dumpEntry,
625 tapeEntry, volEntry));
629 bcdb_MakeDumpAppended(afs_int32 appendedDumpID, afs_int32 initialDumpID,
630 afs_int32 startTapeSeq)
632 return (ubik_BUDB_MakeDumpAppended(udbHandle.uh_client, 0, appendedDumpID,
633 initialDumpID, startTapeSeq));
637 /* -------------------------------------
638 * misc. support routines
639 * -------------------------------------
643 filesize(FILE *stream)
648 offset = ftell(stream);
649 fseek(stream, (afs_int32) 0, 2); /* end of file */
650 size = ftell(stream);
651 fseek(stream, offset, 0);
656 /* ------------------------------------
657 * misc. support routines - general text management
658 * ------------------------------------
663 * locks the text described by the ctPtr
665 * ctptr - client text ptr
672 bc_LockText(udbClientTextP ctPtr)
675 afs_int32 timeout, j = 0;
677 if (ctPtr->lockHandle != 0)
678 return (1); /* already locked */
681 ((ctPtr->textSize == 0) ? 30 : ((ctPtr->textSize / 50000) + 10));
685 ubik_BUDB_GetLock(udbHandle.uh_client, 0,
686 udbHandle.uh_instanceId, ctPtr->textType, timeout,
688 if ((code != BUDB_LOCKED) && (code != BUDB_SELFLOCKED)) {
692 /* Mention something every 30 seconds */
694 afs_com_err(whoami, code,
695 "; Waiting for db configuration text unlock");
698 #ifdef AFS_PTHREAD_ENV
707 ctPtr->lockHandle = 0;
712 * unlocks the text described by the ctPtr
714 * ctptr - client text ptr
721 bc_UnlockText(udbClientTextP ctPtr)
725 if (ctPtr->lockHandle == 0)
729 ubik_BUDB_FreeLock(udbHandle.uh_client, 0, ctPtr->lockHandle);
730 ctPtr->lockHandle = 0;
732 /* Don't try to analyse the error. Let the lock timeout */
736 /* bc_CheckTextVersion
739 * n - out of date or error
743 bc_CheckTextVersion(udbClientTextP ctPtr)
748 if (ctPtr->textVersion == -1)
749 return (BC_VERSIONMISMATCH);
752 ubik_BUDB_GetTextVersion(udbHandle.uh_client, 0,
753 ctPtr->textType, &tversion);
756 if (tversion != ctPtr->textVersion)
757 return (BC_VERSIONMISMATCH);
761 /* -------------------------------------
762 * initialization routines
763 * -------------------------------------
766 static afsconf_secflags
767 parseSecFlags(int noAuthFlag, int localauth, const char **confdir) {
768 afsconf_secflags secFlags;
772 secFlags |= AFSCONF_SECOPTS_NOAUTH;
775 secFlags |= AFSCONF_SECOPTS_LOCALAUTH;
776 *confdir = AFSDIR_SERVER_ETC_DIRPATH;
778 *confdir = AFSDIR_CLIENT_ETC_DIRPATH;
784 * Initialize a client for the vl ubik database.
787 vldbClientInit(int noAuthFlag, int localauth, char *cellName,
788 struct ubik_client **cstruct,
792 struct afsconf_dir *acdir;
793 struct rx_securityClass *sc;
794 afs_int32 i, scIndex = RX_SECIDX_NULL;
795 struct afsconf_cell info;
796 struct rx_connection *serverconns[VLDB_MAXSERVERS];
797 afsconf_secflags secFlags;
800 secFlags = parseSecFlags(noAuthFlag, localauth, &confdir);
801 secFlags |= AFSCONF_SECOPTS_FALLBACK_NULL;
803 /* This just preserves old behaviour of using the default cell when
804 * passed an empty string */
805 if (cellName && cellName[0] == '\0')
808 /* Find out about the given cell */
809 acdir = afsconf_Open(confdir);
811 afs_com_err(whoami, 0, "Can't open configuration directory '%s'", confdir);
812 ERROR(BC_NOCELLCONFIG);
815 code = afsconf_GetCellInfo(acdir, cellName, AFSCONF_VLDBSERVICE, &info);
817 afs_com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
818 cellName, confdir, AFSDIR_CELLSERVDB_FILE);
819 ERROR(BC_NOCELLCONFIG);
822 code = afsconf_PickClientSecObj(acdir, secFlags, &info, cellName,
823 &sc, &scIndex, expires);
825 afs_com_err(whoami, code, "(configuring connection security)");
826 ERROR(BC_NOCELLCONFIG);
828 if (scIndex == RX_SECIDX_NULL && !noAuthFlag)
829 afs_com_err(whoami, 0, "Can't get tokens - running unauthenticated");
831 /* tell UV module about default authentication */
832 UV_SetSecurity(sc, scIndex);
834 if (info.numServers > VLDB_MAXSERVERS) {
835 afs_com_err(whoami, 0,
836 "Warning: %d VLDB servers exist for cell '%s', can only remember the first %d",
837 info.numServers, cellName, VLDB_MAXSERVERS);
838 info.numServers = VLDB_MAXSERVERS;
841 for (i = 0; i < info.numServers; i++)
843 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
844 info.hostAddr[i].sin_port, USER_SERVICE_ID, sc,
849 code = ubik_ClientInit(serverconns, cstruct);
851 afs_com_err(whoami, code, "; Can't initialize ubik connection to vldb");
857 afsconf_Close(acdir);
862 * initialize a client for the backup systems ubik database.
866 udbClientInit(int noAuthFlag, int localauth, char *cellName)
868 struct afsconf_cell info;
869 struct afsconf_dir *acdir;
875 secFlags = parseSecFlags(noAuthFlag, localauth, &confdir);
876 secFlags |= AFSCONF_SECOPTS_FALLBACK_NULL;
878 if (cellName && cellName[0] == '\0')
881 acdir = afsconf_Open(confdir);
883 afs_com_err(whoami, 0, "Can't open configuration directory '%s'",
885 ERROR(BC_NOCELLCONFIG);
888 code = afsconf_GetCellInfo(acdir, cellName, 0, &info);
890 afs_com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
891 cellName, confdir, AFSDIR_CELLSERVDB_FILE);
892 ERROR(BC_NOCELLCONFIG);
895 code = afsconf_PickClientSecObj(acdir, secFlags, &info, cellName,
896 &udbHandle.uh_secobj,
897 &udbHandle.uh_scIndex, NULL);
899 afs_com_err(whoami, code, "(configuring connection security)");
900 ERROR(BC_NOCELLCONFIG);
902 if (&udbHandle.uh_scIndex == RX_SECIDX_NULL && !noAuthFlag)
903 afs_com_err(whoami, 0, "Can't get tokens - running unauthenticated");
905 if (info.numServers > MAXSERVERS) {
906 afs_com_err(whoami, 0,
907 "Warning: %d BDB servers exist for cell '%s', can only remember the first %d",
908 info.numServers, cellName, MAXSERVERS);
909 info.numServers = MAXSERVERS;
912 /* establish connections to the servers. Check for failed connections? */
913 for (i = 0; i < info.numServers; i++) {
914 udbHandle.uh_serverConn[i] =
915 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
916 htons(AFSCONF_BUDBPORT), BUDB_SERVICE,
917 udbHandle.uh_secobj, udbHandle.uh_scIndex);
919 udbHandle.uh_serverConn[i] = 0;
921 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
923 afs_com_err(whoami, code,
924 "; Can't initialize ubik connection to backup database");
928 /* Try to quickly find a good site by setting deadtime low */
929 for (i = 0; i < info.numServers; i++)
930 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 1);
932 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
933 &udbHandle.uh_instanceId);
935 /* Reset dead time back up to default */
936 for (i = 0; i < info.numServers; i++)
937 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 60);
939 /* If did not find a site on first quick pass, try again */
942 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
943 &udbHandle.uh_instanceId);
945 afs_com_err(whoami, code, "; Can't access backup database");
951 afsconf_Close(acdir);
955 /* -------------------------------------
956 * specialized ubik support
957 * -------------------------------------
965 * 1) first call with SINGLESERVER set, record the server to be used.
966 * 2) subsequent calls use that server. If a failure is encountered,
967 * the state is cleaned up and the error returned back to the caller.
968 * 3) upon completion, the user must make a dummy call with
969 * END_SINGLESERVER set, to clean up state.
970 * 4) if the vanilla ubik_Call is ever modified, the END_SINGLESERVER
971 * flag can be discarded. The first call without SINGLESERVER set
972 * can clean up the state.
975 struct ubikCallState {
976 afs_int32 ucs_flags; /* state flags */
977 afs_int32 ucs_selectedServer; /* which server selected */
980 static struct ubikCallState uServer;
982 /* ubik_Call_SingleServer
983 * variant of ubik_Call. This is used by the backup system to initiate
984 * a series of calls to a single ubik server. The first call sets up
985 * process state (not recorded in ubik) that requires all further calls
986 * of that group to be made to the same server process.
988 * call this instead of stub and we'll guarantee to find a host that's up.
989 * in the future, we should also put in a protocol to find the sync site
993 ubik_Call_SingleServer(int (*aproc) (), struct ubik_client *aclient,
994 afs_int32 aflags, char *p1, char *p2, char *p3,
995 char *p4, char *p5, char *p6, char *p7, char *p8,
996 char *p9, char *p10, char *p11, char *p12, char *p13,
997 char *p14, char *p15, char *p16)
1000 afs_int32 someCode, newHost, thisHost;
1005 struct rx_connection *tc;
1006 struct rx_peer *rxp;
1008 if ((aflags & (UF_SINGLESERVER | UF_END_SINGLESERVER)) != 0) {
1009 if (((aflags & UF_SINGLESERVER) != 0)
1010 && ((uServer.ucs_flags & UF_SINGLESERVER) != 0)
1013 /* have a selected server */
1014 tc = aclient->conns[uServer.ucs_selectedServer];
1017 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
1018 p12, p13, p14, p15, p16);
1020 /* error. Clean up single server state */
1021 memset(&uServer, 0, sizeof(uServer));
1024 } else if ((aflags & UF_END_SINGLESERVER) != 0) {
1025 memset(&uServer, 0, sizeof(uServer));
1030 someCode = UNOSERVERS;
1036 /* tc is the next conn to try */
1037 tc = aclient->conns[count];
1040 pass = 1; /* in pass 1, we look at down hosts, too */
1044 break; /* nothing left to try */
1046 if (pass == 0 && (aclient->states[count] & CFLastFailed)) {
1048 continue; /* this guy's down, try someone else first */
1052 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12,
1053 p13, p14, p15, p16);
1055 /* note that getting a UNOTSYNC error code back does *not* guarantee
1056 * that there is a sync site yet elected. However, if there is a
1057 * sync site out there somewhere, and you're trying an operation that
1058 * requires a sync site, ubik will return UNOTSYNC, indicating the
1059 * operation won't work until you find a sync site
1061 if (code == UNOTSYNC) { /*ns */
1062 /* means that this requires a sync site to work */
1063 someCode = code; /* remember an error, if this fails */
1065 /* now see if we can find the sync site host */
1066 code = VOTE_GetSyncSite(tc, &newHost);
1067 if (code == 0 && newHost != 0) {
1068 newHost = htonl(newHost); /* convert back to network order */
1070 /* position count at the appropriate slot in the client
1071 * structure and retry. If we can't find in slot, we'll just
1072 * continue through the whole list
1074 for (i = 0; i < MAXSERVERS; i++) { /*f */
1075 rxp = rx_PeerOf(aclient->conns[i]);
1076 if (!(thisHost = rx_HostOf(rxp))) {
1077 count++; /* host not found, try the next dude */
1080 if (thisHost == newHost) {
1081 /* avoid asking in a loop */
1082 if (chaseCount++ > 2)
1084 count = i; /* we were told to use this one */
1089 count++; /* not directed, keep looking for a sync site */
1092 else if (code == UNOQUORUM) { /* this guy is still recovering */
1096 } else if (code < 0) { /* network errors */
1098 aclient->states[count] |= CFLastFailed;
1102 /* ok, operation worked */
1103 aclient->states[count] &= ~CFLastFailed;
1104 /* either misc ubik code, or misc application code (incl success)
1107 /* if the call succeeded, setup connection state for subsequent
1111 && ((aflags & UF_SINGLESERVER) != 0)
1113 /* need to save state */
1114 uServer.ucs_flags = UF_SINGLESERVER;
1115 uServer.ucs_selectedServer = count;
1125 /* -------------------------------------
1126 * debug and test routines
1127 * -------------------------------------
1131 * For testing only. Open a connect to the database server running on
1134 * 0 - ubik connection established in the global udbHandle structure
1141 afs_uint32 serverList[MAXSERVERS];
1147 /* get our host name */
1148 gethostname(hostname, sizeof(hostname));
1149 /* strcpy(hostname, "hops"); */
1152 args[1] = "-servers";
1155 code = ubik_ParseClientList(3, args, serverList);
1157 afs_com_err(whoami, code, "; udbLocalInit: parsing ubik server list");
1161 udbHandle.uh_scIndex = RX_SECIDX_NULL;
1162 udbHandle.uh_secobj = (struct rx_securityClass *)
1163 rxnull_NewClientSecurityObject();
1165 for (i = 0; serverList[i] != 0; i++) {
1166 udbHandle.uh_serverConn[i] =
1167 rx_NewConnection(serverList[i], htons(AFSCONF_BUDBPORT),
1168 BUDB_SERVICE, udbHandle.uh_secobj,
1169 udbHandle.uh_scIndex);
1170 if (udbHandle.uh_serverConn[i] == 0) {
1171 afs_com_err(whoami, 0, "connection %d failed", i);
1175 udbHandle.uh_serverConn[i] = 0;
1176 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1178 afs_com_err(whoami, code, "; in ubik_ClientInit");
1183 ubik_BUDB_GetInstanceId(udbHandle.uh_client, 0,
1184 &udbHandle.uh_instanceId);
1186 afs_com_err(whoami, code, "; Can't estblish instance Id");
1194 /* bc_openTextFile - This function opens a temp file to read in the
1195 * config text recd from the bu server. On Unix, an unlink() is done on
1196 * the file as soon as it is opened, so when the program exits, the file will
1197 * be removed automatically, while being invisible while in use.
1198 * On NT, however, the file must be explicitly deleted after use with an unlink()
1200 * Pointer to a udhClientTextP struct. The open stream ptr is stored in
1201 * the udbClientTextP.textStream member.
1202 * Output: The temp file name is returned in tmpFileName. This should be used
1203 * to delete the file when done with it.
1209 bc_openTextFile(udbClientTextP ctPtr, char *tmpFileName)
1214 if (ctPtr->textStream != NULL) {
1215 fclose(ctPtr->textStream);
1216 ctPtr->textStream = NULL;
1219 sprintf(tmpFileName, "%s/bu_XXXXXX", gettmpdir());
1220 fd = mkstemp(tmpFileName);
1222 ERROR(BUDB_INTERNALERROR);
1223 ctPtr->textStream = fdopen(fd, "w+");
1224 if (ctPtr->textStream == NULL)
1225 ERROR(BUDB_INTERNALERROR);
1227 #ifndef AFS_NT40_ENV /* This can't be done on NT */
1228 /* make the file invisible to others */
1229 code = unlink(tmpFileName);
1234 afs_dprintf(("file is %s\n", tmpFileName));
1240 if (ctPtr->textStream != NULL) {
1241 fclose(ctPtr->textStream);
1242 ctPtr->textStream = NULL;
1248 /* bc_closeTextFile: This function closes any actual temp files associated with
1249 * a udbClientText structure.
1250 * Input: ctPtr->textStream - stream to close
1251 * tmpFileName - temp file name to delete
1257 bc_closeTextFile(udbClientTextP ctPtr, char *tmpFileName)
1261 if (ctPtr->textStream)
1262 fclose(ctPtr->textStream);
1264 if (*tmpFileName) { /* we have a valid name */
1265 code = unlink(tmpFileName);