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