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