xref: /illumos-gate/usr/src/cmd/sendmail/db/db/db_dup.c (revision 7c478bd9)
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 #include "config.h"
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #ifndef lint
11*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)db_dup.c	10.35 (Sleepycat) 12/2/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 <string.h>
19*7c478bd9Sstevel@tonic-gate #endif
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate #include "db_int.h"
22*7c478bd9Sstevel@tonic-gate #include "db_page.h"
23*7c478bd9Sstevel@tonic-gate #include "btree.h"
24*7c478bd9Sstevel@tonic-gate #include "db_am.h"
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate static int __db_addpage __P((DBC *,
27*7c478bd9Sstevel@tonic-gate     PAGE **, db_indx_t *, int (*)(DBC *, u_int32_t, PAGE **)));
28*7c478bd9Sstevel@tonic-gate static int __db_dsplit __P((DBC *,
29*7c478bd9Sstevel@tonic-gate     PAGE **, db_indx_t *, u_int32_t, int (*)(DBC *, u_int32_t, PAGE **)));
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * __db_dput --
33*7c478bd9Sstevel@tonic-gate  *	Put a duplicate item onto a duplicate page at the given index.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_dput __P((DBC *, DBT *,
36*7c478bd9Sstevel@tonic-gate  * PUBLIC:    PAGE **, db_indx_t *, int (*)(DBC *, u_int32_t, PAGE **)));
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate int
__db_dput(dbc,dbt,pp,indxp,newfunc)39*7c478bd9Sstevel@tonic-gate __db_dput(dbc, dbt, pp, indxp, newfunc)
40*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
41*7c478bd9Sstevel@tonic-gate 	DBT *dbt;
42*7c478bd9Sstevel@tonic-gate 	PAGE **pp;
43*7c478bd9Sstevel@tonic-gate 	db_indx_t *indxp;
44*7c478bd9Sstevel@tonic-gate 	int (*newfunc) __P((DBC *, u_int32_t, PAGE **));
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	BOVERFLOW bo;
47*7c478bd9Sstevel@tonic-gate 	DBT *data_dbtp, hdr_dbt, *hdr_dbtp;
48*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
49*7c478bd9Sstevel@tonic-gate 	db_indx_t size, isize;
50*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
51*7c478bd9Sstevel@tonic-gate 	int ret;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	/*
54*7c478bd9Sstevel@tonic-gate 	 * We need some access method independent threshold for when we put
55*7c478bd9Sstevel@tonic-gate 	 * a duplicate item onto an overflow page.
56*7c478bd9Sstevel@tonic-gate 	 */
57*7c478bd9Sstevel@tonic-gate 	if (dbt->size > 0.25 * dbc->dbp->pgsize) {
58*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_poff(dbc, dbt, &pgno, newfunc)) != 0)
59*7c478bd9Sstevel@tonic-gate 			return (ret);
60*7c478bd9Sstevel@tonic-gate 		UMRW(bo.unused1);
61*7c478bd9Sstevel@tonic-gate 		B_TSET(bo.type, B_OVERFLOW, 0);
62*7c478bd9Sstevel@tonic-gate 		UMRW(bo.unused2);
63*7c478bd9Sstevel@tonic-gate 		bo.tlen = dbt->size;
64*7c478bd9Sstevel@tonic-gate 		bo.pgno = pgno;
65*7c478bd9Sstevel@tonic-gate 		hdr_dbt.data = &bo;
66*7c478bd9Sstevel@tonic-gate 		hdr_dbt.size = isize = BOVERFLOW_SIZE;
67*7c478bd9Sstevel@tonic-gate 		hdr_dbtp = &hdr_dbt;
68*7c478bd9Sstevel@tonic-gate 		size = BOVERFLOW_PSIZE;
69*7c478bd9Sstevel@tonic-gate 		data_dbtp = NULL;
70*7c478bd9Sstevel@tonic-gate 	} else {
71*7c478bd9Sstevel@tonic-gate 		size = BKEYDATA_PSIZE(dbt->size);
72*7c478bd9Sstevel@tonic-gate 		isize = BKEYDATA_SIZE(dbt->size);
73*7c478bd9Sstevel@tonic-gate 		hdr_dbtp = NULL;
74*7c478bd9Sstevel@tonic-gate 		data_dbtp = dbt;
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	pagep = *pp;
78*7c478bd9Sstevel@tonic-gate 	if (size > P_FREESPACE(pagep)) {
79*7c478bd9Sstevel@tonic-gate 		if (*indxp == NUM_ENT(*pp) && NEXT_PGNO(*pp) == PGNO_INVALID)
80*7c478bd9Sstevel@tonic-gate 			ret = __db_addpage(dbc, pp, indxp, newfunc);
81*7c478bd9Sstevel@tonic-gate 		else
82*7c478bd9Sstevel@tonic-gate 			ret = __db_dsplit(dbc, pp, indxp, isize, newfunc);
83*7c478bd9Sstevel@tonic-gate 		if (ret != 0)
84*7c478bd9Sstevel@tonic-gate 			/*
85*7c478bd9Sstevel@tonic-gate 			 * XXX
86*7c478bd9Sstevel@tonic-gate 			 * Pages not returned to free list.
87*7c478bd9Sstevel@tonic-gate 			 */
88*7c478bd9Sstevel@tonic-gate 			return (ret);
89*7c478bd9Sstevel@tonic-gate 		pagep = *pp;
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * Now, pagep references the page on which to insert and indx is the
94*7c478bd9Sstevel@tonic-gate 	 * the location to insert.
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	if ((ret = __db_pitem(dbc,
97*7c478bd9Sstevel@tonic-gate 	    pagep, (u_int32_t)*indxp, isize, hdr_dbtp, data_dbtp)) != 0)
98*7c478bd9Sstevel@tonic-gate 		return (ret);
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	(void)memp_fset(dbc->dbp->mpf, pagep, DB_MPOOL_DIRTY);
101*7c478bd9Sstevel@tonic-gate 	return (0);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * __db_drem --
106*7c478bd9Sstevel@tonic-gate  *	Remove a duplicate at the given index on the given page.
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_drem __P((DBC *,
109*7c478bd9Sstevel@tonic-gate  * PUBLIC:    PAGE **, u_int32_t, int (*)(DBC *, PAGE *)));
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate int
__db_drem(dbc,pp,indx,freefunc)112*7c478bd9Sstevel@tonic-gate __db_drem(dbc, pp, indx, freefunc)
113*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
114*7c478bd9Sstevel@tonic-gate 	PAGE **pp;
115*7c478bd9Sstevel@tonic-gate 	u_int32_t indx;
116*7c478bd9Sstevel@tonic-gate 	int (*freefunc) __P((DBC *, PAGE *));
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
119*7c478bd9Sstevel@tonic-gate 	int ret;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	pagep = *pp;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	/* Check if we are freeing a big item. */
124*7c478bd9Sstevel@tonic-gate 	if (B_TYPE(GET_BKEYDATA(pagep, indx)->type) == B_OVERFLOW) {
125*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_doff(dbc,
126*7c478bd9Sstevel@tonic-gate 		    GET_BOVERFLOW(pagep, indx)->pgno, freefunc)) != 0)
127*7c478bd9Sstevel@tonic-gate 			return (ret);
128*7c478bd9Sstevel@tonic-gate 		ret = __db_ditem(dbc, pagep, indx, BOVERFLOW_SIZE);
129*7c478bd9Sstevel@tonic-gate 	} else
130*7c478bd9Sstevel@tonic-gate 		ret = __db_ditem(dbc, pagep, indx,
131*7c478bd9Sstevel@tonic-gate 		    BKEYDATA_SIZE(GET_BKEYDATA(pagep, indx)->len));
132*7c478bd9Sstevel@tonic-gate 	if (ret != 0)
133*7c478bd9Sstevel@tonic-gate 		return (ret);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (NUM_ENT(pagep) == 0) {
136*7c478bd9Sstevel@tonic-gate 		/*
137*7c478bd9Sstevel@tonic-gate 		 * If the page is emptied, then the page is freed and the pp
138*7c478bd9Sstevel@tonic-gate 		 * parameter is set to reference the next, locked page in the
139*7c478bd9Sstevel@tonic-gate 		 * duplicate chain, if one exists.  If there was no such page,
140*7c478bd9Sstevel@tonic-gate 		 * then it is set to NULL.
141*7c478bd9Sstevel@tonic-gate 		 *
142*7c478bd9Sstevel@tonic-gate 		 * !!!
143*7c478bd9Sstevel@tonic-gate 		 * __db_relink will set the dirty bit for us.
144*7c478bd9Sstevel@tonic-gate 		 */
145*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_relink(dbc, DB_REM_PAGE, pagep, pp, 0)) != 0)
146*7c478bd9Sstevel@tonic-gate 			return (ret);
147*7c478bd9Sstevel@tonic-gate 		if ((ret = freefunc(dbc, pagep)) != 0)
148*7c478bd9Sstevel@tonic-gate 			return (ret);
149*7c478bd9Sstevel@tonic-gate 	} else
150*7c478bd9Sstevel@tonic-gate 		(void)memp_fset(dbc->dbp->mpf, pagep, DB_MPOOL_DIRTY);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	return (0);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * __db_dend --
157*7c478bd9Sstevel@tonic-gate  *	Find the last page in a set of offpage duplicates.
158*7c478bd9Sstevel@tonic-gate  *
159*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_dend __P((DBC *, db_pgno_t, PAGE **));
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate int
__db_dend(dbc,pgno,pp)162*7c478bd9Sstevel@tonic-gate __db_dend(dbc, pgno, pp)
163*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
164*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
165*7c478bd9Sstevel@tonic-gate 	PAGE **pp;
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	DB *dbp;
168*7c478bd9Sstevel@tonic-gate 	PAGE *h;
169*7c478bd9Sstevel@tonic-gate 	int ret;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	/*
174*7c478bd9Sstevel@tonic-gate 	 * This implements DB_KEYLAST.  The last page is returned in pp; pgno
175*7c478bd9Sstevel@tonic-gate 	 * should be the page number of the first page of the duplicate chain.
176*7c478bd9Sstevel@tonic-gate 	 *
177*7c478bd9Sstevel@tonic-gate 	 * *pp may be non-NULL -- if given a valid page use it.
178*7c478bd9Sstevel@tonic-gate 	 */
179*7c478bd9Sstevel@tonic-gate 	if (*pp != NULL)
180*7c478bd9Sstevel@tonic-gate 		goto started;
181*7c478bd9Sstevel@tonic-gate 	for (;;) {
182*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, pp)) != 0) {
183*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pgno);
184*7c478bd9Sstevel@tonic-gate 			return (ret);
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate started:	h = *pp;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		if ((pgno = NEXT_PGNO(h)) == PGNO_INVALID)
189*7c478bd9Sstevel@tonic-gate 			break;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
192*7c478bd9Sstevel@tonic-gate 			return (ret);
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 	return (0);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate /*
198*7c478bd9Sstevel@tonic-gate  * __db_dsplit --
199*7c478bd9Sstevel@tonic-gate  *	Split a page of duplicates, calculating the split point based
200*7c478bd9Sstevel@tonic-gate  *	on an element of size "size" being added at "*indxp".
201*7c478bd9Sstevel@tonic-gate  *	On entry hp contains a pointer to the page-pointer of the original
202*7c478bd9Sstevel@tonic-gate  *	page.  On exit, it returns a pointer to the page containing "*indxp"
203*7c478bd9Sstevel@tonic-gate  *	and "indxp" has been modified to reflect the index on the new page
204*7c478bd9Sstevel@tonic-gate  *	where the element should be added.  The function returns with
205*7c478bd9Sstevel@tonic-gate  *	the page on which the insert should happen, not yet put.
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate static int
__db_dsplit(dbc,hp,indxp,size,newfunc)208*7c478bd9Sstevel@tonic-gate __db_dsplit(dbc, hp, indxp, size, newfunc)
209*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
210*7c478bd9Sstevel@tonic-gate 	PAGE **hp;
211*7c478bd9Sstevel@tonic-gate 	db_indx_t *indxp;
212*7c478bd9Sstevel@tonic-gate 	u_int32_t size;
213*7c478bd9Sstevel@tonic-gate 	int (*newfunc) __P((DBC *, u_int32_t, PAGE **));
214*7c478bd9Sstevel@tonic-gate {
215*7c478bd9Sstevel@tonic-gate 	PAGE *h, *np, *tp;
216*7c478bd9Sstevel@tonic-gate 	BKEYDATA *bk;
217*7c478bd9Sstevel@tonic-gate 	DBT page_dbt;
218*7c478bd9Sstevel@tonic-gate 	DB *dbp;
219*7c478bd9Sstevel@tonic-gate 	size_t pgsize;
220*7c478bd9Sstevel@tonic-gate 	db_indx_t halfbytes, i, indx, lastsum, nindex, oindex, s, sum;
221*7c478bd9Sstevel@tonic-gate 	int did_indx, ret, t_ret;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	h = *hp;
224*7c478bd9Sstevel@tonic-gate 	indx = *indxp;
225*7c478bd9Sstevel@tonic-gate 	ret = 0;
226*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
227*7c478bd9Sstevel@tonic-gate 	pgsize = dbp->pgsize;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	/* Create a temporary page to do compaction onto. */
230*7c478bd9Sstevel@tonic-gate 	if ((ret = __os_malloc(pgsize, NULL, &tp)) != 0)
231*7c478bd9Sstevel@tonic-gate 		return (ret);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	/* Create new page for the split. */
234*7c478bd9Sstevel@tonic-gate 	if ((ret = newfunc(dbc, P_DUPLICATE, &np)) != 0) {
235*7c478bd9Sstevel@tonic-gate 		__os_free(tp, pgsize);
236*7c478bd9Sstevel@tonic-gate 		return (ret);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	P_INIT(np, pgsize, PGNO(np), PGNO(h), NEXT_PGNO(h), 0,
240*7c478bd9Sstevel@tonic-gate 	    P_DUPLICATE);
241*7c478bd9Sstevel@tonic-gate 	P_INIT(tp, pgsize, PGNO(h), PREV_PGNO(h), PGNO(np), 0,
242*7c478bd9Sstevel@tonic-gate 	    P_DUPLICATE);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	/* Figure out the split point */
245*7c478bd9Sstevel@tonic-gate 	halfbytes = (pgsize - HOFFSET(h)) / 2;
246*7c478bd9Sstevel@tonic-gate 	did_indx = 0;
247*7c478bd9Sstevel@tonic-gate 	for (sum = 0, lastsum = 0, i = 0; i < NUM_ENT(h); i++) {
248*7c478bd9Sstevel@tonic-gate 		if (i == indx) {
249*7c478bd9Sstevel@tonic-gate 			sum += size;
250*7c478bd9Sstevel@tonic-gate 			did_indx = 1;
251*7c478bd9Sstevel@tonic-gate 			if (lastsum < halfbytes && sum >= halfbytes) {
252*7c478bd9Sstevel@tonic-gate 				/* We've crossed the halfway point. */
253*7c478bd9Sstevel@tonic-gate 				if ((db_indx_t)(halfbytes - lastsum) <
254*7c478bd9Sstevel@tonic-gate 				    (db_indx_t)(sum - halfbytes)) {
255*7c478bd9Sstevel@tonic-gate 					*hp = np;
256*7c478bd9Sstevel@tonic-gate 					*indxp = 0;
257*7c478bd9Sstevel@tonic-gate 				} else
258*7c478bd9Sstevel@tonic-gate 					*indxp = i;
259*7c478bd9Sstevel@tonic-gate 				break;
260*7c478bd9Sstevel@tonic-gate 			}
261*7c478bd9Sstevel@tonic-gate 			*indxp = i;
262*7c478bd9Sstevel@tonic-gate 			lastsum = sum;
263*7c478bd9Sstevel@tonic-gate 		}
264*7c478bd9Sstevel@tonic-gate 		if (B_TYPE(GET_BKEYDATA(h, i)->type) == B_KEYDATA)
265*7c478bd9Sstevel@tonic-gate 			sum += BKEYDATA_SIZE(GET_BKEYDATA(h, i)->len);
266*7c478bd9Sstevel@tonic-gate 		else
267*7c478bd9Sstevel@tonic-gate 			sum += BOVERFLOW_SIZE;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		if (lastsum < halfbytes && sum >= halfbytes) {
270*7c478bd9Sstevel@tonic-gate 			/* We've crossed the halfway point. */
271*7c478bd9Sstevel@tonic-gate 			if ((db_indx_t)(sum - halfbytes) <
272*7c478bd9Sstevel@tonic-gate 			    (db_indx_t)(halfbytes - lastsum))
273*7c478bd9Sstevel@tonic-gate 				i++;
274*7c478bd9Sstevel@tonic-gate 			break;
275*7c478bd9Sstevel@tonic-gate 		}
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 	/*
278*7c478bd9Sstevel@tonic-gate 	 * Check if we have set the return values of the index pointer and
279*7c478bd9Sstevel@tonic-gate 	 * page pointer.
280*7c478bd9Sstevel@tonic-gate 	 */
281*7c478bd9Sstevel@tonic-gate 	if (!did_indx) {
282*7c478bd9Sstevel@tonic-gate 		*hp = np;
283*7c478bd9Sstevel@tonic-gate 		*indxp = indx - i;
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
287*7c478bd9Sstevel@tonic-gate 		page_dbt.size = dbp->pgsize;
288*7c478bd9Sstevel@tonic-gate 		page_dbt.data = h;
289*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_split_log(dbp->dbenv->lg_info,
290*7c478bd9Sstevel@tonic-gate 		    dbc->txn, &LSN(h), 0, DB_SPLITOLD, dbp->log_fileid,
291*7c478bd9Sstevel@tonic-gate 		    PGNO(h), &page_dbt, &LSN(h))) != 0) {
292*7c478bd9Sstevel@tonic-gate 			__os_free(tp, pgsize);
293*7c478bd9Sstevel@tonic-gate 			return (ret);
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 		LSN(tp) = LSN(h);
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/*
299*7c478bd9Sstevel@tonic-gate 	 * If it's a btree, adjust the cursors.
300*7c478bd9Sstevel@tonic-gate 	 *
301*7c478bd9Sstevel@tonic-gate 	 * i is the index of the first element to move onto the new page.
302*7c478bd9Sstevel@tonic-gate 	 */
303*7c478bd9Sstevel@tonic-gate 	if (dbp->type == DB_BTREE)
304*7c478bd9Sstevel@tonic-gate 		__bam_ca_split(dbp, PGNO(h), PGNO(h), PGNO(np), i, 0);
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	for (nindex = 0, oindex = i; oindex < NUM_ENT(h); oindex++) {
307*7c478bd9Sstevel@tonic-gate 		bk = GET_BKEYDATA(h, oindex);
308*7c478bd9Sstevel@tonic-gate 		if (B_TYPE(bk->type) == B_KEYDATA)
309*7c478bd9Sstevel@tonic-gate 			s = BKEYDATA_SIZE(bk->len);
310*7c478bd9Sstevel@tonic-gate 		else
311*7c478bd9Sstevel@tonic-gate 			s = BOVERFLOW_SIZE;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 		np->inp[nindex++] = HOFFSET(np) -= s;
314*7c478bd9Sstevel@tonic-gate 		memcpy((u_int8_t *)np + HOFFSET(np), bk, s);
315*7c478bd9Sstevel@tonic-gate 		NUM_ENT(np)++;
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	/*
319*7c478bd9Sstevel@tonic-gate 	 * Now do data compaction by copying the remaining stuff onto the
320*7c478bd9Sstevel@tonic-gate 	 * temporary page and then copying it back to the real page.
321*7c478bd9Sstevel@tonic-gate 	 */
322*7c478bd9Sstevel@tonic-gate 	for (nindex = 0, oindex = 0; oindex < i; oindex++) {
323*7c478bd9Sstevel@tonic-gate 		bk = GET_BKEYDATA(h, oindex);
324*7c478bd9Sstevel@tonic-gate 		if (B_TYPE(bk->type) == B_KEYDATA)
325*7c478bd9Sstevel@tonic-gate 			s = BKEYDATA_SIZE(bk->len);
326*7c478bd9Sstevel@tonic-gate 		else
327*7c478bd9Sstevel@tonic-gate 			s = BOVERFLOW_SIZE;
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 		tp->inp[nindex++] = HOFFSET(tp) -= s;
330*7c478bd9Sstevel@tonic-gate 		memcpy((u_int8_t *)tp + HOFFSET(tp), bk, s);
331*7c478bd9Sstevel@tonic-gate 		NUM_ENT(tp)++;
332*7c478bd9Sstevel@tonic-gate 	}
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	/*
335*7c478bd9Sstevel@tonic-gate 	 * This page (the temporary) should be only half full, so we do two
336*7c478bd9Sstevel@tonic-gate 	 * memcpy's, one for the top of the page and one for the bottom of
337*7c478bd9Sstevel@tonic-gate 	 * the page.  This way we avoid copying the middle which should be
338*7c478bd9Sstevel@tonic-gate 	 * about half a page.
339*7c478bd9Sstevel@tonic-gate 	 */
340*7c478bd9Sstevel@tonic-gate 	memcpy(h, tp, LOFFSET(tp));
341*7c478bd9Sstevel@tonic-gate 	memcpy((u_int8_t *)h + HOFFSET(tp),
342*7c478bd9Sstevel@tonic-gate 	    (u_int8_t *)tp + HOFFSET(tp), pgsize - HOFFSET(tp));
343*7c478bd9Sstevel@tonic-gate 	__os_free(tp, pgsize);
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
346*7c478bd9Sstevel@tonic-gate 		/*
347*7c478bd9Sstevel@tonic-gate 		 * XXX
348*7c478bd9Sstevel@tonic-gate 		 * If either of these fails, are we leaving pages pinned?
349*7c478bd9Sstevel@tonic-gate 		 * Yes, but it seems like this happens in error case.
350*7c478bd9Sstevel@tonic-gate 		 */
351*7c478bd9Sstevel@tonic-gate 		page_dbt.size = pgsize;
352*7c478bd9Sstevel@tonic-gate 		page_dbt.data = h;
353*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_split_log(dbp->dbenv->lg_info,
354*7c478bd9Sstevel@tonic-gate 		    dbc->txn, &LSN(h), 0, DB_SPLITNEW, dbp->log_fileid,
355*7c478bd9Sstevel@tonic-gate 		    PGNO(h), &page_dbt, &LSN(h))) != 0)
356*7c478bd9Sstevel@tonic-gate 			return (ret);
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 		page_dbt.size = pgsize;
359*7c478bd9Sstevel@tonic-gate 		page_dbt.data = np;
360*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_split_log(dbp->dbenv->lg_info,
361*7c478bd9Sstevel@tonic-gate 		    dbc->txn, &LSN(np), 0, DB_SPLITNEW, dbp->log_fileid,
362*7c478bd9Sstevel@tonic-gate 		    PGNO(np),  &page_dbt, &LSN(np))) != 0)
363*7c478bd9Sstevel@tonic-gate 			return (ret);
364*7c478bd9Sstevel@tonic-gate 	}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	/*
367*7c478bd9Sstevel@tonic-gate 	 * Finally, if there was a next page after the page being
368*7c478bd9Sstevel@tonic-gate 	 * split, fix its prev pointer.
369*7c478bd9Sstevel@tonic-gate 	 */
370*7c478bd9Sstevel@tonic-gate 	if (np->next_pgno != PGNO_INVALID)
371*7c478bd9Sstevel@tonic-gate 	    ret = __db_relink(dbc, DB_ADD_PAGE, np, NULL, 1);
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	/*
374*7c478bd9Sstevel@tonic-gate 	 * Figure out if the location we're interested in is on the new
375*7c478bd9Sstevel@tonic-gate 	 * page, and if so, reset the callers' pointer.  Push the other
376*7c478bd9Sstevel@tonic-gate 	 * page back to the store.
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 	if (*hp == h)
379*7c478bd9Sstevel@tonic-gate 		t_ret = memp_fput(dbp->mpf, np, DB_MPOOL_DIRTY);
380*7c478bd9Sstevel@tonic-gate 	else
381*7c478bd9Sstevel@tonic-gate 		t_ret = memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	return (ret != 0 ? ret : t_ret);
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate /*
387*7c478bd9Sstevel@tonic-gate  * __db_ditem --
388*7c478bd9Sstevel@tonic-gate  *	Remove an item from a page.
389*7c478bd9Sstevel@tonic-gate  *
390*7c478bd9Sstevel@tonic-gate  * PUBLIC:  int __db_ditem __P((DBC *, PAGE *, u_int32_t, u_int32_t));
391*7c478bd9Sstevel@tonic-gate  */
392*7c478bd9Sstevel@tonic-gate int
__db_ditem(dbc,pagep,indx,nbytes)393*7c478bd9Sstevel@tonic-gate __db_ditem(dbc, pagep, indx, nbytes)
394*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
395*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
396*7c478bd9Sstevel@tonic-gate 	u_int32_t indx, nbytes;
397*7c478bd9Sstevel@tonic-gate {
398*7c478bd9Sstevel@tonic-gate 	DB *dbp;
399*7c478bd9Sstevel@tonic-gate 	DBT ldbt;
400*7c478bd9Sstevel@tonic-gate 	db_indx_t cnt, offset;
401*7c478bd9Sstevel@tonic-gate 	int ret;
402*7c478bd9Sstevel@tonic-gate 	u_int8_t *from;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
405*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
406*7c478bd9Sstevel@tonic-gate 		ldbt.data = P_ENTRY(pagep, indx);
407*7c478bd9Sstevel@tonic-gate 		ldbt.size = nbytes;
408*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbc->txn,
409*7c478bd9Sstevel@tonic-gate 		    &LSN(pagep), 0, DB_REM_DUP, dbp->log_fileid, PGNO(pagep),
410*7c478bd9Sstevel@tonic-gate 		    (u_int32_t)indx, nbytes, &ldbt, NULL, &LSN(pagep))) != 0)
411*7c478bd9Sstevel@tonic-gate 			return (ret);
412*7c478bd9Sstevel@tonic-gate 	}
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	/*
415*7c478bd9Sstevel@tonic-gate 	 * If there's only a single item on the page, we don't have to
416*7c478bd9Sstevel@tonic-gate 	 * work hard.
417*7c478bd9Sstevel@tonic-gate 	 */
418*7c478bd9Sstevel@tonic-gate 	if (NUM_ENT(pagep) == 1) {
419*7c478bd9Sstevel@tonic-gate 		NUM_ENT(pagep) = 0;
420*7c478bd9Sstevel@tonic-gate 		HOFFSET(pagep) = dbp->pgsize;
421*7c478bd9Sstevel@tonic-gate 		return (0);
422*7c478bd9Sstevel@tonic-gate 	}
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	/*
425*7c478bd9Sstevel@tonic-gate 	 * Pack the remaining key/data items at the end of the page.  Use
426*7c478bd9Sstevel@tonic-gate 	 * memmove(3), the regions may overlap.
427*7c478bd9Sstevel@tonic-gate 	 */
428*7c478bd9Sstevel@tonic-gate 	from = (u_int8_t *)pagep + HOFFSET(pagep);
429*7c478bd9Sstevel@tonic-gate 	memmove(from + nbytes, from, pagep->inp[indx] - HOFFSET(pagep));
430*7c478bd9Sstevel@tonic-gate 	HOFFSET(pagep) += nbytes;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	/* Adjust the indices' offsets. */
433*7c478bd9Sstevel@tonic-gate 	offset = pagep->inp[indx];
434*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < NUM_ENT(pagep); ++cnt)
435*7c478bd9Sstevel@tonic-gate 		if (pagep->inp[cnt] < offset)
436*7c478bd9Sstevel@tonic-gate 			pagep->inp[cnt] += nbytes;
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	/* Shift the indices down. */
439*7c478bd9Sstevel@tonic-gate 	--NUM_ENT(pagep);
440*7c478bd9Sstevel@tonic-gate 	if (indx != NUM_ENT(pagep))
441*7c478bd9Sstevel@tonic-gate 		memmove(&pagep->inp[indx], &pagep->inp[indx + 1],
442*7c478bd9Sstevel@tonic-gate 		    sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	/* If it's a btree, adjust the cursors. */
445*7c478bd9Sstevel@tonic-gate 	if (dbp->type == DB_BTREE)
446*7c478bd9Sstevel@tonic-gate 		__bam_ca_di(dbp, PGNO(pagep), indx, -1);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	return (0);
449*7c478bd9Sstevel@tonic-gate }
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate /*
452*7c478bd9Sstevel@tonic-gate  * __db_pitem --
453*7c478bd9Sstevel@tonic-gate  *	Put an item on a page.
454*7c478bd9Sstevel@tonic-gate  *
455*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_pitem
456*7c478bd9Sstevel@tonic-gate  * PUBLIC:     __P((DBC *, PAGE *, u_int32_t, u_int32_t, DBT *, DBT *));
457*7c478bd9Sstevel@tonic-gate  */
458*7c478bd9Sstevel@tonic-gate int
__db_pitem(dbc,pagep,indx,nbytes,hdr,data)459*7c478bd9Sstevel@tonic-gate __db_pitem(dbc, pagep, indx, nbytes, hdr, data)
460*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
461*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
462*7c478bd9Sstevel@tonic-gate 	u_int32_t indx;
463*7c478bd9Sstevel@tonic-gate 	u_int32_t nbytes;
464*7c478bd9Sstevel@tonic-gate 	DBT *hdr, *data;
465*7c478bd9Sstevel@tonic-gate {
466*7c478bd9Sstevel@tonic-gate 	DB *dbp;
467*7c478bd9Sstevel@tonic-gate 	BKEYDATA bk;
468*7c478bd9Sstevel@tonic-gate 	DBT thdr;
469*7c478bd9Sstevel@tonic-gate 	int ret;
470*7c478bd9Sstevel@tonic-gate 	u_int8_t *p;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	/*
473*7c478bd9Sstevel@tonic-gate 	 * Put a single item onto a page.  The logic figuring out where to
474*7c478bd9Sstevel@tonic-gate 	 * insert and whether it fits is handled in the caller.  All we do
475*7c478bd9Sstevel@tonic-gate 	 * here is manage the page shuffling.  We cheat a little bit in that
476*7c478bd9Sstevel@tonic-gate 	 * we don't want to copy the dbt on a normal put twice.  If hdr is
477*7c478bd9Sstevel@tonic-gate 	 * NULL, we create a BKEYDATA structure on the page, otherwise, just
478*7c478bd9Sstevel@tonic-gate 	 * copy the caller's information onto the page.
479*7c478bd9Sstevel@tonic-gate 	 *
480*7c478bd9Sstevel@tonic-gate 	 * This routine is also used to put entries onto the page where the
481*7c478bd9Sstevel@tonic-gate 	 * entry is pre-built, e.g., during recovery.  In this case, the hdr
482*7c478bd9Sstevel@tonic-gate 	 * will point to the entry, and the data argument will be NULL.
483*7c478bd9Sstevel@tonic-gate 	 *
484*7c478bd9Sstevel@tonic-gate 	 * !!!
485*7c478bd9Sstevel@tonic-gate 	 * There's a tremendous potential for off-by-one errors here, since
486*7c478bd9Sstevel@tonic-gate 	 * the passed in header sizes must be adjusted for the structure's
487*7c478bd9Sstevel@tonic-gate 	 * placeholder for the trailing variable-length data field.
488*7c478bd9Sstevel@tonic-gate 	 */
489*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
490*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc))
491*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_addrem_log(dbp->dbenv->lg_info, dbc->txn,
492*7c478bd9Sstevel@tonic-gate 		    &LSN(pagep), 0, DB_ADD_DUP, dbp->log_fileid, PGNO(pagep),
493*7c478bd9Sstevel@tonic-gate 		    (u_int32_t)indx, nbytes, hdr, data, &LSN(pagep))) != 0)
494*7c478bd9Sstevel@tonic-gate 			return (ret);
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	if (hdr == NULL) {
497*7c478bd9Sstevel@tonic-gate 		B_TSET(bk.type, B_KEYDATA, 0);
498*7c478bd9Sstevel@tonic-gate 		bk.len = data == NULL ? 0 : data->size;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		thdr.data = &bk;
501*7c478bd9Sstevel@tonic-gate 		thdr.size = SSZA(BKEYDATA, data);
502*7c478bd9Sstevel@tonic-gate 		hdr = &thdr;
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	/* Adjust the index table, then put the item on the page. */
506*7c478bd9Sstevel@tonic-gate 	if (indx != NUM_ENT(pagep))
507*7c478bd9Sstevel@tonic-gate 		memmove(&pagep->inp[indx + 1], &pagep->inp[indx],
508*7c478bd9Sstevel@tonic-gate 		    sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
509*7c478bd9Sstevel@tonic-gate 	HOFFSET(pagep) -= nbytes;
510*7c478bd9Sstevel@tonic-gate 	pagep->inp[indx] = HOFFSET(pagep);
511*7c478bd9Sstevel@tonic-gate 	++NUM_ENT(pagep);
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	p = P_ENTRY(pagep, indx);
514*7c478bd9Sstevel@tonic-gate 	memcpy(p, hdr->data, hdr->size);
515*7c478bd9Sstevel@tonic-gate 	if (data != NULL)
516*7c478bd9Sstevel@tonic-gate 		memcpy(p + hdr->size, data->data, data->size);
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	/* If it's a btree, adjust the cursors. */
519*7c478bd9Sstevel@tonic-gate 	if (dbp->type == DB_BTREE)
520*7c478bd9Sstevel@tonic-gate 		__bam_ca_di(dbp, PGNO(pagep), indx, 1);
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	return (0);
523*7c478bd9Sstevel@tonic-gate }
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate /*
526*7c478bd9Sstevel@tonic-gate  * __db_relink --
527*7c478bd9Sstevel@tonic-gate  *	Relink around a deleted page.
528*7c478bd9Sstevel@tonic-gate  *
529*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_relink __P((DBC *, u_int32_t, PAGE *, PAGE **, int));
530*7c478bd9Sstevel@tonic-gate  */
531*7c478bd9Sstevel@tonic-gate int
__db_relink(dbc,add_rem,pagep,new_next,needlock)532*7c478bd9Sstevel@tonic-gate __db_relink(dbc, add_rem, pagep, new_next, needlock)
533*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
534*7c478bd9Sstevel@tonic-gate 	u_int32_t add_rem;
535*7c478bd9Sstevel@tonic-gate 	PAGE *pagep, **new_next;
536*7c478bd9Sstevel@tonic-gate 	int needlock;
537*7c478bd9Sstevel@tonic-gate {
538*7c478bd9Sstevel@tonic-gate 	DB *dbp;
539*7c478bd9Sstevel@tonic-gate 	PAGE *np, *pp;
540*7c478bd9Sstevel@tonic-gate 	DB_LOCK npl, ppl;
541*7c478bd9Sstevel@tonic-gate 	DB_LSN *nlsnp, *plsnp;
542*7c478bd9Sstevel@tonic-gate 	int ret;
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	ret = 0;
545*7c478bd9Sstevel@tonic-gate 	np = pp = NULL;
546*7c478bd9Sstevel@tonic-gate 	npl = ppl = LOCK_INVALID;
547*7c478bd9Sstevel@tonic-gate 	nlsnp = plsnp = NULL;
548*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	/*
551*7c478bd9Sstevel@tonic-gate 	 * Retrieve and lock the one/two pages.  For a remove, we may need
552*7c478bd9Sstevel@tonic-gate 	 * two pages (the before and after).  For an add, we only need one
553*7c478bd9Sstevel@tonic-gate 	 * because, the split took care of the prev.
554*7c478bd9Sstevel@tonic-gate 	 */
555*7c478bd9Sstevel@tonic-gate 	if (pagep->next_pgno != PGNO_INVALID) {
556*7c478bd9Sstevel@tonic-gate 		if (needlock && (ret = __bam_lget(dbc,
557*7c478bd9Sstevel@tonic-gate 		    0, pagep->next_pgno, DB_LOCK_WRITE, &npl)) != 0)
558*7c478bd9Sstevel@tonic-gate 			goto err;
559*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf,
560*7c478bd9Sstevel@tonic-gate 		    &pagep->next_pgno, 0, &np)) != 0) {
561*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pagep->next_pgno);
562*7c478bd9Sstevel@tonic-gate 			goto err;
563*7c478bd9Sstevel@tonic-gate 		}
564*7c478bd9Sstevel@tonic-gate 		nlsnp = &np->lsn;
565*7c478bd9Sstevel@tonic-gate 	}
566*7c478bd9Sstevel@tonic-gate 	if (add_rem == DB_REM_PAGE && pagep->prev_pgno != PGNO_INVALID) {
567*7c478bd9Sstevel@tonic-gate 		if (needlock && (ret = __bam_lget(dbc,
568*7c478bd9Sstevel@tonic-gate 		    0, pagep->prev_pgno, DB_LOCK_WRITE, &ppl)) != 0)
569*7c478bd9Sstevel@tonic-gate 			goto err;
570*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf,
571*7c478bd9Sstevel@tonic-gate 		    &pagep->prev_pgno, 0, &pp)) != 0) {
572*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pagep->next_pgno);
573*7c478bd9Sstevel@tonic-gate 			goto err;
574*7c478bd9Sstevel@tonic-gate 		}
575*7c478bd9Sstevel@tonic-gate 		plsnp = &pp->lsn;
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 	/* Log the change. */
579*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
580*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_relink_log(dbp->dbenv->lg_info, dbc->txn,
581*7c478bd9Sstevel@tonic-gate 		    &pagep->lsn, 0, add_rem, dbp->log_fileid,
582*7c478bd9Sstevel@tonic-gate 		    pagep->pgno, &pagep->lsn,
583*7c478bd9Sstevel@tonic-gate 		    pagep->prev_pgno, plsnp, pagep->next_pgno, nlsnp)) != 0)
584*7c478bd9Sstevel@tonic-gate 			goto err;
585*7c478bd9Sstevel@tonic-gate 		if (np != NULL)
586*7c478bd9Sstevel@tonic-gate 			np->lsn = pagep->lsn;
587*7c478bd9Sstevel@tonic-gate 		if (pp != NULL)
588*7c478bd9Sstevel@tonic-gate 			pp->lsn = pagep->lsn;
589*7c478bd9Sstevel@tonic-gate 	}
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	/*
592*7c478bd9Sstevel@tonic-gate 	 * Modify and release the two pages.
593*7c478bd9Sstevel@tonic-gate 	 *
594*7c478bd9Sstevel@tonic-gate 	 * !!!
595*7c478bd9Sstevel@tonic-gate 	 * The parameter new_next gets set to the page following the page we
596*7c478bd9Sstevel@tonic-gate 	 * are removing.  If there is no following page, then new_next gets
597*7c478bd9Sstevel@tonic-gate 	 * set to NULL.
598*7c478bd9Sstevel@tonic-gate 	 */
599*7c478bd9Sstevel@tonic-gate 	if (np != NULL) {
600*7c478bd9Sstevel@tonic-gate 		if (add_rem == DB_ADD_PAGE)
601*7c478bd9Sstevel@tonic-gate 			np->prev_pgno = pagep->pgno;
602*7c478bd9Sstevel@tonic-gate 		else
603*7c478bd9Sstevel@tonic-gate 			np->prev_pgno = pagep->prev_pgno;
604*7c478bd9Sstevel@tonic-gate 		if (new_next == NULL)
605*7c478bd9Sstevel@tonic-gate 			ret = memp_fput(dbp->mpf, np, DB_MPOOL_DIRTY);
606*7c478bd9Sstevel@tonic-gate 		else {
607*7c478bd9Sstevel@tonic-gate 			*new_next = np;
608*7c478bd9Sstevel@tonic-gate 			ret = memp_fset(dbp->mpf, np, DB_MPOOL_DIRTY);
609*7c478bd9Sstevel@tonic-gate 		}
610*7c478bd9Sstevel@tonic-gate 		if (ret != 0)
611*7c478bd9Sstevel@tonic-gate 			goto err;
612*7c478bd9Sstevel@tonic-gate 		if (needlock)
613*7c478bd9Sstevel@tonic-gate 			(void)__bam_lput(dbc, npl);
614*7c478bd9Sstevel@tonic-gate 	} else if (new_next != NULL)
615*7c478bd9Sstevel@tonic-gate 		*new_next = NULL;
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
618*7c478bd9Sstevel@tonic-gate 		pp->next_pgno = pagep->next_pgno;
619*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, pp, DB_MPOOL_DIRTY)) != 0)
620*7c478bd9Sstevel@tonic-gate 			goto err;
621*7c478bd9Sstevel@tonic-gate 		if (needlock)
622*7c478bd9Sstevel@tonic-gate 			(void)__bam_lput(dbc, ppl);
623*7c478bd9Sstevel@tonic-gate 	}
624*7c478bd9Sstevel@tonic-gate 	return (0);
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate err:	if (np != NULL)
627*7c478bd9Sstevel@tonic-gate 		(void)memp_fput(dbp->mpf, np, 0);
628*7c478bd9Sstevel@tonic-gate 	if (needlock && npl != LOCK_INVALID)
629*7c478bd9Sstevel@tonic-gate 		(void)__bam_lput(dbc, npl);
630*7c478bd9Sstevel@tonic-gate 	if (pp != NULL)
631*7c478bd9Sstevel@tonic-gate 		(void)memp_fput(dbp->mpf, pp, 0);
632*7c478bd9Sstevel@tonic-gate 	if (needlock && ppl != LOCK_INVALID)
633*7c478bd9Sstevel@tonic-gate 		(void)__bam_lput(dbc, ppl);
634*7c478bd9Sstevel@tonic-gate 	return (ret);
635*7c478bd9Sstevel@tonic-gate }
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate /*
638*7c478bd9Sstevel@tonic-gate  * __db_ddup --
639*7c478bd9Sstevel@tonic-gate  *	Delete an offpage chain of duplicates.
640*7c478bd9Sstevel@tonic-gate  *
641*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_ddup __P((DBC *, db_pgno_t, int (*)(DBC *, PAGE *)));
642*7c478bd9Sstevel@tonic-gate  */
643*7c478bd9Sstevel@tonic-gate int
__db_ddup(dbc,pgno,freefunc)644*7c478bd9Sstevel@tonic-gate __db_ddup(dbc, pgno, freefunc)
645*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
646*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
647*7c478bd9Sstevel@tonic-gate 	int (*freefunc) __P((DBC *, PAGE *));
648*7c478bd9Sstevel@tonic-gate {
649*7c478bd9Sstevel@tonic-gate 	DB *dbp;
650*7c478bd9Sstevel@tonic-gate 	PAGE *pagep;
651*7c478bd9Sstevel@tonic-gate 	DBT tmp_dbt;
652*7c478bd9Sstevel@tonic-gate 	int ret;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
655*7c478bd9Sstevel@tonic-gate 	do {
656*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
657*7c478bd9Sstevel@tonic-gate 			(void)__db_pgerr(dbp, pgno);
658*7c478bd9Sstevel@tonic-gate 			return (ret);
659*7c478bd9Sstevel@tonic-gate 		}
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate 		if (DB_LOGGING(dbc)) {
662*7c478bd9Sstevel@tonic-gate 			tmp_dbt.data = pagep;
663*7c478bd9Sstevel@tonic-gate 			tmp_dbt.size = dbp->pgsize;
664*7c478bd9Sstevel@tonic-gate 			if ((ret = __db_split_log(dbp->dbenv->lg_info,
665*7c478bd9Sstevel@tonic-gate 			    dbc->txn, &LSN(pagep), 0, DB_SPLITOLD,
666*7c478bd9Sstevel@tonic-gate 			    dbp->log_fileid, PGNO(pagep), &tmp_dbt,
667*7c478bd9Sstevel@tonic-gate 			    &LSN(pagep))) != 0)
668*7c478bd9Sstevel@tonic-gate 				return (ret);
669*7c478bd9Sstevel@tonic-gate 		}
670*7c478bd9Sstevel@tonic-gate 		pgno = pagep->next_pgno;
671*7c478bd9Sstevel@tonic-gate 		if ((ret = freefunc(dbc, pagep)) != 0)
672*7c478bd9Sstevel@tonic-gate 			return (ret);
673*7c478bd9Sstevel@tonic-gate 	} while (pgno != PGNO_INVALID);
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 	return (0);
676*7c478bd9Sstevel@tonic-gate }
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate /*
679*7c478bd9Sstevel@tonic-gate  * __db_addpage --
680*7c478bd9Sstevel@tonic-gate  *	Create a new page and link it onto the next_pgno field of the
681*7c478bd9Sstevel@tonic-gate  *	current page.
682*7c478bd9Sstevel@tonic-gate  */
683*7c478bd9Sstevel@tonic-gate static int
__db_addpage(dbc,hp,indxp,newfunc)684*7c478bd9Sstevel@tonic-gate __db_addpage(dbc, hp, indxp, newfunc)
685*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
686*7c478bd9Sstevel@tonic-gate 	PAGE **hp;
687*7c478bd9Sstevel@tonic-gate 	db_indx_t *indxp;
688*7c478bd9Sstevel@tonic-gate 	int (*newfunc) __P((DBC *, u_int32_t, PAGE **));
689*7c478bd9Sstevel@tonic-gate {
690*7c478bd9Sstevel@tonic-gate 	DB *dbp;
691*7c478bd9Sstevel@tonic-gate 	PAGE *newpage;
692*7c478bd9Sstevel@tonic-gate 	int ret;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
695*7c478bd9Sstevel@tonic-gate 	if ((ret = newfunc(dbc, P_DUPLICATE, &newpage)) != 0)
696*7c478bd9Sstevel@tonic-gate 		return (ret);
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	if (DB_LOGGING(dbc)) {
699*7c478bd9Sstevel@tonic-gate 		if ((ret = __db_addpage_log(dbp->dbenv->lg_info,
700*7c478bd9Sstevel@tonic-gate 		    dbc->txn, &LSN(*hp), 0, dbp->log_fileid,
701*7c478bd9Sstevel@tonic-gate 		    PGNO(*hp), &LSN(*hp), PGNO(newpage), &LSN(newpage))) != 0) {
702*7c478bd9Sstevel@tonic-gate 			return (ret);
703*7c478bd9Sstevel@tonic-gate 		}
704*7c478bd9Sstevel@tonic-gate 		LSN(newpage) = LSN(*hp);
705*7c478bd9Sstevel@tonic-gate 	}
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	PREV_PGNO(newpage) = PGNO(*hp);
708*7c478bd9Sstevel@tonic-gate 	NEXT_PGNO(*hp) = PGNO(newpage);
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 	if ((ret = memp_fput(dbp->mpf, *hp, DB_MPOOL_DIRTY)) != 0)
711*7c478bd9Sstevel@tonic-gate 		return (ret);
712*7c478bd9Sstevel@tonic-gate 	*hp = newpage;
713*7c478bd9Sstevel@tonic-gate 	*indxp = 0;
714*7c478bd9Sstevel@tonic-gate 	return (0);
715*7c478bd9Sstevel@tonic-gate }
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate /*
718*7c478bd9Sstevel@tonic-gate  * __db_dsearch --
719*7c478bd9Sstevel@tonic-gate  *	Search a set of duplicates for the proper position for a new duplicate.
720*7c478bd9Sstevel@tonic-gate  *
721*7c478bd9Sstevel@tonic-gate  *	+ pgno is the page number of the page on which to begin searching.
722*7c478bd9Sstevel@tonic-gate  * 	  Since we can continue duplicate searches, it might not be the first
723*7c478bd9Sstevel@tonic-gate  * 	  page.
724*7c478bd9Sstevel@tonic-gate  *
725*7c478bd9Sstevel@tonic-gate  * 	+ If we are continuing a search, then *pp may be non-NULL in which
726*7c478bd9Sstevel@tonic-gate  * 	  case we do not have to retrieve the page.
727*7c478bd9Sstevel@tonic-gate  *
728*7c478bd9Sstevel@tonic-gate  *	+ If we are continuing a search, then *indxp contains the first
729*7c478bd9Sstevel@tonic-gate  * 	  on pgno of where we should begin the search.
730*7c478bd9Sstevel@tonic-gate  *
731*7c478bd9Sstevel@tonic-gate  * 	NOTE: if there is no comparison function, then continuing is
732*7c478bd9Sstevel@tonic-gate  * 	meaningless, and *pp should always be NULL and *indxp will be
733*7c478bd9Sstevel@tonic-gate  *	ignored.
734*7c478bd9Sstevel@tonic-gate  *
735*7c478bd9Sstevel@tonic-gate  *	3 return values::
736*7c478bd9Sstevel@tonic-gate  *
737*7c478bd9Sstevel@tonic-gate  *	+ pp is the returned page pointer of where this element should go.
738*7c478bd9Sstevel@tonic-gate  *	+ indxp is the returned index on that page
739*7c478bd9Sstevel@tonic-gate  *	+ cmpp is the returned final comparison result.
740*7c478bd9Sstevel@tonic-gate  *
741*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_dsearch __P((DBC *,
742*7c478bd9Sstevel@tonic-gate  * PUBLIC:     int, DBT *, db_pgno_t, db_indx_t *, PAGE **, int *));
743*7c478bd9Sstevel@tonic-gate  */
744*7c478bd9Sstevel@tonic-gate int
__db_dsearch(dbc,is_insert,dbt,pgno,indxp,pp,cmpp)745*7c478bd9Sstevel@tonic-gate __db_dsearch(dbc, is_insert, dbt, pgno, indxp, pp, cmpp)
746*7c478bd9Sstevel@tonic-gate 	DBC *dbc;
747*7c478bd9Sstevel@tonic-gate 	int is_insert, *cmpp;
748*7c478bd9Sstevel@tonic-gate 	DBT *dbt;
749*7c478bd9Sstevel@tonic-gate 	db_pgno_t pgno;
750*7c478bd9Sstevel@tonic-gate 	db_indx_t *indxp;
751*7c478bd9Sstevel@tonic-gate 	PAGE **pp;
752*7c478bd9Sstevel@tonic-gate {
753*7c478bd9Sstevel@tonic-gate 	DB *dbp;
754*7c478bd9Sstevel@tonic-gate 	PAGE *h;
755*7c478bd9Sstevel@tonic-gate 	db_indx_t base, indx, lim, save_indx;
756*7c478bd9Sstevel@tonic-gate 	db_pgno_t save_pgno;
757*7c478bd9Sstevel@tonic-gate 	int ret;
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate 	dbp = dbc->dbp;
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	if (dbp->dup_compare == NULL) {
762*7c478bd9Sstevel@tonic-gate 		/*
763*7c478bd9Sstevel@tonic-gate 		 * We may have been given a valid page, but we may not be
764*7c478bd9Sstevel@tonic-gate 		 * able to use it.  The problem is that the application is
765*7c478bd9Sstevel@tonic-gate 		 * doing a join and we're trying to continue the search,
766*7c478bd9Sstevel@tonic-gate 		 * but since the items aren't sorted, we can't.  Discard
767*7c478bd9Sstevel@tonic-gate 		 * the page if it's not the one we're going to start with
768*7c478bd9Sstevel@tonic-gate 		 * anyway.
769*7c478bd9Sstevel@tonic-gate 		 */
770*7c478bd9Sstevel@tonic-gate 		if (*pp != NULL && (*pp)->pgno != pgno) {
771*7c478bd9Sstevel@tonic-gate 			if ((ret = memp_fput(dbp->mpf, *pp, 0)) != 0)
772*7c478bd9Sstevel@tonic-gate 				return (ret);
773*7c478bd9Sstevel@tonic-gate 			*pp = NULL;
774*7c478bd9Sstevel@tonic-gate 		}
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 		/*
777*7c478bd9Sstevel@tonic-gate 		 * If no duplicate function is specified, just go to the end
778*7c478bd9Sstevel@tonic-gate 		 * of the duplicate set.
779*7c478bd9Sstevel@tonic-gate 		 */
780*7c478bd9Sstevel@tonic-gate 		if (is_insert) {
781*7c478bd9Sstevel@tonic-gate 			if ((ret = __db_dend(dbc, pgno, pp)) != 0)
782*7c478bd9Sstevel@tonic-gate 				return (ret);
783*7c478bd9Sstevel@tonic-gate 			*indxp = NUM_ENT(*pp);
784*7c478bd9Sstevel@tonic-gate 			return (0);
785*7c478bd9Sstevel@tonic-gate 		}
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 		/*
788*7c478bd9Sstevel@tonic-gate 		 * We are looking for a specific duplicate, so do a linear
789*7c478bd9Sstevel@tonic-gate 		 * search.
790*7c478bd9Sstevel@tonic-gate 		 */
791*7c478bd9Sstevel@tonic-gate 		if (*pp != NULL)
792*7c478bd9Sstevel@tonic-gate 			goto nocmp_started;
793*7c478bd9Sstevel@tonic-gate 		for (;;) {
794*7c478bd9Sstevel@tonic-gate 			if ((ret = memp_fget(dbp->mpf, &pgno, 0, pp)) != 0)
795*7c478bd9Sstevel@tonic-gate 				goto pg_err;
796*7c478bd9Sstevel@tonic-gate nocmp_started:		h = *pp;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 			for (*indxp = 0; *indxp < NUM_ENT(h); ++*indxp) {
799*7c478bd9Sstevel@tonic-gate 				if ((*cmpp = __bam_cmp(dbp,
800*7c478bd9Sstevel@tonic-gate 				    dbt, h, *indxp, __bam_defcmp)) != 0)
801*7c478bd9Sstevel@tonic-gate 					continue;
802*7c478bd9Sstevel@tonic-gate 				/*
803*7c478bd9Sstevel@tonic-gate 				 * The duplicate may have already been deleted,
804*7c478bd9Sstevel@tonic-gate 				 * if it's a btree page, in which case we skip
805*7c478bd9Sstevel@tonic-gate 				 * it.
806*7c478bd9Sstevel@tonic-gate 				 */
807*7c478bd9Sstevel@tonic-gate 				if (dbp->type == DB_BTREE &&
808*7c478bd9Sstevel@tonic-gate 				    B_DISSET(GET_BKEYDATA(h, *indxp)->type))
809*7c478bd9Sstevel@tonic-gate 					continue;
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate 				return (0);
812*7c478bd9Sstevel@tonic-gate 			}
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 			if ((pgno = h->next_pgno) == PGNO_INVALID)
815*7c478bd9Sstevel@tonic-gate 				break;
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 			if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
818*7c478bd9Sstevel@tonic-gate 				return (ret);
819*7c478bd9Sstevel@tonic-gate 		}
820*7c478bd9Sstevel@tonic-gate 		*cmpp = 1;			/* We didn't succeed... */
821*7c478bd9Sstevel@tonic-gate 		return (0);
822*7c478bd9Sstevel@tonic-gate 	}
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	/*
825*7c478bd9Sstevel@tonic-gate 	 * We have a comparison routine, i.e., the duplicates are sorted.
826*7c478bd9Sstevel@tonic-gate 	 * Walk through the chain of duplicates, checking the last entry
827*7c478bd9Sstevel@tonic-gate 	 * on each page to decide if it's the page we want to search.
828*7c478bd9Sstevel@tonic-gate 	 *
829*7c478bd9Sstevel@tonic-gate 	 * *pp may be non-NULL -- if we were given a valid page (e.g., are
830*7c478bd9Sstevel@tonic-gate 	 * in mid-search), then use the provided page.
831*7c478bd9Sstevel@tonic-gate 	 */
832*7c478bd9Sstevel@tonic-gate 	if (*pp != NULL)
833*7c478bd9Sstevel@tonic-gate 		goto cmp_started;
834*7c478bd9Sstevel@tonic-gate 	for (;;) {
835*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, pp)) != 0)
836*7c478bd9Sstevel@tonic-gate 			goto pg_err;
837*7c478bd9Sstevel@tonic-gate cmp_started:	h = *pp;
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 		if ((pgno = h->next_pgno) == PGNO_INVALID || __bam_cmp(dbp,
840*7c478bd9Sstevel@tonic-gate 		    dbt, h, h->entries - 1, dbp->dup_compare) <= 0)
841*7c478bd9Sstevel@tonic-gate 			break;
842*7c478bd9Sstevel@tonic-gate 		/*
843*7c478bd9Sstevel@tonic-gate 		 * Even when continuing a search, make sure we don't skip
844*7c478bd9Sstevel@tonic-gate 		 * entries on a new page
845*7c478bd9Sstevel@tonic-gate 		 */
846*7c478bd9Sstevel@tonic-gate 		*indxp = 0;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
849*7c478bd9Sstevel@tonic-gate 			return (ret);
850*7c478bd9Sstevel@tonic-gate 	}
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	/* Next, do a binary search on the page. */
853*7c478bd9Sstevel@tonic-gate 	base = F_ISSET(dbc, DBC_CONTINUE) ? *indxp : 0;
854*7c478bd9Sstevel@tonic-gate 	for (lim = NUM_ENT(h) - base; lim != 0; lim >>= 1) {
855*7c478bd9Sstevel@tonic-gate 		indx = base + (lim >> 1);
856*7c478bd9Sstevel@tonic-gate 		if ((*cmpp = __bam_cmp(dbp,
857*7c478bd9Sstevel@tonic-gate 		    dbt, h, indx, dbp->dup_compare)) == 0) {
858*7c478bd9Sstevel@tonic-gate 			*indxp = indx;
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 			if (dbp->type != DB_BTREE ||
861*7c478bd9Sstevel@tonic-gate 			    !B_DISSET(GET_BKEYDATA(h, *indxp)->type))
862*7c478bd9Sstevel@tonic-gate 				return (0);
863*7c478bd9Sstevel@tonic-gate 			goto check_delete;
864*7c478bd9Sstevel@tonic-gate 		}
865*7c478bd9Sstevel@tonic-gate 		if (*cmpp > 0) {
866*7c478bd9Sstevel@tonic-gate 			base = indx + 1;
867*7c478bd9Sstevel@tonic-gate 			lim--;
868*7c478bd9Sstevel@tonic-gate 		}
869*7c478bd9Sstevel@tonic-gate 	}
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	/*
872*7c478bd9Sstevel@tonic-gate 	 * Base references the smallest index larger than the supplied DBT's
873*7c478bd9Sstevel@tonic-gate 	 * data item, potentially both 0 and NUM_ENT.
874*7c478bd9Sstevel@tonic-gate 	 */
875*7c478bd9Sstevel@tonic-gate 	*indxp = base;
876*7c478bd9Sstevel@tonic-gate 	return (0);
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate check_delete:
879*7c478bd9Sstevel@tonic-gate 	/*
880*7c478bd9Sstevel@tonic-gate 	 * The duplicate may have already been deleted, if it's a btree page,
881*7c478bd9Sstevel@tonic-gate 	 * in which case we wander around, hoping to find an entry that hasn't
882*7c478bd9Sstevel@tonic-gate 	 * been deleted.  First, wander in a forwardly direction.
883*7c478bd9Sstevel@tonic-gate 	 */
884*7c478bd9Sstevel@tonic-gate 	save_pgno = (*pp)->pgno;
885*7c478bd9Sstevel@tonic-gate 	save_indx = *indxp;
886*7c478bd9Sstevel@tonic-gate 	for (++*indxp;;) {
887*7c478bd9Sstevel@tonic-gate 		for (; *indxp < NUM_ENT(h); ++*indxp) {
888*7c478bd9Sstevel@tonic-gate 			if ((*cmpp = __bam_cmp(dbp,
889*7c478bd9Sstevel@tonic-gate 			    dbt, h, *indxp, dbp->dup_compare)) != 0)
890*7c478bd9Sstevel@tonic-gate 				goto check_delete_rev;
891*7c478bd9Sstevel@tonic-gate 
892*7c478bd9Sstevel@tonic-gate 			if (!B_DISSET(GET_BKEYDATA(h, *indxp)->type))
893*7c478bd9Sstevel@tonic-gate 				return (0);
894*7c478bd9Sstevel@tonic-gate 		}
895*7c478bd9Sstevel@tonic-gate 		if ((pgno = h->next_pgno) == PGNO_INVALID)
896*7c478bd9Sstevel@tonic-gate 			break;
897*7c478bd9Sstevel@tonic-gate 
898*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
899*7c478bd9Sstevel@tonic-gate 			return (ret);
900*7c478bd9Sstevel@tonic-gate 
901*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, pp)) != 0)
902*7c478bd9Sstevel@tonic-gate 			goto pg_err;
903*7c478bd9Sstevel@tonic-gate 		h = *pp;
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate 		*indxp = 0;
906*7c478bd9Sstevel@tonic-gate 	}
907*7c478bd9Sstevel@tonic-gate 
908*7c478bd9Sstevel@tonic-gate check_delete_rev:
909*7c478bd9Sstevel@tonic-gate 	/* Go back to where we started, and wander in a backwardly direction. */
910*7c478bd9Sstevel@tonic-gate 	if (h->pgno != save_pgno) {
911*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
912*7c478bd9Sstevel@tonic-gate 			return (ret);
913*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &save_pgno, 0, pp)) != 0)
914*7c478bd9Sstevel@tonic-gate 			goto pg_err;
915*7c478bd9Sstevel@tonic-gate 		h = *pp;
916*7c478bd9Sstevel@tonic-gate 	}
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	for (;;) {
919*7c478bd9Sstevel@tonic-gate 		while (*indxp > 0) {
920*7c478bd9Sstevel@tonic-gate 			--*indxp;
921*7c478bd9Sstevel@tonic-gate 			if ((*cmpp = __bam_cmp(dbp,
922*7c478bd9Sstevel@tonic-gate 			    dbt, h, *indxp, dbp->dup_compare)) != 0)
923*7c478bd9Sstevel@tonic-gate 				goto check_delete_fail;
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 			if (!B_DISSET(GET_BKEYDATA(h, *indxp)->type))
926*7c478bd9Sstevel@tonic-gate 				return (0);
927*7c478bd9Sstevel@tonic-gate 		}
928*7c478bd9Sstevel@tonic-gate 		if ((pgno = h->prev_pgno) == PGNO_INVALID)
929*7c478bd9Sstevel@tonic-gate 			break;
930*7c478bd9Sstevel@tonic-gate 
931*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fput(dbp->mpf, h, 0)) != 0)
932*7c478bd9Sstevel@tonic-gate 			return (ret);
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 		if ((ret = memp_fget(dbp->mpf, &pgno, 0, pp)) != 0)
935*7c478bd9Sstevel@tonic-gate 			goto pg_err;
936*7c478bd9Sstevel@tonic-gate 		h = *pp;
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate 		*indxp = NUM_ENT(h);
939*7c478bd9Sstevel@tonic-gate 	}
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate check_delete_fail:
942*7c478bd9Sstevel@tonic-gate 	*cmpp = 1;			/* We didn't succeed... */
943*7c478bd9Sstevel@tonic-gate 	return (0);
944*7c478bd9Sstevel@tonic-gate 
945*7c478bd9Sstevel@tonic-gate pg_err:	__db_pgerr(dbp, pgno);
946*7c478bd9Sstevel@tonic-gate 	return (ret);
947*7c478bd9Sstevel@tonic-gate }
948