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[] = "@(#)db_overflow.c	10.21 (Sleepycat) 9/27/98";
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 "db_am.h"
63*7c478bd9Sstevel@tonic-gate #include "common_ext.h"
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * Big key/data code.
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  * Big key and data entries are stored on linked lists of pages.  The initial
69*7c478bd9Sstevel@tonic-gate  * reference is a structure with the total length of the item and the page
70*7c478bd9Sstevel@tonic-gate  * number where it begins.  Each entry in the linked list contains a pointer
71*7c478bd9Sstevel@tonic-gate  * to the next page of data, and so on.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * __db_goff --
76*7c478bd9Sstevel@tonic-gate  *	Get an offpage item.
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_goff __P((DB *, DBT *,
79*7c478bd9Sstevel@tonic-gate  * PUBLIC:     u_int32_t, db_pgno_t, void **, u_int32_t *));
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate int
__db_goff(dbp,dbt,tlen,pgno,bpp,bpsz)82*7c478bd9Sstevel@tonic-gate __db_goff(dbp, dbt, tlen, pgno, bpp, bpsz)
83*7c478bd9Sstevel@tonic-gate 	DB *dbp;
84*7c478bd9Sstevel@tonic-gate 	DBT *dbt;
85*7c478bd9Sstevel@tonic-gate 	u_int32_t tlen;
86*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
87*7c478bd9Sstevel@tonic-gate 	void **bpp;
88*7c478bd9Sstevel@tonic-gate 	u_int32_t *bpsz;
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	PAGE *h;
91*7c478bd9Sstevel@tonic-gate 	db_indx_t bytes;
92*7c478bd9Sstevel@tonic-gate 	u_int32_t curoff, needed, start;
93*7c478bd9Sstevel@tonic-gate 	u_int8_t *p, *src;
94*7c478bd9Sstevel@tonic-gate 	int ret;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/*
97*7c478bd9Sstevel@tonic-gate 	 * Check if the buffer is big enough; if it is not and we are
98*7c478bd9Sstevel@tonic-gate 	 * allowed to malloc space, then we'll malloc it.  If we are
99*7c478bd9Sstevel@tonic-gate 	 * not (DB_DBT_USERMEM), then we'll set the dbt and return
100*7c478bd9Sstevel@tonic-gate 	 * appropriately.
101*7c478bd9Sstevel@tonic-gate 	 */
102*7c478bd9Sstevel@tonic-gate 	if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
103*7c478bd9Sstevel@tonic-gate 		start = dbt->doff;
104*7c478bd9Sstevel@tonic-gate 		needed = dbt->dlen;
105*7c478bd9Sstevel@tonic-gate 	} else {
106*7c478bd9Sstevel@tonic-gate 		start = 0;
107*7c478bd9Sstevel@tonic-gate 		needed = tlen;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/* Allocate any necessary memory. */
111*7c478bd9Sstevel@tonic-gate 	if (F_ISSET(dbt, DB_DBT_USERMEM)) {
112*7c478bd9Sstevel@tonic-gate 		if (needed > dbt->ulen) {
113*7c478bd9Sstevel@tonic-gate 			dbt->size = needed;
114*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 	} else if (F_ISSET(dbt, DB_DBT_MALLOC)) {
117*7c478bd9Sstevel@tonic-gate 		if ((ret =
118*7c478bd9Sstevel@tonic-gate 		    __os_malloc(needed, dbp->db_malloc, &dbt->data)) != 0)
119*7c478bd9Sstevel@tonic-gate 			return (ret);
120*7c478bd9Sstevel@tonic-gate 	} else if (*bpsz == 0 || *bpsz < needed) {
121*7c478bd9Sstevel@tonic-gate 		if ((ret = __os_realloc(bpp, needed)) != 0)
122*7c478bd9Sstevel@tonic-gate 			return (ret);
123*7c478bd9Sstevel@tonic-gate 		*bpsz = needed;
124*7c478bd9Sstevel@tonic-gate 		dbt->data = *bpp;
125*7c478bd9Sstevel@tonic-gate 	} else
126*7c478bd9Sstevel@tonic-gate 		dbt->data = *bpp;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	/*
129*7c478bd9Sstevel@tonic-gate 	 * Step through the linked list of pages, copying the data on each
130*7c478bd9Sstevel@tonic-gate 	 * one into the buffer.  Never copy more than the total data length.
131*7c478bd9Sstevel@tonic-gate 	 */
132*7c478bd9Sstevel@tonic-gate 	dbt->size = needed;
133*7c478bd9Sstevel@tonic-gate 	for (curoff = 0, p = dbt->data; pgno != P_INVALID && needed > 0;) {
134*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
135*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pgno);
136*7c478bd9Sstevel@tonic-gate 			return (ret);
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 		/* Check if we need any bytes from this page. */
139*7c478bd9Sstevel@tonic-gate 		if (curoff + OV_LEN(h) >= start) {
140*7c478bd9Sstevel@tonic-gate 			src = (u_int8_t *)h + P_OVERHEAD;
141*7c478bd9Sstevel@tonic-gate 			bytes = OV_LEN(h);
142*7c478bd9Sstevel@tonic-gate 			if (start > curoff) {
143*7c478bd9Sstevel@tonic-gate 				src += start - curoff;
144*7c478bd9Sstevel@tonic-gate 				bytes -= start - curoff;
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 			if (bytes > needed)
147*7c478bd9Sstevel@tonic-gate 				bytes = needed;
148*7c478bd9Sstevel@tonic-gate 			memcpy(p, src, bytes);
149*7c478bd9Sstevel@tonic-gate 			p += bytes;
150*7c478bd9Sstevel@tonic-gate 			needed -= bytes;
151*7c478bd9Sstevel@tonic-gate 		}
152*7c478bd9Sstevel@tonic-gate 		curoff += OV_LEN(h);
153*7c478bd9Sstevel@tonic-gate 		pgno = h->next_pgno;
154*7c478bd9Sstevel@tonic-gate 		memp_fput(dbp->mpf, h, 0);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	return (0);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * __db_poff --
161*7c478bd9Sstevel@tonic-gate  *	Put an offpage item.
162*7c478bd9Sstevel@tonic-gate  *
163*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_poff __P((DBC *, const DBT *, db_pgno_t *,
164*7c478bd9Sstevel@tonic-gate  * PUBLIC:     int (*)(DBC *, u_int32_t, PAGE **)));
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate int
__db_poff(dbc,dbt,pgnop,newfunc)167*7c478bd9Sstevel@tonic-gate __db_poff(dbc, dbt, pgnop, newfunc)
168*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
169*7c478bd9Sstevel@tonic-gate 	const DBT *dbt;
170*7c478bd9Sstevel@tonic-gate 	db_pgno_t *pgnop;
171*7c478bd9Sstevel@tonic-gate 	int (*newfunc) __P((DBC *, u_int32_t, PAGE **));
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	DB *dbp;
174*7c478bd9Sstevel@tonic-gate 	PAGE *pagep, *lastp;
175*7c478bd9Sstevel@tonic-gate 	DB_LSN new_lsn, null_lsn;
176*7c478bd9Sstevel@tonic-gate 	DBT tmp_dbt;
177*7c478bd9Sstevel@tonic-gate 	db_indx_t pagespace;
178*7c478bd9Sstevel@tonic-gate 	u_int32_t sz;
179*7c478bd9Sstevel@tonic-gate 	u_int8_t *p;
180*7c478bd9Sstevel@tonic-gate 	int ret;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	/*
183*7c478bd9Sstevel@tonic-gate 	 * Allocate pages and copy the key/data item into them.  Calculate the
184*7c478bd9Sstevel@tonic-gate 	 * number of bytes we get for pages we fill completely with a single
185*7c478bd9Sstevel@tonic-gate 	 * item.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
188*7c478bd9Sstevel@tonic-gate 	pagespace = P_MAXSPACE(dbp->pgsize);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	lastp = NULL;
191*7c478bd9Sstevel@tonic-gate 	for (p = dbt->data,
192*7c478bd9Sstevel@tonic-gate 	    sz = dbt->size; sz > 0; p += pagespace, sz -= pagespace) {
193*7c478bd9Sstevel@tonic-gate 		/*
194*7c478bd9Sstevel@tonic-gate 		 * Reduce pagespace so we terminate the loop correctly and
195*7c478bd9Sstevel@tonic-gate 		 * don't copy too much data.
196*7c478bd9Sstevel@tonic-gate 		 */
197*7c478bd9Sstevel@tonic-gate 		if (sz < pagespace)
198*7c478bd9Sstevel@tonic-gate 			pagespace = sz;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		/*
201*7c478bd9Sstevel@tonic-gate 		 * Allocate and initialize a new page and copy all or part of
202*7c478bd9Sstevel@tonic-gate 		 * the item onto the page.  If sz is less than pagespace, we
203*7c478bd9Sstevel@tonic-gate 		 * have a partial record.
204*7c478bd9Sstevel@tonic-gate 		 */
205*7c478bd9Sstevel@tonic-gate 		if ((ret = newfunc(dbc, P_OVERFLOW, &pagep)) != 0)
206*7c478bd9Sstevel@tonic-gate 			return (ret);
207*7c478bd9Sstevel@tonic-gate 		if (DB_LOGGING(dbc)) {
208*7c478bd9Sstevel@tonic-gate 			tmp_dbt.data = p;
209*7c478bd9Sstevel@tonic-gate 			tmp_dbt.size = pagespace;
210*7c478bd9Sstevel@tonic-gate 			ZERO_LSN(null_lsn);
211*7c478bd9Sstevel@tonic-gate 			if ((ret = __db_big_log(dbp->dbenv->lg_info, dbc->txn,
212*7c478bd9Sstevel@tonic-gate 			    &new_lsn, 0, DB_ADD_BIG, dbp->log_fileid,
213*7c478bd9Sstevel@tonic-gate 			    PGNO(pagep), lastp ? PGNO(lastp) : PGNO_INVALID,
214*7c478bd9Sstevel@tonic-gate 			    PGNO_INVALID, &tmp_dbt, &LSN(pagep),
215*7c478bd9Sstevel@tonic-gate 			    lastp == NULL ? &null_lsn : &LSN(lastp),
216*7c478bd9Sstevel@tonic-gate 			    &null_lsn)) != 0)
217*7c478bd9Sstevel@tonic-gate 				return (ret);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 			/* Move lsn onto page. */
220*7c478bd9Sstevel@tonic-gate 			if (lastp)
221*7c478bd9Sstevel@tonic-gate 				LSN(lastp) = new_lsn;
222*7c478bd9Sstevel@tonic-gate 			LSN(pagep) = new_lsn;
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		P_INIT(pagep, dbp->pgsize,
226*7c478bd9Sstevel@tonic-gate 		    PGNO(pagep), PGNO_INVALID, PGNO_INVALID, 0, P_OVERFLOW);
227*7c478bd9Sstevel@tonic-gate 		OV_LEN(pagep) = pagespace;
228*7c478bd9Sstevel@tonic-gate 		OV_REF(pagep) = 1;
229*7c478bd9Sstevel@tonic-gate 		memcpy((u_int8_t *)pagep + P_OVERHEAD, p, pagespace);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 		/*
232*7c478bd9Sstevel@tonic-gate 		 * If this is the first entry, update the user's info.
233*7c478bd9Sstevel@tonic-gate 		 * Otherwise, update the entry on the last page filled
234*7c478bd9Sstevel@tonic-gate 		 * in and release that page.
235*7c478bd9Sstevel@tonic-gate 		 */
236*7c478bd9Sstevel@tonic-gate 		if (lastp == NULL)
237*7c478bd9Sstevel@tonic-gate 			*pgnop = PGNO(pagep);
238*7c478bd9Sstevel@tonic-gate 		else {
239*7c478bd9Sstevel@tonic-gate 			lastp->next_pgno = PGNO(pagep);
240*7c478bd9Sstevel@tonic-gate 			pagep->prev_pgno = PGNO(lastp);
241*7c478bd9Sstevel@tonic-gate 			(void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
242*7c478bd9Sstevel@tonic-gate 		}
243*7c478bd9Sstevel@tonic-gate 		lastp = pagep;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	(void)memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
246*7c478bd9Sstevel@tonic-gate 	return (0);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * __db_ovref --
251*7c478bd9Sstevel@tonic-gate  *	Increment/decrement the reference count on an overflow page.
252*7c478bd9Sstevel@tonic-gate  *
253*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_ovref __P((DBC *, db_pgno_t, int32_t));
254*7c478bd9Sstevel@tonic-gate  */
255*7c478bd9Sstevel@tonic-gate int
__db_ovref(dbc,pgno,adjust)256*7c478bd9Sstevel@tonic-gate __db_ovref(dbc, pgno, adjust)
257*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
258*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
259*7c478bd9Sstevel@tonic-gate 	int32_t adjust;
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	DB *dbp;
262*7c478bd9Sstevel@tonic-gate 	PAGE *h;
263*7c478bd9Sstevel@tonic-gate 	int ret;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
266*7c478bd9Sstevel@tonic-gate 	if ((ret = memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
267*7c478bd9Sstevel@tonic-gate 		(void)__db_pgerr(dbp, pgno);
268*7c478bd9Sstevel@tonic-gate 		return (ret);
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc))
272*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_ovref_log(dbp->dbenv->lg_info, dbc->txn,
273*7c478bd9Sstevel@tonic-gate 		    &LSN(h), 0, dbp->log_fileid, h->pgno, adjust,
274*7c478bd9Sstevel@tonic-gate 		    &LSN(h))) != 0)
275*7c478bd9Sstevel@tonic-gate 			return (ret);
276*7c478bd9Sstevel@tonic-gate 	OV_REF(h) += adjust;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	(void)memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
279*7c478bd9Sstevel@tonic-gate 	return (0);
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate /*
283*7c478bd9Sstevel@tonic-gate  * __db_doff --
284*7c478bd9Sstevel@tonic-gate  *	Delete an offpage chain of overflow pages.
285*7c478bd9Sstevel@tonic-gate  *
286*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_doff __P((DBC *, db_pgno_t, int (*)(DBC *, PAGE *)));
287*7c478bd9Sstevel@tonic-gate  */
288*7c478bd9Sstevel@tonic-gate int
__db_doff(dbc,pgno,freefunc)289*7c478bd9Sstevel@tonic-gate __db_doff(dbc, pgno, freefunc)
290*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
291*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
292*7c478bd9Sstevel@tonic-gate 	int (*freefunc) __P((DBC *, PAGE *));
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	DB *dbp;
295*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
296*7c478bd9Sstevel@tonic-gate 	DB_LSN null_lsn;
297*7c478bd9Sstevel@tonic-gate 	DBT tmp_dbt;
298*7c478bd9Sstevel@tonic-gate 	int ret;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
301*7c478bd9Sstevel@tonic-gate 	do {
302*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
303*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pgno);
304*7c478bd9Sstevel@tonic-gate 			return (ret);
305*7c478bd9Sstevel@tonic-gate 		}
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 		/*
308*7c478bd9Sstevel@tonic-gate 		 * If it's an overflow page and it's referenced by more than
309*7c478bd9Sstevel@tonic-gate 		 * one key/data item, decrement the reference count and return.
310*7c478bd9Sstevel@tonic-gate 		 */
311*7c478bd9Sstevel@tonic-gate 		if (TYPE(pagep) == P_OVERFLOW && OV_REF(pagep) > 1) {
312*7c478bd9Sstevel@tonic-gate 			(void)memp_fput(dbp->mpf, pagep, 0);
313*7c478bd9Sstevel@tonic-gate 			return (__db_ovref(dbc, pgno, -1));
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		if (DB_LOGGING(dbc)) {
317*7c478bd9Sstevel@tonic-gate 			tmp_dbt.data = (u_int8_t *)pagep + P_OVERHEAD;
318*7c478bd9Sstevel@tonic-gate 			tmp_dbt.size = OV_LEN(pagep);
319*7c478bd9Sstevel@tonic-gate 			ZERO_LSN(null_lsn);
320*7c478bd9Sstevel@tonic-gate 			if ((ret = __db_big_log(dbp->dbenv->lg_info, dbc->txn,
321*7c478bd9Sstevel@tonic-gate 			    &LSN(pagep), 0, DB_REM_BIG, dbp->log_fileid,
322*7c478bd9Sstevel@tonic-gate 			    PGNO(pagep), PREV_PGNO(pagep), NEXT_PGNO(pagep),
323*7c478bd9Sstevel@tonic-gate 			    &tmp_dbt, &LSN(pagep), &null_lsn, &null_lsn)) != 0)
324*7c478bd9Sstevel@tonic-gate 				return (ret);
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 		pgno = pagep->next_pgno;
327*7c478bd9Sstevel@tonic-gate 		if ((ret = freefunc(dbc, pagep)) != 0)
328*7c478bd9Sstevel@tonic-gate 			return (ret);
329*7c478bd9Sstevel@tonic-gate 	} while (pgno != PGNO_INVALID);
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	return (0);
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate /*
335*7c478bd9Sstevel@tonic-gate  * __db_moff --
336*7c478bd9Sstevel@tonic-gate  *	Match on overflow pages.
337*7c478bd9Sstevel@tonic-gate  *
338*7c478bd9Sstevel@tonic-gate  * Given a starting page number and a key, return <0, 0, >0 to indicate if the
339*7c478bd9Sstevel@tonic-gate  * key on the page is less than, equal to or greater than the key specified.
340*7c478bd9Sstevel@tonic-gate  * We optimize this by doing chunk at a time comparison unless the user has
341*7c478bd9Sstevel@tonic-gate  * specified a comparison function.  In this case, we need to materialize
342*7c478bd9Sstevel@tonic-gate  * the entire object and call their comparison routine.
343*7c478bd9Sstevel@tonic-gate  *
344*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_moff __P((DB *, const DBT *, db_pgno_t, u_int32_t,
345*7c478bd9Sstevel@tonic-gate  * PUBLIC:     int (*)(const DBT *, const DBT *), int *));
346*7c478bd9Sstevel@tonic-gate  */
347*7c478bd9Sstevel@tonic-gate int
__db_moff(dbp,dbt,pgno,tlen,cmpfunc,cmpp)348*7c478bd9Sstevel@tonic-gate __db_moff(dbp, dbt, pgno, tlen, cmpfunc, cmpp)
349*7c478bd9Sstevel@tonic-gate 	DB *dbp;
350*7c478bd9Sstevel@tonic-gate 	const DBT *dbt;
351*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
352*7c478bd9Sstevel@tonic-gate 	u_int32_t tlen;
353*7c478bd9Sstevel@tonic-gate 	int (*cmpfunc) __P((const DBT *, const DBT *)), *cmpp;
354*7c478bd9Sstevel@tonic-gate {
355*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
356*7c478bd9Sstevel@tonic-gate 	DBT local_dbt;
357*7c478bd9Sstevel@tonic-gate 	void *buf;
358*7c478bd9Sstevel@tonic-gate 	u_int32_t bufsize, cmp_bytes, key_left;
359*7c478bd9Sstevel@tonic-gate 	u_int8_t *p1, *p2;
360*7c478bd9Sstevel@tonic-gate 	int ret;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	/*
363*7c478bd9Sstevel@tonic-gate 	 * If there is a user-specified comparison function, build a
364*7c478bd9Sstevel@tonic-gate 	 * contiguous copy of the key, and call it.
365*7c478bd9Sstevel@tonic-gate 	 */
366*7c478bd9Sstevel@tonic-gate 	if (cmpfunc != NULL) {
367*7c478bd9Sstevel@tonic-gate 		memset(&local_dbt, 0, sizeof(local_dbt));
368*7c478bd9Sstevel@tonic-gate 		buf = NULL;
369*7c478bd9Sstevel@tonic-gate 		bufsize = 0;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_goff(dbp,
372*7c478bd9Sstevel@tonic-gate 		    &local_dbt, tlen, pgno, &buf, &bufsize)) != 0)
373*7c478bd9Sstevel@tonic-gate 			return (ret);
374*7c478bd9Sstevel@tonic-gate 		*cmpp = cmpfunc(&local_dbt, dbt);
375*7c478bd9Sstevel@tonic-gate 		__os_free(buf, bufsize);
376*7c478bd9Sstevel@tonic-gate 		return (0);
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	/* While there are both keys to compare. */
380*7c478bd9Sstevel@tonic-gate 	for (*cmpp = 0, p1 = dbt->data,
381*7c478bd9Sstevel@tonic-gate 	    key_left = dbt->size; key_left > 0 && pgno != PGNO_INVALID;) {
382*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0)
383*7c478bd9Sstevel@tonic-gate 			return (ret);
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 		cmp_bytes = OV_LEN(pagep) < key_left ? OV_LEN(pagep) : key_left;
386*7c478bd9Sstevel@tonic-gate 		key_left -= cmp_bytes;
387*7c478bd9Sstevel@tonic-gate 		for (p2 =
388*7c478bd9Sstevel@tonic-gate 		    (u_int8_t *)pagep + P_OVERHEAD; cmp_bytes-- > 0; ++p1, ++p2)
389*7c478bd9Sstevel@tonic-gate 			if (*p1 != *p2) {
390*7c478bd9Sstevel@tonic-gate 				*cmpp = (long)*p1 - (long)*p2;
391*7c478bd9Sstevel@tonic-gate 				break;
392*7c478bd9Sstevel@tonic-gate 			}
393*7c478bd9Sstevel@tonic-gate 		pgno = NEXT_PGNO(pagep);
394*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, pagep, 0)) != 0)
395*7c478bd9Sstevel@tonic-gate 			return (ret);
396*7c478bd9Sstevel@tonic-gate 		if (*cmpp != 0)
397*7c478bd9Sstevel@tonic-gate 			return (0);
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate 	if (key_left > 0)		/* DBT is longer than page key. */
400*7c478bd9Sstevel@tonic-gate 		*cmpp = -1;
401*7c478bd9Sstevel@tonic-gate 	else if (pgno != PGNO_INVALID)	/* DBT is shorter than page key. */
402*7c478bd9Sstevel@tonic-gate 		*cmpp = 1;
403*7c478bd9Sstevel@tonic-gate 	else
404*7c478bd9Sstevel@tonic-gate 		*cmpp = 0;
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	return (0);
407*7c478bd9Sstevel@tonic-gate }
408