xref: /netbsd-src/lib/libc/db/btree/bt_close.c (revision aae80e6be7bf8e6ad43f8b63d296d2d3923c4ee5)
1*aae80e6bSchristos /*	$NetBSD: bt_close.c,v 1.17 2016/09/24 21:31:25 christos Exp $	*/
2a954b078Scgd 
39f0aa214Scgd /*-
424420c01Scgd  * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd  *	The Regents of the University of California.  All rights reserved.
69f0aa214Scgd  *
79f0aa214Scgd  * This code is derived from software contributed to Berkeley by
89f0aa214Scgd  * Mike Olson.
99f0aa214Scgd  *
109f0aa214Scgd  * Redistribution and use in source and binary forms, with or without
119f0aa214Scgd  * modification, are permitted provided that the following conditions
129f0aa214Scgd  * are met:
139f0aa214Scgd  * 1. Redistributions of source code must retain the above copyright
149f0aa214Scgd  *    notice, this list of conditions and the following disclaimer.
159f0aa214Scgd  * 2. Redistributions in binary form must reproduce the above copyright
169f0aa214Scgd  *    notice, this list of conditions and the following disclaimer in the
179f0aa214Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
199f0aa214Scgd  *    may be used to endorse or promote products derived from this software
209f0aa214Scgd  *    without specific prior written permission.
219f0aa214Scgd  *
229f0aa214Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239f0aa214Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249f0aa214Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259f0aa214Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269f0aa214Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279f0aa214Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289f0aa214Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299f0aa214Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309f0aa214Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319f0aa214Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329f0aa214Scgd  * SUCH DAMAGE.
339f0aa214Scgd  */
349f0aa214Scgd 
35d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
36d3595ddfSjoerg #include "nbtool_config.h"
37d3595ddfSjoerg #endif
38d3595ddfSjoerg 
3900ae392dSchristos #include <sys/cdefs.h>
40*aae80e6bSchristos __RCSID("$NetBSD: bt_close.c,v 1.17 2016/09/24 21:31:25 christos Exp $");
419f0aa214Scgd 
4243fa6fe3Sjtc #include "namespace.h"
439f0aa214Scgd 
44cb9daf8fSchristos #include <assert.h>
459f0aa214Scgd #include <errno.h>
469f0aa214Scgd #include <stdio.h>
479f0aa214Scgd #include <stdlib.h>
489f0aa214Scgd #include <string.h>
499f0aa214Scgd #include <unistd.h>
509f0aa214Scgd 
519f0aa214Scgd #include <db.h>
529f0aa214Scgd #include "btree.h"
539f0aa214Scgd 
54cb9daf8fSchristos static int bt_meta(BTREE *);
559f0aa214Scgd 
569f0aa214Scgd /*
579f0aa214Scgd  * BT_CLOSE -- Close a btree.
589f0aa214Scgd  *
599f0aa214Scgd  * Parameters:
609f0aa214Scgd  *	dbp:	pointer to access method
619f0aa214Scgd  *
629f0aa214Scgd  * Returns:
639f0aa214Scgd  *	RET_ERROR, RET_SUCCESS
649f0aa214Scgd  */
659f0aa214Scgd int
__bt_close(DB * dbp)66cb9daf8fSchristos __bt_close(DB *dbp)
679f0aa214Scgd {
689f0aa214Scgd 	BTREE *t;
699f0aa214Scgd 	int fd;
709f0aa214Scgd 
719f0aa214Scgd 	t = dbp->internal;
729f0aa214Scgd 
7345e27c80Scgd 	/* Toss any page pinned across calls. */
7445e27c80Scgd 	if (t->bt_pinned != NULL) {
7545e27c80Scgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
7645e27c80Scgd 		t->bt_pinned = NULL;
7745e27c80Scgd 	}
7845e27c80Scgd 
7924420c01Scgd 	/* Sync the tree. */
809f0aa214Scgd 	if (__bt_sync(dbp, 0) == RET_ERROR)
819f0aa214Scgd 		return (RET_ERROR);
829f0aa214Scgd 
8324420c01Scgd 	/* Close the memory pool. */
849f0aa214Scgd 	if (mpool_close(t->bt_mp) == RET_ERROR)
859f0aa214Scgd 		return (RET_ERROR);
869f0aa214Scgd 
8724420c01Scgd 	/* Free random memory. */
8824420c01Scgd 	if (t->bt_cursor.key.data != NULL) {
8924420c01Scgd 		free(t->bt_cursor.key.data);
9024420c01Scgd 		t->bt_cursor.key.size = 0;
9124420c01Scgd 		t->bt_cursor.key.data = NULL;
9224420c01Scgd 	}
9324420c01Scgd 	if (t->bt_rkey.data) {
9424420c01Scgd 		free(t->bt_rkey.data);
9524420c01Scgd 		t->bt_rkey.size = 0;
9624420c01Scgd 		t->bt_rkey.data = NULL;
9724420c01Scgd 	}
9824420c01Scgd 	if (t->bt_rdata.data) {
9924420c01Scgd 		free(t->bt_rdata.data);
10024420c01Scgd 		t->bt_rdata.size = 0;
10124420c01Scgd 		t->bt_rdata.data = NULL;
10224420c01Scgd 	}
1039f0aa214Scgd 
1049f0aa214Scgd 	fd = t->bt_fd;
1059f0aa214Scgd 	free(t);
1069f0aa214Scgd 	free(dbp);
1079f0aa214Scgd 	return (close(fd) ? RET_ERROR : RET_SUCCESS);
1089f0aa214Scgd }
1099f0aa214Scgd 
1109f0aa214Scgd /*
1119f0aa214Scgd  * BT_SYNC -- sync the btree to disk.
1129f0aa214Scgd  *
1139f0aa214Scgd  * Parameters:
1149f0aa214Scgd  *	dbp:	pointer to access method
1159f0aa214Scgd  *
1169f0aa214Scgd  * Returns:
1179f0aa214Scgd  *	RET_SUCCESS, RET_ERROR.
1189f0aa214Scgd  */
1199f0aa214Scgd int
__bt_sync(const DB * dbp,u_int flags)120cb9daf8fSchristos __bt_sync(const DB *dbp, u_int flags)
1219f0aa214Scgd {
1229f0aa214Scgd 	BTREE *t;
1239f0aa214Scgd 	int status;
1249f0aa214Scgd 
12545e27c80Scgd 	t = dbp->internal;
12645e27c80Scgd 
12745e27c80Scgd 	/* Toss any page pinned across calls. */
12845e27c80Scgd 	if (t->bt_pinned != NULL) {
12945e27c80Scgd 		mpool_put(t->bt_mp, t->bt_pinned, 0);
13045e27c80Scgd 		t->bt_pinned = NULL;
13145e27c80Scgd 	}
13245e27c80Scgd 
13345e27c80Scgd 	/* Sync doesn't currently take any flags. */
1349f0aa214Scgd 	if (flags != 0) {
1359f0aa214Scgd 		errno = EINVAL;
1369f0aa214Scgd 		return (RET_ERROR);
1379f0aa214Scgd 	}
1389f0aa214Scgd 
139d2f713bbSchristos 	if (F_ISSET(t, B_INMEM | B_RDONLY)
140d2f713bbSchristos 	    || !F_ISSET(t, B_MODIFIED | B_METADIRTY))
1419f0aa214Scgd 		return (RET_SUCCESS);
1429f0aa214Scgd 
14324420c01Scgd 	if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
1449f0aa214Scgd 		return (RET_ERROR);
1459f0aa214Scgd 
1469f0aa214Scgd 	if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
14724420c01Scgd 		F_CLR(t, B_MODIFIED);
1489f0aa214Scgd 
1499f0aa214Scgd 	return (status);
1509f0aa214Scgd }
1519f0aa214Scgd 
1529f0aa214Scgd /*
1539f0aa214Scgd  * BT_META -- write the tree meta data to disk.
1549f0aa214Scgd  *
1559f0aa214Scgd  * Parameters:
1569f0aa214Scgd  *	t:	tree
1579f0aa214Scgd  *
1589f0aa214Scgd  * Returns:
1599f0aa214Scgd  *	RET_ERROR, RET_SUCCESS
1609f0aa214Scgd  */
1619f0aa214Scgd static int
bt_meta(BTREE * t)162cb9daf8fSchristos bt_meta(BTREE *t)
1639f0aa214Scgd {
1649f0aa214Scgd 	BTMETA m;
1659f0aa214Scgd 	void *p;
1669f0aa214Scgd 
167*aae80e6bSchristos 	if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
1689f0aa214Scgd 		return (RET_ERROR);
1699f0aa214Scgd 
1709f0aa214Scgd 	/* Fill in metadata. */
17124420c01Scgd 	m.magic = BTREEMAGIC;
17224420c01Scgd 	m.version = BTREEVERSION;
17324420c01Scgd 	m.psize = t->bt_psize;
17424420c01Scgd 	m.free = t->bt_free;
17524420c01Scgd 	m.nrecs = t->bt_nrecs;
17624420c01Scgd 	m.flags = F_ISSET(t, SAVEMETA);
1779f0aa214Scgd 
1789f0aa214Scgd 	memmove(p, &m, sizeof(BTMETA));
1799f0aa214Scgd 	mpool_put(t->bt_mp, p, MPOOL_DIRTY);
1809f0aa214Scgd 	return (RET_SUCCESS);
1819f0aa214Scgd }
182