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