]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1990, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * This code is derived from software contributed to Berkeley by | |
27 | * Mike Olson. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | ||
59 | /* | |
60 | * Implementation of btree access method for 4.4BSD. | |
61 | * | |
62 | * The design here was originally based on that of the btree access method | |
63 | * used in the Postgres database system at UC Berkeley. This implementation | |
64 | * is wholly independent of the Postgres code. | |
65 | */ | |
66 | ||
67 | #include <sys/param.h> | |
68 | #include <sys/stat.h> | |
69 | ||
70 | #include <errno.h> | |
71 | #include <fcntl.h> | |
72 | #include <limits.h> | |
73 | #include <signal.h> | |
74 | #include <stdio.h> | |
75 | #include <stdlib.h> | |
76 | #include <string.h> | |
77 | #include <unistd.h> | |
78 | ||
79 | #include <db.h> | |
80 | #include "btree.h" | |
81 | ||
82 | static int byteorder __P((void)); | |
83 | static int nroot __P((BTREE *)); | |
84 | static int tmp __P((void)); | |
85 | ||
86 | /* | |
87 | * __BT_OPEN -- Open a btree. | |
88 | * | |
89 | * Creates and fills a DB struct, and calls the routine that actually | |
90 | * opens the btree. | |
91 | * | |
92 | * Parameters: | |
93 | * fname: filename (NULL for in-memory trees) | |
94 | * flags: open flag bits | |
95 | * mode: open permission bits | |
96 | * b: BTREEINFO pointer | |
97 | * | |
98 | * Returns: | |
99 | * NULL on failure, pointer to DB on success. | |
100 | * | |
101 | */ | |
102 | DB * | |
103 | __bt_open(fname, flags, mode, openinfo, dflags) | |
104 | const char *fname; | |
105 | int flags, mode, dflags; | |
106 | const BTREEINFO *openinfo; | |
107 | { | |
108 | struct stat sb; | |
109 | BTMETA m; | |
110 | BTREE *t; | |
111 | BTREEINFO b; | |
112 | DB *dbp; | |
113 | pgno_t ncache; | |
114 | ssize_t nr; | |
115 | int machine_lorder; | |
116 | ||
117 | t = NULL; | |
118 | ||
119 | /* | |
120 | * Intention is to make sure all of the user's selections are okay | |
121 | * here and then use them without checking. Can't be complete, since | |
122 | * we don't know the right page size, lorder or flags until the backing | |
123 | * file is opened. Also, the file's page size can cause the cachesize | |
124 | * to change. | |
125 | */ | |
126 | machine_lorder = byteorder(); | |
127 | if (openinfo) { | |
128 | b = *openinfo; | |
129 | ||
130 | /* Flags: R_DUP. */ | |
131 | if (b.flags & ~(R_DUP)) | |
132 | goto einval; | |
133 | ||
134 | /* | |
135 | * Page size must be indx_t aligned and >= MINPSIZE. Default | |
136 | * page size is set farther on, based on the underlying file | |
137 | * transfer size. | |
138 | */ | |
139 | if (b.psize && | |
140 | (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 || | |
141 | b.psize & sizeof(indx_t) - 1)) | |
142 | goto einval; | |
143 | ||
144 | /* Minimum number of keys per page; absolute minimum is 2. */ | |
145 | if (b.minkeypage) { | |
146 | if (b.minkeypage < 2) | |
147 | goto einval; | |
148 | } else | |
149 | b.minkeypage = DEFMINKEYPAGE; | |
150 | ||
151 | /* If no comparison, use default comparison and prefix. */ | |
152 | if (b.compare == NULL) { | |
153 | b.compare = __bt_defcmp; | |
154 | if (b.prefix == NULL) | |
155 | b.prefix = __bt_defpfx; | |
156 | } | |
157 | ||
158 | if (b.lorder == 0) | |
159 | b.lorder = machine_lorder; | |
160 | } else { | |
161 | b.compare = __bt_defcmp; | |
162 | b.cachesize = 0; | |
163 | b.flags = 0; | |
164 | b.lorder = machine_lorder; | |
165 | b.minkeypage = DEFMINKEYPAGE; | |
166 | b.prefix = __bt_defpfx; | |
167 | b.psize = 0; | |
168 | } | |
169 | ||
170 | /* Check for the ubiquitous PDP-11. */ | |
171 | if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) | |
172 | goto einval; | |
173 | ||
174 | /* Allocate and initialize DB and BTREE structures. */ | |
175 | if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL) | |
176 | goto err; | |
177 | memset(t, 0, sizeof(BTREE)); | |
178 | t->bt_bcursor.pgno = P_INVALID; | |
179 | t->bt_fd = -1; /* Don't close unopened fd on error. */ | |
180 | t->bt_lorder = b.lorder; | |
181 | t->bt_order = NOT; | |
182 | t->bt_cmp = b.compare; | |
183 | t->bt_pfx = b.prefix; | |
184 | t->bt_rfd = -1; | |
185 | ||
186 | if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL) | |
187 | goto err; | |
188 | t->bt_flags = 0; | |
189 | if (t->bt_lorder != machine_lorder) | |
190 | SET(t, B_NEEDSWAP); | |
191 | ||
192 | dbp->type = DB_BTREE; | |
193 | dbp->internal = t; | |
194 | dbp->close = __bt_close; | |
195 | dbp->del = __bt_delete; | |
196 | dbp->fd = __bt_fd; | |
197 | dbp->get = __bt_get; | |
198 | dbp->put = __bt_put; | |
199 | dbp->seq = __bt_seq; | |
200 | dbp->sync = __bt_sync; | |
201 | ||
202 | /* | |
203 | * If no file name was supplied, this is an in-memory btree and we | |
204 | * open a backing temporary file. Otherwise, it's a disk-based tree. | |
205 | */ | |
206 | if (fname) { | |
207 | switch(flags & O_ACCMODE) { | |
208 | case O_RDONLY: | |
209 | SET(t, B_RDONLY); | |
210 | break; | |
211 | case O_RDWR: | |
212 | break; | |
213 | case O_WRONLY: | |
214 | default: | |
215 | goto einval; | |
216 | } | |
217 | ||
218 | if ((t->bt_fd = open(fname, flags, mode)) < 0) | |
219 | goto err; | |
220 | ||
221 | } else { | |
222 | if ((flags & O_ACCMODE) != O_RDWR) | |
223 | goto einval; | |
224 | if ((t->bt_fd = tmp()) == -1) | |
225 | goto err; | |
226 | SET(t, B_INMEM); | |
227 | } | |
228 | ||
229 | if (fcntl(t->bt_fd, F_SETFD, 1) == -1) | |
230 | goto err; | |
231 | ||
232 | if (fstat(t->bt_fd, &sb)) | |
233 | goto err; | |
234 | if (sb.st_size) { | |
235 | nr = read(t->bt_fd, &m, sizeof(BTMETA)); | |
236 | if (nr < 0) | |
237 | goto err; | |
238 | if (nr != sizeof(BTMETA)) | |
239 | goto eftype; | |
240 | ||
241 | /* | |
242 | * Read in the meta-data. This can change the notion of what | |
243 | * the lorder, page size and flags are, and, when the page size | |
244 | * changes, the cachesize value can change too. If the user | |
245 | * specified the wrong byte order for an existing database, we | |
246 | * don't bother to return an error, we just clear the NEEDSWAP | |
247 | * bit. | |
248 | */ | |
249 | if (m.m_magic == BTREEMAGIC) | |
250 | CLR(t, B_NEEDSWAP); | |
251 | else { | |
252 | SET(t, B_NEEDSWAP); | |
253 | M_32_SWAP(m.m_magic); | |
254 | M_32_SWAP(m.m_version); | |
255 | M_32_SWAP(m.m_psize); | |
256 | M_32_SWAP(m.m_free); | |
257 | M_32_SWAP(m.m_nrecs); | |
258 | M_32_SWAP(m.m_flags); | |
259 | } | |
260 | if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) | |
261 | goto eftype; | |
262 | if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 || | |
263 | m.m_psize & sizeof(indx_t) - 1) | |
264 | goto eftype; | |
265 | if (m.m_flags & ~SAVEMETA) | |
266 | goto eftype; | |
267 | b.psize = m.m_psize; | |
268 | t->bt_flags |= m.m_flags; | |
269 | t->bt_free = m.m_free; | |
270 | t->bt_nrecs = m.m_nrecs; | |
271 | } else { | |
272 | /* | |
273 | * Set the page size to the best value for I/O to this file. | |
274 | * Don't overflow the page offset type. | |
275 | */ | |
276 | if (b.psize == 0) { | |
277 | b.psize = sb.st_blksize; | |
278 | if (b.psize < MINPSIZE) | |
279 | b.psize = MINPSIZE; | |
280 | if (b.psize > MAX_PAGE_OFFSET + 1) | |
281 | b.psize = MAX_PAGE_OFFSET + 1; | |
282 | } | |
283 | ||
284 | /* Set flag if duplicates permitted. */ | |
285 | if (!(b.flags & R_DUP)) | |
286 | SET(t, B_NODUPS); | |
287 | ||
288 | t->bt_free = P_INVALID; | |
289 | t->bt_nrecs = 0; | |
290 | SET(t, B_METADIRTY); | |
291 | } | |
292 | ||
293 | t->bt_psize = b.psize; | |
294 | ||
295 | /* Set the cache size; must be a multiple of the page size. */ | |
296 | if (b.cachesize && b.cachesize & b.psize - 1) | |
297 | b.cachesize += (~b.cachesize & b.psize - 1) + 1; | |
298 | if (b.cachesize < b.psize * MINCACHE) | |
299 | b.cachesize = b.psize * MINCACHE; | |
300 | ||
301 | /* Calculate number of pages to cache. */ | |
302 | ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; | |
303 | ||
304 | /* | |
305 | * The btree data structure requires that at least two keys can fit on | |
306 | * a page, but other than that there's no fixed requirement. The user | |
307 | * specified a minimum number per page, and we translated that into the | |
308 | * number of bytes a key/data pair can use before being placed on an | |
309 | * overflow page. This calculation includes the page header, the size | |
310 | * of the index referencing the leaf item and the size of the leaf item | |
311 | * structure. Also, don't let the user specify a minkeypage such that | |
312 | * a key/data pair won't fit even if both key and data are on overflow | |
313 | * pages. | |
314 | */ | |
315 | t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - | |
316 | (sizeof(indx_t) + NBLEAFDBT(0, 0)); | |
317 | if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) | |
318 | t->bt_ovflsize = | |
319 | NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t); | |
320 | ||
321 | /* Initialize the buffer pool. */ | |
322 | if ((t->bt_mp = | |
323 | mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) | |
324 | goto err; | |
325 | if (!ISSET(t, B_INMEM)) | |
326 | mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); | |
327 | ||
328 | /* Create a root page if new tree. */ | |
329 | if (nroot(t) == RET_ERROR) | |
330 | goto err; | |
331 | ||
332 | /* Global flags. */ | |
333 | if (dflags & DB_LOCK) | |
334 | SET(t, B_DB_LOCK); | |
335 | if (dflags & DB_SHMEM) | |
336 | SET(t, B_DB_SHMEM); | |
337 | if (dflags & DB_TXN) | |
338 | SET(t, B_DB_TXN); | |
339 | ||
340 | return (dbp); | |
341 | ||
342 | einval: errno = EINVAL; | |
343 | goto err; | |
344 | ||
345 | eftype: errno = EFTYPE; | |
346 | goto err; | |
347 | ||
348 | err: if (t) { | |
349 | if (t->bt_dbp) | |
350 | free(t->bt_dbp); | |
351 | if (t->bt_fd != -1) | |
352 | (void)close(t->bt_fd); | |
353 | free(t); | |
354 | } | |
355 | return (NULL); | |
356 | } | |
357 | ||
358 | /* | |
359 | * NROOT -- Create the root of a new tree. | |
360 | * | |
361 | * Parameters: | |
362 | * t: tree | |
363 | * | |
364 | * Returns: | |
365 | * RET_ERROR, RET_SUCCESS | |
366 | */ | |
367 | static int | |
368 | nroot(t) | |
369 | BTREE *t; | |
370 | { | |
371 | PAGE *meta, *root; | |
372 | pgno_t npg; | |
373 | ||
374 | if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { | |
375 | mpool_put(t->bt_mp, meta, 0); | |
376 | return (RET_SUCCESS); | |
377 | } | |
378 | if (errno != EINVAL) | |
379 | return (RET_ERROR); | |
380 | ||
381 | if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) | |
382 | return (RET_ERROR); | |
383 | ||
384 | if ((root = mpool_new(t->bt_mp, &npg)) == NULL) | |
385 | return (RET_ERROR); | |
386 | ||
387 | if (npg != P_ROOT) | |
388 | return (RET_ERROR); | |
389 | root->pgno = npg; | |
390 | root->prevpg = root->nextpg = P_INVALID; | |
391 | root->lower = BTDATAOFF; | |
392 | root->upper = t->bt_psize; | |
393 | root->flags = P_BLEAF; | |
394 | memset(meta, 0, t->bt_psize); | |
395 | mpool_put(t->bt_mp, meta, MPOOL_DIRTY); | |
396 | mpool_put(t->bt_mp, root, MPOOL_DIRTY); | |
397 | return (RET_SUCCESS); | |
398 | } | |
399 | ||
400 | static int | |
401 | tmp() | |
402 | { | |
403 | sigset_t set, oset; | |
404 | int fd; | |
405 | char *envtmp; | |
406 | char path[MAXPATHLEN]; | |
407 | ||
408 | envtmp = getenv("TMPDIR"); | |
409 | (void)snprintf(path, | |
410 | sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); | |
411 | ||
412 | (void)sigfillset(&set); | |
413 | (void)sigprocmask(SIG_BLOCK, &set, &oset); | |
414 | if ((fd = mkstemp(path)) != -1) | |
415 | (void)unlink(path); | |
416 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); | |
417 | return(fd); | |
418 | } | |
419 | ||
420 | static int | |
421 | byteorder() | |
422 | { | |
423 | u_int32_t x; | |
424 | u_char *p; | |
425 | ||
426 | x = 0x01020304; | |
427 | p = (u_char *)&x; | |
428 | switch (*p) { | |
429 | case 1: | |
430 | return (BIG_ENDIAN); | |
431 | case 4: | |
432 | return (LITTLE_ENDIAN); | |
433 | default: | |
434 | return (0); | |
435 | } | |
436 | } | |
437 | ||
438 | int | |
439 | __bt_fd(dbp) | |
440 | const DB *dbp; | |
441 | { | |
442 | BTREE *t; | |
443 | ||
444 | t = dbp->internal; | |
445 | ||
446 | /* Toss any page pinned across calls. */ | |
447 | if (t->bt_pinned != NULL) { | |
448 | mpool_put(t->bt_mp, t->bt_pinned, 0); | |
449 | t->bt_pinned = NULL; | |
450 | } | |
451 | ||
452 | /* In-memory database can't have a file descriptor. */ | |
453 | if (ISSET(t, B_INMEM)) { | |
454 | errno = ENOENT; | |
455 | return (-1); | |
456 | } | |
457 | return (t->bt_fd); | |
458 | } |