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_open.c 10.27 (Sleepycat) 10/1/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#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/A/*
1N/A * memp_open --
1N/A * Initialize and/or join a memory pool.
1N/A */
1N/Aint
1N/Amemp_open(path, flags, mode, dbenv, retp)
1N/A const char *path;
1N/A u_int32_t flags;
1N/A int mode;
1N/A DB_ENV *dbenv;
1N/A DB_MPOOL **retp;
1N/A{
1N/A DB_MPOOL *dbmp;
1N/A size_t cachesize;
1N/A int is_private, ret;
1N/A
1N/A /* Validate arguments. */
1N/A#ifdef HAVE_SPINLOCKS
1N/A#define OKFLAGS (DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP | DB_THREAD)
1N/A#else
1N/A#define OKFLAGS (DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP)
1N/A#endif
1N/A if ((ret = __db_fchk(dbenv, "memp_open", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A
1N/A /* Extract fields from DB_ENV structure. */
1N/A cachesize = dbenv == NULL ? 0 : dbenv->mp_size;
1N/A
1N/A /* Create and initialize the DB_MPOOL structure. */
1N/A if ((ret = __os_calloc(1, sizeof(DB_MPOOL), &dbmp)) != 0)
1N/A return (ret);
1N/A LIST_INIT(&dbmp->dbregq);
1N/A TAILQ_INIT(&dbmp->dbmfq);
1N/A
1N/A dbmp->dbenv = dbenv;
1N/A
1N/A /* Decide if it's possible for anyone else to access the pool. */
1N/A is_private =
1N/A (dbenv == NULL && path == NULL) || LF_ISSET(DB_MPOOL_PRIVATE);
1N/A
1N/A /*
1N/A * Map in the region. We do locking regardless, as portions of it are
1N/A * implemented in common code (if we put the region in a file, that is).
1N/A */
1N/A F_SET(dbmp, MP_LOCKREGION);
1N/A if ((ret = __memp_ropen(dbmp,
1N/A path, cachesize, mode, is_private, LF_ISSET(DB_CREATE))) != 0)
1N/A goto err;
1N/A F_CLR(dbmp, MP_LOCKREGION);
1N/A
1N/A /*
1N/A * If there's concurrent access, then we have to lock the region.
1N/A * If it's threaded, then we have to lock both the handles and the
1N/A * region, and we need to allocate a mutex for that purpose.
1N/A */
1N/A if (!is_private)
1N/A F_SET(dbmp, MP_LOCKREGION);
1N/A if (LF_ISSET(DB_THREAD)) {
1N/A F_SET(dbmp, MP_LOCKHANDLE | MP_LOCKREGION);
1N/A LOCKREGION(dbmp);
1N/A ret = __memp_alloc(dbmp,
1N/A sizeof(db_mutex_t), NULL, &dbmp->mutexp);
1N/A UNLOCKREGION(dbmp);
1N/A if (ret != 0) {
1N/A (void)memp_close(dbmp);
1N/A goto err;
1N/A }
1N/A LOCKINIT(dbmp, dbmp->mutexp);
1N/A }
1N/A
1N/A *retp = dbmp;
1N/A return (0);
1N/A
1N/Aerr: if (dbmp != NULL)
1N/A __os_free(dbmp, sizeof(DB_MPOOL));
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * memp_close --
1N/A * Close a memory pool.
1N/A */
1N/Aint
1N/Amemp_close(dbmp)
1N/A DB_MPOOL *dbmp;
1N/A{
1N/A DB_MPOOLFILE *dbmfp;
1N/A DB_MPREG *mpreg;
1N/A int ret, t_ret;
1N/A
1N/A ret = 0;
1N/A
1N/A MP_PANIC_CHECK(dbmp);
1N/A
1N/A /* Discard DB_MPREGs. */
1N/A while ((mpreg = LIST_FIRST(&dbmp->dbregq)) != NULL) {
1N/A LIST_REMOVE(mpreg, q);
1N/A __os_free(mpreg, sizeof(DB_MPREG));
1N/A }
1N/A
1N/A /* Discard DB_MPOOLFILEs. */
1N/A while ((dbmfp = TAILQ_FIRST(&dbmp->dbmfq)) != NULL)
1N/A if ((t_ret = memp_fclose(dbmfp)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* Discard thread mutex. */
1N/A if (F_ISSET(dbmp, MP_LOCKHANDLE)) {
1N/A LOCKREGION(dbmp);
1N/A __db_shalloc_free(dbmp->addr, dbmp->mutexp);
1N/A UNLOCKREGION(dbmp);
1N/A }
1N/A
1N/A /* Close the region. */
1N/A if ((t_ret = __db_rdetach(&dbmp->reginfo)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A if (dbmp->reginfo.path != NULL)
1N/A __os_freestr(dbmp->reginfo.path);
1N/A __os_free(dbmp, sizeof(DB_MPOOL));
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __memp_panic --
1N/A * Panic a memory pool.
1N/A *
1N/A * PUBLIC: void __memp_panic __P((DB_ENV *));
1N/A */
1N/Avoid
1N/A__memp_panic(dbenv)
1N/A DB_ENV *dbenv;
1N/A{
1N/A if (dbenv->mp_info != NULL)
1N/A dbenv->mp_info->mp->rlayout.panic = 1;
1N/A}
1N/A
1N/A/*
1N/A * memp_unlink --
1N/A * Exit a memory pool.
1N/A */
1N/Aint
1N/Amemp_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_NONE;
1N/A if (path != NULL && (ret = __os_strdup(path, &reginfo.path)) != 0)
1N/A return (ret);
1N/A reginfo.file = DB_DEFAULT_MPOOL_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 * memp_register --
1N/A * Register a file type's pgin, pgout routines.
1N/A */
1N/Aint
1N/Amemp_register(dbmp, ftype, pgin, pgout)
1N/A DB_MPOOL *dbmp;
1N/A int ftype;
1N/A int (*pgin) __P((db_pgno_t, void *, DBT *));
1N/A int (*pgout) __P((db_pgno_t, void *, DBT *));
1N/A{
1N/A DB_MPREG *mpr;
1N/A int ret;
1N/A
1N/A MP_PANIC_CHECK(dbmp);
1N/A
1N/A if ((ret = __os_malloc(sizeof(DB_MPREG), NULL, &mpr)) != 0)
1N/A return (ret);
1N/A
1N/A mpr->ftype = ftype;
1N/A mpr->pgin = pgin;
1N/A mpr->pgout = pgout;
1N/A
1N/A /*
1N/A * Insert at the head. Because we do a linear walk, we'll find
1N/A * the most recent registry in the case of multiple entries, so
1N/A * we don't have to check for multiple registries.
1N/A */
1N/A LOCKHANDLE(dbmp, dbmp->mutexp);
1N/A LIST_INSERT_HEAD(&dbmp->dbregq, mpr, q);
1N/A UNLOCKHANDLE(dbmp, dbmp->mutexp);
1N/A
1N/A return (0);
1N/A}