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