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) 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 /* XXX Remove the global transaction and hang it off the environment. */
11*0Sstevel@tonic-gate #include "config.h"
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate #ifndef lint
14*0Sstevel@tonic-gate static const char sccsid[] = "@(#)xa.c 10.4 (Sleepycat) 10/11/98";
15*0Sstevel@tonic-gate #endif /* not lint */
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
18*0Sstevel@tonic-gate #include <sys/types.h>
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate #include <stdlib.h>
21*0Sstevel@tonic-gate #include <stdio.h>
22*0Sstevel@tonic-gate #include <string.h>
23*0Sstevel@tonic-gate #endif
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate #include "db_int.h"
26*0Sstevel@tonic-gate #include "db_page.h"
27*0Sstevel@tonic-gate #include "shqueue.h"
28*0Sstevel@tonic-gate #include "log.h"
29*0Sstevel@tonic-gate #include "txn.h"
30*0Sstevel@tonic-gate #include "db_auto.h"
31*0Sstevel@tonic-gate #include "db_ext.h"
32*0Sstevel@tonic-gate #include "db_dispatch.h"
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate static int __db_xa_close __P((char *, int, long));
35*0Sstevel@tonic-gate static int __db_xa_commit __P((XID *, int, long));
36*0Sstevel@tonic-gate static int __db_xa_complete __P((int *, int *, int, long));
37*0Sstevel@tonic-gate static int __db_xa_end __P((XID *, int, long));
38*0Sstevel@tonic-gate static int __db_xa_forget __P((XID *, int, long));
39*0Sstevel@tonic-gate static int __db_xa_open __P((char *, int, long));
40*0Sstevel@tonic-gate static int __db_xa_prepare __P((XID *, int, long));
41*0Sstevel@tonic-gate static int __db_xa_recover __P((XID *, long, int, long));
42*0Sstevel@tonic-gate static int __db_xa_rollback __P((XID *, int, long));
43*0Sstevel@tonic-gate static int __db_xa_start __P((XID *, int, long));
44*0Sstevel@tonic-gate static void __xa_txn_end __P((DB_ENV *));
45*0Sstevel@tonic-gate static void __xa_txn_init __P((DB_ENV *, TXN_DETAIL *, size_t));
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate * Possible flag values:
49*0Sstevel@tonic-gate * Dynamic registration 0 => no dynamic registration
50*0Sstevel@tonic-gate * TMREGISTER => dynamic registration
51*0Sstevel@tonic-gate * Asynchronous operation 0 => no support for asynchrony
52*0Sstevel@tonic-gate * TMUSEASYNC => async support
53*0Sstevel@tonic-gate * Migration support 0 => migration of transactions across
54*0Sstevel@tonic-gate * threads is possible
55*0Sstevel@tonic-gate * TMNOMIGRATE => no migration across threads
56*0Sstevel@tonic-gate */
57*0Sstevel@tonic-gate const struct xa_switch_t db_xa_switch = {
58*0Sstevel@tonic-gate "Berkeley DB", /* name[RMNAMESZ] */
59*0Sstevel@tonic-gate TMNOMIGRATE, /* flags */
60*0Sstevel@tonic-gate 0, /* version */
61*0Sstevel@tonic-gate __db_xa_open, /* xa_open_entry */
62*0Sstevel@tonic-gate __db_xa_close, /* xa_close_entry */
63*0Sstevel@tonic-gate __db_xa_start, /* xa_start_entry */
64*0Sstevel@tonic-gate __db_xa_end, /* xa_end_entry */
65*0Sstevel@tonic-gate __db_xa_rollback, /* xa_rollback_entry */
66*0Sstevel@tonic-gate __db_xa_prepare, /* xa_prepare_entry */
67*0Sstevel@tonic-gate __db_xa_commit, /* xa_commit_entry */
68*0Sstevel@tonic-gate __db_xa_recover, /* xa_recover_entry */
69*0Sstevel@tonic-gate __db_xa_forget, /* xa_forget_entry */
70*0Sstevel@tonic-gate __db_xa_complete /* xa_complete_entry */
71*0Sstevel@tonic-gate };
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate * __db_xa_open --
75*0Sstevel@tonic-gate * The open call in the XA protocol. The rmid field is an id number
76*0Sstevel@tonic-gate * that the TM assigned us and will pass us on every xa call. We need to
77*0Sstevel@tonic-gate * map that rmid number into a dbenv structure that we create during
78*0Sstevel@tonic-gate * initialization. Since this id number is thread specific, we do not
79*0Sstevel@tonic-gate * need to store it in shared memory. The file xa_map.c implements all
80*0Sstevel@tonic-gate * such xa->db mappings.
81*0Sstevel@tonic-gate * The xa_info field is instance specific information. We require
82*0Sstevel@tonic-gate * that the value of DB_HOME be passed in xa_info. Since xa_info is the
83*0Sstevel@tonic-gate * only thing that we get to pass to db_appinit, any config information
84*0Sstevel@tonic-gate * will have to be done via a config file instead of via the db_appinit
85*0Sstevel@tonic-gate * call.
86*0Sstevel@tonic-gate */
87*0Sstevel@tonic-gate static int
__db_xa_open(xa_info,rmid,flags)88*0Sstevel@tonic-gate __db_xa_open(xa_info, rmid, flags)
89*0Sstevel@tonic-gate char *xa_info;
90*0Sstevel@tonic-gate int rmid;
91*0Sstevel@tonic-gate long flags;
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate DB_ENV *env;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
96*0Sstevel@tonic-gate return (XAER_ASYNC);
97*0Sstevel@tonic-gate if (flags != TMNOFLAGS)
98*0Sstevel@tonic-gate return (XAER_INVAL);
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate /* Verify if we already have this environment open. */
101*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 0) == 0)
102*0Sstevel@tonic-gate return (XA_OK);
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate * Since we cannot tell whether the environment is OK or not,
106*0Sstevel@tonic-gate * we can't actually do the db_appinit in xa_open. Instead,
107*0Sstevel@tonic-gate * we save the mapping between the rmid and the xa_info. If
108*0Sstevel@tonic-gate * we next get a call to __xa_recover, we do the db_appinit
109*0Sstevel@tonic-gate * with DB_RECOVER set. If we get any other call, then we
110*0Sstevel@tonic-gate * do the db_appinit.
111*0Sstevel@tonic-gate */
112*0Sstevel@tonic-gate return (__db_map_rmid_name(rmid, xa_info));
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate /*
116*0Sstevel@tonic-gate * __db_xa_close --
117*0Sstevel@tonic-gate * The close call of the XA protocol. The only trickiness here
118*0Sstevel@tonic-gate * is that if there are any active transactions, we must fail. It is
119*0Sstevel@tonic-gate * *not* an error to call close on an environment that has already been
120*0Sstevel@tonic-gate * closed (I am interpreting that to mean it's OK to call close on an
121*0Sstevel@tonic-gate * environment that has never been opened).
122*0Sstevel@tonic-gate */
123*0Sstevel@tonic-gate static int
__db_xa_close(xa_info,rmid,flags)124*0Sstevel@tonic-gate __db_xa_close(xa_info, rmid, flags)
125*0Sstevel@tonic-gate char *xa_info;
126*0Sstevel@tonic-gate int rmid;
127*0Sstevel@tonic-gate long flags;
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate DB_ENV *env;
130*0Sstevel@tonic-gate int ret, t_ret;
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate COMPQUIET(xa_info, NULL);
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
135*0Sstevel@tonic-gate return (XAER_ASYNC);
136*0Sstevel@tonic-gate if (flags != TMNOFLAGS)
137*0Sstevel@tonic-gate return (XAER_INVAL);
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /* If the environment is closed, then we're done. */
140*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 0) != 0)
141*0Sstevel@tonic-gate return (XA_OK);
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /* Check if there are any pending transactions. */
144*0Sstevel@tonic-gate if (env->xa_txn != NULL && env->xa_txn->txnid != TXN_INVALID)
145*0Sstevel@tonic-gate return (XAER_PROTO);
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate /* Now, destroy the mapping and close the environment. */
148*0Sstevel@tonic-gate ret = __db_unmap_rmid(rmid);
149*0Sstevel@tonic-gate if ((t_ret = db_appexit(env)) != 0 && ret == 0)
150*0Sstevel@tonic-gate ret = t_ret;
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate __os_free(env, sizeof(DB_ENV));
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate return (ret == 0 ? XA_OK : XAER_RMERR);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate * __db_xa_start --
159*0Sstevel@tonic-gate * Begin a transaction for the current resource manager.
160*0Sstevel@tonic-gate */
161*0Sstevel@tonic-gate static int
__db_xa_start(xid,rmid,flags)162*0Sstevel@tonic-gate __db_xa_start(xid, rmid, flags)
163*0Sstevel@tonic-gate XID *xid;
164*0Sstevel@tonic-gate int rmid;
165*0Sstevel@tonic-gate long flags;
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate DB_ENV *env;
168*0Sstevel@tonic-gate TXN_DETAIL *td;
169*0Sstevel@tonic-gate size_t off;
170*0Sstevel@tonic-gate int is_known;
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate #define OK_FLAGS (TMJOIN | TMRESUME | TMNOWAIT | TMASYNC | TMNOFLAGS)
173*0Sstevel@tonic-gate if (LF_ISSET(~OK_FLAGS))
174*0Sstevel@tonic-gate return (XAER_INVAL);
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate if (LF_ISSET(TMJOIN) && LF_ISSET(TMRESUME))
177*0Sstevel@tonic-gate return (XAER_INVAL);
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
180*0Sstevel@tonic-gate return (XAER_ASYNC);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 1) != 0)
183*0Sstevel@tonic-gate return (XAER_PROTO);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate is_known = __db_xid_to_txn(env, xid, &off) == 0;
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate if (is_known && !LF_ISSET(TMRESUME) && !LF_ISSET(TMJOIN))
188*0Sstevel@tonic-gate return (XAER_DUPID);
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate if (!is_known && LF_ISSET(TMRESUME | TMJOIN))
191*0Sstevel@tonic-gate return (XAER_NOTA);
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate * This can't block, so we can ignore TMNOWAIT.
195*0Sstevel@tonic-gate *
196*0Sstevel@tonic-gate * Other error conditions: RMERR, RMFAIL, OUTSIDE, PROTO, RB*
197*0Sstevel@tonic-gate */
198*0Sstevel@tonic-gate if (is_known) {
199*0Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
200*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_SUSPENDED && !LF_ISSET(TMRESUME))
201*0Sstevel@tonic-gate return (XAER_PROTO);
202*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_DEADLOCKED)
203*0Sstevel@tonic-gate return (XA_RBDEADLOCK);
204*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_ABORTED)
205*0Sstevel@tonic-gate return (XA_RBOTHER);
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate /* Now, fill in the global transaction structure. */
208*0Sstevel@tonic-gate __xa_txn_init(env, td, off);
209*0Sstevel@tonic-gate td->xa_status = TXN_XA_STARTED;
210*0Sstevel@tonic-gate } else {
211*0Sstevel@tonic-gate if (__txn_xa_begin(env, env->xa_txn) != 0)
212*0Sstevel@tonic-gate return (XAER_RMERR);
213*0Sstevel@tonic-gate (void)__db_map_xid(env, xid, env->xa_txn->off);
214*0Sstevel@tonic-gate td = (TXN_DETAIL *)
215*0Sstevel@tonic-gate ((u_int8_t *)env->tx_info->region + env->xa_txn->off);
216*0Sstevel@tonic-gate td->xa_status = TXN_XA_STARTED;
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate return (XA_OK);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate * __db_xa_end --
223*0Sstevel@tonic-gate * Disassociate the current transaction from the current process.
224*0Sstevel@tonic-gate */
225*0Sstevel@tonic-gate static int
__db_xa_end(xid,rmid,flags)226*0Sstevel@tonic-gate __db_xa_end(xid, rmid, flags)
227*0Sstevel@tonic-gate XID *xid;
228*0Sstevel@tonic-gate int rmid;
229*0Sstevel@tonic-gate long flags;
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate DB_ENV *env;
232*0Sstevel@tonic-gate DB_TXN *txn;
233*0Sstevel@tonic-gate TXN_DETAIL *td;
234*0Sstevel@tonic-gate size_t off;
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate if (flags != TMNOFLAGS && !LF_ISSET(TMSUSPEND | TMSUCCESS | TMFAIL))
237*0Sstevel@tonic-gate return (XAER_INVAL);
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 0) != 0)
240*0Sstevel@tonic-gate return (XAER_PROTO);
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate if (__db_xid_to_txn(env, xid, &off) != 0)
243*0Sstevel@tonic-gate return (XAER_NOTA);
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gate txn = env->xa_txn;
246*0Sstevel@tonic-gate if (off != txn->off)
247*0Sstevel@tonic-gate return (XAER_PROTO);
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
250*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_DEADLOCKED)
251*0Sstevel@tonic-gate return (XA_RBDEADLOCK);
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if (td->status == TXN_ABORTED)
254*0Sstevel@tonic-gate return (XA_RBOTHER);
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate if (td->xa_status != TXN_XA_STARTED)
257*0Sstevel@tonic-gate return (XAER_PROTO);
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate /* Update the shared memory last_lsn field */
260*0Sstevel@tonic-gate td->last_lsn = txn->last_lsn;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate * If we ever support XA migration, we cannot keep SUSPEND/END
264*0Sstevel@tonic-gate * status in the shared region; it would have to be process local.
265*0Sstevel@tonic-gate */
266*0Sstevel@tonic-gate if (LF_ISSET(TMSUSPEND))
267*0Sstevel@tonic-gate td->xa_status = TXN_XA_SUSPENDED;
268*0Sstevel@tonic-gate else
269*0Sstevel@tonic-gate td->xa_status = TXN_XA_ENDED;
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate txn->txnid = TXN_INVALID;
272*0Sstevel@tonic-gate return (XA_OK);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate * __db_xa_prepare --
277*0Sstevel@tonic-gate * Sync the log to disk so we can guarantee recoverability.
278*0Sstevel@tonic-gate */
279*0Sstevel@tonic-gate static int
__db_xa_prepare(xid,rmid,flags)280*0Sstevel@tonic-gate __db_xa_prepare(xid, rmid, flags)
281*0Sstevel@tonic-gate XID *xid;
282*0Sstevel@tonic-gate int rmid;
283*0Sstevel@tonic-gate long flags;
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate DB_ENV *env;
286*0Sstevel@tonic-gate TXN_DETAIL *td;
287*0Sstevel@tonic-gate size_t off;
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
290*0Sstevel@tonic-gate return (XAER_ASYNC);
291*0Sstevel@tonic-gate if (flags != TMNOFLAGS)
292*0Sstevel@tonic-gate return (XAER_INVAL);
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate * We need to know if we've ever called prepare on this.
296*0Sstevel@tonic-gate * As part of the prepare, we set the xa_status field to
297*0Sstevel@tonic-gate * reflect that fact that prepare has been called, and if
298*0Sstevel@tonic-gate * it's ever called again, it's an error.
299*0Sstevel@tonic-gate */
300*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 1) != 0)
301*0Sstevel@tonic-gate return (XAER_PROTO);
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate if (__db_xid_to_txn(env, xid, &off) != 0)
304*0Sstevel@tonic-gate return (XAER_NOTA);
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_DEADLOCKED)
309*0Sstevel@tonic-gate return (XA_RBDEADLOCK);
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate if (td->xa_status != TXN_XA_ENDED && td->xa_status != TXN_XA_SUSPENDED)
312*0Sstevel@tonic-gate return (XAER_PROTO);
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate /* Now, fill in the global transaction structure. */
315*0Sstevel@tonic-gate __xa_txn_init(env, td, off);
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate if (txn_prepare(env->xa_txn) != 0)
318*0Sstevel@tonic-gate return (XAER_RMERR);
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate td->xa_status = TXN_XA_PREPARED;
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate /* No fatal value that would require an XAER_RMFAIL. */
323*0Sstevel@tonic-gate __xa_txn_end(env);
324*0Sstevel@tonic-gate return (XA_OK);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate
327*0Sstevel@tonic-gate /*
328*0Sstevel@tonic-gate * __db_xa_commit --
329*0Sstevel@tonic-gate * Commit the transaction
330*0Sstevel@tonic-gate */
331*0Sstevel@tonic-gate static int
__db_xa_commit(xid,rmid,flags)332*0Sstevel@tonic-gate __db_xa_commit(xid, rmid, flags)
333*0Sstevel@tonic-gate XID *xid;
334*0Sstevel@tonic-gate int rmid;
335*0Sstevel@tonic-gate long flags;
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate DB_ENV *env;
338*0Sstevel@tonic-gate TXN_DETAIL *td;
339*0Sstevel@tonic-gate size_t off;
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
342*0Sstevel@tonic-gate return (XAER_ASYNC);
343*0Sstevel@tonic-gate #undef OK_FLAGS
344*0Sstevel@tonic-gate #define OK_FLAGS (TMNOFLAGS | TMNOWAIT | TMONEPHASE)
345*0Sstevel@tonic-gate if (LF_ISSET(~OK_FLAGS))
346*0Sstevel@tonic-gate return (XAER_INVAL);
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate /*
349*0Sstevel@tonic-gate * We need to know if we've ever called prepare on this.
350*0Sstevel@tonic-gate * We can verify this by examining the xa_status field.
351*0Sstevel@tonic-gate */
352*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 1) != 0)
353*0Sstevel@tonic-gate return (XAER_PROTO);
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate if (__db_xid_to_txn(env, xid, &off) != 0)
356*0Sstevel@tonic-gate return (XAER_NOTA);
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_DEADLOCKED)
361*0Sstevel@tonic-gate return (XA_RBDEADLOCK);
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_ABORTED)
364*0Sstevel@tonic-gate return (XA_RBOTHER);
365*0Sstevel@tonic-gate
366*0Sstevel@tonic-gate if (LF_ISSET(TMONEPHASE) &&
367*0Sstevel@tonic-gate td->xa_status != TXN_XA_ENDED && td->xa_status != TXN_XA_SUSPENDED)
368*0Sstevel@tonic-gate return (XAER_PROTO);
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate if (!LF_ISSET(TMONEPHASE) && td->xa_status != TXN_XA_PREPARED)
371*0Sstevel@tonic-gate return (XAER_PROTO);
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate /* Now, fill in the global transaction structure. */
374*0Sstevel@tonic-gate __xa_txn_init(env, td, off);
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate if (txn_commit(env->xa_txn) != 0)
377*0Sstevel@tonic-gate return (XAER_RMERR);
378*0Sstevel@tonic-gate
379*0Sstevel@tonic-gate /* No fatal value that would require an XAER_RMFAIL. */
380*0Sstevel@tonic-gate __xa_txn_end(env);
381*0Sstevel@tonic-gate return (XA_OK);
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate /*
385*0Sstevel@tonic-gate * __db_xa_recover --
386*0Sstevel@tonic-gate * Returns a list of prepared and heuristically completed transactions.
387*0Sstevel@tonic-gate *
388*0Sstevel@tonic-gate * The return value is the number of xids placed into the xid array (less
389*0Sstevel@tonic-gate * than or equal to the count parameter). The flags are going to indicate
390*0Sstevel@tonic-gate * whether we are starting a scan or continuing one.
391*0Sstevel@tonic-gate */
392*0Sstevel@tonic-gate static int
__db_xa_recover(xids,count,rmid,flags)393*0Sstevel@tonic-gate __db_xa_recover(xids, count, rmid, flags)
394*0Sstevel@tonic-gate XID *xids;
395*0Sstevel@tonic-gate long count, flags;
396*0Sstevel@tonic-gate int rmid;
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate __txn_xa_regop_args *argp;
399*0Sstevel@tonic-gate DBT data;
400*0Sstevel@tonic-gate DB_ENV *env;
401*0Sstevel@tonic-gate DB_LOG *log;
402*0Sstevel@tonic-gate XID *xidp;
403*0Sstevel@tonic-gate char *dbhome;
404*0Sstevel@tonic-gate int err, ret;
405*0Sstevel@tonic-gate u_int32_t rectype, txnid;
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gate ret = 0;
408*0Sstevel@tonic-gate xidp = xids;
409*0Sstevel@tonic-gate
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate * If we are starting a scan, then we need to open the environment
413*0Sstevel@tonic-gate * and run recovery. This recovery puts us in a state where we can
414*0Sstevel@tonic-gate * either commit or abort any transactions that were prepared but not
415*0Sstevel@tonic-gate * yet committed. Once we've done that, we need to figure out where
416*0Sstevel@tonic-gate * to begin checking for such transactions. If we are not starting
417*0Sstevel@tonic-gate * a scan, then the environment had better have already been recovered
418*0Sstevel@tonic-gate * and we'll start from * wherever the log cursor is. Since XA apps
419*0Sstevel@tonic-gate * cannot be threaded, we don't have to worry about someone else
420*0Sstevel@tonic-gate * having moved it.
421*0Sstevel@tonic-gate */
422*0Sstevel@tonic-gate if (LF_ISSET(TMSTARTRSCAN)) {
423*0Sstevel@tonic-gate /* If the environment is open, we have a problem. */
424*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 0) == XA_OK)
425*0Sstevel@tonic-gate return (XAER_PROTO);
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(DB_ENV), &env)) != 0)
428*0Sstevel@tonic-gate return (XAER_RMERR);
429*0Sstevel@tonic-gate
430*0Sstevel@tonic-gate if (__db_rmid_to_name(rmid, &dbhome) != 0)
431*0Sstevel@tonic-gate goto err1;
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate #undef XA_FLAGS
434*0Sstevel@tonic-gate #define XA_FLAGS DB_RECOVER | \
435*0Sstevel@tonic-gate DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN
436*0Sstevel@tonic-gate if ((ret = db_appinit(dbhome, NULL, env, XA_FLAGS)) != 0)
437*0Sstevel@tonic-gate goto err1;
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate if (__db_map_rmid(rmid, env) != 0)
440*0Sstevel@tonic-gate goto err2;
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gate /* Now figure out from where to begin scan. */
443*0Sstevel@tonic-gate log = env->lg_info;
444*0Sstevel@tonic-gate if ((err = __log_findckp(log, &log->xa_first)) == DB_NOTFOUND) {
445*0Sstevel@tonic-gate /*
446*0Sstevel@tonic-gate * If there were no log files, then we have no
447*0Sstevel@tonic-gate * transactions to return, so we simply return 0.
448*0Sstevel@tonic-gate */
449*0Sstevel@tonic-gate return (0);
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate if ((err = __db_txnlist_init(&log->xa_info)) != 0)
452*0Sstevel@tonic-gate goto err3;
453*0Sstevel@tonic-gate } else {
454*0Sstevel@tonic-gate /* We had better already know about this rmid. */
455*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 0) != 0)
456*0Sstevel@tonic-gate return (XAER_PROTO);
457*0Sstevel@tonic-gate /*
458*0Sstevel@tonic-gate * If we are not starting a scan, the log cursor had
459*0Sstevel@tonic-gate * better be set.
460*0Sstevel@tonic-gate */
461*0Sstevel@tonic-gate log = env->lg_info;
462*0Sstevel@tonic-gate if (IS_ZERO_LSN(log->xa_lsn))
463*0Sstevel@tonic-gate return (XAER_PROTO);
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate /*
467*0Sstevel@tonic-gate * At this point log->xa_first contains the point in the log
468*0Sstevel@tonic-gate * to which we need to roll back. If we are starting a scan,
469*0Sstevel@tonic-gate * we'll start at the last record; if we're continuing a scan,
470*0Sstevel@tonic-gate * we'll have to start at log->xa_lsn.
471*0Sstevel@tonic-gate */
472*0Sstevel@tonic-gate
473*0Sstevel@tonic-gate memset(&data, 0, sizeof(data));
474*0Sstevel@tonic-gate for (err = log_get(log, &log->xa_lsn, &data,
475*0Sstevel@tonic-gate LF_ISSET(TMSTARTRSCAN) ? DB_LAST : DB_SET);
476*0Sstevel@tonic-gate err == 0 && log_compare(&log->xa_lsn, &log->xa_first) > 0;
477*0Sstevel@tonic-gate err = log_get(log, &log->xa_lsn, &data, DB_PREV)) {
478*0Sstevel@tonic-gate memcpy(&rectype, data.data, sizeof(rectype));
479*0Sstevel@tonic-gate
480*0Sstevel@tonic-gate /*
481*0Sstevel@tonic-gate * The only record type we care about is an DB_txn_xa_regop.
482*0Sstevel@tonic-gate * If it's a commit, we have to add it to a txnlist. If it's
483*0Sstevel@tonic-gate * a prepare, and we don't have a commit, then we return it.
484*0Sstevel@tonic-gate * We are redoing some of what's in the xa_regop_recovery
485*0Sstevel@tonic-gate * code, but we have to do it here so we can get at the xid
486*0Sstevel@tonic-gate * in the record.
487*0Sstevel@tonic-gate */
488*0Sstevel@tonic-gate if (rectype != DB_txn_xa_regop && rectype != DB_txn_regop)
489*0Sstevel@tonic-gate continue;
490*0Sstevel@tonic-gate
491*0Sstevel@tonic-gate memcpy(&txnid, (u_int8_t *)data.data + sizeof(rectype),
492*0Sstevel@tonic-gate sizeof(txnid));
493*0Sstevel@tonic-gate err = __db_txnlist_find(log->xa_info, txnid);
494*0Sstevel@tonic-gate switch (rectype) {
495*0Sstevel@tonic-gate case DB_txn_regop:
496*0Sstevel@tonic-gate if (err == DB_NOTFOUND)
497*0Sstevel@tonic-gate __db_txnlist_add(log->xa_info, txnid);
498*0Sstevel@tonic-gate err = 0;
499*0Sstevel@tonic-gate break;
500*0Sstevel@tonic-gate case DB_txn_xa_regop:
501*0Sstevel@tonic-gate /*
502*0Sstevel@tonic-gate * This transaction is commited, so we needn't read
503*0Sstevel@tonic-gate * the record and do anything.
504*0Sstevel@tonic-gate */
505*0Sstevel@tonic-gate if (err == 0)
506*0Sstevel@tonic-gate break;
507*0Sstevel@tonic-gate if ((err =
508*0Sstevel@tonic-gate __txn_xa_regop_read(data.data, &argp)) != 0) {
509*0Sstevel@tonic-gate ret = XAER_RMERR;
510*0Sstevel@tonic-gate goto out;
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate
513*0Sstevel@tonic-gate xidp->formatID = argp->formatID;
514*0Sstevel@tonic-gate xidp->gtrid_length = argp->gtrid;
515*0Sstevel@tonic-gate xidp->bqual_length = argp->bqual;
516*0Sstevel@tonic-gate memcpy(xidp->data, argp->xid.data, argp->xid.size);
517*0Sstevel@tonic-gate ret++;
518*0Sstevel@tonic-gate xidp++;
519*0Sstevel@tonic-gate __os_free(argp, sizeof(*argp));
520*0Sstevel@tonic-gate if (ret == count)
521*0Sstevel@tonic-gate goto done;
522*0Sstevel@tonic-gate break;
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate if (err != 0 && err != DB_NOTFOUND)
527*0Sstevel@tonic-gate goto out;
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate done: if (LF_ISSET(TMENDRSCAN)) {
530*0Sstevel@tonic-gate ZERO_LSN(log->xa_lsn);
531*0Sstevel@tonic-gate ZERO_LSN(log->xa_first);
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate out: __db_txnlist_end(log->xa_info);
534*0Sstevel@tonic-gate log->xa_info = NULL;
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate return (ret);
537*0Sstevel@tonic-gate
538*0Sstevel@tonic-gate err3: (void)__db_unmap_rmid(rmid);
539*0Sstevel@tonic-gate err2: (void)db_appexit(env);
540*0Sstevel@tonic-gate err1: __os_free(env, sizeof(DB_ENV));
541*0Sstevel@tonic-gate return (XAER_RMERR);
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate /*
545*0Sstevel@tonic-gate * __db_xa_rollback
546*0Sstevel@tonic-gate * Abort an XA transaction.
547*0Sstevel@tonic-gate */
548*0Sstevel@tonic-gate static int
__db_xa_rollback(xid,rmid,flags)549*0Sstevel@tonic-gate __db_xa_rollback(xid, rmid, flags)
550*0Sstevel@tonic-gate XID *xid;
551*0Sstevel@tonic-gate int rmid;
552*0Sstevel@tonic-gate long flags;
553*0Sstevel@tonic-gate {
554*0Sstevel@tonic-gate DB_ENV *env;
555*0Sstevel@tonic-gate TXN_DETAIL *td;
556*0Sstevel@tonic-gate size_t off;
557*0Sstevel@tonic-gate
558*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
559*0Sstevel@tonic-gate return (XAER_ASYNC);
560*0Sstevel@tonic-gate if (flags != TMNOFLAGS)
561*0Sstevel@tonic-gate return (XAER_INVAL);
562*0Sstevel@tonic-gate
563*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 1) != 0)
564*0Sstevel@tonic-gate return (XAER_PROTO);
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gate if (__db_xid_to_txn(env, xid, &off) != 0)
567*0Sstevel@tonic-gate return (XAER_NOTA);
568*0Sstevel@tonic-gate
569*0Sstevel@tonic-gate td = (TXN_DETAIL *)((u_int8_t *)env->tx_info->region + off);
570*0Sstevel@tonic-gate
571*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_DEADLOCKED)
572*0Sstevel@tonic-gate return (XA_RBDEADLOCK);
573*0Sstevel@tonic-gate
574*0Sstevel@tonic-gate if (td->xa_status == TXN_XA_ABORTED)
575*0Sstevel@tonic-gate return (XA_RBOTHER);
576*0Sstevel@tonic-gate
577*0Sstevel@tonic-gate if (LF_ISSET(TMONEPHASE) &&
578*0Sstevel@tonic-gate td->xa_status != TXN_XA_ENDED && td->xa_status != TXN_XA_SUSPENDED)
579*0Sstevel@tonic-gate return (XAER_PROTO);
580*0Sstevel@tonic-gate
581*0Sstevel@tonic-gate /* Now, fill in the global transaction structure. */
582*0Sstevel@tonic-gate __xa_txn_init(env, td, off);
583*0Sstevel@tonic-gate if (txn_abort(env->xa_txn) != 0)
584*0Sstevel@tonic-gate return (XAER_RMERR);
585*0Sstevel@tonic-gate
586*0Sstevel@tonic-gate /* No fatal value that would require an XAER_RMFAIL. */
587*0Sstevel@tonic-gate __xa_txn_end(env);
588*0Sstevel@tonic-gate return (XA_OK);
589*0Sstevel@tonic-gate }
590*0Sstevel@tonic-gate
591*0Sstevel@tonic-gate /*
592*0Sstevel@tonic-gate * __db_xa_forget --
593*0Sstevel@tonic-gate * Forget about an XID for a transaction that was heuristically
594*0Sstevel@tonic-gate * completed. Since we do not heuristically complete anything, I
595*0Sstevel@tonic-gate * don't think we have to do anything here, but we should make sure
596*0Sstevel@tonic-gate * that we reclaim the slots in the txnid table.
597*0Sstevel@tonic-gate */
598*0Sstevel@tonic-gate static int
__db_xa_forget(xid,rmid,flags)599*0Sstevel@tonic-gate __db_xa_forget(xid, rmid, flags)
600*0Sstevel@tonic-gate XID *xid;
601*0Sstevel@tonic-gate int rmid;
602*0Sstevel@tonic-gate long flags;
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate DB_ENV *env;
605*0Sstevel@tonic-gate size_t off;
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gate if (LF_ISSET(TMASYNC))
608*0Sstevel@tonic-gate return (XAER_ASYNC);
609*0Sstevel@tonic-gate if (flags != TMNOFLAGS)
610*0Sstevel@tonic-gate return (XAER_INVAL);
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate if (__db_rmid_to_env(rmid, &env, 1) != 0)
613*0Sstevel@tonic-gate return (XAER_PROTO);
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate /*
616*0Sstevel@tonic-gate * If mapping is gone, then we're done.
617*0Sstevel@tonic-gate */
618*0Sstevel@tonic-gate if (__db_xid_to_txn(env, xid, &off) != 0)
619*0Sstevel@tonic-gate return (XA_OK);
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate __db_unmap_xid(env, xid, off);
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gate /* No fatal value that would require an XAER_RMFAIL. */
624*0Sstevel@tonic-gate return (XA_OK);
625*0Sstevel@tonic-gate }
626*0Sstevel@tonic-gate
627*0Sstevel@tonic-gate /*
628*0Sstevel@tonic-gate * __db_xa_complete --
629*0Sstevel@tonic-gate * Used to wait for asynchronous operations to complete. Since we're
630*0Sstevel@tonic-gate * not doing asynch, this is an invalid operation.
631*0Sstevel@tonic-gate */
632*0Sstevel@tonic-gate static int
__db_xa_complete(handle,retval,rmid,flags)633*0Sstevel@tonic-gate __db_xa_complete(handle, retval, rmid, flags)
634*0Sstevel@tonic-gate int *handle, *retval, rmid;
635*0Sstevel@tonic-gate long flags;
636*0Sstevel@tonic-gate {
637*0Sstevel@tonic-gate COMPQUIET(handle, NULL);
638*0Sstevel@tonic-gate COMPQUIET(retval, NULL);
639*0Sstevel@tonic-gate COMPQUIET(rmid, 0);
640*0Sstevel@tonic-gate COMPQUIET(flags, 0);
641*0Sstevel@tonic-gate
642*0Sstevel@tonic-gate return (XAER_INVAL);
643*0Sstevel@tonic-gate }
644*0Sstevel@tonic-gate
645*0Sstevel@tonic-gate /*
646*0Sstevel@tonic-gate * __xa_txn_init --
647*0Sstevel@tonic-gate * Fill in the fields of the local transaction structure given
648*0Sstevel@tonic-gate * the detail transaction structure.
649*0Sstevel@tonic-gate */
650*0Sstevel@tonic-gate static void
__xa_txn_init(env,td,off)651*0Sstevel@tonic-gate __xa_txn_init(env, td, off)
652*0Sstevel@tonic-gate DB_ENV *env;
653*0Sstevel@tonic-gate TXN_DETAIL *td;
654*0Sstevel@tonic-gate size_t off;
655*0Sstevel@tonic-gate {
656*0Sstevel@tonic-gate DB_TXN *txn;
657*0Sstevel@tonic-gate
658*0Sstevel@tonic-gate txn = env->xa_txn;
659*0Sstevel@tonic-gate txn->mgrp = env->tx_info;
660*0Sstevel@tonic-gate txn->parent = NULL;
661*0Sstevel@tonic-gate txn->last_lsn = td->last_lsn;
662*0Sstevel@tonic-gate txn->txnid = td->txnid;
663*0Sstevel@tonic-gate txn->off = off;
664*0Sstevel@tonic-gate txn->flags = 0;
665*0Sstevel@tonic-gate }
666*0Sstevel@tonic-gate
667*0Sstevel@tonic-gate /*
668*0Sstevel@tonic-gate * __xa_txn_end --
669*0Sstevel@tonic-gate * Invalidate a transaction structure that was generated by xa_txn_init.
670*0Sstevel@tonic-gate */
671*0Sstevel@tonic-gate static void
__xa_txn_end(env)672*0Sstevel@tonic-gate __xa_txn_end(env)
673*0Sstevel@tonic-gate DB_ENV *env;
674*0Sstevel@tonic-gate {
675*0Sstevel@tonic-gate DB_TXN *txn;
676*0Sstevel@tonic-gate
677*0Sstevel@tonic-gate txn = env->xa_txn;
678*0Sstevel@tonic-gate if (txn != NULL)
679*0Sstevel@tonic-gate txn->txnid = TXN_INVALID;
680*0Sstevel@tonic-gate }
681*0Sstevel@tonic-gate
682