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