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