1 /* $NetBSD: rec_close.c,v 1.11 2002/11/11 20:05:53 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)rec_close.c 8.6 (Berkeley) 8/18/94"; 40 #else 41 __RCSID("$NetBSD: rec_close.c,v 1.11 2002/11/11 20:05:53 thorpej Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/types.h> 47 #include <sys/uio.h> 48 #include <sys/mman.h> 49 50 #include <errno.h> 51 #include <limits.h> 52 #include <stdio.h> 53 #include <unistd.h> 54 55 #include <db.h> 56 #include "recno.h" 57 58 /* 59 * __REC_CLOSE -- Close a recno tree. 60 * 61 * Parameters: 62 * dbp: pointer to access method 63 * 64 * Returns: 65 * RET_ERROR, RET_SUCCESS 66 */ 67 int 68 __rec_close(dbp) 69 DB *dbp; 70 { 71 BTREE *t; 72 int status; 73 74 t = dbp->internal; 75 76 /* Toss any page pinned across calls. */ 77 if (t->bt_pinned != NULL) { 78 mpool_put(t->bt_mp, t->bt_pinned, 0); 79 t->bt_pinned = NULL; 80 } 81 82 if (__rec_sync(dbp, 0) == RET_ERROR) 83 return (RET_ERROR); 84 85 /* Committed to closing. */ 86 status = RET_SUCCESS; 87 if (F_ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize)) 88 status = RET_ERROR; 89 90 if (!F_ISSET(t, R_INMEM)) { 91 if (F_ISSET(t, R_CLOSEFP)) { 92 if (fclose(t->bt_rfp)) 93 status = RET_ERROR; 94 } else { 95 if (close(t->bt_rfd)) 96 status = RET_ERROR; 97 } 98 } 99 100 if (__bt_close(dbp) == RET_ERROR) 101 status = RET_ERROR; 102 103 return (status); 104 } 105 106 /* 107 * __REC_SYNC -- sync the recno tree to disk. 108 * 109 * Parameters: 110 * dbp: pointer to access method 111 * 112 * Returns: 113 * RET_SUCCESS, RET_ERROR. 114 */ 115 int 116 __rec_sync(dbp, flags) 117 const DB *dbp; 118 u_int flags; 119 { 120 struct iovec iov[2]; 121 BTREE *t; 122 DBT data, key; 123 off_t off; 124 recno_t scursor, trec; 125 int status; 126 127 t = dbp->internal; 128 129 /* Toss any page pinned across calls. */ 130 if (t->bt_pinned != NULL) { 131 mpool_put(t->bt_mp, t->bt_pinned, 0); 132 t->bt_pinned = NULL; 133 } 134 135 if (flags == R_RECNOSYNC) 136 return (__bt_sync(dbp, 0)); 137 138 if (F_ISSET(t, R_RDONLY | R_INMEM) || !F_ISSET(t, R_MODIFIED)) 139 return (RET_SUCCESS); 140 141 /* Read any remaining records into the tree. */ 142 if (!F_ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 143 return (RET_ERROR); 144 145 /* Rewind the file descriptor. */ 146 if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0) 147 return (RET_ERROR); 148 149 /* Save the cursor. */ 150 scursor = t->bt_cursor.rcursor; 151 152 key.size = sizeof(recno_t); 153 key.data = &trec; 154 155 if (F_ISSET(t, R_FIXLEN)) { 156 /* 157 * We assume that fixed length records are all fixed length. 158 * Any that aren't are either EINVAL'd or corrected by the 159 * record put code. 160 */ 161 status = (dbp->seq)(dbp, &key, &data, R_FIRST); 162 while (status == RET_SUCCESS) { 163 if (write(t->bt_rfd, data.data, data.size) != 164 (ssize_t) data.size) 165 return (RET_ERROR); 166 status = (dbp->seq)(dbp, &key, &data, R_NEXT); 167 } 168 } else { 169 iov[1].iov_base = &t->bt_bval; 170 iov[1].iov_len = 1; 171 172 status = (dbp->seq)(dbp, &key, &data, R_FIRST); 173 while (status == RET_SUCCESS) { 174 iov[0].iov_base = data.data; 175 iov[0].iov_len = data.size; 176 if (writev(t->bt_rfd, iov, 2) != 177 (ssize_t) (data.size + 1)) 178 return (RET_ERROR); 179 status = (dbp->seq)(dbp, &key, &data, R_NEXT); 180 } 181 } 182 183 /* Restore the cursor. */ 184 t->bt_cursor.rcursor = scursor; 185 186 if (status == RET_ERROR) 187 return (RET_ERROR); 188 if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1) 189 return (RET_ERROR); 190 if (ftruncate(t->bt_rfd, off)) 191 return (RET_ERROR); 192 F_CLR(t, R_MODIFIED); 193 return (RET_SUCCESS); 194 } 195