xref: /csrg-svn/lib/libc/db/btree/bt_open.c (revision 56894)
145876Sbostic /*-
245876Sbostic  * Copyright (c) 1990 The Regents of the University of California.
345876Sbostic  * All rights reserved.
445876Sbostic  *
545876Sbostic  * This code is derived from software contributed to Berkeley by
645876Sbostic  * Mike Olson.
745876Sbostic  *
845876Sbostic  * %sccs.include.redist.c%
945876Sbostic  */
1045876Sbostic 
1145876Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*56894Sbostic static char sccsid[] = "@(#)bt_open.c	5.23 (Berkeley) 11/18/92";
1345876Sbostic #endif /* LIBC_SCCS and not lint */
1445876Sbostic 
1545876Sbostic /*
1650989Sbostic  * Implementation of btree access method for 4.4BSD.
1745876Sbostic  *
1850989Sbostic  * The design here was originally based on that of the btree access method
1950989Sbostic  * used in the Postgres database system at UC Berkeley.  This implementation
2050989Sbostic  * is wholly independent of the Postgres code.
2145876Sbostic  */
2245876Sbostic 
2345876Sbostic #include <sys/param.h>
2445876Sbostic #include <sys/stat.h>
2555316Sbostic 
2656732Sbostic #define	__DBINTERFACE_PRIVATE
2756732Sbostic #include <db.h>
2856732Sbostic #include <errno.h>
2950989Sbostic #include <fcntl.h>
3050989Sbostic #include <limits.h>
3156732Sbostic #include <signal.h>
3250989Sbostic #include <stdio.h>
3346561Sbostic #include <stdlib.h>
3451098Sbostic #include <string.h>
3556732Sbostic #include <unistd.h>
3656732Sbostic 
3746127Smao #include "btree.h"
3845876Sbostic 
3950989Sbostic static int nroot __P((BTREE *));
4050989Sbostic static int tmp __P((void));
4145876Sbostic 
4245876Sbostic /*
4350989Sbostic  * __BT_OPEN -- Open a btree.
4445876Sbostic  *
4550989Sbostic  * Creates and fills a DB struct, and calls the routine that actually
4650989Sbostic  * opens the btree.
4745876Sbostic  *
4850989Sbostic  * Parameters:
4950989Sbostic  *	fname:	filename (NULL for in-memory trees)
5050989Sbostic  *	flags:	open flag bits
5150989Sbostic  *	mode:	open permission bits
5250989Sbostic  *	b:	BTREEINFO pointer
5345876Sbostic  *
5450989Sbostic  * Returns:
5550989Sbostic  *	NULL on failure, pointer to DB on success.
5645876Sbostic  *
5745876Sbostic  */
5845876Sbostic DB *
5950989Sbostic __bt_open(fname, flags, mode, openinfo)
6050989Sbostic 	const char *fname;
6150989Sbostic 	int flags, mode;
6250989Sbostic 	const BTREEINFO *openinfo;
6345876Sbostic {
6451098Sbostic 	BTMETA m;
6550989Sbostic 	BTREE *t;
6651098Sbostic 	BTREEINFO b;
6750989Sbostic 	DB *dbp;
6850989Sbostic 	pgno_t ncache;
6950989Sbostic 	struct stat sb;
7050989Sbostic 	int nr;
7145876Sbostic 
7256732Sbostic 	t = NULL;
7356732Sbostic 
7445876Sbostic 	/*
7550989Sbostic 	 * Intention is to make sure all of the user's selections are okay
7650989Sbostic 	 * here and then use them without checking.  Can't be complete, since
7750989Sbostic 	 * we don't know the right page size, lorder or flags until the backing
7850989Sbostic 	 * file is opened.  Also, the file's page size can cause the cachesize
7950989Sbostic 	 * to change.
8045876Sbostic 	 */
8150989Sbostic 	if (openinfo) {
8250989Sbostic 		b = *openinfo;
8345876Sbostic 
8450989Sbostic 		/* Flags: R_DUP. */
8551098Sbostic 		if (b.flags & ~(R_DUP))
8650989Sbostic 			goto einval;
8745876Sbostic 
8850989Sbostic 		/*
8950989Sbostic 		 * Page size must be index_t aligned and >= MINPSIZE.  Default
9050989Sbostic 		 * page size is set farther on, based on the underlying file
9150989Sbostic 		 * transfer size.
9250989Sbostic 		 */
9350989Sbostic 		if (b.psize &&
9450989Sbostic 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET ||
9550989Sbostic 		    b.psize & sizeof(index_t) - 1))
9651421Sbostic 			goto einval;
9745876Sbostic 
9851098Sbostic 		/* Minimum number of keys per page; absolute minimum is 2. */
9950989Sbostic 		if (b.minkeypage) {
10050989Sbostic 			if (b.minkeypage < 2)
10150989Sbostic 				goto einval;
10250989Sbostic 		} else
10351098Sbostic 			b.minkeypage = DEFMINKEYPAGE;
10451098Sbostic 
10550989Sbostic 		/* If no comparison, use default comparison and prefix. */
10650989Sbostic 		if (b.compare == NULL) {
10750989Sbostic 			b.compare = __bt_defcmp;
10850989Sbostic 			if (b.prefix == NULL)
10950989Sbostic 				b.prefix = __bt_defpfx;
11045876Sbostic 		}
11145876Sbostic 
11250989Sbostic 		if (b.lorder == 0)
11350989Sbostic 			b.lorder = BYTE_ORDER;
11450989Sbostic 		else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
11550989Sbostic 			goto einval;
11650989Sbostic 	} else {
11751421Sbostic 		b.compare = __bt_defcmp;
11856163Sbostic 		b.cachesize = 0;
11950989Sbostic 		b.flags = 0;
12051421Sbostic 		b.lorder = BYTE_ORDER;
12150989Sbostic 		b.minkeypage = DEFMINKEYPAGE;
12250989Sbostic 		b.prefix = __bt_defpfx;
12351421Sbostic 		b.psize = 0;
12450989Sbostic 	}
12545876Sbostic 
12650989Sbostic 	/* Allocate and initialize DB and BTREE structures. */
12750989Sbostic 	if ((t = malloc(sizeof(BTREE))) == NULL)
12850989Sbostic 		goto err;
12950989Sbostic 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
13050989Sbostic 	if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL)
13150989Sbostic 		goto err;
13250989Sbostic 	t->bt_bcursor.pgno = P_INVALID;
13350989Sbostic 	t->bt_bcursor.index = 0;
13450989Sbostic 	t->bt_stack = NULL;
13550989Sbostic 	t->bt_sp = t->bt_maxstack = 0;
13650989Sbostic 	t->bt_kbuf = t->bt_dbuf = NULL;
13750989Sbostic 	t->bt_kbufsz = t->bt_dbufsz = 0;
13850989Sbostic 	t->bt_order = NOT;
13950989Sbostic 	t->bt_cmp = b.compare;
14050989Sbostic 	t->bt_pfx = b.prefix;
14151800Sbostic 	t->bt_flags = 0;
14246453Smao 
14350989Sbostic 	dbp->type = DB_BTREE;
14450989Sbostic 	dbp->internal = t;
14550989Sbostic 	dbp->close = __bt_close;
14650989Sbostic 	dbp->del = __bt_delete;
14750989Sbostic 	dbp->get = __bt_get;
14850989Sbostic 	dbp->put = __bt_put;
14950989Sbostic 	dbp->seq = __bt_seq;
15050989Sbostic 	dbp->sync = __bt_sync;
15145876Sbostic 
15250989Sbostic 	/*
15350989Sbostic 	 * If no file name was supplied, this is an in-memory btree and we
15450989Sbostic 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
15550989Sbostic 	 */
15650989Sbostic 	if (fname) {
15756421Sbostic 		switch(flags & O_ACCMODE) {
15856421Sbostic 		case O_RDONLY:
15956421Sbostic 			SET(t, BTF_RDONLY);
16056421Sbostic 			break;
16156421Sbostic 		case O_RDWR:
16256421Sbostic 			break;
16356421Sbostic 		case O_WRONLY:
16456421Sbostic 		default:
16556421Sbostic 			goto einval;
16656421Sbostic 		}
16756421Sbostic 
168*56894Sbostic 		if ((t->bt_fd =
169*56894Sbostic 		    open(fname, flags & __USE_OPEN_FLAGS, mode)) < 0)
17050989Sbostic 			goto err;
17145876Sbostic 
17250989Sbostic 	} else {
17356421Sbostic 		if ((flags & O_ACCMODE) != O_RDWR)
17456421Sbostic 			goto einval;
17550989Sbostic 		if ((t->bt_fd = tmp()) == -1)
17650989Sbostic 			goto err;
17750989Sbostic 		SET(t, BTF_INMEM);
17850989Sbostic 	}
17945876Sbostic 
18056094Sbostic 	if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
18150989Sbostic 		goto err;
18245876Sbostic 
18350989Sbostic 	if (fstat(t->bt_fd, &sb))
18450989Sbostic 		goto err;
18550989Sbostic 	if (sb.st_size) {
18650989Sbostic 		nr = read(t->bt_fd, &m, sizeof(BTMETA));
18750989Sbostic 		if (nr < 0)
18850989Sbostic 			goto err;
18950989Sbostic 		if (nr != sizeof(BTMETA))
19050989Sbostic 			goto eftype;
19145876Sbostic 
19250989Sbostic 		/*
19350989Sbostic 		 * Read in the meta-data.  This can change the notion of what
19450989Sbostic 		 * the lorder, page size and flags are, and, when the page size
19550989Sbostic 		 * changes the cachesize value can change as well.
19650989Sbostic 		 *
19750989Sbostic 		 * Lorder is always stored in host-independent format.
19850989Sbostic 		 */
19951967Sbostic 		m.m_lorder = ntohl(m.m_lorder);
20050989Sbostic 		if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN)
20150989Sbostic 			goto eftype;
20250989Sbostic 		if (m.m_lorder != BYTE_ORDER) {
20350989Sbostic 			BLSWAP(m.m_magic);
20450989Sbostic 			BLSWAP(m.m_version);
20550989Sbostic 			BLSWAP(m.m_psize);
20650989Sbostic 			BLSWAP(m.m_free);
20750989Sbostic 			BLSWAP(m.m_nrecs);
20850989Sbostic 			BLSWAP(m.m_flags);
20945876Sbostic 		}
21050989Sbostic 		if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
21150989Sbostic 			goto eftype;
21250989Sbostic 		if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET ||
21350989Sbostic 		    m.m_psize & sizeof(index_t) - 1)
21450989Sbostic 			goto eftype;
21551358Sbostic 		if (m.m_flags & ~SAVEMETA)
21650989Sbostic 			goto eftype;
21750989Sbostic 		b.psize = m.m_psize;
21851098Sbostic 		t->bt_flags |= m.m_flags;
21950989Sbostic 		t->bt_free = m.m_free;
22050989Sbostic 		t->bt_lorder = m.m_lorder;
22150989Sbostic 		t->bt_nrecs = m.m_nrecs;
22250989Sbostic 	} else {
22350989Sbostic 		/*
22450989Sbostic 		 * Set the page size to the best value for I/O to this file.
22550989Sbostic 		 * Don't overflow the page offset type.
22650989Sbostic 		 */
22750989Sbostic 		if (b.psize == 0) {
22850989Sbostic 			b.psize = sb.st_blksize;
22950989Sbostic 			if (b.psize < MINPSIZE)
23050989Sbostic 				b.psize = MINPSIZE;
23150989Sbostic 			if (b.psize > MAX_PAGE_OFFSET)
23250989Sbostic 				b.psize = MAX_PAGE_OFFSET;
23350989Sbostic 		}
23456732Sbostic 		if (!(b.flags & R_DUP))
23556732Sbostic 			SET(t, BTF_NODUPS);
23650989Sbostic 		t->bt_free = P_INVALID;
23750989Sbostic 		t->bt_lorder = b.lorder;
23850989Sbostic 		t->bt_nrecs = 0;
23950989Sbostic 		SET(t, BTF_METADIRTY);
24045876Sbostic 	}
24145876Sbostic 
24250989Sbostic 	t->bt_psize = b.psize;
24345876Sbostic 
24450989Sbostic 	/* Set the cache size; must be a multiple of the page size. */
24550989Sbostic 	if (b.cachesize && b.cachesize & b.psize - 1)
24650989Sbostic 		b.cachesize += (~b.cachesize & b.psize - 1) + 1;
24750989Sbostic 	if (b.cachesize < b.psize * MINCACHE)
24850989Sbostic 		b.cachesize = b.psize * MINCACHE;
24945876Sbostic 
25050989Sbostic 	/* Calculate number of pages to cache. */
25150989Sbostic 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
25245876Sbostic 
25345876Sbostic 	/*
25451098Sbostic 	 * The btree data structure requires that at least two keys can fit on
25551098Sbostic 	 * a page, but other than that there's no fixed requirement.  The user
25651098Sbostic 	 * specified a minimum number per page, and we translated that into the
25751098Sbostic 	 * number of bytes a key/data pair can use before being placed on an
25851098Sbostic 	 * overflow page.  This calculation includes the page header, the size
25951098Sbostic 	 * of the index referencing the leaf item and the size of the leaf item
26051098Sbostic 	 * structure.  Also, don't let the user specify a minkeypage such that
26151098Sbostic 	 * a key/data pair won't fit even if both key and data are on overflow
26251098Sbostic 	 * pages.
26345876Sbostic 	 */
26451098Sbostic 	t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
26551098Sbostic 	    (sizeof(index_t) + NBLEAFDBT(0, 0));
26651098Sbostic 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t))
26751098Sbostic 		t->bt_ovflsize =
26851098Sbostic 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t);
26945876Sbostic 
27050989Sbostic 	/* Initialize the buffer pool. */
27150989Sbostic 	if ((t->bt_mp =
27250989Sbostic 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
27350989Sbostic 		goto err;
27456732Sbostic 	if (!ISSET(t, BTF_INMEM))
27551098Sbostic 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
27645876Sbostic 
27750989Sbostic 	/* Create a root page if new tree. */
27850989Sbostic 	if (nroot(t) == RET_ERROR)
27950989Sbostic 		goto err;
28045876Sbostic 
28150989Sbostic 	return (dbp);
28245876Sbostic 
28350989Sbostic einval:	errno = EINVAL;
28450989Sbostic 	goto err;
28545876Sbostic 
28650989Sbostic eftype:	errno = EFTYPE;
28750989Sbostic 	goto err;
28849322Sbostic 
28950989Sbostic err:	if (t) {
29050989Sbostic 		if (t->bt_dbp)
29150989Sbostic 			free(t->bt_dbp);
29250989Sbostic 		if (t->bt_fd != -1)
29350989Sbostic 			(void)close(t->bt_fd);
29450989Sbostic 		free(t);
29545876Sbostic 	}
29650989Sbostic 	return (NULL);
29745876Sbostic }
29845876Sbostic 
29945876Sbostic /*
30050989Sbostic  * NROOT -- Create the root of a new tree.
30145876Sbostic  *
30250989Sbostic  * Parameters:
30350989Sbostic  *	t:	tree
30445876Sbostic  *
30550989Sbostic  * Returns:
30650989Sbostic  *	RET_ERROR, RET_SUCCESS
30745876Sbostic  */
30850989Sbostic static int
30950989Sbostic nroot(t)
31050989Sbostic 	BTREE *t;
31145876Sbostic {
31250989Sbostic 	PAGE *meta, *root;
31350989Sbostic 	pgno_t npg;
31445876Sbostic 
31550989Sbostic 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
31650989Sbostic 		mpool_put(t->bt_mp, meta, 0);
31745876Sbostic 		return (RET_SUCCESS);
31845876Sbostic 	}
31950989Sbostic 	if (errno != EINVAL)
32050989Sbostic 		return (RET_ERROR);
32145876Sbostic 
32250989Sbostic 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
32350989Sbostic 		return (RET_ERROR);
32445876Sbostic 
32550989Sbostic 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
32650989Sbostic 		return (RET_ERROR);
32745876Sbostic 
32850989Sbostic 	if (npg != P_ROOT)
32945876Sbostic 		return (RET_ERROR);
33050989Sbostic 	root->pgno = npg;
33150989Sbostic 	root->prevpg = root->nextpg = P_INVALID;
33250989Sbostic 	root->lower = BTDATAOFF;
33350989Sbostic 	root->upper = t->bt_psize;
33450989Sbostic 	root->flags = P_BLEAF;
33551098Sbostic 	bzero(meta, t->bt_psize);
33650989Sbostic 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
33750989Sbostic 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
33850989Sbostic 	return (RET_SUCCESS);
33945876Sbostic }
34045876Sbostic 
34150989Sbostic static int
34250989Sbostic tmp()
34345876Sbostic {
34450989Sbostic 	sigset_t set, oset;
34550989Sbostic 	int fd;
34650989Sbostic 	char *envtmp;
34750989Sbostic 	char path[MAXPATHLEN];
34845876Sbostic 
34950989Sbostic 	envtmp = getenv("TMPDIR");
35050989Sbostic 	(void)snprintf(path,
35150989Sbostic 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
35249322Sbostic 
35355316Sbostic 	(void)sigfillset(&set);
35450989Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
35550989Sbostic 	if ((fd = mkstemp(path)) != -1)
35650989Sbostic 		(void)unlink(path);
35750989Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
35850989Sbostic 	return(fd);
35945876Sbostic }
360