1N/A * See the file LICENSE for redistribution information. 1N/A * Copyright (c) 1996, 1997, 1998 1N/A * Sleepycat Software. All rights reserved. 1N/A * Copyright (c) 1990, 1993, 1994, 1995, 1996 1N/A * Keith Bostic. All rights reserved. 1N/A * Copyright (c) 1990, 1993, 1994, 1995 1N/A * The Regents of the University of California. All rights reserved. 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 * 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 * 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/Astatic const char sccsid[] =
"@(#)db.c 10.75 (Sleepycat) 12/3/98";
1N/A#
endif /* not lint */ 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 "%s: %s specified in dbinfo argument but not set in file", \
1N/A * Main library interface to the DB access methods. 1N/A /* Validate arguments. */ 1N/A * You can't specify threads during the db_open() if the 1N/A * environment wasn't configured with them. 1N/A "environment not created using DB_THREAD");
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 "cachesize will be ignored if environment exists");
1N/A /* Allocate the DB structure, reference the DB_ENV structure. */ 1N/A /* Initialize for error return. */ 1N/A /* Random initialization. */ 1N/A /* Convert the db_open(3) flags. */ 1N/A /* Convert the dbinfo structure flags. */ 1N/A * We can't check for illegal flags until we know what type 1N/A * of open we're doing. 1N/A * Set based on the dbenv fields, although no logging or transactions 1N/A * are possible for temporary files. 1N/A /* Set the common fields. */ 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 /* Fill in the default file mode. */ 1N/A /* Check if the user wants us to swap byte order. */ 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 * 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 /* Get the real file name. */ 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 * 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 * 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 * 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 * 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 "%s: DBTYPE of unknown with empty file",
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 "%s: unsupported btree version number %lu",
1N/A /* Copy the file's unique id. */ 1N/A "%s: unsupported hash version number %lu",
1N/A /* Copy the file's unique id. */ 1N/A "DBTYPE of unknown without existing file");
1N/A * By the time we get here we've either set the type or we're taking 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 * Should we be checking for a page size that's not a multiple of 512? 1N/A * If no mpool supplied by the application, attach to a local, 1N/A * created buffer pool. 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 * Set and/or correct the cache size; must be a multiple of 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 * 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 /* No further use for the real name. */ 1N/A * Open a backing file in the memory pool. 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 * Set up additional memp_fopen information. 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 * 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 /* Get a log file id. */ 1N/A /* Call the real open function. */ 1N/Aerr:
/* Close the file descriptor. */ 1N/A /* Discard the log file id. */ 1N/A /* Close the memory pool file. */ 1N/A /* If the memory pool was local, close it. */ 1N/A /* If we allocated a DB_ENV, discard it. */ 1N/A * PUBLIC: int __db_close __P((DB *, u_int32_t)); 1N/A /* Validate arguments. */ 1N/A /* Sync the underlying file. */ 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 /* Call the access specific close function. */ 1N/A /* Sync the memory pool. */ 1N/A /* Close the memory pool file. */ 1N/A /* If the memory pool was local, close it. */ 1N/A /* Discard the log file id. */ 1N/A /* If we allocated a DB_ENV, discard it. */