xref: /csrg-svn/lib/libc/db/btree/bt_open.c (revision 53605)
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*53605Sbostic static char sccsid[] = "@(#)bt_open.c	5.17 (Berkeley) 05/15/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>
2550989Sbostic #include <fcntl.h>
2646561Sbostic #include <errno.h>
2750989Sbostic #include <limits.h>
2850989Sbostic #define	__DBINTERFACE_PRIVATE
2945876Sbostic #include <db.h>
3050989Sbostic #include <stdio.h>
3150989Sbostic #include <unistd.h>
3246561Sbostic #include <stdlib.h>
3351098Sbostic #include <string.h>
3446127Smao #include "btree.h"
3545876Sbostic 
3650989Sbostic static int nroot __P((BTREE *));
3750989Sbostic static int tmp __P((void));
3845876Sbostic 
3945876Sbostic /*
4050989Sbostic  * __BT_OPEN -- Open a btree.
4145876Sbostic  *
4250989Sbostic  * Creates and fills a DB struct, and calls the routine that actually
4350989Sbostic  * opens the btree.
4445876Sbostic  *
4550989Sbostic  * Parameters:
4650989Sbostic  *	fname:	filename (NULL for in-memory trees)
4750989Sbostic  *	flags:	open flag bits
4850989Sbostic  *	mode:	open permission bits
4950989Sbostic  *	b:	BTREEINFO pointer
5045876Sbostic  *
5150989Sbostic  * Returns:
5250989Sbostic  *	NULL on failure, pointer to DB on success.
5345876Sbostic  *
5445876Sbostic  */
5545876Sbostic DB *
5650989Sbostic __bt_open(fname, flags, mode, openinfo)
5750989Sbostic 	const char *fname;
5850989Sbostic 	int flags, mode;
5950989Sbostic 	const BTREEINFO *openinfo;
6045876Sbostic {
6151098Sbostic 	BTMETA m;
6250989Sbostic 	BTREE *t;
6351098Sbostic 	BTREEINFO b;
6450989Sbostic 	DB *dbp;
6550989Sbostic 	pgno_t ncache;
6650989Sbostic 	struct stat sb;
6750989Sbostic 	int nr;
6845876Sbostic 
6945876Sbostic 	/*
7050989Sbostic 	 * Intention is to make sure all of the user's selections are okay
7150989Sbostic 	 * here and then use them without checking.  Can't be complete, since
7250989Sbostic 	 * we don't know the right page size, lorder or flags until the backing
7350989Sbostic 	 * file is opened.  Also, the file's page size can cause the cachesize
7450989Sbostic 	 * to change.
7545876Sbostic 	 */
7650989Sbostic 	if (openinfo) {
7750989Sbostic 		b = *openinfo;
7845876Sbostic 
7950989Sbostic 		/* Flags: R_DUP. */
8051098Sbostic 		if (b.flags & ~(R_DUP))
8150989Sbostic 			goto einval;
8245876Sbostic 
8350989Sbostic 		/*
8450989Sbostic 		 * Page size must be index_t aligned and >= MINPSIZE.  Default
8550989Sbostic 		 * page size is set farther on, based on the underlying file
8650989Sbostic 		 * transfer size.
8750989Sbostic 		 */
8850989Sbostic 		if (b.psize &&
8950989Sbostic 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET ||
9050989Sbostic 		    b.psize & sizeof(index_t) - 1))
9151421Sbostic 			goto einval;
9245876Sbostic 
9351098Sbostic 		/* Minimum number of keys per page; absolute minimum is 2. */
9450989Sbostic 		if (b.minkeypage) {
9550989Sbostic 			if (b.minkeypage < 2)
9650989Sbostic 				goto einval;
9750989Sbostic 		} else
9851098Sbostic 			b.minkeypage = DEFMINKEYPAGE;
9951098Sbostic 
10050989Sbostic 		/* If no comparison, use default comparison and prefix. */
10150989Sbostic 		if (b.compare == NULL) {
10250989Sbostic 			b.compare = __bt_defcmp;
10350989Sbostic 			if (b.prefix == NULL)
10450989Sbostic 				b.prefix = __bt_defpfx;
10545876Sbostic 		}
10645876Sbostic 
10750989Sbostic 		if (b.lorder == 0)
10850989Sbostic 			b.lorder = BYTE_ORDER;
10950989Sbostic 		else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
11050989Sbostic 			goto einval;
11150989Sbostic 	} else {
11251421Sbostic 		b.compare = __bt_defcmp;
11350989Sbostic 		b.flags = 0;
11451421Sbostic 		b.lorder = BYTE_ORDER;
11550989Sbostic 		b.minkeypage = DEFMINKEYPAGE;
11650989Sbostic 		b.prefix = __bt_defpfx;
11751421Sbostic 		b.psize = 0;
11850989Sbostic 	}
11945876Sbostic 
12050989Sbostic 	/* Allocate and initialize DB and BTREE structures. */
12150989Sbostic 	if ((t = malloc(sizeof(BTREE))) == NULL)
12250989Sbostic 		goto err;
12350989Sbostic 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
12450989Sbostic 	if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL)
12550989Sbostic 		goto err;
12650989Sbostic 	t->bt_bcursor.pgno = P_INVALID;
12750989Sbostic 	t->bt_bcursor.index = 0;
12850989Sbostic 	t->bt_stack = NULL;
12950989Sbostic 	t->bt_sp = t->bt_maxstack = 0;
13050989Sbostic 	t->bt_kbuf = t->bt_dbuf = NULL;
13150989Sbostic 	t->bt_kbufsz = t->bt_dbufsz = 0;
13250989Sbostic 	t->bt_order = NOT;
13350989Sbostic 	t->bt_cmp = b.compare;
13450989Sbostic 	t->bt_pfx = b.prefix;
13551800Sbostic 	t->bt_flags = 0;
13646453Smao 
13750989Sbostic 	dbp->type = DB_BTREE;
13850989Sbostic 	dbp->internal = t;
13950989Sbostic 	dbp->close = __bt_close;
14050989Sbostic 	dbp->del = __bt_delete;
14150989Sbostic 	dbp->get = __bt_get;
14250989Sbostic 	dbp->put = __bt_put;
14350989Sbostic 	dbp->seq = __bt_seq;
14450989Sbostic 	dbp->sync = __bt_sync;
14545876Sbostic 
14650989Sbostic 	/*
14750989Sbostic 	 * If no file name was supplied, this is an in-memory btree and we
14850989Sbostic 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
14950989Sbostic 	 */
15050989Sbostic 	if (fname) {
151*53605Sbostic #define	USEFLAGS \
152*53605Sbostic 	(O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC|O_WRONLY)
15350989Sbostic 		if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0)
15450989Sbostic 			goto err;
15550989Sbostic 		if ((flags & O_ACCMODE) == O_RDONLY)
15650989Sbostic 			SET(t, BTF_RDONLY);
15745876Sbostic 
15850989Sbostic 	} else {
15950989Sbostic 		if ((t->bt_fd = tmp()) == -1)
16050989Sbostic 			goto err;
16150989Sbostic 		SET(t, BTF_INMEM);
16250989Sbostic 	}
16345876Sbostic 
16450989Sbostic 	if (fcntl(t->bt_fd, F_SETFL, 1) == -1)
16550989Sbostic 		goto err;
16645876Sbostic 
16750989Sbostic 	if (fstat(t->bt_fd, &sb))
16850989Sbostic 		goto err;
16950989Sbostic 	if (sb.st_size) {
17050989Sbostic 		nr = read(t->bt_fd, &m, sizeof(BTMETA));
17150989Sbostic 		if (nr < 0)
17250989Sbostic 			goto err;
17350989Sbostic 		if (nr != sizeof(BTMETA))
17450989Sbostic 			goto eftype;
17545876Sbostic 
17650989Sbostic 		/*
17750989Sbostic 		 * Read in the meta-data.  This can change the notion of what
17850989Sbostic 		 * the lorder, page size and flags are, and, when the page size
17950989Sbostic 		 * changes the cachesize value can change as well.
18050989Sbostic 		 *
18150989Sbostic 		 * Lorder is always stored in host-independent format.
18250989Sbostic 		 */
18351967Sbostic 		m.m_lorder = ntohl(m.m_lorder);
18450989Sbostic 		if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN)
18550989Sbostic 			goto eftype;
18650989Sbostic 		if (m.m_lorder != BYTE_ORDER) {
18750989Sbostic 			BLSWAP(m.m_magic);
18850989Sbostic 			BLSWAP(m.m_version);
18950989Sbostic 			BLSWAP(m.m_psize);
19050989Sbostic 			BLSWAP(m.m_free);
19150989Sbostic 			BLSWAP(m.m_nrecs);
19250989Sbostic 			BLSWAP(m.m_flags);
19345876Sbostic 		}
19450989Sbostic 		if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
19550989Sbostic 			goto eftype;
19650989Sbostic 		if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET ||
19750989Sbostic 		    m.m_psize & sizeof(index_t) - 1)
19850989Sbostic 			goto eftype;
19951358Sbostic 		if (m.m_flags & ~SAVEMETA)
20050989Sbostic 			goto eftype;
20150989Sbostic 		b.psize = m.m_psize;
20251098Sbostic 		t->bt_flags |= m.m_flags;
20350989Sbostic 		t->bt_free = m.m_free;
20450989Sbostic 		t->bt_lorder = m.m_lorder;
20550989Sbostic 		t->bt_nrecs = m.m_nrecs;
20650989Sbostic 	} else {
20750989Sbostic 		/*
20850989Sbostic 		 * Set the page size to the best value for I/O to this file.
20950989Sbostic 		 * Don't overflow the page offset type.
21050989Sbostic 		 */
21150989Sbostic 		if (b.psize == 0) {
21250989Sbostic 			b.psize = sb.st_blksize;
21350989Sbostic 			if (b.psize < MINPSIZE)
21450989Sbostic 				b.psize = MINPSIZE;
21550989Sbostic 			if (b.psize > MAX_PAGE_OFFSET)
21650989Sbostic 				b.psize = MAX_PAGE_OFFSET;
21750989Sbostic 		}
21851098Sbostic 		t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS;
21950989Sbostic 		t->bt_free = P_INVALID;
22050989Sbostic 		t->bt_lorder = b.lorder;
22150989Sbostic 		t->bt_nrecs = 0;
22250989Sbostic 		SET(t, BTF_METADIRTY);
22345876Sbostic 	}
22445876Sbostic 
22550989Sbostic 	t->bt_psize = b.psize;
22645876Sbostic 
22750989Sbostic 	/* Set the cache size; must be a multiple of the page size. */
22850989Sbostic 	if (b.cachesize && b.cachesize & b.psize - 1)
22950989Sbostic 		b.cachesize += (~b.cachesize & b.psize - 1) + 1;
23050989Sbostic 	if (b.cachesize < b.psize * MINCACHE)
23150989Sbostic 		b.cachesize = b.psize * MINCACHE;
23245876Sbostic 
23350989Sbostic 	/* Calculate number of pages to cache. */
23450989Sbostic 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
23545876Sbostic 
23645876Sbostic 	/*
23751098Sbostic 	 * The btree data structure requires that at least two keys can fit on
23851098Sbostic 	 * a page, but other than that there's no fixed requirement.  The user
23951098Sbostic 	 * specified a minimum number per page, and we translated that into the
24051098Sbostic 	 * number of bytes a key/data pair can use before being placed on an
24151098Sbostic 	 * overflow page.  This calculation includes the page header, the size
24251098Sbostic 	 * of the index referencing the leaf item and the size of the leaf item
24351098Sbostic 	 * structure.  Also, don't let the user specify a minkeypage such that
24451098Sbostic 	 * a key/data pair won't fit even if both key and data are on overflow
24551098Sbostic 	 * pages.
24645876Sbostic 	 */
24751098Sbostic 	t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
24851098Sbostic 	    (sizeof(index_t) + NBLEAFDBT(0, 0));
24951098Sbostic 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t))
25051098Sbostic 		t->bt_ovflsize =
25151098Sbostic 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t);
25245876Sbostic 
25350989Sbostic 	/* Initialize the buffer pool. */
25450989Sbostic 	if ((t->bt_mp =
25550989Sbostic 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
25650989Sbostic 		goto err;
25751098Sbostic 	if (NOTSET(t, BTF_INMEM))
25851098Sbostic 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
25945876Sbostic 
26050989Sbostic 	/* Create a root page if new tree. */
26150989Sbostic 	if (nroot(t) == RET_ERROR)
26250989Sbostic 		goto err;
26345876Sbostic 
26450989Sbostic 	return (dbp);
26545876Sbostic 
26650989Sbostic einval:	errno = EINVAL;
26750989Sbostic 	goto err;
26845876Sbostic 
26950989Sbostic eftype:	errno = EFTYPE;
27050989Sbostic 	goto err;
27149322Sbostic 
27250989Sbostic err:	if (t) {
27350989Sbostic 		if (t->bt_dbp)
27450989Sbostic 			free(t->bt_dbp);
27550989Sbostic 		if (t->bt_fd != -1)
27650989Sbostic 			(void)close(t->bt_fd);
27750989Sbostic 		free(t);
27845876Sbostic 	}
27950989Sbostic 	return (NULL);
28045876Sbostic }
28145876Sbostic 
28245876Sbostic /*
28350989Sbostic  * NROOT -- Create the root of a new tree.
28445876Sbostic  *
28550989Sbostic  * Parameters:
28650989Sbostic  *	t:	tree
28745876Sbostic  *
28850989Sbostic  * Returns:
28950989Sbostic  *	RET_ERROR, RET_SUCCESS
29045876Sbostic  */
29150989Sbostic static int
29250989Sbostic nroot(t)
29350989Sbostic 	BTREE *t;
29445876Sbostic {
29550989Sbostic 	PAGE *meta, *root;
29650989Sbostic 	pgno_t npg;
29745876Sbostic 
29850989Sbostic 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
29950989Sbostic 		mpool_put(t->bt_mp, meta, 0);
30045876Sbostic 		return (RET_SUCCESS);
30145876Sbostic 	}
30250989Sbostic 	if (errno != EINVAL)
30350989Sbostic 		return (RET_ERROR);
30445876Sbostic 
30550989Sbostic 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
30650989Sbostic 		return (RET_ERROR);
30745876Sbostic 
30850989Sbostic 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
30950989Sbostic 		return (RET_ERROR);
31045876Sbostic 
31150989Sbostic 	if (npg != P_ROOT)
31245876Sbostic 		return (RET_ERROR);
31350989Sbostic 	root->pgno = npg;
31450989Sbostic 	root->prevpg = root->nextpg = P_INVALID;
31550989Sbostic 	root->lower = BTDATAOFF;
31650989Sbostic 	root->upper = t->bt_psize;
31750989Sbostic 	root->flags = P_BLEAF;
31851098Sbostic 	bzero(meta, t->bt_psize);
31950989Sbostic 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
32050989Sbostic 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
32150989Sbostic 	return (RET_SUCCESS);
32245876Sbostic }
32345876Sbostic 
32450989Sbostic static int
32550989Sbostic tmp()
32645876Sbostic {
32750989Sbostic 	sigset_t set, oset;
32850989Sbostic 	int fd;
32950989Sbostic 	char *envtmp;
33050989Sbostic 	char path[MAXPATHLEN];
33145876Sbostic 
33250989Sbostic 	envtmp = getenv("TMPDIR");
33350989Sbostic 	(void)snprintf(path,
33450989Sbostic 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
33549322Sbostic 
33650989Sbostic 	sigfillset(&set);
33750989Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
33850989Sbostic 	if ((fd = mkstemp(path)) != -1)
33950989Sbostic 		(void)unlink(path);
34050989Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
34150989Sbostic 	return(fd);
34245876Sbostic }
343