Update.
[glibc.git] / db2 / log / log_findckp.c
blobab13c8380eed3ee13bbe96853daa32b951a9b360
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)log_findckp.c 10.17 (Sleepycat) 9/17/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
17 #include <errno.h>
18 #include <string.h>
19 #endif
21 #include "db_int.h"
22 #include "shqueue.h"
23 #include "log.h"
24 #include "txn.h"
25 #include "common_ext.h"
28 * __log_findckp --
30 * Looks for the most recent checkpoint that occurs before the most recent
31 * checkpoint LSN, subject to the constraint that there must be at least two
32 * checkpoints. The reason you need two checkpoints is that you might have
33 * crashed during the most recent one and may not have a copy of all the
34 * open files. This is the point from which recovery can start and the
35 * point up to which archival/truncation can take place. Checkpoints in
36 * the log look like:
38 * -------------------------------------------------------------------
39 * | ckp A, ckplsn 100 | .... record .... | ckp B, ckplsn 600 | ...
40 * -------------------------------------------------------------------
41 * LSN 500 LSN 1000
43 * If we read what log returns from using the DB_CKP parameter to logput,
44 * we'll get the record at LSN 1000. The checkpoint LSN there is 600.
45 * Now we have to scan backwards looking for a checkpoint before LSN 600.
46 * We find one at 500. This means that we can truncate the log before
47 * 500 or run recovery beginning at 500.
49 * Returns 0 if we find a suitable checkpoint or we retrieved the
50 * first record in the log from which to start.
51 * Returns DB_NOTFOUND if there are no log records.
52 * Returns errno on error.
54 * PUBLIC: int __log_findckp __P((DB_LOG *, DB_LSN *));
56 int
57 __log_findckp(lp, lsnp)
58 DB_LOG *lp;
59 DB_LSN *lsnp;
61 DBT data;
62 DB_LSN ckp_lsn, final_ckp, last_ckp, next_lsn;
63 __txn_ckp_args *ckp_args;
64 int ret, verbose;
66 verbose = lp->dbenv != NULL && lp->dbenv->db_verbose != 0;
69 * Need to find the appropriate point from which to begin
70 * recovery.
72 memset(&data, 0, sizeof(data));
73 if (F_ISSET(lp, DB_AM_THREAD))
74 F_SET(&data, DB_DBT_MALLOC);
75 ZERO_LSN(ckp_lsn);
76 if ((ret = log_get(lp, &last_ckp, &data, DB_CHECKPOINT)) != 0) {
77 if (ret == ENOENT)
78 goto get_first;
79 else
80 return (ret);
83 final_ckp = last_ckp;
84 next_lsn = last_ckp;
85 do {
86 if (F_ISSET(lp, DB_AM_THREAD))
87 __os_free(data.data, data.size);
89 if ((ret = log_get(lp, &next_lsn, &data, DB_SET)) != 0)
90 return (ret);
91 if ((ret = __txn_ckp_read(data.data, &ckp_args)) != 0) {
92 if (F_ISSET(lp, DB_AM_THREAD))
93 __os_free(data.data, data.size);
94 return (ret);
96 if (IS_ZERO_LSN(ckp_lsn))
97 ckp_lsn = ckp_args->ckp_lsn;
98 if (verbose) {
99 __db_err(lp->dbenv, "Checkpoint at: [%lu][%lu]",
100 (u_long)last_ckp.file, (u_long)last_ckp.offset);
101 __db_err(lp->dbenv, "Checkpoint LSN: [%lu][%lu]",
102 (u_long)ckp_args->ckp_lsn.file,
103 (u_long)ckp_args->ckp_lsn.offset);
104 __db_err(lp->dbenv, "Previous checkpoint: [%lu][%lu]",
105 (u_long)ckp_args->last_ckp.file,
106 (u_long)ckp_args->last_ckp.offset);
108 last_ckp = next_lsn;
109 next_lsn = ckp_args->last_ckp;
110 __os_free(ckp_args, sizeof(*ckp_args));
113 * Keep looping until either you 1) run out of checkpoints,
114 * 2) you've found a checkpoint before the most recent
115 * checkpoint's LSN and you have at least 2 checkpoints.
117 } while (!IS_ZERO_LSN(next_lsn) &&
118 (log_compare(&last_ckp, &ckp_lsn) > 0 ||
119 log_compare(&final_ckp, &last_ckp) == 0));
121 if (F_ISSET(lp, DB_AM_THREAD))
122 __os_free(data.data, data.size);
125 * At this point, either, next_lsn is ZERO or ckp_lsn is the
126 * checkpoint lsn and last_ckp is the LSN of the last checkpoint
127 * before ckp_lsn. If the compare in the loop is still true, then
128 * next_lsn must be 0 and we need to roll forward from the
129 * beginning of the log.
131 if (log_compare(&last_ckp, &ckp_lsn) > 0 ||
132 log_compare(&final_ckp, &last_ckp) == 0) {
133 get_first: if ((ret = log_get(lp, &last_ckp, &data, DB_FIRST)) != 0)
134 return (ret);
135 if (F_ISSET(lp, DB_AM_THREAD))
136 __os_free(data.data, data.size);
138 *lsnp = last_ckp;
140 return (IS_ZERO_LSN(last_ckp) ? DB_NOTFOUND : 0);