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