145876Sbostic /*-
261196Sbostic * Copyright (c) 1990, 1993
361196Sbostic * 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*66214Sbostic static char sccsid[] = "@(#)bt_open.c 8.5 (Berkeley) 02/21/94";
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 #include <db.h>
3646127Smao #include "btree.h"
3745876Sbostic
3859632Sbostic static int byteorder __P((void));
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 *
__bt_open(fname,flags,mode,openinfo,dflags)5964455Sbostic __bt_open(fname, flags, mode, openinfo, dflags)
6050989Sbostic const char *fname;
6164455Sbostic int flags, mode, dflags;
6250989Sbostic const BTREEINFO *openinfo;
6345876Sbostic {
6466192Sbostic struct stat sb;
6551098Sbostic BTMETA m;
6650989Sbostic BTREE *t;
6751098Sbostic BTREEINFO b;
6850989Sbostic DB *dbp;
6950989Sbostic pgno_t ncache;
7066192Sbostic ssize_t nr;
7166192Sbostic int machine_lorder;
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. */
131*66214Sbostic if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
13250989Sbostic goto err;
13364489Sbostic memset(t, 0, sizeof(BTREE));
13464489Sbostic t->bt_bcursor.pgno = P_INVALID;
13550989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */
13659632Sbostic t->bt_lorder = b.lorder;
13750989Sbostic t->bt_order = NOT;
13850989Sbostic t->bt_cmp = b.compare;
13950989Sbostic t->bt_pfx = b.prefix;
14064489Sbostic t->bt_rfd = -1;
14164489Sbostic
142*66214Sbostic if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
14364489Sbostic goto err;
14451800Sbostic t->bt_flags = 0;
14559632Sbostic if (t->bt_lorder != machine_lorder)
14660040Sbostic SET(t, B_NEEDSWAP);
14746453Smao
14850989Sbostic dbp->type = DB_BTREE;
14950989Sbostic dbp->internal = t;
15050989Sbostic dbp->close = __bt_close;
15150989Sbostic dbp->del = __bt_delete;
15260260Sbostic dbp->fd = __bt_fd;
15350989Sbostic dbp->get = __bt_get;
15450989Sbostic dbp->put = __bt_put;
15550989Sbostic dbp->seq = __bt_seq;
15650989Sbostic dbp->sync = __bt_sync;
15745876Sbostic
15850989Sbostic /*
15950989Sbostic * If no file name was supplied, this is an in-memory btree and we
16050989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree.
16150989Sbostic */
16250989Sbostic if (fname) {
16356421Sbostic switch(flags & O_ACCMODE) {
16456421Sbostic case O_RDONLY:
16560040Sbostic SET(t, B_RDONLY);
16656421Sbostic break;
16756421Sbostic case O_RDWR:
16856421Sbostic break;
16956421Sbostic case O_WRONLY:
17056421Sbostic default:
17156421Sbostic goto einval;
17256421Sbostic }
17356421Sbostic
17464455Sbostic if ((t->bt_fd = open(fname, flags, mode)) < 0)
17550989Sbostic goto err;
17645876Sbostic
17750989Sbostic } else {
17856421Sbostic if ((flags & O_ACCMODE) != O_RDWR)
17956421Sbostic goto einval;
18050989Sbostic if ((t->bt_fd = tmp()) == -1)
18150989Sbostic goto err;
18260040Sbostic SET(t, B_INMEM);
18350989Sbostic }
18445876Sbostic
18556094Sbostic if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
18650989Sbostic goto err;
18745876Sbostic
18850989Sbostic if (fstat(t->bt_fd, &sb))
18950989Sbostic goto err;
19050989Sbostic if (sb.st_size) {
19150989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA));
19250989Sbostic if (nr < 0)
19350989Sbostic goto err;
19450989Sbostic if (nr != sizeof(BTMETA))
19550989Sbostic goto eftype;
19645876Sbostic
19750989Sbostic /*
19850989Sbostic * Read in the meta-data. This can change the notion of what
19950989Sbostic * the lorder, page size and flags are, and, when the page size
20059632Sbostic * changes, the cachesize value can change too. If the user
20159632Sbostic * specified the wrong byte order for an existing database, we
20259632Sbostic * don't bother to return an error, we just clear the NEEDSWAP
20359632Sbostic * bit.
20450989Sbostic */
20559632Sbostic if (m.m_magic == BTREEMAGIC)
20660040Sbostic CLR(t, B_NEEDSWAP);
20759632Sbostic else {
20860040Sbostic SET(t, B_NEEDSWAP);
20966192Sbostic M_32_SWAP(m.m_magic);
21066192Sbostic M_32_SWAP(m.m_version);
21166192Sbostic M_32_SWAP(m.m_psize);
21266192Sbostic M_32_SWAP(m.m_free);
21366192Sbostic M_32_SWAP(m.m_nrecs);
21466192Sbostic M_32_SWAP(m.m_flags);
21545876Sbostic }
21650989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
21750989Sbostic goto eftype;
21860230Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 ||
21957989Sbostic m.m_psize & sizeof(indx_t) - 1)
22050989Sbostic goto eftype;
22151358Sbostic if (m.m_flags & ~SAVEMETA)
22250989Sbostic goto eftype;
22350989Sbostic b.psize = m.m_psize;
22451098Sbostic t->bt_flags |= m.m_flags;
22550989Sbostic t->bt_free = m.m_free;
22650989Sbostic t->bt_nrecs = m.m_nrecs;
22750989Sbostic } else {
22850989Sbostic /*
22950989Sbostic * Set the page size to the best value for I/O to this file.
23050989Sbostic * Don't overflow the page offset type.
23150989Sbostic */
23250989Sbostic if (b.psize == 0) {
23350989Sbostic b.psize = sb.st_blksize;
23450989Sbostic if (b.psize < MINPSIZE)
23550989Sbostic b.psize = MINPSIZE;
23660230Sbostic if (b.psize > MAX_PAGE_OFFSET + 1)
23760230Sbostic b.psize = MAX_PAGE_OFFSET + 1;
23850989Sbostic }
23959632Sbostic
24059632Sbostic /* Set flag if duplicates permitted. */
24156732Sbostic if (!(b.flags & R_DUP))
24260040Sbostic SET(t, B_NODUPS);
24359632Sbostic
24450989Sbostic t->bt_free = P_INVALID;
24550989Sbostic t->bt_nrecs = 0;
24660040Sbostic SET(t, B_METADIRTY);
24745876Sbostic }
24845876Sbostic
24950989Sbostic t->bt_psize = b.psize;
25045876Sbostic
25150989Sbostic /* Set the cache size; must be a multiple of the page size. */
25250989Sbostic if (b.cachesize && b.cachesize & b.psize - 1)
25350989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1;
25450989Sbostic if (b.cachesize < b.psize * MINCACHE)
25550989Sbostic b.cachesize = b.psize * MINCACHE;
25645876Sbostic
25750989Sbostic /* Calculate number of pages to cache. */
25850989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
25945876Sbostic
26045876Sbostic /*
26151098Sbostic * The btree data structure requires that at least two keys can fit on
26251098Sbostic * a page, but other than that there's no fixed requirement. The user
26351098Sbostic * specified a minimum number per page, and we translated that into the
26451098Sbostic * number of bytes a key/data pair can use before being placed on an
26551098Sbostic * overflow page. This calculation includes the page header, the size
26651098Sbostic * of the index referencing the leaf item and the size of the leaf item
26751098Sbostic * structure. Also, don't let the user specify a minkeypage such that
26851098Sbostic * a key/data pair won't fit even if both key and data are on overflow
26951098Sbostic * pages.
27045876Sbostic */
27151098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
27257989Sbostic (sizeof(indx_t) + NBLEAFDBT(0, 0));
27357989Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
27451098Sbostic t->bt_ovflsize =
27557989Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
27645876Sbostic
27750989Sbostic /* Initialize the buffer pool. */
27850989Sbostic if ((t->bt_mp =
27950989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
28050989Sbostic goto err;
28160040Sbostic if (!ISSET(t, B_INMEM))
28251098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
28345876Sbostic
28450989Sbostic /* Create a root page if new tree. */
28550989Sbostic if (nroot(t) == RET_ERROR)
28650989Sbostic goto err;
28745876Sbostic
28864455Sbostic /* Global flags. */
28964455Sbostic if (dflags & DB_LOCK)
29064455Sbostic SET(t, B_DB_LOCK);
29164455Sbostic if (dflags & DB_SHMEM)
29264455Sbostic SET(t, B_DB_SHMEM);
29364455Sbostic if (dflags & DB_TXN)
29464455Sbostic SET(t, B_DB_TXN);
29564455Sbostic
29650989Sbostic return (dbp);
29745876Sbostic
29850989Sbostic einval: errno = EINVAL;
29950989Sbostic goto err;
30045876Sbostic
30150989Sbostic eftype: errno = EFTYPE;
30250989Sbostic goto err;
30349322Sbostic
30450989Sbostic err: if (t) {
30550989Sbostic if (t->bt_dbp)
30650989Sbostic free(t->bt_dbp);
30750989Sbostic if (t->bt_fd != -1)
30850989Sbostic (void)close(t->bt_fd);
30950989Sbostic free(t);
31045876Sbostic }
31150989Sbostic return (NULL);
31245876Sbostic }
31345876Sbostic
31445876Sbostic /*
31550989Sbostic * NROOT -- Create the root of a new tree.
31645876Sbostic *
31750989Sbostic * Parameters:
31850989Sbostic * t: tree
31945876Sbostic *
32050989Sbostic * Returns:
32150989Sbostic * RET_ERROR, RET_SUCCESS
32245876Sbostic */
32350989Sbostic static int
nroot(t)32450989Sbostic nroot(t)
32550989Sbostic BTREE *t;
32645876Sbostic {
32750989Sbostic PAGE *meta, *root;
32850989Sbostic pgno_t npg;
32945876Sbostic
33050989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
33150989Sbostic mpool_put(t->bt_mp, meta, 0);
33245876Sbostic return (RET_SUCCESS);
33345876Sbostic }
33450989Sbostic if (errno != EINVAL)
33550989Sbostic return (RET_ERROR);
33645876Sbostic
33750989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
33850989Sbostic return (RET_ERROR);
33945876Sbostic
34050989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
34150989Sbostic return (RET_ERROR);
34245876Sbostic
34350989Sbostic if (npg != P_ROOT)
34445876Sbostic return (RET_ERROR);
34550989Sbostic root->pgno = npg;
34650989Sbostic root->prevpg = root->nextpg = P_INVALID;
34750989Sbostic root->lower = BTDATAOFF;
34850989Sbostic root->upper = t->bt_psize;
34950989Sbostic root->flags = P_BLEAF;
35058017Sbostic memset(meta, 0, t->bt_psize);
35150989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
35250989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY);
35350989Sbostic return (RET_SUCCESS);
35445876Sbostic }
35545876Sbostic
35650989Sbostic static int
tmp()35750989Sbostic tmp()
35845876Sbostic {
35950989Sbostic sigset_t set, oset;
36050989Sbostic int fd;
36150989Sbostic char *envtmp;
36250989Sbostic char path[MAXPATHLEN];
36345876Sbostic
36450989Sbostic envtmp = getenv("TMPDIR");
36550989Sbostic (void)snprintf(path,
36650989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
36749322Sbostic
36855316Sbostic (void)sigfillset(&set);
36950989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset);
37050989Sbostic if ((fd = mkstemp(path)) != -1)
37150989Sbostic (void)unlink(path);
37250989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL);
37350989Sbostic return(fd);
37445876Sbostic }
37559632Sbostic
37659632Sbostic static int
byteorder()37759632Sbostic byteorder()
37859632Sbostic {
37966192Sbostic u_int32_t x;
38059790Sbostic u_char *p;
38159632Sbostic
38259632Sbostic x = 0x01020304;
38359632Sbostic p = (u_char *)&x;
38459632Sbostic switch (*p) {
38559632Sbostic case 1:
38659632Sbostic return (BIG_ENDIAN);
38759632Sbostic case 4:
38859632Sbostic return (LITTLE_ENDIAN);
38959632Sbostic default:
39059632Sbostic return (0);
39159632Sbostic }
39259632Sbostic }
39360260Sbostic
39460260Sbostic int
__bt_fd(dbp)39560260Sbostic __bt_fd(dbp)
39660260Sbostic const DB *dbp;
39760260Sbostic {
39860260Sbostic BTREE *t;
39960260Sbostic
40060260Sbostic t = dbp->internal;
40160260Sbostic
40264455Sbostic /* Toss any page pinned across calls. */
40364455Sbostic if (t->bt_pinned != NULL) {
40464455Sbostic mpool_put(t->bt_mp, t->bt_pinned, 0);
40564455Sbostic t->bt_pinned = NULL;
40664455Sbostic }
40764455Sbostic
40864455Sbostic /* In-memory database can't have a file descriptor. */
40960260Sbostic if (ISSET(t, B_INMEM)) {
41060260Sbostic errno = ENOENT;
41160260Sbostic return (-1);
41260260Sbostic }
41360260Sbostic return (t->bt_fd);
41460260Sbostic }
415