xref: /netbsd/lib/libc/db/btree/bt_close.c (revision bf9ec67e)
1 /*	$NetBSD: bt_close.c,v 1.10 2002/01/22 20:41:21 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Mike Olson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)bt_close.c	8.7 (Berkeley) 8/17/94";
43 #else
44 __RCSID("$NetBSD: bt_close.c,v 1.10 2002/01/22 20:41:21 thorpej Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include "namespace.h"
49 
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include <db.h>
57 #include "btree.h"
58 
59 static int bt_meta __P((BTREE *));
60 
61 /*
62  * BT_CLOSE -- Close a btree.
63  *
64  * Parameters:
65  *	dbp:	pointer to access method
66  *
67  * Returns:
68  *	RET_ERROR, RET_SUCCESS
69  */
70 int
71 __bt_close(dbp)
72 	DB *dbp;
73 {
74 	BTREE *t;
75 	int fd;
76 
77 	t = dbp->internal;
78 
79 	/* Toss any page pinned across calls. */
80 	if (t->bt_pinned != NULL) {
81 		mpool_put(t->bt_mp, t->bt_pinned, 0);
82 		t->bt_pinned = NULL;
83 	}
84 
85 	/* Sync the tree. */
86 	if (__bt_sync(dbp, 0) == RET_ERROR)
87 		return (RET_ERROR);
88 
89 	/* Close the memory pool. */
90 	if (mpool_close(t->bt_mp) == RET_ERROR)
91 		return (RET_ERROR);
92 
93 	/* Free random memory. */
94 	if (t->bt_cursor.key.data != NULL) {
95 		free(t->bt_cursor.key.data);
96 		t->bt_cursor.key.size = 0;
97 		t->bt_cursor.key.data = NULL;
98 	}
99 	if (t->bt_rkey.data) {
100 		free(t->bt_rkey.data);
101 		t->bt_rkey.size = 0;
102 		t->bt_rkey.data = NULL;
103 	}
104 	if (t->bt_rdata.data) {
105 		free(t->bt_rdata.data);
106 		t->bt_rdata.size = 0;
107 		t->bt_rdata.data = NULL;
108 	}
109 
110 	fd = t->bt_fd;
111 	free(t);
112 	free(dbp);
113 	return (close(fd) ? RET_ERROR : RET_SUCCESS);
114 }
115 
116 /*
117  * BT_SYNC -- sync the btree to disk.
118  *
119  * Parameters:
120  *	dbp:	pointer to access method
121  *
122  * Returns:
123  *	RET_SUCCESS, RET_ERROR.
124  */
125 int
126 __bt_sync(dbp, flags)
127 	const DB *dbp;
128 	u_int flags;
129 {
130 	BTREE *t;
131 	int status;
132 
133 	t = dbp->internal;
134 
135 	/* Toss any page pinned across calls. */
136 	if (t->bt_pinned != NULL) {
137 		mpool_put(t->bt_mp, t->bt_pinned, 0);
138 		t->bt_pinned = NULL;
139 	}
140 
141 	/* Sync doesn't currently take any flags. */
142 	if (flags != 0) {
143 		errno = EINVAL;
144 		return (RET_ERROR);
145 	}
146 
147 	if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
148 		return (RET_SUCCESS);
149 
150 	if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
151 		return (RET_ERROR);
152 
153 	if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
154 		F_CLR(t, B_MODIFIED);
155 
156 	return (status);
157 }
158 
159 /*
160  * BT_META -- write the tree meta data to disk.
161  *
162  * Parameters:
163  *	t:	tree
164  *
165  * Returns:
166  *	RET_ERROR, RET_SUCCESS
167  */
168 static int
169 bt_meta(t)
170 	BTREE *t;
171 {
172 	BTMETA m;
173 	void *p;
174 
175 	if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
176 		return (RET_ERROR);
177 
178 	/* Fill in metadata. */
179 	m.magic = BTREEMAGIC;
180 	m.version = BTREEVERSION;
181 	m.psize = t->bt_psize;
182 	m.free = t->bt_free;
183 	m.nrecs = t->bt_nrecs;
184 	m.flags = F_ISSET(t, SAVEMETA);
185 
186 	memmove(p, &m, sizeof(BTMETA));
187 	mpool_put(t->bt_mp, p, MPOOL_DIRTY);
188 	return (RET_SUCCESS);
189 }
190