xref: /freebsd/lib/libc/db/btree/bt_open.c (revision c05ac53b8bbbbd998e18c6544011dd90f0f082da)
158f0484fSRodney W. Grimes /*-
2f1e396bcSPaul Traina  * Copyright (c) 1990, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Mike Olson.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3592927338SJason Evans  *
3692927338SJason Evans  * $FreeBSD$
3758f0484fSRodney W. Grimes  */
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
40f1e396bcSPaul Traina static char sccsid[] = "@(#)bt_open.c	8.10 (Berkeley) 8/17/94";
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes /*
4458f0484fSRodney W. Grimes  * Implementation of btree access method for 4.4BSD.
4558f0484fSRodney W. Grimes  *
4658f0484fSRodney W. Grimes  * The design here was originally based on that of the btree access method
4758f0484fSRodney W. Grimes  * used in the Postgres database system at UC Berkeley.  This implementation
4858f0484fSRodney W. Grimes  * is wholly independent of the Postgres code.
4958f0484fSRodney W. Grimes  */
5058f0484fSRodney W. Grimes 
51d201fe46SDaniel Eischen #include "namespace.h"
5258f0484fSRodney W. Grimes #include <sys/param.h>
5358f0484fSRodney W. Grimes #include <sys/stat.h>
5458f0484fSRodney W. Grimes 
5558f0484fSRodney W. Grimes #include <errno.h>
5658f0484fSRodney W. Grimes #include <fcntl.h>
5758f0484fSRodney W. Grimes #include <limits.h>
5858f0484fSRodney W. Grimes #include <signal.h>
5958f0484fSRodney W. Grimes #include <stdio.h>
6058f0484fSRodney W. Grimes #include <stdlib.h>
6158f0484fSRodney W. Grimes #include <string.h>
6258f0484fSRodney W. Grimes #include <unistd.h>
63d201fe46SDaniel Eischen #include "un-namespace.h"
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes #include <db.h>
6658f0484fSRodney W. Grimes #include "btree.h"
6758f0484fSRodney W. Grimes 
68f1e396bcSPaul Traina #ifdef DEBUG
69f1e396bcSPaul Traina #undef	MINPSIZE
70f1e396bcSPaul Traina #define	MINPSIZE	128
71f1e396bcSPaul Traina #endif
72f1e396bcSPaul Traina 
73c05ac53bSDavid E. O'Brien static int byteorder(void);
74c05ac53bSDavid E. O'Brien static int nroot(BTREE *);
75c05ac53bSDavid E. O'Brien static int tmp(void);
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes /*
7858f0484fSRodney W. Grimes  * __BT_OPEN -- Open a btree.
7958f0484fSRodney W. Grimes  *
8058f0484fSRodney W. Grimes  * Creates and fills a DB struct, and calls the routine that actually
8158f0484fSRodney W. Grimes  * opens the btree.
8258f0484fSRodney W. Grimes  *
8358f0484fSRodney W. Grimes  * Parameters:
8458f0484fSRodney W. Grimes  *	fname:	filename (NULL for in-memory trees)
8558f0484fSRodney W. Grimes  *	flags:	open flag bits
8658f0484fSRodney W. Grimes  *	mode:	open permission bits
8758f0484fSRodney W. Grimes  *	b:	BTREEINFO pointer
8858f0484fSRodney W. Grimes  *
8958f0484fSRodney W. Grimes  * Returns:
9058f0484fSRodney W. Grimes  *	NULL on failure, pointer to DB on success.
9158f0484fSRodney W. Grimes  *
9258f0484fSRodney W. Grimes  */
9358f0484fSRodney W. Grimes DB *
9458f0484fSRodney W. Grimes __bt_open(fname, flags, mode, openinfo, dflags)
9558f0484fSRodney W. Grimes 	const char *fname;
9658f0484fSRodney W. Grimes 	int flags, mode, dflags;
9758f0484fSRodney W. Grimes 	const BTREEINFO *openinfo;
9858f0484fSRodney W. Grimes {
9958f0484fSRodney W. Grimes 	struct stat sb;
10058f0484fSRodney W. Grimes 	BTMETA m;
10158f0484fSRodney W. Grimes 	BTREE *t;
10258f0484fSRodney W. Grimes 	BTREEINFO b;
10358f0484fSRodney W. Grimes 	DB *dbp;
10458f0484fSRodney W. Grimes 	pgno_t ncache;
10558f0484fSRodney W. Grimes 	ssize_t nr;
10658f0484fSRodney W. Grimes 	int machine_lorder;
10758f0484fSRodney W. Grimes 
10858f0484fSRodney W. Grimes 	t = NULL;
10958f0484fSRodney W. Grimes 
11058f0484fSRodney W. Grimes 	/*
11158f0484fSRodney W. Grimes 	 * Intention is to make sure all of the user's selections are okay
11258f0484fSRodney W. Grimes 	 * here and then use them without checking.  Can't be complete, since
11358f0484fSRodney W. Grimes 	 * we don't know the right page size, lorder or flags until the backing
11458f0484fSRodney W. Grimes 	 * file is opened.  Also, the file's page size can cause the cachesize
11558f0484fSRodney W. Grimes 	 * to change.
11658f0484fSRodney W. Grimes 	 */
11758f0484fSRodney W. Grimes 	machine_lorder = byteorder();
11858f0484fSRodney W. Grimes 	if (openinfo) {
11958f0484fSRodney W. Grimes 		b = *openinfo;
12058f0484fSRodney W. Grimes 
12158f0484fSRodney W. Grimes 		/* Flags: R_DUP. */
12258f0484fSRodney W. Grimes 		if (b.flags & ~(R_DUP))
12358f0484fSRodney W. Grimes 			goto einval;
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 		/*
12658f0484fSRodney W. Grimes 		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
12758f0484fSRodney W. Grimes 		 * page size is set farther on, based on the underlying file
12858f0484fSRodney W. Grimes 		 * transfer size.
12958f0484fSRodney W. Grimes 		 */
13058f0484fSRodney W. Grimes 		if (b.psize &&
13158f0484fSRodney W. Grimes 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
13251295a4dSJordan K. Hubbard 		    b.psize & (sizeof(indx_t) - 1) ))
13358f0484fSRodney W. Grimes 			goto einval;
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes 		/* Minimum number of keys per page; absolute minimum is 2. */
13658f0484fSRodney W. Grimes 		if (b.minkeypage) {
13758f0484fSRodney W. Grimes 			if (b.minkeypage < 2)
13858f0484fSRodney W. Grimes 				goto einval;
13958f0484fSRodney W. Grimes 		} else
14058f0484fSRodney W. Grimes 			b.minkeypage = DEFMINKEYPAGE;
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes 		/* If no comparison, use default comparison and prefix. */
14358f0484fSRodney W. Grimes 		if (b.compare == NULL) {
14458f0484fSRodney W. Grimes 			b.compare = __bt_defcmp;
14558f0484fSRodney W. Grimes 			if (b.prefix == NULL)
14658f0484fSRodney W. Grimes 				b.prefix = __bt_defpfx;
14758f0484fSRodney W. Grimes 		}
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes 		if (b.lorder == 0)
15058f0484fSRodney W. Grimes 			b.lorder = machine_lorder;
15158f0484fSRodney W. Grimes 	} else {
15258f0484fSRodney W. Grimes 		b.compare = __bt_defcmp;
15358f0484fSRodney W. Grimes 		b.cachesize = 0;
15458f0484fSRodney W. Grimes 		b.flags = 0;
15558f0484fSRodney W. Grimes 		b.lorder = machine_lorder;
15658f0484fSRodney W. Grimes 		b.minkeypage = DEFMINKEYPAGE;
15758f0484fSRodney W. Grimes 		b.prefix = __bt_defpfx;
15858f0484fSRodney W. Grimes 		b.psize = 0;
15958f0484fSRodney W. Grimes 	}
16058f0484fSRodney W. Grimes 
16158f0484fSRodney W. Grimes 	/* Check for the ubiquitous PDP-11. */
16258f0484fSRodney W. Grimes 	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
16358f0484fSRodney W. Grimes 		goto einval;
16458f0484fSRodney W. Grimes 
16558f0484fSRodney W. Grimes 	/* Allocate and initialize DB and BTREE structures. */
16658f0484fSRodney W. Grimes 	if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
16758f0484fSRodney W. Grimes 		goto err;
16858f0484fSRodney W. Grimes 	memset(t, 0, sizeof(BTREE));
16958f0484fSRodney W. Grimes 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
17058f0484fSRodney W. Grimes 	t->bt_lorder = b.lorder;
17158f0484fSRodney W. Grimes 	t->bt_order = NOT;
17258f0484fSRodney W. Grimes 	t->bt_cmp = b.compare;
17358f0484fSRodney W. Grimes 	t->bt_pfx = b.prefix;
17458f0484fSRodney W. Grimes 	t->bt_rfd = -1;
17558f0484fSRodney W. Grimes 
17658f0484fSRodney W. Grimes 	if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
17758f0484fSRodney W. Grimes 		goto err;
178f1e396bcSPaul Traina 	memset(t->bt_dbp, 0, sizeof(DB));
17958f0484fSRodney W. Grimes 	if (t->bt_lorder != machine_lorder)
180f1e396bcSPaul Traina 		F_SET(t, B_NEEDSWAP);
18158f0484fSRodney W. Grimes 
18258f0484fSRodney W. Grimes 	dbp->type = DB_BTREE;
18358f0484fSRodney W. Grimes 	dbp->internal = t;
18458f0484fSRodney W. Grimes 	dbp->close = __bt_close;
18558f0484fSRodney W. Grimes 	dbp->del = __bt_delete;
18658f0484fSRodney W. Grimes 	dbp->fd = __bt_fd;
18758f0484fSRodney W. Grimes 	dbp->get = __bt_get;
18858f0484fSRodney W. Grimes 	dbp->put = __bt_put;
18958f0484fSRodney W. Grimes 	dbp->seq = __bt_seq;
19058f0484fSRodney W. Grimes 	dbp->sync = __bt_sync;
19158f0484fSRodney W. Grimes 
19258f0484fSRodney W. Grimes 	/*
19358f0484fSRodney W. Grimes 	 * If no file name was supplied, this is an in-memory btree and we
19458f0484fSRodney W. Grimes 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
19558f0484fSRodney W. Grimes 	 */
19658f0484fSRodney W. Grimes 	if (fname) {
19758f0484fSRodney W. Grimes 		switch (flags & O_ACCMODE) {
19858f0484fSRodney W. Grimes 		case O_RDONLY:
199f1e396bcSPaul Traina 			F_SET(t, B_RDONLY);
20058f0484fSRodney W. Grimes 			break;
20158f0484fSRodney W. Grimes 		case O_RDWR:
20258f0484fSRodney W. Grimes 			break;
20358f0484fSRodney W. Grimes 		case O_WRONLY:
20458f0484fSRodney W. Grimes 		default:
20558f0484fSRodney W. Grimes 			goto einval;
20658f0484fSRodney W. Grimes 		}
20758f0484fSRodney W. Grimes 
2089233c4d9SJason Evans 		if ((t->bt_fd = _open(fname, flags, mode)) < 0)
20958f0484fSRodney W. Grimes 			goto err;
21058f0484fSRodney W. Grimes 
21158f0484fSRodney W. Grimes 	} else {
21258f0484fSRodney W. Grimes 		if ((flags & O_ACCMODE) != O_RDWR)
21358f0484fSRodney W. Grimes 			goto einval;
21458f0484fSRodney W. Grimes 		if ((t->bt_fd = tmp()) == -1)
21558f0484fSRodney W. Grimes 			goto err;
216f1e396bcSPaul Traina 		F_SET(t, B_INMEM);
21758f0484fSRodney W. Grimes 	}
21858f0484fSRodney W. Grimes 
2199233c4d9SJason Evans 	if (_fcntl(t->bt_fd, F_SETFD, 1) == -1)
22058f0484fSRodney W. Grimes 		goto err;
22158f0484fSRodney W. Grimes 
222d201fe46SDaniel Eischen 	if (_fstat(t->bt_fd, &sb))
22358f0484fSRodney W. Grimes 		goto err;
22458f0484fSRodney W. Grimes 	if (sb.st_size) {
2259233c4d9SJason Evans 		if ((nr = _read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
22658f0484fSRodney W. Grimes 			goto err;
22758f0484fSRodney W. Grimes 		if (nr != sizeof(BTMETA))
22858f0484fSRodney W. Grimes 			goto eftype;
22958f0484fSRodney W. Grimes 
23058f0484fSRodney W. Grimes 		/*
23158f0484fSRodney W. Grimes 		 * Read in the meta-data.  This can change the notion of what
23258f0484fSRodney W. Grimes 		 * the lorder, page size and flags are, and, when the page size
23358f0484fSRodney W. Grimes 		 * changes, the cachesize value can change too.  If the user
23458f0484fSRodney W. Grimes 		 * specified the wrong byte order for an existing database, we
23558f0484fSRodney W. Grimes 		 * don't bother to return an error, we just clear the NEEDSWAP
23658f0484fSRodney W. Grimes 		 * bit.
23758f0484fSRodney W. Grimes 		 */
238f1e396bcSPaul Traina 		if (m.magic == BTREEMAGIC)
239f1e396bcSPaul Traina 			F_CLR(t, B_NEEDSWAP);
24058f0484fSRodney W. Grimes 		else {
241f1e396bcSPaul Traina 			F_SET(t, B_NEEDSWAP);
242f1e396bcSPaul Traina 			M_32_SWAP(m.magic);
243f1e396bcSPaul Traina 			M_32_SWAP(m.version);
244f1e396bcSPaul Traina 			M_32_SWAP(m.psize);
245f1e396bcSPaul Traina 			M_32_SWAP(m.free);
246f1e396bcSPaul Traina 			M_32_SWAP(m.nrecs);
247f1e396bcSPaul Traina 			M_32_SWAP(m.flags);
24858f0484fSRodney W. Grimes 		}
249f1e396bcSPaul Traina 		if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
25058f0484fSRodney W. Grimes 			goto eftype;
251f1e396bcSPaul Traina 		if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
25251295a4dSJordan K. Hubbard 		    m.psize & (sizeof(indx_t) - 1) )
25358f0484fSRodney W. Grimes 			goto eftype;
254f1e396bcSPaul Traina 		if (m.flags & ~SAVEMETA)
25558f0484fSRodney W. Grimes 			goto eftype;
256f1e396bcSPaul Traina 		b.psize = m.psize;
257f1e396bcSPaul Traina 		F_SET(t, m.flags);
258f1e396bcSPaul Traina 		t->bt_free = m.free;
259f1e396bcSPaul Traina 		t->bt_nrecs = m.nrecs;
26058f0484fSRodney W. Grimes 	} else {
26158f0484fSRodney W. Grimes 		/*
26258f0484fSRodney W. Grimes 		 * Set the page size to the best value for I/O to this file.
26358f0484fSRodney W. Grimes 		 * Don't overflow the page offset type.
26458f0484fSRodney W. Grimes 		 */
26558f0484fSRodney W. Grimes 		if (b.psize == 0) {
26658f0484fSRodney W. Grimes 			b.psize = sb.st_blksize;
26758f0484fSRodney W. Grimes 			if (b.psize < MINPSIZE)
26858f0484fSRodney W. Grimes 				b.psize = MINPSIZE;
26958f0484fSRodney W. Grimes 			if (b.psize > MAX_PAGE_OFFSET + 1)
27058f0484fSRodney W. Grimes 				b.psize = MAX_PAGE_OFFSET + 1;
27158f0484fSRodney W. Grimes 		}
27258f0484fSRodney W. Grimes 
27358f0484fSRodney W. Grimes 		/* Set flag if duplicates permitted. */
27458f0484fSRodney W. Grimes 		if (!(b.flags & R_DUP))
275f1e396bcSPaul Traina 			F_SET(t, B_NODUPS);
27658f0484fSRodney W. Grimes 
27758f0484fSRodney W. Grimes 		t->bt_free = P_INVALID;
27858f0484fSRodney W. Grimes 		t->bt_nrecs = 0;
279f1e396bcSPaul Traina 		F_SET(t, B_METADIRTY);
28058f0484fSRodney W. Grimes 	}
28158f0484fSRodney W. Grimes 
28258f0484fSRodney W. Grimes 	t->bt_psize = b.psize;
28358f0484fSRodney W. Grimes 
28458f0484fSRodney W. Grimes 	/* Set the cache size; must be a multiple of the page size. */
28551295a4dSJordan K. Hubbard 	if (b.cachesize && b.cachesize & (b.psize - 1) )
28651295a4dSJordan K. Hubbard 		b.cachesize += (~b.cachesize & (b.psize - 1) ) + 1;
28758f0484fSRodney W. Grimes 	if (b.cachesize < b.psize * MINCACHE)
28858f0484fSRodney W. Grimes 		b.cachesize = b.psize * MINCACHE;
28958f0484fSRodney W. Grimes 
29058f0484fSRodney W. Grimes 	/* Calculate number of pages to cache. */
29158f0484fSRodney W. Grimes 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
29258f0484fSRodney W. Grimes 
29358f0484fSRodney W. Grimes 	/*
29458f0484fSRodney W. Grimes 	 * The btree data structure requires that at least two keys can fit on
29558f0484fSRodney W. Grimes 	 * a page, but other than that there's no fixed requirement.  The user
29658f0484fSRodney W. Grimes 	 * specified a minimum number per page, and we translated that into the
29758f0484fSRodney W. Grimes 	 * number of bytes a key/data pair can use before being placed on an
29858f0484fSRodney W. Grimes 	 * overflow page.  This calculation includes the page header, the size
29958f0484fSRodney W. Grimes 	 * of the index referencing the leaf item and the size of the leaf item
30058f0484fSRodney W. Grimes 	 * structure.  Also, don't let the user specify a minkeypage such that
30158f0484fSRodney W. Grimes 	 * a key/data pair won't fit even if both key and data are on overflow
30258f0484fSRodney W. Grimes 	 * pages.
30358f0484fSRodney W. Grimes 	 */
30458f0484fSRodney W. Grimes 	t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
30558f0484fSRodney W. Grimes 	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
30658f0484fSRodney W. Grimes 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
30758f0484fSRodney W. Grimes 		t->bt_ovflsize =
30858f0484fSRodney W. Grimes 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
30958f0484fSRodney W. Grimes 
31058f0484fSRodney W. Grimes 	/* Initialize the buffer pool. */
31158f0484fSRodney W. Grimes 	if ((t->bt_mp =
31258f0484fSRodney W. Grimes 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
31358f0484fSRodney W. Grimes 		goto err;
314f1e396bcSPaul Traina 	if (!F_ISSET(t, B_INMEM))
31558f0484fSRodney W. Grimes 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
31658f0484fSRodney W. Grimes 
31758f0484fSRodney W. Grimes 	/* Create a root page if new tree. */
31858f0484fSRodney W. Grimes 	if (nroot(t) == RET_ERROR)
31958f0484fSRodney W. Grimes 		goto err;
32058f0484fSRodney W. Grimes 
32158f0484fSRodney W. Grimes 	/* Global flags. */
32258f0484fSRodney W. Grimes 	if (dflags & DB_LOCK)
323f1e396bcSPaul Traina 		F_SET(t, B_DB_LOCK);
32458f0484fSRodney W. Grimes 	if (dflags & DB_SHMEM)
325f1e396bcSPaul Traina 		F_SET(t, B_DB_SHMEM);
32658f0484fSRodney W. Grimes 	if (dflags & DB_TXN)
327f1e396bcSPaul Traina 		F_SET(t, B_DB_TXN);
32858f0484fSRodney W. Grimes 
32958f0484fSRodney W. Grimes 	return (dbp);
33058f0484fSRodney W. Grimes 
33158f0484fSRodney W. Grimes einval:	errno = EINVAL;
33258f0484fSRodney W. Grimes 	goto err;
33358f0484fSRodney W. Grimes 
33458f0484fSRodney W. Grimes eftype:	errno = EFTYPE;
33558f0484fSRodney W. Grimes 	goto err;
33658f0484fSRodney W. Grimes 
33758f0484fSRodney W. Grimes err:	if (t) {
33858f0484fSRodney W. Grimes 		if (t->bt_dbp)
33958f0484fSRodney W. Grimes 			free(t->bt_dbp);
34058f0484fSRodney W. Grimes 		if (t->bt_fd != -1)
3419233c4d9SJason Evans 			(void)_close(t->bt_fd);
34258f0484fSRodney W. Grimes 		free(t);
34358f0484fSRodney W. Grimes 	}
34458f0484fSRodney W. Grimes 	return (NULL);
34558f0484fSRodney W. Grimes }
34658f0484fSRodney W. Grimes 
34758f0484fSRodney W. Grimes /*
34858f0484fSRodney W. Grimes  * NROOT -- Create the root of a new tree.
34958f0484fSRodney W. Grimes  *
35058f0484fSRodney W. Grimes  * Parameters:
35158f0484fSRodney W. Grimes  *	t:	tree
35258f0484fSRodney W. Grimes  *
35358f0484fSRodney W. Grimes  * Returns:
35458f0484fSRodney W. Grimes  *	RET_ERROR, RET_SUCCESS
35558f0484fSRodney W. Grimes  */
35658f0484fSRodney W. Grimes static int
35758f0484fSRodney W. Grimes nroot(t)
35858f0484fSRodney W. Grimes 	BTREE *t;
35958f0484fSRodney W. Grimes {
36058f0484fSRodney W. Grimes 	PAGE *meta, *root;
36158f0484fSRodney W. Grimes 	pgno_t npg;
36258f0484fSRodney W. Grimes 
36358f0484fSRodney W. Grimes 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
36458f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, meta, 0);
36558f0484fSRodney W. Grimes 		return (RET_SUCCESS);
36658f0484fSRodney W. Grimes 	}
367f1e396bcSPaul Traina 	if (errno != EINVAL)		/* It's OK to not exist. */
36858f0484fSRodney W. Grimes 		return (RET_ERROR);
369f1e396bcSPaul Traina 	errno = 0;
37058f0484fSRodney W. Grimes 
37158f0484fSRodney W. Grimes 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
37258f0484fSRodney W. Grimes 		return (RET_ERROR);
37358f0484fSRodney W. Grimes 
37458f0484fSRodney W. Grimes 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
37558f0484fSRodney W. Grimes 		return (RET_ERROR);
37658f0484fSRodney W. Grimes 
37758f0484fSRodney W. Grimes 	if (npg != P_ROOT)
37858f0484fSRodney W. Grimes 		return (RET_ERROR);
37958f0484fSRodney W. Grimes 	root->pgno = npg;
38058f0484fSRodney W. Grimes 	root->prevpg = root->nextpg = P_INVALID;
38158f0484fSRodney W. Grimes 	root->lower = BTDATAOFF;
38258f0484fSRodney W. Grimes 	root->upper = t->bt_psize;
38358f0484fSRodney W. Grimes 	root->flags = P_BLEAF;
38458f0484fSRodney W. Grimes 	memset(meta, 0, t->bt_psize);
38558f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
38658f0484fSRodney W. Grimes 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
38758f0484fSRodney W. Grimes 	return (RET_SUCCESS);
38858f0484fSRodney W. Grimes }
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes static int
39158f0484fSRodney W. Grimes tmp()
39258f0484fSRodney W. Grimes {
39358f0484fSRodney W. Grimes 	sigset_t set, oset;
39458f0484fSRodney W. Grimes 	int fd;
39577740e7eSWarner Losh 	char *envtmp = NULL;
39658f0484fSRodney W. Grimes 	char path[MAXPATHLEN];
39758f0484fSRodney W. Grimes 
39877740e7eSWarner Losh 	if (issetugid() == 0)
39958f0484fSRodney W. Grimes 		envtmp = getenv("TMPDIR");
40058f0484fSRodney W. Grimes 	(void)snprintf(path,
401ce41d42aSKris Kennaway 	    sizeof(path), "%s/bt.XXXXXXXXXX", envtmp ? envtmp : "/tmp");
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 	(void)sigfillset(&set);
404d201fe46SDaniel Eischen 	(void)_sigprocmask(SIG_BLOCK, &set, &oset);
40558f0484fSRodney W. Grimes 	if ((fd = mkstemp(path)) != -1)
40658f0484fSRodney W. Grimes 		(void)unlink(path);
407d201fe46SDaniel Eischen 	(void)_sigprocmask(SIG_SETMASK, &oset, NULL);
40858f0484fSRodney W. Grimes 	return(fd);
40958f0484fSRodney W. Grimes }
41058f0484fSRodney W. Grimes 
41158f0484fSRodney W. Grimes static int
41258f0484fSRodney W. Grimes byteorder()
41358f0484fSRodney W. Grimes {
41458f0484fSRodney W. Grimes 	u_int32_t x;
41558f0484fSRodney W. Grimes 	u_char *p;
41658f0484fSRodney W. Grimes 
41758f0484fSRodney W. Grimes 	x = 0x01020304;
41858f0484fSRodney W. Grimes 	p = (u_char *)&x;
41958f0484fSRodney W. Grimes 	switch (*p) {
42058f0484fSRodney W. Grimes 	case 1:
42158f0484fSRodney W. Grimes 		return (BIG_ENDIAN);
42258f0484fSRodney W. Grimes 	case 4:
42358f0484fSRodney W. Grimes 		return (LITTLE_ENDIAN);
42458f0484fSRodney W. Grimes 	default:
42558f0484fSRodney W. Grimes 		return (0);
42658f0484fSRodney W. Grimes 	}
42758f0484fSRodney W. Grimes }
42858f0484fSRodney W. Grimes 
42958f0484fSRodney W. Grimes int
43058f0484fSRodney W. Grimes __bt_fd(dbp)
43158f0484fSRodney W. Grimes         const DB *dbp;
43258f0484fSRodney W. Grimes {
43358f0484fSRodney W. Grimes 	BTREE *t;
43458f0484fSRodney W. Grimes 
43558f0484fSRodney W. Grimes 	t = dbp->internal;
43658f0484fSRodney W. Grimes 
43758f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
43858f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
43958f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
44058f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
44158f0484fSRodney W. Grimes 	}
44258f0484fSRodney W. Grimes 
44358f0484fSRodney W. Grimes 	/* In-memory database can't have a file descriptor. */
444f1e396bcSPaul Traina 	if (F_ISSET(t, B_INMEM)) {
44558f0484fSRodney W. Grimes 		errno = ENOENT;
44658f0484fSRodney W. Grimes 		return (-1);
44758f0484fSRodney W. Grimes 	}
44858f0484fSRodney W. Grimes 	return (t->bt_fd);
44958f0484fSRodney W. Grimes }
450