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*56732Sbostic static char sccsid[] = "@(#)bt_open.c 5.22 (Berkeley) 11/13/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 26*56732Sbostic #define __DBINTERFACE_PRIVATE 27*56732Sbostic #include <db.h> 28*56732Sbostic #include <errno.h> 2950989Sbostic #include <fcntl.h> 3050989Sbostic #include <limits.h> 31*56732Sbostic #include <signal.h> 3250989Sbostic #include <stdio.h> 3346561Sbostic #include <stdlib.h> 3451098Sbostic #include <string.h> 35*56732Sbostic #include <unistd.h> 36*56732Sbostic 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 72*56732Sbostic t = NULL; 73*56732Sbostic 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 16853605Sbostic #define USEFLAGS \ 16956421Sbostic (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC) 17050989Sbostic if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 17150989Sbostic goto err; 17245876Sbostic 17350989Sbostic } else { 17456421Sbostic if ((flags & O_ACCMODE) != O_RDWR) 17556421Sbostic goto einval; 17650989Sbostic if ((t->bt_fd = tmp()) == -1) 17750989Sbostic goto err; 17850989Sbostic SET(t, BTF_INMEM); 17950989Sbostic } 18045876Sbostic 18156094Sbostic if (fcntl(t->bt_fd, F_SETFD, 1) == -1) 18250989Sbostic goto err; 18345876Sbostic 18450989Sbostic if (fstat(t->bt_fd, &sb)) 18550989Sbostic goto err; 18650989Sbostic if (sb.st_size) { 18750989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 18850989Sbostic if (nr < 0) 18950989Sbostic goto err; 19050989Sbostic if (nr != sizeof(BTMETA)) 19150989Sbostic goto eftype; 19245876Sbostic 19350989Sbostic /* 19450989Sbostic * Read in the meta-data. This can change the notion of what 19550989Sbostic * the lorder, page size and flags are, and, when the page size 19650989Sbostic * changes the cachesize value can change as well. 19750989Sbostic * 19850989Sbostic * Lorder is always stored in host-independent format. 19950989Sbostic */ 20051967Sbostic m.m_lorder = ntohl(m.m_lorder); 20150989Sbostic if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 20250989Sbostic goto eftype; 20350989Sbostic if (m.m_lorder != BYTE_ORDER) { 20450989Sbostic BLSWAP(m.m_magic); 20550989Sbostic BLSWAP(m.m_version); 20650989Sbostic BLSWAP(m.m_psize); 20750989Sbostic BLSWAP(m.m_free); 20850989Sbostic BLSWAP(m.m_nrecs); 20950989Sbostic BLSWAP(m.m_flags); 21045876Sbostic } 21150989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 21250989Sbostic goto eftype; 21350989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 21450989Sbostic m.m_psize & sizeof(index_t) - 1) 21550989Sbostic goto eftype; 21651358Sbostic if (m.m_flags & ~SAVEMETA) 21750989Sbostic goto eftype; 21850989Sbostic b.psize = m.m_psize; 21951098Sbostic t->bt_flags |= m.m_flags; 22050989Sbostic t->bt_free = m.m_free; 22150989Sbostic t->bt_lorder = m.m_lorder; 22250989Sbostic t->bt_nrecs = m.m_nrecs; 22350989Sbostic } else { 22450989Sbostic /* 22550989Sbostic * Set the page size to the best value for I/O to this file. 22650989Sbostic * Don't overflow the page offset type. 22750989Sbostic */ 22850989Sbostic if (b.psize == 0) { 22950989Sbostic b.psize = sb.st_blksize; 23050989Sbostic if (b.psize < MINPSIZE) 23150989Sbostic b.psize = MINPSIZE; 23250989Sbostic if (b.psize > MAX_PAGE_OFFSET) 23350989Sbostic b.psize = MAX_PAGE_OFFSET; 23450989Sbostic } 235*56732Sbostic if (!(b.flags & R_DUP)) 236*56732Sbostic SET(t, BTF_NODUPS); 23750989Sbostic t->bt_free = P_INVALID; 23850989Sbostic t->bt_lorder = b.lorder; 23950989Sbostic t->bt_nrecs = 0; 24050989Sbostic SET(t, BTF_METADIRTY); 24145876Sbostic } 24245876Sbostic 24350989Sbostic t->bt_psize = b.psize; 24445876Sbostic 24550989Sbostic /* Set the cache size; must be a multiple of the page size. */ 24650989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 24750989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 24850989Sbostic if (b.cachesize < b.psize * MINCACHE) 24950989Sbostic b.cachesize = b.psize * MINCACHE; 25045876Sbostic 25150989Sbostic /* Calculate number of pages to cache. */ 25250989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 25345876Sbostic 25445876Sbostic /* 25551098Sbostic * The btree data structure requires that at least two keys can fit on 25651098Sbostic * a page, but other than that there's no fixed requirement. The user 25751098Sbostic * specified a minimum number per page, and we translated that into the 25851098Sbostic * number of bytes a key/data pair can use before being placed on an 25951098Sbostic * overflow page. This calculation includes the page header, the size 26051098Sbostic * of the index referencing the leaf item and the size of the leaf item 26151098Sbostic * structure. Also, don't let the user specify a minkeypage such that 26251098Sbostic * a key/data pair won't fit even if both key and data are on overflow 26351098Sbostic * pages. 26445876Sbostic */ 26551098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 26651098Sbostic (sizeof(index_t) + NBLEAFDBT(0, 0)); 26751098Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 26851098Sbostic t->bt_ovflsize = 26951098Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 27045876Sbostic 27150989Sbostic /* Initialize the buffer pool. */ 27250989Sbostic if ((t->bt_mp = 27350989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 27450989Sbostic goto err; 275*56732Sbostic if (!ISSET(t, BTF_INMEM)) 27651098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 27745876Sbostic 27850989Sbostic /* Create a root page if new tree. */ 27950989Sbostic if (nroot(t) == RET_ERROR) 28050989Sbostic goto err; 28145876Sbostic 28250989Sbostic return (dbp); 28345876Sbostic 28450989Sbostic einval: errno = EINVAL; 28550989Sbostic goto err; 28645876Sbostic 28750989Sbostic eftype: errno = EFTYPE; 28850989Sbostic goto err; 28949322Sbostic 29050989Sbostic err: if (t) { 29150989Sbostic if (t->bt_dbp) 29250989Sbostic free(t->bt_dbp); 29350989Sbostic if (t->bt_fd != -1) 29450989Sbostic (void)close(t->bt_fd); 29550989Sbostic free(t); 29645876Sbostic } 29750989Sbostic return (NULL); 29845876Sbostic } 29945876Sbostic 30045876Sbostic /* 30150989Sbostic * NROOT -- Create the root of a new tree. 30245876Sbostic * 30350989Sbostic * Parameters: 30450989Sbostic * t: tree 30545876Sbostic * 30650989Sbostic * Returns: 30750989Sbostic * RET_ERROR, RET_SUCCESS 30845876Sbostic */ 30950989Sbostic static int 31050989Sbostic nroot(t) 31150989Sbostic BTREE *t; 31245876Sbostic { 31350989Sbostic PAGE *meta, *root; 31450989Sbostic pgno_t npg; 31545876Sbostic 31650989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 31750989Sbostic mpool_put(t->bt_mp, meta, 0); 31845876Sbostic return (RET_SUCCESS); 31945876Sbostic } 32050989Sbostic if (errno != EINVAL) 32150989Sbostic return (RET_ERROR); 32245876Sbostic 32350989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 32450989Sbostic return (RET_ERROR); 32545876Sbostic 32650989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 32750989Sbostic return (RET_ERROR); 32845876Sbostic 32950989Sbostic if (npg != P_ROOT) 33045876Sbostic return (RET_ERROR); 33150989Sbostic root->pgno = npg; 33250989Sbostic root->prevpg = root->nextpg = P_INVALID; 33350989Sbostic root->lower = BTDATAOFF; 33450989Sbostic root->upper = t->bt_psize; 33550989Sbostic root->flags = P_BLEAF; 33651098Sbostic bzero(meta, t->bt_psize); 33750989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 33850989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 33950989Sbostic return (RET_SUCCESS); 34045876Sbostic } 34145876Sbostic 34250989Sbostic static int 34350989Sbostic tmp() 34445876Sbostic { 34550989Sbostic sigset_t set, oset; 34650989Sbostic int fd; 34750989Sbostic char *envtmp; 34850989Sbostic char path[MAXPATHLEN]; 34945876Sbostic 35050989Sbostic envtmp = getenv("TMPDIR"); 35150989Sbostic (void)snprintf(path, 35250989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 35349322Sbostic 35455316Sbostic (void)sigfillset(&set); 35550989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 35650989Sbostic if ((fd = mkstemp(path)) != -1) 35750989Sbostic (void)unlink(path); 35850989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 35950989Sbostic return(fd); 36045876Sbostic } 361