Home | History | Annotate | Line # | Download | only in btree
bt_open.c revision 1.24.6.2
      1  1.24.6.2  joerg /*	$NetBSD: bt_open.c,v 1.24.6.2 2008/09/11 12:58:01 joerg Exp $	*/
      2  1.24.6.2  joerg 
      3  1.24.6.2  joerg /*-
      4  1.24.6.2  joerg  * Copyright (c) 1990, 1993, 1994
      5  1.24.6.2  joerg  *	The Regents of the University of California.  All rights reserved.
      6  1.24.6.2  joerg  *
      7  1.24.6.2  joerg  * This code is derived from software contributed to Berkeley by
      8  1.24.6.2  joerg  * Mike Olson.
      9  1.24.6.2  joerg  *
     10  1.24.6.2  joerg  * Redistribution and use in source and binary forms, with or without
     11  1.24.6.2  joerg  * modification, are permitted provided that the following conditions
     12  1.24.6.2  joerg  * are met:
     13  1.24.6.2  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.24.6.2  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.24.6.2  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.24.6.2  joerg  *    notice, this list of conditions and the following disclaimer in the
     17  1.24.6.2  joerg  *    documentation and/or other materials provided with the distribution.
     18  1.24.6.2  joerg  * 3. Neither the name of the University nor the names of its contributors
     19  1.24.6.2  joerg  *    may be used to endorse or promote products derived from this software
     20  1.24.6.2  joerg  *    without specific prior written permission.
     21  1.24.6.2  joerg  *
     22  1.24.6.2  joerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.24.6.2  joerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.24.6.2  joerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.24.6.2  joerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.24.6.2  joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.24.6.2  joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.24.6.2  joerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.24.6.2  joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.24.6.2  joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.24.6.2  joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.24.6.2  joerg  * SUCH DAMAGE.
     33  1.24.6.2  joerg  */
     34  1.24.6.2  joerg 
     35  1.24.6.2  joerg #if HAVE_NBTOOL_CONFIG_H
     36  1.24.6.2  joerg #include "nbtool_config.h"
     37  1.24.6.2  joerg #endif
     38  1.24.6.2  joerg 
     39  1.24.6.2  joerg #include <sys/cdefs.h>
     40  1.24.6.2  joerg __RCSID("$NetBSD: bt_open.c,v 1.24.6.2 2008/09/11 12:58:01 joerg Exp $");
     41  1.24.6.2  joerg 
     42  1.24.6.2  joerg /*
     43  1.24.6.2  joerg  * Implementation of btree access method for 4.4BSD.
     44  1.24.6.2  joerg  *
     45  1.24.6.2  joerg  * The design here was originally based on that of the btree access method
     46  1.24.6.2  joerg  * used in the Postgres database system at UC Berkeley.  This implementation
     47  1.24.6.2  joerg  * is wholly independent of the Postgres code.
     48  1.24.6.2  joerg  */
     49  1.24.6.2  joerg 
     50  1.24.6.2  joerg #include "namespace.h"
     51  1.24.6.2  joerg #include <sys/stat.h>
     52  1.24.6.2  joerg 
     53  1.24.6.2  joerg #include <assert.h>
     54  1.24.6.2  joerg #include <errno.h>
     55  1.24.6.2  joerg #include <fcntl.h>
     56  1.24.6.2  joerg #include <limits.h>
     57  1.24.6.2  joerg #include <signal.h>
     58  1.24.6.2  joerg #include <stdio.h>
     59  1.24.6.2  joerg #include <stdlib.h>
     60  1.24.6.2  joerg #include <string.h>
     61  1.24.6.2  joerg #include <unistd.h>
     62  1.24.6.2  joerg #include <paths.h>
     63  1.24.6.2  joerg 
     64  1.24.6.2  joerg #include <db.h>
     65  1.24.6.2  joerg #include "btree.h"
     66  1.24.6.2  joerg 
     67  1.24.6.2  joerg #ifdef DEBUG
     68  1.24.6.2  joerg #undef	MINPSIZE
     69  1.24.6.2  joerg #define	MINPSIZE	128
     70  1.24.6.2  joerg #endif
     71  1.24.6.2  joerg 
     72  1.24.6.2  joerg static int byteorder(void);
     73  1.24.6.2  joerg static int nroot(BTREE *);
     74  1.24.6.2  joerg static int tmp(void);
     75  1.24.6.2  joerg 
     76  1.24.6.2  joerg /*
     77  1.24.6.2  joerg  * __BT_OPEN -- Open a btree.
     78  1.24.6.2  joerg  *
     79  1.24.6.2  joerg  * Creates and fills a DB struct, and calls the routine that actually
     80  1.24.6.2  joerg  * opens the btree.
     81  1.24.6.2  joerg  *
     82  1.24.6.2  joerg  * Parameters:
     83  1.24.6.2  joerg  *	fname:	filename (NULL for in-memory trees)
     84  1.24.6.2  joerg  *	flags:	open flag bits
     85  1.24.6.2  joerg  *	mode:	open permission bits
     86  1.24.6.2  joerg  *	b:	BTREEINFO pointer
     87  1.24.6.2  joerg  *
     88  1.24.6.2  joerg  * Returns:
     89  1.24.6.2  joerg  *	NULL on failure, pointer to DB on success.
     90  1.24.6.2  joerg  *
     91  1.24.6.2  joerg  */
     92  1.24.6.2  joerg DB *
     93  1.24.6.2  joerg __bt_open(const char *fname, int flags, mode_t mode, const BTREEINFO *openinfo,
     94  1.24.6.2  joerg     int dflags)
     95  1.24.6.2  joerg {
     96  1.24.6.2  joerg 	struct stat sb;
     97  1.24.6.2  joerg 	BTMETA m;
     98  1.24.6.2  joerg 	BTREE *t;
     99  1.24.6.2  joerg 	BTREEINFO b;
    100  1.24.6.2  joerg 	DB *dbp;
    101  1.24.6.2  joerg 	pgno_t ncache;
    102  1.24.6.2  joerg 	ssize_t nr;
    103  1.24.6.2  joerg 	size_t temp;
    104  1.24.6.2  joerg 	int machine_lorder;
    105  1.24.6.2  joerg 
    106  1.24.6.2  joerg 	t = NULL;
    107  1.24.6.2  joerg 
    108  1.24.6.2  joerg 	/*
    109  1.24.6.2  joerg 	 * Intention is to make sure all of the user's selections are okay
    110  1.24.6.2  joerg 	 * here and then use them without checking.  Can't be complete, since
    111  1.24.6.2  joerg 	 * we don't know the right page size, lorder or flags until the backing
    112  1.24.6.2  joerg 	 * file is opened.  Also, the file's page size can cause the cachesize
    113  1.24.6.2  joerg 	 * to change.
    114  1.24.6.2  joerg 	 */
    115  1.24.6.2  joerg 	machine_lorder = byteorder();
    116  1.24.6.2  joerg 	if (openinfo) {
    117  1.24.6.2  joerg 		b = *openinfo;
    118  1.24.6.2  joerg 
    119  1.24.6.2  joerg 		/* Flags: R_DUP. */
    120  1.24.6.2  joerg 		if (b.flags & ~(R_DUP))
    121  1.24.6.2  joerg 			goto einval;
    122  1.24.6.2  joerg 
    123  1.24.6.2  joerg 		/*
    124  1.24.6.2  joerg 		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
    125  1.24.6.2  joerg 		 * page size is set farther on, based on the underlying file
    126  1.24.6.2  joerg 		 * transfer size.
    127  1.24.6.2  joerg 		 */
    128  1.24.6.2  joerg 		if (b.psize &&
    129  1.24.6.2  joerg 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
    130  1.24.6.2  joerg 		    b.psize & (sizeof(indx_t) - 1)))
    131  1.24.6.2  joerg 			goto einval;
    132  1.24.6.2  joerg 
    133  1.24.6.2  joerg 		/* Minimum number of keys per page; absolute minimum is 2. */
    134  1.24.6.2  joerg 		if (b.minkeypage) {
    135  1.24.6.2  joerg 			if (b.minkeypage < 2)
    136  1.24.6.2  joerg 				goto einval;
    137  1.24.6.2  joerg 		} else
    138  1.24.6.2  joerg 			b.minkeypage = DEFMINKEYPAGE;
    139  1.24.6.2  joerg 
    140  1.24.6.2  joerg 		/* If no comparison, use default comparison and prefix. */
    141  1.24.6.2  joerg 		if (b.compare == NULL) {
    142  1.24.6.2  joerg 			b.compare = __bt_defcmp;
    143  1.24.6.2  joerg 			if (b.prefix == NULL)
    144  1.24.6.2  joerg 				b.prefix = __bt_defpfx;
    145  1.24.6.2  joerg 		}
    146  1.24.6.2  joerg 
    147  1.24.6.2  joerg 		if (b.lorder == 0)
    148  1.24.6.2  joerg 			b.lorder = machine_lorder;
    149  1.24.6.2  joerg 	} else {
    150  1.24.6.2  joerg 		b.compare = __bt_defcmp;
    151  1.24.6.2  joerg 		b.cachesize = 0;
    152  1.24.6.2  joerg 		b.flags = 0;
    153  1.24.6.2  joerg 		b.lorder = machine_lorder;
    154  1.24.6.2  joerg 		b.minkeypage = DEFMINKEYPAGE;
    155  1.24.6.2  joerg 		b.prefix = __bt_defpfx;
    156  1.24.6.2  joerg 		b.psize = 0;
    157  1.24.6.2  joerg 	}
    158  1.24.6.2  joerg 
    159  1.24.6.2  joerg 	/* Check for the ubiquitous PDP-11. */
    160  1.24.6.2  joerg 	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
    161  1.24.6.2  joerg 		goto einval;
    162  1.24.6.2  joerg 
    163  1.24.6.2  joerg 	/* Allocate and initialize DB and BTREE structures. */
    164  1.24.6.2  joerg 	if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
    165  1.24.6.2  joerg 		goto err;
    166  1.24.6.2  joerg 	memset(t, 0, sizeof(BTREE));
    167  1.24.6.2  joerg 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
    168  1.24.6.2  joerg 	t->bt_lorder = b.lorder;
    169  1.24.6.2  joerg 	t->bt_order = NOT;
    170  1.24.6.2  joerg 	t->bt_cmp = b.compare;
    171  1.24.6.2  joerg 	t->bt_pfx = b.prefix;
    172  1.24.6.2  joerg 	t->bt_rfd = -1;
    173  1.24.6.2  joerg 
    174  1.24.6.2  joerg 	if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
    175  1.24.6.2  joerg 		goto err;
    176  1.24.6.2  joerg 	memset(t->bt_dbp, 0, sizeof(DB));
    177  1.24.6.2  joerg 	if (t->bt_lorder != machine_lorder)
    178  1.24.6.2  joerg 		F_SET(t, B_NEEDSWAP);
    179  1.24.6.2  joerg 
    180  1.24.6.2  joerg 	dbp->type = DB_BTREE;
    181  1.24.6.2  joerg 	dbp->internal = t;
    182  1.24.6.2  joerg 	dbp->close = __bt_close;
    183  1.24.6.2  joerg 	dbp->del = __bt_delete;
    184  1.24.6.2  joerg 	dbp->fd = __bt_fd;
    185  1.24.6.2  joerg 	dbp->get = __bt_get;
    186  1.24.6.2  joerg 	dbp->put = __bt_put;
    187  1.24.6.2  joerg 	dbp->seq = __bt_seq;
    188  1.24.6.2  joerg 	dbp->sync = __bt_sync;
    189  1.24.6.2  joerg 
    190  1.24.6.2  joerg 	/*
    191  1.24.6.2  joerg 	 * If no file name was supplied, this is an in-memory btree and we
    192  1.24.6.2  joerg 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
    193  1.24.6.2  joerg 	 */
    194  1.24.6.2  joerg 	if (fname) {
    195  1.24.6.2  joerg 		switch (flags & O_ACCMODE) {
    196  1.24.6.2  joerg 		case O_RDONLY:
    197  1.24.6.2  joerg 			F_SET(t, B_RDONLY);
    198  1.24.6.2  joerg 			break;
    199  1.24.6.2  joerg 		case O_RDWR:
    200  1.24.6.2  joerg 			break;
    201  1.24.6.2  joerg 		case O_WRONLY:
    202  1.24.6.2  joerg 		default:
    203  1.24.6.2  joerg 			goto einval;
    204  1.24.6.2  joerg 		}
    205  1.24.6.2  joerg 
    206  1.24.6.2  joerg 		if ((t->bt_fd = open(fname, flags, mode)) == -1)
    207  1.24.6.2  joerg 			goto err;
    208  1.24.6.2  joerg 		if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
    209  1.24.6.2  joerg 			goto err;
    210  1.24.6.2  joerg 	} else {
    211  1.24.6.2  joerg 		if ((flags & O_ACCMODE) != O_RDWR)
    212  1.24.6.2  joerg 			goto einval;
    213  1.24.6.2  joerg 		if ((t->bt_fd = tmp()) == -1)
    214  1.24.6.2  joerg 			goto err;
    215  1.24.6.2  joerg 		F_SET(t, B_INMEM);
    216  1.24.6.2  joerg 	}
    217  1.24.6.2  joerg 
    218  1.24.6.2  joerg 	if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
    219  1.24.6.2  joerg 		goto err;
    220  1.24.6.2  joerg 
    221  1.24.6.2  joerg 	if (fstat(t->bt_fd, &sb))
    222  1.24.6.2  joerg 		goto err;
    223  1.24.6.2  joerg 	if (sb.st_size) {
    224  1.24.6.2  joerg 		if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
    225  1.24.6.2  joerg 			goto err;
    226  1.24.6.2  joerg 		if (nr != sizeof(BTMETA))
    227  1.24.6.2  joerg 			goto eftype;
    228  1.24.6.2  joerg 
    229  1.24.6.2  joerg 		/*
    230  1.24.6.2  joerg 		 * Read in the meta-data.  This can change the notion of what
    231  1.24.6.2  joerg 		 * the lorder, page size and flags are, and, when the page size
    232  1.24.6.2  joerg 		 * changes, the cachesize value can change too.  If the user
    233  1.24.6.2  joerg 		 * specified the wrong byte order for an existing database, we
    234  1.24.6.2  joerg 		 * don't bother to return an error, we just clear the NEEDSWAP
    235  1.24.6.2  joerg 		 * bit.
    236  1.24.6.2  joerg 		 */
    237  1.24.6.2  joerg 		if (m.magic == BTREEMAGIC)
    238  1.24.6.2  joerg 			F_CLR(t, B_NEEDSWAP);
    239  1.24.6.2  joerg 		else {
    240  1.24.6.2  joerg 			F_SET(t, B_NEEDSWAP);
    241  1.24.6.2  joerg 			M_32_SWAP(m.magic);
    242  1.24.6.2  joerg 			M_32_SWAP(m.version);
    243  1.24.6.2  joerg 			M_32_SWAP(m.psize);
    244  1.24.6.2  joerg 			M_32_SWAP(m.free);
    245  1.24.6.2  joerg 			M_32_SWAP(m.nrecs);
    246  1.24.6.2  joerg 			M_32_SWAP(m.flags);
    247  1.24.6.2  joerg 		}
    248  1.24.6.2  joerg 		if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
    249  1.24.6.2  joerg 			goto eftype;
    250  1.24.6.2  joerg 		if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
    251  1.24.6.2  joerg 		    m.psize & (sizeof(indx_t) - 1))
    252  1.24.6.2  joerg 			goto eftype;
    253  1.24.6.2  joerg 		if (m.flags & ~SAVEMETA)
    254  1.24.6.2  joerg 			goto eftype;
    255  1.24.6.2  joerg 		b.psize = m.psize;
    256  1.24.6.2  joerg 		F_SET(t, m.flags);
    257  1.24.6.2  joerg 		t->bt_free = m.free;
    258  1.24.6.2  joerg 		t->bt_nrecs = m.nrecs;
    259  1.24.6.2  joerg 	} else {
    260  1.24.6.2  joerg 		/*
    261  1.24.6.2  joerg 		 * Set the page size to the best value for I/O to this file.
    262  1.24.6.2  joerg 		 * Don't overflow the page offset type.
    263  1.24.6.2  joerg 		 */
    264  1.24.6.2  joerg 		if (b.psize == 0) {
    265  1.24.6.2  joerg 			b.psize = sb.st_blksize;
    266  1.24.6.2  joerg 			if (b.psize < MINPSIZE)
    267  1.24.6.2  joerg 				b.psize = MINPSIZE;
    268  1.24.6.2  joerg 			if (b.psize > MAX_PAGE_OFFSET + 1)
    269  1.24.6.2  joerg 				b.psize = MAX_PAGE_OFFSET + 1;
    270  1.24.6.2  joerg 		}
    271  1.24.6.2  joerg 
    272  1.24.6.2  joerg 		/* Set flag if duplicates permitted. */
    273  1.24.6.2  joerg 		if (!(b.flags & R_DUP))
    274  1.24.6.2  joerg 			F_SET(t, B_NODUPS);
    275  1.24.6.2  joerg 
    276  1.24.6.2  joerg 		t->bt_free = P_INVALID;
    277  1.24.6.2  joerg 		t->bt_nrecs = 0;
    278  1.24.6.2  joerg 		F_SET(t, B_METADIRTY);
    279  1.24.6.2  joerg 	}
    280  1.24.6.2  joerg 
    281  1.24.6.2  joerg 	t->bt_psize = b.psize;
    282  1.24.6.2  joerg 
    283  1.24.6.2  joerg 	/* Set the cache size; must be a multiple of the page size. */
    284  1.24.6.2  joerg 	if (b.cachesize && b.cachesize & (b.psize - 1))
    285  1.24.6.2  joerg 		b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
    286  1.24.6.2  joerg 	if (b.cachesize < b.psize * MINCACHE)
    287  1.24.6.2  joerg 		b.cachesize = b.psize * MINCACHE;
    288  1.24.6.2  joerg 
    289  1.24.6.2  joerg 	/* Calculate number of pages to cache. */
    290  1.24.6.2  joerg 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
    291  1.24.6.2  joerg 
    292  1.24.6.2  joerg 	/*
    293  1.24.6.2  joerg 	 * The btree data structure requires that at least two keys can fit on
    294  1.24.6.2  joerg 	 * a page, but other than that there's no fixed requirement.  The user
    295  1.24.6.2  joerg 	 * specified a minimum number per page, and we translated that into the
    296  1.24.6.2  joerg 	 * number of bytes a key/data pair can use before being placed on an
    297  1.24.6.2  joerg 	 * overflow page.  This calculation includes the page header, the size
    298  1.24.6.2  joerg 	 * of the index referencing the leaf item and the size of the leaf item
    299  1.24.6.2  joerg 	 * structure.  Also, don't let the user specify a minkeypage such that
    300  1.24.6.2  joerg 	 * a key/data pair won't fit even if both key and data are on overflow
    301  1.24.6.2  joerg 	 * pages.
    302  1.24.6.2  joerg 	 */
    303  1.24.6.2  joerg 	temp = (t->bt_psize - BTDATAOFF) / b.minkeypage -
    304  1.24.6.2  joerg 	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
    305  1.24.6.2  joerg 	_DBFIT(temp, indx_t);
    306  1.24.6.2  joerg 	t->bt_ovflsize = (indx_t)temp;
    307  1.24.6.2  joerg 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
    308  1.24.6.2  joerg 		t->bt_ovflsize =
    309  1.24.6.2  joerg 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
    310  1.24.6.2  joerg 
    311  1.24.6.2  joerg 	/* Initialize the buffer pool. */
    312  1.24.6.2  joerg 	if ((t->bt_mp =
    313  1.24.6.2  joerg 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
    314  1.24.6.2  joerg 		goto err;
    315  1.24.6.2  joerg 	if (!F_ISSET(t, B_INMEM))
    316  1.24.6.2  joerg 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
    317  1.24.6.2  joerg 
    318  1.24.6.2  joerg 	/* Create a root page if new tree. */
    319  1.24.6.2  joerg 	if (nroot(t) == RET_ERROR)
    320  1.24.6.2  joerg 		goto err;
    321  1.24.6.2  joerg 
    322  1.24.6.2  joerg 	/* Global flags. */
    323  1.24.6.2  joerg 	if (dflags & DB_LOCK)
    324  1.24.6.2  joerg 		F_SET(t, B_DB_LOCK);
    325  1.24.6.2  joerg 	if (dflags & DB_SHMEM)
    326  1.24.6.2  joerg 		F_SET(t, B_DB_SHMEM);
    327  1.24.6.2  joerg 	if (dflags & DB_TXN)
    328  1.24.6.2  joerg 		F_SET(t, B_DB_TXN);
    329  1.24.6.2  joerg 
    330  1.24.6.2  joerg 	return (dbp);
    331  1.24.6.2  joerg 
    332  1.24.6.2  joerg einval:	errno = EINVAL;
    333  1.24.6.2  joerg 	goto err;
    334  1.24.6.2  joerg 
    335  1.24.6.2  joerg eftype:	errno = EFTYPE;
    336  1.24.6.2  joerg 	goto err;
    337  1.24.6.2  joerg 
    338  1.24.6.2  joerg err:	if (t) {
    339  1.24.6.2  joerg 		if (t->bt_dbp)
    340  1.24.6.2  joerg 			free(t->bt_dbp);
    341  1.24.6.2  joerg 		if (t->bt_fd != -1)
    342  1.24.6.2  joerg 			(void)close(t->bt_fd);
    343  1.24.6.2  joerg 		free(t);
    344  1.24.6.2  joerg 	}
    345  1.24.6.2  joerg 	return (NULL);
    346  1.24.6.2  joerg }
    347  1.24.6.2  joerg 
    348  1.24.6.2  joerg /*
    349  1.24.6.2  joerg  * NROOT -- Create the root of a new tree.
    350  1.24.6.2  joerg  *
    351  1.24.6.2  joerg  * Parameters:
    352  1.24.6.2  joerg  *	t:	tree
    353  1.24.6.2  joerg  *
    354  1.24.6.2  joerg  * Returns:
    355  1.24.6.2  joerg  *	RET_ERROR, RET_SUCCESS
    356  1.24.6.2  joerg  */
    357  1.24.6.2  joerg static int
    358  1.24.6.2  joerg nroot(BTREE *t)
    359  1.24.6.2  joerg {
    360  1.24.6.2  joerg 	PAGE *meta, *root;
    361  1.24.6.2  joerg 	pgno_t npg;
    362  1.24.6.2  joerg 
    363  1.24.6.2  joerg 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
    364  1.24.6.2  joerg 		mpool_put(t->bt_mp, meta, 0);
    365  1.24.6.2  joerg 		return (RET_SUCCESS);
    366  1.24.6.2  joerg 	}
    367  1.24.6.2  joerg 	if (errno != EINVAL)		/* It's OK to not exist. */
    368  1.24.6.2  joerg 		return (RET_ERROR);
    369  1.24.6.2  joerg 	errno = 0;
    370  1.24.6.2  joerg 
    371  1.24.6.2  joerg 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
    372  1.24.6.2  joerg 		return (RET_ERROR);
    373  1.24.6.2  joerg 
    374  1.24.6.2  joerg 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
    375  1.24.6.2  joerg 		return (RET_ERROR);
    376  1.24.6.2  joerg 
    377  1.24.6.2  joerg 	if (npg != P_ROOT)
    378  1.24.6.2  joerg 		return (RET_ERROR);
    379  1.24.6.2  joerg 	root->pgno = npg;
    380  1.24.6.2  joerg 	root->prevpg = root->nextpg = P_INVALID;
    381  1.24.6.2  joerg 	root->lower = BTDATAOFF;
    382  1.24.6.2  joerg 	root->upper = t->bt_psize;
    383  1.24.6.2  joerg 	root->flags = P_BLEAF;
    384  1.24.6.2  joerg 	memset(meta, 0, t->bt_psize);
    385  1.24.6.2  joerg 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
    386  1.24.6.2  joerg 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
    387  1.24.6.2  joerg 	return (RET_SUCCESS);
    388  1.24.6.2  joerg }
    389  1.24.6.2  joerg 
    390  1.24.6.2  joerg static int
    391  1.24.6.2  joerg tmp(void)
    392  1.24.6.2  joerg {
    393  1.24.6.2  joerg 	sigset_t set, oset;
    394  1.24.6.2  joerg 	size_t len;
    395  1.24.6.2  joerg 	int fd;
    396  1.24.6.2  joerg 	char *envtmp;
    397  1.24.6.2  joerg 	char path[PATH_MAX];
    398  1.24.6.2  joerg 
    399  1.24.6.2  joerg 	if (issetugid())
    400  1.24.6.2  joerg 		envtmp = NULL;
    401  1.24.6.2  joerg 	else
    402  1.24.6.2  joerg 		envtmp = getenv("TMPDIR");
    403  1.24.6.2  joerg 
    404  1.24.6.2  joerg 	len = snprintf(path,
    405  1.24.6.2  joerg 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : _PATH_TMP);
    406  1.24.6.2  joerg 	if (len >= sizeof(path))
    407  1.24.6.2  joerg 		return -1;
    408  1.24.6.2  joerg 
    409  1.24.6.2  joerg 	(void)sigfillset(&set);
    410  1.24.6.2  joerg 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
    411  1.24.6.2  joerg 	if ((fd = mkstemp(path)) != -1) {
    412  1.24.6.2  joerg 		(void)unlink(path);
    413  1.24.6.2  joerg 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    414  1.24.6.2  joerg 	}
    415  1.24.6.2  joerg 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    416  1.24.6.2  joerg 	return(fd);
    417  1.24.6.2  joerg }
    418  1.24.6.2  joerg 
    419  1.24.6.2  joerg static int
    420  1.24.6.2  joerg byteorder(void)
    421  1.24.6.2  joerg {
    422  1.24.6.2  joerg 	uint32_t x;
    423  1.24.6.2  joerg 	uint8_t *p;
    424  1.24.6.2  joerg 
    425  1.24.6.2  joerg 	x = 0x01020304;
    426  1.24.6.2  joerg 	p = (uint8_t *)(void *)&x;
    427  1.24.6.2  joerg 	switch (*p) {
    428  1.24.6.2  joerg 	case 1:
    429  1.24.6.2  joerg 		return (BIG_ENDIAN);
    430  1.24.6.2  joerg 	case 4:
    431  1.24.6.2  joerg 		return (LITTLE_ENDIAN);
    432  1.24.6.2  joerg 	default:
    433  1.24.6.2  joerg 		return (0);
    434  1.24.6.2  joerg 	}
    435  1.24.6.2  joerg }
    436  1.24.6.2  joerg 
    437  1.24.6.2  joerg int
    438  1.24.6.2  joerg __bt_fd(const DB *dbp)
    439  1.24.6.2  joerg {
    440  1.24.6.2  joerg 	BTREE *t;
    441  1.24.6.2  joerg 
    442  1.24.6.2  joerg 	t = dbp->internal;
    443  1.24.6.2  joerg 
    444  1.24.6.2  joerg 	/* Toss any page pinned across calls. */
    445  1.24.6.2  joerg 	if (t->bt_pinned != NULL) {
    446  1.24.6.2  joerg 		mpool_put(t->bt_mp, t->bt_pinned, 0);
    447  1.24.6.2  joerg 		t->bt_pinned = NULL;
    448  1.24.6.2  joerg 	}
    449  1.24.6.2  joerg 
    450  1.24.6.2  joerg 	/* In-memory database can't have a file descriptor. */
    451  1.24.6.2  joerg 	if (F_ISSET(t, B_INMEM)) {
    452  1.24.6.2  joerg 		errno = ENOENT;
    453  1.24.6.2  joerg 		return (-1);
    454  1.24.6.2  joerg 	}
    455  1.24.6.2  joerg 	return (t->bt_fd);
    456  1.24.6.2  joerg }
    457