xref: /csrg-svn/lib/libc/db/btree/bt_open.c (revision 61196)
145876Sbostic /*-
2*61196Sbostic  * Copyright (c) 1990, 1993
3*61196Sbostic  *	The Regents of the University of California.  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*61196Sbostic static char sccsid[] = "@(#)bt_open.c	8.1 (Berkeley) 06/04/93";
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 #include <errno.h>
2750989Sbostic #include <fcntl.h>
2850989Sbostic #include <limits.h>
2956732Sbostic #include <signal.h>
3050989Sbostic #include <stdio.h>
3146561Sbostic #include <stdlib.h>
3251098Sbostic #include <string.h>
3356732Sbostic #include <unistd.h>
3456732Sbostic 
3557932Sbostic #define	__DBINTERFACE_PRIVATE
3657932Sbostic #include <db.h>
3746127Smao #include "btree.h"
3845876Sbostic 
3959632Sbostic static int byteorder __P((void));
4050989Sbostic static int nroot __P((BTREE *));
4150989Sbostic static int tmp __P((void));
4245876Sbostic 
4345876Sbostic /*
4450989Sbostic  * __BT_OPEN -- Open a btree.
4545876Sbostic  *
4650989Sbostic  * Creates and fills a DB struct, and calls the routine that actually
4750989Sbostic  * opens the btree.
4845876Sbostic  *
4950989Sbostic  * Parameters:
5050989Sbostic  *	fname:	filename (NULL for in-memory trees)
5150989Sbostic  *	flags:	open flag bits
5250989Sbostic  *	mode:	open permission bits
5350989Sbostic  *	b:	BTREEINFO pointer
5445876Sbostic  *
5550989Sbostic  * Returns:
5650989Sbostic  *	NULL on failure, pointer to DB on success.
5745876Sbostic  *
5845876Sbostic  */
5945876Sbostic DB *
6050989Sbostic __bt_open(fname, flags, mode, openinfo)
6150989Sbostic 	const char *fname;
6250989Sbostic 	int flags, mode;
6350989Sbostic 	const BTREEINFO *openinfo;
6445876Sbostic {
6551098Sbostic 	BTMETA m;
6650989Sbostic 	BTREE *t;
6751098Sbostic 	BTREEINFO b;
6850989Sbostic 	DB *dbp;
6950989Sbostic 	pgno_t ncache;
7050989Sbostic 	struct stat sb;
7159632Sbostic 	int machine_lorder, nr;
7245876Sbostic 
7356732Sbostic 	t = NULL;
7456732Sbostic 
7545876Sbostic 	/*
7650989Sbostic 	 * Intention is to make sure all of the user's selections are okay
7750989Sbostic 	 * here and then use them without checking.  Can't be complete, since
7850989Sbostic 	 * we don't know the right page size, lorder or flags until the backing
7950989Sbostic 	 * file is opened.  Also, the file's page size can cause the cachesize
8050989Sbostic 	 * to change.
8145876Sbostic 	 */
8259632Sbostic 	machine_lorder = byteorder();
8350989Sbostic 	if (openinfo) {
8450989Sbostic 		b = *openinfo;
8545876Sbostic 
8650989Sbostic 		/* Flags: R_DUP. */
8751098Sbostic 		if (b.flags & ~(R_DUP))
8850989Sbostic 			goto einval;
8945876Sbostic 
9050989Sbostic 		/*
9157989Sbostic 		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
9250989Sbostic 		 * page size is set farther on, based on the underlying file
9350989Sbostic 		 * transfer size.
9450989Sbostic 		 */
9550989Sbostic 		if (b.psize &&
9660230Sbostic 		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
9757989Sbostic 		    b.psize & sizeof(indx_t) - 1))
9851421Sbostic 			goto einval;
9945876Sbostic 
10051098Sbostic 		/* Minimum number of keys per page; absolute minimum is 2. */
10150989Sbostic 		if (b.minkeypage) {
10250989Sbostic 			if (b.minkeypage < 2)
10350989Sbostic 				goto einval;
10450989Sbostic 		} else
10551098Sbostic 			b.minkeypage = DEFMINKEYPAGE;
10651098Sbostic 
10750989Sbostic 		/* If no comparison, use default comparison and prefix. */
10850989Sbostic 		if (b.compare == NULL) {
10950989Sbostic 			b.compare = __bt_defcmp;
11050989Sbostic 			if (b.prefix == NULL)
11150989Sbostic 				b.prefix = __bt_defpfx;
11245876Sbostic 		}
11345876Sbostic 
11450989Sbostic 		if (b.lorder == 0)
11559632Sbostic 			b.lorder = machine_lorder;
11650989Sbostic 	} else {
11751421Sbostic 		b.compare = __bt_defcmp;
11856163Sbostic 		b.cachesize = 0;
11950989Sbostic 		b.flags = 0;
12059632Sbostic 		b.lorder = machine_lorder;
12150989Sbostic 		b.minkeypage = DEFMINKEYPAGE;
12250989Sbostic 		b.prefix = __bt_defpfx;
12351421Sbostic 		b.psize = 0;
12450989Sbostic 	}
12545876Sbostic 
12659632Sbostic 	/* Check for the ubiquitous PDP-11. */
12759632Sbostic 	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
12859632Sbostic 		goto einval;
12959632Sbostic 
13050989Sbostic 	/* Allocate and initialize DB and BTREE structures. */
13150989Sbostic 	if ((t = malloc(sizeof(BTREE))) == NULL)
13250989Sbostic 		goto err;
13350989Sbostic 	t->bt_fd = -1;			/* Don't close unopened fd on error. */
13450989Sbostic 	if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL)
13550989Sbostic 		goto err;
13650989Sbostic 	t->bt_bcursor.pgno = P_INVALID;
13750989Sbostic 	t->bt_bcursor.index = 0;
13850989Sbostic 	t->bt_stack = NULL;
13950989Sbostic 	t->bt_sp = t->bt_maxstack = 0;
14050989Sbostic 	t->bt_kbuf = t->bt_dbuf = NULL;
14150989Sbostic 	t->bt_kbufsz = t->bt_dbufsz = 0;
14259632Sbostic 	t->bt_lorder = b.lorder;
14350989Sbostic 	t->bt_order = NOT;
14450989Sbostic 	t->bt_cmp = b.compare;
14550989Sbostic 	t->bt_pfx = b.prefix;
14651800Sbostic 	t->bt_flags = 0;
14759632Sbostic 	if (t->bt_lorder != machine_lorder)
14860040Sbostic 		SET(t, B_NEEDSWAP);
14946453Smao 
15050989Sbostic 	dbp->type = DB_BTREE;
15150989Sbostic 	dbp->internal = t;
15250989Sbostic 	dbp->close = __bt_close;
15350989Sbostic 	dbp->del = __bt_delete;
15460260Sbostic 	dbp->fd = __bt_fd;
15550989Sbostic 	dbp->get = __bt_get;
15650989Sbostic 	dbp->put = __bt_put;
15750989Sbostic 	dbp->seq = __bt_seq;
15850989Sbostic 	dbp->sync = __bt_sync;
15945876Sbostic 
16050989Sbostic 	/*
16150989Sbostic 	 * If no file name was supplied, this is an in-memory btree and we
16250989Sbostic 	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
16350989Sbostic 	 */
16450989Sbostic 	if (fname) {
16556421Sbostic 		switch(flags & O_ACCMODE) {
16656421Sbostic 		case O_RDONLY:
16760040Sbostic 			SET(t, B_RDONLY);
16856421Sbostic 			break;
16956421Sbostic 		case O_RDWR:
17056421Sbostic 			break;
17156421Sbostic 		case O_WRONLY:
17256421Sbostic 		default:
17356421Sbostic 			goto einval;
17456421Sbostic 		}
17556421Sbostic 
17656894Sbostic 		if ((t->bt_fd =
17756894Sbostic 		    open(fname, flags & __USE_OPEN_FLAGS, mode)) < 0)
17850989Sbostic 			goto err;
17945876Sbostic 
18050989Sbostic 	} else {
18156421Sbostic 		if ((flags & O_ACCMODE) != O_RDWR)
18256421Sbostic 			goto einval;
18350989Sbostic 		if ((t->bt_fd = tmp()) == -1)
18450989Sbostic 			goto err;
18560040Sbostic 		SET(t, B_INMEM);
18650989Sbostic 	}
18745876Sbostic 
18856094Sbostic 	if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
18950989Sbostic 		goto err;
19045876Sbostic 
19150989Sbostic 	if (fstat(t->bt_fd, &sb))
19250989Sbostic 		goto err;
19350989Sbostic 	if (sb.st_size) {
19450989Sbostic 		nr = read(t->bt_fd, &m, sizeof(BTMETA));
19550989Sbostic 		if (nr < 0)
19650989Sbostic 			goto err;
19750989Sbostic 		if (nr != sizeof(BTMETA))
19850989Sbostic 			goto eftype;
19945876Sbostic 
20050989Sbostic 		/*
20150989Sbostic 		 * Read in the meta-data.  This can change the notion of what
20250989Sbostic 		 * the lorder, page size and flags are, and, when the page size
20359632Sbostic 		 * changes, the cachesize value can change too.  If the user
20459632Sbostic 		 * specified the wrong byte order for an existing database, we
20559632Sbostic 		 * don't bother to return an error, we just clear the NEEDSWAP
20659632Sbostic 		 * bit.
20750989Sbostic 		 */
20859632Sbostic 		if (m.m_magic == BTREEMAGIC)
20960040Sbostic 			CLR(t, B_NEEDSWAP);
21059632Sbostic 		else {
21160040Sbostic 			SET(t, B_NEEDSWAP);
21250989Sbostic 			BLSWAP(m.m_magic);
21350989Sbostic 			BLSWAP(m.m_version);
21450989Sbostic 			BLSWAP(m.m_psize);
21550989Sbostic 			BLSWAP(m.m_free);
21650989Sbostic 			BLSWAP(m.m_nrecs);
21750989Sbostic 			BLSWAP(m.m_flags);
21845876Sbostic 		}
21950989Sbostic 		if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
22050989Sbostic 			goto eftype;
22160230Sbostic 		if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 ||
22257989Sbostic 		    m.m_psize & sizeof(indx_t) - 1)
22350989Sbostic 			goto eftype;
22451358Sbostic 		if (m.m_flags & ~SAVEMETA)
22550989Sbostic 			goto eftype;
22650989Sbostic 		b.psize = m.m_psize;
22751098Sbostic 		t->bt_flags |= m.m_flags;
22850989Sbostic 		t->bt_free = m.m_free;
22950989Sbostic 		t->bt_nrecs = m.m_nrecs;
23050989Sbostic 	} else {
23150989Sbostic 		/*
23250989Sbostic 		 * Set the page size to the best value for I/O to this file.
23350989Sbostic 		 * Don't overflow the page offset type.
23450989Sbostic 		 */
23550989Sbostic 		if (b.psize == 0) {
23650989Sbostic 			b.psize = sb.st_blksize;
23750989Sbostic 			if (b.psize < MINPSIZE)
23850989Sbostic 				b.psize = MINPSIZE;
23960230Sbostic 			if (b.psize > MAX_PAGE_OFFSET + 1)
24060230Sbostic 				b.psize = MAX_PAGE_OFFSET + 1;
24150989Sbostic 		}
24259632Sbostic 
24359632Sbostic 		/* Set flag if duplicates permitted. */
24456732Sbostic 		if (!(b.flags & R_DUP))
24560040Sbostic 			SET(t, B_NODUPS);
24659632Sbostic 
24750989Sbostic 		t->bt_free = P_INVALID;
24850989Sbostic 		t->bt_nrecs = 0;
24960040Sbostic 		SET(t, B_METADIRTY);
25045876Sbostic 	}
25145876Sbostic 
25250989Sbostic 	t->bt_psize = b.psize;
25345876Sbostic 
25450989Sbostic 	/* Set the cache size; must be a multiple of the page size. */
25550989Sbostic 	if (b.cachesize && b.cachesize & b.psize - 1)
25650989Sbostic 		b.cachesize += (~b.cachesize & b.psize - 1) + 1;
25750989Sbostic 	if (b.cachesize < b.psize * MINCACHE)
25850989Sbostic 		b.cachesize = b.psize * MINCACHE;
25945876Sbostic 
26050989Sbostic 	/* Calculate number of pages to cache. */
26150989Sbostic 	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
26245876Sbostic 
26345876Sbostic 	/*
26451098Sbostic 	 * The btree data structure requires that at least two keys can fit on
26551098Sbostic 	 * a page, but other than that there's no fixed requirement.  The user
26651098Sbostic 	 * specified a minimum number per page, and we translated that into the
26751098Sbostic 	 * number of bytes a key/data pair can use before being placed on an
26851098Sbostic 	 * overflow page.  This calculation includes the page header, the size
26951098Sbostic 	 * of the index referencing the leaf item and the size of the leaf item
27051098Sbostic 	 * structure.  Also, don't let the user specify a minkeypage such that
27151098Sbostic 	 * a key/data pair won't fit even if both key and data are on overflow
27251098Sbostic 	 * pages.
27345876Sbostic 	 */
27451098Sbostic 	t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
27557989Sbostic 	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
27657989Sbostic 	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
27751098Sbostic 		t->bt_ovflsize =
27857989Sbostic 		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
27945876Sbostic 
28050989Sbostic 	/* Initialize the buffer pool. */
28150989Sbostic 	if ((t->bt_mp =
28250989Sbostic 	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
28350989Sbostic 		goto err;
28460040Sbostic 	if (!ISSET(t, B_INMEM))
28551098Sbostic 		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
28645876Sbostic 
28750989Sbostic 	/* Create a root page if new tree. */
28850989Sbostic 	if (nroot(t) == RET_ERROR)
28950989Sbostic 		goto err;
29045876Sbostic 
29150989Sbostic 	return (dbp);
29245876Sbostic 
29350989Sbostic einval:	errno = EINVAL;
29450989Sbostic 	goto err;
29545876Sbostic 
29650989Sbostic eftype:	errno = EFTYPE;
29750989Sbostic 	goto err;
29849322Sbostic 
29950989Sbostic err:	if (t) {
30050989Sbostic 		if (t->bt_dbp)
30150989Sbostic 			free(t->bt_dbp);
30250989Sbostic 		if (t->bt_fd != -1)
30350989Sbostic 			(void)close(t->bt_fd);
30450989Sbostic 		free(t);
30545876Sbostic 	}
30650989Sbostic 	return (NULL);
30745876Sbostic }
30845876Sbostic 
30945876Sbostic /*
31050989Sbostic  * NROOT -- Create the root of a new tree.
31145876Sbostic  *
31250989Sbostic  * Parameters:
31350989Sbostic  *	t:	tree
31445876Sbostic  *
31550989Sbostic  * Returns:
31650989Sbostic  *	RET_ERROR, RET_SUCCESS
31745876Sbostic  */
31850989Sbostic static int
31950989Sbostic nroot(t)
32050989Sbostic 	BTREE *t;
32145876Sbostic {
32250989Sbostic 	PAGE *meta, *root;
32350989Sbostic 	pgno_t npg;
32445876Sbostic 
32550989Sbostic 	if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
32650989Sbostic 		mpool_put(t->bt_mp, meta, 0);
32745876Sbostic 		return (RET_SUCCESS);
32845876Sbostic 	}
32950989Sbostic 	if (errno != EINVAL)
33050989Sbostic 		return (RET_ERROR);
33145876Sbostic 
33250989Sbostic 	if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
33350989Sbostic 		return (RET_ERROR);
33445876Sbostic 
33550989Sbostic 	if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
33650989Sbostic 		return (RET_ERROR);
33745876Sbostic 
33850989Sbostic 	if (npg != P_ROOT)
33945876Sbostic 		return (RET_ERROR);
34050989Sbostic 	root->pgno = npg;
34150989Sbostic 	root->prevpg = root->nextpg = P_INVALID;
34250989Sbostic 	root->lower = BTDATAOFF;
34350989Sbostic 	root->upper = t->bt_psize;
34450989Sbostic 	root->flags = P_BLEAF;
34558017Sbostic 	memset(meta, 0, t->bt_psize);
34650989Sbostic 	mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
34750989Sbostic 	mpool_put(t->bt_mp, root, MPOOL_DIRTY);
34850989Sbostic 	return (RET_SUCCESS);
34945876Sbostic }
35045876Sbostic 
35150989Sbostic static int
35250989Sbostic tmp()
35345876Sbostic {
35450989Sbostic 	sigset_t set, oset;
35550989Sbostic 	int fd;
35650989Sbostic 	char *envtmp;
35750989Sbostic 	char path[MAXPATHLEN];
35845876Sbostic 
35950989Sbostic 	envtmp = getenv("TMPDIR");
36050989Sbostic 	(void)snprintf(path,
36150989Sbostic 	    sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
36249322Sbostic 
36355316Sbostic 	(void)sigfillset(&set);
36450989Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
36550989Sbostic 	if ((fd = mkstemp(path)) != -1)
36650989Sbostic 		(void)unlink(path);
36750989Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
36850989Sbostic 	return(fd);
36945876Sbostic }
37059632Sbostic 
37159632Sbostic static int
37259632Sbostic byteorder()
37359632Sbostic {
37459632Sbostic 	u_long x;			/* XXX: 32-bit assumption. */
37559790Sbostic 	u_char *p;
37659632Sbostic 
37759632Sbostic 	x = 0x01020304;
37859632Sbostic 	p = (u_char *)&x;
37959632Sbostic 	switch (*p) {
38059632Sbostic 	case 1:
38159632Sbostic 		return (BIG_ENDIAN);
38259632Sbostic 	case 4:
38359632Sbostic 		return (LITTLE_ENDIAN);
38459632Sbostic 	default:
38559632Sbostic 		return (0);
38659632Sbostic 	}
38759632Sbostic }
38860260Sbostic 
38960260Sbostic int
39060260Sbostic __bt_fd(dbp)
39160260Sbostic         const DB *dbp;
39260260Sbostic {
39360260Sbostic 	BTREE *t;
39460260Sbostic 
39560260Sbostic 	t = dbp->internal;
39660260Sbostic 
39760260Sbostic 	if (ISSET(t, B_INMEM)) {
39860260Sbostic 		errno = ENOENT;
39960260Sbostic 		return (-1);
40060260Sbostic 	}
40160260Sbostic 	return (t->bt_fd);
40260260Sbostic }
403