1 /*- 2 * Copyright (c) 1990, 1993 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 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)bt_close.c 8.1 (Berkeley) 06/04/93"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/param.h> 16 17 #include <errno.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <unistd.h> 22 23 #include <db.h> 24 #include "btree.h" 25 26 static int bt_meta __P((BTREE *)); 27 28 /* 29 * BT_CLOSE -- Close a btree. 30 * 31 * Parameters: 32 * dbp: pointer to access method 33 * 34 * Returns: 35 * RET_ERROR, RET_SUCCESS 36 */ 37 int 38 __bt_close(dbp) 39 DB *dbp; 40 { 41 BTREE *t; 42 int fd; 43 44 t = dbp->internal; 45 46 /* 47 * Delete any already deleted record that we've been saving 48 * because the cursor pointed to it. 49 */ 50 if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor)) 51 return (RET_ERROR); 52 53 if (__bt_sync(dbp, 0) == RET_ERROR) 54 return (RET_ERROR); 55 56 if (mpool_close(t->bt_mp) == RET_ERROR) 57 return (RET_ERROR); 58 59 if (t->bt_stack) 60 free(t->bt_stack); 61 if (t->bt_kbuf) 62 free(t->bt_kbuf); 63 if (t->bt_dbuf) 64 free(t->bt_dbuf); 65 66 fd = t->bt_fd; 67 free(t); 68 free(dbp); 69 return (close(fd) ? RET_ERROR : RET_SUCCESS); 70 } 71 72 /* 73 * BT_SYNC -- sync the btree to disk. 74 * 75 * Parameters: 76 * dbp: pointer to access method 77 * 78 * Returns: 79 * RET_SUCCESS, RET_ERROR. 80 */ 81 int 82 __bt_sync(dbp, flags) 83 const DB *dbp; 84 u_int flags; 85 { 86 BTREE *t; 87 int status; 88 PAGE *h; 89 void *p; 90 91 if (flags != 0) { 92 errno = EINVAL; 93 return (RET_ERROR); 94 } 95 96 t = dbp->internal; 97 98 if (ISSET(t, B_INMEM | B_RDONLY) || !ISSET(t, B_MODIFIED)) 99 return (RET_SUCCESS); 100 101 if (ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR) 102 return (RET_ERROR); 103 104 /* 105 * Nastiness. If the cursor has been marked for deletion, but not 106 * actually deleted, we have to make a copy of the page, delete the 107 * key/data item, sync the file, and then restore the original page 108 * contents. 109 */ 110 if (ISSET(t, B_DELCRSR)) { 111 if ((p = malloc(t->bt_psize)) == NULL) 112 return (RET_ERROR); 113 if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) 114 return (RET_ERROR); 115 memmove(p, h, t->bt_psize); 116 if ((status = 117 __bt_dleaf(t, h, t->bt_bcursor.index)) == RET_ERROR) 118 goto ecrsr; 119 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 120 } 121 122 if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS) 123 CLR(t, B_MODIFIED); 124 125 ecrsr: if (ISSET(t, B_DELCRSR)) { 126 if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL) 127 return (RET_ERROR); 128 memmove(h, p, t->bt_psize); 129 free(p); 130 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 131 } 132 return (status); 133 } 134 135 /* 136 * BT_META -- write the tree meta data to disk. 137 * 138 * Parameters: 139 * t: tree 140 * 141 * Returns: 142 * RET_ERROR, RET_SUCCESS 143 */ 144 static int 145 bt_meta(t) 146 BTREE *t; 147 { 148 BTMETA m; 149 void *p; 150 151 if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL) 152 return (RET_ERROR); 153 154 /* Fill in metadata. */ 155 m.m_magic = BTREEMAGIC; 156 m.m_version = BTREEVERSION; 157 m.m_psize = t->bt_psize; 158 m.m_free = t->bt_free; 159 m.m_nrecs = t->bt_nrecs; 160 m.m_flags = t->bt_flags & SAVEMETA; 161 162 memmove(p, &m, sizeof(BTMETA)); 163 mpool_put(t->bt_mp, p, MPOOL_DIRTY); 164 return (RET_SUCCESS); 165 } 166