xref: /onnv-gate/usr/src/cmd/sendmail/db/btree/bt_rec.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #include "config.h"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #ifndef lint
11*0Sstevel@tonic-gate static const char sccsid[] = "@(#)bt_rec.c	10.28 (Sleepycat) 9/27/98";
12*0Sstevel@tonic-gate #endif /* not lint */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
15*0Sstevel@tonic-gate #include <sys/types.h>
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #include <errno.h>
18*0Sstevel@tonic-gate #include <string.h>
19*0Sstevel@tonic-gate #endif
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include "db_int.h"
22*0Sstevel@tonic-gate #include "db_page.h"
23*0Sstevel@tonic-gate #include "shqueue.h"
24*0Sstevel@tonic-gate #include "hash.h"
25*0Sstevel@tonic-gate #include "btree.h"
26*0Sstevel@tonic-gate #include "log.h"
27*0Sstevel@tonic-gate #include "common_ext.h"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * __bam_pg_alloc_recover --
31*0Sstevel@tonic-gate  *	Recovery function for pg_alloc.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * PUBLIC: int __bam_pg_alloc_recover
34*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate int
__bam_pg_alloc_recover(logp,dbtp,lsnp,redo,info)37*0Sstevel@tonic-gate __bam_pg_alloc_recover(logp, dbtp, lsnp, redo, info)
38*0Sstevel@tonic-gate 	DB_LOG *logp;
39*0Sstevel@tonic-gate 	DBT *dbtp;
40*0Sstevel@tonic-gate 	DB_LSN *lsnp;
41*0Sstevel@tonic-gate 	int redo;
42*0Sstevel@tonic-gate 	void *info;
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	__bam_pg_alloc_args *argp;
45*0Sstevel@tonic-gate 	BTMETA *meta;
46*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
47*0Sstevel@tonic-gate 	PAGE *pagep;
48*0Sstevel@tonic-gate 	DB *file_dbp;
49*0Sstevel@tonic-gate 	DBC *dbc;
50*0Sstevel@tonic-gate 	db_pgno_t pgno;
51*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	REC_PRINT(__bam_pg_alloc_print);
54*0Sstevel@tonic-gate 	REC_INTRO(__bam_pg_alloc_read);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	/*
57*0Sstevel@tonic-gate 	 * Fix up the allocated page.  If we're redoing the operation, we have
58*0Sstevel@tonic-gate 	 * to get the page (creating it if it doesn't exist), and update its
59*0Sstevel@tonic-gate 	 * LSN.  If we're undoing the operation, we have to reset the page's
60*0Sstevel@tonic-gate 	 * LSN and put it on the free list.
61*0Sstevel@tonic-gate 	 *
62*0Sstevel@tonic-gate 	 * Fix up the metadata page.  If we're redoing the operation, we have
63*0Sstevel@tonic-gate 	 * to get the metadata page and update its LSN and its free pointer.
64*0Sstevel@tonic-gate 	 * If we're undoing the operation and the page was ever created, we put
65*0Sstevel@tonic-gate 	 * it on the freelist.
66*0Sstevel@tonic-gate 	 */
67*0Sstevel@tonic-gate 	pgno = PGNO_METADATA;
68*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &pgno, 0, &meta)) != 0) {
69*0Sstevel@tonic-gate 		/* The metadata page must always exist. */
70*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, pgno);
71*0Sstevel@tonic-gate 		goto out;
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0) {
74*0Sstevel@tonic-gate 		/*
75*0Sstevel@tonic-gate 		 * We specify creation and check for it later, because this
76*0Sstevel@tonic-gate 		 * operation was supposed to create the page, and even in
77*0Sstevel@tonic-gate 		 * the undo case it's going to get linked onto the freelist
78*0Sstevel@tonic-gate 		 * which we're also fixing up.
79*0Sstevel@tonic-gate 		 */
80*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
81*0Sstevel@tonic-gate 		(void)memp_fput(mpf, meta, 0);
82*0Sstevel@tonic-gate 		goto out;
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* Fix up the allocated page. */
86*0Sstevel@tonic-gate 	modified = 0;
87*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
88*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->page_lsn);
89*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
90*0Sstevel@tonic-gate 		/* Need to redo update described. */
91*0Sstevel@tonic-gate 		P_INIT(pagep, file_dbp->pgsize,
92*0Sstevel@tonic-gate 		    argp->pgno, PGNO_INVALID, PGNO_INVALID, 0, argp->ptype);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 		pagep->lsn = *lsnp;
95*0Sstevel@tonic-gate 		modified = 1;
96*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
97*0Sstevel@tonic-gate 		/* Need to undo update described. */
98*0Sstevel@tonic-gate 		P_INIT(pagep, file_dbp->pgsize,
99*0Sstevel@tonic-gate 		    argp->pgno, PGNO_INVALID, meta->free, 0, P_INVALID);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		pagep->lsn = argp->page_lsn;
102*0Sstevel@tonic-gate 		modified = 1;
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0) {
105*0Sstevel@tonic-gate 		(void)memp_fput(mpf, meta, 0);
106*0Sstevel@tonic-gate 		goto out;
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* Fix up the metadata page. */
110*0Sstevel@tonic-gate 	modified = 0;
111*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(meta));
112*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(meta), &argp->meta_lsn);
113*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
114*0Sstevel@tonic-gate 		/* Need to redo update described. */
115*0Sstevel@tonic-gate 		meta->lsn = *lsnp;
116*0Sstevel@tonic-gate 		meta->free = argp->next;
117*0Sstevel@tonic-gate 		modified = 1;
118*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
119*0Sstevel@tonic-gate 		/* Need to undo update described. */
120*0Sstevel@tonic-gate 		meta->lsn = argp->meta_lsn;
121*0Sstevel@tonic-gate 		meta->free = argp->pgno;
122*0Sstevel@tonic-gate 		modified = 1;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, meta, modified ? DB_MPOOL_DIRTY : 0)) != 0)
125*0Sstevel@tonic-gate 		goto out;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
128*0Sstevel@tonic-gate 	ret = 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate out:	REC_CLOSE;
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * __bam_pg_free_recover --
135*0Sstevel@tonic-gate  *	Recovery function for pg_free.
136*0Sstevel@tonic-gate  *
137*0Sstevel@tonic-gate  * PUBLIC: int __bam_pg_free_recover
138*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate int
__bam_pg_free_recover(logp,dbtp,lsnp,redo,info)141*0Sstevel@tonic-gate __bam_pg_free_recover(logp, dbtp, lsnp, redo, info)
142*0Sstevel@tonic-gate 	DB_LOG *logp;
143*0Sstevel@tonic-gate 	DBT *dbtp;
144*0Sstevel@tonic-gate 	DB_LSN *lsnp;
145*0Sstevel@tonic-gate 	int redo;
146*0Sstevel@tonic-gate 	void *info;
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	__bam_pg_free_args *argp;
149*0Sstevel@tonic-gate 	BTMETA *meta;
150*0Sstevel@tonic-gate 	DB *file_dbp;
151*0Sstevel@tonic-gate 	DBC *dbc;
152*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
153*0Sstevel@tonic-gate 	PAGE *pagep;
154*0Sstevel@tonic-gate 	db_pgno_t pgno;
155*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	REC_PRINT(__bam_pg_free_print);
158*0Sstevel@tonic-gate 	REC_INTRO(__bam_pg_free_read);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/*
161*0Sstevel@tonic-gate 	 * Fix up the freed page.  If we're redoing the operation we get the
162*0Sstevel@tonic-gate 	 * page and explicitly discard its contents, then update its LSN.  If
163*0Sstevel@tonic-gate 	 * we're undoing the operation, we get the page and restore its header.
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
166*0Sstevel@tonic-gate 		/*
167*0Sstevel@tonic-gate 		 * We don't automatically create the page.  The only way the
168*0Sstevel@tonic-gate 		 * page might not exist is if the alloc never happened, and
169*0Sstevel@tonic-gate 		 * the only way the alloc might never have happened is if we
170*0Sstevel@tonic-gate 		 * are undoing, in which case there's no reason to create the
171*0Sstevel@tonic-gate 		 * page.
172*0Sstevel@tonic-gate 		 */
173*0Sstevel@tonic-gate 		if (!redo)
174*0Sstevel@tonic-gate 			goto done;
175*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
176*0Sstevel@tonic-gate 		goto out;
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	modified = 0;
179*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
180*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &LSN(argp->header.data));
181*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
182*0Sstevel@tonic-gate 		/* Need to redo update described. */
183*0Sstevel@tonic-gate 		P_INIT(pagep, file_dbp->pgsize,
184*0Sstevel@tonic-gate 		    pagep->pgno, PGNO_INVALID, argp->next, 0, P_INVALID);
185*0Sstevel@tonic-gate 		pagep->lsn = *lsnp;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		modified = 1;
188*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
189*0Sstevel@tonic-gate 		/* Need to undo update described. */
190*0Sstevel@tonic-gate 		memcpy(pagep, argp->header.data, argp->header.size);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		modified = 1;
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
195*0Sstevel@tonic-gate 		goto out;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/*
198*0Sstevel@tonic-gate 	 * Fix up the metadata page.  If we're redoing or undoing the operation
199*0Sstevel@tonic-gate 	 * we get the page and update its LSN and free pointer.
200*0Sstevel@tonic-gate 	 */
201*0Sstevel@tonic-gate 	pgno = PGNO_METADATA;
202*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &pgno, 0, &meta)) != 0) {
203*0Sstevel@tonic-gate 		/* The metadata page must always exist. */
204*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, pgno);
205*0Sstevel@tonic-gate 		goto out;
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	modified = 0;
209*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(meta));
210*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(meta), &argp->meta_lsn);
211*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
212*0Sstevel@tonic-gate 		/* Need to redo update described. */
213*0Sstevel@tonic-gate 		meta->free = argp->pgno;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 		meta->lsn = *lsnp;
216*0Sstevel@tonic-gate 		modified = 1;
217*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
218*0Sstevel@tonic-gate 		/* Need to undo update described. */
219*0Sstevel@tonic-gate 		meta->free = argp->next;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		meta->lsn = argp->meta_lsn;
222*0Sstevel@tonic-gate 		modified = 1;
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, meta, modified ? DB_MPOOL_DIRTY : 0)) != 0)
225*0Sstevel@tonic-gate 		goto out;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
228*0Sstevel@tonic-gate 	ret = 0;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate out:	REC_CLOSE;
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate  * __bam_split_recover --
235*0Sstevel@tonic-gate  *	Recovery function for split.
236*0Sstevel@tonic-gate  *
237*0Sstevel@tonic-gate  * PUBLIC: int __bam_split_recover
238*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
239*0Sstevel@tonic-gate  */
240*0Sstevel@tonic-gate int
__bam_split_recover(logp,dbtp,lsnp,redo,info)241*0Sstevel@tonic-gate __bam_split_recover(logp, dbtp, lsnp, redo, info)
242*0Sstevel@tonic-gate 	DB_LOG *logp;
243*0Sstevel@tonic-gate 	DBT *dbtp;
244*0Sstevel@tonic-gate 	DB_LSN *lsnp;
245*0Sstevel@tonic-gate 	int redo;
246*0Sstevel@tonic-gate 	void *info;
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	__bam_split_args *argp;
249*0Sstevel@tonic-gate 	DB *file_dbp;
250*0Sstevel@tonic-gate 	DBC *dbc;
251*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
252*0Sstevel@tonic-gate 	PAGE *_lp, *lp, *np, *pp, *_rp, *rp, *sp;
253*0Sstevel@tonic-gate 	db_pgno_t pgno;
254*0Sstevel@tonic-gate 	int l_update, p_update, r_update, ret, rootsplit, t_ret;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	REC_PRINT(__bam_split_print);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	mpf = NULL;
259*0Sstevel@tonic-gate 	_lp = lp = np = pp = _rp = rp = NULL;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	REC_INTRO(__bam_split_read);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	/*
264*0Sstevel@tonic-gate 	 * There are two kinds of splits that we have to recover from.  The
265*0Sstevel@tonic-gate 	 * first is a root-page split, where the root page is split from a
266*0Sstevel@tonic-gate 	 * leaf page into an internal page and two new leaf pages are created.
267*0Sstevel@tonic-gate 	 * The second is where a page is split into two pages, and a new key
268*0Sstevel@tonic-gate 	 * is inserted into the parent page.
269*0Sstevel@tonic-gate 	 */
270*0Sstevel@tonic-gate 	sp = argp->pg.data;
271*0Sstevel@tonic-gate 	pgno = PGNO(sp);
272*0Sstevel@tonic-gate 	rootsplit = pgno == PGNO_ROOT;
273*0Sstevel@tonic-gate 	if (memp_fget(mpf, &argp->left, 0, &lp) != 0)
274*0Sstevel@tonic-gate 		lp = NULL;
275*0Sstevel@tonic-gate 	if (memp_fget(mpf, &argp->right, 0, &rp) != 0)
276*0Sstevel@tonic-gate 		rp = NULL;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (redo) {
279*0Sstevel@tonic-gate 		l_update = r_update = p_update = 0;
280*0Sstevel@tonic-gate 		/*
281*0Sstevel@tonic-gate 		 * Decide if we need to resplit the page.
282*0Sstevel@tonic-gate 		 *
283*0Sstevel@tonic-gate 		 * If this is a root split, then the root has to exist, it's
284*0Sstevel@tonic-gate 		 * the page we're splitting and it gets modified.  If this is
285*0Sstevel@tonic-gate 		 * not a root split, then the left page has to exist, for the
286*0Sstevel@tonic-gate 		 * same reason.
287*0Sstevel@tonic-gate 		 */
288*0Sstevel@tonic-gate 		if (rootsplit) {
289*0Sstevel@tonic-gate 			if ((ret = memp_fget(mpf, &pgno, 0, &pp)) != 0) {
290*0Sstevel@tonic-gate 				(void)__db_pgerr(file_dbp, pgno);
291*0Sstevel@tonic-gate 				pp = NULL;
292*0Sstevel@tonic-gate 				goto out;
293*0Sstevel@tonic-gate 			}
294*0Sstevel@tonic-gate 			p_update =
295*0Sstevel@tonic-gate 			    log_compare(&LSN(pp), &LSN(argp->pg.data)) == 0;
296*0Sstevel@tonic-gate 		} else
297*0Sstevel@tonic-gate 			if (lp == NULL) {
298*0Sstevel@tonic-gate 				(void)__db_pgerr(file_dbp, argp->left);
299*0Sstevel@tonic-gate 				goto out;
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 		if (lp == NULL || log_compare(&LSN(lp), &argp->llsn) == 0)
302*0Sstevel@tonic-gate 			l_update = 1;
303*0Sstevel@tonic-gate 		if (rp == NULL || log_compare(&LSN(rp), &argp->rlsn) == 0)
304*0Sstevel@tonic-gate 			r_update = 1;
305*0Sstevel@tonic-gate 		if (!p_update && !l_update && !r_update)
306*0Sstevel@tonic-gate 			goto done;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		/* Allocate and initialize new left/right child pages. */
309*0Sstevel@tonic-gate 		if ((ret = __os_malloc(file_dbp->pgsize, NULL, &_lp)) != 0 ||
310*0Sstevel@tonic-gate 		    (ret = __os_malloc(file_dbp->pgsize, NULL, &_rp)) != 0)
311*0Sstevel@tonic-gate 			goto out;
312*0Sstevel@tonic-gate 		if (rootsplit) {
313*0Sstevel@tonic-gate 			P_INIT(_lp, file_dbp->pgsize, argp->left,
314*0Sstevel@tonic-gate 			    PGNO_INVALID,
315*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ? PGNO_INVALID : argp->right,
316*0Sstevel@tonic-gate 			    LEVEL(sp), TYPE(sp));
317*0Sstevel@tonic-gate 			P_INIT(_rp, file_dbp->pgsize, argp->right,
318*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ?  PGNO_INVALID : argp->left,
319*0Sstevel@tonic-gate 			    PGNO_INVALID, LEVEL(sp), TYPE(sp));
320*0Sstevel@tonic-gate 		} else {
321*0Sstevel@tonic-gate 			P_INIT(_lp, file_dbp->pgsize, PGNO(sp),
322*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ? PGNO_INVALID : PREV_PGNO(sp),
323*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ? PGNO_INVALID : argp->right,
324*0Sstevel@tonic-gate 			    LEVEL(sp), TYPE(sp));
325*0Sstevel@tonic-gate 			P_INIT(_rp, file_dbp->pgsize, argp->right,
326*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ? PGNO_INVALID : sp->pgno,
327*0Sstevel@tonic-gate 			    ISINTERNAL(sp) ? PGNO_INVALID : NEXT_PGNO(sp),
328*0Sstevel@tonic-gate 			    LEVEL(sp), TYPE(sp));
329*0Sstevel@tonic-gate 		}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		/* Split the page. */
332*0Sstevel@tonic-gate 		if ((ret = __bam_copy(file_dbp, sp, _lp, 0, argp->indx)) != 0 ||
333*0Sstevel@tonic-gate 		    (ret = __bam_copy(file_dbp, sp, _rp, argp->indx,
334*0Sstevel@tonic-gate 		    NUM_ENT(sp))) != 0)
335*0Sstevel@tonic-gate 			goto out;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 		/* If the left child is wrong, update it. */
338*0Sstevel@tonic-gate 		if (lp == NULL && (ret =
339*0Sstevel@tonic-gate 		    memp_fget(mpf, &argp->left, DB_MPOOL_CREATE, &lp)) != 0) {
340*0Sstevel@tonic-gate 			(void)__db_pgerr(file_dbp, argp->left);
341*0Sstevel@tonic-gate 			lp = NULL;
342*0Sstevel@tonic-gate 			goto out;
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 		if (l_update) {
345*0Sstevel@tonic-gate 			memcpy(lp, _lp, file_dbp->pgsize);
346*0Sstevel@tonic-gate 			lp->lsn = *lsnp;
347*0Sstevel@tonic-gate 			if ((ret = memp_fput(mpf, lp, DB_MPOOL_DIRTY)) != 0)
348*0Sstevel@tonic-gate 				goto out;
349*0Sstevel@tonic-gate 			lp = NULL;
350*0Sstevel@tonic-gate 		}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 		/* If the right child is wrong, update it. */
353*0Sstevel@tonic-gate 		if (rp == NULL && (ret = memp_fget(mpf,
354*0Sstevel@tonic-gate 		    &argp->right, DB_MPOOL_CREATE, &rp)) != 0) {
355*0Sstevel@tonic-gate 			(void)__db_pgerr(file_dbp, argp->right);
356*0Sstevel@tonic-gate 			rp = NULL;
357*0Sstevel@tonic-gate 			goto out;
358*0Sstevel@tonic-gate 		}
359*0Sstevel@tonic-gate 		if (r_update) {
360*0Sstevel@tonic-gate 			memcpy(rp, _rp, file_dbp->pgsize);
361*0Sstevel@tonic-gate 			rp->lsn = *lsnp;
362*0Sstevel@tonic-gate 			if ((ret = memp_fput(mpf, rp, DB_MPOOL_DIRTY)) != 0)
363*0Sstevel@tonic-gate 				goto out;
364*0Sstevel@tonic-gate 			rp = NULL;
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 		/*
368*0Sstevel@tonic-gate 		 * If the parent page is wrong, update it.  This is of interest
369*0Sstevel@tonic-gate 		 * only if it was a root split, since root splits create parent
370*0Sstevel@tonic-gate 		 * pages.  All other splits modify a parent page, but those are
371*0Sstevel@tonic-gate 		 * separately logged and recovered.
372*0Sstevel@tonic-gate 		 */
373*0Sstevel@tonic-gate 		if (rootsplit && p_update) {
374*0Sstevel@tonic-gate 			if (file_dbp->type == DB_BTREE)
375*0Sstevel@tonic-gate 				P_INIT(pp, file_dbp->pgsize,
376*0Sstevel@tonic-gate 				    PGNO_ROOT, PGNO_INVALID, PGNO_INVALID,
377*0Sstevel@tonic-gate 				    _lp->level + 1, P_IBTREE);
378*0Sstevel@tonic-gate 			else
379*0Sstevel@tonic-gate 				P_INIT(pp, file_dbp->pgsize,
380*0Sstevel@tonic-gate 				    PGNO_ROOT, PGNO_INVALID, PGNO_INVALID,
381*0Sstevel@tonic-gate 				    _lp->level + 1, P_IRECNO);
382*0Sstevel@tonic-gate 			RE_NREC_SET(pp,
383*0Sstevel@tonic-gate 			    file_dbp->type == DB_RECNO ||
384*0Sstevel@tonic-gate 			    F_ISSET(file_dbp, DB_BT_RECNUM) ?
385*0Sstevel@tonic-gate 			    __bam_total(_lp) + __bam_total(_rp) : 0);
386*0Sstevel@tonic-gate 			pp->lsn = *lsnp;
387*0Sstevel@tonic-gate 			if ((ret = memp_fput(mpf, pp, DB_MPOOL_DIRTY)) != 0)
388*0Sstevel@tonic-gate 				goto out;
389*0Sstevel@tonic-gate 			pp = NULL;
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		/*
393*0Sstevel@tonic-gate 		 * Finally, redo the next-page link if necessary.  This is of
394*0Sstevel@tonic-gate 		 * interest only if it wasn't a root split -- inserting a new
395*0Sstevel@tonic-gate 		 * page in the tree requires that any following page have its
396*0Sstevel@tonic-gate 		 * previous-page pointer updated to our new page.  The next
397*0Sstevel@tonic-gate 		 * page must exist because we're redoing the operation.
398*0Sstevel@tonic-gate 		 */
399*0Sstevel@tonic-gate 		if (!rootsplit && !IS_ZERO_LSN(argp->nlsn)) {
400*0Sstevel@tonic-gate 			if ((ret = memp_fget(mpf, &argp->npgno, 0, &np)) != 0) {
401*0Sstevel@tonic-gate 				(void)__db_pgerr(file_dbp, argp->npgno);
402*0Sstevel@tonic-gate 				np = NULL;
403*0Sstevel@tonic-gate 				goto out;
404*0Sstevel@tonic-gate 			}
405*0Sstevel@tonic-gate 			if (log_compare(&LSN(np), &argp->nlsn) == 0) {
406*0Sstevel@tonic-gate 				PREV_PGNO(np) = argp->right;
407*0Sstevel@tonic-gate 				np->lsn = *lsnp;
408*0Sstevel@tonic-gate 				if ((ret =
409*0Sstevel@tonic-gate 				    memp_fput(mpf, np, DB_MPOOL_DIRTY)) != 0)
410*0Sstevel@tonic-gate 					goto out;
411*0Sstevel@tonic-gate 				np = NULL;
412*0Sstevel@tonic-gate 			}
413*0Sstevel@tonic-gate 		}
414*0Sstevel@tonic-gate 	} else {
415*0Sstevel@tonic-gate 		/*
416*0Sstevel@tonic-gate 		 * If the split page is wrong, replace its contents with the
417*0Sstevel@tonic-gate 		 * logged page contents.  If the page doesn't exist, it means
418*0Sstevel@tonic-gate 		 * that the create of the page never happened, nor did any of
419*0Sstevel@tonic-gate 		 * the adds onto the page that caused the split, and there's
420*0Sstevel@tonic-gate 		 * really no undo-ing to be done.
421*0Sstevel@tonic-gate 		 */
422*0Sstevel@tonic-gate 		if ((ret = memp_fget(mpf, &pgno, 0, &pp)) != 0) {
423*0Sstevel@tonic-gate 			pp = NULL;
424*0Sstevel@tonic-gate 			goto lrundo;
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 		if (log_compare(lsnp, &LSN(pp)) == 0) {
427*0Sstevel@tonic-gate 			memcpy(pp, argp->pg.data, argp->pg.size);
428*0Sstevel@tonic-gate 			if ((ret = memp_fput(mpf, pp, DB_MPOOL_DIRTY)) != 0)
429*0Sstevel@tonic-gate 				goto out;
430*0Sstevel@tonic-gate 			pp = NULL;
431*0Sstevel@tonic-gate 		}
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 		/*
434*0Sstevel@tonic-gate 		 * If it's a root split and the left child ever existed, update
435*0Sstevel@tonic-gate 		 * its LSN.  (If it's not a root split, we've updated the left
436*0Sstevel@tonic-gate 		 * page already -- it's the same as the split page.) If the
437*0Sstevel@tonic-gate 		 * right child ever existed, root split or not, update its LSN.
438*0Sstevel@tonic-gate 		 * The undo of the page allocation(s) will restore them to the
439*0Sstevel@tonic-gate 		 * free list.
440*0Sstevel@tonic-gate 		 */
441*0Sstevel@tonic-gate lrundo:		if ((rootsplit && lp != NULL) || rp != NULL) {
442*0Sstevel@tonic-gate 			if (rootsplit && lp != NULL &&
443*0Sstevel@tonic-gate 			    log_compare(lsnp, &LSN(lp)) == 0) {
444*0Sstevel@tonic-gate 				lp->lsn = argp->llsn;
445*0Sstevel@tonic-gate 				if ((ret =
446*0Sstevel@tonic-gate 				    memp_fput(mpf, lp, DB_MPOOL_DIRTY)) != 0)
447*0Sstevel@tonic-gate 					goto out;
448*0Sstevel@tonic-gate 				lp = NULL;
449*0Sstevel@tonic-gate 			}
450*0Sstevel@tonic-gate 			if (rp != NULL &&
451*0Sstevel@tonic-gate 			    log_compare(lsnp, &LSN(rp)) == 0) {
452*0Sstevel@tonic-gate 				rp->lsn = argp->rlsn;
453*0Sstevel@tonic-gate 				if ((ret =
454*0Sstevel@tonic-gate 				    memp_fput(mpf, rp, DB_MPOOL_DIRTY)) != 0)
455*0Sstevel@tonic-gate 					goto out;
456*0Sstevel@tonic-gate 				rp = NULL;
457*0Sstevel@tonic-gate 			}
458*0Sstevel@tonic-gate 		}
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 		/*
461*0Sstevel@tonic-gate 		 * Finally, undo the next-page link if necessary.  This is of
462*0Sstevel@tonic-gate 		 * interest only if it wasn't a root split -- inserting a new
463*0Sstevel@tonic-gate 		 * page in the tree requires that any following page have its
464*0Sstevel@tonic-gate 		 * previous-page pointer updated to our new page.  Since it's
465*0Sstevel@tonic-gate 		 * possible that the next-page never existed, we ignore it as
466*0Sstevel@tonic-gate 		 * if there's nothing to undo.
467*0Sstevel@tonic-gate 		 */
468*0Sstevel@tonic-gate 		if (!rootsplit && !IS_ZERO_LSN(argp->nlsn)) {
469*0Sstevel@tonic-gate 			if ((ret = memp_fget(mpf, &argp->npgno, 0, &np)) != 0) {
470*0Sstevel@tonic-gate 				np = NULL;
471*0Sstevel@tonic-gate 				goto done;
472*0Sstevel@tonic-gate 			}
473*0Sstevel@tonic-gate 			if (log_compare(lsnp, &LSN(np)) == 0) {
474*0Sstevel@tonic-gate 				PREV_PGNO(np) = argp->left;
475*0Sstevel@tonic-gate 				np->lsn = argp->nlsn;
476*0Sstevel@tonic-gate 				if (memp_fput(mpf, np, DB_MPOOL_DIRTY))
477*0Sstevel@tonic-gate 					goto out;
478*0Sstevel@tonic-gate 				np = NULL;
479*0Sstevel@tonic-gate 			}
480*0Sstevel@tonic-gate 		}
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
484*0Sstevel@tonic-gate 	ret = 0;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate out:	/* Free any pages that weren't dirtied. */
487*0Sstevel@tonic-gate 	if (pp != NULL && (t_ret = memp_fput(mpf, pp, 0)) != 0 && ret == 0)
488*0Sstevel@tonic-gate 		ret = t_ret;
489*0Sstevel@tonic-gate 	if (lp != NULL && (t_ret = memp_fput(mpf, lp, 0)) != 0 && ret == 0)
490*0Sstevel@tonic-gate 		ret = t_ret;
491*0Sstevel@tonic-gate 	if (np != NULL && (t_ret = memp_fput(mpf, np, 0)) != 0 && ret == 0)
492*0Sstevel@tonic-gate 		ret = t_ret;
493*0Sstevel@tonic-gate 	if (rp != NULL && (t_ret = memp_fput(mpf, rp, 0)) != 0 && ret == 0)
494*0Sstevel@tonic-gate 		ret = t_ret;
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	/* Free any allocated space. */
497*0Sstevel@tonic-gate 	if (_lp != NULL)
498*0Sstevel@tonic-gate 		__os_free(_lp, file_dbp->pgsize);
499*0Sstevel@tonic-gate 	if (_rp != NULL)
500*0Sstevel@tonic-gate 		__os_free(_rp, file_dbp->pgsize);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	REC_CLOSE;
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate /*
506*0Sstevel@tonic-gate  * __bam_rsplit_recover --
507*0Sstevel@tonic-gate  *	Recovery function for a reverse split.
508*0Sstevel@tonic-gate  *
509*0Sstevel@tonic-gate  * PUBLIC: int __bam_rsplit_recover
510*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate int
__bam_rsplit_recover(logp,dbtp,lsnp,redo,info)513*0Sstevel@tonic-gate __bam_rsplit_recover(logp, dbtp, lsnp, redo, info)
514*0Sstevel@tonic-gate 	DB_LOG *logp;
515*0Sstevel@tonic-gate 	DBT *dbtp;
516*0Sstevel@tonic-gate 	DB_LSN *lsnp;
517*0Sstevel@tonic-gate 	int redo;
518*0Sstevel@tonic-gate 	void *info;
519*0Sstevel@tonic-gate {
520*0Sstevel@tonic-gate 	__bam_rsplit_args *argp;
521*0Sstevel@tonic-gate 	DB *file_dbp;
522*0Sstevel@tonic-gate 	DBC *dbc;
523*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
524*0Sstevel@tonic-gate 	PAGE *pagep;
525*0Sstevel@tonic-gate 	db_pgno_t pgno;
526*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	REC_PRINT(__bam_rsplit_print);
529*0Sstevel@tonic-gate 	REC_INTRO(__bam_rsplit_read);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/* Fix the root page. */
532*0Sstevel@tonic-gate 	pgno = PGNO_ROOT;
533*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &pgno, 0, &pagep)) != 0) {
534*0Sstevel@tonic-gate 		/* The root page must always exist. */
535*0Sstevel@tonic-gate 		__db_pgerr(file_dbp, pgno);
536*0Sstevel@tonic-gate 		goto out;
537*0Sstevel@tonic-gate 	}
538*0Sstevel@tonic-gate 	modified = 0;
539*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
540*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->rootlsn);
541*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
542*0Sstevel@tonic-gate 		/* Need to redo update described. */
543*0Sstevel@tonic-gate 		memcpy(pagep, argp->pgdbt.data, argp->pgdbt.size);
544*0Sstevel@tonic-gate 		pagep->pgno = PGNO_ROOT;
545*0Sstevel@tonic-gate 		pagep->lsn = *lsnp;
546*0Sstevel@tonic-gate 		modified = 1;
547*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
548*0Sstevel@tonic-gate 		/* Need to undo update described. */
549*0Sstevel@tonic-gate 		P_INIT(pagep, file_dbp->pgsize, PGNO_ROOT,
550*0Sstevel@tonic-gate 		    argp->nrec, PGNO_INVALID, pagep->level + 1,
551*0Sstevel@tonic-gate 		    file_dbp->type == DB_BTREE ? P_IBTREE : P_IRECNO);
552*0Sstevel@tonic-gate 		if ((ret = __db_pitem(dbc, pagep, 0,
553*0Sstevel@tonic-gate 		    argp->rootent.size, &argp->rootent, NULL)) != 0)
554*0Sstevel@tonic-gate 			goto out;
555*0Sstevel@tonic-gate 		pagep->lsn = argp->rootlsn;
556*0Sstevel@tonic-gate 		modified = 1;
557*0Sstevel@tonic-gate 	}
558*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
559*0Sstevel@tonic-gate 		goto out;
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	/*
562*0Sstevel@tonic-gate 	 * Fix the page copied over the root page.  It's possible that the
563*0Sstevel@tonic-gate 	 * page never made it to disk, so if we're undo-ing and the page
564*0Sstevel@tonic-gate 	 * doesn't exist, it's okay and there's nothing further to do.
565*0Sstevel@tonic-gate 	 */
566*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
567*0Sstevel@tonic-gate 		if (!redo)
568*0Sstevel@tonic-gate 			goto done;
569*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
570*0Sstevel@tonic-gate 		goto out;
571*0Sstevel@tonic-gate 	}
572*0Sstevel@tonic-gate 	modified = 0;
573*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
574*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &LSN(argp->pgdbt.data));
575*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
576*0Sstevel@tonic-gate 		/* Need to redo update described. */
577*0Sstevel@tonic-gate 		pagep->lsn = *lsnp;
578*0Sstevel@tonic-gate 		modified = 1;
579*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
580*0Sstevel@tonic-gate 		/* Need to undo update described. */
581*0Sstevel@tonic-gate 		memcpy(pagep, argp->pgdbt.data, argp->pgdbt.size);
582*0Sstevel@tonic-gate 		modified = 1;
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
585*0Sstevel@tonic-gate 		goto out;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
588*0Sstevel@tonic-gate 	ret = 0;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate out:	REC_CLOSE;
591*0Sstevel@tonic-gate }
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate /*
594*0Sstevel@tonic-gate  * __bam_adj_recover --
595*0Sstevel@tonic-gate  *	Recovery function for adj.
596*0Sstevel@tonic-gate  *
597*0Sstevel@tonic-gate  * PUBLIC: int __bam_adj_recover
598*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
599*0Sstevel@tonic-gate  */
600*0Sstevel@tonic-gate int
__bam_adj_recover(logp,dbtp,lsnp,redo,info)601*0Sstevel@tonic-gate __bam_adj_recover(logp, dbtp, lsnp, redo, info)
602*0Sstevel@tonic-gate 	DB_LOG *logp;
603*0Sstevel@tonic-gate 	DBT *dbtp;
604*0Sstevel@tonic-gate 	DB_LSN *lsnp;
605*0Sstevel@tonic-gate 	int redo;
606*0Sstevel@tonic-gate 	void *info;
607*0Sstevel@tonic-gate {
608*0Sstevel@tonic-gate 	__bam_adj_args *argp;
609*0Sstevel@tonic-gate 	DB *file_dbp;
610*0Sstevel@tonic-gate 	DBC *dbc;
611*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
612*0Sstevel@tonic-gate 	PAGE *pagep;
613*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	REC_PRINT(__bam_adj_print);
616*0Sstevel@tonic-gate 	REC_INTRO(__bam_adj_read);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	/* Get the page; if it never existed and we're undoing, we're done. */
619*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
620*0Sstevel@tonic-gate 		if (!redo)
621*0Sstevel@tonic-gate 			goto done;
622*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
623*0Sstevel@tonic-gate 		goto out;
624*0Sstevel@tonic-gate 	}
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	modified = 0;
627*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
628*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->lsn);
629*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
630*0Sstevel@tonic-gate 		/* Need to redo update described. */
631*0Sstevel@tonic-gate 		if ((ret = __bam_adjindx(dbc,
632*0Sstevel@tonic-gate 		    pagep, argp->indx, argp->indx_copy, argp->is_insert)) != 0)
633*0Sstevel@tonic-gate 			goto err;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 		LSN(pagep) = *lsnp;
636*0Sstevel@tonic-gate 		modified = 1;
637*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
638*0Sstevel@tonic-gate 		/* Need to undo update described. */
639*0Sstevel@tonic-gate 		if ((ret = __bam_adjindx(dbc,
640*0Sstevel@tonic-gate 		    pagep, argp->indx, argp->indx_copy, !argp->is_insert)) != 0)
641*0Sstevel@tonic-gate 			goto err;
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 		LSN(pagep) = argp->lsn;
644*0Sstevel@tonic-gate 		modified = 1;
645*0Sstevel@tonic-gate 	}
646*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
647*0Sstevel@tonic-gate 		goto out;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
650*0Sstevel@tonic-gate 	ret = 0;
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	if (0) {
653*0Sstevel@tonic-gate err:		(void)memp_fput(mpf, pagep, 0);
654*0Sstevel@tonic-gate 	}
655*0Sstevel@tonic-gate out:	REC_CLOSE;
656*0Sstevel@tonic-gate }
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate /*
659*0Sstevel@tonic-gate  * __bam_cadjust_recover --
660*0Sstevel@tonic-gate  *	Recovery function for the adjust of a count change in an internal
661*0Sstevel@tonic-gate  *	page.
662*0Sstevel@tonic-gate  *
663*0Sstevel@tonic-gate  * PUBLIC: int __bam_cadjust_recover
664*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
665*0Sstevel@tonic-gate  */
666*0Sstevel@tonic-gate int
__bam_cadjust_recover(logp,dbtp,lsnp,redo,info)667*0Sstevel@tonic-gate __bam_cadjust_recover(logp, dbtp, lsnp, redo, info)
668*0Sstevel@tonic-gate 	DB_LOG *logp;
669*0Sstevel@tonic-gate 	DBT *dbtp;
670*0Sstevel@tonic-gate 	DB_LSN *lsnp;
671*0Sstevel@tonic-gate 	int redo;
672*0Sstevel@tonic-gate 	void *info;
673*0Sstevel@tonic-gate {
674*0Sstevel@tonic-gate 	__bam_cadjust_args *argp;
675*0Sstevel@tonic-gate 	DB *file_dbp;
676*0Sstevel@tonic-gate 	DBC *dbc;
677*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
678*0Sstevel@tonic-gate 	PAGE *pagep;
679*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	REC_PRINT(__bam_cadjust_print);
682*0Sstevel@tonic-gate 	REC_INTRO(__bam_cadjust_read);
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	/* Get the page; if it never existed and we're undoing, we're done. */
685*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
686*0Sstevel@tonic-gate 		if (!redo)
687*0Sstevel@tonic-gate 			goto done;
688*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
689*0Sstevel@tonic-gate 		goto out;
690*0Sstevel@tonic-gate 	}
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	modified = 0;
693*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
694*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->lsn);
695*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
696*0Sstevel@tonic-gate 		/* Need to redo update described. */
697*0Sstevel@tonic-gate 		if (file_dbp->type == DB_BTREE &&
698*0Sstevel@tonic-gate 		    F_ISSET(file_dbp, DB_BT_RECNUM)) {
699*0Sstevel@tonic-gate 			GET_BINTERNAL(pagep, argp->indx)->nrecs += argp->adjust;
700*0Sstevel@tonic-gate 			if (argp->total && PGNO(pagep) == PGNO_ROOT)
701*0Sstevel@tonic-gate 				RE_NREC_ADJ(pagep, argp->adjust);
702*0Sstevel@tonic-gate 		}
703*0Sstevel@tonic-gate 		if (file_dbp->type == DB_RECNO) {
704*0Sstevel@tonic-gate 			GET_RINTERNAL(pagep, argp->indx)->nrecs += argp->adjust;
705*0Sstevel@tonic-gate 			if (argp->total && PGNO(pagep) == PGNO_ROOT)
706*0Sstevel@tonic-gate 				RE_NREC_ADJ(pagep, argp->adjust);
707*0Sstevel@tonic-gate 		}
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		LSN(pagep) = *lsnp;
710*0Sstevel@tonic-gate 		modified = 1;
711*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
712*0Sstevel@tonic-gate 		/* Need to undo update described. */
713*0Sstevel@tonic-gate 		if (file_dbp->type == DB_BTREE &&
714*0Sstevel@tonic-gate 		    F_ISSET(file_dbp, DB_BT_RECNUM)) {
715*0Sstevel@tonic-gate 			GET_BINTERNAL(pagep, argp->indx)->nrecs -= argp->adjust;
716*0Sstevel@tonic-gate 			if (argp->total && PGNO(pagep) == PGNO_ROOT)
717*0Sstevel@tonic-gate 				RE_NREC_ADJ(pagep, argp->adjust);
718*0Sstevel@tonic-gate 		}
719*0Sstevel@tonic-gate 		if (file_dbp->type == DB_RECNO) {
720*0Sstevel@tonic-gate 			GET_RINTERNAL(pagep, argp->indx)->nrecs -= argp->adjust;
721*0Sstevel@tonic-gate 			if (argp->total && PGNO(pagep) == PGNO_ROOT)
722*0Sstevel@tonic-gate 				RE_NREC_ADJ(pagep, -(argp->adjust));
723*0Sstevel@tonic-gate 		}
724*0Sstevel@tonic-gate 		LSN(pagep) = argp->lsn;
725*0Sstevel@tonic-gate 		modified = 1;
726*0Sstevel@tonic-gate 	}
727*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
728*0Sstevel@tonic-gate 		goto out;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
731*0Sstevel@tonic-gate 	ret = 0;
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate out:	REC_CLOSE;
734*0Sstevel@tonic-gate }
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate /*
737*0Sstevel@tonic-gate  * __bam_cdel_recover --
738*0Sstevel@tonic-gate  *	Recovery function for the intent-to-delete of a cursor record.
739*0Sstevel@tonic-gate  *
740*0Sstevel@tonic-gate  * PUBLIC: int __bam_cdel_recover
741*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
742*0Sstevel@tonic-gate  */
743*0Sstevel@tonic-gate int
__bam_cdel_recover(logp,dbtp,lsnp,redo,info)744*0Sstevel@tonic-gate __bam_cdel_recover(logp, dbtp, lsnp, redo, info)
745*0Sstevel@tonic-gate 	DB_LOG *logp;
746*0Sstevel@tonic-gate 	DBT *dbtp;
747*0Sstevel@tonic-gate 	DB_LSN *lsnp;
748*0Sstevel@tonic-gate 	int redo;
749*0Sstevel@tonic-gate 	void *info;
750*0Sstevel@tonic-gate {
751*0Sstevel@tonic-gate 	__bam_cdel_args *argp;
752*0Sstevel@tonic-gate 	DB *file_dbp;
753*0Sstevel@tonic-gate 	DBC *dbc;
754*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
755*0Sstevel@tonic-gate 	PAGE *pagep;
756*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	REC_PRINT(__bam_cdel_print);
759*0Sstevel@tonic-gate 	REC_INTRO(__bam_cdel_read);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	/* Get the page; if it never existed and we're undoing, we're done. */
762*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
763*0Sstevel@tonic-gate 		if (!redo)
764*0Sstevel@tonic-gate 			goto done;
765*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
766*0Sstevel@tonic-gate 		goto out;
767*0Sstevel@tonic-gate 	}
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	modified = 0;
770*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
771*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->lsn);
772*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
773*0Sstevel@tonic-gate 		/* Need to redo update described. */
774*0Sstevel@tonic-gate 		if (pagep->type == P_DUPLICATE)
775*0Sstevel@tonic-gate 			B_DSET(GET_BKEYDATA(pagep, argp->indx)->type);
776*0Sstevel@tonic-gate 		else
777*0Sstevel@tonic-gate 			B_DSET(GET_BKEYDATA(pagep, argp->indx + O_INDX)->type);
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 		LSN(pagep) = *lsnp;
780*0Sstevel@tonic-gate 		modified = 1;
781*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
782*0Sstevel@tonic-gate 		/* Need to undo update described. */
783*0Sstevel@tonic-gate 		if (pagep->type == P_DUPLICATE)
784*0Sstevel@tonic-gate 			B_DCLR(GET_BKEYDATA(pagep, argp->indx)->type);
785*0Sstevel@tonic-gate 		else
786*0Sstevel@tonic-gate 			B_DCLR(GET_BKEYDATA(pagep, argp->indx + O_INDX)->type);
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 		LSN(pagep) = argp->lsn;
789*0Sstevel@tonic-gate 		modified = 1;
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
792*0Sstevel@tonic-gate 		goto out;
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
795*0Sstevel@tonic-gate 	ret = 0;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate out:	REC_CLOSE;
798*0Sstevel@tonic-gate }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate /*
801*0Sstevel@tonic-gate  * __bam_repl_recover --
802*0Sstevel@tonic-gate  *	Recovery function for page item replacement.
803*0Sstevel@tonic-gate  *
804*0Sstevel@tonic-gate  * PUBLIC: int __bam_repl_recover
805*0Sstevel@tonic-gate  * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
806*0Sstevel@tonic-gate  */
807*0Sstevel@tonic-gate int
__bam_repl_recover(logp,dbtp,lsnp,redo,info)808*0Sstevel@tonic-gate __bam_repl_recover(logp, dbtp, lsnp, redo, info)
809*0Sstevel@tonic-gate 	DB_LOG *logp;
810*0Sstevel@tonic-gate 	DBT *dbtp;
811*0Sstevel@tonic-gate 	DB_LSN *lsnp;
812*0Sstevel@tonic-gate 	int redo;
813*0Sstevel@tonic-gate 	void *info;
814*0Sstevel@tonic-gate {
815*0Sstevel@tonic-gate 	__bam_repl_args *argp;
816*0Sstevel@tonic-gate 	BKEYDATA *bk;
817*0Sstevel@tonic-gate 	DB *file_dbp;
818*0Sstevel@tonic-gate 	DBC *dbc;
819*0Sstevel@tonic-gate 	DBT dbt;
820*0Sstevel@tonic-gate 	DB_MPOOLFILE *mpf;
821*0Sstevel@tonic-gate 	PAGE *pagep;
822*0Sstevel@tonic-gate 	int cmp_n, cmp_p, modified, ret;
823*0Sstevel@tonic-gate 	u_int8_t *p;
824*0Sstevel@tonic-gate 
825*0Sstevel@tonic-gate 	REC_PRINT(__bam_repl_print);
826*0Sstevel@tonic-gate 	REC_INTRO(__bam_repl_read);
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 	/* Get the page; if it never existed and we're undoing, we're done. */
829*0Sstevel@tonic-gate 	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0) {
830*0Sstevel@tonic-gate 		if (!redo)
831*0Sstevel@tonic-gate 			goto done;
832*0Sstevel@tonic-gate 		(void)__db_pgerr(file_dbp, argp->pgno);
833*0Sstevel@tonic-gate 		goto out;
834*0Sstevel@tonic-gate 	}
835*0Sstevel@tonic-gate 	bk = GET_BKEYDATA(pagep, argp->indx);
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 	modified = 0;
838*0Sstevel@tonic-gate 	cmp_n = log_compare(lsnp, &LSN(pagep));
839*0Sstevel@tonic-gate 	cmp_p = log_compare(&LSN(pagep), &argp->lsn);
840*0Sstevel@tonic-gate 	if (cmp_p == 0 && redo) {
841*0Sstevel@tonic-gate 		/*
842*0Sstevel@tonic-gate 		 * Need to redo update described.
843*0Sstevel@tonic-gate 		 *
844*0Sstevel@tonic-gate 		 * Re-build the replacement item.
845*0Sstevel@tonic-gate 		 */
846*0Sstevel@tonic-gate 		memset(&dbt, 0, sizeof(dbt));
847*0Sstevel@tonic-gate 		dbt.size = argp->prefix + argp->suffix + argp->repl.size;
848*0Sstevel@tonic-gate 		if ((ret = __os_malloc(dbt.size, NULL, &dbt.data)) != 0)
849*0Sstevel@tonic-gate 			goto err;
850*0Sstevel@tonic-gate 		p = dbt.data;
851*0Sstevel@tonic-gate 		memcpy(p, bk->data, argp->prefix);
852*0Sstevel@tonic-gate 		p += argp->prefix;
853*0Sstevel@tonic-gate 		memcpy(p, argp->repl.data, argp->repl.size);
854*0Sstevel@tonic-gate 		p += argp->repl.size;
855*0Sstevel@tonic-gate 		memcpy(p, bk->data + (bk->len - argp->suffix), argp->suffix);
856*0Sstevel@tonic-gate 
857*0Sstevel@tonic-gate 		ret = __bam_ritem(dbc, pagep, argp->indx, &dbt);
858*0Sstevel@tonic-gate 		__os_free(dbt.data, dbt.size);
859*0Sstevel@tonic-gate 		if (ret != 0)
860*0Sstevel@tonic-gate 			goto err;
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 		LSN(pagep) = *lsnp;
863*0Sstevel@tonic-gate 		modified = 1;
864*0Sstevel@tonic-gate 	} else if (cmp_n == 0 && !redo) {
865*0Sstevel@tonic-gate 		/*
866*0Sstevel@tonic-gate 		 * Need to undo update described.
867*0Sstevel@tonic-gate 		 *
868*0Sstevel@tonic-gate 		 * Re-build the original item.
869*0Sstevel@tonic-gate 		 */
870*0Sstevel@tonic-gate 		memset(&dbt, 0, sizeof(dbt));
871*0Sstevel@tonic-gate 		dbt.size = argp->prefix + argp->suffix + argp->orig.size;
872*0Sstevel@tonic-gate 		if ((ret = __os_malloc(dbt.size, NULL, &dbt.data)) != 0)
873*0Sstevel@tonic-gate 			goto err;
874*0Sstevel@tonic-gate 		p = dbt.data;
875*0Sstevel@tonic-gate 		memcpy(p, bk->data, argp->prefix);
876*0Sstevel@tonic-gate 		p += argp->prefix;
877*0Sstevel@tonic-gate 		memcpy(p, argp->orig.data, argp->orig.size);
878*0Sstevel@tonic-gate 		p += argp->orig.size;
879*0Sstevel@tonic-gate 		memcpy(p, bk->data + (bk->len - argp->suffix), argp->suffix);
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 		ret = __bam_ritem(dbc, pagep, argp->indx, &dbt);
882*0Sstevel@tonic-gate 		__os_free(dbt.data, dbt.size);
883*0Sstevel@tonic-gate 		if (ret != 0)
884*0Sstevel@tonic-gate 			goto err;
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 		/* Reset the deleted flag, if necessary. */
887*0Sstevel@tonic-gate 		if (argp->isdeleted)
888*0Sstevel@tonic-gate 			B_DSET(GET_BKEYDATA(pagep, argp->indx)->type);
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 		LSN(pagep) = argp->lsn;
891*0Sstevel@tonic-gate 		modified = 1;
892*0Sstevel@tonic-gate 	}
893*0Sstevel@tonic-gate 	if ((ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0)) != 0)
894*0Sstevel@tonic-gate 		goto out;
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate done:	*lsnp = argp->prev_lsn;
897*0Sstevel@tonic-gate 	ret = 0;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	if (0) {
900*0Sstevel@tonic-gate err:		(void)memp_fput(mpf, pagep, 0);
901*0Sstevel@tonic-gate 	}
902*0Sstevel@tonic-gate out:	REC_CLOSE;
903*0Sstevel@tonic-gate }
904