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