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*50989Sbostic static char sccsid[] = "@(#)bt_open.c 5.10 (Berkeley) 09/04/91"; 1345876Sbostic #endif /* LIBC_SCCS and not lint */ 1445876Sbostic 1545876Sbostic /* 16*50989Sbostic * Implementation of btree access method for 4.4BSD. 1745876Sbostic * 18*50989Sbostic * The design here was originally based on that of the btree access method 19*50989Sbostic * used in the Postgres database system at UC Berkeley. This implementation 20*50989Sbostic * is wholly independent of the Postgres code. 2145876Sbostic */ 2245876Sbostic 2345876Sbostic #include <sys/param.h> 2445876Sbostic #include <sys/stat.h> 25*50989Sbostic #include <fcntl.h> 2646561Sbostic #include <errno.h> 27*50989Sbostic #include <limits.h> 28*50989Sbostic #define __DBINTERFACE_PRIVATE 2945876Sbostic #include <db.h> 30*50989Sbostic #include <stdio.h> 31*50989Sbostic #include <unistd.h> 3246561Sbostic #include <stdlib.h> 3346127Smao #include "btree.h" 3445876Sbostic 35*50989Sbostic static int nroot __P((BTREE *)); 36*50989Sbostic static int tmp __P((void)); 3745876Sbostic 3845876Sbostic /* 39*50989Sbostic * __BT_OPEN -- Open a btree. 4045876Sbostic * 41*50989Sbostic * Creates and fills a DB struct, and calls the routine that actually 42*50989Sbostic * opens the btree. 4345876Sbostic * 44*50989Sbostic * Parameters: 45*50989Sbostic * fname: filename (NULL for in-memory trees) 46*50989Sbostic * flags: open flag bits 47*50989Sbostic * mode: open permission bits 48*50989Sbostic * b: BTREEINFO pointer 4945876Sbostic * 50*50989Sbostic * Returns: 51*50989Sbostic * NULL on failure, pointer to DB on success. 5245876Sbostic * 5345876Sbostic */ 5445876Sbostic DB * 55*50989Sbostic __bt_open(fname, flags, mode, openinfo) 56*50989Sbostic const char *fname; 57*50989Sbostic int flags, mode; 58*50989Sbostic const BTREEINFO *openinfo; 5945876Sbostic { 60*50989Sbostic BTREE *t; 61*50989Sbostic DB *dbp; 6245876Sbostic BTMETA m; 63*50989Sbostic BTREEINFO b; 64*50989Sbostic pgno_t ncache; 65*50989Sbostic struct stat sb; 66*50989Sbostic int nr; 6745876Sbostic 6845876Sbostic /* 69*50989Sbostic * Intention is to make sure all of the user's selections are okay 70*50989Sbostic * here and then use them without checking. Can't be complete, since 71*50989Sbostic * we don't know the right page size, lorder or flags until the backing 72*50989Sbostic * file is opened. Also, the file's page size can cause the cachesize 73*50989Sbostic * to change. 7445876Sbostic */ 75*50989Sbostic if (openinfo) { 76*50989Sbostic b = *openinfo; 7745876Sbostic 78*50989Sbostic /* Flags: R_DUP. */ 79*50989Sbostic if (b.flags && b.flags != R_DUP) 80*50989Sbostic goto einval; 8145876Sbostic 82*50989Sbostic /* 83*50989Sbostic * Page size must be index_t aligned and >= MINPSIZE. Default 84*50989Sbostic * page size is set farther on, based on the underlying file 85*50989Sbostic * transfer size. 86*50989Sbostic */ 87*50989Sbostic if (b.psize && 88*50989Sbostic (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET || 89*50989Sbostic b.psize & sizeof(index_t) - 1)) 90*50989Sbostic goto einval; 91*50989Sbostic #ifdef notdef 92*50989Sbostic if (b.maxkeypage && b.maxkeypage < 1) 93*50989Sbostic goto einval; 9445876Sbostic 95*50989Sbostic if (b.minkeypage) { 96*50989Sbostic if (b.minkeypage < 2) 97*50989Sbostic goto einval; 98*50989Sbostic } else 99*50989Sbostic b.minkeypage = 2; 100*50989Sbostic #else 101*50989Sbostic b.maxkeypage = DEFMAXKEYPAGE; 102*50989Sbostic b.minkeypage = DEFMINKEYPAGE; 10345876Sbostic #endif 104*50989Sbostic 105*50989Sbostic /* If no comparison, use default comparison and prefix. */ 106*50989Sbostic if (b.compare == NULL) { 107*50989Sbostic b.compare = __bt_defcmp; 108*50989Sbostic if (b.prefix == NULL) 109*50989Sbostic b.prefix = __bt_defpfx; 11045876Sbostic } 11145876Sbostic 112*50989Sbostic if (b.lorder == 0) 113*50989Sbostic b.lorder = BYTE_ORDER; 114*50989Sbostic else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) 115*50989Sbostic goto einval; 116*50989Sbostic } else { 117*50989Sbostic b.flags = 0; 118*50989Sbostic b.maxkeypage = DEFMAXKEYPAGE; 119*50989Sbostic b.minkeypage = DEFMINKEYPAGE; 120*50989Sbostic b.compare = __bt_defcmp; 121*50989Sbostic b.prefix = __bt_defpfx; 122*50989Sbostic b.lorder = BYTE_ORDER; 123*50989Sbostic } 12445876Sbostic 125*50989Sbostic /* Allocate and initialize DB and BTREE structures. */ 126*50989Sbostic if ((t = malloc(sizeof(BTREE))) == NULL) 127*50989Sbostic goto err; 128*50989Sbostic t->bt_fd = -1; /* Don't close unopened fd on error. */ 129*50989Sbostic if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 130*50989Sbostic goto err; 131*50989Sbostic t->bt_bcursor.pgno = P_INVALID; 132*50989Sbostic t->bt_bcursor.index = 0; 133*50989Sbostic t->bt_stack = NULL; 134*50989Sbostic t->bt_sp = t->bt_maxstack = 0; 135*50989Sbostic t->bt_kbuf = t->bt_dbuf = NULL; 136*50989Sbostic t->bt_kbufsz = t->bt_dbufsz = 0; 137*50989Sbostic t->bt_maxkeypage = b.maxkeypage; 138*50989Sbostic t->bt_minkeypage = b.minkeypage; 139*50989Sbostic t->bt_order = NOT; 140*50989Sbostic t->bt_cmp = b.compare; 141*50989Sbostic t->bt_pfx = b.prefix; 14246453Smao 143*50989Sbostic dbp->type = DB_BTREE; 144*50989Sbostic dbp->internal = t; 145*50989Sbostic dbp->close = __bt_close; 146*50989Sbostic dbp->del = __bt_delete; 147*50989Sbostic dbp->get = __bt_get; 148*50989Sbostic dbp->put = __bt_put; 149*50989Sbostic dbp->seq = __bt_seq; 150*50989Sbostic dbp->sync = __bt_sync; 15145876Sbostic 152*50989Sbostic /* 153*50989Sbostic * If no file name was supplied, this is an in-memory btree and we 154*50989Sbostic * open a backing temporary file. Otherwise, it's a disk-based tree. 155*50989Sbostic */ 156*50989Sbostic if (fname) { 157*50989Sbostic #define USEFLAGS (O_CREAT|O_EXCL|O_RDONLY|O_RDWR|O_TRUNC|O_WRONLY) 158*50989Sbostic if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 159*50989Sbostic goto err; 160*50989Sbostic if ((flags & O_ACCMODE) == O_RDONLY) 161*50989Sbostic SET(t, BTF_RDONLY); 16245876Sbostic 163*50989Sbostic } else { 164*50989Sbostic if ((t->bt_fd = tmp()) == -1) 165*50989Sbostic goto err; 166*50989Sbostic SET(t, BTF_INMEM); 167*50989Sbostic } 16845876Sbostic 169*50989Sbostic if (fcntl(t->bt_fd, F_SETFL, 1) == -1) 170*50989Sbostic goto err; 17145876Sbostic 172*50989Sbostic if (fstat(t->bt_fd, &sb)) 173*50989Sbostic goto err; 174*50989Sbostic if (sb.st_size) { 175*50989Sbostic nr = read(t->bt_fd, &m, sizeof(BTMETA)); 176*50989Sbostic if (nr < 0) 177*50989Sbostic goto err; 178*50989Sbostic if (nr != sizeof(BTMETA)) 179*50989Sbostic goto eftype; 18045876Sbostic 181*50989Sbostic /* 182*50989Sbostic * Read in the meta-data. This can change the notion of what 183*50989Sbostic * the lorder, page size and flags are, and, when the page size 184*50989Sbostic * changes the cachesize value can change as well. 185*50989Sbostic * 186*50989Sbostic * Lorder is always stored in host-independent format. 187*50989Sbostic */ 188*50989Sbostic NTOHL(m.m_lorder); 189*50989Sbostic if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 190*50989Sbostic goto eftype; 191*50989Sbostic if (m.m_lorder != BYTE_ORDER) { 192*50989Sbostic BLSWAP(m.m_magic); 193*50989Sbostic BLSWAP(m.m_version); 194*50989Sbostic BLSWAP(m.m_psize); 195*50989Sbostic BLSWAP(m.m_free); 196*50989Sbostic BLSWAP(m.m_nrecs); 197*50989Sbostic BLSWAP(m.m_flags); 19845876Sbostic } 199*50989Sbostic if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 200*50989Sbostic goto eftype; 201*50989Sbostic if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 202*50989Sbostic m.m_psize & sizeof(index_t) - 1) 203*50989Sbostic goto eftype; 204*50989Sbostic if (m.m_flags | ~SAVEMETA) 205*50989Sbostic goto eftype; 20645876Sbostic 207*50989Sbostic b.psize = m.m_psize; 208*50989Sbostic t->bt_flags = m.m_flags; 209*50989Sbostic t->bt_free = m.m_free; 210*50989Sbostic t->bt_lorder = m.m_lorder; 211*50989Sbostic t->bt_nrecs = m.m_nrecs; 212*50989Sbostic } else { 213*50989Sbostic /* 214*50989Sbostic * Set the page size to the best value for I/O to this file. 215*50989Sbostic * Don't overflow the page offset type. 216*50989Sbostic */ 217*50989Sbostic if (b.psize == 0) { 218*50989Sbostic b.psize = sb.st_blksize; 219*50989Sbostic if (b.psize < MINPSIZE) 220*50989Sbostic b.psize = MINPSIZE; 221*50989Sbostic if (b.psize > MAX_PAGE_OFFSET) 222*50989Sbostic b.psize = MAX_PAGE_OFFSET; 223*50989Sbostic } 224*50989Sbostic t->bt_flags = b.flags & R_DUP ? 0 : BTF_NODUPS; 225*50989Sbostic t->bt_free = P_INVALID; 226*50989Sbostic t->bt_lorder = b.lorder; 227*50989Sbostic t->bt_nrecs = 0; 228*50989Sbostic SET(t, BTF_METADIRTY); 22945876Sbostic } 23045876Sbostic 231*50989Sbostic t->bt_psize = b.psize; 23245876Sbostic 233*50989Sbostic /* Set the cache size; must be a multiple of the page size. */ 234*50989Sbostic if (b.cachesize && b.cachesize & b.psize - 1) 235*50989Sbostic b.cachesize += (~b.cachesize & b.psize - 1) + 1; 236*50989Sbostic if (b.cachesize < b.psize * MINCACHE) 237*50989Sbostic b.cachesize = b.psize * MINCACHE; 23845876Sbostic 239*50989Sbostic /* Calculate number of pages to cache. */ 240*50989Sbostic ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 24145876Sbostic 24245876Sbostic /* 243*50989Sbostic * The btree data structure requires that at least two keys can fit 244*50989Sbostic * on a page, but other than that there's no fixed requirement. The 245*50989Sbostic * user can specify the minimum number per page, and we translate 246*50989Sbostic * that into the maximum number of bytes a key can use before being 247*50989Sbostic * placed on an overflow page. 24845876Sbostic */ 249*50989Sbostic t->bt_minkeypage = (t->bt_psize - BTDATAOFF) / b.minkeypage; 25045876Sbostic 251*50989Sbostic /* Initialize the buffer pool. */ 252*50989Sbostic if ((t->bt_mp = 253*50989Sbostic mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 254*50989Sbostic goto err; 255*50989Sbostic mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 25645876Sbostic 257*50989Sbostic /* Create a root page if new tree. */ 258*50989Sbostic if (nroot(t) == RET_ERROR) 259*50989Sbostic goto err; 26045876Sbostic 261*50989Sbostic return (dbp); 26245876Sbostic 263*50989Sbostic einval: errno = EINVAL; 264*50989Sbostic goto err; 26545876Sbostic 266*50989Sbostic eftype: errno = EFTYPE; 267*50989Sbostic goto err; 26849322Sbostic 269*50989Sbostic err: if (t) { 270*50989Sbostic if (t->bt_dbp) 271*50989Sbostic free(t->bt_dbp); 272*50989Sbostic if (t->bt_fd != -1) 273*50989Sbostic (void)close(t->bt_fd); 274*50989Sbostic free(t); 27545876Sbostic } 276*50989Sbostic return (NULL); 27745876Sbostic } 27845876Sbostic 27945876Sbostic /* 280*50989Sbostic * NROOT -- Create the root of a new tree. 28145876Sbostic * 282*50989Sbostic * Parameters: 283*50989Sbostic * t: tree 28445876Sbostic * 285*50989Sbostic * Returns: 286*50989Sbostic * RET_ERROR, RET_SUCCESS 28745876Sbostic */ 288*50989Sbostic static int 289*50989Sbostic nroot(t) 290*50989Sbostic BTREE *t; 29145876Sbostic { 292*50989Sbostic PAGE *meta, *root; 293*50989Sbostic pgno_t npg; 29445876Sbostic 295*50989Sbostic if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 296*50989Sbostic mpool_put(t->bt_mp, meta, 0); 29745876Sbostic return (RET_SUCCESS); 29845876Sbostic } 299*50989Sbostic if (errno != EINVAL) 300*50989Sbostic return (RET_ERROR); 30145876Sbostic 302*50989Sbostic if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 303*50989Sbostic return (RET_ERROR); 30445876Sbostic 305*50989Sbostic if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 306*50989Sbostic return (RET_ERROR); 30745876Sbostic 308*50989Sbostic if (npg != P_ROOT) 30945876Sbostic return (RET_ERROR); 310*50989Sbostic root->pgno = npg; 311*50989Sbostic root->prevpg = root->nextpg = P_INVALID; 312*50989Sbostic root->lower = BTDATAOFF; 313*50989Sbostic root->upper = t->bt_psize; 314*50989Sbostic root->flags = P_BLEAF; 315*50989Sbostic mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 316*50989Sbostic mpool_put(t->bt_mp, root, MPOOL_DIRTY); 317*50989Sbostic return (RET_SUCCESS); 31845876Sbostic } 31945876Sbostic 320*50989Sbostic static int 321*50989Sbostic tmp() 32245876Sbostic { 323*50989Sbostic sigset_t set, oset; 324*50989Sbostic int fd; 325*50989Sbostic char *envtmp; 326*50989Sbostic char path[MAXPATHLEN]; 32745876Sbostic 328*50989Sbostic envtmp = getenv("TMPDIR"); 329*50989Sbostic (void)snprintf(path, 330*50989Sbostic sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 33149322Sbostic 332*50989Sbostic sigfillset(&set); 333*50989Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 334*50989Sbostic if ((fd = mkstemp(path)) != -1) 335*50989Sbostic (void)unlink(path); 336*50989Sbostic (void)sigprocmask(SIG_SETMASK, &oset, NULL); 337*50989Sbostic return(fd); 33845876Sbostic } 339