1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: xa_map.c,v 12.12 2008/01/08 20:59:00 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/txn.h"
13
14/*
15 * This file contains all the mapping information that we need to support
16 * the DB/XA interface.
17 */
18
19/*
20 * __db_rmid_to_env
21 *	Return the environment associated with a given XA rmid.
22 *
23 * PUBLIC: int __db_rmid_to_env __P((int, ENV **));
24 */
25int
26__db_rmid_to_env(rmid, envp)
27	int rmid;
28	ENV **envp;
29{
30	ENV *env;
31
32	env = TAILQ_FIRST(&DB_GLOBAL(envq));
33	if (env != NULL && env->xa_rmid == rmid) {
34		*envp = env;
35		return (0);
36	}
37
38	/*
39	 * When we map an rmid, move that environment to be the first one in
40	 * the list of environments, so we acquire the correct environment
41	 * in DB->open.
42	 */
43	for (; env != NULL; env = TAILQ_NEXT(env, links))
44		if (env->xa_rmid == rmid) {
45			TAILQ_REMOVE(&DB_GLOBAL(envq), env, links);
46			TAILQ_INSERT_HEAD(&DB_GLOBAL(envq), env, links);
47			*envp = env;
48			return (0);
49		}
50
51	return (1);
52}
53
54/*
55 * __db_xid_to_txn
56 *	Return the txn that corresponds to this XID.
57 *
58 * PUBLIC: int __db_xid_to_txn __P((ENV *, XID *, roff_t *));
59 */
60int
61__db_xid_to_txn(env, xid, offp)
62	ENV *env;
63	XID *xid;
64	roff_t *offp;
65{
66	struct __txn_detail *td;
67
68	return (__txn_map_gid(env, (u_int8_t *)xid->data, &td, offp));
69}
70
71/*
72 * __db_map_rmid
73 *	Create a mapping between the specified rmid and environment.
74 *
75 * PUBLIC: int __db_map_rmid __P((int, ENV *));
76 */
77int
78__db_map_rmid(rmid, env)
79	int rmid;
80	ENV *env;
81{
82	env->xa_rmid = rmid;
83	TAILQ_INSERT_TAIL(&DB_GLOBAL(envq), env, links);
84	return (0);
85}
86
87/*
88 * __db_unmap_rmid
89 *	Destroy the mapping for the given rmid.
90 *
91 * PUBLIC: int __db_unmap_rmid __P((int));
92 */
93int
94__db_unmap_rmid(rmid)
95	int rmid;
96{
97	ENV *e;
98
99	for (e = TAILQ_FIRST(&DB_GLOBAL(envq));
100	    e->xa_rmid != rmid;
101	    e = TAILQ_NEXT(e, links))
102	    ;
103
104	if (e == NULL)
105		return (EINVAL);
106
107	TAILQ_REMOVE(&DB_GLOBAL(envq), e, links);
108	return (0);
109}
110
111/*
112 * __db_map_xid
113 *	Create a mapping between this XID and the transaction
114 *	"td" in the shared region.
115 *
116 * PUBLIC: int __db_map_xid __P((ENV *, XID *, TXN_DETAIL *));
117 */
118int
119__db_map_xid(env, xid, td)
120	ENV *env;
121	XID *xid;
122	TXN_DETAIL *td;
123{
124	TXN_SYSTEM_LOCK(env);
125	memcpy(td->xid, xid->data, XIDDATASIZE);
126	td->bqual = (u_int32_t)xid->bqual_length;
127	td->gtrid = (u_int32_t)xid->gtrid_length;
128	td->format = (int32_t)xid->formatID;
129	TXN_SYSTEM_UNLOCK(env);
130
131	return (0);
132}
133
134/*
135 * __db_unmap_xid
136 *	Destroy the mapping for the specified XID.
137 *
138 * PUBLIC: void __db_unmap_xid __P((ENV *, XID *, size_t));
139 */
140
141void
142__db_unmap_xid(env, xid, off)
143	ENV *env;
144	XID *xid;
145	size_t off;
146{
147	TXN_DETAIL *td;
148
149	COMPQUIET(xid, NULL);
150
151	td = R_ADDR(&env->tx_handle->reginfo, off);
152	memset(td->xid, 0, sizeof(td->xid));
153}
154