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