bt_open.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_open.c  8.10 (Berkeley) 8/17/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 /*
00042  * Implementation of btree access method for 4.4BSD.
00043  *
00044  * The design here was originally based on that of the btree access method
00045  * used in the Postgres database system at UC Berkeley.  This implementation
00046  * is wholly independent of the Postgres code.
00047  */
00048 
00049 #include <sys/param.h>
00050 #include <sys/stat.h>
00051 
00052 #include <errno.h>
00053 #include <fcntl.h>
00054 #include <limits.h>
00055 #include <signal.h>
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058 #include <string.h>
00059 #include <unistd.h>
00060 
00061 #include "../include/db.h"
00062 #include "btree.h"
00063 
00064 #ifdef DEBUG
00065 #undef   MINPSIZE
00066 #define  MINPSIZE 128
00067 #endif
00068 
00069 static int byteorder __P((void));
00070 static int nroot __P((BTREE *));
00071 static int tmp __P((void));
00072 
00073 /*
00074  * __BT_OPEN -- Open a btree.
00075  *
00076  * Creates and fills a DB struct, and calls the routine that actually
00077  * opens the btree.
00078  *
00079  * Parameters:
00080  * fname:   filename (NULL for in-memory trees)
00081  * flags:   open flag bits
00082  * mode: open permission bits
00083  * b: BTREEINFO pointer
00084  *
00085  * Returns:
00086  * NULL on failure, pointer to DB on success.
00087  *
00088  */
00089 DB *
00090 __bt_open(fname, flags, mode, openinfo, dflags)
00091    const char *fname;
00092    int flags, mode, dflags;
00093    const BTREEINFO *openinfo;
00094 {
00095    struct stat sb;
00096    BTMETA m;
00097    BTREE *t;
00098    BTREEINFO b;
00099    DB *dbp;
00100    pgno_t ncache;
00101    ssize_t nr;
00102    int machine_lorder;
00103 
00104    t = NULL;
00105 
00106    /*
00107     * Intention is to make sure all of the user's selections are okay
00108     * here and then use them without checking.  Can't be complete, since
00109     * we don't know the right page size, lorder or flags until the backing
00110     * file is opened.  Also, the file's page size can cause the cachesize
00111     * to change.
00112     */
00113    machine_lorder = byteorder();
00114    if (openinfo) {
00115       b = *openinfo;
00116 
00117       /* Flags: R_DUP. */
00118       if (b.flags & ~(R_DUP))
00119          goto einval;
00120 
00121       /*
00122        * Page size must be indx_t aligned and >= MINPSIZE.  Default
00123        * page size is set farther on, based on the underlying file
00124        * transfer size.
00125        */
00126       if (b.psize &&
00127           (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
00128           b.psize & (sizeof(indx_t) - 1)))
00129          goto einval;
00130 
00131       /* Minimum number of keys per page; absolute minimum is 2. */
00132       if (b.minkeypage) {
00133          if (b.minkeypage < 2)
00134             goto einval;
00135       } else
00136          b.minkeypage = DEFMINKEYPAGE;
00137 
00138       /* If no comparison, use default comparison and prefix. */
00139       if (b.compare == NULL) {
00140          b.compare = __bt_defcmp;
00141          if (b.prefix == NULL)
00142             b.prefix = __bt_defpfx;
00143       }
00144 
00145       if (b.lorder == 0)
00146          b.lorder = machine_lorder;
00147    } else {
00148       b.compare = __bt_defcmp;
00149       b.cachesize = 0;
00150       b.flags = 0;
00151       b.lorder = machine_lorder;
00152       b.minkeypage = DEFMINKEYPAGE;
00153       b.prefix = __bt_defpfx;
00154       b.psize = 0;
00155    }
00156 
00157    /* Check for the ubiquitous PDP-11. */
00158    if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
00159       goto einval;
00160 
00161    /* Allocate and initialize DB and BTREE structures. */
00162    if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
00163       goto err;
00164    memset(t, 0, sizeof(BTREE));
00165    t->bt_fd = -1;       /* Don't close unopened fd on error. */
00166    t->bt_lorder = b.lorder;
00167    t->bt_order = NOT;
00168    t->bt_cmp = b.compare;
00169    t->bt_pfx = b.prefix;
00170    t->bt_rfd = -1;
00171 
00172    if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
00173       goto err;
00174    memset(t->bt_dbp, 0, sizeof(DB));
00175    if (t->bt_lorder != machine_lorder)
00176       F_SET(t, B_NEEDSWAP);
00177 
00178    dbp->type = DB_BTREE;
00179    dbp->internal = t;
00180    dbp->close = __bt_close;
00181    dbp->del = __bt_delete;
00182    dbp->fd = __bt_fd;
00183    dbp->get = __bt_get;
00184    dbp->put = __bt_put;
00185    dbp->seq = __bt_seq;
00186    dbp->sync = __bt_sync;
00187 
00188    /*
00189     * If no file name was supplied, this is an in-memory btree and we
00190     * open a backing temporary file.  Otherwise, it's a disk-based tree.
00191     */
00192    if (fname) {
00193       switch (flags & O_ACCMODE) {
00194       case O_RDONLY:
00195          F_SET(t, B_RDONLY);
00196          break;
00197       case O_RDWR:
00198          break;
00199       case O_WRONLY:
00200       default:
00201          goto einval;
00202       }
00203 
00204       if ((t->bt_fd = open(fname, flags, mode)) < 0)
00205          goto err;
00206 
00207    } else {
00208       if ((flags & O_ACCMODE) != O_RDWR)
00209          goto einval;
00210       if ((t->bt_fd = tmp()) == -1)
00211          goto err;
00212       F_SET(t, B_INMEM);
00213    }
00214 
00215    if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
00216       goto err;
00217 
00218    if (fstat(t->bt_fd, &sb))
00219       goto err;
00220    if (sb.st_size) {
00221       if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
00222          goto err;
00223       if (nr != sizeof(BTMETA))
00224          goto eftype;
00225 
00226       /*
00227        * Read in the meta-data.  This can change the notion of what
00228        * the lorder, page size and flags are, and, when the page size
00229        * changes, the cachesize value can change too.  If the user
00230        * specified the wrong byte order for an existing database, we
00231        * don't bother to return an error, we just clear the NEEDSWAP
00232        * bit.
00233        */
00234       if (m.magic == BTREEMAGIC)
00235          F_CLR(t, B_NEEDSWAP);
00236       else {
00237          F_SET(t, B_NEEDSWAP);
00238          M_32_SWAP(m.magic);
00239          M_32_SWAP(m.version);
00240          M_32_SWAP(m.psize);
00241          M_32_SWAP(m.free);
00242          M_32_SWAP(m.nrecs);
00243          M_32_SWAP(m.flags);
00244       }
00245       if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
00246          goto eftype;
00247       if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
00248           m.psize & (sizeof(indx_t) - 1))
00249          goto eftype;
00250       if (m.flags & ~SAVEMETA)
00251          goto eftype;
00252       b.psize = m.psize;
00253       F_SET(t, m.flags);
00254       t->bt_free = m.free;
00255       t->bt_nrecs = m.nrecs;
00256    } else {
00257       /*
00258        * Set the page size to the best value for I/O to this file.
00259        * Don't overflow the page offset type.
00260        */
00261       if (b.psize == 0) {
00262 #ifdef _STATBUF_ST_BLKSIZE
00263          b.psize = sb.st_blksize;
00264 #endif
00265          if (b.psize < MINPSIZE)
00266             b.psize = MINPSIZE;
00267          if (b.psize > MAX_PAGE_OFFSET + 1)
00268             b.psize = MAX_PAGE_OFFSET + 1;
00269       }
00270 
00271       /* Set flag if duplicates permitted. */
00272       if (!(b.flags & R_DUP))
00273          F_SET(t, B_NODUPS);
00274 
00275       t->bt_free = P_INVALID;
00276       t->bt_nrecs = 0;
00277       F_SET(t, B_METADIRTY);
00278    }
00279 
00280    t->bt_psize = b.psize;
00281 
00282    /* Set the cache size; must be a multiple of the page size. */
00283    if (b.cachesize && b.cachesize & (b.psize - 1))
00284       b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
00285    if (b.cachesize < b.psize * MINCACHE)
00286       b.cachesize = b.psize * MINCACHE;
00287 
00288    /* Calculate number of pages to cache. */
00289    ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
00290 
00291    /*
00292     * The btree data structure requires that at least two keys can fit on
00293     * a page, but other than that there's no fixed requirement.  The user
00294     * specified a minimum number per page, and we translated that into the
00295     * number of bytes a key/data pair can use before being placed on an
00296     * overflow page.  This calculation includes the page header, the size
00297     * of the index referencing the leaf item and the size of the leaf item
00298     * structure.  Also, don't let the user specify a minkeypage such that
00299     * a key/data pair won't fit even if both key and data are on overflow
00300     * pages.
00301     */
00302    t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
00303        (sizeof(indx_t) + NBLEAFDBT(0, 0));
00304    if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
00305       t->bt_ovflsize =
00306           NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
00307 
00308    /* Initialize the buffer pool. */
00309    if ((t->bt_mp =
00310        mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
00311       goto err;
00312    if (!F_ISSET(t, B_INMEM))
00313       mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
00314 
00315    /* Create a root page if new tree. */
00316    if (nroot(t) == RET_ERROR)
00317       goto err;
00318 
00319    /* Global flags. */
00320    if (dflags & DB_LOCK)
00321       F_SET(t, B_DB_LOCK);
00322    if (dflags & DB_SHMEM)
00323       F_SET(t, B_DB_SHMEM);
00324    if (dflags & DB_TXN)
00325       F_SET(t, B_DB_TXN);
00326 
00327    return (dbp);
00328 
00329 einval:  errno = EINVAL;
00330    goto err;
00331 
00332 eftype:  errno = EFTYPE;
00333    goto err;
00334 
00335 err:  if (t) {
00336       if (t->bt_dbp)
00337          free(t->bt_dbp);
00338       if (t->bt_fd != -1)
00339          (void)close(t->bt_fd);
00340       free(t);
00341    }
00342    return (NULL);
00343 }
00344 
00345 /*
00346  * NROOT -- Create the root of a new tree.
00347  *
00348  * Parameters:
00349  * t: tree
00350  *
00351  * Returns:
00352  * RET_ERROR, RET_SUCCESS
00353  */
00354 static int
00355 nroot(t)
00356    BTREE *t;
00357 {
00358    PAGE *meta, *root;
00359    pgno_t npg;
00360 
00361    if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
00362       mpool_put(t->bt_mp, meta, 0);
00363       return (RET_SUCCESS);
00364    }
00365    if (errno != EINVAL)    /* It's OK to not exist. */
00366       return (RET_ERROR);
00367    errno = 0;
00368 
00369    if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
00370       return (RET_ERROR);
00371 
00372    if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
00373       return (RET_ERROR);
00374 
00375    if (npg != P_ROOT)
00376       return (RET_ERROR);
00377    root->pgno = npg;
00378    root->prevpg = root->nextpg = P_INVALID;
00379    root->lower = BTDATAOFF;
00380    root->upper = t->bt_psize;
00381    root->flags = P_BLEAF;
00382    memset(meta, 0, t->bt_psize);
00383    mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
00384    mpool_put(t->bt_mp, root, MPOOL_DIRTY);
00385    return (RET_SUCCESS);
00386 }
00387 
00388 static int
00389 tmp()
00390 {
00391    sigset_t set, oset;
00392    int fd;
00393    const char *envtmp;
00394    char *path;
00395    static const char fmt[] = "%s/bt.XXXXXX";
00396    size_t n;
00397 
00398    envtmp = getenv("TMPDIR");
00399    if (!envtmp)
00400      envtmp = "/tmp";
00401    n = strlen (envtmp) + sizeof fmt;
00402 #ifdef   __GNUC__
00403    path = __builtin_alloca(n);
00404 #else
00405    path = malloc(n);
00406 #endif
00407    (void)snprintf(path, n, fmt, envtmp ? envtmp : "/tmp");
00408 
00409    (void)sigfillset(&set);
00410    (void)sigprocmask(SIG_BLOCK, &set, &oset);
00411    if ((fd = mkstemp(path)) != -1)
00412       (void)unlink(path);
00413    (void)sigprocmask(SIG_SETMASK, &oset, NULL);
00414 #ifndef  __GNUC__
00415    free(path);
00416 #endif
00417    return(fd);
00418 }
00419 
00420 static int
00421 byteorder()
00422 {
00423    u_int32_t x;
00424    u_char *p;
00425 
00426    x = 0x01020304;
00427    p = (u_char *)&x;
00428    switch (*p) {
00429    case 1:
00430       return (BIG_ENDIAN);
00431    case 4:
00432       return (LITTLE_ENDIAN);
00433    default:
00434       return (0);
00435    }
00436 }
00437 
00438 int
00439 __bt_fd(dbp)
00440         const DB *dbp;
00441 {
00442    BTREE *t;
00443 
00444    t = dbp->internal;
00445 
00446    /* Toss any page pinned across calls. */
00447    if (t->bt_pinned != NULL) {
00448       mpool_put(t->bt_mp, t->bt_pinned, 0);
00449       t->bt_pinned = NULL;
00450    }
00451 
00452    /* In-memory database can't have a file descriptor. */
00453    if (F_ISSET(t, B_INMEM)) {
00454       errno = ENOENT;
00455       return (-1);
00456    }
00457    return (t->bt_fd);
00458 }

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6