1*7c478bd9Sstevel@tonic-gate /*-
2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*7c478bd9Sstevel@tonic-gate */
7*7c478bd9Sstevel@tonic-gate /*
8*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994, 1995, 1996
9*7c478bd9Sstevel@tonic-gate * Keith Bostic. All rights reserved.
10*7c478bd9Sstevel@tonic-gate */
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994, 1995
13*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
14*7c478bd9Sstevel@tonic-gate *
15*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
16*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
17*7c478bd9Sstevel@tonic-gate * are met:
18*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
19*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
20*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
21*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
22*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
23*7c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
24*7c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
25*7c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
26*7c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
27*7c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
28*7c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
29*7c478bd9Sstevel@tonic-gate * without specific prior written permission.
30*7c478bd9Sstevel@tonic-gate *
31*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32*7c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34*7c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35*7c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*7c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*7c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*7c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39*7c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40*7c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41*7c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
42*7c478bd9Sstevel@tonic-gate */
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "config.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #ifndef lint
47*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)db.c 10.75 (Sleepycat) 12/3/98";
48*7c478bd9Sstevel@tonic-gate #endif /* not lint */
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
51*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate #include <errno.h>
54*7c478bd9Sstevel@tonic-gate #include <stddef.h>
55*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
56*7c478bd9Sstevel@tonic-gate #include <string.h>
57*7c478bd9Sstevel@tonic-gate #endif
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate #include "db_int.h"
60*7c478bd9Sstevel@tonic-gate #include "shqueue.h"
61*7c478bd9Sstevel@tonic-gate #include "db_page.h"
62*7c478bd9Sstevel@tonic-gate #include "db_shash.h"
63*7c478bd9Sstevel@tonic-gate #include "db_swap.h"
64*7c478bd9Sstevel@tonic-gate #include "btree.h"
65*7c478bd9Sstevel@tonic-gate #include "hash.h"
66*7c478bd9Sstevel@tonic-gate #include "mp.h"
67*7c478bd9Sstevel@tonic-gate #include "db_am.h"
68*7c478bd9Sstevel@tonic-gate #include "common_ext.h"
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate * If the metadata page has the flag set, set the local flag. If the page
72*7c478bd9Sstevel@tonic-gate * does NOT have the flag set, return EINVAL if the user's dbinfo argument
73*7c478bd9Sstevel@tonic-gate * caused us to already set the local flag.
74*7c478bd9Sstevel@tonic-gate */
75*7c478bd9Sstevel@tonic-gate #define DBINFO_FCHK(dbp, fn, meta_flags, m_name, dbp_name) { \
76*7c478bd9Sstevel@tonic-gate if ((meta_flags) & (m_name)) \
77*7c478bd9Sstevel@tonic-gate F_SET(dbp, dbp_name); \
78*7c478bd9Sstevel@tonic-gate else \
79*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, dbp_name)) { \
80*7c478bd9Sstevel@tonic-gate __db_err(dbenv, \
81*7c478bd9Sstevel@tonic-gate "%s: %s specified in dbinfo argument but not set in file", \
82*7c478bd9Sstevel@tonic-gate fname, fn); \
83*7c478bd9Sstevel@tonic-gate goto einval; \
84*7c478bd9Sstevel@tonic-gate } \
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate * db_open --
89*7c478bd9Sstevel@tonic-gate * Main library interface to the DB access methods.
90*7c478bd9Sstevel@tonic-gate */
91*7c478bd9Sstevel@tonic-gate int
db_open(fname,type,flags,mode,dbenv,dbinfo,dbpp)92*7c478bd9Sstevel@tonic-gate db_open(fname, type, flags, mode, dbenv, dbinfo, dbpp)
93*7c478bd9Sstevel@tonic-gate const char *fname;
94*7c478bd9Sstevel@tonic-gate DBTYPE type;
95*7c478bd9Sstevel@tonic-gate u_int32_t flags;
96*7c478bd9Sstevel@tonic-gate int mode;
97*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
98*7c478bd9Sstevel@tonic-gate DB_INFO *dbinfo;
99*7c478bd9Sstevel@tonic-gate DB **dbpp;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate BTMETA *btm;
102*7c478bd9Sstevel@tonic-gate DB *dbp;
103*7c478bd9Sstevel@tonic-gate DBT pgcookie;
104*7c478bd9Sstevel@tonic-gate DB_ENV *envp, t_dbenv;
105*7c478bd9Sstevel@tonic-gate DB_MPOOL_FINFO finfo;
106*7c478bd9Sstevel@tonic-gate DB_PGINFO pginfo;
107*7c478bd9Sstevel@tonic-gate HASHHDR *hashm;
108*7c478bd9Sstevel@tonic-gate size_t cachesize;
109*7c478bd9Sstevel@tonic-gate ssize_t nr;
110*7c478bd9Sstevel@tonic-gate u_int32_t iopsize;
111*7c478bd9Sstevel@tonic-gate int fd, ftype, need_fileid, restore, ret, retry_cnt, swapped;
112*7c478bd9Sstevel@tonic-gate char *real_name, mbuf[512];
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate /* Validate arguments. */
115*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SPINLOCKS
116*7c478bd9Sstevel@tonic-gate #define OKFLAGS (DB_CREATE | DB_FCNTL_LOCKING | DB_NOMMAP | DB_RDONLY | DB_THREAD | DB_TRUNCATE)
117*7c478bd9Sstevel@tonic-gate #else
118*7c478bd9Sstevel@tonic-gate #define OKFLAGS (DB_CREATE | DB_FCNTL_LOCKING | DB_NOMMAP | DB_RDONLY | DB_TRUNCATE)
119*7c478bd9Sstevel@tonic-gate #endif
120*7c478bd9Sstevel@tonic-gate if ((ret = __db_fchk(dbenv, "db_open", flags, OKFLAGS)) != 0)
121*7c478bd9Sstevel@tonic-gate return (ret);
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate if (dbenv != NULL) {
124*7c478bd9Sstevel@tonic-gate /*
125*7c478bd9Sstevel@tonic-gate * You can't specify threads during the db_open() if the
126*7c478bd9Sstevel@tonic-gate * environment wasn't configured with them.
127*7c478bd9Sstevel@tonic-gate */
128*7c478bd9Sstevel@tonic-gate if (LF_ISSET(DB_THREAD) && !F_ISSET(dbenv, DB_ENV_THREAD)) {
129*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
130*7c478bd9Sstevel@tonic-gate "environment not created using DB_THREAD");
131*7c478bd9Sstevel@tonic-gate return (EINVAL);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate * Specifying a cachesize to db_open(3), after creating an
136*7c478bd9Sstevel@tonic-gate * environment with DB_INIT_MPOOL, is a common mistake.
137*7c478bd9Sstevel@tonic-gate */
138*7c478bd9Sstevel@tonic-gate if (dbenv->mp_info != NULL &&
139*7c478bd9Sstevel@tonic-gate dbinfo != NULL && dbinfo->db_cachesize != 0) {
140*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
141*7c478bd9Sstevel@tonic-gate "cachesize will be ignored if environment exists");
142*7c478bd9Sstevel@tonic-gate return (EINVAL);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate /* Allocate the DB structure, reference the DB_ENV structure. */
147*7c478bd9Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(DB), &dbp)) != 0)
148*7c478bd9Sstevel@tonic-gate return (ret);
149*7c478bd9Sstevel@tonic-gate dbp->dbenv = dbenv;
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate /* Initialize for error return. */
152*7c478bd9Sstevel@tonic-gate dbp->saved_open_fd = fd = -1;
153*7c478bd9Sstevel@tonic-gate need_fileid = 1;
154*7c478bd9Sstevel@tonic-gate real_name = NULL;
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate /* Random initialization. */
157*7c478bd9Sstevel@tonic-gate TAILQ_INIT(&dbp->free_queue);
158*7c478bd9Sstevel@tonic-gate TAILQ_INIT(&dbp->active_queue);
159*7c478bd9Sstevel@tonic-gate if ((ret = __db_init_wrapper(dbp)) != 0)
160*7c478bd9Sstevel@tonic-gate goto err;
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate /* Convert the db_open(3) flags. */
163*7c478bd9Sstevel@tonic-gate if (LF_ISSET(DB_RDONLY))
164*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_RDONLY);
165*7c478bd9Sstevel@tonic-gate if (LF_ISSET(DB_THREAD))
166*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_THREAD);
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate /* Convert the dbinfo structure flags. */
169*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL) {
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate * !!!
172*7c478bd9Sstevel@tonic-gate * We can't check for illegal flags until we know what type
173*7c478bd9Sstevel@tonic-gate * of open we're doing.
174*7c478bd9Sstevel@tonic-gate */
175*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_DELIMITER))
176*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_RE_DELIMITER);
177*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_DUP))
178*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_DUP);
179*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_FIXEDLEN))
180*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_RE_FIXEDLEN);
181*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_PAD))
182*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_RE_PAD);
183*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_RECNUM))
184*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_BT_RECNUM);
185*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_RENUMBER))
186*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_RE_RENUMBER);
187*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_SNAPSHOT))
188*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_RE_SNAPSHOT);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate * Set based on the dbenv fields, although no logging or transactions
193*7c478bd9Sstevel@tonic-gate * are possible for temporary files.
194*7c478bd9Sstevel@tonic-gate */
195*7c478bd9Sstevel@tonic-gate if (dbenv != NULL) {
196*7c478bd9Sstevel@tonic-gate if (dbenv->lk_info != NULL)
197*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbenv, DB_ENV_CDB))
198*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_CDB);
199*7c478bd9Sstevel@tonic-gate else
200*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_LOCKING);
201*7c478bd9Sstevel@tonic-gate if (fname != NULL && dbenv->lg_info != NULL)
202*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_LOGGING);
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate /* Set the common fields. */
206*7c478bd9Sstevel@tonic-gate if (dbinfo == NULL) {
207*7c478bd9Sstevel@tonic-gate dbp->pgsize = 0;
208*7c478bd9Sstevel@tonic-gate dbp->db_malloc = NULL;
209*7c478bd9Sstevel@tonic-gate dbp->dup_compare = NULL;
210*7c478bd9Sstevel@tonic-gate } else {
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate * We don't want anything that's not a power-of-2, as we rely
213*7c478bd9Sstevel@tonic-gate * on that for alignment of various types on the pages.
214*7c478bd9Sstevel@tonic-gate */
215*7c478bd9Sstevel@tonic-gate if ((dbp->pgsize = dbinfo->db_pagesize) != 0 &&
216*7c478bd9Sstevel@tonic-gate (u_int32_t)1 << __db_log2(dbp->pgsize) != dbp->pgsize) {
217*7c478bd9Sstevel@tonic-gate __db_err(dbenv, "page sizes must be a power-of-2");
218*7c478bd9Sstevel@tonic-gate goto einval;
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate dbp->pgsize = dbinfo->db_pagesize;
221*7c478bd9Sstevel@tonic-gate dbp->db_malloc = dbinfo->db_malloc;
222*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_DUPSORT)) {
223*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbinfo, DB_DUP))
224*7c478bd9Sstevel@tonic-gate dbp->dup_compare = dbinfo->dup_compare == NULL ?
225*7c478bd9Sstevel@tonic-gate __bam_defcmp : dbinfo->dup_compare;
226*7c478bd9Sstevel@tonic-gate else {
227*7c478bd9Sstevel@tonic-gate __db_err(dbenv, "DB_DUPSORT requires DB_DUP");
228*7c478bd9Sstevel@tonic-gate goto einval;
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate F_CLR(dbinfo, DB_DUPSORT);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate /* Fill in the default file mode. */
235*7c478bd9Sstevel@tonic-gate if (mode == 0)
236*7c478bd9Sstevel@tonic-gate mode = __db_omode("rwrw--");
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate /* Check if the user wants us to swap byte order. */
239*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL)
240*7c478bd9Sstevel@tonic-gate switch (ret = __db_byteorder(dbenv, dbinfo->db_lorder)) {
241*7c478bd9Sstevel@tonic-gate case 0:
242*7c478bd9Sstevel@tonic-gate break;
243*7c478bd9Sstevel@tonic-gate case DB_SWAPBYTES:
244*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_SWAP);
245*7c478bd9Sstevel@tonic-gate break;
246*7c478bd9Sstevel@tonic-gate default:
247*7c478bd9Sstevel@tonic-gate goto err;
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate dbp->byteswapped = F_ISSET(dbp, DB_AM_SWAP) ? 1 : 0;
250*7c478bd9Sstevel@tonic-gate
251*7c478bd9Sstevel@tonic-gate /*
252*7c478bd9Sstevel@tonic-gate * If we have a file name, try and read the first page, figure out
253*7c478bd9Sstevel@tonic-gate * what type of file it is, and initialize everything we can based
254*7c478bd9Sstevel@tonic-gate * on that file's meta-data page.
255*7c478bd9Sstevel@tonic-gate *
256*7c478bd9Sstevel@tonic-gate * XXX
257*7c478bd9Sstevel@tonic-gate * We don't actually expect zero-length strings as arguments. We
258*7c478bd9Sstevel@tonic-gate * do the check, permitting them, because scripting languages, e.g.,
259*7c478bd9Sstevel@tonic-gate * the Tcl test suite, doesn't know anything about passing NULL's.
260*7c478bd9Sstevel@tonic-gate */
261*7c478bd9Sstevel@tonic-gate if (fname != NULL && fname[0] != '\0') {
262*7c478bd9Sstevel@tonic-gate /* Get the real file name. */
263*7c478bd9Sstevel@tonic-gate if ((ret = __db_appname(dbenv,
264*7c478bd9Sstevel@tonic-gate DB_APP_DATA, NULL, fname, 0, NULL, &real_name)) != 0)
265*7c478bd9Sstevel@tonic-gate goto err;
266*7c478bd9Sstevel@tonic-gate
267*7c478bd9Sstevel@tonic-gate /*
268*7c478bd9Sstevel@tonic-gate * Open the backing file. We need to make sure that multiple
269*7c478bd9Sstevel@tonic-gate * processes attempting to create the file at the same time
270*7c478bd9Sstevel@tonic-gate * are properly ordered so that only one of them creates the
271*7c478bd9Sstevel@tonic-gate * "unique" file id, so we open it O_EXCL and O_CREAT so two
272*7c478bd9Sstevel@tonic-gate * simultaneous attempts to create the region will return
273*7c478bd9Sstevel@tonic-gate * failure in one of the attempts. If we're one of the ones
274*7c478bd9Sstevel@tonic-gate * that fail, we simply retry without the O_CREAT flag, which
275*7c478bd9Sstevel@tonic-gate * will require that the meta-data page exist.
276*7c478bd9Sstevel@tonic-gate */
277*7c478bd9Sstevel@tonic-gate retry_cnt = 0;
278*7c478bd9Sstevel@tonic-gate open_retry: if (LF_ISSET(DB_CREATE)) {
279*7c478bd9Sstevel@tonic-gate if ((ret = __db_open(real_name, flags | DB_EXCL,
280*7c478bd9Sstevel@tonic-gate OKFLAGS | DB_EXCL, mode, &fd)) != 0)
281*7c478bd9Sstevel@tonic-gate if (ret == EEXIST) {
282*7c478bd9Sstevel@tonic-gate LF_CLR(DB_CREATE);
283*7c478bd9Sstevel@tonic-gate goto open_retry;
284*7c478bd9Sstevel@tonic-gate } else {
285*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
286*7c478bd9Sstevel@tonic-gate "%s: %s", fname, strerror(ret));
287*7c478bd9Sstevel@tonic-gate goto err;
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate } else
290*7c478bd9Sstevel@tonic-gate if ((ret = __db_open(real_name,
291*7c478bd9Sstevel@tonic-gate flags, OKFLAGS, mode, &fd)) != 0) {
292*7c478bd9Sstevel@tonic-gate __db_err(dbenv, "%s: %s", fname, strerror(ret));
293*7c478bd9Sstevel@tonic-gate goto err;
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate
296*7c478bd9Sstevel@tonic-gate /*
297*7c478bd9Sstevel@tonic-gate * Use the optimum I/O size as the pagesize if a pagesize not
298*7c478bd9Sstevel@tonic-gate * specified. Some filesystems have 64K as their optimum I/O
299*7c478bd9Sstevel@tonic-gate * size, but as that results in impossibly large default cache
300*7c478bd9Sstevel@tonic-gate * sizes, we limit the default pagesize to 16K.
301*7c478bd9Sstevel@tonic-gate */
302*7c478bd9Sstevel@tonic-gate if (dbp->pgsize == 0) {
303*7c478bd9Sstevel@tonic-gate if ((ret = __os_ioinfo(real_name,
304*7c478bd9Sstevel@tonic-gate fd, NULL, NULL, &iopsize)) != 0) {
305*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
306*7c478bd9Sstevel@tonic-gate "%s: %s", real_name, strerror(ret));
307*7c478bd9Sstevel@tonic-gate goto err;
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate if (iopsize < 512)
310*7c478bd9Sstevel@tonic-gate iopsize = 512;
311*7c478bd9Sstevel@tonic-gate if (iopsize > 16 * 1024)
312*7c478bd9Sstevel@tonic-gate iopsize = 16 * 1024;
313*7c478bd9Sstevel@tonic-gate
314*7c478bd9Sstevel@tonic-gate /*
315*7c478bd9Sstevel@tonic-gate * Sheer paranoia, but we don't want anything that's
316*7c478bd9Sstevel@tonic-gate * not a power-of-2, as we rely on that for alignment
317*7c478bd9Sstevel@tonic-gate * of various types on the pages.
318*7c478bd9Sstevel@tonic-gate */
319*7c478bd9Sstevel@tonic-gate DB_ROUNDOFF(iopsize, 512);
320*7c478bd9Sstevel@tonic-gate
321*7c478bd9Sstevel@tonic-gate dbp->pgsize = iopsize;
322*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_PGDEF);
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate
325*7c478bd9Sstevel@tonic-gate /*
326*7c478bd9Sstevel@tonic-gate * Try and read the first disk sector -- this code assumes
327*7c478bd9Sstevel@tonic-gate * that the meta-data for all access methods fits in 512
328*7c478bd9Sstevel@tonic-gate * bytes, and that no database will be smaller than that.
329*7c478bd9Sstevel@tonic-gate */
330*7c478bd9Sstevel@tonic-gate if ((ret = __os_read(fd, mbuf, sizeof(mbuf), &nr)) != 0)
331*7c478bd9Sstevel@tonic-gate goto err;
332*7c478bd9Sstevel@tonic-gate
333*7c478bd9Sstevel@tonic-gate if (LF_ISSET(DB_FCNTL_LOCKING))
334*7c478bd9Sstevel@tonic-gate dbp->saved_open_fd = fd;
335*7c478bd9Sstevel@tonic-gate else
336*7c478bd9Sstevel@tonic-gate (void)__os_close(fd);
337*7c478bd9Sstevel@tonic-gate fd = -1;
338*7c478bd9Sstevel@tonic-gate
339*7c478bd9Sstevel@tonic-gate if (nr != sizeof(mbuf)) {
340*7c478bd9Sstevel@tonic-gate if (nr != 0) {
341*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
342*7c478bd9Sstevel@tonic-gate "%s: unexpected file format", fname);
343*7c478bd9Sstevel@tonic-gate goto einval;
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate /*
346*7c478bd9Sstevel@tonic-gate * The only way we can reach here with the DB_CREATE
347*7c478bd9Sstevel@tonic-gate * flag set is if we created the file. If that's not
348*7c478bd9Sstevel@tonic-gate * the case, then a) someone else created the file
349*7c478bd9Sstevel@tonic-gate * but has not yet written out the meta-data page, or
350*7c478bd9Sstevel@tonic-gate * b) we truncated the file (DB_TRUNCATE) leaving it
351*7c478bd9Sstevel@tonic-gate * zero-length. In the case of a), we want to sleep
352*7c478bd9Sstevel@tonic-gate * and give the file creator some time to write the
353*7c478bd9Sstevel@tonic-gate * metadata page. In the case of b), charge forward.
354*7c478bd9Sstevel@tonic-gate * Note, there is a race in the case of two processes
355*7c478bd9Sstevel@tonic-gate * opening the file with the DB_TRUNCATE flag set at
356*7c478bd9Sstevel@tonic-gate * roughly the same time, and they could theoretically
357*7c478bd9Sstevel@tonic-gate * hurt each other, although it's pretty unlikely.
358*7c478bd9Sstevel@tonic-gate */
359*7c478bd9Sstevel@tonic-gate if (retry_cnt++ < 3 &&
360*7c478bd9Sstevel@tonic-gate !LF_ISSET(DB_CREATE | DB_TRUNCATE)) {
361*7c478bd9Sstevel@tonic-gate __os_sleep(1, 0);
362*7c478bd9Sstevel@tonic-gate goto open_retry;
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate if (type == DB_UNKNOWN) {
365*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
366*7c478bd9Sstevel@tonic-gate "%s: DBTYPE of unknown with empty file",
367*7c478bd9Sstevel@tonic-gate fname);
368*7c478bd9Sstevel@tonic-gate goto einval;
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate goto empty;
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate
373*7c478bd9Sstevel@tonic-gate /*
374*7c478bd9Sstevel@tonic-gate * A found file overrides some user information. We'll check
375*7c478bd9Sstevel@tonic-gate * for possible error conditions based on conflicts between
376*7c478bd9Sstevel@tonic-gate * the file and the user's arguments below.
377*7c478bd9Sstevel@tonic-gate */
378*7c478bd9Sstevel@tonic-gate swapped = 0;
379*7c478bd9Sstevel@tonic-gate F_CLR(dbp, DB_AM_SWAP);
380*7c478bd9Sstevel@tonic-gate
381*7c478bd9Sstevel@tonic-gate retry: switch (((BTMETA *)mbuf)->magic) {
382*7c478bd9Sstevel@tonic-gate case DB_BTREEMAGIC:
383*7c478bd9Sstevel@tonic-gate if (type != DB_BTREE &&
384*7c478bd9Sstevel@tonic-gate type != DB_RECNO && type != DB_UNKNOWN)
385*7c478bd9Sstevel@tonic-gate goto einval;
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate btm = (BTMETA *)mbuf;
388*7c478bd9Sstevel@tonic-gate if (swapped && (ret = __bam_mswap((PAGE *)btm)) != 0)
389*7c478bd9Sstevel@tonic-gate goto err;
390*7c478bd9Sstevel@tonic-gate
391*7c478bd9Sstevel@tonic-gate if (btm->version < DB_BTREEOLDVER ||
392*7c478bd9Sstevel@tonic-gate btm->version > DB_BTREEVERSION) {
393*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
394*7c478bd9Sstevel@tonic-gate "%s: unsupported btree version number %lu",
395*7c478bd9Sstevel@tonic-gate fname, (u_long)btm->version);
396*7c478bd9Sstevel@tonic-gate goto einval;
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate dbp->pgsize = btm->pagesize;
399*7c478bd9Sstevel@tonic-gate F_CLR(dbp, DB_AM_PGDEF);
400*7c478bd9Sstevel@tonic-gate
401*7c478bd9Sstevel@tonic-gate if ((ret = __db_fchk(dbenv,
402*7c478bd9Sstevel@tonic-gate "db_open", btm->flags, BTM_MASK)) != 0)
403*7c478bd9Sstevel@tonic-gate goto err;
404*7c478bd9Sstevel@tonic-gate DBINFO_FCHK(dbp, "DB_DUP",
405*7c478bd9Sstevel@tonic-gate btm->flags, BTM_DUP, DB_AM_DUP);
406*7c478bd9Sstevel@tonic-gate if (F_ISSET(btm, BTM_RECNO)) {
407*7c478bd9Sstevel@tonic-gate DBINFO_FCHK(dbp, "DB_FIXEDLEN",
408*7c478bd9Sstevel@tonic-gate btm->flags, BTM_FIXEDLEN, DB_RE_FIXEDLEN);
409*7c478bd9Sstevel@tonic-gate DBINFO_FCHK(dbp, "DB_RENUMBER",
410*7c478bd9Sstevel@tonic-gate btm->flags, BTM_RENUMBER, DB_RE_RENUMBER);
411*7c478bd9Sstevel@tonic-gate type = DB_RECNO;
412*7c478bd9Sstevel@tonic-gate } else {
413*7c478bd9Sstevel@tonic-gate DBINFO_FCHK(dbp, "DB_RECNUM",
414*7c478bd9Sstevel@tonic-gate btm->flags, BTM_RECNUM, DB_BT_RECNUM);
415*7c478bd9Sstevel@tonic-gate type = DB_BTREE;
416*7c478bd9Sstevel@tonic-gate }
417*7c478bd9Sstevel@tonic-gate
418*7c478bd9Sstevel@tonic-gate /* Copy the file's unique id. */
419*7c478bd9Sstevel@tonic-gate need_fileid = 0;
420*7c478bd9Sstevel@tonic-gate memcpy(dbp->fileid, btm->uid, DB_FILE_ID_LEN);
421*7c478bd9Sstevel@tonic-gate break;
422*7c478bd9Sstevel@tonic-gate case DB_HASHMAGIC:
423*7c478bd9Sstevel@tonic-gate if (type != DB_HASH && type != DB_UNKNOWN)
424*7c478bd9Sstevel@tonic-gate goto einval;
425*7c478bd9Sstevel@tonic-gate
426*7c478bd9Sstevel@tonic-gate hashm = (HASHHDR *)mbuf;
427*7c478bd9Sstevel@tonic-gate if (swapped && (ret = __ham_mswap((PAGE *)hashm)) != 0)
428*7c478bd9Sstevel@tonic-gate goto err;
429*7c478bd9Sstevel@tonic-gate
430*7c478bd9Sstevel@tonic-gate if (hashm->version < DB_HASHOLDVER ||
431*7c478bd9Sstevel@tonic-gate hashm->version > DB_HASHVERSION) {
432*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
433*7c478bd9Sstevel@tonic-gate "%s: unsupported hash version number %lu",
434*7c478bd9Sstevel@tonic-gate fname, hashm->version);
435*7c478bd9Sstevel@tonic-gate goto einval;
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate dbp->pgsize = hashm->pagesize;
438*7c478bd9Sstevel@tonic-gate F_CLR(dbp, DB_AM_PGDEF);
439*7c478bd9Sstevel@tonic-gate
440*7c478bd9Sstevel@tonic-gate if ((ret = __db_fchk(dbenv,
441*7c478bd9Sstevel@tonic-gate "db_open", hashm->flags, DB_HASH_DUP)) != 0)
442*7c478bd9Sstevel@tonic-gate goto err;
443*7c478bd9Sstevel@tonic-gate DBINFO_FCHK(dbp, "DB_DUP",
444*7c478bd9Sstevel@tonic-gate hashm->flags, DB_HASH_DUP, DB_AM_DUP);
445*7c478bd9Sstevel@tonic-gate type = DB_HASH;
446*7c478bd9Sstevel@tonic-gate
447*7c478bd9Sstevel@tonic-gate /* Copy the file's unique id. */
448*7c478bd9Sstevel@tonic-gate need_fileid = 0;
449*7c478bd9Sstevel@tonic-gate memcpy(dbp->fileid, hashm->uid, DB_FILE_ID_LEN);
450*7c478bd9Sstevel@tonic-gate break;
451*7c478bd9Sstevel@tonic-gate default:
452*7c478bd9Sstevel@tonic-gate if (swapped) {
453*7c478bd9Sstevel@tonic-gate __db_err(dbenv, "unrecognized file type");
454*7c478bd9Sstevel@tonic-gate goto einval;
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate M_32_SWAP(((BTMETA *)mbuf)->magic);
457*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_SWAP);
458*7c478bd9Sstevel@tonic-gate
459*7c478bd9Sstevel@tonic-gate swapped = 1;
460*7c478bd9Sstevel@tonic-gate goto retry;
461*7c478bd9Sstevel@tonic-gate }
462*7c478bd9Sstevel@tonic-gate } else {
463*7c478bd9Sstevel@tonic-gate fname = real_name = NULL;
464*7c478bd9Sstevel@tonic-gate
465*7c478bd9Sstevel@tonic-gate if (type == DB_UNKNOWN) {
466*7c478bd9Sstevel@tonic-gate __db_err(dbenv,
467*7c478bd9Sstevel@tonic-gate "DBTYPE of unknown without existing file");
468*7c478bd9Sstevel@tonic-gate goto einval;
469*7c478bd9Sstevel@tonic-gate }
470*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_INMEM);
471*7c478bd9Sstevel@tonic-gate }
472*7c478bd9Sstevel@tonic-gate
473*7c478bd9Sstevel@tonic-gate empty: /*
474*7c478bd9Sstevel@tonic-gate * By the time we get here we've either set the type or we're taking
475*7c478bd9Sstevel@tonic-gate * it from the user.
476*7c478bd9Sstevel@tonic-gate */
477*7c478bd9Sstevel@tonic-gate dbp->type = type;
478*7c478bd9Sstevel@tonic-gate
479*7c478bd9Sstevel@tonic-gate /*
480*7c478bd9Sstevel@tonic-gate * Set the page size to the best value for I/O to this file. Don't
481*7c478bd9Sstevel@tonic-gate * overflow the page offset type. The page size must be db_indx_t
482*7c478bd9Sstevel@tonic-gate * aligned and >= MIN_PAGE_SIZE.
483*7c478bd9Sstevel@tonic-gate *
484*7c478bd9Sstevel@tonic-gate * XXX
485*7c478bd9Sstevel@tonic-gate * Should we be checking for a page size that's not a multiple of 512?
486*7c478bd9Sstevel@tonic-gate */
487*7c478bd9Sstevel@tonic-gate if (dbp->pgsize == 0) {
488*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_PGDEF);
489*7c478bd9Sstevel@tonic-gate dbp->pgsize = 8 * 1024;
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate if (dbp->pgsize < DB_MIN_PGSIZE ||
492*7c478bd9Sstevel@tonic-gate dbp->pgsize > DB_MAX_PGSIZE ||
493*7c478bd9Sstevel@tonic-gate dbp->pgsize & (sizeof(db_indx_t) - 1)) {
494*7c478bd9Sstevel@tonic-gate __db_err(dbenv, "illegal page size");
495*7c478bd9Sstevel@tonic-gate goto einval;
496*7c478bd9Sstevel@tonic-gate }
497*7c478bd9Sstevel@tonic-gate
498*7c478bd9Sstevel@tonic-gate /*
499*7c478bd9Sstevel@tonic-gate * If no mpool supplied by the application, attach to a local,
500*7c478bd9Sstevel@tonic-gate * created buffer pool.
501*7c478bd9Sstevel@tonic-gate *
502*7c478bd9Sstevel@tonic-gate * XXX
503*7c478bd9Sstevel@tonic-gate * If the user has a DB_ENV structure, we have to use a temporary
504*7c478bd9Sstevel@tonic-gate * one so that we don't step on their values. If the user doesn't,
505*7c478bd9Sstevel@tonic-gate * we have to create one, and keep it around until the call to the
506*7c478bd9Sstevel@tonic-gate * memp_close() function. This is all so the mpool functions get
507*7c478bd9Sstevel@tonic-gate * the error stuff right.
508*7c478bd9Sstevel@tonic-gate */
509*7c478bd9Sstevel@tonic-gate if (dbenv == NULL || dbenv->mp_info == NULL) {
510*7c478bd9Sstevel@tonic-gate F_SET(dbp, DB_AM_MLOCAL);
511*7c478bd9Sstevel@tonic-gate
512*7c478bd9Sstevel@tonic-gate if (dbenv == NULL) {
513*7c478bd9Sstevel@tonic-gate if ((ret = __os_calloc(1,
514*7c478bd9Sstevel@tonic-gate sizeof(DB_ENV), &dbp->mp_dbenv)) != 0)
515*7c478bd9Sstevel@tonic-gate goto err;
516*7c478bd9Sstevel@tonic-gate
517*7c478bd9Sstevel@tonic-gate envp = dbp->mp_dbenv;
518*7c478bd9Sstevel@tonic-gate restore = 0;
519*7c478bd9Sstevel@tonic-gate } else {
520*7c478bd9Sstevel@tonic-gate t_dbenv = *dbenv;
521*7c478bd9Sstevel@tonic-gate
522*7c478bd9Sstevel@tonic-gate envp = dbenv;
523*7c478bd9Sstevel@tonic-gate restore = 1;
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate
526*7c478bd9Sstevel@tonic-gate /*
527*7c478bd9Sstevel@tonic-gate * Set and/or correct the cache size; must be a multiple of
528*7c478bd9Sstevel@tonic-gate * the page size.
529*7c478bd9Sstevel@tonic-gate */
530*7c478bd9Sstevel@tonic-gate if (dbinfo == NULL || dbinfo->db_cachesize == 0)
531*7c478bd9Sstevel@tonic-gate cachesize = dbp->pgsize * DB_MINCACHE;
532*7c478bd9Sstevel@tonic-gate else {
533*7c478bd9Sstevel@tonic-gate cachesize = dbinfo->db_cachesize;
534*7c478bd9Sstevel@tonic-gate if (cachesize & (dbp->pgsize - 1))
535*7c478bd9Sstevel@tonic-gate cachesize +=
536*7c478bd9Sstevel@tonic-gate (~cachesize & (dbp->pgsize - 1)) + 1;
537*7c478bd9Sstevel@tonic-gate if (cachesize < dbp->pgsize * DB_MINCACHE)
538*7c478bd9Sstevel@tonic-gate cachesize = dbp->pgsize * DB_MINCACHE;
539*7c478bd9Sstevel@tonic-gate if (cachesize < 20 * 1024)
540*7c478bd9Sstevel@tonic-gate cachesize = 20 * 1024;
541*7c478bd9Sstevel@tonic-gate }
542*7c478bd9Sstevel@tonic-gate envp->mp_size = cachesize;
543*7c478bd9Sstevel@tonic-gate
544*7c478bd9Sstevel@tonic-gate if ((ret = memp_open(NULL, DB_CREATE | DB_MPOOL_PRIVATE |
545*7c478bd9Sstevel@tonic-gate (F_ISSET(dbp, DB_AM_THREAD) ? DB_THREAD : 0),
546*7c478bd9Sstevel@tonic-gate __db_omode("rw----"), envp, &dbp->mp)) != 0)
547*7c478bd9Sstevel@tonic-gate goto err;
548*7c478bd9Sstevel@tonic-gate if (restore)
549*7c478bd9Sstevel@tonic-gate *dbenv = t_dbenv;
550*7c478bd9Sstevel@tonic-gate } else
551*7c478bd9Sstevel@tonic-gate dbp->mp = dbenv->mp_info;
552*7c478bd9Sstevel@tonic-gate
553*7c478bd9Sstevel@tonic-gate /* Register DB's pgin/pgout functions. */
554*7c478bd9Sstevel@tonic-gate if ((ret = memp_register(dbp->mp,
555*7c478bd9Sstevel@tonic-gate DB_FTYPE_BTREE, __bam_pgin, __bam_pgout)) != 0)
556*7c478bd9Sstevel@tonic-gate goto err;
557*7c478bd9Sstevel@tonic-gate if ((ret = memp_register(dbp->mp,
558*7c478bd9Sstevel@tonic-gate DB_FTYPE_HASH, __ham_pgin, __ham_pgout)) != 0)
559*7c478bd9Sstevel@tonic-gate goto err;
560*7c478bd9Sstevel@tonic-gate
561*7c478bd9Sstevel@tonic-gate /*
562*7c478bd9Sstevel@tonic-gate * If we don't already have one, get a unique file ID. If the file
563*7c478bd9Sstevel@tonic-gate * is a temporary file, then we have to create a unique file ID --
564*7c478bd9Sstevel@tonic-gate * no backing file will be created until the mpool cache is filled
565*7c478bd9Sstevel@tonic-gate * forcing it to go to disk. The created ID must never match any
566*7c478bd9Sstevel@tonic-gate * potential real file ID -- we know it won't because real file IDs
567*7c478bd9Sstevel@tonic-gate * contain a time stamp after the dev/ino pair, and we're simply
568*7c478bd9Sstevel@tonic-gate * storing a 4-byte locker ID.
569*7c478bd9Sstevel@tonic-gate *
570*7c478bd9Sstevel@tonic-gate * XXX
571*7c478bd9Sstevel@tonic-gate * Store the file id in the locker structure -- we can get it from
572*7c478bd9Sstevel@tonic-gate * there as necessary, and it saves having two copies.
573*7c478bd9Sstevel@tonic-gate */
574*7c478bd9Sstevel@tonic-gate if (need_fileid)
575*7c478bd9Sstevel@tonic-gate if (fname == NULL) {
576*7c478bd9Sstevel@tonic-gate memset(dbp->fileid, 0, DB_FILE_ID_LEN);
577*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_LOCKING) &&
578*7c478bd9Sstevel@tonic-gate (ret = lock_id(dbenv->lk_info,
579*7c478bd9Sstevel@tonic-gate (u_int32_t *)dbp->fileid)) != 0)
580*7c478bd9Sstevel@tonic-gate goto err;
581*7c478bd9Sstevel@tonic-gate } else
582*7c478bd9Sstevel@tonic-gate if ((ret = __os_fileid(dbenv,
583*7c478bd9Sstevel@tonic-gate real_name, 1, dbp->fileid)) != 0)
584*7c478bd9Sstevel@tonic-gate goto err;
585*7c478bd9Sstevel@tonic-gate
586*7c478bd9Sstevel@tonic-gate /* No further use for the real name. */
587*7c478bd9Sstevel@tonic-gate if (real_name != NULL)
588*7c478bd9Sstevel@tonic-gate __os_freestr(real_name);
589*7c478bd9Sstevel@tonic-gate real_name = NULL;
590*7c478bd9Sstevel@tonic-gate
591*7c478bd9Sstevel@tonic-gate /*
592*7c478bd9Sstevel@tonic-gate * Open a backing file in the memory pool.
593*7c478bd9Sstevel@tonic-gate *
594*7c478bd9Sstevel@tonic-gate * If we need to process the file's pages on I/O, set the file type.
595*7c478bd9Sstevel@tonic-gate * If it's a hash file, always call pgin and pgout routines. This
596*7c478bd9Sstevel@tonic-gate * means that hash files can never be mapped into process memory. If
597*7c478bd9Sstevel@tonic-gate * it's a btree file and requires swapping, we need to page the file
598*7c478bd9Sstevel@tonic-gate * in and out. This has to be right -- we can't mmap files that are
599*7c478bd9Sstevel@tonic-gate * being paged in and out.
600*7c478bd9Sstevel@tonic-gate */
601*7c478bd9Sstevel@tonic-gate if (type == DB_HASH)
602*7c478bd9Sstevel@tonic-gate ftype = DB_FTYPE_HASH;
603*7c478bd9Sstevel@tonic-gate else
604*7c478bd9Sstevel@tonic-gate ftype = F_ISSET(dbp, DB_AM_SWAP) ? DB_FTYPE_BTREE : 0;
605*7c478bd9Sstevel@tonic-gate pginfo.db_pagesize = dbp->pgsize;
606*7c478bd9Sstevel@tonic-gate pginfo.needswap = F_ISSET(dbp, DB_AM_SWAP);
607*7c478bd9Sstevel@tonic-gate pgcookie.data = &pginfo;
608*7c478bd9Sstevel@tonic-gate pgcookie.size = sizeof(DB_PGINFO);
609*7c478bd9Sstevel@tonic-gate
610*7c478bd9Sstevel@tonic-gate /*
611*7c478bd9Sstevel@tonic-gate * Set up additional memp_fopen information.
612*7c478bd9Sstevel@tonic-gate */
613*7c478bd9Sstevel@tonic-gate memset(&finfo, 0, sizeof(finfo));
614*7c478bd9Sstevel@tonic-gate finfo.ftype = ftype;
615*7c478bd9Sstevel@tonic-gate finfo.pgcookie = &pgcookie;
616*7c478bd9Sstevel@tonic-gate finfo.fileid = dbp->fileid;
617*7c478bd9Sstevel@tonic-gate finfo.lsn_offset = 0;
618*7c478bd9Sstevel@tonic-gate finfo.clear_len = DB_PAGE_CLEAR_LEN;
619*7c478bd9Sstevel@tonic-gate if ((ret = memp_fopen(dbp->mp, fname,
620*7c478bd9Sstevel@tonic-gate F_ISSET(dbp, DB_AM_RDONLY) ? DB_RDONLY : 0,
621*7c478bd9Sstevel@tonic-gate 0, dbp->pgsize, &finfo, &dbp->mpf)) != 0)
622*7c478bd9Sstevel@tonic-gate goto err;
623*7c478bd9Sstevel@tonic-gate
624*7c478bd9Sstevel@tonic-gate /*
625*7c478bd9Sstevel@tonic-gate * XXX
626*7c478bd9Sstevel@tonic-gate * We need a per-thread mutex that lives in shared memory -- HP-UX
627*7c478bd9Sstevel@tonic-gate * can't allocate mutexes in malloc'd memory. Allocate it from the
628*7c478bd9Sstevel@tonic-gate * shared memory region, since it's the only one that is guaranteed
629*7c478bd9Sstevel@tonic-gate * to exist.
630*7c478bd9Sstevel@tonic-gate */
631*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_THREAD)) {
632*7c478bd9Sstevel@tonic-gate if ((ret = __memp_reg_alloc(dbp->mp,
633*7c478bd9Sstevel@tonic-gate sizeof(db_mutex_t), NULL, &dbp->mutexp)) != 0)
634*7c478bd9Sstevel@tonic-gate goto err;
635*7c478bd9Sstevel@tonic-gate /*
636*7c478bd9Sstevel@tonic-gate * Since we only get here if DB_THREAD was specified, we know
637*7c478bd9Sstevel@tonic-gate * we have spinlocks and no file offset argument is needed.
638*7c478bd9Sstevel@tonic-gate */
639*7c478bd9Sstevel@tonic-gate (void)__db_mutex_init(dbp->mutexp, 0);
640*7c478bd9Sstevel@tonic-gate }
641*7c478bd9Sstevel@tonic-gate
642*7c478bd9Sstevel@tonic-gate /* Get a log file id. */
643*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_LOGGING) &&
644*7c478bd9Sstevel@tonic-gate (ret = log_register(dbenv->lg_info,
645*7c478bd9Sstevel@tonic-gate dbp, fname, type, &dbp->log_fileid)) != 0)
646*7c478bd9Sstevel@tonic-gate goto err;
647*7c478bd9Sstevel@tonic-gate
648*7c478bd9Sstevel@tonic-gate /* Call the real open function. */
649*7c478bd9Sstevel@tonic-gate switch (type) {
650*7c478bd9Sstevel@tonic-gate case DB_BTREE:
651*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL && (ret = __db_fchk(dbenv,
652*7c478bd9Sstevel@tonic-gate "db_open", dbinfo->flags, DB_RECNUM | DB_DUP)) != 0)
653*7c478bd9Sstevel@tonic-gate goto err;
654*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL && (ret = __db_fcchk(dbenv,
655*7c478bd9Sstevel@tonic-gate "db_open", dbinfo->flags, DB_DUP, DB_RECNUM)) != 0)
656*7c478bd9Sstevel@tonic-gate goto err;
657*7c478bd9Sstevel@tonic-gate if ((ret = __bam_open(dbp, dbinfo)) != 0)
658*7c478bd9Sstevel@tonic-gate goto err;
659*7c478bd9Sstevel@tonic-gate break;
660*7c478bd9Sstevel@tonic-gate case DB_HASH:
661*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL && (ret = __db_fchk(dbenv,
662*7c478bd9Sstevel@tonic-gate "db_open", dbinfo->flags, DB_DUP)) != 0)
663*7c478bd9Sstevel@tonic-gate goto err;
664*7c478bd9Sstevel@tonic-gate if ((ret = __ham_open(dbp, dbinfo)) != 0)
665*7c478bd9Sstevel@tonic-gate goto err;
666*7c478bd9Sstevel@tonic-gate break;
667*7c478bd9Sstevel@tonic-gate case DB_RECNO:
668*7c478bd9Sstevel@tonic-gate #define DB_INFO_FLAGS \
669*7c478bd9Sstevel@tonic-gate (DB_DELIMITER | DB_FIXEDLEN | DB_PAD | DB_RENUMBER | DB_SNAPSHOT)
670*7c478bd9Sstevel@tonic-gate if (dbinfo != NULL && (ret = __db_fchk(dbenv,
671*7c478bd9Sstevel@tonic-gate "db_open", dbinfo->flags, DB_INFO_FLAGS)) != 0)
672*7c478bd9Sstevel@tonic-gate goto err;
673*7c478bd9Sstevel@tonic-gate if ((ret = __ram_open(dbp, dbinfo)) != 0)
674*7c478bd9Sstevel@tonic-gate goto err;
675*7c478bd9Sstevel@tonic-gate break;
676*7c478bd9Sstevel@tonic-gate default:
677*7c478bd9Sstevel@tonic-gate abort();
678*7c478bd9Sstevel@tonic-gate }
679*7c478bd9Sstevel@tonic-gate
680*7c478bd9Sstevel@tonic-gate *dbpp = dbp;
681*7c478bd9Sstevel@tonic-gate return (0);
682*7c478bd9Sstevel@tonic-gate
683*7c478bd9Sstevel@tonic-gate einval: ret = EINVAL;
684*7c478bd9Sstevel@tonic-gate err: /* Close the file descriptor. */
685*7c478bd9Sstevel@tonic-gate if (fd != -1)
686*7c478bd9Sstevel@tonic-gate (void)__os_close(fd);
687*7c478bd9Sstevel@tonic-gate
688*7c478bd9Sstevel@tonic-gate /* Discard the log file id. */
689*7c478bd9Sstevel@tonic-gate if (dbp->log_fileid != 0)
690*7c478bd9Sstevel@tonic-gate (void)log_unregister(dbenv->lg_info, dbp->log_fileid);
691*7c478bd9Sstevel@tonic-gate
692*7c478bd9Sstevel@tonic-gate /* Close the memory pool file. */
693*7c478bd9Sstevel@tonic-gate if (dbp->mpf != NULL)
694*7c478bd9Sstevel@tonic-gate (void)memp_fclose(dbp->mpf);
695*7c478bd9Sstevel@tonic-gate
696*7c478bd9Sstevel@tonic-gate /* If the memory pool was local, close it. */
697*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_MLOCAL) && dbp->mp != NULL)
698*7c478bd9Sstevel@tonic-gate (void)memp_close(dbp->mp);
699*7c478bd9Sstevel@tonic-gate
700*7c478bd9Sstevel@tonic-gate /* If we allocated a DB_ENV, discard it. */
701*7c478bd9Sstevel@tonic-gate if (dbp->mp_dbenv != NULL)
702*7c478bd9Sstevel@tonic-gate __os_free(dbp->mp_dbenv, sizeof(DB_ENV));
703*7c478bd9Sstevel@tonic-gate
704*7c478bd9Sstevel@tonic-gate if (real_name != NULL)
705*7c478bd9Sstevel@tonic-gate __os_freestr(real_name);
706*7c478bd9Sstevel@tonic-gate if (dbp != NULL)
707*7c478bd9Sstevel@tonic-gate __os_free(dbp, sizeof(DB));
708*7c478bd9Sstevel@tonic-gate
709*7c478bd9Sstevel@tonic-gate return (ret);
710*7c478bd9Sstevel@tonic-gate }
711*7c478bd9Sstevel@tonic-gate
712*7c478bd9Sstevel@tonic-gate /*
713*7c478bd9Sstevel@tonic-gate * __db_close --
714*7c478bd9Sstevel@tonic-gate * Close a DB tree.
715*7c478bd9Sstevel@tonic-gate *
716*7c478bd9Sstevel@tonic-gate * PUBLIC: int __db_close __P((DB *, u_int32_t));
717*7c478bd9Sstevel@tonic-gate */
718*7c478bd9Sstevel@tonic-gate int
__db_close(dbp,flags)719*7c478bd9Sstevel@tonic-gate __db_close(dbp, flags)
720*7c478bd9Sstevel@tonic-gate DB *dbp;
721*7c478bd9Sstevel@tonic-gate u_int32_t flags;
722*7c478bd9Sstevel@tonic-gate {
723*7c478bd9Sstevel@tonic-gate DBC *dbc;
724*7c478bd9Sstevel@tonic-gate int ret, t_ret;
725*7c478bd9Sstevel@tonic-gate
726*7c478bd9Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
727*7c478bd9Sstevel@tonic-gate
728*7c478bd9Sstevel@tonic-gate /* Validate arguments. */
729*7c478bd9Sstevel@tonic-gate if ((ret = __db_closechk(dbp, flags)) != 0)
730*7c478bd9Sstevel@tonic-gate return (ret);
731*7c478bd9Sstevel@tonic-gate
732*7c478bd9Sstevel@tonic-gate /* Sync the underlying file. */
733*7c478bd9Sstevel@tonic-gate if (flags != DB_NOSYNC &&
734*7c478bd9Sstevel@tonic-gate (t_ret = dbp->sync(dbp, 0)) != 0 && ret == 0)
735*7c478bd9Sstevel@tonic-gate ret = t_ret;
736*7c478bd9Sstevel@tonic-gate
737*7c478bd9Sstevel@tonic-gate /*
738*7c478bd9Sstevel@tonic-gate * Go through the active cursors and call the cursor recycle routine,
739*7c478bd9Sstevel@tonic-gate * which resolves pending operations and moves the cursors onto the
740*7c478bd9Sstevel@tonic-gate * free list. Then, walk the free list and call the cursor destroy
741*7c478bd9Sstevel@tonic-gate * routine.
742*7c478bd9Sstevel@tonic-gate */
743*7c478bd9Sstevel@tonic-gate while ((dbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
744*7c478bd9Sstevel@tonic-gate if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
745*7c478bd9Sstevel@tonic-gate ret = t_ret;
746*7c478bd9Sstevel@tonic-gate while ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
747*7c478bd9Sstevel@tonic-gate if ((t_ret = __db_c_destroy(dbc)) != 0 && ret == 0)
748*7c478bd9Sstevel@tonic-gate ret = t_ret;
749*7c478bd9Sstevel@tonic-gate
750*7c478bd9Sstevel@tonic-gate /* Call the access specific close function. */
751*7c478bd9Sstevel@tonic-gate if ((t_ret = dbp->am_close(dbp)) != 0 && ret == 0)
752*7c478bd9Sstevel@tonic-gate ret = t_ret;
753*7c478bd9Sstevel@tonic-gate
754*7c478bd9Sstevel@tonic-gate /* Sync the memory pool. */
755*7c478bd9Sstevel@tonic-gate if (flags != DB_NOSYNC && (t_ret = memp_fsync(dbp->mpf)) != 0 &&
756*7c478bd9Sstevel@tonic-gate t_ret != DB_INCOMPLETE && ret == 0)
757*7c478bd9Sstevel@tonic-gate ret = t_ret;
758*7c478bd9Sstevel@tonic-gate
759*7c478bd9Sstevel@tonic-gate /* Close the memory pool file. */
760*7c478bd9Sstevel@tonic-gate if ((t_ret = memp_fclose(dbp->mpf)) != 0 && ret == 0)
761*7c478bd9Sstevel@tonic-gate ret = t_ret;
762*7c478bd9Sstevel@tonic-gate
763*7c478bd9Sstevel@tonic-gate /* If the memory pool was local, close it. */
764*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_MLOCAL) &&
765*7c478bd9Sstevel@tonic-gate (t_ret = memp_close(dbp->mp)) != 0 && ret == 0)
766*7c478bd9Sstevel@tonic-gate ret = t_ret;
767*7c478bd9Sstevel@tonic-gate
768*7c478bd9Sstevel@tonic-gate if (dbp->saved_open_fd != -1) {
769*7c478bd9Sstevel@tonic-gate (void)__os_close(dbp->saved_open_fd);
770*7c478bd9Sstevel@tonic-gate dbp->saved_open_fd = -1;
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate
773*7c478bd9Sstevel@tonic-gate /* Discard the log file id. */
774*7c478bd9Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_LOGGING))
775*7c478bd9Sstevel@tonic-gate (void)log_unregister(dbp->dbenv->lg_info, dbp->log_fileid);
776*7c478bd9Sstevel@tonic-gate
777*7c478bd9Sstevel@tonic-gate /* If we allocated a DB_ENV, discard it. */
778*7c478bd9Sstevel@tonic-gate if (dbp->mp_dbenv != NULL)
779*7c478bd9Sstevel@tonic-gate __os_free(dbp->mp_dbenv, sizeof(DB_ENV));
780*7c478bd9Sstevel@tonic-gate
781*7c478bd9Sstevel@tonic-gate /* Free the DB. */
782*7c478bd9Sstevel@tonic-gate __os_free(dbp, sizeof(*dbp));
783*7c478bd9Sstevel@tonic-gate
784*7c478bd9Sstevel@tonic-gate return (ret);
785*7c478bd9Sstevel@tonic-gate }
786