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