1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 5.29 (Berkeley) 05/16/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) 61 const char *fname; 62 int flags, mode; 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 || 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->get = __bt_get; 155 dbp->put = __bt_put; 156 dbp->seq = __bt_seq; 157 dbp->sync = __bt_sync; 158 159 /* 160 * If no file name was supplied, this is an in-memory btree and we 161 * open a backing temporary file. Otherwise, it's a disk-based tree. 162 */ 163 if (fname) { 164 switch(flags & O_ACCMODE) { 165 case O_RDONLY: 166 SET(t, B_RDONLY); 167 break; 168 case O_RDWR: 169 break; 170 case O_WRONLY: 171 default: 172 goto einval; 173 } 174 175 if ((t->bt_fd = 176 open(fname, flags & __USE_OPEN_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 || 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) 239 b.psize = MAX_PAGE_OFFSET; 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 return (dbp); 291 292 einval: errno = EINVAL; 293 goto err; 294 295 eftype: errno = EFTYPE; 296 goto err; 297 298 err: if (t) { 299 if (t->bt_dbp) 300 free(t->bt_dbp); 301 if (t->bt_fd != -1) 302 (void)close(t->bt_fd); 303 free(t); 304 } 305 return (NULL); 306 } 307 308 /* 309 * NROOT -- Create the root of a new tree. 310 * 311 * Parameters: 312 * t: tree 313 * 314 * Returns: 315 * RET_ERROR, RET_SUCCESS 316 */ 317 static int 318 nroot(t) 319 BTREE *t; 320 { 321 PAGE *meta, *root; 322 pgno_t npg; 323 324 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 325 mpool_put(t->bt_mp, meta, 0); 326 return (RET_SUCCESS); 327 } 328 if (errno != EINVAL) 329 return (RET_ERROR); 330 331 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 332 return (RET_ERROR); 333 334 if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 335 return (RET_ERROR); 336 337 if (npg != P_ROOT) 338 return (RET_ERROR); 339 root->pgno = npg; 340 root->prevpg = root->nextpg = P_INVALID; 341 root->lower = BTDATAOFF; 342 root->upper = t->bt_psize; 343 root->flags = P_BLEAF; 344 memset(meta, 0, t->bt_psize); 345 mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 346 mpool_put(t->bt_mp, root, MPOOL_DIRTY); 347 return (RET_SUCCESS); 348 } 349 350 static int 351 tmp() 352 { 353 sigset_t set, oset; 354 int fd; 355 char *envtmp; 356 char path[MAXPATHLEN]; 357 358 envtmp = getenv("TMPDIR"); 359 (void)snprintf(path, 360 sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 361 362 (void)sigfillset(&set); 363 (void)sigprocmask(SIG_BLOCK, &set, &oset); 364 if ((fd = mkstemp(path)) != -1) 365 (void)unlink(path); 366 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 367 return(fd); 368 } 369 370 static int 371 byteorder() 372 { 373 u_long x; /* XXX: 32-bit assumption. */ 374 u_char *p; 375 376 x = 0x01020304; 377 p = (u_char *)&x; 378 switch (*p) { 379 case 1: 380 return (BIG_ENDIAN); 381 case 4: 382 return (LITTLE_ENDIAN); 383 default: 384 return (0); 385 } 386 } 387