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