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/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995, 1996
1N/A * Keith Bostic. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1990, 1993, 1994, 1995
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)db.c 10.75 (Sleepycat) 12/3/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 <stddef.h>
1N/A#include <stdlib.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_page.h"
1N/A#include "db_shash.h"
1N/A#include "db_swap.h"
1N/A#include "btree.h"
1N/A#include "hash.h"
1N/A#include "mp.h"
1N/A#include "db_am.h"
1N/A#include "common_ext.h"
1N/A
1N/A/*
1N/A * If the metadata page has the flag set, set the local flag. If the page
1N/A * does NOT have the flag set, return EINVAL if the user's dbinfo argument
1N/A * caused us to already set the local flag.
1N/A */
1N/A#define DBINFO_FCHK(dbp, fn, meta_flags, m_name, dbp_name) { \
1N/A if ((meta_flags) & (m_name)) \
1N/A F_SET(dbp, dbp_name); \
1N/A else \
1N/A if (F_ISSET(dbp, dbp_name)) { \
1N/A __db_err(dbenv, \
1N/A "%s: %s specified in dbinfo argument but not set in file", \
1N/A fname, fn); \
1N/A goto einval; \
1N/A } \
1N/A}
1N/A
1N/A/*
1N/A * db_open --
1N/A * Main library interface to the DB access methods.
1N/A */
1N/Aint
1N/Adb_open(fname, type, flags, mode, dbenv, dbinfo, dbpp)
1N/A const char *fname;
1N/A DBTYPE type;
1N/A u_int32_t flags;
1N/A int mode;
1N/A DB_ENV *dbenv;
1N/A DB_INFO *dbinfo;
1N/A DB **dbpp;
1N/A{
1N/A BTMETA *btm;
1N/A DB *dbp;
1N/A DBT pgcookie;
1N/A DB_ENV *envp, t_dbenv;
1N/A DB_MPOOL_FINFO finfo;
1N/A DB_PGINFO pginfo;
1N/A HASHHDR *hashm;
1N/A size_t cachesize;
1N/A ssize_t nr;
1N/A u_int32_t iopsize;
1N/A int fd, ftype, need_fileid, restore, ret, retry_cnt, swapped;
1N/A char *real_name, mbuf[512];
1N/A
1N/A /* Validate arguments. */
1N/A#ifdef HAVE_SPINLOCKS
1N/A#define OKFLAGS (DB_CREATE | DB_FCNTL_LOCKING | DB_NOMMAP | DB_RDONLY | DB_THREAD | DB_TRUNCATE)
1N/A#else
1N/A#define OKFLAGS (DB_CREATE | DB_FCNTL_LOCKING | DB_NOMMAP | DB_RDONLY | DB_TRUNCATE)
1N/A#endif
1N/A if ((ret = __db_fchk(dbenv, "db_open", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A
1N/A if (dbenv != NULL) {
1N/A /*
1N/A * You can't specify threads during the db_open() if the
1N/A * environment wasn't configured with them.
1N/A */
1N/A if (LF_ISSET(DB_THREAD) && !F_ISSET(dbenv, DB_ENV_THREAD)) {
1N/A __db_err(dbenv,
1N/A "environment not created using DB_THREAD");
1N/A return (EINVAL);
1N/A }
1N/A
1N/A /*
1N/A * Specifying a cachesize to db_open(3), after creating an
1N/A * environment with DB_INIT_MPOOL, is a common mistake.
1N/A */
1N/A if (dbenv->mp_info != NULL &&
1N/A dbinfo != NULL && dbinfo->db_cachesize != 0) {
1N/A __db_err(dbenv,
1N/A "cachesize will be ignored if environment exists");
1N/A return (EINVAL);
1N/A }
1N/A }
1N/A
1N/A /* Allocate the DB structure, reference the DB_ENV structure. */
1N/A if ((ret = __os_calloc(1, sizeof(DB), &dbp)) != 0)
1N/A return (ret);
1N/A dbp->dbenv = dbenv;
1N/A
1N/A /* Initialize for error return. */
1N/A dbp->saved_open_fd = fd = -1;
1N/A need_fileid = 1;
1N/A real_name = NULL;
1N/A
1N/A /* Random initialization. */
1N/A TAILQ_INIT(&dbp->free_queue);
1N/A TAILQ_INIT(&dbp->active_queue);
1N/A if ((ret = __db_init_wrapper(dbp)) != 0)
1N/A goto err;
1N/A
1N/A /* Convert the db_open(3) flags. */
1N/A if (LF_ISSET(DB_RDONLY))
1N/A F_SET(dbp, DB_AM_RDONLY);
1N/A if (LF_ISSET(DB_THREAD))
1N/A F_SET(dbp, DB_AM_THREAD);
1N/A
1N/A /* Convert the dbinfo structure flags. */
1N/A if (dbinfo != NULL) {
1N/A /*
1N/A * !!!
1N/A * We can't check for illegal flags until we know what type
1N/A * of open we're doing.
1N/A */
1N/A if (F_ISSET(dbinfo, DB_DELIMITER))
1N/A F_SET(dbp, DB_RE_DELIMITER);
1N/A if (F_ISSET(dbinfo, DB_DUP))
1N/A F_SET(dbp, DB_AM_DUP);
1N/A if (F_ISSET(dbinfo, DB_FIXEDLEN))
1N/A F_SET(dbp, DB_RE_FIXEDLEN);
1N/A if (F_ISSET(dbinfo, DB_PAD))
1N/A F_SET(dbp, DB_RE_PAD);
1N/A if (F_ISSET(dbinfo, DB_RECNUM))
1N/A F_SET(dbp, DB_BT_RECNUM);
1N/A if (F_ISSET(dbinfo, DB_RENUMBER))
1N/A F_SET(dbp, DB_RE_RENUMBER);
1N/A if (F_ISSET(dbinfo, DB_SNAPSHOT))
1N/A F_SET(dbp, DB_RE_SNAPSHOT);
1N/A }
1N/A
1N/A /*
1N/A * Set based on the dbenv fields, although no logging or transactions
1N/A * are possible for temporary files.
1N/A */
1N/A if (dbenv != NULL) {
1N/A if (dbenv->lk_info != NULL)
1N/A if (F_ISSET(dbenv, DB_ENV_CDB))
1N/A F_SET(dbp, DB_AM_CDB);
1N/A else
1N/A F_SET(dbp, DB_AM_LOCKING);
1N/A if (fname != NULL && dbenv->lg_info != NULL)
1N/A F_SET(dbp, DB_AM_LOGGING);
1N/A }
1N/A
1N/A /* Set the common fields. */
1N/A if (dbinfo == NULL) {
1N/A dbp->pgsize = 0;
1N/A dbp->db_malloc = NULL;
1N/A dbp->dup_compare = NULL;
1N/A } else {
1N/A /*
1N/A * We don't want anything that's not a power-of-2, as we rely
1N/A * on that for alignment of various types on the pages.
1N/A */
1N/A if ((dbp->pgsize = dbinfo->db_pagesize) != 0 &&
1N/A (u_int32_t)1 << __db_log2(dbp->pgsize) != dbp->pgsize) {
1N/A __db_err(dbenv, "page sizes must be a power-of-2");
1N/A goto einval;
1N/A }
1N/A dbp->pgsize = dbinfo->db_pagesize;
1N/A dbp->db_malloc = dbinfo->db_malloc;
1N/A if (F_ISSET(dbinfo, DB_DUPSORT)) {
1N/A if (F_ISSET(dbinfo, DB_DUP))
1N/A dbp->dup_compare = dbinfo->dup_compare == NULL ?
1N/A __bam_defcmp : dbinfo->dup_compare;
1N/A else {
1N/A __db_err(dbenv, "DB_DUPSORT requires DB_DUP");
1N/A goto einval;
1N/A }
1N/A F_CLR(dbinfo, DB_DUPSORT);
1N/A }
1N/A }
1N/A
1N/A /* Fill in the default file mode. */
1N/A if (mode == 0)
1N/A mode = __db_omode("rwrw--");
1N/A
1N/A /* Check if the user wants us to swap byte order. */
1N/A if (dbinfo != NULL)
1N/A switch (ret = __db_byteorder(dbenv, dbinfo->db_lorder)) {
1N/A case 0:
1N/A break;
1N/A case DB_SWAPBYTES:
1N/A F_SET(dbp, DB_AM_SWAP);
1N/A break;
1N/A default:
1N/A goto err;
1N/A }
1N/A dbp->byteswapped = F_ISSET(dbp, DB_AM_SWAP) ? 1 : 0;
1N/A
1N/A /*
1N/A * If we have a file name, try and read the first page, figure out
1N/A * what type of file it is, and initialize everything we can based
1N/A * on that file's meta-data page.
1N/A *
1N/A * XXX
1N/A * We don't actually expect zero-length strings as arguments. We
1N/A * do the check, permitting them, because scripting languages, e.g.,
1N/A * the Tcl test suite, doesn't know anything about passing NULL's.
1N/A */
1N/A if (fname != NULL && fname[0] != '\0') {
1N/A /* Get the real file name. */
1N/A if ((ret = __db_appname(dbenv,
1N/A DB_APP_DATA, NULL, fname, 0, NULL, &real_name)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Open the backing file. We need to make sure that multiple
1N/A * processes attempting to create the file at the same time
1N/A * are properly ordered so that only one of them creates the
1N/A * "unique" file id, so we open it O_EXCL and O_CREAT so two
1N/A * simultaneous attempts to create the region will return
1N/A * failure in one of the attempts. If we're one of the ones
1N/A * that fail, we simply retry without the O_CREAT flag, which
1N/A * will require that the meta-data page exist.
1N/A */
1N/A retry_cnt = 0;
1N/Aopen_retry: if (LF_ISSET(DB_CREATE)) {
1N/A if ((ret = __db_open(real_name, flags | DB_EXCL,
1N/A OKFLAGS | DB_EXCL, mode, &fd)) != 0)
1N/A if (ret == EEXIST) {
1N/A LF_CLR(DB_CREATE);
1N/A goto open_retry;
1N/A } else {
1N/A __db_err(dbenv,
1N/A "%s: %s", fname, strerror(ret));
1N/A goto err;
1N/A }
1N/A } else
1N/A if ((ret = __db_open(real_name,
1N/A flags, OKFLAGS, mode, &fd)) != 0) {
1N/A __db_err(dbenv, "%s: %s", fname, strerror(ret));
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * Use the optimum I/O size as the pagesize if a pagesize not
1N/A * specified. Some filesystems have 64K as their optimum I/O
1N/A * size, but as that results in impossibly large default cache
1N/A * sizes, we limit the default pagesize to 16K.
1N/A */
1N/A if (dbp->pgsize == 0) {
1N/A if ((ret = __os_ioinfo(real_name,
1N/A fd, NULL, NULL, &iopsize)) != 0) {
1N/A __db_err(dbenv,
1N/A "%s: %s", real_name, strerror(ret));
1N/A goto err;
1N/A }
1N/A if (iopsize < 512)
1N/A iopsize = 512;
1N/A if (iopsize > 16 * 1024)
1N/A iopsize = 16 * 1024;
1N/A
1N/A /*
1N/A * Sheer paranoia, but we don't want anything that's
1N/A * not a power-of-2, as we rely on that for alignment
1N/A * of various types on the pages.
1N/A */
1N/A DB_ROUNDOFF(iopsize, 512);
1N/A
1N/A dbp->pgsize = iopsize;
1N/A F_SET(dbp, DB_AM_PGDEF);
1N/A }
1N/A
1N/A /*
1N/A * Try and read the first disk sector -- this code assumes
1N/A * that the meta-data for all access methods fits in 512
1N/A * bytes, and that no database will be smaller than that.
1N/A */
1N/A if ((ret = __os_read(fd, mbuf, sizeof(mbuf), &nr)) != 0)
1N/A goto err;
1N/A
1N/A if (LF_ISSET(DB_FCNTL_LOCKING))
1N/A dbp->saved_open_fd = fd;
1N/A else
1N/A (void)__os_close(fd);
1N/A fd = -1;
1N/A
1N/A if (nr != sizeof(mbuf)) {
1N/A if (nr != 0) {
1N/A __db_err(dbenv,
1N/A "%s: unexpected file format", fname);
1N/A goto einval;
1N/A }
1N/A /*
1N/A * The only way we can reach here with the DB_CREATE
1N/A * flag set is if we created the file. If that's not
1N/A * the case, then a) someone else created the file
1N/A * but has not yet written out the meta-data page, or
1N/A * b) we truncated the file (DB_TRUNCATE) leaving it
1N/A * zero-length. In the case of a), we want to sleep
1N/A * and give the file creator some time to write the
1N/A * metadata page. In the case of b), charge forward.
1N/A * Note, there is a race in the case of two processes
1N/A * opening the file with the DB_TRUNCATE flag set at
1N/A * roughly the same time, and they could theoretically
1N/A * hurt each other, although it's pretty unlikely.
1N/A */
1N/A if (retry_cnt++ < 3 &&
1N/A !LF_ISSET(DB_CREATE | DB_TRUNCATE)) {
1N/A __os_sleep(1, 0);
1N/A goto open_retry;
1N/A }
1N/A if (type == DB_UNKNOWN) {
1N/A __db_err(dbenv,
1N/A "%s: DBTYPE of unknown with empty file",
1N/A fname);
1N/A goto einval;
1N/A }
1N/A goto empty;
1N/A }
1N/A
1N/A /*
1N/A * A found file overrides some user information. We'll check
1N/A * for possible error conditions based on conflicts between
1N/A * the file and the user's arguments below.
1N/A */
1N/A swapped = 0;
1N/A F_CLR(dbp, DB_AM_SWAP);
1N/A
1N/Aretry: switch (((BTMETA *)mbuf)->magic) {
1N/A case DB_BTREEMAGIC:
1N/A if (type != DB_BTREE &&
1N/A type != DB_RECNO && type != DB_UNKNOWN)
1N/A goto einval;
1N/A
1N/A btm = (BTMETA *)mbuf;
1N/A if (swapped && (ret = __bam_mswap((PAGE *)btm)) != 0)
1N/A goto err;
1N/A
1N/A if (btm->version < DB_BTREEOLDVER ||
1N/A btm->version > DB_BTREEVERSION) {
1N/A __db_err(dbenv,
1N/A "%s: unsupported btree version number %lu",
1N/A fname, (u_long)btm->version);
1N/A goto einval;
1N/A }
1N/A dbp->pgsize = btm->pagesize;
1N/A F_CLR(dbp, DB_AM_PGDEF);
1N/A
1N/A if ((ret = __db_fchk(dbenv,
1N/A "db_open", btm->flags, BTM_MASK)) != 0)
1N/A goto err;
1N/A DBINFO_FCHK(dbp, "DB_DUP",
1N/A btm->flags, BTM_DUP, DB_AM_DUP);
1N/A if (F_ISSET(btm, BTM_RECNO)) {
1N/A DBINFO_FCHK(dbp, "DB_FIXEDLEN",
1N/A btm->flags, BTM_FIXEDLEN, DB_RE_FIXEDLEN);
1N/A DBINFO_FCHK(dbp, "DB_RENUMBER",
1N/A btm->flags, BTM_RENUMBER, DB_RE_RENUMBER);
1N/A type = DB_RECNO;
1N/A } else {
1N/A DBINFO_FCHK(dbp, "DB_RECNUM",
1N/A btm->flags, BTM_RECNUM, DB_BT_RECNUM);
1N/A type = DB_BTREE;
1N/A }
1N/A
1N/A /* Copy the file's unique id. */
1N/A need_fileid = 0;
1N/A memcpy(dbp->fileid, btm->uid, DB_FILE_ID_LEN);
1N/A break;
1N/A case DB_HASHMAGIC:
1N/A if (type != DB_HASH && type != DB_UNKNOWN)
1N/A goto einval;
1N/A
1N/A hashm = (HASHHDR *)mbuf;
1N/A if (swapped && (ret = __ham_mswap((PAGE *)hashm)) != 0)
1N/A goto err;
1N/A
1N/A if (hashm->version < DB_HASHOLDVER ||
1N/A hashm->version > DB_HASHVERSION) {
1N/A __db_err(dbenv,
1N/A "%s: unsupported hash version number %lu",
1N/A fname, hashm->version);
1N/A goto einval;
1N/A }
1N/A dbp->pgsize = hashm->pagesize;
1N/A F_CLR(dbp, DB_AM_PGDEF);
1N/A
1N/A if ((ret = __db_fchk(dbenv,
1N/A "db_open", hashm->flags, DB_HASH_DUP)) != 0)
1N/A goto err;
1N/A DBINFO_FCHK(dbp, "DB_DUP",
1N/A hashm->flags, DB_HASH_DUP, DB_AM_DUP);
1N/A type = DB_HASH;
1N/A
1N/A /* Copy the file's unique id. */
1N/A need_fileid = 0;
1N/A memcpy(dbp->fileid, hashm->uid, DB_FILE_ID_LEN);
1N/A break;
1N/A default:
1N/A if (swapped) {
1N/A __db_err(dbenv, "unrecognized file type");
1N/A goto einval;
1N/A }
1N/A M_32_SWAP(((BTMETA *)mbuf)->magic);
1N/A F_SET(dbp, DB_AM_SWAP);
1N/A
1N/A swapped = 1;
1N/A goto retry;
1N/A }
1N/A } else {
1N/A fname = real_name = NULL;
1N/A
1N/A if (type == DB_UNKNOWN) {
1N/A __db_err(dbenv,
1N/A "DBTYPE of unknown without existing file");
1N/A goto einval;
1N/A }
1N/A F_SET(dbp, DB_AM_INMEM);
1N/A }
1N/A
1N/Aempty: /*
1N/A * By the time we get here we've either set the type or we're taking
1N/A * it from the user.
1N/A */
1N/A dbp->type = type;
1N/A
1N/A /*
1N/A * Set the page size to the best value for I/O to this file. Don't
1N/A * overflow the page offset type. The page size must be db_indx_t
1N/A * aligned and >= MIN_PAGE_SIZE.
1N/A *
1N/A * XXX
1N/A * Should we be checking for a page size that's not a multiple of 512?
1N/A */
1N/A if (dbp->pgsize == 0) {
1N/A F_SET(dbp, DB_AM_PGDEF);
1N/A dbp->pgsize = 8 * 1024;
1N/A }
1N/A if (dbp->pgsize < DB_MIN_PGSIZE ||
1N/A dbp->pgsize > DB_MAX_PGSIZE ||
1N/A dbp->pgsize & (sizeof(db_indx_t) - 1)) {
1N/A __db_err(dbenv, "illegal page size");
1N/A goto einval;
1N/A }
1N/A
1N/A /*
1N/A * If no mpool supplied by the application, attach to a local,
1N/A * created buffer pool.
1N/A *
1N/A * XXX
1N/A * If the user has a DB_ENV structure, we have to use a temporary
1N/A * one so that we don't step on their values. If the user doesn't,
1N/A * we have to create one, and keep it around until the call to the
1N/A * memp_close() function. This is all so the mpool functions get
1N/A * the error stuff right.
1N/A */
1N/A if (dbenv == NULL || dbenv->mp_info == NULL) {
1N/A F_SET(dbp, DB_AM_MLOCAL);
1N/A
1N/A if (dbenv == NULL) {
1N/A if ((ret = __os_calloc(1,
1N/A sizeof(DB_ENV), &dbp->mp_dbenv)) != 0)
1N/A goto err;
1N/A
1N/A envp = dbp->mp_dbenv;
1N/A restore = 0;
1N/A } else {
1N/A t_dbenv = *dbenv;
1N/A
1N/A envp = dbenv;
1N/A restore = 1;
1N/A }
1N/A
1N/A /*
1N/A * Set and/or correct the cache size; must be a multiple of
1N/A * the page size.
1N/A */
1N/A if (dbinfo == NULL || dbinfo->db_cachesize == 0)
1N/A cachesize = dbp->pgsize * DB_MINCACHE;
1N/A else {
1N/A cachesize = dbinfo->db_cachesize;
1N/A if (cachesize & (dbp->pgsize - 1))
1N/A cachesize +=
1N/A (~cachesize & (dbp->pgsize - 1)) + 1;
1N/A if (cachesize < dbp->pgsize * DB_MINCACHE)
1N/A cachesize = dbp->pgsize * DB_MINCACHE;
1N/A if (cachesize < 20 * 1024)
1N/A cachesize = 20 * 1024;
1N/A }
1N/A envp->mp_size = cachesize;
1N/A
1N/A if ((ret = memp_open(NULL, DB_CREATE | DB_MPOOL_PRIVATE |
1N/A (F_ISSET(dbp, DB_AM_THREAD) ? DB_THREAD : 0),
1N/A __db_omode("rw----"), envp, &dbp->mp)) != 0)
1N/A goto err;
1N/A if (restore)
1N/A *dbenv = t_dbenv;
1N/A } else
1N/A dbp->mp = dbenv->mp_info;
1N/A
1N/A /* Register DB's pgin/pgout functions. */
1N/A if ((ret = memp_register(dbp->mp,
1N/A DB_FTYPE_BTREE, __bam_pgin, __bam_pgout)) != 0)
1N/A goto err;
1N/A if ((ret = memp_register(dbp->mp,
1N/A DB_FTYPE_HASH, __ham_pgin, __ham_pgout)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * If we don't already have one, get a unique file ID. If the file
1N/A * is a temporary file, then we have to create a unique file ID --
1N/A * no backing file will be created until the mpool cache is filled
1N/A * forcing it to go to disk. The created ID must never match any
1N/A * potential real file ID -- we know it won't because real file IDs
1N/A * contain a time stamp after the dev/ino pair, and we're simply
1N/A * storing a 4-byte locker ID.
1N/A *
1N/A * XXX
1N/A * Store the file id in the locker structure -- we can get it from
1N/A * there as necessary, and it saves having two copies.
1N/A */
1N/A if (need_fileid)
1N/A if (fname == NULL) {
1N/A memset(dbp->fileid, 0, DB_FILE_ID_LEN);
1N/A if (F_ISSET(dbp, DB_AM_LOCKING) &&
1N/A (ret = lock_id(dbenv->lk_info,
1N/A (u_int32_t *)dbp->fileid)) != 0)
1N/A goto err;
1N/A } else
1N/A if ((ret = __os_fileid(dbenv,
1N/A real_name, 1, dbp->fileid)) != 0)
1N/A goto err;
1N/A
1N/A /* No further use for the real name. */
1N/A if (real_name != NULL)
1N/A __os_freestr(real_name);
1N/A real_name = NULL;
1N/A
1N/A /*
1N/A * Open a backing file in the memory pool.
1N/A *
1N/A * If we need to process the file's pages on I/O, set the file type.
1N/A * If it's a hash file, always call pgin and pgout routines. This
1N/A * means that hash files can never be mapped into process memory. If
1N/A * it's a btree file and requires swapping, we need to page the file
1N/A * in and out. This has to be right -- we can't mmap files that are
1N/A * being paged in and out.
1N/A */
1N/A if (type == DB_HASH)
1N/A ftype = DB_FTYPE_HASH;
1N/A else
1N/A ftype = F_ISSET(dbp, DB_AM_SWAP) ? DB_FTYPE_BTREE : 0;
1N/A pginfo.db_pagesize = dbp->pgsize;
1N/A pginfo.needswap = F_ISSET(dbp, DB_AM_SWAP);
1N/A pgcookie.data = &pginfo;
1N/A pgcookie.size = sizeof(DB_PGINFO);
1N/A
1N/A /*
1N/A * Set up additional memp_fopen information.
1N/A */
1N/A memset(&finfo, 0, sizeof(finfo));
1N/A finfo.ftype = ftype;
1N/A finfo.pgcookie = &pgcookie;
1N/A finfo.fileid = dbp->fileid;
1N/A finfo.lsn_offset = 0;
1N/A finfo.clear_len = DB_PAGE_CLEAR_LEN;
1N/A if ((ret = memp_fopen(dbp->mp, fname,
1N/A F_ISSET(dbp, DB_AM_RDONLY) ? DB_RDONLY : 0,
1N/A 0, dbp->pgsize, &finfo, &dbp->mpf)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * XXX
1N/A * We need a per-thread mutex that lives in shared memory -- HP-UX
1N/A * can't allocate mutexes in malloc'd memory. Allocate it from the
1N/A * shared memory region, since it's the only one that is guaranteed
1N/A * to exist.
1N/A */
1N/A if (F_ISSET(dbp, DB_AM_THREAD)) {
1N/A if ((ret = __memp_reg_alloc(dbp->mp,
1N/A sizeof(db_mutex_t), NULL, &dbp->mutexp)) != 0)
1N/A goto err;
1N/A /*
1N/A * Since we only get here if DB_THREAD was specified, we know
1N/A * we have spinlocks and no file offset argument is needed.
1N/A */
1N/A (void)__db_mutex_init(dbp->mutexp, 0);
1N/A }
1N/A
1N/A /* Get a log file id. */
1N/A if (F_ISSET(dbp, DB_AM_LOGGING) &&
1N/A (ret = log_register(dbenv->lg_info,
1N/A dbp, fname, type, &dbp->log_fileid)) != 0)
1N/A goto err;
1N/A
1N/A /* Call the real open function. */
1N/A switch (type) {
1N/A case DB_BTREE:
1N/A if (dbinfo != NULL && (ret = __db_fchk(dbenv,
1N/A "db_open", dbinfo->flags, DB_RECNUM | DB_DUP)) != 0)
1N/A goto err;
1N/A if (dbinfo != NULL && (ret = __db_fcchk(dbenv,
1N/A "db_open", dbinfo->flags, DB_DUP, DB_RECNUM)) != 0)
1N/A goto err;
1N/A if ((ret = __bam_open(dbp, dbinfo)) != 0)
1N/A goto err;
1N/A break;
1N/A case DB_HASH:
1N/A if (dbinfo != NULL && (ret = __db_fchk(dbenv,
1N/A "db_open", dbinfo->flags, DB_DUP)) != 0)
1N/A goto err;
1N/A if ((ret = __ham_open(dbp, dbinfo)) != 0)
1N/A goto err;
1N/A break;
1N/A case DB_RECNO:
1N/A#define DB_INFO_FLAGS \
1N/A (DB_DELIMITER | DB_FIXEDLEN | DB_PAD | DB_RENUMBER | DB_SNAPSHOT)
1N/A if (dbinfo != NULL && (ret = __db_fchk(dbenv,
1N/A "db_open", dbinfo->flags, DB_INFO_FLAGS)) != 0)
1N/A goto err;
1N/A if ((ret = __ram_open(dbp, dbinfo)) != 0)
1N/A goto err;
1N/A break;
1N/A default:
1N/A abort();
1N/A }
1N/A
1N/A *dbpp = dbp;
1N/A return (0);
1N/A
1N/Aeinval: ret = EINVAL;
1N/Aerr: /* Close the file descriptor. */
1N/A if (fd != -1)
1N/A (void)__os_close(fd);
1N/A
1N/A /* Discard the log file id. */
1N/A if (dbp->log_fileid != 0)
1N/A (void)log_unregister(dbenv->lg_info, dbp->log_fileid);
1N/A
1N/A /* Close the memory pool file. */
1N/A if (dbp->mpf != NULL)
1N/A (void)memp_fclose(dbp->mpf);
1N/A
1N/A /* If the memory pool was local, close it. */
1N/A if (F_ISSET(dbp, DB_AM_MLOCAL) && dbp->mp != NULL)
1N/A (void)memp_close(dbp->mp);
1N/A
1N/A /* If we allocated a DB_ENV, discard it. */
1N/A if (dbp->mp_dbenv != NULL)
1N/A __os_free(dbp->mp_dbenv, sizeof(DB_ENV));
1N/A
1N/A if (real_name != NULL)
1N/A __os_freestr(real_name);
1N/A if (dbp != NULL)
1N/A __os_free(dbp, sizeof(DB));
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __db_close --
1N/A * Close a DB tree.
1N/A *
1N/A * PUBLIC: int __db_close __P((DB *, u_int32_t));
1N/A */
1N/Aint
1N/A__db_close(dbp, flags)
1N/A DB *dbp;
1N/A u_int32_t flags;
1N/A{
1N/A DBC *dbc;
1N/A int ret, t_ret;
1N/A
1N/A DB_PANIC_CHECK(dbp);
1N/A
1N/A /* Validate arguments. */
1N/A if ((ret = __db_closechk(dbp, flags)) != 0)
1N/A return (ret);
1N/A
1N/A /* Sync the underlying file. */
1N/A if (flags != DB_NOSYNC &&
1N/A (t_ret = dbp->sync(dbp, 0)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /*
1N/A * Go through the active cursors and call the cursor recycle routine,
1N/A * which resolves pending operations and moves the cursors onto the
1N/A * free list. Then, walk the free list and call the cursor destroy
1N/A * routine.
1N/A */
1N/A while ((dbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
1N/A if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A while ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
1N/A if ((t_ret = __db_c_destroy(dbc)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* Call the access specific close function. */
1N/A if ((t_ret = dbp->am_close(dbp)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* Sync the memory pool. */
1N/A if (flags != DB_NOSYNC && (t_ret = memp_fsync(dbp->mpf)) != 0 &&
1N/A t_ret != DB_INCOMPLETE && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* Close the memory pool file. */
1N/A if ((t_ret = memp_fclose(dbp->mpf)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* If the memory pool was local, close it. */
1N/A if (F_ISSET(dbp, DB_AM_MLOCAL) &&
1N/A (t_ret = memp_close(dbp->mp)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A if (dbp->saved_open_fd != -1) {
1N/A (void)__os_close(dbp->saved_open_fd);
1N/A dbp->saved_open_fd = -1;
1N/A }
1N/A
1N/A /* Discard the log file id. */
1N/A if (F_ISSET(dbp, DB_AM_LOGGING))
1N/A (void)log_unregister(dbp->dbenv->lg_info, dbp->log_fileid);
1N/A
1N/A /* If we allocated a DB_ENV, discard it. */
1N/A if (dbp->mp_dbenv != NULL)
1N/A __os_free(dbp->mp_dbenv, sizeof(DB_ENV));
1N/A
1N/A /* Free the DB. */
1N/A __os_free(dbp, sizeof(*dbp));
1N/A
1N/A return (ret);
1N/A}