2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
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
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
43 static const char sccsid[] = "@(#)log_rec.c 10.11 (Sleepycat) 8/20/97";
46 #ifndef NO_SYSTEM_INCLUDES
47 #include <sys/types.h>
59 #include "db_dispatch.h"
60 #include "common_ext.h"
62 static int __log_open_file __P((DB_LOG *,
63 u_int8_t *, char *, DBTYPE, u_int32_t));
66 * PUBLIC: int __log_register_recover
67 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
70 __log_register_recover(logp, dbtp, lsnp, redo, info)
77 __log_register_args *argp;
81 __log_register_print(logp, dbtp, lsnp, redo, info);
83 info = info; /* XXX: Shut the compiler up. */
86 F_SET(logp, DB_AM_RECOVER);
88 if ((ret = __log_register_read(dbtp->data, &argp)) != 0)
91 ret = __log_open_file(logp,
92 argp->uid.data, argp->name.data, argp->ftype, argp->id);
94 if (redo == TXN_OPENFILES)
96 "warning: file %s not found", argp->name.data);
100 out: F_CLR(logp, DB_AM_RECOVER);
107 * PUBLIC: int __log_unregister_recover
108 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
111 __log_unregister_recover(logp, dbtp, lsnp, redo, info)
118 __log_unregister_args *argp;
122 __log_unregister_print(logp, dbtp, lsnp, redo, info);
124 info = info; /* XXX: Shut the compiler up. */
127 if (redo == TXN_OPENFILES ||
128 redo == TXN_BACKWARD_ROLL || redo == TXN_UNDO)
131 F_SET(logp, DB_AM_RECOVER);
132 if ((ret = __log_unregister_read(dbtp->data, &argp)) != 0)
135 LOCK_LOGTHREAD(logp);
136 if (logp->dbentry[argp->id].dbp == NULL)
138 else if (--logp->dbentry[argp->id].refcount == 0) {
139 ret = logp->dbentry[argp->id].dbp->close(
140 logp->dbentry[argp->id].dbp, 0);
141 logp->dbentry[argp->id].dbp = NULL;
143 UNLOCK_LOGTHREAD(logp);
145 out: F_CLR(logp, DB_AM_RECOVER);
151 /* Hand coded routines. */
154 * Called during log_register recovery. Make sure that we have an
155 * entry in the dbentry table for this ndx.
156 * Returns 0 on success, non-zero on error.
159 __log_open_file(lp, uid, name, ftype, ndx)
170 if (ndx < lp->dbentry_cnt &&
171 (lp->dbentry[ndx].deleted == 1 || lp->dbentry[ndx].dbp != NULL)) {
172 lp->dbentry[ndx].refcount++;
174 UNLOCK_LOGTHREAD(lp);
177 UNLOCK_LOGTHREAD(lp);
179 /* Need to open file. */
181 if ((ret = db_open(name, ftype, 0, 0, lp->dbenv, NULL, &dbp)) == 0) {
183 * Verify that we are opening the same file that we were
184 * referring to when we wrote this log record.
186 if (memcmp(uid, dbp->lock.fileid, DB_FILE_ID_LEN) != 0) {
187 (void)dbp->close(dbp, 0);
193 if (ret == 0 || ret == ENOENT)
194 (void)__log_add_logid(lp, dbp, ndx);
200 * This function returns:
201 * 0 SUCCESS (the entry was not previously set and is now set or the
202 * entry was previously set and we just inced the ref count.
203 * >0 on system error (returns errno value).
204 * PUBLIC: int __log_add_logid __P((DB_LOG *, DB *, u_int32_t));
207 __log_add_logid(logp, dbp, ndx)
212 DB_ENTRY *temp_entryp;
218 LOCK_LOGTHREAD(logp);
220 * Check if we need to grow the table.
222 if (logp->dbentry_cnt <= ndx) {
223 if (logp->dbentry_cnt == 0) {
225 (DB_ENTRY *)malloc(DB_GROW_SIZE * sizeof(DB_ENTRY));
226 if (logp->dbentry == NULL) {
231 temp_entryp = (DB_ENTRY *)realloc(logp->dbentry,
232 (DB_GROW_SIZE + logp->dbentry_cnt) *
234 if (temp_entryp == NULL) {
238 logp->dbentry = temp_entryp;
241 /* Initialize the new entries. */
242 for (i = logp->dbentry_cnt;
243 i < logp->dbentry_cnt + DB_GROW_SIZE; i++) {
244 logp->dbentry[i].dbp = NULL;
245 logp->dbentry[i].deleted = 0;
248 logp->dbentry_cnt += DB_GROW_SIZE;
251 if (logp->dbentry[ndx].deleted == 0 && logp->dbentry[ndx].dbp == NULL) {
252 logp->dbentry[ndx].dbp = dbp;
253 logp->dbentry[ndx].refcount = 1;
254 logp->dbentry[ndx].deleted = dbp == NULL;
256 logp->dbentry[ndx].refcount++;
258 err: UNLOCK_LOGTHREAD(logp);
264 * __db_fileid_to_db --
265 * Return the DB corresponding to the specified fileid.
267 * PUBLIC: int __db_fileid_to_db __P((DB_LOG *, DB **, u_int32_t));
270 __db_fileid_to_db(logp, dbpp, ndx)
278 LOCK_LOGTHREAD(logp);
281 * Return DB_DELETED if the file has been deleted
282 * (it's not an error).
284 if (logp->dbentry[ndx].deleted) {
290 * Otherwise return 0, but if we don't have a corresponding DB,
293 if ((*dbpp = logp->dbentry[ndx].dbp) == NULL)
296 err: UNLOCK_LOGTHREAD(logp);
301 * Close files that were opened by the recovery daemon.
303 * PUBLIC: void __log_close_files __P((DB_LOG *));
306 __log_close_files(logp)
311 LOCK_LOGTHREAD(logp);
312 for (i = 0; i < logp->dbentry_cnt; i++)
313 if (logp->dbentry[i].dbp)
314 logp->dbentry[i].dbp->close(logp->dbentry[i].dbp, 0);
315 UNLOCK_LOGTHREAD(logp);
319 * PUBLIC: void __log_rem_logid __P((DB_LOG *, u_int32_t));
322 __log_rem_logid(logp, ndx)
326 LOCK_LOGTHREAD(logp);
327 if (--logp->dbentry[ndx].refcount == 0) {
328 logp->dbentry[ndx].dbp = NULL;
329 logp->dbentry[ndx].deleted = 0;
331 UNLOCK_LOGTHREAD(logp);