1*7c478bd9Sstevel@tonic-gate /*-
2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*7c478bd9Sstevel@tonic-gate */
7*7c478bd9Sstevel@tonic-gate
8*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
9*7c478bd9Sstevel@tonic-gate
10*7c478bd9Sstevel@tonic-gate #include "config.h"
11*7c478bd9Sstevel@tonic-gate
12*7c478bd9Sstevel@tonic-gate #ifndef lint
13*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)xa_map.c 10.4 (Sleepycat) 10/20/98";
14*7c478bd9Sstevel@tonic-gate #endif /* not lint */
15*7c478bd9Sstevel@tonic-gate
16*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
17*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
18*7c478bd9Sstevel@tonic-gate
19*7c478bd9Sstevel@tonic-gate #include <errno.h>
20*7c478bd9Sstevel@tonic-gate #include <stdio.h>
21*7c478bd9Sstevel@tonic-gate #include <string.h>
22*7c478bd9Sstevel@tonic-gate #endif
23*7c478bd9Sstevel@tonic-gate
24*7c478bd9Sstevel@tonic-gate #include "db_int.h"
25*7c478bd9Sstevel@tonic-gate #include "shqueue.h"
26*7c478bd9Sstevel@tonic-gate #include "txn.h"
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate * This file contains all the mapping information that we need to support
30*7c478bd9Sstevel@tonic-gate * the DB/XA interface.
31*7c478bd9Sstevel@tonic-gate */
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate * __db_rmid_to_env
35*7c478bd9Sstevel@tonic-gate * Return the environment associated with a given XA rmid.
36*7c478bd9Sstevel@tonic-gate *
37*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_rmid_to_env __P((int rmid, DB_ENV **envp, int open_ok));
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate int
__db_rmid_to_env(rmid,envp,open_ok)40*7c478bd9Sstevel@tonic-gate __db_rmid_to_env(rmid, envp, open_ok)
41*7c478bd9Sstevel@tonic-gate int rmid;
42*7c478bd9Sstevel@tonic-gate DB_ENV **envp;
43*7c478bd9Sstevel@tonic-gate int open_ok;
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate DB_ENV *env;
46*7c478bd9Sstevel@tonic-gate char *dbhome;
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate env = TAILQ_FIRST(&DB_GLOBAL(db_envq));
49*7c478bd9Sstevel@tonic-gate if (env != NULL && env->xa_rmid == rmid) {
50*7c478bd9Sstevel@tonic-gate *envp = env;
51*7c478bd9Sstevel@tonic-gate return (0);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * When we map an rmid, move that environment to be the first one in
56*7c478bd9Sstevel@tonic-gate * the list of environments, so we pass the correct environment from
57*7c478bd9Sstevel@tonic-gate * the upcoming db_xa_open() call into db_open().
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate for (; env != NULL; env = TAILQ_NEXT(env, links))
60*7c478bd9Sstevel@tonic-gate if (env->xa_rmid == rmid) {
61*7c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_envq), env, links);
62*7c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
63*7c478bd9Sstevel@tonic-gate *envp = env;
64*7c478bd9Sstevel@tonic-gate return (0);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate * We have not found the rmid on the environment list. If we
69*7c478bd9Sstevel@tonic-gate * are allowed to do an open, search for the rmid on the name
70*7c478bd9Sstevel@tonic-gate * list and, if we find it, then open it.
71*7c478bd9Sstevel@tonic-gate */
72*7c478bd9Sstevel@tonic-gate if (!open_ok)
73*7c478bd9Sstevel@tonic-gate return (1);
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate if (__db_rmid_to_name(rmid, &dbhome) != 0)
76*7c478bd9Sstevel@tonic-gate return (1);
77*7c478bd9Sstevel@tonic-gate #undef XA_FLAGS
78*7c478bd9Sstevel@tonic-gate #define XA_FLAGS \
79*7c478bd9Sstevel@tonic-gate DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate if (__os_calloc(1, sizeof(DB_ENV), &env) != 0)
82*7c478bd9Sstevel@tonic-gate return (1);
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate if (db_appinit(dbhome, NULL, env, XA_FLAGS) != 0)
85*7c478bd9Sstevel@tonic-gate goto err;
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate if (__db_map_rmid(rmid, env) != 0)
88*7c478bd9Sstevel@tonic-gate goto err1;
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate __db_unmap_rmid_name(rmid);
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate *envp = env;
93*7c478bd9Sstevel@tonic-gate return (0);
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate err1: (void)db_appexit(env);
96*7c478bd9Sstevel@tonic-gate err: __os_free(env, sizeof(DB_ENV));
97*7c478bd9Sstevel@tonic-gate return (1);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate * __db_xid_to_txn
102*7c478bd9Sstevel@tonic-gate * Return the txn that corresponds to this XID.
103*7c478bd9Sstevel@tonic-gate *
104*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_xid_to_txn __P((DB_ENV *, XID *, size_t *));
105*7c478bd9Sstevel@tonic-gate */
106*7c478bd9Sstevel@tonic-gate int
__db_xid_to_txn(dbenv,xid,offp)107*7c478bd9Sstevel@tonic-gate __db_xid_to_txn(dbenv, xid, offp)
108*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
109*7c478bd9Sstevel@tonic-gate XID *xid;
110*7c478bd9Sstevel@tonic-gate size_t *offp;
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate DB_TXNREGION *tmr;
113*7c478bd9Sstevel@tonic-gate struct __txn_detail *td;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate /*
116*7c478bd9Sstevel@tonic-gate * Search the internal active transaction table to find the
117*7c478bd9Sstevel@tonic-gate * matching xid. If this is a performance hit, then we
118*7c478bd9Sstevel@tonic-gate * can create a hash table, but I doubt it's worth it.
119*7c478bd9Sstevel@tonic-gate */
120*7c478bd9Sstevel@tonic-gate tmr = dbenv->tx_info->region;
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate LOCK_TXNREGION(dbenv->tx_info);
123*7c478bd9Sstevel@tonic-gate for (td = SH_TAILQ_FIRST(&tmr->active_txn, __txn_detail);
124*7c478bd9Sstevel@tonic-gate td != NULL;
125*7c478bd9Sstevel@tonic-gate td = SH_TAILQ_NEXT(td, links, __txn_detail))
126*7c478bd9Sstevel@tonic-gate if (memcmp(xid->data, td->xid, XIDDATASIZE) == 0)
127*7c478bd9Sstevel@tonic-gate break;
128*7c478bd9Sstevel@tonic-gate UNLOCK_TXNREGION(dbenv->tx_info);
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate if (td == NULL)
131*7c478bd9Sstevel@tonic-gate return (EINVAL);
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate *offp = (u_int8_t *)td - (u_int8_t *)tmr;
134*7c478bd9Sstevel@tonic-gate return (0);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate * __db_map_rmid
139*7c478bd9Sstevel@tonic-gate * Create a mapping between the specified rmid and environment.
140*7c478bd9Sstevel@tonic-gate *
141*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_rmid __P((int, DB_ENV *));
142*7c478bd9Sstevel@tonic-gate */
143*7c478bd9Sstevel@tonic-gate int
__db_map_rmid(rmid,env)144*7c478bd9Sstevel@tonic-gate __db_map_rmid(rmid, env)
145*7c478bd9Sstevel@tonic-gate int rmid;
146*7c478bd9Sstevel@tonic-gate DB_ENV *env;
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate if (__os_calloc(1, sizeof(DB_TXN), &env->xa_txn) != 0)
149*7c478bd9Sstevel@tonic-gate return (XAER_RMERR);
150*7c478bd9Sstevel@tonic-gate env->xa_txn->txnid = TXN_INVALID;
151*7c478bd9Sstevel@tonic-gate env->xa_rmid = rmid;
152*7c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
153*7c478bd9Sstevel@tonic-gate return (XA_OK);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate /*
157*7c478bd9Sstevel@tonic-gate * __db_unmap_rmid
158*7c478bd9Sstevel@tonic-gate * Destroy the mapping for the given rmid.
159*7c478bd9Sstevel@tonic-gate *
160*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_unmap_rmid __P((int));
161*7c478bd9Sstevel@tonic-gate */
162*7c478bd9Sstevel@tonic-gate int
__db_unmap_rmid(rmid)163*7c478bd9Sstevel@tonic-gate __db_unmap_rmid(rmid)
164*7c478bd9Sstevel@tonic-gate int rmid;
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate DB_ENV *e;
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate for (e = TAILQ_FIRST(&DB_GLOBAL(db_envq));
169*7c478bd9Sstevel@tonic-gate e->xa_rmid != rmid;
170*7c478bd9Sstevel@tonic-gate e = TAILQ_NEXT(e, links));
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate if (e == NULL)
173*7c478bd9Sstevel@tonic-gate return (EINVAL);
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_envq), e, links);
176*7c478bd9Sstevel@tonic-gate if (e->xa_txn != NULL)
177*7c478bd9Sstevel@tonic-gate __os_free(e->xa_txn, sizeof(DB_TXN));
178*7c478bd9Sstevel@tonic-gate return (0);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate * __db_map_xid
183*7c478bd9Sstevel@tonic-gate * Create a mapping between this XID and the transaction at
184*7c478bd9Sstevel@tonic-gate * "off" in the shared region.
185*7c478bd9Sstevel@tonic-gate *
186*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_xid __P((DB_ENV *, XID *, size_t));
187*7c478bd9Sstevel@tonic-gate */
188*7c478bd9Sstevel@tonic-gate int
__db_map_xid(env,xid,off)189*7c478bd9Sstevel@tonic-gate __db_map_xid(env, xid, off)
190*7c478bd9Sstevel@tonic-gate DB_ENV *env;
191*7c478bd9Sstevel@tonic-gate XID *xid;
192*7c478bd9Sstevel@tonic-gate size_t off;
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate DB_TXNMGR *tm;
195*7c478bd9Sstevel@tonic-gate TXN_DETAIL *td;
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate tm = env->tx_info;
198*7c478bd9Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)tm->region + off);
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate LOCK_TXNREGION(tm);
201*7c478bd9Sstevel@tonic-gate memcpy(td->xid, xid->data, XIDDATASIZE);
202*7c478bd9Sstevel@tonic-gate UNLOCK_TXNREGION(tm);
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate return (0);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate * __db_unmap_xid
209*7c478bd9Sstevel@tonic-gate * Destroy the mapping for the specified XID.
210*7c478bd9Sstevel@tonic-gate *
211*7c478bd9Sstevel@tonic-gate * PUBLIC: void __db_unmap_xid __P((DB_ENV *, XID *, size_t));
212*7c478bd9Sstevel@tonic-gate */
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate void
__db_unmap_xid(env,xid,off)215*7c478bd9Sstevel@tonic-gate __db_unmap_xid(env, xid, off)
216*7c478bd9Sstevel@tonic-gate DB_ENV *env;
217*7c478bd9Sstevel@tonic-gate XID *xid;
218*7c478bd9Sstevel@tonic-gate size_t off;
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate TXN_DETAIL *td;
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate COMPQUIET(xid, NULL);
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
225*7c478bd9Sstevel@tonic-gate memset(td->xid, 0, sizeof(td->xid));
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate * __db_map_rmid_name --
230*7c478bd9Sstevel@tonic-gate * Create a mapping from an rmid to a name (the xa_info argument).
231*7c478bd9Sstevel@tonic-gate * We use this during create and then at some later point when we are
232*7c478bd9Sstevel@tonic-gate * trying to map an rmid, we might indicate that it's OK to do an open
233*7c478bd9Sstevel@tonic-gate * in which case, we'll get the xa_info parameter from here and then
234*7c478bd9Sstevel@tonic-gate * free it up.
235*7c478bd9Sstevel@tonic-gate *
236*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_map_rmid_name __P((int, char *));
237*7c478bd9Sstevel@tonic-gate */
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate int
__db_map_rmid_name(rmid,dbhome)240*7c478bd9Sstevel@tonic-gate __db_map_rmid_name(rmid, dbhome)
241*7c478bd9Sstevel@tonic-gate int rmid;
242*7c478bd9Sstevel@tonic-gate char *dbhome;
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate struct __rmname *entry;
245*7c478bd9Sstevel@tonic-gate int ret;
246*7c478bd9Sstevel@tonic-gate
247*7c478bd9Sstevel@tonic-gate if ((ret = __os_malloc(sizeof(struct __rmname), NULL, &entry)) != 0)
248*7c478bd9Sstevel@tonic-gate return (ret);
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate if ((ret = __os_strdup(dbhome, &entry->dbhome)) != 0) {
251*7c478bd9Sstevel@tonic-gate __os_free(entry, sizeof(struct __rmname));
252*7c478bd9Sstevel@tonic-gate return (ret);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate
255*7c478bd9Sstevel@tonic-gate entry->rmid = rmid;
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate TAILQ_INSERT_HEAD(&DB_GLOBAL(db_nameq), entry, links);
258*7c478bd9Sstevel@tonic-gate return (0);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate * __db_rmid_to_name --
263*7c478bd9Sstevel@tonic-gate * Given an rmid, return the name of the home directory for that
264*7c478bd9Sstevel@tonic-gate * rmid.
265*7c478bd9Sstevel@tonic-gate *
266*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_rmid_to_name __P((int, char **));
267*7c478bd9Sstevel@tonic-gate */
268*7c478bd9Sstevel@tonic-gate int
__db_rmid_to_name(rmid,dbhomep)269*7c478bd9Sstevel@tonic-gate __db_rmid_to_name(rmid, dbhomep)
270*7c478bd9Sstevel@tonic-gate int rmid;
271*7c478bd9Sstevel@tonic-gate char **dbhomep;
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate struct __rmname *np;
274*7c478bd9Sstevel@tonic-gate
275*7c478bd9Sstevel@tonic-gate for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL;
276*7c478bd9Sstevel@tonic-gate np = TAILQ_NEXT(np, links)) {
277*7c478bd9Sstevel@tonic-gate if (np->rmid == rmid) {
278*7c478bd9Sstevel@tonic-gate *dbhomep = np->dbhome;
279*7c478bd9Sstevel@tonic-gate return (0);
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate return (1);
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate
285*7c478bd9Sstevel@tonic-gate /*
286*7c478bd9Sstevel@tonic-gate * __db_unmap_rmid_name --
287*7c478bd9Sstevel@tonic-gate * Given an rmid, remove its entry from the name list.
288*7c478bd9Sstevel@tonic-gate *
289*7c478bd9Sstevel@tonic-gate * PUBLIC: void __db_unmap_rmid_name __P((int));
290*7c478bd9Sstevel@tonic-gate */
291*7c478bd9Sstevel@tonic-gate void
__db_unmap_rmid_name(rmid)292*7c478bd9Sstevel@tonic-gate __db_unmap_rmid_name(rmid)
293*7c478bd9Sstevel@tonic-gate int rmid;
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate struct __rmname *np, *next;
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL; np = next) {
298*7c478bd9Sstevel@tonic-gate next = TAILQ_NEXT(np, links);
299*7c478bd9Sstevel@tonic-gate if (np->rmid == rmid) {
300*7c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&DB_GLOBAL(db_nameq), np, links);
301*7c478bd9Sstevel@tonic-gate __os_freestr(np->dbhome);
302*7c478bd9Sstevel@tonic-gate __os_free(np, sizeof(struct __rmname));
303*7c478bd9Sstevel@tonic-gate return;
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate return;
307*7c478bd9Sstevel@tonic-gate }
308