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*59790Sbostic static char sccsid[] = "@(#)bt_open.c 5.28 (Berkeley) 05/07/93"; 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 #define __DBINTERFACE_PRIVATE 3657932Sbostic #include <db.h> 3746127Smao #include "btree.h" 3845876Sbostic 3959632Sbostic static int byteorder __P((void)); 4050989Sbostic static int nroot __P((BTREE *)); 4150989Sbostic static int tmp __P((void)); 4245876Sbostic 4345876Sbostic /* 4450989Sbostic * __BT_OPEN -- Open a btree. 4545876Sbostic * 4650989Sbostic * Creates and fills a DB struct, and calls the routine that actually 4750989Sbostic * opens the btree. 4845876Sbostic * 4950989Sbostic * Parameters: 5050989Sbostic * fname: filename (NULL for in-memory trees) 5150989Sbostic * flags: open flag bits 5250989Sbostic * mode: open permission bits 5350989Sbostic * b: BTREEINFO pointer 5445876Sbostic * 5550989Sbostic * Returns: 5650989Sbostic * NULL on failure, pointer to DB on success. 5745876Sbostic * 5845876Sbostic */ 5945876Sbostic DB * 6050989Sbostic __bt_open(fname, flags, mode, openinfo) 6150989Sbostic const char *fname; 6250989Sbostic int flags, mode; 6350989Sbostic const BTREEINFO *openinfo; 6445876Sbostic { 6551098Sbostic BTMETA m; 6650989Sbostic BTREE *t; 6751098Sbostic BTREEINFO b; 6850989Sbostic DB *dbp; 6950989Sbostic pgno_t ncache; 7050989Sbostic struct stat sb; 7159632Sbostic int machine_lorder, nr; 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 && 9650989Sbostic (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET || 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. */ 13150989Sbostic if ((t = malloc(sizeof(BTREE))) == NULL) 13250989Sbostic goto err; 13350989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */ 13450989Sbostic if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 13550989Sbostic goto err; 13650989Sbostic t->bt_bcursor.pgno = P_INVALID; 13750989Sbostic t->bt_bcursor.index = 0; 13850989Sbostic t->bt_stack = NULL; 13950989Sbostic t->bt_sp = t->bt_maxstack = 0; 14050989Sbostic t->bt_kbuf = t->bt_dbuf = NULL; 14150989Sbostic t->bt_kbufsz = t->bt_dbufsz = 0; 14259632Sbostic t->bt_lorder = b.lorder; 14350989Sbostic t->bt_order = NOT; 14450989Sbostic t->bt_cmp = b.compare; 14550989Sbostic t->bt_pfx = b.prefix; 14651800Sbostic t->bt_flags = 0; 14759632Sbostic if (t->bt_lorder != machine_lorder) 14859632Sbostic SET(t, BTF_NEEDSWAP); 14946453Smao 15050989Sbostic dbp->type = DB_BTREE; 15150989Sbostic dbp->internal = t; 15250989Sbostic dbp->close = __bt_close; 15350989Sbostic dbp->del = __bt_delete; 15450989Sbostic dbp->get = __bt_get; 15550989Sbostic dbp->put = __bt_put; 15650989Sbostic dbp->seq = __bt_seq; 15750989Sbostic dbp->sync = __bt_sync; 15845876Sbostic 15950989Sbostic /* 16050989Sbostic * If no file name was supplied, this is an in-memory btree and we 16150989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 16250989Sbostic */ 16350989Sbostic if (fname) { 16456421Sbostic switch(flags & O_ACCMODE) { 16556421Sbostic case O_RDONLY: 16656421Sbostic SET(t, BTF_RDONLY); 16756421Sbostic break; 16856421Sbostic case O_RDWR: 16956421Sbostic break; 17056421Sbostic case O_WRONLY: 17156421Sbostic default: 17256421Sbostic goto einval; 17356421Sbostic } 17456421Sbostic 17556894Sbostic if ((t->bt_fd = 17656894Sbostic open(fname, flags & __USE_OPEN_FLAGS, mode)) < 0) 17750989Sbostic goto err; 17845876Sbostic 17950989Sbostic } else { 18056421Sbostic if ((flags & O_ACCMODE) != O_RDWR) 18156421Sbostic goto einval; 18250989Sbostic if ((t->bt_fd = tmp()) == -1) 18350989Sbostic goto err; 18450989Sbostic SET(t, BTF_INMEM); 18550989Sbostic } 18645876Sbostic 18756094Sbostic if (fcntl(t->bt_fd, F_SETFD, 1) == -1) 18850989Sbostic goto err; 18945876Sbostic 19050989Sbostic if (fstat(t->bt_fd, &sb)) 19150989Sbostic goto err; 19250989Sbostic if (sb.st_size) { 19350989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 19450989Sbostic if (nr < 0) 19550989Sbostic goto err; 19650989Sbostic if (nr != sizeof(BTMETA)) 19750989Sbostic goto eftype; 19845876Sbostic 19950989Sbostic /* 20050989Sbostic * Read in the meta-data. This can change the notion of what 20150989Sbostic * the lorder, page size and flags are, and, when the page size 20259632Sbostic * changes, the cachesize value can change too. If the user 20359632Sbostic * specified the wrong byte order for an existing database, we 20459632Sbostic * don't bother to return an error, we just clear the NEEDSWAP 20559632Sbostic * bit. 20650989Sbostic */ 20759632Sbostic if (m.m_magic == BTREEMAGIC) 20859632Sbostic CLR(t, BTF_NEEDSWAP); 20959632Sbostic else { 21059632Sbostic SET(t, BTF_NEEDSWAP); 21150989Sbostic BLSWAP(m.m_magic); 21250989Sbostic BLSWAP(m.m_version); 21350989Sbostic BLSWAP(m.m_psize); 21450989Sbostic BLSWAP(m.m_free); 21550989Sbostic BLSWAP(m.m_nrecs); 21650989Sbostic BLSWAP(m.m_flags); 21745876Sbostic } 21850989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 21950989Sbostic goto eftype; 22050989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 22157989Sbostic m.m_psize & sizeof(indx_t) - 1) 22250989Sbostic goto eftype; 22351358Sbostic if (m.m_flags & ~SAVEMETA) 22450989Sbostic goto eftype; 22550989Sbostic b.psize = m.m_psize; 22651098Sbostic t->bt_flags |= m.m_flags; 22750989Sbostic t->bt_free = m.m_free; 22850989Sbostic t->bt_nrecs = m.m_nrecs; 22950989Sbostic } else { 23050989Sbostic /* 23150989Sbostic * Set the page size to the best value for I/O to this file. 23250989Sbostic * Don't overflow the page offset type. 23350989Sbostic */ 23450989Sbostic if (b.psize == 0) { 23550989Sbostic b.psize = sb.st_blksize; 23650989Sbostic if (b.psize < MINPSIZE) 23750989Sbostic b.psize = MINPSIZE; 23850989Sbostic if (b.psize > MAX_PAGE_OFFSET) 23950989Sbostic b.psize = MAX_PAGE_OFFSET; 24050989Sbostic } 24159632Sbostic 24259632Sbostic /* Set flag if duplicates permitted. */ 24356732Sbostic if (!(b.flags & R_DUP)) 24456732Sbostic SET(t, BTF_NODUPS); 24559632Sbostic 24650989Sbostic t->bt_free = P_INVALID; 24750989Sbostic t->bt_nrecs = 0; 24850989Sbostic SET(t, BTF_METADIRTY); 24945876Sbostic } 25045876Sbostic 25150989Sbostic t->bt_psize = b.psize; 25245876Sbostic 25350989Sbostic /* Set the cache size; must be a multiple of the page size. */ 25450989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 25550989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 25650989Sbostic if (b.cachesize < b.psize * MINCACHE) 25750989Sbostic b.cachesize = b.psize * MINCACHE; 25845876Sbostic 25950989Sbostic /* Calculate number of pages to cache. */ 26050989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 26145876Sbostic 26245876Sbostic /* 26351098Sbostic * The btree data structure requires that at least two keys can fit on 26451098Sbostic * a page, but other than that there's no fixed requirement. The user 26551098Sbostic * specified a minimum number per page, and we translated that into the 26651098Sbostic * number of bytes a key/data pair can use before being placed on an 26751098Sbostic * overflow page. This calculation includes the page header, the size 26851098Sbostic * of the index referencing the leaf item and the size of the leaf item 26951098Sbostic * structure. Also, don't let the user specify a minkeypage such that 27051098Sbostic * a key/data pair won't fit even if both key and data are on overflow 27151098Sbostic * pages. 27245876Sbostic */ 27351098Sbostic t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 27457989Sbostic (sizeof(indx_t) + NBLEAFDBT(0, 0)); 27557989Sbostic if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) 27651098Sbostic t->bt_ovflsize = 27757989Sbostic NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t); 27845876Sbostic 27950989Sbostic /* Initialize the buffer pool. */ 28050989Sbostic if ((t->bt_mp = 28150989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 28250989Sbostic goto err; 28356732Sbostic if (!ISSET(t, BTF_INMEM)) 28451098Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 28545876Sbostic 28650989Sbostic /* Create a root page if new tree. */ 28750989Sbostic if (nroot(t) == RET_ERROR) 28850989Sbostic goto err; 28945876Sbostic 29050989Sbostic return (dbp); 29145876Sbostic 29250989Sbostic einval: errno = EINVAL; 29350989Sbostic goto err; 29445876Sbostic 29550989Sbostic eftype: errno = EFTYPE; 29650989Sbostic goto err; 29749322Sbostic 29850989Sbostic err: if (t) { 29950989Sbostic if (t->bt_dbp) 30050989Sbostic free(t->bt_dbp); 30150989Sbostic if (t->bt_fd != -1) 30250989Sbostic (void)close(t->bt_fd); 30350989Sbostic free(t); 30445876Sbostic } 30550989Sbostic return (NULL); 30645876Sbostic } 30745876Sbostic 30845876Sbostic /* 30950989Sbostic * NROOT -- Create the root of a new tree. 31045876Sbostic * 31150989Sbostic * Parameters: 31250989Sbostic * t: tree 31345876Sbostic * 31450989Sbostic * Returns: 31550989Sbostic * RET_ERROR, RET_SUCCESS 31645876Sbostic */ 31750989Sbostic static int 31850989Sbostic nroot(t) 31950989Sbostic BTREE *t; 32045876Sbostic { 32150989Sbostic PAGE *meta, *root; 32250989Sbostic pgno_t npg; 32345876Sbostic 32450989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 32550989Sbostic mpool_put(t->bt_mp, meta, 0); 32645876Sbostic return (RET_SUCCESS); 32745876Sbostic } 32850989Sbostic if (errno != EINVAL) 32950989Sbostic return (RET_ERROR); 33045876Sbostic 33150989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 33250989Sbostic return (RET_ERROR); 33345876Sbostic 33450989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 33550989Sbostic return (RET_ERROR); 33645876Sbostic 33750989Sbostic if (npg != P_ROOT) 33845876Sbostic return (RET_ERROR); 33950989Sbostic root->pgno = npg; 34050989Sbostic root->prevpg = root->nextpg = P_INVALID; 34150989Sbostic root->lower = BTDATAOFF; 34250989Sbostic root->upper = t->bt_psize; 34350989Sbostic root->flags = P_BLEAF; 34458017Sbostic memset(meta, 0, t->bt_psize); 34550989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 34650989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 34750989Sbostic return (RET_SUCCESS); 34845876Sbostic } 34945876Sbostic 35050989Sbostic static int 35150989Sbostic tmp() 35245876Sbostic { 35350989Sbostic sigset_t set, oset; 35450989Sbostic int fd; 35550989Sbostic char *envtmp; 35650989Sbostic char path[MAXPATHLEN]; 35745876Sbostic 35850989Sbostic envtmp = getenv("TMPDIR"); 35950989Sbostic (void)snprintf(path, 36050989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 36149322Sbostic 36255316Sbostic (void)sigfillset(&set); 36350989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 36450989Sbostic if ((fd = mkstemp(path)) != -1) 36550989Sbostic (void)unlink(path); 36650989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 36750989Sbostic return(fd); 36845876Sbostic } 36959632Sbostic 37059632Sbostic static int 37159632Sbostic byteorder() 37259632Sbostic { 37359632Sbostic u_long x; /* XXX: 32-bit assumption. */ 374*59790Sbostic u_char *p; 37559632Sbostic 37659632Sbostic x = 0x01020304; 37759632Sbostic p = (u_char *)&x; 37859632Sbostic switch (*p) { 37959632Sbostic case 1: 38059632Sbostic return (BIG_ENDIAN); 38159632Sbostic case 4: 38259632Sbostic return (LITTLE_ENDIAN); 38359632Sbostic default: 38459632Sbostic return (0); 38559632Sbostic } 38659632Sbostic } 387