2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
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.20 (Sleepycat) 4/28/98";
46 #ifndef NO_SYSTEM_INCLUDES
47 #include <sys/types.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 *));
67 __log_register_recover(logp
, dbtp
, lsnp
, redo
, info
)
74 __log_register_args
*argp
;
78 __log_register_print(logp
, dbtp
, lsnp
, redo
, info
);
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)
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
98 ret
= __log_open_file(logp
,
99 argp
->uid
.data
, argp
->name
.data
, argp
->ftype
, argp
->id
);
101 if (redo
== TXN_OPENFILES
)
102 __db_err(logp
->dbenv
,
103 "warning: file %s not found",
107 } else if (argp
->opcode
!= LOG_CHECKPOINT
) {
109 * If we are redoing a close or undoing an open, then we need
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
)
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
);
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.
143 __log_open_file(lp
, uid
, name
, ftype
, ndx
)
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
);
161 UNLOCK_LOGTHREAD(lp
);
163 /* Need to open file. */
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);
177 if (ret
== 0 || ret
== ENOENT
)
178 (void)__log_add_logid(lp
, dbp
, ndx
);
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
)
196 DB_ENTRY
*temp_entryp
;
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
) {
215 temp_entryp
= (DB_ENTRY
*)__db_realloc(logp
->dbentry
,
216 (DB_GROW_SIZE
+ logp
->dbentry_cnt
) *
218 if (temp_entryp
== NULL
) {
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
;
240 logp
->dbentry
[ndx
].refcount
++;
242 err
: UNLOCK_LOGTHREAD(logp
);
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
)
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
) {
274 * Otherwise return 0, but if we don't have a corresponding DB,
277 if ((*dbpp
= logp
->dbentry
[ndx
].dbp
) == NULL
)
280 err
: UNLOCK_LOGTHREAD(logp
);
285 * Close files that were opened by the recovery daemon.
287 * PUBLIC: void __log_close_files __P((DB_LOG *));
290 __log_close_files(logp
)
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));
306 __log_rem_logid(logp
, 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
);