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