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*51967Sbostic static char sccsid[] = "@(#)bt_open.c 5.16 (Berkeley) 12/16/91"; 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> 2550989Sbostic #include <fcntl.h> 2646561Sbostic #include <errno.h> 2750989Sbostic #include <limits.h> 2850989Sbostic #define __DBINTERFACE_PRIVATE 2945876Sbostic #include <db.h> 3050989Sbostic #include <stdio.h> 3150989Sbostic #include <unistd.h> 3246561Sbostic #include <stdlib.h> 3351098Sbostic #include <string.h> 3446127Smao #include "btree.h" 3545876Sbostic 3650989Sbostic static int nroot __P((BTREE *)); 3750989Sbostic static int tmp __P((void)); 3845876Sbostic 3945876Sbostic /* 4050989Sbostic * __BT_OPEN -- Open a btree. 4145876Sbostic * 4250989Sbostic * Creates and fills a DB struct, and calls the routine that actually 4350989Sbostic * opens the btree. 4445876Sbostic * 4550989Sbostic * Parameters: 4650989Sbostic * fname: filename (NULL for in-memory trees) 4750989Sbostic * flags: open flag bits 4850989Sbostic * mode: open permission bits 4950989Sbostic * b: BTREEINFO pointer 5045876Sbostic * 5150989Sbostic * Returns: 5250989Sbostic * NULL on failure, pointer to DB on success. 5345876Sbostic * 5445876Sbostic */ 5545876Sbostic DB * 5650989Sbostic __bt_open(fname, flags, mode, openinfo) 5750989Sbostic const char *fname; 5850989Sbostic int flags, mode; 5950989Sbostic const BTREEINFO *openinfo; 6045876Sbostic { 6151098Sbostic BTMETA m; 6250989Sbostic BTREE *t; 6351098Sbostic BTREEINFO b; 6450989Sbostic DB *dbp; 6550989Sbostic pgno_t ncache; 6650989Sbostic struct stat sb; 6750989Sbostic int nr; 6845876Sbostic 6945876Sbostic /* 7050989Sbostic * Intention is to make sure all of the user's selections are okay 7150989Sbostic * here and then use them without checking. Can't be complete, since 7250989Sbostic * we don't know the right page size, lorder or flags until the backing 7350989Sbostic * file is opened. Also, the file's page size can cause the cachesize 7450989Sbostic * to change. 7545876Sbostic */ 7650989Sbostic if (openinfo) { 7750989Sbostic b = *openinfo; 7845876Sbostic 7950989Sbostic /* Flags: R_DUP. */ 8051098Sbostic if (b.flags & ~(R_DUP)) 8150989Sbostic goto einval; 8245876Sbostic 8350989Sbostic /* 8450989Sbostic * Page size must be index_t aligned and >= MINPSIZE. Default 8550989Sbostic * page size is set farther on, based on the underlying file 8650989Sbostic * transfer size. 8750989Sbostic */ 8850989Sbostic if (b.psize && 8950989Sbostic (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET || 9050989Sbostic b.psize & sizeof(index_t) - 1)) 9151421Sbostic goto einval; 9245876Sbostic 9351098Sbostic /* Minimum number of keys per page; absolute minimum is 2. */ 9450989Sbostic if (b.minkeypage) { 9550989Sbostic if (b.minkeypage < 2) 9650989Sbostic goto einval; 9750989Sbostic } else 9851098Sbostic b.minkeypage = DEFMINKEYPAGE; 9951098Sbostic 10050989Sbostic /* If no comparison, use default comparison and prefix. */ 10150989Sbostic if (b.compare == NULL) { 10250989Sbostic b.compare = __bt_defcmp; 10350989Sbostic if (b.prefix == NULL) 10450989Sbostic b.prefix = __bt_defpfx; 10545876Sbostic } 10645876Sbostic 10750989Sbostic if (b.lorder == 0) 10850989Sbostic b.lorder = BYTE_ORDER; 10950989Sbostic else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) 11050989Sbostic goto einval; 11150989Sbostic } else { 11251421Sbostic b.compare = __bt_defcmp; 11350989Sbostic b.flags = 0; 11451421Sbostic b.lorder = BYTE_ORDER; 11550989Sbostic b.minkeypage = DEFMINKEYPAGE; 11650989Sbostic b.prefix = __bt_defpfx; 11751421Sbostic b.psize = 0; 11850989Sbostic } 11945876Sbostic 12050989Sbostic /* Allocate and initialize DB and BTREE structures. */ 12150989Sbostic if ((t = malloc(sizeof(BTREE))) == NULL) 12250989Sbostic goto err; 12350989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */ 12450989Sbostic if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 12550989Sbostic goto err; 12650989Sbostic t->bt_bcursor.pgno = P_INVALID; 12750989Sbostic t->bt_bcursor.index = 0; 12850989Sbostic t->bt_stack = NULL; 12950989Sbostic t->bt_sp = t->bt_maxstack = 0; 13050989Sbostic t->bt_kbuf = t->bt_dbuf = NULL; 13150989Sbostic t->bt_kbufsz = t->bt_dbufsz = 0; 13250989Sbostic t->bt_order = NOT; 13350989Sbostic t->bt_cmp = b.compare; 13450989Sbostic t->bt_pfx = b.prefix; 13551800Sbostic t->bt_flags = 0; 13646453Smao 13750989Sbostic dbp->type = DB_BTREE; 13850989Sbostic dbp->internal = t; 13950989Sbostic dbp->close = __bt_close; 14050989Sbostic dbp->del = __bt_delete; 14150989Sbostic dbp->get = __bt_get; 14250989Sbostic dbp->put = __bt_put; 14350989Sbostic dbp->seq = __bt_seq; 14450989Sbostic dbp->sync = __bt_sync; 14545876Sbostic 14650989Sbostic /* 14750989Sbostic * If no file name was supplied, this is an in-memory btree and we 14850989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 14950989Sbostic */ 15050989Sbostic if (fname) { 15150989Sbostic #define USEFLAGS (O_CREAT|O_EXCL|O_RDONLY|O_RDWR|O_TRUNC|O_WRONLY) 15250989Sbostic if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 15350989Sbostic goto err; 15450989Sbostic if ((flags & O_ACCMODE) == O_RDONLY) 15550989Sbostic SET(t, BTF_RDONLY); 15645876Sbostic 15750989Sbostic } else { 15850989Sbostic if ((t->bt_fd = tmp()) == -1) 15950989Sbostic goto err; 16050989Sbostic SET(t, BTF_INMEM); 16150989Sbostic } 16245876Sbostic 16350989Sbostic if (fcntl(t->bt_fd, F_SETFL, 1) == -1) 16450989Sbostic goto err; 16545876Sbostic 16650989Sbostic if (fstat(t->bt_fd, &sb)) 16750989Sbostic goto err; 16850989Sbostic if (sb.st_size) { 16950989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 17050989Sbostic if (nr < 0) 17150989Sbostic goto err; 17250989Sbostic if (nr != sizeof(BTMETA)) 17350989Sbostic goto eftype; 17445876Sbostic 17550989Sbostic /* 17650989Sbostic * Read in the meta-data. This can change the notion of what 17750989Sbostic * the lorder, page size and flags are, and, when the page size 17850989Sbostic * changes the cachesize value can change as well. 17950989Sbostic * 18050989Sbostic * Lorder is always stored in host-independent format. 18150989Sbostic */ 182*51967Sbostic m.m_lorder = ntohl(m.m_lorder); 18350989Sbostic if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 18450989Sbostic goto eftype; 18550989Sbostic if (m.m_lorder != BYTE_ORDER) { 18650989Sbostic BLSWAP(m.m_magic); 18750989Sbostic BLSWAP(m.m_version); 18850989Sbostic BLSWAP(m.m_psize); 18950989Sbostic BLSWAP(m.m_free); 19050989Sbostic BLSWAP(m.m_nrecs); 19150989Sbostic BLSWAP(m.m_flags); 19245876Sbostic } 19350989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 19450989Sbostic goto eftype; 19550989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 19650989Sbostic m.m_psize & sizeof(index_t) - 1) 19750989Sbostic goto eftype; 19851358Sbostic if (m.m_flags & ~SAVEMETA) 19950989Sbostic goto eftype; 20050989Sbostic b.psize = m.m_psize; 20151098Sbostic t->bt_flags |= m.m_flags; 20250989Sbostic t->bt_free = m.m_free; 20350989Sbostic t->bt_lorder = m.m_lorder; 20450989Sbostic t->bt_nrecs = m.m_nrecs; 20550989Sbostic } else { 20650989Sbostic /* 20750989Sbostic * Set the page size to the best value for I/O to this file. 20850989Sbostic * Don't overflow the page offset type. 20950989Sbostic */ 21050989Sbostic if (b.psize == 0) { 21150989Sbostic b.psize = sb.st_blksize; 21250989Sbostic if (b.psize < MINPSIZE) 21350989Sbostic b.psize = MINPSIZE; 21450989Sbostic if (b.psize > MAX_PAGE_OFFSET) 21550989Sbostic b.psize = MAX_PAGE_OFFSET; 21650989Sbostic } 21751098Sbostic t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS; 21850989Sbostic t->bt_free = P_INVALID; 21950989Sbostic t->bt_lorder = b.lorder; 22050989Sbostic t->bt_nrecs = 0; 22150989Sbostic SET(t, BTF_METADIRTY); 22245876Sbostic } 22345876Sbostic 22450989Sbostic t->bt_psize = b.psize; 22545876Sbostic 22650989Sbostic /* Set the cache size; must be a multiple of the page size. */ 22750989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 22850989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 22950989Sbostic if (b.cachesize < b.psize * MINCACHE) 23050989Sbostic b.cachesize = b.psize * MINCACHE; 23145876Sbostic 23250989Sbostic /* Calculate number of pages to cache. */ 23350989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 23445876Sbostic 23545876Sbostic /* 23651098Sbostic * The btree data structure requires that at least two keys can fit on 23751098Sbostic * a page, but other than that there's no fixed requirement. The user 23851098Sbostic * specified a minimum number per page, and we translated that into the 23951098Sbostic * number of bytes a key/data pair can use before being placed on an 24051098Sbostic * overflow page. This calculation includes the page header, the size 24151098Sbostic * of the index referencing the leaf item and the size of the leaf item 24251098Sbostic * structure. Also, don't let the user specify a minkeypage such that 24351098Sbostic * a key/data pair won't fit even if both key and data are on overflow 24451098Sbostic * pages. 24545876Sbostic */ 24651098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 24751098Sbostic (sizeof(index_t) + NBLEAFDBT(0, 0)); 24851098Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 24951098Sbostic t->bt_ovflsize = 25051098Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 25145876Sbostic 25250989Sbostic /* Initialize the buffer pool. */ 25350989Sbostic if ((t->bt_mp = 25450989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 25550989Sbostic goto err; 25651098Sbostic if (NOTSET(t, BTF_INMEM)) 25751098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 25845876Sbostic 25950989Sbostic /* Create a root page if new tree. */ 26050989Sbostic if (nroot(t) == RET_ERROR) 26150989Sbostic goto err; 26245876Sbostic 26350989Sbostic return (dbp); 26445876Sbostic 26550989Sbostic einval: errno = EINVAL; 26650989Sbostic goto err; 26745876Sbostic 26850989Sbostic eftype: errno = EFTYPE; 26950989Sbostic goto err; 27049322Sbostic 27150989Sbostic err: if (t) { 27250989Sbostic if (t->bt_dbp) 27350989Sbostic free(t->bt_dbp); 27450989Sbostic if (t->bt_fd != -1) 27550989Sbostic (void)close(t->bt_fd); 27650989Sbostic free(t); 27745876Sbostic } 27850989Sbostic return (NULL); 27945876Sbostic } 28045876Sbostic 28145876Sbostic /* 28250989Sbostic * NROOT -- Create the root of a new tree. 28345876Sbostic * 28450989Sbostic * Parameters: 28550989Sbostic * t: tree 28645876Sbostic * 28750989Sbostic * Returns: 28850989Sbostic * RET_ERROR, RET_SUCCESS 28945876Sbostic */ 29050989Sbostic static int 29150989Sbostic nroot(t) 29250989Sbostic BTREE *t; 29345876Sbostic { 29450989Sbostic PAGE *meta, *root; 29550989Sbostic pgno_t npg; 29645876Sbostic 29750989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 29850989Sbostic mpool_put(t->bt_mp, meta, 0); 29945876Sbostic return (RET_SUCCESS); 30045876Sbostic } 30150989Sbostic if (errno != EINVAL) 30250989Sbostic return (RET_ERROR); 30345876Sbostic 30450989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 30550989Sbostic return (RET_ERROR); 30645876Sbostic 30750989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 30850989Sbostic return (RET_ERROR); 30945876Sbostic 31050989Sbostic if (npg != P_ROOT) 31145876Sbostic return (RET_ERROR); 31250989Sbostic root->pgno = npg; 31350989Sbostic root->prevpg = root->nextpg = P_INVALID; 31450989Sbostic root->lower = BTDATAOFF; 31550989Sbostic root->upper = t->bt_psize; 31650989Sbostic root->flags = P_BLEAF; 31751098Sbostic bzero(meta, t->bt_psize); 31850989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 31950989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 32050989Sbostic return (RET_SUCCESS); 32145876Sbostic } 32245876Sbostic 32350989Sbostic static int 32450989Sbostic tmp() 32545876Sbostic { 32650989Sbostic sigset_t set, oset; 32750989Sbostic int fd; 32850989Sbostic char *envtmp; 32950989Sbostic char path[MAXPATHLEN]; 33045876Sbostic 33150989Sbostic envtmp = getenv("TMPDIR"); 33250989Sbostic (void)snprintf(path, 33350989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 33449322Sbostic 33550989Sbostic sigfillset(&set); 33650989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 33750989Sbostic if ((fd = mkstemp(path)) != -1) 33850989Sbostic (void)unlink(path); 33950989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 34050989Sbostic return(fd); 34145876Sbostic } 342