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