17c478bd9Sstevel@tonic-gate /*-
27c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
57c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
67c478bd9Sstevel@tonic-gate */
77c478bd9Sstevel@tonic-gate
87c478bd9Sstevel@tonic-gate #include "config.h"
97c478bd9Sstevel@tonic-gate
107c478bd9Sstevel@tonic-gate #ifndef lint
117c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)xa_map.c 10.4 (Sleepycat) 10/20/98";
127c478bd9Sstevel@tonic-gate #endif /* not lint */
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
157c478bd9Sstevel@tonic-gate #include <sys/types.h>
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include <errno.h>
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <string.h>
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include "db_int.h"
237c478bd9Sstevel@tonic-gate #include "shqueue.h"
247c478bd9Sstevel@tonic-gate #include "txn.h"
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * This file contains all the mapping information that we need to support
287c478bd9Sstevel@tonic-gate * the DB/XA interface.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * __db_rmid_to_env
337c478bd9Sstevel@tonic-gate * Return the environment associated with a given XA rmid.
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * PUBLIC: int __db_rmid_to_env __P((int rmid, DB_ENV **envp, int open_ok));
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate int
__db_rmid_to_env(rmid,envp,open_ok)387c478bd9Sstevel@tonic-gate __db_rmid_to_env(rmid, envp, open_ok)
397c478bd9Sstevel@tonic-gate int rmid;
407c478bd9Sstevel@tonic-gate DB_ENV **envp;
417c478bd9Sstevel@tonic-gate int open_ok;
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate DB_ENV *env;
447c478bd9Sstevel@tonic-gate char *dbhome;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate env = TAILQ_FIRST(&DB_GLOBAL(db_envq));
477c478bd9Sstevel@tonic-gate if (env != NULL && env->xa_rmid == rmid) {
487c478bd9Sstevel@tonic-gate *envp = env;
497c478bd9Sstevel@tonic-gate return (0);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate * When we map an rmid, move that environment to be the first one in
547c478bd9Sstevel@tonic-gate * the list of environments, so we pass the correct environment from
557c478bd9Sstevel@tonic-gate * the upcoming db_xa_open() call into db_open().
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate for (; env != NULL; env = TAILQ_NEXT(env, links))
587c478bd9Sstevel@tonic-gate if (env->xa_rmid == rmid) {
597c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_envq), env, links);
607c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
617c478bd9Sstevel@tonic-gate *envp = env;
627c478bd9Sstevel@tonic-gate return (0);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * We have not found the rmid on the environment list. If we
677c478bd9Sstevel@tonic-gate * are allowed to do an open, search for the rmid on the name
687c478bd9Sstevel@tonic-gate * list and, if we find it, then open it.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate if (!open_ok)
717c478bd9Sstevel@tonic-gate return (1);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate if (__db_rmid_to_name(rmid, &dbhome) != 0)
747c478bd9Sstevel@tonic-gate return (1);
757c478bd9Sstevel@tonic-gate #undef XA_FLAGS
767c478bd9Sstevel@tonic-gate #define XA_FLAGS \
777c478bd9Sstevel@tonic-gate DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate if (__os_calloc(1, sizeof(DB_ENV), &env) != 0)
807c478bd9Sstevel@tonic-gate return (1);
817c478bd9Sstevel@tonic-gate
82*2a8bcb4eSToomas Soome if (db_appinit(dbhome, NULL, env, XA_FLAGS) != 0)
837c478bd9Sstevel@tonic-gate goto err;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if (__db_map_rmid(rmid, env) != 0)
867c478bd9Sstevel@tonic-gate goto err1;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate __db_unmap_rmid_name(rmid);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate *envp = env;
917c478bd9Sstevel@tonic-gate return (0);
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate err1: (void)db_appexit(env);
947c478bd9Sstevel@tonic-gate err: __os_free(env, sizeof(DB_ENV));
957c478bd9Sstevel@tonic-gate return (1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * __db_xid_to_txn
1007c478bd9Sstevel@tonic-gate * Return the txn that corresponds to this XID.
1017c478bd9Sstevel@tonic-gate *
1027c478bd9Sstevel@tonic-gate * PUBLIC: int __db_xid_to_txn __P((DB_ENV *, XID *, size_t *));
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate int
__db_xid_to_txn(dbenv,xid,offp)1057c478bd9Sstevel@tonic-gate __db_xid_to_txn(dbenv, xid, offp)
1067c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
1077c478bd9Sstevel@tonic-gate XID *xid;
1087c478bd9Sstevel@tonic-gate size_t *offp;
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate DB_TXNREGION *tmr;
1117c478bd9Sstevel@tonic-gate struct __txn_detail *td;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * Search the internal active transaction table to find the
1157c478bd9Sstevel@tonic-gate * matching xid. If this is a performance hit, then we
1167c478bd9Sstevel@tonic-gate * can create a hash table, but I doubt it's worth it.
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate tmr = dbenv->tx_info->region;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate LOCK_TXNREGION(dbenv->tx_info);
1217c478bd9Sstevel@tonic-gate for (td = SH_TAILQ_FIRST(&tmr->active_txn, __txn_detail);
1227c478bd9Sstevel@tonic-gate td != NULL;
1237c478bd9Sstevel@tonic-gate td = SH_TAILQ_NEXT(td, links, __txn_detail))
1247c478bd9Sstevel@tonic-gate if (memcmp(xid->data, td->xid, XIDDATASIZE) == 0)
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate UNLOCK_TXNREGION(dbenv->tx_info);
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (td == NULL)
1297c478bd9Sstevel@tonic-gate return (EINVAL);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate *offp = (u_int8_t *)td - (u_int8_t *)tmr;
1327c478bd9Sstevel@tonic-gate return (0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * __db_map_rmid
1377c478bd9Sstevel@tonic-gate * Create a mapping between the specified rmid and environment.
1387c478bd9Sstevel@tonic-gate *
1397c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_rmid __P((int, DB_ENV *));
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate int
__db_map_rmid(rmid,env)1427c478bd9Sstevel@tonic-gate __db_map_rmid(rmid, env)
1437c478bd9Sstevel@tonic-gate int rmid;
1447c478bd9Sstevel@tonic-gate DB_ENV *env;
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate if (__os_calloc(1, sizeof(DB_TXN), &env->xa_txn) != 0)
1477c478bd9Sstevel@tonic-gate return (XAER_RMERR);
1487c478bd9Sstevel@tonic-gate env->xa_txn->txnid = TXN_INVALID;
1497c478bd9Sstevel@tonic-gate env->xa_rmid = rmid;
1507c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
1517c478bd9Sstevel@tonic-gate return (XA_OK);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate * __db_unmap_rmid
1567c478bd9Sstevel@tonic-gate * Destroy the mapping for the given rmid.
1577c478bd9Sstevel@tonic-gate *
1587c478bd9Sstevel@tonic-gate * PUBLIC: int __db_unmap_rmid __P((int));
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate int
__db_unmap_rmid(rmid)1617c478bd9Sstevel@tonic-gate __db_unmap_rmid(rmid)
1627c478bd9Sstevel@tonic-gate int rmid;
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate DB_ENV *e;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate for (e = TAILQ_FIRST(&DB_GLOBAL(db_envq));
1677c478bd9Sstevel@tonic-gate e->xa_rmid != rmid;
1687c478bd9Sstevel@tonic-gate e = TAILQ_NEXT(e, links));
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (e == NULL)
1717c478bd9Sstevel@tonic-gate return (EINVAL);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_envq), e, links);
1747c478bd9Sstevel@tonic-gate if (e->xa_txn != NULL)
1757c478bd9Sstevel@tonic-gate __os_free(e->xa_txn, sizeof(DB_TXN));
1767c478bd9Sstevel@tonic-gate return (0);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * __db_map_xid
1817c478bd9Sstevel@tonic-gate * Create a mapping between this XID and the transaction at
1827c478bd9Sstevel@tonic-gate * "off" in the shared region.
1837c478bd9Sstevel@tonic-gate *
1847c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_xid __P((DB_ENV *, XID *, size_t));
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate int
__db_map_xid(env,xid,off)1877c478bd9Sstevel@tonic-gate __db_map_xid(env, xid, off)
1887c478bd9Sstevel@tonic-gate DB_ENV *env;
1897c478bd9Sstevel@tonic-gate XID *xid;
1907c478bd9Sstevel@tonic-gate size_t off;
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate DB_TXNMGR *tm;
1937c478bd9Sstevel@tonic-gate TXN_DETAIL *td;
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate tm = env->tx_info;
1967c478bd9Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)tm->region + off);
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate LOCK_TXNREGION(tm);
1997c478bd9Sstevel@tonic-gate memcpy(td->xid, xid->data, XIDDATASIZE);
2007c478bd9Sstevel@tonic-gate UNLOCK_TXNREGION(tm);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate * __db_unmap_xid
2077c478bd9Sstevel@tonic-gate * Destroy the mapping for the specified XID.
2087c478bd9Sstevel@tonic-gate *
2097c478bd9Sstevel@tonic-gate * PUBLIC: void __db_unmap_xid __P((DB_ENV *, XID *, size_t));
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate void
__db_unmap_xid(env,xid,off)2137c478bd9Sstevel@tonic-gate __db_unmap_xid(env, xid, off)
2147c478bd9Sstevel@tonic-gate DB_ENV *env;
2157c478bd9Sstevel@tonic-gate XID *xid;
2167c478bd9Sstevel@tonic-gate size_t off;
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate TXN_DETAIL *td;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate COMPQUIET(xid, NULL);
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
2237c478bd9Sstevel@tonic-gate memset(td->xid, 0, sizeof(td->xid));
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * __db_map_rmid_name --
2287c478bd9Sstevel@tonic-gate * Create a mapping from an rmid to a name (the xa_info argument).
2297c478bd9Sstevel@tonic-gate * We use this during create and then at some later point when we are
2307c478bd9Sstevel@tonic-gate * trying to map an rmid, we might indicate that it's OK to do an open
2317c478bd9Sstevel@tonic-gate * in which case, we'll get the xa_info parameter from here and then
2327c478bd9Sstevel@tonic-gate * free it up.
2337c478bd9Sstevel@tonic-gate *
2347c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_rmid_name __P((int, char *));
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate int
__db_map_rmid_name(rmid,dbhome)2387c478bd9Sstevel@tonic-gate __db_map_rmid_name(rmid, dbhome)
2397c478bd9Sstevel@tonic-gate int rmid;
2407c478bd9Sstevel@tonic-gate char *dbhome;
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate struct __rmname *entry;
2437c478bd9Sstevel@tonic-gate int ret;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if ((ret = __os_malloc(sizeof(struct __rmname), NULL, &entry)) != 0)
2467c478bd9Sstevel@tonic-gate return (ret);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if ((ret = __os_strdup(dbhome, &entry->dbhome)) != 0) {
2497c478bd9Sstevel@tonic-gate __os_free(entry, sizeof(struct __rmname));
2507c478bd9Sstevel@tonic-gate return (ret);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate entry->rmid = rmid;
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_nameq), entry, links);
2567c478bd9Sstevel@tonic-gate return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate * __db_rmid_to_name --
2617c478bd9Sstevel@tonic-gate * Given an rmid, return the name of the home directory for that
2627c478bd9Sstevel@tonic-gate * rmid.
2637c478bd9Sstevel@tonic-gate *
2647c478bd9Sstevel@tonic-gate * PUBLIC: int __db_rmid_to_name __P((int, char **));
2657c478bd9Sstevel@tonic-gate */
2667c478bd9Sstevel@tonic-gate int
__db_rmid_to_name(rmid,dbhomep)2677c478bd9Sstevel@tonic-gate __db_rmid_to_name(rmid, dbhomep)
2687c478bd9Sstevel@tonic-gate int rmid;
2697c478bd9Sstevel@tonic-gate char **dbhomep;
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate struct __rmname *np;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL;
2747c478bd9Sstevel@tonic-gate np = TAILQ_NEXT(np, links)) {
2757c478bd9Sstevel@tonic-gate if (np->rmid == rmid) {
2767c478bd9Sstevel@tonic-gate *dbhomep = np->dbhome;
2777c478bd9Sstevel@tonic-gate return (0);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate return (1);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * __db_unmap_rmid_name --
2857c478bd9Sstevel@tonic-gate * Given an rmid, remove its entry from the name list.
2867c478bd9Sstevel@tonic-gate *
2877c478bd9Sstevel@tonic-gate * PUBLIC: void __db_unmap_rmid_name __P((int));
2887c478bd9Sstevel@tonic-gate */
2897c478bd9Sstevel@tonic-gate void
__db_unmap_rmid_name(rmid)2907c478bd9Sstevel@tonic-gate __db_unmap_rmid_name(rmid)
2917c478bd9Sstevel@tonic-gate int rmid;
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate struct __rmname *np, *next;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL; np = next) {
2967c478bd9Sstevel@tonic-gate next = TAILQ_NEXT(np, links);
2977c478bd9Sstevel@tonic-gate if (np->rmid == rmid) {
2987c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_nameq), np, links);
2997c478bd9Sstevel@tonic-gate __os_freestr(np->dbhome);
3007c478bd9Sstevel@tonic-gate __os_free(np, sizeof(struct __rmname));
3017c478bd9Sstevel@tonic-gate return;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate return;
3057c478bd9Sstevel@tonic-gate }
306