Home | History | Annotate | Line # | Download | only in btree
bt_close.c revision 1.9
      1 /*	$NetBSD: bt_close.c,v 1.9 1997/07/21 14:06:30 jtc 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.9 1997/07/21 14:06:30 jtc Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #include "namespace.h"
     49 #include <sys/param.h>
     50 
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 #include <db.h>
     58 #include "btree.h"
     59 
     60 static int bt_meta __P((BTREE *));
     61 
     62 /*
     63  * BT_CLOSE -- Close a btree.
     64  *
     65  * Parameters:
     66  *	dbp:	pointer to access method
     67  *
     68  * Returns:
     69  *	RET_ERROR, RET_SUCCESS
     70  */
     71 int
     72 __bt_close(dbp)
     73 	DB *dbp;
     74 {
     75 	BTREE *t;
     76 	int fd;
     77 
     78 	t = dbp->internal;
     79 
     80 	/* Toss any page pinned across calls. */
     81 	if (t->bt_pinned != NULL) {
     82 		mpool_put(t->bt_mp, t->bt_pinned, 0);
     83 		t->bt_pinned = NULL;
     84 	}
     85 
     86 	/* Sync the tree. */
     87 	if (__bt_sync(dbp, 0) == RET_ERROR)
     88 		return (RET_ERROR);
     89 
     90 	/* Close the memory pool. */
     91 	if (mpool_close(t->bt_mp) == RET_ERROR)
     92 		return (RET_ERROR);
     93 
     94 	/* Free random memory. */
     95 	if (t->bt_cursor.key.data != NULL) {
     96 		free(t->bt_cursor.key.data);
     97 		t->bt_cursor.key.size = 0;
     98 		t->bt_cursor.key.data = NULL;
     99 	}
    100 	if (t->bt_rkey.data) {
    101 		free(t->bt_rkey.data);
    102 		t->bt_rkey.size = 0;
    103 		t->bt_rkey.data = NULL;
    104 	}
    105 	if (t->bt_rdata.data) {
    106 		free(t->bt_rdata.data);
    107 		t->bt_rdata.size = 0;
    108 		t->bt_rdata.data = NULL;
    109 	}
    110 
    111 	fd = t->bt_fd;
    112 	free(t);
    113 	free(dbp);
    114 	return (close(fd) ? RET_ERROR : RET_SUCCESS);
    115 }
    116 
    117 /*
    118  * BT_SYNC -- sync the btree to disk.
    119  *
    120  * Parameters:
    121  *	dbp:	pointer to access method
    122  *
    123  * Returns:
    124  *	RET_SUCCESS, RET_ERROR.
    125  */
    126 int
    127 __bt_sync(dbp, flags)
    128 	const DB *dbp;
    129 	u_int flags;
    130 {
    131 	BTREE *t;
    132 	int status;
    133 
    134 	t = dbp->internal;
    135 
    136 	/* Toss any page pinned across calls. */
    137 	if (t->bt_pinned != NULL) {
    138 		mpool_put(t->bt_mp, t->bt_pinned, 0);
    139 		t->bt_pinned = NULL;
    140 	}
    141 
    142 	/* Sync doesn't currently take any flags. */
    143 	if (flags != 0) {
    144 		errno = EINVAL;
    145 		return (RET_ERROR);
    146 	}
    147 
    148 	if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
    149 		return (RET_SUCCESS);
    150 
    151 	if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
    152 		return (RET_ERROR);
    153 
    154 	if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
    155 		F_CLR(t, B_MODIFIED);
    156 
    157 	return (status);
    158 }
    159 
    160 /*
    161  * BT_META -- write the tree meta data to disk.
    162  *
    163  * Parameters:
    164  *	t:	tree
    165  *
    166  * Returns:
    167  *	RET_ERROR, RET_SUCCESS
    168  */
    169 static int
    170 bt_meta(t)
    171 	BTREE *t;
    172 {
    173 	BTMETA m;
    174 	void *p;
    175 
    176 	if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
    177 		return (RET_ERROR);
    178 
    179 	/* Fill in metadata. */
    180 	m.magic = BTREEMAGIC;
    181 	m.version = BTREEVERSION;
    182 	m.psize = t->bt_psize;
    183 	m.free = t->bt_free;
    184 	m.nrecs = t->bt_nrecs;
    185 	m.flags = F_ISSET(t, SAVEMETA);
    186 
    187 	memmove(p, &m, sizeof(BTMETA));
    188 	mpool_put(t->bt_mp, p, MPOOL_DIRTY);
    189 	return (RET_SUCCESS);
    190 }
    191