bt_open.c

Go to the documentation of this file.
00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996, 1997, 1998, 1999, 2000
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1990, 1993, 1994, 1995, 1996
00009  *      Keith Bostic.  All rights reserved.
00010  */
00011 /*
00012  * Copyright (c) 1990, 1993, 1994, 1995
00013  *      The Regents of the University of California.  All rights reserved.
00014  *
00015  * This code is derived from software contributed to Berkeley by
00016  * Mike Olson.
00017  *
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions
00020  * are met:
00021  * 1. Redistributions of source code must retain the above copyright
00022  *    notice, this list of conditions and the following disclaimer.
00023  * 2. Redistributions in binary form must reproduce the above copyright
00024  *    notice, this list of conditions and the following disclaimer in the
00025  *    documentation and/or other materials provided with the distribution.
00026  * 3. Neither the name of the University nor the names of its contributors
00027  *    may be used to endorse or promote products derived from this software
00028  *    without specific prior written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00031  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00034  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00036  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00037  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00039  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00040  * SUCH DAMAGE.
00041  */
00042 
00043 #include "config.h"
00044 
00045 #ifndef lint
00046 static const char revid[] = "$Id: bt__open_8c-source.html,v 1.1 2008/06/08 10:13:37 sebdiaz Exp $";
00047 #endif /* not lint */
00048 
00049 #ifndef NO_SYSTEM_INCLUDES
00050 #include <sys/types.h>
00051 
00052 #include <errno.h>
00053 #include <limits.h>
00054 #include <string.h>
00055 #endif
00056 
00057 #include "db_int.h"
00058 #include "db_page.h"
00059 #include "db_swap.h"
00060 #include "btree.h"
00061 #include "db_shash.h"
00062 #include "lock.h"
00063 #include "log.h"
00064 #include "mp.h"
00065 
00066 /*
00067  * CDB___bam_open --
00068  *      Open a btree.
00069  *
00070  * PUBLIC: int CDB___bam_open __P((DB *, const char *, db_pgno_t, u_int32_t));
00071  */
00072 int
00073 CDB___bam_open(dbp, name, base_pgno, flags)
00074         DB *dbp;
00075         const char *name;
00076         db_pgno_t base_pgno;
00077         u_int32_t flags;
00078 {
00079         BTREE *t;
00080 
00081         t = dbp->bt_internal;
00082 
00083         /* Initialize the remaining fields/methods of the DB. */
00084         dbp->del = CDB___bam_delete;
00085         dbp->key_range = CDB___bam_key_range;
00086         dbp->stat = CDB___bam_stat;
00087 
00088         /*
00089          * We don't permit the user to specify a prefix routine if they didn't
00090          * also specify a comparison routine, they can't know enough about our
00091          * comparison routine to get it right.
00092          */
00093         if (t->bt_compare == CDB___bam_defcmp && t->bt_prefix != CDB___bam_defpfx) {
00094                 CDB___db_err(dbp->dbenv,
00095 "prefix comparison may not be specified for default comparison routine");
00096                 return (EINVAL);
00097         }
00098 
00099         /* Start up the tree. */
00100         return (CDB___bam_read_root(dbp, name, base_pgno, flags));
00101 }
00102 
00103 /*
00104  * CDB___bam_metachk --
00105  *
00106  * PUBLIC: int CDB___bam_metachk __P((DB *, const char *, BTMETA *));
00107  */
00108 int
00109 CDB___bam_metachk(dbp, name, btm)
00110         DB *dbp;
00111         const char *name;
00112         BTMETA *btm;
00113 {
00114         DB_ENV *dbenv;
00115         u_int32_t vers;
00116         int ret;
00117 
00118         dbenv = dbp->dbenv;
00119 
00120         /*
00121          * At this point, all we know is that the magic number is for a Btree.
00122          * Check the version, the database may be out of date.
00123          */
00124         vers = btm->dbmeta.version;
00125         if (F_ISSET(dbp, DB_AM_SWAP))
00126                 M_32_SWAP(vers);
00127         switch (vers) {
00128         case 6:
00129         case 7:
00130                 CDB___db_err(dbenv,
00131                     "%s: btree version %lu requires a version upgrade",
00132                     name, (u_long)vers);
00133                 return (DB_OLD_VERSION);
00134         case 8:
00135                 break;
00136         default:
00137                 CDB___db_err(dbenv,
00138                     "%s: unsupported btree version: %lu", name, (u_long)vers);
00139                 return (EINVAL);
00140         }
00141 
00142         /* Swap the page if we need to. */
00143         if (F_ISSET(dbp, DB_AM_SWAP) && (ret = CDB___bam_mswap((PAGE *)btm)) != 0)
00144                 return (ret);
00145 
00146         /*
00147          * Check application info against metadata info, and set info, flags,
00148          * and type based on metadata info.
00149          */
00150         if ((ret =
00151             CDB___db_fchk(dbenv, "DB->open", btm->dbmeta.flags, BTM_MASK)) != 0)
00152                 return (ret);
00153 
00154         if (F_ISSET(&btm->dbmeta, BTM_RECNO)) {
00155                 if (dbp->type == DB_BTREE)
00156                         goto wrong_type;
00157                 dbp->type = DB_RECNO;
00158                 DB_ILLEGAL_METHOD(dbp, DB_OK_RECNO);
00159         } else {
00160                 if (dbp->type == DB_RECNO)
00161                         goto wrong_type;
00162                 dbp->type = DB_BTREE;
00163                 DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE);
00164         }
00165 
00166         if (F_ISSET(&btm->dbmeta, BTM_DUP))
00167                 F_SET(dbp, DB_AM_DUP);
00168         else
00169                 if (F_ISSET(dbp, DB_AM_DUP)) {
00170                         CDB___db_err(dbenv,
00171                 "%s: DB_DUP specified to open method but not set in database",
00172                             name);
00173                         return (EINVAL);
00174                 }
00175 
00176         if (F_ISSET(&btm->dbmeta, BTM_RECNUM)) {
00177                 if (dbp->type != DB_BTREE)
00178                         goto wrong_type;
00179                 F_SET(dbp, DB_BT_RECNUM);
00180 
00181                 if ((ret = CDB___db_fcchk(dbenv,
00182                     "DB->open", dbp->flags, DB_AM_DUP, DB_BT_RECNUM)) != 0)
00183                         return (ret);
00184         } else
00185                 if (F_ISSET(dbp, DB_BT_RECNUM)) {
00186                         CDB___db_err(dbenv,
00187             "%s: DB_RECNUM specified to open method but not set in database",
00188                             name);
00189                         return (EINVAL);
00190                 }
00191 
00192         if (F_ISSET(&btm->dbmeta, BTM_FIXEDLEN)) {
00193                 if (dbp->type != DB_RECNO)
00194                         goto wrong_type;
00195                 F_SET(dbp, DB_RE_FIXEDLEN);
00196         } else
00197                 if (F_ISSET(dbp, DB_RE_FIXEDLEN)) {
00198                         CDB___db_err(dbenv,
00199         "%s: DB_FIXEDLEN specified to open method but not set in database",
00200                             name);
00201                         return (EINVAL);
00202                 }
00203 
00204         if (F_ISSET(&btm->dbmeta, BTM_RENUMBER)) {
00205                 if (dbp->type != DB_RECNO)
00206                         goto wrong_type;
00207                 F_SET(dbp, DB_RE_RENUMBER);
00208         } else
00209                 if (F_ISSET(dbp, DB_RE_RENUMBER)) {
00210                         CDB___db_err(dbenv,
00211             "%s: DB_RENUMBER specified to open method but not set in database",
00212                             name);
00213                         return (EINVAL);
00214                 }
00215 
00216         if (F_ISSET(&btm->dbmeta, BTM_SUBDB))
00217                 F_SET(dbp, DB_AM_SUBDB);
00218         else
00219                 if (F_ISSET(dbp, DB_AM_SUBDB)) {
00220                         CDB___db_err(dbenv,
00221             "%s: multiple databases specified but not supported by file",
00222                             name);
00223                         return (EINVAL);
00224                 }
00225 
00226         if (F_ISSET(&btm->dbmeta, BTM_DUPSORT)) {
00227                 if (dbp->dup_compare == NULL)
00228                         dbp->dup_compare = CDB___bam_defcmp;
00229                 F_SET(dbp, DB_AM_DUPSORT);
00230         } else
00231                 if (dbp->dup_compare != NULL) {
00232                         CDB___db_err(dbenv,
00233                 "%s: duplicate sort specified but not supported in database",
00234                             name);
00235                         return (EINVAL);
00236                 }
00237 
00238         /* Set the page size. */
00239         dbp->pgsize = btm->dbmeta.pagesize;
00240 
00241         /* Copy the file's ID. */
00242         memcpy(dbp->fileid, btm->dbmeta.uid, DB_FILE_ID_LEN);
00243 
00244         return (0);
00245 
00246 wrong_type:
00247         if (dbp->type == DB_BTREE)
00248                 CDB___db_err(dbenv,
00249                     "open method type is Btree, database type is Recno");
00250         else
00251                 CDB___db_err(dbenv,
00252                     "open method type is Recno, database type is Btree");
00253         return (EINVAL);
00254 }
00255 
00256 /*
00257  * CDB___bam_read_root --
00258  *      Check (and optionally create) a tree.
00259  *
00260  * PUBLIC: int CDB___bam_read_root __P((DB *, const char *, db_pgno_t, u_int32_t));
00261  */
00262 int
00263 CDB___bam_read_root(dbp, name, base_pgno, flags)
00264         DB *dbp;
00265         const char *name;
00266         db_pgno_t base_pgno;
00267         u_int32_t flags;
00268 {
00269         BTMETA *meta;
00270         BTREE *t;
00271         DBC *dbc;
00272         DB_LSN orig_lsn;
00273         DB_LOCK metalock;
00274         PAGE *root;
00275         int locked, ret, t_ret;
00276 
00277         ret = 0;
00278         t = dbp->bt_internal;
00279         meta = NULL;
00280         root = NULL;
00281         locked = 0;
00282 
00283         /* 
00284          * Get a cursor.  If DB_CREATE is specified, we may be creating
00285          * the root page, and to do that safely in CDB we need a write
00286          * cursor.  In STD_LOCKING mode, we'll synchronize using the 
00287          * meta page lock instead.
00288          */
00289         if ((ret = dbp->cursor(dbp, dbp->open_txn,
00290             &dbc, LF_ISSET(DB_CREATE) && LOCKING(dbp->dbenv) ? 
00291             DB_WRITECURSOR : 0)) != 0)
00292                 return (ret);
00293 
00294         /* Get, and optionally create the metadata page. */
00295         if ((ret =
00296             CDB___db_lget(dbc, 0, base_pgno, DB_LOCK_READ, 0, &metalock)) != 0)
00297                 goto err;
00298         if ((ret = CDB_memp_fget(
00299             dbp->mpf, &base_pgno, DB_MPOOL_CREATE, (PAGE **)&meta)) != 0)
00300                 goto err;
00301 
00302         /*
00303          * If the magic number is correct, we're not creating the tree.
00304          * Correct any fields that may not be right.  Note, all of the
00305          * local flags were set by DB->open.
00306          */
00307 again:  if (meta->dbmeta.magic != 0) {
00308                 t->bt_maxkey = meta->maxkey;
00309                 t->bt_minkey = meta->minkey;
00310                 t->re_pad = meta->re_pad;
00311                 t->re_len = meta->re_len;
00312 
00313                 t->bt_meta = base_pgno;
00314                 t->bt_root = meta->root;
00315 
00316                 (void)CDB_memp_fput(dbp->mpf, meta, 0);
00317                 meta = NULL;
00318                 goto done;
00319         }
00320 
00321         /* In recovery if it's not there it will be created elsewhere.*/
00322         if (IS_RECOVERING(dbp->dbenv))
00323                 goto done;
00324 
00325         /* If we're doing CDB; we now have to get the write lock. */
00326         if (LOCKING(dbp->dbenv)) {
00327                 /* 
00328                  * We'd better have DB_CREATE set if we're actually doing 
00329                  * the create. 
00330                  */
00331                 DB_ASSERT(LF_ISSET(DB_CREATE));
00332                 if ((ret = CDB_lock_get(dbp->dbenv, dbc->locker, DB_LOCK_UPGRADE,
00333                     &dbc->lock_dbt, DB_LOCK_WRITE, &dbc->mylock)) != 0)
00334                         goto err;
00335         }
00336 
00337         /*
00338          * If we are doing locking, relase the read lock and get a write lock.
00339          * We want to avoid deadlock.
00340          */
00341         if (locked == 0 && STD_LOCKING(dbc)) {
00342                 if ((ret = __LPUT(dbc, metalock)) != 0)
00343                         goto err;
00344                 if ((ret = CDB___db_lget(dbc,
00345                      0, base_pgno, DB_LOCK_WRITE, 0, &metalock)) != 0)
00346                         goto err;
00347                 locked = 1;
00348                 goto again;
00349         }
00350 
00351         /* Initialize the tree structure metadata information. */
00352         orig_lsn = meta->dbmeta.lsn;
00353         memset(meta, 0, sizeof(BTMETA));
00354         meta->dbmeta.lsn = orig_lsn;
00355         meta->dbmeta.pgno = base_pgno;
00356         meta->dbmeta.magic = DB_BTREEMAGIC;
00357         meta->dbmeta.version = DB_BTREEVERSION;
00358         meta->dbmeta.pagesize = dbp->pgsize;
00359         meta->dbmeta.type = P_BTREEMETA;
00360         meta->dbmeta.free = PGNO_INVALID;
00361         if (F_ISSET(dbp, DB_AM_DUP))
00362                 F_SET(&meta->dbmeta, BTM_DUP);
00363         if (F_ISSET(dbp, DB_RE_FIXEDLEN))
00364                 F_SET(&meta->dbmeta, BTM_FIXEDLEN);
00365         if (F_ISSET(dbp, DB_BT_RECNUM))
00366                 F_SET(&meta->dbmeta, BTM_RECNUM);
00367         if (F_ISSET(dbp, DB_RE_RENUMBER))
00368                 F_SET(&meta->dbmeta, BTM_RENUMBER);
00369         if (F_ISSET(dbp, DB_AM_SUBDB))
00370                 F_SET(&meta->dbmeta, BTM_SUBDB);
00371         if (dbp->dup_compare != NULL)
00372                 F_SET(&meta->dbmeta, BTM_DUPSORT);
00373         if (dbp->type == DB_RECNO)
00374                 F_SET(&meta->dbmeta, BTM_RECNO);
00375         memcpy(meta->dbmeta.uid, dbp->fileid, DB_FILE_ID_LEN);
00376 
00377         meta->maxkey = t->bt_maxkey;
00378         meta->minkey = t->bt_minkey;
00379         meta->re_len = t->re_len;
00380         meta->re_pad = t->re_pad;
00381 
00382         /* If necessary, log the meta-data and root page creates.  */
00383         if ((ret = CDB___db_log_page(dbp,
00384             name, &orig_lsn, base_pgno, (PAGE *)meta)) != 0)
00385                 goto err;
00386 
00387         /* Create and initialize a root page. */
00388         if ((ret = CDB___db_new(dbc,
00389             ((dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE) | dbp->tags), &root)) != 0)
00390                 goto err;
00391         root->level = LEAFLEVEL;
00392 
00393         if (dbp->open_txn != NULL && (ret = CDB___bam_root_log(dbp->dbenv,
00394             dbp->open_txn, &meta->dbmeta.lsn, 0, dbp->log_fileid,
00395             meta->dbmeta.pgno, root->pgno, &meta->dbmeta.lsn)) != 0)
00396                 goto err;
00397 
00398         meta->root = root->pgno;
00399 
00400         DB_TEST_RECOVERY(dbp, DB_TEST_POSTLOGMETA, ret, name);
00401         if ((ret = CDB___db_log_page(dbp,
00402             name, &root->lsn, root->pgno, root)) != 0)
00403                 goto err;
00404         DB_TEST_RECOVERY(dbp, DB_TEST_POSTLOG, ret, name);
00405 
00406         t->bt_meta = base_pgno;
00407         t->bt_root = root->pgno;
00408 
00409         /* Release the metadata and root pages. */
00410         if ((ret = CDB_memp_fput(dbp->mpf, meta, DB_MPOOL_DIRTY)) != 0)
00411                 goto err;
00412         meta = NULL;
00413         if ((ret = CDB_memp_fput(dbp->mpf, root, DB_MPOOL_DIRTY)) != 0)
00414                 goto err;
00415         root = NULL;
00416 
00417         /*
00418          * Flush the metadata and root pages to disk.
00419          *
00420          * !!!
00421          * It's not useful to return not-yet-flushed here -- convert it to
00422          * an error.
00423          */
00424         if ((ret = CDB_memp_fsync(dbp->mpf)) == DB_INCOMPLETE) {
00425                 CDB___db_err(dbp->dbenv, "Metapage flush failed");
00426                 ret = EINVAL;
00427         }
00428         DB_TEST_RECOVERY(dbp, DB_TEST_POSTSYNC, ret, name);
00429 
00430 done:   /*
00431          * !!!
00432          * We already did an insert and so the last-page-inserted has been
00433          * set.  I'm not sure where the *right* place to clear this value
00434          * is, it's not intuitively obvious that it belongs here.
00435          */
00436         t->bt_lpgno = PGNO_INVALID;
00437 
00438 err:
00439 DB_TEST_RECOVERY_LABEL
00440         /* Put any remaining pages back. */
00441         if (meta != NULL)
00442                 if ((t_ret = CDB_memp_fput(dbp->mpf, meta, 0)) != 0 &&
00443                     ret == 0)
00444                         ret = t_ret;
00445         if (root != NULL)
00446                 if ((t_ret = CDB_memp_fput(dbp->mpf, root, 0)) != 0 &&
00447                     ret == 0)
00448                         ret = t_ret;
00449 
00450         /* We can release the metapage lock when we are done. */
00451         (void)__LPUT(dbc, metalock);
00452 
00453         if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
00454                 ret = t_ret;
00455         return (ret);
00456 }

Generated on Sun Jun 8 10:56:35 2008 for GNUmifluz by  doxygen 1.5.5