db_dispatch.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) 1995, 1996
00009  *      The President and Fellows of Harvard University.  All rights reserved.
00010  *
00011  * This code is derived from software contributed to Berkeley by
00012  * Margo Seltzer.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions
00016  * are met:
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  * 2. Redistributions in binary form must reproduce the above copyright
00020  *    notice, this list of conditions and the following disclaimer in the
00021  *    documentation and/or other materials provided with the distribution.
00022  * 3. Neither the name of the University nor the names of its contributors
00023  *    may be used to endorse or promote products derived from this software
00024  *    without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00027  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00030  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00031  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00032  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00033  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00034  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00035  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00036  * SUCH DAMAGE.
00037  */
00038 
00039 #include "config.h"
00040 
00041 #ifndef lint
00042 static const char revid[] = "$Id: db__dispatch_8c-source.html,v 1.1 2008/06/08 10:17:25 sebdiaz Exp $";
00043 #endif /* not lint */
00044 
00045 #ifndef NO_SYSTEM_INCLUDES
00046 #include <sys/types.h>
00047 
00048 #include <errno.h>
00049 #include <stddef.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #endif
00053 
00054 #include "db_int.h"
00055 #include "db_page.h"
00056 #include "db_dispatch.h"
00057 #include "db_am.h"
00058 #include "log_auto.h"
00059 #include "txn.h"
00060 #include "txn_auto.h"
00061 #include "log.h"
00062 
00063 /*
00064  * CDB___db_dispatch --
00065  *
00066  * This is the transaction dispatch function used by the db access methods.
00067  * It is designed to handle the record format used by all the access
00068  * methods (the one automatically generated by the db_{h,log,read}.sh
00069  * scripts in the tools directory).  An application using a different
00070  * recovery paradigm will supply a different dispatch function to txn_open.
00071  *
00072  * PUBLIC: int CDB___db_dispatch __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
00073  */
00074 int
00075 CDB___db_dispatch(dbenv, db, lsnp, redo, info)
00076         DB_ENV *dbenv;          /* The environment. */
00077         DBT *db;                /* The log record upon which to dispatch. */
00078         DB_LSN *lsnp;           /* The lsn of the record being dispatched. */
00079         db_recops redo;         /* Redo this op (or undo it). */
00080         void *info;
00081 {
00082         u_int32_t rectype, txnid;
00083 
00084         memcpy(&rectype, db->data, sizeof(rectype));
00085         memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
00086 
00087         switch (redo) {
00088         case DB_TXN_ABORT:
00089                 return ((dbenv->dtab[rectype])(dbenv, db, lsnp, redo, info));
00090         case DB_TXN_OPENFILES:
00091                 if (rectype == DB_log_register)
00092                         return (dbenv->dtab[rectype](dbenv,
00093                             db, lsnp, redo, info));
00094                 break;
00095         case DB_TXN_BACKWARD_ROLL:
00096                 /*
00097                  * Running full recovery in the backward pass.  If we've
00098                  * seen this txnid before and added to it our commit list,
00099                  * then we do nothing during this pass.  If we've never
00100                  * seen it, then we call the appropriate recovery routine
00101                  * in "abort mode".
00102                  *
00103                  * We need to always undo DB_db_noop records, so that we
00104                  * properly handle any aborts before the file was closed.
00105                  */
00106                 if (rectype == DB_log_register || rectype == DB_txn_ckp ||
00107                     rectype == DB_db_noop ||
00108                     (CDB___db_txnlist_find(info, txnid) == DB_NOTFOUND &&
00109                     txnid != 0))
00110                         return (dbenv->dtab[rectype](dbenv,
00111                             db, lsnp, DB_TXN_BACKWARD_ROLL, info));
00112                 break;
00113         case DB_TXN_FORWARD_ROLL:
00114                 /*
00115                  * In the forward pass, if we haven't seen the transaction,
00116                  * do nothing, else recovery it.
00117                  *
00118                  * We need to always redo DB_db_noop records, so that we
00119                  * properly handle any commits after the file was closed.
00120                  */
00121                 if (rectype == DB_log_register || rectype == DB_txn_ckp ||
00122                     rectype == DB_db_noop ||
00123                     CDB___db_txnlist_find(info, txnid) != DB_NOTFOUND)
00124                         return (dbenv->dtab[rectype](dbenv,
00125                             db, lsnp, DB_TXN_FORWARD_ROLL, info));
00126                 break;
00127         default:
00128                 return (CDB___db_unknown_flag(dbenv, "CDB___db_dispatch", redo));
00129         }
00130         return (0);
00131 }
00132 
00133 /*
00134  * CDB___db_add_recovery --
00135  *
00136  * PUBLIC: int CDB___db_add_recovery __P((DB_ENV *,
00137  * PUBLIC:    int (*)(DB_ENV *, DBT *, DB_LSN *, db_recops, void *), u_int32_t));
00138  */
00139 int
00140 CDB___db_add_recovery(dbenv, func, ndx)
00141         DB_ENV *dbenv;
00142         int (*func) __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
00143         u_int32_t ndx;
00144 {
00145         u_int32_t i, nsize;
00146         int ret;
00147 
00148         /* Check if we have to grow the table. */
00149         if (ndx >= dbenv->dtab_size) {
00150                 nsize = ndx + 40;
00151                 if ((ret = CDB___os_realloc(dbenv,
00152                     nsize * sizeof(dbenv->dtab[0]), NULL, &dbenv->dtab)) != 0)
00153                         return (ret);
00154                 for (i = dbenv->dtab_size; i < nsize; ++i)
00155                         dbenv->dtab[i] = NULL;
00156                 dbenv->dtab_size = nsize;
00157         }
00158 
00159         dbenv->dtab[ndx] = func;
00160         return (0);
00161 }
00162 
00163 /*
00164  * CDB___deprecated_recover --
00165  *      Stub routine for deprecated recovery functions.
00166  *
00167  * PUBLIC: int CDB___deprecated_recover
00168  * PUBLIC:     __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
00169  */
00170 int
00171 CDB___deprecated_recover(dbenv, dbtp, lsnp, op, info)
00172         DB_ENV *dbenv;
00173         DBT *dbtp;
00174         DB_LSN *lsnp;
00175         db_recops op;
00176         void *info;
00177 {
00178         COMPQUIET(dbenv, NULL);
00179         COMPQUIET(dbtp, NULL);
00180         COMPQUIET(lsnp, NULL);
00181         COMPQUIET(op, 0);
00182         COMPQUIET(info, NULL);
00183         return (EINVAL);
00184 }
00185 
00186 /*
00187  * CDB___db_txnlist_init --
00188  *      Initialize transaction linked list.
00189  *
00190  * PUBLIC: int CDB___db_txnlist_init __P((DB_ENV *, void *));
00191  */
00192 int
00193 CDB___db_txnlist_init(dbenv, retp)
00194         DB_ENV *dbenv;
00195         void *retp;
00196 {
00197         DB_TXNHEAD *headp;
00198         int ret;
00199 
00200         if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNHEAD), NULL, &headp)) != 0)
00201                 return (ret);
00202 
00203         LIST_INIT(&headp->head);
00204         headp->maxid = 0;
00205         headp->generation = 1;
00206 
00207         *(void **)retp = headp;
00208         return (0);
00209 }
00210 
00211 /*
00212  * CDB___db_txnlist_add --
00213  *      Add an element to our transaction linked list.
00214  *
00215  * PUBLIC: int CDB___db_txnlist_add __P((DB_ENV *, void *, u_int32_t));
00216  */
00217 int
00218 CDB___db_txnlist_add(dbenv, listp, txnid)
00219         DB_ENV *dbenv;
00220         void *listp;
00221         u_int32_t txnid;
00222 {
00223         DB_TXNHEAD *hp;
00224         DB_TXNLIST *elp;
00225         int ret;
00226 
00227         if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &elp)) != 0)
00228                 return (ret);
00229 
00230         hp = (DB_TXNHEAD *)listp;
00231         LIST_INSERT_HEAD(&hp->head, elp, links);
00232 
00233         elp->type = TXNLIST_TXNID;
00234         elp->u.t.txnid = txnid;
00235         if (txnid > hp->maxid)
00236                 hp->maxid = txnid;
00237         elp->u.t.generation = hp->generation;
00238 
00239         return (0);
00240 }
00241 
00242 /* CDB___db_txnlist_close --
00243  *
00244  *      Call this when we close a file.  It allows us to reconcile whether
00245  * we have done any operations on this file with whether the file appears
00246  * to have been deleted.  If you never do any operations on a file, then
00247  * we assume it's OK to appear deleted.
00248  *
00249  * PUBLIC: int CDB___db_txnlist_close __P((void *, int32_t, u_int32_t));
00250  */
00251 
00252 int
00253 CDB___db_txnlist_close(listp, lid, count)
00254         void *listp;
00255         int32_t lid;
00256         u_int32_t count;
00257 {
00258         DB_TXNHEAD *hp;
00259         DB_TXNLIST *p;
00260 
00261         hp = (DB_TXNHEAD *)listp;
00262         for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
00263                 if (p->type == TXNLIST_DELETE)
00264                         if (lid == p->u.d.fileid &&
00265                             !F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED)) {
00266                                 p->u.d.count += count;
00267                                 return (0);
00268                         }
00269         }
00270 
00271         return (0);
00272 }
00273 
00274 /*
00275  * CDB___db_txnlist_delete --
00276  *
00277  *      Record that a file was missing or deleted.  If the deleted
00278  * flag is set, then we've encountered a delete of a file, else we've
00279  * just encountered a file that is missing.  The lid is the log fileid
00280  * and is only meaningful if deleted is not equal to 0.
00281  *
00282  * PUBLIC: int CDB___db_txnlist_delete __P((DB_ENV *,
00283  * PUBLIC:     void *, char *, u_int32_t, int));
00284  */
00285 int
00286 CDB___db_txnlist_delete(dbenv, listp, name, lid, deleted)
00287         DB_ENV *dbenv;
00288         void *listp;
00289         char *name;
00290         u_int32_t lid;
00291         int deleted;
00292 {
00293         DB_TXNHEAD *hp;
00294         DB_TXNLIST *p;
00295         int ret;
00296 
00297         hp = (DB_TXNHEAD *)listp;
00298         for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
00299                 if (p->type == TXNLIST_DELETE)
00300                         if (strcmp(name, p->u.d.fname) == 0) {
00301                                 if (deleted)
00302                                         F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
00303                                 else
00304                                         F_CLR(&p->u.d, TXNLIST_FLAG_CLOSED);
00305                                 return (0);
00306                         }
00307         }
00308 
00309         /* Need to add it. */
00310         if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &p)) != 0)
00311                 return (ret);
00312         LIST_INSERT_HEAD(&hp->head, p, links);
00313 
00314         p->type = TXNLIST_DELETE;
00315         p->u.d.flags = 0;
00316         if (deleted)
00317                 F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
00318         p->u.d.fileid = lid;
00319         p->u.d.count = 0;
00320         ret = CDB___os_strdup(dbenv, name, &p->u.d.fname);
00321 
00322         return (ret);
00323 }
00324 
00325 /*
00326  * CDB___db_txnlist_end --
00327  *      Discard transaction linked list. Print out any error messages
00328  * for deleted files.
00329  *
00330  * PUBLIC: void CDB___db_txnlist_end __P((DB_ENV *, void *));
00331  */
00332 void
00333 CDB___db_txnlist_end(dbenv, listp)
00334         DB_ENV *dbenv;
00335         void *listp;
00336 {
00337         DB_TXNHEAD *hp;
00338         DB_TXNLIST *p;
00339         DB_LOG *lp;
00340 
00341         hp = (DB_TXNHEAD *)listp;
00342         lp = (DB_LOG *)dbenv->lg_handle;
00343         while (hp != NULL &&
00344             (p = LIST_FIRST(&hp->head)) != LIST_END(&hp->head)) {
00345                 LIST_REMOVE(p, links);
00346                 if (p->type == TXNLIST_DELETE) {
00347                         /*
00348                          * If we have a file that is not deleted and has
00349                          * some operations, we flag the warning.  Since
00350                          * the file could still be open, we need to check
00351                          * the actual log table as well.
00352                          */
00353                         if ((!F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) &&
00354                             p->u.d.count != 0) ||
00355                             (!F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) &&
00356                             p->u.d.fileid != (int32_t) TXNLIST_INVALID_ID &&
00357                             p->u.d.fileid < lp->dbentry_cnt &&
00358                             lp->dbentry[p->u.d.fileid].count != 0))
00359                                 CDB___db_err(dbenv, "warning: %s: %s",
00360                                     p->u.d.fname, CDB_db_strerror(ENOENT));
00361                         CDB___os_freestr(p->u.d.fname);
00362                 }
00363                 CDB___os_free(p, sizeof(DB_TXNLIST));
00364         }
00365         CDB___os_free(listp, sizeof(DB_TXNHEAD));
00366 }
00367 
00368 /*
00369  * CDB___db_txnlist_find --
00370  *      Checks to see if a txnid with the current generation is in the
00371  *      txnid list.
00372  *
00373  * PUBLIC: int CDB___db_txnlist_find __P((void *, u_int32_t));
00374  */
00375 int
00376 CDB___db_txnlist_find(listp, txnid)
00377         void *listp;
00378         u_int32_t txnid;
00379 {
00380         DB_TXNHEAD *hp;
00381         DB_TXNLIST *p;
00382 
00383         if (txnid == 0 || (hp = (DB_TXNHEAD *)listp) == NULL)
00384                 return (DB_NOTFOUND);
00385 
00386         for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
00387                 if (p->type != TXNLIST_TXNID)
00388                         continue;
00389                 if (p->u.t.txnid == txnid &&
00390                     hp->generation == p->u.t.generation) {
00391                         /* Move it to head of list. */
00392                         if (p != LIST_FIRST(&hp->head)) {
00393                                 LIST_REMOVE(p, links);
00394                                 LIST_INSERT_HEAD(&hp->head, p, links);
00395                         }
00396                         return (0);
00397                 }
00398         }
00399 
00400         return (DB_NOTFOUND);
00401 }
00402 
00403 /*
00404  * CDB___db_txnlist_gen --
00405  *      Change the current generation number.
00406  *
00407  * PUBLIC: void CDB___db_txnlist_gen __P((void *, int));
00408  */
00409 void
00410 CDB___db_txnlist_gen(listp, incr)
00411         void *listp;
00412         int incr;
00413 {
00414         DB_TXNHEAD *hp;
00415 
00416         /*
00417          * During recovery generation numbers keep track of how many "restart"
00418          * checkpoints we've seen.  Restart checkpoints occur whenever we take
00419          * a checkpoint and there are no outstanding transactions.  When that
00420          * happens, we can reset transaction IDs back to 1.  It always happens
00421          * at recovery and it prevents us from exhausting the transaction IDs
00422          * name space.
00423          */
00424         hp = (DB_TXNHEAD *)listp;
00425         hp->generation += incr;
00426 }
00427 
00428 #ifdef DEBUG
00429 /*
00430  * CDB___db_txnlist_print --
00431  *      Print out the transaction list.
00432  *
00433  * PUBLIC: void CDB___db_txnlist_print __P((void *));
00434  */
00435 void
00436 CDB___db_txnlist_print(listp)
00437         void *listp;
00438 {
00439         DB_TXNHEAD *hp;
00440         DB_TXNLIST *p;
00441 
00442         hp = (DB_TXNHEAD *)listp;
00443 
00444         printf("Maxid: %lu Generation: %lu\n",
00445             (u_long)hp->maxid, (u_long)hp->generation);
00446         for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
00447                 switch (p->type) {
00448                 case TXNLIST_TXNID:
00449                         printf("TXNID: %lu(%lu)\n",
00450                             (u_long)p->u.t.txnid, (u_long)p->u.t.generation);
00451                         break;
00452                 case TXNLIST_DELETE:
00453                         printf("FILE: %s id=%d ops=%d %s %s\n",
00454                             p->u.d.fname, p->u.d.fileid, p->u.d.count,
00455                             F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) ?
00456                             "(deleted)" : "(missing)",
00457                             F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) ?
00458                             "(closed)" : "(open)");
00459 
00460                         break;
00461                 default:
00462                         printf("Unrecognized type: %d\n", p->type);
00463                         break;
00464                 }
00465         }
00466 }
00467 #endif

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