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.24 (Berkeley) 02/11/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 nroot __P((BTREE *)); 40 static int tmp __P((void)); 41 42 /* 43 * __BT_OPEN -- Open a btree. 44 * 45 * Creates and fills a DB struct, and calls the routine that actually 46 * opens the btree. 47 * 48 * Parameters: 49 * fname: filename (NULL for in-memory trees) 50 * flags: open flag bits 51 * mode: open permission bits 52 * b: BTREEINFO pointer 53 * 54 * Returns: 55 * NULL on failure, pointer to DB on success. 56 * 57 */ 58 DB * 59 __bt_open(fname, flags, mode, openinfo) 60 const char *fname; 61 int flags, mode; 62 const BTREEINFO *openinfo; 63 { 64 BTMETA m; 65 BTREE *t; 66 BTREEINFO b; 67 DB *dbp; 68 pgno_t ncache; 69 struct stat sb; 70 int nr; 71 72 t = NULL; 73 74 /* 75 * Intention is to make sure all of the user's selections are okay 76 * here and then use them without checking. Can't be complete, since 77 * we don't know the right page size, lorder or flags until the backing 78 * file is opened. Also, the file's page size can cause the cachesize 79 * to change. 80 */ 81 if (openinfo) { 82 b = *openinfo; 83 84 /* Flags: R_DUP. */ 85 if (b.flags & ~(R_DUP)) 86 goto einval; 87 88 /* 89 * Page size must be index_t aligned and >= MINPSIZE. Default 90 * page size is set farther on, based on the underlying file 91 * transfer size. 92 */ 93 if (b.psize && 94 (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET || 95 b.psize & sizeof(index_t) - 1)) 96 goto einval; 97 98 /* Minimum number of keys per page; absolute minimum is 2. */ 99 if (b.minkeypage) { 100 if (b.minkeypage < 2) 101 goto einval; 102 } else 103 b.minkeypage = DEFMINKEYPAGE; 104 105 /* If no comparison, use default comparison and prefix. */ 106 if (b.compare == NULL) { 107 b.compare = __bt_defcmp; 108 if (b.prefix == NULL) 109 b.prefix = __bt_defpfx; 110 } 111 112 if (b.lorder == 0) 113 b.lorder = BYTE_ORDER; 114 else if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN) 115 goto einval; 116 } else { 117 b.compare = __bt_defcmp; 118 b.cachesize = 0; 119 b.flags = 0; 120 b.lorder = BYTE_ORDER; 121 b.minkeypage = DEFMINKEYPAGE; 122 b.prefix = __bt_defpfx; 123 b.psize = 0; 124 } 125 126 /* Allocate and initialize DB and BTREE structures. */ 127 if ((t = malloc(sizeof(BTREE))) == NULL) 128 goto err; 129 t->bt_fd = -1; /* Don't close unopened fd on error. */ 130 if ((t->bt_dbp = dbp = malloc(sizeof(DB))) == NULL) 131 goto err; 132 t->bt_bcursor.pgno = P_INVALID; 133 t->bt_bcursor.index = 0; 134 t->bt_stack = NULL; 135 t->bt_sp = t->bt_maxstack = 0; 136 t->bt_kbuf = t->bt_dbuf = NULL; 137 t->bt_kbufsz = t->bt_dbufsz = 0; 138 t->bt_order = NOT; 139 t->bt_cmp = b.compare; 140 t->bt_pfx = b.prefix; 141 t->bt_flags = 0; 142 143 dbp->type = DB_BTREE; 144 dbp->internal = t; 145 dbp->close = __bt_close; 146 dbp->del = __bt_delete; 147 dbp->get = __bt_get; 148 dbp->put = __bt_put; 149 dbp->seq = __bt_seq; 150 dbp->sync = __bt_sync; 151 152 /* 153 * If no file name was supplied, this is an in-memory btree and we 154 * open a backing temporary file. Otherwise, it's a disk-based tree. 155 */ 156 if (fname) { 157 switch(flags & O_ACCMODE) { 158 case O_RDONLY: 159 SET(t, BTF_RDONLY); 160 break; 161 case O_RDWR: 162 break; 163 case O_WRONLY: 164 default: 165 goto einval; 166 } 167 168 if ((t->bt_fd = 169 open(fname, flags & __USE_OPEN_FLAGS, mode)) < 0) 170 goto err; 171 172 } else { 173 if ((flags & O_ACCMODE) != O_RDWR) 174 goto einval; 175 if ((t->bt_fd = tmp()) == -1) 176 goto err; 177 SET(t, BTF_INMEM); 178 } 179 180 if (fcntl(t->bt_fd, F_SETFD, 1) == -1) 181 goto err; 182 183 if (fstat(t->bt_fd, &sb)) 184 goto err; 185 if (sb.st_size) { 186 nr = read(t->bt_fd, &m, sizeof(BTMETA)); 187 if (nr < 0) 188 goto err; 189 if (nr != sizeof(BTMETA)) 190 goto eftype; 191 192 /* 193 * Read in the meta-data. This can change the notion of what 194 * the lorder, page size and flags are, and, when the page size 195 * changes the cachesize value can change as well. 196 * 197 * Lorder is always stored in host-independent format. 198 */ 199 m.m_lorder = ntohl(m.m_lorder); 200 if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 201 goto eftype; 202 if (m.m_lorder != BYTE_ORDER) { 203 BLSWAP(m.m_magic); 204 BLSWAP(m.m_version); 205 BLSWAP(m.m_psize); 206 BLSWAP(m.m_free); 207 BLSWAP(m.m_nrecs); 208 BLSWAP(m.m_flags); 209 } 210 if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 211 goto eftype; 212 if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 213 m.m_psize & sizeof(index_t) - 1) 214 goto eftype; 215 if (m.m_flags & ~SAVEMETA) 216 goto eftype; 217 b.psize = m.m_psize; 218 t->bt_flags |= m.m_flags; 219 t->bt_free = m.m_free; 220 t->bt_lorder = m.m_lorder; 221 t->bt_nrecs = m.m_nrecs; 222 } else { 223 /* 224 * Set the page size to the best value for I/O to this file. 225 * Don't overflow the page offset type. 226 */ 227 if (b.psize == 0) { 228 b.psize = sb.st_blksize; 229 if (b.psize < MINPSIZE) 230 b.psize = MINPSIZE; 231 if (b.psize > MAX_PAGE_OFFSET) 232 b.psize = MAX_PAGE_OFFSET; 233 } 234 if (!(b.flags & R_DUP)) 235 SET(t, BTF_NODUPS); 236 t->bt_free = P_INVALID; 237 t->bt_lorder = b.lorder; 238 t->bt_nrecs = 0; 239 SET(t, BTF_METADIRTY); 240 } 241 242 t->bt_psize = b.psize; 243 244 /* Set the cache size; must be a multiple of the page size. */ 245 if (b.cachesize && b.cachesize & b.psize - 1) 246 b.cachesize += (~b.cachesize & b.psize - 1) + 1; 247 if (b.cachesize < b.psize * MINCACHE) 248 b.cachesize = b.psize * MINCACHE; 249 250 /* Calculate number of pages to cache. */ 251 ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 252 253 /* 254 * The btree data structure requires that at least two keys can fit on 255 * a page, but other than that there's no fixed requirement. The user 256 * specified a minimum number per page, and we translated that into the 257 * number of bytes a key/data pair can use before being placed on an 258 * overflow page. This calculation includes the page header, the size 259 * of the index referencing the leaf item and the size of the leaf item 260 * structure. Also, don't let the user specify a minkeypage such that 261 * a key/data pair won't fit even if both key and data are on overflow 262 * pages. 263 */ 264 t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 265 (sizeof(index_t) + NBLEAFDBT(0, 0)); 266 if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 267 t->bt_ovflsize = 268 NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 269 270 /* Initialize the buffer pool. */ 271 if ((t->bt_mp = 272 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 273 goto err; 274 if (!ISSET(t, BTF_INMEM)) 275 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 276 277 /* Create a root page if new tree. */ 278 if (nroot(t) == RET_ERROR) 279 goto err; 280 281 return (dbp); 282 283 einval: errno = EINVAL; 284 goto err; 285 286 eftype: errno = EFTYPE; 287 goto err; 288 289 err: if (t) { 290 if (t->bt_dbp) 291 free(t->bt_dbp); 292 if (t->bt_fd != -1) 293 (void)close(t->bt_fd); 294 free(t); 295 } 296 return (NULL); 297 } 298 299 /* 300 * NROOT -- Create the root of a new tree. 301 * 302 * Parameters: 303 * t: tree 304 * 305 * Returns: 306 * RET_ERROR, RET_SUCCESS 307 */ 308 static int 309 nroot(t) 310 BTREE *t; 311 { 312 PAGE *meta, *root; 313 pgno_t npg; 314 315 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 316 mpool_put(t->bt_mp, meta, 0); 317 return (RET_SUCCESS); 318 } 319 if (errno != EINVAL) 320 return (RET_ERROR); 321 322 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 323 return (RET_ERROR); 324 325 if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 326 return (RET_ERROR); 327 328 if (npg != P_ROOT) 329 return (RET_ERROR); 330 root->pgno = npg; 331 root->prevpg = root->nextpg = P_INVALID; 332 root->lower = BTDATAOFF; 333 root->upper = t->bt_psize; 334 root->flags = P_BLEAF; 335 bzero(meta, t->bt_psize); 336 mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 337 mpool_put(t->bt_mp, root, MPOOL_DIRTY); 338 return (RET_SUCCESS); 339 } 340 341 static int 342 tmp() 343 { 344 sigset_t set, oset; 345 int fd; 346 char *envtmp; 347 char path[MAXPATHLEN]; 348 349 envtmp = getenv("TMPDIR"); 350 (void)snprintf(path, 351 sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 352 353 (void)sigfillset(&set); 354 (void)sigprocmask(SIG_BLOCK, &set, &oset); 355 if ((fd = mkstemp(path)) != -1) 356 (void)unlink(path); 357 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 358 return(fd); 359 } 360