xref: /csrg-svn/lib/libc/db/btree/bt_open.c (revision 51098)
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*51098Sbostic static char sccsid[] = "@(#)bt_open.c	5.11 (Berkeley) 09/12/91";
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>
33*51098Sbostic #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 {
61*51098Sbostic 	BTMETA m;
6250989Sbostic 	BTREE *t;
63*51098Sbostic 	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. */
80*51098Sbostic 		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))
9150989Sbostic 				goto einval;
9245876Sbostic 
93*51098Sbostic 		/* Minimum number of keys per page; absolute minimum is 2. */
9450989Sbostic 		if (b.minkeypage) {
9550989Sbostic 			if (b.minkeypage < 2)
9650989Sbostic 				goto einval;
9750989Sbostic 		} else
98*51098Sbostic 			b.minkeypage = DEFMINKEYPAGE;
99*51098Sbostic 
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 {
11250989Sbostic 		b.flags = 0;
11350989Sbostic 		b.minkeypage = DEFMINKEYPAGE;
11450989Sbostic 		b.compare = __bt_defcmp;
11550989Sbostic 		b.prefix = __bt_defpfx;
11650989Sbostic 		b.lorder = BYTE_ORDER;
11750989Sbostic 	}
11845876Sbostic 
11950989Sbostic 	/* Allocate and initialize DB and BTREE structures. */
12050989Sbostic 	if ((t = malloc(sizeof(BTREE))) == NULL)
12150989Sbostic 		goto err;
12250989Sbostic 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
12350989Sbostic 	if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL)
12450989Sbostic 		goto err;
12550989Sbostic 	t->bt_bcursor.pgno = P_INVALID;
12650989Sbostic 	t->bt_bcursor.index = 0;
12750989Sbostic 	t->bt_stack = NULL;
12850989Sbostic 	t->bt_sp = t->bt_maxstack = 0;
12950989Sbostic 	t->bt_kbuf = t->bt_dbuf = NULL;
13050989Sbostic 	t->bt_kbufsz = t->bt_dbufsz = 0;
13150989Sbostic 	t->bt_order = NOT;
13250989Sbostic 	t->bt_cmp = b.compare;
13350989Sbostic 	t->bt_pfx = b.prefix;
13446453Smao 
13550989Sbostic 	dbp->type = DB_BTREE;
13650989Sbostic 	dbp->internal = t;
13750989Sbostic 	dbp->close = __bt_close;
13850989Sbostic 	dbp->del = __bt_delete;
13950989Sbostic 	dbp->get = __bt_get;
14050989Sbostic 	dbp->put = __bt_put;
14150989Sbostic 	dbp->seq = __bt_seq;
14250989Sbostic 	dbp->sync = __bt_sync;
14345876Sbostic 
14450989Sbostic 	/*
14550989Sbostic 	 * If no file name was supplied, this is an in-memory btree and we
14650989Sbostic 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
14750989Sbostic 	 */
14850989Sbostic 	if (fname) {
14950989Sbostic #define	USEFLAGS	(O_CREAT|O_EXCL|O_RDONLY|O_RDWR|O_TRUNC|O_WRONLY)
15050989Sbostic 		if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0)
15150989Sbostic 			goto err;
15250989Sbostic 		if ((flags & O_ACCMODE) == O_RDONLY)
15350989Sbostic 			SET(t, BTF_RDONLY);
15445876Sbostic 
15550989Sbostic 	} else {
15650989Sbostic 		if ((t->bt_fd = tmp()) == -1)
15750989Sbostic 			goto err;
15850989Sbostic 		SET(t, BTF_INMEM);
15950989Sbostic 	}
16045876Sbostic 
16150989Sbostic 	if (fcntl(t->bt_fd, F_SETFL, 1) == -1)
16250989Sbostic 		goto err;
16345876Sbostic 
16450989Sbostic 	if (fstat(t->bt_fd, &sb))
16550989Sbostic 		goto err;
16650989Sbostic 	if (sb.st_size) {
16750989Sbostic 		nr = read(t->bt_fd, &m, sizeof(BTMETA));
16850989Sbostic 		if (nr < 0)
16950989Sbostic 			goto err;
17050989Sbostic 		if (nr != sizeof(BTMETA))
17150989Sbostic 			goto eftype;
17245876Sbostic 
17350989Sbostic 		/*
17450989Sbostic 		 * Read in the meta-data.  This can change the notion of what
17550989Sbostic 		 * the lorder, page size and flags are, and, when the page size
17650989Sbostic 		 * changes the cachesize value can change as well.
17750989Sbostic 		 *
17850989Sbostic 		 * Lorder is always stored in host-independent format.
17950989Sbostic 		 */
18050989Sbostic 		NTOHL(m.m_lorder);
18150989Sbostic 		if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN)
18250989Sbostic 			goto eftype;
18350989Sbostic 		if (m.m_lorder != BYTE_ORDER) {
18450989Sbostic 			BLSWAP(m.m_magic);
18550989Sbostic 			BLSWAP(m.m_version);
18650989Sbostic 			BLSWAP(m.m_psize);
18750989Sbostic 			BLSWAP(m.m_free);
18850989Sbostic 			BLSWAP(m.m_nrecs);
18950989Sbostic 			BLSWAP(m.m_flags);
19045876Sbostic 		}
19150989Sbostic 		if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
19250989Sbostic 			goto eftype;
19350989Sbostic 		if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET ||
19450989Sbostic 		    m.m_psize & sizeof(index_t) - 1)
19550989Sbostic 			goto eftype;
19650989Sbostic 		if (m.m_flags | ~SAVEMETA)
19750989Sbostic 			goto eftype;
19850989Sbostic 		b.psize = m.m_psize;
199*51098Sbostic 		t->bt_flags |= m.m_flags;
20050989Sbostic 		t->bt_free = m.m_free;
20150989Sbostic 		t->bt_lorder = m.m_lorder;
20250989Sbostic 		t->bt_nrecs = m.m_nrecs;
20350989Sbostic 	} else {
20450989Sbostic 		/*
20550989Sbostic 		 * Set the page size to the best value for I/O to this file.
20650989Sbostic 		 * Don't overflow the page offset type.
20750989Sbostic 		 */
20850989Sbostic 		if (b.psize == 0) {
20950989Sbostic 			b.psize = sb.st_blksize;
21050989Sbostic 			if (b.psize < MINPSIZE)
21150989Sbostic 				b.psize = MINPSIZE;
21250989Sbostic 			if (b.psize > MAX_PAGE_OFFSET)
21350989Sbostic 				b.psize = MAX_PAGE_OFFSET;
21450989Sbostic 		}
215*51098Sbostic 		t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS;
21650989Sbostic 		t->bt_free = P_INVALID;
21750989Sbostic 		t->bt_lorder = b.lorder;
21850989Sbostic 		t->bt_nrecs = 0;
21950989Sbostic 		SET(t, BTF_METADIRTY);
22045876Sbostic 	}
22145876Sbostic 
22250989Sbostic 	t->bt_psize = b.psize;
22345876Sbostic 
22450989Sbostic 	/* Set the cache size; must be a multiple of the page size. */
22550989Sbostic 	if (b.cachesize && b.cachesize & b.psize - 1)
22650989Sbostic 		b.cachesize += (~b.cachesize & b.psize - 1) + 1;
22750989Sbostic 	if (b.cachesize < b.psize * MINCACHE)
22850989Sbostic 		b.cachesize = b.psize * MINCACHE;
22945876Sbostic 
23050989Sbostic 	/* Calculate number of pages to cache. */
23150989Sbostic 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
23245876Sbostic 
23345876Sbostic 	/*
234*51098Sbostic 	 * The btree data structure requires that at least two keys can fit on
235*51098Sbostic 	 * a page, but other than that there's no fixed requirement.  The user
236*51098Sbostic 	 * specified a minimum number per page, and we translated that into the
237*51098Sbostic 	 * number of bytes a key/data pair can use before being placed on an
238*51098Sbostic 	 * overflow page.  This calculation includes the page header, the size
239*51098Sbostic 	 * of the index referencing the leaf item and the size of the leaf item
240*51098Sbostic 	 * structure.  Also, don't let the user specify a minkeypage such that
241*51098Sbostic 	 * a key/data pair won't fit even if both key and data are on overflow
242*51098Sbostic 	 * pages.
24345876Sbostic 	 */
244*51098Sbostic 	t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
245*51098Sbostic 	    (sizeof(index_t) + NBLEAFDBT(0, 0));
246*51098Sbostic 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t))
247*51098Sbostic 		t->bt_ovflsize =
248*51098Sbostic 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t);
24945876Sbostic 
25050989Sbostic 	/* Initialize the buffer pool. */
25150989Sbostic 	if ((t->bt_mp =
25250989Sbostic 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
25350989Sbostic 		goto err;
254*51098Sbostic 	if (NOTSET(t, BTF_INMEM))
255*51098Sbostic 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
25645876Sbostic 
25750989Sbostic 	/* Create a root page if new tree. */
25850989Sbostic 	if (nroot(t) == RET_ERROR)
25950989Sbostic 		goto err;
26045876Sbostic 
26150989Sbostic 	return (dbp);
26245876Sbostic 
26350989Sbostic einval:	errno = EINVAL;
26450989Sbostic 	goto err;
26545876Sbostic 
26650989Sbostic eftype:	errno = EFTYPE;
26750989Sbostic 	goto err;
26849322Sbostic 
26950989Sbostic err:	if (t) {
27050989Sbostic 		if (t->bt_dbp)
27150989Sbostic 			free(t->bt_dbp);
27250989Sbostic 		if (t->bt_fd != -1)
27350989Sbostic 			(void)close(t->bt_fd);
27450989Sbostic 		free(t);
27545876Sbostic 	}
27650989Sbostic 	return (NULL);
27745876Sbostic }
27845876Sbostic 
27945876Sbostic /*
28050989Sbostic  * NROOT -- Create the root of a new tree.
28145876Sbostic  *
28250989Sbostic  * Parameters:
28350989Sbostic  *	t:	tree
28445876Sbostic  *
28550989Sbostic  * Returns:
28650989Sbostic  *	RET_ERROR, RET_SUCCESS
28745876Sbostic  */
28850989Sbostic static int
28950989Sbostic nroot(t)
29050989Sbostic 	BTREE *t;
29145876Sbostic {
29250989Sbostic 	PAGE *meta, *root;
29350989Sbostic 	pgno_t npg;
29445876Sbostic 
29550989Sbostic 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
29650989Sbostic 		mpool_put(t->bt_mp, meta, 0);
29745876Sbostic 		return (RET_SUCCESS);
29845876Sbostic 	}
29950989Sbostic 	if (errno != EINVAL)
30050989Sbostic 		return (RET_ERROR);
30145876Sbostic 
30250989Sbostic 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
30350989Sbostic 		return (RET_ERROR);
30445876Sbostic 
30550989Sbostic 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
30650989Sbostic 		return (RET_ERROR);
30745876Sbostic 
30850989Sbostic 	if (npg != P_ROOT)
30945876Sbostic 		return (RET_ERROR);
31050989Sbostic 	root->pgno = npg;
31150989Sbostic 	root->prevpg = root->nextpg = P_INVALID;
31250989Sbostic 	root->lower = BTDATAOFF;
31350989Sbostic 	root->upper = t->bt_psize;
31450989Sbostic 	root->flags = P_BLEAF;
315*51098Sbostic 	bzero(meta, t->bt_psize);
31650989Sbostic 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
31750989Sbostic 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
31850989Sbostic 	return (RET_SUCCESS);
31945876Sbostic }
32045876Sbostic 
32150989Sbostic static int
32250989Sbostic tmp()
32345876Sbostic {
32450989Sbostic 	sigset_t set, oset;
32550989Sbostic 	int fd;
32650989Sbostic 	char *envtmp;
32750989Sbostic 	char path[MAXPATHLEN];
32845876Sbostic 
32950989Sbostic 	envtmp = getenv("TMPDIR");
33050989Sbostic 	(void)snprintf(path,
33150989Sbostic 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
33249322Sbostic 
33350989Sbostic 	sigfillset(&set);
33450989Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
33550989Sbostic 	if ((fd = mkstemp(path)) != -1)
33650989Sbostic 		(void)unlink(path);
33750989Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
33850989Sbostic 	return(fd);
33945876Sbostic }
340