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