145876Sbostic /*- 261196Sbostic * Copyright (c) 1990, 1993 361196Sbostic * The Regents of the University of California. 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*64455Sbostic static char sccsid[] = "@(#)bt_open.c 8.2 (Berkeley) 09/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 * 60*64455Sbostic __bt_open(fname, flags, mode, openinfo, dflags) 6150989Sbostic const char *fname; 62*64455Sbostic int flags, mode, dflags; 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 && 9660230Sbostic (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 || 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) 14860040Sbostic SET(t, B_NEEDSWAP); 14946453Smao 15050989Sbostic dbp->type = DB_BTREE; 15150989Sbostic dbp->internal = t; 15250989Sbostic dbp->close = __bt_close; 15350989Sbostic dbp->del = __bt_delete; 15460260Sbostic dbp->fd = __bt_fd; 15550989Sbostic dbp->get = __bt_get; 15650989Sbostic dbp->put = __bt_put; 15750989Sbostic dbp->seq = __bt_seq; 15850989Sbostic dbp->sync = __bt_sync; 15945876Sbostic 16050989Sbostic /* 16150989Sbostic * If no file name was supplied, this is an in-memory btree and we 16250989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 16350989Sbostic */ 16450989Sbostic if (fname) { 16556421Sbostic switch(flags & O_ACCMODE) { 16656421Sbostic case O_RDONLY: 16760040Sbostic SET(t, B_RDONLY); 16856421Sbostic break; 16956421Sbostic case O_RDWR: 17056421Sbostic break; 17156421Sbostic case O_WRONLY: 17256421Sbostic default: 17356421Sbostic goto einval; 17456421Sbostic } 17556421Sbostic 176*64455Sbostic if ((t->bt_fd = open(fname, 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; 18460040Sbostic SET(t, B_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) 20860040Sbostic CLR(t, B_NEEDSWAP); 20959632Sbostic else { 21060040Sbostic SET(t, B_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; 22060230Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 || 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; 23860230Sbostic if (b.psize > MAX_PAGE_OFFSET + 1) 23960230Sbostic b.psize = MAX_PAGE_OFFSET + 1; 24050989Sbostic } 24159632Sbostic 24259632Sbostic /* Set flag if duplicates permitted. */ 24356732Sbostic if (!(b.flags & R_DUP)) 24460040Sbostic SET(t, B_NODUPS); 24559632Sbostic 24650989Sbostic t->bt_free = P_INVALID; 24750989Sbostic t->bt_nrecs = 0; 24860040Sbostic SET(t, B_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; 28360040Sbostic if (!ISSET(t, B_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 290*64455Sbostic /* Global flags. */ 291*64455Sbostic if (dflags & DB_LOCK) 292*64455Sbostic SET(t, B_DB_LOCK); 293*64455Sbostic if (dflags & DB_SHMEM) 294*64455Sbostic SET(t, B_DB_SHMEM); 295*64455Sbostic if (dflags & DB_TXN) 296*64455Sbostic SET(t, B_DB_TXN); 297*64455Sbostic 29850989Sbostic return (dbp); 29945876Sbostic 30050989Sbostic einval: errno = EINVAL; 30150989Sbostic goto err; 30245876Sbostic 30350989Sbostic eftype: errno = EFTYPE; 30450989Sbostic goto err; 30549322Sbostic 30650989Sbostic err: if (t) { 30750989Sbostic if (t->bt_dbp) 30850989Sbostic free(t->bt_dbp); 30950989Sbostic if (t->bt_fd != -1) 31050989Sbostic (void)close(t->bt_fd); 31150989Sbostic free(t); 31245876Sbostic } 31350989Sbostic return (NULL); 31445876Sbostic } 31545876Sbostic 31645876Sbostic /* 31750989Sbostic * NROOT -- Create the root of a new tree. 31845876Sbostic * 31950989Sbostic * Parameters: 32050989Sbostic * t: tree 32145876Sbostic * 32250989Sbostic * Returns: 32350989Sbostic * RET_ERROR, RET_SUCCESS 32445876Sbostic */ 32550989Sbostic static int 32650989Sbostic nroot(t) 32750989Sbostic BTREE *t; 32845876Sbostic { 32950989Sbostic PAGE *meta, *root; 33050989Sbostic pgno_t npg; 33145876Sbostic 33250989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 33350989Sbostic mpool_put(t->bt_mp, meta, 0); 33445876Sbostic return (RET_SUCCESS); 33545876Sbostic } 33650989Sbostic if (errno != EINVAL) 33750989Sbostic return (RET_ERROR); 33845876Sbostic 33950989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 34050989Sbostic return (RET_ERROR); 34145876Sbostic 34250989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 34350989Sbostic return (RET_ERROR); 34445876Sbostic 34550989Sbostic if (npg != P_ROOT) 34645876Sbostic return (RET_ERROR); 34750989Sbostic root->pgno = npg; 34850989Sbostic root->prevpg = root->nextpg = P_INVALID; 34950989Sbostic root->lower = BTDATAOFF; 35050989Sbostic root->upper = t->bt_psize; 35150989Sbostic root->flags = P_BLEAF; 35258017Sbostic memset(meta, 0, t->bt_psize); 35350989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 35450989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 35550989Sbostic return (RET_SUCCESS); 35645876Sbostic } 35745876Sbostic 35850989Sbostic static int 35950989Sbostic tmp() 36045876Sbostic { 36150989Sbostic sigset_t set, oset; 36250989Sbostic int fd; 36350989Sbostic char *envtmp; 36450989Sbostic char path[MAXPATHLEN]; 36545876Sbostic 36650989Sbostic envtmp = getenv("TMPDIR"); 36750989Sbostic (void)snprintf(path, 36850989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 36949322Sbostic 37055316Sbostic (void)sigfillset(&set); 37150989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 37250989Sbostic if ((fd = mkstemp(path)) != -1) 37350989Sbostic (void)unlink(path); 37450989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 37550989Sbostic return(fd); 37645876Sbostic } 37759632Sbostic 37859632Sbostic static int 37959632Sbostic byteorder() 38059632Sbostic { 38159632Sbostic u_long x; /* XXX: 32-bit assumption. */ 38259790Sbostic u_char *p; 38359632Sbostic 38459632Sbostic x = 0x01020304; 38559632Sbostic p = (u_char *)&x; 38659632Sbostic switch (*p) { 38759632Sbostic case 1: 38859632Sbostic return (BIG_ENDIAN); 38959632Sbostic case 4: 39059632Sbostic return (LITTLE_ENDIAN); 39159632Sbostic default: 39259632Sbostic return (0); 39359632Sbostic } 39459632Sbostic } 39560260Sbostic 39660260Sbostic int 39760260Sbostic __bt_fd(dbp) 39860260Sbostic const DB *dbp; 39960260Sbostic { 40060260Sbostic BTREE *t; 40160260Sbostic 40260260Sbostic t = dbp->internal; 40360260Sbostic 404*64455Sbostic /* Toss any page pinned across calls. */ 405*64455Sbostic if (t->bt_pinned != NULL) { 406*64455Sbostic mpool_put(t->bt_mp, t->bt_pinned, 0); 407*64455Sbostic t->bt_pinned = NULL; 408*64455Sbostic } 409*64455Sbostic 410*64455Sbostic /* In-memory database can't have a file descriptor. */ 41160260Sbostic if (ISSET(t, B_INMEM)) { 41260260Sbostic errno = ENOENT; 41360260Sbostic return (-1); 41460260Sbostic } 41560260Sbostic return (t->bt_fd); 41660260Sbostic } 417