]> git.ipfire.org Git - thirdparty/glibc.git/blame - db2/btree/bt_close.c
Update.
[thirdparty/glibc.git] / db2 / btree / bt_close.c
CommitLineData
92f1da4d
UD
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
7/*
8 * Copyright (c) 1990, 1993, 1994, 1995, 1996
9 * Keith Bostic. All rights reserved.
10 */
11/*
12 * Copyright (c) 1990, 1993, 1994, 1995
13 * The Regents of the University of California. All rights reserved.
14 *
15 * This code is derived from software contributed to Berkeley by
16 * Mike Olson.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47#include "config.h"
48
49#ifndef lint
af69217f 50static const char sccsid[] = "@(#)bt_close.c 10.25 (Sleepycat) 1/6/98";
92f1da4d
UD
51#endif /* not lint */
52
53#ifndef NO_SYSTEM_INCLUDES
54#include <sys/types.h>
55#include <sys/mman.h>
56
57#include <errno.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#endif
63
64#include "db_int.h"
65#include "db_page.h"
66#include "btree.h"
67
68static void __bam_upstat __P((DB *dbp));
69
70/*
71 * __bam_close --
72 * Close a btree.
73 *
74 * PUBLIC: int __bam_close __P((DB *));
75 */
76int
77__bam_close(dbp)
78 DB *dbp;
79{
80 BTREE *t;
81
82 DEBUG_LWRITE(dbp, NULL, "bam_close", NULL, NULL, 0);
83
84 t = dbp->internal;
85
86 /* Update tree statistics. */
87 __bam_upstat(dbp);
88
89 /* Free any allocated memory. */
90 if (t->bt_rkey.data)
91 FREE(t->bt_rkey.data, t->bt_rkey.size);
92 if (t->bt_rdata.data)
93 FREE(t->bt_rdata.data, t->bt_rdata.ulen);
94 if (t->bt_sp != t->bt_stack)
95 FREE(t->bt_sp, (t->bt_esp - t->bt_sp) * sizeof(EPG));
96
97 FREE(t, sizeof(BTREE));
98 dbp->internal = NULL;
99
100 return (0);
101}
102
103/*
104 * __bam_sync --
105 * Sync the btree to disk.
106 *
107 * PUBLIC: int __bam_sync __P((DB *, int));
108 */
109int
110__bam_sync(argdbp, flags)
111 DB *argdbp;
112 int flags;
113{
114 DB *dbp;
115 int ret;
116
117 DEBUG_LWRITE(argdbp, NULL, "bam_sync", NULL, NULL, flags);
118
119 /* Check for invalid flags. */
120 if ((ret = __db_syncchk(argdbp, flags)) != 0)
121 return (ret);
122
123 /* If it wasn't possible to modify the file, we're done. */
124 if (F_ISSET(argdbp, DB_AM_INMEM | DB_AM_RDONLY))
125 return (0);
126
127 GETHANDLE(argdbp, NULL, &dbp, ret);
128
129 /* Flush any dirty pages from the cache to the backing file. */
130 if ((ret = memp_fsync(dbp->mpf)) == DB_INCOMPLETE)
131 ret = 0;
132
133 PUTHANDLE(dbp);
134 return (ret);
135}
136
137/*
138 * __bam_upstat --
139 * Update tree statistics.
140 */
141static void
142__bam_upstat(dbp)
143 DB *dbp;
144{
145 BTREE *t;
146 BTMETA *meta;
af69217f 147 DB_LOCK metalock;
92f1da4d
UD
148 db_pgno_t pgno;
149 int flags, ret;
150
151 /*
152 * We use a no-op log call to log the update of the statistics onto the
a5a0310d 153 * metadata page. The Db->close call isn't transaction protected to
92f1da4d
UD
154 * start with, and I'm not sure what undoing a statistics update means,
155 * anyway.
156 */
157 if (F_ISSET(dbp, DB_AM_INMEM | DB_AM_RDONLY))
158 return;
159
92f1da4d
UD
160 flags = 0;
161 pgno = PGNO_METADATA;
162
26b4d766 163 /* Lock and retrieve the page. */
af69217f 164 if (__bam_lget(dbp, 0, pgno, DB_LOCK_WRITE, &metalock) != 0)
26b4d766 165 return;
92f1da4d
UD
166 if (__bam_pget(dbp, (PAGE **)&meta, &pgno, 0) == 0) {
167 /* Log the change. */
168 if (DB_LOGGING(dbp) &&
169 (ret = __db_noop_log(dbp->dbenv->lg_info, dbp->txn,
170 &LSN(meta), 0)) == 0)
171 goto err;
172
173 /* Update the statistics. */
174 t = dbp->internal;
175 __bam_add_mstat(&t->lstat, &meta->stat);
176
177 flags = DB_MPOOL_DIRTY;
178 }
179
180err: (void)memp_fput(dbp->mpf, (PAGE *)meta, flags);
af69217f 181 (void)__BT_LPUT(dbp, metalock);
92f1da4d 182}