Update.
[glibc.git] / db2 / log / log_rec.c
blob5deac46298f79b73ce269800bdbcd0f0400f3afd
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
7 /*
8 * Copyright (c) 1995, 1996
9 * The President and Fellows of Harvard University. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "config.h"
42 #ifndef lint
43 static const char sccsid[] = "@(#)log_rec.c 10.20 (Sleepycat) 4/28/98";
44 #endif /* not lint */
46 #ifndef NO_SYSTEM_INCLUDES
47 #include <sys/types.h>
49 #include <errno.h>
50 #include <string.h>
51 #endif
53 #include "db_int.h"
54 #include "shqueue.h"
55 #include "log.h"
56 #include "db_dispatch.h"
57 #include "common_ext.h"
59 static int __log_open_file __P((DB_LOG *,
60 u_int8_t *, char *, DBTYPE, u_int32_t));
63 * PUBLIC: int __log_register_recover
64 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
66 int
67 __log_register_recover(logp, dbtp, lsnp, redo, info)
68 DB_LOG *logp;
69 DBT *dbtp;
70 DB_LSN *lsnp;
71 int redo;
72 void *info;
74 __log_register_args *argp;
75 int ret;
77 #ifdef DEBUG_RECOVER
78 __log_register_print(logp, dbtp, lsnp, redo, info);
79 #endif
80 COMPQUIET(info, NULL);
81 COMPQUIET(lsnp, NULL);
83 F_SET(logp, DB_AM_RECOVER);
85 if ((ret = __log_register_read(dbtp->data, &argp)) != 0)
86 goto out;
88 if ((argp->opcode == LOG_CHECKPOINT && redo == TXN_OPENFILES) ||
89 (argp->opcode == LOG_OPEN &&
90 (redo == TXN_REDO || redo == TXN_OPENFILES ||
91 redo == TXN_FORWARD_ROLL)) ||
92 (argp->opcode == LOG_CLOSE &&
93 (redo == TXN_UNDO || redo == TXN_BACKWARD_ROLL))) {
95 * If we are redoing an open or undoing a close, then we need
96 * to open a file.
98 ret = __log_open_file(logp,
99 argp->uid.data, argp->name.data, argp->ftype, argp->id);
100 if (ret == ENOENT) {
101 if (redo == TXN_OPENFILES)
102 __db_err(logp->dbenv,
103 "warning: file %s not found",
104 argp->name.data);
105 ret = 0;
107 } else if (argp->opcode != LOG_CHECKPOINT) {
109 * If we are redoing a close or undoing an open, then we need
110 * to close the file.
112 * If the file is deleted, then we can just ignore this close.
113 * Otherwise, we'd better have a valid dbp that we should either
114 * close or whose reference count should be decremented.
116 LOCK_LOGTHREAD(logp);
117 if (logp->dbentry[argp->id].dbp == NULL) {
118 if (!logp->dbentry[argp->id].deleted)
119 ret = EINVAL;
120 } else if (--logp->dbentry[argp->id].refcount == 0) {
121 F_SET(logp->dbentry[argp->id].dbp, DB_AM_RECOVER);
122 ret = logp->dbentry[argp->id].dbp->close(
123 logp->dbentry[argp->id].dbp, 0);
124 logp->dbentry[argp->id].dbp = NULL;
126 UNLOCK_LOGTHREAD(logp);
129 out: F_CLR(logp, DB_AM_RECOVER);
130 if (argp != NULL)
131 __db_free(argp);
132 return (ret);
135 /* Hand coded routines. */
138 * Called during log_register recovery. Make sure that we have an
139 * entry in the dbentry table for this ndx.
140 * Returns 0 on success, non-zero on error.
142 static int
143 __log_open_file(lp, uid, name, ftype, ndx)
144 DB_LOG *lp;
145 u_int8_t *uid;
146 char *name;
147 DBTYPE ftype;
148 u_int32_t ndx;
150 DB *dbp;
151 int ret;
153 LOCK_LOGTHREAD(lp);
154 if (ndx < lp->dbentry_cnt &&
155 (lp->dbentry[ndx].deleted == 1 || lp->dbentry[ndx].dbp != NULL)) {
156 lp->dbentry[ndx].refcount++;
158 UNLOCK_LOGTHREAD(lp);
159 return (0);
161 UNLOCK_LOGTHREAD(lp);
163 /* Need to open file. */
164 dbp = NULL;
165 if ((ret = db_open(name, ftype, 0, 0, lp->dbenv, NULL, &dbp)) == 0) {
167 * Verify that we are opening the same file that we were
168 * referring to when we wrote this log record.
170 if (memcmp(uid, dbp->lock.fileid, DB_FILE_ID_LEN) != 0) {
171 (void)dbp->close(dbp, 0);
172 dbp = NULL;
173 ret = ENOENT;
177 if (ret == 0 || ret == ENOENT)
178 (void)__log_add_logid(lp, dbp, ndx);
180 return (ret);
184 * This function returns:
185 * 0 SUCCESS (the entry was not previously set and is now set or the
186 * entry was previously set and we just inced the ref count.
187 * >0 on system error (returns errno value).
188 * PUBLIC: int __log_add_logid __P((DB_LOG *, DB *, u_int32_t));
191 __log_add_logid(logp, dbp, ndx)
192 DB_LOG *logp;
193 DB *dbp;
194 u_int32_t ndx;
196 DB_ENTRY *temp_entryp;
197 u_int32_t i;
198 int ret;
200 ret = 0;
202 LOCK_LOGTHREAD(logp);
204 * Check if we need to grow the table.
206 if (logp->dbentry_cnt <= ndx) {
207 if (logp->dbentry_cnt == 0) {
208 logp->dbentry = (DB_ENTRY *)
209 __db_malloc(DB_GROW_SIZE * sizeof(DB_ENTRY));
210 if (logp->dbentry == NULL) {
211 ret = ENOMEM;
212 goto err;
214 } else {
215 temp_entryp = (DB_ENTRY *)__db_realloc(logp->dbentry,
216 (DB_GROW_SIZE + logp->dbentry_cnt) *
217 sizeof(DB_ENTRY));
218 if (temp_entryp == NULL) {
219 ret = ENOMEM;
220 goto err;
222 logp->dbentry = temp_entryp;
225 /* Initialize the new entries. */
226 for (i = logp->dbentry_cnt;
227 i < logp->dbentry_cnt + DB_GROW_SIZE; i++) {
228 logp->dbentry[i].dbp = NULL;
229 logp->dbentry[i].deleted = 0;
232 logp->dbentry_cnt += DB_GROW_SIZE;
235 if (logp->dbentry[ndx].deleted == 0 && logp->dbentry[ndx].dbp == NULL) {
236 logp->dbentry[ndx].dbp = dbp;
237 logp->dbentry[ndx].refcount = 1;
238 logp->dbentry[ndx].deleted = dbp == NULL;
239 } else
240 logp->dbentry[ndx].refcount++;
242 err: UNLOCK_LOGTHREAD(logp);
243 return (ret);
248 * __db_fileid_to_db --
249 * Return the DB corresponding to the specified fileid.
251 * PUBLIC: int __db_fileid_to_db __P((DB_LOG *, DB **, u_int32_t));
254 __db_fileid_to_db(logp, dbpp, ndx)
255 DB_LOG *logp;
256 DB **dbpp;
257 u_int32_t ndx;
259 int ret;
261 ret = 0;
262 LOCK_LOGTHREAD(logp);
265 * Return DB_DELETED if the file has been deleted
266 * (it's not an error).
268 if (logp->dbentry[ndx].deleted) {
269 ret = DB_DELETED;
270 goto err;
274 * Otherwise return 0, but if we don't have a corresponding DB,
275 * it's an error.
277 if ((*dbpp = logp->dbentry[ndx].dbp) == NULL)
278 ret = ENOENT;
280 err: UNLOCK_LOGTHREAD(logp);
281 return (ret);
285 * Close files that were opened by the recovery daemon.
287 * PUBLIC: void __log_close_files __P((DB_LOG *));
289 void
290 __log_close_files(logp)
291 DB_LOG *logp;
293 u_int32_t i;
295 LOCK_LOGTHREAD(logp);
296 for (i = 0; i < logp->dbentry_cnt; i++)
297 if (logp->dbentry[i].dbp)
298 logp->dbentry[i].dbp->close(logp->dbentry[i].dbp, 0);
299 UNLOCK_LOGTHREAD(logp);
303 * PUBLIC: void __log_rem_logid __P((DB_LOG *, u_int32_t));
305 void
306 __log_rem_logid(logp, ndx)
307 DB_LOG *logp;
308 u_int32_t ndx;
310 LOCK_LOGTHREAD(logp);
311 if (--logp->dbentry[ndx].refcount == 0) {
312 logp->dbentry[ndx].dbp = NULL;
313 logp->dbentry[ndx].deleted = 0;
315 UNLOCK_LOGTHREAD(logp);