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