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) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*7c478bd9Sstevel@tonic-gate  */
7*7c478bd9Sstevel@tonic-gate /*
8*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995, 1996
9*7c478bd9Sstevel@tonic-gate  *	Keith Bostic.  All rights reserved.
10*7c478bd9Sstevel@tonic-gate  */
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995
13*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
16*7c478bd9Sstevel@tonic-gate  * Mike Olson.
17*7c478bd9Sstevel@tonic-gate  *
18*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
19*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
20*7c478bd9Sstevel@tonic-gate  * are met:
21*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
22*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
23*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
24*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
25*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
26*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
27*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
28*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
29*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
30*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
31*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
32*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include "config.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #ifndef lint
50*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)bt_page.c	10.17 (Sleepycat) 1/3/99";
51*7c478bd9Sstevel@tonic-gate #endif /* not lint */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
54*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <errno.h>
57*7c478bd9Sstevel@tonic-gate #include <string.h>
58*7c478bd9Sstevel@tonic-gate #endif
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #include "db_int.h"
61*7c478bd9Sstevel@tonic-gate #include "db_page.h"
62*7c478bd9Sstevel@tonic-gate #include "btree.h"
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * __bam_new --
66*7c478bd9Sstevel@tonic-gate  *	Get a new page, preferably from the freelist.
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __bam_new __P((DBC *, u_int32_t, PAGE **));
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate int
__bam_new(dbc,type,pagepp)71*7c478bd9Sstevel@tonic-gate __bam_new(dbc, type, pagepp)
72*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
73*7c478bd9Sstevel@tonic-gate 	u_int32_t type;
74*7c478bd9Sstevel@tonic-gate 	PAGE **pagepp;
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	BTMETA *meta;
77*7c478bd9Sstevel@tonic-gate 	DB *dbp;
78*7c478bd9Sstevel@tonic-gate 	DB_LOCK metalock;
79*7c478bd9Sstevel@tonic-gate 	PAGE *h;
80*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
81*7c478bd9Sstevel@tonic-gate 	int ret;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
84*7c478bd9Sstevel@tonic-gate 	meta = NULL;
85*7c478bd9Sstevel@tonic-gate 	h = NULL;
86*7c478bd9Sstevel@tonic-gate 	metalock = LOCK_INVALID;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	pgno = PGNO_METADATA;
89*7c478bd9Sstevel@tonic-gate 	if ((ret = __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &metalock)) != 0)
90*7c478bd9Sstevel@tonic-gate 		goto err;
91*7c478bd9Sstevel@tonic-gate 	if ((ret = memp_fget(dbp->mpf, &pgno, 0, (PAGE **)&meta)) != 0)
92*7c478bd9Sstevel@tonic-gate 		goto err;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (meta->free == PGNO_INVALID) {
95*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, DB_MPOOL_NEW, &h)) != 0)
96*7c478bd9Sstevel@tonic-gate 			goto err;
97*7c478bd9Sstevel@tonic-gate 		ZERO_LSN(h->lsn);
98*7c478bd9Sstevel@tonic-gate 		h->pgno = pgno;
99*7c478bd9Sstevel@tonic-gate 	} else {
100*7c478bd9Sstevel@tonic-gate 		pgno = meta->free;
101*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
102*7c478bd9Sstevel@tonic-gate 			goto err;
103*7c478bd9Sstevel@tonic-gate 		meta->free = h->next_pgno;
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	/* Log the change. */
107*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
108*7c478bd9Sstevel@tonic-gate 		if ((ret = __bam_pg_alloc_log(dbp->dbenv->lg_info, dbc->txn,
109*7c478bd9Sstevel@tonic-gate 		    &meta->lsn, 0, dbp->log_fileid, &meta->lsn, &h->lsn,
110*7c478bd9Sstevel@tonic-gate 		    h->pgno, (u_int32_t)type, meta->free)) != 0)
111*7c478bd9Sstevel@tonic-gate 			goto err;
112*7c478bd9Sstevel@tonic-gate 		LSN(h) = LSN(meta);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	(void)memp_fput(dbp->mpf, (PAGE *)meta, DB_MPOOL_DIRTY);
116*7c478bd9Sstevel@tonic-gate 	(void)__BT_TLPUT(dbc, metalock);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	P_INIT(h, dbp->pgsize, h->pgno, PGNO_INVALID, PGNO_INVALID, 0, type);
119*7c478bd9Sstevel@tonic-gate 	*pagepp = h;
120*7c478bd9Sstevel@tonic-gate 	return (0);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate err:	if (h != NULL)
123*7c478bd9Sstevel@tonic-gate 		(void)memp_fput(dbp->mpf, h, 0);
124*7c478bd9Sstevel@tonic-gate 	if (meta != NULL)
125*7c478bd9Sstevel@tonic-gate 		(void)memp_fput(dbp->mpf, meta, 0);
126*7c478bd9Sstevel@tonic-gate 	if (metalock != LOCK_INVALID)
127*7c478bd9Sstevel@tonic-gate 		(void)__BT_TLPUT(dbc, metalock);
128*7c478bd9Sstevel@tonic-gate 	return (ret);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate /*
132*7c478bd9Sstevel@tonic-gate  * __bam_lput --
133*7c478bd9Sstevel@tonic-gate  *	The standard lock put call.
134*7c478bd9Sstevel@tonic-gate  *
135*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __bam_lput __P((DBC *, DB_LOCK));
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate int
__bam_lput(dbc,lock)138*7c478bd9Sstevel@tonic-gate __bam_lput(dbc, lock)
139*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
140*7c478bd9Sstevel@tonic-gate 	DB_LOCK lock;
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	return (__BT_LPUT(dbc, lock));
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate  * __bam_free --
147*7c478bd9Sstevel@tonic-gate  *	Add a page to the head of the freelist.
148*7c478bd9Sstevel@tonic-gate  *
149*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __bam_free __P((DBC *, PAGE *));
150*7c478bd9Sstevel@tonic-gate  */
151*7c478bd9Sstevel@tonic-gate int
__bam_free(dbc,h)152*7c478bd9Sstevel@tonic-gate __bam_free(dbc, h)
153*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
154*7c478bd9Sstevel@tonic-gate 	PAGE *h;
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	BTMETA *meta;
157*7c478bd9Sstevel@tonic-gate 	DB *dbp;
158*7c478bd9Sstevel@tonic-gate 	DBT ldbt;
159*7c478bd9Sstevel@tonic-gate 	DB_LOCK metalock;
160*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
161*7c478bd9Sstevel@tonic-gate 	u_int32_t dirty_flag;
162*7c478bd9Sstevel@tonic-gate 	int ret, t_ret;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	/*
167*7c478bd9Sstevel@tonic-gate 	 * Retrieve the metadata page and insert the page at the head of
168*7c478bd9Sstevel@tonic-gate 	 * the free list.  If either the lock get or page get routines
169*7c478bd9Sstevel@tonic-gate 	 * fail, then we need to put the page with which we were called
170*7c478bd9Sstevel@tonic-gate 	 * back because our caller assumes we take care of it.
171*7c478bd9Sstevel@tonic-gate 	 */
172*7c478bd9Sstevel@tonic-gate 	dirty_flag = 0;
173*7c478bd9Sstevel@tonic-gate 	pgno = PGNO_METADATA;
174*7c478bd9Sstevel@tonic-gate 	if ((ret = __bam_lget(dbc, 0, pgno, DB_LOCK_WRITE, &metalock)) != 0)
175*7c478bd9Sstevel@tonic-gate 		goto err;
176*7c478bd9Sstevel@tonic-gate 	if ((ret = memp_fget(dbp->mpf, &pgno, 0, (PAGE **)&meta)) != 0) {
177*7c478bd9Sstevel@tonic-gate 		(void)__BT_TLPUT(dbc, metalock);
178*7c478bd9Sstevel@tonic-gate 		goto err;
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/* Log the change. */
182*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
183*7c478bd9Sstevel@tonic-gate 		memset(&ldbt, 0, sizeof(ldbt));
184*7c478bd9Sstevel@tonic-gate 		ldbt.data = h;
185*7c478bd9Sstevel@tonic-gate 		ldbt.size = P_OVERHEAD;
186*7c478bd9Sstevel@tonic-gate 		if ((ret = __bam_pg_free_log(dbp->dbenv->lg_info,
187*7c478bd9Sstevel@tonic-gate 		    dbc->txn, &meta->lsn, 0, dbp->log_fileid, h->pgno,
188*7c478bd9Sstevel@tonic-gate 		    &meta->lsn, &ldbt, meta->free)) != 0) {
189*7c478bd9Sstevel@tonic-gate 			(void)memp_fput(dbp->mpf, (PAGE *)meta, 0);
190*7c478bd9Sstevel@tonic-gate 			(void)__BT_TLPUT(dbc, metalock);
191*7c478bd9Sstevel@tonic-gate 			return (ret);
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 		LSN(h) = LSN(meta);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	/*
197*7c478bd9Sstevel@tonic-gate 	 * The page should have nothing interesting on it, re-initialize it,
198*7c478bd9Sstevel@tonic-gate 	 * leaving only the page number and the LSN.
199*7c478bd9Sstevel@tonic-gate 	 */
200*7c478bd9Sstevel@tonic-gate #ifdef DIAGNOSTIC
201*7c478bd9Sstevel@tonic-gate 	{ db_pgno_t __pgno; DB_LSN __lsn;
202*7c478bd9Sstevel@tonic-gate 		__pgno = h->pgno;
203*7c478bd9Sstevel@tonic-gate 		__lsn = h->lsn;
204*7c478bd9Sstevel@tonic-gate 		memset(h, 0xdb, dbp->pgsize);
205*7c478bd9Sstevel@tonic-gate 		h->pgno = __pgno;
206*7c478bd9Sstevel@tonic-gate 		h->lsn = __lsn;
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate #endif
209*7c478bd9Sstevel@tonic-gate 	P_INIT(h, dbp->pgsize, h->pgno, PGNO_INVALID, meta->free, 0, P_INVALID);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/* Link the page on the metadata free list. */
212*7c478bd9Sstevel@tonic-gate 	meta->free = h->pgno;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	/* Discard the metadata page. */
215*7c478bd9Sstevel@tonic-gate 	ret = memp_fput(dbp->mpf, (PAGE *)meta, DB_MPOOL_DIRTY);
216*7c478bd9Sstevel@tonic-gate 	if ((t_ret = __BT_TLPUT(dbc, metalock)) != 0)
217*7c478bd9Sstevel@tonic-gate 		ret = t_ret;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	/* Discard the caller's page reference. */
220*7c478bd9Sstevel@tonic-gate 	dirty_flag = DB_MPOOL_DIRTY;
221*7c478bd9Sstevel@tonic-gate err:	if ((t_ret = memp_fput(dbp->mpf, h, dirty_flag)) != 0 && ret == 0)
222*7c478bd9Sstevel@tonic-gate 		ret = t_ret;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	/*
225*7c478bd9Sstevel@tonic-gate 	 * XXX
226*7c478bd9Sstevel@tonic-gate 	 * We have to unlock the caller's page in the caller!
227*7c478bd9Sstevel@tonic-gate 	 */
228*7c478bd9Sstevel@tonic-gate 	return (ret);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
232*7c478bd9Sstevel@tonic-gate /*
233*7c478bd9Sstevel@tonic-gate  * __bam_lt --
234*7c478bd9Sstevel@tonic-gate  *	Print out the list of locks currently held by a cursor.
235*7c478bd9Sstevel@tonic-gate  *
236*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __bam_lt __P((DBC *));
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate int
__bam_lt(dbc)239*7c478bd9Sstevel@tonic-gate __bam_lt(dbc)
240*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	DB *dbp;
243*7c478bd9Sstevel@tonic-gate 	DB_LOCKREQ req;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
246*7c478bd9Sstevel@tonic-gate 	if (F_ISSET(dbp, DB_AM_LOCKING)) {
247*7c478bd9Sstevel@tonic-gate 		req.op = DB_LOCK_DUMP;
248*7c478bd9Sstevel@tonic-gate 		lock_vec(dbp->dbenv->lk_info, dbc->locker, 0, &req, 1, NULL);
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	return (0);
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate #endif
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate /*
255*7c478bd9Sstevel@tonic-gate  * __bam_lget --
256*7c478bd9Sstevel@tonic-gate  *	The standard lock get call.
257*7c478bd9Sstevel@tonic-gate  *
258*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __bam_lget
259*7c478bd9Sstevel@tonic-gate  * PUBLIC:    __P((DBC *, int, db_pgno_t, db_lockmode_t, DB_LOCK *));
260*7c478bd9Sstevel@tonic-gate  */
261*7c478bd9Sstevel@tonic-gate int
__bam_lget(dbc,do_couple,pgno,mode,lockp)262*7c478bd9Sstevel@tonic-gate __bam_lget(dbc, do_couple, pgno, mode, lockp)
263*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
264*7c478bd9Sstevel@tonic-gate 	int do_couple;
265*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
266*7c478bd9Sstevel@tonic-gate 	db_lockmode_t mode;
267*7c478bd9Sstevel@tonic-gate 	DB_LOCK *lockp;
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	DB *dbp;
270*7c478bd9Sstevel@tonic-gate 	DB_LOCKREQ couple[2];
271*7c478bd9Sstevel@tonic-gate 	int ret;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if (!F_ISSET(dbp, DB_AM_LOCKING)) {
276*7c478bd9Sstevel@tonic-gate 		*lockp = LOCK_INVALID;
277*7c478bd9Sstevel@tonic-gate 		return (0);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	dbc->lock.pgno = pgno;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	/*
283*7c478bd9Sstevel@tonic-gate 	 * If the object not currently locked, acquire the lock and return,
284*7c478bd9Sstevel@tonic-gate 	 * otherwise, lock couple.  If we fail and it's not a system error,
285*7c478bd9Sstevel@tonic-gate 	 * convert to EAGAIN.
286*7c478bd9Sstevel@tonic-gate 	 */
287*7c478bd9Sstevel@tonic-gate 	if (do_couple) {
288*7c478bd9Sstevel@tonic-gate 		couple[0].op = DB_LOCK_GET;
289*7c478bd9Sstevel@tonic-gate 		couple[0].obj = &dbc->lock_dbt;
290*7c478bd9Sstevel@tonic-gate 		couple[0].mode = mode;
291*7c478bd9Sstevel@tonic-gate 		couple[1].op = DB_LOCK_PUT;
292*7c478bd9Sstevel@tonic-gate 		couple[1].lock = *lockp;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 		if (dbc->txn == NULL)
295*7c478bd9Sstevel@tonic-gate 			ret = lock_vec(dbp->dbenv->lk_info,
296*7c478bd9Sstevel@tonic-gate 			    dbc->locker, 0, couple, 2, NULL);
297*7c478bd9Sstevel@tonic-gate 		else
298*7c478bd9Sstevel@tonic-gate 			ret = lock_tvec(dbp->dbenv->lk_info,
299*7c478bd9Sstevel@tonic-gate 			    dbc->txn, 0, couple, 2, NULL);
300*7c478bd9Sstevel@tonic-gate 		if (ret != 0) {
301*7c478bd9Sstevel@tonic-gate 			/* If we fail, discard the lock we held. */
302*7c478bd9Sstevel@tonic-gate 			__BT_LPUT(dbc, *lockp);
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 			return (ret < 0 ? EAGAIN : ret);
305*7c478bd9Sstevel@tonic-gate 		}
306*7c478bd9Sstevel@tonic-gate 		*lockp = couple[0].lock;
307*7c478bd9Sstevel@tonic-gate 	} else {
308*7c478bd9Sstevel@tonic-gate 		if (dbc->txn == NULL)
309*7c478bd9Sstevel@tonic-gate 			ret = lock_get(dbp->dbenv->lk_info,
310*7c478bd9Sstevel@tonic-gate 			    dbc->locker, 0, &dbc->lock_dbt, mode, lockp);
311*7c478bd9Sstevel@tonic-gate 		else
312*7c478bd9Sstevel@tonic-gate 			ret = lock_tget(dbp->dbenv->lk_info,
313*7c478bd9Sstevel@tonic-gate 			    dbc->txn, 0, &dbc->lock_dbt, mode, lockp);
314*7c478bd9Sstevel@tonic-gate 		 return (ret < 0 ? EAGAIN : ret);
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 	return (0);
317*7c478bd9Sstevel@tonic-gate }
318