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*56421Sbostic static char sccsid[] = "@(#)bt_open.c 5.21 (Berkeley) 10/04/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 2655316Sbostic #include <signal.h> 2750989Sbostic #include <fcntl.h> 2846561Sbostic #include <errno.h> 2950989Sbostic #include <limits.h> 3050989Sbostic #define __DBINTERFACE_PRIVATE 3145876Sbostic #include <db.h> 3250989Sbostic #include <stdio.h> 3350989Sbostic #include <unistd.h> 3446561Sbostic #include <stdlib.h> 3551098Sbostic #include <string.h> 3646127Smao #include "btree.h" 3745876Sbostic 3850989Sbostic static int nroot __P((BTREE *)); 3950989Sbostic static int tmp __P((void)); 4045876Sbostic 4145876Sbostic /* 4250989Sbostic * __BT_OPEN -- Open a btree. 4345876Sbostic * 4450989Sbostic * Creates and fills a DB struct, and calls the routine that actually 4550989Sbostic * opens the btree. 4645876Sbostic * 4750989Sbostic * Parameters: 4850989Sbostic * fname: filename (NULL for in-memory trees) 4950989Sbostic * flags: open flag bits 5050989Sbostic * mode: open permission bits 5150989Sbostic * b: BTREEINFO pointer 5245876Sbostic * 5350989Sbostic * Returns: 5450989Sbostic * NULL on failure, pointer to DB on success. 5545876Sbostic * 5645876Sbostic */ 5745876Sbostic DB * 5850989Sbostic __bt_open(fname, flags, mode, openinfo) 5950989Sbostic const char *fname; 6050989Sbostic int flags, mode; 6150989Sbostic const BTREEINFO *openinfo; 6245876Sbostic { 6351098Sbostic BTMETA m; 6450989Sbostic BTREE *t; 6551098Sbostic BTREEINFO b; 6650989Sbostic DB *dbp; 6750989Sbostic pgno_t ncache; 6850989Sbostic struct stat sb; 6950989Sbostic int nr; 7045876Sbostic 7145876Sbostic /* 7250989Sbostic * Intention is to make sure all of the user's selections are okay 7350989Sbostic * here and then use them without checking. Can't be complete, since 7450989Sbostic * we don't know the right page size, lorder or flags until the backing 7550989Sbostic * file is opened. Also, the file's page size can cause the cachesize 7650989Sbostic * to change. 7745876Sbostic */ 7850989Sbostic if (openinfo) { 7950989Sbostic b = *openinfo; 8045876Sbostic 8150989Sbostic /* Flags: R_DUP. */ 8251098Sbostic if (b.flags & ~(R_DUP)) 8350989Sbostic goto einval; 8445876Sbostic 8550989Sbostic /* 8650989Sbostic * Page size must be index_t aligned and >= MINPSIZE. Default 8750989Sbostic * page size is set farther on, based on the underlying file 8850989Sbostic * transfer size. 8950989Sbostic */ 9050989Sbostic if (b.psize && 9150989Sbostic (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET || 9250989Sbostic b.psize & sizeof(index_t) - 1)) 9351421Sbostic goto einval; 9445876Sbostic 9551098Sbostic /* Minimum number of keys per page; absolute minimum is 2. */ 9650989Sbostic if (b.minkeypage) { 9750989Sbostic if (b.minkeypage < 2) 9850989Sbostic goto einval; 9950989Sbostic } else 10051098Sbostic b.minkeypage = DEFMINKEYPAGE; 10151098Sbostic 10250989Sbostic /* If no comparison, use default comparison and prefix. */ 10350989Sbostic if (b.compare == NULL) { 10450989Sbostic b.compare = __bt_defcmp; 10550989Sbostic if (b.prefix == NULL) 10650989Sbostic b.prefix = __bt_defpfx; 10745876Sbostic } 10845876Sbostic 10950989Sbostic if (b.lorder == 0) 11050989Sbostic b.lorder = BYTE_ORDER; 11150989Sbostic else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) 11250989Sbostic goto einval; 11350989Sbostic } else { 11451421Sbostic b.compare = __bt_defcmp; 11556163Sbostic b.cachesize = 0; 11650989Sbostic b.flags = 0; 11751421Sbostic b.lorder = BYTE_ORDER; 11850989Sbostic b.minkeypage = DEFMINKEYPAGE; 11950989Sbostic b.prefix = __bt_defpfx; 12051421Sbostic b.psize = 0; 12150989Sbostic } 12245876Sbostic 12350989Sbostic /* Allocate and initialize DB and BTREE structures. */ 12450989Sbostic if ((t = malloc(sizeof(BTREE))) == NULL) 12550989Sbostic goto err; 12650989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */ 12750989Sbostic if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 12850989Sbostic goto err; 12950989Sbostic t->bt_bcursor.pgno = P_INVALID; 13050989Sbostic t->bt_bcursor.index = 0; 13150989Sbostic t->bt_stack = NULL; 13250989Sbostic t->bt_sp = t->bt_maxstack = 0; 13350989Sbostic t->bt_kbuf = t->bt_dbuf = NULL; 13450989Sbostic t->bt_kbufsz = t->bt_dbufsz = 0; 13550989Sbostic t->bt_order = NOT; 13650989Sbostic t->bt_cmp = b.compare; 13750989Sbostic t->bt_pfx = b.prefix; 13851800Sbostic t->bt_flags = 0; 13946453Smao 14050989Sbostic dbp->type = DB_BTREE; 14150989Sbostic dbp->internal = t; 14250989Sbostic dbp->close = __bt_close; 14350989Sbostic dbp->del = __bt_delete; 14450989Sbostic dbp->get = __bt_get; 14550989Sbostic dbp->put = __bt_put; 14650989Sbostic dbp->seq = __bt_seq; 14750989Sbostic dbp->sync = __bt_sync; 14845876Sbostic 14950989Sbostic /* 15050989Sbostic * If no file name was supplied, this is an in-memory btree and we 15150989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 15250989Sbostic */ 15350989Sbostic if (fname) { 154*56421Sbostic switch(flags & O_ACCMODE) { 155*56421Sbostic case O_RDONLY: 156*56421Sbostic SET(t, BTF_RDONLY); 157*56421Sbostic break; 158*56421Sbostic case O_RDWR: 159*56421Sbostic break; 160*56421Sbostic case O_WRONLY: 161*56421Sbostic default: 162*56421Sbostic goto einval; 163*56421Sbostic } 164*56421Sbostic 16553605Sbostic #define USEFLAGS \ 166*56421Sbostic (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC) 16750989Sbostic if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 16850989Sbostic goto err; 16945876Sbostic 17050989Sbostic } else { 171*56421Sbostic if ((flags & O_ACCMODE) != O_RDWR) 172*56421Sbostic goto einval; 17350989Sbostic if ((t->bt_fd = tmp()) == -1) 17450989Sbostic goto err; 17550989Sbostic SET(t, BTF_INMEM); 17650989Sbostic } 17745876Sbostic 17856094Sbostic if (fcntl(t->bt_fd, F_SETFD, 1) == -1) 17950989Sbostic goto err; 18045876Sbostic 18150989Sbostic if (fstat(t->bt_fd, &sb)) 18250989Sbostic goto err; 18350989Sbostic if (sb.st_size) { 18450989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 18550989Sbostic if (nr < 0) 18650989Sbostic goto err; 18750989Sbostic if (nr != sizeof(BTMETA)) 18850989Sbostic goto eftype; 18945876Sbostic 19050989Sbostic /* 19150989Sbostic * Read in the meta-data. This can change the notion of what 19250989Sbostic * the lorder, page size and flags are, and, when the page size 19350989Sbostic * changes the cachesize value can change as well. 19450989Sbostic * 19550989Sbostic * Lorder is always stored in host-independent format. 19650989Sbostic */ 19751967Sbostic m.m_lorder = ntohl(m.m_lorder); 19850989Sbostic if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 19950989Sbostic goto eftype; 20050989Sbostic if (m.m_lorder != BYTE_ORDER) { 20150989Sbostic BLSWAP(m.m_magic); 20250989Sbostic BLSWAP(m.m_version); 20350989Sbostic BLSWAP(m.m_psize); 20450989Sbostic BLSWAP(m.m_free); 20550989Sbostic BLSWAP(m.m_nrecs); 20650989Sbostic BLSWAP(m.m_flags); 20745876Sbostic } 20850989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 20950989Sbostic goto eftype; 21050989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 21150989Sbostic m.m_psize & sizeof(index_t) - 1) 21250989Sbostic goto eftype; 21351358Sbostic if (m.m_flags & ~SAVEMETA) 21450989Sbostic goto eftype; 21550989Sbostic b.psize = m.m_psize; 21651098Sbostic t->bt_flags |= m.m_flags; 21750989Sbostic t->bt_free = m.m_free; 21850989Sbostic t->bt_lorder = m.m_lorder; 21950989Sbostic t->bt_nrecs = m.m_nrecs; 22050989Sbostic } else { 22150989Sbostic /* 22250989Sbostic * Set the page size to the best value for I/O to this file. 22350989Sbostic * Don't overflow the page offset type. 22450989Sbostic */ 22550989Sbostic if (b.psize == 0) { 22650989Sbostic b.psize = sb.st_blksize; 22750989Sbostic if (b.psize < MINPSIZE) 22850989Sbostic b.psize = MINPSIZE; 22950989Sbostic if (b.psize > MAX_PAGE_OFFSET) 23050989Sbostic b.psize = MAX_PAGE_OFFSET; 23150989Sbostic } 23251098Sbostic t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS; 23350989Sbostic t->bt_free = P_INVALID; 23450989Sbostic t->bt_lorder = b.lorder; 23550989Sbostic t->bt_nrecs = 0; 23650989Sbostic SET(t, BTF_METADIRTY); 23745876Sbostic } 23845876Sbostic 23950989Sbostic t->bt_psize = b.psize; 24045876Sbostic 24150989Sbostic /* Set the cache size; must be a multiple of the page size. */ 24250989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 24350989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 24450989Sbostic if (b.cachesize < b.psize * MINCACHE) 24550989Sbostic b.cachesize = b.psize * MINCACHE; 24645876Sbostic 24750989Sbostic /* Calculate number of pages to cache. */ 24850989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 24945876Sbostic 25045876Sbostic /* 25151098Sbostic * The btree data structure requires that at least two keys can fit on 25251098Sbostic * a page, but other than that there's no fixed requirement. The user 25351098Sbostic * specified a minimum number per page, and we translated that into the 25451098Sbostic * number of bytes a key/data pair can use before being placed on an 25551098Sbostic * overflow page. This calculation includes the page header, the size 25651098Sbostic * of the index referencing the leaf item and the size of the leaf item 25751098Sbostic * structure. Also, don't let the user specify a minkeypage such that 25851098Sbostic * a key/data pair won't fit even if both key and data are on overflow 25951098Sbostic * pages. 26045876Sbostic */ 26151098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 26251098Sbostic (sizeof(index_t) + NBLEAFDBT(0, 0)); 26351098Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 26451098Sbostic t->bt_ovflsize = 26551098Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 26645876Sbostic 26750989Sbostic /* Initialize the buffer pool. */ 26850989Sbostic if ((t->bt_mp = 26950989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 27050989Sbostic goto err; 27151098Sbostic if (NOTSET(t, BTF_INMEM)) 27251098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 27345876Sbostic 27450989Sbostic /* Create a root page if new tree. */ 27550989Sbostic if (nroot(t) == RET_ERROR) 27650989Sbostic goto err; 27745876Sbostic 27850989Sbostic return (dbp); 27945876Sbostic 28050989Sbostic einval: errno = EINVAL; 28150989Sbostic goto err; 28245876Sbostic 28350989Sbostic eftype: errno = EFTYPE; 28450989Sbostic goto err; 28549322Sbostic 28650989Sbostic err: if (t) { 28750989Sbostic if (t->bt_dbp) 28850989Sbostic free(t->bt_dbp); 28950989Sbostic if (t->bt_fd != -1) 29050989Sbostic (void)close(t->bt_fd); 29150989Sbostic free(t); 29245876Sbostic } 29350989Sbostic return (NULL); 29445876Sbostic } 29545876Sbostic 29645876Sbostic /* 29750989Sbostic * NROOT -- Create the root of a new tree. 29845876Sbostic * 29950989Sbostic * Parameters: 30050989Sbostic * t: tree 30145876Sbostic * 30250989Sbostic * Returns: 30350989Sbostic * RET_ERROR, RET_SUCCESS 30445876Sbostic */ 30550989Sbostic static int 30650989Sbostic nroot(t) 30750989Sbostic BTREE *t; 30845876Sbostic { 30950989Sbostic PAGE *meta, *root; 31050989Sbostic pgno_t npg; 31145876Sbostic 31250989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 31350989Sbostic mpool_put(t->bt_mp, meta, 0); 31445876Sbostic return (RET_SUCCESS); 31545876Sbostic } 31650989Sbostic if (errno != EINVAL) 31750989Sbostic return (RET_ERROR); 31845876Sbostic 31950989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 32050989Sbostic return (RET_ERROR); 32145876Sbostic 32250989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 32350989Sbostic return (RET_ERROR); 32445876Sbostic 32550989Sbostic if (npg != P_ROOT) 32645876Sbostic return (RET_ERROR); 32750989Sbostic root->pgno = npg; 32850989Sbostic root->prevpg = root->nextpg = P_INVALID; 32950989Sbostic root->lower = BTDATAOFF; 33050989Sbostic root->upper = t->bt_psize; 33150989Sbostic root->flags = P_BLEAF; 33251098Sbostic bzero(meta, t->bt_psize); 33350989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 33450989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 33550989Sbostic return (RET_SUCCESS); 33645876Sbostic } 33745876Sbostic 33850989Sbostic static int 33950989Sbostic tmp() 34045876Sbostic { 34150989Sbostic sigset_t set, oset; 34250989Sbostic int fd; 34350989Sbostic char *envtmp; 34450989Sbostic char path[MAXPATHLEN]; 34545876Sbostic 34650989Sbostic envtmp = getenv("TMPDIR"); 34750989Sbostic (void)snprintf(path, 34850989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 34949322Sbostic 35055316Sbostic (void)sigfillset(&set); 35150989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 35250989Sbostic if ((fd = mkstemp(path)) != -1) 35350989Sbostic (void)unlink(path); 35450989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 35550989Sbostic return(fd); 35645876Sbostic } 357