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*55316Sbostic static char sccsid[] = "@(#)bt_open.c 5.18 (Berkeley) 07/17/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> 25*55316Sbostic 26*55316Sbostic #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; 11550989Sbostic b.flags = 0; 11651421Sbostic b.lorder = BYTE_ORDER; 11750989Sbostic b.minkeypage = DEFMINKEYPAGE; 11850989Sbostic b.prefix = __bt_defpfx; 11951421Sbostic b.psize = 0; 12050989Sbostic } 12145876Sbostic 12250989Sbostic /* Allocate and initialize DB and BTREE structures. */ 12350989Sbostic if ((t = malloc(sizeof(BTREE))) == NULL) 12450989Sbostic goto err; 12550989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */ 12650989Sbostic if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 12750989Sbostic goto err; 12850989Sbostic t->bt_bcursor.pgno = P_INVALID; 12950989Sbostic t->bt_bcursor.index = 0; 13050989Sbostic t->bt_stack = NULL; 13150989Sbostic t->bt_sp = t->bt_maxstack = 0; 13250989Sbostic t->bt_kbuf = t->bt_dbuf = NULL; 13350989Sbostic t->bt_kbufsz = t->bt_dbufsz = 0; 13450989Sbostic t->bt_order = NOT; 13550989Sbostic t->bt_cmp = b.compare; 13650989Sbostic t->bt_pfx = b.prefix; 13751800Sbostic t->bt_flags = 0; 13846453Smao 13950989Sbostic dbp->type = DB_BTREE; 14050989Sbostic dbp->internal = t; 14150989Sbostic dbp->close = __bt_close; 14250989Sbostic dbp->del = __bt_delete; 14350989Sbostic dbp->get = __bt_get; 14450989Sbostic dbp->put = __bt_put; 14550989Sbostic dbp->seq = __bt_seq; 14650989Sbostic dbp->sync = __bt_sync; 14745876Sbostic 14850989Sbostic /* 14950989Sbostic * If no file name was supplied, this is an in-memory btree and we 15050989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 15150989Sbostic */ 15250989Sbostic if (fname) { 15353605Sbostic #define USEFLAGS \ 15453605Sbostic (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC|O_WRONLY) 15550989Sbostic if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 15650989Sbostic goto err; 15750989Sbostic if ((flags & O_ACCMODE) == O_RDONLY) 15850989Sbostic SET(t, BTF_RDONLY); 15945876Sbostic 16050989Sbostic } else { 16150989Sbostic if ((t->bt_fd = tmp()) == -1) 16250989Sbostic goto err; 16350989Sbostic SET(t, BTF_INMEM); 16450989Sbostic } 16545876Sbostic 16650989Sbostic if (fcntl(t->bt_fd, F_SETFL, 1) == -1) 16750989Sbostic goto err; 16845876Sbostic 16950989Sbostic if (fstat(t->bt_fd, &sb)) 17050989Sbostic goto err; 17150989Sbostic if (sb.st_size) { 17250989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 17350989Sbostic if (nr < 0) 17450989Sbostic goto err; 17550989Sbostic if (nr != sizeof(BTMETA)) 17650989Sbostic goto eftype; 17745876Sbostic 17850989Sbostic /* 17950989Sbostic * Read in the meta-data. This can change the notion of what 18050989Sbostic * the lorder, page size and flags are, and, when the page size 18150989Sbostic * changes the cachesize value can change as well. 18250989Sbostic * 18350989Sbostic * Lorder is always stored in host-independent format. 18450989Sbostic */ 18551967Sbostic m.m_lorder = ntohl(m.m_lorder); 18650989Sbostic if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 18750989Sbostic goto eftype; 18850989Sbostic if (m.m_lorder != BYTE_ORDER) { 18950989Sbostic BLSWAP(m.m_magic); 19050989Sbostic BLSWAP(m.m_version); 19150989Sbostic BLSWAP(m.m_psize); 19250989Sbostic BLSWAP(m.m_free); 19350989Sbostic BLSWAP(m.m_nrecs); 19450989Sbostic BLSWAP(m.m_flags); 19545876Sbostic } 19650989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 19750989Sbostic goto eftype; 19850989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 19950989Sbostic m.m_psize & sizeof(index_t) - 1) 20050989Sbostic goto eftype; 20151358Sbostic if (m.m_flags & ~SAVEMETA) 20250989Sbostic goto eftype; 20350989Sbostic b.psize = m.m_psize; 20451098Sbostic t->bt_flags |= m.m_flags; 20550989Sbostic t->bt_free = m.m_free; 20650989Sbostic t->bt_lorder = m.m_lorder; 20750989Sbostic t->bt_nrecs = m.m_nrecs; 20850989Sbostic } else { 20950989Sbostic /* 21050989Sbostic * Set the page size to the best value for I/O to this file. 21150989Sbostic * Don't overflow the page offset type. 21250989Sbostic */ 21350989Sbostic if (b.psize == 0) { 21450989Sbostic b.psize = sb.st_blksize; 21550989Sbostic if (b.psize < MINPSIZE) 21650989Sbostic b.psize = MINPSIZE; 21750989Sbostic if (b.psize > MAX_PAGE_OFFSET) 21850989Sbostic b.psize = MAX_PAGE_OFFSET; 21950989Sbostic } 22051098Sbostic t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS; 22150989Sbostic t->bt_free = P_INVALID; 22250989Sbostic t->bt_lorder = b.lorder; 22350989Sbostic t->bt_nrecs = 0; 22450989Sbostic SET(t, BTF_METADIRTY); 22545876Sbostic } 22645876Sbostic 22750989Sbostic t->bt_psize = b.psize; 22845876Sbostic 22950989Sbostic /* Set the cache size; must be a multiple of the page size. */ 23050989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 23150989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 23250989Sbostic if (b.cachesize < b.psize * MINCACHE) 23350989Sbostic b.cachesize = b.psize * MINCACHE; 23445876Sbostic 23550989Sbostic /* Calculate number of pages to cache. */ 23650989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 23745876Sbostic 23845876Sbostic /* 23951098Sbostic * The btree data structure requires that at least two keys can fit on 24051098Sbostic * a page, but other than that there's no fixed requirement. The user 24151098Sbostic * specified a minimum number per page, and we translated that into the 24251098Sbostic * number of bytes a key/data pair can use before being placed on an 24351098Sbostic * overflow page. This calculation includes the page header, the size 24451098Sbostic * of the index referencing the leaf item and the size of the leaf item 24551098Sbostic * structure. Also, don't let the user specify a minkeypage such that 24651098Sbostic * a key/data pair won't fit even if both key and data are on overflow 24751098Sbostic * pages. 24845876Sbostic */ 24951098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 25051098Sbostic (sizeof(index_t) + NBLEAFDBT(0, 0)); 25151098Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 25251098Sbostic t->bt_ovflsize = 25351098Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 25445876Sbostic 25550989Sbostic /* Initialize the buffer pool. */ 25650989Sbostic if ((t->bt_mp = 25750989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 25850989Sbostic goto err; 25951098Sbostic if (NOTSET(t, BTF_INMEM)) 26051098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 26145876Sbostic 26250989Sbostic /* Create a root page if new tree. */ 26350989Sbostic if (nroot(t) == RET_ERROR) 26450989Sbostic goto err; 26545876Sbostic 26650989Sbostic return (dbp); 26745876Sbostic 26850989Sbostic einval: errno = EINVAL; 26950989Sbostic goto err; 27045876Sbostic 27150989Sbostic eftype: errno = EFTYPE; 27250989Sbostic goto err; 27349322Sbostic 27450989Sbostic err: if (t) { 27550989Sbostic if (t->bt_dbp) 27650989Sbostic free(t->bt_dbp); 27750989Sbostic if (t->bt_fd != -1) 27850989Sbostic (void)close(t->bt_fd); 27950989Sbostic free(t); 28045876Sbostic } 28150989Sbostic return (NULL); 28245876Sbostic } 28345876Sbostic 28445876Sbostic /* 28550989Sbostic * NROOT -- Create the root of a new tree. 28645876Sbostic * 28750989Sbostic * Parameters: 28850989Sbostic * t: tree 28945876Sbostic * 29050989Sbostic * Returns: 29150989Sbostic * RET_ERROR, RET_SUCCESS 29245876Sbostic */ 29350989Sbostic static int 29450989Sbostic nroot(t) 29550989Sbostic BTREE *t; 29645876Sbostic { 29750989Sbostic PAGE *meta, *root; 29850989Sbostic pgno_t npg; 29945876Sbostic 30050989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 30150989Sbostic mpool_put(t->bt_mp, meta, 0); 30245876Sbostic return (RET_SUCCESS); 30345876Sbostic } 30450989Sbostic if (errno != EINVAL) 30550989Sbostic return (RET_ERROR); 30645876Sbostic 30750989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 30850989Sbostic return (RET_ERROR); 30945876Sbostic 31050989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 31150989Sbostic return (RET_ERROR); 31245876Sbostic 31350989Sbostic if (npg != P_ROOT) 31445876Sbostic return (RET_ERROR); 31550989Sbostic root->pgno = npg; 31650989Sbostic root->prevpg = root->nextpg = P_INVALID; 31750989Sbostic root->lower = BTDATAOFF; 31850989Sbostic root->upper = t->bt_psize; 31950989Sbostic root->flags = P_BLEAF; 32051098Sbostic bzero(meta, t->bt_psize); 32150989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 32250989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 32350989Sbostic return (RET_SUCCESS); 32445876Sbostic } 32545876Sbostic 32650989Sbostic static int 32750989Sbostic tmp() 32845876Sbostic { 32950989Sbostic sigset_t set, oset; 33050989Sbostic int fd; 33150989Sbostic char *envtmp; 33250989Sbostic char path[MAXPATHLEN]; 33345876Sbostic 33450989Sbostic envtmp = getenv("TMPDIR"); 33550989Sbostic (void)snprintf(path, 33650989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 33749322Sbostic 338*55316Sbostic (void)sigfillset(&set); 33950989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 34050989Sbostic if ((fd = mkstemp(path)) != -1) 34150989Sbostic (void)unlink(path); 34250989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 34350989Sbostic return(fd); 34445876Sbostic } 345