about summary refs log tree commit diff
path: root/db2/xa/xa_map.c
blob: d4ebbae22f777108a8a21cc954472d6f5d59ba00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998
 *	Sleepycat Software.  All rights reserved.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)xa_map.c	10.4 (Sleepycat) 10/20/98";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>
#endif

#include "db_int.h"
#include "shqueue.h"
#include "txn.h"

/*
 * This file contains all the mapping information that we need to support
 * the DB/XA interface.
 */

/*
 * __db_rmid_to_env
 *	Return the environment associated with a given XA rmid.
 *
 * PUBLIC: int __db_rmid_to_env __P((int rmid, DB_ENV **envp, int open_ok));
 */
int
__db_rmid_to_env(rmid, envp, open_ok)
	int rmid;
	DB_ENV **envp;
	int open_ok;
{
	DB_ENV *env;
	char *dbhome;

	env = TAILQ_FIRST(&DB_GLOBAL(db_envq));
	if (env != NULL && env->xa_rmid == rmid) {
		*envp = env;
		return (0);
	}

	/*
	 * When we map an rmid, move that environment to be the first one in
	 * the list of environments, so we pass the correct environment from
	 * the upcoming db_xa_open() call into db_open().
	 */
	for (; env != NULL; env = TAILQ_NEXT(env, links))
		if (env->xa_rmid == rmid) {
			TAILQ_REMOVE(&DB_GLOBAL(db_envq), env, links);
			TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
			*envp = env;
			return (0);
		}

	/*
	 * We have not found the rmid on the environment list.  If we
	 * are allowed to do an open, search for the rmid on the name
	 * list and, if we find it, then open it.
	 */
	if (!open_ok)
		return (1);

	if (__db_rmid_to_name(rmid, &dbhome) != 0)
		return (1);
#undef XA_FLAGS
#define	XA_FLAGS \
	DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN

	if (__os_calloc(1, sizeof(DB_ENV), &env) != 0)
		return (1);

	if (db_appinit(dbhome, NULL, env, XA_FLAGS) != 0) 
		goto err;

	if (__db_map_rmid(rmid, env) != 0)
		goto err1;

	__db_unmap_rmid_name(rmid);

	*envp = env;
	return (0);

err1:	(void)db_appexit(env);
err:	__os_free(env, sizeof(DB_ENV));
	return (1);
}

/*
 * __db_xid_to_txn
 *	Return the txn that corresponds to this XID.
 *
 * PUBLIC: int __db_xid_to_txn __P((DB_ENV *, XID *, size_t *));
 */
int
__db_xid_to_txn(dbenv, xid, offp)
	DB_ENV *dbenv;
	XID *xid;
	size_t *offp;
{
	DB_TXNREGION *tmr;
	struct __txn_detail *td;

	/*
	 * Search the internal active transaction table to find the
	 * matching xid.  If this is a performance hit, then we
	 * can create a hash table, but I doubt it's worth it.
	 */
	tmr = dbenv->tx_info->region;

	LOCK_TXNREGION(dbenv->tx_info);
	for (td = SH_TAILQ_FIRST(&tmr->active_txn, __txn_detail);
	    td != NULL;
	    td = SH_TAILQ_NEXT(td, links, __txn_detail))
		if (memcmp(xid->data, td->xid, XIDDATASIZE) == 0)
			break;
	UNLOCK_TXNREGION(dbenv->tx_info);

	if (td == NULL)
		return (EINVAL);

	*offp = (u_int8_t *)td - (u_int8_t *)tmr;
	return (0);
}

/*
 * __db_map_rmid
 *	Create a mapping between the specified rmid and environment.
 *
 * PUBLIC: int __db_map_rmid __P((int, DB_ENV *));
 */
int
__db_map_rmid(rmid, env)
	int rmid;
	DB_ENV *env;
{
	if (__os_calloc(1, sizeof(DB_TXN), &env->xa_txn) != 0)
		return (XAER_RMERR);
	env->xa_txn->txnid = TXN_INVALID;
	env->xa_rmid = rmid;
	TAILQ_INSERT_HEAD(&DB_GLOBAL(db_envq), env, links);
	return (XA_OK);
}

