2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid[] = "@(#)log.c 10.25 (Sleepycat) 8/27/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
28 #include "db_dispatch.h"
30 #include "common_ext.h"
32 static int __log_recover __P((DB_ENV *, DB_LOG *));
36 * Initialize and/or join a log.
39 log_open(path, flags, mode, dbenv, lpp)
49 int fd, newregion, ret, retry_cnt;
51 /* Validate arguments. */
53 #define OKFLAGS (DB_CREATE | DB_THREAD)
55 #define OKFLAGS (DB_CREATE)
57 if ((ret = __db_fchk(dbenv, "log_open", flags, OKFLAGS)) != 0)
61 * We store 4-byte offsets into the file, so the maximum file
62 * size can't be larger than that.
64 if (dbenv != NULL && dbenv->lg_max > UINT32_T_MAX) {
65 __db_err(dbenv, "log_open: maximum file size too large");
69 /* Create and initialize the DB_LOG structure. */
70 if ((dblp = (DB_LOG *)calloc(1, sizeof(DB_LOG))) == NULL)
75 ZERO_LSN(dblp->c_lsn);
77 if (LF_ISSET(DB_THREAD)) {
78 F_SET(dblp, DB_AM_THREAD);
79 (void)__db_mutex_init(&dblp->mutex, -1);
83 * The log region isn't fixed size because we store the registered
84 * file names there. Make it fairly large so that we don't have to
89 /* Map in the region. */
90 retry_cnt = newregion = 0;
91 retry: if (LF_ISSET(DB_CREATE)) {
92 ret = __db_rcreate(dbenv, DB_APP_LOG, path,
93 DB_DEFAULT_LOG_FILE, mode, len, &fd, &dblp->maddr);
95 /* Put the LOG structure first in the region. */
98 /* Initialize the rest of the region as free space. */
99 dblp->addr = (u_int8_t *)dblp->maddr + sizeof(LOG);
100 __db_shalloc_init(dblp->addr, len - sizeof(LOG));
102 /* Initialize the LOG structure. */
103 lp->persist.lg_max = dbenv == NULL ? 0 : dbenv->lg_max;
104 if (lp->persist.lg_max == 0)
105 lp->persist.lg_max = DEFAULT_MAX;
106 lp->persist.magic = DB_LOGMAGIC;
107 lp->persist.version = DB_LOGVERSION;
108 lp->persist.mode = mode;
109 SH_TAILQ_INIT(&lp->fq);
111 /* Initialize LOG LSNs. */
116 } else if (ret != EEXIST)
120 /* If we didn't or couldn't create the region, try and join it. */
122 (ret = __db_ropen(dbenv, DB_APP_LOG,
123 path, DB_DEFAULT_LOG_FILE, 0, &fd, &dblp->maddr)) != 0) {
125 * If we fail because the file isn't available, wait a
126 * second and try again.
128 if (ret == EAGAIN && ++retry_cnt < 3) {
129 (void)__db_sleep(1, 0);
135 /* Set up the common information. */
136 dblp->lp = dblp->maddr;
137 dblp->addr = (u_int8_t *)dblp->maddr + sizeof(LOG);
141 * If doing recovery, try and recover any previous log files
142 * before releasing the lock.
145 if ((ret = __log_recover(dbenv, dblp)) != 0) {
146 log_unlink(path, 1, dbenv);
149 UNLOCK_LOGREGION(dblp);
160 __log_recover(dbenv, dblp)
168 int cnt, found_checkpoint, ret;
173 * Find a log file. If none exist, we simply return, leaving
174 * everything initialized to a new log.
176 if ((ret = __log_find(dbenv, lp, &cnt)) != 0)
181 /* We have a log file name, find the last one. */
182 while (cnt < MAXLFNAME)
183 if (__log_valid(dbenv, lp, ++cnt) != 0) {
189 * We have the last useful log file and we've loaded any persistent
190 * information. Pretend that the log is larger than it can possibly
191 * be, and read this file, looking for a checkpoint and its end.
193 dblp->c_lsn.file = cnt;
194 dblp->c_lsn.offset = 0;
196 lp->lsn.file = cnt + 1;
199 /* Set the cursor. Shouldn't fail, leave error messages on. */
200 memset(&dbt, 0, sizeof(dbt));
201 if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
205 * Read to the end of the file, saving checkpoints. This will fail
206 * at some point, so turn off error messages.
208 found_checkpoint = 0;
209 while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 1) == 0) {
210 if (dbt.size < sizeof(u_int32_t))
212 memcpy(&chk, dbt.data, sizeof(u_int32_t));
213 if (chk == DB_txn_ckp) {
215 found_checkpoint = 1;
220 * We know where the end of the log is. Since that record is on disk,
221 * it's also the last-synced LSN.
224 lp->lsn.offset += dblp->c_len;
227 /* Set up the current buffer information, too. */
228 lp->len = dblp->c_len;
230 lp->w_off = lp->lsn.offset;
233 * It's possible that we didn't find a checkpoint because there wasn't
234 * one in the last log file. Start searching.
236 while (!found_checkpoint && cnt > 1) {
237 dblp->c_lsn.file = --cnt;
238 dblp->c_lsn.offset = 0;
241 /* Set the cursor. Shouldn't fail, leave error messages on. */
242 if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
246 * Read to the end of the file, saving checkpoints. Shouldn't
247 * fail, leave error messages on.
249 while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 0) == 0) {
250 if (dbt.size < sizeof(u_int32_t))
252 memcpy(&chk, dbt.data, sizeof(u_int32_t));
253 if (chk == DB_txn_ckp) {
255 found_checkpoint = 1;
260 /* If we never find a checkpoint, that's okay, just 0 it out. */
261 if (!found_checkpoint) {
263 lp->c_lsn.offset = 0;
267 "Recovering the log: last valid LSN: file: %lu offset %lu",
268 (u_long)lp->lsn.file, (u_long)lp->lsn.offset);
270 /* Reset the cursor. */
271 ZERO_LSN(dblp->c_lsn);
278 * Try to find a log file.
280 * PUBLIC: int __log_find __P((DB_ENV *, LOG *, int *));
283 __log_find(dbenv, lp, valp)
288 int cnt, fcnt, logval, ret;
290 char **names, *p, *q;
292 /* Find the directory name. */
293 if ((ret = __log_name(dbenv, 1, &p)) != 0)
295 if ((q = __db_rpath(p)) == NULL)
302 /* Get the list of file names. */
303 ret = __db_dir(dbenv, dir, &names, &fcnt);
309 * Search for a valid log file name, return a value of 0 on
313 for (cnt = fcnt, logval = 0; --cnt >= 0;)
314 if (strncmp(names[cnt], "log.", sizeof("log.") - 1) == 0) {
315 logval = atoi(names[cnt] + 4);
317 __log_valid(dbenv, lp, logval) == 0) {
323 /* Discard the list. */
324 __db_dirf(dbenv, names, fcnt);
331 * Validate a log file.
333 * PUBLIC: int __log_valid __P((DB_ENV *, LOG *, int));
336 __log_valid(dbenv, lp, cnt)
346 if ((ret = __log_name(dbenv, cnt, &p)) != 0)
350 if ((ret = __db_fdopen(p,
351 DB_RDONLY | DB_SEQUENTIAL,
352 DB_RDONLY | DB_SEQUENTIAL, 0, &fd)) != 0 ||
353 (ret = __db_lseek(fd, 0, 0, sizeof(HDR), SEEK_SET)) != 0 ||
354 (ret = __db_read(fd, &persist, sizeof(LOGP), &nw)) != 0 ||
355 nw != sizeof(LOGP)) {
359 (void)__db_close(fd);
361 "Ignoring log file: %s: %s", p, strerror(ret));
365 (void)__db_close(fd);
367 if (persist.magic != DB_LOGMAGIC) {
369 "Ignoring log file: %s: magic number %lx, not %lx",
370 p, (u_long)persist.magic, (u_long)DB_LOGMAGIC);
374 if (persist.version < DB_LOGOLDVER || persist.version > DB_LOGVERSION) {
376 "Ignoring log file: %s: unsupported log version %lu",
377 p, (u_long)persist.version);
383 lp->persist.lg_max = persist.lg_max;
384 lp->persist.mode = persist.mode;
404 /* Close the region. */
406 __db_rclose(dblp->dbenv, dblp->fd, dblp->maddr)) != 0 && ret == 0)
409 /* Close open files, release allocated memory. */
410 if (dblp->lfd != -1 && (t_ret = __db_close(dblp->lfd)) != 0 && ret == 0)
412 if (dblp->c_dbt.data != NULL)
413 FREE(dblp->c_dbt.data, dblp->c_dbt.ulen);
414 if (dblp->c_fd != -1 &&
415 (t_ret = __db_close(dblp->c_fd)) != 0 && ret == 0)
418 /* Free the structure. */
419 if (dblp->dbentry != NULL)
420 FREE(dblp->dbentry, (dblp->dbentry_cnt * sizeof(DB_ENTRY)));
421 FREE(dblp, sizeof(DB_LOG));
431 log_unlink(path, force, dbenv)
436 return (__db_runlink(dbenv,
437 DB_APP_LOG, path, DB_DEFAULT_LOG_FILE, force));