/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Olson.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <sys/param.h>3637#include <errno.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <unistd.h>42#include "un-namespace.h"4344#include <db.h>45#include "btree.h"4647static int bt_meta(BTREE *);4849/*50* BT_CLOSE -- Close a btree.51*52* Parameters:53* dbp: pointer to access method54*55* Returns:56* RET_ERROR, RET_SUCCESS57*/58int59__bt_close(DB *dbp)60{61BTREE *t;62int fd;6364t = dbp->internal;6566/* Toss any page pinned across calls. */67if (t->bt_pinned != NULL) {68mpool_put(t->bt_mp, t->bt_pinned, 0);69t->bt_pinned = NULL;70}7172/* Sync the tree. */73if (__bt_sync(dbp, 0) == RET_ERROR)74return (RET_ERROR);7576/* Close the memory pool. */77if (mpool_close(t->bt_mp) == RET_ERROR)78return (RET_ERROR);7980/* Free random memory. */81if (t->bt_cursor.key.data != NULL) {82free(t->bt_cursor.key.data);83t->bt_cursor.key.size = 0;84t->bt_cursor.key.data = NULL;85}86if (t->bt_rkey.data) {87free(t->bt_rkey.data);88t->bt_rkey.size = 0;89t->bt_rkey.data = NULL;90}91if (t->bt_rdata.data) {92free(t->bt_rdata.data);93t->bt_rdata.size = 0;94t->bt_rdata.data = NULL;95}9697fd = t->bt_fd;98free(t);99free(dbp);100return (_close(fd) ? RET_ERROR : RET_SUCCESS);101}102103/*104* BT_SYNC -- sync the btree to disk.105*106* Parameters:107* dbp: pointer to access method108*109* Returns:110* RET_SUCCESS, RET_ERROR.111*/112int113__bt_sync(const DB *dbp, u_int flags)114{115BTREE *t;116int status;117118t = dbp->internal;119120/* Toss any page pinned across calls. */121if (t->bt_pinned != NULL) {122mpool_put(t->bt_mp, t->bt_pinned, 0);123t->bt_pinned = NULL;124}125126/* Sync doesn't currently take any flags. */127if (flags != 0) {128errno = EINVAL;129return (RET_ERROR);130}131132if (F_ISSET(t, B_INMEM | B_RDONLY) ||133!F_ISSET(t, B_MODIFIED | B_METADIRTY))134return (RET_SUCCESS);135136if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)137return (RET_ERROR);138139if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)140F_CLR(t, B_MODIFIED);141142return (status);143}144145/*146* BT_META -- write the tree meta data to disk.147*148* Parameters:149* t: tree150*151* Returns:152* RET_ERROR, RET_SUCCESS153*/154static int155bt_meta(BTREE *t)156{157BTMETA m;158void *p;159160if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)161return (RET_ERROR);162163/* Fill in metadata. */164m.magic = BTREEMAGIC;165m.version = BTREEVERSION;166m.psize = t->bt_psize;167m.free = t->bt_free;168m.nrecs = t->bt_nrecs;169m.flags = F_ISSET(t, SAVEMETA);170171memmove(p, &m, sizeof(BTMETA));172mpool_put(t->bt_mp, p, MPOOL_DIRTY);173return (RET_SUCCESS);174}175176177