xref: /freebsd/lib/libc/db/recno/rec_open.c (revision 9233c4d9426e03b28e043baeefb6d5a37dc4086e)
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.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3592927338SJason Evans  *
3692927338SJason Evans  * $FreeBSD$
3758f0484fSRodney W. Grimes  */
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
40ef5d438eSPaul Traina static char sccsid[] = "@(#)rec_open.c	8.10 (Berkeley) 9/1/94";
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <sys/types.h>
4458f0484fSRodney W. Grimes #include <sys/mman.h>
4558f0484fSRodney W. Grimes #include <sys/stat.h>
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <errno.h>
4858f0484fSRodney W. Grimes #include <fcntl.h>
4958f0484fSRodney W. Grimes #include <limits.h>
5058f0484fSRodney W. Grimes #include <stddef.h>
5158f0484fSRodney W. Grimes #include <stdio.h>
5258f0484fSRodney W. Grimes #include <unistd.h>
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes #include <db.h>
5558f0484fSRodney W. Grimes #include "recno.h"
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes DB *
5858f0484fSRodney W. Grimes __rec_open(fname, flags, mode, openinfo, dflags)
5958f0484fSRodney W. Grimes 	const char *fname;
6058f0484fSRodney W. Grimes 	int flags, mode, dflags;
6158f0484fSRodney W. Grimes 	const RECNOINFO *openinfo;
6258f0484fSRodney W. Grimes {
6358f0484fSRodney W. Grimes 	BTREE *t;
6458f0484fSRodney W. Grimes 	BTREEINFO btopeninfo;
6558f0484fSRodney W. Grimes 	DB *dbp;
6658f0484fSRodney W. Grimes 	PAGE *h;
6758f0484fSRodney W. Grimes 	struct stat sb;
6858f0484fSRodney W. Grimes 	int rfd, sverrno;
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes 	/* Open the user's file -- if this fails, we're done. */
719233c4d9SJason Evans 	if (fname != NULL && (rfd = _open(fname, flags, mode)) < 0)
7258f0484fSRodney W. Grimes 		return (NULL);
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes 	/* Create a btree in memory (backed by disk). */
7558f0484fSRodney W. Grimes 	dbp = NULL;
7658f0484fSRodney W. Grimes 	if (openinfo) {
7758f0484fSRodney W. Grimes 		if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
7858f0484fSRodney W. Grimes 			goto einval;
7958f0484fSRodney W. Grimes 		btopeninfo.flags = 0;
8058f0484fSRodney W. Grimes 		btopeninfo.cachesize = openinfo->cachesize;
8158f0484fSRodney W. Grimes 		btopeninfo.maxkeypage = 0;
8258f0484fSRodney W. Grimes 		btopeninfo.minkeypage = 0;
8358f0484fSRodney W. Grimes 		btopeninfo.psize = openinfo->psize;
8458f0484fSRodney W. Grimes 		btopeninfo.compare = NULL;
8558f0484fSRodney W. Grimes 		btopeninfo.prefix = NULL;
8658f0484fSRodney W. Grimes 		btopeninfo.lorder = openinfo->lorder;
8758f0484fSRodney W. Grimes 		dbp = __bt_open(openinfo->bfname,
8858f0484fSRodney W. Grimes 		    O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
8958f0484fSRodney W. Grimes 	} else
9058f0484fSRodney W. Grimes 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
9158f0484fSRodney W. Grimes 	if (dbp == NULL)
9258f0484fSRodney W. Grimes 		goto err;
9358f0484fSRodney W. Grimes 
9458f0484fSRodney W. Grimes 	/*
9558f0484fSRodney W. Grimes 	 * Some fields in the tree structure are recno specific.  Fill them
9658f0484fSRodney W. Grimes 	 * in and make the btree structure look like a recno structure.  We
9758f0484fSRodney W. Grimes 	 * don't change the bt_ovflsize value, it's close enough and slightly
9858f0484fSRodney W. Grimes 	 * bigger.
9958f0484fSRodney W. Grimes 	 */
10058f0484fSRodney W. Grimes 	t = dbp->internal;
10158f0484fSRodney W. Grimes 	if (openinfo) {
10258f0484fSRodney W. Grimes 		if (openinfo->flags & R_FIXEDLEN) {
103ef5d438eSPaul Traina 			F_SET(t, R_FIXLEN);
10458f0484fSRodney W. Grimes 			t->bt_reclen = openinfo->reclen;
10558f0484fSRodney W. Grimes 			if (t->bt_reclen == 0)
10658f0484fSRodney W. Grimes 				goto einval;
10758f0484fSRodney W. Grimes 		}
10858f0484fSRodney W. Grimes 		t->bt_bval = openinfo->bval;
10958f0484fSRodney W. Grimes 	} else
11058f0484fSRodney W. Grimes 		t->bt_bval = '\n';
11158f0484fSRodney W. Grimes 
112ef5d438eSPaul Traina 	F_SET(t, R_RECNO);
11358f0484fSRodney W. Grimes 	if (fname == NULL)
114ef5d438eSPaul Traina 		F_SET(t, R_EOF | R_INMEM);
11558f0484fSRodney W. Grimes 	else
11658f0484fSRodney W. Grimes 		t->bt_rfd = rfd;
11758f0484fSRodney W. Grimes 
11858f0484fSRodney W. Grimes 	if (fname != NULL) {
11958f0484fSRodney W. Grimes 		/*
12058f0484fSRodney W. Grimes 		 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
12158f0484fSRodney W. Grimes 		 * Unfortunately, that's not portable, so we use lseek
12258f0484fSRodney W. Grimes 		 * and check the errno values.
12358f0484fSRodney W. Grimes 		 */
12458f0484fSRodney W. Grimes 		errno = 0;
12558f0484fSRodney W. Grimes 		if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
12658f0484fSRodney W. Grimes 			switch (flags & O_ACCMODE) {
12758f0484fSRodney W. Grimes 			case O_RDONLY:
128ef5d438eSPaul Traina 				F_SET(t, R_RDONLY);
12958f0484fSRodney W. Grimes 				break;
13058f0484fSRodney W. Grimes 			default:
13158f0484fSRodney W. Grimes 				goto einval;
13258f0484fSRodney W. Grimes 			}
13358f0484fSRodney W. Grimes slow:			if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
13458f0484fSRodney W. Grimes 				goto err;
135ef5d438eSPaul Traina 			F_SET(t, R_CLOSEFP);
13658f0484fSRodney W. Grimes 			t->bt_irec =
137ef5d438eSPaul Traina 			    F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
13858f0484fSRodney W. Grimes 		} else {
13958f0484fSRodney W. Grimes 			switch (flags & O_ACCMODE) {
14058f0484fSRodney W. Grimes 			case O_RDONLY:
141ef5d438eSPaul Traina 				F_SET(t, R_RDONLY);
14258f0484fSRodney W. Grimes 				break;
14358f0484fSRodney W. Grimes 			case O_RDWR:
14458f0484fSRodney W. Grimes 				break;
14558f0484fSRodney W. Grimes 			default:
14658f0484fSRodney W. Grimes 				goto einval;
14758f0484fSRodney W. Grimes 			}
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes 			if (fstat(rfd, &sb))
15058f0484fSRodney W. Grimes 				goto err;
15158f0484fSRodney W. Grimes 			/*
15258f0484fSRodney W. Grimes 			 * Kluge -- we'd like to test to see if the file is too
15358f0484fSRodney W. Grimes 			 * big to mmap.  Since, we don't know what size or type
15458f0484fSRodney W. Grimes 			 * off_t's or size_t's are, what the largest unsigned
15558f0484fSRodney W. Grimes 			 * integral type is, or what random insanity the local
15658f0484fSRodney W. Grimes 			 * C compiler will perpetrate, doing the comparison in
15758f0484fSRodney W. Grimes 			 * a portable way is flatly impossible.  Hope that mmap
15858f0484fSRodney W. Grimes 			 * fails if the file is too large.
15958f0484fSRodney W. Grimes 			 */
16058f0484fSRodney W. Grimes 			if (sb.st_size == 0)
161ef5d438eSPaul Traina 				F_SET(t, R_EOF);
16258f0484fSRodney W. Grimes 			else {
163ef5d438eSPaul Traina #ifdef MMAP_NOT_AVAILABLE
164ef5d438eSPaul Traina 				/*
165ef5d438eSPaul Traina 				 * XXX
166ef5d438eSPaul Traina 				 * Mmap doesn't work correctly on many current
167ef5d438eSPaul Traina 				 * systems.  In particular, it can fail subtly,
168ef5d438eSPaul Traina 				 * with cache coherency problems.  Don't use it
169ef5d438eSPaul Traina 				 * for now.
170ef5d438eSPaul Traina 				 */
17158f0484fSRodney W. Grimes 				t->bt_msize = sb.st_size;
17258f0484fSRodney W. Grimes 				if ((t->bt_smap = mmap(NULL, t->bt_msize,
17358f0484fSRodney W. Grimes 				    PROT_READ, MAP_PRIVATE, rfd,
1748abdc2ebSAlexander Langer 				    (off_t)0)) == MAP_FAILED)
17558f0484fSRodney W. Grimes 					goto slow;
17658f0484fSRodney W. Grimes 				t->bt_cmap = t->bt_smap;
17758f0484fSRodney W. Grimes 				t->bt_emap = t->bt_smap + sb.st_size;
178ef5d438eSPaul Traina 				t->bt_irec = F_ISSET(t, R_FIXLEN) ?
17958f0484fSRodney W. Grimes 				    __rec_fmap : __rec_vmap;
180ef5d438eSPaul Traina 				F_SET(t, R_MEMMAPPED);
181ef5d438eSPaul Traina #else
182ef5d438eSPaul Traina 				goto slow;
183ef5d438eSPaul Traina #endif
18458f0484fSRodney W. Grimes 			}
18558f0484fSRodney W. Grimes 		}
18658f0484fSRodney W. Grimes 	}
18758f0484fSRodney W. Grimes 
18858f0484fSRodney W. Grimes 	/* Use the recno routines. */
18958f0484fSRodney W. Grimes 	dbp->close = __rec_close;
19058f0484fSRodney W. Grimes 	dbp->del = __rec_delete;
19158f0484fSRodney W. Grimes 	dbp->fd = __rec_fd;
19258f0484fSRodney W. Grimes 	dbp->get = __rec_get;
19358f0484fSRodney W. Grimes 	dbp->put = __rec_put;
19458f0484fSRodney W. Grimes 	dbp->seq = __rec_seq;
19558f0484fSRodney W. Grimes 	dbp->sync = __rec_sync;
19658f0484fSRodney W. Grimes 
19758f0484fSRodney W. Grimes 	/* If the root page was created, reset the flags. */
19858f0484fSRodney W. Grimes 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
19958f0484fSRodney W. Grimes 		goto err;
20058f0484fSRodney W. Grimes 	if ((h->flags & P_TYPE) == P_BLEAF) {
201ef5d438eSPaul Traina 		F_CLR(h, P_TYPE);
202ef5d438eSPaul Traina 		F_SET(h, P_RLEAF);
20358f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
20458f0484fSRodney W. Grimes 	} else
20558f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, h, 0);
20658f0484fSRodney W. Grimes 
20758f0484fSRodney W. Grimes 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
208ef5d438eSPaul Traina 	    !F_ISSET(t, R_EOF | R_INMEM) &&
20958f0484fSRodney W. Grimes 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
21058f0484fSRodney W. Grimes                 goto err;
21158f0484fSRodney W. Grimes 	return (dbp);
21258f0484fSRodney W. Grimes 
21358f0484fSRodney W. Grimes einval:	errno = EINVAL;
21458f0484fSRodney W. Grimes err:	sverrno = errno;
21558f0484fSRodney W. Grimes 	if (dbp != NULL)
21658f0484fSRodney W. Grimes 		(void)__bt_close(dbp);
21758f0484fSRodney W. Grimes 	if (fname != NULL)
2189233c4d9SJason Evans 		(void)_close(rfd);
21958f0484fSRodney W. Grimes 	errno = sverrno;
22058f0484fSRodney W. Grimes 	return (NULL);
22158f0484fSRodney W. Grimes }
22258f0484fSRodney W. Grimes 
22358f0484fSRodney W. Grimes int
22458f0484fSRodney W. Grimes __rec_fd(dbp)
22558f0484fSRodney W. Grimes 	const DB *dbp;
22658f0484fSRodney W. Grimes {
22758f0484fSRodney W. Grimes 	BTREE *t;
22858f0484fSRodney W. Grimes 
22958f0484fSRodney W. Grimes 	t = dbp->internal;
23058f0484fSRodney W. Grimes 
23158f0484fSRodney W. Grimes 	/* Toss any page pinned across calls. */
23258f0484fSRodney W. Grimes 	if (t->bt_pinned != NULL) {
23358f0484fSRodney W. Grimes 		mpool_put(t->bt_mp, t->bt_pinned, 0);
23458f0484fSRodney W. Grimes 		t->bt_pinned = NULL;
23558f0484fSRodney W. Grimes 	}
23658f0484fSRodney W. Grimes 
23758f0484fSRodney W. Grimes 	/* In-memory database can't have a file descriptor. */
238ef5d438eSPaul Traina 	if (F_ISSET(t, R_INMEM)) {
23958f0484fSRodney W. Grimes 		errno = ENOENT;
24058f0484fSRodney W. Grimes 		return (-1);
24158f0484fSRodney W. Grimes 	}
24258f0484fSRodney W. Grimes 	return (t->bt_rfd);
24358f0484fSRodney W. Grimes }
244