150990Sbostic /*- 2*61194Sbostic * Copyright (c) 1990, 1993 3*61194Sbostic * The Regents of the University of California. All rights reserved. 450990Sbostic * 550990Sbostic * This code is derived from software contributed to Berkeley by 650990Sbostic * Mike Olson. 750990Sbostic * 850990Sbostic * %sccs.include.redist.c% 950990Sbostic */ 1050990Sbostic 1150990Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*61194Sbostic static char sccsid[] = "@(#)bt_close.c 8.1 (Berkeley) 06/04/93"; 1350990Sbostic #endif /* LIBC_SCCS and not lint */ 1450990Sbostic 1550990Sbostic #include <sys/param.h> 1656742Sbostic 1750990Sbostic #include <errno.h> 1850990Sbostic #include <stdio.h> 1950990Sbostic #include <stdlib.h> 2050990Sbostic #include <string.h> 2156742Sbostic #include <unistd.h> 2256742Sbostic 2357932Sbostic #include <db.h> 2450990Sbostic #include "btree.h" 2550990Sbostic 2650990Sbostic static int bt_meta __P((BTREE *)); 2750990Sbostic 2850990Sbostic /* 2950990Sbostic * BT_CLOSE -- Close a btree. 3050990Sbostic * 3150990Sbostic * Parameters: 3250990Sbostic * dbp: pointer to access method 3350990Sbostic * 3450990Sbostic * Returns: 3550990Sbostic * RET_ERROR, RET_SUCCESS 3650990Sbostic */ 3750990Sbostic int 3850990Sbostic __bt_close(dbp) 3950990Sbostic DB *dbp; 4050990Sbostic { 4150990Sbostic BTREE *t; 4250990Sbostic int fd; 4350990Sbostic 4450990Sbostic t = dbp->internal; 4550990Sbostic 4650990Sbostic /* 4750990Sbostic * Delete any already deleted record that we've been saving 4850990Sbostic * because the cursor pointed to it. 4950990Sbostic */ 5060047Sbostic if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor)) 5150990Sbostic return (RET_ERROR); 5250990Sbostic 5360047Sbostic if (__bt_sync(dbp, 0) == RET_ERROR) 5450990Sbostic return (RET_ERROR); 5550990Sbostic 5650990Sbostic if (mpool_close(t->bt_mp) == RET_ERROR) 5750990Sbostic return (RET_ERROR); 5850990Sbostic 5950990Sbostic if (t->bt_stack) 6050990Sbostic free(t->bt_stack); 6150990Sbostic if (t->bt_kbuf) 6250990Sbostic free(t->bt_kbuf); 6350990Sbostic if (t->bt_dbuf) 6450990Sbostic free(t->bt_dbuf); 6550990Sbostic 6650990Sbostic fd = t->bt_fd; 6750990Sbostic free(t); 6850990Sbostic free(dbp); 6950990Sbostic return (close(fd) ? RET_ERROR : RET_SUCCESS); 7050990Sbostic } 7150990Sbostic 7250990Sbostic /* 7350990Sbostic * BT_SYNC -- sync the btree to disk. 7450990Sbostic * 7550990Sbostic * Parameters: 7650990Sbostic * dbp: pointer to access method 7750990Sbostic * 7850990Sbostic * Returns: 7950990Sbostic * RET_SUCCESS, RET_ERROR. 8050990Sbostic */ 8150990Sbostic int 8260047Sbostic __bt_sync(dbp, flags) 8350990Sbostic const DB *dbp; 8460047Sbostic u_int flags; 8550990Sbostic { 8650990Sbostic BTREE *t; 8750990Sbostic int status; 8851097Sbostic PAGE *h; 8951097Sbostic void *p; 9050990Sbostic 9160047Sbostic if (flags != 0) { 9260047Sbostic errno = EINVAL; 9360047Sbostic return (RET_ERROR); 9460047Sbostic } 9560047Sbostic 9650990Sbostic t = dbp->internal; 9750990Sbostic 9860047Sbostic if (ISSET(t, B_INMEM | B_RDONLY) || !ISSET(t, B_MODIFIED)) 9950990Sbostic return (RET_SUCCESS); 10050990Sbostic 10160047Sbostic if (ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR) 10250990Sbostic return (RET_ERROR); 10350990Sbostic 10451097Sbostic /* 10551097Sbostic * Nastiness. If the cursor has been marked for deletion, but not 10651097Sbostic * actually deleted, we have to make a copy of the page, delete the 10751097Sbostic * key/data item, sync the file, and then restore the original page 10851097Sbostic * contents. 10951097Sbostic */ 11060047Sbostic if (ISSET(t, B_DELCRSR)) { 11156742Sbostic if ((p = malloc(t->bt_psize)) == NULL) 11256742Sbostic return (RET_ERROR); 11351097Sbostic if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) 11451097Sbostic return (RET_ERROR); 11558017Sbostic memmove(p, h, t->bt_psize); 11659630Sbostic if ((status = 11759630Sbostic __bt_dleaf(t, h, t->bt_bcursor.index)) == RET_ERROR) 11851097Sbostic goto ecrsr; 11957652Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 12051097Sbostic } 12151097Sbostic 12251097Sbostic if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS) 12360047Sbostic CLR(t, B_MODIFIED); 12456742Sbostic 12560047Sbostic ecrsr: if (ISSET(t, B_DELCRSR)) { 12657652Sbostic if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) 12757652Sbostic return (RET_ERROR); 12858017Sbostic memmove(h, p, t->bt_psize); 12951097Sbostic free(p); 13057652Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 13150990Sbostic } 13250990Sbostic return (status); 13350990Sbostic } 13450990Sbostic 13550990Sbostic /* 13650990Sbostic * BT_META -- write the tree meta data to disk. 13750990Sbostic * 13850990Sbostic * Parameters: 13950990Sbostic * t: tree 14050990Sbostic * 14150990Sbostic * Returns: 14250990Sbostic * RET_ERROR, RET_SUCCESS 14350990Sbostic */ 14450990Sbostic static int 14550990Sbostic bt_meta(t) 14650990Sbostic BTREE *t; 14750990Sbostic { 14850990Sbostic BTMETA m; 14950990Sbostic void *p; 15050990Sbostic 15150990Sbostic if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL) 15250990Sbostic return (RET_ERROR); 15350990Sbostic 15459630Sbostic /* Fill in metadata. */ 15550990Sbostic m.m_magic = BTREEMAGIC; 15650990Sbostic m.m_version = BTREEVERSION; 15750990Sbostic m.m_psize = t->bt_psize; 15856491Sbostic m.m_free = t->bt_free; 15950990Sbostic m.m_nrecs = t->bt_nrecs; 16050990Sbostic m.m_flags = t->bt_flags & SAVEMETA; 16150990Sbostic 16258017Sbostic memmove(p, &m, sizeof(BTMETA)); 16350990Sbostic mpool_put(t->bt_mp, p, MPOOL_DIRTY); 16450990Sbostic return (RET_SUCCESS); 16550990Sbostic } 166