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.16 (Berkeley) 12/16/91"; 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 (O_CREAT|O_EXCL|O_RDONLY|O_RDWR|O_TRUNC|O_WRONLY) 152 if ((t->bt_fd = open(fname, flags & USEFLAGS, mode)) < 0) 153 goto err; 154 if ((flags & O_ACCMODE) == O_RDONLY) 155 SET(t, BTF_RDONLY); 156 157 } else { 158 if ((t->bt_fd = tmp()) == -1) 159 goto err; 160 SET(t, BTF_INMEM); 161 } 162 163 if (fcntl(t->bt_fd, F_SETFL, 1) == -1) 164 goto err; 165 166 if (fstat(t->bt_fd, &sb)) 167 goto err; 168 if (sb.st_size) { 169 nr = read(t->bt_fd, &m, sizeof(BTMETA)); 170 if (nr < 0) 171 goto err; 172 if (nr != sizeof(BTMETA)) 173 goto eftype; 174 175 /* 176 * Read in the meta-data. This can change the notion of what 177 * the lorder, page size and flags are, and, when the page size 178 * changes the cachesize value can change as well. 179 * 180 * Lorder is always stored in host-independent format. 181 */ 182 m.m_lorder = ntohl(m.m_lorder); 183 if (m.m_lorder != BIG_ENDIAN && m.m_lorder != LITTLE_ENDIAN) 184 goto eftype; 185 if (m.m_lorder != BYTE_ORDER) { 186 BLSWAP(m.m_magic); 187 BLSWAP(m.m_version); 188 BLSWAP(m.m_psize); 189 BLSWAP(m.m_free); 190 BLSWAP(m.m_nrecs); 191 BLSWAP(m.m_flags); 192 } 193 if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION) 194 goto eftype; 195 if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET || 196 m.m_psize & sizeof(index_t) - 1) 197 goto eftype; 198 if (m.m_flags & ~SAVEMETA) 199 goto eftype; 200 b.psize = m.m_psize; 201 t->bt_flags |= m.m_flags; 202 t->bt_free = m.m_free; 203 t->bt_lorder = m.m_lorder; 204 t->bt_nrecs = m.m_nrecs; 205 } else { 206 /* 207 * Set the page size to the best value for I/O to this file. 208 * Don't overflow the page offset type. 209 */ 210 if (b.psize == 0) { 211 b.psize = sb.st_blksize; 212 if (b.psize < MINPSIZE) 213 b.psize = MINPSIZE; 214 if (b.psize > MAX_PAGE_OFFSET) 215 b.psize = MAX_PAGE_OFFSET; 216 } 217 t->bt_flags |= b.flags & R_DUP ? 0 : BTF_NODUPS; 218 t->bt_free = P_INVALID; 219 t->bt_lorder = b.lorder; 220 t->bt_nrecs = 0; 221 SET(t, BTF_METADIRTY); 222 } 223 224 t->bt_psize = b.psize; 225 226 /* Set the cache size; must be a multiple of the page size. */ 227 if (b.cachesize && b.cachesize & b.psize - 1) 228 b.cachesize += (~b.cachesize & b.psize - 1) + 1; 229 if (b.cachesize < b.psize * MINCACHE) 230 b.cachesize = b.psize * MINCACHE; 231 232 /* Calculate number of pages to cache. */ 233 ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize; 234 235 /* 236 * The btree data structure requires that at least two keys can fit on 237 * a page, but other than that there's no fixed requirement. The user 238 * specified a minimum number per page, and we translated that into the 239 * number of bytes a key/data pair can use before being placed on an 240 * overflow page. This calculation includes the page header, the size 241 * of the index referencing the leaf item and the size of the leaf item 242 * structure. Also, don't let the user specify a minkeypage such that 243 * a key/data pair won't fit even if both key and data are on overflow 244 * pages. 245 */ 246 t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage - 247 (sizeof(index_t) + NBLEAFDBT(0, 0)); 248 if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t)) 249 t->bt_ovflsize = 250 NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(index_t); 251 252 /* Initialize the buffer pool. */ 253 if ((t->bt_mp = 254 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL) 255 goto err; 256 if (NOTSET(t, BTF_INMEM)) 257 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t); 258 259 /* Create a root page if new tree. */ 260 if (nroot(t) == RET_ERROR) 261 goto err; 262 263 return (dbp); 264 265 einval: errno = EINVAL; 266 goto err; 267 268 eftype: errno = EFTYPE; 269 goto err; 270 271 err: if (t) { 272 if (t->bt_dbp) 273 free(t->bt_dbp); 274 if (t->bt_fd != -1) 275 (void)close(t->bt_fd); 276 free(t); 277 } 278 return (NULL); 279 } 280 281 /* 282 * NROOT -- Create the root of a new tree. 283 * 284 * Parameters: 285 * t: tree 286 * 287 * Returns: 288 * RET_ERROR, RET_SUCCESS 289 */ 290 static int 291 nroot(t) 292 BTREE *t; 293 { 294 PAGE *meta, *root; 295 pgno_t npg; 296 297 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) { 298 mpool_put(t->bt_mp, meta, 0); 299 return (RET_SUCCESS); 300 } 301 if (errno != EINVAL) 302 return (RET_ERROR); 303 304 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL) 305 return (RET_ERROR); 306 307 if ((root = mpool_new(t->bt_mp, &npg)) == NULL) 308 return (RET_ERROR); 309 310 if (npg != P_ROOT) 311 return (RET_ERROR); 312 root->pgno = npg; 313 root->prevpg = root->nextpg = P_INVALID; 314 root->lower = BTDATAOFF; 315 root->upper = t->bt_psize; 316 root->flags = P_BLEAF; 317 bzero(meta, t->bt_psize); 318 mpool_put(t->bt_mp, meta, MPOOL_DIRTY); 319 mpool_put(t->bt_mp, root, MPOOL_DIRTY); 320 return (RET_SUCCESS); 321 } 322 323 static int 324 tmp() 325 { 326 sigset_t set, oset; 327 int fd; 328 char *envtmp; 329 char path[MAXPATHLEN]; 330 331 envtmp = getenv("TMPDIR"); 332 (void)snprintf(path, 333 sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp"); 334 335 sigfillset(&set); 336 (void)sigprocmask(SIG_BLOCK, &set, &oset); 337 if ((fd = mkstemp(path)) != -1) 338 (void)unlink(path); 339 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 340 return(fd); 341 } 342