/*
 * __db_unmap_rmid
 *	Destroy the mapping for the given rmid.
 *
 * PUBLIC: int __db_unmap_rmid __P((int));
 */
int
__db_unmap_rmid(rmid)
	int rmid;
{
	DB_ENV *e;

	for (e = TAILQ_FIRST(&DB_GLOBAL(db_envq));
	    e->xa_rmid != rmid;
	    e = TAILQ_NEXT(e, links));

	if (e == NULL)
		return (EINVAL);

	TAILQ_REMOVE(&DB_GLOBAL(db_envq), e, links);
	if (e->xa_txn != NULL)
		__os_free(e->xa_txn, sizeof(DB_TXN));
	return (0);
}

/*
 * __db_map_xid
 *	Create a mapping between this XID and the transaction at
 *	"off" in the shared region.
 *
 * PUBLIC: int __db_map_xid __P((DB_ENV *, XID *, size_t));
 */
int
__db_map_xid(env, xid, off)
	DB_ENV *env;
	XID *xid;
	size_t off;
{
	DB_TXNMGR *tm;
	TXN_DETAIL *td;

	tm = env->tx_info;
	td = (TXN_DETAIL *)((u_int8_t *)tm->region + off);

	LOCK_TXNREGION(tm);
	memcpy(td->xid, xid->data, XIDDATASIZE);
	UNLOCK_TXNREGION(tm);

	return (0);
}

/*
 * __db_unmap_xid
 *	Destroy the mapping for the specified XID.
 *
 * PUBLIC: void __db_unmap_xid __P((DB_ENV *, XID *, size_t));
 */

void
__db_unmap_xid(env, xid, off)
	DB_ENV *env;
	XID *xid;
	size_t off;
{
	TXN_DETAIL *td;

	COMPQUIET(xid, NULL);

	td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
	memset(td->xid, 0, sizeof(td->xid));
}

/*
 * __db_map_rmid_name --
 * 	Create a mapping from an rmid to a name (the xa_info argument).
 * We use this during create and then at some later point when we are
 * trying to map an rmid, we might indicate that it's OK to do an open
 * in which case, we'll get the xa_info parameter from here and then
 * free it up.
 *
 * PUBLIC: int __db_map_rmid_name __P((int, char *));
 */

int
__db_map_rmid_name(rmid, dbhome)
	int rmid;
	char *dbhome;
{
	struct __rmname *entry;
	int ret;

	if ((ret = __os_malloc(sizeof(struct __rmname), NULL, &entry)) != 0)
		return (ret);

	if ((ret = __os_strdup(dbhome, &entry->dbhome)) != 0) {
		__os_free(entry, sizeof(struct __rmname));
		return (ret);
	}

	entry->rmid = rmid;

	TAILQ_INSERT_HEAD(&DB_GLOBAL(db_nameq), entry, links);
	return (0);
}

/*
 * __db_rmid_to_name --
 *	Given an rmid, return the name of the home directory for that
 * rmid.
 *
 * PUBLIC: int __db_rmid_to_name __P((int, char **));
 */
int
__db_rmid_to_name(rmid, dbhomep)
	int rmid;
	char **dbhomep;
{
	struct __rmname *np;

	for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL;
	    np = TAILQ_NEXT(np, links)) {
		if (np->rmid == rmid) {
			*dbhomep = np->dbhome;
			return (0);
		}
	}
	return (1);
}

/*
 * __db_unmap_rmid_name --
 *	Given an rmid, remove its entry from the name list.
 *
 * PUBLIC:  void __db_unmap_rmid_name __P((int));
 */
void
__db_unmap_rmid_name(rmid)
	int rmid;
{
	struct __rmname *np, *next;

	for (np = TAILQ_FIRST(&DB_GLOBAL(db_nameq)); np != NULL; np = next) {
		next = TAILQ_NEXT(np, links);
		if (np->rmid == rmid) {
			TAILQ_REMOVE(&DB_GLOBAL(db_nameq), np, links);
			__os_freestr(np->dbhome);
			__os_free(np, sizeof(struct __rmname));
			return;
		}
	}
	return;
}