xref: /freebsd/lib/libc/db/recno/rec_open.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
158f0484fSRodney W. Grimes /*-
2ef5d438eSPaul Traina  * Copyright (c) 1990, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Mike Olson.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes  *    without specific prior written permission.
1958f0484fSRodney W. Grimes  *
2058f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes  * SUCH DAMAGE.
3158f0484fSRodney W. Grimes  */
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
34ef5d438eSPaul Traina static char sccsid[] = "@(#)rec_open.c	8.10 (Berkeley) 9/1/94";
3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
36333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3858f0484fSRodney W. Grimes 
39d201fe46SDaniel Eischen #include "namespace.h"
4058f0484fSRodney W. Grimes #include <sys/types.h>
4158f0484fSRodney W. Grimes #include <sys/mman.h>
4258f0484fSRodney W. Grimes #include <sys/stat.h>
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes #include <errno.h>
4558f0484fSRodney W. Grimes #include <fcntl.h>
4658f0484fSRodney W. Grimes #include <limits.h>
4758f0484fSRodney W. Grimes #include <stddef.h>
4858f0484fSRodney W. Grimes #include <stdio.h>
4958f0484fSRodney W. Grimes #include <unistd.h>
50d201fe46SDaniel Eischen #include "un-namespace.h"
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes #include <db.h>
5358f0484fSRodney W. Grimes #include "recno.h"
5458f0484fSRodney W. Grimes 
5558f0484fSRodney W. Grimes DB *
560ac22237SXin LI __rec_open(const char *fname, int flags, int mode, const RECNOINFO *openinfo,
570ac22237SXin LI     int dflags)
5858f0484fSRodney W. Grimes {
5958f0484fSRodney W. Grimes 	BTREE *t;
6058f0484fSRodney W. Grimes 	BTREEINFO btopeninfo;
6158f0484fSRodney W. Grimes 	DB *dbp;
6258f0484fSRodney W. Grimes 	PAGE *h;
6358f0484fSRodney W. Grimes 	struct stat sb;
6458f0484fSRodney W. Grimes 	int rfd, sverrno;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes 	/* Open the user's file -- if this fails, we're done. */
67636b8d93SJilles Tjoelker 	if (fname != NULL && (rfd = _open(fname, flags | O_CLOEXEC, mode)) < 0)
6858f0484fSRodney W. Grimes 		return (NULL);
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes 	/* Create a btree in memory (backed by disk). */
7158f0484fSRodney W. Grimes 	dbp = NULL;
7258f0484fSRodney W. Grimes 	if (openinfo) {
7358f0484fSRodney W. Grimes 		if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
7458f0484fSRodney W. Grimes 			goto einval;
7558f0484fSRodney W. Grimes 		btopeninfo.flags = 0;
7658f0484fSRodney W. Grimes 		btopeninfo.cachesize = openinfo->cachesize;
7758f0484fSRodney W. Grimes 		btopeninfo.maxkeypage = 0;
7858f0484fSRodney W. Grimes 		btopeninfo.minkeypage = 0;
7958f0484fSRodney W. Grimes 		btopeninfo.psize = openinfo->psize;
8058f0484fSRodney W. Grimes 		btopeninfo.compare = NULL;
8158f0484fSRodney W. Grimes 		btopeninfo.prefix = NULL;
8258f0484fSRodney W. Grimes 		btopeninfo.lorder = openinfo->lorder;
8358f0484fSRodney W. Grimes 		dbp = __bt_open(openinfo->bfname,
8458f0484fSRodney W. Grimes 		    O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
8558f0484fSRodney W. Grimes 	} else
8658f0484fSRodney W. Grimes 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
8758f0484fSRodney W. Grimes 	if (dbp == NULL)
8858f0484fSRodney W. Grimes 		goto err;
8958f0484fSRodney W. Grimes 
9058f0484fSRodney W. Grimes 	/*
9158f0484fSRodney W. Grimes 	 * Some fields in the tree structure are recno specific.  Fill them
9258f0484fSRodney W. Grimes 	 * in and make the btree structure look like a recno structure.  We
9358f0484fSRodney W. Grimes 	 * don't change the bt_ovflsize value, it's close enough and slightly
9458f0484fSRodney W. Grimes 	 * bigger.
9558f0484fSRodney W. Grimes 	 */
9658f0484fSRodney W. Grimes 	t = dbp->internal;
9758f0484fSRodney W. Grimes 	if (openinfo) {
9858f0484fSRodney W. Grimes 		if (openinfo->flags & R_FIXEDLEN) {
99ef5d438eSPaul Traina 			F_SET(t, R_FIXLEN);
10058f0484fSRodney W. Grimes 			t->bt_reclen = openinfo->reclen;
10158f0484fSRodney W. Grimes 			if (t->bt_reclen == 0)
10258f0484fSRodney W. Grimes 				goto einval;
10358f0484fSRodney W. Grimes 		}
10458f0484fSRodney W. Grimes 		t->bt_bval = openinfo->bval;
10558f0484fSRodney W. Grimes 	} else
10658f0484fSRodney W. Grimes 		t->bt_bval = '\n';
10758f0484fSRodney W. Grimes 
108ef5d438eSPaul Traina 	F_SET(t, R_RECNO);
10958f0484fSRodney W. Grimes 	if (fname == NULL)
110ef5d438eSPaul Traina 		F_SET(t, R_EOF | R_INMEM);
11158f0484fSRodney W. Grimes 	else
11258f0484fSRodney W. Grimes 		t->bt_rfd = rfd;
11358f0484fSRodney W. Grimes 
11458f0484fSRodney W. Grimes 	if (fname != NULL) {
11558f0484fSRodney W. Grimes 		/*
11658f0484fSRodney W. Grimes 		 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
11758f0484fSRodney W. Grimes 		 * Unfortunately, that's not portable, so we use lseek
11858f0484fSRodney W. Grimes 		 * and check the errno values.
11958f0484fSRodney W. Grimes 		 */
12058f0484fSRodney W. Grimes 		errno = 0;
12158f0484fSRodney W. Grimes 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
12258f0484fSRodney W. Grimes 			switch (flags & O_ACCMODE) {
12358f0484fSRodney W. Grimes 			case O_RDONLY:
124ef5d438eSPaul Traina 				F_SET(t, R_RDONLY);
12558f0484fSRodney W. Grimes 				break;
12658f0484fSRodney W. Grimes 			default:
12758f0484fSRodney W. Grimes 				goto einval;
12858f0484fSRodney W. Grimes 			}
12958f0484fSRodney W. Grimes slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
13058f0484fSRodney W. Grimes 				goto err;
131ef5d438eSPaul Traina 			F_SET(t, R_CLOSEFP);
13258f0484fSRodney W. Grimes 			t->bt_irec =
133ef5d438eSPaul Traina 			    F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
13458f0484fSRodney W. Grimes 		} else {
13558f0484fSRodney W. Grimes 			switch (flags & O_ACCMODE) {
13658f0484fSRodney W. Grimes 			case O_RDONLY:
137ef5d438eSPaul Traina 				F_SET(t, R_RDONLY);
13858f0484fSRodney W. Grimes 				break;
13958f0484fSRodney W. Grimes 			case O_RDWR:
14058f0484fSRodney W. Grimes 				break;
14158f0484fSRodney W. Grimes 			default:
14258f0484fSRodney W. Grimes 				goto einval;
14358f0484fSRodney W. Grimes 			}
14458f0484fSRodney W. Grimes 
145d201fe46SDaniel Eischen 			if (_fstat(rfd, &sb))
14658f0484fSRodney W. Grimes 				goto err;
14758f0484fSRodney W. Grimes 			/*
14858f0484fSRodney W. Grimes 			 * Kluge -- we'd like to test to see if the file is too
14958f0484fSRodney W. Grimes 			 * big to mmap.  Since, we don't know what size or type
15058f0484fSRodney W. Grimes 			 * off_t's or size_t's are, what the largest unsigned
15158f0484fSRodney W. Grimes 			 * integral type is, or what random insanity the local
15258f0484fSRodney W. Grimes 			 * C compiler will perpetrate, doing the comparison in
15358f0484fSRodney W. Grimes 			 * a portable way is flatly impossible.  Hope that mmap
15458f0484fSRodney W. Grimes 			 * fails if the file is too large.
15558f0484fSRodney W. Grimes 			 */
15658f0484fSRodney W. Grimes 			if (sb.st_size == 0)
157ef5d438eSPaul Traina 				F_SET(t, R_EOF);
15858f0484fSRodney W. Grimes 			else {
159ef5d438eSPaul Traina #ifdef MMAP_NOT_AVAILABLE
160ef5d438eSPaul Traina 				/*
161ef5d438eSPaul Traina 				 * XXX
162ef5d438eSPaul Traina 				 * Mmap doesn't work correctly on many current
163ef5d438eSPaul Traina 				 * systems.  In particular, it can fail subtly,
164ef5d438eSPaul Traina 				 * with cache coherency problems.  Don't use it
165ef5d438eSPaul Traina 				 * for now.
166ef5d438eSPaul Traina 				 */
16758f0484fSRodney W. Grimes 				t->bt_msize = sb.st_size;
16858f0484fSRodney W. Grimes 				if ((t->bt_smap = mmap(NULL, t->bt_msize,
16958f0484fSRodney W. Grimes 				    PROT_READ, MAP_PRIVATE, rfd,
1708abdc2ebSAlexander Langer 				    (off_t)0)) == MAP_FAILED)
17158f0484fSRodney W. Grimes 					goto slow;
17258f0484fSRodney W. Grimes 				t->bt_cmap = t->bt_smap;
17358f0484fSRodney W. Grimes 				t->bt_emap = t->bt_smap + sb.st_size;
174ef5d438eSPaul Traina 				t->bt_irec = F_ISSET(t, R_FIXLEN) ?
17558f0484fSRodney W. Grimes 				    __rec_fmap : __rec_vmap;
176ef5d438eSPaul Traina 				F_SET(t, R_MEMMAPPED);
177ef5d438eSPaul Traina #else
178ef5d438eSPaul Traina 				goto slow;
179ef5d438eSPaul Traina #endif
18058f0484fSRodney W. Grimes 			}
18158f0484fSRodney W. Grimes 		}
18258f0484fSRodney W. Grimes 	}
18358f0484fSRodney W. Grimes 
18458f0484fSRodney W. Grimes 	/* Use the recno routines. */
18558f0484fSRodney W. Grimes 	dbp->close = __rec_close;
18658f0484fSRodney W. Grimes 	dbp->del = __rec_delete;
18758f0484fSRodney W. Grimes 	dbp->fd = __rec_fd;
18858f0484fSRodney W. Grimes 	dbp->get = __rec_get;
18958f0484fSRodney W. Grimes 	dbp->put = __rec_put;
19058f0484fSRodney W. Grimes 	dbp->seq = __rec_seq;
19158f0484fSRodney W. Grimes 	dbp->sync = __rec_sync;
19258f0484fSRodney W. Grimes 
19358f0484fSRodney W. Grimes 	/* If the root page was created, reset the flags. */
19458f0484fSRodney W. Grimes 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
19558f0484fSRodney W. Grimes 		goto err;
19658f0484fSRodney W. Grimes 	if ((h->flags & P_TYPE) == P_BLEAF) {
197ef5d438eSPaul Traina 		F_CLR(h, P_TYPE);
198ef5d438eSPaul Traina 		F_SET(h, P_RLEAF);
19958f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
20058f0484fSRodney W. Grimes 	} else
20158f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
20258f0484fSRodney W. Grimes 
20358f0484fSRodney W. Grimes 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
204ef5d438eSPaul Traina 	    !F_ISSET(t, R_EOF | R_INMEM) &&
20558f0484fSRodney W. Grimes 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
20658f0484fSRodney W. Grimes 		goto err;
20758f0484fSRodney W. Grimes 	return (dbp);
20858f0484fSRodney W. Grimes 
20958f0484fSRodney W. Grimes einval:	errno = EINVAL;
21058f0484fSRodney W. Grimes err:	sverrno = errno;
21158f0484fSRodney W. Grimes 	if (dbp != NULL)
21258f0484fSRodney W. Grimes 		(void)__bt_close(dbp);
21358f0484fSRodney W. Grimes 	if (fname != NULL)
2149233c4d9SJason Evans 		(void)_close(rfd);
21558f0484fSRodney W. Grimes 	errno = sverrno;
21658f0484fSRodney W. Grimes 	return (NULL);
21758f0484fSRodney W. Grimes }
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes int
2200ac22237SXin LI __rec_fd(const DB *dbp)
22158f0484fSRodney W. Grimes {
22258f0484fSRodney W. Grimes 	BTREE *t;
22358f0484fSRodney W. Grimes 
22458f0484fSRodney W. Grimes 	t = dbp->internal;
22558f0484fSRodney W. Grimes 
22658f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
22758f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
22858f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
22958f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
23058f0484fSRodney W. Grimes 	}
23158f0484fSRodney W. Grimes 
23258f0484fSRodney W. Grimes 	/* In-memory database can't have a file descriptor. */
233ef5d438eSPaul Traina 	if (F_ISSET(t, R_INMEM)) {
23458f0484fSRodney W. Grimes 		errno = ENOENT;
23558f0484fSRodney W. Grimes 		return (-1);
23658f0484fSRodney W. Grimes 	}
23758f0484fSRodney W. Grimes 	return (t->bt_rfd);
23858f0484fSRodney W. Grimes }
239