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