1 /* $NetBSD: bt_open.c,v 1.13 1998/12/09 12:42:46 christos 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.13 1998/12/09 12:42:46 christos 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/param.h> 58 #include <sys/stat.h> 59 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <limits.h> 63 #include <signal.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 #include <paths.h> 69 70 #include <db.h> 71 #include "btree.h" 72 73 #ifdef DEBUG 74 #undef MINPSIZE 75 #define MINPSIZE 128 76 #endif 77 78 static int byteorder __P((void)); 79 static int nroot __P((BTREE *)); 80 static int tmp __P((void)); 81 82 /* 83 * __BT_OPEN -- Open a btree. 84 * 85 * Creates and fills a DB struct, and calls the routine that actually 86 * opens the btree. 87 * 88 * Parameters: 89 * fname: filename (NULL for in-memory trees) 90 * flags: open flag bits 91 * mode: open permission bits 92 * b: BTREEINFO pointer 93 * 94 * Returns: 95 * NULL on failure, pointer to DB on success. 96 * 97 */ 98 DB * 99 __bt_open(fname, flags, mode, openinfo, dflags) 100 const char *fname; 101 int flags; 102 mode_t mode; 103 const BTREEINFO *openinfo; 104 int dflags; 105 { 106 struct stat sb; 107 BTMETA m; 108 BTREE *t; 109 BTREEINFO b; 110 DB *dbp; 111 pgno_t ncache; 112 ssize_t nr; 113 int machine_lorder; 114 115 t = NULL; 116 117 /* 118 * Intention is to make sure all of the user's selections are okay 119 * here and then use them without checking. Can't be complete, since 120 * we don't know the right page size, lorder or flags until the backing 121 * file is opened. Also, the file's page size can cause the cachesize 122 * to change. 123 */ 124 machine_lorder = byteorder(); 125 if (openinfo) { 126 b = *openinfo; 127 128 /* Flags: R_DUP. */ 129 if (b.flags & ~(R_DUP)) 130 goto einval; 131 132 /* 133 * Page size must be indx_t aligned and >= MINPSIZE. Default 134 * page size is set farther on, based on the underlying file 135 * transfer size. 136 */ 137 if (b.psize && 138 (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 || 139 b.psize & (sizeof(indx_t) - 1))) 140 goto einval; 141 142 /* Minimum number of keys per page; absolute minimum is 2. */ 143 if (b.minkeypage) { 144 if (b.minkeypage < 2) 145 goto einval; 146 } else 147 b.minkeypage = DEFMINKEYPAGE; 148 149 /* If no comparison, use default comparison and prefix. */ 150 if (b.compare == NULL) { 151 b.compare = __bt_defcmp; 152 if (b.prefix == NULL) 153 b.prefix = __bt_defpfx; 154 } 155 156 if (b.lorder == 0) 157 b.lorder = machine_lorder; 158 } else { 159 b.compare = __bt_defcmp; 160 b.cachesize = 0; 161 b.flags = 0; 162 b.lorder = machine_lorder; 163 b.minkeypage = DEFMINKEYPAGE; 164 b.prefix = __bt_defpfx; 165 b.psize = 0; 166 } 167 168 /* Check for the ubiquitous PDP-11. */ 169 if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) 170 goto einval; 171 172 /* Allocate and initialize DB and BTREE structures. */ 173 if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL) 174 goto err; 175 memset(t, 0, sizeof(BTREE)); 176 t->bt_fd = -1; /* Don't close unopened fd on error. */ 177 t->bt_lorder = b.lorder; 178 t->bt_order = NOT; 179 t->bt_cmp = b.compare; 180 t->bt_pfx = b.prefix; 181 t->bt_rfd = -1; 182 183 if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL) 184 goto err; 185 memset(t->bt_dbp, 0, sizeof(DB)); 186 if (t->bt_lorder != machine_lorder) 187 F_SET(t, B_NEEDSWAP); 188 189 dbp->type = DB_BTREE; 190 dbp->internal = t; 191 dbp->close = __bt_close; 192 dbp->del = __bt_delete; 193 dbp->fd = __bt_fd; 194 dbp->get = __bt_get; 195 dbp->put = __bt_put; 196 dbp->seq = __bt_seq; 197 dbp->sync = __bt_sync; 198 199 /* 200 * If no file name was supplied, this is an in-memory btree and we 201 * open a backing temporary file. Otherwise, it's a disk-based tree. 202 */ 203 if (fname) { 204 switch (flags & O_ACCMODE) { 205 case O_RDONLY: 206 F_SET(t, B_RDONLY); 207 break; 208 case O_RDWR: 209 break; 210 case O_WRONLY: 211 default: 212 goto einval; 213 } 214 215 if ((t->bt_fd = open(fname, flags, mode)) < 0) 216 goto err; 217 218 } else { 219 if ((flags & O_ACCMODE) != O_RDWR) 220 goto einval; 221 if ((t->bt_fd = tmp()) == -1) 222 goto err; 223 F_SET(t, B_INMEM); 224 } 225 226 if (fcntl(t->bt_fd, F_SETFD, 1) == -1) 227 goto err; 228 229 if (fstat(t->bt_fd, &sb)) 230 goto err; 231 if (sb.st_size) { 232 if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0) 233 goto err; 234 if (nr != sizeof(BTMETA)) 235 goto eftype; 236 237 /* 238 * Read in the meta-data. This can change the notion of what 239 * the lorder, page size and flags are, and, when the page size 240 * changes, the cachesize value can change too. If the user 241 * specified the wrong byte order for an existing database, we 242 * don't bother to return an error, we just clear the NEEDSWAP 243 * bit. 244 */ 245 if (m.magic == BTREEMAGIC) 246 F_CLR(t, B_NEEDSWAP); 247 else { 248 F_SET(t, B_NEEDSWAP); 249 M_32_SWAP(m.magic); 250 M_32_SWAP(m.version); 251 M_32_SWAP(m.psize); 252 M_32_SWAP(m.free); 253 M_32_SWAP(m.nrecs); 254 M_32_SWAP(m.flags); 255 } 256 if (m.magic != BTREEMAGIC || m.version != BTREEVERSION) 257 goto eftype; 258 if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 || 259 m.psize & (sizeof(indx_t) - 1)) 260 goto eftype; 261 if (m.flags & ~SAVEMETA) 262 goto eftype; 263 b.psize = m.psize; 264 F_SET(t, m.flags); 265 t->bt_free = m.free; 266 t->bt_nrecs = m.nrecs; 267 } else { 268 /* 269 * Set the page size to the best value for I/O to this file. 270 * Don't overflow the page offset type. 271 */ 272 if (b.psize == 0) { 273 b.psize = sb.st_blksize; 274 if (b.psize < MINPSIZE) 275 b.psize = MINPSIZE; 276 if (b.psize > MAX_PAGE_OFFSET + 1) 277 b.psize = MAX_PAGE_OFFSET + 1; 278 } 279 280 /* Set flag if duplicates permitted. */ 281 if (!(b.flags & R_DUP)) 282 F_SET(t, B_NODUPS); 283 284 t->bt_free = P_INVALID; 285 t->bt_nrecs = 0; 286 F_SET(t, B_METADIRTY); 287 } 288 289 t->bt_psize = b.psize; 290 291 /* Set the cache size; must be a multiple of the page size. */ 292 if (b.cachesize && b.cachesize & (b.psize - 1)) 293 b.cachesize += (~b.cachesize & (b.psize - 1)) + 1; 294 if (b.cachesize < b.psize * MINCACHE) 295 b.cachesize = b.psize * MINCACHE; 296 297 /* Calculate number of pages to cache. */ 298 ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 299 300 /* 301 * The btree data structure requires that at least two keys can fit on 302 * a page, but other than that there's no fixed requirement. The user 303 * specified a minimum number per page, and we translated that into the 304 * number of bytes a key/data pair can use before being placed on an 305 * overflow page. This calculation includes the page header, the size 306 * of the index referencing the leaf item and the size of the leaf item 307 * structure. Also, don't let the user specify a minkeypage such that 308 * a key/data pair won't fit even if both key and data are on overflow 309 * pages. 310 */ 311 t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 312 (sizeof(indx_t) + NBLEAFDBT(0, 0)); 313 if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t)) 314 t->bt_ovflsize = 315 NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t); 316 317 /* Initialize the buffer pool. */ 318 if ((t->bt_mp = 319 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 320 goto err; 321 if (!F_ISSET(t, B_INMEM)) 322 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 323 324 /* Create a root page if new tree. */ 325 if (nroot(t) == RET_ERROR) 326 goto err; 327 328 /* Global flags. */ 329 if (dflags & DB_LOCK) 330 F_SET(t, B_DB_LOCK); 331 if (dflags & DB_SHMEM) 332 F_SET(t, B_DB_SHMEM); 333 if (dflags & DB_TXN) 334 F_SET(t, B_DB_TXN); 335 336 return (dbp); 337 338 einval: errno = EINVAL; 339 goto err; 340 341 eftype: errno = EFTYPE; 342 goto err; 343 344 err: if (t) { 345 if (t->bt_dbp) 346 free(t->bt_dbp); 347 if (t->bt_fd != -1) 348 (void)close(t->bt_fd); 349 free(t); 350 } 351 return (NULL); 352 } 353 354 /* 355 * NROOT -- Create the root of a new tree. 356 * 357 * Parameters: 358 * t: tree 359 * 360 * Returns: 361 * RET_ERROR, RET_SUCCESS 362 */ 363 static int 364 nroot(t) 365 BTREE *t; 366 { 367 PAGE *meta, *root; 368 pgno_t npg; 369 370 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 371 mpool_put(t->bt_mp, meta, 0); 372 return (RET_SUCCESS); 373 } 374 if (errno != EINVAL) /* It's OK to not exist. */ 375 return (RET_ERROR); 376 errno = 0; 377 378 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 379 return (RET_ERROR); 380 381 if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 382 return (RET_ERROR); 383 384 if (npg != P_ROOT) 385 return (RET_ERROR); 386 root->pgno = npg; 387 root->prevpg = root->nextpg = P_INVALID; 388 root->lower = BTDATAOFF; 389 root->upper = t->bt_psize; 390 root->flags = P_BLEAF; 391 memset(meta, 0, t->bt_psize); 392 mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 393 mpool_put(t->bt_mp, root, MPOOL_DIRTY); 394 return (RET_SUCCESS); 395 } 396 397 static int 398 tmp() 399 { 400 sigset_t set, oset; 401 int fd; 402 char *envtmp; 403 char path[MAXPATHLEN]; 404 405 envtmp = getenv("TMPDIR"); 406 (void)snprintf(path, 407 sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : _PATH_TMP); 408 409 (void)sigfillset(&set); 410 (void)sigprocmask(SIG_BLOCK, &set, &oset); 411 if ((fd = mkstemp(path)) != -1) 412 (void)unlink(path); 413 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 414 return(fd); 415 } 416 417 static int 418 byteorder() 419 { 420 u_int32_t x; 421 u_char *p; 422 423 x = 0x01020304; 424 p = (u_char *)(void *)&x; 425 switch (*p) { 426 case 1: 427 return (BIG_ENDIAN); 428 case 4: 429 return (LITTLE_ENDIAN); 430 default: 431 return (0); 432 } 433 } 434 435 int 436 __bt_fd(dbp) 437 const DB *dbp; 438 { 439 BTREE *t; 440 441 t = dbp->internal; 442 443 /* Toss any page pinned across calls. */ 444 if (t->bt_pinned != NULL) { 445 mpool_put(t->bt_mp, t->bt_pinned, 0); 446 t->bt_pinned = NULL; 447 } 448 449 /* In-memory database can't have a file descriptor. */ 450 if (F_ISSET(t, B_INMEM)) { 451 errno = ENOENT; 452 return (-1); 453 } 454 return (t->bt_fd); 455 } 456