1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. 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 8.5 (Berkeley) 02/21/94";
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 #include <db.h>
36 #include "btree.h"
37
38 static int byteorder __P((void));
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 *
__bt_open(fname,flags,mode,openinfo,dflags)59 __bt_open(fname, flags, mode, openinfo, dflags)
60 const char *fname;
61 int flags, mode, dflags;
62 const BTREEINFO *openinfo;
63 {
64 struct stat sb;
65 BTMETA m;
66 BTREE *t;
67 BTREEINFO b;
68 DB *dbp;
69 pgno_t ncache;
70 ssize_t nr;
71 int machine_lorder;
72
73 t = NULL;
74
75 /*
76 * Intention is to make sure all of the user's selections are okay
77 * here and then use them without checking. Can't be complete, since
78 * we don't know the right page size, lorder or flags until the backing
79 * file is opened. Also, the file's page size can cause the cachesize
80 * to change.
81 */
82 machine_lorder = byteorder();
83 if (openinfo) {
84 b = *openinfo;
85
86 /* Flags: R_DUP. */
87 if (b.flags & ~(R_DUP))
88 goto einval;
89
90 /*
91 * Page size must be indx_t aligned and >= MINPSIZE. Default
92 * page size is set farther on, based on the underlying file
93 * transfer size.
94 */
95 if (b.psize &&
96 (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
97 b.psize & sizeof(indx_t) - 1))
98 goto einval;
99
100 /* Minimum number of keys per page; absolute minimum is 2. */
101 if (b.minkeypage) {
102 if (b.minkeypage < 2)
103 goto einval;
104 } else
105 b.minkeypage = DEFMINKEYPAGE;
106
107 /* If no comparison, use default comparison and prefix. */
108 if (b.compare == NULL) {
109 b.compare = __bt_defcmp;
110 if (b.prefix == NULL)
111 b.prefix = __bt_defpfx;
112 }
113
114 if (b.lorder == 0)
115 b.lorder = machine_lorder;
116 } else {
117 b.compare = __bt_defcmp;
118 b.cachesize = 0;
119 b.flags = 0;
120 b.lorder = machine_lorder;
121 b.minkeypage = DEFMINKEYPAGE;
122 b.prefix = __bt_defpfx;
123 b.psize = 0;
124 }
125
126 /* Check for the ubiquitous PDP-11. */
127 if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
128 goto einval;
129
130 /* Allocate and initialize DB and BTREE structures. */
131 if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
132 goto err;
133 memset(t, 0, sizeof(BTREE));
134 t->bt_bcursor.pgno = P_INVALID;
135 t->bt_fd = -1; /* Don't close unopened fd on error. */
136 t->bt_lorder = b.lorder;
137 t->bt_order = NOT;
138 t->bt_cmp = b.compare;
139 t->bt_pfx = b.prefix;
140 t->bt_rfd = -1;
141
142 if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
143 goto err;
144 t->bt_flags = 0;
145 if (t->bt_lorder != machine_lorder)
146 SET(t, B_NEEDSWAP);
147
148 dbp->type = DB_BTREE;
149 dbp->internal = t;
150 dbp->close = __bt_close;
151 dbp->del = __bt_delete;
152 dbp->fd = __bt_fd;
153 dbp->get = __bt_get;
154 dbp->put = __bt_put;
155 dbp->seq = __bt_seq;
156 dbp->sync = __bt_sync;
157
158 /*
159 * If no file name was supplied, this is an in-memory btree and we
160 * open a backing temporary file. Otherwise, it's a disk-based tree.
161 */
162 if (fname) {
163 switch(flags & O_ACCMODE) {
164 case O_RDONLY:
165 SET(t, B_RDONLY);
166 break;
167 case O_RDWR:
168 break;
169 case O_WRONLY:
170 default:
171 goto einval;
172 }
173
174 if ((t->bt_fd = open(fname, flags, mode)) < 0)
175 goto err;
176
177 } else {
178 if ((flags & O_ACCMODE) != O_RDWR)
179 goto einval;
180 if ((t->bt_fd = tmp()) == -1)
181 goto err;
182 SET(t, B_INMEM);
183 }
184
185 if (fcntl(t->bt_fd, F_SETFD, 1) == -1)
186 goto err;
187
188 if (fstat(t->bt_fd, &sb))
189 goto err;
190 if (sb.st_size) {
191 nr = read(t->bt_fd, &m, sizeof(BTMETA));
192 if (nr < 0)
193 goto err;
194 if (nr != sizeof(BTMETA))
195 goto eftype;
196
197 /*
198 * Read in the meta-data. This can change the notion of what
199 * the lorder, page size and flags are, and, when the page size
200 * changes, the cachesize value can change too. If the user
201 * specified the wrong byte order for an existing database, we
202 * don't bother to return an error, we just clear the NEEDSWAP
203 * bit.
204 */
205 if (m.m_magic == BTREEMAGIC)
206 CLR(t, B_NEEDSWAP);
207 else {
208 SET(t, B_NEEDSWAP);
209 M_32_SWAP(m.m_magic);
210 M_32_SWAP(m.m_version);
211 M_32_SWAP(m.m_psize);
212 M_32_SWAP(m.m_free);
213 M_32_SWAP(m.m_nrecs);
214 M_32_SWAP(m.m_flags);
215 }
216 if (m.m_magic != BTREEMAGIC || m.m_version != BTREEVERSION)
217 goto eftype;
218 if (m.m_psize < MINPSIZE || m.m_psize > MAX_PAGE_OFFSET + 1 ||
219 m.m_psize & sizeof(indx_t) - 1)
220 goto eftype;
221 if (m.m_flags & ~SAVEMETA)
222 goto eftype;
223 b.psize = m.m_psize;
224 t->bt_flags |= m.m_flags;
225 t->bt_free = m.m_free;
226 t->bt_nrecs = m.m_nrecs;
227 } else {
228 /*
229 * Set the page size to the best value for I/O to this file.
230 * Don't overflow the page offset type.
231 */
232 if (b.psize == 0) {
233 b.psize = sb.st_blksize;
234 if (b.psize < MINPSIZE)
235 b.psize = MINPSIZE;
236 if (b.psize > MAX_PAGE_OFFSET + 1)
237 b.psize = MAX_PAGE_OFFSET + 1;
238 }
239
240 /* Set flag if duplicates permitted. */
241 if (!(b.flags & R_DUP))
242 SET(t, B_NODUPS);
243
244 t->bt_free = P_INVALID;
245 t->bt_nrecs = 0;
246 SET(t, B_METADIRTY);
247 }
248
249 t->bt_psize = b.psize;
250
251 /* Set the cache size; must be a multiple of the page size. */
252 if (b.cachesize && b.cachesize & b.psize - 1)
253 b.cachesize += (~b.cachesize & b.psize - 1) + 1;
254 if (b.cachesize < b.psize * MINCACHE)
255 b.cachesize = b.psize * MINCACHE;
256
257 /* Calculate number of pages to cache. */
258 ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;
259
260 /*
261 * The btree data structure requires that at least two keys can fit on
262 * a page, but other than that there's no fixed requirement. The user
263 * specified a minimum number per page, and we translated that into the
264 * number of bytes a key/data pair can use before being placed on an
265 * overflow page. This calculation includes the page header, the size
266 * of the index referencing the leaf item and the size of the leaf item
267 * structure. Also, don't let the user specify a minkeypage such that
268 * a key/data pair won't fit even if both key and data are on overflow
269 * pages.
270 */
271 t->bt_ovflsize = (t->bt_psize - BTDATAOFF) / b.minkeypage -
272 (sizeof(indx_t) + NBLEAFDBT(0, 0));
273 if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
274 t->bt_ovflsize =
275 NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);
276
277 /* Initialize the buffer pool. */
278 if ((t->bt_mp =
279 mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
280 goto err;
281 if (!ISSET(t, B_INMEM))
282 mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);
283
284 /* Create a root page if new tree. */
285 if (nroot(t) == RET_ERROR)
286 goto err;
287
288 /* Global flags. */
289 if (dflags & DB_LOCK)
290 SET(t, B_DB_LOCK);
291 if (dflags & DB_SHMEM)
292 SET(t, B_DB_SHMEM);
293 if (dflags & DB_TXN)
294 SET(t, B_DB_TXN);
295
296 return (dbp);
297
298 einval: errno = EINVAL;
299 goto err;
300
301 eftype: errno = EFTYPE;
302 goto err;
303
304 err: if (t) {
305 if (t->bt_dbp)
306 free(t->bt_dbp);
307 if (t->bt_fd != -1)
308 (void)close(t->bt_fd);
309 free(t);
310 }
311 return (NULL);
312 }
313
314 /*
315 * NROOT -- Create the root of a new tree.
316 *
317 * Parameters:
318 * t: tree
319 *
320 * Returns:
321 * RET_ERROR, RET_SUCCESS
322 */
323 static int
nroot(t)324 nroot(t)
325 BTREE *t;
326 {
327 PAGE *meta, *root;
328 pgno_t npg;
329
330 if ((meta = mpool_get(t->bt_mp, 0, 0)) != NULL) {
331 mpool_put(t->bt_mp, meta, 0);
332 return (RET_SUCCESS);
333 }
334 if (errno != EINVAL)
335 return (RET_ERROR);
336
337 if ((meta = mpool_new(t->bt_mp, &npg)) == NULL)
338 return (RET_ERROR);
339
340 if ((root = mpool_new(t->bt_mp, &npg)) == NULL)
341 return (RET_ERROR);
342
343 if (npg != P_ROOT)
344 return (RET_ERROR);
345 root->pgno = npg;
346 root->prevpg = root->nextpg = P_INVALID;
347 root->lower = BTDATAOFF;
348 root->upper = t->bt_psize;
349 root->flags = P_BLEAF;
350 memset(meta, 0, t->bt_psize);
351 mpool_put(t->bt_mp, meta, MPOOL_DIRTY);
352 mpool_put(t->bt_mp, root, MPOOL_DIRTY);
353 return (RET_SUCCESS);
354 }
355
356 static int
tmp()357 tmp()
358 {
359 sigset_t set, oset;
360 int fd;
361 char *envtmp;
362 char path[MAXPATHLEN];
363
364 envtmp = getenv("TMPDIR");
365 (void)snprintf(path,
366 sizeof(path), "%s/bt.XXXXXX", envtmp ? envtmp : "/tmp");
367
368 (void)sigfillset(&set);
369 (void)sigprocmask(SIG_BLOCK, &set, &oset);
370 if ((fd = mkstemp(path)) != -1)
371 (void)unlink(path);
372 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
373 return(fd);
374 }
375
376 static int
byteorder()377 byteorder()
378 {
379 u_int32_t x;
380 u_char *p;
381
382 x = 0x01020304;
383 p = (u_char *)&x;
384 switch (*p) {
385 case 1:
386 return (BIG_ENDIAN);
387 case 4:
388 return (LITTLE_ENDIAN);
389 default:
390 return (0);
391 }
392 }
393
394 int
__bt_fd(dbp)395 __bt_fd(dbp)
396 const DB *dbp;
397 {
398 BTREE *t;
399
400 t = dbp->internal;
401
402 /* Toss any page pinned across calls. */
403 if (t->bt_pinned != NULL) {
404 mpool_put(t->bt_mp, t->bt_pinned, 0);
405 t->bt_pinned = NULL;
406 }
407
408 /* In-memory database can't have a file descriptor. */
409 if (ISSET(t, B_INMEM)) {
410 errno = ENOENT;
411 return (-1);
412 }
413 return (t->bt_fd);
414 }
415