]>
git.saurik.com Git - apple/libc.git/blob - db/btree/bt_open.c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid
[] = "@(#)bt_open.c 8.10 (Berkeley) 8/17/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
65 * Implementation of btree access method for 4.4BSD.
67 * The design here was originally based on that of the btree access method
68 * used in the Postgres database system at UC Berkeley. This implementation
69 * is wholly independent of the Postgres code.
72 #include <sys/param.h>
92 static int byteorder(void);
93 static int nroot(BTREE
*);
97 * __BT_OPEN -- Open a btree.
99 * Creates and fills a DB struct, and calls the routine that actually
103 * fname: filename (NULL for in-memory trees)
104 * flags: open flag bits
105 * mode: open permission bits
106 * b: BTREEINFO pointer
109 * NULL on failure, pointer to DB on success.
113 __bt_open(fname
, flags
, mode
, openinfo
, dflags
)
115 int flags
, mode
, dflags
;
116 const BTREEINFO
*openinfo
;
130 * Intention is to make sure all of the user's selections are okay
131 * here and then use them without checking. Can't be complete, since
132 * we don't know the right page size, lorder or flags until the backing
133 * file is opened. Also, the file's page size can cause the cachesize
136 machine_lorder
= byteorder();
141 if (b
.flags
& ~(R_DUP
))
145 * Page size must be indx_t aligned and >= MINPSIZE. Default
146 * page size is set farther on, based on the underlying file
150 (b
.psize
< MINPSIZE
|| b
.psize
> MAX_PAGE_OFFSET
+ 1 ||
151 b
.psize
& (sizeof(indx_t
) - 1) ))
154 /* Minimum number of keys per page; absolute minimum is 2. */
156 if (b
.minkeypage
< 2)
159 b
.minkeypage
= DEFMINKEYPAGE
;
161 /* If no comparison, use default comparison and prefix. */
162 if (b
.compare
== NULL
) {
163 b
.compare
= __bt_defcmp
;
164 if (b
.prefix
== NULL
)
165 b
.prefix
= __bt_defpfx
;
169 b
.lorder
= machine_lorder
;
171 b
.compare
= __bt_defcmp
;
174 b
.lorder
= machine_lorder
;
175 b
.minkeypage
= DEFMINKEYPAGE
;
176 b
.prefix
= __bt_defpfx
;
180 /* Check for the ubiquitous PDP-11. */
181 if (b
.lorder
!= BIG_ENDIAN
&& b
.lorder
!= LITTLE_ENDIAN
)
184 /* Allocate and initialize DB and BTREE structures. */
185 if ((t
= (BTREE
*)malloc(sizeof(BTREE
))) == NULL
)
187 memset(t
, 0, sizeof(BTREE
));
188 t
->bt_fd
= -1; /* Don't close unopened fd on error. */
189 t
->bt_lorder
= b
.lorder
;
191 t
->bt_cmp
= b
.compare
;
192 t
->bt_pfx
= b
.prefix
;
195 if ((t
->bt_dbp
= dbp
= (DB
*)malloc(sizeof(DB
))) == NULL
)
197 memset(t
->bt_dbp
, 0, sizeof(DB
));
198 if (t
->bt_lorder
!= machine_lorder
)
199 F_SET(t
, B_NEEDSWAP
);
201 dbp
->type
= DB_BTREE
;
203 dbp
->close
= __bt_close
;
204 dbp
->del
= __bt_delete
;
209 dbp
->sync
= __bt_sync
;
212 * If no file name was supplied, this is an in-memory btree and we
213 * open a backing temporary file. Otherwise, it's a disk-based tree.
216 switch (flags
& O_ACCMODE
) {
227 if ((t
->bt_fd
= open(fname
, flags
, mode
)) < 0)
231 if ((flags
& O_ACCMODE
) != O_RDWR
)
233 if ((t
->bt_fd
= tmp()) == -1)
238 if (fcntl(t
->bt_fd
, F_SETFD
, 1) == -1)
241 if (fstat(t
->bt_fd
, &sb
))
244 if ((nr
= read(t
->bt_fd
, &m
, sizeof(BTMETA
))) < 0)
246 if (nr
!= sizeof(BTMETA
))
250 * Read in the meta-data. This can change the notion of what
251 * the lorder, page size and flags are, and, when the page size
252 * changes, the cachesize value can change too. If the user
253 * specified the wrong byte order for an existing database, we
254 * don't bother to return an error, we just clear the NEEDSWAP
257 if (m
.magic
== BTREEMAGIC
)
258 F_CLR(t
, B_NEEDSWAP
);
260 F_SET(t
, B_NEEDSWAP
);
262 M_32_SWAP(m
.version
);
268 if (m
.magic
!= BTREEMAGIC
|| m
.version
!= BTREEVERSION
)
270 if (m
.psize
< MINPSIZE
|| m
.psize
> MAX_PAGE_OFFSET
+ 1 ||
271 m
.psize
& (sizeof(indx_t
) - 1) )
273 if (m
.flags
& ~SAVEMETA
)
278 t
->bt_nrecs
= m
.nrecs
;
281 * Set the page size to the best value for I/O to this file.
282 * Don't overflow the page offset type.
285 b
.psize
= sb
.st_blksize
;
286 if (b
.psize
< MINPSIZE
)
288 if (b
.psize
> MAX_PAGE_OFFSET
+ 1)
289 b
.psize
= MAX_PAGE_OFFSET
+ 1;
292 /* Set flag if duplicates permitted. */
293 if (!(b
.flags
& R_DUP
))
296 t
->bt_free
= P_INVALID
;
298 F_SET(t
, B_METADIRTY
);
301 t
->bt_psize
= b
.psize
;
303 /* Set the cache size; must be a multiple of the page size. */
304 if (b
.cachesize
&& b
.cachesize
& (b
.psize
- 1) )
305 b
.cachesize
+= (~b
.cachesize
& (b
.psize
- 1) ) + 1;
306 if (b
.cachesize
< b
.psize
* MINCACHE
)
307 b
.cachesize
= b
.psize
* MINCACHE
;
309 /* Calculate number of pages to cache. */
310 ncache
= (b
.cachesize
+ t
->bt_psize
- 1) / t
->bt_psize
;
313 * The btree data structure requires that at least two keys can fit on
314 * a page, but other than that there's no fixed requirement. The user
315 * specified a minimum number per page, and we translated that into the
316 * number of bytes a key/data pair can use before being placed on an
317 * overflow page. This calculation includes the page header, the size
318 * of the index referencing the leaf item and the size of the leaf item
319 * structure. Also, don't let the user specify a minkeypage such that
320 * a key/data pair won't fit even if both key and data are on overflow
323 t
->bt_ovflsize
= (t
->bt_psize
- BTDATAOFF
) / b
.minkeypage
-
324 (sizeof(indx_t
) + NBLEAFDBT(0, 0));
325 if (t
->bt_ovflsize
< NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
))
327 NBLEAFDBT(NOVFLSIZE
, NOVFLSIZE
) + sizeof(indx_t
);
329 /* Initialize the buffer pool. */
331 mpool_open(NULL
, t
->bt_fd
, t
->bt_psize
, ncache
)) == NULL
)
333 if (!F_ISSET(t
, B_INMEM
))
334 mpool_filter(t
->bt_mp
, __bt_pgin
, __bt_pgout
, t
);
336 /* Create a root page if new tree. */
337 if (nroot(t
) == RET_ERROR
)
341 if (dflags
& DB_LOCK
)
343 if (dflags
& DB_SHMEM
)
344 F_SET(t
, B_DB_SHMEM
);
350 einval
: errno
= EINVAL
;
353 eftype
: errno
= EFTYPE
;
360 (void)close(t
->bt_fd
);
367 * NROOT -- Create the root of a new tree.
373 * RET_ERROR, RET_SUCCESS
382 if ((meta
= mpool_get(t
->bt_mp
, 0, 0)) != NULL
) {
383 mpool_put(t
->bt_mp
, meta
, 0);
384 return (RET_SUCCESS
);
386 if (errno
!= EINVAL
) /* It's OK to not exist. */
390 if ((meta
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
393 if ((root
= mpool_new(t
->bt_mp
, &npg
)) == NULL
)
399 root
->prevpg
= root
->nextpg
= P_INVALID
;
400 root
->lower
= BTDATAOFF
;
401 root
->upper
= t
->bt_psize
;
402 root
->flags
= P_BLEAF
;
403 memset(meta
, 0, t
->bt_psize
);
404 mpool_put(t
->bt_mp
, meta
, MPOOL_DIRTY
);
405 mpool_put(t
->bt_mp
, root
, MPOOL_DIRTY
);
406 return (RET_SUCCESS
);
415 char path
[MAXPATHLEN
];
417 if (issetugid() == 0)
418 envtmp
= getenv("TMPDIR");
420 sizeof(path
), "%s/bt.XXXXXXXXXX", envtmp
? envtmp
: "/tmp");
422 (void)sigfillset(&set
);
423 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
424 if ((fd
= mkstemp(path
)) != -1)
426 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);
442 return (LITTLE_ENDIAN
);
456 /* Toss any page pinned across calls. */
457 if (t
->bt_pinned
!= NULL
) {
458 mpool_put(t
->bt_mp
, t
->bt_pinned
, 0);
462 /* In-memory database can't have a file descriptor. */
463 if (F_ISSET(t
, B_INMEM
)) {