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