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[] = "@(#)mp_bh.c 10.45 (Sleepycat) 11/25/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 <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 "db_shash.h"
1N/A#include "mp.h"
1N/A#include "common_ext.h"
1N/A
1N/Astatic int __memp_upgrade __P((DB_MPOOL *, DB_MPOOLFILE *, MPOOLFILE *));
1N/A
1N/A/*
1N/A * __memp_bhwrite --
1N/A * Write the page associated with a given bucket header.
1N/A *
1N/A * PUBLIC: int __memp_bhwrite
1N/A * PUBLIC: __P((DB_MPOOL *, MPOOLFILE *, BH *, int *, int *));
1N/A */
1N/Aint
1N/A__memp_bhwrite(dbmp, mfp, bhp, restartp, wrotep)
1N/A DB_MPOOL *dbmp;
1N/A MPOOLFILE *mfp;
1N/A BH *bhp;
1N/A int *restartp, *wrotep;
1N/A{
1N/A DB_MPOOLFILE *dbmfp;
1N/A DB_MPREG *mpreg;
1N/A int incremented, ret;
1N/A
1N/A if (restartp != NULL)
1N/A *restartp = 0;
1N/A if (wrotep != NULL)
1N/A *wrotep = 0;
1N/A incremented = 0;
1N/A
1N/A /*
1N/A * Walk the process' DB_MPOOLFILE list and find a file descriptor for
1N/A * the file. We also check that the descriptor is open for writing.
1N/A * If we find a descriptor on the file that's not open for writing, we
1N/A * try and upgrade it to make it writeable. If that fails, we're done.
1N/A */
1N/A LOCKHANDLE(dbmp, dbmp->mutexp);
1N/A for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
1N/A dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q))
1N/A if (dbmfp->mfp == mfp) {
1N/A if (F_ISSET(dbmfp, MP_READONLY) &&
1N/A __memp_upgrade(dbmp, dbmfp, mfp)) {
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * Increment the reference count -- see the comment in
1N/A * memp_fclose().
1N/A */
1N/A ++dbmfp->ref;
1N/A incremented = 1;
1N/A break;
1N/A }
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A if (dbmfp != NULL)
1N/A goto found;
1N/A
1N/A /*
1N/A * It's not a page from a file we've opened. If the file requires
1N/A * input/output processing, see if this process has ever registered
1N/A * information as to how to write this type of file. If not, there's
1N/A * nothing we can do.
1N/A */
1N/A if (mfp->ftype != 0) {
1N/A LOCKHANDLE(dbmp, dbmp->mutexp);
1N/A for (mpreg = LIST_FIRST(&dbmp->dbregq);
1N/A mpreg != NULL; mpreg = LIST_NEXT(mpreg, q))
1N/A if (mpreg->ftype == mfp->ftype)
1N/A break;
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A if (mpreg == NULL)
1N/A return (0);
1N/A }
1N/A
1N/A /*
1N/A * Try and open the file, attaching to the underlying shared area.
1N/A *
1N/A * XXX
1N/A * Don't try to attach to temporary files. There are two problems in
1N/A * trying to do that. First, if we have different privileges than the
1N/A * process that "owns" the temporary file, we might create the backing
1N/A * disk file such that the owning process couldn't read/write its own
1N/A * buffers, e.g., memp_trickle() running as root creating a file owned
1N/A * as root, mode 600. Second, if the temporary file has already been
1N/A * created, we don't have any way of finding out what its real name is,
1N/A * and, even if we did, it was already unlinked (so that it won't be
1N/A * left if the process dies horribly). This decision causes a problem,
1N/A * however: if the temporary file consumes the entire buffer cache,
1N/A * and the owner doesn't flush the buffers to disk, we could end up
1N/A * with resource starvation, and the memp_trickle() thread couldn't do
1N/A * anything about it. That's a pretty unlikely scenario, though.
1N/A *
1N/A * XXX
1N/A * There's no negative cache, so we may repeatedly try and open files
1N/A * that we have previously tried (and failed) to open.
1N/A *
1N/A * Ignore any error, assume it's a permissions problem.
1N/A */
1N/A if (F_ISSET(mfp, MP_TEMP))
1N/A return (0);
1N/A
1N/A if (__memp_fopen(dbmp, mfp, R_ADDR(dbmp, mfp->path_off),
1N/A 0, 0, mfp->stat.st_pagesize, 0, NULL, &dbmfp) != 0)
1N/A return (0);
1N/A
1N/Afound: ret = __memp_pgwrite(dbmfp, bhp, restartp, wrotep);
1N/A
1N/A if (incremented) {
1N/A LOCKHANDLE(dbmp, dbmp->mutexp);
1N/A --dbmfp->ref;
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A }
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __memp_pgread --
1N/A * Read a page from a file.
1N/A *
1N/A * PUBLIC: int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
1N/A */
1N/Aint
1N/A__memp_pgread(dbmfp, bhp, can_create)
1N/A DB_MPOOLFILE *dbmfp;
1N/A BH *bhp;
1N/A int can_create;
1N/A{
1N/A DB_IO db_io;
1N/A DB_MPOOL *dbmp;
1N/A MPOOLFILE *mfp;
1N/A size_t len, pagesize;
1N/A ssize_t nr;
1N/A int created, ret;
1N/A
1N/A dbmp = dbmfp->dbmp;
1N/A mfp = dbmfp->mfp;
1N/A pagesize = mfp->stat.st_pagesize;
1N/A
1N/A F_SET(bhp, BH_LOCKED | BH_TRASH);
1N/A LOCKBUFFER(dbmp, bhp);
1N/A UNLOCKREGION(dbmp);
1N/A
1N/A /*
1N/A * Temporary files may not yet have been created. We don't create
1N/A * them now, we create them when the pages have to be flushed.
1N/A */
1N/A nr = 0;
1N/A if (dbmfp->fd == -1)
1N/A ret = 0;
1N/A else {
1N/A /*
1N/A * Ignore read errors if we have permission to create the page.
1N/A * Assume that the page doesn't exist, and that we'll create it
1N/A * when we write it out.
1N/A */
1N/A db_io.fd_io = dbmfp->fd;
1N/A db_io.fd_lock = dbmp->reginfo.fd;
1N/A db_io.mutexp =
1N/A F_ISSET(dbmp, MP_LOCKHANDLE) ? dbmfp->mutexp : NULL;
1N/A db_io.pagesize = db_io.bytes = pagesize;
1N/A db_io.pgno = bhp->pgno;
1N/A db_io.buf = bhp->buf;
1N/A
1N/A ret = __os_io(&db_io, DB_IO_READ, &nr);
1N/A }
1N/A
1N/A created = 0;
1N/A if (nr < (ssize_t)pagesize)
1N/A if (can_create)
1N/A created = 1;
1N/A else {
1N/A /* If we had a short read, ret may be 0. */
1N/A if (ret == 0)
1N/A ret = EIO;
1N/A __db_err(dbmp->dbenv,
1N/A "%s: page %lu doesn't exist, create flag not set",
1N/A __memp_fn(dbmfp), (u_long)bhp->pgno);
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * Clear any bytes we didn't read that need to be cleared. If we're
1N/A * running in diagnostic mode, smash any bytes on the page that are
1N/A * unknown quantities for the caller.
1N/A */
1N/A if (nr != (ssize_t)pagesize) {
1N/A len = mfp->clear_len == 0 ? pagesize : mfp->clear_len;
1N/A if (nr < (ssize_t)len)
1N/A memset(bhp->buf + nr, 0, len - nr);
1N/A#ifdef DIAGNOSTIC
1N/A if (nr > (ssize_t)len)
1N/A len = nr;
1N/A if (len < pagesize)
1N/A memset(bhp->buf + len, 0xdb, pagesize - len);
1N/A#endif
1N/A }
1N/A
1N/A /* Call any pgin function. */
1N/A ret = mfp->ftype == 0 ? 0 : __memp_pg(dbmfp, bhp, 1);
1N/A
1N/A /* Unlock the buffer and reacquire the region lock. */
1N/Aerr: UNLOCKBUFFER(dbmp, bhp);
1N/A LOCKREGION(dbmp);
1N/A
1N/A /*
1N/A * If no errors occurred, the data is now valid, clear the BH_TRASH
1N/A * flag; regardless, clear the lock bit and let other threads proceed.
1N/A */
1N/A F_CLR(bhp, BH_LOCKED);
1N/A if (ret == 0) {
1N/A F_CLR(bhp, BH_TRASH);
1N/A
1N/A /* Update the statistics. */
1N/A if (created) {
1N/A ++dbmp->mp->stat.st_page_create;
1N/A ++mfp->stat.st_page_create;
1N/A } else {
1N/A ++dbmp->mp->stat.st_page_in;
1N/A ++mfp->stat.st_page_in;
1N/A }
1N/A }
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __memp_pgwrite --
1N/A * Write a page to a file.
1N/A *
1N/A * PUBLIC: int __memp_pgwrite __P((DB_MPOOLFILE *, BH *, int *, int *));
1N/A */
1N/Aint
1N/A__memp_pgwrite(dbmfp, bhp, restartp, wrotep)
1N/A DB_MPOOLFILE *dbmfp;
1N/A BH *bhp;
1N/A int *restartp, *wrotep;
1N/A{
1N/A DB_ENV *dbenv;
1N/A DB_IO db_io;
1N/A DB_LOG *lg_info;
1N/A DB_LSN lsn;
1N/A DB_MPOOL *dbmp;
1N/A MPOOL *mp;
1N/A MPOOLFILE *mfp;
1N/A ssize_t nw;
1N/A int callpgin, dosync, ret, syncfail;
1N/A const char *fail;
1N/A
1N/A dbmp = dbmfp->dbmp;
1N/A dbenv = dbmp->dbenv;
1N/A mp = dbmp->mp;
1N/A mfp = dbmfp->mfp;
1N/A
1N/A if (restartp != NULL)
1N/A *restartp = 0;
1N/A if (wrotep != NULL)
1N/A *wrotep = 0;
1N/A callpgin = 0;
1N/A
1N/A /*
1N/A * Check the dirty bit -- this buffer may have been written since we
1N/A * decided to write it.
1N/A */
1N/A if (!F_ISSET(bhp, BH_DIRTY)) {
1N/A if (wrotep != NULL)
1N/A *wrotep = 1;
1N/A return (0);
1N/A }
1N/A
1N/A LOCKBUFFER(dbmp, bhp);
1N/A
1N/A /*
1N/A * If there were two writers, we may have just been waiting while the
1N/A * other writer completed I/O on this buffer. Check the dirty bit one
1N/A * more time.
1N/A */
1N/A if (!F_ISSET(bhp, BH_DIRTY)) {
1N/A UNLOCKBUFFER(dbmp, bhp);
1N/A
1N/A if (wrotep != NULL)
1N/A *wrotep = 1;
1N/A return (0);
1N/A }
1N/A
1N/A F_SET(bhp, BH_LOCKED);
1N/A UNLOCKREGION(dbmp);
1N/A
1N/A if (restartp != NULL)
1N/A *restartp = 1;
1N/A
1N/A /* Copy the LSN off the page if we're going to need it. */
1N/A lg_info = dbenv->lg_info;
1N/A if (lg_info != NULL || F_ISSET(bhp, BH_WRITE))
1N/A memcpy(&lsn, bhp->buf + mfp->lsn_off, sizeof(DB_LSN));
1N/A
1N/A /* Ensure the appropriate log records are on disk. */
1N/A if (lg_info != NULL && (ret = log_flush(lg_info, &lsn)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Call any pgout function. We set the callpgin flag so that we flag
1N/A * that the contents of the buffer will need to be passed through pgin
1N/A * before they are reused.
1N/A */
1N/A if (mfp->ftype == 0)
1N/A ret = 0;
1N/A else {
1N/A callpgin = 1;
1N/A if ((ret = __memp_pg(dbmfp, bhp, 0)) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A /* Temporary files may not yet have been created. */
1N/A if (dbmfp->fd == -1) {
1N/A LOCKHANDLE(dbmp, dbmfp->mutexp);
1N/A if (dbmfp->fd == -1 && ((ret = __db_appname(dbenv,
1N/A DB_APP_TMP, NULL, NULL, DB_CREATE | DB_EXCL | DB_TEMPORARY,
1N/A &dbmfp->fd, NULL)) != 0 || dbmfp->fd == -1)) {
1N/A UNLOCKHANDLE(dbmp, dbmfp->mutexp);
1N/A __db_err(dbenv,
1N/A "unable to create temporary backing file");
1N/A goto err;
1N/A }
1N/A UNLOCKHANDLE(dbmp, dbmfp->mutexp);
1N/A }
1N/A
1N/A /* Write the page. */
1N/A db_io.fd_io = dbmfp->fd;
1N/A db_io.fd_lock = dbmp->reginfo.fd;
1N/A db_io.mutexp = F_ISSET(dbmp, MP_LOCKHANDLE) ? dbmfp->mutexp : NULL;
1N/A db_io.pagesize = db_io.bytes = mfp->stat.st_pagesize;
1N/A db_io.pgno = bhp->pgno;
1N/A db_io.buf = bhp->buf;
1N/A if ((ret = __os_io(&db_io, DB_IO_WRITE, &nw)) != 0) {
1N/A __db_panic(dbenv, ret);
1N/A fail = "write";
1N/A goto syserr;
1N/A }
1N/A if (nw != (ssize_t)mfp->stat.st_pagesize) {
1N/A ret = EIO;
1N/A fail = "write";
1N/A goto syserr;
1N/A }
1N/A
1N/A if (wrotep != NULL)
1N/A *wrotep = 1;
1N/A
1N/A /* Unlock the buffer and reacquire the region lock. */
1N/A UNLOCKBUFFER(dbmp, bhp);
1N/A LOCKREGION(dbmp);
1N/A
1N/A /*
1N/A * Clean up the flags based on a successful write.
1N/A *
1N/A * If we rewrote the page, it will need processing by the pgin
1N/A * routine before reuse.
1N/A */
1N/A if (callpgin)
1N/A F_SET(bhp, BH_CALLPGIN);
1N/A F_CLR(bhp, BH_DIRTY | BH_LOCKED);
1N/A
1N/A /*
1N/A * If we write a buffer for which a checkpoint is waiting, update
1N/A * the count of pending buffers (both in the mpool as a whole and
1N/A * for this file). If the count for this file goes to zero, set a
1N/A * flag so we flush the writes.
1N/A */
1N/A if (F_ISSET(bhp, BH_WRITE)) {
1N/A F_CLR(bhp, BH_WRITE);
1N/A
1N/A --mp->lsn_cnt;
1N/A dosync = --mfp->lsn_cnt == 0 ? 1 : 0;
1N/A } else
1N/A dosync = 0;
1N/A
1N/A /* Update the page clean/dirty statistics. */
1N/A ++mp->stat.st_page_clean;
1N/A --mp->stat.st_page_dirty;
1N/A
1N/A /* Update I/O statistics. */
1N/A ++mp->stat.st_page_out;
1N/A ++mfp->stat.st_page_out;
1N/A
1N/A /*
1N/A * Do the sync after everything else has been updated, so any incoming
1N/A * checkpoint doesn't see inconsistent information.
1N/A *
1N/A * XXX:
1N/A * Don't lock the region around the sync, fsync(2) has no atomicity
1N/A * issues.
1N/A *
1N/A * XXX:
1N/A * We ignore errors from the sync -- it makes no sense to return an
1N/A * error to the calling process, so set a flag causing the checkpoint
1N/A * to be retried later. There is a possibility, of course, that a
1N/A * subsequent checkpoint was started and that we're going to force it
1N/A * to fail. That should be unlikely, and fixing it would be difficult.
1N/A */
1N/A if (dosync) {
1N/A UNLOCKREGION(dbmp);
1N/A syncfail = __os_fsync(dbmfp->fd) != 0;
1N/A LOCKREGION(dbmp);
1N/A if (syncfail)
1N/A F_SET(mp, MP_LSN_RETRY);
1N/A }
1N/A
1N/A return (0);
1N/A
1N/Asyserr: __db_err(dbenv, "%s: %s failed for page %lu",
1N/A __memp_fn(dbmfp), fail, (u_long)bhp->pgno);
1N/A
1N/Aerr: /* Unlock the buffer and reacquire the region lock. */
1N/A UNLOCKBUFFER(dbmp, bhp);
1N/A LOCKREGION(dbmp);
1N/A
1N/A /*
1N/A * Clean up the flags based on a failure.
1N/A *
1N/A * The page remains dirty but we remove our lock. If we rewrote the
1N/A * page, it will need processing by the pgin routine before reuse.
1N/A */
1N/A if (callpgin)
1N/A F_SET(bhp, BH_CALLPGIN);
1N/A F_CLR(bhp, BH_LOCKED);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __memp_pg --
1N/A * Call the pgin/pgout routine.
1N/A *
1N/A * PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
1N/A */
1N/Aint
1N/A__memp_pg(dbmfp, bhp, is_pgin)
1N/A DB_MPOOLFILE *dbmfp;
1N/A BH *bhp;
1N/A int is_pgin;
1N/A{
1N/A DBT dbt, *dbtp;
1N/A DB_MPOOL *dbmp;
1N/A DB_MPREG *mpreg;
1N/A MPOOLFILE *mfp;
1N/A int ftype, ret;
1N/A
1N/A dbmp = dbmfp->dbmp;
1N/A mfp = dbmfp->mfp;
1N/A
1N/A LOCKHANDLE(dbmp, dbmp->mutexp);
1N/A
1N/A ftype = mfp->ftype;
1N/A for (mpreg = LIST_FIRST(&dbmp->dbregq);
1N/A mpreg != NULL; mpreg = LIST_NEXT(mpreg, q)) {
1N/A if (ftype != mpreg->ftype)
1N/A continue;
1N/A if (mfp->pgcookie_len == 0)
1N/A dbtp = NULL;
1N/A else {
1N/A dbt.size = mfp->pgcookie_len;
1N/A dbt.data = R_ADDR(dbmp, mfp->pgcookie_off);
1N/A dbtp = &dbt;
1N/A }
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A
1N/A if (is_pgin) {
1N/A if (mpreg->pgin != NULL && (ret =
1N/A mpreg->pgin(bhp->pgno, bhp->buf, dbtp)) != 0)
1N/A goto err;
1N/A } else
1N/A if (mpreg->pgout != NULL && (ret =
1N/A mpreg->pgout(bhp->pgno, bhp->buf, dbtp)) != 0)
1N/A goto err;
1N/A break;
1N/A }
1N/A
1N/A if (mpreg == NULL)
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A
1N/A return (0);
1N/A
1N/Aerr: UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A __db_err(dbmp->dbenv, "%s: %s failed for page %lu",
1N/A __memp_fn(dbmfp), is_pgin ? "pgin" : "pgout", (u_long)bhp->pgno);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __memp_bhfree --
1N/A * Free a bucket header and its referenced data.
1N/A *
1N/A * PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
1N/A */
1N/Avoid
1N/A__memp_bhfree(dbmp, mfp, bhp, free_mem)
1N/A DB_MPOOL *dbmp;
1N/A MPOOLFILE *mfp;
1N/A BH *bhp;
1N/A int free_mem;
1N/A{
1N/A size_t off;
1N/A
1N/A /* Delete the buffer header from the hash bucket queue. */
1N/A off = BUCKET(dbmp->mp, R_OFFSET(dbmp, mfp), bhp->pgno);
1N/A SH_TAILQ_REMOVE(&dbmp->htab[off], bhp, hq, __bh);
1N/A
1N/A /* Delete the buffer header from the LRU queue. */
1N/A SH_TAILQ_REMOVE(&dbmp->mp->bhq, bhp, q, __bh);
1N/A
1N/A /*
1N/A * If we're not reusing it immediately, free the buffer header
1N/A * and data for real.
1N/A */
1N/A if (free_mem) {
1N/A __db_shalloc_free(dbmp->addr, bhp);
1N/A --dbmp->mp->stat.st_page_clean;
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * __memp_upgrade --
1N/A * Upgrade a file descriptor from readonly to readwrite.
1N/A */
1N/Astatic int
1N/A__memp_upgrade(dbmp, dbmfp, mfp)
1N/A DB_MPOOL *dbmp;
1N/A DB_MPOOLFILE *dbmfp;
1N/A MPOOLFILE *mfp;
1N/A{
1N/A int fd, ret;
1N/A char *rpath;
1N/A
1N/A /*
1N/A * !!!
1N/A * We expect the handle to already be locked.
1N/A */
1N/A
1N/A /* Check to see if we've already upgraded. */
1N/A if (F_ISSET(dbmfp, MP_UPGRADE))
1N/A return (0);
1N/A
1N/A /* Check to see if we've already failed. */
1N/A if (F_ISSET(dbmfp, MP_UPGRADE_FAIL))
1N/A return (1);
1N/A
1N/A /*
1N/A * Calculate the real name for this file and try to open it read/write.
1N/A * We know we have a valid pathname for the file because it's the only
1N/A * way we could have gotten a file descriptor of any kind.
1N/A */
1N/A if ((ret = __db_appname(dbmp->dbenv, DB_APP_DATA,
1N/A NULL, R_ADDR(dbmp, mfp->path_off), 0, NULL, &rpath)) != 0)
1N/A return (ret);
1N/A if (__db_open(rpath, 0, 0, 0, &fd) != 0) {
1N/A F_SET(dbmfp, MP_UPGRADE_FAIL);
1N/A ret = 1;
1N/A } else {
1N/A /* Swap the descriptors and set the upgrade flag. */
1N/A (void)__os_close(dbmfp->fd);
1N/A dbmfp->fd = fd;
1N/A F_SET(dbmfp, MP_UPGRADE);
1N/A ret = 0;
1N/A }
1N/A __os_freestr(rpath);
1N/A return (ret);
1N/A}