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