Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / ubik / ubik.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15
16 #include <afs/opr.h>
17 #ifdef AFS_PTHREAD_ENV
18 # include <opr/lock.h>
19 #else
20 # include <opr/lockstub.h>
21 #endif
22
23 #include <lock.h>
24 #include <rx/rx.h>
25 #include <afs/cellconfig.h>
26 #include <afs/afsutil.h>
27
28
29 #define UBIK_INTERNALS
30 #include "ubik.h"
31 #include "ubik_int.h"
32
33 #include <lwp.h> /* temporary hack by klm */
34
35 #define ERROR_EXIT(code) do { \
36 error = (code); \
37 goto error_exit; \
38 } while (0)
39
40 /*!
41 * \file
42 * This system is organized in a hierarchical set of related modules. Modules
43 * at one level can only call modules at the same level or below.
44 *
45 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
46 * operating system primitives.
47 *
48 * At the next level (1) we have
49 *
50 * \li VOTER--The module responsible for casting votes when asked. It is also
51 * responsible for determining whether this server should try to become
52 * a synchronization site.
53 * \li BEACONER--The module responsible for sending keep-alives out when a
54 * server is actually the sync site, or trying to become a sync site.
55 * \li DISK--The module responsible for representing atomic transactions
56 * on the local disk. It maintains a new-value only log.
57 * \li LOCK--The module responsible for locking byte ranges in the database file.
58 *
59 * At the next level (2) we have
60 *
61 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
62 * have the same up-to-date database after a new synchronization site is
63 * elected. This module runs only on the synchronization site.
64 *
65 * At the next level (3) we have
66 *
67 * \li REMOTE--The module responsible for interpreting requests from the sync
68 * site and applying them to the database, after obtaining the appropriate
69 * locks.
70 *
71 * At the next level (4) we have
72 *
73 * \li UBIK--The module users call to perform operations on the database.
74 */
75
76
77 /* some globals */
78 afs_int32 ubik_quorum = 0;
79 struct ubik_dbase *ubik_dbase = 0;
80 struct ubik_stats ubik_stats;
81 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
82 afs_int32 urecovery_state = 0;
83 int (*ubik_SyncWriterCacheProc) (void);
84 struct ubik_server *ubik_servers;
85 short ubik_callPortal;
86
87 /* These global variables were used to control the server security layers.
88 * They are retained for backwards compatibility with legacy callers.
89 *
90 * The ubik_SetServerSecurityProcs() interface should be used instead.
91 */
92
93 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
94 void *ubik_SRXSecurityRock;
95 int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
96 void *ubik_CheckRXSecurityRock;
97
98
99
100 static int BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
101 struct ubik_trans **transPtr, int readAny);
102
103 static struct rx_securityClass **ubik_sc = NULL;
104 static void (*buildSecClassesProc)(void *, struct rx_securityClass ***,
105 afs_int32 *) = NULL;
106 static int (*checkSecurityProc)(void *, struct rx_call *) = NULL;
107 static void *securityRock = NULL;
108
109 struct version_data version_globals;
110
111 #define CStampVersion 1 /* meaning set ts->version */
112 #define CCheckSyncAdvertised 2 /* check if the remote knows we are the sync-site */
113
114 static_inline struct rx_connection *
115 Quorum_StartIO(struct ubik_trans *atrans, struct ubik_server *as)
116 {
117 struct rx_connection *conn;
118
119 UBIK_ADDR_LOCK;
120 conn = as->disk_rxcid;
121
122 #ifdef AFS_PTHREAD_ENV
123 rx_GetConnection(conn);
124 UBIK_ADDR_UNLOCK;
125 DBRELE(atrans->dbase);
126 #else
127 UBIK_ADDR_UNLOCK;
128 #endif /* AFS_PTHREAD_ENV */
129
130 return conn;
131 }
132
133 static_inline void
134 Quorum_EndIO(struct ubik_trans *atrans, struct rx_connection *aconn)
135 {
136 #ifdef AFS_PTHREAD_ENV
137 DBHOLD(atrans->dbase);
138 rx_PutConnection(aconn);
139 #endif /* AFS_PTHREAD_ENV */
140 }
141
142
143 /*
144 * Iterate over all servers. Callers pass in *ts which is used to track
145 * the current server.
146 * - Returns 1 if there are no more servers
147 * - Returns 0 with conn set to the connection for the current server if
148 * it's up and current
149 */
150 static int
151 ContactQuorum_iterate(struct ubik_trans *atrans, int aflags, struct ubik_server **ts,
152 struct rx_connection **conn, afs_int32 *rcode,
153 afs_int32 *okcalls, afs_int32 code)
154 {
155 if (!*ts) {
156 /* Initial call - start iterating over servers */
157 *ts = ubik_servers;
158 *conn = NULL;
159 *rcode = 0;
160 *okcalls = 0;
161 } else {
162 if (*conn) {
163 Quorum_EndIO(atrans, *conn);
164 *conn = NULL;
165 if (code) { /* failure */
166 *rcode = code;
167 UBIK_BEACON_LOCK;
168 (*ts)->up = 0; /* mark as down now; beacons will no longer be sent */
169 (*ts)->beaconSinceDown = 0;
170 UBIK_BEACON_UNLOCK;
171 (*ts)->currentDB = 0;
172 urecovery_LostServer(*ts); /* tell recovery to try to resend dbase later */
173 } else { /* success */
174 if (!(*ts)->isClone)
175 (*okcalls)++; /* count up how many worked */
176 if (aflags & CStampVersion) {
177 (*ts)->version = atrans->dbase->version;
178 }
179 }
180 }
181 *ts = (*ts)->next;
182 }
183 if (!(*ts))
184 return 1;
185 UBIK_BEACON_LOCK;
186 if (!(*ts)->up || !(*ts)->currentDB ||
187 /* do not call DISK_Begin until we know that lastYesState is set on the
188 * remote in question; otherwise, DISK_Begin will fail. */
189 ((aflags & CCheckSyncAdvertised) && !((*ts)->beaconSinceDown && (*ts)->lastVote))) {
190 UBIK_BEACON_UNLOCK;
191 (*ts)->currentDB = 0; /* db is no longer current; we just missed an update */
192 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
193 }
194 UBIK_BEACON_UNLOCK;
195 *conn = Quorum_StartIO(atrans, *ts);
196 return 0;
197 }
198
199 static int
200 ContactQuorum_rcode(int okcalls, afs_int32 rcode)
201 {
202 /*
203 * return 0 if we successfully contacted a quorum, otherwise return error code.
204 * We don't have to contact ourselves (that was done locally)
205 */
206 if (okcalls + 1 >= ubik_quorum)
207 return 0;
208 else
209 return (rcode != 0) ? rcode : UNOQUORUM;
210 }
211
212 /*!
213 * \brief Perform an operation at a quorum, handling error conditions.
214 * \return 0 if all worked and a quorum was contacted successfully
215 * \return otherwise mark failing server as down and return #UERROR
216 *
217 * \note If any server misses an update, we must wait #BIGTIME seconds before
218 * allowing the transaction to commit, to ensure that the missing and
219 * possibly still functioning server times out and stops handing out old
220 * data. This is done in the commit code, where we wait for a server marked
221 * down to have stayed down for #BIGTIME seconds before we allow a transaction
222 * to commit. A server that fails but comes back up won't give out old data
223 * because it is sent the sync count along with the beacon message that
224 * marks it as \b really up (\p beaconSinceDown).
225 */
226 static afs_int32
227 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
228 struct ubik_trans *atrans, int aflags)
229 {
230 struct ubik_server *ts = NULL;
231 afs_int32 code = 0, rcode, okcalls;
232 struct rx_connection *conn;
233 int done;
234
235 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
236 while (!done) {
237 if (conn)
238 code = (*proc)(conn, &atrans->tid);
239 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
240 }
241 return ContactQuorum_rcode(okcalls, rcode);
242 }
243
244
245 static afs_int32
246 ContactQuorum_DISK_Lock(struct ubik_trans *atrans, int aflags,afs_int32 file,
247 afs_int32 position, afs_int32 length, afs_int32 type)
248 {
249 struct ubik_server *ts = NULL;
250 afs_int32 code = 0, rcode, okcalls;
251 struct rx_connection *conn;
252 int done;
253
254 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
255 while (!done) {
256 if (conn)
257 code = DISK_Lock(conn, &atrans->tid, file, position, length, type);
258 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
259 }
260 return ContactQuorum_rcode(okcalls, rcode);
261 }
262
263 static afs_int32
264 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans, int aflags,
265 afs_int32 file, afs_int32 length)
266 {
267 struct ubik_server *ts = NULL;
268 afs_int32 code = 0, rcode, okcalls;
269 struct rx_connection *conn;
270 int done;
271
272 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
273 while (!done) {
274 if (conn)
275 code = DISK_Truncate(conn, &atrans->tid, file, length);
276 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
277 }
278 return ContactQuorum_rcode(okcalls, rcode);
279 }
280
281
282 static afs_int32
283 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans, int aflags,
284 iovec_wrt * io_vector, iovec_buf *io_buffer)
285 {
286 struct ubik_server *ts = NULL;
287 afs_int32 code = 0, rcode, okcalls;
288 struct rx_connection *conn;
289 int done;
290
291 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
292 while (!done) {
293 if (conn) {
294 code = DISK_WriteV(conn, &atrans->tid, io_vector, io_buffer);
295 if ((code <= -450) && (code > -500)) {
296 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
297 * Un-bulk the entries and do individual DISK_Write calls
298 * instead of DISK_WriteV.
299 */
300 struct ubik_iovec *iovec =
301 (struct ubik_iovec *)io_vector->iovec_wrt_val;
302 char *iobuf = (char *)io_buffer->iovec_buf_val;
303 bulkdata tcbs;
304 afs_int32 i, offset;
305
306 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
307 /* Sanity check for going off end of buffer */
308 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
309 code = UINTERNAL;
310 break;
311 }
312 tcbs.bulkdata_len = iovec[i].length;
313 tcbs.bulkdata_val = &iobuf[offset];
314 code = DISK_Write(conn, &atrans->tid, iovec[i].file,
315 iovec[i].position, &tcbs);
316 if (code)
317 break;
318 offset += iovec[i].length;
319 }
320 }
321 }
322 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
323 }
324 return ContactQuorum_rcode(okcalls, rcode);
325 }
326
327
328 afs_int32
329 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans, int aflags,
330 ubik_version *OldVersion,
331 ubik_version *NewVersion)
332 {
333 struct ubik_server *ts = NULL;
334 afs_int32 code = 0, rcode, okcalls;
335 struct rx_connection *conn;
336 int done;
337
338 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
339 while (!done) {
340 if (conn)
341 code = DISK_SetVersion(conn, &atrans->tid, OldVersion, NewVersion);
342 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
343 }
344 return ContactQuorum_rcode(okcalls, rcode);
345 }
346
347 #if defined(AFS_PTHREAD_ENV)
348 static int
349 ubik_thread_create(pthread_attr_t *tattr, pthread_t *thread, void *proc) {
350 opr_Verify(pthread_attr_init(tattr) == 0);
351 opr_Verify(pthread_attr_setdetachstate(tattr,
352 PTHREAD_CREATE_DETACHED) == 0);
353 opr_Verify(pthread_create(thread, tattr, proc, NULL) == 0);
354 return 0;
355 }
356 #endif
357
358 /*!
359 * \brief This routine initializes the ubik system for a set of servers.
360 * \return 0 for success, or an error code on failure.
361 * \param serverList set of servers specified; nServers gives the number of entries in this array.
362 * \param pathName provides an initial prefix used for naming storage files used by this system.
363 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
364 *
365 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
366 *
367 * \warning The host named by myHost should not also be listed in serverList.
368 *
369 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
370 */
371 static int
372 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
373 struct afsconf_cell *info, char clones[],
374 afs_uint32 serverList[], const char *pathName,
375 struct ubik_dbase **dbase)
376 {
377 struct ubik_dbase *tdb;
378 afs_int32 code;
379 #ifdef AFS_PTHREAD_ENV
380 pthread_t rxServerThread; /* pthread variables */
381 pthread_t ubeacon_InteractThread;
382 pthread_t urecovery_InteractThread;
383 pthread_attr_t rxServer_tattr;
384 pthread_attr_t ubeacon_Interact_tattr;
385 pthread_attr_t urecovery_Interact_tattr;
386 #else
387 PROCESS junk;
388 extern int rx_stackSize;
389 #endif
390
391 afs_int32 secIndex;
392 struct rx_securityClass *secClass;
393 int numClasses;
394
395 struct rx_service *tservice;
396
397 initialize_U_error_table();
398
399 tdb = malloc(sizeof(struct ubik_dbase));
400 tdb->pathName = strdup(pathName);
401 tdb->activeTrans = (struct ubik_trans *)0;
402 memset(&tdb->version, 0, sizeof(struct ubik_version));
403 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
404 #ifdef AFS_PTHREAD_ENV
405 opr_mutex_init(&tdb->versionLock);
406 opr_mutex_init(&beacon_globals.beacon_lock);
407 opr_mutex_init(&vote_globals.vote_lock);
408 opr_mutex_init(&addr_globals.addr_lock);
409 opr_mutex_init(&version_globals.version_lock);
410 #else
411 Lock_Init(&tdb->versionLock);
412 #endif
413 Lock_Init(&tdb->cache_lock);
414 tdb->flags = 0;
415 tdb->read = uphys_read;
416 tdb->write = uphys_write;
417 tdb->truncate = uphys_truncate;
418 tdb->open = uphys_invalidate; /* this function isn't used any more */
419 tdb->sync = uphys_sync;
420 tdb->stat = uphys_stat;
421 tdb->getlabel = uphys_getlabel;
422 tdb->setlabel = uphys_setlabel;
423 tdb->getnfiles = uphys_getnfiles;
424 tdb->buffered_append = uphys_buf_append;
425 tdb->readers = 0;
426 tdb->tidCounter = tdb->writeTidCounter = 0;
427 *dbase = tdb;
428 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
429
430 #ifdef AFS_PTHREAD_ENV
431 opr_cv_init(&tdb->version_cond);
432 opr_cv_init(&tdb->flags_cond);
433 #endif /* AFS_PTHREAD_ENV */
434
435 /* initialize RX */
436
437 /* the following call is idempotent so when/if it got called earlier,
438 * by whatever called us, it doesn't really matter -- klm */
439 code = rx_Init(myPort);
440 if (code < 0)
441 return code;
442
443 ubik_callPortal = myPort;
444
445 udisk_Init(ubik_nBuffers);
446 ulock_Init();
447
448 code = uvote_Init();
449 if (code)
450 return code;
451 code = urecovery_Initialize(tdb);
452 if (code)
453 return code;
454 if (info)
455 code = ubeacon_InitServerListByInfo(myHost, info, clones);
456 else
457 code = ubeacon_InitServerList(myHost, serverList);
458 if (code)
459 return code;
460
461 /* try to get an additional security object */
462 if (buildSecClassesProc == NULL) {
463 numClasses = 3;
464 ubik_sc = calloc(numClasses, sizeof(struct rx_securityClass *));
465 ubik_sc[0] = rxnull_NewServerSecurityObject();
466 if (ubik_SRXSecurityProc) {
467 code = (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock,
468 &secClass,
469 &secIndex);
470 if (code == 0) {
471 ubik_sc[secIndex] = secClass;
472 }
473 }
474 } else {
475 (*buildSecClassesProc) (securityRock, &ubik_sc, &numClasses);
476 }
477 /* for backwards compat this should keep working as it does now
478 and not host bind */
479
480 tservice =
481 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, numClasses,
482 VOTE_ExecuteRequest);
483 if (tservice == (struct rx_service *)0) {
484 ViceLog(5, ("Could not create VOTE rx service!\n"));
485 return -1;
486 }
487 rx_SetMinProcs(tservice, 2);
488 rx_SetMaxProcs(tservice, 3);
489
490 tservice =
491 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, numClasses,
492 DISK_ExecuteRequest);
493 if (tservice == (struct rx_service *)0) {
494 ViceLog(5, ("Could not create DISK rx service!\n"));
495 return -1;
496 }
497 rx_SetMinProcs(tservice, 2);
498 rx_SetMaxProcs(tservice, 3);
499
500 /* start an rx_ServerProc to handle incoming RPC's in particular the
501 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
502 * the "steplock" problem in ubik initialization. Defect 11037.
503 */
504 #ifdef AFS_PTHREAD_ENV
505 ubik_thread_create(&rxServer_tattr, &rxServerThread, (void *)rx_ServerProc);
506 #else
507 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
508 NULL, "rx_ServerProc", &junk);
509 #endif
510
511 /* send addrs to all other servers */
512 code = ubeacon_updateUbikNetworkAddress(ubik_host);
513 if (code)
514 return code;
515
516 /* now start up async processes */
517 #ifdef AFS_PTHREAD_ENV
518 ubik_thread_create(&ubeacon_Interact_tattr, &ubeacon_InteractThread,
519 (void *)ubeacon_Interact);
520 #else
521 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
522 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
523 &junk);
524 if (code)
525 return code;
526 #endif
527
528 #ifdef AFS_PTHREAD_ENV
529 ubik_thread_create(&urecovery_Interact_tattr, &urecovery_InteractThread,
530 (void *)urecovery_Interact);
531 return 0; /* is this correct? - klm */
532 #else
533 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
534 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
535 &junk);
536 return code;
537 #endif
538
539 }
540
541 /*!
542 * \see ubik_ServerInitCommon()
543 */
544 int
545 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
546 struct afsconf_cell *info, char clones[],
547 const char *pathName, struct ubik_dbase **dbase)
548 {
549 afs_int32 code;
550
551 code =
552 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
553 dbase);
554 return code;
555 }
556
557 /*!
558 * \see ubik_ServerInitCommon()
559 */
560 int
561 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
562 const char *pathName, struct ubik_dbase **dbase)
563 {
564 afs_int32 code;
565
566 code =
567 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
568 serverList, pathName, dbase);
569 return code;
570 }
571
572 /*!
573 * \brief This routine begins a read or write transaction on the transaction
574 * identified by transPtr, in the dbase named by dbase.
575 *
576 * An open mode of ubik_READTRANS identifies this as a read transaction,
577 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
578 * transPtr is set to the returned transaction control block.
579 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
580 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
581 * ubik_BeginTransReadAnyWrite() below.
582 *
583 * \note We can only begin transaction when we have an up-to-date database.
584 */
585 static int
586 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
587 struct ubik_trans **transPtr, int readAny)
588 {
589 struct ubik_trans *jt;
590 struct ubik_trans *tt;
591 afs_int32 code;
592
593 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
594 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
595 * cache-syncing function; fall back to ubik_BeginTransReadAny,
596 * which is safe but slower */
597 ViceLog(0, ("ubik_BeginTransReadAnyWrite called, but "
598 "ubik_SyncWriterCacheProc not set; pretending "
599 "ubik_BeginTransReadAny was called instead\n"));
600 readAny = 1;
601 }
602
603 if ((transMode != UBIK_READTRANS) && readAny)
604 return UBADTYPE;
605 DBHOLD(dbase);
606 if (urecovery_AllBetter(dbase, readAny) == 0) {
607 DBRELE(dbase);
608 return UNOQUORUM;
609 }
610 /* otherwise we have a quorum, use it */
611
612 /* make sure that at most one write transaction occurs at any one time. This
613 * has nothing to do with transaction locking; that's enforced by the lock package. However,
614 * we can't even handle two non-conflicting writes, since our log and recovery modules
615 * don't know how to restore one without possibly picking up some data from the other. */
616 if (transMode == UBIK_WRITETRANS) {
617 /* if we're writing already, wait */
618 while (dbase->flags & DBWRITING) {
619 #ifdef AFS_PTHREAD_ENV
620 opr_cv_wait(&dbase->flags_cond, &dbase->versionLock);
621 #else
622 DBRELE(dbase);
623 LWP_WaitProcess(&dbase->flags);
624 DBHOLD(dbase);
625 #endif
626 }
627
628 if (!ubeacon_AmSyncSite()) {
629 DBRELE(dbase);
630 return UNOTSYNC;
631 }
632 if (!ubeacon_SyncSiteAdvertised()) {
633 /* i am the sync-site but the remotes are not aware yet */
634 DBRELE(dbase);
635 return UNOQUORUM;
636 }
637 }
638
639 /* create the transaction */
640 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
641 tt = jt; /* move to a register */
642 if (code || tt == NULL) {
643 DBRELE(dbase);
644 return code;
645 }
646 UBIK_VERSION_LOCK;
647 if (readAny) {
648 tt->flags |= TRREADANY;
649 if (readAny > 1) {
650 tt->flags |= TRREADWRITE;
651 }
652 }
653 /* label trans and dbase with new tid */
654 tt->tid.epoch = version_globals.ubik_epochTime;
655 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
656 tt->tid.counter = (dbase->tidCounter += 2);
657
658 if (transMode == UBIK_WRITETRANS) {
659 /* for a write trans, we have to keep track of the write tid counter too */
660 dbase->writeTidCounter = tt->tid.counter;
661 }
662
663 UBIK_VERSION_UNLOCK;
664
665 if (transMode == UBIK_WRITETRANS) {
666 /* next try to start transaction on appropriate number of machines */
667 code = ContactQuorum_NoArguments(DISK_Begin, tt, CCheckSyncAdvertised);
668 if (code) {
669 /* we must abort the operation */
670 udisk_abort(tt);
671 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
672 udisk_end(tt);
673 DBRELE(dbase);
674 return code;
675 }
676 }
677
678 *transPtr = tt;
679 DBRELE(dbase);
680 return 0;
681 }
682
683 /*!
684 * \see BeginTrans()
685 */
686 int
687 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
688 struct ubik_trans **transPtr)
689 {
690 return BeginTrans(dbase, transMode, transPtr, 0);
691 }
692
693 /*!
694 * \see BeginTrans()
695 */
696 int
697 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
698 struct ubik_trans **transPtr)
699 {
700 return BeginTrans(dbase, transMode, transPtr, 1);
701 }
702
703 /*!
704 * \see BeginTrans()
705 */
706 int
707 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
708 struct ubik_trans **transPtr)
709 {
710 return BeginTrans(dbase, transMode, transPtr, 2);
711 }
712
713 /*!
714 * \brief This routine ends a read or write transaction by aborting it.
715 */
716 int
717 ubik_AbortTrans(struct ubik_trans *transPtr)
718 {
719 afs_int32 code;
720 afs_int32 code2;
721 struct ubik_dbase *dbase;
722
723 dbase = transPtr->dbase;
724
725 if (transPtr->flags & TRCACHELOCKED) {
726 ReleaseReadLock(&dbase->cache_lock);
727 transPtr->flags &= ~TRCACHELOCKED;
728 }
729
730 ObtainWriteLock(&dbase->cache_lock);
731
732 DBHOLD(dbase);
733 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
734
735 ReleaseWriteLock(&dbase->cache_lock);
736
737 /* see if we're still up-to-date */
738 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
739 udisk_abort(transPtr);
740 udisk_end(transPtr);
741 DBRELE(dbase);
742 return UNOQUORUM;
743 }
744
745 if (transPtr->type == UBIK_READTRANS) {
746 code = udisk_abort(transPtr);
747 udisk_end(transPtr);
748 DBRELE(dbase);
749 return code;
750 }
751
752 /* below here, we know we're doing a write transaction */
753 if (!ubeacon_AmSyncSite()) {
754 udisk_abort(transPtr);
755 udisk_end(transPtr);
756 DBRELE(dbase);
757 return UNOTSYNC;
758 }
759
760 /* now it is safe to try remote abort */
761 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
762 code2 = udisk_abort(transPtr);
763 udisk_end(transPtr);
764 DBRELE(dbase);
765 return (code ? code : code2);
766 }
767
768 static void
769 WritebackApplicationCache(struct ubik_dbase *dbase)
770 {
771 int code = 0;
772 if (ubik_SyncWriterCacheProc) {
773 code = ubik_SyncWriterCacheProc();
774 }
775 if (code) {
776 /* we failed to sync the local cache, so just invalidate the cache;
777 * we'll try to read the cache in again on the next read */
778 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
779 } else {
780 memcpy(&dbase->cachedVersion, &dbase->version,
781 sizeof(dbase->cachedVersion));
782 }
783 }
784
785 /*!
786 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
787 * \return an error code.
788 */
789 int
790 ubik_EndTrans(struct ubik_trans *transPtr)
791 {
792 afs_int32 code;
793 struct timeval tv;
794 afs_int32 realStart;
795 struct ubik_server *ts;
796 afs_int32 now;
797 int cachelocked = 0;
798 struct ubik_dbase *dbase;
799
800 if (transPtr->type == UBIK_WRITETRANS) {
801 code = ubik_Flush(transPtr);
802 if (code) {
803 ubik_AbortTrans(transPtr);
804 return (code);
805 }
806 }
807
808 dbase = transPtr->dbase;
809
810 if (transPtr->flags & TRCACHELOCKED) {
811 ReleaseReadLock(&dbase->cache_lock);
812 transPtr->flags &= ~TRCACHELOCKED;
813 }
814
815 if (transPtr->type != UBIK_READTRANS) {
816 /* must hold cache_lock before DBHOLD'ing */
817 ObtainWriteLock(&dbase->cache_lock);
818 cachelocked = 1;
819 }
820
821 DBHOLD(dbase);
822
823 /* give up if no longer current */
824 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
825 udisk_abort(transPtr);
826 udisk_end(transPtr);
827 DBRELE(dbase);
828 code = UNOQUORUM;
829 goto error;
830 }
831
832 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
833 code = udisk_commit(transPtr);
834 if (code == 0)
835 goto success; /* update cachedVersion correctly */
836 udisk_end(transPtr);
837 DBRELE(dbase);
838 goto error;
839 }
840
841 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
842 udisk_abort(transPtr);
843 udisk_end(transPtr);
844 DBRELE(dbase);
845 code = UNOTSYNC;
846 goto error;
847 }
848
849 /* now it is safe to do commit */
850 code = udisk_commit(transPtr);
851 if (code == 0) {
852 /* db data has been committed locally; update the local cache so
853 * readers can get at it */
854 WritebackApplicationCache(dbase);
855
856 ReleaseWriteLock(&dbase->cache_lock);
857
858 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
859
860 } else {
861 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
862 ReleaseWriteLock(&dbase->cache_lock);
863 }
864 cachelocked = 0;
865 if (code) {
866 /* failed to commit, so must return failure. Try to clear locks first, just for fun
867 * Note that we don't know if this transaction will eventually commit at this point.
868 * If it made it to a site that will be present in the next quorum, we win, otherwise
869 * we lose. If we contact a majority of sites, then we won't be here: contacting
870 * a majority guarantees commit, since it guarantees that one dude will be a
871 * member of the next quorum. */
872 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
873 udisk_end(transPtr);
874 DBRELE(dbase);
875 goto error;
876 }
877 /* before we can start sending unlock messages, we must wait until all servers
878 * that are possibly still functioning on the other side of a network partition
879 * have timed out. Check the server structures, compute how long to wait, then
880 * start the unlocks */
881 realStart = FT_ApproxTime();
882 while (1) {
883 /* wait for all servers to time out */
884 code = 0;
885 now = FT_ApproxTime();
886 /* check if we're still sync site, the guy should either come up
887 * to us, or timeout. Put safety check in anyway */
888 if (now - realStart > 10 * BIGTIME) {
889 ubik_stats.escapes++;
890 ViceLog(0, ("ubik escaping from commit wait\n"));
891 break;
892 }
893 for (ts = ubik_servers; ts; ts = ts->next) {
894 UBIK_BEACON_LOCK;
895 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
896 UBIK_BEACON_UNLOCK;
897
898 /* this guy could have some damaged data, wait for him */
899 code = 1;
900 tv.tv_sec = 1; /* try again after a while (ha ha) */
901 tv.tv_usec = 0;
902
903 #ifdef AFS_PTHREAD_ENV
904 /* we could release the dbase outside of the loop, but we do
905 * it here, in the loop, to avoid an unnecessary RELE/HOLD
906 * if all sites are up */
907 DBRELE(dbase);
908 select(0, 0, 0, 0, &tv);
909 DBHOLD(dbase);
910 #else
911 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
912 #endif
913
914 break;
915 }
916 UBIK_BEACON_UNLOCK;
917 }
918 if (code == 0)
919 break; /* no down ones still pseudo-active */
920 }
921
922 /* finally, unlock all the dudes. We can return success independent of the number of servers
923 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
924 * The transaction is committed anyway, since we succeeded in contacting a quorum
925 * at the start (when invoking the DiskCommit function).
926 */
927 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
928
929 success:
930 udisk_end(transPtr);
931 /* don't update cachedVersion here; it should have been updated way back
932 * in ubik_CheckCache, and earlier in this function for writes */
933 DBRELE(dbase);
934 if (cachelocked) {
935 ReleaseWriteLock(&dbase->cache_lock);
936 }
937 return 0;
938
939 error:
940 if (!cachelocked) {
941 ObtainWriteLock(&dbase->cache_lock);
942 }
943 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
944 ReleaseWriteLock(&dbase->cache_lock);
945 return code;
946 }
947
948 /*!
949 * \brief This routine reads length bytes into buffer from the current position in the database.
950 *
951 * The file pointer is updated appropriately (by adding the number of bytes actually transferred), and the length actually transferred is stored in the long integer pointed to by length. A short read returns zero for an error code.
952 *
953 * \note *length is an INOUT parameter: at the start it represents the size of the buffer, and when done, it contains the number of bytes actually transferred.
954 */
955 int
956 ubik_Read(struct ubik_trans *transPtr, void *buffer,
957 afs_int32 length)
958 {
959 afs_int32 code;
960
961 /* reads are easy to do: handle locally */
962 DBHOLD(transPtr->dbase);
963 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
964 DBRELE(transPtr->dbase);
965 return UNOQUORUM;
966 }
967
968 code =
969 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
970 length);
971 if (code == 0) {
972 transPtr->seekPos += length;
973 }
974 DBRELE(transPtr->dbase);
975 return code;
976 }
977
978 /*!
979 * \brief This routine will flush the io data in the iovec structures.
980 *
981 * It first flushes to the local disk and then uses ContactQuorum to write it
982 * to the other servers.
983 */
984 int
985 ubik_Flush(struct ubik_trans *transPtr)
986 {
987 afs_int32 code, error = 0;
988
989 if (transPtr->type != UBIK_WRITETRANS)
990 return UBADTYPE;
991
992 DBHOLD(transPtr->dbase);
993 if (!transPtr->iovec_info.iovec_wrt_len
994 || !transPtr->iovec_info.iovec_wrt_val) {
995 DBRELE(transPtr->dbase);
996 return 0;
997 }
998
999 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1000 ERROR_EXIT(UNOQUORUM);
1001 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1002 ERROR_EXIT(UNOTSYNC);
1003
1004 /* Update the rest of the servers in the quorum */
1005 code =
1006 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
1007 &transPtr->iovec_data);
1008 if (code) {
1009 udisk_abort(transPtr);
1010 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1011 transPtr->iovec_info.iovec_wrt_len = 0;
1012 transPtr->iovec_data.iovec_buf_len = 0;
1013 ERROR_EXIT(code);
1014 }
1015
1016 /* Wrote the buffers out, so start at scratch again */
1017 transPtr->iovec_info.iovec_wrt_len = 0;
1018 transPtr->iovec_data.iovec_buf_len = 0;
1019
1020 error_exit:
1021 DBRELE(transPtr->dbase);
1022 return error;
1023 }
1024
1025 int
1026 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
1027 afs_int32 length)
1028 {
1029 struct ubik_iovec *iovec;
1030 afs_int32 code, error = 0;
1031 afs_int32 pos, len, size;
1032 char * buffer = (char *)vbuffer;
1033
1034 if (transPtr->type != UBIK_WRITETRANS)
1035 return UBADTYPE;
1036 if (!length)
1037 return 0;
1038
1039 if (length > IOVEC_MAXBUF) {
1040 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1041 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1042 code = ubik_Write(transPtr, buffer+pos, size);
1043 if (code)
1044 return (code);
1045 }
1046 return 0;
1047 }
1048
1049 DBHOLD(transPtr->dbase);
1050 if (!transPtr->iovec_info.iovec_wrt_val) {
1051 transPtr->iovec_info.iovec_wrt_len = 0;
1052 transPtr->iovec_info.iovec_wrt_val =
1053 malloc(IOVEC_MAXWRT * sizeof(struct ubik_iovec));
1054 transPtr->iovec_data.iovec_buf_len = 0;
1055 transPtr->iovec_data.iovec_buf_val = malloc(IOVEC_MAXBUF);
1056 if (!transPtr->iovec_info.iovec_wrt_val
1057 || !transPtr->iovec_data.iovec_buf_val) {
1058 if (transPtr->iovec_info.iovec_wrt_val)
1059 free(transPtr->iovec_info.iovec_wrt_val);
1060 transPtr->iovec_info.iovec_wrt_val = 0;
1061 if (transPtr->iovec_data.iovec_buf_val)
1062 free(transPtr->iovec_data.iovec_buf_val);
1063 transPtr->iovec_data.iovec_buf_val = 0;
1064 DBRELE(transPtr->dbase);
1065 return UNOMEM;
1066 }
1067 }
1068
1069 /* If this write won't fit in the structure, then flush it out and start anew */
1070 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1071 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1072 /* Can't hold the DB lock over ubik_Flush */
1073 DBRELE(transPtr->dbase);
1074 code = ubik_Flush(transPtr);
1075 if (code)
1076 return (code);
1077 DBHOLD(transPtr->dbase);
1078 }
1079
1080 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1081 ERROR_EXIT(UNOQUORUM);
1082 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1083 ERROR_EXIT(UNOTSYNC);
1084
1085 /* Write to the local disk */
1086 code =
1087 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1088 length);
1089 if (code) {
1090 udisk_abort(transPtr);
1091 transPtr->iovec_info.iovec_wrt_len = 0;
1092 transPtr->iovec_data.iovec_buf_len = 0;
1093 DBRELE(transPtr->dbase);
1094 return (code);
1095 }
1096
1097 /* Collect writes for the other ubik servers (to be done in bulk) */
1098 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1099 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1100 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1101 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1102
1103 memcpy(&transPtr->iovec_data.
1104 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1105
1106 transPtr->iovec_info.iovec_wrt_len++;
1107 transPtr->iovec_data.iovec_buf_len += length;
1108 transPtr->seekPos += length;
1109
1110 error_exit:
1111 DBRELE(transPtr->dbase);
1112 return error;
1113 }
1114
1115 /*!
1116 * \brief This sets the file pointer associated with the current transaction
1117 * to the appropriate file and byte position.
1118 *
1119 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1120 * and a byte position relative to the specified file \p position.
1121 */
1122 int
1123 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1124 afs_int32 position)
1125 {
1126 afs_int32 code;
1127
1128 DBHOLD(transPtr->dbase);
1129 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1130 code = UNOQUORUM;
1131 } else {
1132 transPtr->seekFile = fileid;
1133 transPtr->seekPos = position;
1134 code = 0;
1135 }
1136 DBRELE(transPtr->dbase);
1137 return code;
1138 }
1139
1140 /*!
1141 * \brief This call returns the file pointer associated with the specified
1142 * transaction in \p fileid and \p position.
1143 */
1144 int
1145 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1146 afs_int32 * position)
1147 {
1148 DBHOLD(transPtr->dbase);
1149 *fileid = transPtr->seekFile;
1150 *position = transPtr->seekPos;
1151 DBRELE(transPtr->dbase);
1152 return 0;
1153 }
1154
1155 /*!
1156 * \brief This sets the file size for the currently-selected file to \p length
1157 * bytes, if length is less than the file's current size.
1158 */
1159 int
1160 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1161 {
1162 afs_int32 code, error = 0;
1163
1164 /* Will also catch if not UBIK_WRITETRANS */
1165 code = ubik_Flush(transPtr);
1166 if (code)
1167 return (code);
1168
1169 DBHOLD(transPtr->dbase);
1170 /* first, check that quorum is still good, and that dbase is up-to-date */
1171 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1172 ERROR_EXIT(UNOQUORUM);
1173 if (!ubeacon_AmSyncSite())
1174 ERROR_EXIT(UNOTSYNC);
1175
1176 /* now do the operation locally, and propagate it out */
1177 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1178 if (!code) {
1179 code =
1180 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1181 length);
1182 }
1183 if (code) {
1184 /* we must abort the operation */
1185 udisk_abort(transPtr);
1186 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1187 ERROR_EXIT(code);
1188 }
1189
1190 error_exit:
1191 DBRELE(transPtr->dbase);
1192 return error;
1193 }
1194
1195 /*!
1196 * \brief set a lock; all locks are released on transaction end (commit/abort)
1197 */
1198 int
1199 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1200 int atype)
1201 {
1202 afs_int32 code = 0, error = 0;
1203
1204 if (atype == LOCKWRITE) {
1205 if (atrans->type == UBIK_READTRANS)
1206 return UBADTYPE;
1207 code = ubik_Flush(atrans);
1208 if (code)
1209 return (code);
1210 }
1211
1212 DBHOLD(atrans->dbase);
1213 if (atype == LOCKREAD) {
1214 code = ulock_getLock(atrans, atype, 1);
1215 if (code)
1216 ERROR_EXIT(code);
1217 } else {
1218 /* first, check that quorum is still good, and that dbase is up-to-date */
1219 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1220 ERROR_EXIT(UNOQUORUM);
1221 if (!ubeacon_AmSyncSite())
1222 ERROR_EXIT(UNOTSYNC);
1223
1224 /* now do the operation locally, and propagate it out */
1225 code = ulock_getLock(atrans, atype, 1);
1226 if (code == 0) {
1227 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1228 1 /*unused */ , LOCKWRITE);
1229 }
1230 if (code) {
1231 /* we must abort the operation */
1232 udisk_abort(atrans);
1233 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1234 ERROR_EXIT(code);
1235 }
1236 }
1237
1238 error_exit:
1239 DBRELE(atrans->dbase);
1240 return error;
1241 }
1242
1243 /*!
1244 * \brief utility to wait for a version # to change
1245 */
1246 int
1247 ubik_WaitVersion(struct ubik_dbase *adatabase,
1248 struct ubik_version *aversion)
1249 {
1250 DBHOLD(adatabase);
1251 while (1) {
1252 /* wait until version # changes, and then return */
1253 if (vcmp(*aversion, adatabase->version) != 0) {
1254 DBRELE(adatabase);
1255 return 0;
1256 }
1257 #ifdef AFS_PTHREAD_ENV
1258 opr_cv_wait(&adatabase->version_cond, &adatabase->versionLock);
1259 #else
1260 DBRELE(adatabase);
1261 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1262 DBHOLD(adatabase);
1263 #endif
1264 }
1265 }
1266
1267 /*!
1268 * \brief utility to get the version of the dbase a transaction is dealing with
1269 */
1270 int
1271 ubik_GetVersion(struct ubik_trans *atrans,
1272 struct ubik_version *avers)
1273 {
1274 DBHOLD(atrans->dbase);
1275 *avers = atrans->dbase->version;
1276 DBRELE(atrans->dbase);
1277 return 0;
1278 }
1279
1280 /*!
1281 * \brief Facility to simplify database caching.
1282 * \return zero if last trans was done on the local server and was successful.
1283 * \return -1 means bad (NULL) argument.
1284 *
1285 * If return value is non-zero and the caller is a server caching part of the
1286 * Ubik database, it should invalidate that cache.
1287 */
1288 static int
1289 ubik_CacheUpdate(struct ubik_trans *atrans)
1290 {
1291 if (!(atrans && atrans->dbase))
1292 return -1;
1293 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1294 }
1295
1296 /**
1297 * check and possibly update cache of ubik db.
1298 *
1299 * If the version of the cached db data is out of date, this calls (*check) to
1300 * update the cache. If (*check) returns success, we update the version of the
1301 * cached db data.
1302 *
1303 * Checking the version of the cached db data is done under a read lock;
1304 * updating the cache (and thus calling (*check)) is done under a write lock
1305 * so is guaranteed not to interfere with another thread's (*check). On
1306 * successful return, a read lock on the cached db data is obtained, which
1307 * will be released by ubik_EndTrans or ubik_AbortTrans.
1308 *
1309 * @param[in] atrans ubik transaction
1310 * @param[in] check function to call to check/update cache
1311 * @param[in] rock rock to pass to *check
1312 *
1313 * @return operation status
1314 * @retval 0 success
1315 * @retval nonzero error; cachedVersion not updated
1316 *
1317 * @post On success, application cache is read-locked, and cache data is
1318 * up-to-date
1319 */
1320 int
1321 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1322 {
1323 int ret = 0;
1324
1325 if (!(atrans && atrans->dbase))
1326 return -1;
1327
1328 ObtainReadLock(&atrans->dbase->cache_lock);
1329
1330 while (ubik_CacheUpdate(atrans) != 0) {
1331
1332 ReleaseReadLock(&atrans->dbase->cache_lock);
1333 ObtainSharedLock(&atrans->dbase->cache_lock);
1334
1335 if (ubik_CacheUpdate(atrans) != 0) {
1336
1337 BoostSharedLock(&atrans->dbase->cache_lock);
1338
1339 ret = (*cbf) (atrans, rock);
1340 if (ret == 0) {
1341 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1342 sizeof(atrans->dbase->cachedVersion));
1343 }
1344 }
1345
1346 /* It would be nice if we could convert from a shared lock to a read
1347 * lock... instead, just release the shared and acquire the read */
1348 ReleaseSharedLock(&atrans->dbase->cache_lock);
1349
1350 if (ret) {
1351 /* if we have an error, don't retry, and don't hold any locks */
1352 return ret;
1353 }
1354
1355 ObtainReadLock(&atrans->dbase->cache_lock);
1356 }
1357
1358 atrans->flags |= TRCACHELOCKED;
1359
1360 return 0;
1361 }
1362
1363 /*!
1364 * "Who said anything about panicking?" snapped Arthur.
1365 * "This is still just the culture shock. You wait till I've settled down
1366 * into the situation and found my bearings. \em Then I'll start panicking!"
1367 * --Authur Dent
1368 *
1369 * \returns There is no return from panic.
1370 */
1371 void
1372 panic(char *format, ...)
1373 {
1374 va_list ap;
1375
1376 va_start(ap, format);
1377 ViceLog(0, ("Ubik PANIC:\n"));
1378 vViceLog(0, (format, ap));
1379 va_end(ap);
1380
1381 abort();
1382 ViceLog(0, ("BACK FROM ABORT\n")); /* shouldn't come back */
1383 exit(1); /* never know, though */
1384 }
1385
1386 /*!
1387 * This function takes an IP addresses as its parameter. It returns the
1388 * the primary IP address that is on the host passed in, or 0 if not found.
1389 */
1390 afs_uint32
1391 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1392 {
1393 struct ubik_server *ts;
1394 int j;
1395
1396 UBIK_ADDR_LOCK;
1397 for (ts = ubik_servers; ts; ts = ts->next)
1398 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1399 if (ts->addr[j] == addr) {
1400 UBIK_ADDR_UNLOCK;
1401 return ts->addr[0]; /* net byte order */
1402 }
1403 UBIK_ADDR_UNLOCK;
1404 return 0; /* if not in server database, return error */
1405 }
1406
1407 int
1408 ubik_CheckAuth(struct rx_call *acall)
1409 {
1410 if (checkSecurityProc)
1411 return (*checkSecurityProc) (securityRock, acall);
1412 else if (ubik_CheckRXSecurityProc) {
1413 return (*ubik_CheckRXSecurityProc) (ubik_CheckRXSecurityRock, acall);
1414 } else
1415 return 0;
1416 }
1417
1418 void
1419 ubik_SetServerSecurityProcs(void (*buildproc) (void *,
1420 struct rx_securityClass ***,
1421 afs_int32 *),
1422 int (*checkproc) (void *, struct rx_call *),
1423 void *rock)
1424 {
1425 buildSecClassesProc = buildproc;
1426 checkSecurityProc = checkproc;
1427 securityRock = rock;
1428 }