1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)log.c 10.63 (Sleepycat) 10/10/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <shqueue.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <unistd.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "shqueue.h"
1N/A#include "log.h"
1N/A#include "db_dispatch.h"
1N/A#include "txn.h"
1N/A#include "txn_auto.h"
1N/A#include "common_ext.h"
1N/A
1N/Astatic int __log_recover __P((DB_LOG *));
1N/A
1N/A/*
1N/A * log_open --
1N/A * Initialize and/or join a log.
1N/A */
1N/Aint
1N/Alog_open(path, flags, mode, dbenv, lpp)
1N/A const char *path;
1N/A u_int32_t flags;
1N/A int mode;
1N/A DB_ENV *dbenv;
1N/A DB_LOG **lpp;
1N/A{
1N/A DB_LOG *dblp;
1N/A LOG *lp;
1N/A int ret;
1N/A
1N/A /* Validate arguments. */
1N/A#ifdef HAVE_SPINLOCKS
1N/A#define OKFLAGS (DB_CREATE | DB_THREAD)
1N/A#else
1N/A#define OKFLAGS (DB_CREATE)
1N/A#endif
1N/A if ((ret = __db_fchk(dbenv, "log_open", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A
1N/A /* Create and initialize the DB_LOG structure. */
1N/A if ((ret = __os_calloc(1, sizeof(DB_LOG), &dblp)) != 0)
1N/A return (ret);
1N/A
1N/A if (path != NULL && (ret = __os_strdup(path, &dblp->dir)) != 0)
1N/A goto err;
1N/A
1N/A dblp->dbenv = dbenv;
1N/A dblp->lfd = -1;
1N/A ZERO_LSN(dblp->c_lsn);
1N/A dblp->c_fd = -1;
1N/A
1N/A /*
1N/A * The log region isn't fixed size because we store the registered
1N/A * file names there. Make it fairly large so that we don't have to
1N/A * grow it.
1N/A */
1N/A#define DEF_LOG_SIZE (30 * 1024)
1N/A
1N/A /* Map in the region. */
1N/A dblp->reginfo.dbenv = dbenv;
1N/A dblp->reginfo.appname = DB_APP_LOG;
1N/A if (path == NULL)
1N/A dblp->reginfo.path = NULL;
1N/A else
1N/A if ((ret = __os_strdup(path, &dblp->reginfo.path)) != 0)
1N/A goto err;
1N/A dblp->reginfo.file = DB_DEFAULT_LOG_FILE;
1N/A dblp->reginfo.mode = mode;
1N/A dblp->reginfo.size = DEF_LOG_SIZE;
1N/A dblp->reginfo.dbflags = flags;
1N/A dblp->reginfo.flags = REGION_SIZEDEF;
1N/A if ((ret = __db_rattach(&dblp->reginfo)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * The LOG structure is first in the region, the rest of the region
1N/A * is free space.
1N/A */
1N/A dblp->lp = dblp->reginfo.addr;
1N/A dblp->addr = (u_int8_t *)dblp->lp + sizeof(LOG);
1N/A
1N/A /* Initialize a created region. */
1N/A if (F_ISSET(&dblp->reginfo, REGION_CREATED)) {
1N/A __db_shalloc_init(dblp->addr, DEF_LOG_SIZE - sizeof(LOG));
1N/A
1N/A /* Initialize the LOG structure. */
1N/A lp = dblp->lp;
1N/A lp->persist.lg_max = dbenv == NULL ? 0 : dbenv->lg_max;
1N/A if (lp->persist.lg_max == 0)
1N/A lp->persist.lg_max = DEFAULT_MAX;
1N/A lp->persist.magic = DB_LOGMAGIC;
1N/A lp->persist.version = DB_LOGVERSION;
1N/A lp->persist.mode = mode;
1N/A SH_TAILQ_INIT(&lp->fq);
1N/A
1N/A /* Initialize LOG LSNs. */
1N/A lp->lsn.file = 1;
1N/A lp->lsn.offset = 0;
1N/A }
1N/A
1N/A /* Initialize thread information, mutex. */
1N/A if (LF_ISSET(DB_THREAD)) {
1N/A F_SET(dblp, DB_AM_THREAD);
1N/A if ((ret = __db_shalloc(dblp->addr,
1N/A sizeof(db_mutex_t), MUTEX_ALIGNMENT, &dblp->mutexp)) != 0)
1N/A goto err;
1N/A (void)__db_mutex_init(dblp->mutexp, 0);
1N/A }
1N/A
1N/A /*
1N/A * If doing recovery, try and recover any previous log files before
1N/A * releasing the lock.
1N/A */
1N/A if (F_ISSET(&dblp->reginfo, REGION_CREATED) &&
1N/A (ret = __log_recover(dblp)) != 0)
1N/A goto err;
1N/A
1N/A UNLOCK_LOGREGION(dblp);
1N/A *lpp = dblp;
1N/A return (0);
1N/A
1N/Aerr: if (dblp->reginfo.addr != NULL) {
1N/A if (dblp->mutexp != NULL)
1N/A __db_shalloc_free(dblp->addr, dblp->mutexp);
1N/A
1N/A UNLOCK_LOGREGION(dblp);
1N/A (void)__db_rdetach(&dblp->reginfo);
1N/A if (F_ISSET(&dblp->reginfo, REGION_CREATED))
1N/A (void)log_unlink(path, 1, dbenv);
1N/A }
1N/A
1N/A if (dblp->reginfo.path != NULL)
1N/A __os_freestr(dblp->reginfo.path);
1N/A if (dblp->dir != NULL)
1N/A __os_freestr(dblp->dir);
1N/A __os_free(dblp, sizeof(*dblp));
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __log_panic --
1N/A * Panic a log.
1N/A *
1N/A * PUBLIC: void __log_panic __P((DB_ENV *));
1N/A */
1N/Avoid
1N/A__log_panic(dbenv)
1N/A DB_ENV *dbenv;
1N/A{
1N/A if (dbenv->lg_info != NULL)
1N/A dbenv->lg_info->lp->rlayout.panic = 1;
1N/A}
1N/A
1N/A/*
1N/A * __log_recover --
1N/A * Recover a log.
1N/A */
1N/Astatic int
1N/A__log_recover(dblp)
1N/A DB_LOG *dblp;
1N/A{
1N/A DBT dbt;
1N/A DB_LSN lsn;
1N/A LOG *lp;
1N/A u_int32_t chk;
1N/A int cnt, found_checkpoint, ret;
1N/A
1N/A lp = dblp->lp;
1N/A
1N/A /*
1N/A * Find a log file. If none exist, we simply return, leaving
1N/A * everything initialized to a new log.
1N/A */
1N/A if ((ret = __log_find(dblp, 0, &cnt)) != 0)
1N/A return (ret);
1N/A if (cnt == 0)
1N/A return (0);
1N/A
1N/A /*
1N/A * We have the last useful log file and we've loaded any persistent
1N/A * information. Pretend that the log is larger than it can possibly
1N/A * be, and read the last file, looking for the last checkpoint and
1N/A * the log's end.
1N/A */
1N/A lp->lsn.file = cnt + 1;
1N/A lp->lsn.offset = 0;
1N/A lsn.file = cnt;
1N/A lsn.offset = 0;
1N/A
1N/A /* Set the cursor. Shouldn't fail, leave error messages on. */
1N/A memset(&dbt, 0, sizeof(dbt));
1N/A if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * Read to the end of the file, saving checkpoints. This will fail
1N/A * at some point, so turn off error messages.
1N/A */
1N/A found_checkpoint = 0;
1N/A while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 1) == 0) {
1N/A if (dbt.size < sizeof(u_int32_t))
1N/A continue;
1N/A memcpy(&chk, dbt.data, sizeof(u_int32_t));
1N/A if (chk == DB_txn_ckp) {
1N/A lp->chkpt_lsn = lsn;
1N/A found_checkpoint = 1;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * We now know where the end of the log is. Set the first LSN that
1N/A * we want to return to an application and the LSN of the last known
1N/A * record on disk.
1N/A */
1N/A lp->lsn = lp->s_lsn = lsn;
1N/A lp->lsn.offset += dblp->c_len;
1N/A
1N/A /* Set up the current buffer information, too. */
1N/A lp->len = dblp->c_len;
1N/A lp->b_off = 0;
1N/A lp->w_off = lp->lsn.offset;
1N/A
1N/A /*
1N/A * It's possible that we didn't find a checkpoint because there wasn't
1N/A * one in the last log file. Start searching.
1N/A */
1N/A while (!found_checkpoint && cnt > 1) {
1N/A lsn.file = --cnt;
1N/A lsn.offset = 0;
1N/A
1N/A /* Set the cursor. Shouldn't fail, leave error messages on. */
1N/A if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * Read to the end of the file, saving checkpoints. Shouldn't
1N/A * fail, leave error messages on.
1N/A */
1N/A while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 0) == 0) {
1N/A if (dbt.size < sizeof(u_int32_t))
1N/A continue;
1N/A memcpy(&chk, dbt.data, sizeof(u_int32_t));
1N/A if (chk == DB_txn_ckp) {
1N/A lp->chkpt_lsn = lsn;
1N/A found_checkpoint = 1;
1N/A }
1N/A }
1N/A }
1N/A /*
1N/A * Reset the cursor lsn to the beginning of the log, so that an
1N/A * initial call to DB_NEXT does the right thing.
1N/A */
1N/A ZERO_LSN(dblp->c_lsn);
1N/A
1N/A /* If we never find a checkpoint, that's okay, just 0 it out. */
1N/A if (!found_checkpoint)
1N/A ZERO_LSN(lp->chkpt_lsn);
1N/A
1N/A /*
1N/A * !!!
1N/A * The test suite explicitly looks for this string -- don't change
1N/A * it here unless you also change it there.
1N/A */
1N/A __db_err(dblp->dbenv,
1N/A "Finding last valid log LSN: file: %lu offset %lu",
1N/A (u_long)lp->lsn.file, (u_long)lp->lsn.offset);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __log_find --
1N/A * Try to find a log file. If find_first is set, valp will contain
1N/A * the number of the first log file, else it will contain the number of
1N/A * the last log file.
1N/A *
1N/A * PUBLIC: int __log_find __P((DB_LOG *, int, int *));
1N/A */
1N/Aint
1N/A__log_find(dblp, find_first, valp)
1N/A DB_LOG *dblp;
1N/A int find_first, *valp;
1N/A{
1N/A u_int32_t clv, logval;
1N/A int cnt, fcnt, ret;
1N/A const char *dir;
1N/A char **names, *p, *q;
1N/A
1N/A *valp = 0;
1N/A
1N/A /* Find the directory name. */
1N/A if ((ret = __log_name(dblp, 1, &p, NULL, 0)) != 0)
1N/A return (ret);
1N/A if ((q = __db_rpath(p)) == NULL)
1N/A dir = PATH_DOT;
1N/A else {
1N/A *q = '\0';
1N/A dir = p;
1N/A }
1N/A
1N/A /* Get the list of file names. */
1N/A ret = __os_dirlist(dir, &names, &fcnt);
1N/A __os_freestr(p);
1N/A if (ret != 0) {
1N/A __db_err(dblp->dbenv, "%s: %s", dir, strerror(ret));
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Search for a valid log file name, return a value of 0 on
1N/A * failure.
1N/A *
1N/A * XXX
1N/A * Assumes that atoi(3) returns a 32-bit number.
1N/A */
1N/A for (cnt = fcnt, clv = logval = 0; --cnt >= 0;) {
1N/A if (strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1) != 0)
1N/A continue;
1N/A
1N/A clv = atoi(names[cnt] + (sizeof(LFPREFIX) - 1));
1N/A if (find_first) {
1N/A if (logval != 0 && clv > logval)
1N/A continue;
1N/A } else
1N/A if (logval != 0 && clv < logval)
1N/A continue;
1N/A
1N/A if (__log_valid(dblp, clv, 1) == 0)
1N/A logval = clv;
1N/A }
1N/A
1N/A *valp = logval;
1N/A
1N/A /* Discard the list. */
1N/A __os_dirfree(names, fcnt);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * log_valid --
1N/A * Validate a log file.
1N/A *
1N/A * PUBLIC: int __log_valid __P((DB_LOG *, u_int32_t, int));
1N/A */
1N/Aint
1N/A__log_valid(dblp, number, set_persist)
1N/A DB_LOG *dblp;
1N/A u_int32_t number;
1N/A int set_persist;
1N/A{
1N/A LOGP persist;
1N/A ssize_t nw;
1N/A char *fname;
1N/A int fd, ret;
1N/A
1N/A /* Try to open the log file. */
1N/A if ((ret = __log_name(dblp,
1N/A number, &fname, &fd, DB_RDONLY | DB_SEQUENTIAL)) != 0) {
1N/A __os_freestr(fname);
1N/A return (ret);
1N/A }
1N/A
1N/A /* Try to read the header. */
1N/A if ((ret = __os_seek(fd, 0, 0, sizeof(HDR), 0, SEEK_SET)) != 0 ||
1N/A (ret = __os_read(fd, &persist, sizeof(LOGP), &nw)) != 0 ||
1N/A nw != sizeof(LOGP)) {
1N/A if (ret == 0)
1N/A ret = EIO;
1N/A
1N/A (void)__os_close(fd);
1N/A
1N/A __db_err(dblp->dbenv,
1N/A "Ignoring log file: %s: %s", fname, strerror(ret));
1N/A goto err;
1N/A }
1N/A (void)__os_close(fd);
1N/A
1N/A /* Validate the header. */
1N/A if (persist.magic != DB_LOGMAGIC) {
1N/A __db_err(dblp->dbenv,
1N/A "Ignoring log file: %s: magic number %lx, not %lx",
1N/A fname, (u_long)persist.magic, (u_long)DB_LOGMAGIC);
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A if (persist.version < DB_LOGOLDVER || persist.version > DB_LOGVERSION) {
1N/A __db_err(dblp->dbenv,
1N/A "Ignoring log file: %s: unsupported log version %lu",
1N/A fname, (u_long)persist.version);
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * If we're going to use this log file, set the region's persistent
1N/A * information based on the headers.
1N/A */
1N/A if (set_persist) {
1N/A dblp->lp->persist.lg_max = persist.lg_max;
1N/A dblp->lp->persist.mode = persist.mode;
1N/A }
1N/A ret = 0;
1N/A
1N/Aerr: __os_freestr(fname);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * log_close --
1N/A * Close a log.
1N/A */
1N/Aint
1N/Alog_close(dblp)
1N/A DB_LOG *dblp;
1N/A{
1N/A u_int32_t i;
1N/A int ret, t_ret;
1N/A
1N/A LOG_PANIC_CHECK(dblp);
1N/A
1N/A /* We may have opened files as part of XA; if so, close them. */
1N/A __log_close_files(dblp);
1N/A
1N/A /* Discard the per-thread pointer. */
1N/A if (dblp->mutexp != NULL) {
1N/A LOCK_LOGREGION(dblp);
1N/A __db_shalloc_free(dblp->addr, dblp->mutexp);
1N/A UNLOCK_LOGREGION(dblp);
1N/A }
1N/A
1N/A /* Close the region. */
1N/A ret = __db_rdetach(&dblp->reginfo);
1N/A
1N/A /* Close open files, release allocated memory. */
1N/A if (dblp->lfd != -1 && (t_ret = __os_close(dblp->lfd)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A if (dblp->c_dbt.data != NULL)
1N/A __os_free(dblp->c_dbt.data, dblp->c_dbt.ulen);
1N/A if (dblp->c_fd != -1 &&
1N/A (t_ret = __os_close(dblp->c_fd)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A if (dblp->dbentry != NULL) {
1N/A for (i = 0; i < dblp->dbentry_cnt; i++)
1N/A if (dblp->dbentry[i].name != NULL)
1N/A __os_freestr(dblp->dbentry[i].name);
1N/A __os_free(dblp->dbentry,
1N/A (dblp->dbentry_cnt * sizeof(DB_ENTRY)));
1N/A }
1N/A
1N/A if (dblp->dir != NULL)
1N/A __os_freestr(dblp->dir);
1N/A
1N/A if (dblp->reginfo.path != NULL)
1N/A __os_freestr(dblp->reginfo.path);
1N/A __os_free(dblp, sizeof(*dblp));
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * log_unlink --
1N/A * Exit a log.
1N/A */
1N/Aint
1N/Alog_unlink(path, force, dbenv)
1N/A const char *path;
1N/A int force;
1N/A DB_ENV *dbenv;
1N/A{
1N/A REGINFO reginfo;
1N/A int ret;
1N/A
1N/A memset(&reginfo, 0, sizeof(reginfo));
1N/A reginfo.dbenv = dbenv;
1N/A reginfo.appname = DB_APP_LOG;
1N/A if (path != NULL && (ret = __os_strdup(path, &reginfo.path)) != 0)
1N/A return (ret);
1N/A reginfo.file = DB_DEFAULT_LOG_FILE;
1N/A ret = __db_runlink(&reginfo, force);
1N/A if (reginfo.path != NULL)
1N/A __os_freestr(reginfo.path);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * log_stat --
1N/A * Return LOG statistics.
1N/A */
1N/Aint
1N/Alog_stat(dblp, gspp, db_malloc)
1N/A DB_LOG *dblp;
1N/A DB_LOG_STAT **gspp;
1N/A void *(*db_malloc) __P((size_t));
1N/A{
1N/A LOG *lp;
1N/A int ret;
1N/A
1N/A *gspp = NULL;
1N/A lp = dblp->lp;
1N/A
1N/A LOG_PANIC_CHECK(dblp);
1N/A
1N/A if ((ret = __os_malloc(sizeof(**gspp), db_malloc, gspp)) != 0)
1N/A return (ret);
1N/A
1N/A /* Copy out the global statistics. */
1N/A LOCK_LOGREGION(dblp);
1N/A **gspp = lp->stat;
1N/A
1N/A (*gspp)->st_magic = lp->persist.magic;
1N/A (*gspp)->st_version = lp->persist.version;
1N/A (*gspp)->st_mode = lp->persist.mode;
1N/A (*gspp)->st_lg_max = lp->persist.lg_max;
1N/A
1N/A (*gspp)->st_region_nowait = lp->rlayout.lock.mutex_set_nowait;
1N/A (*gspp)->st_region_wait = lp->rlayout.lock.mutex_set_wait;
1N/A
1N/A (*gspp)->st_cur_file = lp->lsn.file;
1N/A (*gspp)->st_cur_offset = lp->lsn.offset;
1N/A
1N/A (*gspp)->st_refcnt = lp->rlayout.refcnt;
1N/A (*gspp)->st_regsize = lp->rlayout.size;
1N/A
1N/A UNLOCK_LOGREGION(dblp);
1N/A
1N/A return (0);
1N/A}