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) 1998
5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*7c478bd9Sstevel@tonic-gate */
7*7c478bd9Sstevel@tonic-gate
8*7c478bd9Sstevel@tonic-gate #include "config.h"
9*7c478bd9Sstevel@tonic-gate
10*7c478bd9Sstevel@tonic-gate #ifndef lint
11*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)xa_db.c 10.6 (Sleepycat) 12/19/98";
12*7c478bd9Sstevel@tonic-gate #endif /* not lint */
13*7c478bd9Sstevel@tonic-gate
14*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
15*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
16*7c478bd9Sstevel@tonic-gate
17*7c478bd9Sstevel@tonic-gate #include <errno.h>
18*7c478bd9Sstevel@tonic-gate #include <stdio.h>
19*7c478bd9Sstevel@tonic-gate #include <string.h>
20*7c478bd9Sstevel@tonic-gate #endif
21*7c478bd9Sstevel@tonic-gate
22*7c478bd9Sstevel@tonic-gate #include "db_int.h"
23*7c478bd9Sstevel@tonic-gate #include "db_page.h"
24*7c478bd9Sstevel@tonic-gate #include "xa.h"
25*7c478bd9Sstevel@tonic-gate #include "xa_ext.h"
26*7c478bd9Sstevel@tonic-gate #include "db_am.h"
27*7c478bd9Sstevel@tonic-gate #include "db_ext.h"
28*7c478bd9Sstevel@tonic-gate #include "common_ext.h"
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate static int __xa_c_close __P((DBC *));
31*7c478bd9Sstevel@tonic-gate static int __xa_c_del __P((DBC *, u_int32_t));
32*7c478bd9Sstevel@tonic-gate static int __xa_c_get __P((DBC *, DBT *, DBT *, u_int32_t));
33*7c478bd9Sstevel@tonic-gate static int __xa_c_put __P((DBC *, DBT *, DBT *, u_int32_t));
34*7c478bd9Sstevel@tonic-gate static int __xa_close __P((DB *, u_int32_t));
35*7c478bd9Sstevel@tonic-gate static int __xa_cursor __P((DB *, DB_TXN *, DBC **, u_int32_t));
36*7c478bd9Sstevel@tonic-gate static int __xa_del __P((DB *, DB_TXN *, DBT *, u_int32_t));
37*7c478bd9Sstevel@tonic-gate static int __xa_fd __P((DB *, int *));
38*7c478bd9Sstevel@tonic-gate static int __xa_get __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
39*7c478bd9Sstevel@tonic-gate static int __xa_put __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
40*7c478bd9Sstevel@tonic-gate static int __xa_stat __P((DB *, void *, void *(*)(size_t), u_int32_t));
41*7c478bd9Sstevel@tonic-gate static int __xa_sync __P((DB *, u_int32_t));
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate int
db_xa_open(fname,type,flags,mode,dbinfo,dbpp)44*7c478bd9Sstevel@tonic-gate db_xa_open(fname, type, flags, mode, dbinfo, dbpp)
45*7c478bd9Sstevel@tonic-gate const char *fname;
46*7c478bd9Sstevel@tonic-gate DBTYPE type;
47*7c478bd9Sstevel@tonic-gate u_int32_t flags;
48*7c478bd9Sstevel@tonic-gate int mode;
49*7c478bd9Sstevel@tonic-gate DB_INFO *dbinfo;
50*7c478bd9Sstevel@tonic-gate DB **dbpp;
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate DB *dbp, *real_dbp;
53*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
54*7c478bd9Sstevel@tonic-gate struct __rmname *rp;
55*7c478bd9Sstevel@tonic-gate int ret;
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate * First try to open up the underlying DB.
59*7c478bd9Sstevel@tonic-gate *
60*7c478bd9Sstevel@tonic-gate * !!!
61*7c478bd9Sstevel@tonic-gate * The dbenv argument is taken from the global list of environments.
62*7c478bd9Sstevel@tonic-gate * When the transaction manager called xa_start() (__db_xa_start()),
63*7c478bd9Sstevel@tonic-gate * the "current" DB environment was moved to the start of the list.
64*7c478bd9Sstevel@tonic-gate * However, if we were called in a tpsvrinit function (which is
65*7c478bd9Sstevel@tonic-gate * entirely plausible), then it's possible that xa_open was called
66*7c478bd9Sstevel@tonic-gate * (which simply recorded the name of the environment to open) and
67*7c478bd9Sstevel@tonic-gate * this is the next call into DB. In that case, we still have to
68*7c478bd9Sstevel@tonic-gate * open the environment.
69*7c478bd9Sstevel@tonic-gate *
70*7c478bd9Sstevel@tonic-gate * The way that we know that xa_open and nothing else was called
71*7c478bd9Sstevel@tonic-gate * is because the nameq is not NULL.
72*7c478bd9Sstevel@tonic-gate */
73*7c478bd9Sstevel@tonic-gate if ((rp = TAILQ_FIRST(&DB_GLOBAL(db_nameq))) != NULL &&
74*7c478bd9Sstevel@tonic-gate (ret = __db_rmid_to_env(rp->rmid, &dbenv, 1)) != 0)
75*7c478bd9Sstevel@tonic-gate return (ret);
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate dbenv = TAILQ_FIRST(&DB_GLOBAL(db_envq));
78*7c478bd9Sstevel@tonic-gate if ((ret = db_open(fname,
79*7c478bd9Sstevel@tonic-gate type, flags, mode, dbenv, dbinfo, &real_dbp)) != 0)
80*7c478bd9Sstevel@tonic-gate return (ret);
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate * Allocate our own DB handle, and copy the exported fields and
84*7c478bd9Sstevel@tonic-gate * function pointers into it. The internal pointer references
85*7c478bd9Sstevel@tonic-gate * the real underlying DB handle.
86*7c478bd9Sstevel@tonic-gate */
87*7c478bd9Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(DB), &dbp)) != 0) {
88*7c478bd9Sstevel@tonic-gate (void)real_dbp->close(real_dbp, 0);
89*7c478bd9Sstevel@tonic-gate return (ret);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate dbp->type = real_dbp->type;
92*7c478bd9Sstevel@tonic-gate dbp->byteswapped = real_dbp->byteswapped;
93*7c478bd9Sstevel@tonic-gate dbp->dbenv = dbenv;
94*7c478bd9Sstevel@tonic-gate dbp->internal = real_dbp;
95*7c478bd9Sstevel@tonic-gate TAILQ_INIT(&dbp->active_queue);
96*7c478bd9Sstevel@tonic-gate TAILQ_INIT(&dbp->free_queue);
97*7c478bd9Sstevel@tonic-gate dbp->close = __xa_close;
98*7c478bd9Sstevel@tonic-gate dbp->cursor = __xa_cursor;
99*7c478bd9Sstevel@tonic-gate dbp->del = __xa_del;
100*7c478bd9Sstevel@tonic-gate dbp->fd = __xa_fd;
101*7c478bd9Sstevel@tonic-gate dbp->get = __xa_get;
102*7c478bd9Sstevel@tonic-gate dbp->join = real_dbp->join;
103*7c478bd9Sstevel@tonic-gate dbp->put = __xa_put;
104*7c478bd9Sstevel@tonic-gate dbp->stat = __xa_stat;
105*7c478bd9Sstevel@tonic-gate dbp->sync = __xa_sync;
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate *dbpp = dbp;
108*7c478bd9Sstevel@tonic-gate return (0);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate static int
__xa_close(dbp,flags)112*7c478bd9Sstevel@tonic-gate __xa_close(dbp, flags)
113*7c478bd9Sstevel@tonic-gate DB *dbp;
114*7c478bd9Sstevel@tonic-gate u_int32_t flags;
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate DB *real_dbp;
117*7c478bd9Sstevel@tonic-gate DBC *dbc;
118*7c478bd9Sstevel@tonic-gate int ret;
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate /* Close any associated cursors. */
121*7c478bd9Sstevel@tonic-gate while ((dbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
122*7c478bd9Sstevel@tonic-gate (void)dbc->c_close(dbc);
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate /* Close the DB handle. */
125*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
126*7c478bd9Sstevel@tonic-gate ret = real_dbp->close(real_dbp, flags);
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate __os_free(dbp, sizeof(DB));
129*7c478bd9Sstevel@tonic-gate return (ret);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate static int
__xa_cursor(dbp,txn,dbcp,flags)133*7c478bd9Sstevel@tonic-gate __xa_cursor(dbp, txn, dbcp, flags)
134*7c478bd9Sstevel@tonic-gate DB *dbp;
135*7c478bd9Sstevel@tonic-gate DB_TXN *txn;
136*7c478bd9Sstevel@tonic-gate DBC **dbcp;
137*7c478bd9Sstevel@tonic-gate u_int32_t flags;
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate DB *real_dbp;
140*7c478bd9Sstevel@tonic-gate DBC *real_dbc, *dbc;
141*7c478bd9Sstevel@tonic-gate int ret;
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
144*7c478bd9Sstevel@tonic-gate txn = dbp->dbenv->xa_txn;
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate if ((ret = real_dbp->cursor(real_dbp, txn, &real_dbc, flags)) != 0)
147*7c478bd9Sstevel@tonic-gate return (ret);
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate * Allocate our own DBC handle, and copy the exported fields and
151*7c478bd9Sstevel@tonic-gate * function pointers into it. The internal pointer references
152*7c478bd9Sstevel@tonic-gate * the real underlying DBC handle.
153*7c478bd9Sstevel@tonic-gate */
154*7c478bd9Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(DBC), &dbc)) != 0) {
155*7c478bd9Sstevel@tonic-gate (void)real_dbc->c_close(real_dbc);
156*7c478bd9Sstevel@tonic-gate return (ret);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate dbc->dbp = dbp;
159*7c478bd9Sstevel@tonic-gate dbc->c_close = __xa_c_close;
160*7c478bd9Sstevel@tonic-gate dbc->c_del = __xa_c_del;
161*7c478bd9Sstevel@tonic-gate dbc->c_get = __xa_c_get;
162*7c478bd9Sstevel@tonic-gate dbc->c_put = __xa_c_put;
163*7c478bd9Sstevel@tonic-gate dbc->internal = real_dbc;
164*7c478bd9Sstevel@tonic-gate TAILQ_INSERT_TAIL(&dbp->active_queue, dbc, links);
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate *dbcp = dbc;
167*7c478bd9Sstevel@tonic-gate return (0);
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate
170*7c478bd9Sstevel@tonic-gate static int
__xa_fd(dbp,fdp)171*7c478bd9Sstevel@tonic-gate __xa_fd(dbp, fdp)
172*7c478bd9Sstevel@tonic-gate DB *dbp;
173*7c478bd9Sstevel@tonic-gate int *fdp;
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate DB *real_dbp;
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate COMPQUIET(fdp, NULL);
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
180*7c478bd9Sstevel@tonic-gate return (__db_eopnotsup(real_dbp->dbenv));
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate static int
__xa_del(dbp,txn,key,flags)184*7c478bd9Sstevel@tonic-gate __xa_del(dbp, txn, key, flags)
185*7c478bd9Sstevel@tonic-gate DB *dbp;
186*7c478bd9Sstevel@tonic-gate DB_TXN *txn;
187*7c478bd9Sstevel@tonic-gate DBT *key;
188*7c478bd9Sstevel@tonic-gate u_int32_t flags;
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate DB *real_dbp;
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
193*7c478bd9Sstevel@tonic-gate txn = dbp->dbenv->xa_txn;
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate return (real_dbp->del(real_dbp, txn, key, flags));
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate static int
__xa_get(dbp,txn,key,data,flags)199*7c478bd9Sstevel@tonic-gate __xa_get(dbp, txn, key, data, flags)
200*7c478bd9Sstevel@tonic-gate DB *dbp;
201*7c478bd9Sstevel@tonic-gate DB_TXN *txn;
202*7c478bd9Sstevel@tonic-gate DBT *key;
203*7c478bd9Sstevel@tonic-gate DBT *data;
204*7c478bd9Sstevel@tonic-gate u_int32_t flags;
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate DB *real_dbp;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
209*7c478bd9Sstevel@tonic-gate txn = dbp->dbenv->xa_txn;
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate return (real_dbp->get(real_dbp, txn, key, data, flags));
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate static int
__xa_put(dbp,txn,key,data,flags)215*7c478bd9Sstevel@tonic-gate __xa_put(dbp, txn, key, data, flags)
216*7c478bd9Sstevel@tonic-gate DB *dbp;
217*7c478bd9Sstevel@tonic-gate DB_TXN *txn;
218*7c478bd9Sstevel@tonic-gate DBT *key;
219*7c478bd9Sstevel@tonic-gate DBT *data;
220*7c478bd9Sstevel@tonic-gate u_int32_t flags;
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate DB *real_dbp;
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
225*7c478bd9Sstevel@tonic-gate txn = dbp->dbenv->xa_txn;
226*7c478bd9Sstevel@tonic-gate
227*7c478bd9Sstevel@tonic-gate return (real_dbp->put(real_dbp, txn, key, data, flags));
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate static int
__xa_stat(dbp,spp,db_malloc,flags)231*7c478bd9Sstevel@tonic-gate __xa_stat(dbp, spp, db_malloc, flags)
232*7c478bd9Sstevel@tonic-gate DB *dbp;
233*7c478bd9Sstevel@tonic-gate void *spp;
234*7c478bd9Sstevel@tonic-gate void *(*db_malloc) __P((size_t));
235*7c478bd9Sstevel@tonic-gate u_int32_t flags;
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate DB *real_dbp;
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
240*7c478bd9Sstevel@tonic-gate return (real_dbp->stat(real_dbp, spp, db_malloc, flags));
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate static int
__xa_sync(dbp,flags)244*7c478bd9Sstevel@tonic-gate __xa_sync(dbp, flags)
245*7c478bd9Sstevel@tonic-gate DB *dbp;
246*7c478bd9Sstevel@tonic-gate u_int32_t flags;
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate DB *real_dbp;
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate real_dbp = (DB *)dbp->internal;
251*7c478bd9Sstevel@tonic-gate return (real_dbp->sync(real_dbp, flags));
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate static int
__xa_c_close(dbc)255*7c478bd9Sstevel@tonic-gate __xa_c_close(dbc)
256*7c478bd9Sstevel@tonic-gate DBC *dbc;
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate DBC *real_dbc;
259*7c478bd9Sstevel@tonic-gate int ret;
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate real_dbc = (DBC *)dbc->internal;
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate ret = real_dbc->c_close(real_dbc);
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&dbc->dbp->active_queue, dbc, links);
266*7c478bd9Sstevel@tonic-gate __os_free(dbc, sizeof(DBC));
267*7c478bd9Sstevel@tonic-gate
268*7c478bd9Sstevel@tonic-gate return (ret);
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate
271*7c478bd9Sstevel@tonic-gate static int
__xa_c_del(dbc,flags)272*7c478bd9Sstevel@tonic-gate __xa_c_del(dbc, flags)
273*7c478bd9Sstevel@tonic-gate DBC *dbc;
274*7c478bd9Sstevel@tonic-gate u_int32_t flags;
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate DBC *real_dbc;
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate real_dbc = (DBC *)dbc->internal;
279*7c478bd9Sstevel@tonic-gate return (real_dbc->c_del(real_dbc, flags));
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate static int
__xa_c_get(dbc,key,data,flags)283*7c478bd9Sstevel@tonic-gate __xa_c_get(dbc, key, data, flags)
284*7c478bd9Sstevel@tonic-gate DBC *dbc;
285*7c478bd9Sstevel@tonic-gate DBT *key;
286*7c478bd9Sstevel@tonic-gate DBT *data;
287*7c478bd9Sstevel@tonic-gate u_int32_t flags;
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate DBC *real_dbc;
290*7c478bd9Sstevel@tonic-gate
291*7c478bd9Sstevel@tonic-gate real_dbc = (DBC *)dbc->internal;
292*7c478bd9Sstevel@tonic-gate return (real_dbc->c_get(real_dbc, key, data, flags));
293*7c478bd9Sstevel@tonic-gate }
294*7c478bd9Sstevel@tonic-gate
295*7c478bd9Sstevel@tonic-gate static int
__xa_c_put(dbc,key,data,flags)296*7c478bd9Sstevel@tonic-gate __xa_c_put(dbc, key, data, flags)
297*7c478bd9Sstevel@tonic-gate DBC *dbc;
298*7c478bd9Sstevel@tonic-gate DBT *key;
299*7c478bd9Sstevel@tonic-gate DBT *data;
300*7c478bd9Sstevel@tonic-gate u_int32_t flags;
301*7c478bd9Sstevel@tonic-gate {
302*7c478bd9Sstevel@tonic-gate DBC *real_dbc;
303*7c478bd9Sstevel@tonic-gate
304*7c478bd9Sstevel@tonic-gate real_dbc = (DBC *)dbc->internal;
305*7c478bd9Sstevel@tonic-gate return (real_dbc->c_put(real_dbc, key, data, flags));
306*7c478bd9Sstevel@tonic-gate }
307