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