]> git.ipfire.org Git - thirdparty/glibc.git/blob - db2/log/log_rec.c
Update.
[thirdparty/glibc.git] / db2 / log / log_rec.c
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
7 /*
8 * Copyright (c) 1995, 1996
9 * The President and Fellows of Harvard University. All rights reserved.
10 *
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.
26 *
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.
38 */
39
40 #include "config.h"
41
42 #ifndef lint
43 static const char sccsid[] = "@(#)log_rec.c 10.16 (Sleepycat) 1/17/98";
44 #endif /* not lint */
45
46 #ifndef NO_SYSTEM_INCLUDES
47 #include <sys/types.h>
48
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stddef.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #endif
55
56 #include "db_int.h"
57 #include "shqueue.h"
58 #include "log.h"
59 #include "db_dispatch.h"
60 #include "common_ext.h"
61
62 static int __log_open_file __P((DB_LOG *,
63 u_int8_t *, char *, DBTYPE, u_int32_t));
64
65 /*
66 * PUBLIC: int __log_register_recover
67 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
68 */
69 int
70 __log_register_recover(logp, dbtp, lsnp, redo, info)
71 DB_LOG *logp;
72 DBT *dbtp;
73 DB_LSN *lsnp;
74 int redo;
75 void *info;
76 {
77 __log_register_args *argp;
78 int ret;
79
80 #ifdef DEBUG_RECOVER
81 __log_register_print(logp, dbtp, lsnp, redo, info);
82 #endif
83 COMPQUIET(info, NULL);
84 COMPQUIET(lsnp, NULL);
85
86 F_SET(logp, DB_AM_RECOVER);
87
88 if ((ret = __log_register_read(dbtp->data, &argp)) != 0)
89 goto out;
90
91 if ((argp->opcode == LOG_CHECKPOINT && redo == TXN_OPENFILES) ||
92 (argp->opcode == LOG_OPEN &&
93 (redo == TXN_REDO || redo == TXN_OPENFILES ||
94 redo == TXN_FORWARD_ROLL)) ||
95 (argp->opcode == LOG_CLOSE &&
96 (redo == TXN_UNDO || redo == TXN_BACKWARD_ROLL))) {
97 /*
98 * If we are redoing an open or undoing a close, then we need
99 * to open a file.
100 */
101 ret = __log_open_file(logp,
102 argp->uid.data, argp->name.data, argp->ftype, argp->id);
103 if (ret == ENOENT) {
104 if (redo == TXN_OPENFILES)
105 __db_err(logp->dbenv,
106 "warning: file %s not found",
107 argp->name.data);
108 ret = 0;
109 }
110 } else if (argp->opcode != LOG_CHECKPOINT) {
111 /*
112 * If we are redoing a close or undoing an open, then we need
113 * to close the file.
114 *
115 * If the file is deleted, then we can just ignore this close.
116 * Otherwise, we'd better have a valid dbp that we should either
117 * close or whose reference count should be decremented.
118 */
119 LOCK_LOGTHREAD(logp);
120 if (logp->dbentry[argp->id].dbp == NULL) {
121 if (!logp->dbentry[argp->id].deleted)
122 ret = EINVAL;
123 } else if (--logp->dbentry[argp->id].refcount == 0) {
124 ret = logp->dbentry[argp->id].dbp->close(
125 logp->dbentry[argp->id].dbp, 0);
126 logp->dbentry[argp->id].dbp = NULL;
127 }
128 UNLOCK_LOGTHREAD(logp);
129 }
130
131 out: F_CLR(logp, DB_AM_RECOVER);
132 if (argp != NULL)
133 __db_free(argp);
134 return (ret);
135 }
136
137 /* Hand coded routines. */
138
139 /*
140 * Called during log_register recovery. Make sure that we have an
141 * entry in the dbentry table for this ndx.
142 * Returns 0 on success, non-zero on error.
143 */
144 static int
145 __log_open_file(lp, uid, name, ftype, ndx)
146 DB_LOG *lp;
147 u_int8_t *uid;
148 char *name;
149 DBTYPE ftype;
150 u_int32_t ndx;
151 {
152 DB *dbp;
153 int ret;
154
155 LOCK_LOGTHREAD(lp);
156 if (ndx < lp->dbentry_cnt &&
157 (lp->dbentry[ndx].deleted == 1 || lp->dbentry[ndx].dbp != NULL)) {
158 lp->dbentry[ndx].refcount++;
159
160 UNLOCK_LOGTHREAD(lp);
161 return (0);
162 }
163 UNLOCK_LOGTHREAD(lp);
164
165 /* Need to open file. */
166 dbp = NULL;
167 if ((ret = db_open(name, ftype, 0, 0, lp->dbenv, NULL, &dbp)) == 0) {
168 /*
169 * Verify that we are opening the same file that we were
170 * referring to when we wrote this log record.
171 */
172 if (memcmp(uid, dbp->lock.fileid, DB_FILE_ID_LEN) != 0) {
173 (void)dbp->close(dbp, 0);
174 dbp = NULL;
175 ret = ENOENT;
176 }
177 }
178
179 if (ret == 0 || ret == ENOENT)
180 (void)__log_add_logid(lp, dbp, ndx);
181
182 return (ret);
183 }
184
185 /*
186 * This function returns:
187 * 0 SUCCESS (the entry was not previously set and is now set or the
188 * entry was previously set and we just inced the ref count.
189 * >0 on system error (returns errno value).
190 * PUBLIC: int __log_add_logid __P((DB_LOG *, DB *, u_int32_t));
191 */
192 int
193 __log_add_logid(logp, dbp, ndx)
194 DB_LOG *logp;
195 DB *dbp;
196 u_int32_t ndx;
197 {
198 DB_ENTRY *temp_entryp;
199 u_int32_t i;
200 int ret;
201
202 ret = 0;
203
204 LOCK_LOGTHREAD(logp);
205 /*
206 * Check if we need to grow the table.
207 */
208 if (logp->dbentry_cnt <= ndx) {
209 if (logp->dbentry_cnt == 0) {
210 logp->dbentry = (DB_ENTRY *)
211 __db_malloc(DB_GROW_SIZE * sizeof(DB_ENTRY));
212 if (logp->dbentry == NULL) {
213 ret = ENOMEM;
214 goto err;
215 }
216 } else {
217 temp_entryp = (DB_ENTRY *)__db_realloc(logp->dbentry,
218 (DB_GROW_SIZE + logp->dbentry_cnt) *
219 sizeof(DB_ENTRY));
220 if (temp_entryp == NULL) {
221 ret = ENOMEM;
222 goto err;
223 }
224 logp->dbentry = temp_entryp;
225
226 }
227 /* Initialize the new entries. */
228 for (i = logp->dbentry_cnt;
229 i < logp->dbentry_cnt + DB_GROW_SIZE; i++) {
230 logp->dbentry[i].dbp = NULL;
231 logp->dbentry[i].deleted = 0;
232 }
233
234 logp->dbentry_cnt += DB_GROW_SIZE;
235 }
236
237 if (logp->dbentry[ndx].deleted == 0 && logp->dbentry[ndx].dbp == NULL) {
238 logp->dbentry[ndx].dbp = dbp;
239 logp->dbentry[ndx].refcount = 1;
240 logp->dbentry[ndx].deleted = dbp == NULL;
241 } else
242 logp->dbentry[ndx].refcount++;
243
244 err: UNLOCK_LOGTHREAD(logp);
245 return (ret);
246 }
247
248
249 /*
250 * __db_fileid_to_db --
251 * Return the DB corresponding to the specified fileid.
252 *
253 * PUBLIC: int __db_fileid_to_db __P((DB_LOG *, DB **, u_int32_t));
254 */
255 int
256 __db_fileid_to_db(logp, dbpp, ndx)
257 DB_LOG *logp;
258 DB **dbpp;
259 u_int32_t ndx;
260 {
261 int ret;
262
263 ret = 0;
264 LOCK_LOGTHREAD(logp);
265
266 /*
267 * Return DB_DELETED if the file has been deleted
268 * (it's not an error).
269 */
270 if (logp->dbentry[ndx].deleted) {
271 ret = DB_DELETED;
272 goto err;
273 }
274
275 /*
276 * Otherwise return 0, but if we don't have a corresponding DB,
277 * it's an error.
278 */
279 if ((*dbpp = logp->dbentry[ndx].dbp) == NULL)
280 ret = ENOENT;
281
282 err: UNLOCK_LOGTHREAD(logp);
283 return (ret);
284 }
285
286 /*
287 * Close files that were opened by the recovery daemon.
288 *
289 * PUBLIC: void __log_close_files __P((DB_LOG *));
290 */
291 void
292 __log_close_files(logp)
293 DB_LOG *logp;
294 {
295 u_int32_t i;
296
297 LOCK_LOGTHREAD(logp);
298 for (i = 0; i < logp->dbentry_cnt; i++)
299 if (logp->dbentry[i].dbp)
300 logp->dbentry[i].dbp->close(logp->dbentry[i].dbp, 0);
301 UNLOCK_LOGTHREAD(logp);
302 }
303
304 /*
305 * PUBLIC: void __log_rem_logid __P((DB_LOG *, u_int32_t));
306 */
307 void
308 __log_rem_logid(logp, ndx)
309 DB_LOG *logp;
310 u_int32_t ndx;
311 {
312 LOCK_LOGTHREAD(logp);
313 if (--logp->dbentry[ndx].refcount == 0) {
314 logp->dbentry[ndx].dbp = NULL;
315 logp->dbentry[ndx].deleted = 0;
316 }
317 UNLOCK_LOGTHREAD(logp);
318 }