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